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