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