073dfc6c34c1d76d06f3f1bf72390022e91026cf
[gcc.git] / gcc / ada / sem_eval.adb
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-2017, 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 ------------------------------------------------------------------------------
25
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;
57
58 package body Sem_Eval is
59
60 -----------------------------------------
61 -- Handling of Compile Time Evaluation --
62 -----------------------------------------
63
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.
68
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.
72
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.
76
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.
84
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).
91
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.
98
99 ----------------
100 -- Local Data --
101 ----------------
102
103 type Bits is array (Nat range <>) of Boolean;
104 -- Used to convert unsigned (modular) values for folding logical ops
105
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).
112
113 CV_Bits : constant := 5;
114 -- Number of low order bits of Node_Id value used to reference entries
115 -- in the cache table.
116
117 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
118 -- Size of cache for compile time values
119
120 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
121
122 type CV_Entry is record
123 N : Node_Id;
124 V : Uint;
125 end record;
126
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.
131
132 type CV_Cache_Array is array (CV_Range) of CV_Entry;
133
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.
137
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.
141
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
145
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.
162 --
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.
166
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).
176
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.
184
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).
191
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).
197
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.
201
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.
205
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.
210
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.
214
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).
223
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).
231
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.
238
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.
248
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.
252
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.
259
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
266
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).
280 --
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.
285 --
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.
291 --
292 -- If Fold and Stat are both set to False then this routine performs also
293 -- the following extra actions:
294 --
295 -- If either operand is Any_Type then propagate it to result to prevent
296 -- cascaded errors.
297 --
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.
301
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.
314
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.
326
327 procedure To_Bits (U : Uint; B : out Bits);
328 -- Converts a Uint value to a bit string of length B'Length
329
330 -----------------------------------------------
331 -- Check_Expression_Against_Static_Predicate --
332 -----------------------------------------------
333
334 procedure Check_Expression_Against_Static_Predicate
335 (Expr : Node_Id;
336 Typ : Entity_Id)
337 is
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).
342
343 if not (Has_Static_Predicate (Typ)
344 and then Compile_Time_Known_Value (Expr))
345 then
346 return;
347 end if;
348
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.
356
357 -- Case of real static predicate
358
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;
367
368 -- Case of string static predicate
369
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;
377
378 -- Case of discrete static predicate
379
380 else
381 pragma Assert (Is_Discrete_Type (Typ));
382
383 -- If static predicate matches, nothing to do
384
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;
390
391 -- Here we know that the predicate will fail
392
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.
396
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);
406
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.
410
411 else
412 Error_Msg_NE
413 ("??expression fails predicate check on &", Expr, Typ);
414 end if;
415 end Check_Expression_Against_Static_Predicate;
416
417 ------------------------------
418 -- Check_Non_Static_Context --
419 ------------------------------
420
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);
426
427 begin
428 -- Ignore cases of non-scalar types, error types, or universal real
429 -- types that have no usable bounds.
430
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;
438
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.
442
443 if Raises_Constraint_Error (N) then
444 return;
445 end if;
446
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.
454
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);
460
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)).
466
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;
475
476 return;
477 end if;
478
479 -- Here we have the case of outer level static expression of scalar
480 -- type, where the processing of this procedure is needed.
481
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.
490
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.
498
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.
507
508 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
509 Out_Of_Range (N);
510 return;
511 end if;
512
513 -- Note: we have to copy the node, to avoid problems with conformance
514 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
515
516 Rewrite (N, New_Copy (N));
517
518 if not Is_Floating_Point_Type (T) then
519 Set_Realval
520 (N, Corresponding_Integer_Value (N) * Small_Value (T));
521
522 elsif not UR_Is_Zero (Realval (N)) then
523
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.
532
533 Set_Realval
534 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
535 Set_Is_Machine_Number (N);
536 end if;
537
538 end if;
539
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.
543
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.
549
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);
561
562 -- Check out of range of base type
563
564 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
565 Out_Of_Range (N);
566
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).
571
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;
575
576 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
577 Apply_Compile_Time_Constraint_Error
578 (N, "value not in range of}<<", CE_Range_Check_Failed);
579
580 elsif Checks_On then
581 Enable_Range_Check (N);
582
583 else
584 Set_Do_Range_Check (N, False);
585 end if;
586 end if;
587 end Check_Non_Static_Context;
588
589 ---------------------------------
590 -- Check_String_Literal_Length --
591 ---------------------------------
592
593 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
594 begin
595 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
596 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
597 then
598 Apply_Compile_Time_Constraint_Error
599 (N, "string length wrong for}??",
600 CE_Length_Check_Failed,
601 Ent => Ttype,
602 Typ => Ttype);
603 end if;
604 end if;
605 end Check_String_Literal_Length;
606
607 --------------------
608 -- Choice_Matches --
609 --------------------
610
611 function Choice_Matches
612 (Expr : Node_Id;
613 Choice : Node_Id) return Match_Result
614 is
615 Etyp : constant Entity_Id := Etype (Expr);
616 Val : Uint;
617 ValR : Ureal;
618 ValS : Node_Id;
619
620 begin
621 pragma Assert (Compile_Time_Known_Value (Expr));
622 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
623
624 if not Is_OK_Static_Choice (Choice) then
625 Set_Raises_Constraint_Error (Choice);
626 return Non_Static;
627
628 -- When the choice denotes a subtype with a static predictate, check the
629 -- expression against the predicate values.
630
631 elsif (Nkind (Choice) = N_Subtype_Indication
632 or else (Is_Entity_Name (Choice)
633 and then Is_Type (Entity (Choice))))
634 and then Has_Predicates (Etype (Choice))
635 and then Has_Static_Predicate (Etype (Choice))
636 then
637 return
638 Choices_Match (Expr, Static_Discrete_Predicate (Etype (Choice)));
639
640 -- Discrete type case
641
642 elsif Is_Discrete_Type (Etyp) then
643 Val := Expr_Value (Expr);
644
645 if Nkind (Choice) = N_Range then
646 if Val >= Expr_Value (Low_Bound (Choice))
647 and then
648 Val <= Expr_Value (High_Bound (Choice))
649 then
650 return Match;
651 else
652 return No_Match;
653 end if;
654
655 elsif Nkind (Choice) = N_Subtype_Indication
656 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
657 then
658 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
659 and then
660 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
661 then
662 return Match;
663 else
664 return No_Match;
665 end if;
666
667 elsif Nkind (Choice) = N_Others_Choice then
668 return Match;
669
670 else
671 if Val = Expr_Value (Choice) then
672 return Match;
673 else
674 return No_Match;
675 end if;
676 end if;
677
678 -- Real type case
679
680 elsif Is_Real_Type (Etyp) then
681 ValR := Expr_Value_R (Expr);
682
683 if Nkind (Choice) = N_Range then
684 if ValR >= Expr_Value_R (Low_Bound (Choice))
685 and then
686 ValR <= Expr_Value_R (High_Bound (Choice))
687 then
688 return Match;
689 else
690 return No_Match;
691 end if;
692
693 elsif Nkind (Choice) = N_Subtype_Indication
694 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
695 then
696 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
697 and then
698 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
699 then
700 return Match;
701 else
702 return No_Match;
703 end if;
704
705 else
706 if ValR = Expr_Value_R (Choice) then
707 return Match;
708 else
709 return No_Match;
710 end if;
711 end if;
712
713 -- String type cases
714
715 else
716 pragma Assert (Is_String_Type (Etyp));
717 ValS := Expr_Value_S (Expr);
718
719 if Nkind (Choice) = N_Subtype_Indication
720 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
721 then
722 if not Is_Constrained (Etype (Choice)) then
723 return Match;
724
725 else
726 declare
727 Typlen : constant Uint :=
728 String_Type_Len (Etype (Choice));
729 Strlen : constant Uint :=
730 UI_From_Int (String_Length (Strval (ValS)));
731 begin
732 if Typlen = Strlen then
733 return Match;
734 else
735 return No_Match;
736 end if;
737 end;
738 end if;
739
740 else
741 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
742 then
743 return Match;
744 else
745 return No_Match;
746 end if;
747 end if;
748 end if;
749 end Choice_Matches;
750
751 -------------------
752 -- Choices_Match --
753 -------------------
754
755 function Choices_Match
756 (Expr : Node_Id;
757 Choices : List_Id) return Match_Result
758 is
759 Choice : Node_Id;
760 Result : Match_Result;
761
762 begin
763 Choice := First (Choices);
764 while Present (Choice) loop
765 Result := Choice_Matches (Expr, Choice);
766
767 if Result /= No_Match then
768 return Result;
769 end if;
770
771 Next (Choice);
772 end loop;
773
774 return No_Match;
775 end Choices_Match;
776
777 --------------------------
778 -- Compile_Time_Compare --
779 --------------------------
780
781 function Compile_Time_Compare
782 (L, R : Node_Id;
783 Assume_Valid : Boolean) return Compare_Result
784 is
785 Discard : aliased Uint;
786 begin
787 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
788 end Compile_Time_Compare;
789
790 function Compile_Time_Compare
791 (L, R : Node_Id;
792 Diff : access Uint;
793 Assume_Valid : Boolean;
794 Rec : Boolean := False) return Compare_Result
795 is
796 Ltyp : Entity_Id := Etype (L);
797 Rtyp : Entity_Id := Etype (R);
798
799 Discard : aliased Uint;
800
801 procedure Compare_Decompose
802 (N : Node_Id;
803 R : out Node_Id;
804 V : out Uint);
805 -- This procedure decomposes the node N into an expression node and a
806 -- signed offset, so that the value of N is equal to the value of R plus
807 -- the value V (which may be negative). If no such decomposition is
808 -- possible, then on return R is a copy of N, and V is set to zero.
809
810 function Compare_Fixup (N : Node_Id) return Node_Id;
811 -- This function deals with replacing 'Last and 'First references with
812 -- their corresponding type bounds, which we then can compare. The
813 -- argument is the original node, the result is the identity, unless we
814 -- have a 'Last/'First reference in which case the value returned is the
815 -- appropriate type bound.
816
817 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
818 -- Even if the context does not assume that values are valid, some
819 -- simple cases can be recognized.
820
821 function Is_Same_Value (L, R : Node_Id) return Boolean;
822 -- Returns True iff L and R represent expressions that definitely have
823 -- identical (but not necessarily compile time known) values Indeed the
824 -- caller is expected to have already dealt with the cases of compile
825 -- time known values, so these are not tested here.
826
827 -----------------------
828 -- Compare_Decompose --
829 -----------------------
830
831 procedure Compare_Decompose
832 (N : Node_Id;
833 R : out Node_Id;
834 V : out Uint)
835 is
836 begin
837 if Nkind (N) = N_Op_Add
838 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
839 then
840 R := Left_Opnd (N);
841 V := Intval (Right_Opnd (N));
842 return;
843
844 elsif Nkind (N) = N_Op_Subtract
845 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
846 then
847 R := Left_Opnd (N);
848 V := UI_Negate (Intval (Right_Opnd (N)));
849 return;
850
851 elsif Nkind (N) = N_Attribute_Reference then
852 if Attribute_Name (N) = Name_Succ then
853 R := First (Expressions (N));
854 V := Uint_1;
855 return;
856
857 elsif Attribute_Name (N) = Name_Pred then
858 R := First (Expressions (N));
859 V := Uint_Minus_1;
860 return;
861 end if;
862 end if;
863
864 R := N;
865 V := Uint_0;
866 end Compare_Decompose;
867
868 -------------------
869 -- Compare_Fixup --
870 -------------------
871
872 function Compare_Fixup (N : Node_Id) return Node_Id is
873 Indx : Node_Id;
874 Xtyp : Entity_Id;
875 Subs : Nat;
876
877 begin
878 -- Fixup only required for First/Last attribute reference
879
880 if Nkind (N) = N_Attribute_Reference
881 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
882 then
883 Xtyp := Etype (Prefix (N));
884
885 -- If we have no type, then just abandon the attempt to do
886 -- a fixup, this is probably the result of some other error.
887
888 if No (Xtyp) then
889 return N;
890 end if;
891
892 -- Dereference an access type
893
894 if Is_Access_Type (Xtyp) then
895 Xtyp := Designated_Type (Xtyp);
896 end if;
897
898 -- If we don't have an array type at this stage, something is
899 -- peculiar, e.g. another error, and we abandon the attempt at
900 -- a fixup.
901
902 if not Is_Array_Type (Xtyp) then
903 return N;
904 end if;
905
906 -- Ignore unconstrained array, since bounds are not meaningful
907
908 if not Is_Constrained (Xtyp) then
909 return N;
910 end if;
911
912 if Ekind (Xtyp) = E_String_Literal_Subtype then
913 if Attribute_Name (N) = Name_First then
914 return String_Literal_Low_Bound (Xtyp);
915 else
916 return
917 Make_Integer_Literal (Sloc (N),
918 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
919 String_Literal_Length (Xtyp));
920 end if;
921 end if;
922
923 -- Find correct index type
924
925 Indx := First_Index (Xtyp);
926
927 if Present (Expressions (N)) then
928 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
929
930 for J in 2 .. Subs loop
931 Indx := Next_Index (Indx);
932 end loop;
933 end if;
934
935 Xtyp := Etype (Indx);
936
937 if Attribute_Name (N) = Name_First then
938 return Type_Low_Bound (Xtyp);
939 else
940 return Type_High_Bound (Xtyp);
941 end if;
942 end if;
943
944 return N;
945 end Compare_Fixup;
946
947 ----------------------------
948 -- Is_Known_Valid_Operand --
949 ----------------------------
950
951 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
952 begin
953 return (Is_Entity_Name (Opnd)
954 and then
955 (Is_Known_Valid (Entity (Opnd))
956 or else Ekind (Entity (Opnd)) = E_In_Parameter
957 or else
958 (Ekind (Entity (Opnd)) in Object_Kind
959 and then Present (Current_Value (Entity (Opnd))))))
960 or else Is_OK_Static_Expression (Opnd);
961 end Is_Known_Valid_Operand;
962
963 -------------------
964 -- Is_Same_Value --
965 -------------------
966
967 function Is_Same_Value (L, R : Node_Id) return Boolean is
968 Lf : constant Node_Id := Compare_Fixup (L);
969 Rf : constant Node_Id := Compare_Fixup (R);
970
971 function Is_Same_Subscript (L, R : List_Id) return Boolean;
972 -- L, R are the Expressions values from two attribute nodes for First
973 -- or Last attributes. Either may be set to No_List if no expressions
974 -- are present (indicating subscript 1). The result is True if both
975 -- expressions represent the same subscript (note one case is where
976 -- one subscript is missing and the other is explicitly set to 1).
977
978 -----------------------
979 -- Is_Same_Subscript --
980 -----------------------
981
982 function Is_Same_Subscript (L, R : List_Id) return Boolean is
983 begin
984 if L = No_List then
985 if R = No_List then
986 return True;
987 else
988 return Expr_Value (First (R)) = Uint_1;
989 end if;
990
991 else
992 if R = No_List then
993 return Expr_Value (First (L)) = Uint_1;
994 else
995 return Expr_Value (First (L)) = Expr_Value (First (R));
996 end if;
997 end if;
998 end Is_Same_Subscript;
999
1000 -- Start of processing for Is_Same_Value
1001
1002 begin
1003 -- Values are the same if they refer to the same entity and the
1004 -- entity is non-volatile. This does not however apply to Float
1005 -- types, since we may have two NaN values and they should never
1006 -- compare equal.
1007
1008 -- If the entity is a discriminant, the two expressions may be bounds
1009 -- of components of objects of the same discriminated type. The
1010 -- values of the discriminants are not static, and therefore the
1011 -- result is unknown.
1012
1013 -- It would be better to comment individual branches of this test ???
1014
1015 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
1016 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
1017 and then Entity (Lf) = Entity (Rf)
1018 and then Ekind (Entity (Lf)) /= E_Discriminant
1019 and then Present (Entity (Lf))
1020 and then not Is_Floating_Point_Type (Etype (L))
1021 and then not Is_Volatile_Reference (L)
1022 and then not Is_Volatile_Reference (R)
1023 then
1024 return True;
1025
1026 -- Or if they are compile time known and identical
1027
1028 elsif Compile_Time_Known_Value (Lf)
1029 and then
1030 Compile_Time_Known_Value (Rf)
1031 and then Expr_Value (Lf) = Expr_Value (Rf)
1032 then
1033 return True;
1034
1035 -- False if Nkind of the two nodes is different for remaining cases
1036
1037 elsif Nkind (Lf) /= Nkind (Rf) then
1038 return False;
1039
1040 -- True if both 'First or 'Last values applying to the same entity
1041 -- (first and last don't change even if value does). Note that we
1042 -- need this even with the calls to Compare_Fixup, to handle the
1043 -- case of unconstrained array attributes where Compare_Fixup
1044 -- cannot find useful bounds.
1045
1046 elsif Nkind (Lf) = N_Attribute_Reference
1047 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1048 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
1049 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
1050 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
1051 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1052 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1053 then
1054 return True;
1055
1056 -- True if the same selected component from the same record
1057
1058 elsif Nkind (Lf) = N_Selected_Component
1059 and then Selector_Name (Lf) = Selector_Name (Rf)
1060 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1061 then
1062 return True;
1063
1064 -- True if the same unary operator applied to the same operand
1065
1066 elsif Nkind (Lf) in N_Unary_Op
1067 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1068 then
1069 return True;
1070
1071 -- True if the same binary operator applied to the same operands
1072
1073 elsif Nkind (Lf) in N_Binary_Op
1074 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1075 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1076 then
1077 return True;
1078
1079 -- All other cases, we can't tell, so return False
1080
1081 else
1082 return False;
1083 end if;
1084 end Is_Same_Value;
1085
1086 -- Start of processing for Compile_Time_Compare
1087
1088 begin
1089 Diff.all := No_Uint;
1090
1091 -- In preanalysis mode, always return Unknown unless the expression
1092 -- is static. It is too early to be thinking we know the result of a
1093 -- comparison, save that judgment for the full analysis. This is
1094 -- particularly important in the case of pre and postconditions, which
1095 -- otherwise can be prematurely collapsed into having True or False
1096 -- conditions when this is inappropriate.
1097
1098 if not (Full_Analysis
1099 or else (Is_OK_Static_Expression (L)
1100 and then
1101 Is_OK_Static_Expression (R)))
1102 then
1103 return Unknown;
1104 end if;
1105
1106 -- If either operand could raise constraint error, then we cannot
1107 -- know the result at compile time (since CE may be raised).
1108
1109 if not (Cannot_Raise_Constraint_Error (L)
1110 and then
1111 Cannot_Raise_Constraint_Error (R))
1112 then
1113 return Unknown;
1114 end if;
1115
1116 -- Identical operands are most certainly equal
1117
1118 if L = R then
1119 return EQ;
1120 end if;
1121
1122 -- If expressions have no types, then do not attempt to determine if
1123 -- they are the same, since something funny is going on. One case in
1124 -- which this happens is during generic template analysis, when bounds
1125 -- are not fully analyzed.
1126
1127 if No (Ltyp) or else No (Rtyp) then
1128 return Unknown;
1129 end if;
1130
1131 -- These get reset to the base type for the case of entities where
1132 -- Is_Known_Valid is not set. This takes care of handling possible
1133 -- invalid representations using the value of the base type, in
1134 -- accordance with RM 13.9.1(10).
1135
1136 Ltyp := Underlying_Type (Ltyp);
1137 Rtyp := Underlying_Type (Rtyp);
1138
1139 -- Same rationale as above, but for Underlying_Type instead of Etype
1140
1141 if No (Ltyp) or else No (Rtyp) then
1142 return Unknown;
1143 end if;
1144
1145 -- We do not attempt comparisons for packed arrays represented as
1146 -- modular types, where the semantics of comparison is quite different.
1147
1148 if Is_Packed_Array_Impl_Type (Ltyp)
1149 and then Is_Modular_Integer_Type (Ltyp)
1150 then
1151 return Unknown;
1152
1153 -- For access types, the only time we know the result at compile time
1154 -- (apart from identical operands, which we handled already) is if we
1155 -- know one operand is null and the other is not, or both operands are
1156 -- known null.
1157
1158 elsif Is_Access_Type (Ltyp) then
1159 if Known_Null (L) then
1160 if Known_Null (R) then
1161 return EQ;
1162 elsif Known_Non_Null (R) then
1163 return NE;
1164 else
1165 return Unknown;
1166 end if;
1167
1168 elsif Known_Non_Null (L) and then Known_Null (R) then
1169 return NE;
1170
1171 else
1172 return Unknown;
1173 end if;
1174
1175 -- Case where comparison involves two compile time known values
1176
1177 elsif Compile_Time_Known_Value (L)
1178 and then
1179 Compile_Time_Known_Value (R)
1180 then
1181 -- For the floating-point case, we have to be a little careful, since
1182 -- at compile time we are dealing with universal exact values, but at
1183 -- runtime, these will be in non-exact target form. That's why the
1184 -- returned results are LE and GE below instead of LT and GT.
1185
1186 if Is_Floating_Point_Type (Ltyp)
1187 or else
1188 Is_Floating_Point_Type (Rtyp)
1189 then
1190 declare
1191 Lo : constant Ureal := Expr_Value_R (L);
1192 Hi : constant Ureal := Expr_Value_R (R);
1193 begin
1194 if Lo < Hi then
1195 return LE;
1196 elsif Lo = Hi then
1197 return EQ;
1198 else
1199 return GE;
1200 end if;
1201 end;
1202
1203 -- For string types, we have two string literals and we proceed to
1204 -- compare them using the Ada style dictionary string comparison.
1205
1206 elsif not Is_Scalar_Type (Ltyp) then
1207 declare
1208 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1209 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1210 Llen : constant Nat := String_Length (Lstring);
1211 Rlen : constant Nat := String_Length (Rstring);
1212
1213 begin
1214 for J in 1 .. Nat'Min (Llen, Rlen) loop
1215 declare
1216 LC : constant Char_Code := Get_String_Char (Lstring, J);
1217 RC : constant Char_Code := Get_String_Char (Rstring, J);
1218 begin
1219 if LC < RC then
1220 return LT;
1221 elsif LC > RC then
1222 return GT;
1223 end if;
1224 end;
1225 end loop;
1226
1227 if Llen < Rlen then
1228 return LT;
1229 elsif Llen > Rlen then
1230 return GT;
1231 else
1232 return EQ;
1233 end if;
1234 end;
1235
1236 -- For remaining scalar cases we know exactly (note that this does
1237 -- include the fixed-point case, where we know the run time integer
1238 -- values now).
1239
1240 else
1241 declare
1242 Lo : constant Uint := Expr_Value (L);
1243 Hi : constant Uint := Expr_Value (R);
1244 begin
1245 if Lo < Hi then
1246 Diff.all := Hi - Lo;
1247 return LT;
1248 elsif Lo = Hi then
1249 return EQ;
1250 else
1251 Diff.all := Lo - Hi;
1252 return GT;
1253 end if;
1254 end;
1255 end if;
1256
1257 -- Cases where at least one operand is not known at compile time
1258
1259 else
1260 -- Remaining checks apply only for discrete types
1261
1262 if not Is_Discrete_Type (Ltyp)
1263 or else
1264 not Is_Discrete_Type (Rtyp)
1265 then
1266 return Unknown;
1267 end if;
1268
1269 -- Defend against generic types, or actually any expressions that
1270 -- contain a reference to a generic type from within a generic
1271 -- template. We don't want to do any range analysis of such
1272 -- expressions for two reasons. First, the bounds of a generic type
1273 -- itself are junk and cannot be used for any kind of analysis.
1274 -- Second, we may have a case where the range at run time is indeed
1275 -- known, but we don't want to do compile time analysis in the
1276 -- template based on that range since in an instance the value may be
1277 -- static, and able to be elaborated without reference to the bounds
1278 -- of types involved. As an example, consider:
1279
1280 -- (F'Pos (F'Last) + 1) > Integer'Last
1281
1282 -- The expression on the left side of > is Universal_Integer and thus
1283 -- acquires the type Integer for evaluation at run time, and at run
1284 -- time it is true that this condition is always False, but within
1285 -- an instance F may be a type with a static range greater than the
1286 -- range of Integer, and the expression statically evaluates to True.
1287
1288 if References_Generic_Formal_Type (L)
1289 or else
1290 References_Generic_Formal_Type (R)
1291 then
1292 return Unknown;
1293 end if;
1294
1295 -- Replace types by base types for the case of values which are not
1296 -- known to have valid representations. This takes care of properly
1297 -- dealing with invalid representations.
1298
1299 if not Assume_Valid then
1300 if not (Is_Entity_Name (L)
1301 and then (Is_Known_Valid (Entity (L))
1302 or else Assume_No_Invalid_Values))
1303 then
1304 Ltyp := Underlying_Type (Base_Type (Ltyp));
1305 end if;
1306
1307 if not (Is_Entity_Name (R)
1308 and then (Is_Known_Valid (Entity (R))
1309 or else Assume_No_Invalid_Values))
1310 then
1311 Rtyp := Underlying_Type (Base_Type (Rtyp));
1312 end if;
1313 end if;
1314
1315 -- First attempt is to decompose the expressions to extract a
1316 -- constant offset resulting from the use of any of the forms:
1317
1318 -- expr + literal
1319 -- expr - literal
1320 -- typ'Succ (expr)
1321 -- typ'Pred (expr)
1322
1323 -- Then we see if the two expressions are the same value, and if so
1324 -- the result is obtained by comparing the offsets.
1325
1326 -- Note: the reason we do this test first is that it returns only
1327 -- decisive results (with diff set), where other tests, like the
1328 -- range test, may not be as so decisive. Consider for example
1329 -- J .. J + 1. This code can conclude LT with a difference of 1,
1330 -- even if the range of J is not known.
1331
1332 declare
1333 Lnode : Node_Id;
1334 Loffs : Uint;
1335 Rnode : Node_Id;
1336 Roffs : Uint;
1337
1338 begin
1339 Compare_Decompose (L, Lnode, Loffs);
1340 Compare_Decompose (R, Rnode, Roffs);
1341
1342 if Is_Same_Value (Lnode, Rnode) then
1343 if Loffs = Roffs then
1344 return EQ;
1345 end if;
1346
1347 -- When the offsets are not equal, we can go farther only if
1348 -- the types are not modular (e.g. X < X + 1 is False if X is
1349 -- the largest number).
1350
1351 if not Is_Modular_Integer_Type (Ltyp)
1352 and then not Is_Modular_Integer_Type (Rtyp)
1353 then
1354 if Loffs < Roffs then
1355 Diff.all := Roffs - Loffs;
1356 return LT;
1357 else
1358 Diff.all := Loffs - Roffs;
1359 return GT;
1360 end if;
1361 end if;
1362 end if;
1363 end;
1364
1365 -- Next, try range analysis and see if operand ranges are disjoint
1366
1367 declare
1368 LOK, ROK : Boolean;
1369 LLo, LHi : Uint;
1370 RLo, RHi : Uint;
1371
1372 Single : Boolean;
1373 -- True if each range is a single point
1374
1375 begin
1376 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1377 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1378
1379 if LOK and ROK then
1380 Single := (LLo = LHi) and then (RLo = RHi);
1381
1382 if LHi < RLo then
1383 if Single and Assume_Valid then
1384 Diff.all := RLo - LLo;
1385 end if;
1386
1387 return LT;
1388
1389 elsif RHi < LLo then
1390 if Single and Assume_Valid then
1391 Diff.all := LLo - RLo;
1392 end if;
1393
1394 return GT;
1395
1396 elsif Single and then LLo = RLo then
1397
1398 -- If the range includes a single literal and we can assume
1399 -- validity then the result is known even if an operand is
1400 -- not static.
1401
1402 if Assume_Valid then
1403 return EQ;
1404 else
1405 return Unknown;
1406 end if;
1407
1408 elsif LHi = RLo then
1409 return LE;
1410
1411 elsif RHi = LLo then
1412 return GE;
1413
1414 elsif not Is_Known_Valid_Operand (L)
1415 and then not Assume_Valid
1416 then
1417 if Is_Same_Value (L, R) then
1418 return EQ;
1419 else
1420 return Unknown;
1421 end if;
1422 end if;
1423
1424 -- If the range of either operand cannot be determined, nothing
1425 -- further can be inferred.
1426
1427 else
1428 return Unknown;
1429 end if;
1430 end;
1431
1432 -- Here is where we check for comparisons against maximum bounds of
1433 -- types, where we know that no value can be outside the bounds of
1434 -- the subtype. Note that this routine is allowed to assume that all
1435 -- expressions are within their subtype bounds. Callers wishing to
1436 -- deal with possibly invalid values must in any case take special
1437 -- steps (e.g. conversions to larger types) to avoid this kind of
1438 -- optimization, which is always considered to be valid. We do not
1439 -- attempt this optimization with generic types, since the type
1440 -- bounds may not be meaningful in this case.
1441
1442 -- We are in danger of an infinite recursion here. It does not seem
1443 -- useful to go more than one level deep, so the parameter Rec is
1444 -- used to protect ourselves against this infinite recursion.
1445
1446 if not Rec then
1447
1448 -- See if we can get a decisive check against one operand and a
1449 -- bound of the other operand (four possible tests here). Note
1450 -- that we avoid testing junk bounds of a generic type.
1451
1452 if not Is_Generic_Type (Rtyp) then
1453 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1454 Discard'Access,
1455 Assume_Valid, Rec => True)
1456 is
1457 when LT => return LT;
1458 when LE => return LE;
1459 when EQ => return LE;
1460 when others => null;
1461 end case;
1462
1463 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1464 Discard'Access,
1465 Assume_Valid, Rec => True)
1466 is
1467 when GT => return GT;
1468 when GE => return GE;
1469 when EQ => return GE;
1470 when others => null;
1471 end case;
1472 end if;
1473
1474 if not Is_Generic_Type (Ltyp) then
1475 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1476 Discard'Access,
1477 Assume_Valid, Rec => True)
1478 is
1479 when GT => return GT;
1480 when GE => return GE;
1481 when EQ => return GE;
1482 when others => null;
1483 end case;
1484
1485 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1486 Discard'Access,
1487 Assume_Valid, Rec => True)
1488 is
1489 when LT => return LT;
1490 when LE => return LE;
1491 when EQ => return LE;
1492 when others => null;
1493 end case;
1494 end if;
1495 end if;
1496
1497 -- Next attempt is to see if we have an entity compared with a
1498 -- compile time known value, where there is a current value
1499 -- conditional for the entity which can tell us the result.
1500
1501 declare
1502 Var : Node_Id;
1503 -- Entity variable (left operand)
1504
1505 Val : Uint;
1506 -- Value (right operand)
1507
1508 Inv : Boolean;
1509 -- If False, we have reversed the operands
1510
1511 Op : Node_Kind;
1512 -- Comparison operator kind from Get_Current_Value_Condition call
1513
1514 Opn : Node_Id;
1515 -- Value from Get_Current_Value_Condition call
1516
1517 Opv : Uint;
1518 -- Value of Opn
1519
1520 Result : Compare_Result;
1521 -- Known result before inversion
1522
1523 begin
1524 if Is_Entity_Name (L)
1525 and then Compile_Time_Known_Value (R)
1526 then
1527 Var := L;
1528 Val := Expr_Value (R);
1529 Inv := False;
1530
1531 elsif Is_Entity_Name (R)
1532 and then Compile_Time_Known_Value (L)
1533 then
1534 Var := R;
1535 Val := Expr_Value (L);
1536 Inv := True;
1537
1538 -- That was the last chance at finding a compile time result
1539
1540 else
1541 return Unknown;
1542 end if;
1543
1544 Get_Current_Value_Condition (Var, Op, Opn);
1545
1546 -- That was the last chance, so if we got nothing return
1547
1548 if No (Opn) then
1549 return Unknown;
1550 end if;
1551
1552 Opv := Expr_Value (Opn);
1553
1554 -- We got a comparison, so we might have something interesting
1555
1556 -- Convert LE to LT and GE to GT, just so we have fewer cases
1557
1558 if Op = N_Op_Le then
1559 Op := N_Op_Lt;
1560 Opv := Opv + 1;
1561
1562 elsif Op = N_Op_Ge then
1563 Op := N_Op_Gt;
1564 Opv := Opv - 1;
1565 end if;
1566
1567 -- Deal with equality case
1568
1569 if Op = N_Op_Eq then
1570 if Val = Opv then
1571 Result := EQ;
1572 elsif Opv < Val then
1573 Result := LT;
1574 else
1575 Result := GT;
1576 end if;
1577
1578 -- Deal with inequality case
1579
1580 elsif Op = N_Op_Ne then
1581 if Val = Opv then
1582 Result := NE;
1583 else
1584 return Unknown;
1585 end if;
1586
1587 -- Deal with greater than case
1588
1589 elsif Op = N_Op_Gt then
1590 if Opv >= Val then
1591 Result := GT;
1592 elsif Opv = Val - 1 then
1593 Result := GE;
1594 else
1595 return Unknown;
1596 end if;
1597
1598 -- Deal with less than case
1599
1600 else pragma Assert (Op = N_Op_Lt);
1601 if Opv <= Val then
1602 Result := LT;
1603 elsif Opv = Val + 1 then
1604 Result := LE;
1605 else
1606 return Unknown;
1607 end if;
1608 end if;
1609
1610 -- Deal with inverting result
1611
1612 if Inv then
1613 case Result is
1614 when GT => return LT;
1615 when GE => return LE;
1616 when LT => return GT;
1617 when LE => return GE;
1618 when others => return Result;
1619 end case;
1620 end if;
1621
1622 return Result;
1623 end;
1624 end if;
1625 end Compile_Time_Compare;
1626
1627 -------------------------------
1628 -- Compile_Time_Known_Bounds --
1629 -------------------------------
1630
1631 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1632 Indx : Node_Id;
1633 Typ : Entity_Id;
1634
1635 begin
1636 if T = Any_Composite or else not Is_Array_Type (T) then
1637 return False;
1638 end if;
1639
1640 Indx := First_Index (T);
1641 while Present (Indx) loop
1642 Typ := Underlying_Type (Etype (Indx));
1643
1644 -- Never look at junk bounds of a generic type
1645
1646 if Is_Generic_Type (Typ) then
1647 return False;
1648 end if;
1649
1650 -- Otherwise check bounds for compile time known
1651
1652 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1653 return False;
1654 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1655 return False;
1656 else
1657 Next_Index (Indx);
1658 end if;
1659 end loop;
1660
1661 return True;
1662 end Compile_Time_Known_Bounds;
1663
1664 ------------------------------
1665 -- Compile_Time_Known_Value --
1666 ------------------------------
1667
1668 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1669 K : constant Node_Kind := Nkind (Op);
1670 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1671
1672 begin
1673 -- Never known at compile time if bad type or raises constraint error
1674 -- or empty (latter case occurs only as a result of a previous error).
1675
1676 if No (Op) then
1677 Check_Error_Detected;
1678 return False;
1679
1680 elsif Op = Error
1681 or else Etype (Op) = Any_Type
1682 or else Raises_Constraint_Error (Op)
1683 then
1684 return False;
1685 end if;
1686
1687 -- If we have an entity name, then see if it is the name of a constant
1688 -- and if so, test the corresponding constant value, or the name of
1689 -- an enumeration literal, which is always a constant.
1690
1691 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1692 declare
1693 E : constant Entity_Id := Entity (Op);
1694 V : Node_Id;
1695
1696 begin
1697 -- Never known at compile time if it is a packed array value.
1698 -- We might want to try to evaluate these at compile time one
1699 -- day, but we do not make that attempt now.
1700
1701 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1702 return False;
1703 end if;
1704
1705 if Ekind (E) = E_Enumeration_Literal then
1706 return True;
1707
1708 elsif Ekind (E) = E_Constant then
1709 V := Constant_Value (E);
1710 return Present (V) and then Compile_Time_Known_Value (V);
1711 end if;
1712 end;
1713
1714 -- We have a value, see if it is compile time known
1715
1716 else
1717 -- Integer literals are worth storing in the cache
1718
1719 if K = N_Integer_Literal then
1720 CV_Ent.N := Op;
1721 CV_Ent.V := Intval (Op);
1722 return True;
1723
1724 -- Other literals and NULL are known at compile time
1725
1726 elsif
1727 Nkind_In (K, N_Character_Literal,
1728 N_Real_Literal,
1729 N_String_Literal,
1730 N_Null)
1731 then
1732 return True;
1733 end if;
1734 end if;
1735
1736 -- If we fall through, not known at compile time
1737
1738 return False;
1739
1740 -- If we get an exception while trying to do this test, then some error
1741 -- has occurred, and we simply say that the value is not known after all
1742
1743 exception
1744 when others =>
1745 return False;
1746 end Compile_Time_Known_Value;
1747
1748 --------------------------------------
1749 -- Compile_Time_Known_Value_Or_Aggr --
1750 --------------------------------------
1751
1752 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1753 begin
1754 -- If we have an entity name, then see if it is the name of a constant
1755 -- and if so, test the corresponding constant value, or the name of
1756 -- an enumeration literal, which is always a constant.
1757
1758 if Is_Entity_Name (Op) then
1759 declare
1760 E : constant Entity_Id := Entity (Op);
1761 V : Node_Id;
1762
1763 begin
1764 if Ekind (E) = E_Enumeration_Literal then
1765 return True;
1766
1767 elsif Ekind (E) /= E_Constant then
1768 return False;
1769
1770 else
1771 V := Constant_Value (E);
1772 return Present (V)
1773 and then Compile_Time_Known_Value_Or_Aggr (V);
1774 end if;
1775 end;
1776
1777 -- We have a value, see if it is compile time known
1778
1779 else
1780 if Compile_Time_Known_Value (Op) then
1781 return True;
1782
1783 elsif Nkind (Op) = N_Aggregate then
1784
1785 if Present (Expressions (Op)) then
1786 declare
1787 Expr : Node_Id;
1788 begin
1789 Expr := First (Expressions (Op));
1790 while Present (Expr) loop
1791 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1792 return False;
1793 else
1794 Next (Expr);
1795 end if;
1796 end loop;
1797 end;
1798 end if;
1799
1800 if Present (Component_Associations (Op)) then
1801 declare
1802 Cass : Node_Id;
1803
1804 begin
1805 Cass := First (Component_Associations (Op));
1806 while Present (Cass) loop
1807 if not
1808 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1809 then
1810 return False;
1811 end if;
1812
1813 Next (Cass);
1814 end loop;
1815 end;
1816 end if;
1817
1818 return True;
1819
1820 -- All other types of values are not known at compile time
1821
1822 else
1823 return False;
1824 end if;
1825
1826 end if;
1827 end Compile_Time_Known_Value_Or_Aggr;
1828
1829 ---------------------------------------
1830 -- CRT_Safe_Compile_Time_Known_Value --
1831 ---------------------------------------
1832
1833 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1834 begin
1835 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1836 and then not Is_OK_Static_Expression (Op)
1837 then
1838 return False;
1839 else
1840 return Compile_Time_Known_Value (Op);
1841 end if;
1842 end CRT_Safe_Compile_Time_Known_Value;
1843
1844 -----------------
1845 -- Eval_Actual --
1846 -----------------
1847
1848 -- This is only called for actuals of functions that are not predefined
1849 -- operators (which have already been rewritten as operators at this
1850 -- stage), so the call can never be folded, and all that needs doing for
1851 -- the actual is to do the check for a non-static context.
1852
1853 procedure Eval_Actual (N : Node_Id) is
1854 begin
1855 Check_Non_Static_Context (N);
1856 end Eval_Actual;
1857
1858 --------------------
1859 -- Eval_Allocator --
1860 --------------------
1861
1862 -- Allocators are never static, so all we have to do is to do the
1863 -- check for a non-static context if an expression is present.
1864
1865 procedure Eval_Allocator (N : Node_Id) is
1866 Expr : constant Node_Id := Expression (N);
1867 begin
1868 if Nkind (Expr) = N_Qualified_Expression then
1869 Check_Non_Static_Context (Expression (Expr));
1870 end if;
1871 end Eval_Allocator;
1872
1873 ------------------------
1874 -- Eval_Arithmetic_Op --
1875 ------------------------
1876
1877 -- Arithmetic operations are static functions, so the result is static
1878 -- if both operands are static (RM 4.9(7), 4.9(20)).
1879
1880 procedure Eval_Arithmetic_Op (N : Node_Id) is
1881 Left : constant Node_Id := Left_Opnd (N);
1882 Right : constant Node_Id := Right_Opnd (N);
1883 Ltype : constant Entity_Id := Etype (Left);
1884 Rtype : constant Entity_Id := Etype (Right);
1885 Otype : Entity_Id := Empty;
1886 Stat : Boolean;
1887 Fold : Boolean;
1888
1889 begin
1890 -- If not foldable we are done
1891
1892 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1893
1894 if not Fold then
1895 return;
1896 end if;
1897
1898 -- Otherwise attempt to fold
1899
1900 if Is_Universal_Numeric_Type (Etype (Left))
1901 and then
1902 Is_Universal_Numeric_Type (Etype (Right))
1903 then
1904 Otype := Find_Universal_Operator_Type (N);
1905 end if;
1906
1907 -- Fold for cases where both operands are of integer type
1908
1909 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1910 declare
1911 Left_Int : constant Uint := Expr_Value (Left);
1912 Right_Int : constant Uint := Expr_Value (Right);
1913 Result : Uint;
1914
1915 begin
1916 case Nkind (N) is
1917 when N_Op_Add =>
1918 Result := Left_Int + Right_Int;
1919
1920 when N_Op_Subtract =>
1921 Result := Left_Int - Right_Int;
1922
1923 when N_Op_Multiply =>
1924 if OK_Bits
1925 (N, UI_From_Int
1926 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1927 then
1928 Result := Left_Int * Right_Int;
1929 else
1930 Result := Left_Int;
1931 end if;
1932
1933 when N_Op_Divide =>
1934
1935 -- The exception Constraint_Error is raised by integer
1936 -- division, rem and mod if the right operand is zero.
1937
1938 if Right_Int = 0 then
1939
1940 -- When SPARK_Mode is On, force a warning instead of
1941 -- an error in that case, as this likely corresponds
1942 -- to deactivated code.
1943
1944 Apply_Compile_Time_Constraint_Error
1945 (N, "division by zero", CE_Divide_By_Zero,
1946 Warn => not Stat or SPARK_Mode = On);
1947 Set_Raises_Constraint_Error (N);
1948 return;
1949
1950 -- Otherwise we can do the division
1951
1952 else
1953 Result := Left_Int / Right_Int;
1954 end if;
1955
1956 when N_Op_Mod =>
1957
1958 -- The exception Constraint_Error is raised by integer
1959 -- division, rem and mod if the right operand is zero.
1960
1961 if Right_Int = 0 then
1962
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.
1966
1967 Apply_Compile_Time_Constraint_Error
1968 (N, "mod with zero divisor", CE_Divide_By_Zero,
1969 Warn => not Stat or SPARK_Mode = On);
1970 return;
1971
1972 else
1973 Result := Left_Int mod Right_Int;
1974 end if;
1975
1976 when N_Op_Rem =>
1977
1978 -- The exception Constraint_Error is raised by integer
1979 -- division, rem and mod if the right operand is zero.
1980
1981 if Right_Int = 0 then
1982
1983 -- When SPARK_Mode is On, force a warning instead of
1984 -- an error in that case, as this likely corresponds
1985 -- to deactivated code.
1986
1987 Apply_Compile_Time_Constraint_Error
1988 (N, "rem with zero divisor", CE_Divide_By_Zero,
1989 Warn => not Stat or SPARK_Mode = On);
1990 return;
1991
1992 else
1993 Result := Left_Int rem Right_Int;
1994 end if;
1995
1996 when others =>
1997 raise Program_Error;
1998 end case;
1999
2000 -- Adjust the result by the modulus if the type is a modular type
2001
2002 if Is_Modular_Integer_Type (Ltype) then
2003 Result := Result mod Modulus (Ltype);
2004
2005 -- For a signed integer type, check non-static overflow
2006
2007 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
2008 declare
2009 BT : constant Entity_Id := Base_Type (Ltype);
2010 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
2011 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
2012 begin
2013 if Result < Lo or else Result > Hi then
2014 Apply_Compile_Time_Constraint_Error
2015 (N, "value not in range of }??",
2016 CE_Overflow_Check_Failed,
2017 Ent => BT);
2018 return;
2019 end if;
2020 end;
2021 end if;
2022
2023 -- If we get here we can fold the result
2024
2025 Fold_Uint (N, Result, Stat);
2026 end;
2027
2028 -- Cases where at least one operand is a real. We handle the cases of
2029 -- both reals, or mixed/real integer cases (the latter happen only for
2030 -- divide and multiply, and the result is always real).
2031
2032 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2033 declare
2034 Left_Real : Ureal;
2035 Right_Real : Ureal;
2036 Result : Ureal;
2037
2038 begin
2039 if Is_Real_Type (Ltype) then
2040 Left_Real := Expr_Value_R (Left);
2041 else
2042 Left_Real := UR_From_Uint (Expr_Value (Left));
2043 end if;
2044
2045 if Is_Real_Type (Rtype) then
2046 Right_Real := Expr_Value_R (Right);
2047 else
2048 Right_Real := UR_From_Uint (Expr_Value (Right));
2049 end if;
2050
2051 if Nkind (N) = N_Op_Add then
2052 Result := Left_Real + Right_Real;
2053
2054 elsif Nkind (N) = N_Op_Subtract then
2055 Result := Left_Real - Right_Real;
2056
2057 elsif Nkind (N) = N_Op_Multiply then
2058 Result := Left_Real * Right_Real;
2059
2060 else pragma Assert (Nkind (N) = N_Op_Divide);
2061 if UR_Is_Zero (Right_Real) then
2062 Apply_Compile_Time_Constraint_Error
2063 (N, "division by zero", CE_Divide_By_Zero);
2064 return;
2065 end if;
2066
2067 Result := Left_Real / Right_Real;
2068 end if;
2069
2070 Fold_Ureal (N, Result, Stat);
2071 end;
2072 end if;
2073
2074 -- If the operator was resolved to a specific type, make sure that type
2075 -- is frozen even if the expression is folded into a literal (which has
2076 -- a universal type).
2077
2078 if Present (Otype) then
2079 Freeze_Before (N, Otype);
2080 end if;
2081 end Eval_Arithmetic_Op;
2082
2083 ----------------------------
2084 -- Eval_Character_Literal --
2085 ----------------------------
2086
2087 -- Nothing to be done
2088
2089 procedure Eval_Character_Literal (N : Node_Id) is
2090 pragma Warnings (Off, N);
2091 begin
2092 null;
2093 end Eval_Character_Literal;
2094
2095 ---------------
2096 -- Eval_Call --
2097 ---------------
2098
2099 -- Static function calls are either calls to predefined operators
2100 -- with static arguments, or calls to functions that rename a literal.
2101 -- Only the latter case is handled here, predefined operators are
2102 -- constant-folded elsewhere.
2103
2104 -- If the function is itself inherited (see 7423-001) the literal of
2105 -- the parent type must be explicitly converted to the return type
2106 -- of the function.
2107
2108 procedure Eval_Call (N : Node_Id) is
2109 Loc : constant Source_Ptr := Sloc (N);
2110 Typ : constant Entity_Id := Etype (N);
2111 Lit : Entity_Id;
2112
2113 begin
2114 if Nkind (N) = N_Function_Call
2115 and then No (Parameter_Associations (N))
2116 and then Is_Entity_Name (Name (N))
2117 and then Present (Alias (Entity (Name (N))))
2118 and then Is_Enumeration_Type (Base_Type (Typ))
2119 then
2120 Lit := Ultimate_Alias (Entity (Name (N)));
2121
2122 if Ekind (Lit) = E_Enumeration_Literal then
2123 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2124 Rewrite
2125 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2126 else
2127 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2128 end if;
2129
2130 Resolve (N, Typ);
2131 end if;
2132 end if;
2133 end Eval_Call;
2134
2135 --------------------------
2136 -- Eval_Case_Expression --
2137 --------------------------
2138
2139 -- A conditional expression is static if all its conditions and dependent
2140 -- expressions are static. Note that we do not care if the dependent
2141 -- expressions raise CE, except for the one that will be selected.
2142
2143 procedure Eval_Case_Expression (N : Node_Id) is
2144 Alt : Node_Id;
2145 Choice : Node_Id;
2146
2147 begin
2148 Set_Is_Static_Expression (N, False);
2149
2150 if not Is_Static_Expression (Expression (N)) then
2151 Check_Non_Static_Context (Expression (N));
2152 return;
2153 end if;
2154
2155 -- First loop, make sure all the alternatives are static expressions
2156 -- none of which raise Constraint_Error. We make the constraint error
2157 -- check because part of the legality condition for a correct static
2158 -- case expression is that the cases are covered, like any other case
2159 -- expression. And we can't do that if any of the conditions raise an
2160 -- exception, so we don't even try to evaluate if that is the case.
2161
2162 Alt := First (Alternatives (N));
2163 while Present (Alt) loop
2164
2165 -- The expression must be static, but we don't care at this stage
2166 -- if it raises Constraint_Error (the alternative might not match,
2167 -- in which case the expression is statically unevaluated anyway).
2168
2169 if not Is_Static_Expression (Expression (Alt)) then
2170 Check_Non_Static_Context (Expression (Alt));
2171 return;
2172 end if;
2173
2174 -- The choices of a case always have to be static, and cannot raise
2175 -- an exception. If this condition is not met, then the expression
2176 -- is plain illegal, so just abandon evaluation attempts. No need
2177 -- to check non-static context when we have something illegal anyway.
2178
2179 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2180 return;
2181 end if;
2182
2183 Next (Alt);
2184 end loop;
2185
2186 -- OK, if the above loop gets through it means that all choices are OK
2187 -- static (don't raise exceptions), so the whole case is static, and we
2188 -- can find the matching alternative.
2189
2190 Set_Is_Static_Expression (N);
2191
2192 -- Now to deal with propagating a possible constraint error
2193
2194 -- If the selecting expression raises CE, propagate and we are done
2195
2196 if Raises_Constraint_Error (Expression (N)) then
2197 Set_Raises_Constraint_Error (N);
2198
2199 -- Otherwise we need to check the alternatives to find the matching
2200 -- one. CE's in other than the matching one are not relevant. But we
2201 -- do need to check the matching one. Unlike the first loop, we do not
2202 -- have to go all the way through, when we find the matching one, quit.
2203
2204 else
2205 Alt := First (Alternatives (N));
2206 Search : loop
2207
2208 -- We must find a match among the alternatives. If not, this must
2209 -- be due to other errors, so just ignore, leaving as non-static.
2210
2211 if No (Alt) then
2212 Set_Is_Static_Expression (N, False);
2213 return;
2214 end if;
2215
2216 -- Otherwise loop through choices of this alternative
2217
2218 Choice := First (Discrete_Choices (Alt));
2219 while Present (Choice) loop
2220
2221 -- If we find a matching choice, then the Expression of this
2222 -- alternative replaces N (Raises_Constraint_Error flag is
2223 -- included, so we don't have to special case that).
2224
2225 if Choice_Matches (Expression (N), Choice) = Match then
2226 Rewrite (N, Relocate_Node (Expression (Alt)));
2227 return;
2228 end if;
2229
2230 Next (Choice);
2231 end loop;
2232
2233 Next (Alt);
2234 end loop Search;
2235 end if;
2236 end Eval_Case_Expression;
2237
2238 ------------------------
2239 -- Eval_Concatenation --
2240 ------------------------
2241
2242 -- Concatenation is a static function, so the result is static if both
2243 -- operands are static (RM 4.9(7), 4.9(21)).
2244
2245 procedure Eval_Concatenation (N : Node_Id) is
2246 Left : constant Node_Id := Left_Opnd (N);
2247 Right : constant Node_Id := Right_Opnd (N);
2248 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2249 Stat : Boolean;
2250 Fold : Boolean;
2251
2252 begin
2253 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2254 -- non-static context.
2255
2256 if Ada_Version = Ada_83
2257 and then Comes_From_Source (N)
2258 then
2259 Check_Non_Static_Context (Left);
2260 Check_Non_Static_Context (Right);
2261 return;
2262 end if;
2263
2264 -- If not foldable we are done. In principle concatenation that yields
2265 -- any string type is static (i.e. an array type of character types).
2266 -- However, character types can include enumeration literals, and
2267 -- concatenation in that case cannot be described by a literal, so we
2268 -- only consider the operation static if the result is an array of
2269 -- (a descendant of) a predefined character type.
2270
2271 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2272
2273 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2274 Set_Is_Static_Expression (N, False);
2275 return;
2276 end if;
2277
2278 -- Compile time string concatenation
2279
2280 -- ??? Note that operands that are aggregates can be marked as static,
2281 -- so we should attempt at a later stage to fold concatenations with
2282 -- such aggregates.
2283
2284 declare
2285 Left_Str : constant Node_Id := Get_String_Val (Left);
2286 Left_Len : Nat;
2287 Right_Str : constant Node_Id := Get_String_Val (Right);
2288 Folded_Val : String_Id;
2289
2290 begin
2291 -- Establish new string literal, and store left operand. We make
2292 -- sure to use the special Start_String that takes an operand if
2293 -- the left operand is a string literal. Since this is optimized
2294 -- in the case where that is the most recently created string
2295 -- literal, we ensure efficient time/space behavior for the
2296 -- case of a concatenation of a series of string literals.
2297
2298 if Nkind (Left_Str) = N_String_Literal then
2299 Left_Len := String_Length (Strval (Left_Str));
2300
2301 -- If the left operand is the empty string, and the right operand
2302 -- is a string literal (the case of "" & "..."), the result is the
2303 -- value of the right operand. This optimization is important when
2304 -- Is_Folded_In_Parser, to avoid copying an enormous right
2305 -- operand.
2306
2307 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2308 Folded_Val := Strval (Right_Str);
2309 else
2310 Start_String (Strval (Left_Str));
2311 end if;
2312
2313 else
2314 Start_String;
2315 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2316 Left_Len := 1;
2317 end if;
2318
2319 -- Now append the characters of the right operand, unless we
2320 -- optimized the "" & "..." case above.
2321
2322 if Nkind (Right_Str) = N_String_Literal then
2323 if Left_Len /= 0 then
2324 Store_String_Chars (Strval (Right_Str));
2325 Folded_Val := End_String;
2326 end if;
2327 else
2328 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2329 Folded_Val := End_String;
2330 end if;
2331
2332 Set_Is_Static_Expression (N, Stat);
2333
2334 -- If left operand is the empty string, the result is the
2335 -- right operand, including its bounds if anomalous.
2336
2337 if Left_Len = 0
2338 and then Is_Array_Type (Etype (Right))
2339 and then Etype (Right) /= Any_String
2340 then
2341 Set_Etype (N, Etype (Right));
2342 end if;
2343
2344 Fold_Str (N, Folded_Val, Static => Stat);
2345 end;
2346 end Eval_Concatenation;
2347
2348 ----------------------
2349 -- Eval_Entity_Name --
2350 ----------------------
2351
2352 -- This procedure is used for identifiers and expanded names other than
2353 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2354 -- static if they denote a static constant (RM 4.9(6)) or if the name
2355 -- denotes an enumeration literal (RM 4.9(22)).
2356
2357 procedure Eval_Entity_Name (N : Node_Id) is
2358 Def_Id : constant Entity_Id := Entity (N);
2359 Val : Node_Id;
2360
2361 begin
2362 -- Enumeration literals are always considered to be constants
2363 -- and cannot raise constraint error (RM 4.9(22)).
2364
2365 if Ekind (Def_Id) = E_Enumeration_Literal then
2366 Set_Is_Static_Expression (N);
2367 return;
2368
2369 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2370 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2371 -- it does not violate 10.2.1(8) here, since this is not a variable.
2372
2373 elsif Ekind (Def_Id) = E_Constant then
2374
2375 -- Deferred constants must always be treated as nonstatic outside the
2376 -- scope of their full view.
2377
2378 if Present (Full_View (Def_Id))
2379 and then not In_Open_Scopes (Scope (Def_Id))
2380 then
2381 Val := Empty;
2382 else
2383 Val := Constant_Value (Def_Id);
2384 end if;
2385
2386 if Present (Val) then
2387 Set_Is_Static_Expression
2388 (N, Is_Static_Expression (Val)
2389 and then Is_Static_Subtype (Etype (Def_Id)));
2390 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2391
2392 if not Is_Static_Expression (N)
2393 and then not Is_Generic_Type (Etype (N))
2394 then
2395 Validate_Static_Object_Name (N);
2396 end if;
2397
2398 -- Mark constant condition in SCOs
2399
2400 if Generate_SCO
2401 and then Comes_From_Source (N)
2402 and then Is_Boolean_Type (Etype (Def_Id))
2403 and then Compile_Time_Known_Value (N)
2404 then
2405 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2406 end if;
2407
2408 return;
2409 end if;
2410 end if;
2411
2412 -- Fall through if the name is not static
2413
2414 Validate_Static_Object_Name (N);
2415 end Eval_Entity_Name;
2416
2417 ------------------------
2418 -- Eval_If_Expression --
2419 ------------------------
2420
2421 -- We can fold to a static expression if the condition and both dependent
2422 -- expressions are static. Otherwise, the only required processing is to do
2423 -- the check for non-static context for the then and else expressions.
2424
2425 procedure Eval_If_Expression (N : Node_Id) is
2426 Condition : constant Node_Id := First (Expressions (N));
2427 Then_Expr : constant Node_Id := Next (Condition);
2428 Else_Expr : constant Node_Id := Next (Then_Expr);
2429 Result : Node_Id;
2430 Non_Result : Node_Id;
2431
2432 Rstat : constant Boolean :=
2433 Is_Static_Expression (Condition)
2434 and then
2435 Is_Static_Expression (Then_Expr)
2436 and then
2437 Is_Static_Expression (Else_Expr);
2438 -- True if result is static
2439
2440 begin
2441 -- If result not static, nothing to do, otherwise set static result
2442
2443 if not Rstat then
2444 return;
2445 else
2446 Set_Is_Static_Expression (N);
2447 end if;
2448
2449 -- If any operand is Any_Type, just propagate to result and do not try
2450 -- to fold, this prevents cascaded errors.
2451
2452 if Etype (Condition) = Any_Type or else
2453 Etype (Then_Expr) = Any_Type or else
2454 Etype (Else_Expr) = Any_Type
2455 then
2456 Set_Etype (N, Any_Type);
2457 Set_Is_Static_Expression (N, False);
2458 return;
2459 end if;
2460
2461 -- If condition raises constraint error then we have already signaled
2462 -- an error, and we just propagate to the result and do not fold.
2463
2464 if Raises_Constraint_Error (Condition) then
2465 Set_Raises_Constraint_Error (N);
2466 return;
2467 end if;
2468
2469 -- Static case where we can fold. Note that we don't try to fold cases
2470 -- where the condition is known at compile time, but the result is
2471 -- non-static. This avoids possible cases of infinite recursion where
2472 -- the expander puts in a redundant test and we remove it. Instead we
2473 -- deal with these cases in the expander.
2474
2475 -- Select result operand
2476
2477 if Is_True (Expr_Value (Condition)) then
2478 Result := Then_Expr;
2479 Non_Result := Else_Expr;
2480 else
2481 Result := Else_Expr;
2482 Non_Result := Then_Expr;
2483 end if;
2484
2485 -- Note that it does not matter if the non-result operand raises a
2486 -- Constraint_Error, but if the result raises constraint error then we
2487 -- replace the node with a raise constraint error. This will properly
2488 -- propagate Raises_Constraint_Error since this flag is set in Result.
2489
2490 if Raises_Constraint_Error (Result) then
2491 Rewrite_In_Raise_CE (N, Result);
2492 Check_Non_Static_Context (Non_Result);
2493
2494 -- Otherwise the result operand replaces the original node
2495
2496 else
2497 Rewrite (N, Relocate_Node (Result));
2498 Set_Is_Static_Expression (N);
2499 end if;
2500 end Eval_If_Expression;
2501
2502 ----------------------------
2503 -- Eval_Indexed_Component --
2504 ----------------------------
2505
2506 -- Indexed components are never static, so we need to perform the check
2507 -- for non-static context on the index values. Then, we check if the
2508 -- value can be obtained at compile time, even though it is non-static.
2509
2510 procedure Eval_Indexed_Component (N : Node_Id) is
2511 Expr : Node_Id;
2512
2513 begin
2514 -- Check for non-static context on index values
2515
2516 Expr := First (Expressions (N));
2517 while Present (Expr) loop
2518 Check_Non_Static_Context (Expr);
2519 Next (Expr);
2520 end loop;
2521
2522 -- If the indexed component appears in an object renaming declaration
2523 -- then we do not want to try to evaluate it, since in this case we
2524 -- need the identity of the array element.
2525
2526 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2527 return;
2528
2529 -- Similarly if the indexed component appears as the prefix of an
2530 -- attribute we don't want to evaluate it, because at least for
2531 -- some cases of attributes we need the identify (e.g. Access, Size)
2532
2533 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2534 return;
2535 end if;
2536
2537 -- Note: there are other cases, such as the left side of an assignment,
2538 -- or an OUT parameter for a call, where the replacement results in the
2539 -- illegal use of a constant, But these cases are illegal in the first
2540 -- place, so the replacement, though silly, is harmless.
2541
2542 -- Now see if this is a constant array reference
2543
2544 if List_Length (Expressions (N)) = 1
2545 and then Is_Entity_Name (Prefix (N))
2546 and then Ekind (Entity (Prefix (N))) = E_Constant
2547 and then Present (Constant_Value (Entity (Prefix (N))))
2548 then
2549 declare
2550 Loc : constant Source_Ptr := Sloc (N);
2551 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2552 Sub : constant Node_Id := First (Expressions (N));
2553
2554 Atyp : Entity_Id;
2555 -- Type of array
2556
2557 Lin : Nat;
2558 -- Linear one's origin subscript value for array reference
2559
2560 Lbd : Node_Id;
2561 -- Lower bound of the first array index
2562
2563 Elm : Node_Id;
2564 -- Value from constant array
2565
2566 begin
2567 Atyp := Etype (Arr);
2568
2569 if Is_Access_Type (Atyp) then
2570 Atyp := Designated_Type (Atyp);
2571 end if;
2572
2573 -- If we have an array type (we should have but perhaps there are
2574 -- error cases where this is not the case), then see if we can do
2575 -- a constant evaluation of the array reference.
2576
2577 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2578 if Ekind (Atyp) = E_String_Literal_Subtype then
2579 Lbd := String_Literal_Low_Bound (Atyp);
2580 else
2581 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2582 end if;
2583
2584 if Compile_Time_Known_Value (Sub)
2585 and then Nkind (Arr) = N_Aggregate
2586 and then Compile_Time_Known_Value (Lbd)
2587 and then Is_Discrete_Type (Component_Type (Atyp))
2588 then
2589 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2590
2591 if List_Length (Expressions (Arr)) >= Lin then
2592 Elm := Pick (Expressions (Arr), Lin);
2593
2594 -- If the resulting expression is compile time known,
2595 -- then we can rewrite the indexed component with this
2596 -- value, being sure to mark the result as non-static.
2597 -- We also reset the Sloc, in case this generates an
2598 -- error later on (e.g. 136'Access).
2599
2600 if Compile_Time_Known_Value (Elm) then
2601 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2602 Set_Is_Static_Expression (N, False);
2603 Set_Sloc (N, Loc);
2604 end if;
2605 end if;
2606
2607 -- We can also constant-fold if the prefix is a string literal.
2608 -- This will be useful in an instantiation or an inlining.
2609
2610 elsif Compile_Time_Known_Value (Sub)
2611 and then Nkind (Arr) = N_String_Literal
2612 and then Compile_Time_Known_Value (Lbd)
2613 and then Expr_Value (Lbd) = 1
2614 and then Expr_Value (Sub) <=
2615 String_Literal_Length (Etype (Arr))
2616 then
2617 declare
2618 C : constant Char_Code :=
2619 Get_String_Char (Strval (Arr),
2620 UI_To_Int (Expr_Value (Sub)));
2621 begin
2622 Set_Character_Literal_Name (C);
2623
2624 Elm :=
2625 Make_Character_Literal (Loc,
2626 Chars => Name_Find,
2627 Char_Literal_Value => UI_From_CC (C));
2628 Set_Etype (Elm, Component_Type (Atyp));
2629 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2630 Set_Is_Static_Expression (N, False);
2631 end;
2632 end if;
2633 end if;
2634 end;
2635 end if;
2636 end Eval_Indexed_Component;
2637
2638 --------------------------
2639 -- Eval_Integer_Literal --
2640 --------------------------
2641
2642 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2643 -- as static by the analyzer. The reason we did it that early is to allow
2644 -- the possibility of turning off the Is_Static_Expression flag after
2645 -- analysis, but before resolution, when integer literals are generated in
2646 -- the expander that do not correspond to static expressions.
2647
2648 procedure Eval_Integer_Literal (N : Node_Id) is
2649 T : constant Entity_Id := Etype (N);
2650
2651 function In_Any_Integer_Context return Boolean;
2652 -- If the literal is resolved with a specific type in a context where
2653 -- the expected type is Any_Integer, there are no range checks on the
2654 -- literal. By the time the literal is evaluated, it carries the type
2655 -- imposed by the enclosing expression, and we must recover the context
2656 -- to determine that Any_Integer is meant.
2657
2658 ----------------------------
2659 -- In_Any_Integer_Context --
2660 ----------------------------
2661
2662 function In_Any_Integer_Context return Boolean is
2663 Par : constant Node_Id := Parent (N);
2664 K : constant Node_Kind := Nkind (Par);
2665
2666 begin
2667 -- Any_Integer also appears in digits specifications for real types,
2668 -- but those have bounds smaller that those of any integer base type,
2669 -- so we can safely ignore these cases.
2670
2671 return Nkind_In (K, N_Number_Declaration,
2672 N_Attribute_Reference,
2673 N_Attribute_Definition_Clause,
2674 N_Modular_Type_Definition,
2675 N_Signed_Integer_Type_Definition);
2676 end In_Any_Integer_Context;
2677
2678 -- Start of processing for Eval_Integer_Literal
2679
2680 begin
2681
2682 -- If the literal appears in a non-expression context, then it is
2683 -- certainly appearing in a non-static context, so check it. This is
2684 -- actually a redundant check, since Check_Non_Static_Context would
2685 -- check it, but it seems worthwhile to optimize out the call.
2686
2687 -- An exception is made for a literal in an if or case expression
2688
2689 if (Nkind_In (Parent (N), N_If_Expression, N_Case_Expression_Alternative)
2690 or else Nkind (Parent (N)) not in N_Subexpr)
2691 and then not In_Any_Integer_Context
2692 then
2693 Check_Non_Static_Context (N);
2694 end if;
2695
2696 -- Modular integer literals must be in their base range
2697
2698 if Is_Modular_Integer_Type (T)
2699 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
2700 then
2701 Out_Of_Range (N);
2702 end if;
2703 end Eval_Integer_Literal;
2704
2705 ---------------------
2706 -- Eval_Logical_Op --
2707 ---------------------
2708
2709 -- Logical operations are static functions, so the result is potentially
2710 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2711
2712 procedure Eval_Logical_Op (N : Node_Id) is
2713 Left : constant Node_Id := Left_Opnd (N);
2714 Right : constant Node_Id := Right_Opnd (N);
2715 Stat : Boolean;
2716 Fold : Boolean;
2717
2718 begin
2719 -- If not foldable we are done
2720
2721 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2722
2723 if not Fold then
2724 return;
2725 end if;
2726
2727 -- Compile time evaluation of logical operation
2728
2729 declare
2730 Left_Int : constant Uint := Expr_Value (Left);
2731 Right_Int : constant Uint := Expr_Value (Right);
2732
2733 begin
2734 if Is_Modular_Integer_Type (Etype (N)) then
2735 declare
2736 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2737 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2738
2739 begin
2740 To_Bits (Left_Int, Left_Bits);
2741 To_Bits (Right_Int, Right_Bits);
2742
2743 -- Note: should really be able to use array ops instead of
2744 -- these loops, but they weren't working at the time ???
2745
2746 if Nkind (N) = N_Op_And then
2747 for J in Left_Bits'Range loop
2748 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2749 end loop;
2750
2751 elsif Nkind (N) = N_Op_Or then
2752 for J in Left_Bits'Range loop
2753 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2754 end loop;
2755
2756 else
2757 pragma Assert (Nkind (N) = N_Op_Xor);
2758
2759 for J in Left_Bits'Range loop
2760 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2761 end loop;
2762 end if;
2763
2764 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2765 end;
2766
2767 else
2768 pragma Assert (Is_Boolean_Type (Etype (N)));
2769
2770 if Nkind (N) = N_Op_And then
2771 Fold_Uint (N,
2772 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2773
2774 elsif Nkind (N) = N_Op_Or then
2775 Fold_Uint (N,
2776 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2777
2778 else
2779 pragma Assert (Nkind (N) = N_Op_Xor);
2780 Fold_Uint (N,
2781 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2782 end if;
2783 end if;
2784 end;
2785 end Eval_Logical_Op;
2786
2787 ------------------------
2788 -- Eval_Membership_Op --
2789 ------------------------
2790
2791 -- A membership test is potentially static if the expression is static, and
2792 -- the range is a potentially static range, or is a subtype mark denoting a
2793 -- static subtype (RM 4.9(12)).
2794
2795 procedure Eval_Membership_Op (N : Node_Id) is
2796 Alts : constant List_Id := Alternatives (N);
2797 Choice : constant Node_Id := Right_Opnd (N);
2798 Expr : constant Node_Id := Left_Opnd (N);
2799 Result : Match_Result;
2800
2801 begin
2802 -- Ignore if error in either operand, except to make sure that Any_Type
2803 -- is properly propagated to avoid junk cascaded errors.
2804
2805 if Etype (Expr) = Any_Type
2806 or else (Present (Choice) and then Etype (Choice) = Any_Type)
2807 then
2808 Set_Etype (N, Any_Type);
2809 return;
2810 end if;
2811
2812 -- If left operand non-static, then nothing to do
2813
2814 if not Is_Static_Expression (Expr) then
2815 return;
2816 end if;
2817
2818 -- If choice is non-static, left operand is in non-static context
2819
2820 if (Present (Choice) and then not Is_Static_Choice (Choice))
2821 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
2822 then
2823 Check_Non_Static_Context (Expr);
2824 return;
2825 end if;
2826
2827 -- Otherwise we definitely have a static expression
2828
2829 Set_Is_Static_Expression (N);
2830
2831 -- If left operand raises constraint error, propagate and we are done
2832
2833 if Raises_Constraint_Error (Expr) then
2834 Set_Raises_Constraint_Error (N, True);
2835
2836 -- See if we match
2837
2838 else
2839 if Present (Choice) then
2840 Result := Choice_Matches (Expr, Choice);
2841 else
2842 Result := Choices_Match (Expr, Alts);
2843 end if;
2844
2845 -- If result is Non_Static, it means that we raise Constraint_Error,
2846 -- since we already tested that the operands were themselves static.
2847
2848 if Result = Non_Static then
2849 Set_Raises_Constraint_Error (N);
2850
2851 -- Otherwise we have our result (flipped if NOT IN case)
2852
2853 else
2854 Fold_Uint
2855 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
2856 Warn_On_Known_Condition (N);
2857 end if;
2858 end if;
2859 end Eval_Membership_Op;
2860
2861 ------------------------
2862 -- Eval_Named_Integer --
2863 ------------------------
2864
2865 procedure Eval_Named_Integer (N : Node_Id) is
2866 begin
2867 Fold_Uint (N,
2868 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2869 end Eval_Named_Integer;
2870
2871 ---------------------
2872 -- Eval_Named_Real --
2873 ---------------------
2874
2875 procedure Eval_Named_Real (N : Node_Id) is
2876 begin
2877 Fold_Ureal (N,
2878 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2879 end Eval_Named_Real;
2880
2881 -------------------
2882 -- Eval_Op_Expon --
2883 -------------------
2884
2885 -- Exponentiation is a static functions, so the result is potentially
2886 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2887
2888 procedure Eval_Op_Expon (N : Node_Id) is
2889 Left : constant Node_Id := Left_Opnd (N);
2890 Right : constant Node_Id := Right_Opnd (N);
2891 Stat : Boolean;
2892 Fold : Boolean;
2893
2894 begin
2895 -- If not foldable we are done
2896
2897 Test_Expression_Is_Foldable
2898 (N, Left, Right, Stat, Fold, CRT_Safe => True);
2899
2900 -- Return if not foldable
2901
2902 if not Fold then
2903 return;
2904 end if;
2905
2906 if Configurable_Run_Time_Mode and not Stat then
2907 return;
2908 end if;
2909
2910 -- Fold exponentiation operation
2911
2912 declare
2913 Right_Int : constant Uint := Expr_Value (Right);
2914
2915 begin
2916 -- Integer case
2917
2918 if Is_Integer_Type (Etype (Left)) then
2919 declare
2920 Left_Int : constant Uint := Expr_Value (Left);
2921 Result : Uint;
2922
2923 begin
2924 -- Exponentiation of an integer raises Constraint_Error for a
2925 -- negative exponent (RM 4.5.6).
2926
2927 if Right_Int < 0 then
2928 Apply_Compile_Time_Constraint_Error
2929 (N, "integer exponent negative", CE_Range_Check_Failed,
2930 Warn => not Stat);
2931 return;
2932
2933 else
2934 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2935 Result := Left_Int ** Right_Int;
2936 else
2937 Result := Left_Int;
2938 end if;
2939
2940 if Is_Modular_Integer_Type (Etype (N)) then
2941 Result := Result mod Modulus (Etype (N));
2942 end if;
2943
2944 Fold_Uint (N, Result, Stat);
2945 end if;
2946 end;
2947
2948 -- Real case
2949
2950 else
2951 declare
2952 Left_Real : constant Ureal := Expr_Value_R (Left);
2953
2954 begin
2955 -- Cannot have a zero base with a negative exponent
2956
2957 if UR_Is_Zero (Left_Real) then
2958
2959 if Right_Int < 0 then
2960 Apply_Compile_Time_Constraint_Error
2961 (N, "zero ** negative integer", CE_Range_Check_Failed,
2962 Warn => not Stat);
2963 return;
2964 else
2965 Fold_Ureal (N, Ureal_0, Stat);
2966 end if;
2967
2968 else
2969 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2970 end if;
2971 end;
2972 end if;
2973 end;
2974 end Eval_Op_Expon;
2975
2976 -----------------
2977 -- Eval_Op_Not --
2978 -----------------
2979
2980 -- The not operation is a static functions, so the result is potentially
2981 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2982
2983 procedure Eval_Op_Not (N : Node_Id) is
2984 Right : constant Node_Id := Right_Opnd (N);
2985 Stat : Boolean;
2986 Fold : Boolean;
2987
2988 begin
2989 -- If not foldable we are done
2990
2991 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2992
2993 if not Fold then
2994 return;
2995 end if;
2996
2997 -- Fold not operation
2998
2999 declare
3000 Rint : constant Uint := Expr_Value (Right);
3001 Typ : constant Entity_Id := Etype (N);
3002
3003 begin
3004 -- Negation is equivalent to subtracting from the modulus minus one.
3005 -- For a binary modulus this is equivalent to the ones-complement of
3006 -- the original value. For a nonbinary modulus this is an arbitrary
3007 -- but consistent definition.
3008
3009 if Is_Modular_Integer_Type (Typ) then
3010 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3011 else pragma Assert (Is_Boolean_Type (Typ));
3012 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3013 end if;
3014
3015 Set_Is_Static_Expression (N, Stat);
3016 end;
3017 end Eval_Op_Not;
3018
3019 -------------------------------
3020 -- Eval_Qualified_Expression --
3021 -------------------------------
3022
3023 -- A qualified expression is potentially static if its subtype mark denotes
3024 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
3025
3026 procedure Eval_Qualified_Expression (N : Node_Id) is
3027 Operand : constant Node_Id := Expression (N);
3028 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3029
3030 Stat : Boolean;
3031 Fold : Boolean;
3032 Hex : Boolean;
3033
3034 begin
3035 -- Can only fold if target is string or scalar and subtype is static.
3036 -- Also, do not fold if our parent is an allocator (this is because the
3037 -- qualified expression is really part of the syntactic structure of an
3038 -- allocator, and we do not want to end up with something that
3039 -- corresponds to "new 1" where the 1 is the result of folding a
3040 -- qualified expression).
3041
3042 if not Is_Static_Subtype (Target_Type)
3043 or else Nkind (Parent (N)) = N_Allocator
3044 then
3045 Check_Non_Static_Context (Operand);
3046
3047 -- If operand is known to raise constraint_error, set the flag on the
3048 -- expression so it does not get optimized away.
3049
3050 if Nkind (Operand) = N_Raise_Constraint_Error then
3051 Set_Raises_Constraint_Error (N);
3052 end if;
3053
3054 return;
3055 end if;
3056
3057 -- If not foldable we are done
3058
3059 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3060
3061 if not Fold then
3062 return;
3063
3064 -- Don't try fold if target type has constraint error bounds
3065
3066 elsif not Is_OK_Static_Subtype (Target_Type) then
3067 Set_Raises_Constraint_Error (N);
3068 return;
3069 end if;
3070
3071 -- Here we will fold, save Print_In_Hex indication
3072
3073 Hex := Nkind (Operand) = N_Integer_Literal
3074 and then Print_In_Hex (Operand);
3075
3076 -- Fold the result of qualification
3077
3078 if Is_Discrete_Type (Target_Type) then
3079 Fold_Uint (N, Expr_Value (Operand), Stat);
3080
3081 -- Preserve Print_In_Hex indication
3082
3083 if Hex and then Nkind (N) = N_Integer_Literal then
3084 Set_Print_In_Hex (N);
3085 end if;
3086
3087 elsif Is_Real_Type (Target_Type) then
3088 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3089
3090 else
3091 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3092
3093 if not Stat then
3094 Set_Is_Static_Expression (N, False);
3095 else
3096 Check_String_Literal_Length (N, Target_Type);
3097 end if;
3098
3099 return;
3100 end if;
3101
3102 -- The expression may be foldable but not static
3103
3104 Set_Is_Static_Expression (N, Stat);
3105
3106 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3107 Out_Of_Range (N);
3108 end if;
3109 end Eval_Qualified_Expression;
3110
3111 -----------------------
3112 -- Eval_Real_Literal --
3113 -----------------------
3114
3115 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3116 -- as static by the analyzer. The reason we did it that early is to allow
3117 -- the possibility of turning off the Is_Static_Expression flag after
3118 -- analysis, but before resolution, when integer literals are generated
3119 -- in the expander that do not correspond to static expressions.
3120
3121 procedure Eval_Real_Literal (N : Node_Id) is
3122 PK : constant Node_Kind := Nkind (Parent (N));
3123
3124 begin
3125 -- If the literal appears in a non-expression context and not as part of
3126 -- a number declaration, then it is appearing in a non-static context,
3127 -- so check it.
3128
3129 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3130 Check_Non_Static_Context (N);
3131 end if;
3132 end Eval_Real_Literal;
3133
3134 ------------------------
3135 -- Eval_Relational_Op --
3136 ------------------------
3137
3138 -- Relational operations are static functions, so the result is static if
3139 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3140 -- the result is never static, even if the operands are.
3141
3142 -- However, for internally generated nodes, we allow string equality and
3143 -- inequality to be static. This is because we rewrite A in "ABC" as an
3144 -- equality test A = "ABC", and the former is definitely static.
3145
3146 procedure Eval_Relational_Op (N : Node_Id) is
3147 Left : constant Node_Id := Left_Opnd (N);
3148 Right : constant Node_Id := Right_Opnd (N);
3149
3150 procedure Decompose_Expr
3151 (Expr : Node_Id;
3152 Ent : out Entity_Id;
3153 Kind : out Character;
3154 Cons : out Uint;
3155 Orig : Boolean := True);
3156 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3157 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3158 -- simple entity, and Cons is the value of K. If the expression is not
3159 -- of the required form, Ent is set to Empty.
3160 --
3161 -- Orig indicates whether Expr is the original expression to consider,
3162 -- or if we are handling a subexpression (e.g. recursive call to
3163 -- Decompose_Expr).
3164
3165 procedure Fold_General_Op (Is_Static : Boolean);
3166 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3167 -- be set when the operator denotes a static expression.
3168
3169 procedure Fold_Static_Real_Op;
3170 -- Attempt to fold static real type relational operator N
3171
3172 function Static_Length (Expr : Node_Id) return Uint;
3173 -- If Expr is an expression for a constrained array whose length is
3174 -- known at compile time, return the non-negative length, otherwise
3175 -- return -1.
3176
3177 --------------------
3178 -- Decompose_Expr --
3179 --------------------
3180
3181 procedure Decompose_Expr
3182 (Expr : Node_Id;
3183 Ent : out Entity_Id;
3184 Kind : out Character;
3185 Cons : out Uint;
3186 Orig : Boolean := True)
3187 is
3188 Exp : Node_Id;
3189
3190 begin
3191 -- Assume that the expression does not meet the expected form
3192
3193 Cons := No_Uint;
3194 Ent := Empty;
3195 Kind := '?';
3196
3197 if Nkind (Expr) = N_Op_Add
3198 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3199 then
3200 Exp := Left_Opnd (Expr);
3201 Cons := Expr_Value (Right_Opnd (Expr));
3202
3203 elsif Nkind (Expr) = N_Op_Subtract
3204 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3205 then
3206 Exp := Left_Opnd (Expr);
3207 Cons := -Expr_Value (Right_Opnd (Expr));
3208
3209 -- If the bound is a constant created to remove side effects, recover
3210 -- the original expression to see if it has one of the recognizable
3211 -- forms.
3212
3213 elsif Nkind (Expr) = N_Identifier
3214 and then not Comes_From_Source (Entity (Expr))
3215 and then Ekind (Entity (Expr)) = E_Constant
3216 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3217 then
3218 Exp := Expression (Parent (Entity (Expr)));
3219 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3220
3221 -- If original expression includes an entity, create a reference
3222 -- to it for use below.
3223
3224 if Present (Ent) then
3225 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3226 else
3227 return;
3228 end if;
3229
3230 else
3231 -- Only consider the case of X + 0 for a full expression, and
3232 -- not when recursing, otherwise we may end up with evaluating
3233 -- expressions not known at compile time to 0.
3234
3235 if Orig then
3236 Exp := Expr;
3237 Cons := Uint_0;
3238 else
3239 return;
3240 end if;
3241 end if;
3242
3243 -- At this stage Exp is set to the potential X
3244
3245 if Nkind (Exp) = N_Attribute_Reference then
3246 if Attribute_Name (Exp) = Name_First then
3247 Kind := 'F';
3248 elsif Attribute_Name (Exp) = Name_Last then
3249 Kind := 'L';
3250 else
3251 return;
3252 end if;
3253
3254 Exp := Prefix (Exp);
3255
3256 else
3257 Kind := 'E';
3258 end if;
3259
3260 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3261 Ent := Entity (Exp);
3262 end if;
3263 end Decompose_Expr;
3264
3265 ---------------------
3266 -- Fold_General_Op --
3267 ---------------------
3268
3269 procedure Fold_General_Op (Is_Static : Boolean) is
3270 CR : constant Compare_Result :=
3271 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3272
3273 Result : Boolean;
3274
3275 begin
3276 if CR = Unknown then
3277 return;
3278 end if;
3279
3280 case Nkind (N) is
3281 when N_Op_Eq =>
3282 if CR = EQ then
3283 Result := True;
3284 elsif CR = NE or else CR = GT or else CR = LT then
3285 Result := False;
3286 else
3287 return;
3288 end if;
3289
3290 when N_Op_Ge =>
3291 if CR = GT or else CR = EQ or else CR = GE then
3292 Result := True;
3293 elsif CR = LT then
3294 Result := False;
3295 else
3296 return;
3297 end if;
3298
3299 when N_Op_Gt =>
3300 if CR = GT then
3301 Result := True;
3302 elsif CR = EQ or else CR = LT or else CR = LE then
3303 Result := False;
3304 else
3305 return;
3306 end if;
3307
3308 when N_Op_Le =>
3309 if CR = LT or else CR = EQ or else CR = LE then
3310 Result := True;
3311 elsif CR = GT then
3312 Result := False;
3313 else
3314 return;
3315 end if;
3316
3317 when N_Op_Lt =>
3318 if CR = LT then
3319 Result := True;
3320 elsif CR = EQ or else CR = GT or else CR = GE then
3321 Result := False;
3322 else
3323 return;
3324 end if;
3325
3326 when N_Op_Ne =>
3327 if CR = NE or else CR = GT or else CR = LT then
3328 Result := True;
3329 elsif CR = EQ then
3330 Result := False;
3331 else
3332 return;
3333 end if;
3334
3335 when others =>
3336 raise Program_Error;
3337 end case;
3338
3339 -- Determine the potential outcome of the relation assuming the
3340 -- operands are valid and emit a warning when the relation yields
3341 -- True or False only in the presence of invalid values.
3342
3343 Warn_On_Constant_Valid_Condition (N);
3344
3345 Fold_Uint (N, Test (Result), Is_Static);
3346 end Fold_General_Op;
3347
3348 -------------------------
3349 -- Fold_Static_Real_Op --
3350 -------------------------
3351
3352 procedure Fold_Static_Real_Op is
3353 Left_Real : constant Ureal := Expr_Value_R (Left);
3354 Right_Real : constant Ureal := Expr_Value_R (Right);
3355 Result : Boolean;
3356
3357 begin
3358 case Nkind (N) is
3359 when N_Op_Eq => Result := (Left_Real = Right_Real);
3360 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3361 when N_Op_Gt => Result := (Left_Real > Right_Real);
3362 when N_Op_Le => Result := (Left_Real <= Right_Real);
3363 when N_Op_Lt => Result := (Left_Real < Right_Real);
3364 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3365 when others => raise Program_Error;
3366 end case;
3367
3368 Fold_Uint (N, Test (Result), True);
3369 end Fold_Static_Real_Op;
3370
3371 -------------------
3372 -- Static_Length --
3373 -------------------
3374
3375 function Static_Length (Expr : Node_Id) return Uint is
3376 Cons1 : Uint;
3377 Cons2 : Uint;
3378 Ent1 : Entity_Id;
3379 Ent2 : Entity_Id;
3380 Kind1 : Character;
3381 Kind2 : Character;
3382 Typ : Entity_Id;
3383
3384 begin
3385 -- First easy case string literal
3386
3387 if Nkind (Expr) = N_String_Literal then
3388 return UI_From_Int (String_Length (Strval (Expr)));
3389
3390 -- Second easy case, not constrained subtype, so no length
3391
3392 elsif not Is_Constrained (Etype (Expr)) then
3393 return Uint_Minus_1;
3394 end if;
3395
3396 -- General case
3397
3398 Typ := Etype (First_Index (Etype (Expr)));
3399
3400 -- The simple case, both bounds are known at compile time
3401
3402 if Is_Discrete_Type (Typ)
3403 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3404 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3405 then
3406 return
3407 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3408 Expr_Value (Type_Low_Bound (Typ)) + 1);
3409 end if;
3410
3411 -- A more complex case, where the bounds are of the form X [+/- K1]
3412 -- .. X [+/- K2]), where X is an expression that is either A'First or
3413 -- A'Last (with A an entity name), or X is an entity name, and the
3414 -- two X's are the same and K1 and K2 are known at compile time, in
3415 -- this case, the length can also be computed at compile time, even
3416 -- though the bounds are not known. A common case of this is e.g.
3417 -- (X'First .. X'First+5).
3418
3419 Decompose_Expr
3420 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3421 Decompose_Expr
3422 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3423
3424 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3425 return Cons2 - Cons1 + 1;
3426 else
3427 return Uint_Minus_1;
3428 end if;
3429 end Static_Length;
3430
3431 -- Local variables
3432
3433 Left_Typ : constant Entity_Id := Etype (Left);
3434 Right_Typ : constant Entity_Id := Etype (Right);
3435 Fold : Boolean;
3436 Left_Len : Uint;
3437 Op_Typ : Entity_Id := Empty;
3438 Right_Len : Uint;
3439
3440 Is_Static_Expression : Boolean;
3441
3442 -- Start of processing for Eval_Relational_Op
3443
3444 begin
3445 -- One special case to deal with first. If we can tell that the result
3446 -- will be false because the lengths of one or more index subtypes are
3447 -- compile-time known and different, then we can replace the entire
3448 -- result by False. We only do this for one-dimensional arrays, because
3449 -- the case of multidimensional arrays is rare and too much trouble. If
3450 -- one of the operands is an illegal aggregate, its type might still be
3451 -- an arbitrary composite type, so nothing to do.
3452
3453 if Is_Array_Type (Left_Typ)
3454 and then Left_Typ /= Any_Composite
3455 and then Number_Dimensions (Left_Typ) = 1
3456 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3457 then
3458 if Raises_Constraint_Error (Left)
3459 or else
3460 Raises_Constraint_Error (Right)
3461 then
3462 return;
3463
3464 -- OK, we have the case where we may be able to do this fold
3465
3466 else
3467 Left_Len := Static_Length (Left);
3468 Right_Len := Static_Length (Right);
3469
3470 if Left_Len /= Uint_Minus_1
3471 and then Right_Len /= Uint_Minus_1
3472 and then Left_Len /= Right_Len
3473 then
3474 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
3475 Warn_On_Known_Condition (N);
3476 return;
3477 end if;
3478 end if;
3479
3480 -- General case
3481
3482 else
3483 -- Initialize the value of Is_Static_Expression. The value of Fold
3484 -- returned by Test_Expression_Is_Foldable is not needed since, even
3485 -- when some operand is a variable, we can still perform the static
3486 -- evaluation of the expression in some cases (for example, for a
3487 -- variable of a subtype of Integer we statically know that any value
3488 -- stored in such variable is smaller than Integer'Last).
3489
3490 Test_Expression_Is_Foldable
3491 (N, Left, Right, Is_Static_Expression, Fold);
3492
3493 -- Only comparisons of scalars can give static results. A comparison
3494 -- of strings never yields a static result, even if both operands are
3495 -- static strings, except that as noted above, we allow equality and
3496 -- inequality for strings.
3497
3498 if Is_String_Type (Left_Typ)
3499 and then not Comes_From_Source (N)
3500 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3501 then
3502 null;
3503
3504 elsif not Is_Scalar_Type (Left_Typ) then
3505 Is_Static_Expression := False;
3506 Set_Is_Static_Expression (N, False);
3507 end if;
3508
3509 -- For operators on universal numeric types called as functions with
3510 -- an explicit scope, determine appropriate specific numeric type,
3511 -- and diagnose possible ambiguity.
3512
3513 if Is_Universal_Numeric_Type (Left_Typ)
3514 and then
3515 Is_Universal_Numeric_Type (Right_Typ)
3516 then
3517 Op_Typ := Find_Universal_Operator_Type (N);
3518 end if;
3519
3520 -- Attempt to fold the relational operator
3521
3522 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3523 Fold_Static_Real_Op;
3524 else
3525 Fold_General_Op (Is_Static_Expression);
3526 end if;
3527 end if;
3528
3529 -- For the case of a folded relational operator on a specific numeric
3530 -- type, freeze the operand type now.
3531
3532 if Present (Op_Typ) then
3533 Freeze_Before (N, Op_Typ);
3534 end if;
3535
3536 Warn_On_Known_Condition (N);
3537 end Eval_Relational_Op;
3538
3539 ----------------
3540 -- Eval_Shift --
3541 ----------------
3542
3543 -- Shift operations are intrinsic operations that can never be static, so
3544 -- the only processing required is to perform the required check for a non
3545 -- static context for the two operands.
3546
3547 -- Actually we could do some compile time evaluation here some time ???
3548
3549 procedure Eval_Shift (N : Node_Id) is
3550 begin
3551 Check_Non_Static_Context (Left_Opnd (N));
3552 Check_Non_Static_Context (Right_Opnd (N));
3553 end Eval_Shift;
3554
3555 ------------------------
3556 -- Eval_Short_Circuit --
3557 ------------------------
3558
3559 -- A short circuit operation is potentially static if both operands are
3560 -- potentially static (RM 4.9 (13)).
3561
3562 procedure Eval_Short_Circuit (N : Node_Id) is
3563 Kind : constant Node_Kind := Nkind (N);
3564 Left : constant Node_Id := Left_Opnd (N);
3565 Right : constant Node_Id := Right_Opnd (N);
3566 Left_Int : Uint;
3567
3568 Rstat : constant Boolean :=
3569 Is_Static_Expression (Left)
3570 and then
3571 Is_Static_Expression (Right);
3572
3573 begin
3574 -- Short circuit operations are never static in Ada 83
3575
3576 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3577 Check_Non_Static_Context (Left);
3578 Check_Non_Static_Context (Right);
3579 return;
3580 end if;
3581
3582 -- Now look at the operands, we can't quite use the normal call to
3583 -- Test_Expression_Is_Foldable here because short circuit operations
3584 -- are a special case, they can still be foldable, even if the right
3585 -- operand raises constraint error.
3586
3587 -- If either operand is Any_Type, just propagate to result and do not
3588 -- try to fold, this prevents cascaded errors.
3589
3590 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3591 Set_Etype (N, Any_Type);
3592 return;
3593
3594 -- If left operand raises constraint error, then replace node N with
3595 -- the raise constraint error node, and we are obviously not foldable.
3596 -- Is_Static_Expression is set from the two operands in the normal way,
3597 -- and we check the right operand if it is in a non-static context.
3598
3599 elsif Raises_Constraint_Error (Left) then
3600 if not Rstat then
3601 Check_Non_Static_Context (Right);
3602 end if;
3603
3604 Rewrite_In_Raise_CE (N, Left);
3605 Set_Is_Static_Expression (N, Rstat);
3606 return;
3607
3608 -- If the result is not static, then we won't in any case fold
3609
3610 elsif not Rstat then
3611 Check_Non_Static_Context (Left);
3612 Check_Non_Static_Context (Right);
3613 return;
3614 end if;
3615
3616 -- Here the result is static, note that, unlike the normal processing
3617 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3618 -- the right operand raises constraint error, that's because it is not
3619 -- significant if the left operand is decisive.
3620
3621 Set_Is_Static_Expression (N);
3622
3623 -- It does not matter if the right operand raises constraint error if
3624 -- it will not be evaluated. So deal specially with the cases where
3625 -- the right operand is not evaluated. Note that we will fold these
3626 -- cases even if the right operand is non-static, which is fine, but
3627 -- of course in these cases the result is not potentially static.
3628
3629 Left_Int := Expr_Value (Left);
3630
3631 if (Kind = N_And_Then and then Is_False (Left_Int))
3632 or else
3633 (Kind = N_Or_Else and then Is_True (Left_Int))
3634 then
3635 Fold_Uint (N, Left_Int, Rstat);
3636 return;
3637 end if;
3638
3639 -- If first operand not decisive, then it does matter if the right
3640 -- operand raises constraint error, since it will be evaluated, so
3641 -- we simply replace the node with the right operand. Note that this
3642 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3643 -- (both are set to True in Right).
3644
3645 if Raises_Constraint_Error (Right) then
3646 Rewrite_In_Raise_CE (N, Right);
3647 Check_Non_Static_Context (Left);
3648 return;
3649 end if;
3650
3651 -- Otherwise the result depends on the right operand
3652
3653 Fold_Uint (N, Expr_Value (Right), Rstat);
3654 return;
3655 end Eval_Short_Circuit;
3656
3657 ----------------
3658 -- Eval_Slice --
3659 ----------------
3660
3661 -- Slices can never be static, so the only processing required is to check
3662 -- for non-static context if an explicit range is given.
3663
3664 procedure Eval_Slice (N : Node_Id) is
3665 Drange : constant Node_Id := Discrete_Range (N);
3666
3667 begin
3668 if Nkind (Drange) = N_Range then
3669 Check_Non_Static_Context (Low_Bound (Drange));
3670 Check_Non_Static_Context (High_Bound (Drange));
3671 end if;
3672
3673 -- A slice of the form A (subtype), when the subtype is the index of
3674 -- the type of A, is redundant, the slice can be replaced with A, and
3675 -- this is worth a warning.
3676
3677 if Is_Entity_Name (Prefix (N)) then
3678 declare
3679 E : constant Entity_Id := Entity (Prefix (N));
3680 T : constant Entity_Id := Etype (E);
3681
3682 begin
3683 if Ekind (E) = E_Constant
3684 and then Is_Array_Type (T)
3685 and then Is_Entity_Name (Drange)
3686 then
3687 if Is_Entity_Name (Original_Node (First_Index (T)))
3688 and then Entity (Original_Node (First_Index (T)))
3689 = Entity (Drange)
3690 then
3691 if Warn_On_Redundant_Constructs then
3692 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3693 end if;
3694
3695 -- The following might be a useful optimization???
3696
3697 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3698 end if;
3699 end if;
3700 end;
3701 end if;
3702 end Eval_Slice;
3703
3704 -------------------------
3705 -- Eval_String_Literal --
3706 -------------------------
3707
3708 procedure Eval_String_Literal (N : Node_Id) is
3709 Typ : constant Entity_Id := Etype (N);
3710 Bas : constant Entity_Id := Base_Type (Typ);
3711 Xtp : Entity_Id;
3712 Len : Nat;
3713 Lo : Node_Id;
3714
3715 begin
3716 -- Nothing to do if error type (handles cases like default expressions
3717 -- or generics where we have not yet fully resolved the type).
3718
3719 if Bas = Any_Type or else Bas = Any_String then
3720 return;
3721 end if;
3722
3723 -- String literals are static if the subtype is static (RM 4.9(2)), so
3724 -- reset the static expression flag (it was set unconditionally in
3725 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3726 -- the subtype is static by looking at the lower bound.
3727
3728 if Ekind (Typ) = E_String_Literal_Subtype then
3729 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3730 Set_Is_Static_Expression (N, False);
3731 return;
3732 end if;
3733
3734 -- Here if Etype of string literal is normal Etype (not yet possible,
3735 -- but may be possible in future).
3736
3737 elsif not Is_OK_Static_Expression
3738 (Type_Low_Bound (Etype (First_Index (Typ))))
3739 then
3740 Set_Is_Static_Expression (N, False);
3741 return;
3742 end if;
3743
3744 -- If original node was a type conversion, then result if non-static
3745
3746 if Nkind (Original_Node (N)) = N_Type_Conversion then
3747 Set_Is_Static_Expression (N, False);
3748 return;
3749 end if;
3750
3751 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3752 -- if its bounds are outside the index base type and this index type is
3753 -- static. This can happen in only two ways. Either the string literal
3754 -- is too long, or it is null, and the lower bound is type'First. Either
3755 -- way it is the upper bound that is out of range of the index type.
3756
3757 if Ada_Version >= Ada_95 then
3758 if Is_Standard_String_Type (Bas) then
3759 Xtp := Standard_Positive;
3760 else
3761 Xtp := Etype (First_Index (Bas));
3762 end if;
3763
3764 if Ekind (Typ) = E_String_Literal_Subtype then
3765 Lo := String_Literal_Low_Bound (Typ);
3766 else
3767 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3768 end if;
3769
3770 -- Check for string too long
3771
3772 Len := String_Length (Strval (N));
3773
3774 if UI_From_Int (Len) > String_Type_Len (Bas) then
3775
3776 -- Issue message. Note that this message is a warning if the
3777 -- string literal is not marked as static (happens in some cases
3778 -- of folding strings known at compile time, but not static).
3779 -- Furthermore in such cases, we reword the message, since there
3780 -- is no string literal in the source program.
3781
3782 if Is_Static_Expression (N) then
3783 Apply_Compile_Time_Constraint_Error
3784 (N, "string literal too long for}", CE_Length_Check_Failed,
3785 Ent => Bas,
3786 Typ => First_Subtype (Bas));
3787 else
3788 Apply_Compile_Time_Constraint_Error
3789 (N, "string value too long for}", CE_Length_Check_Failed,
3790 Ent => Bas,
3791 Typ => First_Subtype (Bas),
3792 Warn => True);
3793 end if;
3794
3795 -- Test for null string not allowed
3796
3797 elsif Len = 0
3798 and then not Is_Generic_Type (Xtp)
3799 and then
3800 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3801 then
3802 -- Same specialization of message
3803
3804 if Is_Static_Expression (N) then
3805 Apply_Compile_Time_Constraint_Error
3806 (N, "null string literal not allowed for}",
3807 CE_Length_Check_Failed,
3808 Ent => Bas,
3809 Typ => First_Subtype (Bas));
3810 else
3811 Apply_Compile_Time_Constraint_Error
3812 (N, "null string value not allowed for}",
3813 CE_Length_Check_Failed,
3814 Ent => Bas,
3815 Typ => First_Subtype (Bas),
3816 Warn => True);
3817 end if;
3818 end if;
3819 end if;
3820 end Eval_String_Literal;
3821
3822 --------------------------
3823 -- Eval_Type_Conversion --
3824 --------------------------
3825
3826 -- A type conversion is potentially static if its subtype mark is for a
3827 -- static scalar subtype, and its operand expression is potentially static
3828 -- (RM 4.9(10)).
3829
3830 procedure Eval_Type_Conversion (N : Node_Id) is
3831 Operand : constant Node_Id := Expression (N);
3832 Source_Type : constant Entity_Id := Etype (Operand);
3833 Target_Type : constant Entity_Id := Etype (N);
3834
3835 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3836 -- Returns true if type T is an integer type, or if it is a fixed-point
3837 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3838 -- on the conversion node).
3839
3840 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3841 -- Returns true if type T is a floating-point type, or if it is a
3842 -- fixed-point type that is not to be treated as an integer (i.e. the
3843 -- flag Conversion_OK is not set on the conversion node).
3844
3845 ------------------------------
3846 -- To_Be_Treated_As_Integer --
3847 ------------------------------
3848
3849 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3850 begin
3851 return
3852 Is_Integer_Type (T)
3853 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3854 end To_Be_Treated_As_Integer;
3855
3856 ---------------------------
3857 -- To_Be_Treated_As_Real --
3858 ---------------------------
3859
3860 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3861 begin
3862 return
3863 Is_Floating_Point_Type (T)
3864 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3865 end To_Be_Treated_As_Real;
3866
3867 -- Local variables
3868
3869 Fold : Boolean;
3870 Stat : Boolean;
3871
3872 -- Start of processing for Eval_Type_Conversion
3873
3874 begin
3875 -- Cannot fold if target type is non-static or if semantic error
3876
3877 if not Is_Static_Subtype (Target_Type) then
3878 Check_Non_Static_Context (Operand);
3879 return;
3880 elsif Error_Posted (N) then
3881 return;
3882 end if;
3883
3884 -- If not foldable we are done
3885
3886 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3887
3888 if not Fold then
3889 return;
3890
3891 -- Don't try fold if target type has constraint error bounds
3892
3893 elsif not Is_OK_Static_Subtype (Target_Type) then
3894 Set_Raises_Constraint_Error (N);
3895 return;
3896 end if;
3897
3898 -- Remaining processing depends on operand types. Note that in the
3899 -- following type test, fixed-point counts as real unless the flag
3900 -- Conversion_OK is set, in which case it counts as integer.
3901
3902 -- Fold conversion, case of string type. The result is not static
3903
3904 if Is_String_Type (Target_Type) then
3905 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3906 return;
3907
3908 -- Fold conversion, case of integer target type
3909
3910 elsif To_Be_Treated_As_Integer (Target_Type) then
3911 declare
3912 Result : Uint;
3913
3914 begin
3915 -- Integer to integer conversion
3916
3917 if To_Be_Treated_As_Integer (Source_Type) then
3918 Result := Expr_Value (Operand);
3919
3920 -- Real to integer conversion
3921
3922 else
3923 Result := UR_To_Uint (Expr_Value_R (Operand));
3924 end if;
3925
3926 -- If fixed-point type (Conversion_OK must be set), then the
3927 -- result is logically an integer, but we must replace the
3928 -- conversion with the corresponding real literal, since the
3929 -- type from a semantic point of view is still fixed-point.
3930
3931 if Is_Fixed_Point_Type (Target_Type) then
3932 Fold_Ureal
3933 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3934
3935 -- Otherwise result is integer literal
3936
3937 else
3938 Fold_Uint (N, Result, Stat);
3939 end if;
3940 end;
3941
3942 -- Fold conversion, case of real target type
3943
3944 elsif To_Be_Treated_As_Real (Target_Type) then
3945 declare
3946 Result : Ureal;
3947
3948 begin
3949 if To_Be_Treated_As_Real (Source_Type) then
3950 Result := Expr_Value_R (Operand);
3951 else
3952 Result := UR_From_Uint (Expr_Value (Operand));
3953 end if;
3954
3955 Fold_Ureal (N, Result, Stat);
3956 end;
3957
3958 -- Enumeration types
3959
3960 else
3961 Fold_Uint (N, Expr_Value (Operand), Stat);
3962 end if;
3963
3964 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3965 Out_Of_Range (N);
3966 end if;
3967
3968 end Eval_Type_Conversion;
3969
3970 -------------------
3971 -- Eval_Unary_Op --
3972 -------------------
3973
3974 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3975 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3976
3977 procedure Eval_Unary_Op (N : Node_Id) is
3978 Right : constant Node_Id := Right_Opnd (N);
3979 Otype : Entity_Id := Empty;
3980 Stat : Boolean;
3981 Fold : Boolean;
3982
3983 begin
3984 -- If not foldable we are done
3985
3986 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3987
3988 if not Fold then
3989 return;
3990 end if;
3991
3992 if Etype (Right) = Universal_Integer
3993 or else
3994 Etype (Right) = Universal_Real
3995 then
3996 Otype := Find_Universal_Operator_Type (N);
3997 end if;
3998
3999 -- Fold for integer case
4000
4001 if Is_Integer_Type (Etype (N)) then
4002 declare
4003 Rint : constant Uint := Expr_Value (Right);
4004 Result : Uint;
4005
4006 begin
4007 -- In the case of modular unary plus and abs there is no need
4008 -- to adjust the result of the operation since if the original
4009 -- operand was in bounds the result will be in the bounds of the
4010 -- modular type. However, in the case of modular unary minus the
4011 -- result may go out of the bounds of the modular type and needs
4012 -- adjustment.
4013
4014 if Nkind (N) = N_Op_Plus then
4015 Result := Rint;
4016
4017 elsif Nkind (N) = N_Op_Minus then
4018 if Is_Modular_Integer_Type (Etype (N)) then
4019 Result := (-Rint) mod Modulus (Etype (N));
4020 else
4021 Result := (-Rint);
4022 end if;
4023
4024 else
4025 pragma Assert (Nkind (N) = N_Op_Abs);
4026 Result := abs Rint;
4027 end if;
4028
4029 Fold_Uint (N, Result, Stat);
4030 end;
4031
4032 -- Fold for real case
4033
4034 elsif Is_Real_Type (Etype (N)) then
4035 declare
4036 Rreal : constant Ureal := Expr_Value_R (Right);
4037 Result : Ureal;
4038
4039 begin
4040 if Nkind (N) = N_Op_Plus then
4041 Result := Rreal;
4042 elsif Nkind (N) = N_Op_Minus then
4043 Result := UR_Negate (Rreal);
4044 else
4045 pragma Assert (Nkind (N) = N_Op_Abs);
4046 Result := abs Rreal;
4047 end if;
4048
4049 Fold_Ureal (N, Result, Stat);
4050 end;
4051 end if;
4052
4053 -- If the operator was resolved to a specific type, make sure that type
4054 -- is frozen even if the expression is folded into a literal (which has
4055 -- a universal type).
4056
4057 if Present (Otype) then
4058 Freeze_Before (N, Otype);
4059 end if;
4060 end Eval_Unary_Op;
4061
4062 -------------------------------
4063 -- Eval_Unchecked_Conversion --
4064 -------------------------------
4065
4066 -- Unchecked conversions can never be static, so the only required
4067 -- processing is to check for a non-static context for the operand.
4068
4069 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4070 begin
4071 Check_Non_Static_Context (Expression (N));
4072 end Eval_Unchecked_Conversion;
4073
4074 --------------------
4075 -- Expr_Rep_Value --
4076 --------------------
4077
4078 function Expr_Rep_Value (N : Node_Id) return Uint is
4079 Kind : constant Node_Kind := Nkind (N);
4080 Ent : Entity_Id;
4081
4082 begin
4083 if Is_Entity_Name (N) then
4084 Ent := Entity (N);
4085
4086 -- An enumeration literal that was either in the source or created
4087 -- as a result of static evaluation.
4088
4089 if Ekind (Ent) = E_Enumeration_Literal then
4090 return Enumeration_Rep (Ent);
4091
4092 -- A user defined static constant
4093
4094 else
4095 pragma Assert (Ekind (Ent) = E_Constant);
4096 return Expr_Rep_Value (Constant_Value (Ent));
4097 end if;
4098
4099 -- An integer literal that was either in the source or created as a
4100 -- result of static evaluation.
4101
4102 elsif Kind = N_Integer_Literal then
4103 return Intval (N);
4104
4105 -- A real literal for a fixed-point type. This must be the fixed-point
4106 -- case, either the literal is of a fixed-point type, or it is a bound
4107 -- of a fixed-point type, with type universal real. In either case we
4108 -- obtain the desired value from Corresponding_Integer_Value.
4109
4110 elsif Kind = N_Real_Literal then
4111 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4112 return Corresponding_Integer_Value (N);
4113
4114 -- Otherwise must be character literal
4115
4116 else
4117 pragma Assert (Kind = N_Character_Literal);
4118 Ent := Entity (N);
4119
4120 -- Since Character literals of type Standard.Character don't have any
4121 -- defining character literals built for them, they do not have their
4122 -- Entity set, so just use their Char code. Otherwise for user-
4123 -- defined character literals use their Pos value as usual which is
4124 -- the same as the Rep value.
4125
4126 if No (Ent) then
4127 return Char_Literal_Value (N);
4128 else
4129 return Enumeration_Rep (Ent);
4130 end if;
4131 end if;
4132 end Expr_Rep_Value;
4133
4134 ----------------
4135 -- Expr_Value --
4136 ----------------
4137
4138 function Expr_Value (N : Node_Id) return Uint is
4139 Kind : constant Node_Kind := Nkind (N);
4140 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4141 Ent : Entity_Id;
4142 Val : Uint;
4143
4144 begin
4145 -- If already in cache, then we know it's compile time known and we can
4146 -- return the value that was previously stored in the cache since
4147 -- compile time known values cannot change.
4148
4149 if CV_Ent.N = N then
4150 return CV_Ent.V;
4151 end if;
4152
4153 -- Otherwise proceed to test value
4154
4155 if Is_Entity_Name (N) then
4156 Ent := Entity (N);
4157
4158 -- An enumeration literal that was either in the source or created as
4159 -- a result of static evaluation.
4160
4161 if Ekind (Ent) = E_Enumeration_Literal then
4162 Val := Enumeration_Pos (Ent);
4163
4164 -- A user defined static constant
4165
4166 else
4167 pragma Assert (Ekind (Ent) = E_Constant);
4168 Val := Expr_Value (Constant_Value (Ent));
4169 end if;
4170
4171 -- An integer literal that was either in the source or created as a
4172 -- result of static evaluation.
4173
4174 elsif Kind = N_Integer_Literal then
4175 Val := Intval (N);
4176
4177 -- A real literal for a fixed-point type. This must be the fixed-point
4178 -- case, either the literal is of a fixed-point type, or it is a bound
4179 -- of a fixed-point type, with type universal real. In either case we
4180 -- obtain the desired value from Corresponding_Integer_Value.
4181
4182 elsif Kind = N_Real_Literal then
4183 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4184 Val := Corresponding_Integer_Value (N);
4185
4186 -- Otherwise must be character literal
4187
4188 else
4189 pragma Assert (Kind = N_Character_Literal);
4190 Ent := Entity (N);
4191
4192 -- Since Character literals of type Standard.Character don't
4193 -- have any defining character literals built for them, they
4194 -- do not have their Entity set, so just use their Char
4195 -- code. Otherwise for user-defined character literals use
4196 -- their Pos value as usual.
4197
4198 if No (Ent) then
4199 Val := Char_Literal_Value (N);
4200 else
4201 Val := Enumeration_Pos (Ent);
4202 end if;
4203 end if;
4204
4205 -- Come here with Val set to value to be returned, set cache
4206
4207 CV_Ent.N := N;
4208 CV_Ent.V := Val;
4209 return Val;
4210 end Expr_Value;
4211
4212 ------------------
4213 -- Expr_Value_E --
4214 ------------------
4215
4216 function Expr_Value_E (N : Node_Id) return Entity_Id is
4217 Ent : constant Entity_Id := Entity (N);
4218 begin
4219 if Ekind (Ent) = E_Enumeration_Literal then
4220 return Ent;
4221 else
4222 pragma Assert (Ekind (Ent) = E_Constant);
4223 return Expr_Value_E (Constant_Value (Ent));
4224 end if;
4225 end Expr_Value_E;
4226
4227 ------------------
4228 -- Expr_Value_R --
4229 ------------------
4230
4231 function Expr_Value_R (N : Node_Id) return Ureal is
4232 Kind : constant Node_Kind := Nkind (N);
4233 Ent : Entity_Id;
4234
4235 begin
4236 if Kind = N_Real_Literal then
4237 return Realval (N);
4238
4239 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4240 Ent := Entity (N);
4241 pragma Assert (Ekind (Ent) = E_Constant);
4242 return Expr_Value_R (Constant_Value (Ent));
4243
4244 elsif Kind = N_Integer_Literal then
4245 return UR_From_Uint (Expr_Value (N));
4246
4247 -- Here, we have a node that cannot be interpreted as a compile time
4248 -- constant. That is definitely an error.
4249
4250 else
4251 raise Program_Error;
4252 end if;
4253 end Expr_Value_R;
4254
4255 ------------------
4256 -- Expr_Value_S --
4257 ------------------
4258
4259 function Expr_Value_S (N : Node_Id) return Node_Id is
4260 begin
4261 if Nkind (N) = N_String_Literal then
4262 return N;
4263 else
4264 pragma Assert (Ekind (Entity (N)) = E_Constant);
4265 return Expr_Value_S (Constant_Value (Entity (N)));
4266 end if;
4267 end Expr_Value_S;
4268
4269 ----------------------------------
4270 -- Find_Universal_Operator_Type --
4271 ----------------------------------
4272
4273 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4274 PN : constant Node_Id := Parent (N);
4275 Call : constant Node_Id := Original_Node (N);
4276 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4277
4278 Is_Fix : constant Boolean :=
4279 Nkind (N) in N_Binary_Op
4280 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4281 -- A mixed-mode operation in this context indicates the presence of
4282 -- fixed-point type in the designated package.
4283
4284 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4285 -- Case where N is a relational (or membership) operator (else it is an
4286 -- arithmetic one).
4287
4288 In_Membership : constant Boolean :=
4289 Nkind (PN) in N_Membership_Test
4290 and then
4291 Nkind (Right_Opnd (PN)) = N_Range
4292 and then
4293 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4294 and then
4295 Is_Universal_Numeric_Type
4296 (Etype (Low_Bound (Right_Opnd (PN))))
4297 and then
4298 Is_Universal_Numeric_Type
4299 (Etype (High_Bound (Right_Opnd (PN))));
4300 -- Case where N is part of a membership test with a universal range
4301
4302 E : Entity_Id;
4303 Pack : Entity_Id;
4304 Typ1 : Entity_Id := Empty;
4305 Priv_E : Entity_Id;
4306
4307 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4308 -- Check whether one operand is a mixed-mode operation that requires the
4309 -- presence of a fixed-point type. Given that all operands are universal
4310 -- and have been constant-folded, retrieve the original function call.
4311
4312 ---------------------------
4313 -- Is_Mixed_Mode_Operand --
4314 ---------------------------
4315
4316 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4317 Onod : constant Node_Id := Original_Node (Op);
4318 begin
4319 return Nkind (Onod) = N_Function_Call
4320 and then Present (Next_Actual (First_Actual (Onod)))
4321 and then Etype (First_Actual (Onod)) /=
4322 Etype (Next_Actual (First_Actual (Onod)));
4323 end Is_Mixed_Mode_Operand;
4324
4325 -- Start of processing for Find_Universal_Operator_Type
4326
4327 begin
4328 if Nkind (Call) /= N_Function_Call
4329 or else Nkind (Name (Call)) /= N_Expanded_Name
4330 then
4331 return Empty;
4332
4333 -- There are several cases where the context does not imply the type of
4334 -- the operands:
4335 -- - the universal expression appears in a type conversion;
4336 -- - the expression is a relational operator applied to universal
4337 -- operands;
4338 -- - the expression is a membership test with a universal operand
4339 -- and a range with universal bounds.
4340
4341 elsif Nkind (Parent (N)) = N_Type_Conversion
4342 or else Is_Relational
4343 or else In_Membership
4344 then
4345 Pack := Entity (Prefix (Name (Call)));
4346
4347 -- If the prefix is a package declared elsewhere, iterate over its
4348 -- visible entities, otherwise iterate over all declarations in the
4349 -- designated scope.
4350
4351 if Ekind (Pack) = E_Package
4352 and then not In_Open_Scopes (Pack)
4353 then
4354 Priv_E := First_Private_Entity (Pack);
4355 else
4356 Priv_E := Empty;
4357 end if;
4358
4359 Typ1 := Empty;
4360 E := First_Entity (Pack);
4361 while Present (E) and then E /= Priv_E loop
4362 if Is_Numeric_Type (E)
4363 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4364 and then Comes_From_Source (E)
4365 and then Is_Integer_Type (E) = Is_Int
4366 and then (Nkind (N) in N_Unary_Op
4367 or else Is_Relational
4368 or else Is_Fixed_Point_Type (E) = Is_Fix)
4369 then
4370 if No (Typ1) then
4371 Typ1 := E;
4372
4373 -- Before emitting an error, check for the presence of a
4374 -- mixed-mode operation that specifies a fixed point type.
4375
4376 elsif Is_Relational
4377 and then
4378 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4379 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4380 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4381
4382 then
4383 if Is_Fixed_Point_Type (E) then
4384 Typ1 := E;
4385 end if;
4386
4387 else
4388 -- More than one type of the proper class declared in P
4389
4390 Error_Msg_N ("ambiguous operation", N);
4391 Error_Msg_Sloc := Sloc (Typ1);
4392 Error_Msg_N ("\possible interpretation (inherited)#", N);
4393 Error_Msg_Sloc := Sloc (E);
4394 Error_Msg_N ("\possible interpretation (inherited)#", N);
4395 return Empty;
4396 end if;
4397 end if;
4398
4399 Next_Entity (E);
4400 end loop;
4401 end if;
4402
4403 return Typ1;
4404 end Find_Universal_Operator_Type;
4405
4406 --------------------------
4407 -- Flag_Non_Static_Expr --
4408 --------------------------
4409
4410 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4411 begin
4412 if Error_Posted (Expr) and then not All_Errors_Mode then
4413 return;
4414 else
4415 Error_Msg_F (Msg, Expr);
4416 Why_Not_Static (Expr);
4417 end if;
4418 end Flag_Non_Static_Expr;
4419
4420 --------------
4421 -- Fold_Str --
4422 --------------
4423
4424 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4425 Loc : constant Source_Ptr := Sloc (N);
4426 Typ : constant Entity_Id := Etype (N);
4427
4428 begin
4429 if Raises_Constraint_Error (N) then
4430 Set_Is_Static_Expression (N, Static);
4431 return;
4432 end if;
4433
4434 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4435
4436 -- We now have the literal with the right value, both the actual type
4437 -- and the expected type of this literal are taken from the expression
4438 -- that was evaluated. So now we do the Analyze and Resolve.
4439
4440 -- Note that we have to reset Is_Static_Expression both after the
4441 -- analyze step (because Resolve will evaluate the literal, which
4442 -- will cause semantic errors if it is marked as static), and after
4443 -- the Resolve step (since Resolve in some cases resets this flag).
4444
4445 Analyze (N);
4446 Set_Is_Static_Expression (N, Static);
4447 Set_Etype (N, Typ);
4448 Resolve (N);
4449 Set_Is_Static_Expression (N, Static);
4450 end Fold_Str;
4451
4452 ---------------
4453 -- Fold_Uint --
4454 ---------------
4455
4456 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4457 Loc : constant Source_Ptr := Sloc (N);
4458 Typ : Entity_Id := Etype (N);
4459 Ent : Entity_Id;
4460
4461 begin
4462 if Raises_Constraint_Error (N) then
4463 Set_Is_Static_Expression (N, Static);
4464 return;
4465 end if;
4466
4467 -- If we are folding a named number, retain the entity in the literal,
4468 -- for ASIS use.
4469
4470 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
4471 Ent := Entity (N);
4472 else
4473 Ent := Empty;
4474 end if;
4475
4476 if Is_Private_Type (Typ) then
4477 Typ := Full_View (Typ);
4478 end if;
4479
4480 -- For a result of type integer, substitute an N_Integer_Literal node
4481 -- for the result of the compile time evaluation of the expression.
4482 -- For ASIS use, set a link to the original named number when not in
4483 -- a generic context.
4484
4485 if Is_Integer_Type (Typ) then
4486 Rewrite (N, Make_Integer_Literal (Loc, Val));
4487 Set_Original_Entity (N, Ent);
4488
4489 -- Otherwise we have an enumeration type, and we substitute either
4490 -- an N_Identifier or N_Character_Literal to represent the enumeration
4491 -- literal corresponding to the given value, which must always be in
4492 -- range, because appropriate tests have already been made for this.
4493
4494 else pragma Assert (Is_Enumeration_Type (Typ));
4495 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4496 end if;
4497
4498 -- We now have the literal with the right value, both the actual type
4499 -- and the expected type of this literal are taken from the expression
4500 -- that was evaluated. So now we do the Analyze and Resolve.
4501
4502 -- Note that we have to reset Is_Static_Expression both after the
4503 -- analyze step (because Resolve will evaluate the literal, which
4504 -- will cause semantic errors if it is marked as static), and after
4505 -- the Resolve step (since Resolve in some cases sets this flag).
4506
4507 Analyze (N);
4508 Set_Is_Static_Expression (N, Static);
4509 Set_Etype (N, Typ);
4510 Resolve (N);
4511 Set_Is_Static_Expression (N, Static);
4512 end Fold_Uint;
4513
4514 ----------------
4515 -- Fold_Ureal --
4516 ----------------
4517
4518 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4519 Loc : constant Source_Ptr := Sloc (N);
4520 Typ : constant Entity_Id := Etype (N);
4521 Ent : Entity_Id;
4522
4523 begin
4524 if Raises_Constraint_Error (N) then
4525 Set_Is_Static_Expression (N, Static);
4526 return;
4527 end if;
4528
4529 -- If we are folding a named number, retain the entity in the literal,
4530 -- for ASIS use.
4531
4532 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
4533 Ent := Entity (N);
4534 else
4535 Ent := Empty;
4536 end if;
4537
4538 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4539
4540 -- Set link to original named number, for ASIS use
4541
4542 Set_Original_Entity (N, Ent);
4543
4544 -- We now have the literal with the right value, both the actual type
4545 -- and the expected type of this literal are taken from the expression
4546 -- that was evaluated. So now we do the Analyze and Resolve.
4547
4548 -- Note that we have to reset Is_Static_Expression both after the
4549 -- analyze step (because Resolve will evaluate the literal, which
4550 -- will cause semantic errors if it is marked as static), and after
4551 -- the Resolve step (since Resolve in some cases sets this flag).
4552
4553 Analyze (N);
4554 Set_Is_Static_Expression (N, Static);
4555 Set_Etype (N, Typ);
4556 Resolve (N);
4557 Set_Is_Static_Expression (N, Static);
4558 end Fold_Ureal;
4559
4560 ---------------
4561 -- From_Bits --
4562 ---------------
4563
4564 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4565 V : Uint := Uint_0;
4566
4567 begin
4568 for J in 0 .. B'Last loop
4569 if B (J) then
4570 V := V + 2 ** J;
4571 end if;
4572 end loop;
4573
4574 if Non_Binary_Modulus (T) then
4575 V := V mod Modulus (T);
4576 end if;
4577
4578 return V;
4579 end From_Bits;
4580
4581 --------------------
4582 -- Get_String_Val --
4583 --------------------
4584
4585 function Get_String_Val (N : Node_Id) return Node_Id is
4586 begin
4587 if Nkind_In (N, N_String_Literal, N_Character_Literal) then
4588 return N;
4589 else
4590 pragma Assert (Is_Entity_Name (N));
4591 return Get_String_Val (Constant_Value (Entity (N)));
4592 end if;
4593 end Get_String_Val;
4594
4595 ----------------
4596 -- Initialize --
4597 ----------------
4598
4599 procedure Initialize is
4600 begin
4601 CV_Cache := (others => (Node_High_Bound, Uint_0));
4602 end Initialize;
4603
4604 --------------------
4605 -- In_Subrange_Of --
4606 --------------------
4607
4608 function In_Subrange_Of
4609 (T1 : Entity_Id;
4610 T2 : Entity_Id;
4611 Fixed_Int : Boolean := False) return Boolean
4612 is
4613 L1 : Node_Id;
4614 H1 : Node_Id;
4615
4616 L2 : Node_Id;
4617 H2 : Node_Id;
4618
4619 begin
4620 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4621 return True;
4622
4623 -- Never in range if both types are not scalar. Don't know if this can
4624 -- actually happen, but just in case.
4625
4626 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4627 return False;
4628
4629 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4630 -- definitely not compatible with T2.
4631
4632 elsif Is_Floating_Point_Type (T1)
4633 and then Has_Infinities (T1)
4634 and then Is_Floating_Point_Type (T2)
4635 and then not Has_Infinities (T2)
4636 then
4637 return False;
4638
4639 else
4640 L1 := Type_Low_Bound (T1);
4641 H1 := Type_High_Bound (T1);
4642
4643 L2 := Type_Low_Bound (T2);
4644 H2 := Type_High_Bound (T2);
4645
4646 -- Check bounds to see if comparison possible at compile time
4647
4648 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4649 and then
4650 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4651 then
4652 return True;
4653 end if;
4654
4655 -- If bounds not comparable at compile time, then the bounds of T2
4656 -- must be compile time known or we cannot answer the query.
4657
4658 if not Compile_Time_Known_Value (L2)
4659 or else not Compile_Time_Known_Value (H2)
4660 then
4661 return False;
4662 end if;
4663
4664 -- If the bounds of T1 are know at compile time then use these
4665 -- ones, otherwise use the bounds of the base type (which are of
4666 -- course always static).
4667
4668 if not Compile_Time_Known_Value (L1) then
4669 L1 := Type_Low_Bound (Base_Type (T1));
4670 end if;
4671
4672 if not Compile_Time_Known_Value (H1) then
4673 H1 := Type_High_Bound (Base_Type (T1));
4674 end if;
4675
4676 -- Fixed point types should be considered as such only if
4677 -- flag Fixed_Int is set to False.
4678
4679 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4680 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4681 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4682 then
4683 return
4684 Expr_Value_R (L2) <= Expr_Value_R (L1)
4685 and then
4686 Expr_Value_R (H2) >= Expr_Value_R (H1);
4687
4688 else
4689 return
4690 Expr_Value (L2) <= Expr_Value (L1)
4691 and then
4692 Expr_Value (H2) >= Expr_Value (H1);
4693
4694 end if;
4695 end if;
4696
4697 -- If any exception occurs, it means that we have some bug in the compiler
4698 -- possibly triggered by a previous error, or by some unforeseen peculiar
4699 -- occurrence. However, this is only an optimization attempt, so there is
4700 -- really no point in crashing the compiler. Instead we just decide, too
4701 -- bad, we can't figure out the answer in this case after all.
4702
4703 exception
4704 when others =>
4705
4706 -- Debug flag K disables this behavior (useful for debugging)
4707
4708 if Debug_Flag_K then
4709 raise;
4710 else
4711 return False;
4712 end if;
4713 end In_Subrange_Of;
4714
4715 -----------------
4716 -- Is_In_Range --
4717 -----------------
4718
4719 function Is_In_Range
4720 (N : Node_Id;
4721 Typ : Entity_Id;
4722 Assume_Valid : Boolean := False;
4723 Fixed_Int : Boolean := False;
4724 Int_Real : Boolean := False) return Boolean
4725 is
4726 begin
4727 return
4728 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
4729 end Is_In_Range;
4730
4731 -------------------
4732 -- Is_Null_Range --
4733 -------------------
4734
4735 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4736 Typ : constant Entity_Id := Etype (Lo);
4737
4738 begin
4739 if not Compile_Time_Known_Value (Lo)
4740 or else not Compile_Time_Known_Value (Hi)
4741 then
4742 return False;
4743 end if;
4744
4745 if Is_Discrete_Type (Typ) then
4746 return Expr_Value (Lo) > Expr_Value (Hi);
4747 else pragma Assert (Is_Real_Type (Typ));
4748 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4749 end if;
4750 end Is_Null_Range;
4751
4752 -------------------------
4753 -- Is_OK_Static_Choice --
4754 -------------------------
4755
4756 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
4757 begin
4758 -- Check various possibilities for choice
4759
4760 -- Note: for membership tests, we test more cases than are possible
4761 -- (in particular subtype indication), but it doesn't matter because
4762 -- it just won't occur (we have already done a syntax check).
4763
4764 if Nkind (Choice) = N_Others_Choice then
4765 return True;
4766
4767 elsif Nkind (Choice) = N_Range then
4768 return Is_OK_Static_Range (Choice);
4769
4770 elsif Nkind (Choice) = N_Subtype_Indication
4771 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4772 then
4773 return Is_OK_Static_Subtype (Etype (Choice));
4774
4775 else
4776 return Is_OK_Static_Expression (Choice);
4777 end if;
4778 end Is_OK_Static_Choice;
4779
4780 ------------------------------
4781 -- Is_OK_Static_Choice_List --
4782 ------------------------------
4783
4784 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
4785 Choice : Node_Id;
4786
4787 begin
4788 if not Is_Static_Choice_List (Choices) then
4789 return False;
4790 end if;
4791
4792 Choice := First (Choices);
4793 while Present (Choice) loop
4794 if not Is_OK_Static_Choice (Choice) then
4795 Set_Raises_Constraint_Error (Choice);
4796 return False;
4797 end if;
4798
4799 Next (Choice);
4800 end loop;
4801
4802 return True;
4803 end Is_OK_Static_Choice_List;
4804
4805 -----------------------------
4806 -- Is_OK_Static_Expression --
4807 -----------------------------
4808
4809 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4810 begin
4811 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
4812 end Is_OK_Static_Expression;
4813
4814 ------------------------
4815 -- Is_OK_Static_Range --
4816 ------------------------
4817
4818 -- A static range is a range whose bounds are static expressions, or a
4819 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4820 -- We have already converted range attribute references, so we get the
4821 -- "or" part of this rule without needing a special test.
4822
4823 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4824 begin
4825 return Is_OK_Static_Expression (Low_Bound (N))
4826 and then Is_OK_Static_Expression (High_Bound (N));
4827 end Is_OK_Static_Range;
4828
4829 --------------------------
4830 -- Is_OK_Static_Subtype --
4831 --------------------------
4832
4833 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4834 -- neither bound raises constraint error when evaluated.
4835
4836 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4837 Base_T : constant Entity_Id := Base_Type (Typ);
4838 Anc_Subt : Entity_Id;
4839
4840 begin
4841 -- First a quick check on the non static subtype flag. As described
4842 -- in further detail in Einfo, this flag is not decisive in all cases,
4843 -- but if it is set, then the subtype is definitely non-static.
4844
4845 if Is_Non_Static_Subtype (Typ) then
4846 return False;
4847 end if;
4848
4849 Anc_Subt := Ancestor_Subtype (Typ);
4850
4851 if Anc_Subt = Empty then
4852 Anc_Subt := Base_T;
4853 end if;
4854
4855 if Is_Generic_Type (Root_Type (Base_T))
4856 or else Is_Generic_Actual_Type (Base_T)
4857 then
4858 return False;
4859
4860 elsif Has_Dynamic_Predicate_Aspect (Typ) then
4861 return False;
4862
4863 -- String types
4864
4865 elsif Is_String_Type (Typ) then
4866 return
4867 Ekind (Typ) = E_String_Literal_Subtype
4868 or else
4869 (Is_OK_Static_Subtype (Component_Type (Typ))
4870 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4871
4872 -- Scalar types
4873
4874 elsif Is_Scalar_Type (Typ) then
4875 if Base_T = Typ then
4876 return True;
4877
4878 else
4879 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4880 -- Get_Type_{Low,High}_Bound.
4881
4882 return Is_OK_Static_Subtype (Anc_Subt)
4883 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4884 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4885 end if;
4886
4887 -- Types other than string and scalar types are never static
4888
4889 else
4890 return False;
4891 end if;
4892 end Is_OK_Static_Subtype;
4893
4894 ---------------------
4895 -- Is_Out_Of_Range --
4896 ---------------------
4897
4898 function Is_Out_Of_Range
4899 (N : Node_Id;
4900 Typ : Entity_Id;
4901 Assume_Valid : Boolean := False;
4902 Fixed_Int : Boolean := False;
4903 Int_Real : Boolean := False) return Boolean
4904 is
4905 begin
4906 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
4907 Out_Of_Range;
4908 end Is_Out_Of_Range;
4909
4910 ----------------------
4911 -- Is_Static_Choice --
4912 ----------------------
4913
4914 function Is_Static_Choice (Choice : Node_Id) return Boolean is
4915 begin
4916 -- Check various possibilities for choice
4917
4918 -- Note: for membership tests, we test more cases than are possible
4919 -- (in particular subtype indication), but it doesn't matter because
4920 -- it just won't occur (we have already done a syntax check).
4921
4922 if Nkind (Choice) = N_Others_Choice then
4923 return True;
4924
4925 elsif Nkind (Choice) = N_Range then
4926 return Is_Static_Range (Choice);
4927
4928 elsif Nkind (Choice) = N_Subtype_Indication
4929 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4930 then
4931 return Is_Static_Subtype (Etype (Choice));
4932
4933 else
4934 return Is_Static_Expression (Choice);
4935 end if;
4936 end Is_Static_Choice;
4937
4938 ---------------------------
4939 -- Is_Static_Choice_List --
4940 ---------------------------
4941
4942 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
4943 Choice : Node_Id;
4944
4945 begin
4946 Choice := First (Choices);
4947 while Present (Choice) loop
4948 if not Is_Static_Choice (Choice) then
4949 return False;
4950 end if;
4951
4952 Next (Choice);
4953 end loop;
4954
4955 return True;
4956 end Is_Static_Choice_List;
4957
4958 ---------------------
4959 -- Is_Static_Range --
4960 ---------------------
4961
4962 -- A static range is a range whose bounds are static expressions, or a
4963 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4964 -- We have already converted range attribute references, so we get the
4965 -- "or" part of this rule without needing a special test.
4966
4967 function Is_Static_Range (N : Node_Id) return Boolean is
4968 begin
4969 return Is_Static_Expression (Low_Bound (N))
4970 and then
4971 Is_Static_Expression (High_Bound (N));
4972 end Is_Static_Range;
4973
4974 -----------------------
4975 -- Is_Static_Subtype --
4976 -----------------------
4977
4978 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4979
4980 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
4981 Base_T : constant Entity_Id := Base_Type (Typ);
4982 Anc_Subt : Entity_Id;
4983
4984 begin
4985 -- First a quick check on the non static subtype flag. As described
4986 -- in further detail in Einfo, this flag is not decisive in all cases,
4987 -- but if it is set, then the subtype is definitely non-static.
4988
4989 if Is_Non_Static_Subtype (Typ) then
4990 return False;
4991 end if;
4992
4993 Anc_Subt := Ancestor_Subtype (Typ);
4994
4995 if Anc_Subt = Empty then
4996 Anc_Subt := Base_T;
4997 end if;
4998
4999 if Is_Generic_Type (Root_Type (Base_T))
5000 or else Is_Generic_Actual_Type (Base_T)
5001 then
5002 return False;
5003
5004 -- If there is a dynamic predicate for the type (declared or inherited)
5005 -- the expression is not static.
5006
5007 elsif Has_Dynamic_Predicate_Aspect (Typ)
5008 or else (Is_Derived_Type (Typ)
5009 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5010 then
5011 return False;
5012
5013 -- String types
5014
5015 elsif Is_String_Type (Typ) then
5016 return
5017 Ekind (Typ) = E_String_Literal_Subtype
5018 or else (Is_Static_Subtype (Component_Type (Typ))
5019 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5020
5021 -- Scalar types
5022
5023 elsif Is_Scalar_Type (Typ) then
5024 if Base_T = Typ then
5025 return True;
5026
5027 else
5028 return Is_Static_Subtype (Anc_Subt)
5029 and then Is_Static_Expression (Type_Low_Bound (Typ))
5030 and then Is_Static_Expression (Type_High_Bound (Typ));
5031 end if;
5032
5033 -- Types other than string and scalar types are never static
5034
5035 else
5036 return False;
5037 end if;
5038 end Is_Static_Subtype;
5039
5040 -------------------------------
5041 -- Is_Statically_Unevaluated --
5042 -------------------------------
5043
5044 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5045 function Check_Case_Expr_Alternative
5046 (CEA : Node_Id) return Match_Result;
5047 -- We have a message emanating from the Expression of a case expression
5048 -- alternative. We examine this alternative, as follows:
5049 --
5050 -- If the selecting expression of the parent case is non-static, or
5051 -- if any of the discrete choices of the given case alternative are
5052 -- non-static or raise Constraint_Error, return Non_Static.
5053 --
5054 -- Otherwise check if the selecting expression matches any of the given
5055 -- discrete choices. If so, the alternative is executed and we return
5056 -- Match, otherwise, the alternative can never be executed, and so we
5057 -- return No_Match.
5058
5059 ---------------------------------
5060 -- Check_Case_Expr_Alternative --
5061 ---------------------------------
5062
5063 function Check_Case_Expr_Alternative
5064 (CEA : Node_Id) return Match_Result
5065 is
5066 Case_Exp : constant Node_Id := Parent (CEA);
5067 Choice : Node_Id;
5068 Prev_CEA : Node_Id;
5069
5070 begin
5071 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5072
5073 -- Check that selecting expression is static
5074
5075 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5076 return Non_Static;
5077 end if;
5078
5079 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5080 return Non_Static;
5081 end if;
5082
5083 -- All choices are now known to be static. Now see if alternative
5084 -- matches one of the choices.
5085
5086 Choice := First (Discrete_Choices (CEA));
5087 while Present (Choice) loop
5088
5089 -- Check various possibilities for choice, returning Match if we
5090 -- find the selecting value matches any of the choices. Note that
5091 -- we know we are the last choice, so we don't have to keep going.
5092
5093 if Nkind (Choice) = N_Others_Choice then
5094
5095 -- Others choice is a bit annoying, it matches if none of the
5096 -- previous alternatives matches (note that we know we are the
5097 -- last alternative in this case, so we can just go backwards
5098 -- from us to see if any previous one matches).
5099
5100 Prev_CEA := Prev (CEA);
5101 while Present (Prev_CEA) loop
5102 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5103 return No_Match;
5104 end if;
5105
5106 Prev (Prev_CEA);
5107 end loop;
5108
5109 return Match;
5110
5111 -- Else we have a normal static choice
5112
5113 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5114 return Match;
5115 end if;
5116
5117 -- If we fall through, it means that the discrete choice did not
5118 -- match the selecting expression, so continue.
5119
5120 Next (Choice);
5121 end loop;
5122
5123 -- If we get through that loop then all choices were static, and none
5124 -- of them matched the selecting expression. So return No_Match.
5125
5126 return No_Match;
5127 end Check_Case_Expr_Alternative;
5128
5129 -- Local variables
5130
5131 P : Node_Id;
5132 OldP : Node_Id;
5133 Choice : Node_Id;
5134
5135 -- Start of processing for Is_Statically_Unevaluated
5136
5137 begin
5138 -- The (32.x) references here are from RM section 4.9
5139
5140 -- (32.1) An expression is statically unevaluated if it is part of ...
5141
5142 -- This means we have to climb the tree looking for one of the cases
5143
5144 P := Expr;
5145 loop
5146 OldP := P;
5147 P := Parent (P);
5148
5149 -- (32.2) The right operand of a static short-circuit control form
5150 -- whose value is determined by its left operand.
5151
5152 -- AND THEN with False as left operand
5153
5154 if Nkind (P) = N_And_Then
5155 and then Compile_Time_Known_Value (Left_Opnd (P))
5156 and then Is_False (Expr_Value (Left_Opnd (P)))
5157 then
5158 return True;
5159
5160 -- OR ELSE with True as left operand
5161
5162 elsif Nkind (P) = N_Or_Else
5163 and then Compile_Time_Known_Value (Left_Opnd (P))
5164 and then Is_True (Expr_Value (Left_Opnd (P)))
5165 then
5166 return True;
5167
5168 -- (32.3) A dependent_expression of an if_expression whose associated
5169 -- condition is static and equals False.
5170
5171 elsif Nkind (P) = N_If_Expression then
5172 declare
5173 Cond : constant Node_Id := First (Expressions (P));
5174 Texp : constant Node_Id := Next (Cond);
5175 Fexp : constant Node_Id := Next (Texp);
5176
5177 begin
5178 if Compile_Time_Known_Value (Cond) then
5179
5180 -- Condition is True and we are in the right operand
5181
5182 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5183 return True;
5184
5185 -- Condition is False and we are in the left operand
5186
5187 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5188 return True;
5189 end if;
5190 end if;
5191 end;
5192
5193 -- (32.4) A condition or dependent_expression of an if_expression
5194 -- where the condition corresponding to at least one preceding
5195 -- dependent_expression of the if_expression is static and equals
5196 -- True.
5197
5198 -- This refers to cases like
5199
5200 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5201
5202 -- But we expand elsif's out anyway, so the above looks like:
5203
5204 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5205
5206 -- So for us this is caught by the above check for the 32.3 case.
5207
5208 -- (32.5) A dependent_expression of a case_expression whose
5209 -- selecting_expression is static and whose value is not covered
5210 -- by the corresponding discrete_choice_list.
5211
5212 elsif Nkind (P) = N_Case_Expression_Alternative then
5213
5214 -- First, we have to be in the expression to suppress messages.
5215 -- If we are within one of the choices, we want the message.
5216
5217 if OldP = Expression (P) then
5218
5219 -- Statically unevaluated if alternative does not match
5220
5221 if Check_Case_Expr_Alternative (P) = No_Match then
5222 return True;
5223 end if;
5224 end if;
5225
5226 -- (32.6) A choice_expression (or a simple_expression of a range
5227 -- that occurs as a membership_choice of a membership_choice_list)
5228 -- of a static membership test that is preceded in the enclosing
5229 -- membership_choice_list by another item whose individual
5230 -- membership test (see (RM 4.5.2)) statically yields True.
5231
5232 elsif Nkind (P) in N_Membership_Test then
5233
5234 -- Only possibly unevaluated if simple expression is static
5235
5236 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5237 null;
5238
5239 -- All members of the choice list must be static
5240
5241 elsif (Present (Right_Opnd (P))
5242 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5243 or else (Present (Alternatives (P))
5244 and then
5245 not Is_OK_Static_Choice_List (Alternatives (P)))
5246 then
5247 null;
5248
5249 -- If expression is the one and only alternative, then it is
5250 -- definitely not statically unevaluated, so we only have to
5251 -- test the case where there are alternatives present.
5252
5253 elsif Present (Alternatives (P)) then
5254
5255 -- Look for previous matching Choice
5256
5257 Choice := First (Alternatives (P));
5258 while Present (Choice) loop
5259
5260 -- If we reached us and no previous choices matched, this
5261 -- is not the case where we are statically unevaluated.
5262
5263 exit when OldP = Choice;
5264
5265 -- If a previous choice matches, then that is the case where
5266 -- we know our choice is statically unevaluated.
5267
5268 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5269 return True;
5270 end if;
5271
5272 Next (Choice);
5273 end loop;
5274
5275 -- If we fall through the loop, we were not one of the choices,
5276 -- we must have been the expression, so that is not covered by
5277 -- this rule, and we keep going.
5278
5279 null;
5280 end if;
5281 end if;
5282
5283 -- OK, not statically unevaluated at this level, see if we should
5284 -- keep climbing to look for a higher level reason.
5285
5286 -- Special case for component association in aggregates, where
5287 -- we want to keep climbing up to the parent aggregate.
5288
5289 if Nkind (P) = N_Component_Association
5290 and then Nkind (Parent (P)) = N_Aggregate
5291 then
5292 null;
5293
5294 -- All done if not still within subexpression
5295
5296 else
5297 exit when Nkind (P) not in N_Subexpr;
5298 end if;
5299 end loop;
5300
5301 -- If we fall through the loop, not one of the cases covered!
5302
5303 return False;
5304 end Is_Statically_Unevaluated;
5305
5306 --------------------
5307 -- Not_Null_Range --
5308 --------------------
5309
5310 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5311 Typ : constant Entity_Id := Etype (Lo);
5312
5313 begin
5314 if not Compile_Time_Known_Value (Lo)
5315 or else not Compile_Time_Known_Value (Hi)
5316 then
5317 return False;
5318 end if;
5319
5320 if Is_Discrete_Type (Typ) then
5321 return Expr_Value (Lo) <= Expr_Value (Hi);
5322 else pragma Assert (Is_Real_Type (Typ));
5323 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
5324 end if;
5325 end Not_Null_Range;
5326
5327 -------------
5328 -- OK_Bits --
5329 -------------
5330
5331 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
5332 begin
5333 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5334
5335 if Bits < 500_000 then
5336 return True;
5337
5338 -- Error if this maximum is exceeded
5339
5340 else
5341 Error_Msg_N ("static value too large, capacity exceeded", N);
5342 return False;
5343 end if;
5344 end OK_Bits;
5345
5346 ------------------
5347 -- Out_Of_Range --
5348 ------------------
5349
5350 procedure Out_Of_Range (N : Node_Id) is
5351 begin
5352 -- If we have the static expression case, then this is an illegality
5353 -- in Ada 95 mode, except that in an instance, we never generate an
5354 -- error (if the error is legitimate, it was already diagnosed in the
5355 -- template).
5356
5357 if Is_Static_Expression (N)
5358 and then not In_Instance
5359 and then not In_Inlined_Body
5360 and then Ada_Version >= Ada_95
5361 then
5362 -- No message if we are statically unevaluated
5363
5364 if Is_Statically_Unevaluated (N) then
5365 null;
5366
5367 -- The expression to compute the length of a packed array is attached
5368 -- to the array type itself, and deserves a separate message.
5369
5370 elsif Nkind (Parent (N)) = N_Defining_Identifier
5371 and then Is_Array_Type (Parent (N))
5372 and then Present (Packed_Array_Impl_Type (Parent (N)))
5373 and then Present (First_Rep_Item (Parent (N)))
5374 then
5375 Error_Msg_N
5376 ("length of packed array must not exceed Integer''Last",
5377 First_Rep_Item (Parent (N)));
5378 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
5379
5380 -- All cases except the special array case
5381
5382 else
5383 Apply_Compile_Time_Constraint_Error
5384 (N, "value not in range of}", CE_Range_Check_Failed);
5385 end if;
5386
5387 -- Here we generate a warning for the Ada 83 case, or when we are in an
5388 -- instance, or when we have a non-static expression case.
5389
5390 else
5391 Apply_Compile_Time_Constraint_Error
5392 (N, "value not in range of}??", CE_Range_Check_Failed);
5393 end if;
5394 end Out_Of_Range;
5395
5396 ----------------------
5397 -- Predicates_Match --
5398 ----------------------
5399
5400 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
5401 Pred1 : Node_Id;
5402 Pred2 : Node_Id;
5403
5404 begin
5405 if Ada_Version < Ada_2012 then
5406 return True;
5407
5408 -- Both types must have predicates or lack them
5409
5410 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
5411 return False;
5412
5413 -- Check matching predicates
5414
5415 else
5416 Pred1 :=
5417 Get_Rep_Item
5418 (T1, Name_Static_Predicate, Check_Parents => False);
5419 Pred2 :=
5420 Get_Rep_Item
5421 (T2, Name_Static_Predicate, Check_Parents => False);
5422
5423 -- Subtypes statically match if the predicate comes from the
5424 -- same declaration, which can only happen if one is a subtype
5425 -- of the other and has no explicit predicate.
5426
5427 -- Suppress warnings on order of actuals, which is otherwise
5428 -- triggered by one of the two calls below.
5429
5430 pragma Warnings (Off);
5431 return Pred1 = Pred2
5432 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
5433 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
5434 pragma Warnings (On);
5435 end if;
5436 end Predicates_Match;
5437
5438 ---------------------------------------------
5439 -- Real_Or_String_Static_Predicate_Matches --
5440 ---------------------------------------------
5441
5442 function Real_Or_String_Static_Predicate_Matches
5443 (Val : Node_Id;
5444 Typ : Entity_Id) return Boolean
5445 is
5446 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
5447 -- The predicate expression from the type
5448
5449 Pfun : constant Entity_Id := Predicate_Function (Typ);
5450 -- The entity for the predicate function
5451
5452 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
5453 -- The name of the formal of the predicate function. Occurrences of the
5454 -- type name in Expr have been rewritten as references to this formal,
5455 -- and it has a unique name, so we can identify references by this name.
5456
5457 Copy : Node_Id;
5458 -- Copy of the predicate function tree
5459
5460 function Process (N : Node_Id) return Traverse_Result;
5461 -- Function used to process nodes during the traversal in which we will
5462 -- find occurrences of the entity name, and replace such occurrences
5463 -- by a real literal with the value to be tested.
5464
5465 procedure Traverse is new Traverse_Proc (Process);
5466 -- The actual traversal procedure
5467
5468 -------------
5469 -- Process --
5470 -------------
5471
5472 function Process (N : Node_Id) return Traverse_Result is
5473 begin
5474 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
5475 declare
5476 Nod : constant Node_Id := New_Copy (Val);
5477 begin
5478 Set_Sloc (Nod, Sloc (N));
5479 Rewrite (N, Nod);
5480 return Skip;
5481 end;
5482
5483 -- The predicate function may contain string-comparison operations
5484 -- that have been converted into calls to run-time array-comparison
5485 -- routines. To evaluate the predicate statically, we recover the
5486 -- original comparison operation and replace the occurrence of the
5487 -- formal by the static string value. The actuals of the generated
5488 -- call are of the form X'Address.
5489
5490 elsif Nkind (N) in N_Op_Compare
5491 and then Nkind (Left_Opnd (N)) = N_Function_Call
5492 then
5493 declare
5494 C : constant Node_Id := Left_Opnd (N);
5495 F : constant Node_Id := First (Parameter_Associations (C));
5496 L : constant Node_Id := Prefix (F);
5497 R : constant Node_Id := Prefix (Next (F));
5498
5499 begin
5500 -- If an operand is an entity name, it is the formal of the
5501 -- predicate function, so replace it with the string value.
5502 -- It may be either operand in the call. The other operand
5503 -- is a static string from the original predicate.
5504
5505 if Is_Entity_Name (L) then
5506 Rewrite (Left_Opnd (N), New_Copy (Val));
5507 Rewrite (Right_Opnd (N), New_Copy (R));
5508
5509 else
5510 Rewrite (Left_Opnd (N), New_Copy (L));
5511 Rewrite (Right_Opnd (N), New_Copy (Val));
5512 end if;
5513
5514 return Skip;
5515 end;
5516
5517 else
5518 return OK;
5519 end if;
5520 end Process;
5521
5522 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5523
5524 begin
5525 -- First deal with special case of inherited predicate, where the
5526 -- predicate expression looks like:
5527
5528 -- xxPredicate (typ (Ent)) and then Expr
5529
5530 -- where Expr is the predicate expression for this level, and the
5531 -- left operand is the call to evaluate the inherited predicate.
5532
5533 if Nkind (Expr) = N_And_Then
5534 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
5535 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
5536 then
5537 -- OK we have the inherited case, so make a call to evaluate the
5538 -- inherited predicate. If that fails, so do we!
5539
5540 if not
5541 Real_Or_String_Static_Predicate_Matches
5542 (Val => Val,
5543 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
5544 then
5545 return False;
5546 end if;
5547
5548 -- Use the right operand for the continued processing
5549
5550 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
5551
5552 -- Case where call to predicate function appears on its own (this means
5553 -- that the predicate at this level is just inherited from the parent).
5554
5555 elsif Nkind (Expr) = N_Function_Call then
5556 declare
5557 Typ : constant Entity_Id :=
5558 Etype (First_Formal (Entity (Name (Expr))));
5559
5560 begin
5561 -- If the inherited predicate is dynamic, just ignore it. We can't
5562 -- go trying to evaluate a dynamic predicate as a static one!
5563
5564 if Has_Dynamic_Predicate_Aspect (Typ) then
5565 return True;
5566
5567 -- Otherwise inherited predicate is static, check for match
5568
5569 else
5570 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
5571 end if;
5572 end;
5573
5574 -- If not just an inherited predicate, copy whole expression
5575
5576 else
5577 Copy := Copy_Separate_Tree (Expr);
5578 end if;
5579
5580 -- Now we replace occurrences of the entity by the value
5581
5582 Traverse (Copy);
5583
5584 -- And analyze the resulting static expression to see if it is True
5585
5586 Analyze_And_Resolve (Copy, Standard_Boolean);
5587 return Is_True (Expr_Value (Copy));
5588 end Real_Or_String_Static_Predicate_Matches;
5589
5590 -------------------------
5591 -- Rewrite_In_Raise_CE --
5592 -------------------------
5593
5594 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
5595 Typ : constant Entity_Id := Etype (N);
5596 Stat : constant Boolean := Is_Static_Expression (N);
5597
5598 begin
5599 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5600 -- can just clear the condition if the reason is appropriate. We do
5601 -- not do this operation if the parent has a reason other than range
5602 -- check failed, because otherwise we would change the reason.
5603
5604 if Present (Parent (N))
5605 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
5606 and then Reason (Parent (N)) =
5607 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
5608 then
5609 Set_Condition (Parent (N), Empty);
5610
5611 -- Else build an explicit N_Raise_CE
5612
5613 else
5614 Rewrite (N,
5615 Make_Raise_Constraint_Error (Sloc (Exp),
5616 Reason => CE_Range_Check_Failed));
5617 Set_Raises_Constraint_Error (N);
5618 Set_Etype (N, Typ);
5619 end if;
5620
5621 -- Set proper flags in result
5622
5623 Set_Raises_Constraint_Error (N, True);
5624 Set_Is_Static_Expression (N, Stat);
5625 end Rewrite_In_Raise_CE;
5626
5627 ---------------------
5628 -- String_Type_Len --
5629 ---------------------
5630
5631 function String_Type_Len (Stype : Entity_Id) return Uint is
5632 NT : constant Entity_Id := Etype (First_Index (Stype));
5633 T : Entity_Id;
5634
5635 begin
5636 if Is_OK_Static_Subtype (NT) then
5637 T := NT;
5638 else
5639 T := Base_Type (NT);
5640 end if;
5641
5642 return Expr_Value (Type_High_Bound (T)) -
5643 Expr_Value (Type_Low_Bound (T)) + 1;
5644 end String_Type_Len;
5645
5646 ------------------------------------
5647 -- Subtypes_Statically_Compatible --
5648 ------------------------------------
5649
5650 function Subtypes_Statically_Compatible
5651 (T1 : Entity_Id;
5652 T2 : Entity_Id;
5653 Formal_Derived_Matching : Boolean := False) return Boolean
5654 is
5655 begin
5656 -- Scalar types
5657
5658 if Is_Scalar_Type (T1) then
5659
5660 -- Definitely compatible if we match
5661
5662 if Subtypes_Statically_Match (T1, T2) then
5663 return True;
5664
5665 -- If either subtype is nonstatic then they're not compatible
5666
5667 elsif not Is_OK_Static_Subtype (T1)
5668 or else
5669 not Is_OK_Static_Subtype (T2)
5670 then
5671 return False;
5672
5673 -- If either type has constraint error bounds, then consider that
5674 -- they match to avoid junk cascaded errors here.
5675
5676 elsif not Is_OK_Static_Subtype (T1)
5677 or else not Is_OK_Static_Subtype (T2)
5678 then
5679 return True;
5680
5681 -- Base types must match, but we don't check that (should we???) but
5682 -- we do at least check that both types are real, or both types are
5683 -- not real.
5684
5685 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
5686 return False;
5687
5688 -- Here we check the bounds
5689
5690 else
5691 declare
5692 LB1 : constant Node_Id := Type_Low_Bound (T1);
5693 HB1 : constant Node_Id := Type_High_Bound (T1);
5694 LB2 : constant Node_Id := Type_Low_Bound (T2);
5695 HB2 : constant Node_Id := Type_High_Bound (T2);
5696
5697 begin
5698 if Is_Real_Type (T1) then
5699 return
5700 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
5701 or else
5702 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
5703 and then
5704 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
5705
5706 else
5707 return
5708 (Expr_Value (LB1) > Expr_Value (HB1))
5709 or else
5710 (Expr_Value (LB2) <= Expr_Value (LB1)
5711 and then
5712 Expr_Value (HB1) <= Expr_Value (HB2));
5713 end if;
5714 end;
5715 end if;
5716
5717 -- Access types
5718
5719 elsif Is_Access_Type (T1) then
5720 return (not Is_Constrained (T2)
5721 or else (Subtypes_Statically_Match
5722 (Designated_Type (T1), Designated_Type (T2))))
5723 and then not (Can_Never_Be_Null (T2)
5724 and then not Can_Never_Be_Null (T1));
5725
5726 -- All other cases
5727
5728 else
5729 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
5730 or else Subtypes_Statically_Match (T1, T2, Formal_Derived_Matching);
5731 end if;
5732 end Subtypes_Statically_Compatible;
5733
5734 -------------------------------
5735 -- Subtypes_Statically_Match --
5736 -------------------------------
5737
5738 -- Subtypes statically match if they have statically matching constraints
5739 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5740 -- they are the same identical constraint, or if they are static and the
5741 -- values match (RM 4.9.1(1)).
5742
5743 -- In addition, in GNAT, the object size (Esize) values of the types must
5744 -- match if they are set (unless checking an actual for a formal derived
5745 -- type). The use of 'Object_Size can cause this to be false even if the
5746 -- types would otherwise match in the RM sense.
5747
5748 function Subtypes_Statically_Match
5749 (T1 : Entity_Id;
5750 T2 : Entity_Id;
5751 Formal_Derived_Matching : Boolean := False) return Boolean
5752 is
5753 begin
5754 -- A type always statically matches itself
5755
5756 if T1 = T2 then
5757 return True;
5758
5759 -- No match if sizes different (from use of 'Object_Size). This test
5760 -- is excluded if Formal_Derived_Matching is True, as the base types
5761 -- can be different in that case and typically have different sizes
5762 -- (and Esizes can be set when Frontend_Layout_On_Target is True).
5763
5764 elsif not Formal_Derived_Matching
5765 and then Known_Static_Esize (T1)
5766 and then Known_Static_Esize (T2)
5767 and then Esize (T1) /= Esize (T2)
5768 then
5769 return False;
5770
5771 -- No match if predicates do not match
5772
5773 elsif not Predicates_Match (T1, T2) then
5774 return False;
5775
5776 -- Scalar types
5777
5778 elsif Is_Scalar_Type (T1) then
5779
5780 -- Base types must be the same
5781
5782 if Base_Type (T1) /= Base_Type (T2) then
5783 return False;
5784 end if;
5785
5786 -- A constrained numeric subtype never matches an unconstrained
5787 -- subtype, i.e. both types must be constrained or unconstrained.
5788
5789 -- To understand the requirement for this test, see RM 4.9.1(1).
5790 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5791 -- a constrained subtype with constraint bounds matching the bounds
5792 -- of its corresponding unconstrained base type. In this situation,
5793 -- Integer and Integer'Base do not statically match, even though
5794 -- they have the same bounds.
5795
5796 -- We only apply this test to types in Standard and types that appear
5797 -- in user programs. That way, we do not have to be too careful about
5798 -- setting Is_Constrained right for Itypes.
5799
5800 if Is_Numeric_Type (T1)
5801 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5802 and then (Scope (T1) = Standard_Standard
5803 or else Comes_From_Source (T1))
5804 and then (Scope (T2) = Standard_Standard
5805 or else Comes_From_Source (T2))
5806 then
5807 return False;
5808
5809 -- A generic scalar type does not statically match its base type
5810 -- (AI-311). In this case we make sure that the formals, which are
5811 -- first subtypes of their bases, are constrained.
5812
5813 elsif Is_Generic_Type (T1)
5814 and then Is_Generic_Type (T2)
5815 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5816 then
5817 return False;
5818 end if;
5819
5820 -- If there was an error in either range, then just assume the types
5821 -- statically match to avoid further junk errors.
5822
5823 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
5824 or else Error_Posted (Scalar_Range (T1))
5825 or else Error_Posted (Scalar_Range (T2))
5826 then
5827 return True;
5828 end if;
5829
5830 -- Otherwise both types have bounds that can be compared
5831
5832 declare
5833 LB1 : constant Node_Id := Type_Low_Bound (T1);
5834 HB1 : constant Node_Id := Type_High_Bound (T1);
5835 LB2 : constant Node_Id := Type_Low_Bound (T2);
5836 HB2 : constant Node_Id := Type_High_Bound (T2);
5837
5838 begin
5839 -- If the bounds are the same tree node, then match (common case)
5840
5841 if LB1 = LB2 and then HB1 = HB2 then
5842 return True;
5843
5844 -- Otherwise bounds must be static and identical value
5845
5846 else
5847 if not Is_OK_Static_Subtype (T1)
5848 or else not Is_OK_Static_Subtype (T2)
5849 then
5850 return False;
5851
5852 -- If either type has constraint error bounds, then say that
5853 -- they match to avoid junk cascaded errors here.
5854
5855 elsif not Is_OK_Static_Subtype (T1)
5856 or else not Is_OK_Static_Subtype (T2)
5857 then
5858 return True;
5859
5860 elsif Is_Real_Type (T1) then
5861 return
5862 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
5863 and then
5864 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
5865
5866 else
5867 return
5868 Expr_Value (LB1) = Expr_Value (LB2)
5869 and then
5870 Expr_Value (HB1) = Expr_Value (HB2);
5871 end if;
5872 end if;
5873 end;
5874
5875 -- Type with discriminants
5876
5877 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
5878
5879 -- Because of view exchanges in multiple instantiations, conformance
5880 -- checking might try to match a partial view of a type with no
5881 -- discriminants with a full view that has defaulted discriminants.
5882 -- In such a case, use the discriminant constraint of the full view,
5883 -- which must exist because we know that the two subtypes have the
5884 -- same base type.
5885
5886 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
5887 -- A generic actual type is declared through a subtype declaration
5888 -- and may have an inconsistent indication of the presence of
5889 -- discriminants, so check the type it renames.
5890
5891 if Is_Generic_Actual_Type (T1)
5892 and then not Has_Discriminants (Etype (T1))
5893 and then not Has_Discriminants (T2)
5894 then
5895 return True;
5896
5897 elsif In_Instance then
5898 if Is_Private_Type (T2)
5899 and then Present (Full_View (T2))
5900 and then Has_Discriminants (Full_View (T2))
5901 then
5902 return Subtypes_Statically_Match (T1, Full_View (T2));
5903
5904 elsif Is_Private_Type (T1)
5905 and then Present (Full_View (T1))
5906 and then Has_Discriminants (Full_View (T1))
5907 then
5908 return Subtypes_Statically_Match (Full_View (T1), T2);
5909
5910 else
5911 return False;
5912 end if;
5913 else
5914 return False;
5915 end if;
5916 end if;
5917
5918 declare
5919 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
5920 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
5921
5922 DA1 : Elmt_Id;
5923 DA2 : Elmt_Id;
5924
5925 begin
5926 if DL1 = DL2 then
5927 return True;
5928 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
5929 return False;
5930 end if;
5931
5932 -- Now loop through the discriminant constraints
5933
5934 -- Note: the guard here seems necessary, since it is possible at
5935 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5936
5937 if Present (DL1) and then Present (DL2) then
5938 DA1 := First_Elmt (DL1);
5939 DA2 := First_Elmt (DL2);
5940 while Present (DA1) loop
5941 declare
5942 Expr1 : constant Node_Id := Node (DA1);
5943 Expr2 : constant Node_Id := Node (DA2);
5944
5945 begin
5946 if not Is_OK_Static_Expression (Expr1)
5947 or else not Is_OK_Static_Expression (Expr2)
5948 then
5949 return False;
5950
5951 -- If either expression raised a constraint error,
5952 -- consider the expressions as matching, since this
5953 -- helps to prevent cascading errors.
5954
5955 elsif Raises_Constraint_Error (Expr1)
5956 or else Raises_Constraint_Error (Expr2)
5957 then
5958 null;
5959
5960 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
5961 return False;
5962 end if;
5963 end;
5964
5965 Next_Elmt (DA1);
5966 Next_Elmt (DA2);
5967 end loop;
5968 end if;
5969 end;
5970
5971 return True;
5972
5973 -- A definite type does not match an indefinite or classwide type.
5974 -- However, a generic type with unknown discriminants may be
5975 -- instantiated with a type with no discriminants, and conformance
5976 -- checking on an inherited operation may compare the actual with the
5977 -- subtype that renames it in the instance.
5978
5979 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
5980 then
5981 return
5982 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
5983
5984 -- Array type
5985
5986 elsif Is_Array_Type (T1) then
5987
5988 -- If either subtype is unconstrained then both must be, and if both
5989 -- are unconstrained then no further checking is needed.
5990
5991 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
5992 return not (Is_Constrained (T1) or else Is_Constrained (T2));
5993 end if;
5994
5995 -- Both subtypes are constrained, so check that the index subtypes
5996 -- statically match.
5997
5998 declare
5999 Index1 : Node_Id := First_Index (T1);
6000 Index2 : Node_Id := First_Index (T2);
6001
6002 begin
6003 while Present (Index1) loop
6004 if not
6005 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6006 then
6007 return False;
6008 end if;
6009
6010 Next_Index (Index1);
6011 Next_Index (Index2);
6012 end loop;
6013
6014 return True;
6015 end;
6016
6017 elsif Is_Access_Type (T1) then
6018 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6019 return False;
6020
6021 elsif Ekind_In (T1, E_Access_Subprogram_Type,
6022 E_Anonymous_Access_Subprogram_Type)
6023 then
6024 return
6025 Subtype_Conformant
6026 (Designated_Type (T1),
6027 Designated_Type (T2));
6028 else
6029 return
6030 Subtypes_Statically_Match
6031 (Designated_Type (T1),
6032 Designated_Type (T2))
6033 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
6034 end if;
6035
6036 -- All other types definitely match
6037
6038 else
6039 return True;
6040 end if;
6041 end Subtypes_Statically_Match;
6042
6043 ----------
6044 -- Test --
6045 ----------
6046
6047 function Test (Cond : Boolean) return Uint is
6048 begin
6049 if Cond then
6050 return Uint_1;
6051 else
6052 return Uint_0;
6053 end if;
6054 end Test;
6055
6056 ---------------------
6057 -- Test_Comparison --
6058 ---------------------
6059
6060 procedure Test_Comparison
6061 (Op : Node_Id;
6062 Assume_Valid : Boolean;
6063 True_Result : out Boolean;
6064 False_Result : out Boolean)
6065 is
6066 Left : constant Node_Id := Left_Opnd (Op);
6067 Left_Typ : constant Entity_Id := Etype (Left);
6068 Orig_Op : constant Node_Id := Original_Node (Op);
6069
6070 procedure Replacement_Warning (Msg : String);
6071 -- Emit a warning on a comparison that can be replaced by '='
6072
6073 -------------------------
6074 -- Replacement_Warning --
6075 -------------------------
6076
6077 procedure Replacement_Warning (Msg : String) is
6078 begin
6079 if Constant_Condition_Warnings
6080 and then Comes_From_Source (Orig_Op)
6081 and then Is_Integer_Type (Left_Typ)
6082 and then not Error_Posted (Op)
6083 and then not Has_Warnings_Off (Left_Typ)
6084 and then not In_Instance
6085 then
6086 Error_Msg_N (Msg, Op);
6087 end if;
6088 end Replacement_Warning;
6089
6090 -- Local variables
6091
6092 Res : constant Compare_Result :=
6093 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
6094
6095 -- Start of processing for Test_Comparison
6096
6097 begin
6098 case N_Op_Compare (Nkind (Op)) is
6099 when N_Op_Eq =>
6100 True_Result := Res = EQ;
6101 False_Result := Res = LT or else Res = GT or else Res = NE;
6102
6103 when N_Op_Ge =>
6104 True_Result := Res in Compare_GE;
6105 False_Result := Res = LT;
6106
6107 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
6108 Replacement_Warning
6109 ("can never be greater than, could replace by ""'=""?c?");
6110 end if;
6111
6112 when N_Op_Gt =>
6113 True_Result := Res = GT;
6114 False_Result := Res in Compare_LE;
6115
6116 when N_Op_Le =>
6117 True_Result := Res in Compare_LE;
6118 False_Result := Res = GT;
6119
6120 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
6121 Replacement_Warning
6122 ("can never be less than, could replace by ""'=""?c?");
6123 end if;
6124
6125 when N_Op_Lt =>
6126 True_Result := Res = LT;
6127 False_Result := Res in Compare_GE;
6128
6129 when N_Op_Ne =>
6130 True_Result := Res = NE or else Res = GT or else Res = LT;
6131 False_Result := Res = EQ;
6132 end case;
6133 end Test_Comparison;
6134
6135 ---------------------------------
6136 -- Test_Expression_Is_Foldable --
6137 ---------------------------------
6138
6139 -- One operand case
6140
6141 procedure Test_Expression_Is_Foldable
6142 (N : Node_Id;
6143 Op1 : Node_Id;
6144 Stat : out Boolean;
6145 Fold : out Boolean)
6146 is
6147 begin
6148 Stat := False;
6149 Fold := False;
6150
6151 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6152 return;
6153 end if;
6154
6155 -- If operand is Any_Type, just propagate to result and do not
6156 -- try to fold, this prevents cascaded errors.
6157
6158 if Etype (Op1) = Any_Type then
6159 Set_Etype (N, Any_Type);
6160 return;
6161
6162 -- If operand raises constraint error, then replace node N with the
6163 -- raise constraint error node, and we are obviously not foldable.
6164 -- Note that this replacement inherits the Is_Static_Expression flag
6165 -- from the operand.
6166
6167 elsif Raises_Constraint_Error (Op1) then
6168 Rewrite_In_Raise_CE (N, Op1);
6169 return;
6170
6171 -- If the operand is not static, then the result is not static, and
6172 -- all we have to do is to check the operand since it is now known
6173 -- to appear in a non-static context.
6174
6175 elsif not Is_Static_Expression (Op1) then
6176 Check_Non_Static_Context (Op1);
6177 Fold := Compile_Time_Known_Value (Op1);
6178 return;
6179
6180 -- An expression of a formal modular type is not foldable because
6181 -- the modulus is unknown.
6182
6183 elsif Is_Modular_Integer_Type (Etype (Op1))
6184 and then Is_Generic_Type (Etype (Op1))
6185 then
6186 Check_Non_Static_Context (Op1);
6187 return;
6188
6189 -- Here we have the case of an operand whose type is OK, which is
6190 -- static, and which does not raise constraint error, we can fold.
6191
6192 else
6193 Set_Is_Static_Expression (N);
6194 Fold := True;
6195 Stat := True;
6196 end if;
6197 end Test_Expression_Is_Foldable;
6198
6199 -- Two operand case
6200
6201 procedure Test_Expression_Is_Foldable
6202 (N : Node_Id;
6203 Op1 : Node_Id;
6204 Op2 : Node_Id;
6205 Stat : out Boolean;
6206 Fold : out Boolean;
6207 CRT_Safe : Boolean := False)
6208 is
6209 Rstat : constant Boolean := Is_Static_Expression (Op1)
6210 and then
6211 Is_Static_Expression (Op2);
6212
6213 begin
6214 Stat := False;
6215 Fold := False;
6216
6217 -- Inhibit folding if -gnatd.f flag set
6218
6219 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6220 return;
6221 end if;
6222
6223 -- If either operand is Any_Type, just propagate to result and
6224 -- do not try to fold, this prevents cascaded errors.
6225
6226 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
6227 Set_Etype (N, Any_Type);
6228 return;
6229
6230 -- If left operand raises constraint error, then replace node N with the
6231 -- Raise_Constraint_Error node, and we are obviously not foldable.
6232 -- Is_Static_Expression is set from the two operands in the normal way,
6233 -- and we check the right operand if it is in a non-static context.
6234
6235 elsif Raises_Constraint_Error (Op1) then
6236 if not Rstat then
6237 Check_Non_Static_Context (Op2);
6238 end if;
6239
6240 Rewrite_In_Raise_CE (N, Op1);
6241 Set_Is_Static_Expression (N, Rstat);
6242 return;
6243
6244 -- Similar processing for the case of the right operand. Note that we
6245 -- don't use this routine for the short-circuit case, so we do not have
6246 -- to worry about that special case here.
6247
6248 elsif Raises_Constraint_Error (Op2) then
6249 if not Rstat then
6250 Check_Non_Static_Context (Op1);
6251 end if;
6252
6253 Rewrite_In_Raise_CE (N, Op2);
6254 Set_Is_Static_Expression (N, Rstat);
6255 return;
6256
6257 -- Exclude expressions of a generic modular type, as above
6258
6259 elsif Is_Modular_Integer_Type (Etype (Op1))
6260 and then Is_Generic_Type (Etype (Op1))
6261 then
6262 Check_Non_Static_Context (Op1);
6263 return;
6264
6265 -- If result is not static, then check non-static contexts on operands
6266 -- since one of them may be static and the other one may not be static.
6267
6268 elsif not Rstat then
6269 Check_Non_Static_Context (Op1);
6270 Check_Non_Static_Context (Op2);
6271
6272 if CRT_Safe then
6273 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
6274 and then CRT_Safe_Compile_Time_Known_Value (Op2);
6275 else
6276 Fold := Compile_Time_Known_Value (Op1)
6277 and then Compile_Time_Known_Value (Op2);
6278 end if;
6279
6280 return;
6281
6282 -- Else result is static and foldable. Both operands are static, and
6283 -- neither raises constraint error, so we can definitely fold.
6284
6285 else
6286 Set_Is_Static_Expression (N);
6287 Fold := True;
6288 Stat := True;
6289 return;
6290 end if;
6291 end Test_Expression_Is_Foldable;
6292
6293 -------------------
6294 -- Test_In_Range --
6295 -------------------
6296
6297 function Test_In_Range
6298 (N : Node_Id;
6299 Typ : Entity_Id;
6300 Assume_Valid : Boolean;
6301 Fixed_Int : Boolean;
6302 Int_Real : Boolean) return Range_Membership
6303 is
6304 Val : Uint;
6305 Valr : Ureal;
6306
6307 pragma Warnings (Off, Assume_Valid);
6308 -- For now Assume_Valid is unreferenced since the current implementation
6309 -- always returns Unknown if N is not a compile time known value, but we
6310 -- keep the parameter to allow for future enhancements in which we try
6311 -- to get the information in the variable case as well.
6312
6313 begin
6314 -- If an error was posted on expression, then return Unknown, we do not
6315 -- want cascaded errors based on some false analysis of a junk node.
6316
6317 if Error_Posted (N) then
6318 return Unknown;
6319
6320 -- Expression that raises constraint error is an odd case. We certainly
6321 -- do not want to consider it to be in range. It might make sense to
6322 -- consider it always out of range, but this causes incorrect error
6323 -- messages about static expressions out of range. So we just return
6324 -- Unknown, which is always safe.
6325
6326 elsif Raises_Constraint_Error (N) then
6327 return Unknown;
6328
6329 -- Universal types have no range limits, so always in range
6330
6331 elsif Typ = Universal_Integer or else Typ = Universal_Real then
6332 return In_Range;
6333
6334 -- Never known if not scalar type. Don't know if this can actually
6335 -- happen, but our spec allows it, so we must check.
6336
6337 elsif not Is_Scalar_Type (Typ) then
6338 return Unknown;
6339
6340 -- Never known if this is a generic type, since the bounds of generic
6341 -- types are junk. Note that if we only checked for static expressions
6342 -- (instead of compile time known values) below, we would not need this
6343 -- check, because values of a generic type can never be static, but they
6344 -- can be known at compile time.
6345
6346 elsif Is_Generic_Type (Typ) then
6347 return Unknown;
6348
6349 -- Case of a known compile time value, where we can check if it is in
6350 -- the bounds of the given type.
6351
6352 elsif Compile_Time_Known_Value (N) then
6353 declare
6354 Lo : Node_Id;
6355 Hi : Node_Id;
6356
6357 LB_Known : Boolean;
6358 HB_Known : Boolean;
6359
6360 begin
6361 Lo := Type_Low_Bound (Typ);
6362 Hi := Type_High_Bound (Typ);
6363
6364 LB_Known := Compile_Time_Known_Value (Lo);
6365 HB_Known := Compile_Time_Known_Value (Hi);
6366
6367 -- Fixed point types should be considered as such only if flag
6368 -- Fixed_Int is set to False.
6369
6370 if Is_Floating_Point_Type (Typ)
6371 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
6372 or else Int_Real
6373 then
6374 Valr := Expr_Value_R (N);
6375
6376 if LB_Known and HB_Known then
6377 if Valr >= Expr_Value_R (Lo)
6378 and then
6379 Valr <= Expr_Value_R (Hi)
6380 then
6381 return In_Range;
6382 else
6383 return Out_Of_Range;
6384 end if;
6385
6386 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
6387 or else
6388 (HB_Known and then Valr > Expr_Value_R (Hi))
6389 then
6390 return Out_Of_Range;
6391
6392 else
6393 return Unknown;
6394 end if;
6395
6396 else
6397 Val := Expr_Value (N);
6398
6399 if LB_Known and HB_Known then
6400 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
6401 then
6402 return In_Range;
6403 else
6404 return Out_Of_Range;
6405 end if;
6406
6407 elsif (LB_Known and then Val < Expr_Value (Lo))
6408 or else
6409 (HB_Known and then Val > Expr_Value (Hi))
6410 then
6411 return Out_Of_Range;
6412
6413 else
6414 return Unknown;
6415 end if;
6416 end if;
6417 end;
6418
6419 -- Here for value not known at compile time. Case of expression subtype
6420 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6421 -- In this case we know it is in range without knowing its value.
6422
6423 elsif Assume_Valid
6424 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
6425 then
6426 return In_Range;
6427
6428 -- Another special case. For signed integer types, if the target type
6429 -- has Is_Known_Valid set, and the source type does not have a larger
6430 -- size, then the source value must be in range. We exclude biased
6431 -- types, because they bizarrely can generate out of range values.
6432
6433 elsif Is_Signed_Integer_Type (Etype (N))
6434 and then Is_Known_Valid (Typ)
6435 and then Esize (Etype (N)) <= Esize (Typ)
6436 and then not Has_Biased_Representation (Etype (N))
6437 then
6438 return In_Range;
6439
6440 -- For all other cases, result is unknown
6441
6442 else
6443 return Unknown;
6444 end if;
6445 end Test_In_Range;
6446
6447 --------------
6448 -- To_Bits --
6449 --------------
6450
6451 procedure To_Bits (U : Uint; B : out Bits) is
6452 begin
6453 for J in 0 .. B'Last loop
6454 B (J) := (U / (2 ** J)) mod 2 /= 0;
6455 end loop;
6456 end To_Bits;
6457
6458 --------------------
6459 -- Why_Not_Static --
6460 --------------------
6461
6462 procedure Why_Not_Static (Expr : Node_Id) is
6463 N : constant Node_Id := Original_Node (Expr);
6464 Typ : Entity_Id;
6465 E : Entity_Id;
6466 Alt : Node_Id;
6467 Exp : Node_Id;
6468
6469 procedure Why_Not_Static_List (L : List_Id);
6470 -- A version that can be called on a list of expressions. Finds all
6471 -- non-static violations in any element of the list.
6472
6473 -------------------------
6474 -- Why_Not_Static_List --
6475 -------------------------
6476
6477 procedure Why_Not_Static_List (L : List_Id) is
6478 N : Node_Id;
6479 begin
6480 if Is_Non_Empty_List (L) then
6481 N := First (L);
6482 while Present (N) loop
6483 Why_Not_Static (N);
6484 Next (N);
6485 end loop;
6486 end if;
6487 end Why_Not_Static_List;
6488
6489 -- Start of processing for Why_Not_Static
6490
6491 begin
6492 -- Ignore call on error or empty node
6493
6494 if No (Expr) or else Nkind (Expr) = N_Error then
6495 return;
6496 end if;
6497
6498 -- Preprocessing for sub expressions
6499
6500 if Nkind (Expr) in N_Subexpr then
6501
6502 -- Nothing to do if expression is static
6503
6504 if Is_OK_Static_Expression (Expr) then
6505 return;
6506 end if;
6507
6508 -- Test for constraint error raised
6509
6510 if Raises_Constraint_Error (Expr) then
6511
6512 -- Special case membership to find out which piece to flag
6513
6514 if Nkind (N) in N_Membership_Test then
6515 if Raises_Constraint_Error (Left_Opnd (N)) then
6516 Why_Not_Static (Left_Opnd (N));
6517 return;
6518
6519 elsif Present (Right_Opnd (N))
6520 and then Raises_Constraint_Error (Right_Opnd (N))
6521 then
6522 Why_Not_Static (Right_Opnd (N));
6523 return;
6524
6525 else
6526 pragma Assert (Present (Alternatives (N)));
6527
6528 Alt := First (Alternatives (N));
6529 while Present (Alt) loop
6530 if Raises_Constraint_Error (Alt) then
6531 Why_Not_Static (Alt);
6532 return;
6533 else
6534 Next (Alt);
6535 end if;
6536 end loop;
6537 end if;
6538
6539 -- Special case a range to find out which bound to flag
6540
6541 elsif Nkind (N) = N_Range then
6542 if Raises_Constraint_Error (Low_Bound (N)) then
6543 Why_Not_Static (Low_Bound (N));
6544 return;
6545
6546 elsif Raises_Constraint_Error (High_Bound (N)) then
6547 Why_Not_Static (High_Bound (N));
6548 return;
6549 end if;
6550
6551 -- Special case attribute to see which part to flag
6552
6553 elsif Nkind (N) = N_Attribute_Reference then
6554 if Raises_Constraint_Error (Prefix (N)) then
6555 Why_Not_Static (Prefix (N));
6556 return;
6557 end if;
6558
6559 if Present (Expressions (N)) then
6560 Exp := First (Expressions (N));
6561 while Present (Exp) loop
6562 if Raises_Constraint_Error (Exp) then
6563 Why_Not_Static (Exp);
6564 return;
6565 end if;
6566
6567 Next (Exp);
6568 end loop;
6569 end if;
6570
6571 -- Special case a subtype name
6572
6573 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
6574 Error_Msg_NE
6575 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
6576 return;
6577 end if;
6578
6579 -- End of special cases
6580
6581 Error_Msg_N
6582 ("!expression raises exception, cannot be static (RM 4.9(34))",
6583 N);
6584 return;
6585 end if;
6586
6587 -- If no type, then something is pretty wrong, so ignore
6588
6589 Typ := Etype (Expr);
6590
6591 if No (Typ) then
6592 return;
6593 end if;
6594
6595 -- Type must be scalar or string type (but allow Bignum, since this
6596 -- is really a scalar type from our point of view in this diagnosis).
6597
6598 if not Is_Scalar_Type (Typ)
6599 and then not Is_String_Type (Typ)
6600 and then not Is_RTE (Typ, RE_Bignum)
6601 then
6602 Error_Msg_N
6603 ("!static expression must have scalar or string type " &
6604 "(RM 4.9(2))", N);
6605 return;
6606 end if;
6607 end if;
6608
6609 -- If we got through those checks, test particular node kind
6610
6611 case Nkind (N) is
6612
6613 -- Entity name
6614
6615 when N_Expanded_Name
6616 | N_Identifier
6617 | N_Operator_Symbol
6618 =>
6619 E := Entity (N);
6620
6621 if Is_Named_Number (E) then
6622 null;
6623
6624 elsif Ekind (E) = E_Constant then
6625
6626 -- One case we can give a metter message is when we have a
6627 -- string literal created by concatenating an aggregate with
6628 -- an others expression.
6629
6630 Entity_Case : declare
6631 CV : constant Node_Id := Constant_Value (E);
6632 CO : constant Node_Id := Original_Node (CV);
6633
6634 function Is_Aggregate (N : Node_Id) return Boolean;
6635 -- See if node N came from an others aggregate, if so
6636 -- return True and set Error_Msg_Sloc to aggregate.
6637
6638 ------------------
6639 -- Is_Aggregate --
6640 ------------------
6641
6642 function Is_Aggregate (N : Node_Id) return Boolean is
6643 begin
6644 if Nkind (Original_Node (N)) = N_Aggregate then
6645 Error_Msg_Sloc := Sloc (Original_Node (N));
6646 return True;
6647
6648 elsif Is_Entity_Name (N)
6649 and then Ekind (Entity (N)) = E_Constant
6650 and then
6651 Nkind (Original_Node (Constant_Value (Entity (N)))) =
6652 N_Aggregate
6653 then
6654 Error_Msg_Sloc :=
6655 Sloc (Original_Node (Constant_Value (Entity (N))));
6656 return True;
6657
6658 else
6659 return False;
6660 end if;
6661 end Is_Aggregate;
6662
6663 -- Start of processing for Entity_Case
6664
6665 begin
6666 if Is_Aggregate (CV)
6667 or else (Nkind (CO) = N_Op_Concat
6668 and then (Is_Aggregate (Left_Opnd (CO))
6669 or else
6670 Is_Aggregate (Right_Opnd (CO))))
6671 then
6672 Error_Msg_N ("!aggregate (#) is never static", N);
6673
6674 elsif No (CV) or else not Is_Static_Expression (CV) then
6675 Error_Msg_NE
6676 ("!& is not a static constant (RM 4.9(5))", N, E);
6677 end if;
6678 end Entity_Case;
6679
6680 elsif Is_Type (E) then
6681 Error_Msg_NE
6682 ("!& is not a static subtype (RM 4.9(26))", N, E);
6683
6684 else
6685 Error_Msg_NE
6686 ("!& is not static constant or named number "
6687 & "(RM 4.9(5))", N, E);
6688 end if;
6689
6690 -- Binary operator
6691
6692 when N_Binary_Op
6693 | N_Membership_Test
6694 | N_Short_Circuit
6695 =>
6696 if Nkind (N) in N_Op_Shift then
6697 Error_Msg_N
6698 ("!shift functions are never static (RM 4.9(6,18))", N);
6699 else
6700 Why_Not_Static (Left_Opnd (N));
6701 Why_Not_Static (Right_Opnd (N));
6702 end if;
6703
6704 -- Unary operator
6705
6706 when N_Unary_Op =>
6707 Why_Not_Static (Right_Opnd (N));
6708
6709 -- Attribute reference
6710
6711 when N_Attribute_Reference =>
6712 Why_Not_Static_List (Expressions (N));
6713
6714 E := Etype (Prefix (N));
6715
6716 if E = Standard_Void_Type then
6717 return;
6718 end if;
6719
6720 -- Special case non-scalar'Size since this is a common error
6721
6722 if Attribute_Name (N) = Name_Size then
6723 Error_Msg_N
6724 ("!size attribute is only static for static scalar type "
6725 & "(RM 4.9(7,8))", N);
6726
6727 -- Flag array cases
6728
6729 elsif Is_Array_Type (E) then
6730 if not Nam_In (Attribute_Name (N), Name_First,
6731 Name_Last,
6732 Name_Length)
6733 then
6734 Error_Msg_N
6735 ("!static array attribute must be Length, First, or Last "
6736 & "(RM 4.9(8))", N);
6737
6738 -- Since we know the expression is not-static (we already
6739 -- tested for this, must mean array is not static).
6740
6741 else
6742 Error_Msg_N
6743 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
6744 end if;
6745
6746 return;
6747
6748 -- Special case generic types, since again this is a common source
6749 -- of confusion.
6750
6751 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
6752 Error_Msg_N
6753 ("!attribute of generic type is never static "
6754 & "(RM 4.9(7,8))", N);
6755
6756 elsif Is_OK_Static_Subtype (E) then
6757 null;
6758
6759 elsif Is_Scalar_Type (E) then
6760 Error_Msg_N
6761 ("!prefix type for attribute is not static scalar subtype "
6762 & "(RM 4.9(7))", N);
6763
6764 else
6765 Error_Msg_N
6766 ("!static attribute must apply to array/scalar type "
6767 & "(RM 4.9(7,8))", N);
6768 end if;
6769
6770 -- String literal
6771
6772 when N_String_Literal =>
6773 Error_Msg_N
6774 ("!subtype of string literal is non-static (RM 4.9(4))", N);
6775
6776 -- Explicit dereference
6777
6778 when N_Explicit_Dereference =>
6779 Error_Msg_N
6780 ("!explicit dereference is never static (RM 4.9)", N);
6781
6782 -- Function call
6783
6784 when N_Function_Call =>
6785 Why_Not_Static_List (Parameter_Associations (N));
6786
6787 -- Complain about non-static function call unless we have Bignum
6788 -- which means that the underlying expression is really some
6789 -- scalar arithmetic operation.
6790
6791 if not Is_RTE (Typ, RE_Bignum) then
6792 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
6793 end if;
6794
6795 -- Parameter assocation (test actual parameter)
6796
6797 when N_Parameter_Association =>
6798 Why_Not_Static (Explicit_Actual_Parameter (N));
6799
6800 -- Indexed component
6801
6802 when N_Indexed_Component =>
6803 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
6804
6805 -- Procedure call
6806
6807 when N_Procedure_Call_Statement =>
6808 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
6809
6810 -- Qualified expression (test expression)
6811
6812 when N_Qualified_Expression =>
6813 Why_Not_Static (Expression (N));
6814
6815 -- Aggregate
6816
6817 when N_Aggregate
6818 | N_Extension_Aggregate
6819 =>
6820 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
6821
6822 -- Range
6823
6824 when N_Range =>
6825 Why_Not_Static (Low_Bound (N));
6826 Why_Not_Static (High_Bound (N));
6827
6828 -- Range constraint, test range expression
6829
6830 when N_Range_Constraint =>
6831 Why_Not_Static (Range_Expression (N));
6832
6833 -- Subtype indication, test constraint
6834
6835 when N_Subtype_Indication =>
6836 Why_Not_Static (Constraint (N));
6837
6838 -- Selected component
6839
6840 when N_Selected_Component =>
6841 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
6842
6843 -- Slice
6844
6845 when N_Slice =>
6846 Error_Msg_N ("!slice is never static (RM 4.9)", N);
6847
6848 when N_Type_Conversion =>
6849 Why_Not_Static (Expression (N));
6850
6851 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
6852 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
6853 then
6854 Error_Msg_N
6855 ("!static conversion requires static scalar subtype result "
6856 & "(RM 4.9(9))", N);
6857 end if;
6858
6859 -- Unchecked type conversion
6860
6861 when N_Unchecked_Type_Conversion =>
6862 Error_Msg_N
6863 ("!unchecked type conversion is never static (RM 4.9)", N);
6864
6865 -- All other cases, no reason to give
6866
6867 when others =>
6868 null;
6869 end case;
6870 end Why_Not_Static;
6871
6872 end Sem_Eval;