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