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