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