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