2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / ada / sem_eval.adb
blobebfa49243c3ef8b4e39060c4f45d651311c103e1
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-2015, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Util; use Exp_Util;
34 with Freeze; use Freeze;
35 with Lib; use Lib;
36 with Namet; use Namet;
37 with Nmake; use Nmake;
38 with Nlists; use Nlists;
39 with Opt; use Opt;
40 with Par_SCO; use Par_SCO;
41 with Rtsfind; use Rtsfind;
42 with Sem; use Sem;
43 with Sem_Aux; use Sem_Aux;
44 with Sem_Cat; use Sem_Cat;
45 with Sem_Ch6; use Sem_Ch6;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Res; use Sem_Res;
48 with Sem_Util; use Sem_Util;
49 with Sem_Type; use Sem_Type;
50 with Sem_Warn; use Sem_Warn;
51 with Sinfo; use Sinfo;
52 with Snames; use Snames;
53 with Stand; use Stand;
54 with Stringt; use Stringt;
55 with Targparm; use Targparm;
56 with Tbuild; use Tbuild;
58 package body Sem_Eval is
60 -----------------------------------------
61 -- Handling of Compile Time Evaluation --
62 -----------------------------------------
64 -- The compile time evaluation of expressions is distributed over several
65 -- Eval_xxx procedures. These procedures are called immediately after
66 -- a subexpression is resolved and is therefore accomplished in a bottom
67 -- up fashion. The flags are synthesized using the following approach.
69 -- Is_Static_Expression is determined by following the detailed rules
70 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
71 -- flag of the operands in many cases.
73 -- Raises_Constraint_Error is set if any of the operands have the flag
74 -- set or if an attempt to compute the value of the current expression
75 -- results in detection of a runtime constraint error.
77 -- As described in the spec, the requirement is that Is_Static_Expression
78 -- be accurately set, and in addition for nodes for which this flag is set,
79 -- Raises_Constraint_Error must also be set. Furthermore a node which has
80 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
81 -- requirement is that the expression value must be precomputed, and the
82 -- node is either a literal, or the name of a constant entity whose value
83 -- is a static expression.
85 -- The general approach is as follows. First compute Is_Static_Expression.
86 -- If the node is not static, then the flag is left off in the node and
87 -- we are all done. Otherwise for a static node, we test if any of the
88 -- operands will raise constraint error, and if so, propagate the flag
89 -- Raises_Constraint_Error to the result node and we are done (since the
90 -- error was already posted at a lower level).
92 -- For the case of a static node whose operands do not raise constraint
93 -- error, we attempt to evaluate the node. If this evaluation succeeds,
94 -- then the node is replaced by the result of this computation. If the
95 -- evaluation raises constraint error, then we rewrite the node with
96 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
97 -- to post appropriate error messages.
99 ----------------
100 -- Local Data --
101 ----------------
103 type Bits is array (Nat range <>) of Boolean;
104 -- Used to convert unsigned (modular) values for folding logical ops
106 -- The following declarations are used to maintain a cache of nodes that
107 -- have compile time known values. The cache is maintained only for
108 -- discrete types (the most common case), and is populated by calls to
109 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
110 -- since it is possible for the status to change (in particular it is
111 -- possible for a node to get replaced by a constraint error node).
113 CV_Bits : constant := 5;
114 -- Number of low order bits of Node_Id value used to reference entries
115 -- in the cache table.
117 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
118 -- Size of cache for compile time values
120 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
122 type CV_Entry is record
123 N : Node_Id;
124 V : Uint;
125 end record;
127 type Match_Result is (Match, No_Match, Non_Static);
128 -- Result returned from functions that test for a matching result. If the
129 -- operands are not OK_Static then Non_Static will be returned. Otherwise
130 -- Match/No_Match is returned depending on whether the match succeeds.
132 type CV_Cache_Array is array (CV_Range) of CV_Entry;
134 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
135 -- This is the actual cache, with entries consisting of node/value pairs,
136 -- and the impossible value Node_High_Bound used for unset entries.
138 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
139 -- Range membership may either be statically known to be in range or out
140 -- of range, or not statically known. Used for Test_In_Range below.
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 function Choice_Matches
147 (Expr : Node_Id;
148 Choice : Node_Id) return Match_Result;
149 -- Determines whether given value Expr matches the given Choice. The Expr
150 -- can be of discrete, real, or string type and must be a compile time
151 -- known value (it is an error to make the call if these conditions are
152 -- not met). The choice can be a range, subtype name, subtype indication,
153 -- or expression. The returned result is Non_Static if Choice is not
154 -- OK_Static, otherwise either Match or No_Match is returned depending
155 -- on whether Choice matches Expr. This is used for case expression
156 -- alternatives, and also for membership tests. In each case, more
157 -- possibilities are tested than the syntax allows (e.g. membership allows
158 -- subtype indications and non-discrete types, and case allows an OTHERS
159 -- choice), but it does not matter, since we have already done a full
160 -- semantic and syntax check of the construct, so the extra possibilities
161 -- just will not arise for correct expressions.
163 -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g
164 -- a reference to a type, one of whose bounds raises Constraint_Error, then
165 -- it also sets the Raises_Constraint_Error flag on the Choice itself.
167 function Choices_Match
168 (Expr : Node_Id;
169 Choices : List_Id) return Match_Result;
170 -- This function applies Choice_Matches to each element of Choices. If the
171 -- result is No_Match, then it continues and checks the next element. If
172 -- the result is Match or Non_Static, this result is immediately given
173 -- as the result without checking the rest of the list. Expr can be of
174 -- discrete, real, or string type and must be a compile time known value
175 -- (it is an error to make the call if these conditions are not met).
177 function From_Bits (B : Bits; T : Entity_Id) return Uint;
178 -- Converts a bit string of length B'Length to a Uint value to be used for
179 -- a target of type T, which is a modular type. This procedure includes the
180 -- necessary reduction by the modulus in the case of a nonbinary modulus
181 -- (for a binary modulus, the bit string is the right length any way so all
182 -- is well).
184 function Is_Static_Choice (Choice : Node_Id) return Boolean;
185 -- Given a choice (from a case expression or membership test), returns
186 -- True if the choice is static. No test is made for raising of constraint
187 -- error, so this function is used only for legality tests.
189 function Is_Static_Choice_List (Choices : List_Id) return Boolean;
190 -- Given a choice list (from a case expression or membership test), return
191 -- True if all choices are static in the sense of Is_Static_Choice.
193 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean;
194 -- Given a choice (from a case expression or membership test), returns
195 -- True if the choice is static and does not raise a Constraint_Error.
197 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean;
198 -- Given a choice list (from a case expression or membership test), return
199 -- True if all choices are static in the sense of Is_OK_Static_Choice.
201 function Is_Static_Range (N : Node_Id) return Boolean;
202 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
203 -- argument is an N_Range node (but note that the semantic analysis of
204 -- equivalent range attribute references already turned them into the
205 -- equivalent range). This differs from Is_OK_Static_Range (which is what
206 -- must be used by clients) in that it does not care whether the bounds
207 -- raise Constraint_Error or not. Used for checking whether expressions are
208 -- static in the 4.9 sense (without worrying about exceptions).
210 function Get_String_Val (N : Node_Id) return Node_Id;
211 -- Given a tree node for a folded string or character value, returns the
212 -- corresponding string literal or character literal (one of the two must
213 -- be available, or the operand would not have been marked as foldable in
214 -- the earlier analysis of the operation).
216 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
217 -- Bits represents the number of bits in an integer value to be computed
218 -- (but the value has not been computed yet). If this value in Bits is
219 -- reasonable, a result of True is returned, with the implication that the
220 -- caller should go ahead and complete the calculation. If the value in
221 -- Bits is unreasonably large, then an error is posted on node N, and
222 -- False is returned (and the caller skips the proposed calculation).
224 procedure Out_Of_Range (N : Node_Id);
225 -- This procedure is called if it is determined that node N, which appears
226 -- in a non-static context, is a compile time known value which is outside
227 -- its range, i.e. the range of Etype. This is used in contexts where
228 -- this is an illegality if N is static, and should generate a warning
229 -- otherwise.
231 function Real_Or_String_Static_Predicate_Matches
232 (Val : Node_Id;
233 Typ : Entity_Id) return Boolean;
234 -- This is the function used to evaluate real or string static predicates.
235 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
236 -- represents the value to be tested against the predicate. Typ is the
237 -- type with the predicate, from which the predicate expression can be
238 -- extracted. The result returned is True if the given value satisfies
239 -- the predicate.
241 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
242 -- N and Exp are nodes representing an expression, Exp is known to raise
243 -- CE. N is rewritten in term of Exp in the optimal way.
245 function String_Type_Len (Stype : Entity_Id) return Uint;
246 -- Given a string type, determines the length of the index type, or, if
247 -- this index type is non-static, the length of the base type of this index
248 -- type. Note that if the string type is itself static, then the index type
249 -- is static, so the second case applies only if the string type passed is
250 -- non-static.
252 function Test (Cond : Boolean) return Uint;
253 pragma Inline (Test);
254 -- This function simply returns the appropriate Boolean'Pos value
255 -- corresponding to the value of Cond as a universal integer. It is
256 -- used for producing the result of the static evaluation of the
257 -- logical operators
259 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
260 -- Check whether an arithmetic operation with universal operands which is a
261 -- rewritten function call with an explicit scope indication is ambiguous:
262 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
263 -- type declared in P and the context does not impose a type on the result
264 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
265 -- error and return Empty, else return the result type of the operator.
267 procedure Test_Expression_Is_Foldable
268 (N : Node_Id;
269 Op1 : Node_Id;
270 Stat : out Boolean;
271 Fold : out Boolean);
272 -- Tests to see if expression N whose single operand is Op1 is foldable,
273 -- i.e. the operand value is known at compile time. If the operation is
274 -- foldable, then Fold is True on return, and Stat indicates whether the
275 -- result is static (i.e. the operand was static). Note that it is quite
276 -- possible for Fold to be True, and Stat to be False, since there are
277 -- cases in which we know the value of an operand even though it is not
278 -- technically static (e.g. the static lower bound of a range whose upper
279 -- bound is non-static).
281 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes
282 -- a call to Check_Non_Static_Context on the operand. If Fold is False on
283 -- return, then all processing is complete, and the caller should return,
284 -- since there is nothing else to do.
286 -- If Stat is set True on return, then Is_Static_Expression is also set
287 -- true in node N. There are some cases where this is over-enthusiastic,
288 -- e.g. in the two operand case below, for string comparison, the result is
289 -- not static even though the two operands are static. In such cases, the
290 -- caller must reset the Is_Static_Expression flag in N.
292 -- If Fold and Stat are both set to False then this routine performs also
293 -- the following extra actions:
295 -- If either operand is Any_Type then propagate it to result to prevent
296 -- cascaded errors.
298 -- If some operand raises constraint error, then replace the node N
299 -- with the raise constraint error node. This replacement inherits the
300 -- Is_Static_Expression flag from the operands.
302 procedure Test_Expression_Is_Foldable
303 (N : Node_Id;
304 Op1 : Node_Id;
305 Op2 : Node_Id;
306 Stat : out Boolean;
307 Fold : out Boolean;
308 CRT_Safe : Boolean := False);
309 -- Same processing, except applies to an expression N with two operands
310 -- Op1 and Op2. The result is static only if both operands are static. If
311 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
312 -- for the tests that the two operands are known at compile time. See
313 -- spec of this routine for further details.
315 function Test_In_Range
316 (N : Node_Id;
317 Typ : Entity_Id;
318 Assume_Valid : Boolean;
319 Fixed_Int : Boolean;
320 Int_Real : Boolean) return Range_Membership;
321 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
322 -- or Out_Of_Range if it can be guaranteed at compile time that expression
323 -- N is known to be in or out of range of the subtype Typ. If not compile
324 -- time known, Unknown is returned. See documentation of Is_In_Range for
325 -- complete description of parameters.
327 procedure To_Bits (U : Uint; B : out Bits);
328 -- Converts a Uint value to a bit string of length B'Length
330 -----------------------------------------------
331 -- Check_Expression_Against_Static_Predicate --
332 -----------------------------------------------
334 procedure Check_Expression_Against_Static_Predicate
335 (Expr : Node_Id;
336 Typ : Entity_Id)
338 begin
339 -- Nothing to do if expression is not known at compile time, or the
340 -- type has no static predicate set (will be the case for all non-scalar
341 -- types, so no need to make a special test for that).
343 if not (Has_Static_Predicate (Typ)
344 and then Compile_Time_Known_Value (Expr))
345 then
346 return;
347 end if;
349 -- Here we have a static predicate (note that it could have arisen from
350 -- an explicitly specified Dynamic_Predicate whose expression met the
351 -- rules for being predicate-static).
353 -- Case of real static predicate
355 if Is_Real_Type (Typ) then
356 if Real_Or_String_Static_Predicate_Matches
357 (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)),
358 Typ => Typ)
359 then
360 return;
361 end if;
363 -- Case of string static predicate
365 elsif Is_String_Type (Typ) then
366 if Real_Or_String_Static_Predicate_Matches
367 (Val => Expr_Value_S (Expr), Typ => Typ)
368 then
369 return;
370 end if;
372 -- Case of discrete static predicate
374 else
375 pragma Assert (Is_Discrete_Type (Typ));
377 -- If static predicate matches, nothing to do
379 if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then
380 return;
381 end if;
382 end if;
384 -- Here we know that the predicate will fail
386 -- Special case of static expression failing a predicate (other than one
387 -- that was explicitly specified with a Dynamic_Predicate aspect). This
388 -- is the case where the expression is no longer considered static.
390 if Is_Static_Expression (Expr)
391 and then not Has_Dynamic_Predicate_Aspect (Typ)
392 then
393 Error_Msg_NE
394 ("??static expression fails static predicate check on &",
395 Expr, Typ);
396 Error_Msg_N
397 ("\??expression is no longer considered static", Expr);
398 Set_Is_Static_Expression (Expr, False);
400 -- In all other cases, this is just a warning that a test will fail.
401 -- It does not matter if the expression is static or not, or if the
402 -- predicate comes from a dynamic predicate aspect or not.
404 else
405 Error_Msg_NE
406 ("??expression fails predicate check on &", Expr, Typ);
407 end if;
408 end Check_Expression_Against_Static_Predicate;
410 ------------------------------
411 -- Check_Non_Static_Context --
412 ------------------------------
414 procedure Check_Non_Static_Context (N : Node_Id) is
415 T : constant Entity_Id := Etype (N);
416 Checks_On : constant Boolean :=
417 not Index_Checks_Suppressed (T)
418 and not Range_Checks_Suppressed (T);
420 begin
421 -- Ignore cases of non-scalar types, error types, or universal real
422 -- types that have no usable bounds.
424 if T = Any_Type
425 or else not Is_Scalar_Type (T)
426 or else T = Universal_Fixed
427 or else T = Universal_Real
428 then
429 return;
430 end if;
432 -- At this stage we have a scalar type. If we have an expression that
433 -- raises CE, then we already issued a warning or error msg so there is
434 -- nothing more to be done in this routine.
436 if Raises_Constraint_Error (N) then
437 return;
438 end if;
440 -- Now we have a scalar type which is not marked as raising a constraint
441 -- error exception. The main purpose of this routine is to deal with
442 -- static expressions appearing in a non-static context. That means
443 -- that if we do not have a static expression then there is not much
444 -- to do. The one case that we deal with here is that if we have a
445 -- floating-point value that is out of range, then we post a warning
446 -- that an infinity will result.
448 if not Is_Static_Expression (N) then
449 if Is_Floating_Point_Type (T)
450 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
451 then
452 Error_Msg_N
453 ("??float value out of range, infinity will be generated", N);
454 end if;
456 return;
457 end if;
459 -- Here we have the case of outer level static expression of scalar
460 -- type, where the processing of this procedure is needed.
462 -- For real types, this is where we convert the value to a machine
463 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
464 -- need to do this if the parent is a constant declaration, since in
465 -- other cases, gigi should do the necessary conversion correctly, but
466 -- experimentation shows that this is not the case on all machines, in
467 -- particular if we do not convert all literals to machine values in
468 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
469 -- and SGI/Irix.
471 if Nkind (N) = N_Real_Literal
472 and then not Is_Machine_Number (N)
473 and then not Is_Generic_Type (Etype (N))
474 and then Etype (N) /= Universal_Real
475 then
476 -- Check that value is in bounds before converting to machine
477 -- number, so as not to lose case where value overflows in the
478 -- least significant bit or less. See B490001.
480 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
481 Out_Of_Range (N);
482 return;
483 end if;
485 -- Note: we have to copy the node, to avoid problems with conformance
486 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
488 Rewrite (N, New_Copy (N));
490 if not Is_Floating_Point_Type (T) then
491 Set_Realval
492 (N, Corresponding_Integer_Value (N) * Small_Value (T));
494 elsif not UR_Is_Zero (Realval (N)) then
496 -- Note: even though RM 4.9(38) specifies biased rounding, this
497 -- has been modified by AI-100 in order to prevent confusing
498 -- differences in rounding between static and non-static
499 -- expressions. AI-100 specifies that the effect of such rounding
500 -- is implementation dependent, and in GNAT we round to nearest
501 -- even to match the run-time behavior. Note that this applies
502 -- to floating point literals, not fixed points ones, even though
503 -- their compiler representation is also as a universal real.
505 Set_Realval
506 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
507 Set_Is_Machine_Number (N);
508 end if;
510 end if;
512 -- Check for out of range universal integer. This is a non-static
513 -- context, so the integer value must be in range of the runtime
514 -- representation of universal integers.
516 -- We do this only within an expression, because that is the only
517 -- case in which non-static universal integer values can occur, and
518 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
519 -- called in contexts like the expression of a number declaration where
520 -- we certainly want to allow out of range values.
522 if Etype (N) = Universal_Integer
523 and then Nkind (N) = N_Integer_Literal
524 and then Nkind (Parent (N)) in N_Subexpr
525 and then
526 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
527 or else
528 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
529 then
530 Apply_Compile_Time_Constraint_Error
531 (N, "non-static universal integer value out of range<<",
532 CE_Range_Check_Failed);
534 -- Check out of range of base type
536 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
537 Out_Of_Range (N);
539 -- Give warning if outside subtype (where one or both of the bounds of
540 -- the subtype is static). This warning is omitted if the expression
541 -- appears in a range that could be null (warnings are handled elsewhere
542 -- for this case).
544 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
545 if Is_In_Range (N, T, Assume_Valid => True) then
546 null;
548 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
549 Apply_Compile_Time_Constraint_Error
550 (N, "value not in range of}<<", CE_Range_Check_Failed);
552 elsif Checks_On then
553 Enable_Range_Check (N);
555 else
556 Set_Do_Range_Check (N, False);
557 end if;
558 end if;
559 end Check_Non_Static_Context;
561 ---------------------------------
562 -- Check_String_Literal_Length --
563 ---------------------------------
565 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
566 begin
567 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
568 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
569 then
570 Apply_Compile_Time_Constraint_Error
571 (N, "string length wrong for}??",
572 CE_Length_Check_Failed,
573 Ent => Ttype,
574 Typ => Ttype);
575 end if;
576 end if;
577 end Check_String_Literal_Length;
579 --------------------
580 -- Choice_Matches --
581 --------------------
583 function Choice_Matches
584 (Expr : Node_Id;
585 Choice : Node_Id) return Match_Result
587 Etyp : constant Entity_Id := Etype (Expr);
588 Val : Uint;
589 ValR : Ureal;
590 ValS : Node_Id;
592 begin
593 pragma Assert (Compile_Time_Known_Value (Expr));
594 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
596 if not Is_OK_Static_Choice (Choice) then
597 Set_Raises_Constraint_Error (Choice);
598 return Non_Static;
600 -- Discrete type case
602 elsif Is_Discrete_Type (Etype (Expr)) then
603 Val := Expr_Value (Expr);
605 if Nkind (Choice) = N_Range then
606 if Val >= Expr_Value (Low_Bound (Choice))
607 and then
608 Val <= Expr_Value (High_Bound (Choice))
609 then
610 return Match;
611 else
612 return No_Match;
613 end if;
615 elsif Nkind (Choice) = N_Subtype_Indication
616 or else
617 (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
618 then
619 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
620 and then
621 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
622 then
623 return Match;
624 else
625 return No_Match;
626 end if;
628 elsif Nkind (Choice) = N_Others_Choice then
629 return Match;
631 else
632 if Val = Expr_Value (Choice) then
633 return Match;
634 else
635 return No_Match;
636 end if;
637 end if;
639 -- Real type case
641 elsif Is_Real_Type (Etype (Expr)) then
642 ValR := Expr_Value_R (Expr);
644 if Nkind (Choice) = N_Range then
645 if ValR >= Expr_Value_R (Low_Bound (Choice))
646 and then
647 ValR <= Expr_Value_R (High_Bound (Choice))
648 then
649 return Match;
650 else
651 return No_Match;
652 end if;
654 elsif Nkind (Choice) = N_Subtype_Indication
655 or else
656 (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
657 then
658 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
659 and then
660 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
661 then
662 return Match;
663 else
664 return No_Match;
665 end if;
667 else
668 if ValR = Expr_Value_R (Choice) then
669 return Match;
670 else
671 return No_Match;
672 end if;
673 end if;
675 -- String type cases
677 else
678 pragma Assert (Is_String_Type (Etype (Expr)));
679 ValS := Expr_Value_S (Expr);
681 if Nkind (Choice) = N_Subtype_Indication
682 or else
683 (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
684 then
685 if not Is_Constrained (Etype (Choice)) then
686 return Match;
688 else
689 declare
690 Typlen : constant Uint :=
691 String_Type_Len (Etype (Choice));
692 Strlen : constant Uint :=
693 UI_From_Int (String_Length (Strval (ValS)));
694 begin
695 if Typlen = Strlen then
696 return Match;
697 else
698 return No_Match;
699 end if;
700 end;
701 end if;
703 else
704 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
705 then
706 return Match;
707 else
708 return No_Match;
709 end if;
710 end if;
711 end if;
712 end Choice_Matches;
714 -------------------
715 -- Choices_Match --
716 -------------------
718 function Choices_Match
719 (Expr : Node_Id;
720 Choices : List_Id) return Match_Result
722 Choice : Node_Id;
723 Result : Match_Result;
725 begin
726 Choice := First (Choices);
727 while Present (Choice) loop
728 Result := Choice_Matches (Expr, Choice);
730 if Result /= No_Match then
731 return Result;
732 end if;
734 Next (Choice);
735 end loop;
737 return No_Match;
738 end Choices_Match;
740 --------------------------
741 -- Compile_Time_Compare --
742 --------------------------
744 function Compile_Time_Compare
745 (L, R : Node_Id;
746 Assume_Valid : Boolean) return Compare_Result
748 Discard : aliased Uint;
749 begin
750 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
751 end Compile_Time_Compare;
753 function Compile_Time_Compare
754 (L, R : Node_Id;
755 Diff : access Uint;
756 Assume_Valid : Boolean;
757 Rec : Boolean := False) return Compare_Result
759 Ltyp : Entity_Id := Underlying_Type (Etype (L));
760 Rtyp : Entity_Id := Underlying_Type (Etype (R));
761 -- These get reset to the base type for the case of entities where
762 -- Is_Known_Valid is not set. This takes care of handling possible
763 -- invalid representations using the value of the base type, in
764 -- accordance with RM 13.9.1(10).
766 Discard : aliased Uint;
768 procedure Compare_Decompose
769 (N : Node_Id;
770 R : out Node_Id;
771 V : out Uint);
772 -- This procedure decomposes the node N into an expression node and a
773 -- signed offset, so that the value of N is equal to the value of R plus
774 -- the value V (which may be negative). If no such decomposition is
775 -- possible, then on return R is a copy of N, and V is set to zero.
777 function Compare_Fixup (N : Node_Id) return Node_Id;
778 -- This function deals with replacing 'Last and 'First references with
779 -- their corresponding type bounds, which we then can compare. The
780 -- argument is the original node, the result is the identity, unless we
781 -- have a 'Last/'First reference in which case the value returned is the
782 -- appropriate type bound.
784 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
785 -- Even if the context does not assume that values are valid, some
786 -- simple cases can be recognized.
788 function Is_Same_Value (L, R : Node_Id) return Boolean;
789 -- Returns True iff L and R represent expressions that definitely have
790 -- identical (but not necessarily compile time known) values Indeed the
791 -- caller is expected to have already dealt with the cases of compile
792 -- time known values, so these are not tested here.
794 -----------------------
795 -- Compare_Decompose --
796 -----------------------
798 procedure Compare_Decompose
799 (N : Node_Id;
800 R : out Node_Id;
801 V : out Uint)
803 begin
804 if Nkind (N) = N_Op_Add
805 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
806 then
807 R := Left_Opnd (N);
808 V := Intval (Right_Opnd (N));
809 return;
811 elsif Nkind (N) = N_Op_Subtract
812 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
813 then
814 R := Left_Opnd (N);
815 V := UI_Negate (Intval (Right_Opnd (N)));
816 return;
818 elsif Nkind (N) = N_Attribute_Reference then
819 if Attribute_Name (N) = Name_Succ then
820 R := First (Expressions (N));
821 V := Uint_1;
822 return;
824 elsif Attribute_Name (N) = Name_Pred then
825 R := First (Expressions (N));
826 V := Uint_Minus_1;
827 return;
828 end if;
829 end if;
831 R := N;
832 V := Uint_0;
833 end Compare_Decompose;
835 -------------------
836 -- Compare_Fixup --
837 -------------------
839 function Compare_Fixup (N : Node_Id) return Node_Id is
840 Indx : Node_Id;
841 Xtyp : Entity_Id;
842 Subs : Nat;
844 begin
845 -- Fixup only required for First/Last attribute reference
847 if Nkind (N) = N_Attribute_Reference
848 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
849 then
850 Xtyp := Etype (Prefix (N));
852 -- If we have no type, then just abandon the attempt to do
853 -- a fixup, this is probably the result of some other error.
855 if No (Xtyp) then
856 return N;
857 end if;
859 -- Dereference an access type
861 if Is_Access_Type (Xtyp) then
862 Xtyp := Designated_Type (Xtyp);
863 end if;
865 -- If we don't have an array type at this stage, something is
866 -- peculiar, e.g. another error, and we abandon the attempt at
867 -- a fixup.
869 if not Is_Array_Type (Xtyp) then
870 return N;
871 end if;
873 -- Ignore unconstrained array, since bounds are not meaningful
875 if not Is_Constrained (Xtyp) then
876 return N;
877 end if;
879 if Ekind (Xtyp) = E_String_Literal_Subtype then
880 if Attribute_Name (N) = Name_First then
881 return String_Literal_Low_Bound (Xtyp);
882 else
883 return
884 Make_Integer_Literal (Sloc (N),
885 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
886 String_Literal_Length (Xtyp));
887 end if;
888 end if;
890 -- Find correct index type
892 Indx := First_Index (Xtyp);
894 if Present (Expressions (N)) then
895 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
897 for J in 2 .. Subs loop
898 Indx := Next_Index (Indx);
899 end loop;
900 end if;
902 Xtyp := Etype (Indx);
904 if Attribute_Name (N) = Name_First then
905 return Type_Low_Bound (Xtyp);
906 else
907 return Type_High_Bound (Xtyp);
908 end if;
909 end if;
911 return N;
912 end Compare_Fixup;
914 ----------------------------
915 -- Is_Known_Valid_Operand --
916 ----------------------------
918 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
919 begin
920 return (Is_Entity_Name (Opnd)
921 and then
922 (Is_Known_Valid (Entity (Opnd))
923 or else Ekind (Entity (Opnd)) = E_In_Parameter
924 or else
925 (Ekind (Entity (Opnd)) in Object_Kind
926 and then Present (Current_Value (Entity (Opnd))))))
927 or else Is_OK_Static_Expression (Opnd);
928 end Is_Known_Valid_Operand;
930 -------------------
931 -- Is_Same_Value --
932 -------------------
934 function Is_Same_Value (L, R : Node_Id) return Boolean is
935 Lf : constant Node_Id := Compare_Fixup (L);
936 Rf : constant Node_Id := Compare_Fixup (R);
938 function Is_Same_Subscript (L, R : List_Id) return Boolean;
939 -- L, R are the Expressions values from two attribute nodes for First
940 -- or Last attributes. Either may be set to No_List if no expressions
941 -- are present (indicating subscript 1). The result is True if both
942 -- expressions represent the same subscript (note one case is where
943 -- one subscript is missing and the other is explicitly set to 1).
945 -----------------------
946 -- Is_Same_Subscript --
947 -----------------------
949 function Is_Same_Subscript (L, R : List_Id) return Boolean is
950 begin
951 if L = No_List then
952 if R = No_List then
953 return True;
954 else
955 return Expr_Value (First (R)) = Uint_1;
956 end if;
958 else
959 if R = No_List then
960 return Expr_Value (First (L)) = Uint_1;
961 else
962 return Expr_Value (First (L)) = Expr_Value (First (R));
963 end if;
964 end if;
965 end Is_Same_Subscript;
967 -- Start of processing for Is_Same_Value
969 begin
970 -- Values are the same if they refer to the same entity and the
971 -- entity is non-volatile. This does not however apply to Float
972 -- types, since we may have two NaN values and they should never
973 -- compare equal.
975 -- If the entity is a discriminant, the two expressions may be bounds
976 -- of components of objects of the same discriminated type. The
977 -- values of the discriminants are not static, and therefore the
978 -- result is unknown.
980 -- It would be better to comment individual branches of this test ???
982 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
983 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
984 and then Entity (Lf) = Entity (Rf)
985 and then Ekind (Entity (Lf)) /= E_Discriminant
986 and then Present (Entity (Lf))
987 and then not Is_Floating_Point_Type (Etype (L))
988 and then not Is_Volatile_Reference (L)
989 and then not Is_Volatile_Reference (R)
990 then
991 return True;
993 -- Or if they are compile time known and identical
995 elsif Compile_Time_Known_Value (Lf)
996 and then
997 Compile_Time_Known_Value (Rf)
998 and then Expr_Value (Lf) = Expr_Value (Rf)
999 then
1000 return True;
1002 -- False if Nkind of the two nodes is different for remaining cases
1004 elsif Nkind (Lf) /= Nkind (Rf) then
1005 return False;
1007 -- True if both 'First or 'Last values applying to the same entity
1008 -- (first and last don't change even if value does). Note that we
1009 -- need this even with the calls to Compare_Fixup, to handle the
1010 -- case of unconstrained array attributes where Compare_Fixup
1011 -- cannot find useful bounds.
1013 elsif Nkind (Lf) = N_Attribute_Reference
1014 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1015 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
1016 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
1017 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
1018 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1019 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1020 then
1021 return True;
1023 -- True if the same selected component from the same record
1025 elsif Nkind (Lf) = N_Selected_Component
1026 and then Selector_Name (Lf) = Selector_Name (Rf)
1027 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1028 then
1029 return True;
1031 -- True if the same unary operator applied to the same operand
1033 elsif Nkind (Lf) in N_Unary_Op
1034 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1035 then
1036 return True;
1038 -- True if the same binary operator applied to the same operands
1040 elsif Nkind (Lf) in N_Binary_Op
1041 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1042 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1043 then
1044 return True;
1046 -- All other cases, we can't tell, so return False
1048 else
1049 return False;
1050 end if;
1051 end Is_Same_Value;
1053 -- Start of processing for Compile_Time_Compare
1055 begin
1056 Diff.all := No_Uint;
1058 -- In preanalysis mode, always return Unknown unless the expression
1059 -- is static. It is too early to be thinking we know the result of a
1060 -- comparison, save that judgment for the full analysis. This is
1061 -- particularly important in the case of pre and postconditions, which
1062 -- otherwise can be prematurely collapsed into having True or False
1063 -- conditions when this is inappropriate.
1065 if not (Full_Analysis
1066 or else (Is_OK_Static_Expression (L)
1067 and then
1068 Is_OK_Static_Expression (R)))
1069 then
1070 return Unknown;
1071 end if;
1073 -- If either operand could raise constraint error, then we cannot
1074 -- know the result at compile time (since CE may be raised).
1076 if not (Cannot_Raise_Constraint_Error (L)
1077 and then
1078 Cannot_Raise_Constraint_Error (R))
1079 then
1080 return Unknown;
1081 end if;
1083 -- Identical operands are most certainly equal
1085 if L = R then
1086 return EQ;
1088 -- If expressions have no types, then do not attempt to determine if
1089 -- they are the same, since something funny is going on. One case in
1090 -- which this happens is during generic template analysis, when bounds
1091 -- are not fully analyzed.
1093 elsif No (Ltyp) or else No (Rtyp) then
1094 return Unknown;
1096 -- We do not attempt comparisons for packed arrays arrays represented as
1097 -- modular types, where the semantics of comparison is quite different.
1099 elsif Is_Packed_Array_Impl_Type (Ltyp)
1100 and then Is_Modular_Integer_Type (Ltyp)
1101 then
1102 return Unknown;
1104 -- For access types, the only time we know the result at compile time
1105 -- (apart from identical operands, which we handled already) is if we
1106 -- know one operand is null and the other is not, or both operands are
1107 -- known null.
1109 elsif Is_Access_Type (Ltyp) then
1110 if Known_Null (L) then
1111 if Known_Null (R) then
1112 return EQ;
1113 elsif Known_Non_Null (R) then
1114 return NE;
1115 else
1116 return Unknown;
1117 end if;
1119 elsif Known_Non_Null (L) and then Known_Null (R) then
1120 return NE;
1122 else
1123 return Unknown;
1124 end if;
1126 -- Case where comparison involves two compile time known values
1128 elsif Compile_Time_Known_Value (L)
1129 and then
1130 Compile_Time_Known_Value (R)
1131 then
1132 -- For the floating-point case, we have to be a little careful, since
1133 -- at compile time we are dealing with universal exact values, but at
1134 -- runtime, these will be in non-exact target form. That's why the
1135 -- returned results are LE and GE below instead of LT and GT.
1137 if Is_Floating_Point_Type (Ltyp)
1138 or else
1139 Is_Floating_Point_Type (Rtyp)
1140 then
1141 declare
1142 Lo : constant Ureal := Expr_Value_R (L);
1143 Hi : constant Ureal := Expr_Value_R (R);
1144 begin
1145 if Lo < Hi then
1146 return LE;
1147 elsif Lo = Hi then
1148 return EQ;
1149 else
1150 return GE;
1151 end if;
1152 end;
1154 -- For string types, we have two string literals and we proceed to
1155 -- compare them using the Ada style dictionary string comparison.
1157 elsif not Is_Scalar_Type (Ltyp) then
1158 declare
1159 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1160 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1161 Llen : constant Nat := String_Length (Lstring);
1162 Rlen : constant Nat := String_Length (Rstring);
1164 begin
1165 for J in 1 .. Nat'Min (Llen, Rlen) loop
1166 declare
1167 LC : constant Char_Code := Get_String_Char (Lstring, J);
1168 RC : constant Char_Code := Get_String_Char (Rstring, J);
1169 begin
1170 if LC < RC then
1171 return LT;
1172 elsif LC > RC then
1173 return GT;
1174 end if;
1175 end;
1176 end loop;
1178 if Llen < Rlen then
1179 return LT;
1180 elsif Llen > Rlen then
1181 return GT;
1182 else
1183 return EQ;
1184 end if;
1185 end;
1187 -- For remaining scalar cases we know exactly (note that this does
1188 -- include the fixed-point case, where we know the run time integer
1189 -- values now).
1191 else
1192 declare
1193 Lo : constant Uint := Expr_Value (L);
1194 Hi : constant Uint := Expr_Value (R);
1195 begin
1196 if Lo < Hi then
1197 Diff.all := Hi - Lo;
1198 return LT;
1199 elsif Lo = Hi then
1200 return EQ;
1201 else
1202 Diff.all := Lo - Hi;
1203 return GT;
1204 end if;
1205 end;
1206 end if;
1208 -- Cases where at least one operand is not known at compile time
1210 else
1211 -- Remaining checks apply only for discrete types
1213 if not Is_Discrete_Type (Ltyp)
1214 or else
1215 not Is_Discrete_Type (Rtyp)
1216 then
1217 return Unknown;
1218 end if;
1220 -- Defend against generic types, or actually any expressions that
1221 -- contain a reference to a generic type from within a generic
1222 -- template. We don't want to do any range analysis of such
1223 -- expressions for two reasons. First, the bounds of a generic type
1224 -- itself are junk and cannot be used for any kind of analysis.
1225 -- Second, we may have a case where the range at run time is indeed
1226 -- known, but we don't want to do compile time analysis in the
1227 -- template based on that range since in an instance the value may be
1228 -- static, and able to be elaborated without reference to the bounds
1229 -- of types involved. As an example, consider:
1231 -- (F'Pos (F'Last) + 1) > Integer'Last
1233 -- The expression on the left side of > is Universal_Integer and thus
1234 -- acquires the type Integer for evaluation at run time, and at run
1235 -- time it is true that this condition is always False, but within
1236 -- an instance F may be a type with a static range greater than the
1237 -- range of Integer, and the expression statically evaluates to True.
1239 if References_Generic_Formal_Type (L)
1240 or else
1241 References_Generic_Formal_Type (R)
1242 then
1243 return Unknown;
1244 end if;
1246 -- Replace types by base types for the case of values which are not
1247 -- known to have valid representations. This takes care of properly
1248 -- dealing with invalid representations.
1250 if not Assume_Valid then
1251 if not (Is_Entity_Name (L)
1252 and then (Is_Known_Valid (Entity (L))
1253 or else Assume_No_Invalid_Values))
1254 then
1255 Ltyp := Underlying_Type (Base_Type (Ltyp));
1256 end if;
1258 if not (Is_Entity_Name (R)
1259 and then (Is_Known_Valid (Entity (R))
1260 or else Assume_No_Invalid_Values))
1261 then
1262 Rtyp := Underlying_Type (Base_Type (Rtyp));
1263 end if;
1264 end if;
1266 -- First attempt is to decompose the expressions to extract a
1267 -- constant offset resulting from the use of any of the forms:
1269 -- expr + literal
1270 -- expr - literal
1271 -- typ'Succ (expr)
1272 -- typ'Pred (expr)
1274 -- Then we see if the two expressions are the same value, and if so
1275 -- the result is obtained by comparing the offsets.
1277 -- Note: the reason we do this test first is that it returns only
1278 -- decisive results (with diff set), where other tests, like the
1279 -- range test, may not be as so decisive. Consider for example
1280 -- J .. J + 1. This code can conclude LT with a difference of 1,
1281 -- even if the range of J is not known.
1283 declare
1284 Lnode : Node_Id;
1285 Loffs : Uint;
1286 Rnode : Node_Id;
1287 Roffs : Uint;
1289 begin
1290 Compare_Decompose (L, Lnode, Loffs);
1291 Compare_Decompose (R, Rnode, Roffs);
1293 if Is_Same_Value (Lnode, Rnode) then
1294 if Loffs = Roffs then
1295 return EQ;
1296 elsif Loffs < Roffs then
1297 Diff.all := Roffs - Loffs;
1298 return LT;
1299 else
1300 Diff.all := Loffs - Roffs;
1301 return GT;
1302 end if;
1303 end if;
1304 end;
1306 -- Next, try range analysis and see if operand ranges are disjoint
1308 declare
1309 LOK, ROK : Boolean;
1310 LLo, LHi : Uint;
1311 RLo, RHi : Uint;
1313 Single : Boolean;
1314 -- True if each range is a single point
1316 begin
1317 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1318 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1320 if LOK and ROK then
1321 Single := (LLo = LHi) and then (RLo = RHi);
1323 if LHi < RLo then
1324 if Single and Assume_Valid then
1325 Diff.all := RLo - LLo;
1326 end if;
1328 return LT;
1330 elsif RHi < LLo then
1331 if Single and Assume_Valid then
1332 Diff.all := LLo - RLo;
1333 end if;
1335 return GT;
1337 elsif Single and then LLo = RLo then
1339 -- If the range includes a single literal and we can assume
1340 -- validity then the result is known even if an operand is
1341 -- not static.
1343 if Assume_Valid then
1344 return EQ;
1345 else
1346 return Unknown;
1347 end if;
1349 elsif LHi = RLo then
1350 return LE;
1352 elsif RHi = LLo then
1353 return GE;
1355 elsif not Is_Known_Valid_Operand (L)
1356 and then not Assume_Valid
1357 then
1358 if Is_Same_Value (L, R) then
1359 return EQ;
1360 else
1361 return Unknown;
1362 end if;
1363 end if;
1365 -- If the range of either operand cannot be determined, nothing
1366 -- further can be inferred.
1368 else
1369 return Unknown;
1370 end if;
1371 end;
1373 -- Here is where we check for comparisons against maximum bounds of
1374 -- types, where we know that no value can be outside the bounds of
1375 -- the subtype. Note that this routine is allowed to assume that all
1376 -- expressions are within their subtype bounds. Callers wishing to
1377 -- deal with possibly invalid values must in any case take special
1378 -- steps (e.g. conversions to larger types) to avoid this kind of
1379 -- optimization, which is always considered to be valid. We do not
1380 -- attempt this optimization with generic types, since the type
1381 -- bounds may not be meaningful in this case.
1383 -- We are in danger of an infinite recursion here. It does not seem
1384 -- useful to go more than one level deep, so the parameter Rec is
1385 -- used to protect ourselves against this infinite recursion.
1387 if not Rec then
1389 -- See if we can get a decisive check against one operand and a
1390 -- bound of the other operand (four possible tests here). Note
1391 -- that we avoid testing junk bounds of a generic type.
1393 if not Is_Generic_Type (Rtyp) then
1394 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1395 Discard'Access,
1396 Assume_Valid, Rec => True)
1398 when LT => return LT;
1399 when LE => return LE;
1400 when EQ => return LE;
1401 when others => null;
1402 end case;
1404 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1405 Discard'Access,
1406 Assume_Valid, Rec => True)
1408 when GT => return GT;
1409 when GE => return GE;
1410 when EQ => return GE;
1411 when others => null;
1412 end case;
1413 end if;
1415 if not Is_Generic_Type (Ltyp) then
1416 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1417 Discard'Access,
1418 Assume_Valid, Rec => True)
1420 when GT => return GT;
1421 when GE => return GE;
1422 when EQ => return GE;
1423 when others => null;
1424 end case;
1426 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1427 Discard'Access,
1428 Assume_Valid, Rec => True)
1430 when LT => return LT;
1431 when LE => return LE;
1432 when EQ => return LE;
1433 when others => null;
1434 end case;
1435 end if;
1436 end if;
1438 -- Next attempt is to see if we have an entity compared with a
1439 -- compile time known value, where there is a current value
1440 -- conditional for the entity which can tell us the result.
1442 declare
1443 Var : Node_Id;
1444 -- Entity variable (left operand)
1446 Val : Uint;
1447 -- Value (right operand)
1449 Inv : Boolean;
1450 -- If False, we have reversed the operands
1452 Op : Node_Kind;
1453 -- Comparison operator kind from Get_Current_Value_Condition call
1455 Opn : Node_Id;
1456 -- Value from Get_Current_Value_Condition call
1458 Opv : Uint;
1459 -- Value of Opn
1461 Result : Compare_Result;
1462 -- Known result before inversion
1464 begin
1465 if Is_Entity_Name (L)
1466 and then Compile_Time_Known_Value (R)
1467 then
1468 Var := L;
1469 Val := Expr_Value (R);
1470 Inv := False;
1472 elsif Is_Entity_Name (R)
1473 and then Compile_Time_Known_Value (L)
1474 then
1475 Var := R;
1476 Val := Expr_Value (L);
1477 Inv := True;
1479 -- That was the last chance at finding a compile time result
1481 else
1482 return Unknown;
1483 end if;
1485 Get_Current_Value_Condition (Var, Op, Opn);
1487 -- That was the last chance, so if we got nothing return
1489 if No (Opn) then
1490 return Unknown;
1491 end if;
1493 Opv := Expr_Value (Opn);
1495 -- We got a comparison, so we might have something interesting
1497 -- Convert LE to LT and GE to GT, just so we have fewer cases
1499 if Op = N_Op_Le then
1500 Op := N_Op_Lt;
1501 Opv := Opv + 1;
1503 elsif Op = N_Op_Ge then
1504 Op := N_Op_Gt;
1505 Opv := Opv - 1;
1506 end if;
1508 -- Deal with equality case
1510 if Op = N_Op_Eq then
1511 if Val = Opv then
1512 Result := EQ;
1513 elsif Opv < Val then
1514 Result := LT;
1515 else
1516 Result := GT;
1517 end if;
1519 -- Deal with inequality case
1521 elsif Op = N_Op_Ne then
1522 if Val = Opv then
1523 Result := NE;
1524 else
1525 return Unknown;
1526 end if;
1528 -- Deal with greater than case
1530 elsif Op = N_Op_Gt then
1531 if Opv >= Val then
1532 Result := GT;
1533 elsif Opv = Val - 1 then
1534 Result := GE;
1535 else
1536 return Unknown;
1537 end if;
1539 -- Deal with less than case
1541 else pragma Assert (Op = N_Op_Lt);
1542 if Opv <= Val then
1543 Result := LT;
1544 elsif Opv = Val + 1 then
1545 Result := LE;
1546 else
1547 return Unknown;
1548 end if;
1549 end if;
1551 -- Deal with inverting result
1553 if Inv then
1554 case Result is
1555 when GT => return LT;
1556 when GE => return LE;
1557 when LT => return GT;
1558 when LE => return GE;
1559 when others => return Result;
1560 end case;
1561 end if;
1563 return Result;
1564 end;
1565 end if;
1566 end Compile_Time_Compare;
1568 -------------------------------
1569 -- Compile_Time_Known_Bounds --
1570 -------------------------------
1572 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1573 Indx : Node_Id;
1574 Typ : Entity_Id;
1576 begin
1577 if T = Any_Composite or else not Is_Array_Type (T) then
1578 return False;
1579 end if;
1581 Indx := First_Index (T);
1582 while Present (Indx) loop
1583 Typ := Underlying_Type (Etype (Indx));
1585 -- Never look at junk bounds of a generic type
1587 if Is_Generic_Type (Typ) then
1588 return False;
1589 end if;
1591 -- Otherwise check bounds for compile time known
1593 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1594 return False;
1595 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1596 return False;
1597 else
1598 Next_Index (Indx);
1599 end if;
1600 end loop;
1602 return True;
1603 end Compile_Time_Known_Bounds;
1605 ------------------------------
1606 -- Compile_Time_Known_Value --
1607 ------------------------------
1609 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1610 K : constant Node_Kind := Nkind (Op);
1611 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1613 begin
1614 -- Never known at compile time if bad type or raises constraint error
1615 -- or empty (latter case occurs only as a result of a previous error).
1617 if No (Op) then
1618 Check_Error_Detected;
1619 return False;
1621 elsif Op = Error
1622 or else Etype (Op) = Any_Type
1623 or else Raises_Constraint_Error (Op)
1624 then
1625 return False;
1626 end if;
1628 -- If we have an entity name, then see if it is the name of a constant
1629 -- and if so, test the corresponding constant value, or the name of
1630 -- an enumeration literal, which is always a constant.
1632 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1633 declare
1634 E : constant Entity_Id := Entity (Op);
1635 V : Node_Id;
1637 begin
1638 -- Never known at compile time if it is a packed array value.
1639 -- We might want to try to evaluate these at compile time one
1640 -- day, but we do not make that attempt now.
1642 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1643 return False;
1644 end if;
1646 if Ekind (E) = E_Enumeration_Literal then
1647 return True;
1649 elsif Ekind (E) = E_Constant then
1650 V := Constant_Value (E);
1651 return Present (V) and then Compile_Time_Known_Value (V);
1652 end if;
1653 end;
1655 -- We have a value, see if it is compile time known
1657 else
1658 -- Integer literals are worth storing in the cache
1660 if K = N_Integer_Literal then
1661 CV_Ent.N := Op;
1662 CV_Ent.V := Intval (Op);
1663 return True;
1665 -- Other literals and NULL are known at compile time
1667 elsif
1668 Nkind_In (K, N_Character_Literal,
1669 N_Real_Literal,
1670 N_String_Literal,
1671 N_Null)
1672 then
1673 return True;
1674 end if;
1675 end if;
1677 -- If we fall through, not known at compile time
1679 return False;
1681 -- If we get an exception while trying to do this test, then some error
1682 -- has occurred, and we simply say that the value is not known after all
1684 exception
1685 when others =>
1686 return False;
1687 end Compile_Time_Known_Value;
1689 --------------------------------------
1690 -- Compile_Time_Known_Value_Or_Aggr --
1691 --------------------------------------
1693 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1694 begin
1695 -- If we have an entity name, then see if it is the name of a constant
1696 -- and if so, test the corresponding constant value, or the name of
1697 -- an enumeration literal, which is always a constant.
1699 if Is_Entity_Name (Op) then
1700 declare
1701 E : constant Entity_Id := Entity (Op);
1702 V : Node_Id;
1704 begin
1705 if Ekind (E) = E_Enumeration_Literal then
1706 return True;
1708 elsif Ekind (E) /= E_Constant then
1709 return False;
1711 else
1712 V := Constant_Value (E);
1713 return Present (V)
1714 and then Compile_Time_Known_Value_Or_Aggr (V);
1715 end if;
1716 end;
1718 -- We have a value, see if it is compile time known
1720 else
1721 if Compile_Time_Known_Value (Op) then
1722 return True;
1724 elsif Nkind (Op) = N_Aggregate then
1726 if Present (Expressions (Op)) then
1727 declare
1728 Expr : Node_Id;
1729 begin
1730 Expr := First (Expressions (Op));
1731 while Present (Expr) loop
1732 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1733 return False;
1734 else
1735 Next (Expr);
1736 end if;
1737 end loop;
1738 end;
1739 end if;
1741 if Present (Component_Associations (Op)) then
1742 declare
1743 Cass : Node_Id;
1745 begin
1746 Cass := First (Component_Associations (Op));
1747 while Present (Cass) loop
1748 if not
1749 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1750 then
1751 return False;
1752 end if;
1754 Next (Cass);
1755 end loop;
1756 end;
1757 end if;
1759 return True;
1761 -- All other types of values are not known at compile time
1763 else
1764 return False;
1765 end if;
1767 end if;
1768 end Compile_Time_Known_Value_Or_Aggr;
1770 ---------------------------------------
1771 -- CRT_Safe_Compile_Time_Known_Value --
1772 ---------------------------------------
1774 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1775 begin
1776 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1777 and then not Is_OK_Static_Expression (Op)
1778 then
1779 return False;
1780 else
1781 return Compile_Time_Known_Value (Op);
1782 end if;
1783 end CRT_Safe_Compile_Time_Known_Value;
1785 -----------------
1786 -- Eval_Actual --
1787 -----------------
1789 -- This is only called for actuals of functions that are not predefined
1790 -- operators (which have already been rewritten as operators at this
1791 -- stage), so the call can never be folded, and all that needs doing for
1792 -- the actual is to do the check for a non-static context.
1794 procedure Eval_Actual (N : Node_Id) is
1795 begin
1796 Check_Non_Static_Context (N);
1797 end Eval_Actual;
1799 --------------------
1800 -- Eval_Allocator --
1801 --------------------
1803 -- Allocators are never static, so all we have to do is to do the
1804 -- check for a non-static context if an expression is present.
1806 procedure Eval_Allocator (N : Node_Id) is
1807 Expr : constant Node_Id := Expression (N);
1808 begin
1809 if Nkind (Expr) = N_Qualified_Expression then
1810 Check_Non_Static_Context (Expression (Expr));
1811 end if;
1812 end Eval_Allocator;
1814 ------------------------
1815 -- Eval_Arithmetic_Op --
1816 ------------------------
1818 -- Arithmetic operations are static functions, so the result is static
1819 -- if both operands are static (RM 4.9(7), 4.9(20)).
1821 procedure Eval_Arithmetic_Op (N : Node_Id) is
1822 Left : constant Node_Id := Left_Opnd (N);
1823 Right : constant Node_Id := Right_Opnd (N);
1824 Ltype : constant Entity_Id := Etype (Left);
1825 Rtype : constant Entity_Id := Etype (Right);
1826 Otype : Entity_Id := Empty;
1827 Stat : Boolean;
1828 Fold : Boolean;
1830 begin
1831 -- If not foldable we are done
1833 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1835 if not Fold then
1836 return;
1837 end if;
1839 -- Otherwise attempt to fold
1841 if Is_Universal_Numeric_Type (Etype (Left))
1842 and then
1843 Is_Universal_Numeric_Type (Etype (Right))
1844 then
1845 Otype := Find_Universal_Operator_Type (N);
1846 end if;
1848 -- Fold for cases where both operands are of integer type
1850 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1851 declare
1852 Left_Int : constant Uint := Expr_Value (Left);
1853 Right_Int : constant Uint := Expr_Value (Right);
1854 Result : Uint;
1856 begin
1857 case Nkind (N) is
1858 when N_Op_Add =>
1859 Result := Left_Int + Right_Int;
1861 when N_Op_Subtract =>
1862 Result := Left_Int - Right_Int;
1864 when N_Op_Multiply =>
1865 if OK_Bits
1866 (N, UI_From_Int
1867 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1868 then
1869 Result := Left_Int * Right_Int;
1870 else
1871 Result := Left_Int;
1872 end if;
1874 when N_Op_Divide =>
1876 -- The exception Constraint_Error is raised by integer
1877 -- division, rem and mod if the right operand is zero.
1879 if Right_Int = 0 then
1880 Apply_Compile_Time_Constraint_Error
1881 (N, "division by zero", CE_Divide_By_Zero,
1882 Warn => not Stat);
1883 Set_Raises_Constraint_Error (N);
1884 return;
1886 -- Otherwise we can do the division
1888 else
1889 Result := Left_Int / Right_Int;
1890 end if;
1892 when N_Op_Mod =>
1894 -- The exception Constraint_Error is raised by integer
1895 -- division, rem and mod if the right operand is zero.
1897 if Right_Int = 0 then
1898 Apply_Compile_Time_Constraint_Error
1899 (N, "mod with zero divisor", CE_Divide_By_Zero,
1900 Warn => not Stat);
1901 return;
1902 else
1903 Result := Left_Int mod Right_Int;
1904 end if;
1906 when N_Op_Rem =>
1908 -- The exception Constraint_Error is raised by integer
1909 -- division, rem and mod if the right operand is zero.
1911 if Right_Int = 0 then
1912 Apply_Compile_Time_Constraint_Error
1913 (N, "rem with zero divisor", CE_Divide_By_Zero,
1914 Warn => not Stat);
1915 return;
1917 else
1918 Result := Left_Int rem Right_Int;
1919 end if;
1921 when others =>
1922 raise Program_Error;
1923 end case;
1925 -- Adjust the result by the modulus if the type is a modular type
1927 if Is_Modular_Integer_Type (Ltype) then
1928 Result := Result mod Modulus (Ltype);
1930 -- For a signed integer type, check non-static overflow
1932 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1933 declare
1934 BT : constant Entity_Id := Base_Type (Ltype);
1935 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1936 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1937 begin
1938 if Result < Lo or else Result > Hi then
1939 Apply_Compile_Time_Constraint_Error
1940 (N, "value not in range of }??",
1941 CE_Overflow_Check_Failed,
1942 Ent => BT);
1943 return;
1944 end if;
1945 end;
1946 end if;
1948 -- If we get here we can fold the result
1950 Fold_Uint (N, Result, Stat);
1951 end;
1953 -- Cases where at least one operand is a real. We handle the cases of
1954 -- both reals, or mixed/real integer cases (the latter happen only for
1955 -- divide and multiply, and the result is always real).
1957 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1958 declare
1959 Left_Real : Ureal;
1960 Right_Real : Ureal;
1961 Result : Ureal;
1963 begin
1964 if Is_Real_Type (Ltype) then
1965 Left_Real := Expr_Value_R (Left);
1966 else
1967 Left_Real := UR_From_Uint (Expr_Value (Left));
1968 end if;
1970 if Is_Real_Type (Rtype) then
1971 Right_Real := Expr_Value_R (Right);
1972 else
1973 Right_Real := UR_From_Uint (Expr_Value (Right));
1974 end if;
1976 if Nkind (N) = N_Op_Add then
1977 Result := Left_Real + Right_Real;
1979 elsif Nkind (N) = N_Op_Subtract then
1980 Result := Left_Real - Right_Real;
1982 elsif Nkind (N) = N_Op_Multiply then
1983 Result := Left_Real * Right_Real;
1985 else pragma Assert (Nkind (N) = N_Op_Divide);
1986 if UR_Is_Zero (Right_Real) then
1987 Apply_Compile_Time_Constraint_Error
1988 (N, "division by zero", CE_Divide_By_Zero);
1989 return;
1990 end if;
1992 Result := Left_Real / Right_Real;
1993 end if;
1995 Fold_Ureal (N, Result, Stat);
1996 end;
1997 end if;
1999 -- If the operator was resolved to a specific type, make sure that type
2000 -- is frozen even if the expression is folded into a literal (which has
2001 -- a universal type).
2003 if Present (Otype) then
2004 Freeze_Before (N, Otype);
2005 end if;
2006 end Eval_Arithmetic_Op;
2008 ----------------------------
2009 -- Eval_Character_Literal --
2010 ----------------------------
2012 -- Nothing to be done
2014 procedure Eval_Character_Literal (N : Node_Id) is
2015 pragma Warnings (Off, N);
2016 begin
2017 null;
2018 end Eval_Character_Literal;
2020 ---------------
2021 -- Eval_Call --
2022 ---------------
2024 -- Static function calls are either calls to predefined operators
2025 -- with static arguments, or calls to functions that rename a literal.
2026 -- Only the latter case is handled here, predefined operators are
2027 -- constant-folded elsewhere.
2029 -- If the function is itself inherited (see 7423-001) the literal of
2030 -- the parent type must be explicitly converted to the return type
2031 -- of the function.
2033 procedure Eval_Call (N : Node_Id) is
2034 Loc : constant Source_Ptr := Sloc (N);
2035 Typ : constant Entity_Id := Etype (N);
2036 Lit : Entity_Id;
2038 begin
2039 if Nkind (N) = N_Function_Call
2040 and then No (Parameter_Associations (N))
2041 and then Is_Entity_Name (Name (N))
2042 and then Present (Alias (Entity (Name (N))))
2043 and then Is_Enumeration_Type (Base_Type (Typ))
2044 then
2045 Lit := Ultimate_Alias (Entity (Name (N)));
2047 if Ekind (Lit) = E_Enumeration_Literal then
2048 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2049 Rewrite
2050 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2051 else
2052 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2053 end if;
2055 Resolve (N, Typ);
2056 end if;
2057 end if;
2058 end Eval_Call;
2060 --------------------------
2061 -- Eval_Case_Expression --
2062 --------------------------
2064 -- A conditional expression is static if all its conditions and dependent
2065 -- expressions are static. Note that we do not care if the dependent
2066 -- expressions raise CE, except for the one that will be selected.
2068 procedure Eval_Case_Expression (N : Node_Id) is
2069 Alt : Node_Id;
2070 Choice : Node_Id;
2072 begin
2073 Set_Is_Static_Expression (N, False);
2075 if not Is_Static_Expression (Expression (N)) then
2076 Check_Non_Static_Context (Expression (N));
2077 return;
2078 end if;
2080 -- First loop, make sure all the alternatives are static expressions
2081 -- none of which raise Constraint_Error. We make the constraint error
2082 -- check because part of the legality condition for a correct static
2083 -- case expression is that the cases are covered, like any other case
2084 -- expression. And we can't do that if any of the conditions raise an
2085 -- exception, so we don't even try to evaluate if that is the case.
2087 Alt := First (Alternatives (N));
2088 while Present (Alt) loop
2090 -- The expression must be static, but we don't care at this stage
2091 -- if it raises Constraint_Error (the alternative might not match,
2092 -- in which case the expression is statically unevaluated anyway).
2094 if not Is_Static_Expression (Expression (Alt)) then
2095 Check_Non_Static_Context (Expression (Alt));
2096 return;
2097 end if;
2099 -- The choices of a case always have to be static, and cannot raise
2100 -- an exception. If this condition is not met, then the expression
2101 -- is plain illegal, so just abandon evaluation attempts. No need
2102 -- to check non-static context when we have something illegal anyway.
2104 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2105 return;
2106 end if;
2108 Next (Alt);
2109 end loop;
2111 -- OK, if the above loop gets through it means that all choices are OK
2112 -- static (don't raise exceptions), so the whole case is static, and we
2113 -- can find the matching alternative.
2115 Set_Is_Static_Expression (N);
2117 -- Now to deal with propagating a possible constraint error
2119 -- If the selecting expression raises CE, propagate and we are done
2121 if Raises_Constraint_Error (Expression (N)) then
2122 Set_Raises_Constraint_Error (N);
2124 -- Otherwise we need to check the alternatives to find the matching
2125 -- one. CE's in other than the matching one are not relevant. But we
2126 -- do need to check the matching one. Unlike the first loop, we do not
2127 -- have to go all the way through, when we find the matching one, quit.
2129 else
2130 Alt := First (Alternatives (N));
2131 Search : loop
2133 -- We must find a match among the alternatives. If not, this must
2134 -- be due to other errors, so just ignore, leaving as non-static.
2136 if No (Alt) then
2137 Set_Is_Static_Expression (N, False);
2138 return;
2139 end if;
2141 -- Otherwise loop through choices of this alternative
2143 Choice := First (Discrete_Choices (Alt));
2144 while Present (Choice) loop
2146 -- If we find a matching choice, then the Expression of this
2147 -- alternative replaces N (Raises_Constraint_Error flag is
2148 -- included, so we don't have to special case that).
2150 if Choice_Matches (Expression (N), Choice) = Match then
2151 Rewrite (N, Relocate_Node (Expression (Alt)));
2152 return;
2153 end if;
2155 Next (Choice);
2156 end loop;
2158 Next (Alt);
2159 end loop Search;
2160 end if;
2161 end Eval_Case_Expression;
2163 ------------------------
2164 -- Eval_Concatenation --
2165 ------------------------
2167 -- Concatenation is a static function, so the result is static if both
2168 -- operands are static (RM 4.9(7), 4.9(21)).
2170 procedure Eval_Concatenation (N : Node_Id) is
2171 Left : constant Node_Id := Left_Opnd (N);
2172 Right : constant Node_Id := Right_Opnd (N);
2173 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2174 Stat : Boolean;
2175 Fold : Boolean;
2177 begin
2178 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2179 -- non-static context.
2181 if Ada_Version = Ada_83
2182 and then Comes_From_Source (N)
2183 then
2184 Check_Non_Static_Context (Left);
2185 Check_Non_Static_Context (Right);
2186 return;
2187 end if;
2189 -- If not foldable we are done. In principle concatenation that yields
2190 -- any string type is static (i.e. an array type of character types).
2191 -- However, character types can include enumeration literals, and
2192 -- concatenation in that case cannot be described by a literal, so we
2193 -- only consider the operation static if the result is an array of
2194 -- (a descendant of) a predefined character type.
2196 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2198 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2199 Set_Is_Static_Expression (N, False);
2200 return;
2201 end if;
2203 -- Compile time string concatenation
2205 -- ??? Note that operands that are aggregates can be marked as static,
2206 -- so we should attempt at a later stage to fold concatenations with
2207 -- such aggregates.
2209 declare
2210 Left_Str : constant Node_Id := Get_String_Val (Left);
2211 Left_Len : Nat;
2212 Right_Str : constant Node_Id := Get_String_Val (Right);
2213 Folded_Val : String_Id;
2215 begin
2216 -- Establish new string literal, and store left operand. We make
2217 -- sure to use the special Start_String that takes an operand if
2218 -- the left operand is a string literal. Since this is optimized
2219 -- in the case where that is the most recently created string
2220 -- literal, we ensure efficient time/space behavior for the
2221 -- case of a concatenation of a series of string literals.
2223 if Nkind (Left_Str) = N_String_Literal then
2224 Left_Len := String_Length (Strval (Left_Str));
2226 -- If the left operand is the empty string, and the right operand
2227 -- is a string literal (the case of "" & "..."), the result is the
2228 -- value of the right operand. This optimization is important when
2229 -- Is_Folded_In_Parser, to avoid copying an enormous right
2230 -- operand.
2232 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2233 Folded_Val := Strval (Right_Str);
2234 else
2235 Start_String (Strval (Left_Str));
2236 end if;
2238 else
2239 Start_String;
2240 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2241 Left_Len := 1;
2242 end if;
2244 -- Now append the characters of the right operand, unless we
2245 -- optimized the "" & "..." case above.
2247 if Nkind (Right_Str) = N_String_Literal then
2248 if Left_Len /= 0 then
2249 Store_String_Chars (Strval (Right_Str));
2250 Folded_Val := End_String;
2251 end if;
2252 else
2253 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2254 Folded_Val := End_String;
2255 end if;
2257 Set_Is_Static_Expression (N, Stat);
2259 -- If left operand is the empty string, the result is the
2260 -- right operand, including its bounds if anomalous.
2262 if Left_Len = 0
2263 and then Is_Array_Type (Etype (Right))
2264 and then Etype (Right) /= Any_String
2265 then
2266 Set_Etype (N, Etype (Right));
2267 end if;
2269 Fold_Str (N, Folded_Val, Static => Stat);
2270 end;
2271 end Eval_Concatenation;
2273 ----------------------
2274 -- Eval_Entity_Name --
2275 ----------------------
2277 -- This procedure is used for identifiers and expanded names other than
2278 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2279 -- static if they denote a static constant (RM 4.9(6)) or if the name
2280 -- denotes an enumeration literal (RM 4.9(22)).
2282 procedure Eval_Entity_Name (N : Node_Id) is
2283 Def_Id : constant Entity_Id := Entity (N);
2284 Val : Node_Id;
2286 begin
2287 -- Enumeration literals are always considered to be constants
2288 -- and cannot raise constraint error (RM 4.9(22)).
2290 if Ekind (Def_Id) = E_Enumeration_Literal then
2291 Set_Is_Static_Expression (N);
2292 return;
2294 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2295 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2296 -- it does not violate 10.2.1(8) here, since this is not a variable.
2298 elsif Ekind (Def_Id) = E_Constant then
2300 -- Deferred constants must always be treated as nonstatic outside the
2301 -- scope of their full view.
2303 if Present (Full_View (Def_Id))
2304 and then not In_Open_Scopes (Scope (Def_Id))
2305 then
2306 Val := Empty;
2307 else
2308 Val := Constant_Value (Def_Id);
2309 end if;
2311 if Present (Val) then
2312 Set_Is_Static_Expression
2313 (N, Is_Static_Expression (Val)
2314 and then Is_Static_Subtype (Etype (Def_Id)));
2315 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2317 if not Is_Static_Expression (N)
2318 and then not Is_Generic_Type (Etype (N))
2319 then
2320 Validate_Static_Object_Name (N);
2321 end if;
2323 -- Mark constant condition in SCOs
2325 if Generate_SCO
2326 and then Comes_From_Source (N)
2327 and then Is_Boolean_Type (Etype (Def_Id))
2328 and then Compile_Time_Known_Value (N)
2329 then
2330 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2331 end if;
2333 return;
2334 end if;
2335 end if;
2337 -- Fall through if the name is not static
2339 Validate_Static_Object_Name (N);
2340 end Eval_Entity_Name;
2342 ------------------------
2343 -- Eval_If_Expression --
2344 ------------------------
2346 -- We can fold to a static expression if the condition and both dependent
2347 -- expressions are static. Otherwise, the only required processing is to do
2348 -- the check for non-static context for the then and else expressions.
2350 procedure Eval_If_Expression (N : Node_Id) is
2351 Condition : constant Node_Id := First (Expressions (N));
2352 Then_Expr : constant Node_Id := Next (Condition);
2353 Else_Expr : constant Node_Id := Next (Then_Expr);
2354 Result : Node_Id;
2355 Non_Result : Node_Id;
2357 Rstat : constant Boolean :=
2358 Is_Static_Expression (Condition)
2359 and then
2360 Is_Static_Expression (Then_Expr)
2361 and then
2362 Is_Static_Expression (Else_Expr);
2363 -- True if result is static
2365 begin
2366 -- If result not static, nothing to do, otherwise set static result
2368 if not Rstat then
2369 return;
2370 else
2371 Set_Is_Static_Expression (N);
2372 end if;
2374 -- If any operand is Any_Type, just propagate to result and do not try
2375 -- to fold, this prevents cascaded errors.
2377 if Etype (Condition) = Any_Type or else
2378 Etype (Then_Expr) = Any_Type or else
2379 Etype (Else_Expr) = Any_Type
2380 then
2381 Set_Etype (N, Any_Type);
2382 Set_Is_Static_Expression (N, False);
2383 return;
2384 end if;
2386 -- If condition raises constraint error then we have already signaled
2387 -- an error, and we just propagate to the result and do not fold.
2389 if Raises_Constraint_Error (Condition) then
2390 Set_Raises_Constraint_Error (N);
2391 return;
2392 end if;
2394 -- Static case where we can fold. Note that we don't try to fold cases
2395 -- where the condition is known at compile time, but the result is
2396 -- non-static. This avoids possible cases of infinite recursion where
2397 -- the expander puts in a redundant test and we remove it. Instead we
2398 -- deal with these cases in the expander.
2400 -- Select result operand
2402 if Is_True (Expr_Value (Condition)) then
2403 Result := Then_Expr;
2404 Non_Result := Else_Expr;
2405 else
2406 Result := Else_Expr;
2407 Non_Result := Then_Expr;
2408 end if;
2410 -- Note that it does not matter if the non-result operand raises a
2411 -- Constraint_Error, but if the result raises constraint error then we
2412 -- replace the node with a raise constraint error. This will properly
2413 -- propagate Raises_Constraint_Error since this flag is set in Result.
2415 if Raises_Constraint_Error (Result) then
2416 Rewrite_In_Raise_CE (N, Result);
2417 Check_Non_Static_Context (Non_Result);
2419 -- Otherwise the result operand replaces the original node
2421 else
2422 Rewrite (N, Relocate_Node (Result));
2423 Set_Is_Static_Expression (N);
2424 end if;
2425 end Eval_If_Expression;
2427 ----------------------------
2428 -- Eval_Indexed_Component --
2429 ----------------------------
2431 -- Indexed components are never static, so we need to perform the check
2432 -- for non-static context on the index values. Then, we check if the
2433 -- value can be obtained at compile time, even though it is non-static.
2435 procedure Eval_Indexed_Component (N : Node_Id) is
2436 Expr : Node_Id;
2438 begin
2439 -- Check for non-static context on index values
2441 Expr := First (Expressions (N));
2442 while Present (Expr) loop
2443 Check_Non_Static_Context (Expr);
2444 Next (Expr);
2445 end loop;
2447 -- If the indexed component appears in an object renaming declaration
2448 -- then we do not want to try to evaluate it, since in this case we
2449 -- need the identity of the array element.
2451 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2452 return;
2454 -- Similarly if the indexed component appears as the prefix of an
2455 -- attribute we don't want to evaluate it, because at least for
2456 -- some cases of attributes we need the identify (e.g. Access, Size)
2458 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2459 return;
2460 end if;
2462 -- Note: there are other cases, such as the left side of an assignment,
2463 -- or an OUT parameter for a call, where the replacement results in the
2464 -- illegal use of a constant, But these cases are illegal in the first
2465 -- place, so the replacement, though silly, is harmless.
2467 -- Now see if this is a constant array reference
2469 if List_Length (Expressions (N)) = 1
2470 and then Is_Entity_Name (Prefix (N))
2471 and then Ekind (Entity (Prefix (N))) = E_Constant
2472 and then Present (Constant_Value (Entity (Prefix (N))))
2473 then
2474 declare
2475 Loc : constant Source_Ptr := Sloc (N);
2476 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2477 Sub : constant Node_Id := First (Expressions (N));
2479 Atyp : Entity_Id;
2480 -- Type of array
2482 Lin : Nat;
2483 -- Linear one's origin subscript value for array reference
2485 Lbd : Node_Id;
2486 -- Lower bound of the first array index
2488 Elm : Node_Id;
2489 -- Value from constant array
2491 begin
2492 Atyp := Etype (Arr);
2494 if Is_Access_Type (Atyp) then
2495 Atyp := Designated_Type (Atyp);
2496 end if;
2498 -- If we have an array type (we should have but perhaps there are
2499 -- error cases where this is not the case), then see if we can do
2500 -- a constant evaluation of the array reference.
2502 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2503 if Ekind (Atyp) = E_String_Literal_Subtype then
2504 Lbd := String_Literal_Low_Bound (Atyp);
2505 else
2506 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2507 end if;
2509 if Compile_Time_Known_Value (Sub)
2510 and then Nkind (Arr) = N_Aggregate
2511 and then Compile_Time_Known_Value (Lbd)
2512 and then Is_Discrete_Type (Component_Type (Atyp))
2513 then
2514 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2516 if List_Length (Expressions (Arr)) >= Lin then
2517 Elm := Pick (Expressions (Arr), Lin);
2519 -- If the resulting expression is compile time known,
2520 -- then we can rewrite the indexed component with this
2521 -- value, being sure to mark the result as non-static.
2522 -- We also reset the Sloc, in case this generates an
2523 -- error later on (e.g. 136'Access).
2525 if Compile_Time_Known_Value (Elm) then
2526 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2527 Set_Is_Static_Expression (N, False);
2528 Set_Sloc (N, Loc);
2529 end if;
2530 end if;
2532 -- We can also constant-fold if the prefix is a string literal.
2533 -- This will be useful in an instantiation or an inlining.
2535 elsif Compile_Time_Known_Value (Sub)
2536 and then Nkind (Arr) = N_String_Literal
2537 and then Compile_Time_Known_Value (Lbd)
2538 and then Expr_Value (Lbd) = 1
2539 and then Expr_Value (Sub) <=
2540 String_Literal_Length (Etype (Arr))
2541 then
2542 declare
2543 C : constant Char_Code :=
2544 Get_String_Char (Strval (Arr),
2545 UI_To_Int (Expr_Value (Sub)));
2546 begin
2547 Set_Character_Literal_Name (C);
2549 Elm :=
2550 Make_Character_Literal (Loc,
2551 Chars => Name_Find,
2552 Char_Literal_Value => UI_From_CC (C));
2553 Set_Etype (Elm, Component_Type (Atyp));
2554 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2555 Set_Is_Static_Expression (N, False);
2556 end;
2557 end if;
2558 end if;
2559 end;
2560 end if;
2561 end Eval_Indexed_Component;
2563 --------------------------
2564 -- Eval_Integer_Literal --
2565 --------------------------
2567 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2568 -- as static by the analyzer. The reason we did it that early is to allow
2569 -- the possibility of turning off the Is_Static_Expression flag after
2570 -- analysis, but before resolution, when integer literals are generated in
2571 -- the expander that do not correspond to static expressions.
2573 procedure Eval_Integer_Literal (N : Node_Id) is
2574 T : constant Entity_Id := Etype (N);
2576 function In_Any_Integer_Context return Boolean;
2577 -- If the literal is resolved with a specific type in a context where
2578 -- the expected type is Any_Integer, there are no range checks on the
2579 -- literal. By the time the literal is evaluated, it carries the type
2580 -- imposed by the enclosing expression, and we must recover the context
2581 -- to determine that Any_Integer is meant.
2583 ----------------------------
2584 -- In_Any_Integer_Context --
2585 ----------------------------
2587 function In_Any_Integer_Context return Boolean is
2588 Par : constant Node_Id := Parent (N);
2589 K : constant Node_Kind := Nkind (Par);
2591 begin
2592 -- Any_Integer also appears in digits specifications for real types,
2593 -- but those have bounds smaller that those of any integer base type,
2594 -- so we can safely ignore these cases.
2596 return Nkind_In (K, N_Number_Declaration,
2597 N_Attribute_Reference,
2598 N_Attribute_Definition_Clause,
2599 N_Modular_Type_Definition,
2600 N_Signed_Integer_Type_Definition);
2601 end In_Any_Integer_Context;
2603 -- Start of processing for Eval_Integer_Literal
2605 begin
2607 -- If the literal appears in a non-expression context, then it is
2608 -- certainly appearing in a non-static context, so check it. This is
2609 -- actually a redundant check, since Check_Non_Static_Context would
2610 -- check it, but it seems worth while avoiding the call.
2612 if Nkind (Parent (N)) not in N_Subexpr
2613 and then not In_Any_Integer_Context
2614 then
2615 Check_Non_Static_Context (N);
2616 end if;
2618 -- Modular integer literals must be in their base range
2620 if Is_Modular_Integer_Type (T)
2621 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
2622 then
2623 Out_Of_Range (N);
2624 end if;
2625 end Eval_Integer_Literal;
2627 ---------------------
2628 -- Eval_Logical_Op --
2629 ---------------------
2631 -- Logical operations are static functions, so the result is potentially
2632 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2634 procedure Eval_Logical_Op (N : Node_Id) is
2635 Left : constant Node_Id := Left_Opnd (N);
2636 Right : constant Node_Id := Right_Opnd (N);
2637 Stat : Boolean;
2638 Fold : Boolean;
2640 begin
2641 -- If not foldable we are done
2643 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2645 if not Fold then
2646 return;
2647 end if;
2649 -- Compile time evaluation of logical operation
2651 declare
2652 Left_Int : constant Uint := Expr_Value (Left);
2653 Right_Int : constant Uint := Expr_Value (Right);
2655 begin
2656 if Is_Modular_Integer_Type (Etype (N)) then
2657 declare
2658 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2659 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2661 begin
2662 To_Bits (Left_Int, Left_Bits);
2663 To_Bits (Right_Int, Right_Bits);
2665 -- Note: should really be able to use array ops instead of
2666 -- these loops, but they weren't working at the time ???
2668 if Nkind (N) = N_Op_And then
2669 for J in Left_Bits'Range loop
2670 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2671 end loop;
2673 elsif Nkind (N) = N_Op_Or then
2674 for J in Left_Bits'Range loop
2675 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2676 end loop;
2678 else
2679 pragma Assert (Nkind (N) = N_Op_Xor);
2681 for J in Left_Bits'Range loop
2682 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2683 end loop;
2684 end if;
2686 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2687 end;
2689 else
2690 pragma Assert (Is_Boolean_Type (Etype (N)));
2692 if Nkind (N) = N_Op_And then
2693 Fold_Uint (N,
2694 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2696 elsif Nkind (N) = N_Op_Or then
2697 Fold_Uint (N,
2698 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2700 else
2701 pragma Assert (Nkind (N) = N_Op_Xor);
2702 Fold_Uint (N,
2703 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2704 end if;
2705 end if;
2706 end;
2707 end Eval_Logical_Op;
2709 ------------------------
2710 -- Eval_Membership_Op --
2711 ------------------------
2713 -- A membership test is potentially static if the expression is static, and
2714 -- the range is a potentially static range, or is a subtype mark denoting a
2715 -- static subtype (RM 4.9(12)).
2717 procedure Eval_Membership_Op (N : Node_Id) is
2718 Left : constant Node_Id := Left_Opnd (N);
2719 Right : constant Node_Id := Right_Opnd (N);
2720 Alts : constant List_Id := Alternatives (N);
2721 Result : Match_Result;
2723 begin
2724 -- Ignore if error in either operand, except to make sure that Any_Type
2725 -- is properly propagated to avoid junk cascaded errors.
2727 if Etype (Left) = Any_Type
2728 or else (Present (Right) and then Etype (Right) = Any_Type)
2729 then
2730 Set_Etype (N, Any_Type);
2731 return;
2732 end if;
2734 -- Ignore if types involved have predicates
2735 -- Is this right for static predicates ???
2736 -- And what about the alternatives ???
2738 if Present (Predicate_Function (Etype (Left)))
2739 or else (Present (Right)
2740 and then Present (Predicate_Function (Etype (Right))))
2741 then
2742 return;
2743 end if;
2745 -- If left operand non-static, then nothing to do
2747 if not Is_Static_Expression (Left) then
2748 return;
2749 end if;
2751 -- If choice is non-static, left operand is in non-static context
2753 if (Present (Right) and then not Is_Static_Choice (Right))
2754 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
2755 then
2756 Check_Non_Static_Context (Left);
2757 return;
2758 end if;
2760 -- Otherwise we definitely have a static expression
2762 Set_Is_Static_Expression (N);
2764 -- If left operand raises constraint error, propagate and we are done
2766 if Raises_Constraint_Error (Left) then
2767 Set_Raises_Constraint_Error (N, True);
2769 -- See if we match
2771 else
2772 if Present (Right) then
2773 Result := Choice_Matches (Left, Right);
2774 else
2775 Result := Choices_Match (Left, Alts);
2776 end if;
2778 -- If result is Non_Static, it means that we raise Constraint_Error,
2779 -- since we already tested that the operands were themselves static.
2781 if Result = Non_Static then
2782 Set_Raises_Constraint_Error (N);
2784 -- Otherwise we have our result (flipped if NOT IN case)
2786 else
2787 Fold_Uint
2788 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
2789 Warn_On_Known_Condition (N);
2790 end if;
2791 end if;
2792 end Eval_Membership_Op;
2794 ------------------------
2795 -- Eval_Named_Integer --
2796 ------------------------
2798 procedure Eval_Named_Integer (N : Node_Id) is
2799 begin
2800 Fold_Uint (N,
2801 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2802 end Eval_Named_Integer;
2804 ---------------------
2805 -- Eval_Named_Real --
2806 ---------------------
2808 procedure Eval_Named_Real (N : Node_Id) is
2809 begin
2810 Fold_Ureal (N,
2811 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2812 end Eval_Named_Real;
2814 -------------------
2815 -- Eval_Op_Expon --
2816 -------------------
2818 -- Exponentiation is a static functions, so the result is potentially
2819 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2821 procedure Eval_Op_Expon (N : Node_Id) is
2822 Left : constant Node_Id := Left_Opnd (N);
2823 Right : constant Node_Id := Right_Opnd (N);
2824 Stat : Boolean;
2825 Fold : Boolean;
2827 begin
2828 -- If not foldable we are done
2830 Test_Expression_Is_Foldable
2831 (N, Left, Right, Stat, Fold, CRT_Safe => True);
2833 -- Return if not foldable
2835 if not Fold then
2836 return;
2837 end if;
2839 if Configurable_Run_Time_Mode and not Stat then
2840 return;
2841 end if;
2843 -- Fold exponentiation operation
2845 declare
2846 Right_Int : constant Uint := Expr_Value (Right);
2848 begin
2849 -- Integer case
2851 if Is_Integer_Type (Etype (Left)) then
2852 declare
2853 Left_Int : constant Uint := Expr_Value (Left);
2854 Result : Uint;
2856 begin
2857 -- Exponentiation of an integer raises Constraint_Error for a
2858 -- negative exponent (RM 4.5.6).
2860 if Right_Int < 0 then
2861 Apply_Compile_Time_Constraint_Error
2862 (N, "integer exponent negative", CE_Range_Check_Failed,
2863 Warn => not Stat);
2864 return;
2866 else
2867 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2868 Result := Left_Int ** Right_Int;
2869 else
2870 Result := Left_Int;
2871 end if;
2873 if Is_Modular_Integer_Type (Etype (N)) then
2874 Result := Result mod Modulus (Etype (N));
2875 end if;
2877 Fold_Uint (N, Result, Stat);
2878 end if;
2879 end;
2881 -- Real case
2883 else
2884 declare
2885 Left_Real : constant Ureal := Expr_Value_R (Left);
2887 begin
2888 -- Cannot have a zero base with a negative exponent
2890 if UR_Is_Zero (Left_Real) then
2892 if Right_Int < 0 then
2893 Apply_Compile_Time_Constraint_Error
2894 (N, "zero ** negative integer", CE_Range_Check_Failed,
2895 Warn => not Stat);
2896 return;
2897 else
2898 Fold_Ureal (N, Ureal_0, Stat);
2899 end if;
2901 else
2902 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2903 end if;
2904 end;
2905 end if;
2906 end;
2907 end Eval_Op_Expon;
2909 -----------------
2910 -- Eval_Op_Not --
2911 -----------------
2913 -- The not operation is a static functions, so the result is potentially
2914 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2916 procedure Eval_Op_Not (N : Node_Id) is
2917 Right : constant Node_Id := Right_Opnd (N);
2918 Stat : Boolean;
2919 Fold : Boolean;
2921 begin
2922 -- If not foldable we are done
2924 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2926 if not Fold then
2927 return;
2928 end if;
2930 -- Fold not operation
2932 declare
2933 Rint : constant Uint := Expr_Value (Right);
2934 Typ : constant Entity_Id := Etype (N);
2936 begin
2937 -- Negation is equivalent to subtracting from the modulus minus one.
2938 -- For a binary modulus this is equivalent to the ones-complement of
2939 -- the original value. For a nonbinary modulus this is an arbitrary
2940 -- but consistent definition.
2942 if Is_Modular_Integer_Type (Typ) then
2943 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2944 else pragma Assert (Is_Boolean_Type (Typ));
2945 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2946 end if;
2948 Set_Is_Static_Expression (N, Stat);
2949 end;
2950 end Eval_Op_Not;
2952 -------------------------------
2953 -- Eval_Qualified_Expression --
2954 -------------------------------
2956 -- A qualified expression is potentially static if its subtype mark denotes
2957 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2959 procedure Eval_Qualified_Expression (N : Node_Id) is
2960 Operand : constant Node_Id := Expression (N);
2961 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2963 Stat : Boolean;
2964 Fold : Boolean;
2965 Hex : Boolean;
2967 begin
2968 -- Can only fold if target is string or scalar and subtype is static.
2969 -- Also, do not fold if our parent is an allocator (this is because the
2970 -- qualified expression is really part of the syntactic structure of an
2971 -- allocator, and we do not want to end up with something that
2972 -- corresponds to "new 1" where the 1 is the result of folding a
2973 -- qualified expression).
2975 if not Is_Static_Subtype (Target_Type)
2976 or else Nkind (Parent (N)) = N_Allocator
2977 then
2978 Check_Non_Static_Context (Operand);
2980 -- If operand is known to raise constraint_error, set the flag on the
2981 -- expression so it does not get optimized away.
2983 if Nkind (Operand) = N_Raise_Constraint_Error then
2984 Set_Raises_Constraint_Error (N);
2985 end if;
2987 return;
2988 end if;
2990 -- If not foldable we are done
2992 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2994 if not Fold then
2995 return;
2997 -- Don't try fold if target type has constraint error bounds
2999 elsif not Is_OK_Static_Subtype (Target_Type) then
3000 Set_Raises_Constraint_Error (N);
3001 return;
3002 end if;
3004 -- Here we will fold, save Print_In_Hex indication
3006 Hex := Nkind (Operand) = N_Integer_Literal
3007 and then Print_In_Hex (Operand);
3009 -- Fold the result of qualification
3011 if Is_Discrete_Type (Target_Type) then
3012 Fold_Uint (N, Expr_Value (Operand), Stat);
3014 -- Preserve Print_In_Hex indication
3016 if Hex and then Nkind (N) = N_Integer_Literal then
3017 Set_Print_In_Hex (N);
3018 end if;
3020 elsif Is_Real_Type (Target_Type) then
3021 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3023 else
3024 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3026 if not Stat then
3027 Set_Is_Static_Expression (N, False);
3028 else
3029 Check_String_Literal_Length (N, Target_Type);
3030 end if;
3032 return;
3033 end if;
3035 -- The expression may be foldable but not static
3037 Set_Is_Static_Expression (N, Stat);
3039 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3040 Out_Of_Range (N);
3041 end if;
3042 end Eval_Qualified_Expression;
3044 -----------------------
3045 -- Eval_Real_Literal --
3046 -----------------------
3048 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3049 -- as static by the analyzer. The reason we did it that early is to allow
3050 -- the possibility of turning off the Is_Static_Expression flag after
3051 -- analysis, but before resolution, when integer literals are generated
3052 -- in the expander that do not correspond to static expressions.
3054 procedure Eval_Real_Literal (N : Node_Id) is
3055 PK : constant Node_Kind := Nkind (Parent (N));
3057 begin
3058 -- If the literal appears in a non-expression context and not as part of
3059 -- a number declaration, then it is appearing in a non-static context,
3060 -- so check it.
3062 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3063 Check_Non_Static_Context (N);
3064 end if;
3065 end Eval_Real_Literal;
3067 ------------------------
3068 -- Eval_Relational_Op --
3069 ------------------------
3071 -- Relational operations are static functions, so the result is static if
3072 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3073 -- the result is never static, even if the operands are.
3075 -- However, for internally generated nodes, we allow string equality and
3076 -- inequality to be static. This is because we rewrite A in "ABC" as an
3077 -- equality test A = "ABC", and the former is definitely static.
3079 procedure Eval_Relational_Op (N : Node_Id) is
3080 Left : constant Node_Id := Left_Opnd (N);
3081 Right : constant Node_Id := Right_Opnd (N);
3082 Typ : constant Entity_Id := Etype (Left);
3083 Otype : Entity_Id := Empty;
3084 Result : Boolean;
3086 begin
3087 -- One special case to deal with first. If we can tell that the result
3088 -- will be false because the lengths of one or more index subtypes are
3089 -- compile time known and different, then we can replace the entire
3090 -- result by False. We only do this for one dimensional arrays, because
3091 -- the case of multi-dimensional arrays is rare and too much trouble. If
3092 -- one of the operands is an illegal aggregate, its type might still be
3093 -- an arbitrary composite type, so nothing to do.
3095 if Is_Array_Type (Typ)
3096 and then Typ /= Any_Composite
3097 and then Number_Dimensions (Typ) = 1
3098 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
3099 then
3100 if Raises_Constraint_Error (Left)
3101 or else
3102 Raises_Constraint_Error (Right)
3103 then
3104 return;
3105 end if;
3107 -- OK, we have the case where we may be able to do this fold
3109 Length_Mismatch : declare
3110 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
3111 -- If Op is an expression for a constrained array with a known at
3112 -- compile time length, then Len is set to this (non-negative
3113 -- length). Otherwise Len is set to minus 1.
3115 -----------------------
3116 -- Get_Static_Length --
3117 -----------------------
3119 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
3120 T : Entity_Id;
3122 begin
3123 -- First easy case string literal
3125 if Nkind (Op) = N_String_Literal then
3126 Len := UI_From_Int (String_Length (Strval (Op)));
3127 return;
3128 end if;
3130 -- Second easy case, not constrained subtype, so no length
3132 if not Is_Constrained (Etype (Op)) then
3133 Len := Uint_Minus_1;
3134 return;
3135 end if;
3137 -- General case
3139 T := Etype (First_Index (Etype (Op)));
3141 -- The simple case, both bounds are known at compile time
3143 if Is_Discrete_Type (T)
3144 and then Compile_Time_Known_Value (Type_Low_Bound (T))
3145 and then Compile_Time_Known_Value (Type_High_Bound (T))
3146 then
3147 Len := UI_Max (Uint_0,
3148 Expr_Value (Type_High_Bound (T)) -
3149 Expr_Value (Type_Low_Bound (T)) + 1);
3150 return;
3151 end if;
3153 -- A more complex case, where the bounds are of the form
3154 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
3155 -- either A'First or A'Last (with A an entity name), or X is an
3156 -- entity name, and the two X's are the same and K1 and K2 are
3157 -- known at compile time, in this case, the length can also be
3158 -- computed at compile time, even though the bounds are not
3159 -- known. A common case of this is e.g. (X'First .. X'First+5).
3161 Extract_Length : declare
3162 procedure Decompose_Expr
3163 (Expr : Node_Id;
3164 Ent : out Entity_Id;
3165 Kind : out Character;
3166 Cons : out Uint;
3167 Orig : Boolean := True);
3168 -- Given an expression see if it is of the form given above,
3169 -- X [+/- K]. If so Ent is set to the entity in X, Kind is
3170 -- 'F','L','E' for 'First/'Last/simple entity, and Cons is
3171 -- the value of K. If the expression is not of the required
3172 -- form, Ent is set to Empty.
3174 -- Orig indicates whether Expr is the original expression
3175 -- to consider, or if we are handling a sub-expression
3176 -- (e.g. recursive call to Decompose_Expr).
3178 --------------------
3179 -- Decompose_Expr --
3180 --------------------
3182 procedure Decompose_Expr
3183 (Expr : Node_Id;
3184 Ent : out Entity_Id;
3185 Kind : out Character;
3186 Cons : out Uint;
3187 Orig : Boolean := True)
3189 Exp : Node_Id;
3191 begin
3192 Ent := Empty;
3194 if Nkind (Expr) = N_Op_Add
3195 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3196 then
3197 Exp := Left_Opnd (Expr);
3198 Cons := Expr_Value (Right_Opnd (Expr));
3200 elsif Nkind (Expr) = N_Op_Subtract
3201 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3202 then
3203 Exp := Left_Opnd (Expr);
3204 Cons := -Expr_Value (Right_Opnd (Expr));
3206 -- If the bound is a constant created to remove side
3207 -- effects, recover original expression to see if it has
3208 -- one of the recognizable forms.
3210 elsif Nkind (Expr) = N_Identifier
3211 and then not Comes_From_Source (Entity (Expr))
3212 and then Ekind (Entity (Expr)) = E_Constant
3213 and then
3214 Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3215 then
3216 Exp := Expression (Parent (Entity (Expr)));
3217 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3219 -- If original expression includes an entity, create a
3220 -- reference to it for use below.
3222 if Present (Ent) then
3223 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3224 else
3225 return;
3226 end if;
3228 else
3229 -- Only consider the case of X + 0 for a full
3230 -- expression, and not when recursing, otherwise we
3231 -- may end up with evaluating expressions not known
3232 -- at compile time to 0.
3234 if Orig then
3235 Exp := Expr;
3236 Cons := Uint_0;
3237 else
3238 return;
3239 end if;
3240 end if;
3242 -- At this stage Exp is set to the potential X
3244 if Nkind (Exp) = N_Attribute_Reference then
3245 if Attribute_Name (Exp) = Name_First then
3246 Kind := 'F';
3247 elsif Attribute_Name (Exp) = Name_Last then
3248 Kind := 'L';
3249 else
3250 return;
3251 end if;
3253 Exp := Prefix (Exp);
3255 else
3256 Kind := 'E';
3257 end if;
3259 if Is_Entity_Name (Exp)
3260 and then Present (Entity (Exp))
3261 then
3262 Ent := Entity (Exp);
3263 end if;
3264 end Decompose_Expr;
3266 -- Local Variables
3268 Ent1, Ent2 : Entity_Id;
3269 Kind1, Kind2 : Character;
3270 Cons1, Cons2 : Uint;
3272 -- Start of processing for Extract_Length
3274 begin
3275 Decompose_Expr
3276 (Original_Node (Type_Low_Bound (T)), Ent1, Kind1, Cons1);
3277 Decompose_Expr
3278 (Original_Node (Type_High_Bound (T)), Ent2, Kind2, Cons2);
3280 if Present (Ent1)
3281 and then Kind1 = Kind2
3282 and then Ent1 = Ent2
3283 then
3284 Len := Cons2 - Cons1 + 1;
3285 else
3286 Len := Uint_Minus_1;
3287 end if;
3288 end Extract_Length;
3289 end Get_Static_Length;
3291 -- Local Variables
3293 Len_L : Uint;
3294 Len_R : Uint;
3296 -- Start of processing for Length_Mismatch
3298 begin
3299 Get_Static_Length (Left, Len_L);
3300 Get_Static_Length (Right, Len_R);
3302 if Len_L /= Uint_Minus_1
3303 and then Len_R /= Uint_Minus_1
3304 and then Len_L /= Len_R
3305 then
3306 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
3307 Warn_On_Known_Condition (N);
3308 return;
3309 end if;
3310 end Length_Mismatch;
3311 end if;
3313 declare
3314 Is_Static_Expression : Boolean;
3316 Is_Foldable : Boolean;
3317 pragma Unreferenced (Is_Foldable);
3319 begin
3320 -- Initialize the value of Is_Static_Expression. The value of
3321 -- Is_Foldable returned by Test_Expression_Is_Foldable is not needed
3322 -- since, even when some operand is a variable, we can still perform
3323 -- the static evaluation of the expression in some cases (for
3324 -- example, for a variable of a subtype of Integer we statically
3325 -- know that any value stored in such variable is smaller than
3326 -- Integer'Last).
3328 Test_Expression_Is_Foldable
3329 (N, Left, Right, Is_Static_Expression, Is_Foldable);
3331 -- Only comparisons of scalars can give static results. In
3332 -- particular, comparisons of strings never yield a static
3333 -- result, even if both operands are static strings, except that
3334 -- as noted above, we allow equality/inequality for strings.
3336 if Is_String_Type (Typ)
3337 and then not Comes_From_Source (N)
3338 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3339 then
3340 null;
3342 elsif not Is_Scalar_Type (Typ) then
3343 Is_Static_Expression := False;
3344 Set_Is_Static_Expression (N, False);
3345 end if;
3347 -- For operators on universal numeric types called as functions with
3348 -- an explicit scope, determine appropriate specific numeric type,
3349 -- and diagnose possible ambiguity.
3351 if Is_Universal_Numeric_Type (Etype (Left))
3352 and then
3353 Is_Universal_Numeric_Type (Etype (Right))
3354 then
3355 Otype := Find_Universal_Operator_Type (N);
3356 end if;
3358 -- For static real type expressions, do not use Compile_Time_Compare
3359 -- since it worries about run-time results which are not exact.
3361 if Is_Static_Expression and then Is_Real_Type (Typ) then
3362 declare
3363 Left_Real : constant Ureal := Expr_Value_R (Left);
3364 Right_Real : constant Ureal := Expr_Value_R (Right);
3366 begin
3367 case Nkind (N) is
3368 when N_Op_Eq => Result := (Left_Real = Right_Real);
3369 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3370 when N_Op_Lt => Result := (Left_Real < Right_Real);
3371 when N_Op_Le => Result := (Left_Real <= Right_Real);
3372 when N_Op_Gt => Result := (Left_Real > Right_Real);
3373 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3375 when others =>
3376 raise Program_Error;
3377 end case;
3379 Fold_Uint (N, Test (Result), True);
3380 end;
3382 -- For all other cases, we use Compile_Time_Compare to do the compare
3384 else
3385 declare
3386 CR : constant Compare_Result :=
3387 Compile_Time_Compare
3388 (Left, Right, Assume_Valid => False);
3390 begin
3391 if CR = Unknown then
3392 return;
3393 end if;
3395 case Nkind (N) is
3396 when N_Op_Eq =>
3397 if CR = EQ then
3398 Result := True;
3399 elsif CR = NE or else CR = GT or else CR = LT then
3400 Result := False;
3401 else
3402 return;
3403 end if;
3405 when N_Op_Ne =>
3406 if CR = NE or else CR = GT or else CR = LT then
3407 Result := True;
3408 elsif CR = EQ then
3409 Result := False;
3410 else
3411 return;
3412 end if;
3414 when N_Op_Lt =>
3415 if CR = LT then
3416 Result := True;
3417 elsif CR = EQ or else CR = GT or else CR = GE then
3418 Result := False;
3419 else
3420 return;
3421 end if;
3423 when N_Op_Le =>
3424 if CR = LT or else CR = EQ or else CR = LE then
3425 Result := True;
3426 elsif CR = GT then
3427 Result := False;
3428 else
3429 return;
3430 end if;
3432 when N_Op_Gt =>
3433 if CR = GT then
3434 Result := True;
3435 elsif CR = EQ or else CR = LT or else CR = LE then
3436 Result := False;
3437 else
3438 return;
3439 end if;
3441 when N_Op_Ge =>
3442 if CR = GT or else CR = EQ or else CR = GE then
3443 Result := True;
3444 elsif CR = LT then
3445 Result := False;
3446 else
3447 return;
3448 end if;
3450 when others =>
3451 raise Program_Error;
3452 end case;
3453 end;
3455 Fold_Uint (N, Test (Result), Is_Static_Expression);
3456 end if;
3457 end;
3459 -- For the case of a folded relational operator on a specific numeric
3460 -- type, freeze operand type now.
3462 if Present (Otype) then
3463 Freeze_Before (N, Otype);
3464 end if;
3466 Warn_On_Known_Condition (N);
3467 end Eval_Relational_Op;
3469 ----------------
3470 -- Eval_Shift --
3471 ----------------
3473 -- Shift operations are intrinsic operations that can never be static, so
3474 -- the only processing required is to perform the required check for a non
3475 -- static context for the two operands.
3477 -- Actually we could do some compile time evaluation here some time ???
3479 procedure Eval_Shift (N : Node_Id) is
3480 begin
3481 Check_Non_Static_Context (Left_Opnd (N));
3482 Check_Non_Static_Context (Right_Opnd (N));
3483 end Eval_Shift;
3485 ------------------------
3486 -- Eval_Short_Circuit --
3487 ------------------------
3489 -- A short circuit operation is potentially static if both operands are
3490 -- potentially static (RM 4.9 (13)).
3492 procedure Eval_Short_Circuit (N : Node_Id) is
3493 Kind : constant Node_Kind := Nkind (N);
3494 Left : constant Node_Id := Left_Opnd (N);
3495 Right : constant Node_Id := Right_Opnd (N);
3496 Left_Int : Uint;
3498 Rstat : constant Boolean :=
3499 Is_Static_Expression (Left)
3500 and then
3501 Is_Static_Expression (Right);
3503 begin
3504 -- Short circuit operations are never static in Ada 83
3506 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3507 Check_Non_Static_Context (Left);
3508 Check_Non_Static_Context (Right);
3509 return;
3510 end if;
3512 -- Now look at the operands, we can't quite use the normal call to
3513 -- Test_Expression_Is_Foldable here because short circuit operations
3514 -- are a special case, they can still be foldable, even if the right
3515 -- operand raises constraint error.
3517 -- If either operand is Any_Type, just propagate to result and do not
3518 -- try to fold, this prevents cascaded errors.
3520 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3521 Set_Etype (N, Any_Type);
3522 return;
3524 -- If left operand raises constraint error, then replace node N with
3525 -- the raise constraint error node, and we are obviously not foldable.
3526 -- Is_Static_Expression is set from the two operands in the normal way,
3527 -- and we check the right operand if it is in a non-static context.
3529 elsif Raises_Constraint_Error (Left) then
3530 if not Rstat then
3531 Check_Non_Static_Context (Right);
3532 end if;
3534 Rewrite_In_Raise_CE (N, Left);
3535 Set_Is_Static_Expression (N, Rstat);
3536 return;
3538 -- If the result is not static, then we won't in any case fold
3540 elsif not Rstat then
3541 Check_Non_Static_Context (Left);
3542 Check_Non_Static_Context (Right);
3543 return;
3544 end if;
3546 -- Here the result is static, note that, unlike the normal processing
3547 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3548 -- the right operand raises constraint error, that's because it is not
3549 -- significant if the left operand is decisive.
3551 Set_Is_Static_Expression (N);
3553 -- It does not matter if the right operand raises constraint error if
3554 -- it will not be evaluated. So deal specially with the cases where
3555 -- the right operand is not evaluated. Note that we will fold these
3556 -- cases even if the right operand is non-static, which is fine, but
3557 -- of course in these cases the result is not potentially static.
3559 Left_Int := Expr_Value (Left);
3561 if (Kind = N_And_Then and then Is_False (Left_Int))
3562 or else
3563 (Kind = N_Or_Else and then Is_True (Left_Int))
3564 then
3565 Fold_Uint (N, Left_Int, Rstat);
3566 return;
3567 end if;
3569 -- If first operand not decisive, then it does matter if the right
3570 -- operand raises constraint error, since it will be evaluated, so
3571 -- we simply replace the node with the right operand. Note that this
3572 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3573 -- (both are set to True in Right).
3575 if Raises_Constraint_Error (Right) then
3576 Rewrite_In_Raise_CE (N, Right);
3577 Check_Non_Static_Context (Left);
3578 return;
3579 end if;
3581 -- Otherwise the result depends on the right operand
3583 Fold_Uint (N, Expr_Value (Right), Rstat);
3584 return;
3585 end Eval_Short_Circuit;
3587 ----------------
3588 -- Eval_Slice --
3589 ----------------
3591 -- Slices can never be static, so the only processing required is to check
3592 -- for non-static context if an explicit range is given.
3594 procedure Eval_Slice (N : Node_Id) is
3595 Drange : constant Node_Id := Discrete_Range (N);
3597 begin
3598 if Nkind (Drange) = N_Range then
3599 Check_Non_Static_Context (Low_Bound (Drange));
3600 Check_Non_Static_Context (High_Bound (Drange));
3601 end if;
3603 -- A slice of the form A (subtype), when the subtype is the index of
3604 -- the type of A, is redundant, the slice can be replaced with A, and
3605 -- this is worth a warning.
3607 if Is_Entity_Name (Prefix (N)) then
3608 declare
3609 E : constant Entity_Id := Entity (Prefix (N));
3610 T : constant Entity_Id := Etype (E);
3612 begin
3613 if Ekind (E) = E_Constant
3614 and then Is_Array_Type (T)
3615 and then Is_Entity_Name (Drange)
3616 then
3617 if Is_Entity_Name (Original_Node (First_Index (T)))
3618 and then Entity (Original_Node (First_Index (T)))
3619 = Entity (Drange)
3620 then
3621 if Warn_On_Redundant_Constructs then
3622 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3623 end if;
3625 -- The following might be a useful optimization???
3627 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3628 end if;
3629 end if;
3630 end;
3631 end if;
3632 end Eval_Slice;
3634 -------------------------
3635 -- Eval_String_Literal --
3636 -------------------------
3638 procedure Eval_String_Literal (N : Node_Id) is
3639 Typ : constant Entity_Id := Etype (N);
3640 Bas : constant Entity_Id := Base_Type (Typ);
3641 Xtp : Entity_Id;
3642 Len : Nat;
3643 Lo : Node_Id;
3645 begin
3646 -- Nothing to do if error type (handles cases like default expressions
3647 -- or generics where we have not yet fully resolved the type).
3649 if Bas = Any_Type or else Bas = Any_String then
3650 return;
3651 end if;
3653 -- String literals are static if the subtype is static (RM 4.9(2)), so
3654 -- reset the static expression flag (it was set unconditionally in
3655 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3656 -- the subtype is static by looking at the lower bound.
3658 if Ekind (Typ) = E_String_Literal_Subtype then
3659 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3660 Set_Is_Static_Expression (N, False);
3661 return;
3662 end if;
3664 -- Here if Etype of string literal is normal Etype (not yet possible,
3665 -- but may be possible in future).
3667 elsif not Is_OK_Static_Expression
3668 (Type_Low_Bound (Etype (First_Index (Typ))))
3669 then
3670 Set_Is_Static_Expression (N, False);
3671 return;
3672 end if;
3674 -- If original node was a type conversion, then result if non-static
3676 if Nkind (Original_Node (N)) = N_Type_Conversion then
3677 Set_Is_Static_Expression (N, False);
3678 return;
3679 end if;
3681 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3682 -- if its bounds are outside the index base type and this index type is
3683 -- static. This can happen in only two ways. Either the string literal
3684 -- is too long, or it is null, and the lower bound is type'First. Either
3685 -- way it is the upper bound that is out of range of the index type.
3687 if Ada_Version >= Ada_95 then
3688 if Is_Standard_String_Type (Bas) then
3689 Xtp := Standard_Positive;
3690 else
3691 Xtp := Etype (First_Index (Bas));
3692 end if;
3694 if Ekind (Typ) = E_String_Literal_Subtype then
3695 Lo := String_Literal_Low_Bound (Typ);
3696 else
3697 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3698 end if;
3700 -- Check for string too long
3702 Len := String_Length (Strval (N));
3704 if UI_From_Int (Len) > String_Type_Len (Bas) then
3706 -- Issue message. Note that this message is a warning if the
3707 -- string literal is not marked as static (happens in some cases
3708 -- of folding strings known at compile time, but not static).
3709 -- Furthermore in such cases, we reword the message, since there
3710 -- is no string literal in the source program.
3712 if Is_Static_Expression (N) then
3713 Apply_Compile_Time_Constraint_Error
3714 (N, "string literal too long for}", CE_Length_Check_Failed,
3715 Ent => Bas,
3716 Typ => First_Subtype (Bas));
3717 else
3718 Apply_Compile_Time_Constraint_Error
3719 (N, "string value too long for}", CE_Length_Check_Failed,
3720 Ent => Bas,
3721 Typ => First_Subtype (Bas),
3722 Warn => True);
3723 end if;
3725 -- Test for null string not allowed
3727 elsif Len = 0
3728 and then not Is_Generic_Type (Xtp)
3729 and then
3730 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3731 then
3732 -- Same specialization of message
3734 if Is_Static_Expression (N) then
3735 Apply_Compile_Time_Constraint_Error
3736 (N, "null string literal not allowed for}",
3737 CE_Length_Check_Failed,
3738 Ent => Bas,
3739 Typ => First_Subtype (Bas));
3740 else
3741 Apply_Compile_Time_Constraint_Error
3742 (N, "null string value not allowed for}",
3743 CE_Length_Check_Failed,
3744 Ent => Bas,
3745 Typ => First_Subtype (Bas),
3746 Warn => True);
3747 end if;
3748 end if;
3749 end if;
3750 end Eval_String_Literal;
3752 --------------------------
3753 -- Eval_Type_Conversion --
3754 --------------------------
3756 -- A type conversion is potentially static if its subtype mark is for a
3757 -- static scalar subtype, and its operand expression is potentially static
3758 -- (RM 4.9(10)).
3760 procedure Eval_Type_Conversion (N : Node_Id) is
3761 Operand : constant Node_Id := Expression (N);
3762 Source_Type : constant Entity_Id := Etype (Operand);
3763 Target_Type : constant Entity_Id := Etype (N);
3765 Stat : Boolean;
3766 Fold : Boolean;
3768 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3769 -- Returns true if type T is an integer type, or if it is a fixed-point
3770 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3771 -- on the conversion node).
3773 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3774 -- Returns true if type T is a floating-point type, or if it is a
3775 -- fixed-point type that is not to be treated as an integer (i.e. the
3776 -- flag Conversion_OK is not set on the conversion node).
3778 ------------------------------
3779 -- To_Be_Treated_As_Integer --
3780 ------------------------------
3782 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3783 begin
3784 return
3785 Is_Integer_Type (T)
3786 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3787 end To_Be_Treated_As_Integer;
3789 ---------------------------
3790 -- To_Be_Treated_As_Real --
3791 ---------------------------
3793 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3794 begin
3795 return
3796 Is_Floating_Point_Type (T)
3797 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3798 end To_Be_Treated_As_Real;
3800 -- Start of processing for Eval_Type_Conversion
3802 begin
3803 -- Cannot fold if target type is non-static or if semantic error
3805 if not Is_Static_Subtype (Target_Type) then
3806 Check_Non_Static_Context (Operand);
3807 return;
3808 elsif Error_Posted (N) then
3809 return;
3810 end if;
3812 -- If not foldable we are done
3814 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3816 if not Fold then
3817 return;
3819 -- Don't try fold if target type has constraint error bounds
3821 elsif not Is_OK_Static_Subtype (Target_Type) then
3822 Set_Raises_Constraint_Error (N);
3823 return;
3824 end if;
3826 -- Remaining processing depends on operand types. Note that in the
3827 -- following type test, fixed-point counts as real unless the flag
3828 -- Conversion_OK is set, in which case it counts as integer.
3830 -- Fold conversion, case of string type. The result is not static
3832 if Is_String_Type (Target_Type) then
3833 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3834 return;
3836 -- Fold conversion, case of integer target type
3838 elsif To_Be_Treated_As_Integer (Target_Type) then
3839 declare
3840 Result : Uint;
3842 begin
3843 -- Integer to integer conversion
3845 if To_Be_Treated_As_Integer (Source_Type) then
3846 Result := Expr_Value (Operand);
3848 -- Real to integer conversion
3850 else
3851 Result := UR_To_Uint (Expr_Value_R (Operand));
3852 end if;
3854 -- If fixed-point type (Conversion_OK must be set), then the
3855 -- result is logically an integer, but we must replace the
3856 -- conversion with the corresponding real literal, since the
3857 -- type from a semantic point of view is still fixed-point.
3859 if Is_Fixed_Point_Type (Target_Type) then
3860 Fold_Ureal
3861 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3863 -- Otherwise result is integer literal
3865 else
3866 Fold_Uint (N, Result, Stat);
3867 end if;
3868 end;
3870 -- Fold conversion, case of real target type
3872 elsif To_Be_Treated_As_Real (Target_Type) then
3873 declare
3874 Result : Ureal;
3876 begin
3877 if To_Be_Treated_As_Real (Source_Type) then
3878 Result := Expr_Value_R (Operand);
3879 else
3880 Result := UR_From_Uint (Expr_Value (Operand));
3881 end if;
3883 Fold_Ureal (N, Result, Stat);
3884 end;
3886 -- Enumeration types
3888 else
3889 Fold_Uint (N, Expr_Value (Operand), Stat);
3890 end if;
3892 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3893 Out_Of_Range (N);
3894 end if;
3896 end Eval_Type_Conversion;
3898 -------------------
3899 -- Eval_Unary_Op --
3900 -------------------
3902 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3903 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3905 procedure Eval_Unary_Op (N : Node_Id) is
3906 Right : constant Node_Id := Right_Opnd (N);
3907 Otype : Entity_Id := Empty;
3908 Stat : Boolean;
3909 Fold : Boolean;
3911 begin
3912 -- If not foldable we are done
3914 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3916 if not Fold then
3917 return;
3918 end if;
3920 if Etype (Right) = Universal_Integer
3921 or else
3922 Etype (Right) = Universal_Real
3923 then
3924 Otype := Find_Universal_Operator_Type (N);
3925 end if;
3927 -- Fold for integer case
3929 if Is_Integer_Type (Etype (N)) then
3930 declare
3931 Rint : constant Uint := Expr_Value (Right);
3932 Result : Uint;
3934 begin
3935 -- In the case of modular unary plus and abs there is no need
3936 -- to adjust the result of the operation since if the original
3937 -- operand was in bounds the result will be in the bounds of the
3938 -- modular type. However, in the case of modular unary minus the
3939 -- result may go out of the bounds of the modular type and needs
3940 -- adjustment.
3942 if Nkind (N) = N_Op_Plus then
3943 Result := Rint;
3945 elsif Nkind (N) = N_Op_Minus then
3946 if Is_Modular_Integer_Type (Etype (N)) then
3947 Result := (-Rint) mod Modulus (Etype (N));
3948 else
3949 Result := (-Rint);
3950 end if;
3952 else
3953 pragma Assert (Nkind (N) = N_Op_Abs);
3954 Result := abs Rint;
3955 end if;
3957 Fold_Uint (N, Result, Stat);
3958 end;
3960 -- Fold for real case
3962 elsif Is_Real_Type (Etype (N)) then
3963 declare
3964 Rreal : constant Ureal := Expr_Value_R (Right);
3965 Result : Ureal;
3967 begin
3968 if Nkind (N) = N_Op_Plus then
3969 Result := Rreal;
3970 elsif Nkind (N) = N_Op_Minus then
3971 Result := UR_Negate (Rreal);
3972 else
3973 pragma Assert (Nkind (N) = N_Op_Abs);
3974 Result := abs Rreal;
3975 end if;
3977 Fold_Ureal (N, Result, Stat);
3978 end;
3979 end if;
3981 -- If the operator was resolved to a specific type, make sure that type
3982 -- is frozen even if the expression is folded into a literal (which has
3983 -- a universal type).
3985 if Present (Otype) then
3986 Freeze_Before (N, Otype);
3987 end if;
3988 end Eval_Unary_Op;
3990 -------------------------------
3991 -- Eval_Unchecked_Conversion --
3992 -------------------------------
3994 -- Unchecked conversions can never be static, so the only required
3995 -- processing is to check for a non-static context for the operand.
3997 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3998 begin
3999 Check_Non_Static_Context (Expression (N));
4000 end Eval_Unchecked_Conversion;
4002 --------------------
4003 -- Expr_Rep_Value --
4004 --------------------
4006 function Expr_Rep_Value (N : Node_Id) return Uint is
4007 Kind : constant Node_Kind := Nkind (N);
4008 Ent : Entity_Id;
4010 begin
4011 if Is_Entity_Name (N) then
4012 Ent := Entity (N);
4014 -- An enumeration literal that was either in the source or created
4015 -- as a result of static evaluation.
4017 if Ekind (Ent) = E_Enumeration_Literal then
4018 return Enumeration_Rep (Ent);
4020 -- A user defined static constant
4022 else
4023 pragma Assert (Ekind (Ent) = E_Constant);
4024 return Expr_Rep_Value (Constant_Value (Ent));
4025 end if;
4027 -- An integer literal that was either in the source or created as a
4028 -- result of static evaluation.
4030 elsif Kind = N_Integer_Literal then
4031 return Intval (N);
4033 -- A real literal for a fixed-point type. This must be the fixed-point
4034 -- case, either the literal is of a fixed-point type, or it is a bound
4035 -- of a fixed-point type, with type universal real. In either case we
4036 -- obtain the desired value from Corresponding_Integer_Value.
4038 elsif Kind = N_Real_Literal then
4039 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4040 return Corresponding_Integer_Value (N);
4042 -- Otherwise must be character literal
4044 else
4045 pragma Assert (Kind = N_Character_Literal);
4046 Ent := Entity (N);
4048 -- Since Character literals of type Standard.Character don't have any
4049 -- defining character literals built for them, they do not have their
4050 -- Entity set, so just use their Char code. Otherwise for user-
4051 -- defined character literals use their Pos value as usual which is
4052 -- the same as the Rep value.
4054 if No (Ent) then
4055 return Char_Literal_Value (N);
4056 else
4057 return Enumeration_Rep (Ent);
4058 end if;
4059 end if;
4060 end Expr_Rep_Value;
4062 ----------------
4063 -- Expr_Value --
4064 ----------------
4066 function Expr_Value (N : Node_Id) return Uint is
4067 Kind : constant Node_Kind := Nkind (N);
4068 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4069 Ent : Entity_Id;
4070 Val : Uint;
4072 begin
4073 -- If already in cache, then we know it's compile time known and we can
4074 -- return the value that was previously stored in the cache since
4075 -- compile time known values cannot change.
4077 if CV_Ent.N = N then
4078 return CV_Ent.V;
4079 end if;
4081 -- Otherwise proceed to test value
4083 if Is_Entity_Name (N) then
4084 Ent := Entity (N);
4086 -- An enumeration literal that was either in the source or created as
4087 -- a result of static evaluation.
4089 if Ekind (Ent) = E_Enumeration_Literal then
4090 Val := Enumeration_Pos (Ent);
4092 -- A user defined static constant
4094 else
4095 pragma Assert (Ekind (Ent) = E_Constant);
4096 Val := Expr_Value (Constant_Value (Ent));
4097 end if;
4099 -- An integer literal that was either in the source or created as a
4100 -- result of static evaluation.
4102 elsif Kind = N_Integer_Literal then
4103 Val := Intval (N);
4105 -- A real literal for a fixed-point type. This must be the fixed-point
4106 -- case, either the literal is of a fixed-point type, or it is a bound
4107 -- of a fixed-point type, with type universal real. In either case we
4108 -- obtain the desired value from Corresponding_Integer_Value.
4110 elsif Kind = N_Real_Literal then
4111 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4112 Val := Corresponding_Integer_Value (N);
4114 -- Otherwise must be character literal
4116 else
4117 pragma Assert (Kind = N_Character_Literal);
4118 Ent := Entity (N);
4120 -- Since Character literals of type Standard.Character don't
4121 -- have any defining character literals built for them, they
4122 -- do not have their Entity set, so just use their Char
4123 -- code. Otherwise for user-defined character literals use
4124 -- their Pos value as usual.
4126 if No (Ent) then
4127 Val := Char_Literal_Value (N);
4128 else
4129 Val := Enumeration_Pos (Ent);
4130 end if;
4131 end if;
4133 -- Come here with Val set to value to be returned, set cache
4135 CV_Ent.N := N;
4136 CV_Ent.V := Val;
4137 return Val;
4138 end Expr_Value;
4140 ------------------
4141 -- Expr_Value_E --
4142 ------------------
4144 function Expr_Value_E (N : Node_Id) return Entity_Id is
4145 Ent : constant Entity_Id := Entity (N);
4146 begin
4147 if Ekind (Ent) = E_Enumeration_Literal then
4148 return Ent;
4149 else
4150 pragma Assert (Ekind (Ent) = E_Constant);
4151 return Expr_Value_E (Constant_Value (Ent));
4152 end if;
4153 end Expr_Value_E;
4155 ------------------
4156 -- Expr_Value_R --
4157 ------------------
4159 function Expr_Value_R (N : Node_Id) return Ureal is
4160 Kind : constant Node_Kind := Nkind (N);
4161 Ent : Entity_Id;
4163 begin
4164 if Kind = N_Real_Literal then
4165 return Realval (N);
4167 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4168 Ent := Entity (N);
4169 pragma Assert (Ekind (Ent) = E_Constant);
4170 return Expr_Value_R (Constant_Value (Ent));
4172 elsif Kind = N_Integer_Literal then
4173 return UR_From_Uint (Expr_Value (N));
4175 -- Here, we have a node that cannot be interpreted as a compile time
4176 -- constant. That is definitely an error.
4178 else
4179 raise Program_Error;
4180 end if;
4181 end Expr_Value_R;
4183 ------------------
4184 -- Expr_Value_S --
4185 ------------------
4187 function Expr_Value_S (N : Node_Id) return Node_Id is
4188 begin
4189 if Nkind (N) = N_String_Literal then
4190 return N;
4191 else
4192 pragma Assert (Ekind (Entity (N)) = E_Constant);
4193 return Expr_Value_S (Constant_Value (Entity (N)));
4194 end if;
4195 end Expr_Value_S;
4197 ----------------------------------
4198 -- Find_Universal_Operator_Type --
4199 ----------------------------------
4201 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4202 PN : constant Node_Id := Parent (N);
4203 Call : constant Node_Id := Original_Node (N);
4204 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4206 Is_Fix : constant Boolean :=
4207 Nkind (N) in N_Binary_Op
4208 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4209 -- A mixed-mode operation in this context indicates the presence of
4210 -- fixed-point type in the designated package.
4212 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4213 -- Case where N is a relational (or membership) operator (else it is an
4214 -- arithmetic one).
4216 In_Membership : constant Boolean :=
4217 Nkind (PN) in N_Membership_Test
4218 and then
4219 Nkind (Right_Opnd (PN)) = N_Range
4220 and then
4221 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4222 and then
4223 Is_Universal_Numeric_Type
4224 (Etype (Low_Bound (Right_Opnd (PN))))
4225 and then
4226 Is_Universal_Numeric_Type
4227 (Etype (High_Bound (Right_Opnd (PN))));
4228 -- Case where N is part of a membership test with a universal range
4230 E : Entity_Id;
4231 Pack : Entity_Id;
4232 Typ1 : Entity_Id := Empty;
4233 Priv_E : Entity_Id;
4235 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4236 -- Check whether one operand is a mixed-mode operation that requires the
4237 -- presence of a fixed-point type. Given that all operands are universal
4238 -- and have been constant-folded, retrieve the original function call.
4240 ---------------------------
4241 -- Is_Mixed_Mode_Operand --
4242 ---------------------------
4244 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4245 Onod : constant Node_Id := Original_Node (Op);
4246 begin
4247 return Nkind (Onod) = N_Function_Call
4248 and then Present (Next_Actual (First_Actual (Onod)))
4249 and then Etype (First_Actual (Onod)) /=
4250 Etype (Next_Actual (First_Actual (Onod)));
4251 end Is_Mixed_Mode_Operand;
4253 -- Start of processing for Find_Universal_Operator_Type
4255 begin
4256 if Nkind (Call) /= N_Function_Call
4257 or else Nkind (Name (Call)) /= N_Expanded_Name
4258 then
4259 return Empty;
4261 -- There are several cases where the context does not imply the type of
4262 -- the operands:
4263 -- - the universal expression appears in a type conversion;
4264 -- - the expression is a relational operator applied to universal
4265 -- operands;
4266 -- - the expression is a membership test with a universal operand
4267 -- and a range with universal bounds.
4269 elsif Nkind (Parent (N)) = N_Type_Conversion
4270 or else Is_Relational
4271 or else In_Membership
4272 then
4273 Pack := Entity (Prefix (Name (Call)));
4275 -- If the prefix is a package declared elsewhere, iterate over its
4276 -- visible entities, otherwise iterate over all declarations in the
4277 -- designated scope.
4279 if Ekind (Pack) = E_Package
4280 and then not In_Open_Scopes (Pack)
4281 then
4282 Priv_E := First_Private_Entity (Pack);
4283 else
4284 Priv_E := Empty;
4285 end if;
4287 Typ1 := Empty;
4288 E := First_Entity (Pack);
4289 while Present (E) and then E /= Priv_E loop
4290 if Is_Numeric_Type (E)
4291 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4292 and then Comes_From_Source (E)
4293 and then Is_Integer_Type (E) = Is_Int
4294 and then (Nkind (N) in N_Unary_Op
4295 or else Is_Relational
4296 or else Is_Fixed_Point_Type (E) = Is_Fix)
4297 then
4298 if No (Typ1) then
4299 Typ1 := E;
4301 -- Before emitting an error, check for the presence of a
4302 -- mixed-mode operation that specifies a fixed point type.
4304 elsif Is_Relational
4305 and then
4306 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4307 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4308 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4310 then
4311 if Is_Fixed_Point_Type (E) then
4312 Typ1 := E;
4313 end if;
4315 else
4316 -- More than one type of the proper class declared in P
4318 Error_Msg_N ("ambiguous operation", N);
4319 Error_Msg_Sloc := Sloc (Typ1);
4320 Error_Msg_N ("\possible interpretation (inherited)#", N);
4321 Error_Msg_Sloc := Sloc (E);
4322 Error_Msg_N ("\possible interpretation (inherited)#", N);
4323 return Empty;
4324 end if;
4325 end if;
4327 Next_Entity (E);
4328 end loop;
4329 end if;
4331 return Typ1;
4332 end Find_Universal_Operator_Type;
4334 --------------------------
4335 -- Flag_Non_Static_Expr --
4336 --------------------------
4338 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4339 begin
4340 if Error_Posted (Expr) and then not All_Errors_Mode then
4341 return;
4342 else
4343 Error_Msg_F (Msg, Expr);
4344 Why_Not_Static (Expr);
4345 end if;
4346 end Flag_Non_Static_Expr;
4348 --------------
4349 -- Fold_Str --
4350 --------------
4352 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4353 Loc : constant Source_Ptr := Sloc (N);
4354 Typ : constant Entity_Id := Etype (N);
4356 begin
4357 if Raises_Constraint_Error (N) then
4358 Set_Is_Static_Expression (N, Static);
4359 return;
4360 end if;
4362 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4364 -- We now have the literal with the right value, both the actual type
4365 -- and the expected type of this literal are taken from the expression
4366 -- that was evaluated. So now we do the Analyze and Resolve.
4368 -- Note that we have to reset Is_Static_Expression both after the
4369 -- analyze step (because Resolve will evaluate the literal, which
4370 -- will cause semantic errors if it is marked as static), and after
4371 -- the Resolve step (since Resolve in some cases resets this flag).
4373 Analyze (N);
4374 Set_Is_Static_Expression (N, Static);
4375 Set_Etype (N, Typ);
4376 Resolve (N);
4377 Set_Is_Static_Expression (N, Static);
4378 end Fold_Str;
4380 ---------------
4381 -- Fold_Uint --
4382 ---------------
4384 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4385 Loc : constant Source_Ptr := Sloc (N);
4386 Typ : Entity_Id := Etype (N);
4387 Ent : Entity_Id;
4389 begin
4390 if Raises_Constraint_Error (N) then
4391 Set_Is_Static_Expression (N, Static);
4392 return;
4393 end if;
4395 -- If we are folding a named number, retain the entity in the literal,
4396 -- for ASIS use.
4398 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
4399 Ent := Entity (N);
4400 else
4401 Ent := Empty;
4402 end if;
4404 if Is_Private_Type (Typ) then
4405 Typ := Full_View (Typ);
4406 end if;
4408 -- For a result of type integer, substitute an N_Integer_Literal node
4409 -- for the result of the compile time evaluation of the expression.
4410 -- For ASIS use, set a link to the original named number when not in
4411 -- a generic context.
4413 if Is_Integer_Type (Typ) then
4414 Rewrite (N, Make_Integer_Literal (Loc, Val));
4415 Set_Original_Entity (N, Ent);
4417 -- Otherwise we have an enumeration type, and we substitute either
4418 -- an N_Identifier or N_Character_Literal to represent the enumeration
4419 -- literal corresponding to the given value, which must always be in
4420 -- range, because appropriate tests have already been made for this.
4422 else pragma Assert (Is_Enumeration_Type (Typ));
4423 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4424 end if;
4426 -- We now have the literal with the right value, both the actual type
4427 -- and the expected type of this literal are taken from the expression
4428 -- that was evaluated. So now we do the Analyze and Resolve.
4430 -- Note that we have to reset Is_Static_Expression both after the
4431 -- analyze step (because Resolve will evaluate the literal, which
4432 -- will cause semantic errors if it is marked as static), and after
4433 -- the Resolve step (since Resolve in some cases sets this flag).
4435 Analyze (N);
4436 Set_Is_Static_Expression (N, Static);
4437 Set_Etype (N, Typ);
4438 Resolve (N);
4439 Set_Is_Static_Expression (N, Static);
4440 end Fold_Uint;
4442 ----------------
4443 -- Fold_Ureal --
4444 ----------------
4446 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4447 Loc : constant Source_Ptr := Sloc (N);
4448 Typ : constant Entity_Id := Etype (N);
4449 Ent : Entity_Id;
4451 begin
4452 if Raises_Constraint_Error (N) then
4453 Set_Is_Static_Expression (N, Static);
4454 return;
4455 end if;
4457 -- If we are folding a named number, retain the entity in the literal,
4458 -- for ASIS use.
4460 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
4461 Ent := Entity (N);
4462 else
4463 Ent := Empty;
4464 end if;
4466 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4468 -- Set link to original named number, for ASIS use
4470 Set_Original_Entity (N, Ent);
4472 -- We now have the literal with the right value, both the actual type
4473 -- and the expected type of this literal are taken from the expression
4474 -- that was evaluated. So now we do the Analyze and Resolve.
4476 -- Note that we have to reset Is_Static_Expression both after the
4477 -- analyze step (because Resolve will evaluate the literal, which
4478 -- will cause semantic errors if it is marked as static), and after
4479 -- the Resolve step (since Resolve in some cases sets this flag).
4481 Analyze (N);
4482 Set_Is_Static_Expression (N, Static);
4483 Set_Etype (N, Typ);
4484 Resolve (N);
4485 Set_Is_Static_Expression (N, Static);
4486 end Fold_Ureal;
4488 ---------------
4489 -- From_Bits --
4490 ---------------
4492 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4493 V : Uint := Uint_0;
4495 begin
4496 for J in 0 .. B'Last loop
4497 if B (J) then
4498 V := V + 2 ** J;
4499 end if;
4500 end loop;
4502 if Non_Binary_Modulus (T) then
4503 V := V mod Modulus (T);
4504 end if;
4506 return V;
4507 end From_Bits;
4509 --------------------
4510 -- Get_String_Val --
4511 --------------------
4513 function Get_String_Val (N : Node_Id) return Node_Id is
4514 begin
4515 if Nkind_In (N, N_String_Literal, N_Character_Literal) then
4516 return N;
4517 else
4518 pragma Assert (Is_Entity_Name (N));
4519 return Get_String_Val (Constant_Value (Entity (N)));
4520 end if;
4521 end Get_String_Val;
4523 ----------------
4524 -- Initialize --
4525 ----------------
4527 procedure Initialize is
4528 begin
4529 CV_Cache := (others => (Node_High_Bound, Uint_0));
4530 end Initialize;
4532 --------------------
4533 -- In_Subrange_Of --
4534 --------------------
4536 function In_Subrange_Of
4537 (T1 : Entity_Id;
4538 T2 : Entity_Id;
4539 Fixed_Int : Boolean := False) return Boolean
4541 L1 : Node_Id;
4542 H1 : Node_Id;
4544 L2 : Node_Id;
4545 H2 : Node_Id;
4547 begin
4548 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4549 return True;
4551 -- Never in range if both types are not scalar. Don't know if this can
4552 -- actually happen, but just in case.
4554 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4555 return False;
4557 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4558 -- definitely not compatible with T2.
4560 elsif Is_Floating_Point_Type (T1)
4561 and then Has_Infinities (T1)
4562 and then Is_Floating_Point_Type (T2)
4563 and then not Has_Infinities (T2)
4564 then
4565 return False;
4567 else
4568 L1 := Type_Low_Bound (T1);
4569 H1 := Type_High_Bound (T1);
4571 L2 := Type_Low_Bound (T2);
4572 H2 := Type_High_Bound (T2);
4574 -- Check bounds to see if comparison possible at compile time
4576 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4577 and then
4578 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4579 then
4580 return True;
4581 end if;
4583 -- If bounds not comparable at compile time, then the bounds of T2
4584 -- must be compile time known or we cannot answer the query.
4586 if not Compile_Time_Known_Value (L2)
4587 or else not Compile_Time_Known_Value (H2)
4588 then
4589 return False;
4590 end if;
4592 -- If the bounds of T1 are know at compile time then use these
4593 -- ones, otherwise use the bounds of the base type (which are of
4594 -- course always static).
4596 if not Compile_Time_Known_Value (L1) then
4597 L1 := Type_Low_Bound (Base_Type (T1));
4598 end if;
4600 if not Compile_Time_Known_Value (H1) then
4601 H1 := Type_High_Bound (Base_Type (T1));
4602 end if;
4604 -- Fixed point types should be considered as such only if
4605 -- flag Fixed_Int is set to False.
4607 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4608 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4609 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4610 then
4611 return
4612 Expr_Value_R (L2) <= Expr_Value_R (L1)
4613 and then
4614 Expr_Value_R (H2) >= Expr_Value_R (H1);
4616 else
4617 return
4618 Expr_Value (L2) <= Expr_Value (L1)
4619 and then
4620 Expr_Value (H2) >= Expr_Value (H1);
4622 end if;
4623 end if;
4625 -- If any exception occurs, it means that we have some bug in the compiler
4626 -- possibly triggered by a previous error, or by some unforeseen peculiar
4627 -- occurrence. However, this is only an optimization attempt, so there is
4628 -- really no point in crashing the compiler. Instead we just decide, too
4629 -- bad, we can't figure out the answer in this case after all.
4631 exception
4632 when others =>
4634 -- Debug flag K disables this behavior (useful for debugging)
4636 if Debug_Flag_K then
4637 raise;
4638 else
4639 return False;
4640 end if;
4641 end In_Subrange_Of;
4643 -----------------
4644 -- Is_In_Range --
4645 -----------------
4647 function Is_In_Range
4648 (N : Node_Id;
4649 Typ : Entity_Id;
4650 Assume_Valid : Boolean := False;
4651 Fixed_Int : Boolean := False;
4652 Int_Real : Boolean := False) return Boolean
4654 begin
4655 return
4656 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
4657 end Is_In_Range;
4659 -------------------
4660 -- Is_Null_Range --
4661 -------------------
4663 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4664 Typ : constant Entity_Id := Etype (Lo);
4666 begin
4667 if not Compile_Time_Known_Value (Lo)
4668 or else not Compile_Time_Known_Value (Hi)
4669 then
4670 return False;
4671 end if;
4673 if Is_Discrete_Type (Typ) then
4674 return Expr_Value (Lo) > Expr_Value (Hi);
4675 else pragma Assert (Is_Real_Type (Typ));
4676 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4677 end if;
4678 end Is_Null_Range;
4680 -------------------------
4681 -- Is_OK_Static_Choice --
4682 -------------------------
4684 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
4685 begin
4686 -- Check various possibilities for choice
4688 -- Note: for membership tests, we test more cases than are possible
4689 -- (in particular subtype indication), but it doesn't matter because
4690 -- it just won't occur (we have already done a syntax check).
4692 if Nkind (Choice) = N_Others_Choice then
4693 return True;
4695 elsif Nkind (Choice) = N_Range then
4696 return Is_OK_Static_Range (Choice);
4698 elsif Nkind (Choice) = N_Subtype_Indication
4699 or else
4700 (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4701 then
4702 return Is_OK_Static_Subtype (Etype (Choice));
4704 else
4705 return Is_OK_Static_Expression (Choice);
4706 end if;
4707 end Is_OK_Static_Choice;
4709 ------------------------------
4710 -- Is_OK_Static_Choice_List --
4711 ------------------------------
4713 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
4714 Choice : Node_Id;
4716 begin
4717 if not Is_Static_Choice_List (Choices) then
4718 return False;
4719 end if;
4721 Choice := First (Choices);
4722 while Present (Choice) loop
4723 if not Is_OK_Static_Choice (Choice) then
4724 Set_Raises_Constraint_Error (Choice);
4725 return False;
4726 end if;
4728 Next (Choice);
4729 end loop;
4731 return True;
4732 end Is_OK_Static_Choice_List;
4734 -----------------------------
4735 -- Is_OK_Static_Expression --
4736 -----------------------------
4738 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4739 begin
4740 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
4741 end Is_OK_Static_Expression;
4743 ------------------------
4744 -- Is_OK_Static_Range --
4745 ------------------------
4747 -- A static range is a range whose bounds are static expressions, or a
4748 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4749 -- We have already converted range attribute references, so we get the
4750 -- "or" part of this rule without needing a special test.
4752 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4753 begin
4754 return Is_OK_Static_Expression (Low_Bound (N))
4755 and then Is_OK_Static_Expression (High_Bound (N));
4756 end Is_OK_Static_Range;
4758 --------------------------
4759 -- Is_OK_Static_Subtype --
4760 --------------------------
4762 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4763 -- neither bound raises constraint error when evaluated.
4765 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4766 Base_T : constant Entity_Id := Base_Type (Typ);
4767 Anc_Subt : Entity_Id;
4769 begin
4770 -- First a quick check on the non static subtype flag. As described
4771 -- in further detail in Einfo, this flag is not decisive in all cases,
4772 -- but if it is set, then the subtype is definitely non-static.
4774 if Is_Non_Static_Subtype (Typ) then
4775 return False;
4776 end if;
4778 Anc_Subt := Ancestor_Subtype (Typ);
4780 if Anc_Subt = Empty then
4781 Anc_Subt := Base_T;
4782 end if;
4784 if Is_Generic_Type (Root_Type (Base_T))
4785 or else Is_Generic_Actual_Type (Base_T)
4786 then
4787 return False;
4789 -- String types
4791 elsif Is_String_Type (Typ) then
4792 return
4793 Ekind (Typ) = E_String_Literal_Subtype
4794 or else
4795 (Is_OK_Static_Subtype (Component_Type (Typ))
4796 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4798 -- Scalar types
4800 elsif Is_Scalar_Type (Typ) then
4801 if Base_T = Typ then
4802 return True;
4804 else
4805 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4806 -- Get_Type_{Low,High}_Bound.
4808 return Is_OK_Static_Subtype (Anc_Subt)
4809 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4810 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4811 end if;
4813 -- Types other than string and scalar types are never static
4815 else
4816 return False;
4817 end if;
4818 end Is_OK_Static_Subtype;
4820 ---------------------
4821 -- Is_Out_Of_Range --
4822 ---------------------
4824 function Is_Out_Of_Range
4825 (N : Node_Id;
4826 Typ : Entity_Id;
4827 Assume_Valid : Boolean := False;
4828 Fixed_Int : Boolean := False;
4829 Int_Real : Boolean := False) return Boolean
4831 begin
4832 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
4833 Out_Of_Range;
4834 end Is_Out_Of_Range;
4836 ----------------------
4837 -- Is_Static_Choice --
4838 ----------------------
4840 function Is_Static_Choice (Choice : Node_Id) return Boolean is
4841 begin
4842 -- Check various possibilities for choice
4844 -- Note: for membership tests, we test more cases than are possible
4845 -- (in particular subtype indication), but it doesn't matter because
4846 -- it just won't occur (we have already done a syntax check).
4848 if Nkind (Choice) = N_Others_Choice then
4849 return True;
4851 elsif Nkind (Choice) = N_Range then
4852 return Is_Static_Range (Choice);
4854 elsif Nkind (Choice) = N_Subtype_Indication
4855 or else
4856 (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4857 then
4858 return Is_Static_Subtype (Etype (Choice));
4860 else
4861 return Is_Static_Expression (Choice);
4862 end if;
4863 end Is_Static_Choice;
4865 ---------------------------
4866 -- Is_Static_Choice_List --
4867 ---------------------------
4869 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
4870 Choice : Node_Id;
4872 begin
4873 Choice := First (Choices);
4874 while Present (Choice) loop
4875 if not Is_Static_Choice (Choice) then
4876 return False;
4877 end if;
4879 Next (Choice);
4880 end loop;
4882 return True;
4883 end Is_Static_Choice_List;
4885 ---------------------
4886 -- Is_Static_Range --
4887 ---------------------
4889 -- A static range is a range whose bounds are static expressions, or a
4890 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4891 -- We have already converted range attribute references, so we get the
4892 -- "or" part of this rule without needing a special test.
4894 function Is_Static_Range (N : Node_Id) return Boolean is
4895 begin
4896 return Is_Static_Expression (Low_Bound (N))
4897 and then
4898 Is_Static_Expression (High_Bound (N));
4899 end Is_Static_Range;
4901 -----------------------
4902 -- Is_Static_Subtype --
4903 -----------------------
4905 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4907 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
4908 Base_T : constant Entity_Id := Base_Type (Typ);
4909 Anc_Subt : Entity_Id;
4911 begin
4912 -- First a quick check on the non static subtype flag. As described
4913 -- in further detail in Einfo, this flag is not decisive in all cases,
4914 -- but if it is set, then the subtype is definitely non-static.
4916 if Is_Non_Static_Subtype (Typ) then
4917 return False;
4918 end if;
4920 Anc_Subt := Ancestor_Subtype (Typ);
4922 if Anc_Subt = Empty then
4923 Anc_Subt := Base_T;
4924 end if;
4926 if Is_Generic_Type (Root_Type (Base_T))
4927 or else Is_Generic_Actual_Type (Base_T)
4928 then
4929 return False;
4931 -- String types
4933 elsif Is_String_Type (Typ) then
4934 return
4935 Ekind (Typ) = E_String_Literal_Subtype
4936 or else (Is_Static_Subtype (Component_Type (Typ))
4937 and then Is_Static_Subtype (Etype (First_Index (Typ))));
4939 -- Scalar types
4941 elsif Is_Scalar_Type (Typ) then
4942 if Base_T = Typ then
4943 return True;
4945 else
4946 return Is_Static_Subtype (Anc_Subt)
4947 and then Is_Static_Expression (Type_Low_Bound (Typ))
4948 and then Is_Static_Expression (Type_High_Bound (Typ));
4949 end if;
4951 -- Types other than string and scalar types are never static
4953 else
4954 return False;
4955 end if;
4956 end Is_Static_Subtype;
4958 -------------------------------
4959 -- Is_Statically_Unevaluated --
4960 -------------------------------
4962 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
4963 function Check_Case_Expr_Alternative
4964 (CEA : Node_Id) return Match_Result;
4965 -- We have a message emanating from the Expression of a case expression
4966 -- alternative. We examine this alternative, as follows:
4968 -- If the selecting expression of the parent case is non-static, or
4969 -- if any of the discrete choices of the given case alternative are
4970 -- non-static or raise Constraint_Error, return Non_Static.
4972 -- Otherwise check if the selecting expression matches any of the given
4973 -- discrete choices. If so, the alternative is executed and we return
4974 -- Match, otherwise, the alternative can never be executed, and so we
4975 -- return No_Match.
4977 ---------------------------------
4978 -- Check_Case_Expr_Alternative --
4979 ---------------------------------
4981 function Check_Case_Expr_Alternative
4982 (CEA : Node_Id) return Match_Result
4984 Case_Exp : constant Node_Id := Parent (CEA);
4985 Choice : Node_Id;
4986 Prev_CEA : Node_Id;
4988 begin
4989 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
4991 -- Check that selecting expression is static
4993 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
4994 return Non_Static;
4995 end if;
4997 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
4998 return Non_Static;
4999 end if;
5001 -- All choices are now known to be static. Now see if alternative
5002 -- matches one of the choices.
5004 Choice := First (Discrete_Choices (CEA));
5005 while Present (Choice) loop
5007 -- Check various possibilities for choice, returning Match if we
5008 -- find the selecting value matches any of the choices. Note that
5009 -- we know we are the last choice, so we don't have to keep going.
5011 if Nkind (Choice) = N_Others_Choice then
5013 -- Others choice is a bit annoying, it matches if none of the
5014 -- previous alternatives matches (note that we know we are the
5015 -- last alternative in this case, so we can just go backwards
5016 -- from us to see if any previous one matches).
5018 Prev_CEA := Prev (CEA);
5019 while Present (Prev_CEA) loop
5020 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5021 return No_Match;
5022 end if;
5024 Prev (Prev_CEA);
5025 end loop;
5027 return Match;
5029 -- Else we have a normal static choice
5031 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5032 return Match;
5033 end if;
5035 -- If we fall through, it means that the discrete choice did not
5036 -- match the selecting expression, so continue.
5038 Next (Choice);
5039 end loop;
5041 -- If we get through that loop then all choices were static, and none
5042 -- of them matched the selecting expression. So return No_Match.
5044 return No_Match;
5045 end Check_Case_Expr_Alternative;
5047 -- Local variables
5049 P : Node_Id;
5050 OldP : Node_Id;
5051 Choice : Node_Id;
5053 -- Start of processing for Is_Statically_Unevaluated
5055 begin
5056 -- The (32.x) references here are from RM section 4.9
5058 -- (32.1) An expression is statically unevaluated if it is part of ...
5060 -- This means we have to climb the tree looking for one of the cases
5062 P := Expr;
5063 loop
5064 OldP := P;
5065 P := Parent (P);
5067 -- (32.2) The right operand of a static short-circuit control form
5068 -- whose value is determined by its left operand.
5070 -- AND THEN with False as left operand
5072 if Nkind (P) = N_And_Then
5073 and then Compile_Time_Known_Value (Left_Opnd (P))
5074 and then Is_False (Expr_Value (Left_Opnd (P)))
5075 then
5076 return True;
5078 -- OR ELSE with True as left operand
5080 elsif Nkind (P) = N_Or_Else
5081 and then Compile_Time_Known_Value (Left_Opnd (P))
5082 and then Is_True (Expr_Value (Left_Opnd (P)))
5083 then
5084 return True;
5086 -- (32.3) A dependent_expression of an if_expression whose associated
5087 -- condition is static and equals False.
5089 elsif Nkind (P) = N_If_Expression then
5090 declare
5091 Cond : constant Node_Id := First (Expressions (P));
5092 Texp : constant Node_Id := Next (Cond);
5093 Fexp : constant Node_Id := Next (Texp);
5095 begin
5096 if Compile_Time_Known_Value (Cond) then
5098 -- Condition is True and we are in the right operand
5100 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5101 return True;
5103 -- Condition is False and we are in the left operand
5105 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5106 return True;
5107 end if;
5108 end if;
5109 end;
5111 -- (32.4) A condition or dependent_expression of an if_expression
5112 -- where the condition corresponding to at least one preceding
5113 -- dependent_expression of the if_expression is static and equals
5114 -- True.
5116 -- This refers to cases like
5118 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5120 -- But we expand elsif's out anyway, so the above looks like:
5122 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5124 -- So for us this is caught by the above check for the 32.3 case.
5126 -- (32.5) A dependent_expression of a case_expression whose
5127 -- selecting_expression is static and whose value is not covered
5128 -- by the corresponding discrete_choice_list.
5130 elsif Nkind (P) = N_Case_Expression_Alternative then
5132 -- First, we have to be in the expression to suppress messages.
5133 -- If we are within one of the choices, we want the message.
5135 if OldP = Expression (P) then
5137 -- Statically unevaluated if alternative does not match
5139 if Check_Case_Expr_Alternative (P) = No_Match then
5140 return True;
5141 end if;
5142 end if;
5144 -- (32.6) A choice_expression (or a simple_expression of a range
5145 -- that occurs as a membership_choice of a membership_choice_list)
5146 -- of a static membership test that is preceded in the enclosing
5147 -- membership_choice_list by another item whose individual
5148 -- membership test (see (RM 4.5.2)) statically yields True.
5150 elsif Nkind (P) in N_Membership_Test then
5152 -- Only possibly unevaluated if simple expression is static
5154 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5155 null;
5157 -- All members of the choice list must be static
5159 elsif (Present (Right_Opnd (P))
5160 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5161 or else (Present (Alternatives (P))
5162 and then
5163 not Is_OK_Static_Choice_List (Alternatives (P)))
5164 then
5165 null;
5167 -- If expression is the one and only alternative, then it is
5168 -- definitely not statically unevaluated, so we only have to
5169 -- test the case where there are alternatives present.
5171 elsif Present (Alternatives (P)) then
5173 -- Look for previous matching Choice
5175 Choice := First (Alternatives (P));
5176 while Present (Choice) loop
5178 -- If we reached us and no previous choices matched, this
5179 -- is not the case where we are statically unevaluated.
5181 exit when OldP = Choice;
5183 -- If a previous choice matches, then that is the case where
5184 -- we know our choice is statically unevaluated.
5186 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5187 return True;
5188 end if;
5190 Next (Choice);
5191 end loop;
5193 -- If we fall through the loop, we were not one of the choices,
5194 -- we must have been the expression, so that is not covered by
5195 -- this rule, and we keep going.
5197 null;
5198 end if;
5199 end if;
5201 -- OK, not statically unevaluated at this level, see if we should
5202 -- keep climbing to look for a higher level reason.
5204 -- Special case for component association in aggregates, where
5205 -- we want to keep climbing up to the parent aggregate.
5207 if Nkind (P) = N_Component_Association
5208 and then Nkind (Parent (P)) = N_Aggregate
5209 then
5210 null;
5212 -- All done if not still within subexpression
5214 else
5215 exit when Nkind (P) not in N_Subexpr;
5216 end if;
5217 end loop;
5219 -- If we fall through the loop, not one of the cases covered!
5221 return False;
5222 end Is_Statically_Unevaluated;
5224 --------------------
5225 -- Not_Null_Range --
5226 --------------------
5228 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5229 Typ : constant Entity_Id := Etype (Lo);
5231 begin
5232 if not Compile_Time_Known_Value (Lo)
5233 or else not Compile_Time_Known_Value (Hi)
5234 then
5235 return False;
5236 end if;
5238 if Is_Discrete_Type (Typ) then
5239 return Expr_Value (Lo) <= Expr_Value (Hi);
5240 else pragma Assert (Is_Real_Type (Typ));
5241 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
5242 end if;
5243 end Not_Null_Range;
5245 -------------
5246 -- OK_Bits --
5247 -------------
5249 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
5250 begin
5251 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5253 if Bits < 500_000 then
5254 return True;
5256 -- Error if this maximum is exceeded
5258 else
5259 Error_Msg_N ("static value too large, capacity exceeded", N);
5260 return False;
5261 end if;
5262 end OK_Bits;
5264 ------------------
5265 -- Out_Of_Range --
5266 ------------------
5268 procedure Out_Of_Range (N : Node_Id) is
5269 begin
5270 -- If we have the static expression case, then this is an illegality
5271 -- in Ada 95 mode, except that in an instance, we never generate an
5272 -- error (if the error is legitimate, it was already diagnosed in the
5273 -- template).
5275 if Is_Static_Expression (N)
5276 and then not In_Instance
5277 and then not In_Inlined_Body
5278 and then Ada_Version >= Ada_95
5279 then
5280 -- No message if we are statically unevaluated
5282 if Is_Statically_Unevaluated (N) then
5283 null;
5285 -- The expression to compute the length of a packed array is attached
5286 -- to the array type itself, and deserves a separate message.
5288 elsif Nkind (Parent (N)) = N_Defining_Identifier
5289 and then Is_Array_Type (Parent (N))
5290 and then Present (Packed_Array_Impl_Type (Parent (N)))
5291 and then Present (First_Rep_Item (Parent (N)))
5292 then
5293 Error_Msg_N
5294 ("length of packed array must not exceed Integer''Last",
5295 First_Rep_Item (Parent (N)));
5296 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
5298 -- All cases except the special array case
5300 else
5301 Apply_Compile_Time_Constraint_Error
5302 (N, "value not in range of}", CE_Range_Check_Failed);
5303 end if;
5305 -- Here we generate a warning for the Ada 83 case, or when we are in an
5306 -- instance, or when we have a non-static expression case.
5308 else
5309 Apply_Compile_Time_Constraint_Error
5310 (N, "value not in range of}??", CE_Range_Check_Failed);
5311 end if;
5312 end Out_Of_Range;
5314 ----------------------
5315 -- Predicates_Match --
5316 ----------------------
5318 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
5319 Pred1 : Node_Id;
5320 Pred2 : Node_Id;
5322 begin
5323 if Ada_Version < Ada_2012 then
5324 return True;
5326 -- Both types must have predicates or lack them
5328 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
5329 return False;
5331 -- Check matching predicates
5333 else
5334 Pred1 :=
5335 Get_Rep_Item
5336 (T1, Name_Static_Predicate, Check_Parents => False);
5337 Pred2 :=
5338 Get_Rep_Item
5339 (T2, Name_Static_Predicate, Check_Parents => False);
5341 -- Subtypes statically match if the predicate comes from the
5342 -- same declaration, which can only happen if one is a subtype
5343 -- of the other and has no explicit predicate.
5345 -- Suppress warnings on order of actuals, which is otherwise
5346 -- triggered by one of the two calls below.
5348 pragma Warnings (Off);
5349 return Pred1 = Pred2
5350 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
5351 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
5352 pragma Warnings (On);
5353 end if;
5354 end Predicates_Match;
5356 ---------------------------------------------
5357 -- Real_Or_String_Static_Predicate_Matches --
5358 ---------------------------------------------
5360 function Real_Or_String_Static_Predicate_Matches
5361 (Val : Node_Id;
5362 Typ : Entity_Id) return Boolean
5364 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
5365 -- The predicate expression from the type
5367 Pfun : constant Entity_Id := Predicate_Function (Typ);
5368 -- The entity for the predicate function
5370 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
5371 -- The name of the formal of the predicate function. Occurrences of the
5372 -- type name in Expr have been rewritten as references to this formal,
5373 -- and it has a unique name, so we can identify references by this name.
5375 Copy : Node_Id;
5376 -- Copy of the predicate function tree
5378 function Process (N : Node_Id) return Traverse_Result;
5379 -- Function used to process nodes during the traversal in which we will
5380 -- find occurrences of the entity name, and replace such occurrences
5381 -- by a real literal with the value to be tested.
5383 procedure Traverse is new Traverse_Proc (Process);
5384 -- The actual traversal procedure
5386 -------------
5387 -- Process --
5388 -------------
5390 function Process (N : Node_Id) return Traverse_Result is
5391 begin
5392 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
5393 declare
5394 Nod : constant Node_Id := New_Copy (Val);
5395 begin
5396 Set_Sloc (Nod, Sloc (N));
5397 Rewrite (N, Nod);
5398 return Skip;
5399 end;
5401 else
5402 return OK;
5403 end if;
5404 end Process;
5406 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5408 begin
5409 -- First deal with special case of inherited predicate, where the
5410 -- predicate expression looks like:
5412 -- Expr and then xxPredicate (typ (Ent))
5414 -- where Expr is the predicate expression for this level, and the
5415 -- right operand is the call to evaluate the inherited predicate.
5417 if Nkind (Expr) = N_And_Then
5418 and then Nkind (Right_Opnd (Expr)) = N_Function_Call
5419 then
5420 -- OK we have the inherited case, so make a call to evaluate the
5421 -- inherited predicate. If that fails, so do we!
5423 if not
5424 Real_Or_String_Static_Predicate_Matches
5425 (Val => Val,
5426 Typ => Etype (First_Formal (Entity (Name (Right_Opnd (Expr))))))
5427 then
5428 return False;
5429 end if;
5431 -- Use the left operand for the continued processing
5433 Copy := Copy_Separate_Tree (Left_Opnd (Expr));
5435 -- Case where call to predicate function appears on its own (this means
5436 -- that the predicate at this level is just inherited from the parent).
5438 elsif Nkind (Expr) = N_Function_Call then
5439 declare
5440 Typ : constant Entity_Id :=
5441 Etype (First_Formal (Entity (Name (Expr))));
5443 begin
5444 -- If the inherited predicate is dynamic, just ignore it. We can't
5445 -- go trying to evaluate a dynamic predicate as a static one!
5447 if Has_Dynamic_Predicate_Aspect (Typ) then
5448 return True;
5450 -- Otherwise inherited predicate is static, check for match
5452 else
5453 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
5454 end if;
5455 end;
5457 -- If not just an inherited predicate, copy whole expression
5459 else
5460 Copy := Copy_Separate_Tree (Expr);
5461 end if;
5463 -- Now we replace occurrences of the entity by the value
5465 Traverse (Copy);
5467 -- And analyze the resulting static expression to see if it is True
5469 Analyze_And_Resolve (Copy, Standard_Boolean);
5470 return Is_True (Expr_Value (Copy));
5471 end Real_Or_String_Static_Predicate_Matches;
5473 -------------------------
5474 -- Rewrite_In_Raise_CE --
5475 -------------------------
5477 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
5478 Typ : constant Entity_Id := Etype (N);
5479 Stat : constant Boolean := Is_Static_Expression (N);
5481 begin
5482 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5483 -- can just clear the condition if the reason is appropriate. We do
5484 -- not do this operation if the parent has a reason other than range
5485 -- check failed, because otherwise we would change the reason.
5487 if Present (Parent (N))
5488 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
5489 and then Reason (Parent (N)) =
5490 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
5491 then
5492 Set_Condition (Parent (N), Empty);
5494 -- Else build an explicit N_Raise_CE
5496 else
5497 Rewrite (N,
5498 Make_Raise_Constraint_Error (Sloc (Exp),
5499 Reason => CE_Range_Check_Failed));
5500 Set_Raises_Constraint_Error (N);
5501 Set_Etype (N, Typ);
5502 end if;
5504 -- Set proper flags in result
5506 Set_Raises_Constraint_Error (N, True);
5507 Set_Is_Static_Expression (N, Stat);
5508 end Rewrite_In_Raise_CE;
5510 ---------------------
5511 -- String_Type_Len --
5512 ---------------------
5514 function String_Type_Len (Stype : Entity_Id) return Uint is
5515 NT : constant Entity_Id := Etype (First_Index (Stype));
5516 T : Entity_Id;
5518 begin
5519 if Is_OK_Static_Subtype (NT) then
5520 T := NT;
5521 else
5522 T := Base_Type (NT);
5523 end if;
5525 return Expr_Value (Type_High_Bound (T)) -
5526 Expr_Value (Type_Low_Bound (T)) + 1;
5527 end String_Type_Len;
5529 ------------------------------------
5530 -- Subtypes_Statically_Compatible --
5531 ------------------------------------
5533 function Subtypes_Statically_Compatible
5534 (T1 : Entity_Id;
5535 T2 : Entity_Id;
5536 Formal_Derived_Matching : Boolean := False) return Boolean
5538 begin
5539 -- Scalar types
5541 if Is_Scalar_Type (T1) then
5543 -- Definitely compatible if we match
5545 if Subtypes_Statically_Match (T1, T2) then
5546 return True;
5548 -- If either subtype is nonstatic then they're not compatible
5550 elsif not Is_OK_Static_Subtype (T1)
5551 or else
5552 not Is_OK_Static_Subtype (T2)
5553 then
5554 return False;
5556 -- If either type has constraint error bounds, then consider that
5557 -- they match to avoid junk cascaded errors here.
5559 elsif not Is_OK_Static_Subtype (T1)
5560 or else not Is_OK_Static_Subtype (T2)
5561 then
5562 return True;
5564 -- Base types must match, but we don't check that (should we???) but
5565 -- we do at least check that both types are real, or both types are
5566 -- not real.
5568 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
5569 return False;
5571 -- Here we check the bounds
5573 else
5574 declare
5575 LB1 : constant Node_Id := Type_Low_Bound (T1);
5576 HB1 : constant Node_Id := Type_High_Bound (T1);
5577 LB2 : constant Node_Id := Type_Low_Bound (T2);
5578 HB2 : constant Node_Id := Type_High_Bound (T2);
5580 begin
5581 if Is_Real_Type (T1) then
5582 return
5583 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
5584 or else
5585 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
5586 and then
5587 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
5589 else
5590 return
5591 (Expr_Value (LB1) > Expr_Value (HB1))
5592 or else
5593 (Expr_Value (LB2) <= Expr_Value (LB1)
5594 and then
5595 Expr_Value (HB1) <= Expr_Value (HB2));
5596 end if;
5597 end;
5598 end if;
5600 -- Access types
5602 elsif Is_Access_Type (T1) then
5603 return (not Is_Constrained (T2)
5604 or else (Subtypes_Statically_Match
5605 (Designated_Type (T1), Designated_Type (T2))))
5606 and then not (Can_Never_Be_Null (T2)
5607 and then not Can_Never_Be_Null (T1));
5609 -- All other cases
5611 else
5612 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
5613 or else Subtypes_Statically_Match (T1, T2, Formal_Derived_Matching);
5614 end if;
5615 end Subtypes_Statically_Compatible;
5617 -------------------------------
5618 -- Subtypes_Statically_Match --
5619 -------------------------------
5621 -- Subtypes statically match if they have statically matching constraints
5622 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5623 -- they are the same identical constraint, or if they are static and the
5624 -- values match (RM 4.9.1(1)).
5626 -- In addition, in GNAT, the object size (Esize) values of the types must
5627 -- match if they are set (unless checking an actual for a formal derived
5628 -- type). The use of 'Object_Size can cause this to be false even if the
5629 -- types would otherwise match in the RM sense.
5631 function Subtypes_Statically_Match
5632 (T1 : Entity_Id;
5633 T2 : Entity_Id;
5634 Formal_Derived_Matching : Boolean := False) return Boolean
5636 begin
5637 -- A type always statically matches itself
5639 if T1 = T2 then
5640 return True;
5642 -- No match if sizes different (from use of 'Object_Size). This test
5643 -- is excluded if Formal_Derived_Matching is True, as the base types
5644 -- can be different in that case and typically have different sizes
5645 -- (and Esizes can be set when Frontend_Layout_On_Target is True).
5647 elsif not Formal_Derived_Matching
5648 and then Known_Static_Esize (T1)
5649 and then Known_Static_Esize (T2)
5650 and then Esize (T1) /= Esize (T2)
5651 then
5652 return False;
5654 -- No match if predicates do not match
5656 elsif not Predicates_Match (T1, T2) then
5657 return False;
5659 -- Scalar types
5661 elsif Is_Scalar_Type (T1) then
5663 -- Base types must be the same
5665 if Base_Type (T1) /= Base_Type (T2) then
5666 return False;
5667 end if;
5669 -- A constrained numeric subtype never matches an unconstrained
5670 -- subtype, i.e. both types must be constrained or unconstrained.
5672 -- To understand the requirement for this test, see RM 4.9.1(1).
5673 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5674 -- a constrained subtype with constraint bounds matching the bounds
5675 -- of its corresponding unconstrained base type. In this situation,
5676 -- Integer and Integer'Base do not statically match, even though
5677 -- they have the same bounds.
5679 -- We only apply this test to types in Standard and types that appear
5680 -- in user programs. That way, we do not have to be too careful about
5681 -- setting Is_Constrained right for Itypes.
5683 if Is_Numeric_Type (T1)
5684 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5685 and then (Scope (T1) = Standard_Standard
5686 or else Comes_From_Source (T1))
5687 and then (Scope (T2) = Standard_Standard
5688 or else Comes_From_Source (T2))
5689 then
5690 return False;
5692 -- A generic scalar type does not statically match its base type
5693 -- (AI-311). In this case we make sure that the formals, which are
5694 -- first subtypes of their bases, are constrained.
5696 elsif Is_Generic_Type (T1)
5697 and then Is_Generic_Type (T2)
5698 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5699 then
5700 return False;
5701 end if;
5703 -- If there was an error in either range, then just assume the types
5704 -- statically match to avoid further junk errors.
5706 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
5707 or else Error_Posted (Scalar_Range (T1))
5708 or else Error_Posted (Scalar_Range (T2))
5709 then
5710 return True;
5711 end if;
5713 -- Otherwise both types have bounds that can be compared
5715 declare
5716 LB1 : constant Node_Id := Type_Low_Bound (T1);
5717 HB1 : constant Node_Id := Type_High_Bound (T1);
5718 LB2 : constant Node_Id := Type_Low_Bound (T2);
5719 HB2 : constant Node_Id := Type_High_Bound (T2);
5721 begin
5722 -- If the bounds are the same tree node, then match (common case)
5724 if LB1 = LB2 and then HB1 = HB2 then
5725 return True;
5727 -- Otherwise bounds must be static and identical value
5729 else
5730 if not Is_OK_Static_Subtype (T1)
5731 or else not Is_OK_Static_Subtype (T2)
5732 then
5733 return False;
5735 -- If either type has constraint error bounds, then say that
5736 -- they match to avoid junk cascaded errors here.
5738 elsif not Is_OK_Static_Subtype (T1)
5739 or else not Is_OK_Static_Subtype (T2)
5740 then
5741 return True;
5743 elsif Is_Real_Type (T1) then
5744 return
5745 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
5746 and then
5747 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
5749 else
5750 return
5751 Expr_Value (LB1) = Expr_Value (LB2)
5752 and then
5753 Expr_Value (HB1) = Expr_Value (HB2);
5754 end if;
5755 end if;
5756 end;
5758 -- Type with discriminants
5760 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
5762 -- Because of view exchanges in multiple instantiations, conformance
5763 -- checking might try to match a partial view of a type with no
5764 -- discriminants with a full view that has defaulted discriminants.
5765 -- In such a case, use the discriminant constraint of the full view,
5766 -- which must exist because we know that the two subtypes have the
5767 -- same base type.
5769 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
5770 -- A generic actual type is declared through a subtype declaration
5771 -- and may have an inconsistent indication of the presence of
5772 -- discriminants, so check the type it renames.
5774 if Is_Generic_Actual_Type (T1)
5775 and then not Has_Discriminants (Etype (T1))
5776 and then not Has_Discriminants (T2)
5777 then
5778 return True;
5780 elsif In_Instance then
5781 if Is_Private_Type (T2)
5782 and then Present (Full_View (T2))
5783 and then Has_Discriminants (Full_View (T2))
5784 then
5785 return Subtypes_Statically_Match (T1, Full_View (T2));
5787 elsif Is_Private_Type (T1)
5788 and then Present (Full_View (T1))
5789 and then Has_Discriminants (Full_View (T1))
5790 then
5791 return Subtypes_Statically_Match (Full_View (T1), T2);
5793 else
5794 return False;
5795 end if;
5796 else
5797 return False;
5798 end if;
5799 end if;
5801 declare
5802 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
5803 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
5805 DA1 : Elmt_Id;
5806 DA2 : Elmt_Id;
5808 begin
5809 if DL1 = DL2 then
5810 return True;
5811 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
5812 return False;
5813 end if;
5815 -- Now loop through the discriminant constraints
5817 -- Note: the guard here seems necessary, since it is possible at
5818 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5820 if Present (DL1) and then Present (DL2) then
5821 DA1 := First_Elmt (DL1);
5822 DA2 := First_Elmt (DL2);
5823 while Present (DA1) loop
5824 declare
5825 Expr1 : constant Node_Id := Node (DA1);
5826 Expr2 : constant Node_Id := Node (DA2);
5828 begin
5829 if not Is_OK_Static_Expression (Expr1)
5830 or else not Is_OK_Static_Expression (Expr2)
5831 then
5832 return False;
5834 -- If either expression raised a constraint error,
5835 -- consider the expressions as matching, since this
5836 -- helps to prevent cascading errors.
5838 elsif Raises_Constraint_Error (Expr1)
5839 or else Raises_Constraint_Error (Expr2)
5840 then
5841 null;
5843 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
5844 return False;
5845 end if;
5846 end;
5848 Next_Elmt (DA1);
5849 Next_Elmt (DA2);
5850 end loop;
5851 end if;
5852 end;
5854 return True;
5856 -- A definite type does not match an indefinite or classwide type.
5857 -- However, a generic type with unknown discriminants may be
5858 -- instantiated with a type with no discriminants, and conformance
5859 -- checking on an inherited operation may compare the actual with the
5860 -- subtype that renames it in the instance.
5862 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
5863 then
5864 return
5865 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
5867 -- Array type
5869 elsif Is_Array_Type (T1) then
5871 -- If either subtype is unconstrained then both must be, and if both
5872 -- are unconstrained then no further checking is needed.
5874 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
5875 return not (Is_Constrained (T1) or else Is_Constrained (T2));
5876 end if;
5878 -- Both subtypes are constrained, so check that the index subtypes
5879 -- statically match.
5881 declare
5882 Index1 : Node_Id := First_Index (T1);
5883 Index2 : Node_Id := First_Index (T2);
5885 begin
5886 while Present (Index1) loop
5887 if not
5888 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
5889 then
5890 return False;
5891 end if;
5893 Next_Index (Index1);
5894 Next_Index (Index2);
5895 end loop;
5897 return True;
5898 end;
5900 elsif Is_Access_Type (T1) then
5901 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
5902 return False;
5904 elsif Ekind_In (T1, E_Access_Subprogram_Type,
5905 E_Anonymous_Access_Subprogram_Type)
5906 then
5907 return
5908 Subtype_Conformant
5909 (Designated_Type (T1),
5910 Designated_Type (T2));
5911 else
5912 return
5913 Subtypes_Statically_Match
5914 (Designated_Type (T1),
5915 Designated_Type (T2))
5916 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
5917 end if;
5919 -- All other types definitely match
5921 else
5922 return True;
5923 end if;
5924 end Subtypes_Statically_Match;
5926 ----------
5927 -- Test --
5928 ----------
5930 function Test (Cond : Boolean) return Uint is
5931 begin
5932 if Cond then
5933 return Uint_1;
5934 else
5935 return Uint_0;
5936 end if;
5937 end Test;
5939 ---------------------------------
5940 -- Test_Expression_Is_Foldable --
5941 ---------------------------------
5943 -- One operand case
5945 procedure Test_Expression_Is_Foldable
5946 (N : Node_Id;
5947 Op1 : Node_Id;
5948 Stat : out Boolean;
5949 Fold : out Boolean)
5951 begin
5952 Stat := False;
5953 Fold := False;
5955 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5956 return;
5957 end if;
5959 -- If operand is Any_Type, just propagate to result and do not
5960 -- try to fold, this prevents cascaded errors.
5962 if Etype (Op1) = Any_Type then
5963 Set_Etype (N, Any_Type);
5964 return;
5966 -- If operand raises constraint error, then replace node N with the
5967 -- raise constraint error node, and we are obviously not foldable.
5968 -- Note that this replacement inherits the Is_Static_Expression flag
5969 -- from the operand.
5971 elsif Raises_Constraint_Error (Op1) then
5972 Rewrite_In_Raise_CE (N, Op1);
5973 return;
5975 -- If the operand is not static, then the result is not static, and
5976 -- all we have to do is to check the operand since it is now known
5977 -- to appear in a non-static context.
5979 elsif not Is_Static_Expression (Op1) then
5980 Check_Non_Static_Context (Op1);
5981 Fold := Compile_Time_Known_Value (Op1);
5982 return;
5984 -- An expression of a formal modular type is not foldable because
5985 -- the modulus is unknown.
5987 elsif Is_Modular_Integer_Type (Etype (Op1))
5988 and then Is_Generic_Type (Etype (Op1))
5989 then
5990 Check_Non_Static_Context (Op1);
5991 return;
5993 -- Here we have the case of an operand whose type is OK, which is
5994 -- static, and which does not raise constraint error, we can fold.
5996 else
5997 Set_Is_Static_Expression (N);
5998 Fold := True;
5999 Stat := True;
6000 end if;
6001 end Test_Expression_Is_Foldable;
6003 -- Two operand case
6005 procedure Test_Expression_Is_Foldable
6006 (N : Node_Id;
6007 Op1 : Node_Id;
6008 Op2 : Node_Id;
6009 Stat : out Boolean;
6010 Fold : out Boolean;
6011 CRT_Safe : Boolean := False)
6013 Rstat : constant Boolean := Is_Static_Expression (Op1)
6014 and then
6015 Is_Static_Expression (Op2);
6017 begin
6018 Stat := False;
6019 Fold := False;
6021 -- Inhibit folding if -gnatd.f flag set
6023 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6024 return;
6025 end if;
6027 -- If either operand is Any_Type, just propagate to result and
6028 -- do not try to fold, this prevents cascaded errors.
6030 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
6031 Set_Etype (N, Any_Type);
6032 return;
6034 -- If left operand raises constraint error, then replace node N with the
6035 -- Raise_Constraint_Error node, and we are obviously not foldable.
6036 -- Is_Static_Expression is set from the two operands in the normal way,
6037 -- and we check the right operand if it is in a non-static context.
6039 elsif Raises_Constraint_Error (Op1) then
6040 if not Rstat then
6041 Check_Non_Static_Context (Op2);
6042 end if;
6044 Rewrite_In_Raise_CE (N, Op1);
6045 Set_Is_Static_Expression (N, Rstat);
6046 return;
6048 -- Similar processing for the case of the right operand. Note that we
6049 -- don't use this routine for the short-circuit case, so we do not have
6050 -- to worry about that special case here.
6052 elsif Raises_Constraint_Error (Op2) then
6053 if not Rstat then
6054 Check_Non_Static_Context (Op1);
6055 end if;
6057 Rewrite_In_Raise_CE (N, Op2);
6058 Set_Is_Static_Expression (N, Rstat);
6059 return;
6061 -- Exclude expressions of a generic modular type, as above
6063 elsif Is_Modular_Integer_Type (Etype (Op1))
6064 and then Is_Generic_Type (Etype (Op1))
6065 then
6066 Check_Non_Static_Context (Op1);
6067 return;
6069 -- If result is not static, then check non-static contexts on operands
6070 -- since one of them may be static and the other one may not be static.
6072 elsif not Rstat then
6073 Check_Non_Static_Context (Op1);
6074 Check_Non_Static_Context (Op2);
6076 if CRT_Safe then
6077 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
6078 and then CRT_Safe_Compile_Time_Known_Value (Op2);
6079 else
6080 Fold := Compile_Time_Known_Value (Op1)
6081 and then Compile_Time_Known_Value (Op2);
6082 end if;
6084 return;
6086 -- Else result is static and foldable. Both operands are static, and
6087 -- neither raises constraint error, so we can definitely fold.
6089 else
6090 Set_Is_Static_Expression (N);
6091 Fold := True;
6092 Stat := True;
6093 return;
6094 end if;
6095 end Test_Expression_Is_Foldable;
6097 -------------------
6098 -- Test_In_Range --
6099 -------------------
6101 function Test_In_Range
6102 (N : Node_Id;
6103 Typ : Entity_Id;
6104 Assume_Valid : Boolean;
6105 Fixed_Int : Boolean;
6106 Int_Real : Boolean) return Range_Membership
6108 Val : Uint;
6109 Valr : Ureal;
6111 pragma Warnings (Off, Assume_Valid);
6112 -- For now Assume_Valid is unreferenced since the current implementation
6113 -- always returns Unknown if N is not a compile time known value, but we
6114 -- keep the parameter to allow for future enhancements in which we try
6115 -- to get the information in the variable case as well.
6117 begin
6118 -- If an error was posted on expression, then return Unknown, we do not
6119 -- want cascaded errors based on some false analysis of a junk node.
6121 if Error_Posted (N) then
6122 return Unknown;
6124 -- Expression that raises constraint error is an odd case. We certainly
6125 -- do not want to consider it to be in range. It might make sense to
6126 -- consider it always out of range, but this causes incorrect error
6127 -- messages about static expressions out of range. So we just return
6128 -- Unknown, which is always safe.
6130 elsif Raises_Constraint_Error (N) then
6131 return Unknown;
6133 -- Universal types have no range limits, so always in range
6135 elsif Typ = Universal_Integer or else Typ = Universal_Real then
6136 return In_Range;
6138 -- Never known if not scalar type. Don't know if this can actually
6139 -- happen, but our spec allows it, so we must check.
6141 elsif not Is_Scalar_Type (Typ) then
6142 return Unknown;
6144 -- Never known if this is a generic type, since the bounds of generic
6145 -- types are junk. Note that if we only checked for static expressions
6146 -- (instead of compile time known values) below, we would not need this
6147 -- check, because values of a generic type can never be static, but they
6148 -- can be known at compile time.
6150 elsif Is_Generic_Type (Typ) then
6151 return Unknown;
6153 -- Case of a known compile time value, where we can check if it is in
6154 -- the bounds of the given type.
6156 elsif Compile_Time_Known_Value (N) then
6157 declare
6158 Lo : Node_Id;
6159 Hi : Node_Id;
6161 LB_Known : Boolean;
6162 HB_Known : Boolean;
6164 begin
6165 Lo := Type_Low_Bound (Typ);
6166 Hi := Type_High_Bound (Typ);
6168 LB_Known := Compile_Time_Known_Value (Lo);
6169 HB_Known := Compile_Time_Known_Value (Hi);
6171 -- Fixed point types should be considered as such only if flag
6172 -- Fixed_Int is set to False.
6174 if Is_Floating_Point_Type (Typ)
6175 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
6176 or else Int_Real
6177 then
6178 Valr := Expr_Value_R (N);
6180 if LB_Known and HB_Known then
6181 if Valr >= Expr_Value_R (Lo)
6182 and then
6183 Valr <= Expr_Value_R (Hi)
6184 then
6185 return In_Range;
6186 else
6187 return Out_Of_Range;
6188 end if;
6190 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
6191 or else
6192 (HB_Known and then Valr > Expr_Value_R (Hi))
6193 then
6194 return Out_Of_Range;
6196 else
6197 return Unknown;
6198 end if;
6200 else
6201 Val := Expr_Value (N);
6203 if LB_Known and HB_Known then
6204 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
6205 then
6206 return In_Range;
6207 else
6208 return Out_Of_Range;
6209 end if;
6211 elsif (LB_Known and then Val < Expr_Value (Lo))
6212 or else
6213 (HB_Known and then Val > Expr_Value (Hi))
6214 then
6215 return Out_Of_Range;
6217 else
6218 return Unknown;
6219 end if;
6220 end if;
6221 end;
6223 -- Here for value not known at compile time. Case of expression subtype
6224 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6225 -- In this case we know it is in range without knowing its value.
6227 elsif Assume_Valid
6228 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
6229 then
6230 return In_Range;
6232 -- Another special case. For signed integer types, if the target type
6233 -- has Is_Known_Valid set, and the source type does not have a larger
6234 -- size, then the source value must be in range. We exclude biased
6235 -- types, because they bizarrely can generate out of range values.
6237 elsif Is_Signed_Integer_Type (Etype (N))
6238 and then Is_Known_Valid (Typ)
6239 and then Esize (Etype (N)) <= Esize (Typ)
6240 and then not Has_Biased_Representation (Etype (N))
6242 -- This check cannot be disabled under VM targets because in some
6243 -- unusual cases the backend of the native compiler raises a run-time
6244 -- exception but the virtual machines do not raise any exception.
6246 and then VM_Target = No_VM
6247 then
6248 return In_Range;
6250 -- For all other cases, result is unknown
6252 else
6253 return Unknown;
6254 end if;
6255 end Test_In_Range;
6257 --------------
6258 -- To_Bits --
6259 --------------
6261 procedure To_Bits (U : Uint; B : out Bits) is
6262 begin
6263 for J in 0 .. B'Last loop
6264 B (J) := (U / (2 ** J)) mod 2 /= 0;
6265 end loop;
6266 end To_Bits;
6268 --------------------
6269 -- Why_Not_Static --
6270 --------------------
6272 procedure Why_Not_Static (Expr : Node_Id) is
6273 N : constant Node_Id := Original_Node (Expr);
6274 Typ : Entity_Id;
6275 E : Entity_Id;
6276 Alt : Node_Id;
6277 Exp : Node_Id;
6279 procedure Why_Not_Static_List (L : List_Id);
6280 -- A version that can be called on a list of expressions. Finds all
6281 -- non-static violations in any element of the list.
6283 -------------------------
6284 -- Why_Not_Static_List --
6285 -------------------------
6287 procedure Why_Not_Static_List (L : List_Id) is
6288 N : Node_Id;
6289 begin
6290 if Is_Non_Empty_List (L) then
6291 N := First (L);
6292 while Present (N) loop
6293 Why_Not_Static (N);
6294 Next (N);
6295 end loop;
6296 end if;
6297 end Why_Not_Static_List;
6299 -- Start of processing for Why_Not_Static
6301 begin
6302 -- Ignore call on error or empty node
6304 if No (Expr) or else Nkind (Expr) = N_Error then
6305 return;
6306 end if;
6308 -- Preprocessing for sub expressions
6310 if Nkind (Expr) in N_Subexpr then
6312 -- Nothing to do if expression is static
6314 if Is_OK_Static_Expression (Expr) then
6315 return;
6316 end if;
6318 -- Test for constraint error raised
6320 if Raises_Constraint_Error (Expr) then
6322 -- Special case membership to find out which piece to flag
6324 if Nkind (N) in N_Membership_Test then
6325 if Raises_Constraint_Error (Left_Opnd (N)) then
6326 Why_Not_Static (Left_Opnd (N));
6327 return;
6329 elsif Present (Right_Opnd (N))
6330 and then Raises_Constraint_Error (Right_Opnd (N))
6331 then
6332 Why_Not_Static (Right_Opnd (N));
6333 return;
6335 else
6336 pragma Assert (Present (Alternatives (N)));
6338 Alt := First (Alternatives (N));
6339 while Present (Alt) loop
6340 if Raises_Constraint_Error (Alt) then
6341 Why_Not_Static (Alt);
6342 return;
6343 else
6344 Next (Alt);
6345 end if;
6346 end loop;
6347 end if;
6349 -- Special case a range to find out which bound to flag
6351 elsif Nkind (N) = N_Range then
6352 if Raises_Constraint_Error (Low_Bound (N)) then
6353 Why_Not_Static (Low_Bound (N));
6354 return;
6356 elsif Raises_Constraint_Error (High_Bound (N)) then
6357 Why_Not_Static (High_Bound (N));
6358 return;
6359 end if;
6361 -- Special case attribute to see which part to flag
6363 elsif Nkind (N) = N_Attribute_Reference then
6364 if Raises_Constraint_Error (Prefix (N)) then
6365 Why_Not_Static (Prefix (N));
6366 return;
6367 end if;
6369 if Present (Expressions (N)) then
6370 Exp := First (Expressions (N));
6371 while Present (Exp) loop
6372 if Raises_Constraint_Error (Exp) then
6373 Why_Not_Static (Exp);
6374 return;
6375 end if;
6377 Next (Exp);
6378 end loop;
6379 end if;
6381 -- Special case a subtype name
6383 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
6384 Error_Msg_NE
6385 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
6386 return;
6387 end if;
6389 -- End of special cases
6391 Error_Msg_N
6392 ("!expression raises exception, cannot be static (RM 4.9(34))",
6394 return;
6395 end if;
6397 -- If no type, then something is pretty wrong, so ignore
6399 Typ := Etype (Expr);
6401 if No (Typ) then
6402 return;
6403 end if;
6405 -- Type must be scalar or string type (but allow Bignum, since this
6406 -- is really a scalar type from our point of view in this diagnosis).
6408 if not Is_Scalar_Type (Typ)
6409 and then not Is_String_Type (Typ)
6410 and then not Is_RTE (Typ, RE_Bignum)
6411 then
6412 Error_Msg_N
6413 ("!static expression must have scalar or string type " &
6414 "(RM 4.9(2))", N);
6415 return;
6416 end if;
6417 end if;
6419 -- If we got through those checks, test particular node kind
6421 case Nkind (N) is
6423 -- Entity name
6425 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
6426 E := Entity (N);
6428 if Is_Named_Number (E) then
6429 null;
6431 elsif Ekind (E) = E_Constant then
6433 -- One case we can give a metter message is when we have a
6434 -- string literal created by concatenating an aggregate with
6435 -- an others expression.
6437 Entity_Case : declare
6438 CV : constant Node_Id := Constant_Value (E);
6439 CO : constant Node_Id := Original_Node (CV);
6441 function Is_Aggregate (N : Node_Id) return Boolean;
6442 -- See if node N came from an others aggregate, if so
6443 -- return True and set Error_Msg_Sloc to aggregate.
6445 ------------------
6446 -- Is_Aggregate --
6447 ------------------
6449 function Is_Aggregate (N : Node_Id) return Boolean is
6450 begin
6451 if Nkind (Original_Node (N)) = N_Aggregate then
6452 Error_Msg_Sloc := Sloc (Original_Node (N));
6453 return True;
6455 elsif Is_Entity_Name (N)
6456 and then Ekind (Entity (N)) = E_Constant
6457 and then
6458 Nkind (Original_Node (Constant_Value (Entity (N)))) =
6459 N_Aggregate
6460 then
6461 Error_Msg_Sloc :=
6462 Sloc (Original_Node (Constant_Value (Entity (N))));
6463 return True;
6465 else
6466 return False;
6467 end if;
6468 end Is_Aggregate;
6470 -- Start of processing for Entity_Case
6472 begin
6473 if Is_Aggregate (CV)
6474 or else (Nkind (CO) = N_Op_Concat
6475 and then (Is_Aggregate (Left_Opnd (CO))
6476 or else
6477 Is_Aggregate (Right_Opnd (CO))))
6478 then
6479 Error_Msg_N ("!aggregate (#) is never static", N);
6481 elsif No (CV) or else not Is_Static_Expression (CV) then
6482 Error_Msg_NE
6483 ("!& is not a static constant (RM 4.9(5))", N, E);
6484 end if;
6485 end Entity_Case;
6487 elsif Is_Type (E) then
6488 Error_Msg_NE
6489 ("!& is not a static subtype (RM 4.9(26))", N, E);
6491 else
6492 Error_Msg_NE
6493 ("!& is not static constant or named number "
6494 & "(RM 4.9(5))", N, E);
6495 end if;
6497 -- Binary operator
6499 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6500 if Nkind (N) in N_Op_Shift then
6501 Error_Msg_N
6502 ("!shift functions are never static (RM 4.9(6,18))", N);
6503 else
6504 Why_Not_Static (Left_Opnd (N));
6505 Why_Not_Static (Right_Opnd (N));
6506 end if;
6508 -- Unary operator
6510 when N_Unary_Op =>
6511 Why_Not_Static (Right_Opnd (N));
6513 -- Attribute reference
6515 when N_Attribute_Reference =>
6516 Why_Not_Static_List (Expressions (N));
6518 E := Etype (Prefix (N));
6520 if E = Standard_Void_Type then
6521 return;
6522 end if;
6524 -- Special case non-scalar'Size since this is a common error
6526 if Attribute_Name (N) = Name_Size then
6527 Error_Msg_N
6528 ("!size attribute is only static for static scalar type "
6529 & "(RM 4.9(7,8))", N);
6531 -- Flag array cases
6533 elsif Is_Array_Type (E) then
6534 if not Nam_In (Attribute_Name (N), Name_First,
6535 Name_Last,
6536 Name_Length)
6537 then
6538 Error_Msg_N
6539 ("!static array attribute must be Length, First, or Last "
6540 & "(RM 4.9(8))", N);
6542 -- Since we know the expression is not-static (we already
6543 -- tested for this, must mean array is not static).
6545 else
6546 Error_Msg_N
6547 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
6548 end if;
6550 return;
6552 -- Special case generic types, since again this is a common source
6553 -- of confusion.
6555 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
6556 Error_Msg_N
6557 ("!attribute of generic type is never static "
6558 & "(RM 4.9(7,8))", N);
6560 elsif Is_OK_Static_Subtype (E) then
6561 null;
6563 elsif Is_Scalar_Type (E) then
6564 Error_Msg_N
6565 ("!prefix type for attribute is not static scalar subtype "
6566 & "(RM 4.9(7))", N);
6568 else
6569 Error_Msg_N
6570 ("!static attribute must apply to array/scalar type "
6571 & "(RM 4.9(7,8))", N);
6572 end if;
6574 -- String literal
6576 when N_String_Literal =>
6577 Error_Msg_N
6578 ("!subtype of string literal is non-static (RM 4.9(4))", N);
6580 -- Explicit dereference
6582 when N_Explicit_Dereference =>
6583 Error_Msg_N
6584 ("!explicit dereference is never static (RM 4.9)", N);
6586 -- Function call
6588 when N_Function_Call =>
6589 Why_Not_Static_List (Parameter_Associations (N));
6591 -- Complain about non-static function call unless we have Bignum
6592 -- which means that the underlying expression is really some
6593 -- scalar arithmetic operation.
6595 if not Is_RTE (Typ, RE_Bignum) then
6596 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
6597 end if;
6599 -- Parameter assocation (test actual parameter)
6601 when N_Parameter_Association =>
6602 Why_Not_Static (Explicit_Actual_Parameter (N));
6604 -- Indexed component
6606 when N_Indexed_Component =>
6607 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
6609 -- Procedure call
6611 when N_Procedure_Call_Statement =>
6612 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
6614 -- Qualified expression (test expression)
6616 when N_Qualified_Expression =>
6617 Why_Not_Static (Expression (N));
6619 -- Aggregate
6621 when N_Aggregate | N_Extension_Aggregate =>
6622 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
6624 -- Range
6626 when N_Range =>
6627 Why_Not_Static (Low_Bound (N));
6628 Why_Not_Static (High_Bound (N));
6630 -- Range constraint, test range expression
6632 when N_Range_Constraint =>
6633 Why_Not_Static (Range_Expression (N));
6635 -- Subtype indication, test constraint
6637 when N_Subtype_Indication =>
6638 Why_Not_Static (Constraint (N));
6640 -- Selected component
6642 when N_Selected_Component =>
6643 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
6645 -- Slice
6647 when N_Slice =>
6648 Error_Msg_N ("!slice is never static (RM 4.9)", N);
6650 when N_Type_Conversion =>
6651 Why_Not_Static (Expression (N));
6653 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
6654 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
6655 then
6656 Error_Msg_N
6657 ("!static conversion requires static scalar subtype result "
6658 & "(RM 4.9(9))", N);
6659 end if;
6661 -- Unchecked type conversion
6663 when N_Unchecked_Type_Conversion =>
6664 Error_Msg_N
6665 ("!unchecked type conversion is never static (RM 4.9)", N);
6667 -- All other cases, no reason to give
6669 when others =>
6670 null;
6672 end case;
6673 end Why_Not_Static;
6675 end Sem_Eval;