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