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