* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / sem_eval.adb
blob59e867256ea51960614163616f263762e3e7e11c
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-2018, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Lib; use Lib;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Par_SCO; use Par_SCO;
42 with Rtsfind; use Rtsfind;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Cat; use Sem_Cat;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Res; use Sem_Res;
49 with Sem_Util; use Sem_Util;
50 with Sem_Type; use Sem_Type;
51 with Sem_Warn; use Sem_Warn;
52 with Sinfo; use Sinfo;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
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 Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
178 -- Check whether an arithmetic operation with universal operands which is a
179 -- rewritten function call with an explicit scope indication is ambiguous:
180 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
181 -- type declared in P and the context does not impose a type on the result
182 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
183 -- error and return Empty, else return the result type of the operator.
185 function From_Bits (B : Bits; T : Entity_Id) return Uint;
186 -- Converts a bit string of length B'Length to a Uint value to be used for
187 -- a target of type T, which is a modular type. This procedure includes the
188 -- necessary reduction by the modulus in the case of a nonbinary modulus
189 -- (for a binary modulus, the bit string is the right length any way so all
190 -- is well).
192 function Get_String_Val (N : Node_Id) return Node_Id;
193 -- Given a tree node for a folded string or character value, returns the
194 -- corresponding string literal or character literal (one of the two must
195 -- be available, or the operand would not have been marked as foldable in
196 -- the earlier analysis of the operation).
198 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean;
199 -- Given a choice (from a case expression or membership test), returns
200 -- True if the choice is static and does not raise a Constraint_Error.
202 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean;
203 -- Given a choice list (from a case expression or membership test), return
204 -- True if all choices are static in the sense of Is_OK_Static_Choice.
206 function Is_Static_Choice (Choice : Node_Id) return Boolean;
207 -- Given a choice (from a case expression or membership test), returns
208 -- True if the choice is static. No test is made for raising of constraint
209 -- error, so this function is used only for legality tests.
211 function Is_Static_Choice_List (Choices : List_Id) return Boolean;
212 -- Given a choice list (from a case expression or membership test), return
213 -- True if all choices are static in the sense of Is_Static_Choice.
215 function Is_Static_Range (N : Node_Id) return Boolean;
216 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
217 -- argument is an N_Range node (but note that the semantic analysis of
218 -- equivalent range attribute references already turned them into the
219 -- equivalent range). This differs from Is_OK_Static_Range (which is what
220 -- must be used by clients) in that it does not care whether the bounds
221 -- raise Constraint_Error or not. Used for checking whether expressions are
222 -- static in the 4.9 sense (without worrying about exceptions).
224 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
225 -- Bits represents the number of bits in an integer value to be computed
226 -- (but the value has not been computed yet). If this value in Bits is
227 -- reasonable, a result of True is returned, with the implication that the
228 -- caller should go ahead and complete the calculation. If the value in
229 -- Bits is unreasonably large, then an error is posted on node N, and
230 -- False is returned (and the caller skips the proposed calculation).
232 procedure Out_Of_Range (N : Node_Id);
233 -- This procedure is called if it is determined that node N, which appears
234 -- in a non-static context, is a compile time known value which is outside
235 -- its range, i.e. the range of Etype. This is used in contexts where
236 -- this is an illegality if N is static, and should generate a warning
237 -- otherwise.
239 function Real_Or_String_Static_Predicate_Matches
240 (Val : Node_Id;
241 Typ : Entity_Id) return Boolean;
242 -- This is the function used to evaluate real or string static predicates.
243 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
244 -- represents the value to be tested against the predicate. Typ is the
245 -- type with the predicate, from which the predicate expression can be
246 -- extracted. The result returned is True if the given value satisfies
247 -- the predicate.
249 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
250 -- N and Exp are nodes representing an expression, Exp is known to raise
251 -- CE. N is rewritten in term of Exp in the optimal way.
253 function String_Type_Len (Stype : Entity_Id) return Uint;
254 -- Given a string type, determines the length of the index type, or, if
255 -- this index type is non-static, the length of the base type of this index
256 -- type. Note that if the string type is itself static, then the index type
257 -- is static, so the second case applies only if the string type passed is
258 -- non-static.
260 function Test (Cond : Boolean) return Uint;
261 pragma Inline (Test);
262 -- This function simply returns the appropriate Boolean'Pos value
263 -- corresponding to the value of Cond as a universal integer. It is
264 -- used for producing the result of the static evaluation of the
265 -- logical operators
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). If the expression is known at
352 -- compile time and obeys the predicate, then it is static and must be
353 -- labeled as such, which matters e.g. for case statements. The original
354 -- expression may be a type conversion of a variable with a known value,
355 -- which might otherwise not be marked static.
357 -- Case of real static predicate
359 if Is_Real_Type (Typ) then
360 if Real_Or_String_Static_Predicate_Matches
361 (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)),
362 Typ => Typ)
363 then
364 Set_Is_Static_Expression (Expr);
365 return;
366 end if;
368 -- Case of string static predicate
370 elsif Is_String_Type (Typ) then
371 if Real_Or_String_Static_Predicate_Matches
372 (Val => Expr_Value_S (Expr), Typ => Typ)
373 then
374 Set_Is_Static_Expression (Expr);
375 return;
376 end if;
378 -- Case of discrete static predicate
380 else
381 pragma Assert (Is_Discrete_Type (Typ));
383 -- If static predicate matches, nothing to do
385 if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then
386 Set_Is_Static_Expression (Expr);
387 return;
388 end if;
389 end if;
391 -- Here we know that the predicate will fail
393 -- Special case of static expression failing a predicate (other than one
394 -- that was explicitly specified with a Dynamic_Predicate aspect). This
395 -- is the case where the expression is no longer considered static.
397 if Is_Static_Expression (Expr)
398 and then not Has_Dynamic_Predicate_Aspect (Typ)
399 then
400 Error_Msg_NE
401 ("??static expression fails static predicate check on &",
402 Expr, Typ);
403 Error_Msg_N
404 ("\??expression is no longer considered static", Expr);
405 Set_Is_Static_Expression (Expr, False);
407 -- In all other cases, this is just a warning that a test will fail.
408 -- It does not matter if the expression is static or not, or if the
409 -- predicate comes from a dynamic predicate aspect or not.
411 else
412 Error_Msg_NE
413 ("??expression fails predicate check on &", Expr, Typ);
414 end if;
415 end Check_Expression_Against_Static_Predicate;
417 ------------------------------
418 -- Check_Non_Static_Context --
419 ------------------------------
421 procedure Check_Non_Static_Context (N : Node_Id) is
422 T : constant Entity_Id := Etype (N);
423 Checks_On : constant Boolean :=
424 not Index_Checks_Suppressed (T)
425 and not Range_Checks_Suppressed (T);
427 begin
428 -- Ignore cases of non-scalar types, error types, or universal real
429 -- types that have no usable bounds.
431 if T = Any_Type
432 or else not Is_Scalar_Type (T)
433 or else T = Universal_Fixed
434 or else T = Universal_Real
435 then
436 return;
437 end if;
439 -- At this stage we have a scalar type. If we have an expression that
440 -- raises CE, then we already issued a warning or error msg so there is
441 -- nothing more to be done in this routine.
443 if Raises_Constraint_Error (N) then
444 return;
445 end if;
447 -- Now we have a scalar type which is not marked as raising a constraint
448 -- error exception. The main purpose of this routine is to deal with
449 -- static expressions appearing in a non-static context. That means
450 -- that if we do not have a static expression then there is not much
451 -- to do. The one case that we deal with here is that if we have a
452 -- floating-point value that is out of range, then we post a warning
453 -- that an infinity will result.
455 if not Is_Static_Expression (N) then
456 if Is_Floating_Point_Type (T) then
457 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
458 Error_Msg_N
459 ("??float value out of range, infinity will be generated", N);
461 -- The literal may be the result of constant-folding of a non-
462 -- static subexpression of a larger expression (e.g. a conversion
463 -- of a non-static variable whose value happens to be known). At
464 -- this point we must reduce the value of the subexpression to a
465 -- machine number (RM 4.9 (38/2)).
467 elsif Nkind (N) = N_Real_Literal
468 and then Nkind (Parent (N)) in N_Subexpr
469 then
470 Rewrite (N, New_Copy (N));
471 Set_Realval
472 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
473 end if;
474 end if;
476 return;
477 end if;
479 -- Here we have the case of outer level static expression of scalar
480 -- type, where the processing of this procedure is needed.
482 -- For real types, this is where we convert the value to a machine
483 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
484 -- need to do this if the parent is a constant declaration, since in
485 -- other cases, gigi should do the necessary conversion correctly, but
486 -- experimentation shows that this is not the case on all machines, in
487 -- particular if we do not convert all literals to machine values in
488 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
489 -- and SGI/Irix.
491 -- This conversion is always done by GNATprove on real literals in
492 -- non-static expressions, by calling Check_Non_Static_Context from
493 -- gnat2why, as GNATprove cannot do the conversion later contrary
494 -- to gigi. The frontend computes the information about which
495 -- expressions are static, which is used by gnat2why to call
496 -- Check_Non_Static_Context on exactly those real literals that are
497 -- not subexpressions of static expressions.
499 if Nkind (N) = N_Real_Literal
500 and then not Is_Machine_Number (N)
501 and then not Is_Generic_Type (Etype (N))
502 and then Etype (N) /= Universal_Real
503 then
504 -- Check that value is in bounds before converting to machine
505 -- number, so as not to lose case where value overflows in the
506 -- least significant bit or less. See B490001.
508 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
509 Out_Of_Range (N);
510 return;
511 end if;
513 -- Note: we have to copy the node, to avoid problems with conformance
514 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
516 Rewrite (N, New_Copy (N));
518 if not Is_Floating_Point_Type (T) then
519 Set_Realval
520 (N, Corresponding_Integer_Value (N) * Small_Value (T));
522 elsif not UR_Is_Zero (Realval (N)) then
524 -- Note: even though RM 4.9(38) specifies biased rounding, this
525 -- has been modified by AI-100 in order to prevent confusing
526 -- differences in rounding between static and non-static
527 -- expressions. AI-100 specifies that the effect of such rounding
528 -- is implementation dependent, and in GNAT we round to nearest
529 -- even to match the run-time behavior. Note that this applies
530 -- to floating point literals, not fixed points ones, even though
531 -- their compiler representation is also as a universal real.
533 Set_Realval
534 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
535 Set_Is_Machine_Number (N);
536 end if;
538 end if;
540 -- Check for out of range universal integer. This is a non-static
541 -- context, so the integer value must be in range of the runtime
542 -- representation of universal integers.
544 -- We do this only within an expression, because that is the only
545 -- case in which non-static universal integer values can occur, and
546 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
547 -- called in contexts like the expression of a number declaration where
548 -- we certainly want to allow out of range values.
550 if Etype (N) = Universal_Integer
551 and then Nkind (N) = N_Integer_Literal
552 and then Nkind (Parent (N)) in N_Subexpr
553 and then
554 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
555 or else
556 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
557 then
558 Apply_Compile_Time_Constraint_Error
559 (N, "non-static universal integer value out of range<<",
560 CE_Range_Check_Failed);
562 -- Check out of range of base type
564 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
565 Out_Of_Range (N);
567 -- Give warning if outside subtype (where one or both of the bounds of
568 -- the subtype is static). This warning is omitted if the expression
569 -- appears in a range that could be null (warnings are handled elsewhere
570 -- for this case).
572 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
573 if Is_In_Range (N, T, Assume_Valid => True) then
574 null;
576 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
578 -- Ignore out of range values for System.Priority in CodePeer
579 -- mode since the actual target compiler may provide a wider
580 -- range.
582 if CodePeer_Mode and then T = RTE (RE_Priority) then
583 Set_Do_Range_Check (N, False);
584 else
585 Apply_Compile_Time_Constraint_Error
586 (N, "value not in range of}<<", CE_Range_Check_Failed);
587 end if;
589 elsif Checks_On then
590 Enable_Range_Check (N);
592 else
593 Set_Do_Range_Check (N, False);
594 end if;
595 end if;
596 end Check_Non_Static_Context;
598 ---------------------------------
599 -- Check_String_Literal_Length --
600 ---------------------------------
602 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
603 begin
604 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
605 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
606 then
607 Apply_Compile_Time_Constraint_Error
608 (N, "string length wrong for}??",
609 CE_Length_Check_Failed,
610 Ent => Ttype,
611 Typ => Ttype);
612 end if;
613 end if;
614 end Check_String_Literal_Length;
616 --------------------
617 -- Choice_Matches --
618 --------------------
620 function Choice_Matches
621 (Expr : Node_Id;
622 Choice : Node_Id) return Match_Result
624 Etyp : constant Entity_Id := Etype (Expr);
625 Val : Uint;
626 ValR : Ureal;
627 ValS : Node_Id;
629 begin
630 pragma Assert (Compile_Time_Known_Value (Expr));
631 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
633 if not Is_OK_Static_Choice (Choice) then
634 Set_Raises_Constraint_Error (Choice);
635 return Non_Static;
637 -- When the choice denotes a subtype with a static predictate, check the
638 -- expression against the predicate values. Different procedures apply
639 -- to discrete and non-discrete types.
641 elsif (Nkind (Choice) = N_Subtype_Indication
642 or else (Is_Entity_Name (Choice)
643 and then Is_Type (Entity (Choice))))
644 and then Has_Predicates (Etype (Choice))
645 and then Has_Static_Predicate (Etype (Choice))
646 then
647 if Is_Discrete_Type (Etype (Choice)) then
648 return
649 Choices_Match
650 (Expr, Static_Discrete_Predicate (Etype (Choice)));
652 elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice))
653 then
654 return Match;
656 else
657 return No_Match;
658 end if;
660 -- Discrete type case only
662 elsif Is_Discrete_Type (Etyp) then
663 Val := Expr_Value (Expr);
665 if Nkind (Choice) = N_Range then
666 if Val >= Expr_Value (Low_Bound (Choice))
667 and then
668 Val <= Expr_Value (High_Bound (Choice))
669 then
670 return Match;
671 else
672 return No_Match;
673 end if;
675 elsif Nkind (Choice) = N_Subtype_Indication
676 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
677 then
678 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
679 and then
680 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
681 then
682 return Match;
683 else
684 return No_Match;
685 end if;
687 elsif Nkind (Choice) = N_Others_Choice then
688 return Match;
690 else
691 if Val = Expr_Value (Choice) then
692 return Match;
693 else
694 return No_Match;
695 end if;
696 end if;
698 -- Real type case
700 elsif Is_Real_Type (Etyp) then
701 ValR := Expr_Value_R (Expr);
703 if Nkind (Choice) = N_Range then
704 if ValR >= Expr_Value_R (Low_Bound (Choice))
705 and then
706 ValR <= Expr_Value_R (High_Bound (Choice))
707 then
708 return Match;
709 else
710 return No_Match;
711 end if;
713 elsif Nkind (Choice) = N_Subtype_Indication
714 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
715 then
716 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
717 and then
718 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
719 then
720 return Match;
721 else
722 return No_Match;
723 end if;
725 else
726 if ValR = Expr_Value_R (Choice) then
727 return Match;
728 else
729 return No_Match;
730 end if;
731 end if;
733 -- String type cases
735 else
736 pragma Assert (Is_String_Type (Etyp));
737 ValS := Expr_Value_S (Expr);
739 if Nkind (Choice) = N_Subtype_Indication
740 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
741 then
742 if not Is_Constrained (Etype (Choice)) then
743 return Match;
745 else
746 declare
747 Typlen : constant Uint :=
748 String_Type_Len (Etype (Choice));
749 Strlen : constant Uint :=
750 UI_From_Int (String_Length (Strval (ValS)));
751 begin
752 if Typlen = Strlen then
753 return Match;
754 else
755 return No_Match;
756 end if;
757 end;
758 end if;
760 else
761 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
762 then
763 return Match;
764 else
765 return No_Match;
766 end if;
767 end if;
768 end if;
769 end Choice_Matches;
771 -------------------
772 -- Choices_Match --
773 -------------------
775 function Choices_Match
776 (Expr : Node_Id;
777 Choices : List_Id) return Match_Result
779 Choice : Node_Id;
780 Result : Match_Result;
782 begin
783 Choice := First (Choices);
784 while Present (Choice) loop
785 Result := Choice_Matches (Expr, Choice);
787 if Result /= No_Match then
788 return Result;
789 end if;
791 Next (Choice);
792 end loop;
794 return No_Match;
795 end Choices_Match;
797 --------------------------
798 -- Compile_Time_Compare --
799 --------------------------
801 function Compile_Time_Compare
802 (L, R : Node_Id;
803 Assume_Valid : Boolean) return Compare_Result
805 Discard : aliased Uint;
806 begin
807 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
808 end Compile_Time_Compare;
810 function Compile_Time_Compare
811 (L, R : Node_Id;
812 Diff : access Uint;
813 Assume_Valid : Boolean;
814 Rec : Boolean := False) return Compare_Result
816 Ltyp : Entity_Id := Etype (L);
817 Rtyp : Entity_Id := Etype (R);
819 Discard : aliased Uint;
821 procedure Compare_Decompose
822 (N : Node_Id;
823 R : out Node_Id;
824 V : out Uint);
825 -- This procedure decomposes the node N into an expression node and a
826 -- signed offset, so that the value of N is equal to the value of R plus
827 -- the value V (which may be negative). If no such decomposition is
828 -- possible, then on return R is a copy of N, and V is set to zero.
830 function Compare_Fixup (N : Node_Id) return Node_Id;
831 -- This function deals with replacing 'Last and 'First references with
832 -- their corresponding type bounds, which we then can compare. The
833 -- argument is the original node, the result is the identity, unless we
834 -- have a 'Last/'First reference in which case the value returned is the
835 -- appropriate type bound.
837 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
838 -- Even if the context does not assume that values are valid, some
839 -- simple cases can be recognized.
841 function Is_Same_Value (L, R : Node_Id) return Boolean;
842 -- Returns True iff L and R represent expressions that definitely have
843 -- identical (but not necessarily compile time known) values Indeed the
844 -- caller is expected to have already dealt with the cases of compile
845 -- time known values, so these are not tested here.
847 -----------------------
848 -- Compare_Decompose --
849 -----------------------
851 procedure Compare_Decompose
852 (N : Node_Id;
853 R : out Node_Id;
854 V : out Uint)
856 begin
857 if Nkind (N) = N_Op_Add
858 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
859 then
860 R := Left_Opnd (N);
861 V := Intval (Right_Opnd (N));
862 return;
864 elsif Nkind (N) = N_Op_Subtract
865 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
866 then
867 R := Left_Opnd (N);
868 V := UI_Negate (Intval (Right_Opnd (N)));
869 return;
871 elsif Nkind (N) = N_Attribute_Reference then
872 if Attribute_Name (N) = Name_Succ then
873 R := First (Expressions (N));
874 V := Uint_1;
875 return;
877 elsif Attribute_Name (N) = Name_Pred then
878 R := First (Expressions (N));
879 V := Uint_Minus_1;
880 return;
881 end if;
882 end if;
884 R := N;
885 V := Uint_0;
886 end Compare_Decompose;
888 -------------------
889 -- Compare_Fixup --
890 -------------------
892 function Compare_Fixup (N : Node_Id) return Node_Id is
893 Indx : Node_Id;
894 Xtyp : Entity_Id;
895 Subs : Nat;
897 begin
898 -- Fixup only required for First/Last attribute reference
900 if Nkind (N) = N_Attribute_Reference
901 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
902 then
903 Xtyp := Etype (Prefix (N));
905 -- If we have no type, then just abandon the attempt to do
906 -- a fixup, this is probably the result of some other error.
908 if No (Xtyp) then
909 return N;
910 end if;
912 -- Dereference an access type
914 if Is_Access_Type (Xtyp) then
915 Xtyp := Designated_Type (Xtyp);
916 end if;
918 -- If we don't have an array type at this stage, something is
919 -- peculiar, e.g. another error, and we abandon the attempt at
920 -- a fixup.
922 if not Is_Array_Type (Xtyp) then
923 return N;
924 end if;
926 -- Ignore unconstrained array, since bounds are not meaningful
928 if not Is_Constrained (Xtyp) then
929 return N;
930 end if;
932 if Ekind (Xtyp) = E_String_Literal_Subtype then
933 if Attribute_Name (N) = Name_First then
934 return String_Literal_Low_Bound (Xtyp);
935 else
936 return
937 Make_Integer_Literal (Sloc (N),
938 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
939 String_Literal_Length (Xtyp));
940 end if;
941 end if;
943 -- Find correct index type
945 Indx := First_Index (Xtyp);
947 if Present (Expressions (N)) then
948 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
950 for J in 2 .. Subs loop
951 Indx := Next_Index (Indx);
952 end loop;
953 end if;
955 Xtyp := Etype (Indx);
957 if Attribute_Name (N) = Name_First then
958 return Type_Low_Bound (Xtyp);
959 else
960 return Type_High_Bound (Xtyp);
961 end if;
962 end if;
964 return N;
965 end Compare_Fixup;
967 ----------------------------
968 -- Is_Known_Valid_Operand --
969 ----------------------------
971 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
972 begin
973 return (Is_Entity_Name (Opnd)
974 and then
975 (Is_Known_Valid (Entity (Opnd))
976 or else Ekind (Entity (Opnd)) = E_In_Parameter
977 or else
978 (Ekind (Entity (Opnd)) in Object_Kind
979 and then Present (Current_Value (Entity (Opnd))))))
980 or else Is_OK_Static_Expression (Opnd);
981 end Is_Known_Valid_Operand;
983 -------------------
984 -- Is_Same_Value --
985 -------------------
987 function Is_Same_Value (L, R : Node_Id) return Boolean is
988 Lf : constant Node_Id := Compare_Fixup (L);
989 Rf : constant Node_Id := Compare_Fixup (R);
991 function Is_Same_Subscript (L, R : List_Id) return Boolean;
992 -- L, R are the Expressions values from two attribute nodes for First
993 -- or Last attributes. Either may be set to No_List if no expressions
994 -- are present (indicating subscript 1). The result is True if both
995 -- expressions represent the same subscript (note one case is where
996 -- one subscript is missing and the other is explicitly set to 1).
998 -----------------------
999 -- Is_Same_Subscript --
1000 -----------------------
1002 function Is_Same_Subscript (L, R : List_Id) return Boolean is
1003 begin
1004 if L = No_List then
1005 if R = No_List then
1006 return True;
1007 else
1008 return Expr_Value (First (R)) = Uint_1;
1009 end if;
1011 else
1012 if R = No_List then
1013 return Expr_Value (First (L)) = Uint_1;
1014 else
1015 return Expr_Value (First (L)) = Expr_Value (First (R));
1016 end if;
1017 end if;
1018 end Is_Same_Subscript;
1020 -- Start of processing for Is_Same_Value
1022 begin
1023 -- Values are the same if they refer to the same entity and the
1024 -- entity is non-volatile. This does not however apply to Float
1025 -- types, since we may have two NaN values and they should never
1026 -- compare equal.
1028 -- If the entity is a discriminant, the two expressions may be bounds
1029 -- of components of objects of the same discriminated type. The
1030 -- values of the discriminants are not static, and therefore the
1031 -- result is unknown.
1033 -- It would be better to comment individual branches of this test ???
1035 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
1036 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
1037 and then Entity (Lf) = Entity (Rf)
1038 and then Ekind (Entity (Lf)) /= E_Discriminant
1039 and then Present (Entity (Lf))
1040 and then not Is_Floating_Point_Type (Etype (L))
1041 and then not Is_Volatile_Reference (L)
1042 and then not Is_Volatile_Reference (R)
1043 then
1044 return True;
1046 -- Or if they are compile time known and identical
1048 elsif Compile_Time_Known_Value (Lf)
1049 and then
1050 Compile_Time_Known_Value (Rf)
1051 and then Expr_Value (Lf) = Expr_Value (Rf)
1052 then
1053 return True;
1055 -- False if Nkind of the two nodes is different for remaining cases
1057 elsif Nkind (Lf) /= Nkind (Rf) then
1058 return False;
1060 -- True if both 'First or 'Last values applying to the same entity
1061 -- (first and last don't change even if value does). Note that we
1062 -- need this even with the calls to Compare_Fixup, to handle the
1063 -- case of unconstrained array attributes where Compare_Fixup
1064 -- cannot find useful bounds.
1066 elsif Nkind (Lf) = N_Attribute_Reference
1067 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1068 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
1069 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
1070 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
1071 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1072 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1073 then
1074 return True;
1076 -- True if the same selected component from the same record
1078 elsif Nkind (Lf) = N_Selected_Component
1079 and then Selector_Name (Lf) = Selector_Name (Rf)
1080 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1081 then
1082 return True;
1084 -- True if the same unary operator applied to the same operand
1086 elsif Nkind (Lf) in N_Unary_Op
1087 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1088 then
1089 return True;
1091 -- True if the same binary operator applied to the same operands
1093 elsif Nkind (Lf) in N_Binary_Op
1094 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1095 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1096 then
1097 return True;
1099 -- All other cases, we can't tell, so return False
1101 else
1102 return False;
1103 end if;
1104 end Is_Same_Value;
1106 -- Start of processing for Compile_Time_Compare
1108 begin
1109 Diff.all := No_Uint;
1111 -- In preanalysis mode, always return Unknown unless the expression
1112 -- is static. It is too early to be thinking we know the result of a
1113 -- comparison, save that judgment for the full analysis. This is
1114 -- particularly important in the case of pre and postconditions, which
1115 -- otherwise can be prematurely collapsed into having True or False
1116 -- conditions when this is inappropriate.
1118 if not (Full_Analysis
1119 or else (Is_OK_Static_Expression (L)
1120 and then
1121 Is_OK_Static_Expression (R)))
1122 then
1123 return Unknown;
1124 end if;
1126 -- If either operand could raise constraint error, then we cannot
1127 -- know the result at compile time (since CE may be raised).
1129 if not (Cannot_Raise_Constraint_Error (L)
1130 and then
1131 Cannot_Raise_Constraint_Error (R))
1132 then
1133 return Unknown;
1134 end if;
1136 -- Identical operands are most certainly equal
1138 if L = R then
1139 return EQ;
1140 end if;
1142 -- If expressions have no types, then do not attempt to determine if
1143 -- they are the same, since something funny is going on. One case in
1144 -- which this happens is during generic template analysis, when bounds
1145 -- are not fully analyzed.
1147 if No (Ltyp) or else No (Rtyp) then
1148 return Unknown;
1149 end if;
1151 -- These get reset to the base type for the case of entities where
1152 -- Is_Known_Valid is not set. This takes care of handling possible
1153 -- invalid representations using the value of the base type, in
1154 -- accordance with RM 13.9.1(10).
1156 Ltyp := Underlying_Type (Ltyp);
1157 Rtyp := Underlying_Type (Rtyp);
1159 -- Same rationale as above, but for Underlying_Type instead of Etype
1161 if No (Ltyp) or else No (Rtyp) then
1162 return Unknown;
1163 end if;
1165 -- We do not attempt comparisons for packed arrays represented as
1166 -- modular types, where the semantics of comparison is quite different.
1168 if Is_Packed_Array_Impl_Type (Ltyp)
1169 and then Is_Modular_Integer_Type (Ltyp)
1170 then
1171 return Unknown;
1173 -- For access types, the only time we know the result at compile time
1174 -- (apart from identical operands, which we handled already) is if we
1175 -- know one operand is null and the other is not, or both operands are
1176 -- known null.
1178 elsif Is_Access_Type (Ltyp) then
1179 if Known_Null (L) then
1180 if Known_Null (R) then
1181 return EQ;
1182 elsif Known_Non_Null (R) then
1183 return NE;
1184 else
1185 return Unknown;
1186 end if;
1188 elsif Known_Non_Null (L) and then Known_Null (R) then
1189 return NE;
1191 else
1192 return Unknown;
1193 end if;
1195 -- Case where comparison involves two compile time known values
1197 elsif Compile_Time_Known_Value (L)
1198 and then
1199 Compile_Time_Known_Value (R)
1200 then
1201 -- For the floating-point case, we have to be a little careful, since
1202 -- at compile time we are dealing with universal exact values, but at
1203 -- runtime, these will be in non-exact target form. That's why the
1204 -- returned results are LE and GE below instead of LT and GT.
1206 if Is_Floating_Point_Type (Ltyp)
1207 or else
1208 Is_Floating_Point_Type (Rtyp)
1209 then
1210 declare
1211 Lo : constant Ureal := Expr_Value_R (L);
1212 Hi : constant Ureal := Expr_Value_R (R);
1213 begin
1214 if Lo < Hi then
1215 return LE;
1216 elsif Lo = Hi then
1217 return EQ;
1218 else
1219 return GE;
1220 end if;
1221 end;
1223 -- For string types, we have two string literals and we proceed to
1224 -- compare them using the Ada style dictionary string comparison.
1226 elsif not Is_Scalar_Type (Ltyp) then
1227 declare
1228 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1229 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1230 Llen : constant Nat := String_Length (Lstring);
1231 Rlen : constant Nat := String_Length (Rstring);
1233 begin
1234 for J in 1 .. Nat'Min (Llen, Rlen) loop
1235 declare
1236 LC : constant Char_Code := Get_String_Char (Lstring, J);
1237 RC : constant Char_Code := Get_String_Char (Rstring, J);
1238 begin
1239 if LC < RC then
1240 return LT;
1241 elsif LC > RC then
1242 return GT;
1243 end if;
1244 end;
1245 end loop;
1247 if Llen < Rlen then
1248 return LT;
1249 elsif Llen > Rlen then
1250 return GT;
1251 else
1252 return EQ;
1253 end if;
1254 end;
1256 -- For remaining scalar cases we know exactly (note that this does
1257 -- include the fixed-point case, where we know the run time integer
1258 -- values now).
1260 else
1261 declare
1262 Lo : constant Uint := Expr_Value (L);
1263 Hi : constant Uint := Expr_Value (R);
1264 begin
1265 if Lo < Hi then
1266 Diff.all := Hi - Lo;
1267 return LT;
1268 elsif Lo = Hi then
1269 return EQ;
1270 else
1271 Diff.all := Lo - Hi;
1272 return GT;
1273 end if;
1274 end;
1275 end if;
1277 -- Cases where at least one operand is not known at compile time
1279 else
1280 -- Remaining checks apply only for discrete types
1282 if not Is_Discrete_Type (Ltyp)
1283 or else
1284 not Is_Discrete_Type (Rtyp)
1285 then
1286 return Unknown;
1287 end if;
1289 -- Defend against generic types, or actually any expressions that
1290 -- contain a reference to a generic type from within a generic
1291 -- template. We don't want to do any range analysis of such
1292 -- expressions for two reasons. First, the bounds of a generic type
1293 -- itself are junk and cannot be used for any kind of analysis.
1294 -- Second, we may have a case where the range at run time is indeed
1295 -- known, but we don't want to do compile time analysis in the
1296 -- template based on that range since in an instance the value may be
1297 -- static, and able to be elaborated without reference to the bounds
1298 -- of types involved. As an example, consider:
1300 -- (F'Pos (F'Last) + 1) > Integer'Last
1302 -- The expression on the left side of > is Universal_Integer and thus
1303 -- acquires the type Integer for evaluation at run time, and at run
1304 -- time it is true that this condition is always False, but within
1305 -- an instance F may be a type with a static range greater than the
1306 -- range of Integer, and the expression statically evaluates to True.
1308 if References_Generic_Formal_Type (L)
1309 or else
1310 References_Generic_Formal_Type (R)
1311 then
1312 return Unknown;
1313 end if;
1315 -- Replace types by base types for the case of values which are not
1316 -- known to have valid representations. This takes care of properly
1317 -- dealing with invalid representations.
1319 if not Assume_Valid then
1320 if not (Is_Entity_Name (L)
1321 and then (Is_Known_Valid (Entity (L))
1322 or else Assume_No_Invalid_Values))
1323 then
1324 Ltyp := Underlying_Type (Base_Type (Ltyp));
1325 end if;
1327 if not (Is_Entity_Name (R)
1328 and then (Is_Known_Valid (Entity (R))
1329 or else Assume_No_Invalid_Values))
1330 then
1331 Rtyp := Underlying_Type (Base_Type (Rtyp));
1332 end if;
1333 end if;
1335 -- First attempt is to decompose the expressions to extract a
1336 -- constant offset resulting from the use of any of the forms:
1338 -- expr + literal
1339 -- expr - literal
1340 -- typ'Succ (expr)
1341 -- typ'Pred (expr)
1343 -- Then we see if the two expressions are the same value, and if so
1344 -- the result is obtained by comparing the offsets.
1346 -- Note: the reason we do this test first is that it returns only
1347 -- decisive results (with diff set), where other tests, like the
1348 -- range test, may not be as so decisive. Consider for example
1349 -- J .. J + 1. This code can conclude LT with a difference of 1,
1350 -- even if the range of J is not known.
1352 declare
1353 Lnode : Node_Id;
1354 Loffs : Uint;
1355 Rnode : Node_Id;
1356 Roffs : Uint;
1358 begin
1359 Compare_Decompose (L, Lnode, Loffs);
1360 Compare_Decompose (R, Rnode, Roffs);
1362 if Is_Same_Value (Lnode, Rnode) then
1363 if Loffs = Roffs then
1364 return EQ;
1365 end if;
1367 -- When the offsets are not equal, we can go farther only if
1368 -- the types are not modular (e.g. X < X + 1 is False if X is
1369 -- the largest number).
1371 if not Is_Modular_Integer_Type (Ltyp)
1372 and then not Is_Modular_Integer_Type (Rtyp)
1373 then
1374 if Loffs < Roffs then
1375 Diff.all := Roffs - Loffs;
1376 return LT;
1377 else
1378 Diff.all := Loffs - Roffs;
1379 return GT;
1380 end if;
1381 end if;
1382 end if;
1383 end;
1385 -- Next, try range analysis and see if operand ranges are disjoint
1387 declare
1388 LOK, ROK : Boolean;
1389 LLo, LHi : Uint;
1390 RLo, RHi : Uint;
1392 Single : Boolean;
1393 -- True if each range is a single point
1395 begin
1396 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1397 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1399 if LOK and ROK then
1400 Single := (LLo = LHi) and then (RLo = RHi);
1402 if LHi < RLo then
1403 if Single and Assume_Valid then
1404 Diff.all := RLo - LLo;
1405 end if;
1407 return LT;
1409 elsif RHi < LLo then
1410 if Single and Assume_Valid then
1411 Diff.all := LLo - RLo;
1412 end if;
1414 return GT;
1416 elsif Single and then LLo = RLo then
1418 -- If the range includes a single literal and we can assume
1419 -- validity then the result is known even if an operand is
1420 -- not static.
1422 if Assume_Valid then
1423 return EQ;
1424 else
1425 return Unknown;
1426 end if;
1428 elsif LHi = RLo then
1429 return LE;
1431 elsif RHi = LLo then
1432 return GE;
1434 elsif not Is_Known_Valid_Operand (L)
1435 and then not Assume_Valid
1436 then
1437 if Is_Same_Value (L, R) then
1438 return EQ;
1439 else
1440 return Unknown;
1441 end if;
1442 end if;
1444 -- If the range of either operand cannot be determined, nothing
1445 -- further can be inferred.
1447 else
1448 return Unknown;
1449 end if;
1450 end;
1452 -- Here is where we check for comparisons against maximum bounds of
1453 -- types, where we know that no value can be outside the bounds of
1454 -- the subtype. Note that this routine is allowed to assume that all
1455 -- expressions are within their subtype bounds. Callers wishing to
1456 -- deal with possibly invalid values must in any case take special
1457 -- steps (e.g. conversions to larger types) to avoid this kind of
1458 -- optimization, which is always considered to be valid. We do not
1459 -- attempt this optimization with generic types, since the type
1460 -- bounds may not be meaningful in this case.
1462 -- We are in danger of an infinite recursion here. It does not seem
1463 -- useful to go more than one level deep, so the parameter Rec is
1464 -- used to protect ourselves against this infinite recursion.
1466 if not Rec then
1468 -- See if we can get a decisive check against one operand and a
1469 -- bound of the other operand (four possible tests here). Note
1470 -- that we avoid testing junk bounds of a generic type.
1472 if not Is_Generic_Type (Rtyp) then
1473 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1474 Discard'Access,
1475 Assume_Valid, Rec => True)
1477 when LT => return LT;
1478 when LE => return LE;
1479 when EQ => return LE;
1480 when others => null;
1481 end case;
1483 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1484 Discard'Access,
1485 Assume_Valid, Rec => True)
1487 when GT => return GT;
1488 when GE => return GE;
1489 when EQ => return GE;
1490 when others => null;
1491 end case;
1492 end if;
1494 if not Is_Generic_Type (Ltyp) then
1495 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1496 Discard'Access,
1497 Assume_Valid, Rec => True)
1499 when GT => return GT;
1500 when GE => return GE;
1501 when EQ => return GE;
1502 when others => null;
1503 end case;
1505 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1506 Discard'Access,
1507 Assume_Valid, Rec => True)
1509 when LT => return LT;
1510 when LE => return LE;
1511 when EQ => return LE;
1512 when others => null;
1513 end case;
1514 end if;
1515 end if;
1517 -- Next attempt is to see if we have an entity compared with a
1518 -- compile time known value, where there is a current value
1519 -- conditional for the entity which can tell us the result.
1521 declare
1522 Var : Node_Id;
1523 -- Entity variable (left operand)
1525 Val : Uint;
1526 -- Value (right operand)
1528 Inv : Boolean;
1529 -- If False, we have reversed the operands
1531 Op : Node_Kind;
1532 -- Comparison operator kind from Get_Current_Value_Condition call
1534 Opn : Node_Id;
1535 -- Value from Get_Current_Value_Condition call
1537 Opv : Uint;
1538 -- Value of Opn
1540 Result : Compare_Result;
1541 -- Known result before inversion
1543 begin
1544 if Is_Entity_Name (L)
1545 and then Compile_Time_Known_Value (R)
1546 then
1547 Var := L;
1548 Val := Expr_Value (R);
1549 Inv := False;
1551 elsif Is_Entity_Name (R)
1552 and then Compile_Time_Known_Value (L)
1553 then
1554 Var := R;
1555 Val := Expr_Value (L);
1556 Inv := True;
1558 -- That was the last chance at finding a compile time result
1560 else
1561 return Unknown;
1562 end if;
1564 Get_Current_Value_Condition (Var, Op, Opn);
1566 -- That was the last chance, so if we got nothing return
1568 if No (Opn) then
1569 return Unknown;
1570 end if;
1572 Opv := Expr_Value (Opn);
1574 -- We got a comparison, so we might have something interesting
1576 -- Convert LE to LT and GE to GT, just so we have fewer cases
1578 if Op = N_Op_Le then
1579 Op := N_Op_Lt;
1580 Opv := Opv + 1;
1582 elsif Op = N_Op_Ge then
1583 Op := N_Op_Gt;
1584 Opv := Opv - 1;
1585 end if;
1587 -- Deal with equality case
1589 if Op = N_Op_Eq then
1590 if Val = Opv then
1591 Result := EQ;
1592 elsif Opv < Val then
1593 Result := LT;
1594 else
1595 Result := GT;
1596 end if;
1598 -- Deal with inequality case
1600 elsif Op = N_Op_Ne then
1601 if Val = Opv then
1602 Result := NE;
1603 else
1604 return Unknown;
1605 end if;
1607 -- Deal with greater than case
1609 elsif Op = N_Op_Gt then
1610 if Opv >= Val then
1611 Result := GT;
1612 elsif Opv = Val - 1 then
1613 Result := GE;
1614 else
1615 return Unknown;
1616 end if;
1618 -- Deal with less than case
1620 else pragma Assert (Op = N_Op_Lt);
1621 if Opv <= Val then
1622 Result := LT;
1623 elsif Opv = Val + 1 then
1624 Result := LE;
1625 else
1626 return Unknown;
1627 end if;
1628 end if;
1630 -- Deal with inverting result
1632 if Inv then
1633 case Result is
1634 when GT => return LT;
1635 when GE => return LE;
1636 when LT => return GT;
1637 when LE => return GE;
1638 when others => return Result;
1639 end case;
1640 end if;
1642 return Result;
1643 end;
1644 end if;
1645 end Compile_Time_Compare;
1647 -------------------------------
1648 -- Compile_Time_Known_Bounds --
1649 -------------------------------
1651 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1652 Indx : Node_Id;
1653 Typ : Entity_Id;
1655 begin
1656 if T = Any_Composite or else not Is_Array_Type (T) then
1657 return False;
1658 end if;
1660 Indx := First_Index (T);
1661 while Present (Indx) loop
1662 Typ := Underlying_Type (Etype (Indx));
1664 -- Never look at junk bounds of a generic type
1666 if Is_Generic_Type (Typ) then
1667 return False;
1668 end if;
1670 -- Otherwise check bounds for compile time known
1672 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1673 return False;
1674 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1675 return False;
1676 else
1677 Next_Index (Indx);
1678 end if;
1679 end loop;
1681 return True;
1682 end Compile_Time_Known_Bounds;
1684 ------------------------------
1685 -- Compile_Time_Known_Value --
1686 ------------------------------
1688 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1689 K : constant Node_Kind := Nkind (Op);
1690 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1692 begin
1693 -- Never known at compile time if bad type or raises constraint error
1694 -- or empty (latter case occurs only as a result of a previous error).
1696 if No (Op) then
1697 Check_Error_Detected;
1698 return False;
1700 elsif Op = Error
1701 or else Etype (Op) = Any_Type
1702 or else Raises_Constraint_Error (Op)
1703 then
1704 return False;
1705 end if;
1707 -- If we have an entity name, then see if it is the name of a constant
1708 -- and if so, test the corresponding constant value, or the name of
1709 -- an enumeration literal, which is always a constant.
1711 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1712 declare
1713 E : constant Entity_Id := Entity (Op);
1714 V : Node_Id;
1716 begin
1717 -- Never known at compile time if it is a packed array value.
1718 -- We might want to try to evaluate these at compile time one
1719 -- day, but we do not make that attempt now.
1721 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1722 return False;
1723 end if;
1725 if Ekind (E) = E_Enumeration_Literal then
1726 return True;
1728 elsif Ekind (E) = E_Constant then
1729 V := Constant_Value (E);
1730 return Present (V) and then Compile_Time_Known_Value (V);
1731 end if;
1732 end;
1734 -- We have a value, see if it is compile time known
1736 else
1737 -- Integer literals are worth storing in the cache
1739 if K = N_Integer_Literal then
1740 CV_Ent.N := Op;
1741 CV_Ent.V := Intval (Op);
1742 return True;
1744 -- Other literals and NULL are known at compile time
1746 elsif
1747 Nkind_In (K, N_Character_Literal,
1748 N_Real_Literal,
1749 N_String_Literal,
1750 N_Null)
1751 then
1752 return True;
1753 end if;
1754 end if;
1756 -- If we fall through, not known at compile time
1758 return False;
1760 -- If we get an exception while trying to do this test, then some error
1761 -- has occurred, and we simply say that the value is not known after all
1763 exception
1764 when others =>
1765 return False;
1766 end Compile_Time_Known_Value;
1768 --------------------------------------
1769 -- Compile_Time_Known_Value_Or_Aggr --
1770 --------------------------------------
1772 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1773 begin
1774 -- If we have an entity name, then see if it is the name of a constant
1775 -- and if so, test the corresponding constant value, or the name of
1776 -- an enumeration literal, which is always a constant.
1778 if Is_Entity_Name (Op) then
1779 declare
1780 E : constant Entity_Id := Entity (Op);
1781 V : Node_Id;
1783 begin
1784 if Ekind (E) = E_Enumeration_Literal then
1785 return True;
1787 elsif Ekind (E) /= E_Constant then
1788 return False;
1790 else
1791 V := Constant_Value (E);
1792 return Present (V)
1793 and then Compile_Time_Known_Value_Or_Aggr (V);
1794 end if;
1795 end;
1797 -- We have a value, see if it is compile time known
1799 else
1800 if Compile_Time_Known_Value (Op) then
1801 return True;
1803 elsif Nkind (Op) = N_Aggregate then
1805 if Present (Expressions (Op)) then
1806 declare
1807 Expr : Node_Id;
1808 begin
1809 Expr := First (Expressions (Op));
1810 while Present (Expr) loop
1811 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1812 return False;
1813 else
1814 Next (Expr);
1815 end if;
1816 end loop;
1817 end;
1818 end if;
1820 if Present (Component_Associations (Op)) then
1821 declare
1822 Cass : Node_Id;
1824 begin
1825 Cass := First (Component_Associations (Op));
1826 while Present (Cass) loop
1827 if not
1828 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1829 then
1830 return False;
1831 end if;
1833 Next (Cass);
1834 end loop;
1835 end;
1836 end if;
1838 return True;
1840 elsif Nkind (Op) = N_Qualified_Expression then
1841 return Compile_Time_Known_Value_Or_Aggr (Expression (Op));
1843 -- All other types of values are not known at compile time
1845 else
1846 return False;
1847 end if;
1849 end if;
1850 end Compile_Time_Known_Value_Or_Aggr;
1852 ---------------------------------------
1853 -- CRT_Safe_Compile_Time_Known_Value --
1854 ---------------------------------------
1856 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1857 begin
1858 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1859 and then not Is_OK_Static_Expression (Op)
1860 then
1861 return False;
1862 else
1863 return Compile_Time_Known_Value (Op);
1864 end if;
1865 end CRT_Safe_Compile_Time_Known_Value;
1867 -----------------
1868 -- Eval_Actual --
1869 -----------------
1871 -- This is only called for actuals of functions that are not predefined
1872 -- operators (which have already been rewritten as operators at this
1873 -- stage), so the call can never be folded, and all that needs doing for
1874 -- the actual is to do the check for a non-static context.
1876 procedure Eval_Actual (N : Node_Id) is
1877 begin
1878 Check_Non_Static_Context (N);
1879 end Eval_Actual;
1881 --------------------
1882 -- Eval_Allocator --
1883 --------------------
1885 -- Allocators are never static, so all we have to do is to do the
1886 -- check for a non-static context if an expression is present.
1888 procedure Eval_Allocator (N : Node_Id) is
1889 Expr : constant Node_Id := Expression (N);
1890 begin
1891 if Nkind (Expr) = N_Qualified_Expression then
1892 Check_Non_Static_Context (Expression (Expr));
1893 end if;
1894 end Eval_Allocator;
1896 ------------------------
1897 -- Eval_Arithmetic_Op --
1898 ------------------------
1900 -- Arithmetic operations are static functions, so the result is static
1901 -- if both operands are static (RM 4.9(7), 4.9(20)).
1903 procedure Eval_Arithmetic_Op (N : Node_Id) is
1904 Left : constant Node_Id := Left_Opnd (N);
1905 Right : constant Node_Id := Right_Opnd (N);
1906 Ltype : constant Entity_Id := Etype (Left);
1907 Rtype : constant Entity_Id := Etype (Right);
1908 Otype : Entity_Id := Empty;
1909 Stat : Boolean;
1910 Fold : Boolean;
1912 begin
1913 -- If not foldable we are done
1915 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1917 if not Fold then
1918 return;
1919 end if;
1921 -- Otherwise attempt to fold
1923 if Is_Universal_Numeric_Type (Etype (Left))
1924 and then
1925 Is_Universal_Numeric_Type (Etype (Right))
1926 then
1927 Otype := Find_Universal_Operator_Type (N);
1928 end if;
1930 -- Fold for cases where both operands are of integer type
1932 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1933 declare
1934 Left_Int : constant Uint := Expr_Value (Left);
1935 Right_Int : constant Uint := Expr_Value (Right);
1936 Result : Uint;
1938 begin
1939 case Nkind (N) is
1940 when N_Op_Add =>
1941 Result := Left_Int + Right_Int;
1943 when N_Op_Subtract =>
1944 Result := Left_Int - Right_Int;
1946 when N_Op_Multiply =>
1947 if OK_Bits
1948 (N, UI_From_Int
1949 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1950 then
1951 Result := Left_Int * Right_Int;
1952 else
1953 Result := Left_Int;
1954 end if;
1956 when N_Op_Divide =>
1958 -- The exception Constraint_Error is raised by integer
1959 -- division, rem and mod if the right operand is zero.
1961 if Right_Int = 0 then
1963 -- When SPARK_Mode is On, force a warning instead of
1964 -- an error in that case, as this likely corresponds
1965 -- to deactivated code.
1967 Apply_Compile_Time_Constraint_Error
1968 (N, "division by zero", CE_Divide_By_Zero,
1969 Warn => not Stat or SPARK_Mode = On);
1970 Set_Raises_Constraint_Error (N);
1971 return;
1973 -- Otherwise we can do the division
1975 else
1976 Result := Left_Int / Right_Int;
1977 end if;
1979 when N_Op_Mod =>
1981 -- The exception Constraint_Error is raised by integer
1982 -- division, rem and mod if the right operand is zero.
1984 if Right_Int = 0 then
1986 -- When SPARK_Mode is On, force a warning instead of
1987 -- an error in that case, as this likely corresponds
1988 -- to deactivated code.
1990 Apply_Compile_Time_Constraint_Error
1991 (N, "mod with zero divisor", CE_Divide_By_Zero,
1992 Warn => not Stat or SPARK_Mode = On);
1993 return;
1995 else
1996 Result := Left_Int mod Right_Int;
1997 end if;
1999 when N_Op_Rem =>
2001 -- The exception Constraint_Error is raised by integer
2002 -- division, rem and mod if the right operand is zero.
2004 if Right_Int = 0 then
2006 -- When SPARK_Mode is On, force a warning instead of
2007 -- an error in that case, as this likely corresponds
2008 -- to deactivated code.
2010 Apply_Compile_Time_Constraint_Error
2011 (N, "rem with zero divisor", CE_Divide_By_Zero,
2012 Warn => not Stat or SPARK_Mode = On);
2013 return;
2015 else
2016 Result := Left_Int rem Right_Int;
2017 end if;
2019 when others =>
2020 raise Program_Error;
2021 end case;
2023 -- Adjust the result by the modulus if the type is a modular type
2025 if Is_Modular_Integer_Type (Ltype) then
2026 Result := Result mod Modulus (Ltype);
2028 -- For a signed integer type, check non-static overflow
2030 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
2031 declare
2032 BT : constant Entity_Id := Base_Type (Ltype);
2033 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
2034 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
2035 begin
2036 if Result < Lo or else Result > Hi then
2037 Apply_Compile_Time_Constraint_Error
2038 (N, "value not in range of }??",
2039 CE_Overflow_Check_Failed,
2040 Ent => BT);
2041 return;
2042 end if;
2043 end;
2044 end if;
2046 -- If we get here we can fold the result
2048 Fold_Uint (N, Result, Stat);
2049 end;
2051 -- Cases where at least one operand is a real. We handle the cases of
2052 -- both reals, or mixed/real integer cases (the latter happen only for
2053 -- divide and multiply, and the result is always real).
2055 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2056 declare
2057 Left_Real : Ureal;
2058 Right_Real : Ureal;
2059 Result : Ureal;
2061 begin
2062 if Is_Real_Type (Ltype) then
2063 Left_Real := Expr_Value_R (Left);
2064 else
2065 Left_Real := UR_From_Uint (Expr_Value (Left));
2066 end if;
2068 if Is_Real_Type (Rtype) then
2069 Right_Real := Expr_Value_R (Right);
2070 else
2071 Right_Real := UR_From_Uint (Expr_Value (Right));
2072 end if;
2074 if Nkind (N) = N_Op_Add then
2075 Result := Left_Real + Right_Real;
2077 elsif Nkind (N) = N_Op_Subtract then
2078 Result := Left_Real - Right_Real;
2080 elsif Nkind (N) = N_Op_Multiply then
2081 Result := Left_Real * Right_Real;
2083 else pragma Assert (Nkind (N) = N_Op_Divide);
2084 if UR_Is_Zero (Right_Real) then
2085 Apply_Compile_Time_Constraint_Error
2086 (N, "division by zero", CE_Divide_By_Zero);
2087 return;
2088 end if;
2090 Result := Left_Real / Right_Real;
2091 end if;
2093 Fold_Ureal (N, Result, Stat);
2094 end;
2095 end if;
2097 -- If the operator was resolved to a specific type, make sure that type
2098 -- is frozen even if the expression is folded into a literal (which has
2099 -- a universal type).
2101 if Present (Otype) then
2102 Freeze_Before (N, Otype);
2103 end if;
2104 end Eval_Arithmetic_Op;
2106 ----------------------------
2107 -- Eval_Character_Literal --
2108 ----------------------------
2110 -- Nothing to be done
2112 procedure Eval_Character_Literal (N : Node_Id) is
2113 pragma Warnings (Off, N);
2114 begin
2115 null;
2116 end Eval_Character_Literal;
2118 ---------------
2119 -- Eval_Call --
2120 ---------------
2122 -- Static function calls are either calls to predefined operators
2123 -- with static arguments, or calls to functions that rename a literal.
2124 -- Only the latter case is handled here, predefined operators are
2125 -- constant-folded elsewhere.
2127 -- If the function is itself inherited (see 7423-001) the literal of
2128 -- the parent type must be explicitly converted to the return type
2129 -- of the function.
2131 procedure Eval_Call (N : Node_Id) is
2132 Loc : constant Source_Ptr := Sloc (N);
2133 Typ : constant Entity_Id := Etype (N);
2134 Lit : Entity_Id;
2136 begin
2137 if Nkind (N) = N_Function_Call
2138 and then No (Parameter_Associations (N))
2139 and then Is_Entity_Name (Name (N))
2140 and then Present (Alias (Entity (Name (N))))
2141 and then Is_Enumeration_Type (Base_Type (Typ))
2142 then
2143 Lit := Ultimate_Alias (Entity (Name (N)));
2145 if Ekind (Lit) = E_Enumeration_Literal then
2146 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2147 Rewrite
2148 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2149 else
2150 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2151 end if;
2153 Resolve (N, Typ);
2154 end if;
2155 end if;
2156 end Eval_Call;
2158 --------------------------
2159 -- Eval_Case_Expression --
2160 --------------------------
2162 -- A conditional expression is static if all its conditions and dependent
2163 -- expressions are static. Note that we do not care if the dependent
2164 -- expressions raise CE, except for the one that will be selected.
2166 procedure Eval_Case_Expression (N : Node_Id) is
2167 Alt : Node_Id;
2168 Choice : Node_Id;
2170 begin
2171 Set_Is_Static_Expression (N, False);
2173 if Error_Posted (Expression (N))
2174 or else not Is_Static_Expression (Expression (N))
2175 then
2176 Check_Non_Static_Context (Expression (N));
2177 return;
2178 end if;
2180 -- First loop, make sure all the alternatives are static expressions
2181 -- none of which raise Constraint_Error. We make the constraint error
2182 -- check because part of the legality condition for a correct static
2183 -- case expression is that the cases are covered, like any other case
2184 -- expression. And we can't do that if any of the conditions raise an
2185 -- exception, so we don't even try to evaluate if that is the case.
2187 Alt := First (Alternatives (N));
2188 while Present (Alt) loop
2190 -- The expression must be static, but we don't care at this stage
2191 -- if it raises Constraint_Error (the alternative might not match,
2192 -- in which case the expression is statically unevaluated anyway).
2194 if not Is_Static_Expression (Expression (Alt)) then
2195 Check_Non_Static_Context (Expression (Alt));
2196 return;
2197 end if;
2199 -- The choices of a case always have to be static, and cannot raise
2200 -- an exception. If this condition is not met, then the expression
2201 -- is plain illegal, so just abandon evaluation attempts. No need
2202 -- to check non-static context when we have something illegal anyway.
2204 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2205 return;
2206 end if;
2208 Next (Alt);
2209 end loop;
2211 -- OK, if the above loop gets through it means that all choices are OK
2212 -- static (don't raise exceptions), so the whole case is static, and we
2213 -- can find the matching alternative.
2215 Set_Is_Static_Expression (N);
2217 -- Now to deal with propagating a possible constraint error
2219 -- If the selecting expression raises CE, propagate and we are done
2221 if Raises_Constraint_Error (Expression (N)) then
2222 Set_Raises_Constraint_Error (N);
2224 -- Otherwise we need to check the alternatives to find the matching
2225 -- one. CE's in other than the matching one are not relevant. But we
2226 -- do need to check the matching one. Unlike the first loop, we do not
2227 -- have to go all the way through, when we find the matching one, quit.
2229 else
2230 Alt := First (Alternatives (N));
2231 Search : loop
2233 -- We must find a match among the alternatives. If not, this must
2234 -- be due to other errors, so just ignore, leaving as non-static.
2236 if No (Alt) then
2237 Set_Is_Static_Expression (N, False);
2238 return;
2239 end if;
2241 -- Otherwise loop through choices of this alternative
2243 Choice := First (Discrete_Choices (Alt));
2244 while Present (Choice) loop
2246 -- If we find a matching choice, then the Expression of this
2247 -- alternative replaces N (Raises_Constraint_Error flag is
2248 -- included, so we don't have to special case that).
2250 if Choice_Matches (Expression (N), Choice) = Match then
2251 Rewrite (N, Relocate_Node (Expression (Alt)));
2252 return;
2253 end if;
2255 Next (Choice);
2256 end loop;
2258 Next (Alt);
2259 end loop Search;
2260 end if;
2261 end Eval_Case_Expression;
2263 ------------------------
2264 -- Eval_Concatenation --
2265 ------------------------
2267 -- Concatenation is a static function, so the result is static if both
2268 -- operands are static (RM 4.9(7), 4.9(21)).
2270 procedure Eval_Concatenation (N : Node_Id) is
2271 Left : constant Node_Id := Left_Opnd (N);
2272 Right : constant Node_Id := Right_Opnd (N);
2273 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2274 Stat : Boolean;
2275 Fold : Boolean;
2277 begin
2278 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2279 -- non-static context.
2281 if Ada_Version = Ada_83
2282 and then Comes_From_Source (N)
2283 then
2284 Check_Non_Static_Context (Left);
2285 Check_Non_Static_Context (Right);
2286 return;
2287 end if;
2289 -- If not foldable we are done. In principle concatenation that yields
2290 -- any string type is static (i.e. an array type of character types).
2291 -- However, character types can include enumeration literals, and
2292 -- concatenation in that case cannot be described by a literal, so we
2293 -- only consider the operation static if the result is an array of
2294 -- (a descendant of) a predefined character type.
2296 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2298 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2299 Set_Is_Static_Expression (N, False);
2300 return;
2301 end if;
2303 -- Compile time string concatenation
2305 -- ??? Note that operands that are aggregates can be marked as static,
2306 -- so we should attempt at a later stage to fold concatenations with
2307 -- such aggregates.
2309 declare
2310 Left_Str : constant Node_Id := Get_String_Val (Left);
2311 Left_Len : Nat;
2312 Right_Str : constant Node_Id := Get_String_Val (Right);
2313 Folded_Val : String_Id := No_String;
2315 begin
2316 -- Establish new string literal, and store left operand. We make
2317 -- sure to use the special Start_String that takes an operand if
2318 -- the left operand is a string literal. Since this is optimized
2319 -- in the case where that is the most recently created string
2320 -- literal, we ensure efficient time/space behavior for the
2321 -- case of a concatenation of a series of string literals.
2323 if Nkind (Left_Str) = N_String_Literal then
2324 Left_Len := String_Length (Strval (Left_Str));
2326 -- If the left operand is the empty string, and the right operand
2327 -- is a string literal (the case of "" & "..."), the result is the
2328 -- value of the right operand. This optimization is important when
2329 -- Is_Folded_In_Parser, to avoid copying an enormous right
2330 -- operand.
2332 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2333 Folded_Val := Strval (Right_Str);
2334 else
2335 Start_String (Strval (Left_Str));
2336 end if;
2338 else
2339 Start_String;
2340 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2341 Left_Len := 1;
2342 end if;
2344 -- Now append the characters of the right operand, unless we
2345 -- optimized the "" & "..." case above.
2347 if Nkind (Right_Str) = N_String_Literal then
2348 if Left_Len /= 0 then
2349 Store_String_Chars (Strval (Right_Str));
2350 Folded_Val := End_String;
2351 end if;
2352 else
2353 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2354 Folded_Val := End_String;
2355 end if;
2357 Set_Is_Static_Expression (N, Stat);
2359 -- If left operand is the empty string, the result is the
2360 -- right operand, including its bounds if anomalous.
2362 if Left_Len = 0
2363 and then Is_Array_Type (Etype (Right))
2364 and then Etype (Right) /= Any_String
2365 then
2366 Set_Etype (N, Etype (Right));
2367 end if;
2369 Fold_Str (N, Folded_Val, Static => Stat);
2370 end;
2371 end Eval_Concatenation;
2373 ----------------------
2374 -- Eval_Entity_Name --
2375 ----------------------
2377 -- This procedure is used for identifiers and expanded names other than
2378 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2379 -- static if they denote a static constant (RM 4.9(6)) or if the name
2380 -- denotes an enumeration literal (RM 4.9(22)).
2382 procedure Eval_Entity_Name (N : Node_Id) is
2383 Def_Id : constant Entity_Id := Entity (N);
2384 Val : Node_Id;
2386 begin
2387 -- Enumeration literals are always considered to be constants
2388 -- and cannot raise constraint error (RM 4.9(22)).
2390 if Ekind (Def_Id) = E_Enumeration_Literal then
2391 Set_Is_Static_Expression (N);
2392 return;
2394 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2395 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2396 -- it does not violate 10.2.1(8) here, since this is not a variable.
2398 elsif Ekind (Def_Id) = E_Constant then
2400 -- Deferred constants must always be treated as nonstatic outside the
2401 -- scope of their full view.
2403 if Present (Full_View (Def_Id))
2404 and then not In_Open_Scopes (Scope (Def_Id))
2405 then
2406 Val := Empty;
2407 else
2408 Val := Constant_Value (Def_Id);
2409 end if;
2411 if Present (Val) then
2412 Set_Is_Static_Expression
2413 (N, Is_Static_Expression (Val)
2414 and then Is_Static_Subtype (Etype (Def_Id)));
2415 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2417 if not Is_Static_Expression (N)
2418 and then not Is_Generic_Type (Etype (N))
2419 then
2420 Validate_Static_Object_Name (N);
2421 end if;
2423 -- Mark constant condition in SCOs
2425 if Generate_SCO
2426 and then Comes_From_Source (N)
2427 and then Is_Boolean_Type (Etype (Def_Id))
2428 and then Compile_Time_Known_Value (N)
2429 then
2430 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2431 end if;
2433 return;
2434 end if;
2435 end if;
2437 -- Fall through if the name is not static
2439 Validate_Static_Object_Name (N);
2440 end Eval_Entity_Name;
2442 ------------------------
2443 -- Eval_If_Expression --
2444 ------------------------
2446 -- We can fold to a static expression if the condition and both dependent
2447 -- expressions are static. Otherwise, the only required processing is to do
2448 -- the check for non-static context for the then and else expressions.
2450 procedure Eval_If_Expression (N : Node_Id) is
2451 Condition : constant Node_Id := First (Expressions (N));
2452 Then_Expr : constant Node_Id := Next (Condition);
2453 Else_Expr : constant Node_Id := Next (Then_Expr);
2454 Result : Node_Id;
2455 Non_Result : Node_Id;
2457 Rstat : constant Boolean :=
2458 Is_Static_Expression (Condition)
2459 and then
2460 Is_Static_Expression (Then_Expr)
2461 and then
2462 Is_Static_Expression (Else_Expr);
2463 -- True if result is static
2465 begin
2466 -- If result not static, nothing to do, otherwise set static result
2468 if not Rstat then
2469 return;
2470 else
2471 Set_Is_Static_Expression (N);
2472 end if;
2474 -- If any operand is Any_Type, just propagate to result and do not try
2475 -- to fold, this prevents cascaded errors.
2477 if Etype (Condition) = Any_Type or else
2478 Etype (Then_Expr) = Any_Type or else
2479 Etype (Else_Expr) = Any_Type
2480 then
2481 Set_Etype (N, Any_Type);
2482 Set_Is_Static_Expression (N, False);
2483 return;
2484 end if;
2486 -- If condition raises constraint error then we have already signaled
2487 -- an error, and we just propagate to the result and do not fold.
2489 if Raises_Constraint_Error (Condition) then
2490 Set_Raises_Constraint_Error (N);
2491 return;
2492 end if;
2494 -- Static case where we can fold. Note that we don't try to fold cases
2495 -- where the condition is known at compile time, but the result is
2496 -- non-static. This avoids possible cases of infinite recursion where
2497 -- the expander puts in a redundant test and we remove it. Instead we
2498 -- deal with these cases in the expander.
2500 -- Select result operand
2502 if Is_True (Expr_Value (Condition)) then
2503 Result := Then_Expr;
2504 Non_Result := Else_Expr;
2505 else
2506 Result := Else_Expr;
2507 Non_Result := Then_Expr;
2508 end if;
2510 -- Note that it does not matter if the non-result operand raises a
2511 -- Constraint_Error, but if the result raises constraint error then we
2512 -- replace the node with a raise constraint error. This will properly
2513 -- propagate Raises_Constraint_Error since this flag is set in Result.
2515 if Raises_Constraint_Error (Result) then
2516 Rewrite_In_Raise_CE (N, Result);
2517 Check_Non_Static_Context (Non_Result);
2519 -- Otherwise the result operand replaces the original node
2521 else
2522 Rewrite (N, Relocate_Node (Result));
2523 Set_Is_Static_Expression (N);
2524 end if;
2525 end Eval_If_Expression;
2527 ----------------------------
2528 -- Eval_Indexed_Component --
2529 ----------------------------
2531 -- Indexed components are never static, so we need to perform the check
2532 -- for non-static context on the index values. Then, we check if the
2533 -- value can be obtained at compile time, even though it is non-static.
2535 procedure Eval_Indexed_Component (N : Node_Id) is
2536 Expr : Node_Id;
2538 begin
2539 -- Check for non-static context on index values
2541 Expr := First (Expressions (N));
2542 while Present (Expr) loop
2543 Check_Non_Static_Context (Expr);
2544 Next (Expr);
2545 end loop;
2547 -- If the indexed component appears in an object renaming declaration
2548 -- then we do not want to try to evaluate it, since in this case we
2549 -- need the identity of the array element.
2551 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2552 return;
2554 -- Similarly if the indexed component appears as the prefix of an
2555 -- attribute we don't want to evaluate it, because at least for
2556 -- some cases of attributes we need the identify (e.g. Access, Size)
2558 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2559 return;
2560 end if;
2562 -- Note: there are other cases, such as the left side of an assignment,
2563 -- or an OUT parameter for a call, where the replacement results in the
2564 -- illegal use of a constant, But these cases are illegal in the first
2565 -- place, so the replacement, though silly, is harmless.
2567 -- Now see if this is a constant array reference
2569 if List_Length (Expressions (N)) = 1
2570 and then Is_Entity_Name (Prefix (N))
2571 and then Ekind (Entity (Prefix (N))) = E_Constant
2572 and then Present (Constant_Value (Entity (Prefix (N))))
2573 then
2574 declare
2575 Loc : constant Source_Ptr := Sloc (N);
2576 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2577 Sub : constant Node_Id := First (Expressions (N));
2579 Atyp : Entity_Id;
2580 -- Type of array
2582 Lin : Nat;
2583 -- Linear one's origin subscript value for array reference
2585 Lbd : Node_Id;
2586 -- Lower bound of the first array index
2588 Elm : Node_Id;
2589 -- Value from constant array
2591 begin
2592 Atyp := Etype (Arr);
2594 if Is_Access_Type (Atyp) then
2595 Atyp := Designated_Type (Atyp);
2596 end if;
2598 -- If we have an array type (we should have but perhaps there are
2599 -- error cases where this is not the case), then see if we can do
2600 -- a constant evaluation of the array reference.
2602 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2603 if Ekind (Atyp) = E_String_Literal_Subtype then
2604 Lbd := String_Literal_Low_Bound (Atyp);
2605 else
2606 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2607 end if;
2609 if Compile_Time_Known_Value (Sub)
2610 and then Nkind (Arr) = N_Aggregate
2611 and then Compile_Time_Known_Value (Lbd)
2612 and then Is_Discrete_Type (Component_Type (Atyp))
2613 then
2614 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2616 if List_Length (Expressions (Arr)) >= Lin then
2617 Elm := Pick (Expressions (Arr), Lin);
2619 -- If the resulting expression is compile time known,
2620 -- then we can rewrite the indexed component with this
2621 -- value, being sure to mark the result as non-static.
2622 -- We also reset the Sloc, in case this generates an
2623 -- error later on (e.g. 136'Access).
2625 if Compile_Time_Known_Value (Elm) then
2626 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2627 Set_Is_Static_Expression (N, False);
2628 Set_Sloc (N, Loc);
2629 end if;
2630 end if;
2632 -- We can also constant-fold if the prefix is a string literal.
2633 -- This will be useful in an instantiation or an inlining.
2635 elsif Compile_Time_Known_Value (Sub)
2636 and then Nkind (Arr) = N_String_Literal
2637 and then Compile_Time_Known_Value (Lbd)
2638 and then Expr_Value (Lbd) = 1
2639 and then Expr_Value (Sub) <=
2640 String_Literal_Length (Etype (Arr))
2641 then
2642 declare
2643 C : constant Char_Code :=
2644 Get_String_Char (Strval (Arr),
2645 UI_To_Int (Expr_Value (Sub)));
2646 begin
2647 Set_Character_Literal_Name (C);
2649 Elm :=
2650 Make_Character_Literal (Loc,
2651 Chars => Name_Find,
2652 Char_Literal_Value => UI_From_CC (C));
2653 Set_Etype (Elm, Component_Type (Atyp));
2654 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2655 Set_Is_Static_Expression (N, False);
2656 end;
2657 end if;
2658 end if;
2659 end;
2660 end if;
2661 end Eval_Indexed_Component;
2663 --------------------------
2664 -- Eval_Integer_Literal --
2665 --------------------------
2667 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2668 -- as static by the analyzer. The reason we did it that early is to allow
2669 -- the possibility of turning off the Is_Static_Expression flag after
2670 -- analysis, but before resolution, when integer literals are generated in
2671 -- the expander that do not correspond to static expressions.
2673 procedure Eval_Integer_Literal (N : Node_Id) is
2674 T : constant Entity_Id := Etype (N);
2676 function In_Any_Integer_Context return Boolean;
2677 -- If the literal is resolved with a specific type in a context where
2678 -- the expected type is Any_Integer, there are no range checks on the
2679 -- literal. By the time the literal is evaluated, it carries the type
2680 -- imposed by the enclosing expression, and we must recover the context
2681 -- to determine that Any_Integer is meant.
2683 ----------------------------
2684 -- In_Any_Integer_Context --
2685 ----------------------------
2687 function In_Any_Integer_Context return Boolean is
2688 Par : constant Node_Id := Parent (N);
2689 K : constant Node_Kind := Nkind (Par);
2691 begin
2692 -- Any_Integer also appears in digits specifications for real types,
2693 -- but those have bounds smaller that those of any integer base type,
2694 -- so we can safely ignore these cases.
2696 return Nkind_In (K, N_Number_Declaration,
2697 N_Attribute_Reference,
2698 N_Attribute_Definition_Clause,
2699 N_Modular_Type_Definition,
2700 N_Signed_Integer_Type_Definition);
2701 end In_Any_Integer_Context;
2703 -- Start of processing for Eval_Integer_Literal
2705 begin
2707 -- If the literal appears in a non-expression context, then it is
2708 -- certainly appearing in a non-static context, so check it. This is
2709 -- actually a redundant check, since Check_Non_Static_Context would
2710 -- check it, but it seems worthwhile to optimize out the call.
2712 -- An exception is made for a literal in an if or case expression
2714 if (Nkind_In (Parent (N), N_If_Expression, N_Case_Expression_Alternative)
2715 or else Nkind (Parent (N)) not in N_Subexpr)
2716 and then not In_Any_Integer_Context
2717 then
2718 Check_Non_Static_Context (N);
2719 end if;
2721 -- Modular integer literals must be in their base range
2723 if Is_Modular_Integer_Type (T)
2724 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
2725 then
2726 Out_Of_Range (N);
2727 end if;
2728 end Eval_Integer_Literal;
2730 ---------------------
2731 -- Eval_Logical_Op --
2732 ---------------------
2734 -- Logical operations are static functions, so the result is potentially
2735 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2737 procedure Eval_Logical_Op (N : Node_Id) is
2738 Left : constant Node_Id := Left_Opnd (N);
2739 Right : constant Node_Id := Right_Opnd (N);
2740 Stat : Boolean;
2741 Fold : Boolean;
2743 begin
2744 -- If not foldable we are done
2746 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2748 if not Fold then
2749 return;
2750 end if;
2752 -- Compile time evaluation of logical operation
2754 declare
2755 Left_Int : constant Uint := Expr_Value (Left);
2756 Right_Int : constant Uint := Expr_Value (Right);
2758 begin
2759 if Is_Modular_Integer_Type (Etype (N)) then
2760 declare
2761 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2762 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2764 begin
2765 To_Bits (Left_Int, Left_Bits);
2766 To_Bits (Right_Int, Right_Bits);
2768 -- Note: should really be able to use array ops instead of
2769 -- these loops, but they weren't working at the time ???
2771 if Nkind (N) = N_Op_And then
2772 for J in Left_Bits'Range loop
2773 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2774 end loop;
2776 elsif Nkind (N) = N_Op_Or then
2777 for J in Left_Bits'Range loop
2778 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2779 end loop;
2781 else
2782 pragma Assert (Nkind (N) = N_Op_Xor);
2784 for J in Left_Bits'Range loop
2785 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2786 end loop;
2787 end if;
2789 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2790 end;
2792 else
2793 pragma Assert (Is_Boolean_Type (Etype (N)));
2795 if Nkind (N) = N_Op_And then
2796 Fold_Uint (N,
2797 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2799 elsif Nkind (N) = N_Op_Or then
2800 Fold_Uint (N,
2801 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2803 else
2804 pragma Assert (Nkind (N) = N_Op_Xor);
2805 Fold_Uint (N,
2806 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2807 end if;
2808 end if;
2809 end;
2810 end Eval_Logical_Op;
2812 ------------------------
2813 -- Eval_Membership_Op --
2814 ------------------------
2816 -- A membership test is potentially static if the expression is static, and
2817 -- the range is a potentially static range, or is a subtype mark denoting a
2818 -- static subtype (RM 4.9(12)).
2820 procedure Eval_Membership_Op (N : Node_Id) is
2821 Alts : constant List_Id := Alternatives (N);
2822 Choice : constant Node_Id := Right_Opnd (N);
2823 Expr : constant Node_Id := Left_Opnd (N);
2824 Result : Match_Result;
2826 begin
2827 -- Ignore if error in either operand, except to make sure that Any_Type
2828 -- is properly propagated to avoid junk cascaded errors.
2830 if Etype (Expr) = Any_Type
2831 or else (Present (Choice) and then Etype (Choice) = Any_Type)
2832 then
2833 Set_Etype (N, Any_Type);
2834 return;
2835 end if;
2837 -- If left operand non-static, then nothing to do
2839 if not Is_Static_Expression (Expr) then
2840 return;
2841 end if;
2843 -- If choice is non-static, left operand is in non-static context
2845 if (Present (Choice) and then not Is_Static_Choice (Choice))
2846 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
2847 then
2848 Check_Non_Static_Context (Expr);
2849 return;
2850 end if;
2852 -- Otherwise we definitely have a static expression
2854 Set_Is_Static_Expression (N);
2856 -- If left operand raises constraint error, propagate and we are done
2858 if Raises_Constraint_Error (Expr) then
2859 Set_Raises_Constraint_Error (N, True);
2861 -- See if we match
2863 else
2864 if Present (Choice) then
2865 Result := Choice_Matches (Expr, Choice);
2866 else
2867 Result := Choices_Match (Expr, Alts);
2868 end if;
2870 -- If result is Non_Static, it means that we raise Constraint_Error,
2871 -- since we already tested that the operands were themselves static.
2873 if Result = Non_Static then
2874 Set_Raises_Constraint_Error (N);
2876 -- Otherwise we have our result (flipped if NOT IN case)
2878 else
2879 Fold_Uint
2880 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
2881 Warn_On_Known_Condition (N);
2882 end if;
2883 end if;
2884 end Eval_Membership_Op;
2886 ------------------------
2887 -- Eval_Named_Integer --
2888 ------------------------
2890 procedure Eval_Named_Integer (N : Node_Id) is
2891 begin
2892 Fold_Uint (N,
2893 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2894 end Eval_Named_Integer;
2896 ---------------------
2897 -- Eval_Named_Real --
2898 ---------------------
2900 procedure Eval_Named_Real (N : Node_Id) is
2901 begin
2902 Fold_Ureal (N,
2903 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2904 end Eval_Named_Real;
2906 -------------------
2907 -- Eval_Op_Expon --
2908 -------------------
2910 -- Exponentiation is a static functions, so the result is potentially
2911 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2913 procedure Eval_Op_Expon (N : Node_Id) is
2914 Left : constant Node_Id := Left_Opnd (N);
2915 Right : constant Node_Id := Right_Opnd (N);
2916 Stat : Boolean;
2917 Fold : Boolean;
2919 begin
2920 -- If not foldable we are done
2922 Test_Expression_Is_Foldable
2923 (N, Left, Right, Stat, Fold, CRT_Safe => True);
2925 -- Return if not foldable
2927 if not Fold then
2928 return;
2929 end if;
2931 if Configurable_Run_Time_Mode and not Stat then
2932 return;
2933 end if;
2935 -- Fold exponentiation operation
2937 declare
2938 Right_Int : constant Uint := Expr_Value (Right);
2940 begin
2941 -- Integer case
2943 if Is_Integer_Type (Etype (Left)) then
2944 declare
2945 Left_Int : constant Uint := Expr_Value (Left);
2946 Result : Uint;
2948 begin
2949 -- Exponentiation of an integer raises Constraint_Error for a
2950 -- negative exponent (RM 4.5.6).
2952 if Right_Int < 0 then
2953 Apply_Compile_Time_Constraint_Error
2954 (N, "integer exponent negative", CE_Range_Check_Failed,
2955 Warn => not Stat);
2956 return;
2958 else
2959 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2960 Result := Left_Int ** Right_Int;
2961 else
2962 Result := Left_Int;
2963 end if;
2965 if Is_Modular_Integer_Type (Etype (N)) then
2966 Result := Result mod Modulus (Etype (N));
2967 end if;
2969 Fold_Uint (N, Result, Stat);
2970 end if;
2971 end;
2973 -- Real case
2975 else
2976 declare
2977 Left_Real : constant Ureal := Expr_Value_R (Left);
2979 begin
2980 -- Cannot have a zero base with a negative exponent
2982 if UR_Is_Zero (Left_Real) then
2984 if Right_Int < 0 then
2985 Apply_Compile_Time_Constraint_Error
2986 (N, "zero ** negative integer", CE_Range_Check_Failed,
2987 Warn => not Stat);
2988 return;
2989 else
2990 Fold_Ureal (N, Ureal_0, Stat);
2991 end if;
2993 else
2994 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2995 end if;
2996 end;
2997 end if;
2998 end;
2999 end Eval_Op_Expon;
3001 -----------------
3002 -- Eval_Op_Not --
3003 -----------------
3005 -- The not operation is a static functions, so the result is potentially
3006 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3008 procedure Eval_Op_Not (N : Node_Id) is
3009 Right : constant Node_Id := Right_Opnd (N);
3010 Stat : Boolean;
3011 Fold : Boolean;
3013 begin
3014 -- If not foldable we are done
3016 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3018 if not Fold then
3019 return;
3020 end if;
3022 -- Fold not operation
3024 declare
3025 Rint : constant Uint := Expr_Value (Right);
3026 Typ : constant Entity_Id := Etype (N);
3028 begin
3029 -- Negation is equivalent to subtracting from the modulus minus one.
3030 -- For a binary modulus this is equivalent to the ones-complement of
3031 -- the original value. For a nonbinary modulus this is an arbitrary
3032 -- but consistent definition.
3034 if Is_Modular_Integer_Type (Typ) then
3035 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3036 else pragma Assert (Is_Boolean_Type (Typ));
3037 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3038 end if;
3040 Set_Is_Static_Expression (N, Stat);
3041 end;
3042 end Eval_Op_Not;
3044 -------------------------------
3045 -- Eval_Qualified_Expression --
3046 -------------------------------
3048 -- A qualified expression is potentially static if its subtype mark denotes
3049 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
3051 procedure Eval_Qualified_Expression (N : Node_Id) is
3052 Operand : constant Node_Id := Expression (N);
3053 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3055 Stat : Boolean;
3056 Fold : Boolean;
3057 Hex : Boolean;
3059 begin
3060 -- Can only fold if target is string or scalar and subtype is static.
3061 -- Also, do not fold if our parent is an allocator (this is because the
3062 -- qualified expression is really part of the syntactic structure of an
3063 -- allocator, and we do not want to end up with something that
3064 -- corresponds to "new 1" where the 1 is the result of folding a
3065 -- qualified expression).
3067 if not Is_Static_Subtype (Target_Type)
3068 or else Nkind (Parent (N)) = N_Allocator
3069 then
3070 Check_Non_Static_Context (Operand);
3072 -- If operand is known to raise constraint_error, set the flag on the
3073 -- expression so it does not get optimized away.
3075 if Nkind (Operand) = N_Raise_Constraint_Error then
3076 Set_Raises_Constraint_Error (N);
3077 end if;
3079 return;
3080 end if;
3082 -- If not foldable we are done
3084 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3086 if not Fold then
3087 return;
3089 -- Don't try fold if target type has constraint error bounds
3091 elsif not Is_OK_Static_Subtype (Target_Type) then
3092 Set_Raises_Constraint_Error (N);
3093 return;
3094 end if;
3096 -- Here we will fold, save Print_In_Hex indication
3098 Hex := Nkind (Operand) = N_Integer_Literal
3099 and then Print_In_Hex (Operand);
3101 -- Fold the result of qualification
3103 if Is_Discrete_Type (Target_Type) then
3104 Fold_Uint (N, Expr_Value (Operand), Stat);
3106 -- Preserve Print_In_Hex indication
3108 if Hex and then Nkind (N) = N_Integer_Literal then
3109 Set_Print_In_Hex (N);
3110 end if;
3112 elsif Is_Real_Type (Target_Type) then
3113 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3115 else
3116 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3118 if not Stat then
3119 Set_Is_Static_Expression (N, False);
3120 else
3121 Check_String_Literal_Length (N, Target_Type);
3122 end if;
3124 return;
3125 end if;
3127 -- The expression may be foldable but not static
3129 Set_Is_Static_Expression (N, Stat);
3131 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3132 Out_Of_Range (N);
3133 end if;
3134 end Eval_Qualified_Expression;
3136 -----------------------
3137 -- Eval_Real_Literal --
3138 -----------------------
3140 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3141 -- as static by the analyzer. The reason we did it that early is to allow
3142 -- the possibility of turning off the Is_Static_Expression flag after
3143 -- analysis, but before resolution, when integer literals are generated
3144 -- in the expander that do not correspond to static expressions.
3146 procedure Eval_Real_Literal (N : Node_Id) is
3147 PK : constant Node_Kind := Nkind (Parent (N));
3149 begin
3150 -- If the literal appears in a non-expression context and not as part of
3151 -- a number declaration, then it is appearing in a non-static context,
3152 -- so check it.
3154 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3155 Check_Non_Static_Context (N);
3156 end if;
3157 end Eval_Real_Literal;
3159 ------------------------
3160 -- Eval_Relational_Op --
3161 ------------------------
3163 -- Relational operations are static functions, so the result is static if
3164 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3165 -- the result is never static, even if the operands are.
3167 -- However, for internally generated nodes, we allow string equality and
3168 -- inequality to be static. This is because we rewrite A in "ABC" as an
3169 -- equality test A = "ABC", and the former is definitely static.
3171 procedure Eval_Relational_Op (N : Node_Id) is
3172 Left : constant Node_Id := Left_Opnd (N);
3173 Right : constant Node_Id := Right_Opnd (N);
3175 procedure Decompose_Expr
3176 (Expr : Node_Id;
3177 Ent : out Entity_Id;
3178 Kind : out Character;
3179 Cons : out Uint;
3180 Orig : Boolean := True);
3181 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3182 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3183 -- simple entity, and Cons is the value of K. If the expression is not
3184 -- of the required form, Ent is set to Empty.
3186 -- Orig indicates whether Expr is the original expression to consider,
3187 -- or if we are handling a subexpression (e.g. recursive call to
3188 -- Decompose_Expr).
3190 procedure Fold_General_Op (Is_Static : Boolean);
3191 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3192 -- be set when the operator denotes a static expression.
3194 procedure Fold_Static_Real_Op;
3195 -- Attempt to fold static real type relational operator N
3197 function Static_Length (Expr : Node_Id) return Uint;
3198 -- If Expr is an expression for a constrained array whose length is
3199 -- known at compile time, return the non-negative length, otherwise
3200 -- return -1.
3202 --------------------
3203 -- Decompose_Expr --
3204 --------------------
3206 procedure Decompose_Expr
3207 (Expr : Node_Id;
3208 Ent : out Entity_Id;
3209 Kind : out Character;
3210 Cons : out Uint;
3211 Orig : Boolean := True)
3213 Exp : Node_Id;
3215 begin
3216 -- Assume that the expression does not meet the expected form
3218 Cons := No_Uint;
3219 Ent := Empty;
3220 Kind := '?';
3222 if Nkind (Expr) = N_Op_Add
3223 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3224 then
3225 Exp := Left_Opnd (Expr);
3226 Cons := Expr_Value (Right_Opnd (Expr));
3228 elsif Nkind (Expr) = N_Op_Subtract
3229 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3230 then
3231 Exp := Left_Opnd (Expr);
3232 Cons := -Expr_Value (Right_Opnd (Expr));
3234 -- If the bound is a constant created to remove side effects, recover
3235 -- the original expression to see if it has one of the recognizable
3236 -- forms.
3238 elsif Nkind (Expr) = N_Identifier
3239 and then not Comes_From_Source (Entity (Expr))
3240 and then Ekind (Entity (Expr)) = E_Constant
3241 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3242 then
3243 Exp := Expression (Parent (Entity (Expr)));
3244 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3246 -- If original expression includes an entity, create a reference
3247 -- to it for use below.
3249 if Present (Ent) then
3250 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3251 else
3252 return;
3253 end if;
3255 else
3256 -- Only consider the case of X + 0 for a full expression, and
3257 -- not when recursing, otherwise we may end up with evaluating
3258 -- expressions not known at compile time to 0.
3260 if Orig then
3261 Exp := Expr;
3262 Cons := Uint_0;
3263 else
3264 return;
3265 end if;
3266 end if;
3268 -- At this stage Exp is set to the potential X
3270 if Nkind (Exp) = N_Attribute_Reference then
3271 if Attribute_Name (Exp) = Name_First then
3272 Kind := 'F';
3273 elsif Attribute_Name (Exp) = Name_Last then
3274 Kind := 'L';
3275 else
3276 return;
3277 end if;
3279 Exp := Prefix (Exp);
3281 else
3282 Kind := 'E';
3283 end if;
3285 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3286 Ent := Entity (Exp);
3287 end if;
3288 end Decompose_Expr;
3290 ---------------------
3291 -- Fold_General_Op --
3292 ---------------------
3294 procedure Fold_General_Op (Is_Static : Boolean) is
3295 CR : constant Compare_Result :=
3296 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3298 Result : Boolean;
3300 begin
3301 if CR = Unknown then
3302 return;
3303 end if;
3305 case Nkind (N) is
3306 when N_Op_Eq =>
3307 if CR = EQ then
3308 Result := True;
3309 elsif CR = NE or else CR = GT or else CR = LT then
3310 Result := False;
3311 else
3312 return;
3313 end if;
3315 when N_Op_Ge =>
3316 if CR = GT or else CR = EQ or else CR = GE then
3317 Result := True;
3318 elsif CR = LT then
3319 Result := False;
3320 else
3321 return;
3322 end if;
3324 when N_Op_Gt =>
3325 if CR = GT then
3326 Result := True;
3327 elsif CR = EQ or else CR = LT or else CR = LE then
3328 Result := False;
3329 else
3330 return;
3331 end if;
3333 when N_Op_Le =>
3334 if CR = LT or else CR = EQ or else CR = LE then
3335 Result := True;
3336 elsif CR = GT then
3337 Result := False;
3338 else
3339 return;
3340 end if;
3342 when N_Op_Lt =>
3343 if CR = LT then
3344 Result := True;
3345 elsif CR = EQ or else CR = GT or else CR = GE then
3346 Result := False;
3347 else
3348 return;
3349 end if;
3351 when N_Op_Ne =>
3352 if CR = NE or else CR = GT or else CR = LT then
3353 Result := True;
3354 elsif CR = EQ then
3355 Result := False;
3356 else
3357 return;
3358 end if;
3360 when others =>
3361 raise Program_Error;
3362 end case;
3364 -- Determine the potential outcome of the relation assuming the
3365 -- operands are valid and emit a warning when the relation yields
3366 -- True or False only in the presence of invalid values.
3368 Warn_On_Constant_Valid_Condition (N);
3370 Fold_Uint (N, Test (Result), Is_Static);
3371 end Fold_General_Op;
3373 -------------------------
3374 -- Fold_Static_Real_Op --
3375 -------------------------
3377 procedure Fold_Static_Real_Op is
3378 Left_Real : constant Ureal := Expr_Value_R (Left);
3379 Right_Real : constant Ureal := Expr_Value_R (Right);
3380 Result : Boolean;
3382 begin
3383 case Nkind (N) is
3384 when N_Op_Eq => Result := (Left_Real = Right_Real);
3385 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3386 when N_Op_Gt => Result := (Left_Real > Right_Real);
3387 when N_Op_Le => Result := (Left_Real <= Right_Real);
3388 when N_Op_Lt => Result := (Left_Real < Right_Real);
3389 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3390 when others => raise Program_Error;
3391 end case;
3393 Fold_Uint (N, Test (Result), True);
3394 end Fold_Static_Real_Op;
3396 -------------------
3397 -- Static_Length --
3398 -------------------
3400 function Static_Length (Expr : Node_Id) return Uint is
3401 Cons1 : Uint;
3402 Cons2 : Uint;
3403 Ent1 : Entity_Id;
3404 Ent2 : Entity_Id;
3405 Kind1 : Character;
3406 Kind2 : Character;
3407 Typ : Entity_Id;
3409 begin
3410 -- First easy case string literal
3412 if Nkind (Expr) = N_String_Literal then
3413 return UI_From_Int (String_Length (Strval (Expr)));
3415 -- With frontend inlining as performed in GNATprove mode, a variable
3416 -- may be inserted that has a string literal subtype. Deal with this
3417 -- specially as for the previous case.
3419 elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then
3420 return String_Literal_Length (Etype (Expr));
3422 -- Second easy case, not constrained subtype, so no length
3424 elsif not Is_Constrained (Etype (Expr)) then
3425 return Uint_Minus_1;
3426 end if;
3428 -- General case
3430 Typ := Etype (First_Index (Etype (Expr)));
3432 -- The simple case, both bounds are known at compile time
3434 if Is_Discrete_Type (Typ)
3435 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3436 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3437 then
3438 return
3439 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3440 Expr_Value (Type_Low_Bound (Typ)) + 1);
3441 end if;
3443 -- A more complex case, where the bounds are of the form X [+/- K1]
3444 -- .. X [+/- K2]), where X is an expression that is either A'First or
3445 -- A'Last (with A an entity name), or X is an entity name, and the
3446 -- two X's are the same and K1 and K2 are known at compile time, in
3447 -- this case, the length can also be computed at compile time, even
3448 -- though the bounds are not known. A common case of this is e.g.
3449 -- (X'First .. X'First+5).
3451 Decompose_Expr
3452 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3453 Decompose_Expr
3454 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3456 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3457 return Cons2 - Cons1 + 1;
3458 else
3459 return Uint_Minus_1;
3460 end if;
3461 end Static_Length;
3463 -- Local variables
3465 Left_Typ : constant Entity_Id := Etype (Left);
3466 Right_Typ : constant Entity_Id := Etype (Right);
3467 Fold : Boolean;
3468 Left_Len : Uint;
3469 Op_Typ : Entity_Id := Empty;
3470 Right_Len : Uint;
3472 Is_Static_Expression : Boolean;
3474 -- Start of processing for Eval_Relational_Op
3476 begin
3477 -- One special case to deal with first. If we can tell that the result
3478 -- will be false because the lengths of one or more index subtypes are
3479 -- compile-time known and different, then we can replace the entire
3480 -- result by False. We only do this for one-dimensional arrays, because
3481 -- the case of multidimensional arrays is rare and too much trouble. If
3482 -- one of the operands is an illegal aggregate, its type might still be
3483 -- an arbitrary composite type, so nothing to do.
3485 if Is_Array_Type (Left_Typ)
3486 and then Left_Typ /= Any_Composite
3487 and then Number_Dimensions (Left_Typ) = 1
3488 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3489 then
3490 if Raises_Constraint_Error (Left)
3491 or else
3492 Raises_Constraint_Error (Right)
3493 then
3494 return;
3496 -- OK, we have the case where we may be able to do this fold
3498 else
3499 Left_Len := Static_Length (Left);
3500 Right_Len := Static_Length (Right);
3502 if Left_Len /= Uint_Minus_1
3503 and then Right_Len /= Uint_Minus_1
3504 and then Left_Len /= Right_Len
3505 then
3506 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
3507 Warn_On_Known_Condition (N);
3508 return;
3509 end if;
3510 end if;
3512 -- General case
3514 else
3515 -- Initialize the value of Is_Static_Expression. The value of Fold
3516 -- returned by Test_Expression_Is_Foldable is not needed since, even
3517 -- when some operand is a variable, we can still perform the static
3518 -- evaluation of the expression in some cases (for example, for a
3519 -- variable of a subtype of Integer we statically know that any value
3520 -- stored in such variable is smaller than Integer'Last).
3522 Test_Expression_Is_Foldable
3523 (N, Left, Right, Is_Static_Expression, Fold);
3525 -- Only comparisons of scalars can give static results. A comparison
3526 -- of strings never yields a static result, even if both operands are
3527 -- static strings, except that as noted above, we allow equality and
3528 -- inequality for strings.
3530 if Is_String_Type (Left_Typ)
3531 and then not Comes_From_Source (N)
3532 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3533 then
3534 null;
3536 elsif not Is_Scalar_Type (Left_Typ) then
3537 Is_Static_Expression := False;
3538 Set_Is_Static_Expression (N, False);
3539 end if;
3541 -- For operators on universal numeric types called as functions with
3542 -- an explicit scope, determine appropriate specific numeric type,
3543 -- and diagnose possible ambiguity.
3545 if Is_Universal_Numeric_Type (Left_Typ)
3546 and then
3547 Is_Universal_Numeric_Type (Right_Typ)
3548 then
3549 Op_Typ := Find_Universal_Operator_Type (N);
3550 end if;
3552 -- Attempt to fold the relational operator
3554 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3555 Fold_Static_Real_Op;
3556 else
3557 Fold_General_Op (Is_Static_Expression);
3558 end if;
3559 end if;
3561 -- For the case of a folded relational operator on a specific numeric
3562 -- type, freeze the operand type now.
3564 if Present (Op_Typ) then
3565 Freeze_Before (N, Op_Typ);
3566 end if;
3568 Warn_On_Known_Condition (N);
3569 end Eval_Relational_Op;
3571 ----------------
3572 -- Eval_Shift --
3573 ----------------
3575 -- Shift operations are intrinsic operations that can never be static, so
3576 -- the only processing required is to perform the required check for a non
3577 -- static context for the two operands.
3579 -- Actually we could do some compile time evaluation here some time ???
3581 procedure Eval_Shift (N : Node_Id) is
3582 begin
3583 Check_Non_Static_Context (Left_Opnd (N));
3584 Check_Non_Static_Context (Right_Opnd (N));
3585 end Eval_Shift;
3587 ------------------------
3588 -- Eval_Short_Circuit --
3589 ------------------------
3591 -- A short circuit operation is potentially static if both operands are
3592 -- potentially static (RM 4.9 (13)).
3594 procedure Eval_Short_Circuit (N : Node_Id) is
3595 Kind : constant Node_Kind := Nkind (N);
3596 Left : constant Node_Id := Left_Opnd (N);
3597 Right : constant Node_Id := Right_Opnd (N);
3598 Left_Int : Uint;
3600 Rstat : constant Boolean :=
3601 Is_Static_Expression (Left)
3602 and then
3603 Is_Static_Expression (Right);
3605 begin
3606 -- Short circuit operations are never static in Ada 83
3608 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3609 Check_Non_Static_Context (Left);
3610 Check_Non_Static_Context (Right);
3611 return;
3612 end if;
3614 -- Now look at the operands, we can't quite use the normal call to
3615 -- Test_Expression_Is_Foldable here because short circuit operations
3616 -- are a special case, they can still be foldable, even if the right
3617 -- operand raises constraint error.
3619 -- If either operand is Any_Type, just propagate to result and do not
3620 -- try to fold, this prevents cascaded errors.
3622 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3623 Set_Etype (N, Any_Type);
3624 return;
3626 -- If left operand raises constraint error, then replace node N with
3627 -- the raise constraint error node, and we are obviously not foldable.
3628 -- Is_Static_Expression is set from the two operands in the normal way,
3629 -- and we check the right operand if it is in a non-static context.
3631 elsif Raises_Constraint_Error (Left) then
3632 if not Rstat then
3633 Check_Non_Static_Context (Right);
3634 end if;
3636 Rewrite_In_Raise_CE (N, Left);
3637 Set_Is_Static_Expression (N, Rstat);
3638 return;
3640 -- If the result is not static, then we won't in any case fold
3642 elsif not Rstat then
3643 Check_Non_Static_Context (Left);
3644 Check_Non_Static_Context (Right);
3645 return;
3646 end if;
3648 -- Here the result is static, note that, unlike the normal processing
3649 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3650 -- the right operand raises constraint error, that's because it is not
3651 -- significant if the left operand is decisive.
3653 Set_Is_Static_Expression (N);
3655 -- It does not matter if the right operand raises constraint error if
3656 -- it will not be evaluated. So deal specially with the cases where
3657 -- the right operand is not evaluated. Note that we will fold these
3658 -- cases even if the right operand is non-static, which is fine, but
3659 -- of course in these cases the result is not potentially static.
3661 Left_Int := Expr_Value (Left);
3663 if (Kind = N_And_Then and then Is_False (Left_Int))
3664 or else
3665 (Kind = N_Or_Else and then Is_True (Left_Int))
3666 then
3667 Fold_Uint (N, Left_Int, Rstat);
3668 return;
3669 end if;
3671 -- If first operand not decisive, then it does matter if the right
3672 -- operand raises constraint error, since it will be evaluated, so
3673 -- we simply replace the node with the right operand. Note that this
3674 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3675 -- (both are set to True in Right).
3677 if Raises_Constraint_Error (Right) then
3678 Rewrite_In_Raise_CE (N, Right);
3679 Check_Non_Static_Context (Left);
3680 return;
3681 end if;
3683 -- Otherwise the result depends on the right operand
3685 Fold_Uint (N, Expr_Value (Right), Rstat);
3686 return;
3687 end Eval_Short_Circuit;
3689 ----------------
3690 -- Eval_Slice --
3691 ----------------
3693 -- Slices can never be static, so the only processing required is to check
3694 -- for non-static context if an explicit range is given.
3696 procedure Eval_Slice (N : Node_Id) is
3697 Drange : constant Node_Id := Discrete_Range (N);
3699 begin
3700 if Nkind (Drange) = N_Range then
3701 Check_Non_Static_Context (Low_Bound (Drange));
3702 Check_Non_Static_Context (High_Bound (Drange));
3703 end if;
3705 -- A slice of the form A (subtype), when the subtype is the index of
3706 -- the type of A, is redundant, the slice can be replaced with A, and
3707 -- this is worth a warning.
3709 if Is_Entity_Name (Prefix (N)) then
3710 declare
3711 E : constant Entity_Id := Entity (Prefix (N));
3712 T : constant Entity_Id := Etype (E);
3714 begin
3715 if Ekind (E) = E_Constant
3716 and then Is_Array_Type (T)
3717 and then Is_Entity_Name (Drange)
3718 then
3719 if Is_Entity_Name (Original_Node (First_Index (T)))
3720 and then Entity (Original_Node (First_Index (T)))
3721 = Entity (Drange)
3722 then
3723 if Warn_On_Redundant_Constructs then
3724 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3725 end if;
3727 -- The following might be a useful optimization???
3729 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3730 end if;
3731 end if;
3732 end;
3733 end if;
3734 end Eval_Slice;
3736 -------------------------
3737 -- Eval_String_Literal --
3738 -------------------------
3740 procedure Eval_String_Literal (N : Node_Id) is
3741 Typ : constant Entity_Id := Etype (N);
3742 Bas : constant Entity_Id := Base_Type (Typ);
3743 Xtp : Entity_Id;
3744 Len : Nat;
3745 Lo : Node_Id;
3747 begin
3748 -- Nothing to do if error type (handles cases like default expressions
3749 -- or generics where we have not yet fully resolved the type).
3751 if Bas = Any_Type or else Bas = Any_String then
3752 return;
3753 end if;
3755 -- String literals are static if the subtype is static (RM 4.9(2)), so
3756 -- reset the static expression flag (it was set unconditionally in
3757 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3758 -- the subtype is static by looking at the lower bound.
3760 if Ekind (Typ) = E_String_Literal_Subtype then
3761 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3762 Set_Is_Static_Expression (N, False);
3763 return;
3764 end if;
3766 -- Here if Etype of string literal is normal Etype (not yet possible,
3767 -- but may be possible in future).
3769 elsif not Is_OK_Static_Expression
3770 (Type_Low_Bound (Etype (First_Index (Typ))))
3771 then
3772 Set_Is_Static_Expression (N, False);
3773 return;
3774 end if;
3776 -- If original node was a type conversion, then result if non-static
3778 if Nkind (Original_Node (N)) = N_Type_Conversion then
3779 Set_Is_Static_Expression (N, False);
3780 return;
3781 end if;
3783 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3784 -- if its bounds are outside the index base type and this index type is
3785 -- static. This can happen in only two ways. Either the string literal
3786 -- is too long, or it is null, and the lower bound is type'First. Either
3787 -- way it is the upper bound that is out of range of the index type.
3789 if Ada_Version >= Ada_95 then
3790 if Is_Standard_String_Type (Bas) then
3791 Xtp := Standard_Positive;
3792 else
3793 Xtp := Etype (First_Index (Bas));
3794 end if;
3796 if Ekind (Typ) = E_String_Literal_Subtype then
3797 Lo := String_Literal_Low_Bound (Typ);
3798 else
3799 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3800 end if;
3802 -- Check for string too long
3804 Len := String_Length (Strval (N));
3806 if UI_From_Int (Len) > String_Type_Len (Bas) then
3808 -- Issue message. Note that this message is a warning if the
3809 -- string literal is not marked as static (happens in some cases
3810 -- of folding strings known at compile time, but not static).
3811 -- Furthermore in such cases, we reword the message, since there
3812 -- is no string literal in the source program.
3814 if Is_Static_Expression (N) then
3815 Apply_Compile_Time_Constraint_Error
3816 (N, "string literal too long for}", CE_Length_Check_Failed,
3817 Ent => Bas,
3818 Typ => First_Subtype (Bas));
3819 else
3820 Apply_Compile_Time_Constraint_Error
3821 (N, "string value too long for}", CE_Length_Check_Failed,
3822 Ent => Bas,
3823 Typ => First_Subtype (Bas),
3824 Warn => True);
3825 end if;
3827 -- Test for null string not allowed
3829 elsif Len = 0
3830 and then not Is_Generic_Type (Xtp)
3831 and then
3832 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3833 then
3834 -- Same specialization of message
3836 if Is_Static_Expression (N) then
3837 Apply_Compile_Time_Constraint_Error
3838 (N, "null string literal not allowed for}",
3839 CE_Length_Check_Failed,
3840 Ent => Bas,
3841 Typ => First_Subtype (Bas));
3842 else
3843 Apply_Compile_Time_Constraint_Error
3844 (N, "null string value not allowed for}",
3845 CE_Length_Check_Failed,
3846 Ent => Bas,
3847 Typ => First_Subtype (Bas),
3848 Warn => True);
3849 end if;
3850 end if;
3851 end if;
3852 end Eval_String_Literal;
3854 --------------------------
3855 -- Eval_Type_Conversion --
3856 --------------------------
3858 -- A type conversion is potentially static if its subtype mark is for a
3859 -- static scalar subtype, and its operand expression is potentially static
3860 -- (RM 4.9(10)).
3862 procedure Eval_Type_Conversion (N : Node_Id) is
3863 Operand : constant Node_Id := Expression (N);
3864 Source_Type : constant Entity_Id := Etype (Operand);
3865 Target_Type : constant Entity_Id := Etype (N);
3867 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3868 -- Returns true if type T is an integer type, or if it is a fixed-point
3869 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3870 -- on the conversion node).
3872 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3873 -- Returns true if type T is a floating-point type, or if it is a
3874 -- fixed-point type that is not to be treated as an integer (i.e. the
3875 -- flag Conversion_OK is not set on the conversion node).
3877 ------------------------------
3878 -- To_Be_Treated_As_Integer --
3879 ------------------------------
3881 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3882 begin
3883 return
3884 Is_Integer_Type (T)
3885 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3886 end To_Be_Treated_As_Integer;
3888 ---------------------------
3889 -- To_Be_Treated_As_Real --
3890 ---------------------------
3892 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3893 begin
3894 return
3895 Is_Floating_Point_Type (T)
3896 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3897 end To_Be_Treated_As_Real;
3899 -- Local variables
3901 Fold : Boolean;
3902 Stat : Boolean;
3904 -- Start of processing for Eval_Type_Conversion
3906 begin
3907 -- Cannot fold if target type is non-static or if semantic error
3909 if not Is_Static_Subtype (Target_Type) then
3910 Check_Non_Static_Context (Operand);
3911 return;
3912 elsif Error_Posted (N) then
3913 return;
3914 end if;
3916 -- If not foldable we are done
3918 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3920 if not Fold then
3921 return;
3923 -- Don't try fold if target type has constraint error bounds
3925 elsif not Is_OK_Static_Subtype (Target_Type) then
3926 Set_Raises_Constraint_Error (N);
3927 return;
3928 end if;
3930 -- Remaining processing depends on operand types. Note that in the
3931 -- following type test, fixed-point counts as real unless the flag
3932 -- Conversion_OK is set, in which case it counts as integer.
3934 -- Fold conversion, case of string type. The result is not static
3936 if Is_String_Type (Target_Type) then
3937 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3938 return;
3940 -- Fold conversion, case of integer target type
3942 elsif To_Be_Treated_As_Integer (Target_Type) then
3943 declare
3944 Result : Uint;
3946 begin
3947 -- Integer to integer conversion
3949 if To_Be_Treated_As_Integer (Source_Type) then
3950 Result := Expr_Value (Operand);
3952 -- Real to integer conversion
3954 else
3955 Result := UR_To_Uint (Expr_Value_R (Operand));
3956 end if;
3958 -- If fixed-point type (Conversion_OK must be set), then the
3959 -- result is logically an integer, but we must replace the
3960 -- conversion with the corresponding real literal, since the
3961 -- type from a semantic point of view is still fixed-point.
3963 if Is_Fixed_Point_Type (Target_Type) then
3964 Fold_Ureal
3965 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3967 -- Otherwise result is integer literal
3969 else
3970 Fold_Uint (N, Result, Stat);
3971 end if;
3972 end;
3974 -- Fold conversion, case of real target type
3976 elsif To_Be_Treated_As_Real (Target_Type) then
3977 declare
3978 Result : Ureal;
3980 begin
3981 if To_Be_Treated_As_Real (Source_Type) then
3982 Result := Expr_Value_R (Operand);
3983 else
3984 Result := UR_From_Uint (Expr_Value (Operand));
3985 end if;
3987 Fold_Ureal (N, Result, Stat);
3988 end;
3990 -- Enumeration types
3992 else
3993 Fold_Uint (N, Expr_Value (Operand), Stat);
3994 end if;
3996 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3997 Out_Of_Range (N);
3998 end if;
4000 end Eval_Type_Conversion;
4002 -------------------
4003 -- Eval_Unary_Op --
4004 -------------------
4006 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4007 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4009 procedure Eval_Unary_Op (N : Node_Id) is
4010 Right : constant Node_Id := Right_Opnd (N);
4011 Otype : Entity_Id := Empty;
4012 Stat : Boolean;
4013 Fold : Boolean;
4015 begin
4016 -- If not foldable we are done
4018 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
4020 if not Fold then
4021 return;
4022 end if;
4024 if Etype (Right) = Universal_Integer
4025 or else
4026 Etype (Right) = Universal_Real
4027 then
4028 Otype := Find_Universal_Operator_Type (N);
4029 end if;
4031 -- Fold for integer case
4033 if Is_Integer_Type (Etype (N)) then
4034 declare
4035 Rint : constant Uint := Expr_Value (Right);
4036 Result : Uint;
4038 begin
4039 -- In the case of modular unary plus and abs there is no need
4040 -- to adjust the result of the operation since if the original
4041 -- operand was in bounds the result will be in the bounds of the
4042 -- modular type. However, in the case of modular unary minus the
4043 -- result may go out of the bounds of the modular type and needs
4044 -- adjustment.
4046 if Nkind (N) = N_Op_Plus then
4047 Result := Rint;
4049 elsif Nkind (N) = N_Op_Minus then
4050 if Is_Modular_Integer_Type (Etype (N)) then
4051 Result := (-Rint) mod Modulus (Etype (N));
4052 else
4053 Result := (-Rint);
4054 end if;
4056 else
4057 pragma Assert (Nkind (N) = N_Op_Abs);
4058 Result := abs Rint;
4059 end if;
4061 Fold_Uint (N, Result, Stat);
4062 end;
4064 -- Fold for real case
4066 elsif Is_Real_Type (Etype (N)) then
4067 declare
4068 Rreal : constant Ureal := Expr_Value_R (Right);
4069 Result : Ureal;
4071 begin
4072 if Nkind (N) = N_Op_Plus then
4073 Result := Rreal;
4074 elsif Nkind (N) = N_Op_Minus then
4075 Result := UR_Negate (Rreal);
4076 else
4077 pragma Assert (Nkind (N) = N_Op_Abs);
4078 Result := abs Rreal;
4079 end if;
4081 Fold_Ureal (N, Result, Stat);
4082 end;
4083 end if;
4085 -- If the operator was resolved to a specific type, make sure that type
4086 -- is frozen even if the expression is folded into a literal (which has
4087 -- a universal type).
4089 if Present (Otype) then
4090 Freeze_Before (N, Otype);
4091 end if;
4092 end Eval_Unary_Op;
4094 -------------------------------
4095 -- Eval_Unchecked_Conversion --
4096 -------------------------------
4098 -- Unchecked conversions can never be static, so the only required
4099 -- processing is to check for a non-static context for the operand.
4101 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4102 begin
4103 Check_Non_Static_Context (Expression (N));
4104 end Eval_Unchecked_Conversion;
4106 --------------------
4107 -- Expr_Rep_Value --
4108 --------------------
4110 function Expr_Rep_Value (N : Node_Id) return Uint is
4111 Kind : constant Node_Kind := Nkind (N);
4112 Ent : Entity_Id;
4114 begin
4115 if Is_Entity_Name (N) then
4116 Ent := Entity (N);
4118 -- An enumeration literal that was either in the source or created
4119 -- as a result of static evaluation.
4121 if Ekind (Ent) = E_Enumeration_Literal then
4122 return Enumeration_Rep (Ent);
4124 -- A user defined static constant
4126 else
4127 pragma Assert (Ekind (Ent) = E_Constant);
4128 return Expr_Rep_Value (Constant_Value (Ent));
4129 end if;
4131 -- An integer literal that was either in the source or created as a
4132 -- result of static evaluation.
4134 elsif Kind = N_Integer_Literal then
4135 return Intval (N);
4137 -- A real literal for a fixed-point type. This must be the fixed-point
4138 -- case, either the literal is of a fixed-point type, or it is a bound
4139 -- of a fixed-point type, with type universal real. In either case we
4140 -- obtain the desired value from Corresponding_Integer_Value.
4142 elsif Kind = N_Real_Literal then
4143 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4144 return Corresponding_Integer_Value (N);
4146 -- Otherwise must be character literal
4148 else
4149 pragma Assert (Kind = N_Character_Literal);
4150 Ent := Entity (N);
4152 -- Since Character literals of type Standard.Character don't have any
4153 -- defining character literals built for them, they do not have their
4154 -- Entity set, so just use their Char code. Otherwise for user-
4155 -- defined character literals use their Pos value as usual which is
4156 -- the same as the Rep value.
4158 if No (Ent) then
4159 return Char_Literal_Value (N);
4160 else
4161 return Enumeration_Rep (Ent);
4162 end if;
4163 end if;
4164 end Expr_Rep_Value;
4166 ----------------
4167 -- Expr_Value --
4168 ----------------
4170 function Expr_Value (N : Node_Id) return Uint is
4171 Kind : constant Node_Kind := Nkind (N);
4172 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4173 Ent : Entity_Id;
4174 Val : Uint;
4176 begin
4177 -- If already in cache, then we know it's compile time known and we can
4178 -- return the value that was previously stored in the cache since
4179 -- compile time known values cannot change.
4181 if CV_Ent.N = N then
4182 return CV_Ent.V;
4183 end if;
4185 -- Otherwise proceed to test value
4187 if Is_Entity_Name (N) then
4188 Ent := Entity (N);
4190 -- An enumeration literal that was either in the source or created as
4191 -- a result of static evaluation.
4193 if Ekind (Ent) = E_Enumeration_Literal then
4194 Val := Enumeration_Pos (Ent);
4196 -- A user defined static constant
4198 else
4199 pragma Assert (Ekind (Ent) = E_Constant);
4200 Val := Expr_Value (Constant_Value (Ent));
4201 end if;
4203 -- An integer literal that was either in the source or created as a
4204 -- result of static evaluation.
4206 elsif Kind = N_Integer_Literal then
4207 Val := Intval (N);
4209 -- A real literal for a fixed-point type. This must be the fixed-point
4210 -- case, either the literal is of a fixed-point type, or it is a bound
4211 -- of a fixed-point type, with type universal real. In either case we
4212 -- obtain the desired value from Corresponding_Integer_Value.
4214 elsif Kind = N_Real_Literal then
4215 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4216 Val := Corresponding_Integer_Value (N);
4218 -- The NULL access value
4220 elsif Kind = N_Null then
4221 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N))));
4222 Val := Uint_0;
4224 -- Otherwise must be character literal
4226 else
4227 pragma Assert (Kind = N_Character_Literal);
4228 Ent := Entity (N);
4230 -- Since Character literals of type Standard.Character don't
4231 -- have any defining character literals built for them, they
4232 -- do not have their Entity set, so just use their Char
4233 -- code. Otherwise for user-defined character literals use
4234 -- their Pos value as usual.
4236 if No (Ent) then
4237 Val := Char_Literal_Value (N);
4238 else
4239 Val := Enumeration_Pos (Ent);
4240 end if;
4241 end if;
4243 -- Come here with Val set to value to be returned, set cache
4245 CV_Ent.N := N;
4246 CV_Ent.V := Val;
4247 return Val;
4248 end Expr_Value;
4250 ------------------
4251 -- Expr_Value_E --
4252 ------------------
4254 function Expr_Value_E (N : Node_Id) return Entity_Id is
4255 Ent : constant Entity_Id := Entity (N);
4256 begin
4257 if Ekind (Ent) = E_Enumeration_Literal then
4258 return Ent;
4259 else
4260 pragma Assert (Ekind (Ent) = E_Constant);
4261 return Expr_Value_E (Constant_Value (Ent));
4262 end if;
4263 end Expr_Value_E;
4265 ------------------
4266 -- Expr_Value_R --
4267 ------------------
4269 function Expr_Value_R (N : Node_Id) return Ureal is
4270 Kind : constant Node_Kind := Nkind (N);
4271 Ent : Entity_Id;
4273 begin
4274 if Kind = N_Real_Literal then
4275 return Realval (N);
4277 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4278 Ent := Entity (N);
4279 pragma Assert (Ekind (Ent) = E_Constant);
4280 return Expr_Value_R (Constant_Value (Ent));
4282 elsif Kind = N_Integer_Literal then
4283 return UR_From_Uint (Expr_Value (N));
4285 -- Here, we have a node that cannot be interpreted as a compile time
4286 -- constant. That is definitely an error.
4288 else
4289 raise Program_Error;
4290 end if;
4291 end Expr_Value_R;
4293 ------------------
4294 -- Expr_Value_S --
4295 ------------------
4297 function Expr_Value_S (N : Node_Id) return Node_Id is
4298 begin
4299 if Nkind (N) = N_String_Literal then
4300 return N;
4301 else
4302 pragma Assert (Ekind (Entity (N)) = E_Constant);
4303 return Expr_Value_S (Constant_Value (Entity (N)));
4304 end if;
4305 end Expr_Value_S;
4307 ----------------------------------
4308 -- Find_Universal_Operator_Type --
4309 ----------------------------------
4311 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4312 PN : constant Node_Id := Parent (N);
4313 Call : constant Node_Id := Original_Node (N);
4314 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4316 Is_Fix : constant Boolean :=
4317 Nkind (N) in N_Binary_Op
4318 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4319 -- A mixed-mode operation in this context indicates the presence of
4320 -- fixed-point type in the designated package.
4322 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4323 -- Case where N is a relational (or membership) operator (else it is an
4324 -- arithmetic one).
4326 In_Membership : constant Boolean :=
4327 Nkind (PN) in N_Membership_Test
4328 and then
4329 Nkind (Right_Opnd (PN)) = N_Range
4330 and then
4331 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4332 and then
4333 Is_Universal_Numeric_Type
4334 (Etype (Low_Bound (Right_Opnd (PN))))
4335 and then
4336 Is_Universal_Numeric_Type
4337 (Etype (High_Bound (Right_Opnd (PN))));
4338 -- Case where N is part of a membership test with a universal range
4340 E : Entity_Id;
4341 Pack : Entity_Id;
4342 Typ1 : Entity_Id := Empty;
4343 Priv_E : Entity_Id;
4345 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4346 -- Check whether one operand is a mixed-mode operation that requires the
4347 -- presence of a fixed-point type. Given that all operands are universal
4348 -- and have been constant-folded, retrieve the original function call.
4350 ---------------------------
4351 -- Is_Mixed_Mode_Operand --
4352 ---------------------------
4354 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4355 Onod : constant Node_Id := Original_Node (Op);
4356 begin
4357 return Nkind (Onod) = N_Function_Call
4358 and then Present (Next_Actual (First_Actual (Onod)))
4359 and then Etype (First_Actual (Onod)) /=
4360 Etype (Next_Actual (First_Actual (Onod)));
4361 end Is_Mixed_Mode_Operand;
4363 -- Start of processing for Find_Universal_Operator_Type
4365 begin
4366 if Nkind (Call) /= N_Function_Call
4367 or else Nkind (Name (Call)) /= N_Expanded_Name
4368 then
4369 return Empty;
4371 -- There are several cases where the context does not imply the type of
4372 -- the operands:
4373 -- - the universal expression appears in a type conversion;
4374 -- - the expression is a relational operator applied to universal
4375 -- operands;
4376 -- - the expression is a membership test with a universal operand
4377 -- and a range with universal bounds.
4379 elsif Nkind (Parent (N)) = N_Type_Conversion
4380 or else Is_Relational
4381 or else In_Membership
4382 then
4383 Pack := Entity (Prefix (Name (Call)));
4385 -- If the prefix is a package declared elsewhere, iterate over its
4386 -- visible entities, otherwise iterate over all declarations in the
4387 -- designated scope.
4389 if Ekind (Pack) = E_Package
4390 and then not In_Open_Scopes (Pack)
4391 then
4392 Priv_E := First_Private_Entity (Pack);
4393 else
4394 Priv_E := Empty;
4395 end if;
4397 Typ1 := Empty;
4398 E := First_Entity (Pack);
4399 while Present (E) and then E /= Priv_E loop
4400 if Is_Numeric_Type (E)
4401 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4402 and then Comes_From_Source (E)
4403 and then Is_Integer_Type (E) = Is_Int
4404 and then (Nkind (N) in N_Unary_Op
4405 or else Is_Relational
4406 or else Is_Fixed_Point_Type (E) = Is_Fix)
4407 then
4408 if No (Typ1) then
4409 Typ1 := E;
4411 -- Before emitting an error, check for the presence of a
4412 -- mixed-mode operation that specifies a fixed point type.
4414 elsif Is_Relational
4415 and then
4416 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4417 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4418 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4420 then
4421 if Is_Fixed_Point_Type (E) then
4422 Typ1 := E;
4423 end if;
4425 else
4426 -- More than one type of the proper class declared in P
4428 Error_Msg_N ("ambiguous operation", N);
4429 Error_Msg_Sloc := Sloc (Typ1);
4430 Error_Msg_N ("\possible interpretation (inherited)#", N);
4431 Error_Msg_Sloc := Sloc (E);
4432 Error_Msg_N ("\possible interpretation (inherited)#", N);
4433 return Empty;
4434 end if;
4435 end if;
4437 Next_Entity (E);
4438 end loop;
4439 end if;
4441 return Typ1;
4442 end Find_Universal_Operator_Type;
4444 --------------------------
4445 -- Flag_Non_Static_Expr --
4446 --------------------------
4448 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4449 begin
4450 if Error_Posted (Expr) and then not All_Errors_Mode then
4451 return;
4452 else
4453 Error_Msg_F (Msg, Expr);
4454 Why_Not_Static (Expr);
4455 end if;
4456 end Flag_Non_Static_Expr;
4458 --------------
4459 -- Fold_Str --
4460 --------------
4462 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4463 Loc : constant Source_Ptr := Sloc (N);
4464 Typ : constant Entity_Id := Etype (N);
4466 begin
4467 if Raises_Constraint_Error (N) then
4468 Set_Is_Static_Expression (N, Static);
4469 return;
4470 end if;
4472 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4474 -- We now have the literal with the right value, both the actual type
4475 -- and the expected type of this literal are taken from the expression
4476 -- that was evaluated. So now we do the Analyze and Resolve.
4478 -- Note that we have to reset Is_Static_Expression both after the
4479 -- analyze step (because Resolve will evaluate the literal, which
4480 -- will cause semantic errors if it is marked as static), and after
4481 -- the Resolve step (since Resolve in some cases resets this flag).
4483 Analyze (N);
4484 Set_Is_Static_Expression (N, Static);
4485 Set_Etype (N, Typ);
4486 Resolve (N);
4487 Set_Is_Static_Expression (N, Static);
4488 end Fold_Str;
4490 ---------------
4491 -- Fold_Uint --
4492 ---------------
4494 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4495 Loc : constant Source_Ptr := Sloc (N);
4496 Typ : Entity_Id := Etype (N);
4497 Ent : Entity_Id;
4499 begin
4500 if Raises_Constraint_Error (N) then
4501 Set_Is_Static_Expression (N, Static);
4502 return;
4503 end if;
4505 -- If we are folding a named number, retain the entity in the literal,
4506 -- for ASIS use.
4508 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
4509 Ent := Entity (N);
4510 else
4511 Ent := Empty;
4512 end if;
4514 if Is_Private_Type (Typ) then
4515 Typ := Full_View (Typ);
4516 end if;
4518 -- For a result of type integer, substitute an N_Integer_Literal node
4519 -- for the result of the compile time evaluation of the expression.
4520 -- For ASIS use, set a link to the original named number when not in
4521 -- a generic context.
4523 if Is_Integer_Type (Typ) then
4524 Rewrite (N, Make_Integer_Literal (Loc, Val));
4525 Set_Original_Entity (N, Ent);
4527 -- Otherwise we have an enumeration type, and we substitute either
4528 -- an N_Identifier or N_Character_Literal to represent the enumeration
4529 -- literal corresponding to the given value, which must always be in
4530 -- range, because appropriate tests have already been made for this.
4532 else pragma Assert (Is_Enumeration_Type (Typ));
4533 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4534 end if;
4536 -- We now have the literal with the right value, both the actual type
4537 -- and the expected type of this literal are taken from the expression
4538 -- that was evaluated. So now we do the Analyze and Resolve.
4540 -- Note that we have to reset Is_Static_Expression both after the
4541 -- analyze step (because Resolve will evaluate the literal, which
4542 -- will cause semantic errors if it is marked as static), and after
4543 -- the Resolve step (since Resolve in some cases sets this flag).
4545 Analyze (N);
4546 Set_Is_Static_Expression (N, Static);
4547 Set_Etype (N, Typ);
4548 Resolve (N);
4549 Set_Is_Static_Expression (N, Static);
4550 end Fold_Uint;
4552 ----------------
4553 -- Fold_Ureal --
4554 ----------------
4556 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4557 Loc : constant Source_Ptr := Sloc (N);
4558 Typ : constant Entity_Id := Etype (N);
4559 Ent : Entity_Id;
4561 begin
4562 if Raises_Constraint_Error (N) then
4563 Set_Is_Static_Expression (N, Static);
4564 return;
4565 end if;
4567 -- If we are folding a named number, retain the entity in the literal,
4568 -- for ASIS use.
4570 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
4571 Ent := Entity (N);
4572 else
4573 Ent := Empty;
4574 end if;
4576 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4578 -- Set link to original named number, for ASIS use
4580 Set_Original_Entity (N, Ent);
4582 -- We now have the literal with the right value, both the actual type
4583 -- and the expected type of this literal are taken from the expression
4584 -- that was evaluated. So now we do the Analyze and Resolve.
4586 -- Note that we have to reset Is_Static_Expression both after the
4587 -- analyze step (because Resolve will evaluate the literal, which
4588 -- will cause semantic errors if it is marked as static), and after
4589 -- the Resolve step (since Resolve in some cases sets this flag).
4591 Analyze (N);
4592 Set_Is_Static_Expression (N, Static);
4593 Set_Etype (N, Typ);
4594 Resolve (N);
4595 Set_Is_Static_Expression (N, Static);
4596 end Fold_Ureal;
4598 ---------------
4599 -- From_Bits --
4600 ---------------
4602 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4603 V : Uint := Uint_0;
4605 begin
4606 for J in 0 .. B'Last loop
4607 if B (J) then
4608 V := V + 2 ** J;
4609 end if;
4610 end loop;
4612 if Non_Binary_Modulus (T) then
4613 V := V mod Modulus (T);
4614 end if;
4616 return V;
4617 end From_Bits;
4619 --------------------
4620 -- Get_String_Val --
4621 --------------------
4623 function Get_String_Val (N : Node_Id) return Node_Id is
4624 begin
4625 if Nkind_In (N, N_String_Literal, N_Character_Literal) then
4626 return N;
4627 else
4628 pragma Assert (Is_Entity_Name (N));
4629 return Get_String_Val (Constant_Value (Entity (N)));
4630 end if;
4631 end Get_String_Val;
4633 ----------------
4634 -- Initialize --
4635 ----------------
4637 procedure Initialize is
4638 begin
4639 CV_Cache := (others => (Node_High_Bound, Uint_0));
4640 end Initialize;
4642 --------------------
4643 -- In_Subrange_Of --
4644 --------------------
4646 function In_Subrange_Of
4647 (T1 : Entity_Id;
4648 T2 : Entity_Id;
4649 Fixed_Int : Boolean := False) return Boolean
4651 L1 : Node_Id;
4652 H1 : Node_Id;
4654 L2 : Node_Id;
4655 H2 : Node_Id;
4657 begin
4658 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4659 return True;
4661 -- Never in range if both types are not scalar. Don't know if this can
4662 -- actually happen, but just in case.
4664 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4665 return False;
4667 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4668 -- definitely not compatible with T2.
4670 elsif Is_Floating_Point_Type (T1)
4671 and then Has_Infinities (T1)
4672 and then Is_Floating_Point_Type (T2)
4673 and then not Has_Infinities (T2)
4674 then
4675 return False;
4677 else
4678 L1 := Type_Low_Bound (T1);
4679 H1 := Type_High_Bound (T1);
4681 L2 := Type_Low_Bound (T2);
4682 H2 := Type_High_Bound (T2);
4684 -- Check bounds to see if comparison possible at compile time
4686 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4687 and then
4688 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4689 then
4690 return True;
4691 end if;
4693 -- If bounds not comparable at compile time, then the bounds of T2
4694 -- must be compile time known or we cannot answer the query.
4696 if not Compile_Time_Known_Value (L2)
4697 or else not Compile_Time_Known_Value (H2)
4698 then
4699 return False;
4700 end if;
4702 -- If the bounds of T1 are know at compile time then use these
4703 -- ones, otherwise use the bounds of the base type (which are of
4704 -- course always static).
4706 if not Compile_Time_Known_Value (L1) then
4707 L1 := Type_Low_Bound (Base_Type (T1));
4708 end if;
4710 if not Compile_Time_Known_Value (H1) then
4711 H1 := Type_High_Bound (Base_Type (T1));
4712 end if;
4714 -- Fixed point types should be considered as such only if
4715 -- flag Fixed_Int is set to False.
4717 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4718 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4719 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4720 then
4721 return
4722 Expr_Value_R (L2) <= Expr_Value_R (L1)
4723 and then
4724 Expr_Value_R (H2) >= Expr_Value_R (H1);
4726 else
4727 return
4728 Expr_Value (L2) <= Expr_Value (L1)
4729 and then
4730 Expr_Value (H2) >= Expr_Value (H1);
4732 end if;
4733 end if;
4735 -- If any exception occurs, it means that we have some bug in the compiler
4736 -- possibly triggered by a previous error, or by some unforeseen peculiar
4737 -- occurrence. However, this is only an optimization attempt, so there is
4738 -- really no point in crashing the compiler. Instead we just decide, too
4739 -- bad, we can't figure out the answer in this case after all.
4741 exception
4742 when others =>
4744 -- Debug flag K disables this behavior (useful for debugging)
4746 if Debug_Flag_K then
4747 raise;
4748 else
4749 return False;
4750 end if;
4751 end In_Subrange_Of;
4753 -----------------
4754 -- Is_In_Range --
4755 -----------------
4757 function Is_In_Range
4758 (N : Node_Id;
4759 Typ : Entity_Id;
4760 Assume_Valid : Boolean := False;
4761 Fixed_Int : Boolean := False;
4762 Int_Real : Boolean := False) return Boolean
4764 begin
4765 return
4766 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
4767 end Is_In_Range;
4769 -------------------
4770 -- Is_Null_Range --
4771 -------------------
4773 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4774 begin
4775 if Compile_Time_Known_Value (Lo)
4776 and then Compile_Time_Known_Value (Hi)
4777 then
4778 declare
4779 Typ : Entity_Id := Etype (Lo);
4780 begin
4781 -- When called from the frontend, as part of the analysis of
4782 -- potentially static expressions, Typ will be the full view of a
4783 -- type with all the info needed to answer this query. When called
4784 -- from the backend, for example to know whether a range of a loop
4785 -- is null, Typ might be a private type and we need to explicitly
4786 -- switch to its corresponding full view to access the same info.
4788 if Is_Incomplete_Or_Private_Type (Typ)
4789 and then Present (Full_View (Typ))
4790 then
4791 Typ := Full_View (Typ);
4792 end if;
4794 if Is_Discrete_Type (Typ) then
4795 return Expr_Value (Lo) > Expr_Value (Hi);
4796 else pragma Assert (Is_Real_Type (Typ));
4797 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4798 end if;
4799 end;
4800 else
4801 return False;
4802 end if;
4803 end Is_Null_Range;
4805 -------------------------
4806 -- Is_OK_Static_Choice --
4807 -------------------------
4809 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
4810 begin
4811 -- Check various possibilities for choice
4813 -- Note: for membership tests, we test more cases than are possible
4814 -- (in particular subtype indication), but it doesn't matter because
4815 -- it just won't occur (we have already done a syntax check).
4817 if Nkind (Choice) = N_Others_Choice then
4818 return True;
4820 elsif Nkind (Choice) = N_Range then
4821 return Is_OK_Static_Range (Choice);
4823 elsif Nkind (Choice) = N_Subtype_Indication
4824 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4825 then
4826 return Is_OK_Static_Subtype (Etype (Choice));
4828 else
4829 return Is_OK_Static_Expression (Choice);
4830 end if;
4831 end Is_OK_Static_Choice;
4833 ------------------------------
4834 -- Is_OK_Static_Choice_List --
4835 ------------------------------
4837 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
4838 Choice : Node_Id;
4840 begin
4841 if not Is_Static_Choice_List (Choices) then
4842 return False;
4843 end if;
4845 Choice := First (Choices);
4846 while Present (Choice) loop
4847 if not Is_OK_Static_Choice (Choice) then
4848 Set_Raises_Constraint_Error (Choice);
4849 return False;
4850 end if;
4852 Next (Choice);
4853 end loop;
4855 return True;
4856 end Is_OK_Static_Choice_List;
4858 -----------------------------
4859 -- Is_OK_Static_Expression --
4860 -----------------------------
4862 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4863 begin
4864 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
4865 end Is_OK_Static_Expression;
4867 ------------------------
4868 -- Is_OK_Static_Range --
4869 ------------------------
4871 -- A static range is a range whose bounds are static expressions, or a
4872 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4873 -- We have already converted range attribute references, so we get the
4874 -- "or" part of this rule without needing a special test.
4876 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4877 begin
4878 return Is_OK_Static_Expression (Low_Bound (N))
4879 and then Is_OK_Static_Expression (High_Bound (N));
4880 end Is_OK_Static_Range;
4882 --------------------------
4883 -- Is_OK_Static_Subtype --
4884 --------------------------
4886 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4887 -- neither bound raises constraint error when evaluated.
4889 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4890 Base_T : constant Entity_Id := Base_Type (Typ);
4891 Anc_Subt : Entity_Id;
4893 begin
4894 -- First a quick check on the non static subtype flag. As described
4895 -- in further detail in Einfo, this flag is not decisive in all cases,
4896 -- but if it is set, then the subtype is definitely non-static.
4898 if Is_Non_Static_Subtype (Typ) then
4899 return False;
4900 end if;
4902 Anc_Subt := Ancestor_Subtype (Typ);
4904 if Anc_Subt = Empty then
4905 Anc_Subt := Base_T;
4906 end if;
4908 if Is_Generic_Type (Root_Type (Base_T))
4909 or else Is_Generic_Actual_Type (Base_T)
4910 then
4911 return False;
4913 elsif Has_Dynamic_Predicate_Aspect (Typ) then
4914 return False;
4916 -- String types
4918 elsif Is_String_Type (Typ) then
4919 return
4920 Ekind (Typ) = E_String_Literal_Subtype
4921 or else
4922 (Is_OK_Static_Subtype (Component_Type (Typ))
4923 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4925 -- Scalar types
4927 elsif Is_Scalar_Type (Typ) then
4928 if Base_T = Typ then
4929 return True;
4931 else
4932 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4933 -- Get_Type_{Low,High}_Bound.
4935 return Is_OK_Static_Subtype (Anc_Subt)
4936 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4937 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4938 end if;
4940 -- Types other than string and scalar types are never static
4942 else
4943 return False;
4944 end if;
4945 end Is_OK_Static_Subtype;
4947 ---------------------
4948 -- Is_Out_Of_Range --
4949 ---------------------
4951 function Is_Out_Of_Range
4952 (N : Node_Id;
4953 Typ : Entity_Id;
4954 Assume_Valid : Boolean := False;
4955 Fixed_Int : Boolean := False;
4956 Int_Real : Boolean := False) return Boolean
4958 begin
4959 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
4960 Out_Of_Range;
4961 end Is_Out_Of_Range;
4963 ----------------------
4964 -- Is_Static_Choice --
4965 ----------------------
4967 function Is_Static_Choice (Choice : Node_Id) return Boolean is
4968 begin
4969 -- Check various possibilities for choice
4971 -- Note: for membership tests, we test more cases than are possible
4972 -- (in particular subtype indication), but it doesn't matter because
4973 -- it just won't occur (we have already done a syntax check).
4975 if Nkind (Choice) = N_Others_Choice then
4976 return True;
4978 elsif Nkind (Choice) = N_Range then
4979 return Is_Static_Range (Choice);
4981 elsif Nkind (Choice) = N_Subtype_Indication
4982 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4983 then
4984 return Is_Static_Subtype (Etype (Choice));
4986 else
4987 return Is_Static_Expression (Choice);
4988 end if;
4989 end Is_Static_Choice;
4991 ---------------------------
4992 -- Is_Static_Choice_List --
4993 ---------------------------
4995 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
4996 Choice : Node_Id;
4998 begin
4999 Choice := First (Choices);
5000 while Present (Choice) loop
5001 if not Is_Static_Choice (Choice) then
5002 return False;
5003 end if;
5005 Next (Choice);
5006 end loop;
5008 return True;
5009 end Is_Static_Choice_List;
5011 ---------------------
5012 -- Is_Static_Range --
5013 ---------------------
5015 -- A static range is a range whose bounds are static expressions, or a
5016 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5017 -- We have already converted range attribute references, so we get the
5018 -- "or" part of this rule without needing a special test.
5020 function Is_Static_Range (N : Node_Id) return Boolean is
5021 begin
5022 return Is_Static_Expression (Low_Bound (N))
5023 and then
5024 Is_Static_Expression (High_Bound (N));
5025 end Is_Static_Range;
5027 -----------------------
5028 -- Is_Static_Subtype --
5029 -----------------------
5031 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5033 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
5034 Base_T : constant Entity_Id := Base_Type (Typ);
5035 Anc_Subt : Entity_Id;
5037 begin
5038 -- First a quick check on the non static subtype flag. As described
5039 -- in further detail in Einfo, this flag is not decisive in all cases,
5040 -- but if it is set, then the subtype is definitely non-static.
5042 if Is_Non_Static_Subtype (Typ) then
5043 return False;
5044 end if;
5046 Anc_Subt := Ancestor_Subtype (Typ);
5048 if Anc_Subt = Empty then
5049 Anc_Subt := Base_T;
5050 end if;
5052 if Is_Generic_Type (Root_Type (Base_T))
5053 or else Is_Generic_Actual_Type (Base_T)
5054 then
5055 return False;
5057 -- If there is a dynamic predicate for the type (declared or inherited)
5058 -- the expression is not static.
5060 elsif Has_Dynamic_Predicate_Aspect (Typ)
5061 or else (Is_Derived_Type (Typ)
5062 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5063 then
5064 return False;
5066 -- String types
5068 elsif Is_String_Type (Typ) then
5069 return
5070 Ekind (Typ) = E_String_Literal_Subtype
5071 or else (Is_Static_Subtype (Component_Type (Typ))
5072 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5074 -- Scalar types
5076 elsif Is_Scalar_Type (Typ) then
5077 if Base_T = Typ then
5078 return True;
5080 else
5081 return Is_Static_Subtype (Anc_Subt)
5082 and then Is_Static_Expression (Type_Low_Bound (Typ))
5083 and then Is_Static_Expression (Type_High_Bound (Typ));
5084 end if;
5086 -- Types other than string and scalar types are never static
5088 else
5089 return False;
5090 end if;
5091 end Is_Static_Subtype;
5093 -------------------------------
5094 -- Is_Statically_Unevaluated --
5095 -------------------------------
5097 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5098 function Check_Case_Expr_Alternative
5099 (CEA : Node_Id) return Match_Result;
5100 -- We have a message emanating from the Expression of a case expression
5101 -- alternative. We examine this alternative, as follows:
5103 -- If the selecting expression of the parent case is non-static, or
5104 -- if any of the discrete choices of the given case alternative are
5105 -- non-static or raise Constraint_Error, return Non_Static.
5107 -- Otherwise check if the selecting expression matches any of the given
5108 -- discrete choices. If so, the alternative is executed and we return
5109 -- Match, otherwise, the alternative can never be executed, and so we
5110 -- return No_Match.
5112 ---------------------------------
5113 -- Check_Case_Expr_Alternative --
5114 ---------------------------------
5116 function Check_Case_Expr_Alternative
5117 (CEA : Node_Id) return Match_Result
5119 Case_Exp : constant Node_Id := Parent (CEA);
5120 Choice : Node_Id;
5121 Prev_CEA : Node_Id;
5123 begin
5124 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5126 -- Check that selecting expression is static
5128 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5129 return Non_Static;
5130 end if;
5132 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5133 return Non_Static;
5134 end if;
5136 -- All choices are now known to be static. Now see if alternative
5137 -- matches one of the choices.
5139 Choice := First (Discrete_Choices (CEA));
5140 while Present (Choice) loop
5142 -- Check various possibilities for choice, returning Match if we
5143 -- find the selecting value matches any of the choices. Note that
5144 -- we know we are the last choice, so we don't have to keep going.
5146 if Nkind (Choice) = N_Others_Choice then
5148 -- Others choice is a bit annoying, it matches if none of the
5149 -- previous alternatives matches (note that we know we are the
5150 -- last alternative in this case, so we can just go backwards
5151 -- from us to see if any previous one matches).
5153 Prev_CEA := Prev (CEA);
5154 while Present (Prev_CEA) loop
5155 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5156 return No_Match;
5157 end if;
5159 Prev (Prev_CEA);
5160 end loop;
5162 return Match;
5164 -- Else we have a normal static choice
5166 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5167 return Match;
5168 end if;
5170 -- If we fall through, it means that the discrete choice did not
5171 -- match the selecting expression, so continue.
5173 Next (Choice);
5174 end loop;
5176 -- If we get through that loop then all choices were static, and none
5177 -- of them matched the selecting expression. So return No_Match.
5179 return No_Match;
5180 end Check_Case_Expr_Alternative;
5182 -- Local variables
5184 P : Node_Id;
5185 OldP : Node_Id;
5186 Choice : Node_Id;
5188 -- Start of processing for Is_Statically_Unevaluated
5190 begin
5191 -- The (32.x) references here are from RM section 4.9
5193 -- (32.1) An expression is statically unevaluated if it is part of ...
5195 -- This means we have to climb the tree looking for one of the cases
5197 P := Expr;
5198 loop
5199 OldP := P;
5200 P := Parent (P);
5202 -- (32.2) The right operand of a static short-circuit control form
5203 -- whose value is determined by its left operand.
5205 -- AND THEN with False as left operand
5207 if Nkind (P) = N_And_Then
5208 and then Compile_Time_Known_Value (Left_Opnd (P))
5209 and then Is_False (Expr_Value (Left_Opnd (P)))
5210 then
5211 return True;
5213 -- OR ELSE with True as left operand
5215 elsif Nkind (P) = N_Or_Else
5216 and then Compile_Time_Known_Value (Left_Opnd (P))
5217 and then Is_True (Expr_Value (Left_Opnd (P)))
5218 then
5219 return True;
5221 -- (32.3) A dependent_expression of an if_expression whose associated
5222 -- condition is static and equals False.
5224 elsif Nkind (P) = N_If_Expression then
5225 declare
5226 Cond : constant Node_Id := First (Expressions (P));
5227 Texp : constant Node_Id := Next (Cond);
5228 Fexp : constant Node_Id := Next (Texp);
5230 begin
5231 if Compile_Time_Known_Value (Cond) then
5233 -- Condition is True and we are in the right operand
5235 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5236 return True;
5238 -- Condition is False and we are in the left operand
5240 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5241 return True;
5242 end if;
5243 end if;
5244 end;
5246 -- (32.4) A condition or dependent_expression of an if_expression
5247 -- where the condition corresponding to at least one preceding
5248 -- dependent_expression of the if_expression is static and equals
5249 -- True.
5251 -- This refers to cases like
5253 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5255 -- But we expand elsif's out anyway, so the above looks like:
5257 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5259 -- So for us this is caught by the above check for the 32.3 case.
5261 -- (32.5) A dependent_expression of a case_expression whose
5262 -- selecting_expression is static and whose value is not covered
5263 -- by the corresponding discrete_choice_list.
5265 elsif Nkind (P) = N_Case_Expression_Alternative then
5267 -- First, we have to be in the expression to suppress messages.
5268 -- If we are within one of the choices, we want the message.
5270 if OldP = Expression (P) then
5272 -- Statically unevaluated if alternative does not match
5274 if Check_Case_Expr_Alternative (P) = No_Match then
5275 return True;
5276 end if;
5277 end if;
5279 -- (32.6) A choice_expression (or a simple_expression of a range
5280 -- that occurs as a membership_choice of a membership_choice_list)
5281 -- of a static membership test that is preceded in the enclosing
5282 -- membership_choice_list by another item whose individual
5283 -- membership test (see (RM 4.5.2)) statically yields True.
5285 elsif Nkind (P) in N_Membership_Test then
5287 -- Only possibly unevaluated if simple expression is static
5289 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5290 null;
5292 -- All members of the choice list must be static
5294 elsif (Present (Right_Opnd (P))
5295 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5296 or else (Present (Alternatives (P))
5297 and then
5298 not Is_OK_Static_Choice_List (Alternatives (P)))
5299 then
5300 null;
5302 -- If expression is the one and only alternative, then it is
5303 -- definitely not statically unevaluated, so we only have to
5304 -- test the case where there are alternatives present.
5306 elsif Present (Alternatives (P)) then
5308 -- Look for previous matching Choice
5310 Choice := First (Alternatives (P));
5311 while Present (Choice) loop
5313 -- If we reached us and no previous choices matched, this
5314 -- is not the case where we are statically unevaluated.
5316 exit when OldP = Choice;
5318 -- If a previous choice matches, then that is the case where
5319 -- we know our choice is statically unevaluated.
5321 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5322 return True;
5323 end if;
5325 Next (Choice);
5326 end loop;
5328 -- If we fall through the loop, we were not one of the choices,
5329 -- we must have been the expression, so that is not covered by
5330 -- this rule, and we keep going.
5332 null;
5333 end if;
5334 end if;
5336 -- OK, not statically unevaluated at this level, see if we should
5337 -- keep climbing to look for a higher level reason.
5339 -- Special case for component association in aggregates, where
5340 -- we want to keep climbing up to the parent aggregate.
5342 if Nkind (P) = N_Component_Association
5343 and then Nkind (Parent (P)) = N_Aggregate
5344 then
5345 null;
5347 -- All done if not still within subexpression
5349 else
5350 exit when Nkind (P) not in N_Subexpr;
5351 end if;
5352 end loop;
5354 -- If we fall through the loop, not one of the cases covered!
5356 return False;
5357 end Is_Statically_Unevaluated;
5359 --------------------
5360 -- Not_Null_Range --
5361 --------------------
5363 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5364 begin
5365 if Compile_Time_Known_Value (Lo)
5366 and then Compile_Time_Known_Value (Hi)
5367 then
5368 declare
5369 Typ : Entity_Id := Etype (Lo);
5370 begin
5371 -- When called from the frontend, as part of the analysis of
5372 -- potentially static expressions, Typ will be the full view of a
5373 -- type with all the info needed to answer this query. When called
5374 -- from the backend, for example to know whether a range of a loop
5375 -- is null, Typ might be a private type and we need to explicitly
5376 -- switch to its corresponding full view to access the same info.
5378 if Is_Incomplete_Or_Private_Type (Typ)
5379 and then Present (Full_View (Typ))
5380 then
5381 Typ := Full_View (Typ);
5382 end if;
5384 if Is_Discrete_Type (Typ) then
5385 return Expr_Value (Lo) <= Expr_Value (Hi);
5386 else pragma Assert (Is_Real_Type (Typ));
5387 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
5388 end if;
5389 end;
5390 else
5391 return False;
5392 end if;
5394 end Not_Null_Range;
5396 -------------
5397 -- OK_Bits --
5398 -------------
5400 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
5401 begin
5402 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5404 if Bits < 500_000 then
5405 return True;
5407 -- Error if this maximum is exceeded
5409 else
5410 Error_Msg_N ("static value too large, capacity exceeded", N);
5411 return False;
5412 end if;
5413 end OK_Bits;
5415 ------------------
5416 -- Out_Of_Range --
5417 ------------------
5419 procedure Out_Of_Range (N : Node_Id) is
5420 begin
5421 -- If we have the static expression case, then this is an illegality
5422 -- in Ada 95 mode, except that in an instance, we never generate an
5423 -- error (if the error is legitimate, it was already diagnosed in the
5424 -- template).
5426 if Is_Static_Expression (N)
5427 and then not In_Instance
5428 and then not In_Inlined_Body
5429 and then Ada_Version >= Ada_95
5430 then
5431 -- No message if we are statically unevaluated
5433 if Is_Statically_Unevaluated (N) then
5434 null;
5436 -- The expression to compute the length of a packed array is attached
5437 -- to the array type itself, and deserves a separate message.
5439 elsif Nkind (Parent (N)) = N_Defining_Identifier
5440 and then Is_Array_Type (Parent (N))
5441 and then Present (Packed_Array_Impl_Type (Parent (N)))
5442 and then Present (First_Rep_Item (Parent (N)))
5443 then
5444 Error_Msg_N
5445 ("length of packed array must not exceed Integer''Last",
5446 First_Rep_Item (Parent (N)));
5447 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
5449 -- All cases except the special array case.
5450 -- No message if we are dealing with System.Priority values in
5451 -- CodePeer mode where the target runtime may have more priorities.
5453 elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then
5454 Apply_Compile_Time_Constraint_Error
5455 (N, "value not in range of}", CE_Range_Check_Failed);
5456 end if;
5458 -- Here we generate a warning for the Ada 83 case, or when we are in an
5459 -- instance, or when we have a non-static expression case.
5461 else
5462 Apply_Compile_Time_Constraint_Error
5463 (N, "value not in range of}??", CE_Range_Check_Failed);
5464 end if;
5465 end Out_Of_Range;
5467 ----------------------
5468 -- Predicates_Match --
5469 ----------------------
5471 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
5472 Pred1 : Node_Id;
5473 Pred2 : Node_Id;
5475 begin
5476 if Ada_Version < Ada_2012 then
5477 return True;
5479 -- Both types must have predicates or lack them
5481 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
5482 return False;
5484 -- Check matching predicates
5486 else
5487 Pred1 :=
5488 Get_Rep_Item
5489 (T1, Name_Static_Predicate, Check_Parents => False);
5490 Pred2 :=
5491 Get_Rep_Item
5492 (T2, Name_Static_Predicate, Check_Parents => False);
5494 -- Subtypes statically match if the predicate comes from the
5495 -- same declaration, which can only happen if one is a subtype
5496 -- of the other and has no explicit predicate.
5498 -- Suppress warnings on order of actuals, which is otherwise
5499 -- triggered by one of the two calls below.
5501 pragma Warnings (Off);
5502 return Pred1 = Pred2
5503 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
5504 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
5505 pragma Warnings (On);
5506 end if;
5507 end Predicates_Match;
5509 ---------------------------------------------
5510 -- Real_Or_String_Static_Predicate_Matches --
5511 ---------------------------------------------
5513 function Real_Or_String_Static_Predicate_Matches
5514 (Val : Node_Id;
5515 Typ : Entity_Id) return Boolean
5517 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
5518 -- The predicate expression from the type
5520 Pfun : constant Entity_Id := Predicate_Function (Typ);
5521 -- The entity for the predicate function
5523 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
5524 -- The name of the formal of the predicate function. Occurrences of the
5525 -- type name in Expr have been rewritten as references to this formal,
5526 -- and it has a unique name, so we can identify references by this name.
5528 Copy : Node_Id;
5529 -- Copy of the predicate function tree
5531 function Process (N : Node_Id) return Traverse_Result;
5532 -- Function used to process nodes during the traversal in which we will
5533 -- find occurrences of the entity name, and replace such occurrences
5534 -- by a real literal with the value to be tested.
5536 procedure Traverse is new Traverse_Proc (Process);
5537 -- The actual traversal procedure
5539 -------------
5540 -- Process --
5541 -------------
5543 function Process (N : Node_Id) return Traverse_Result is
5544 begin
5545 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
5546 declare
5547 Nod : constant Node_Id := New_Copy (Val);
5548 begin
5549 Set_Sloc (Nod, Sloc (N));
5550 Rewrite (N, Nod);
5551 return Skip;
5552 end;
5554 -- The predicate function may contain string-comparison operations
5555 -- that have been converted into calls to run-time array-comparison
5556 -- routines. To evaluate the predicate statically, we recover the
5557 -- original comparison operation and replace the occurrence of the
5558 -- formal by the static string value. The actuals of the generated
5559 -- call are of the form X'Address.
5561 elsif Nkind (N) in N_Op_Compare
5562 and then Nkind (Left_Opnd (N)) = N_Function_Call
5563 then
5564 declare
5565 C : constant Node_Id := Left_Opnd (N);
5566 F : constant Node_Id := First (Parameter_Associations (C));
5567 L : constant Node_Id := Prefix (F);
5568 R : constant Node_Id := Prefix (Next (F));
5570 begin
5571 -- If an operand is an entity name, it is the formal of the
5572 -- predicate function, so replace it with the string value.
5573 -- It may be either operand in the call. The other operand
5574 -- is a static string from the original predicate.
5576 if Is_Entity_Name (L) then
5577 Rewrite (Left_Opnd (N), New_Copy (Val));
5578 Rewrite (Right_Opnd (N), New_Copy (R));
5580 else
5581 Rewrite (Left_Opnd (N), New_Copy (L));
5582 Rewrite (Right_Opnd (N), New_Copy (Val));
5583 end if;
5585 return Skip;
5586 end;
5588 else
5589 return OK;
5590 end if;
5591 end Process;
5593 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5595 begin
5596 -- First deal with special case of inherited predicate, where the
5597 -- predicate expression looks like:
5599 -- xxPredicate (typ (Ent)) and then Expr
5601 -- where Expr is the predicate expression for this level, and the
5602 -- left operand is the call to evaluate the inherited predicate.
5604 if Nkind (Expr) = N_And_Then
5605 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
5606 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
5607 then
5608 -- OK we have the inherited case, so make a call to evaluate the
5609 -- inherited predicate. If that fails, so do we!
5611 if not
5612 Real_Or_String_Static_Predicate_Matches
5613 (Val => Val,
5614 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
5615 then
5616 return False;
5617 end if;
5619 -- Use the right operand for the continued processing
5621 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
5623 -- Case where call to predicate function appears on its own (this means
5624 -- that the predicate at this level is just inherited from the parent).
5626 elsif Nkind (Expr) = N_Function_Call then
5627 declare
5628 Typ : constant Entity_Id :=
5629 Etype (First_Formal (Entity (Name (Expr))));
5631 begin
5632 -- If the inherited predicate is dynamic, just ignore it. We can't
5633 -- go trying to evaluate a dynamic predicate as a static one!
5635 if Has_Dynamic_Predicate_Aspect (Typ) then
5636 return True;
5638 -- Otherwise inherited predicate is static, check for match
5640 else
5641 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
5642 end if;
5643 end;
5645 -- If not just an inherited predicate, copy whole expression
5647 else
5648 Copy := Copy_Separate_Tree (Expr);
5649 end if;
5651 -- Now we replace occurrences of the entity by the value
5653 Traverse (Copy);
5655 -- And analyze the resulting static expression to see if it is True
5657 Analyze_And_Resolve (Copy, Standard_Boolean);
5658 return Is_True (Expr_Value (Copy));
5659 end Real_Or_String_Static_Predicate_Matches;
5661 -------------------------
5662 -- Rewrite_In_Raise_CE --
5663 -------------------------
5665 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
5666 Typ : constant Entity_Id := Etype (N);
5667 Stat : constant Boolean := Is_Static_Expression (N);
5669 begin
5670 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5671 -- can just clear the condition if the reason is appropriate. We do
5672 -- not do this operation if the parent has a reason other than range
5673 -- check failed, because otherwise we would change the reason.
5675 if Present (Parent (N))
5676 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
5677 and then Reason (Parent (N)) =
5678 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
5679 then
5680 Set_Condition (Parent (N), Empty);
5682 -- Else build an explicit N_Raise_CE
5684 else
5685 Rewrite (N,
5686 Make_Raise_Constraint_Error (Sloc (Exp),
5687 Reason => CE_Range_Check_Failed));
5688 Set_Raises_Constraint_Error (N);
5689 Set_Etype (N, Typ);
5690 end if;
5692 -- Set proper flags in result
5694 Set_Raises_Constraint_Error (N, True);
5695 Set_Is_Static_Expression (N, Stat);
5696 end Rewrite_In_Raise_CE;
5698 ---------------------
5699 -- String_Type_Len --
5700 ---------------------
5702 function String_Type_Len (Stype : Entity_Id) return Uint is
5703 NT : constant Entity_Id := Etype (First_Index (Stype));
5704 T : Entity_Id;
5706 begin
5707 if Is_OK_Static_Subtype (NT) then
5708 T := NT;
5709 else
5710 T := Base_Type (NT);
5711 end if;
5713 return Expr_Value (Type_High_Bound (T)) -
5714 Expr_Value (Type_Low_Bound (T)) + 1;
5715 end String_Type_Len;
5717 ------------------------------------
5718 -- Subtypes_Statically_Compatible --
5719 ------------------------------------
5721 function Subtypes_Statically_Compatible
5722 (T1 : Entity_Id;
5723 T2 : Entity_Id;
5724 Formal_Derived_Matching : Boolean := False) return Boolean
5726 begin
5727 -- Scalar types
5729 if Is_Scalar_Type (T1) then
5731 -- Definitely compatible if we match
5733 if Subtypes_Statically_Match (T1, T2) then
5734 return True;
5736 -- If either subtype is nonstatic then they're not compatible
5738 elsif not Is_OK_Static_Subtype (T1)
5739 or else
5740 not Is_OK_Static_Subtype (T2)
5741 then
5742 return False;
5744 -- Base types must match, but we don't check that (should we???) but
5745 -- we do at least check that both types are real, or both types are
5746 -- not real.
5748 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
5749 return False;
5751 -- Here we check the bounds
5753 else
5754 declare
5755 LB1 : constant Node_Id := Type_Low_Bound (T1);
5756 HB1 : constant Node_Id := Type_High_Bound (T1);
5757 LB2 : constant Node_Id := Type_Low_Bound (T2);
5758 HB2 : constant Node_Id := Type_High_Bound (T2);
5760 begin
5761 if Is_Real_Type (T1) then
5762 return
5763 Expr_Value_R (LB1) > Expr_Value_R (HB1)
5764 or else
5765 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
5766 and then Expr_Value_R (HB1) <= Expr_Value_R (HB2));
5768 else
5769 return
5770 Expr_Value (LB1) > Expr_Value (HB1)
5771 or else
5772 (Expr_Value (LB2) <= Expr_Value (LB1)
5773 and then Expr_Value (HB1) <= Expr_Value (HB2));
5774 end if;
5775 end;
5776 end if;
5778 -- Access types
5780 elsif Is_Access_Type (T1) then
5781 return
5782 (not Is_Constrained (T2)
5783 or else Subtypes_Statically_Match
5784 (Designated_Type (T1), Designated_Type (T2)))
5785 and then not (Can_Never_Be_Null (T2)
5786 and then not Can_Never_Be_Null (T1));
5788 -- All other cases
5790 else
5791 return
5792 (Is_Composite_Type (T1) and then not Is_Constrained (T2))
5793 or else Subtypes_Statically_Match
5794 (T1, T2, Formal_Derived_Matching);
5795 end if;
5796 end Subtypes_Statically_Compatible;
5798 -------------------------------
5799 -- Subtypes_Statically_Match --
5800 -------------------------------
5802 -- Subtypes statically match if they have statically matching constraints
5803 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5804 -- they are the same identical constraint, or if they are static and the
5805 -- values match (RM 4.9.1(1)).
5807 -- In addition, in GNAT, the object size (Esize) values of the types must
5808 -- match if they are set (unless checking an actual for a formal derived
5809 -- type). The use of 'Object_Size can cause this to be false even if the
5810 -- types would otherwise match in the RM sense.
5812 function Subtypes_Statically_Match
5813 (T1 : Entity_Id;
5814 T2 : Entity_Id;
5815 Formal_Derived_Matching : Boolean := False) return Boolean
5817 begin
5818 -- A type always statically matches itself
5820 if T1 = T2 then
5821 return True;
5823 -- No match if sizes different (from use of 'Object_Size). This test
5824 -- is excluded if Formal_Derived_Matching is True, as the base types
5825 -- can be different in that case and typically have different sizes.
5826 -- ??? Frontend_Layout_On_Target used to set Esizes but this is no
5827 -- longer the case, consider removing the last test below.
5829 elsif not Formal_Derived_Matching
5830 and then Known_Static_Esize (T1)
5831 and then Known_Static_Esize (T2)
5832 and then Esize (T1) /= Esize (T2)
5833 then
5834 return False;
5836 -- No match if predicates do not match
5838 elsif not Predicates_Match (T1, T2) then
5839 return False;
5841 -- Scalar types
5843 elsif Is_Scalar_Type (T1) then
5845 -- Base types must be the same
5847 if Base_Type (T1) /= Base_Type (T2) then
5848 return False;
5849 end if;
5851 -- A constrained numeric subtype never matches an unconstrained
5852 -- subtype, i.e. both types must be constrained or unconstrained.
5854 -- To understand the requirement for this test, see RM 4.9.1(1).
5855 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5856 -- a constrained subtype with constraint bounds matching the bounds
5857 -- of its corresponding unconstrained base type. In this situation,
5858 -- Integer and Integer'Base do not statically match, even though
5859 -- they have the same bounds.
5861 -- We only apply this test to types in Standard and types that appear
5862 -- in user programs. That way, we do not have to be too careful about
5863 -- setting Is_Constrained right for Itypes.
5865 if Is_Numeric_Type (T1)
5866 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5867 and then (Scope (T1) = Standard_Standard
5868 or else Comes_From_Source (T1))
5869 and then (Scope (T2) = Standard_Standard
5870 or else Comes_From_Source (T2))
5871 then
5872 return False;
5874 -- A generic scalar type does not statically match its base type
5875 -- (AI-311). In this case we make sure that the formals, which are
5876 -- first subtypes of their bases, are constrained.
5878 elsif Is_Generic_Type (T1)
5879 and then Is_Generic_Type (T2)
5880 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5881 then
5882 return False;
5883 end if;
5885 -- If there was an error in either range, then just assume the types
5886 -- statically match to avoid further junk errors.
5888 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
5889 or else Error_Posted (Scalar_Range (T1))
5890 or else Error_Posted (Scalar_Range (T2))
5891 then
5892 return True;
5893 end if;
5895 -- Otherwise both types have bounds that can be compared
5897 declare
5898 LB1 : constant Node_Id := Type_Low_Bound (T1);
5899 HB1 : constant Node_Id := Type_High_Bound (T1);
5900 LB2 : constant Node_Id := Type_Low_Bound (T2);
5901 HB2 : constant Node_Id := Type_High_Bound (T2);
5903 begin
5904 -- If the bounds are the same tree node, then match (common case)
5906 if LB1 = LB2 and then HB1 = HB2 then
5907 return True;
5909 -- Otherwise bounds must be static and identical value
5911 else
5912 if not Is_OK_Static_Subtype (T1)
5913 or else
5914 not Is_OK_Static_Subtype (T2)
5915 then
5916 return False;
5918 elsif Is_Real_Type (T1) then
5919 return
5920 Expr_Value_R (LB1) = Expr_Value_R (LB2)
5921 and then
5922 Expr_Value_R (HB1) = Expr_Value_R (HB2);
5924 else
5925 return
5926 Expr_Value (LB1) = Expr_Value (LB2)
5927 and then
5928 Expr_Value (HB1) = Expr_Value (HB2);
5929 end if;
5930 end if;
5931 end;
5933 -- Type with discriminants
5935 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
5937 -- Because of view exchanges in multiple instantiations, conformance
5938 -- checking might try to match a partial view of a type with no
5939 -- discriminants with a full view that has defaulted discriminants.
5940 -- In such a case, use the discriminant constraint of the full view,
5941 -- which must exist because we know that the two subtypes have the
5942 -- same base type.
5944 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
5945 -- A generic actual type is declared through a subtype declaration
5946 -- and may have an inconsistent indication of the presence of
5947 -- discriminants, so check the type it renames.
5949 if Is_Generic_Actual_Type (T1)
5950 and then not Has_Discriminants (Etype (T1))
5951 and then not Has_Discriminants (T2)
5952 then
5953 return True;
5955 elsif In_Instance then
5956 if Is_Private_Type (T2)
5957 and then Present (Full_View (T2))
5958 and then Has_Discriminants (Full_View (T2))
5959 then
5960 return Subtypes_Statically_Match (T1, Full_View (T2));
5962 elsif Is_Private_Type (T1)
5963 and then Present (Full_View (T1))
5964 and then Has_Discriminants (Full_View (T1))
5965 then
5966 return Subtypes_Statically_Match (Full_View (T1), T2);
5968 else
5969 return False;
5970 end if;
5971 else
5972 return False;
5973 end if;
5974 end if;
5976 declare
5977 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
5978 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
5980 DA1 : Elmt_Id;
5981 DA2 : Elmt_Id;
5983 begin
5984 if DL1 = DL2 then
5985 return True;
5986 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
5987 return False;
5988 end if;
5990 -- Now loop through the discriminant constraints
5992 -- Note: the guard here seems necessary, since it is possible at
5993 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5995 if Present (DL1) and then Present (DL2) then
5996 DA1 := First_Elmt (DL1);
5997 DA2 := First_Elmt (DL2);
5998 while Present (DA1) loop
5999 declare
6000 Expr1 : constant Node_Id := Node (DA1);
6001 Expr2 : constant Node_Id := Node (DA2);
6003 begin
6004 if not Is_OK_Static_Expression (Expr1)
6005 or else not Is_OK_Static_Expression (Expr2)
6006 then
6007 return False;
6009 -- If either expression raised a constraint error,
6010 -- consider the expressions as matching, since this
6011 -- helps to prevent cascading errors.
6013 elsif Raises_Constraint_Error (Expr1)
6014 or else Raises_Constraint_Error (Expr2)
6015 then
6016 null;
6018 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
6019 return False;
6020 end if;
6021 end;
6023 Next_Elmt (DA1);
6024 Next_Elmt (DA2);
6025 end loop;
6026 end if;
6027 end;
6029 return True;
6031 -- A definite type does not match an indefinite or classwide type.
6032 -- However, a generic type with unknown discriminants may be
6033 -- instantiated with a type with no discriminants, and conformance
6034 -- checking on an inherited operation may compare the actual with the
6035 -- subtype that renames it in the instance.
6037 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
6038 then
6039 return
6040 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
6042 -- Array type
6044 elsif Is_Array_Type (T1) then
6046 -- If either subtype is unconstrained then both must be, and if both
6047 -- are unconstrained then no further checking is needed.
6049 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
6050 return not (Is_Constrained (T1) or else Is_Constrained (T2));
6051 end if;
6053 -- Both subtypes are constrained, so check that the index subtypes
6054 -- statically match.
6056 declare
6057 Index1 : Node_Id := First_Index (T1);
6058 Index2 : Node_Id := First_Index (T2);
6060 begin
6061 while Present (Index1) loop
6062 if not
6063 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6064 then
6065 return False;
6066 end if;
6068 Next_Index (Index1);
6069 Next_Index (Index2);
6070 end loop;
6072 return True;
6073 end;
6075 elsif Is_Access_Type (T1) then
6076 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6077 return False;
6079 elsif Ekind_In (T1, E_Access_Subprogram_Type,
6080 E_Anonymous_Access_Subprogram_Type)
6081 then
6082 return
6083 Subtype_Conformant
6084 (Designated_Type (T1),
6085 Designated_Type (T2));
6086 else
6087 return
6088 Subtypes_Statically_Match
6089 (Designated_Type (T1),
6090 Designated_Type (T2))
6091 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
6092 end if;
6094 -- All other types definitely match
6096 else
6097 return True;
6098 end if;
6099 end Subtypes_Statically_Match;
6101 ----------
6102 -- Test --
6103 ----------
6105 function Test (Cond : Boolean) return Uint is
6106 begin
6107 if Cond then
6108 return Uint_1;
6109 else
6110 return Uint_0;
6111 end if;
6112 end Test;
6114 ---------------------
6115 -- Test_Comparison --
6116 ---------------------
6118 procedure Test_Comparison
6119 (Op : Node_Id;
6120 Assume_Valid : Boolean;
6121 True_Result : out Boolean;
6122 False_Result : out Boolean)
6124 Left : constant Node_Id := Left_Opnd (Op);
6125 Left_Typ : constant Entity_Id := Etype (Left);
6126 Orig_Op : constant Node_Id := Original_Node (Op);
6128 procedure Replacement_Warning (Msg : String);
6129 -- Emit a warning on a comparison that can be replaced by '='
6131 -------------------------
6132 -- Replacement_Warning --
6133 -------------------------
6135 procedure Replacement_Warning (Msg : String) is
6136 begin
6137 if Constant_Condition_Warnings
6138 and then Comes_From_Source (Orig_Op)
6139 and then Is_Integer_Type (Left_Typ)
6140 and then not Error_Posted (Op)
6141 and then not Has_Warnings_Off (Left_Typ)
6142 and then not In_Instance
6143 then
6144 Error_Msg_N (Msg, Op);
6145 end if;
6146 end Replacement_Warning;
6148 -- Local variables
6150 Res : constant Compare_Result :=
6151 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
6153 -- Start of processing for Test_Comparison
6155 begin
6156 case N_Op_Compare (Nkind (Op)) is
6157 when N_Op_Eq =>
6158 True_Result := Res = EQ;
6159 False_Result := Res = LT or else Res = GT or else Res = NE;
6161 when N_Op_Ge =>
6162 True_Result := Res in Compare_GE;
6163 False_Result := Res = LT;
6165 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
6166 Replacement_Warning
6167 ("can never be greater than, could replace by ""'=""?c?");
6168 end if;
6170 when N_Op_Gt =>
6171 True_Result := Res = GT;
6172 False_Result := Res in Compare_LE;
6174 when N_Op_Le =>
6175 True_Result := Res in Compare_LE;
6176 False_Result := Res = GT;
6178 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
6179 Replacement_Warning
6180 ("can never be less than, could replace by ""'=""?c?");
6181 end if;
6183 when N_Op_Lt =>
6184 True_Result := Res = LT;
6185 False_Result := Res in Compare_GE;
6187 when N_Op_Ne =>
6188 True_Result := Res = NE or else Res = GT or else Res = LT;
6189 False_Result := Res = EQ;
6190 end case;
6191 end Test_Comparison;
6193 ---------------------------------
6194 -- Test_Expression_Is_Foldable --
6195 ---------------------------------
6197 -- One operand case
6199 procedure Test_Expression_Is_Foldable
6200 (N : Node_Id;
6201 Op1 : Node_Id;
6202 Stat : out Boolean;
6203 Fold : out Boolean)
6205 begin
6206 Stat := False;
6207 Fold := False;
6209 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6210 return;
6211 end if;
6213 -- If operand is Any_Type, just propagate to result and do not
6214 -- try to fold, this prevents cascaded errors.
6216 if Etype (Op1) = Any_Type then
6217 Set_Etype (N, Any_Type);
6218 return;
6220 -- If operand raises constraint error, then replace node N with the
6221 -- raise constraint error node, and we are obviously not foldable.
6222 -- Note that this replacement inherits the Is_Static_Expression flag
6223 -- from the operand.
6225 elsif Raises_Constraint_Error (Op1) then
6226 Rewrite_In_Raise_CE (N, Op1);
6227 return;
6229 -- If the operand is not static, then the result is not static, and
6230 -- all we have to do is to check the operand since it is now known
6231 -- to appear in a non-static context.
6233 elsif not Is_Static_Expression (Op1) then
6234 Check_Non_Static_Context (Op1);
6235 Fold := Compile_Time_Known_Value (Op1);
6236 return;
6238 -- An expression of a formal modular type is not foldable because
6239 -- the modulus is unknown.
6241 elsif Is_Modular_Integer_Type (Etype (Op1))
6242 and then Is_Generic_Type (Etype (Op1))
6243 then
6244 Check_Non_Static_Context (Op1);
6245 return;
6247 -- Here we have the case of an operand whose type is OK, which is
6248 -- static, and which does not raise constraint error, we can fold.
6250 else
6251 Set_Is_Static_Expression (N);
6252 Fold := True;
6253 Stat := True;
6254 end if;
6255 end Test_Expression_Is_Foldable;
6257 -- Two operand case
6259 procedure Test_Expression_Is_Foldable
6260 (N : Node_Id;
6261 Op1 : Node_Id;
6262 Op2 : Node_Id;
6263 Stat : out Boolean;
6264 Fold : out Boolean;
6265 CRT_Safe : Boolean := False)
6267 Rstat : constant Boolean := Is_Static_Expression (Op1)
6268 and then
6269 Is_Static_Expression (Op2);
6271 begin
6272 Stat := False;
6273 Fold := False;
6275 -- Inhibit folding if -gnatd.f flag set
6277 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6278 return;
6279 end if;
6281 -- If either operand is Any_Type, just propagate to result and
6282 -- do not try to fold, this prevents cascaded errors.
6284 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
6285 Set_Etype (N, Any_Type);
6286 return;
6288 -- If left operand raises constraint error, then replace node N with the
6289 -- Raise_Constraint_Error node, and we are obviously not foldable.
6290 -- Is_Static_Expression is set from the two operands in the normal way,
6291 -- and we check the right operand if it is in a non-static context.
6293 elsif Raises_Constraint_Error (Op1) then
6294 if not Rstat then
6295 Check_Non_Static_Context (Op2);
6296 end if;
6298 Rewrite_In_Raise_CE (N, Op1);
6299 Set_Is_Static_Expression (N, Rstat);
6300 return;
6302 -- Similar processing for the case of the right operand. Note that we
6303 -- don't use this routine for the short-circuit case, so we do not have
6304 -- to worry about that special case here.
6306 elsif Raises_Constraint_Error (Op2) then
6307 if not Rstat then
6308 Check_Non_Static_Context (Op1);
6309 end if;
6311 Rewrite_In_Raise_CE (N, Op2);
6312 Set_Is_Static_Expression (N, Rstat);
6313 return;
6315 -- Exclude expressions of a generic modular type, as above
6317 elsif Is_Modular_Integer_Type (Etype (Op1))
6318 and then Is_Generic_Type (Etype (Op1))
6319 then
6320 Check_Non_Static_Context (Op1);
6321 return;
6323 -- If result is not static, then check non-static contexts on operands
6324 -- since one of them may be static and the other one may not be static.
6326 elsif not Rstat then
6327 Check_Non_Static_Context (Op1);
6328 Check_Non_Static_Context (Op2);
6330 if CRT_Safe then
6331 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
6332 and then CRT_Safe_Compile_Time_Known_Value (Op2);
6333 else
6334 Fold := Compile_Time_Known_Value (Op1)
6335 and then Compile_Time_Known_Value (Op2);
6336 end if;
6338 return;
6340 -- Else result is static and foldable. Both operands are static, and
6341 -- neither raises constraint error, so we can definitely fold.
6343 else
6344 Set_Is_Static_Expression (N);
6345 Fold := True;
6346 Stat := True;
6347 return;
6348 end if;
6349 end Test_Expression_Is_Foldable;
6351 -------------------
6352 -- Test_In_Range --
6353 -------------------
6355 function Test_In_Range
6356 (N : Node_Id;
6357 Typ : Entity_Id;
6358 Assume_Valid : Boolean;
6359 Fixed_Int : Boolean;
6360 Int_Real : Boolean) return Range_Membership
6362 Val : Uint;
6363 Valr : Ureal;
6365 pragma Warnings (Off, Assume_Valid);
6366 -- For now Assume_Valid is unreferenced since the current implementation
6367 -- always returns Unknown if N is not a compile time known value, but we
6368 -- keep the parameter to allow for future enhancements in which we try
6369 -- to get the information in the variable case as well.
6371 begin
6372 -- If an error was posted on expression, then return Unknown, we do not
6373 -- want cascaded errors based on some false analysis of a junk node.
6375 if Error_Posted (N) then
6376 return Unknown;
6378 -- Expression that raises constraint error is an odd case. We certainly
6379 -- do not want to consider it to be in range. It might make sense to
6380 -- consider it always out of range, but this causes incorrect error
6381 -- messages about static expressions out of range. So we just return
6382 -- Unknown, which is always safe.
6384 elsif Raises_Constraint_Error (N) then
6385 return Unknown;
6387 -- Universal types have no range limits, so always in range
6389 elsif Typ = Universal_Integer or else Typ = Universal_Real then
6390 return In_Range;
6392 -- Never known if not scalar type. Don't know if this can actually
6393 -- happen, but our spec allows it, so we must check.
6395 elsif not Is_Scalar_Type (Typ) then
6396 return Unknown;
6398 -- Never known if this is a generic type, since the bounds of generic
6399 -- types are junk. Note that if we only checked for static expressions
6400 -- (instead of compile time known values) below, we would not need this
6401 -- check, because values of a generic type can never be static, but they
6402 -- can be known at compile time.
6404 elsif Is_Generic_Type (Typ) then
6405 return Unknown;
6407 -- Case of a known compile time value, where we can check if it is in
6408 -- the bounds of the given type.
6410 elsif Compile_Time_Known_Value (N) then
6411 declare
6412 Lo : Node_Id;
6413 Hi : Node_Id;
6415 LB_Known : Boolean;
6416 HB_Known : Boolean;
6418 begin
6419 Lo := Type_Low_Bound (Typ);
6420 Hi := Type_High_Bound (Typ);
6422 LB_Known := Compile_Time_Known_Value (Lo);
6423 HB_Known := Compile_Time_Known_Value (Hi);
6425 -- Fixed point types should be considered as such only if flag
6426 -- Fixed_Int is set to False.
6428 if Is_Floating_Point_Type (Typ)
6429 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
6430 or else Int_Real
6431 then
6432 Valr := Expr_Value_R (N);
6434 if LB_Known and HB_Known then
6435 if Valr >= Expr_Value_R (Lo)
6436 and then
6437 Valr <= Expr_Value_R (Hi)
6438 then
6439 return In_Range;
6440 else
6441 return Out_Of_Range;
6442 end if;
6444 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
6445 or else
6446 (HB_Known and then Valr > Expr_Value_R (Hi))
6447 then
6448 return Out_Of_Range;
6450 else
6451 return Unknown;
6452 end if;
6454 else
6455 Val := Expr_Value (N);
6457 if LB_Known and HB_Known then
6458 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
6459 then
6460 return In_Range;
6461 else
6462 return Out_Of_Range;
6463 end if;
6465 elsif (LB_Known and then Val < Expr_Value (Lo))
6466 or else
6467 (HB_Known and then Val > Expr_Value (Hi))
6468 then
6469 return Out_Of_Range;
6471 else
6472 return Unknown;
6473 end if;
6474 end if;
6475 end;
6477 -- Here for value not known at compile time. Case of expression subtype
6478 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6479 -- In this case we know it is in range without knowing its value.
6481 elsif Assume_Valid
6482 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
6483 then
6484 return In_Range;
6486 -- Another special case. For signed integer types, if the target type
6487 -- has Is_Known_Valid set, and the source type does not have a larger
6488 -- size, then the source value must be in range. We exclude biased
6489 -- types, because they bizarrely can generate out of range values.
6491 elsif Is_Signed_Integer_Type (Etype (N))
6492 and then Is_Known_Valid (Typ)
6493 and then Esize (Etype (N)) <= Esize (Typ)
6494 and then not Has_Biased_Representation (Etype (N))
6495 then
6496 return In_Range;
6498 -- For all other cases, result is unknown
6500 else
6501 return Unknown;
6502 end if;
6503 end Test_In_Range;
6505 --------------
6506 -- To_Bits --
6507 --------------
6509 procedure To_Bits (U : Uint; B : out Bits) is
6510 begin
6511 for J in 0 .. B'Last loop
6512 B (J) := (U / (2 ** J)) mod 2 /= 0;
6513 end loop;
6514 end To_Bits;
6516 --------------------
6517 -- Why_Not_Static --
6518 --------------------
6520 procedure Why_Not_Static (Expr : Node_Id) is
6521 N : constant Node_Id := Original_Node (Expr);
6522 Typ : Entity_Id := Empty;
6523 E : Entity_Id;
6524 Alt : Node_Id;
6525 Exp : Node_Id;
6527 procedure Why_Not_Static_List (L : List_Id);
6528 -- A version that can be called on a list of expressions. Finds all
6529 -- non-static violations in any element of the list.
6531 -------------------------
6532 -- Why_Not_Static_List --
6533 -------------------------
6535 procedure Why_Not_Static_List (L : List_Id) is
6536 N : Node_Id;
6537 begin
6538 if Is_Non_Empty_List (L) then
6539 N := First (L);
6540 while Present (N) loop
6541 Why_Not_Static (N);
6542 Next (N);
6543 end loop;
6544 end if;
6545 end Why_Not_Static_List;
6547 -- Start of processing for Why_Not_Static
6549 begin
6550 -- Ignore call on error or empty node
6552 if No (Expr) or else Nkind (Expr) = N_Error then
6553 return;
6554 end if;
6556 -- Preprocessing for sub expressions
6558 if Nkind (Expr) in N_Subexpr then
6560 -- Nothing to do if expression is static
6562 if Is_OK_Static_Expression (Expr) then
6563 return;
6564 end if;
6566 -- Test for constraint error raised
6568 if Raises_Constraint_Error (Expr) then
6570 -- Special case membership to find out which piece to flag
6572 if Nkind (N) in N_Membership_Test then
6573 if Raises_Constraint_Error (Left_Opnd (N)) then
6574 Why_Not_Static (Left_Opnd (N));
6575 return;
6577 elsif Present (Right_Opnd (N))
6578 and then Raises_Constraint_Error (Right_Opnd (N))
6579 then
6580 Why_Not_Static (Right_Opnd (N));
6581 return;
6583 else
6584 pragma Assert (Present (Alternatives (N)));
6586 Alt := First (Alternatives (N));
6587 while Present (Alt) loop
6588 if Raises_Constraint_Error (Alt) then
6589 Why_Not_Static (Alt);
6590 return;
6591 else
6592 Next (Alt);
6593 end if;
6594 end loop;
6595 end if;
6597 -- Special case a range to find out which bound to flag
6599 elsif Nkind (N) = N_Range then
6600 if Raises_Constraint_Error (Low_Bound (N)) then
6601 Why_Not_Static (Low_Bound (N));
6602 return;
6604 elsif Raises_Constraint_Error (High_Bound (N)) then
6605 Why_Not_Static (High_Bound (N));
6606 return;
6607 end if;
6609 -- Special case attribute to see which part to flag
6611 elsif Nkind (N) = N_Attribute_Reference then
6612 if Raises_Constraint_Error (Prefix (N)) then
6613 Why_Not_Static (Prefix (N));
6614 return;
6615 end if;
6617 if Present (Expressions (N)) then
6618 Exp := First (Expressions (N));
6619 while Present (Exp) loop
6620 if Raises_Constraint_Error (Exp) then
6621 Why_Not_Static (Exp);
6622 return;
6623 end if;
6625 Next (Exp);
6626 end loop;
6627 end if;
6629 -- Special case a subtype name
6631 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
6632 Error_Msg_NE
6633 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
6634 return;
6635 end if;
6637 -- End of special cases
6639 Error_Msg_N
6640 ("!expression raises exception, cannot be static (RM 4.9(34))",
6642 return;
6643 end if;
6645 -- If no type, then something is pretty wrong, so ignore
6647 Typ := Etype (Expr);
6649 if No (Typ) then
6650 return;
6651 end if;
6653 -- Type must be scalar or string type (but allow Bignum, since this
6654 -- is really a scalar type from our point of view in this diagnosis).
6656 if not Is_Scalar_Type (Typ)
6657 and then not Is_String_Type (Typ)
6658 and then not Is_RTE (Typ, RE_Bignum)
6659 then
6660 Error_Msg_N
6661 ("!static expression must have scalar or string type " &
6662 "(RM 4.9(2))", N);
6663 return;
6664 end if;
6665 end if;
6667 -- If we got through those checks, test particular node kind
6669 case Nkind (N) is
6671 -- Entity name
6673 when N_Expanded_Name
6674 | N_Identifier
6675 | N_Operator_Symbol
6677 E := Entity (N);
6679 if Is_Named_Number (E) then
6680 null;
6682 elsif Ekind (E) = E_Constant then
6684 -- One case we can give a metter message is when we have a
6685 -- string literal created by concatenating an aggregate with
6686 -- an others expression.
6688 Entity_Case : declare
6689 CV : constant Node_Id := Constant_Value (E);
6690 CO : constant Node_Id := Original_Node (CV);
6692 function Is_Aggregate (N : Node_Id) return Boolean;
6693 -- See if node N came from an others aggregate, if so
6694 -- return True and set Error_Msg_Sloc to aggregate.
6696 ------------------
6697 -- Is_Aggregate --
6698 ------------------
6700 function Is_Aggregate (N : Node_Id) return Boolean is
6701 begin
6702 if Nkind (Original_Node (N)) = N_Aggregate then
6703 Error_Msg_Sloc := Sloc (Original_Node (N));
6704 return True;
6706 elsif Is_Entity_Name (N)
6707 and then Ekind (Entity (N)) = E_Constant
6708 and then
6709 Nkind (Original_Node (Constant_Value (Entity (N)))) =
6710 N_Aggregate
6711 then
6712 Error_Msg_Sloc :=
6713 Sloc (Original_Node (Constant_Value (Entity (N))));
6714 return True;
6716 else
6717 return False;
6718 end if;
6719 end Is_Aggregate;
6721 -- Start of processing for Entity_Case
6723 begin
6724 if Is_Aggregate (CV)
6725 or else (Nkind (CO) = N_Op_Concat
6726 and then (Is_Aggregate (Left_Opnd (CO))
6727 or else
6728 Is_Aggregate (Right_Opnd (CO))))
6729 then
6730 Error_Msg_N ("!aggregate (#) is never static", N);
6732 elsif No (CV) or else not Is_Static_Expression (CV) then
6733 Error_Msg_NE
6734 ("!& is not a static constant (RM 4.9(5))", N, E);
6735 end if;
6736 end Entity_Case;
6738 elsif Is_Type (E) then
6739 Error_Msg_NE
6740 ("!& is not a static subtype (RM 4.9(26))", N, E);
6742 else
6743 Error_Msg_NE
6744 ("!& is not static constant or named number "
6745 & "(RM 4.9(5))", N, E);
6746 end if;
6748 -- Binary operator
6750 when N_Binary_Op
6751 | N_Membership_Test
6752 | N_Short_Circuit
6754 if Nkind (N) in N_Op_Shift then
6755 Error_Msg_N
6756 ("!shift functions are never static (RM 4.9(6,18))", N);
6757 else
6758 Why_Not_Static (Left_Opnd (N));
6759 Why_Not_Static (Right_Opnd (N));
6760 end if;
6762 -- Unary operator
6764 when N_Unary_Op =>
6765 Why_Not_Static (Right_Opnd (N));
6767 -- Attribute reference
6769 when N_Attribute_Reference =>
6770 Why_Not_Static_List (Expressions (N));
6772 E := Etype (Prefix (N));
6774 if E = Standard_Void_Type then
6775 return;
6776 end if;
6778 -- Special case non-scalar'Size since this is a common error
6780 if Attribute_Name (N) = Name_Size then
6781 Error_Msg_N
6782 ("!size attribute is only static for static scalar type "
6783 & "(RM 4.9(7,8))", N);
6785 -- Flag array cases
6787 elsif Is_Array_Type (E) then
6788 if not Nam_In (Attribute_Name (N), Name_First,
6789 Name_Last,
6790 Name_Length)
6791 then
6792 Error_Msg_N
6793 ("!static array attribute must be Length, First, or Last "
6794 & "(RM 4.9(8))", N);
6796 -- Since we know the expression is not-static (we already
6797 -- tested for this, must mean array is not static).
6799 else
6800 Error_Msg_N
6801 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
6802 end if;
6804 return;
6806 -- Special case generic types, since again this is a common source
6807 -- of confusion.
6809 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
6810 Error_Msg_N
6811 ("!attribute of generic type is never static "
6812 & "(RM 4.9(7,8))", N);
6814 elsif Is_OK_Static_Subtype (E) then
6815 null;
6817 elsif Is_Scalar_Type (E) then
6818 Error_Msg_N
6819 ("!prefix type for attribute is not static scalar subtype "
6820 & "(RM 4.9(7))", N);
6822 else
6823 Error_Msg_N
6824 ("!static attribute must apply to array/scalar type "
6825 & "(RM 4.9(7,8))", N);
6826 end if;
6828 -- String literal
6830 when N_String_Literal =>
6831 Error_Msg_N
6832 ("!subtype of string literal is non-static (RM 4.9(4))", N);
6834 -- Explicit dereference
6836 when N_Explicit_Dereference =>
6837 Error_Msg_N
6838 ("!explicit dereference is never static (RM 4.9)", N);
6840 -- Function call
6842 when N_Function_Call =>
6843 Why_Not_Static_List (Parameter_Associations (N));
6845 -- Complain about non-static function call unless we have Bignum
6846 -- which means that the underlying expression is really some
6847 -- scalar arithmetic operation.
6849 if not Is_RTE (Typ, RE_Bignum) then
6850 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
6851 end if;
6853 -- Parameter assocation (test actual parameter)
6855 when N_Parameter_Association =>
6856 Why_Not_Static (Explicit_Actual_Parameter (N));
6858 -- Indexed component
6860 when N_Indexed_Component =>
6861 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
6863 -- Procedure call
6865 when N_Procedure_Call_Statement =>
6866 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
6868 -- Qualified expression (test expression)
6870 when N_Qualified_Expression =>
6871 Why_Not_Static (Expression (N));
6873 -- Aggregate
6875 when N_Aggregate
6876 | N_Extension_Aggregate
6878 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
6880 -- Range
6882 when N_Range =>
6883 Why_Not_Static (Low_Bound (N));
6884 Why_Not_Static (High_Bound (N));
6886 -- Range constraint, test range expression
6888 when N_Range_Constraint =>
6889 Why_Not_Static (Range_Expression (N));
6891 -- Subtype indication, test constraint
6893 when N_Subtype_Indication =>
6894 Why_Not_Static (Constraint (N));
6896 -- Selected component
6898 when N_Selected_Component =>
6899 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
6901 -- Slice
6903 when N_Slice =>
6904 Error_Msg_N ("!slice is never static (RM 4.9)", N);
6906 when N_Type_Conversion =>
6907 Why_Not_Static (Expression (N));
6909 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
6910 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
6911 then
6912 Error_Msg_N
6913 ("!static conversion requires static scalar subtype result "
6914 & "(RM 4.9(9))", N);
6915 end if;
6917 -- Unchecked type conversion
6919 when N_Unchecked_Type_Conversion =>
6920 Error_Msg_N
6921 ("!unchecked type conversion is never static (RM 4.9)", N);
6923 -- All other cases, no reason to give
6925 when others =>
6926 null;
6927 end case;
6928 end Why_Not_Static;
6930 end Sem_Eval;