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