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