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