]>
Commit | Line | Data |
---|---|---|
ee6ba406 | 1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- C H E C K S -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
612930c1 | 9 | -- Copyright (C) 1992-2018, Free Software Foundation, Inc. -- |
ee6ba406 | 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- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
ee6ba406 | 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 -- | |
80df182a | 18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
ee6ba406 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
ee6ba406 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
29448168 | 27 | with Casing; use Casing; |
ee6ba406 | 28 | with Debug; use Debug; |
29 | with Einfo; use Einfo; | |
7b8fa048 | 30 | with Elists; use Elists; |
31 | with Eval_Fat; use Eval_Fat; | |
32 | with Exp_Ch11; use Exp_Ch11; | |
ee6ba406 | 33 | with Exp_Ch2; use Exp_Ch2; |
df40eeb0 | 34 | with Exp_Ch4; use Exp_Ch4; |
05fcfafb | 35 | with Exp_Pakd; use Exp_Pakd; |
ee6ba406 | 36 | with Exp_Util; use Exp_Util; |
4fb5f0a0 | 37 | with Expander; use Expander; |
ee6ba406 | 38 | with Freeze; use Freeze; |
9dfe12ae | 39 | with Lib; use Lib; |
ee6ba406 | 40 | with Nlists; use Nlists; |
41 | with Nmake; use Nmake; | |
42 | with Opt; use Opt; | |
9dfe12ae | 43 | with Output; use Output; |
c2b56224 | 44 | with Restrict; use Restrict; |
1e16c51c | 45 | with Rident; use Rident; |
ee6ba406 | 46 | with Rtsfind; use Rtsfind; |
47 | with Sem; use Sem; | |
d60c9ff7 | 48 | with Sem_Aux; use Sem_Aux; |
00f91aef | 49 | with Sem_Ch3; use Sem_Ch3; |
9dfe12ae | 50 | with Sem_Ch8; use Sem_Ch8; |
7e933b61 | 51 | with Sem_Disp; use Sem_Disp; |
7b8fa048 | 52 | with Sem_Eval; use Sem_Eval; |
ee6ba406 | 53 | with Sem_Res; use Sem_Res; |
54 | with Sem_Util; use Sem_Util; | |
55 | with Sem_Warn; use Sem_Warn; | |
56 | with Sinfo; use Sinfo; | |
9dfe12ae | 57 | with Sinput; use Sinput; |
ee6ba406 | 58 | with Snames; use Snames; |
9dfe12ae | 59 | with Sprint; use Sprint; |
ee6ba406 | 60 | with Stand; use Stand; |
bb569db0 | 61 | with Stringt; use Stringt; |
f15731c4 | 62 | with Targparm; use Targparm; |
ee6ba406 | 63 | with Tbuild; use Tbuild; |
64 | with Ttypes; use Ttypes; | |
ee6ba406 | 65 | with Validsw; use Validsw; |
66 | ||
67 | package body Checks is | |
68 | ||
69 | -- General note: many of these routines are concerned with generating | |
70 | -- checking code to make sure that constraint error is raised at runtime. | |
71 | -- Clearly this code is only needed if the expander is active, since | |
72 | -- otherwise we will not be generating code or going into the runtime | |
73 | -- execution anyway. | |
74 | ||
75 | -- We therefore disconnect most of these checks if the expander is | |
76 | -- inactive. This has the additional benefit that we do not need to | |
77 | -- worry about the tree being messed up by previous errors (since errors | |
78 | -- turn off expansion anyway). | |
79 | ||
80 | -- There are a few exceptions to the above rule. For instance routines | |
81 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
82 | -- safely called even when the Expander is inactive (but Errors_Detected | |
83 | -- is 0). The benefit of executing this code when expansion is off, is | |
84 | -- the ability to emit constraint error warning for static expressions | |
85 | -- even when we are not generating code. | |
86 | ||
20cf157b | 87 | -- The above is modified in gnatprove mode to ensure that proper check |
88 | -- flags are always placed, even if expansion is off. | |
89 | ||
9dfe12ae | 90 | ------------------------------------- |
91 | -- Suppression of Redundant Checks -- | |
92 | ------------------------------------- | |
93 | ||
94 | -- This unit implements a limited circuit for removal of redundant | |
95 | -- checks. The processing is based on a tracing of simple sequential | |
96 | -- flow. For any sequence of statements, we save expressions that are | |
97 | -- marked to be checked, and then if the same expression appears later | |
98 | -- with the same check, then under certain circumstances, the second | |
99 | -- check can be suppressed. | |
100 | ||
101 | -- Basically, we can suppress the check if we know for certain that | |
102 | -- the previous expression has been elaborated (together with its | |
103 | -- check), and we know that the exception frame is the same, and that | |
104 | -- nothing has happened to change the result of the exception. | |
105 | ||
106 | -- Let us examine each of these three conditions in turn to describe | |
107 | -- how we ensure that this condition is met. | |
108 | ||
109 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 110 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 111 | -- Conditional_Statements_Begin at the start of any statement sequence |
112 | -- and Conditional_Statements_End at the end. The End call causes all | |
113 | -- checks remembered since the Begin call to be discarded. This does | |
114 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
115 | -- no exception handlers. But the important thing is to be conservative. | |
116 | -- The other protection is that all checks are discarded if a label | |
117 | -- is encountered, since then the assumption of sequential execution | |
118 | -- is violated, and we don't know enough about the flow. | |
119 | ||
120 | -- Second, we need to know that the exception frame is the same. We | |
121 | -- do this by killing all remembered checks when we enter a new frame. | |
122 | -- Again, that's over-conservative, but generally the cases we can help | |
123 | -- with are pretty local anyway (like the body of a loop for example). | |
124 | ||
125 | -- Third, we must be sure to forget any checks which are no longer valid. | |
126 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
127 | -- used to note any changes to local variables. We only attempt to deal | |
128 | -- with checks involving local variables, so we do not need to worry | |
129 | -- about global variables. Second, a call to any non-global procedure | |
130 | -- causes us to abandon all stored checks, since such a all may affect | |
131 | -- the values of any local variables. | |
132 | ||
133 | -- The following define the data structures used to deal with remembering | |
134 | -- checks so that redundant checks can be eliminated as described above. | |
135 | ||
136 | -- Right now, the only expressions that we deal with are of the form of | |
137 | -- simple local objects (either declared locally, or IN parameters) or | |
138 | -- such objects plus/minus a compile time known constant. We can do | |
139 | -- more later on if it seems worthwhile, but this catches many simple | |
140 | -- cases in practice. | |
141 | ||
142 | -- The following record type reflects a single saved check. An entry | |
143 | -- is made in the stack of saved checks if and only if the expression | |
144 | -- has been elaborated with the indicated checks. | |
145 | ||
146 | type Saved_Check is record | |
147 | Killed : Boolean; | |
148 | -- Set True if entry is killed by Kill_Checks | |
149 | ||
150 | Entity : Entity_Id; | |
151 | -- The entity involved in the expression that is checked | |
152 | ||
153 | Offset : Uint; | |
154 | -- A compile time value indicating the result of adding or | |
155 | -- subtracting a compile time value. This value is to be | |
156 | -- added to the value of the Entity. A value of zero is | |
157 | -- used for the case of a simple entity reference. | |
158 | ||
159 | Check_Type : Character; | |
160 | -- This is set to 'R' for a range check (in which case Target_Type | |
161 | -- is set to the target type for the range check) or to 'O' for an | |
162 | -- overflow check (in which case Target_Type is set to Empty). | |
163 | ||
164 | Target_Type : Entity_Id; | |
165 | -- Used only if Do_Range_Check is set. Records the target type for | |
166 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 167 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 168 | -- range that is smaller or equal to the stored target type of a |
169 | -- saved check). | |
170 | end record; | |
171 | ||
172 | -- The following table keeps track of saved checks. Rather than use an | |
bbbfe30c | 173 | -- extensible table, we just use a table of fixed size, and we discard |
9dfe12ae | 174 | -- any saved checks that do not fit. That's very unlikely to happen and |
175 | -- this is only an optimization in any case. | |
176 | ||
177 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
178 | -- Array of saved checks | |
179 | ||
180 | Num_Saved_Checks : Nat := 0; | |
181 | -- Number of saved checks | |
182 | ||
183 | -- The following stack keeps track of statement ranges. It is treated | |
184 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
185 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
186 | -- at the time of the call. Then when Conditional_Statements_End is | |
187 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
188 | ||
189 | -- Note: again, this is a fixed length stack with a size that should | |
190 | -- always be fine. If the value of the stack pointer goes above the | |
191 | -- limit, then we just forget all saved checks. | |
192 | ||
193 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
194 | Saved_Checks_TOS : Nat := 0; | |
195 | ||
196 | ----------------------- | |
197 | -- Local Subprograms -- | |
198 | ----------------------- | |
ee6ba406 | 199 | |
0df9d43f | 200 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 201 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 202 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 203 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
204 | -- signed integer arithmetic operator (but not an if or case expression). | |
205 | -- It is also called for types other than signed integers. | |
3cce7f32 | 206 | |
207 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
208 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 209 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
210 | -- arithmetic op (which includes the case of if and case expressions). Note | |
211 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
212 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 213 | |
2fe22c69 | 214 | procedure Apply_Division_Check |
215 | (N : Node_Id; | |
216 | Rlo : Uint; | |
217 | Rhi : Uint; | |
218 | ROK : Boolean); | |
219 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
220 | -- division checks as required if the Do_Division_Check flag is set. | |
221 | -- Rlo and Rhi give the possible range of the right operand, these values | |
222 | -- can be referenced and trusted only if ROK is set True. | |
223 | ||
224 | procedure Apply_Float_Conversion_Check | |
225 | (Ck_Node : Node_Id; | |
226 | Target_Typ : Entity_Id); | |
227 | -- The checks on a conversion from a floating-point type to an integer | |
228 | -- type are delicate. They have to be performed before conversion, they | |
229 | -- have to raise an exception when the operand is a NaN, and rounding must | |
230 | -- be taken into account to determine the safe bounds of the operand. | |
231 | ||
ee6ba406 | 232 | procedure Apply_Selected_Length_Checks |
233 | (Ck_Node : Node_Id; | |
234 | Target_Typ : Entity_Id; | |
235 | Source_Typ : Entity_Id; | |
236 | Do_Static : Boolean); | |
237 | -- This is the subprogram that does all the work for Apply_Length_Check | |
238 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
239 | -- described for the above routines. The Do_Static flag indicates that | |
240 | -- only a static check is to be done. | |
241 | ||
242 | procedure Apply_Selected_Range_Checks | |
243 | (Ck_Node : Node_Id; | |
244 | Target_Typ : Entity_Id; | |
245 | Source_Typ : Entity_Id; | |
246 | Do_Static : Boolean); | |
247 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
248 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
249 | -- routine. The Do_Static flag indicates that only a static check is | |
250 | -- to be done. | |
251 | ||
2af58f67 | 252 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 253 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
254 | -- This function is used to see if an access or division by zero check is | |
255 | -- needed. The check is to be applied to a single variable appearing in the | |
256 | -- source, and N is the node for the reference. If N is not of this form, | |
257 | -- True is returned with no further processing. If N is of the right form, | |
258 | -- then further processing determines if the given Check is needed. | |
259 | -- | |
260 | -- The particular circuit is to see if we have the case of a check that is | |
261 | -- not needed because it appears in the right operand of a short circuited | |
262 | -- conditional where the left operand guards the check. For example: | |
263 | -- | |
264 | -- if Var = 0 or else Q / Var > 12 then | |
265 | -- ... | |
266 | -- end if; | |
267 | -- | |
268 | -- In this example, the division check is not required. At the same time | |
269 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
270 | -- such as: | |
271 | -- | |
272 | -- if Var = 0 or Q / Var > 12 then | |
273 | -- ... | |
274 | -- end if; | |
275 | ||
9dfe12ae | 276 | procedure Find_Check |
277 | (Expr : Node_Id; | |
278 | Check_Type : Character; | |
279 | Target_Type : Entity_Id; | |
280 | Entry_OK : out Boolean; | |
281 | Check_Num : out Nat; | |
282 | Ent : out Entity_Id; | |
283 | Ofs : out Uint); | |
284 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
285 | -- to see if a check is of the form for optimization, and if so, to see | |
286 | -- if it has already been performed. Expr is the expression to check, | |
287 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
288 | -- Target_Type is the target type for a range check, and Empty for an | |
289 | -- overflow check. If the entry is not of the form for optimization, | |
290 | -- then Entry_OK is set to False, and the remaining out parameters | |
291 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
292 | -- entity and offset from the expression. Check_Num is the number of | |
293 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
294 | -- is located. | |
295 | ||
ee6ba406 | 296 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
297 | -- If a discriminal is used in constraining a prival, Return reference | |
298 | -- to the discriminal of the protected body (which renames the parameter | |
299 | -- of the enclosing protected operation). This clumsy transformation is | |
300 | -- needed because privals are created too late and their actual subtypes | |
301 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 302 | -- This function is called whenever the bound is an entity and the scope |
303 | -- indicates a protected operation. If the bound is an in-parameter of | |
304 | -- a protected operation that is not a prival, the function returns the | |
305 | -- bound itself. | |
ee6ba406 | 306 | -- To be cleaned up??? |
307 | ||
308 | function Guard_Access | |
309 | (Cond : Node_Id; | |
310 | Loc : Source_Ptr; | |
314a23b6 | 311 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 312 | -- In the access type case, guard the test with a test to ensure |
313 | -- that the access value is non-null, since the checks do not | |
314 | -- not apply to null access values. | |
315 | ||
316 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
317 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
318 | -- Constraint_Error node. | |
319 | ||
3cce7f32 | 320 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
321 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 322 | -- integer operands. This includes unary and binary operators, and also |
323 | -- if and case expression nodes where the dependent expressions are of | |
324 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 325 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 326 | |
0577b0b1 | 327 | function Range_Or_Validity_Checks_Suppressed |
328 | (Expr : Node_Id) return Boolean; | |
329 | -- Returns True if either range or validity checks or both are suppressed | |
330 | -- for the type of the given expression, or, if the expression is the name | |
331 | -- of an entity, if these checks are suppressed for the entity. | |
332 | ||
ee6ba406 | 333 | function Selected_Length_Checks |
334 | (Ck_Node : Node_Id; | |
335 | Target_Typ : Entity_Id; | |
336 | Source_Typ : Entity_Id; | |
314a23b6 | 337 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 338 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
339 | -- anything, just returns a list of nodes as described in the spec of | |
340 | -- this package for the Range_Check function. | |
18cb6d78 | 341 | -- ??? In fact it does construct the test and insert it into the tree, |
342 | -- and insert actions in various ways (calling Insert_Action directly | |
343 | -- in particular) so we do not call it in GNATprove mode, contrary to | |
344 | -- Selected_Range_Checks. | |
ee6ba406 | 345 | |
346 | function Selected_Range_Checks | |
347 | (Ck_Node : Node_Id; | |
348 | Target_Typ : Entity_Id; | |
349 | Source_Typ : Entity_Id; | |
314a23b6 | 350 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 351 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
352 | -- just returns a list of nodes as described in the spec of this package | |
353 | -- for the Range_Check function. | |
354 | ||
355 | ------------------------------ | |
356 | -- Access_Checks_Suppressed -- | |
357 | ------------------------------ | |
358 | ||
359 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
360 | begin | |
9dfe12ae | 361 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
362 | return Is_Check_Suppressed (E, Access_Check); | |
363 | else | |
fafc6b97 | 364 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 365 | end if; |
ee6ba406 | 366 | end Access_Checks_Suppressed; |
367 | ||
368 | ------------------------------------- | |
369 | -- Accessibility_Checks_Suppressed -- | |
370 | ------------------------------------- | |
371 | ||
372 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
373 | begin | |
9dfe12ae | 374 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
375 | return Is_Check_Suppressed (E, Accessibility_Check); | |
376 | else | |
fafc6b97 | 377 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 378 | end if; |
ee6ba406 | 379 | end Accessibility_Checks_Suppressed; |
380 | ||
00c403ee | 381 | ----------------------------- |
382 | -- Activate_Division_Check -- | |
383 | ----------------------------- | |
384 | ||
385 | procedure Activate_Division_Check (N : Node_Id) is | |
386 | begin | |
387 | Set_Do_Division_Check (N, True); | |
388 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
389 | end Activate_Division_Check; | |
390 | ||
391 | ----------------------------- | |
392 | -- Activate_Overflow_Check -- | |
393 | ----------------------------- | |
394 | ||
395 | procedure Activate_Overflow_Check (N : Node_Id) is | |
c8e92b5f | 396 | Typ : constant Entity_Id := Etype (N); |
397 | ||
00c403ee | 398 | begin |
c8e92b5f | 399 | -- Floating-point case. If Etype is not set (this can happen when we |
400 | -- activate a check on a node that has not yet been analyzed), then | |
401 | -- we assume we do not have a floating-point type (as per our spec). | |
402 | ||
403 | if Present (Typ) and then Is_Floating_Point_Type (Typ) then | |
404 | ||
405 | -- Ignore call if we have no automatic overflow checks on the target | |
406 | -- and Check_Float_Overflow mode is not set. These are the cases in | |
407 | -- which we expect to generate infinities and NaN's with no check. | |
408 | ||
409 | if not (Machine_Overflows_On_Target or Check_Float_Overflow) then | |
410 | return; | |
411 | ||
412 | -- Ignore for unary operations ("+", "-", abs) since these can never | |
413 | -- result in overflow for floating-point cases. | |
b8446e0d | 414 | |
c8e92b5f | 415 | elsif Nkind (N) in N_Unary_Op then |
416 | return; | |
417 | ||
418 | -- Otherwise we will set the flag | |
419 | ||
420 | else | |
421 | null; | |
422 | end if; | |
423 | ||
424 | -- Discrete case | |
425 | ||
426 | else | |
427 | -- Nothing to do for Rem/Mod/Plus (overflow not possible, the check | |
428 | -- for zero-divide is a divide check, not an overflow check). | |
b8446e0d | 429 | |
c8e92b5f | 430 | if Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then |
431 | return; | |
432 | end if; | |
b8446e0d | 433 | end if; |
434 | ||
c8e92b5f | 435 | -- Fall through for cases where we do set the flag |
b8446e0d | 436 | |
437 | Set_Do_Overflow_Check (N, True); | |
438 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
00c403ee | 439 | end Activate_Overflow_Check; |
440 | ||
441 | -------------------------- | |
442 | -- Activate_Range_Check -- | |
443 | -------------------------- | |
444 | ||
445 | procedure Activate_Range_Check (N : Node_Id) is | |
446 | begin | |
447 | Set_Do_Range_Check (N, True); | |
448 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
449 | end Activate_Range_Check; | |
450 | ||
0577b0b1 | 451 | --------------------------------- |
452 | -- Alignment_Checks_Suppressed -- | |
453 | --------------------------------- | |
454 | ||
455 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
456 | begin | |
457 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
458 | return Is_Check_Suppressed (E, Alignment_Check); | |
459 | else | |
fafc6b97 | 460 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 461 | end if; |
462 | end Alignment_Checks_Suppressed; | |
463 | ||
2d70530c | 464 | ---------------------------------- |
465 | -- Allocation_Checks_Suppressed -- | |
466 | ---------------------------------- | |
467 | ||
fa771c05 | 468 | -- Note: at the current time there are no calls to this function, because |
469 | -- the relevant check is in the run-time, so it is not a check that the | |
470 | -- compiler can suppress anyway, but we still have to recognize the check | |
471 | -- name Allocation_Check since it is part of the standard. | |
472 | ||
2d70530c | 473 | function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
474 | begin | |
475 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
476 | return Is_Check_Suppressed (E, Allocation_Check); | |
477 | else | |
478 | return Scope_Suppress.Suppress (Allocation_Check); | |
479 | end if; | |
480 | end Allocation_Checks_Suppressed; | |
481 | ||
ee6ba406 | 482 | ------------------------- |
483 | -- Append_Range_Checks -- | |
484 | ------------------------- | |
485 | ||
486 | procedure Append_Range_Checks | |
487 | (Checks : Check_Result; | |
488 | Stmts : List_Id; | |
489 | Suppress_Typ : Entity_Id; | |
490 | Static_Sloc : Source_Ptr; | |
491 | Flag_Node : Node_Id) | |
492 | is | |
2b4f2458 | 493 | Checks_On : constant Boolean := |
494 | not Index_Checks_Suppressed (Suppress_Typ) | |
495 | or else | |
496 | not Range_Checks_Suppressed (Suppress_Typ); | |
497 | ||
9dfe12ae | 498 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
499 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
500 | ||
ee6ba406 | 501 | begin |
2b4f2458 | 502 | -- For now we just return if Checks_On is false, however this should be |
503 | -- enhanced to check for an always True value in the condition and to | |
504 | -- generate a compilation warning??? | |
ee6ba406 | 505 | |
506 | if not Checks_On then | |
507 | return; | |
508 | end if; | |
509 | ||
510 | for J in 1 .. 2 loop | |
511 | exit when No (Checks (J)); | |
512 | ||
513 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
514 | and then Present (Condition (Checks (J))) | |
515 | then | |
516 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
517 | Append_To (Stmts, Checks (J)); | |
518 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
519 | end if; | |
520 | ||
521 | else | |
522 | Append_To | |
f15731c4 | 523 | (Stmts, |
524 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
525 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 526 | end if; |
527 | end loop; | |
528 | end Append_Range_Checks; | |
529 | ||
530 | ------------------------ | |
531 | -- Apply_Access_Check -- | |
532 | ------------------------ | |
533 | ||
534 | procedure Apply_Access_Check (N : Node_Id) is | |
535 | P : constant Node_Id := Prefix (N); | |
536 | ||
537 | begin | |
13dbf220 | 538 | -- We do not need checks if we are not generating code (i.e. the |
539 | -- expander is not active). This is not just an optimization, there | |
540 | -- are cases (e.g. with pragma Debug) where generating the checks | |
541 | -- can cause real trouble). | |
284faf8b | 542 | |
a33565dd | 543 | if not Expander_Active then |
13dbf220 | 544 | return; |
9dfe12ae | 545 | end if; |
ee6ba406 | 546 | |
84d0d4a5 | 547 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 548 | |
84d0d4a5 | 549 | if not Check_Needed (P, Access_Check) then |
550 | return; | |
ee6ba406 | 551 | end if; |
9dfe12ae | 552 | |
cc60bd16 | 553 | -- No check if accessing the Offset_To_Top component of a dispatch |
554 | -- table. They are safe by construction. | |
555 | ||
040277b1 | 556 | if Tagged_Type_Expansion |
557 | and then Present (Etype (P)) | |
cc60bd16 | 558 | and then RTU_Loaded (Ada_Tags) |
559 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
560 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
561 | then | |
562 | return; | |
563 | end if; | |
564 | ||
84d0d4a5 | 565 | -- Otherwise go ahead and install the check |
9dfe12ae | 566 | |
fa7497e8 | 567 | Install_Null_Excluding_Check (P); |
ee6ba406 | 568 | end Apply_Access_Check; |
569 | ||
570 | ------------------------------- | |
571 | -- Apply_Accessibility_Check -- | |
572 | ------------------------------- | |
573 | ||
55dc6dc2 | 574 | procedure Apply_Accessibility_Check |
575 | (N : Node_Id; | |
576 | Typ : Entity_Id; | |
577 | Insert_Node : Node_Id) | |
578 | is | |
ee6ba406 | 579 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 580 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 581 | Param_Level : Node_Id; |
582 | Type_Level : Node_Id; | |
583 | ||
584 | begin | |
47d210a3 | 585 | if Ada_Version >= Ada_2012 |
586 | and then not Present (Param_Ent) | |
587 | and then Is_Entity_Name (N) | |
588 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
589 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
590 | then | |
591 | Param_Ent := Entity (N); | |
592 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 593 | |
47d210a3 | 594 | -- Renamed_Object must return an Entity_Name here |
595 | -- because of preceding "Present (E_E_A (...))" test. | |
596 | ||
597 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
598 | end loop; | |
599 | end if; | |
600 | ||
ee6ba406 | 601 | if Inside_A_Generic then |
602 | return; | |
603 | ||
6ffc64fc | 604 | -- Only apply the run-time check if the access parameter has an |
605 | -- associated extra access level parameter and when the level of the | |
606 | -- type is less deep than the level of the access parameter, and | |
607 | -- accessibility checks are not suppressed. | |
ee6ba406 | 608 | |
609 | elsif Present (Param_Ent) | |
610 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 611 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 612 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 613 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
614 | and then not Accessibility_Checks_Suppressed (Typ) | |
615 | then | |
616 | Param_Level := | |
617 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
618 | ||
1a9cc6cd | 619 | Type_Level := |
620 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
ee6ba406 | 621 | |
bf3e1520 | 622 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 623 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 624 | |
55dc6dc2 | 625 | Insert_Action (Insert_Node, |
ee6ba406 | 626 | Make_Raise_Program_Error (Loc, |
627 | Condition => | |
628 | Make_Op_Gt (Loc, | |
629 | Left_Opnd => Param_Level, | |
f15731c4 | 630 | Right_Opnd => Type_Level), |
631 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 632 | |
633 | Analyze_And_Resolve (N); | |
634 | end if; | |
635 | end Apply_Accessibility_Check; | |
636 | ||
0577b0b1 | 637 | -------------------------------- |
638 | -- Apply_Address_Clause_Check -- | |
639 | -------------------------------- | |
640 | ||
641 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 642 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
643 | ||
8650387e | 644 | AC : constant Node_Id := Address_Clause (E); |
645 | Loc : constant Source_Ptr := Sloc (AC); | |
646 | Typ : constant Entity_Id := Etype (E); | |
c2b56224 | 647 | |
c2b56224 | 648 | Expr : Node_Id; |
0577b0b1 | 649 | -- Address expression (not necessarily the same as Aexp, for example |
650 | -- when Aexp is a reference to a constant, in which case Expr gets | |
7b8fa048 | 651 | -- reset to reference the value expression of the constant). |
0577b0b1 | 652 | |
c2b56224 | 653 | begin |
d6da7448 | 654 | -- See if alignment check needed. Note that we never need a check if the |
655 | -- maximum alignment is one, since the check will always succeed. | |
656 | ||
657 | -- Note: we do not check for checks suppressed here, since that check | |
658 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
659 | -- only called if checks were not suppressed. The reason for this is | |
660 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
661 | -- time (so that all types etc are elaborated), but we have to check | |
662 | -- the status of check suppressing at the point of the address clause. | |
663 | ||
664 | if No (AC) | |
665 | or else not Check_Address_Alignment (AC) | |
666 | or else Maximum_Alignment = 1 | |
667 | then | |
668 | return; | |
669 | end if; | |
670 | ||
671 | -- Obtain expression from address clause | |
9dfe12ae | 672 | |
514a5555 | 673 | Expr := Address_Value (Expression (AC)); |
0577b0b1 | 674 | |
514a5555 | 675 | -- See if we know that Expr has an acceptable value at compile time. If |
676 | -- it hasn't or we don't know, we defer issuing the warning until the | |
677 | -- end of the compilation to take into account back end annotations. | |
c2b56224 | 678 | |
679 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 680 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 681 | then |
f2a06be9 | 682 | declare |
683 | AL : Uint := Alignment (Typ); | |
684 | ||
685 | begin | |
8650387e | 686 | -- The object alignment might be more restrictive than the type |
687 | -- alignment. | |
f2a06be9 | 688 | |
689 | if Known_Alignment (E) then | |
690 | AL := Alignment (E); | |
691 | end if; | |
692 | ||
514a5555 | 693 | if Expr_Value (Expr) mod AL = 0 then |
0577b0b1 | 694 | return; |
f2a06be9 | 695 | end if; |
696 | end; | |
c2b56224 | 697 | |
7161e166 | 698 | -- If the expression has the form X'Address, then we can find out if the |
699 | -- object X has an alignment that is compatible with the object E. If it | |
700 | -- hasn't or we don't know, we defer issuing the warning until the end | |
701 | -- of the compilation to take into account back end annotations. | |
c2b56224 | 702 | |
0577b0b1 | 703 | elsif Nkind (Expr) = N_Attribute_Reference |
704 | and then Attribute_Name (Expr) = Name_Address | |
7161e166 | 705 | and then |
706 | Has_Compatible_Alignment (E, Prefix (Expr), False) = Known_Compatible | |
0577b0b1 | 707 | then |
d6da7448 | 708 | return; |
0577b0b1 | 709 | end if; |
c2b56224 | 710 | |
6fb3c314 | 711 | -- Here we do not know if the value is acceptable. Strictly we don't |
712 | -- have to do anything, since if the alignment is bad, we have an | |
713 | -- erroneous program. However we are allowed to check for erroneous | |
714 | -- conditions and we decide to do this by default if the check is not | |
715 | -- suppressed. | |
0577b0b1 | 716 | |
717 | -- However, don't do the check if elaboration code is unwanted | |
718 | ||
719 | if Restriction_Active (No_Elaboration_Code) then | |
720 | return; | |
721 | ||
722 | -- Generate a check to raise PE if alignment may be inappropriate | |
723 | ||
724 | else | |
8650387e | 725 | -- If the original expression is a non-static constant, use the name |
726 | -- of the constant itself rather than duplicating its initialization | |
727 | -- expression, which was extracted above. | |
0577b0b1 | 728 | |
00c403ee | 729 | -- Note: Expr is empty if the address-clause is applied to in-mode |
730 | -- actuals (allowed by 13.1(22)). | |
731 | ||
732 | if not Present (Expr) | |
733 | or else | |
734 | (Is_Entity_Name (Expression (AC)) | |
735 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
8650387e | 736 | and then Nkind (Parent (Entity (Expression (AC)))) = |
737 | N_Object_Declaration) | |
0577b0b1 | 738 | then |
739 | Expr := New_Copy_Tree (Expression (AC)); | |
740 | else | |
741 | Remove_Side_Effects (Expr); | |
c2b56224 | 742 | end if; |
c2b56224 | 743 | |
d950dc79 | 744 | if No (Actions (N)) then |
745 | Set_Actions (N, New_List); | |
746 | end if; | |
747 | ||
748 | Prepend_To (Actions (N), | |
0577b0b1 | 749 | Make_Raise_Program_Error (Loc, |
750 | Condition => | |
751 | Make_Op_Ne (Loc, | |
8650387e | 752 | Left_Opnd => |
0577b0b1 | 753 | Make_Op_Mod (Loc, |
8650387e | 754 | Left_Opnd => |
0577b0b1 | 755 | Unchecked_Convert_To |
756 | (RTE (RE_Integer_Address), Expr), | |
757 | Right_Opnd => | |
758 | Make_Attribute_Reference (Loc, | |
d950dc79 | 759 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 760 | Attribute_Name => Name_Alignment)), |
761 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
8650387e | 762 | Reason => PE_Misaligned_Address_Value)); |
7b8fa048 | 763 | |
764 | Warning_Msg := No_Error_Msg; | |
d950dc79 | 765 | Analyze (First (Actions (N)), Suppress => All_Checks); |
cd309f05 | 766 | |
514a5555 | 767 | -- If the above raise action generated a warning message (for example |
7b8fa048 | 768 | -- from Warn_On_Non_Local_Exception mode with the active restriction |
769 | -- No_Exception_Propagation). | |
770 | ||
771 | if Warning_Msg /= No_Error_Msg then | |
8650387e | 772 | |
7b8fa048 | 773 | -- If the expression has a known at compile time value, then |
774 | -- once we know the alignment of the type, we can check if the | |
775 | -- exception will be raised or not, and if not, we don't need | |
776 | -- the warning so we will kill the warning later on. | |
777 | ||
778 | if Compile_Time_Known_Value (Expr) then | |
779 | Alignment_Warnings.Append | |
780 | ((E => E, A => Expr_Value (Expr), W => Warning_Msg)); | |
cd309f05 | 781 | |
8650387e | 782 | -- Add explanation of the warning generated by the check |
783 | ||
784 | else | |
514a5555 | 785 | Error_Msg_N |
8650387e | 786 | ("\address value may be incompatible with alignment of " |
787 | & "object?X?", AC); | |
514a5555 | 788 | end if; |
cd309f05 | 789 | end if; |
78be29d1 | 790 | |
0577b0b1 | 791 | return; |
792 | end if; | |
9dfe12ae | 793 | |
794 | exception | |
8650387e | 795 | |
0577b0b1 | 796 | -- If we have some missing run time component in configurable run time |
797 | -- mode then just skip the check (it is not required in any case). | |
798 | ||
9dfe12ae | 799 | when RE_Not_Available => |
800 | return; | |
0577b0b1 | 801 | end Apply_Address_Clause_Check; |
c2b56224 | 802 | |
ee6ba406 | 803 | ------------------------------------- |
804 | -- Apply_Arithmetic_Overflow_Check -- | |
805 | ------------------------------------- | |
806 | ||
3cce7f32 | 807 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
808 | begin | |
809 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 810 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 811 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 812 | |
0df9d43f | 813 | if Overflow_Check_Mode = Strict |
3cce7f32 | 814 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
815 | then | |
0df9d43f | 816 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 817 | |
21a55437 | 818 | -- Otherwise use the new routine for the case of a signed integer |
819 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
820 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 821 | |
822 | else | |
823 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
824 | end if; | |
825 | end Apply_Arithmetic_Overflow_Check; | |
826 | ||
0df9d43f | 827 | -------------------------------------- |
828 | -- Apply_Arithmetic_Overflow_Strict -- | |
829 | -------------------------------------- | |
3cce7f32 | 830 | |
fc1dbe36 | 831 | -- This routine is called only if the type is an integer type and an |
832 | -- arithmetic overflow check may be needed for op (add, subtract, or | |
833 | -- multiply). This check is performed if Backend_Overflow_Checks_On_Target | |
834 | -- is not enabled and Do_Overflow_Check is set. In this case we expand the | |
f40f9731 | 835 | -- operation into a more complex sequence of tests that ensures that |
836 | -- overflow is properly caught. | |
ee6ba406 | 837 | |
0df9d43f | 838 | -- This is used in CHECKED modes. It is identical to the code for this |
839 | -- cases before the big overflow earthquake, thus ensuring that in this | |
840 | -- modes we have compatible behavior (and reliability) to what was there | |
841 | -- before. It is also called for types other than signed integers, and if | |
842 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 843 | |
844 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
845 | -- to give up and just generate an overflow check without any fuss. | |
846 | ||
0df9d43f | 847 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 848 | Loc : constant Source_Ptr := Sloc (N); |
849 | Typ : constant Entity_Id := Etype (N); | |
850 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 851 | |
852 | begin | |
0df9d43f | 853 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
854 | -- suppressed. | |
855 | ||
856 | if not Do_Overflow_Check (N) then | |
857 | return; | |
858 | end if; | |
859 | ||
f40f9731 | 860 | -- An interesting special case. If the arithmetic operation appears as |
861 | -- the operand of a type conversion: | |
862 | ||
863 | -- type1 (x op y) | |
864 | ||
865 | -- and all the following conditions apply: | |
866 | ||
867 | -- arithmetic operation is for a signed integer type | |
868 | -- target type type1 is a static integer subtype | |
869 | -- range of x and y are both included in the range of type1 | |
870 | -- range of x op y is included in the range of type1 | |
871 | -- size of type1 is at least twice the result size of op | |
872 | ||
a36d34e4 | 873 | -- then we don't do an overflow check in any case. Instead, we transform |
f40f9731 | 874 | -- the operation so that we end up with: |
875 | ||
876 | -- type1 (type1 (x) op type1 (y)) | |
877 | ||
878 | -- This avoids intermediate overflow before the conversion. It is | |
879 | -- explicitly permitted by RM 3.5.4(24): | |
880 | ||
881 | -- For the execution of a predefined operation of a signed integer | |
882 | -- type, the implementation need not raise Constraint_Error if the | |
883 | -- result is outside the base range of the type, so long as the | |
884 | -- correct result is produced. | |
885 | ||
886 | -- It's hard to imagine that any programmer counts on the exception | |
887 | -- being raised in this case, and in any case it's wrong coding to | |
888 | -- have this expectation, given the RM permission. Furthermore, other | |
889 | -- Ada compilers do allow such out of range results. | |
890 | ||
891 | -- Note that we do this transformation even if overflow checking is | |
892 | -- off, since this is precisely about giving the "right" result and | |
893 | -- avoiding the need for an overflow check. | |
894 | ||
8eb4a5eb | 895 | -- Note: this circuit is partially redundant with respect to the similar |
896 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
897 | -- with cases that do not come through here. We still need the following | |
898 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
899 | -- sure not to generate the arithmetic overflow check in these cases | |
900 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
901 | ||
f40f9731 | 902 | if Is_Signed_Integer_Type (Typ) |
903 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 904 | then |
f32c377d | 905 | Conversion_Optimization : declare |
f40f9731 | 906 | Target_Type : constant Entity_Id := |
b6341c67 | 907 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 908 | |
909 | Llo, Lhi : Uint; | |
910 | Rlo, Rhi : Uint; | |
911 | LOK, ROK : Boolean; | |
912 | ||
913 | Vlo : Uint; | |
914 | Vhi : Uint; | |
915 | VOK : Boolean; | |
916 | ||
917 | Tlo : Uint; | |
918 | Thi : Uint; | |
919 | ||
920 | begin | |
921 | if Is_Integer_Type (Target_Type) | |
922 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
923 | then | |
924 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
925 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
926 | ||
9c486805 | 927 | Determine_Range |
928 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
929 | Determine_Range | |
930 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 931 | |
932 | if (LOK and ROK) | |
933 | and then Tlo <= Llo and then Lhi <= Thi | |
934 | and then Tlo <= Rlo and then Rhi <= Thi | |
935 | then | |
9c486805 | 936 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 937 | |
938 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
939 | Rewrite (Left_Opnd (N), | |
940 | Make_Type_Conversion (Loc, | |
941 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
942 | Expression => Relocate_Node (Left_Opnd (N)))); | |
943 | ||
944 | Rewrite (Right_Opnd (N), | |
945 | Make_Type_Conversion (Loc, | |
946 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
947 | Expression => Relocate_Node (Right_Opnd (N)))); | |
948 | ||
780bfb21 | 949 | -- Rewrite the conversion operand so that the original |
950 | -- node is retained, in order to avoid the warning for | |
951 | -- redundant conversions in Resolve_Type_Conversion. | |
952 | ||
953 | Rewrite (N, Relocate_Node (N)); | |
954 | ||
f40f9731 | 955 | Set_Etype (N, Target_Type); |
780bfb21 | 956 | |
f40f9731 | 957 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
958 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
959 | ||
960 | -- Given that the target type is twice the size of the | |
961 | -- source type, overflow is now impossible, so we can | |
962 | -- safely kill the overflow check and return. | |
963 | ||
964 | Set_Do_Overflow_Check (N, False); | |
965 | return; | |
966 | end if; | |
967 | end if; | |
968 | end if; | |
f32c377d | 969 | end Conversion_Optimization; |
ee6ba406 | 970 | end if; |
971 | ||
f40f9731 | 972 | -- Now see if an overflow check is required |
973 | ||
974 | declare | |
975 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
976 | Dsiz : constant Int := Siz * 2; | |
977 | Opnod : Node_Id; | |
978 | Ctyp : Entity_Id; | |
979 | Opnd : Node_Id; | |
980 | Cent : RE_Id; | |
ee6ba406 | 981 | |
f40f9731 | 982 | begin |
983 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 984 | -- is not set anyway, or we are not doing code expansion, or the |
985 | -- parent node is a type conversion whose operand is an arithmetic | |
986 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 987 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 988 | |
f40f9731 | 989 | if Backend_Overflow_Checks_On_Target |
990 | or else not Do_Overflow_Check (N) | |
a33565dd | 991 | or else not Expander_Active |
df40eeb0 | 992 | or else (Present (Parent (N)) |
993 | and then Nkind (Parent (N)) = N_Type_Conversion | |
994 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 995 | then |
996 | return; | |
997 | end if; | |
ee6ba406 | 998 | |
f40f9731 | 999 | -- Otherwise, generate the full general code for front end overflow |
1000 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 1001 | |
f40f9731 | 1002 | -- x op y |
ee6ba406 | 1003 | |
f40f9731 | 1004 | -- is expanded into |
ee6ba406 | 1005 | |
f40f9731 | 1006 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 1007 | |
f40f9731 | 1008 | -- where Typ is the type of the original expression, and Checktyp is |
1009 | -- an integer type of sufficient length to hold the largest possible | |
1010 | -- result. | |
ee6ba406 | 1011 | |
f40f9731 | 1012 | -- If the size of check type exceeds the size of Long_Long_Integer, |
1013 | -- we use a different approach, expanding to: | |
ee6ba406 | 1014 | |
f40f9731 | 1015 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 1016 | |
f40f9731 | 1017 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 1018 | |
f40f9731 | 1019 | -- Find check type if one exists |
1020 | ||
1021 | if Dsiz <= Standard_Integer_Size then | |
1022 | Ctyp := Standard_Integer; | |
ee6ba406 | 1023 | |
f40f9731 | 1024 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
1025 | Ctyp := Standard_Long_Long_Integer; | |
1026 | ||
c9f84db7 | 1027 | -- No check type exists, use runtime call |
ee6ba406 | 1028 | |
1029 | else | |
f40f9731 | 1030 | if Nkind (N) = N_Op_Add then |
1031 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1032 | |
f40f9731 | 1033 | elsif Nkind (N) = N_Op_Multiply then |
1034 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1035 | |
f40f9731 | 1036 | else |
1037 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1038 | Cent := RE_Subtract_With_Ovflo_Check; | |
1039 | end if; | |
1040 | ||
1041 | Rewrite (N, | |
1042 | OK_Convert_To (Typ, | |
1043 | Make_Function_Call (Loc, | |
83c6c069 | 1044 | Name => New_Occurrence_Of (RTE (Cent), Loc), |
f40f9731 | 1045 | Parameter_Associations => New_List ( |
1046 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1047 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1048 | |
f40f9731 | 1049 | Analyze_And_Resolve (N, Typ); |
1050 | return; | |
1051 | end if; | |
ee6ba406 | 1052 | |
f40f9731 | 1053 | -- If we fall through, we have the case where we do the arithmetic |
1054 | -- in the next higher type and get the check by conversion. In these | |
1055 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1056 | |
f40f9731 | 1057 | Opnod := Relocate_Node (N); |
ee6ba406 | 1058 | |
f40f9731 | 1059 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1060 | |
f40f9731 | 1061 | Analyze (Opnd); |
1062 | Set_Etype (Opnd, Ctyp); | |
1063 | Set_Analyzed (Opnd, True); | |
1064 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1065 | |
f40f9731 | 1066 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1067 | |
f40f9731 | 1068 | Analyze (Opnd); |
1069 | Set_Etype (Opnd, Ctyp); | |
1070 | Set_Analyzed (Opnd, True); | |
1071 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1072 | |
f40f9731 | 1073 | -- The type of the operation changes to the base type of the check |
1074 | -- type, and we reset the overflow check indication, since clearly no | |
1075 | -- overflow is possible now that we are using a double length type. | |
1076 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1077 | -- expand the node. | |
ee6ba406 | 1078 | |
f40f9731 | 1079 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1080 | Set_Do_Overflow_Check (Opnod, False); | |
1081 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1082 | |
f40f9731 | 1083 | -- Now build the outer conversion |
ee6ba406 | 1084 | |
f40f9731 | 1085 | Opnd := OK_Convert_To (Typ, Opnod); |
1086 | Analyze (Opnd); | |
1087 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1088 | |
f40f9731 | 1089 | -- In the discrete type case, we directly generate the range check |
1090 | -- for the outer operand. This range check will implement the | |
1091 | -- required overflow check. | |
9dfe12ae | 1092 | |
f40f9731 | 1093 | if Is_Discrete_Type (Typ) then |
1094 | Rewrite (N, Opnd); | |
1095 | Generate_Range_Check | |
1096 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1097 | |
f40f9731 | 1098 | -- For other types, we enable overflow checking on the conversion, |
1099 | -- after setting the node as analyzed to prevent recursive attempts | |
1100 | -- to expand the conversion node. | |
9dfe12ae | 1101 | |
f40f9731 | 1102 | else |
1103 | Set_Analyzed (Opnd, True); | |
1104 | Enable_Overflow_Check (Opnd); | |
1105 | Rewrite (N, Opnd); | |
1106 | end if; | |
1107 | ||
1108 | exception | |
1109 | when RE_Not_Available => | |
1110 | return; | |
1111 | end; | |
0df9d43f | 1112 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1113 | |
1114 | ---------------------------------------------------- | |
1115 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1116 | ---------------------------------------------------- | |
1117 | ||
1118 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1119 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1120 | |
1121 | Loc : constant Source_Ptr := Sloc (Op); | |
1122 | P : constant Node_Id := Parent (Op); | |
1123 | ||
49b3a812 | 1124 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1125 | -- Operands and results are of this type when we convert | |
1126 | ||
3cce7f32 | 1127 | Result_Type : constant Entity_Id := Etype (Op); |
1128 | -- Original result type | |
1129 | ||
db415383 | 1130 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1131 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1132 | ||
1133 | Lo, Hi : Uint; | |
1134 | -- Ranges of values for result | |
1135 | ||
1136 | begin | |
1137 | -- Nothing to do if our parent is one of the following: | |
1138 | ||
0326b4d4 | 1139 | -- Another signed integer arithmetic op |
3cce7f32 | 1140 | -- A membership operation |
1141 | -- A comparison operation | |
1142 | ||
1143 | -- In all these cases, we will process at the higher level (and then | |
1144 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1145 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1146 | |
1147 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1148 | or else Nkind (P) in N_Membership_Test |
1149 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1150 | |
70a2dff4 | 1151 | -- This is also true for an alternative in a case expression |
1152 | ||
1153 | or else Nkind (P) = N_Case_Expression_Alternative | |
1154 | ||
1155 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1156 | |
b8a17a21 | 1157 | or else (Nkind (P) = N_Range |
1158 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1159 | then |
af90720d | 1160 | -- If_Expressions and Case_Expressions are treated as arithmetic |
1161 | -- ops, but if they appear in an assignment or similar contexts | |
1162 | -- there is no overflow check that starts from that parent node, | |
1163 | -- so apply check now. | |
1164 | ||
1165 | if Nkind_In (P, N_If_Expression, N_Case_Expression) | |
1166 | and then not Is_Signed_Integer_Arithmetic_Op (Parent (P)) | |
1167 | then | |
1168 | null; | |
1169 | else | |
1170 | return; | |
1171 | end if; | |
3cce7f32 | 1172 | end if; |
1173 | ||
0326b4d4 | 1174 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1175 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1176 | -- modes. This is the case where we tell the machinery not to move into | |
1177 | -- Bignum mode at this top level (of course the top level operation | |
1178 | -- will still be in Bignum mode if either of its operands are of type | |
1179 | -- Bignum). | |
3cce7f32 | 1180 | |
0df9d43f | 1181 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1182 | |
1183 | -- That call may but does not necessarily change the result type of Op. | |
1184 | -- It is the job of this routine to undo such changes, so that at the | |
1185 | -- top level, we have the proper type. This "undoing" is a point at | |
1186 | -- which a final overflow check may be applied. | |
1187 | ||
f32c377d | 1188 | -- If the result type was not fiddled we are all set. We go to base |
1189 | -- types here because things may have been rewritten to generate the | |
1190 | -- base type of the operand types. | |
3cce7f32 | 1191 | |
f32c377d | 1192 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1193 | return; |
1194 | ||
1195 | -- Bignum case | |
1196 | ||
49b3a812 | 1197 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1198 | |
d94b5da2 | 1199 | -- We need a sequence that looks like: |
3cce7f32 | 1200 | |
1201 | -- Rnn : Result_Type; | |
1202 | ||
1203 | -- declare | |
d94b5da2 | 1204 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1205 | -- begin |
49b3a812 | 1206 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1207 | -- SS_Release (M); |
1208 | -- end; | |
1209 | ||
1210 | -- This block is inserted (using Insert_Actions), and then the node | |
1211 | -- is replaced with a reference to Rnn. | |
1212 | ||
75b45a21 | 1213 | -- If our parent is a conversion node then there is no point in |
a36d34e4 | 1214 | -- generating a conversion to Result_Type. Instead, we let the parent |
75b45a21 | 1215 | -- handle this. Note that this special case is not just about |
1216 | -- optimization. Consider | |
3cce7f32 | 1217 | |
1218 | -- A,B,C : Integer; | |
1219 | -- ... | |
49b3a812 | 1220 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1221 | |
1222 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1223 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1224 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1225 | |
1226 | declare | |
49b3a812 | 1227 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1228 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1229 | RHS : Node_Id; | |
1230 | ||
1231 | Rtype : Entity_Id; | |
1232 | ||
1233 | begin | |
1234 | RHS := Convert_From_Bignum (Op); | |
1235 | ||
1236 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1237 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1238 | Rtype := Result_Type; |
1239 | ||
1240 | -- Interesting question, do we need a check on that conversion | |
1241 | -- operation. Answer, not if we know the result is in range. | |
1242 | -- At the moment we are not taking advantage of this. To be | |
1243 | -- looked at later ??? | |
1244 | ||
1245 | else | |
49b3a812 | 1246 | Rtype := LLIB; |
3cce7f32 | 1247 | end if; |
1248 | ||
1249 | Insert_Before | |
1250 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1251 | Make_Assignment_Statement (Loc, | |
1252 | Name => New_Occurrence_Of (Rnn, Loc), | |
1253 | Expression => RHS)); | |
1254 | ||
1255 | Insert_Actions (Op, New_List ( | |
1256 | Make_Object_Declaration (Loc, | |
1257 | Defining_Identifier => Rnn, | |
1258 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1259 | Blk)); | |
1260 | ||
1261 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1262 | Analyze_And_Resolve (Op); | |
1263 | end; | |
1264 | ||
af90720d | 1265 | -- Here we know the result is Long_Long_Integer'Base, or that it has |
412f75eb | 1266 | -- been rewritten because the parent operation is a conversion. See |
0df9d43f | 1267 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1268 | |
1269 | else | |
f32c377d | 1270 | pragma Assert |
1271 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1272 | |
1273 | -- All we need to do here is to convert the result to the proper | |
1274 | -- result type. As explained above for the Bignum case, we can | |
1275 | -- omit this if our parent is a type conversion. | |
1276 | ||
1277 | if Nkind (P) /= N_Type_Conversion then | |
1278 | Convert_To_And_Rewrite (Result_Type, Op); | |
1279 | end if; | |
1280 | ||
1281 | Analyze_And_Resolve (Op); | |
1282 | end if; | |
1283 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1284 | |
ee6ba406 | 1285 | ---------------------------- |
1286 | -- Apply_Constraint_Check -- | |
1287 | ---------------------------- | |
1288 | ||
1289 | procedure Apply_Constraint_Check | |
1290 | (N : Node_Id; | |
1291 | Typ : Entity_Id; | |
1292 | No_Sliding : Boolean := False) | |
1293 | is | |
1294 | Desig_Typ : Entity_Id; | |
1295 | ||
1296 | begin | |
7aafae1c | 1297 | -- No checks inside a generic (check the instantiations) |
1298 | ||
ee6ba406 | 1299 | if Inside_A_Generic then |
1300 | return; | |
7aafae1c | 1301 | end if; |
ee6ba406 | 1302 | |
6fb3c314 | 1303 | -- Apply required constraint checks |
7aafae1c | 1304 | |
1305 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1306 | Apply_Scalar_Range_Check (N, Typ); |
1307 | ||
1308 | elsif Is_Array_Type (Typ) then | |
1309 | ||
05fcfafb | 1310 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1311 | -- always has the right bounds. |
1312 | ||
1313 | if Nkind (N) = N_Aggregate | |
1314 | and then No (Expressions (N)) | |
1315 | and then Nkind | |
1316 | (First (Choices (First (Component_Associations (N))))) | |
1317 | = N_Others_Choice | |
1318 | then | |
1319 | return; | |
1320 | end if; | |
1321 | ||
ee6ba406 | 1322 | if Is_Constrained (Typ) then |
1323 | Apply_Length_Check (N, Typ); | |
1324 | ||
1325 | if No_Sliding then | |
1326 | Apply_Range_Check (N, Typ); | |
1327 | end if; | |
1328 | else | |
1329 | Apply_Range_Check (N, Typ); | |
1330 | end if; | |
1331 | ||
4fb5f0a0 | 1332 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1333 | and then Has_Discriminants (Base_Type (Typ)) |
1334 | and then Is_Constrained (Typ) | |
1335 | then | |
1336 | Apply_Discriminant_Check (N, Typ); | |
1337 | ||
1338 | elsif Is_Access_Type (Typ) then | |
1339 | ||
1340 | Desig_Typ := Designated_Type (Typ); | |
1341 | ||
1342 | -- No checks necessary if expression statically null | |
1343 | ||
2af58f67 | 1344 | if Known_Null (N) then |
00c403ee | 1345 | if Can_Never_Be_Null (Typ) then |
1346 | Install_Null_Excluding_Check (N); | |
1347 | end if; | |
ee6ba406 | 1348 | |
1349 | -- No sliding possible on access to arrays | |
1350 | ||
1351 | elsif Is_Array_Type (Desig_Typ) then | |
1352 | if Is_Constrained (Desig_Typ) then | |
1353 | Apply_Length_Check (N, Typ); | |
1354 | end if; | |
1355 | ||
1356 | Apply_Range_Check (N, Typ); | |
1357 | ||
a63a0aad | 1358 | -- Do not install a discriminant check for a constrained subtype |
1359 | -- created for an unconstrained nominal type because the subtype | |
1360 | -- has the correct constraints by construction. | |
1361 | ||
ee6ba406 | 1362 | elsif Has_Discriminants (Base_Type (Desig_Typ)) |
a63a0aad | 1363 | and then Is_Constrained (Desig_Typ) |
1364 | and then not Is_Constr_Subt_For_U_Nominal (Desig_Typ) | |
ee6ba406 | 1365 | then |
1366 | Apply_Discriminant_Check (N, Typ); | |
1367 | end if; | |
fa7497e8 | 1368 | |
bf3e1520 | 1369 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1370 | -- this check if the constraint node is illegal, as shown by having |
1371 | -- an error posted. This additional guard prevents cascaded errors | |
1372 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1373 | ||
fa7497e8 | 1374 | if Can_Never_Be_Null (Typ) |
1375 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1376 | and then not Error_Posted (N) |
fa7497e8 | 1377 | then |
1378 | Install_Null_Excluding_Check (N); | |
1379 | end if; | |
ee6ba406 | 1380 | end if; |
1381 | end Apply_Constraint_Check; | |
1382 | ||
1383 | ------------------------------ | |
1384 | -- Apply_Discriminant_Check -- | |
1385 | ------------------------------ | |
1386 | ||
1387 | procedure Apply_Discriminant_Check | |
1388 | (N : Node_Id; | |
1389 | Typ : Entity_Id; | |
1390 | Lhs : Node_Id := Empty) | |
1391 | is | |
1392 | Loc : constant Source_Ptr := Sloc (N); | |
1393 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1394 | S_Typ : Entity_Id := Etype (N); | |
1395 | Cond : Node_Id; | |
1396 | T_Typ : Entity_Id; | |
1397 | ||
7be5088a | 1398 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1399 | -- A heap object with an indefinite subtype is constrained by its | |
1400 | -- initial value, and assigning to it requires a constraint_check. | |
1401 | -- The target may be an explicit dereference, or a renaming of one. | |
1402 | ||
ee6ba406 | 1403 | function Is_Aliased_Unconstrained_Component return Boolean; |
1404 | -- It is possible for an aliased component to have a nominal | |
1405 | -- unconstrained subtype (through instantiation). If this is a | |
1406 | -- discriminated component assigned in the expansion of an aggregate | |
1407 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1408 | -- situation requires a predicate of its own. |
ee6ba406 | 1409 | |
7be5088a | 1410 | ---------------------------------- |
1411 | -- Denotes_Explicit_Dereference -- | |
1412 | ---------------------------------- | |
1413 | ||
1414 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1415 | begin | |
1416 | return | |
1417 | Nkind (Obj) = N_Explicit_Dereference | |
1418 | or else | |
1419 | (Is_Entity_Name (Obj) | |
1420 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1421 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1422 | N_Explicit_Dereference); | |
7be5088a | 1423 | end Denotes_Explicit_Dereference; |
1424 | ||
ee6ba406 | 1425 | ---------------------------------------- |
1426 | -- Is_Aliased_Unconstrained_Component -- | |
1427 | ---------------------------------------- | |
1428 | ||
1429 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1430 | Comp : Entity_Id; | |
1431 | Pref : Node_Id; | |
1432 | ||
1433 | begin | |
1434 | if Nkind (Lhs) /= N_Selected_Component then | |
1435 | return False; | |
1436 | else | |
1437 | Comp := Entity (Selector_Name (Lhs)); | |
1438 | Pref := Prefix (Lhs); | |
1439 | end if; | |
1440 | ||
1441 | if Ekind (Comp) /= E_Component | |
1442 | or else not Is_Aliased (Comp) | |
1443 | then | |
1444 | return False; | |
1445 | end if; | |
1446 | ||
1447 | return not Comes_From_Source (Pref) | |
1448 | and then In_Instance | |
1449 | and then not Is_Constrained (Etype (Comp)); | |
1450 | end Is_Aliased_Unconstrained_Component; | |
1451 | ||
1452 | -- Start of processing for Apply_Discriminant_Check | |
1453 | ||
1454 | begin | |
1455 | if Do_Access then | |
1456 | T_Typ := Designated_Type (Typ); | |
1457 | else | |
1458 | T_Typ := Typ; | |
1459 | end if; | |
1460 | ||
105fa703 | 1461 | -- If the expression is a function call that returns a limited object |
1462 | -- it cannot be copied. It is not clear how to perform the proper | |
1463 | -- discriminant check in this case because the discriminant value must | |
1464 | -- be retrieved from the constructed object itself. | |
1465 | ||
1466 | if Nkind (N) = N_Function_Call | |
1467 | and then Is_Limited_Type (Typ) | |
1468 | and then Is_Entity_Name (Name (N)) | |
1469 | and then Returns_By_Ref (Entity (Name (N))) | |
1470 | then | |
1471 | return; | |
1472 | end if; | |
1473 | ||
f0d65dae | 1474 | -- Only apply checks when generating code and discriminant checks are |
1475 | -- not suppressed. In GNATprove mode, we do not apply the checks, but we | |
1476 | -- still analyze the expression to possibly issue errors on SPARK code | |
1477 | -- when a run-time error can be detected at compile time. | |
1478 | ||
1479 | if not GNATprove_Mode then | |
1480 | if not Expander_Active | |
1481 | or else Discriminant_Checks_Suppressed (T_Typ) | |
1482 | then | |
1483 | return; | |
1484 | end if; | |
ee6ba406 | 1485 | end if; |
1486 | ||
feff2f05 | 1487 | -- No discriminant checks necessary for an access when expression is |
1488 | -- statically Null. This is not only an optimization, it is fundamental | |
1489 | -- because otherwise discriminant checks may be generated in init procs | |
1490 | -- for types containing an access to a not-yet-frozen record, causing a | |
1491 | -- deadly forward reference. | |
ee6ba406 | 1492 | |
feff2f05 | 1493 | -- Also, if the expression is of an access type whose designated type is |
1494 | -- incomplete, then the access value must be null and we suppress the | |
1495 | -- check. | |
ee6ba406 | 1496 | |
2af58f67 | 1497 | if Known_Null (N) then |
ee6ba406 | 1498 | return; |
1499 | ||
1500 | elsif Is_Access_Type (S_Typ) then | |
1501 | S_Typ := Designated_Type (S_Typ); | |
1502 | ||
1503 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1504 | return; | |
1505 | end if; | |
1506 | end if; | |
1507 | ||
0577b0b1 | 1508 | -- If an assignment target is present, then we need to generate the |
1509 | -- actual subtype if the target is a parameter or aliased object with | |
1510 | -- an unconstrained nominal subtype. | |
1511 | ||
1512 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1513 | -- subtype to the parameter and dereference cases, since other aliased | |
1514 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1515 | -- constrained). |
ee6ba406 | 1516 | |
1517 | if Present (Lhs) | |
1518 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1519 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1520 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1521 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1522 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1523 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1524 | and then not Is_Constrained (T_Typ) |
7be5088a | 1525 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1526 | and then Nkind (Original_Node (Lhs)) /= |
1527 | N_Function_Call)) | |
ee6ba406 | 1528 | then |
1529 | T_Typ := Get_Actual_Subtype (Lhs); | |
1530 | end if; | |
1531 | ||
feff2f05 | 1532 | -- Nothing to do if the type is unconstrained (this is the case where |
1533 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1534 | -- is required). | |
ee6ba406 | 1535 | |
1536 | if not Is_Constrained (T_Typ) then | |
1537 | return; | |
05fcfafb | 1538 | |
1539 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1540 | -- partial view that is constrained. | |
1541 | ||
de54c5ab | 1542 | elsif Ada_Version >= Ada_2005 |
0d78d2d4 | 1543 | and then Object_Type_Has_Constrained_Partial_View |
d41a3f41 | 1544 | (Typ => Base_Type (T_Typ), |
1545 | Scop => Current_Scope) | |
05fcfafb | 1546 | then |
1547 | return; | |
ee6ba406 | 1548 | end if; |
1549 | ||
00f91aef | 1550 | -- Nothing to do if the type is an Unchecked_Union |
1551 | ||
1552 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1553 | return; | |
1554 | end if; | |
1555 | ||
8d11916f | 1556 | -- Suppress checks if the subtypes are the same. The check must be |
feff2f05 | 1557 | -- preserved in an assignment to a formal, because the constraint is |
1558 | -- given by the actual. | |
ee6ba406 | 1559 | |
1560 | if Nkind (Original_Node (N)) /= N_Allocator | |
1561 | and then (No (Lhs) | |
8143bf7c | 1562 | or else not Is_Entity_Name (Lhs) |
1563 | or else No (Param_Entity (Lhs))) | |
ee6ba406 | 1564 | then |
1565 | if (Etype (N) = Typ | |
1566 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1567 | and then not Is_Aliased_View (Lhs) | |
1568 | then | |
1569 | return; | |
1570 | end if; | |
1571 | ||
feff2f05 | 1572 | -- We can also eliminate checks on allocators with a subtype mark that |
1573 | -- coincides with the context type. The context type may be a subtype | |
1574 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1575 | |
1576 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1577 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1578 | then | |
1579 | declare | |
9dfe12ae | 1580 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1581 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1582 | |
1583 | begin | |
1584 | if Alloc_Typ = T_Typ | |
1585 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1586 | and then Is_Entity_Name ( | |
1587 | Subtype_Indication (Parent (T_Typ))) | |
1588 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1589 | ||
1590 | then | |
1591 | return; | |
1592 | end if; | |
1593 | end; | |
1594 | end if; | |
1595 | ||
feff2f05 | 1596 | -- See if we have a case where the types are both constrained, and all |
1597 | -- the constraints are constants. In this case, we can do the check | |
1598 | -- successfully at compile time. | |
ee6ba406 | 1599 | |
8d11916f | 1600 | -- We skip this check for the case where the node is rewritten as |
d7ec9a29 | 1601 | -- an allocator, because it already carries the context subtype, |
1602 | -- and extracting the discriminants from the aggregate is messy. | |
ee6ba406 | 1603 | |
1604 | if Is_Constrained (S_Typ) | |
1605 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1606 | then | |
1607 | declare | |
1608 | DconT : Elmt_Id; | |
1609 | Discr : Entity_Id; | |
1610 | DconS : Elmt_Id; | |
1611 | ItemS : Node_Id; | |
1612 | ItemT : Node_Id; | |
1613 | ||
1614 | begin | |
1615 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1616 | -- private type completed by a default discriminated type. In that |
8d11916f | 1617 | -- case, we need to get the constraints from the underlying type. |
feff2f05 | 1618 | -- If the underlying type is unconstrained (i.e. has no default |
1619 | -- discriminants) no check is needed. | |
ee6ba406 | 1620 | |
1621 | if Has_Discriminants (S_Typ) then | |
1622 | Discr := First_Discriminant (S_Typ); | |
1623 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1624 | ||
1625 | else | |
1626 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1627 | DconS := | |
1628 | First_Elmt | |
1629 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1630 | ||
1631 | if No (DconS) then | |
1632 | return; | |
1633 | end if; | |
fccb5da7 | 1634 | |
1635 | -- A further optimization: if T_Typ is derived from S_Typ | |
1636 | -- without imposing a constraint, no check is needed. | |
1637 | ||
1638 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1639 | N_Full_Type_Declaration | |
1640 | then | |
1641 | declare | |
5c61a0ff | 1642 | Type_Def : constant Node_Id := |
b6341c67 | 1643 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1644 | begin |
1645 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1646 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1647 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1648 | then | |
1649 | return; | |
1650 | end if; | |
1651 | end; | |
1652 | end if; | |
ee6ba406 | 1653 | end if; |
1654 | ||
86594966 | 1655 | -- Constraint may appear in full view of type |
1656 | ||
1657 | if Ekind (T_Typ) = E_Private_Subtype | |
1658 | and then Present (Full_View (T_Typ)) | |
1659 | then | |
d7ec9a29 | 1660 | DconT := |
86594966 | 1661 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
86594966 | 1662 | else |
d7ec9a29 | 1663 | DconT := |
1664 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
86594966 | 1665 | end if; |
ee6ba406 | 1666 | |
1667 | while Present (Discr) loop | |
1668 | ItemS := Node (DconS); | |
1669 | ItemT := Node (DconT); | |
1670 | ||
00c403ee | 1671 | -- For a discriminated component type constrained by the |
1672 | -- current instance of an enclosing type, there is no | |
1673 | -- applicable discriminant check. | |
1674 | ||
1675 | if Nkind (ItemT) = N_Attribute_Reference | |
1676 | and then Is_Access_Type (Etype (ItemT)) | |
1677 | and then Is_Entity_Name (Prefix (ItemT)) | |
1678 | and then Is_Type (Entity (Prefix (ItemT))) | |
1679 | then | |
1680 | return; | |
1681 | end if; | |
1682 | ||
cc60bd16 | 1683 | -- If the expressions for the discriminants are identical |
1684 | -- and it is side-effect free (for now just an entity), | |
1685 | -- this may be a shared constraint, e.g. from a subtype | |
1686 | -- without a constraint introduced as a generic actual. | |
1687 | -- Examine other discriminants if any. | |
1688 | ||
1689 | if ItemS = ItemT | |
1690 | and then Is_Entity_Name (ItemS) | |
1691 | then | |
1692 | null; | |
1693 | ||
1694 | elsif not Is_OK_Static_Expression (ItemS) | |
1695 | or else not Is_OK_Static_Expression (ItemT) | |
1696 | then | |
1697 | exit; | |
ee6ba406 | 1698 | |
cc60bd16 | 1699 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1700 | if Do_Access then -- needs run-time check. |
1701 | exit; | |
1702 | else | |
1703 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1704 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1705 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1706 | return; |
1707 | end if; | |
1708 | end if; | |
1709 | ||
1710 | Next_Elmt (DconS); | |
1711 | Next_Elmt (DconT); | |
1712 | Next_Discriminant (Discr); | |
1713 | end loop; | |
1714 | ||
1715 | if No (Discr) then | |
1716 | return; | |
1717 | end if; | |
1718 | end; | |
1719 | end if; | |
1720 | ||
f0d65dae | 1721 | -- In GNATprove mode, we do not apply the checks |
1722 | ||
1723 | if GNATprove_Mode then | |
1724 | return; | |
1725 | end if; | |
1726 | ||
ee6ba406 | 1727 | -- Here we need a discriminant check. First build the expression |
1728 | -- for the comparisons of the discriminants: | |
1729 | ||
1730 | -- (n.disc1 /= typ.disc1) or else | |
1731 | -- (n.disc2 /= typ.disc2) or else | |
1732 | -- ... | |
1733 | -- (n.discn /= typ.discn) | |
1734 | ||
1735 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1736 | ||
3cce7f32 | 1737 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1738 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1739 | |
1740 | if Present (Param_Entity (Lhs)) then | |
1741 | Cond := | |
1742 | Make_And_Then (Loc, | |
1743 | Left_Opnd => | |
1744 | Make_Attribute_Reference (Loc, | |
1745 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1746 | Attribute_Name => Name_Constrained), | |
1747 | Right_Opnd => Cond); | |
1748 | end if; | |
1749 | ||
1750 | if Do_Access then | |
1751 | Cond := Guard_Access (Cond, Loc, N); | |
1752 | end if; | |
1753 | ||
1754 | Insert_Action (N, | |
f15731c4 | 1755 | Make_Raise_Constraint_Error (Loc, |
1756 | Condition => Cond, | |
1757 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1758 | end Apply_Discriminant_Check; |
1759 | ||
2fe22c69 | 1760 | ------------------------- |
1761 | -- Apply_Divide_Checks -- | |
1762 | ------------------------- | |
ee6ba406 | 1763 | |
2fe22c69 | 1764 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1765 | Loc : constant Source_Ptr := Sloc (N); |
1766 | Typ : constant Entity_Id := Etype (N); | |
1767 | Left : constant Node_Id := Left_Opnd (N); | |
1768 | Right : constant Node_Id := Right_Opnd (N); | |
1769 | ||
db415383 | 1770 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1771 | -- Current overflow checking mode |
1772 | ||
ee6ba406 | 1773 | LLB : Uint; |
1774 | Llo : Uint; | |
1775 | Lhi : Uint; | |
1776 | LOK : Boolean; | |
1777 | Rlo : Uint; | |
1778 | Rhi : Uint; | |
2fe22c69 | 1779 | ROK : Boolean; |
96da3284 | 1780 | |
1781 | pragma Warnings (Off, Lhi); | |
1782 | -- Don't actually use this value | |
ee6ba406 | 1783 | |
1784 | begin | |
0df9d43f | 1785 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1786 | -- operating on signed integer types, then the only thing this routine | |
1787 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1788 | -- procedure will (possibly later on during recursive downward calls), | |
1789 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1790 | |
1791 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1792 | and then Is_Signed_Integer_Type (Typ) |
1793 | then | |
1794 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1795 | return; | |
1796 | end if; | |
1797 | ||
1798 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1799 | ||
a33565dd | 1800 | if Expander_Active |
13dbf220 | 1801 | and then not Backend_Divide_Checks_On_Target |
1802 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1803 | then |
9c486805 | 1804 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1805 | |
2fe22c69 | 1806 | -- Deal with division check |
ee6ba406 | 1807 | |
2fe22c69 | 1808 | if Do_Division_Check (N) |
1809 | and then not Division_Checks_Suppressed (Typ) | |
1810 | then | |
1811 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1812 | end if; |
1813 | ||
2fe22c69 | 1814 | -- Deal with overflow check |
1815 | ||
0df9d43f | 1816 | if Do_Overflow_Check (N) |
1817 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1818 | then | |
f3ccbbb3 | 1819 | Set_Do_Overflow_Check (N, False); |
1820 | ||
2fe22c69 | 1821 | -- Test for extremely annoying case of xxx'First divided by -1 |
1822 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1823 | |
ee6ba406 | 1824 | if Nkind (N) = N_Op_Divide |
1825 | and then Is_Signed_Integer_Type (Typ) | |
1826 | then | |
9c486805 | 1827 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1828 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1829 | ||
1830 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1831 | and then |
1832 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1833 | then |
f72e7b2a | 1834 | -- Ensure that expressions are not evaluated twice (once |
1835 | -- for their runtime checks and once for their regular | |
1836 | -- computation). | |
1837 | ||
1838 | Force_Evaluation (Left, Mode => Strict); | |
1839 | Force_Evaluation (Right, Mode => Strict); | |
1840 | ||
ee6ba406 | 1841 | Insert_Action (N, |
1842 | Make_Raise_Constraint_Error (Loc, | |
1843 | Condition => | |
1844 | Make_And_Then (Loc, | |
2fe22c69 | 1845 | Left_Opnd => |
1846 | Make_Op_Eq (Loc, | |
1847 | Left_Opnd => | |
1848 | Duplicate_Subexpr_Move_Checks (Left), | |
1849 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1850 | |
2fe22c69 | 1851 | Right_Opnd => |
1852 | Make_Op_Eq (Loc, | |
1853 | Left_Opnd => Duplicate_Subexpr (Right), | |
1854 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1855 | |
f15731c4 | 1856 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1857 | end if; |
1858 | end if; | |
1859 | end if; | |
1860 | end if; | |
2fe22c69 | 1861 | end Apply_Divide_Checks; |
1862 | ||
1863 | -------------------------- | |
1864 | -- Apply_Division_Check -- | |
1865 | -------------------------- | |
1866 | ||
1867 | procedure Apply_Division_Check | |
1868 | (N : Node_Id; | |
1869 | Rlo : Uint; | |
1870 | Rhi : Uint; | |
1871 | ROK : Boolean) | |
1872 | is | |
1873 | pragma Assert (Do_Division_Check (N)); | |
1874 | ||
1875 | Loc : constant Source_Ptr := Sloc (N); | |
f81a201b | 1876 | Right : constant Node_Id := Right_Opnd (N); |
1877 | Opnd : Node_Id; | |
2fe22c69 | 1878 | |
1879 | begin | |
a33565dd | 1880 | if Expander_Active |
2fe22c69 | 1881 | and then not Backend_Divide_Checks_On_Target |
1882 | and then Check_Needed (Right, Division_Check) | |
2fe22c69 | 1883 | |
f81a201b | 1884 | -- See if division by zero possible, and if so generate test. This |
1885 | -- part of the test is not controlled by the -gnato switch, since it | |
1886 | -- is a Division_Check and not an Overflow_Check. | |
f3ccbbb3 | 1887 | |
f81a201b | 1888 | and then Do_Division_Check (N) |
1889 | then | |
1890 | Set_Do_Division_Check (N, False); | |
75f4b34c | 1891 | |
f81a201b | 1892 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then |
1893 | if Is_Floating_Point_Type (Etype (N)) then | |
1894 | Opnd := Make_Real_Literal (Loc, Ureal_0); | |
1895 | else | |
1896 | Opnd := Make_Integer_Literal (Loc, 0); | |
2fe22c69 | 1897 | end if; |
f81a201b | 1898 | |
1899 | Insert_Action (N, | |
1900 | Make_Raise_Constraint_Error (Loc, | |
1901 | Condition => | |
1902 | Make_Op_Eq (Loc, | |
1903 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1904 | Right_Opnd => Opnd), | |
1905 | Reason => CE_Divide_By_Zero)); | |
2fe22c69 | 1906 | end if; |
1907 | end if; | |
1908 | end Apply_Division_Check; | |
ee6ba406 | 1909 | |
5329ca64 | 1910 | ---------------------------------- |
1911 | -- Apply_Float_Conversion_Check -- | |
1912 | ---------------------------------- | |
1913 | ||
feff2f05 | 1914 | -- Let F and I be the source and target types of the conversion. The RM |
1915 | -- specifies that a floating-point value X is rounded to the nearest | |
1916 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1917 | -- value of X is checked against I'Range. | |
1918 | ||
1919 | -- The catch in the above paragraph is that there is no good way to know | |
1920 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1921 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1922 | |
5329ca64 | 1923 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1924 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1925 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1926 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1927 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1928 | -- sign of I'First and I'Last. | |
5329ca64 | 1929 | -- (5) X may be a NaN, which will fail any comparison |
1930 | ||
2af58f67 | 1931 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1932 | |
5329ca64 | 1933 | -- (1) If either I'First or I'Last is not known at compile time, use |
1934 | -- I'Base instead of I in the next three steps and perform a | |
1935 | -- regular range check against I'Range after conversion. | |
1936 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1937 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1938 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1939 | -- In other words, take one of the closest floating-point numbers | |
1940 | -- (which is an integer value) to I'First, and see if it is in | |
1941 | -- range or not. | |
5329ca64 | 1942 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1943 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1944 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1945 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1946 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1947 | ||
2af58f67 | 1948 | -- For the truncating case, replace steps (2) and (3) as follows: |
1949 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1950 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1951 | -- Lo_OK be True. | |
1952 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1953 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1954 | -- Hi_OK be True. |
2af58f67 | 1955 | |
5329ca64 | 1956 | procedure Apply_Float_Conversion_Check |
1957 | (Ck_Node : Node_Id; | |
1958 | Target_Typ : Entity_Id) | |
1959 | is | |
feff2f05 | 1960 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1961 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1962 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1963 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1964 | Target_Base : constant Entity_Id := |
b6341c67 | 1965 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1966 | |
2af58f67 | 1967 | Par : constant Node_Id := Parent (Ck_Node); |
1968 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1969 | -- Parent of check node, must be a type conversion | |
1970 | ||
1971 | Truncate : constant Boolean := Float_Truncate (Par); | |
1972 | Max_Bound : constant Uint := | |
b6341c67 | 1973 | UI_Expon |
1974 | (Machine_Radix_Value (Expr_Type), | |
1975 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 1976 | |
5329ca64 | 1977 | -- Largest bound, so bound plus or minus half is a machine number of F |
1978 | ||
feff2f05 | 1979 | Ifirst, Ilast : Uint; |
1980 | -- Bounds of integer type | |
1981 | ||
1982 | Lo, Hi : Ureal; | |
1983 | -- Bounds to check in floating-point domain | |
5329ca64 | 1984 | |
feff2f05 | 1985 | Lo_OK, Hi_OK : Boolean; |
1986 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1987 | |
feff2f05 | 1988 | Lo_Chk, Hi_Chk : Node_Id; |
1989 | -- Expressions that are False iff check fails | |
1990 | ||
1991 | Reason : RT_Exception_Code; | |
5329ca64 | 1992 | |
1993 | begin | |
41f06abf | 1994 | -- We do not need checks if we are not generating code (i.e. the full |
1995 | -- expander is not active). In SPARK mode, we specifically don't want | |
1996 | -- the frontend to expand these checks, which are dealt with directly | |
1997 | -- in the formal verification backend. | |
1998 | ||
a33565dd | 1999 | if not Expander_Active then |
41f06abf | 2000 | return; |
2001 | end if; | |
2002 | ||
5329ca64 | 2003 | if not Compile_Time_Known_Value (LB) |
2004 | or not Compile_Time_Known_Value (HB) | |
2005 | then | |
2006 | declare | |
feff2f05 | 2007 | -- First check that the value falls in the range of the base type, |
2008 | -- to prevent overflow during conversion and then perform a | |
2009 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 2010 | |
5329ca64 | 2011 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 2012 | |
46eb6933 | 2013 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 2014 | |
2015 | begin | |
2016 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
2017 | Set_Etype (Temp, Target_Base); | |
2018 | ||
2019 | Insert_Action (Parent (Par), | |
2020 | Make_Object_Declaration (Loc, | |
2021 | Defining_Identifier => Temp, | |
2022 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
2023 | Expression => New_Copy_Tree (Par)), | |
2024 | Suppress => All_Checks); | |
2025 | ||
2026 | Insert_Action (Par, | |
2027 | Make_Raise_Constraint_Error (Loc, | |
2028 | Condition => | |
2029 | Make_Not_In (Loc, | |
2030 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
2031 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
2032 | Reason => CE_Range_Check_Failed)); | |
2033 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
2034 | ||
2035 | return; | |
2036 | end; | |
2037 | end if; | |
2038 | ||
7d86aa98 | 2039 | -- Get the (static) bounds of the target type |
5329ca64 | 2040 | |
2041 | Ifirst := Expr_Value (LB); | |
2042 | Ilast := Expr_Value (HB); | |
2043 | ||
7d86aa98 | 2044 | -- A simple optimization: if the expression is a universal literal, |
2045 | -- we can do the comparison with the bounds and the conversion to | |
2046 | -- an integer type statically. The range checks are unchanged. | |
2047 | ||
2048 | if Nkind (Ck_Node) = N_Real_Literal | |
2049 | and then Etype (Ck_Node) = Universal_Real | |
2050 | and then Is_Integer_Type (Target_Typ) | |
2051 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
2052 | then | |
2053 | declare | |
2054 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
2055 | ||
2056 | begin | |
2057 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
2058 | ||
4309515d | 2059 | -- Conversion is safe |
7d86aa98 | 2060 | |
2061 | Rewrite (Parent (Ck_Node), | |
2062 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
2063 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
2064 | return; | |
2065 | end if; | |
2066 | end; | |
2067 | end if; | |
2068 | ||
5329ca64 | 2069 | -- Check against lower bound |
2070 | ||
2af58f67 | 2071 | if Truncate and then Ifirst > 0 then |
2072 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
2073 | Lo_OK := False; | |
2074 | ||
2075 | elsif Truncate then | |
2076 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
2077 | Lo_OK := True; | |
2078 | ||
2079 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 2080 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
2081 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 2082 | |
5329ca64 | 2083 | else |
2084 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
2085 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
2086 | end if; | |
2087 | ||
2088 | if Lo_OK then | |
2089 | ||
2090 | -- Lo_Chk := (X >= Lo) | |
2091 | ||
2092 | Lo_Chk := Make_Op_Ge (Loc, | |
2093 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2094 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2095 | ||
2096 | else | |
2097 | -- Lo_Chk := (X > Lo) | |
2098 | ||
2099 | Lo_Chk := Make_Op_Gt (Loc, | |
2100 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2101 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2102 | end if; | |
2103 | ||
2104 | -- Check against higher bound | |
2105 | ||
2af58f67 | 2106 | if Truncate and then Ilast < 0 then |
2107 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2108 | Hi_OK := False; |
2af58f67 | 2109 | |
2110 | elsif Truncate then | |
2111 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2112 | Hi_OK := True; | |
2113 | ||
2114 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2115 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2116 | Hi_OK := (Ilast < 0); | |
2117 | else | |
2118 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2119 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2120 | end if; | |
2121 | ||
2122 | if Hi_OK then | |
2123 | ||
2124 | -- Hi_Chk := (X <= Hi) | |
2125 | ||
2126 | Hi_Chk := Make_Op_Le (Loc, | |
2127 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2128 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2129 | ||
2130 | else | |
2131 | -- Hi_Chk := (X < Hi) | |
2132 | ||
2133 | Hi_Chk := Make_Op_Lt (Loc, | |
2134 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2135 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2136 | end if; | |
2137 | ||
feff2f05 | 2138 | -- If the bounds of the target type are the same as those of the base |
2139 | -- type, the check is an overflow check as a range check is not | |
2140 | -- performed in these cases. | |
5329ca64 | 2141 | |
2142 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2143 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2144 | then | |
2145 | Reason := CE_Overflow_Check_Failed; | |
2146 | else | |
2147 | Reason := CE_Range_Check_Failed; | |
2148 | end if; | |
2149 | ||
2150 | -- Raise CE if either conditions does not hold | |
2151 | ||
2152 | Insert_Action (Ck_Node, | |
2153 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2154 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2155 | Reason => Reason)); |
2156 | end Apply_Float_Conversion_Check; | |
2157 | ||
ee6ba406 | 2158 | ------------------------ |
2159 | -- Apply_Length_Check -- | |
2160 | ------------------------ | |
2161 | ||
2162 | procedure Apply_Length_Check | |
2163 | (Ck_Node : Node_Id; | |
2164 | Target_Typ : Entity_Id; | |
2165 | Source_Typ : Entity_Id := Empty) | |
2166 | is | |
2167 | begin | |
2168 | Apply_Selected_Length_Checks | |
2169 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2170 | end Apply_Length_Check; | |
2171 | ||
3b045963 | 2172 | ------------------------------------- |
2173 | -- Apply_Parameter_Aliasing_Checks -- | |
2174 | ------------------------------------- | |
b73adb97 | 2175 | |
3b045963 | 2176 | procedure Apply_Parameter_Aliasing_Checks |
2177 | (Call : Node_Id; | |
2178 | Subp : Entity_Id) | |
2179 | is | |
bb569db0 | 2180 | Loc : constant Source_Ptr := Sloc (Call); |
2181 | ||
3b045963 | 2182 | function May_Cause_Aliasing |
2183 | (Formal_1 : Entity_Id; | |
2184 | Formal_2 : Entity_Id) return Boolean; | |
2185 | -- Determine whether two formal parameters can alias each other | |
2186 | -- depending on their modes. | |
2187 | ||
2188 | function Original_Actual (N : Node_Id) return Node_Id; | |
2189 | -- The expander may replace an actual with a temporary for the sake of | |
2190 | -- side effect removal. The temporary may hide a potential aliasing as | |
2191 | -- it does not share the address of the actual. This routine attempts | |
2192 | -- to retrieve the original actual. | |
2193 | ||
bb569db0 | 2194 | procedure Overlap_Check |
2195 | (Actual_1 : Node_Id; | |
2196 | Actual_2 : Node_Id; | |
2197 | Formal_1 : Entity_Id; | |
2198 | Formal_2 : Entity_Id; | |
2199 | Check : in out Node_Id); | |
2200 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2201 | -- If detailed exception messages are enabled, the check is augmented to | |
2202 | -- provide information about the names of the corresponding formals. See | |
2203 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2204 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2205 | -- Check contains all and-ed simple tests generated so far or remains | |
2206 | -- unchanged in the case of detailed exception messaged. | |
2207 | ||
3b045963 | 2208 | ------------------------ |
2209 | -- May_Cause_Aliasing -- | |
2210 | ------------------------ | |
b73adb97 | 2211 | |
3b045963 | 2212 | function May_Cause_Aliasing |
4a9e7f0c | 2213 | (Formal_1 : Entity_Id; |
3b045963 | 2214 | Formal_2 : Entity_Id) return Boolean |
2215 | is | |
2216 | begin | |
2217 | -- The following combination cannot lead to aliasing | |
2218 | ||
2219 | -- Formal 1 Formal 2 | |
2220 | -- IN IN | |
2221 | ||
2222 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2223 | and then |
2224 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2225 | then |
2226 | return False; | |
2227 | ||
2228 | -- The following combinations may lead to aliasing | |
2229 | ||
2230 | -- Formal 1 Formal 2 | |
2231 | -- IN OUT | |
2232 | -- IN IN OUT | |
2233 | -- OUT IN | |
2234 | -- OUT IN OUT | |
2235 | -- OUT OUT | |
2236 | ||
2237 | else | |
2238 | return True; | |
2239 | end if; | |
2240 | end May_Cause_Aliasing; | |
2241 | ||
2242 | --------------------- | |
2243 | -- Original_Actual -- | |
2244 | --------------------- | |
2245 | ||
2246 | function Original_Actual (N : Node_Id) return Node_Id is | |
2247 | begin | |
2248 | if Nkind (N) = N_Type_Conversion then | |
2249 | return Expression (N); | |
2250 | ||
2251 | -- The expander created a temporary to capture the result of a type | |
2252 | -- conversion where the expression is the real actual. | |
2253 | ||
2254 | elsif Nkind (N) = N_Identifier | |
2255 | and then Present (Original_Node (N)) | |
2256 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2257 | then | |
2258 | return Expression (Original_Node (N)); | |
2259 | end if; | |
2260 | ||
2261 | return N; | |
2262 | end Original_Actual; | |
2263 | ||
bb569db0 | 2264 | ------------------- |
2265 | -- Overlap_Check -- | |
2266 | ------------------- | |
2267 | ||
2268 | procedure Overlap_Check | |
2269 | (Actual_1 : Node_Id; | |
2270 | Actual_2 : Node_Id; | |
2271 | Formal_1 : Entity_Id; | |
2272 | Formal_2 : Entity_Id; | |
2273 | Check : in out Node_Id) | |
2274 | is | |
29448168 | 2275 | Cond : Node_Id; |
2276 | ID_Casing : constant Casing_Type := | |
2277 | Identifier_Casing (Source_Index (Current_Sem_Unit)); | |
bb569db0 | 2278 | |
2279 | begin | |
2280 | -- Generate: | |
2281 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2282 | ||
2283 | Cond := | |
2284 | Make_Attribute_Reference (Loc, | |
2285 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2286 | Attribute_Name => Name_Overlaps_Storage, | |
2287 | Expressions => | |
2288 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2289 | ||
2290 | -- Generate the following check when detailed exception messages are | |
2291 | -- enabled: | |
2292 | ||
2293 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2294 | -- raise Program_Error with <detailed message>; | |
2295 | -- end if; | |
2296 | ||
2297 | if Exception_Extra_Info then | |
2298 | Start_String; | |
2299 | ||
2300 | -- Do not generate location information for internal calls | |
2301 | ||
2302 | if Comes_From_Source (Call) then | |
2303 | Store_String_Chars (Build_Location_String (Loc)); | |
2304 | Store_String_Char (' '); | |
2305 | end if; | |
2306 | ||
2307 | Store_String_Chars ("aliased parameters, actuals for """); | |
29448168 | 2308 | |
2309 | Get_Name_String (Chars (Formal_1)); | |
2310 | Set_Casing (ID_Casing); | |
2311 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2312 | ||
bb569db0 | 2313 | Store_String_Chars (""" and """); |
29448168 | 2314 | |
2315 | Get_Name_String (Chars (Formal_2)); | |
2316 | Set_Casing (ID_Casing); | |
2317 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2318 | ||
bb569db0 | 2319 | Store_String_Chars (""" overlap"); |
2320 | ||
2321 | Insert_Action (Call, | |
2322 | Make_If_Statement (Loc, | |
2323 | Condition => Cond, | |
2324 | Then_Statements => New_List ( | |
2325 | Make_Raise_Statement (Loc, | |
2326 | Name => | |
83c6c069 | 2327 | New_Occurrence_Of (Standard_Program_Error, Loc), |
bb569db0 | 2328 | Expression => Make_String_Literal (Loc, End_String))))); |
2329 | ||
2330 | -- Create a sequence of overlapping checks by and-ing them all | |
2331 | -- together. | |
2332 | ||
2333 | else | |
2334 | if No (Check) then | |
2335 | Check := Cond; | |
2336 | else | |
2337 | Check := | |
2338 | Make_And_Then (Loc, | |
2339 | Left_Opnd => Check, | |
2340 | Right_Opnd => Cond); | |
2341 | end if; | |
2342 | end if; | |
2343 | end Overlap_Check; | |
2344 | ||
3b045963 | 2345 | -- Local variables |
2346 | ||
15fca308 | 2347 | Actual_1 : Node_Id; |
2348 | Actual_2 : Node_Id; | |
2349 | Check : Node_Id; | |
2350 | Formal_1 : Entity_Id; | |
2351 | Formal_2 : Entity_Id; | |
2352 | Orig_Act_1 : Node_Id; | |
2353 | Orig_Act_2 : Node_Id; | |
3b045963 | 2354 | |
2355 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2356 | ||
2357 | begin | |
bb569db0 | 2358 | Check := Empty; |
3b045963 | 2359 | |
2360 | Actual_1 := First_Actual (Call); | |
2361 | Formal_1 := First_Formal (Subp); | |
2362 | while Present (Actual_1) and then Present (Formal_1) loop | |
15fca308 | 2363 | Orig_Act_1 := Original_Actual (Actual_1); |
3b045963 | 2364 | |
2365 | -- Ensure that the actual is an object that is not passed by value. | |
2366 | -- Elementary types are always passed by value, therefore actuals of | |
747426db | 2367 | -- such types cannot lead to aliasing. An aggregate is an object in |
2368 | -- Ada 2012, but an actual that is an aggregate cannot overlap with | |
4dc3174c | 2369 | -- another actual. A type that is By_Reference (such as an array of |
2370 | -- controlled types) is not subject to the check because any update | |
2371 | -- will be done in place and a subsequent read will always see the | |
2372 | -- correct value, see RM 6.2 (12/3). | |
3b045963 | 2373 | |
15fca308 | 2374 | if Nkind (Orig_Act_1) = N_Aggregate |
2375 | or else (Nkind (Orig_Act_1) = N_Qualified_Expression | |
2376 | and then Nkind (Expression (Orig_Act_1)) = N_Aggregate) | |
747426db | 2377 | then |
2378 | null; | |
2379 | ||
15fca308 | 2380 | elsif Is_Object_Reference (Orig_Act_1) |
2381 | and then not Is_Elementary_Type (Etype (Orig_Act_1)) | |
2382 | and then not Is_By_Reference_Type (Etype (Orig_Act_1)) | |
3b045963 | 2383 | then |
2384 | Actual_2 := Next_Actual (Actual_1); | |
2385 | Formal_2 := Next_Formal (Formal_1); | |
2386 | while Present (Actual_2) and then Present (Formal_2) loop | |
15fca308 | 2387 | Orig_Act_2 := Original_Actual (Actual_2); |
3b045963 | 2388 | |
2389 | -- The other actual we are testing against must also denote | |
2390 | -- a non pass-by-value object. Generate the check only when | |
2391 | -- the mode of the two formals may lead to aliasing. | |
2392 | ||
15fca308 | 2393 | if Is_Object_Reference (Orig_Act_2) |
2394 | and then not Is_Elementary_Type (Etype (Orig_Act_2)) | |
3b045963 | 2395 | and then May_Cause_Aliasing (Formal_1, Formal_2) |
2396 | then | |
72f889fa | 2397 | Remove_Side_Effects (Actual_1); |
2398 | Remove_Side_Effects (Actual_2); | |
2399 | ||
bb569db0 | 2400 | Overlap_Check |
2401 | (Actual_1 => Actual_1, | |
2402 | Actual_2 => Actual_2, | |
2403 | Formal_1 => Formal_1, | |
2404 | Formal_2 => Formal_2, | |
2405 | Check => Check); | |
3b045963 | 2406 | end if; |
2407 | ||
2408 | Next_Actual (Actual_2); | |
2409 | Next_Formal (Formal_2); | |
2410 | end loop; | |
2411 | end if; | |
2412 | ||
2413 | Next_Actual (Actual_1); | |
2414 | Next_Formal (Formal_1); | |
2415 | end loop; | |
2416 | ||
bb569db0 | 2417 | -- Place a simple check right before the call |
3b045963 | 2418 | |
bb569db0 | 2419 | if Present (Check) and then not Exception_Extra_Info then |
3b045963 | 2420 | Insert_Action (Call, |
2421 | Make_Raise_Program_Error (Loc, | |
bb569db0 | 2422 | Condition => Check, |
2423 | Reason => PE_Aliased_Parameters)); | |
3b045963 | 2424 | end if; |
2425 | end Apply_Parameter_Aliasing_Checks; | |
2426 | ||
2427 | ------------------------------------- | |
2428 | -- Apply_Parameter_Validity_Checks -- | |
2429 | ------------------------------------- | |
2430 | ||
2431 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2432 | Subp_Decl : Node_Id; | |
b73adb97 | 2433 | |
4a9e7f0c | 2434 | procedure Add_Validity_Check |
ed695684 | 2435 | (Formal : Entity_Id; |
2436 | Prag_Nam : Name_Id; | |
4a9e7f0c | 2437 | For_Result : Boolean := False); |
2438 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
ed695684 | 2439 | -- of Formal. Prag_Nam denotes the pre or post condition pragma name. |
4a9e7f0c | 2440 | -- Set flag For_Result when to verify the result of a function. |
b73adb97 | 2441 | |
b73adb97 | 2442 | ------------------------ |
2443 | -- Add_Validity_Check -- | |
2444 | ------------------------ | |
2445 | ||
2446 | procedure Add_Validity_Check | |
ed695684 | 2447 | (Formal : Entity_Id; |
2448 | Prag_Nam : Name_Id; | |
b73adb97 | 2449 | For_Result : Boolean := False) |
2450 | is | |
ed695684 | 2451 | procedure Build_Pre_Post_Condition (Expr : Node_Id); |
2452 | -- Create a pre/postcondition pragma that tests expression Expr | |
2453 | ||
2454 | ------------------------------ | |
2455 | -- Build_Pre_Post_Condition -- | |
2456 | ------------------------------ | |
2457 | ||
2458 | procedure Build_Pre_Post_Condition (Expr : Node_Id) is | |
2459 | Loc : constant Source_Ptr := Sloc (Subp); | |
2460 | Decls : List_Id; | |
2461 | Prag : Node_Id; | |
2462 | ||
2463 | begin | |
2464 | Prag := | |
2465 | Make_Pragma (Loc, | |
c33cb5ff | 2466 | Chars => Prag_Nam, |
ed695684 | 2467 | Pragma_Argument_Associations => New_List ( |
2468 | Make_Pragma_Argument_Association (Loc, | |
2469 | Chars => Name_Check, | |
2470 | Expression => Expr))); | |
2471 | ||
2472 | -- Add a message unless exception messages are suppressed | |
2473 | ||
2474 | if not Exception_Locations_Suppressed then | |
2475 | Append_To (Pragma_Argument_Associations (Prag), | |
2476 | Make_Pragma_Argument_Association (Loc, | |
2477 | Chars => Name_Message, | |
2478 | Expression => | |
2479 | Make_String_Literal (Loc, | |
2480 | Strval => "failed " | |
2481 | & Get_Name_String (Prag_Nam) | |
2482 | & " from " | |
2483 | & Build_Location_String (Loc)))); | |
2484 | end if; | |
2485 | ||
2486 | -- Insert the pragma in the tree | |
2487 | ||
2488 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2489 | Add_Global_Declaration (Prag); | |
2490 | Analyze (Prag); | |
2491 | ||
2492 | -- PPC pragmas associated with subprogram bodies must be inserted | |
2493 | -- in the declarative part of the body. | |
2494 | ||
2495 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2496 | Decls := Declarations (Subp_Decl); | |
2497 | ||
2498 | if No (Decls) then | |
2499 | Decls := New_List; | |
2500 | Set_Declarations (Subp_Decl, Decls); | |
2501 | end if; | |
2502 | ||
2503 | Prepend_To (Decls, Prag); | |
2504 | Analyze (Prag); | |
2505 | ||
2506 | -- For subprogram declarations insert the PPC pragma right after | |
2507 | -- the declarative node. | |
2508 | ||
2509 | else | |
2510 | Insert_After_And_Analyze (Subp_Decl, Prag); | |
2511 | end if; | |
2512 | end Build_Pre_Post_Condition; | |
2513 | ||
2514 | -- Local variables | |
2515 | ||
4a9e7f0c | 2516 | Loc : constant Source_Ptr := Sloc (Subp); |
ed695684 | 2517 | Typ : constant Entity_Id := Etype (Formal); |
b73adb97 | 2518 | Check : Node_Id; |
2519 | Nam : Name_Id; | |
2520 | ||
ed695684 | 2521 | -- Start of processing for Add_Validity_Check |
2522 | ||
b73adb97 | 2523 | begin |
9916a361 | 2524 | -- For scalars, generate 'Valid test |
b73adb97 | 2525 | |
2526 | if Is_Scalar_Type (Typ) then | |
2527 | Nam := Name_Valid; | |
9916a361 | 2528 | |
2529 | -- For any non-scalar with scalar parts, generate 'Valid_Scalars test | |
2530 | ||
2531 | elsif Scalar_Part_Present (Typ) then | |
b73adb97 | 2532 | Nam := Name_Valid_Scalars; |
9916a361 | 2533 | |
2534 | -- No test needed for other cases (no scalars to test) | |
2535 | ||
b73adb97 | 2536 | else |
2537 | return; | |
2538 | end if; | |
2539 | ||
2540 | -- Step 1: Create the expression to verify the validity of the | |
2541 | -- context. | |
2542 | ||
ed695684 | 2543 | Check := New_Occurrence_Of (Formal, Loc); |
b73adb97 | 2544 | |
2545 | -- When processing a function result, use 'Result. Generate | |
2546 | -- Context'Result | |
2547 | ||
2548 | if For_Result then | |
2549 | Check := | |
2550 | Make_Attribute_Reference (Loc, | |
2551 | Prefix => Check, | |
2552 | Attribute_Name => Name_Result); | |
2553 | end if; | |
2554 | ||
2555 | -- Generate: | |
2556 | -- Context['Result]'Valid[_Scalars] | |
2557 | ||
2558 | Check := | |
2559 | Make_Attribute_Reference (Loc, | |
2560 | Prefix => Check, | |
2561 | Attribute_Name => Nam); | |
2562 | ||
4a9e7f0c | 2563 | -- Step 2: Create a pre or post condition pragma |
2564 | ||
ed695684 | 2565 | Build_Pre_Post_Condition (Check); |
4a9e7f0c | 2566 | end Add_Validity_Check; |
2567 | ||
4a9e7f0c | 2568 | -- Local variables |
2569 | ||
2570 | Formal : Entity_Id; | |
4a9e7f0c | 2571 | Subp_Spec : Node_Id; |
2572 | ||
3b045963 | 2573 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2574 | |
2575 | begin | |
4a9e7f0c | 2576 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2577 | |
4a9e7f0c | 2578 | Subp_Spec := Parent (Subp); |
a45d946f | 2579 | |
4a9e7f0c | 2580 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2581 | Subp_Spec := Parent (Subp_Spec); | |
2582 | end if; | |
a45d946f | 2583 | |
4a9e7f0c | 2584 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2585 | |
b73adb97 | 2586 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2587 | |
2588 | -- Do not process formal subprograms because the corresponding actual | |
2589 | -- will receive the proper checks when the instance is analyzed. | |
2590 | ||
2591 | or else Is_Formal_Subprogram (Subp) | |
2592 | ||
95ac2d90 | 2593 | -- Do not process imported subprograms since pre and postconditions |
a45d946f | 2594 | -- are never verified on routines coming from a different language. |
4a9e7f0c | 2595 | |
b73adb97 | 2596 | or else Is_Imported (Subp) |
2597 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2598 | |
a45d946f | 2599 | -- The PPC pragmas generated by this routine do not correspond to |
2600 | -- source aspects, therefore they cannot be applied to abstract | |
2601 | -- subprograms. | |
4a9e7f0c | 2602 | |
7c443ae8 | 2603 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2604 | |
a45d946f | 2605 | -- Do not consider subprogram renaminds because the renamed entity |
2606 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2607 | |
2608 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2609 | ||
a45d946f | 2610 | -- Do not process null procedures because there is no benefit of |
2611 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2612 | |
2613 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2614 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2615 | then |
2616 | return; | |
2617 | end if; | |
2618 | ||
4a9e7f0c | 2619 | -- Inspect all the formals applying aliasing and scalar initialization |
2620 | -- checks where applicable. | |
b73adb97 | 2621 | |
2622 | Formal := First_Formal (Subp); | |
2623 | while Present (Formal) loop | |
4a9e7f0c | 2624 | |
2625 | -- Generate the following scalar initialization checks for each | |
2626 | -- formal parameter: | |
2627 | ||
2628 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2629 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2630 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2631 | ||
2632 | if Check_Validity_Of_Parameters then | |
2633 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2634 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2635 | end if; | |
2636 | ||
2637 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2638 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2639 | end if; | |
b73adb97 | 2640 | end if; |
2641 | ||
b73adb97 | 2642 | Next_Formal (Formal); |
2643 | end loop; | |
2644 | ||
a45d946f | 2645 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2646 | |
2647 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2648 | |
a45d946f | 2649 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2650 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2651 | end if; |
3b045963 | 2652 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2653 | |
7aafae1c | 2654 | --------------------------- |
2655 | -- Apply_Predicate_Check -- | |
2656 | --------------------------- | |
2657 | ||
e60047e5 | 2658 | procedure Apply_Predicate_Check |
2659 | (N : Node_Id; | |
2660 | Typ : Entity_Id; | |
2661 | Fun : Entity_Id := Empty) | |
2662 | is | |
301d5ec3 | 2663 | S : Entity_Id; |
9e58d7ed | 2664 | |
7aafae1c | 2665 | begin |
da2270e7 | 2666 | if Predicate_Checks_Suppressed (Empty) then |
2667 | return; | |
301d5ec3 | 2668 | |
2c011bc5 | 2669 | elsif Predicates_Ignored (Typ) then |
2670 | return; | |
2671 | ||
da2270e7 | 2672 | elsif Present (Predicate_Function (Typ)) then |
301d5ec3 | 2673 | S := Current_Scope; |
9e58d7ed | 2674 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2675 | S := Scope (S); |
2676 | end loop; | |
2677 | ||
ea822fd4 | 2678 | -- A predicate check does not apply within internally generated |
2679 | -- subprograms, such as TSS functions. | |
2680 | ||
2681 | if Within_Internal_Subprogram then | |
301d5ec3 | 2682 | return; |
22631b41 | 2683 | |
96a2d100 | 2684 | -- If the check appears within the predicate function itself, it |
2685 | -- means that the user specified a check whose formal is the | |
2686 | -- predicated subtype itself, rather than some covering type. This | |
2687 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2688 | |
0e9014a7 | 2689 | elsif Present (S) and then S = Predicate_Function (Typ) then |
e60047e5 | 2690 | Error_Msg_NE |
0c4abd51 | 2691 | ("predicate check includes a call to& that requires a " |
2692 | & "predicate check??", Parent (N), Fun); | |
96a2d100 | 2693 | Error_Msg_N |
cb97ae5c | 2694 | ("\this will result in infinite recursion??", Parent (N)); |
e60047e5 | 2695 | |
2696 | if Is_First_Subtype (Typ) then | |
2697 | Error_Msg_NE | |
0c4abd51 | 2698 | ("\use an explicit subtype of& to carry the predicate", |
e60047e5 | 2699 | Parent (N), Typ); |
2700 | end if; | |
2701 | ||
96a2d100 | 2702 | Insert_Action (N, |
61016a7a | 2703 | Make_Raise_Storage_Error (Sloc (N), |
2704 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2705 | |
64cc9e5d | 2706 | -- Here for normal case of predicate active |
e6281d47 | 2707 | |
61016a7a | 2708 | else |
b04165c4 | 2709 | -- If the type has a static predicate and the expression is known |
2710 | -- at compile time, see if the expression satisfies the predicate. | |
3a75f20b | 2711 | |
2712 | Check_Expression_Against_Static_Predicate (N, Typ); | |
e6281d47 | 2713 | |
e34cd69d | 2714 | if not Expander_Active then |
2715 | return; | |
2716 | end if; | |
2717 | ||
2718 | -- For an entity of the type, generate a call to the predicate | |
2719 | -- function, unless its type is an actual subtype, which is not | |
2720 | -- visible outside of the enclosing subprogram. | |
2721 | ||
2722 | if Is_Entity_Name (N) | |
2723 | and then not Is_Actual_Subtype (Typ) | |
2724 | then | |
da2270e7 | 2725 | Insert_Action (N, |
2726 | Make_Predicate_Check | |
2727 | (Typ, New_Occurrence_Of (Entity (N), Sloc (N)))); | |
2728 | ||
f9906591 | 2729 | -- If the expression is not an entity it may have side effects, |
bc885df9 | 2730 | -- and the following call will create an object declaration for |
2731 | -- it. We disable checks during its analysis, to prevent an | |
2732 | -- infinite recursion. | |
da2270e7 | 2733 | |
e3b910d8 | 2734 | -- If the prefix is an aggregate in an assignment, apply the |
2735 | -- check to the LHS after assignment, rather than create a | |
2736 | -- redundant temporary. This is only necessary in rare cases | |
2737 | -- of array types (including strings) initialized with an | |
2738 | -- aggregate with an "others" clause, either coming from source | |
2739 | -- or generated by an Initialize_Scalars pragma. | |
2740 | ||
2741 | elsif Nkind (N) = N_Aggregate | |
2742 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
2743 | then | |
2744 | Insert_Action_After (Parent (N), | |
2745 | Make_Predicate_Check | |
2746 | (Typ, Duplicate_Subexpr (Name (Parent (N))))); | |
2747 | ||
da2270e7 | 2748 | else |
2749 | Insert_Action (N, | |
bc885df9 | 2750 | Make_Predicate_Check |
2751 | (Typ, Duplicate_Subexpr (N)), Suppress => All_Checks); | |
da2270e7 | 2752 | end if; |
301d5ec3 | 2753 | end if; |
7aafae1c | 2754 | end if; |
2755 | end Apply_Predicate_Check; | |
2756 | ||
ee6ba406 | 2757 | ----------------------- |
2758 | -- Apply_Range_Check -- | |
2759 | ----------------------- | |
2760 | ||
2761 | procedure Apply_Range_Check | |
2762 | (Ck_Node : Node_Id; | |
2763 | Target_Typ : Entity_Id; | |
2764 | Source_Typ : Entity_Id := Empty) | |
2765 | is | |
2766 | begin | |
2767 | Apply_Selected_Range_Checks | |
2768 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2769 | end Apply_Range_Check; | |
2770 | ||
2771 | ------------------------------ | |
2772 | -- Apply_Scalar_Range_Check -- | |
2773 | ------------------------------ | |
2774 | ||
feff2f05 | 2775 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2776 | -- off if it is already set on. | |
ee6ba406 | 2777 | |
2778 | procedure Apply_Scalar_Range_Check | |
2779 | (Expr : Node_Id; | |
2780 | Target_Typ : Entity_Id; | |
2781 | Source_Typ : Entity_Id := Empty; | |
2782 | Fixed_Int : Boolean := False) | |
2783 | is | |
2784 | Parnt : constant Node_Id := Parent (Expr); | |
2785 | S_Typ : Entity_Id; | |
2786 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2787 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
ee6ba406 | 2788 | |
2789 | Is_Subscr_Ref : Boolean; | |
2790 | -- Set true if Expr is a subscript | |
2791 | ||
2792 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2793 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2794 | -- case we do not attempt to do an analysis of the value against the | |
2795 | -- range of the subscript, since we don't know the actual subtype. | |
2796 | ||
2797 | Int_Real : Boolean; | |
feff2f05 | 2798 | -- Set to True if Expr should be regarded as a real value even though |
2799 | -- the type of Expr might be discrete. | |
ee6ba406 | 2800 | |
a0a15971 | 2801 | procedure Bad_Value (Warn : Boolean := False); |
2802 | -- Procedure called if value is determined to be out of range. Warn is | |
2803 | -- True to force a warning instead of an error, even when SPARK_Mode is | |
2804 | -- On. | |
ee6ba406 | 2805 | |
9dfe12ae | 2806 | --------------- |
2807 | -- Bad_Value -- | |
2808 | --------------- | |
2809 | ||
a0a15971 | 2810 | procedure Bad_Value (Warn : Boolean := False) is |
ee6ba406 | 2811 | begin |
2812 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2813 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
a0a15971 | 2814 | Ent => Target_Typ, |
2815 | Typ => Target_Typ, | |
2816 | Warn => Warn); | |
ee6ba406 | 2817 | end Bad_Value; |
2818 | ||
9dfe12ae | 2819 | -- Start of processing for Apply_Scalar_Range_Check |
2820 | ||
ee6ba406 | 2821 | begin |
2af58f67 | 2822 | -- Return if check obviously not needed |
ee6ba406 | 2823 | |
2af58f67 | 2824 | if |
2825 | -- Not needed inside generic | |
ee6ba406 | 2826 | |
2af58f67 | 2827 | Inside_A_Generic |
2828 | ||
2829 | -- Not needed if previous error | |
2830 | ||
2831 | or else Target_Typ = Any_Type | |
2832 | or else Nkind (Expr) = N_Error | |
2833 | ||
2834 | -- Not needed for non-scalar type | |
2835 | ||
2836 | or else not Is_Scalar_Type (Target_Typ) | |
2837 | ||
2838 | -- Not needed if we know node raises CE already | |
2839 | ||
2840 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2841 | then |
2842 | return; | |
2843 | end if; | |
2844 | ||
2845 | -- Now, see if checks are suppressed | |
2846 | ||
2847 | Is_Subscr_Ref := | |
2848 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2849 | ||
2850 | if Is_Subscr_Ref then | |
2851 | Arr := Prefix (Parnt); | |
2852 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2853 | |
a3a76ccc | 2854 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2855 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2856 | end if; |
ee6ba406 | 2857 | end if; |
2858 | ||
2859 | if not Do_Range_Check (Expr) then | |
2860 | ||
2861 | -- Subscript reference. Check for Index_Checks suppressed | |
2862 | ||
2863 | if Is_Subscr_Ref then | |
2864 | ||
2865 | -- Check array type and its base type | |
2866 | ||
2867 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2868 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2869 | then |
2870 | return; | |
2871 | ||
2872 | -- Check array itself if it is an entity name | |
2873 | ||
2874 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2875 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2876 | then |
2877 | return; | |
2878 | ||
2879 | -- Check expression itself if it is an entity name | |
2880 | ||
2881 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2882 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2883 | then |
2884 | return; | |
2885 | end if; | |
2886 | ||
2887 | -- All other cases, check for Range_Checks suppressed | |
2888 | ||
2889 | else | |
2890 | -- Check target type and its base type | |
2891 | ||
2892 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2893 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2894 | then |
2895 | return; | |
2896 | ||
2897 | -- Check expression itself if it is an entity name | |
2898 | ||
2899 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2900 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2901 | then |
2902 | return; | |
2903 | ||
feff2f05 | 2904 | -- If Expr is part of an assignment statement, then check left |
2905 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2906 | |
2907 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2908 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2909 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2910 | then |
2911 | return; | |
2912 | end if; | |
2913 | end if; | |
2914 | end if; | |
2915 | ||
9dfe12ae | 2916 | -- Do not set range checks if they are killed |
2917 | ||
2918 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2919 | and then Kill_Range_Check (Expr) | |
2920 | then | |
2921 | return; | |
2922 | end if; | |
2923 | ||
2924 | -- Do not set range checks for any values from System.Scalar_Values | |
39a0c1d3 | 2925 | -- since the whole idea of such values is to avoid checking them. |
9dfe12ae | 2926 | |
2927 | if Is_Entity_Name (Expr) | |
2928 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2929 | then | |
2930 | return; | |
2931 | end if; | |
2932 | ||
ee6ba406 | 2933 | -- Now see if we need a check |
2934 | ||
2935 | if No (Source_Typ) then | |
2936 | S_Typ := Etype (Expr); | |
2937 | else | |
2938 | S_Typ := Source_Typ; | |
2939 | end if; | |
2940 | ||
2941 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
2942 | return; | |
2943 | end if; | |
2944 | ||
2945 | Is_Unconstrained_Subscr_Ref := | |
2946 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
2947 | ||
b40670e1 | 2948 | -- Special checks for floating-point type |
ee6ba406 | 2949 | |
b40670e1 | 2950 | if Is_Floating_Point_Type (S_Typ) then |
2951 | ||
2952 | -- Always do a range check if the source type includes infinities and | |
2953 | -- the target type does not include infinities. We do not do this if | |
2954 | -- range checks are killed. | |
cb388b10 | 2955 | -- If the expression is a literal and the bounds of the type are |
2956 | -- static constants it may be possible to optimize the check. | |
b40670e1 | 2957 | |
2958 | if Has_Infinities (S_Typ) | |
2959 | and then not Has_Infinities (Target_Typ) | |
2960 | then | |
cb388b10 | 2961 | -- If the expression is a literal and the bounds of the type are |
2962 | -- static constants it may be possible to optimize the check. | |
2963 | ||
2964 | if Nkind (Expr) = N_Real_Literal then | |
2965 | declare | |
2966 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
2967 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
2968 | ||
2969 | begin | |
2970 | if Compile_Time_Known_Value (Tlo) | |
2971 | and then Compile_Time_Known_Value (Thi) | |
2972 | and then Expr_Value_R (Expr) >= Expr_Value_R (Tlo) | |
2973 | and then Expr_Value_R (Expr) <= Expr_Value_R (Thi) | |
2974 | then | |
2975 | return; | |
2976 | else | |
2977 | Enable_Range_Check (Expr); | |
2978 | end if; | |
2979 | end; | |
2980 | ||
2981 | else | |
2982 | Enable_Range_Check (Expr); | |
2983 | end if; | |
b40670e1 | 2984 | end if; |
ee6ba406 | 2985 | end if; |
2986 | ||
feff2f05 | 2987 | -- Return if we know expression is definitely in the range of the target |
2988 | -- type as determined by Determine_Range. Right now we only do this for | |
2989 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 2990 | |
f2a06be9 | 2991 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 2992 | |
54022749 | 2993 | -- In GNATprove_Mode, also deal with the case of a conversion from |
2994 | -- floating-point to integer. It is only possible because analysis | |
2995 | -- in GNATprove rules out the possibility of a NaN or infinite value. | |
2996 | ||
feff2f05 | 2997 | -- Note: skip this if we are given a source_typ, since the point of |
2998 | -- supplying a Source_Typ is to stop us looking at the expression. | |
2999 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 3000 | |
3001 | if Is_Discrete_Type (Target_Typ) | |
54022749 | 3002 | and then (Is_Discrete_Type (Etype (Expr)) |
3003 | or else (GNATprove_Mode | |
3004 | and then Is_Floating_Point_Type (Etype (Expr)))) | |
ee6ba406 | 3005 | and then not Is_Unconstrained_Subscr_Ref |
3006 | and then No (Source_Typ) | |
3007 | then | |
3008 | declare | |
ee6ba406 | 3009 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); |
552d7cbc | 3010 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); |
ee6ba406 | 3011 | |
3012 | begin | |
3013 | if Compile_Time_Known_Value (Tlo) | |
3014 | and then Compile_Time_Known_Value (Thi) | |
3015 | then | |
9dfe12ae | 3016 | declare |
5bb74b99 | 3017 | OK : Boolean := False; -- initialize to prevent warning |
9dfe12ae | 3018 | Hiv : constant Uint := Expr_Value (Thi); |
552d7cbc | 3019 | Lov : constant Uint := Expr_Value (Tlo); |
5bb74b99 | 3020 | Hi : Uint := No_Uint; |
3021 | Lo : Uint := No_Uint; | |
ee6ba406 | 3022 | |
9dfe12ae | 3023 | begin |
552d7cbc | 3024 | -- If range is null, we for sure have a constraint error (we |
3025 | -- don't even need to look at the value involved, since all | |
3026 | -- possible values will raise CE). | |
9dfe12ae | 3027 | |
3028 | if Lov > Hiv then | |
f4f2bf51 | 3029 | |
a0a15971 | 3030 | -- When SPARK_Mode is On, force a warning instead of |
3031 | -- an error in that case, as this likely corresponds | |
3032 | -- to deactivated code. | |
3033 | ||
3034 | Bad_Value (Warn => SPARK_Mode = On); | |
3035 | ||
3036 | -- In GNATprove mode, we enable the range check so that | |
3037 | -- GNATprove will issue a message if it cannot be proved. | |
f4f2bf51 | 3038 | |
3039 | if GNATprove_Mode then | |
3040 | Enable_Range_Check (Expr); | |
f4f2bf51 | 3041 | end if; |
3042 | ||
9dfe12ae | 3043 | return; |
3044 | end if; | |
3045 | ||
3046 | -- Otherwise determine range of value | |
3047 | ||
54022749 | 3048 | if Is_Discrete_Type (Etype (Expr)) then |
552d7cbc | 3049 | Determine_Range |
3050 | (Expr, OK, Lo, Hi, Assume_Valid => True); | |
54022749 | 3051 | |
3052 | -- When converting a float to an integer type, determine the | |
3053 | -- range in real first, and then convert the bounds using | |
3054 | -- UR_To_Uint which correctly rounds away from zero when | |
3055 | -- half way between two integers, as required by normal | |
3056 | -- Ada 95 rounding semantics. It is only possible because | |
3057 | -- analysis in GNATprove rules out the possibility of a NaN | |
3058 | -- or infinite value. | |
3059 | ||
3060 | elsif GNATprove_Mode | |
3061 | and then Is_Floating_Point_Type (Etype (Expr)) | |
3062 | then | |
3063 | declare | |
54022749 | 3064 | Hir : Ureal; |
552d7cbc | 3065 | Lor : Ureal; |
3066 | ||
54022749 | 3067 | begin |
552d7cbc | 3068 | Determine_Range_R |
3069 | (Expr, OK, Lor, Hir, Assume_Valid => True); | |
54022749 | 3070 | |
3071 | if OK then | |
3072 | Lo := UR_To_Uint (Lor); | |
3073 | Hi := UR_To_Uint (Hir); | |
3074 | end if; | |
3075 | end; | |
3076 | end if; | |
9dfe12ae | 3077 | |
3078 | if OK then | |
3079 | ||
3080 | -- If definitely in range, all OK | |
ee6ba406 | 3081 | |
ee6ba406 | 3082 | if Lo >= Lov and then Hi <= Hiv then |
3083 | return; | |
3084 | ||
9dfe12ae | 3085 | -- If definitely not in range, warn |
3086 | ||
ee6ba406 | 3087 | elsif Lov > Hi or else Hiv < Lo then |
e9dae6c3 | 3088 | |
c59854a0 | 3089 | -- Ignore out of range values for System.Priority in |
3090 | -- CodePeer mode since the actual target compiler may | |
3091 | -- provide a wider range. | |
3092 | ||
3093 | if not CodePeer_Mode | |
3094 | or else Target_Typ /= RTE (RE_Priority) | |
3095 | then | |
3096 | Bad_Value; | |
3097 | end if; | |
3098 | ||
ee6ba406 | 3099 | return; |
9dfe12ae | 3100 | |
3101 | -- Otherwise we don't know | |
3102 | ||
3103 | else | |
3104 | null; | |
ee6ba406 | 3105 | end if; |
9dfe12ae | 3106 | end if; |
3107 | end; | |
ee6ba406 | 3108 | end if; |
3109 | end; | |
3110 | end if; | |
3111 | ||
3112 | Int_Real := | |
3113 | Is_Floating_Point_Type (S_Typ) | |
3114 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
3115 | ||
3116 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 3117 | -- range of the target type. Note that if S_Typ is within the bounds |
3118 | -- of Target_Typ then this must be the case. This check is meaningful | |
3119 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 3120 | |
3121 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 3122 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 3123 | and then |
7a1dabb3 | 3124 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ac5f5168 | 3125 | |
3126 | -- Also check if the expression itself is in the range of the | |
3127 | -- target type if it is a known at compile time value. We skip | |
3128 | -- this test if S_Typ is set since for OUT and IN OUT parameters | |
3129 | -- the Expr itself is not relevant to the checking. | |
3130 | ||
ee6ba406 | 3131 | or else |
ac5f5168 | 3132 | (No (Source_Typ) |
3133 | and then Is_In_Range (Expr, Target_Typ, | |
3134 | Assume_Valid => True, | |
3135 | Fixed_Int => Fixed_Int, | |
3136 | Int_Real => Int_Real))) | |
ee6ba406 | 3137 | then |
3138 | return; | |
3139 | ||
9c486805 | 3140 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
3141 | Assume_Valid => True, | |
3142 | Fixed_Int => Fixed_Int, | |
3143 | Int_Real => Int_Real) | |
3144 | then | |
ee6ba406 | 3145 | Bad_Value; |
3146 | return; | |
3147 | ||
b40670e1 | 3148 | -- Floating-point case |
feff2f05 | 3149 | -- In the floating-point case, we only do range checks if the type is |
3150 | -- constrained. We definitely do NOT want range checks for unconstrained | |
29d958a7 | 3151 | -- types, since we want to have infinities, except when |
3152 | -- Check_Float_Overflow is set. | |
ee6ba406 | 3153 | |
9dfe12ae | 3154 | elsif Is_Floating_Point_Type (S_Typ) then |
29d958a7 | 3155 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then |
9dfe12ae | 3156 | Enable_Range_Check (Expr); |
3157 | end if; | |
ee6ba406 | 3158 | |
9dfe12ae | 3159 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 3160 | |
3161 | else | |
3162 | Enable_Range_Check (Expr); | |
3163 | return; | |
3164 | end if; | |
ee6ba406 | 3165 | end Apply_Scalar_Range_Check; |
3166 | ||
3167 | ---------------------------------- | |
3168 | -- Apply_Selected_Length_Checks -- | |
3169 | ---------------------------------- | |
3170 | ||
3171 | procedure Apply_Selected_Length_Checks | |
3172 | (Ck_Node : Node_Id; | |
3173 | Target_Typ : Entity_Id; | |
3174 | Source_Typ : Entity_Id; | |
3175 | Do_Static : Boolean) | |
3176 | is | |
2b4f2458 | 3177 | Checks_On : constant Boolean := |
3178 | not Index_Checks_Suppressed (Target_Typ) | |
3179 | or else | |
3180 | not Length_Checks_Suppressed (Target_Typ); | |
3181 | ||
3182 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
3183 | ||
ee6ba406 | 3184 | Cond : Node_Id; |
ee6ba406 | 3185 | R_Cno : Node_Id; |
2b4f2458 | 3186 | R_Result : Check_Result; |
ee6ba406 | 3187 | |
3188 | begin | |
18cb6d78 | 3189 | -- Only apply checks when generating code |
f0d65dae | 3190 | |
4098232e | 3191 | -- Note: this means that we lose some useful warnings if the expander |
f0d65dae | 3192 | -- is not active. |
4098232e | 3193 | |
18cb6d78 | 3194 | if not Expander_Active then |
ee6ba406 | 3195 | return; |
3196 | end if; | |
3197 | ||
3198 | R_Result := | |
3199 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3200 | ||
3201 | for J in 1 .. 2 loop | |
ee6ba406 | 3202 | R_Cno := R_Result (J); |
3203 | exit when No (R_Cno); | |
3204 | ||
3205 | -- A length check may mention an Itype which is attached to a | |
3206 | -- subsequent node. At the top level in a package this can cause | |
3207 | -- an order-of-elaboration problem, so we make sure that the itype | |
3208 | -- is referenced now. | |
3209 | ||
3210 | if Ekind (Current_Scope) = E_Package | |
3211 | and then Is_Compilation_Unit (Current_Scope) | |
3212 | then | |
3213 | Ensure_Defined (Target_Typ, Ck_Node); | |
3214 | ||
3215 | if Present (Source_Typ) then | |
3216 | Ensure_Defined (Source_Typ, Ck_Node); | |
3217 | ||
3218 | elsif Is_Itype (Etype (Ck_Node)) then | |
3219 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
3220 | end if; | |
3221 | end if; | |
3222 | ||
feff2f05 | 3223 | -- If the item is a conditional raise of constraint error, then have |
3224 | -- a look at what check is being performed and ??? | |
ee6ba406 | 3225 | |
3226 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3227 | and then Present (Condition (R_Cno)) | |
3228 | then | |
3229 | Cond := Condition (R_Cno); | |
3230 | ||
0577b0b1 | 3231 | -- Case where node does not now have a dynamic check |
ee6ba406 | 3232 | |
0577b0b1 | 3233 | if not Has_Dynamic_Length_Check (Ck_Node) then |
3234 | ||
3235 | -- If checks are on, just insert the check | |
3236 | ||
3237 | if Checks_On then | |
3238 | Insert_Action (Ck_Node, R_Cno); | |
3239 | ||
3240 | if not Do_Static then | |
3241 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
3242 | end if; | |
3243 | ||
3244 | -- If checks are off, then analyze the length check after | |
3245 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 3246 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 3247 | -- compile time warning in this case. |
3248 | ||
3249 | else | |
3250 | Set_Parent (R_Cno, Ck_Node); | |
3251 | Analyze (R_Cno); | |
ee6ba406 | 3252 | end if; |
ee6ba406 | 3253 | end if; |
3254 | ||
3255 | -- Output a warning if the condition is known to be True | |
3256 | ||
3257 | if Is_Entity_Name (Cond) | |
3258 | and then Entity (Cond) = Standard_True | |
3259 | then | |
3260 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3261 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 3262 | CE_Length_Check_Failed, |
ee6ba406 | 3263 | Ent => Target_Typ, |
3264 | Typ => Target_Typ); | |
3265 | ||
3266 | -- If we were only doing a static check, or if checks are not | |
3267 | -- on, then we want to delete the check, since it is not needed. | |
3268 | -- We do this by replacing the if statement by a null statement | |
3269 | ||
3270 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3271 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3272 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3273 | end if; | |
3274 | ||
3275 | else | |
3276 | Install_Static_Check (R_Cno, Loc); | |
3277 | end if; | |
ee6ba406 | 3278 | end loop; |
ee6ba406 | 3279 | end Apply_Selected_Length_Checks; |
3280 | ||
3281 | --------------------------------- | |
3282 | -- Apply_Selected_Range_Checks -- | |
3283 | --------------------------------- | |
3284 | ||
3285 | procedure Apply_Selected_Range_Checks | |
3286 | (Ck_Node : Node_Id; | |
3287 | Target_Typ : Entity_Id; | |
3288 | Source_Typ : Entity_Id; | |
3289 | Do_Static : Boolean) | |
3290 | is | |
ee6ba406 | 3291 | Checks_On : constant Boolean := |
f9bcba0d | 3292 | not Index_Checks_Suppressed (Target_Typ) |
5372d110 | 3293 | or else |
3294 | not Range_Checks_Suppressed (Target_Typ); | |
f9bcba0d | 3295 | |
2b4f2458 | 3296 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
3297 | ||
f9bcba0d | 3298 | Cond : Node_Id; |
3299 | R_Cno : Node_Id; | |
3300 | R_Result : Check_Result; | |
ee6ba406 | 3301 | |
3302 | begin | |
f0d65dae | 3303 | -- Only apply checks when generating code. In GNATprove mode, we do not |
3304 | -- apply the checks, but we still call Selected_Range_Checks to possibly | |
3305 | -- issue errors on SPARK code when a run-time error can be detected at | |
3306 | -- compile time. | |
3307 | ||
3308 | if not GNATprove_Mode then | |
3309 | if not Expander_Active or not Checks_On then | |
3310 | return; | |
3311 | end if; | |
ee6ba406 | 3312 | end if; |
3313 | ||
3314 | R_Result := | |
3315 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3316 | ||
f0d65dae | 3317 | if GNATprove_Mode then |
3318 | return; | |
3319 | end if; | |
3320 | ||
ee6ba406 | 3321 | for J in 1 .. 2 loop |
ee6ba406 | 3322 | R_Cno := R_Result (J); |
3323 | exit when No (R_Cno); | |
3324 | ||
f9bcba0d | 3325 | -- The range check requires runtime evaluation. Depending on what its |
3326 | -- triggering condition is, the check may be converted into a compile | |
3327 | -- time constraint check. | |
ee6ba406 | 3328 | |
3329 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3330 | and then Present (Condition (R_Cno)) | |
3331 | then | |
3332 | Cond := Condition (R_Cno); | |
3333 | ||
f9bcba0d | 3334 | -- Insert the range check before the related context. Note that |
3335 | -- this action analyses the triggering condition. | |
ee6ba406 | 3336 | |
f9bcba0d | 3337 | Insert_Action (Ck_Node, R_Cno); |
3338 | ||
3339 | -- This old code doesn't make sense, why is the context flagged as | |
3340 | -- requiring dynamic range checks now in the middle of generating | |
3341 | -- them ??? | |
3342 | ||
3343 | if not Do_Static then | |
3344 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
ee6ba406 | 3345 | end if; |
3346 | ||
f9bcba0d | 3347 | -- The triggering condition evaluates to True, the range check |
3348 | -- can be converted into a compile time constraint check. | |
ee6ba406 | 3349 | |
3350 | if Is_Entity_Name (Cond) | |
3351 | and then Entity (Cond) = Standard_True | |
3352 | then | |
feff2f05 | 3353 | -- Since an N_Range is technically not an expression, we have |
3354 | -- to set one of the bounds to C_E and then just flag the | |
3355 | -- N_Range. The warning message will point to the lower bound | |
3356 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3357 | |
3358 | if Nkind (Ck_Node) = N_Range then | |
3359 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3360 | (Low_Bound (Ck_Node), |
3361 | "static range out of bounds of}??", | |
f15731c4 | 3362 | CE_Range_Check_Failed, |
ee6ba406 | 3363 | Ent => Target_Typ, |
3364 | Typ => Target_Typ); | |
3365 | ||
3366 | Set_Raises_Constraint_Error (Ck_Node); | |
3367 | ||
3368 | else | |
3369 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3370 | (Ck_Node, |
1581f2d7 | 3371 | "static value out of range of}??", |
f15731c4 | 3372 | CE_Range_Check_Failed, |
ee6ba406 | 3373 | Ent => Target_Typ, |
3374 | Typ => Target_Typ); | |
3375 | end if; | |
3376 | ||
3377 | -- If we were only doing a static check, or if checks are not | |
3378 | -- on, then we want to delete the check, since it is not needed. | |
3379 | -- We do this by replacing the if statement by a null statement | |
3380 | ||
3fabf0ca | 3381 | elsif Do_Static then |
00c403ee | 3382 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3383 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3384 | end if; | |
3385 | ||
4ee78e36 | 3386 | -- The range check raises Constraint_Error explicitly |
f9bcba0d | 3387 | |
ee6ba406 | 3388 | else |
3389 | Install_Static_Check (R_Cno, Loc); | |
3390 | end if; | |
ee6ba406 | 3391 | end loop; |
ee6ba406 | 3392 | end Apply_Selected_Range_Checks; |
3393 | ||
3394 | ------------------------------- | |
3395 | -- Apply_Static_Length_Check -- | |
3396 | ------------------------------- | |
3397 | ||
3398 | procedure Apply_Static_Length_Check | |
3399 | (Expr : Node_Id; | |
3400 | Target_Typ : Entity_Id; | |
3401 | Source_Typ : Entity_Id := Empty) | |
3402 | is | |
3403 | begin | |
3404 | Apply_Selected_Length_Checks | |
3405 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3406 | end Apply_Static_Length_Check; | |
3407 | ||
3408 | ------------------------------------- | |
3409 | -- Apply_Subscript_Validity_Checks -- | |
3410 | ------------------------------------- | |
3411 | ||
3412 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3413 | Sub : Node_Id; | |
3414 | ||
3415 | begin | |
3416 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3417 | ||
3418 | -- Loop through subscripts | |
3419 | ||
3420 | Sub := First (Expressions (Expr)); | |
3421 | while Present (Sub) loop | |
3422 | ||
feff2f05 | 3423 | -- Check one subscript. Note that we do not worry about enumeration |
3424 | -- type with holes, since we will convert the value to a Pos value | |
3425 | -- for the subscript, and that convert will do the necessary validity | |
3426 | -- check. | |
ee6ba406 | 3427 | |
3428 | Ensure_Valid (Sub, Holes_OK => True); | |
3429 | ||
3430 | -- Move to next subscript | |
3431 | ||
3432 | Sub := Next (Sub); | |
3433 | end loop; | |
3434 | end Apply_Subscript_Validity_Checks; | |
3435 | ||
3436 | ---------------------------------- | |
3437 | -- Apply_Type_Conversion_Checks -- | |
3438 | ---------------------------------- | |
3439 | ||
3440 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3441 | Target_Type : constant Entity_Id := Etype (N); | |
3442 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3443 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3444 | |
3445 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3446 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3447 | -- full view might have discriminants with defaults, so we need the | |
3448 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3449 | |
3450 | begin | |
3451 | if Inside_A_Generic then | |
3452 | return; | |
3453 | ||
f15731c4 | 3454 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3455 | -- situations of incomplete trees that blow things up. |
3456 | ||
f15731c4 | 3457 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3458 | return; |
3459 | ||
ea822fd4 | 3460 | -- Never generate discriminant checks for Unchecked_Union types |
3461 | ||
3462 | elsif Present (Expr_Type) | |
3463 | and then Is_Unchecked_Union (Expr_Type) | |
3464 | then | |
3465 | return; | |
3466 | ||
feff2f05 | 3467 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3468 | -- range check if we cannot be sure that Expr is in the base type of | |
3469 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3470 | -- are not quite the same condition from an implementation point of | |
3471 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3472 | |
3473 | elsif Is_Scalar_Type (Target_Type) then | |
3474 | declare | |
3475 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3476 | -- If the Conversion_OK flag on the type conversion is set and no |
ea822fd4 | 3477 | -- floating-point type is involved in the type conversion then |
3478 | -- fixed-point values must be read as integral values. | |
ee6ba406 | 3479 | |
5329ca64 | 3480 | Float_To_Int : constant Boolean := |
b6341c67 | 3481 | Is_Floating_Point_Type (Expr_Type) |
3482 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3483 | |
ee6ba406 | 3484 | begin |
ee6ba406 | 3485 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3486 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3487 | and then not |
7a1dabb3 | 3488 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3489 | and then not Float_To_Int |
ee6ba406 | 3490 | then |
4642b679 | 3491 | -- A small optimization: the attribute 'Pos applied to an |
c59f2b2d | 3492 | -- enumeration type has a known range, even though its type is |
3493 | -- Universal_Integer. So in numeric conversions it is usually | |
3494 | -- within range of the target integer type. Use the static | |
3495 | -- bounds of the base types to check. Disable this optimization | |
3496 | -- in case of a generic formal discrete type, because we don't | |
3497 | -- necessarily know the upper bound yet. | |
259716a0 | 3498 | |
3499 | if Nkind (Expr) = N_Attribute_Reference | |
3500 | and then Attribute_Name (Expr) = Name_Pos | |
3501 | and then Is_Enumeration_Type (Etype (Prefix (Expr))) | |
c59f2b2d | 3502 | and then not Is_Generic_Type (Etype (Prefix (Expr))) |
259716a0 | 3503 | and then Is_Integer_Type (Target_Type) |
3504 | then | |
3505 | declare | |
4642b679 | 3506 | Enum_T : constant Entity_Id := |
3507 | Root_Type (Etype (Prefix (Expr))); | |
3508 | Int_T : constant Entity_Id := Base_Type (Target_Type); | |
3509 | Last_I : constant Uint := | |
3510 | Intval (High_Bound (Scalar_Range (Int_T))); | |
3511 | Last_E : Uint; | |
259716a0 | 3512 | |
3513 | begin | |
4642b679 | 3514 | -- Character types have no explicit literals, so we use |
259716a0 | 3515 | -- the known number of characters in the type. |
3516 | ||
3517 | if Root_Type (Enum_T) = Standard_Character then | |
3518 | Last_E := UI_From_Int (255); | |
3519 | ||
3520 | elsif Enum_T = Standard_Wide_Character | |
3521 | or else Enum_T = Standard_Wide_Wide_Character | |
3522 | then | |
3523 | Last_E := UI_From_Int (65535); | |
3524 | ||
3525 | else | |
4642b679 | 3526 | Last_E := |
3527 | Enumeration_Pos | |
259716a0 | 3528 | (Entity (High_Bound (Scalar_Range (Enum_T)))); |
3529 | end if; | |
3530 | ||
3531 | if Last_E <= Last_I then | |
3532 | null; | |
3533 | ||
3534 | else | |
3535 | Activate_Overflow_Check (N); | |
3536 | end if; | |
3537 | end; | |
3538 | ||
3539 | else | |
3540 | Activate_Overflow_Check (N); | |
3541 | end if; | |
ee6ba406 | 3542 | end if; |
3543 | ||
3544 | if not Range_Checks_Suppressed (Target_Type) | |
3545 | and then not Range_Checks_Suppressed (Expr_Type) | |
3546 | then | |
54022749 | 3547 | if Float_To_Int |
3548 | and then not GNATprove_Mode | |
3549 | then | |
5329ca64 | 3550 | Apply_Float_Conversion_Check (Expr, Target_Type); |
f81a201b | 3551 | |
5329ca64 | 3552 | else |
7d97dbc9 | 3553 | -- Conversions involving fixed-point types are expanded |
3554 | -- separately, and do not need a Range_Check flag, except | |
3555 | -- in SPARK_Mode, where the explicit constraint check will | |
3556 | -- not be generated. | |
3557 | ||
95c16286 | 3558 | if GNATprove_Mode |
3559 | or else not Is_Fixed_Point_Type (Expr_Type) | |
7d97dbc9 | 3560 | then |
3561 | Apply_Scalar_Range_Check | |
3562 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
3563 | ||
3564 | else | |
3565 | Set_Do_Range_Check (Expression (N), False); | |
3566 | end if; | |
798afddc | 3567 | |
3568 | -- If the target type has predicates, we need to indicate | |
ea822fd4 | 3569 | -- the need for a check, even if Determine_Range finds that |
3570 | -- the value is within bounds. This may be the case e.g for | |
3571 | -- a division with a constant denominator. | |
798afddc | 3572 | |
3573 | if Has_Predicates (Target_Type) then | |
3574 | Enable_Range_Check (Expr); | |
3575 | end if; | |
5329ca64 | 3576 | end if; |
ee6ba406 | 3577 | end if; |
3578 | end; | |
3579 | ||
3580 | elsif Comes_From_Source (N) | |
f40f9731 | 3581 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3582 | and then Is_Record_Type (Target_Type) |
3583 | and then Is_Derived_Type (Target_Type) | |
3584 | and then not Is_Tagged_Type (Target_Type) | |
3585 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3586 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3587 | then |
141d591a | 3588 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3589 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3590 | -- constraint, to verify that the expression of the parent type |
ea822fd4 | 3591 | -- satisfies the constraints imposed by the (unconstrained) derived |
3592 | -- type. This applies to value conversions, not to view conversions | |
3593 | -- of tagged types. | |
ee6ba406 | 3594 | |
3595 | declare | |
9dfe12ae | 3596 | Loc : constant Source_Ptr := Sloc (N); |
3597 | Cond : Node_Id; | |
3598 | Constraint : Elmt_Id; | |
3599 | Discr_Value : Node_Id; | |
3600 | Discr : Entity_Id; | |
3601 | ||
3602 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3603 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3604 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3605 | |
3606 | begin | |
9dfe12ae | 3607 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3608 | while Present (Constraint) loop |
3609 | Discr_Value := Node (Constraint); | |
3610 | ||
3611 | if Is_Entity_Name (Discr_Value) | |
3612 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3613 | then | |
3614 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3615 | ||
3616 | if Present (Discr) | |
3617 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3618 | then | |
3619 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3620 | -- Value of original discriminant in expression. If the |
3621 | -- new discriminant has been used to constrain more than | |
3622 | -- one of the stored discriminants, this will provide the | |
3623 | -- required consistency check. | |
ee6ba406 | 3624 | |
55868293 | 3625 | Append_Elmt |
3626 | (Make_Selected_Component (Loc, | |
3627 | Prefix => | |
9dfe12ae | 3628 | Duplicate_Subexpr_No_Checks |
3629 | (Expr, Name_Req => True), | |
ee6ba406 | 3630 | Selector_Name => |
3631 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3632 | New_Constraints); |
ee6ba406 | 3633 | |
3634 | else | |
3635 | -- Discriminant of more remote ancestor ??? | |
3636 | ||
3637 | return; | |
3638 | end if; | |
3639 | ||
feff2f05 | 3640 | -- Derived type definition has an explicit value for this |
3641 | -- stored discriminant. | |
ee6ba406 | 3642 | |
3643 | else | |
3644 | Append_Elmt | |
9dfe12ae | 3645 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3646 | New_Constraints); | |
ee6ba406 | 3647 | end if; |
3648 | ||
3649 | Next_Elmt (Constraint); | |
3650 | end loop; | |
3651 | ||
3652 | -- Use the unconstrained expression type to retrieve the | |
3653 | -- discriminants of the parent, and apply momentarily the | |
3654 | -- discriminant constraint synthesized above. | |
3655 | ||
3656 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3657 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3658 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3659 | ||
3660 | Insert_Action (N, | |
f15731c4 | 3661 | Make_Raise_Constraint_Error (Loc, |
3662 | Condition => Cond, | |
3663 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3664 | end; |
3665 | ||
175a6969 | 3666 | -- For arrays, checks are set now, but conversions are applied during |
3667 | -- expansion, to take into accounts changes of representation. The | |
3668 | -- checks become range checks on the base type or length checks on the | |
3669 | -- subtype, depending on whether the target type is unconstrained or | |
8e802312 | 3670 | -- constrained. Note that the range check is put on the expression of a |
3671 | -- type conversion, while the length check is put on the type conversion | |
3672 | -- itself. | |
175a6969 | 3673 | |
3674 | elsif Is_Array_Type (Target_Type) then | |
3675 | if Is_Constrained (Target_Type) then | |
3676 | Set_Do_Length_Check (N); | |
3677 | else | |
3678 | Set_Do_Range_Check (Expr); | |
3679 | end if; | |
ee6ba406 | 3680 | end if; |
ee6ba406 | 3681 | end Apply_Type_Conversion_Checks; |
3682 | ||
3683 | ---------------------------------------------- | |
3684 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3685 | ---------------------------------------------- | |
3686 | ||
3687 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3688 | Loc : constant Source_Ptr := Sloc (N); | |
3689 | Typ : constant Entity_Id := Etype (N); | |
3690 | ||
3691 | begin | |
3692 | if Inside_A_Generic then | |
3693 | return; | |
3694 | ||
3695 | -- Nothing to do if checks are suppressed | |
3696 | ||
3697 | elsif Range_Checks_Suppressed (Typ) | |
3698 | and then Overflow_Checks_Suppressed (Typ) | |
3699 | then | |
3700 | return; | |
3701 | ||
3702 | -- Nothing to do if the attribute does not come from source. The | |
3703 | -- internal attributes we generate of this type do not need checks, | |
3704 | -- and furthermore the attempt to check them causes some circular | |
3705 | -- elaboration orders when dealing with packed types. | |
3706 | ||
3707 | elsif not Comes_From_Source (N) then | |
3708 | return; | |
3709 | ||
9dfe12ae | 3710 | -- If the prefix is a selected component that depends on a discriminant |
3711 | -- the check may improperly expose a discriminant instead of using | |
3712 | -- the bounds of the object itself. Set the type of the attribute to | |
3713 | -- the base type of the context, so that a check will be imposed when | |
3714 | -- needed (e.g. if the node appears as an index). | |
3715 | ||
3716 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3717 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3718 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3719 | then | |
3720 | Set_Etype (N, Base_Type (Typ)); | |
3721 | ||
feff2f05 | 3722 | -- Otherwise, replace the attribute node with a type conversion node |
3723 | -- whose expression is the attribute, retyped to universal integer, and | |
3724 | -- whose subtype mark is the target type. The call to analyze this | |
3725 | -- conversion will set range and overflow checks as required for proper | |
3726 | -- detection of an out of range value. | |
ee6ba406 | 3727 | |
3728 | else | |
3729 | Set_Etype (N, Universal_Integer); | |
3730 | Set_Analyzed (N, True); | |
3731 | ||
3732 | Rewrite (N, | |
3733 | Make_Type_Conversion (Loc, | |
3734 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3735 | Expression => Relocate_Node (N))); | |
3736 | ||
3737 | Analyze_And_Resolve (N, Typ); | |
3738 | return; | |
3739 | end if; | |
ee6ba406 | 3740 | end Apply_Universal_Integer_Attribute_Checks; |
3741 | ||
07c191b0 | 3742 | ------------------------------------- |
3743 | -- Atomic_Synchronization_Disabled -- | |
3744 | ------------------------------------- | |
3745 | ||
3746 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3747 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3748 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3749 | ||
3750 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3751 | begin | |
b444f81d | 3752 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3753 | -- looks enabled, since it is never disabled. | |
3754 | ||
3755 | if Debug_Flag_Dot_E then | |
3756 | return False; | |
3757 | ||
3758 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3759 | -- sync looks disabled, since it always tests True. | |
3760 | ||
3761 | elsif Debug_Flag_Dot_D then | |
3762 | return True; | |
3763 | ||
3764 | -- If entity present, then check result for that entity | |
3765 | ||
3766 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3767 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3768 | |
3769 | -- Otherwise result depends on current scope setting | |
3770 | ||
07c191b0 | 3771 | else |
fafc6b97 | 3772 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3773 | end if; |
3774 | end Atomic_Synchronization_Disabled; | |
3775 | ||
ee6ba406 | 3776 | ------------------------------- |
3777 | -- Build_Discriminant_Checks -- | |
3778 | ------------------------------- | |
3779 | ||
3780 | function Build_Discriminant_Checks | |
3781 | (N : Node_Id; | |
314a23b6 | 3782 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3783 | is |
3784 | Loc : constant Source_Ptr := Sloc (N); | |
3785 | Cond : Node_Id; | |
3786 | Disc : Elmt_Id; | |
3787 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3788 | Dref : Node_Id; |
ee6ba406 | 3789 | Dval : Node_Id; |
3790 | ||
84d0d4a5 | 3791 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3792 | ||
bacd5059 | 3793 | -------------------------------- |
3794 | -- Aggregate_Discriminant_Val -- | |
3795 | -------------------------------- | |
84d0d4a5 | 3796 | |
3797 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3798 | Assoc : Node_Id; | |
3799 | ||
3800 | begin | |
feff2f05 | 3801 | -- The aggregate has been normalized with named associations. We use |
3802 | -- the Chars field to locate the discriminant to take into account | |
3803 | -- discriminants in derived types, which carry the same name as those | |
3804 | -- in the parent. | |
84d0d4a5 | 3805 | |
3806 | Assoc := First (Component_Associations (N)); | |
3807 | while Present (Assoc) loop | |
3808 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3809 | return Expression (Assoc); | |
3810 | else | |
3811 | Next (Assoc); | |
3812 | end if; | |
3813 | end loop; | |
3814 | ||
3815 | -- Discriminant must have been found in the loop above | |
3816 | ||
3817 | raise Program_Error; | |
3818 | end Aggregate_Discriminant_Val; | |
3819 | ||
3820 | -- Start of processing for Build_Discriminant_Checks | |
3821 | ||
ee6ba406 | 3822 | begin |
84d0d4a5 | 3823 | -- Loop through discriminants evolving the condition |
3824 | ||
ee6ba406 | 3825 | Cond := Empty; |
3826 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3827 | ||
9dfe12ae | 3828 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3829 | |
3830 | if Is_Private_Type (T_Typ) | |
3831 | and then No (Full_View (T_Typ)) | |
3832 | then | |
3833 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3834 | else | |
3835 | Disc_Ent := First_Discriminant (T_Typ); | |
3836 | end if; | |
3837 | ||
3838 | while Present (Disc) loop | |
ee6ba406 | 3839 | Dval := Node (Disc); |
3840 | ||
3841 | if Nkind (Dval) = N_Identifier | |
3842 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3843 | then | |
3844 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3845 | else | |
9dfe12ae | 3846 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3847 | end if; |
3848 | ||
00f91aef | 3849 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3850 | -- of the node. | |
9dfe12ae | 3851 | |
00f91aef | 3852 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3853 | Dref := New_Copy ( | |
3854 | Get_Discriminant_Value ( | |
3855 | First_Discriminant (T_Typ), | |
3856 | T_Typ, | |
3857 | Stored_Constraint (T_Typ))); | |
3858 | ||
84d0d4a5 | 3859 | elsif Nkind (N) = N_Aggregate then |
3860 | Dref := | |
3861 | Duplicate_Subexpr_No_Checks | |
3862 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3863 | ||
00f91aef | 3864 | else |
3865 | Dref := | |
3866 | Make_Selected_Component (Loc, | |
20cf157b | 3867 | Prefix => |
00f91aef | 3868 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
20cf157b | 3869 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
00f91aef | 3870 | |
3871 | Set_Is_In_Discriminant_Check (Dref); | |
3872 | end if; | |
9dfe12ae | 3873 | |
ee6ba406 | 3874 | Evolve_Or_Else (Cond, |
3875 | Make_Op_Ne (Loc, | |
20cf157b | 3876 | Left_Opnd => Dref, |
ee6ba406 | 3877 | Right_Opnd => Dval)); |
3878 | ||
3879 | Next_Elmt (Disc); | |
3880 | Next_Discriminant (Disc_Ent); | |
3881 | end loop; | |
3882 | ||
3883 | return Cond; | |
3884 | end Build_Discriminant_Checks; | |
3885 | ||
13dbf220 | 3886 | ------------------ |
3887 | -- Check_Needed -- | |
3888 | ------------------ | |
3889 | ||
3890 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3891 | N : Node_Id; | |
3892 | P : Node_Id; | |
3893 | K : Node_Kind; | |
3894 | L : Node_Id; | |
3895 | R : Node_Id; | |
3896 | ||
9b2068d4 | 3897 | function Left_Expression (Op : Node_Id) return Node_Id; |
3898 | -- Return the relevant expression from the left operand of the given | |
3899 | -- short circuit form: this is LO itself, except if LO is a qualified | |
3900 | -- expression, a type conversion, or an expression with actions, in | |
3901 | -- which case this is Left_Expression (Expression (LO)). | |
3902 | ||
3903 | --------------------- | |
3904 | -- Left_Expression -- | |
3905 | --------------------- | |
3906 | ||
3907 | function Left_Expression (Op : Node_Id) return Node_Id is | |
3908 | LE : Node_Id := Left_Opnd (Op); | |
3909 | begin | |
20cf157b | 3910 | while Nkind_In (LE, N_Qualified_Expression, |
3911 | N_Type_Conversion, | |
3912 | N_Expression_With_Actions) | |
9b2068d4 | 3913 | loop |
3914 | LE := Expression (LE); | |
3915 | end loop; | |
3916 | ||
3917 | return LE; | |
3918 | end Left_Expression; | |
3919 | ||
3920 | -- Start of processing for Check_Needed | |
3921 | ||
13dbf220 | 3922 | begin |
3923 | -- Always check if not simple entity | |
3924 | ||
3925 | if Nkind (Nod) not in N_Has_Entity | |
3926 | or else not Comes_From_Source (Nod) | |
3927 | then | |
3928 | return True; | |
3929 | end if; | |
3930 | ||
3931 | -- Look up tree for short circuit | |
3932 | ||
3933 | N := Nod; | |
3934 | loop | |
3935 | P := Parent (N); | |
3936 | K := Nkind (P); | |
3937 | ||
7b17e51b | 3938 | -- Done if out of subexpression (note that we allow generated stuff |
3939 | -- such as itype declarations in this context, to keep the loop going | |
3940 | -- since we may well have generated such stuff in complex situations. | |
3941 | -- Also done if no parent (probably an error condition, but no point | |
39a0c1d3 | 3942 | -- in behaving nasty if we find it). |
7b17e51b | 3943 | |
3944 | if No (P) | |
3945 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
3946 | then | |
13dbf220 | 3947 | return True; |
3948 | ||
7b17e51b | 3949 | -- Or/Or Else case, where test is part of the right operand, or is |
3950 | -- part of one of the actions associated with the right operand, and | |
3951 | -- the left operand is an equality test. | |
13dbf220 | 3952 | |
7b17e51b | 3953 | elsif K = N_Op_Or then |
13dbf220 | 3954 | exit when N = Right_Opnd (P) |
9b2068d4 | 3955 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3956 | |
7b17e51b | 3957 | elsif K = N_Or_Else then |
3958 | exit when (N = Right_Opnd (P) | |
3959 | or else | |
3960 | (Is_List_Member (N) | |
3961 | and then List_Containing (N) = Actions (P))) | |
9b2068d4 | 3962 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3963 | |
7b17e51b | 3964 | -- Similar test for the And/And then case, where the left operand |
3965 | -- is an inequality test. | |
3966 | ||
3967 | elsif K = N_Op_And then | |
13dbf220 | 3968 | exit when N = Right_Opnd (P) |
9b2068d4 | 3969 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
7b17e51b | 3970 | |
3971 | elsif K = N_And_Then then | |
3972 | exit when (N = Right_Opnd (P) | |
3973 | or else | |
3974 | (Is_List_Member (N) | |
20cf157b | 3975 | and then List_Containing (N) = Actions (P))) |
9b2068d4 | 3976 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
13dbf220 | 3977 | end if; |
3978 | ||
3979 | N := P; | |
3980 | end loop; | |
3981 | ||
3982 | -- If we fall through the loop, then we have a conditional with an | |
9b2068d4 | 3983 | -- appropriate test as its left operand, so look further. |
3984 | ||
3985 | L := Left_Expression (P); | |
3986 | ||
3987 | -- L is an "=" or "/=" operator: extract its operands | |
13dbf220 | 3988 | |
13dbf220 | 3989 | R := Right_Opnd (L); |
3990 | L := Left_Opnd (L); | |
3991 | ||
3992 | -- Left operand of test must match original variable | |
3993 | ||
20cf157b | 3994 | if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
13dbf220 | 3995 | return True; |
3996 | end if; | |
3997 | ||
2af58f67 | 3998 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 3999 | |
4000 | case Check is | |
4001 | when Access_Check => | |
2af58f67 | 4002 | if not Known_Null (R) then |
13dbf220 | 4003 | return True; |
4004 | end if; | |
4005 | ||
4006 | when Division_Check => | |
4007 | if not Compile_Time_Known_Value (R) | |
4008 | or else Expr_Value (R) /= Uint_0 | |
4009 | then | |
4010 | return True; | |
4011 | end if; | |
2af58f67 | 4012 | |
4013 | when others => | |
4014 | raise Program_Error; | |
13dbf220 | 4015 | end case; |
4016 | ||
4017 | -- Here we have the optimizable case, warn if not short-circuited | |
4018 | ||
4019 | if K = N_Op_And or else K = N_Op_Or then | |
c4968aa2 | 4020 | Error_Msg_Warn := SPARK_Mode /= On; |
4098232e | 4021 | |
13dbf220 | 4022 | case Check is |
4023 | when Access_Check => | |
4098232e | 4024 | if GNATprove_Mode then |
4025 | Error_Msg_N | |
4026 | ("Constraint_Error might have been raised (access check)", | |
4027 | Parent (Nod)); | |
4028 | else | |
4029 | Error_Msg_N | |
4030 | ("Constraint_Error may be raised (access check)??", | |
4031 | Parent (Nod)); | |
4032 | end if; | |
4033 | ||
13dbf220 | 4034 | when Division_Check => |
4098232e | 4035 | if GNATprove_Mode then |
4036 | Error_Msg_N | |
4037 | ("Constraint_Error might have been raised (zero divide)", | |
4038 | Parent (Nod)); | |
4039 | else | |
4040 | Error_Msg_N | |
4041 | ("Constraint_Error may be raised (zero divide)??", | |
4042 | Parent (Nod)); | |
4043 | end if; | |
2af58f67 | 4044 | |
4045 | when others => | |
4046 | raise Program_Error; | |
13dbf220 | 4047 | end case; |
4048 | ||
4049 | if K = N_Op_And then | |
e977c0cf | 4050 | Error_Msg_N -- CODEFIX |
cb97ae5c | 4051 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 4052 | else |
e977c0cf | 4053 | Error_Msg_N -- CODEFIX |
cb97ae5c | 4054 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 4055 | end if; |
4056 | ||
6fb3c314 | 4057 | -- If not short-circuited, we need the check |
13dbf220 | 4058 | |
4059 | return True; | |
4060 | ||
4061 | -- If short-circuited, we can omit the check | |
4062 | ||
4063 | else | |
4064 | return False; | |
4065 | end if; | |
4066 | end Check_Needed; | |
4067 | ||
ee6ba406 | 4068 | ----------------------------------- |
4069 | -- Check_Valid_Lvalue_Subscripts -- | |
4070 | ----------------------------------- | |
4071 | ||
4072 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
4073 | begin | |
4074 | -- Skip this if range checks are suppressed | |
4075 | ||
4076 | if Range_Checks_Suppressed (Etype (Expr)) then | |
4077 | return; | |
4078 | ||
feff2f05 | 4079 | -- Only do this check for expressions that come from source. We assume |
4080 | -- that expander generated assignments explicitly include any necessary | |
4081 | -- checks. Note that this is not just an optimization, it avoids | |
39a0c1d3 | 4082 | -- infinite recursions. |
ee6ba406 | 4083 | |
4084 | elsif not Comes_From_Source (Expr) then | |
4085 | return; | |
4086 | ||
4087 | -- For a selected component, check the prefix | |
4088 | ||
4089 | elsif Nkind (Expr) = N_Selected_Component then | |
4090 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4091 | return; | |
4092 | ||
4093 | -- Case of indexed component | |
4094 | ||
4095 | elsif Nkind (Expr) = N_Indexed_Component then | |
4096 | Apply_Subscript_Validity_Checks (Expr); | |
4097 | ||
feff2f05 | 4098 | -- Prefix may itself be or contain an indexed component, and these |
4099 | -- subscripts need checking as well. | |
ee6ba406 | 4100 | |
4101 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4102 | end if; | |
4103 | end Check_Valid_Lvalue_Subscripts; | |
4104 | ||
fa7497e8 | 4105 | ---------------------------------- |
4106 | -- Null_Exclusion_Static_Checks -- | |
4107 | ---------------------------------- | |
4108 | ||
e9998840 | 4109 | procedure Null_Exclusion_Static_Checks |
69733a9a | 4110 | (N : Node_Id; |
4111 | Comp : Node_Id := Empty; | |
4112 | Array_Comp : Boolean := False) | |
e9998840 | 4113 | is |
b1e656fb | 4114 | Has_Null : constant Boolean := Has_Null_Exclusion (N); |
4115 | Kind : constant Node_Kind := Nkind (N); | |
4116 | Error_Nod : Node_Id; | |
4117 | Expr : Node_Id; | |
4118 | Typ : Entity_Id; | |
fa7497e8 | 4119 | |
13dbf220 | 4120 | begin |
0577b0b1 | 4121 | pragma Assert |
b1e656fb | 4122 | (Nkind_In (Kind, N_Component_Declaration, |
4123 | N_Discriminant_Specification, | |
4124 | N_Function_Specification, | |
4125 | N_Object_Declaration, | |
4126 | N_Parameter_Specification)); | |
0577b0b1 | 4127 | |
b1e656fb | 4128 | if Kind = N_Function_Specification then |
0577b0b1 | 4129 | Typ := Etype (Defining_Entity (N)); |
4130 | else | |
4131 | Typ := Etype (Defining_Identifier (N)); | |
4132 | end if; | |
fa7497e8 | 4133 | |
b1e656fb | 4134 | case Kind is |
13dbf220 | 4135 | when N_Component_Declaration => |
4136 | if Present (Access_Definition (Component_Definition (N))) then | |
b1e656fb | 4137 | Error_Nod := Component_Definition (N); |
13dbf220 | 4138 | else |
b1e656fb | 4139 | Error_Nod := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 4140 | end if; |
5329ca64 | 4141 | |
0577b0b1 | 4142 | when N_Discriminant_Specification => |
b1e656fb | 4143 | Error_Nod := Discriminant_Type (N); |
0577b0b1 | 4144 | |
4145 | when N_Function_Specification => | |
b1e656fb | 4146 | Error_Nod := Result_Definition (N); |
0577b0b1 | 4147 | |
4148 | when N_Object_Declaration => | |
b1e656fb | 4149 | Error_Nod := Object_Definition (N); |
0577b0b1 | 4150 | |
4151 | when N_Parameter_Specification => | |
b1e656fb | 4152 | Error_Nod := Parameter_Type (N); |
0577b0b1 | 4153 | |
13dbf220 | 4154 | when others => |
4155 | raise Program_Error; | |
4156 | end case; | |
5329ca64 | 4157 | |
0577b0b1 | 4158 | if Has_Null then |
5329ca64 | 4159 | |
0577b0b1 | 4160 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
4161 | -- applied to an access [sub]type. | |
5329ca64 | 4162 | |
0577b0b1 | 4163 | if not Is_Access_Type (Typ) then |
503f7fd3 | 4164 | Error_Msg_N |
b1e656fb | 4165 | ("`NOT NULL` allowed only for an access type", Error_Nod); |
5329ca64 | 4166 | |
feff2f05 | 4167 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 4168 | -- be applied to a [sub]type that does not exclude null already. |
4169 | ||
b1e656fb | 4170 | elsif Can_Never_Be_Null (Typ) and then Comes_From_Source (Typ) then |
503f7fd3 | 4171 | Error_Msg_NE |
00c403ee | 4172 | ("`NOT NULL` not allowed (& already excludes null)", |
b1e656fb | 4173 | Error_Nod, Typ); |
0577b0b1 | 4174 | end if; |
13dbf220 | 4175 | end if; |
5329ca64 | 4176 | |
cc60bd16 | 4177 | -- Check that null-excluding objects are always initialized, except for |
4178 | -- deferred constants, for which the expression will appear in the full | |
4179 | -- declaration. | |
13dbf220 | 4180 | |
b1e656fb | 4181 | if Kind = N_Object_Declaration |
84d0d4a5 | 4182 | and then No (Expression (N)) |
cc60bd16 | 4183 | and then not Constant_Present (N) |
feff2f05 | 4184 | and then not No_Initialization (N) |
13dbf220 | 4185 | then |
e9998840 | 4186 | if Present (Comp) then |
4187 | ||
b1ff36e7 | 4188 | -- Specialize the warning message to indicate that we are dealing |
e9998840 | 4189 | -- with an uninitialized composite object that has a defaulted |
4190 | -- null-excluding component. | |
4191 | ||
4192 | Error_Msg_Name_1 := Chars (Defining_Identifier (Comp)); | |
4193 | Error_Msg_Name_2 := Chars (Defining_Identifier (N)); | |
4194 | ||
69733a9a | 4195 | Discard_Node |
4196 | (Compile_Time_Constraint_Error | |
4197 | (N => N, | |
4198 | Msg => | |
4199 | "(Ada 2005) null-excluding component % of object % must " | |
4200 | & "be initialized??", | |
4201 | Ent => Defining_Identifier (Comp))); | |
4202 | ||
4203 | -- This is a case of an array with null-excluding components, so | |
4204 | -- indicate that in the warning. | |
4205 | ||
4206 | elsif Array_Comp then | |
4207 | Discard_Node | |
4208 | (Compile_Time_Constraint_Error | |
4209 | (N => N, | |
4210 | Msg => | |
4211 | "(Ada 2005) null-excluding array components must " | |
4212 | & "be initialized??", | |
4213 | Ent => Defining_Identifier (N))); | |
4214 | ||
4215 | -- Normal case of object of a null-excluding access type | |
b1ff36e7 | 4216 | |
e9998840 | 4217 | else |
69733a9a | 4218 | -- Add an expression that assigns null. This node is needed by |
4219 | -- Apply_Compile_Time_Constraint_Error, which will replace this | |
4220 | -- with a Constraint_Error node. | |
4221 | ||
4222 | Set_Expression (N, Make_Null (Sloc (N))); | |
4223 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
4224 | ||
e9998840 | 4225 | Apply_Compile_Time_Constraint_Error |
4226 | (N => Expression (N), | |
4227 | Msg => | |
4228 | "(Ada 2005) null-excluding objects must be initialized??", | |
4229 | Reason => CE_Null_Not_Allowed); | |
4230 | end if; | |
13dbf220 | 4231 | end if; |
5329ca64 | 4232 | |
cc60bd16 | 4233 | -- Check that a null-excluding component, formal or object is not being |
4234 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 4235 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 4236 | |
b1e656fb | 4237 | if Kind /= N_Function_Specification then |
0577b0b1 | 4238 | Expr := Expression (N); |
5329ca64 | 4239 | |
2af58f67 | 4240 | if Present (Expr) and then Known_Null (Expr) then |
b1e656fb | 4241 | case Kind is |
99378362 | 4242 | when N_Component_Declaration |
4243 | | N_Discriminant_Specification | |
4244 | => | |
7189d17f | 4245 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4246 | (N => Expr, |
99378362 | 4247 | Msg => |
4248 | "(Ada 2005) null not allowed in null-excluding " | |
4249 | & "components??", | |
0577b0b1 | 4250 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 4251 | |
0577b0b1 | 4252 | when N_Object_Declaration => |
7189d17f | 4253 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4254 | (N => Expr, |
99378362 | 4255 | Msg => |
4256 | "(Ada 2005) null not allowed in null-excluding " | |
4257 | & "objects??", | |
0577b0b1 | 4258 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 4259 | |
0577b0b1 | 4260 | when N_Parameter_Specification => |
7189d17f | 4261 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4262 | (N => Expr, |
99378362 | 4263 | Msg => |
4264 | "(Ada 2005) null not allowed in null-excluding " | |
4265 | & "formals??", | |
0577b0b1 | 4266 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 4267 | |
4268 | when others => | |
4269 | null; | |
5329ca64 | 4270 | end case; |
4271 | end if; | |
0577b0b1 | 4272 | end if; |
fa7497e8 | 4273 | end Null_Exclusion_Static_Checks; |
4274 | ||
9dfe12ae | 4275 | ---------------------------------- |
4276 | -- Conditional_Statements_Begin -- | |
4277 | ---------------------------------- | |
4278 | ||
4279 | procedure Conditional_Statements_Begin is | |
4280 | begin | |
4281 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
4282 | ||
feff2f05 | 4283 | -- If stack overflows, kill all checks, that way we know to simply reset |
4284 | -- the number of saved checks to zero on return. This should never occur | |
4285 | -- in practice. | |
9dfe12ae | 4286 | |
4287 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4288 | Kill_All_Checks; | |
4289 | ||
feff2f05 | 4290 | -- In the normal case, we just make a new stack entry saving the current |
4291 | -- number of saved checks for a later restore. | |
9dfe12ae | 4292 | |
4293 | else | |
4294 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
4295 | ||
4296 | if Debug_Flag_CC then | |
4297 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
4298 | Num_Saved_Checks); | |
4299 | end if; | |
4300 | end if; | |
4301 | end Conditional_Statements_Begin; | |
4302 | ||
4303 | -------------------------------- | |
4304 | -- Conditional_Statements_End -- | |
4305 | -------------------------------- | |
4306 | ||
4307 | procedure Conditional_Statements_End is | |
4308 | begin | |
4309 | pragma Assert (Saved_Checks_TOS > 0); | |
4310 | ||
feff2f05 | 4311 | -- If the saved checks stack overflowed, then we killed all checks, so |
4312 | -- setting the number of saved checks back to zero is correct. This | |
4313 | -- should never occur in practice. | |
9dfe12ae | 4314 | |
4315 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4316 | Num_Saved_Checks := 0; | |
4317 | ||
feff2f05 | 4318 | -- In the normal case, restore the number of saved checks from the top |
4319 | -- stack entry. | |
9dfe12ae | 4320 | |
4321 | else | |
4322 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
20cf157b | 4323 | |
9dfe12ae | 4324 | if Debug_Flag_CC then |
4325 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
4326 | Num_Saved_Checks); | |
4327 | end if; | |
4328 | end if; | |
4329 | ||
4330 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
4331 | end Conditional_Statements_End; | |
4332 | ||
3cce7f32 | 4333 | ------------------------- |
4334 | -- Convert_From_Bignum -- | |
4335 | ------------------------- | |
4336 | ||
4337 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
4338 | Loc : constant Source_Ptr := Sloc (N); | |
4339 | ||
4340 | begin | |
4341 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
4342 | ||
4343 | -- Construct call From Bignum | |
4344 | ||
4345 | return | |
4346 | Make_Function_Call (Loc, | |
4347 | Name => | |
4348 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4349 | Parameter_Associations => New_List (Relocate_Node (N))); | |
4350 | end Convert_From_Bignum; | |
4351 | ||
4352 | ----------------------- | |
4353 | -- Convert_To_Bignum -- | |
4354 | ----------------------- | |
4355 | ||
4356 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
4357 | Loc : constant Source_Ptr := Sloc (N); | |
4358 | ||
4359 | begin | |
0326b4d4 | 4360 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 4361 | |
4362 | if Is_RTE (Etype (N), RE_Bignum) then | |
4363 | return Relocate_Node (N); | |
4364 | ||
21a55437 | 4365 | -- Otherwise construct call to To_Bignum, converting the operand to the |
4366 | -- required Long_Long_Integer form. | |
3cce7f32 | 4367 | |
4368 | else | |
4369 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
4370 | return | |
4371 | Make_Function_Call (Loc, | |
4372 | Name => | |
4373 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
4374 | Parameter_Associations => New_List ( | |
4375 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
4376 | end if; | |
4377 | end Convert_To_Bignum; | |
4378 | ||
ee6ba406 | 4379 | --------------------- |
4380 | -- Determine_Range -- | |
4381 | --------------------- | |
4382 | ||
6af1bdbc | 4383 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 4384 | type Cache_Index is range 0 .. Cache_Size - 1; |
39a0c1d3 | 4385 | -- Determine size of below cache (power of 2 is more efficient) |
ee6ba406 | 4386 | |
7ac8c2b1 | 4387 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; |
4388 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; | |
4389 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; | |
4390 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
4391 | Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal; | |
4392 | Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal; | |
feff2f05 | 4393 | -- The above arrays are used to implement a small direct cache for |
7ac8c2b1 | 4394 | -- Determine_Range and Determine_Range_R calls. Because of the way these |
4395 | -- subprograms recursively traces subexpressions, and because overflow | |
4396 | -- checking calls the routine on the way up the tree, a quadratic behavior | |
4397 | -- can otherwise be encountered in large expressions. The cache entry for | |
4398 | -- node N is stored in the (N mod Cache_Size) entry, and can be validated | |
4399 | -- by checking the actual node value stored there. The Range_Cache_V array | |
4400 | -- records the setting of Assume_Valid for the cache entry. | |
ee6ba406 | 4401 | |
4402 | procedure Determine_Range | |
9c486805 | 4403 | (N : Node_Id; |
4404 | OK : out Boolean; | |
4405 | Lo : out Uint; | |
4406 | Hi : out Uint; | |
4407 | Assume_Valid : Boolean := False) | |
ee6ba406 | 4408 | is |
e254d721 | 4409 | Typ : Entity_Id := Etype (N); |
4410 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 4411 | |
4412 | Lo_Left : Uint; | |
4413 | Hi_Left : Uint; | |
4414 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 4415 | |
5bb74b99 | 4416 | Lo_Right : Uint := No_Uint; |
4417 | Hi_Right : Uint := No_Uint; | |
8880be85 | 4418 | -- Lo and Hi bounds of right (or only) operand |
4419 | ||
4420 | Bound : Node_Id; | |
4421 | -- Temp variable used to hold a bound node | |
4422 | ||
4423 | Hbound : Uint; | |
4424 | -- High bound of base type of expression | |
4425 | ||
4426 | Lor : Uint; | |
4427 | Hir : Uint; | |
4428 | -- Refined values for low and high bounds, after tightening | |
4429 | ||
4430 | OK1 : Boolean; | |
4431 | -- Used in lower level calls to indicate if call succeeded | |
4432 | ||
4433 | Cindex : Cache_Index; | |
4434 | -- Used to search cache | |
ee6ba406 | 4435 | |
094ed68e | 4436 | Btyp : Entity_Id; |
4437 | -- Base type | |
4438 | ||
ee6ba406 | 4439 | function OK_Operands return Boolean; |
4440 | -- Used for binary operators. Determines the ranges of the left and | |
4441 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 4442 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 4443 | |
4444 | ----------------- | |
4445 | -- OK_Operands -- | |
4446 | ----------------- | |
4447 | ||
4448 | function OK_Operands return Boolean is | |
4449 | begin | |
9c486805 | 4450 | Determine_Range |
4451 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 4452 | |
4453 | if not OK1 then | |
4454 | return False; | |
4455 | end if; | |
4456 | ||
9c486805 | 4457 | Determine_Range |
4458 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4459 | return OK1; |
4460 | end OK_Operands; | |
4461 | ||
4462 | -- Start of processing for Determine_Range | |
4463 | ||
4464 | begin | |
e12b2502 | 4465 | -- Prevent junk warnings by initializing range variables |
4466 | ||
4467 | Lo := No_Uint; | |
4468 | Hi := No_Uint; | |
4469 | Lor := No_Uint; | |
4470 | Hir := No_Uint; | |
4471 | ||
87bdc21d | 4472 | -- For temporary constants internally generated to remove side effects |
4473 | -- we must use the corresponding expression to determine the range of | |
e12b2502 | 4474 | -- the expression. But note that the expander can also generate |
4475 | -- constants in other cases, including deferred constants. | |
87bdc21d | 4476 | |
4477 | if Is_Entity_Name (N) | |
4478 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4479 | and then Ekind (Entity (N)) = E_Constant | |
4480 | and then Is_Internal_Name (Chars (Entity (N))) | |
4481 | then | |
e12b2502 | 4482 | if Present (Expression (Parent (Entity (N)))) then |
4483 | Determine_Range | |
4484 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
87bdc21d | 4485 | |
e12b2502 | 4486 | elsif Present (Full_View (Entity (N))) then |
4487 | Determine_Range | |
4488 | (Expression (Parent (Full_View (Entity (N)))), | |
4489 | OK, Lo, Hi, Assume_Valid); | |
ee6ba406 | 4490 | |
e12b2502 | 4491 | else |
4492 | OK := False; | |
4493 | end if; | |
4494 | return; | |
4495 | end if; | |
ee6ba406 | 4496 | |
a781c0fc | 4497 | -- If type is not defined, we can't determine its range |
ee6ba406 | 4498 | |
a781c0fc | 4499 | if No (Typ) |
4500 | ||
4501 | -- We don't deal with anything except discrete types | |
4502 | ||
4503 | or else not Is_Discrete_Type (Typ) | |
4504 | ||
cdfda0e3 | 4505 | -- Don't deal with enumerated types with non-standard representation |
4506 | ||
4507 | or else (Is_Enumeration_Type (Typ) | |
4508 | and then Present (Enum_Pos_To_Rep (Base_Type (Typ)))) | |
4509 | ||
a781c0fc | 4510 | -- Ignore type for which an error has been posted, since range in |
4511 | -- this case may well be a bogosity deriving from the error. Also | |
4512 | -- ignore if error posted on the reference node. | |
4513 | ||
4514 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 4515 | then |
4516 | OK := False; | |
4517 | return; | |
4518 | end if; | |
4519 | ||
4520 | -- For all other cases, we can determine the range | |
4521 | ||
4522 | OK := True; | |
4523 | ||
feff2f05 | 4524 | -- If value is compile time known, then the possible range is the one |
39a0c1d3 | 4525 | -- value that we know this expression definitely has. |
ee6ba406 | 4526 | |
4527 | if Compile_Time_Known_Value (N) then | |
4528 | Lo := Expr_Value (N); | |
4529 | Hi := Lo; | |
4530 | return; | |
4531 | end if; | |
4532 | ||
4533 | -- Return if already in the cache | |
4534 | ||
4535 | Cindex := Cache_Index (N mod Cache_Size); | |
4536 | ||
9c486805 | 4537 | if Determine_Range_Cache_N (Cindex) = N |
4538 | and then | |
4539 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4540 | then | |
ee6ba406 | 4541 | Lo := Determine_Range_Cache_Lo (Cindex); |
4542 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4543 | return; | |
4544 | end if; | |
4545 | ||
feff2f05 | 4546 | -- Otherwise, start by finding the bounds of the type of the expression, |
4547 | -- the value cannot be outside this range (if it is, then we have an | |
4548 | -- overflow situation, which is a separate check, we are talking here | |
4549 | -- only about the expression value). | |
ee6ba406 | 4550 | |
341bd953 | 4551 | -- First a check, never try to find the bounds of a generic type, since |
4552 | -- these bounds are always junk values, and it is only valid to look at | |
4553 | -- the bounds in an instance. | |
4554 | ||
4555 | if Is_Generic_Type (Typ) then | |
4556 | OK := False; | |
4557 | return; | |
4558 | end if; | |
4559 | ||
9c486805 | 4560 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4561 | |
9c486805 | 4562 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4563 | or else Assume_No_Invalid_Values | |
4564 | or else Assume_Valid | |
e254d721 | 4565 | then |
9c486805 | 4566 | null; |
4567 | else | |
4568 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4569 | end if; |
4570 | ||
094ed68e | 4571 | -- Retrieve the base type. Handle the case where the base type is a |
4572 | -- private enumeration type. | |
4573 | ||
4574 | Btyp := Base_Type (Typ); | |
4575 | ||
4576 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4577 | Btyp := Full_View (Btyp); | |
4578 | end if; | |
4579 | ||
feff2f05 | 4580 | -- We use the actual bound unless it is dynamic, in which case use the |
4581 | -- corresponding base type bound if possible. If we can't get a bound | |
4582 | -- then we figure we can't determine the range (a peculiar case, that | |
4583 | -- perhaps cannot happen, but there is no point in bombing in this | |
4584 | -- optimization circuit. | |
8880be85 | 4585 | |
4586 | -- First the low bound | |
ee6ba406 | 4587 | |
4588 | Bound := Type_Low_Bound (Typ); | |
4589 | ||
4590 | if Compile_Time_Known_Value (Bound) then | |
4591 | Lo := Expr_Value (Bound); | |
4592 | ||
094ed68e | 4593 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4594 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4595 | |
4596 | else | |
4597 | OK := False; | |
4598 | return; | |
4599 | end if; | |
4600 | ||
8880be85 | 4601 | -- Now the high bound |
4602 | ||
ee6ba406 | 4603 | Bound := Type_High_Bound (Typ); |
4604 | ||
8880be85 | 4605 | -- We need the high bound of the base type later on, and this should |
4606 | -- always be compile time known. Again, it is not clear that this | |
4607 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4608 | |
094ed68e | 4609 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4610 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4611 | Hi := Hbound; |
4612 | ||
4613 | else | |
4614 | OK := False; | |
4615 | return; | |
4616 | end if; | |
4617 | ||
feff2f05 | 4618 | -- If we have a static subtype, then that may have a tighter bound so |
4619 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4620 | |
4621 | if Compile_Time_Known_Value (Bound) then | |
4622 | Hi := Expr_Value (Bound); | |
4623 | end if; | |
4624 | ||
feff2f05 | 4625 | -- We may be able to refine this value in certain situations. If any |
4626 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4627 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4628 | |
4629 | case Nkind (N) is | |
4630 | ||
4631 | -- For unary plus, result is limited by range of operand | |
4632 | ||
4633 | when N_Op_Plus => | |
9c486805 | 4634 | Determine_Range |
4635 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4636 | |
4637 | -- For unary minus, determine range of operand, and negate it | |
4638 | ||
4639 | when N_Op_Minus => | |
9c486805 | 4640 | Determine_Range |
4641 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4642 | |
4643 | if OK1 then | |
4644 | Lor := -Hi_Right; | |
4645 | Hir := -Lo_Right; | |
4646 | end if; | |
4647 | ||
4648 | -- For binary addition, get range of each operand and do the | |
4649 | -- addition to get the result range. | |
4650 | ||
4651 | when N_Op_Add => | |
4652 | if OK_Operands then | |
4653 | Lor := Lo_Left + Lo_Right; | |
4654 | Hir := Hi_Left + Hi_Right; | |
4655 | end if; | |
4656 | ||
feff2f05 | 4657 | -- Division is tricky. The only case we consider is where the right |
4658 | -- operand is a positive constant, and in this case we simply divide | |
4659 | -- the bounds of the left operand | |
ee6ba406 | 4660 | |
4661 | when N_Op_Divide => | |
4662 | if OK_Operands then | |
4663 | if Lo_Right = Hi_Right | |
4664 | and then Lo_Right > 0 | |
4665 | then | |
4666 | Lor := Lo_Left / Lo_Right; | |
4667 | Hir := Hi_Left / Lo_Right; | |
ee6ba406 | 4668 | else |
4669 | OK1 := False; | |
4670 | end if; | |
4671 | end if; | |
4672 | ||
feff2f05 | 4673 | -- For binary subtraction, get range of each operand and do the worst |
4674 | -- case subtraction to get the result range. | |
ee6ba406 | 4675 | |
4676 | when N_Op_Subtract => | |
4677 | if OK_Operands then | |
4678 | Lor := Lo_Left - Hi_Right; | |
4679 | Hir := Hi_Left - Lo_Right; | |
4680 | end if; | |
4681 | ||
feff2f05 | 4682 | -- For MOD, if right operand is a positive constant, then result must |
4683 | -- be in the allowable range of mod results. | |
ee6ba406 | 4684 | |
4685 | when N_Op_Mod => | |
4686 | if OK_Operands then | |
9dfe12ae | 4687 | if Lo_Right = Hi_Right |
4688 | and then Lo_Right /= 0 | |
4689 | then | |
ee6ba406 | 4690 | if Lo_Right > 0 then |
4691 | Lor := Uint_0; | |
4692 | Hir := Lo_Right - 1; | |
4693 | ||
9dfe12ae | 4694 | else -- Lo_Right < 0 |
ee6ba406 | 4695 | Lor := Lo_Right + 1; |
4696 | Hir := Uint_0; | |
4697 | end if; | |
4698 | ||
4699 | else | |
4700 | OK1 := False; | |
4701 | end if; | |
4702 | end if; | |
4703 | ||
feff2f05 | 4704 | -- For REM, if right operand is a positive constant, then result must |
4705 | -- be in the allowable range of mod results. | |
ee6ba406 | 4706 | |
4707 | when N_Op_Rem => | |
4708 | if OK_Operands then | |
99378362 | 4709 | if Lo_Right = Hi_Right and then Lo_Right /= 0 then |
ee6ba406 | 4710 | declare |
4711 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4712 | ||
4713 | begin | |
4714 | -- The sign of the result depends on the sign of the | |
4715 | -- dividend (but not on the sign of the divisor, hence | |
4716 | -- the abs operation above). | |
4717 | ||
4718 | if Lo_Left < 0 then | |
4719 | Lor := -Dval; | |
4720 | else | |
4721 | Lor := Uint_0; | |
4722 | end if; | |
4723 | ||
4724 | if Hi_Left < 0 then | |
4725 | Hir := Uint_0; | |
4726 | else | |
4727 | Hir := Dval; | |
4728 | end if; | |
4729 | end; | |
4730 | ||
4731 | else | |
4732 | OK1 := False; | |
4733 | end if; | |
4734 | end if; | |
4735 | ||
4736 | -- Attribute reference cases | |
4737 | ||
4738 | when N_Attribute_Reference => | |
4739 | case Attribute_Name (N) is | |
4740 | ||
4741 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4742 | -- possible range of values of the attribute expression. |
ee6ba406 | 4743 | |
99378362 | 4744 | when Name_Pos |
4745 | | Name_Val | |
4746 | => | |
9c486805 | 4747 | Determine_Range |
4748 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4749 | |
4750 | -- For Length attribute, use the bounds of the corresponding | |
4751 | -- index type to refine the range. | |
4752 | ||
4753 | when Name_Length => | |
4754 | declare | |
4755 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4756 | Inum : Nat; | |
4757 | Indx : Node_Id; | |
4758 | ||
4759 | LL, LU : Uint; | |
4760 | UL, UU : Uint; | |
4761 | ||
4762 | begin | |
4763 | if Is_Access_Type (Atyp) then | |
4764 | Atyp := Designated_Type (Atyp); | |
4765 | end if; | |
4766 | ||
4767 | -- For string literal, we know exact value | |
4768 | ||
4769 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4770 | OK := True; | |
4771 | Lo := String_Literal_Length (Atyp); | |
4772 | Hi := String_Literal_Length (Atyp); | |
4773 | return; | |
4774 | end if; | |
4775 | ||
4776 | -- Otherwise check for expression given | |
4777 | ||
4778 | if No (Expressions (N)) then | |
4779 | Inum := 1; | |
4780 | else | |
4781 | Inum := | |
4782 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4783 | end if; | |
4784 | ||
4785 | Indx := First_Index (Atyp); | |
4786 | for J in 2 .. Inum loop | |
4787 | Indx := Next_Index (Indx); | |
4788 | end loop; | |
4789 | ||
9116df93 | 4790 | -- If the index type is a formal type or derived from |
c8da6114 | 4791 | -- one, the bounds are not static. |
4792 | ||
4793 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4794 | OK := False; | |
4795 | return; | |
4796 | end if; | |
4797 | ||
ee6ba406 | 4798 | Determine_Range |
9c486805 | 4799 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4800 | Assume_Valid); | |
ee6ba406 | 4801 | |
4802 | if OK1 then | |
4803 | Determine_Range | |
9c486805 | 4804 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4805 | Assume_Valid); | |
ee6ba406 | 4806 | |
4807 | if OK1 then | |
4808 | ||
4809 | -- The maximum value for Length is the biggest | |
4810 | -- possible gap between the values of the bounds. | |
4811 | -- But of course, this value cannot be negative. | |
4812 | ||
9c486805 | 4813 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4814 | |
4815 | -- For constrained arrays, the minimum value for | |
4816 | -- Length is taken from the actual value of the | |
9116df93 | 4817 | -- bounds, since the index will be exactly of this |
4818 | -- subtype. | |
ee6ba406 | 4819 | |
4820 | if Is_Constrained (Atyp) then | |
9c486805 | 4821 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4822 | |
4823 | -- For an unconstrained array, the minimum value | |
4824 | -- for length is always zero. | |
4825 | ||
4826 | else | |
4827 | Lor := Uint_0; | |
4828 | end if; | |
4829 | end if; | |
4830 | end if; | |
4831 | end; | |
4832 | ||
4833 | -- No special handling for other attributes | |
9116df93 | 4834 | -- Probably more opportunities exist here??? |
ee6ba406 | 4835 | |
4836 | when others => | |
4837 | OK1 := False; | |
4838 | ||
4839 | end case; | |
4840 | ||
ee6ba406 | 4841 | when N_Type_Conversion => |
54022749 | 4842 | |
4843 | -- For type conversion from one discrete type to another, we can | |
4844 | -- refine the range using the converted value. | |
4845 | ||
4846 | if Is_Discrete_Type (Etype (Expression (N))) then | |
4847 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
4848 | ||
4849 | -- When converting a float to an integer type, determine the range | |
4850 | -- in real first, and then convert the bounds using UR_To_Uint | |
4851 | -- which correctly rounds away from zero when half way between two | |
4852 | -- integers, as required by normal Ada 95 rounding semantics. It | |
4853 | -- is only possible because analysis in GNATprove rules out the | |
4854 | -- possibility of a NaN or infinite value. | |
4855 | ||
4856 | elsif GNATprove_Mode | |
4857 | and then Is_Floating_Point_Type (Etype (Expression (N))) | |
4858 | then | |
4859 | declare | |
4860 | Lor_Real, Hir_Real : Ureal; | |
4861 | begin | |
4862 | Determine_Range_R (Expression (N), OK1, Lor_Real, Hir_Real, | |
4863 | Assume_Valid); | |
4864 | ||
4865 | if OK1 then | |
4866 | Lor := UR_To_Uint (Lor_Real); | |
4867 | Hir := UR_To_Uint (Hir_Real); | |
4868 | end if; | |
4869 | end; | |
4870 | ||
4871 | else | |
4872 | OK1 := False; | |
4873 | end if; | |
ee6ba406 | 4874 | |
4875 | -- Nothing special to do for all other expression kinds | |
4876 | ||
4877 | when others => | |
4878 | OK1 := False; | |
4879 | Lor := No_Uint; | |
4880 | Hir := No_Uint; | |
4881 | end case; | |
4882 | ||
9116df93 | 4883 | -- At this stage, if OK1 is true, then we know that the actual result of |
4884 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4885 | -- to restrict the possible range of results. | |
ee6ba406 | 4886 | |
4887 | if OK1 then | |
4888 | ||
9116df93 | 4889 | -- If the refined value of the low bound is greater than the type |
7ac8c2b1 | 4890 | -- low bound, then reset it to the more restrictive value. However, |
9116df93 | 4891 | -- we do NOT do this for the case of a modular type where the |
4892 | -- possible upper bound on the value is above the base type high | |
4893 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4894 | |
4895 | if Lor > Lo | |
9116df93 | 4896 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4897 | then |
4898 | Lo := Lor; | |
4899 | end if; | |
4900 | ||
9116df93 | 4901 | -- Similarly, if the refined value of the high bound is less than the |
4902 | -- value so far, then reset it to the more restrictive value. Again, | |
4903 | -- we do not do this if the refined low bound is negative for a | |
4904 | -- modular type, since this would wrap. | |
ee6ba406 | 4905 | |
4906 | if Hir < Hi | |
9116df93 | 4907 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4908 | then |
4909 | Hi := Hir; | |
4910 | end if; | |
4911 | end if; | |
4912 | ||
4913 | -- Set cache entry for future call and we are all done | |
4914 | ||
4915 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4916 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4917 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4918 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4919 | return; | |
4920 | ||
9116df93 | 4921 | -- If any exception occurs, it means that we have some bug in the compiler, |
4922 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4923 | -- occurrence. However, this is only an optimization attempt, so there is |
4924 | -- really no point in crashing the compiler. Instead we just decide, too | |
4925 | -- bad, we can't figure out a range in this case after all. | |
4926 | ||
4927 | exception | |
4928 | when others => | |
4929 | ||
4930 | -- Debug flag K disables this behavior (useful for debugging) | |
4931 | ||
4932 | if Debug_Flag_K then | |
4933 | raise; | |
4934 | else | |
4935 | OK := False; | |
4936 | Lo := No_Uint; | |
4937 | Hi := No_Uint; | |
4938 | return; | |
4939 | end if; | |
ee6ba406 | 4940 | end Determine_Range; |
4941 | ||
7ac8c2b1 | 4942 | ----------------------- |
4943 | -- Determine_Range_R -- | |
4944 | ----------------------- | |
4945 | ||
4946 | procedure Determine_Range_R | |
4947 | (N : Node_Id; | |
4948 | OK : out Boolean; | |
4949 | Lo : out Ureal; | |
4950 | Hi : out Ureal; | |
4951 | Assume_Valid : Boolean := False) | |
4952 | is | |
4953 | Typ : Entity_Id := Etype (N); | |
4954 | -- Type to use, may get reset to base type for possibly invalid entity | |
4955 | ||
4956 | Lo_Left : Ureal; | |
4957 | Hi_Left : Ureal; | |
4958 | -- Lo and Hi bounds of left operand | |
4959 | ||
5bb74b99 | 4960 | Lo_Right : Ureal := No_Ureal; |
4961 | Hi_Right : Ureal := No_Ureal; | |
7ac8c2b1 | 4962 | -- Lo and Hi bounds of right (or only) operand |
4963 | ||
4964 | Bound : Node_Id; | |
4965 | -- Temp variable used to hold a bound node | |
4966 | ||
4967 | Hbound : Ureal; | |
4968 | -- High bound of base type of expression | |
4969 | ||
4970 | Lor : Ureal; | |
4971 | Hir : Ureal; | |
4972 | -- Refined values for low and high bounds, after tightening | |
4973 | ||
4974 | OK1 : Boolean; | |
4975 | -- Used in lower level calls to indicate if call succeeded | |
4976 | ||
4977 | Cindex : Cache_Index; | |
4978 | -- Used to search cache | |
4979 | ||
4980 | Btyp : Entity_Id; | |
4981 | -- Base type | |
4982 | ||
4983 | function OK_Operands return Boolean; | |
4984 | -- Used for binary operators. Determines the ranges of the left and | |
4985 | -- right operands, and if they are both OK, returns True, and puts | |
4986 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. | |
4987 | ||
4988 | function Round_Machine (B : Ureal) return Ureal; | |
4989 | -- B is a real bound. Round it using mode Round_Even. | |
4990 | ||
4991 | ----------------- | |
4992 | -- OK_Operands -- | |
4993 | ----------------- | |
4994 | ||
4995 | function OK_Operands return Boolean is | |
4996 | begin | |
4997 | Determine_Range_R | |
4998 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
4999 | ||
5000 | if not OK1 then | |
5001 | return False; | |
5002 | end if; | |
5003 | ||
5004 | Determine_Range_R | |
5005 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5006 | return OK1; | |
5007 | end OK_Operands; | |
5008 | ||
5009 | ------------------- | |
5010 | -- Round_Machine -- | |
5011 | ------------------- | |
5012 | ||
5013 | function Round_Machine (B : Ureal) return Ureal is | |
5014 | begin | |
5015 | return Machine (Typ, B, Round_Even, N); | |
5016 | end Round_Machine; | |
5017 | ||
5018 | -- Start of processing for Determine_Range_R | |
5019 | ||
5020 | begin | |
5021 | -- Prevent junk warnings by initializing range variables | |
5022 | ||
5023 | Lo := No_Ureal; | |
5024 | Hi := No_Ureal; | |
5025 | Lor := No_Ureal; | |
5026 | Hir := No_Ureal; | |
5027 | ||
5028 | -- For temporary constants internally generated to remove side effects | |
5029 | -- we must use the corresponding expression to determine the range of | |
5030 | -- the expression. But note that the expander can also generate | |
5031 | -- constants in other cases, including deferred constants. | |
5032 | ||
5033 | if Is_Entity_Name (N) | |
5034 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
5035 | and then Ekind (Entity (N)) = E_Constant | |
5036 | and then Is_Internal_Name (Chars (Entity (N))) | |
5037 | then | |
5038 | if Present (Expression (Parent (Entity (N)))) then | |
5039 | Determine_Range_R | |
5040 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
5041 | ||
5042 | elsif Present (Full_View (Entity (N))) then | |
5043 | Determine_Range_R | |
5044 | (Expression (Parent (Full_View (Entity (N)))), | |
5045 | OK, Lo, Hi, Assume_Valid); | |
5046 | ||
5047 | else | |
5048 | OK := False; | |
5049 | end if; | |
4c1c7f3f | 5050 | |
7ac8c2b1 | 5051 | return; |
5052 | end if; | |
5053 | ||
5054 | -- If type is not defined, we can't determine its range | |
5055 | ||
5056 | if No (Typ) | |
5057 | ||
5058 | -- We don't deal with anything except IEEE floating-point types | |
5059 | ||
5060 | or else not Is_Floating_Point_Type (Typ) | |
5061 | or else Float_Rep (Typ) /= IEEE_Binary | |
5062 | ||
5063 | -- Ignore type for which an error has been posted, since range in | |
5064 | -- this case may well be a bogosity deriving from the error. Also | |
5065 | -- ignore if error posted on the reference node. | |
5066 | ||
5067 | or else Error_Posted (N) or else Error_Posted (Typ) | |
5068 | then | |
5069 | OK := False; | |
5070 | return; | |
5071 | end if; | |
5072 | ||
5073 | -- For all other cases, we can determine the range | |
5074 | ||
5075 | OK := True; | |
5076 | ||
5077 | -- If value is compile time known, then the possible range is the one | |
5078 | -- value that we know this expression definitely has. | |
5079 | ||
5080 | if Compile_Time_Known_Value (N) then | |
5081 | Lo := Expr_Value_R (N); | |
5082 | Hi := Lo; | |
5083 | return; | |
5084 | end if; | |
5085 | ||
5086 | -- Return if already in the cache | |
5087 | ||
5088 | Cindex := Cache_Index (N mod Cache_Size); | |
5089 | ||
5090 | if Determine_Range_Cache_N (Cindex) = N | |
5091 | and then | |
5092 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
5093 | then | |
5094 | Lo := Determine_Range_Cache_Lo_R (Cindex); | |
5095 | Hi := Determine_Range_Cache_Hi_R (Cindex); | |
5096 | return; | |
5097 | end if; | |
5098 | ||
5099 | -- Otherwise, start by finding the bounds of the type of the expression, | |
5100 | -- the value cannot be outside this range (if it is, then we have an | |
5101 | -- overflow situation, which is a separate check, we are talking here | |
5102 | -- only about the expression value). | |
5103 | ||
5104 | -- First a check, never try to find the bounds of a generic type, since | |
5105 | -- these bounds are always junk values, and it is only valid to look at | |
5106 | -- the bounds in an instance. | |
5107 | ||
5108 | if Is_Generic_Type (Typ) then | |
5109 | OK := False; | |
5110 | return; | |
5111 | end if; | |
5112 | ||
5113 | -- First step, change to use base type unless we know the value is valid | |
5114 | ||
5115 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) | |
5116 | or else Assume_No_Invalid_Values | |
5117 | or else Assume_Valid | |
5118 | then | |
5119 | null; | |
5120 | else | |
5121 | Typ := Underlying_Type (Base_Type (Typ)); | |
5122 | end if; | |
5123 | ||
5124 | -- Retrieve the base type. Handle the case where the base type is a | |
5125 | -- private type. | |
5126 | ||
5127 | Btyp := Base_Type (Typ); | |
5128 | ||
5129 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
5130 | Btyp := Full_View (Btyp); | |
5131 | end if; | |
5132 | ||
5133 | -- We use the actual bound unless it is dynamic, in which case use the | |
5134 | -- corresponding base type bound if possible. If we can't get a bound | |
5135 | -- then we figure we can't determine the range (a peculiar case, that | |
5136 | -- perhaps cannot happen, but there is no point in bombing in this | |
5137 | -- optimization circuit). | |
5138 | ||
5139 | -- First the low bound | |
5140 | ||
5141 | Bound := Type_Low_Bound (Typ); | |
5142 | ||
5143 | if Compile_Time_Known_Value (Bound) then | |
5144 | Lo := Expr_Value_R (Bound); | |
5145 | ||
5146 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then | |
5147 | Lo := Expr_Value_R (Type_Low_Bound (Btyp)); | |
5148 | ||
5149 | else | |
5150 | OK := False; | |
5151 | return; | |
5152 | end if; | |
5153 | ||
5154 | -- Now the high bound | |
5155 | ||
5156 | Bound := Type_High_Bound (Typ); | |
5157 | ||
5158 | -- We need the high bound of the base type later on, and this should | |
5159 | -- always be compile time known. Again, it is not clear that this | |
5160 | -- can ever be false, but no point in bombing. | |
5161 | ||
5162 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then | |
5163 | Hbound := Expr_Value_R (Type_High_Bound (Btyp)); | |
5164 | Hi := Hbound; | |
5165 | ||
5166 | else | |
5167 | OK := False; | |
5168 | return; | |
5169 | end if; | |
5170 | ||
5171 | -- If we have a static subtype, then that may have a tighter bound so | |
5172 | -- use the upper bound of the subtype instead in this case. | |
5173 | ||
5174 | if Compile_Time_Known_Value (Bound) then | |
5175 | Hi := Expr_Value_R (Bound); | |
5176 | end if; | |
5177 | ||
5178 | -- We may be able to refine this value in certain situations. If any | |
5179 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
5180 | -- bounds, and OK1 is set to True. | |
5181 | ||
5182 | case Nkind (N) is | |
5183 | ||
5184 | -- For unary plus, result is limited by range of operand | |
5185 | ||
5186 | when N_Op_Plus => | |
5187 | Determine_Range_R | |
5188 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
5189 | ||
5190 | -- For unary minus, determine range of operand, and negate it | |
5191 | ||
5192 | when N_Op_Minus => | |
5193 | Determine_Range_R | |
5194 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5195 | ||
5196 | if OK1 then | |
5197 | Lor := -Hi_Right; | |
5198 | Hir := -Lo_Right; | |
5199 | end if; | |
5200 | ||
5201 | -- For binary addition, get range of each operand and do the | |
5202 | -- addition to get the result range. | |
5203 | ||
5204 | when N_Op_Add => | |
5205 | if OK_Operands then | |
5206 | Lor := Round_Machine (Lo_Left + Lo_Right); | |
5207 | Hir := Round_Machine (Hi_Left + Hi_Right); | |
5208 | end if; | |
5209 | ||
5210 | -- For binary subtraction, get range of each operand and do the worst | |
5211 | -- case subtraction to get the result range. | |
5212 | ||
5213 | when N_Op_Subtract => | |
5214 | if OK_Operands then | |
5215 | Lor := Round_Machine (Lo_Left - Hi_Right); | |
5216 | Hir := Round_Machine (Hi_Left - Lo_Right); | |
5217 | end if; | |
5218 | ||
5219 | -- For multiplication, get range of each operand and do the | |
5220 | -- four multiplications to get the result range. | |
5221 | ||
5222 | when N_Op_Multiply => | |
5223 | if OK_Operands then | |
5224 | declare | |
5225 | M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right); | |
5226 | M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right); | |
5227 | M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right); | |
5228 | M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right); | |
552d7cbc | 5229 | |
7ac8c2b1 | 5230 | begin |
5231 | Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4)); | |
5232 | Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4)); | |
5233 | end; | |
5234 | end if; | |
5235 | ||
5236 | -- For division, consider separately the cases where the right | |
5237 | -- operand is positive or negative. Otherwise, the right operand | |
5238 | -- can be arbitrarily close to zero, so the result is likely to | |
5239 | -- be unbounded in one direction, do not attempt to compute it. | |
5240 | ||
5241 | when N_Op_Divide => | |
5242 | if OK_Operands then | |
5243 | ||
5244 | -- Right operand is positive | |
5245 | ||
5246 | if Lo_Right > Ureal_0 then | |
5247 | ||
5248 | -- If the low bound of the left operand is negative, obtain | |
5249 | -- the overall low bound by dividing it by the smallest | |
5250 | -- value of the right operand, and otherwise by the largest | |
5251 | -- value of the right operand. | |
5252 | ||
5253 | if Lo_Left < Ureal_0 then | |
5254 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5255 | else | |
5256 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5257 | end if; | |
5258 | ||
5259 | -- If the high bound of the left operand is negative, obtain | |
5260 | -- the overall high bound by dividing it by the largest | |
5261 | -- value of the right operand, and otherwise by the | |
5262 | -- smallest value of the right operand. | |
5263 | ||
5264 | if Hi_Left < Ureal_0 then | |
5265 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5266 | else | |
5267 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5268 | end if; | |
5269 | ||
5270 | -- Right operand is negative | |
5271 | ||
5272 | elsif Hi_Right < Ureal_0 then | |
5273 | ||
5274 | -- If the low bound of the left operand is negative, obtain | |
5275 | -- the overall low bound by dividing it by the largest | |
5276 | -- value of the right operand, and otherwise by the smallest | |
5277 | -- value of the right operand. | |
5278 | ||
5279 | if Lo_Left < Ureal_0 then | |
5280 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5281 | else | |
5282 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5283 | end if; | |
5284 | ||
5285 | -- If the high bound of the left operand is negative, obtain | |
5286 | -- the overall high bound by dividing it by the smallest | |
5287 | -- value of the right operand, and otherwise by the | |
5288 | -- largest value of the right operand. | |
5289 | ||
5290 | if Hi_Left < Ureal_0 then | |
5291 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5292 | else | |
5293 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5294 | end if; | |
5295 | ||
5296 | else | |
5297 | OK1 := False; | |
5298 | end if; | |
5299 | end if; | |
5300 | ||
7ac8c2b1 | 5301 | when N_Type_Conversion => |
3c5c732f | 5302 | |
5303 | -- For type conversion from one floating-point type to another, we | |
5304 | -- can refine the range using the converted value. | |
5305 | ||
5306 | if Is_Floating_Point_Type (Etype (Expression (N))) then | |
5307 | Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
5308 | ||
5309 | -- When converting an integer to a floating-point type, determine | |
5310 | -- the range in integer first, and then convert the bounds. | |
5311 | ||
5312 | elsif Is_Discrete_Type (Etype (Expression (N))) then | |
5313 | declare | |
552d7cbc | 5314 | Hir_Int : Uint; |
5315 | Lor_Int : Uint; | |
5316 | ||
3c5c732f | 5317 | begin |
552d7cbc | 5318 | Determine_Range |
5319 | (Expression (N), OK1, Lor_Int, Hir_Int, Assume_Valid); | |
3c5c732f | 5320 | |
5321 | if OK1 then | |
5322 | Lor := Round_Machine (UR_From_Uint (Lor_Int)); | |
5323 | Hir := Round_Machine (UR_From_Uint (Hir_Int)); | |
5324 | end if; | |
5325 | end; | |
5326 | ||
5327 | else | |
5328 | OK1 := False; | |
5329 | end if; | |
7ac8c2b1 | 5330 | |
5331 | -- Nothing special to do for all other expression kinds | |
5332 | ||
5333 | when others => | |
5334 | OK1 := False; | |
5335 | Lor := No_Ureal; | |
5336 | Hir := No_Ureal; | |
5337 | end case; | |
5338 | ||
5339 | -- At this stage, if OK1 is true, then we know that the actual result of | |
5340 | -- the computed expression is in the range Lor .. Hir. We can use this | |
5341 | -- to restrict the possible range of results. | |
5342 | ||
5343 | if OK1 then | |
5344 | ||
5345 | -- If the refined value of the low bound is greater than the type | |
5346 | -- low bound, then reset it to the more restrictive value. | |
5347 | ||
5348 | if Lor > Lo then | |
5349 | Lo := Lor; | |
5350 | end if; | |
5351 | ||
5352 | -- Similarly, if the refined value of the high bound is less than the | |
5353 | -- value so far, then reset it to the more restrictive value. | |
5354 | ||
5355 | if Hir < Hi then | |
5356 | Hi := Hir; | |
5357 | end if; | |
5358 | end if; | |
5359 | ||
5360 | -- Set cache entry for future call and we are all done | |
5361 | ||
5362 | Determine_Range_Cache_N (Cindex) := N; | |
5363 | Determine_Range_Cache_V (Cindex) := Assume_Valid; | |
5364 | Determine_Range_Cache_Lo_R (Cindex) := Lo; | |
5365 | Determine_Range_Cache_Hi_R (Cindex) := Hi; | |
5366 | return; | |
5367 | ||
5368 | -- If any exception occurs, it means that we have some bug in the compiler, | |
5369 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
5370 | -- occurrence. However, this is only an optimization attempt, so there is | |
5371 | -- really no point in crashing the compiler. Instead we just decide, too | |
5372 | -- bad, we can't figure out a range in this case after all. | |
5373 | ||
5374 | exception | |
5375 | when others => | |
5376 | ||
5377 | -- Debug flag K disables this behavior (useful for debugging) | |
5378 | ||
5379 | if Debug_Flag_K then | |
5380 | raise; | |
5381 | else | |
5382 | OK := False; | |
5383 | Lo := No_Ureal; | |
5384 | Hi := No_Ureal; | |
5385 | return; | |
5386 | end if; | |
5387 | end Determine_Range_R; | |
5388 | ||
ee6ba406 | 5389 | ------------------------------------ |
5390 | -- Discriminant_Checks_Suppressed -- | |
5391 | ------------------------------------ | |
5392 | ||
5393 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5394 | begin | |
9dfe12ae | 5395 | if Present (E) then |
5396 | if Is_Unchecked_Union (E) then | |
5397 | return True; | |
5398 | elsif Checks_May_Be_Suppressed (E) then | |
5399 | return Is_Check_Suppressed (E, Discriminant_Check); | |
5400 | end if; | |
5401 | end if; | |
5402 | ||
fafc6b97 | 5403 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 5404 | end Discriminant_Checks_Suppressed; |
5405 | ||
5406 | -------------------------------- | |
5407 | -- Division_Checks_Suppressed -- | |
5408 | -------------------------------- | |
5409 | ||
5410 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5411 | begin | |
9dfe12ae | 5412 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5413 | return Is_Check_Suppressed (E, Division_Check); | |
5414 | else | |
fafc6b97 | 5415 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 5416 | end if; |
ee6ba406 | 5417 | end Division_Checks_Suppressed; |
5418 | ||
fa771c05 | 5419 | -------------------------------------- |
5420 | -- Duplicated_Tag_Checks_Suppressed -- | |
5421 | -------------------------------------- | |
5422 | ||
5423 | function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5424 | begin | |
5425 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
5426 | return Is_Check_Suppressed (E, Duplicated_Tag_Check); | |
5427 | else | |
5428 | return Scope_Suppress.Suppress (Duplicated_Tag_Check); | |
5429 | end if; | |
5430 | end Duplicated_Tag_Checks_Suppressed; | |
5431 | ||
ee6ba406 | 5432 | ----------------------------------- |
5433 | -- Elaboration_Checks_Suppressed -- | |
5434 | ----------------------------------- | |
5435 | ||
5436 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5437 | begin | |
38f5559f | 5438 | -- The complication in this routine is that if we are in the dynamic |
5439 | -- model of elaboration, we also check All_Checks, since All_Checks | |
5440 | -- does not set Elaboration_Check explicitly. | |
5441 | ||
9dfe12ae | 5442 | if Present (E) then |
5443 | if Kill_Elaboration_Checks (E) then | |
5444 | return True; | |
38f5559f | 5445 | |
9dfe12ae | 5446 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 5447 | if Is_Check_Suppressed (E, Elaboration_Check) then |
5448 | return True; | |
fe48a434 | 5449 | |
38f5559f | 5450 | elsif Dynamic_Elaboration_Checks then |
5451 | return Is_Check_Suppressed (E, All_Checks); | |
fe48a434 | 5452 | |
38f5559f | 5453 | else |
5454 | return False; | |
5455 | end if; | |
9dfe12ae | 5456 | end if; |
5457 | end if; | |
5458 | ||
fafc6b97 | 5459 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 5460 | return True; |
fe48a434 | 5461 | |
38f5559f | 5462 | elsif Dynamic_Elaboration_Checks then |
fafc6b97 | 5463 | return Scope_Suppress.Suppress (All_Checks); |
fe48a434 | 5464 | |
38f5559f | 5465 | else |
5466 | return False; | |
5467 | end if; | |
ee6ba406 | 5468 | end Elaboration_Checks_Suppressed; |
5469 | ||
9dfe12ae | 5470 | --------------------------- |
5471 | -- Enable_Overflow_Check -- | |
5472 | --------------------------- | |
5473 | ||
5474 | procedure Enable_Overflow_Check (N : Node_Id) is | |
4c1c7f3f | 5475 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 5476 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 5477 | Chk : Nat; |
5478 | OK : Boolean; | |
5479 | Ent : Entity_Id; | |
5480 | Ofs : Uint; | |
5481 | Lo : Uint; | |
5482 | Hi : Uint; | |
ee6ba406 | 5483 | |
f3ccbbb3 | 5484 | Do_Ovflow_Check : Boolean; |
5485 | ||
ee6ba406 | 5486 | begin |
9dfe12ae | 5487 | if Debug_Flag_CC then |
5488 | w ("Enable_Overflow_Check for node ", Int (N)); | |
5489 | Write_Str (" Source location = "); | |
5490 | wl (Sloc (N)); | |
00c403ee | 5491 | pg (Union_Id (N)); |
ee6ba406 | 5492 | end if; |
ee6ba406 | 5493 | |
75209ec5 | 5494 | -- No check if overflow checks suppressed for type of node |
5495 | ||
0df9d43f | 5496 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 5497 | return; |
5498 | ||
49260fa5 | 5499 | -- Nothing to do for unsigned integer types, which do not overflow |
5500 | ||
5501 | elsif Is_Modular_Integer_Type (Typ) then | |
5502 | return; | |
3cce7f32 | 5503 | end if; |
5504 | ||
0df9d43f | 5505 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 5506 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
5507 | -- probably more extreme that it needs to be, but what is going on here | |
5508 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 5509 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 5510 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 5511 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 5512 | -- legacy reliable. |
3cce7f32 | 5513 | |
0df9d43f | 5514 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 5515 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
5516 | -- know that no check is needed. We skip all that in the two new modes, | |
5517 | -- since really overflow checking happens over a whole subtree, and we | |
5518 | -- do the corresponding optimizations later on when applying the checks. | |
5519 | ||
5520 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 5521 | if not (Overflow_Checks_Suppressed (Etype (N))) |
5522 | and then not (Is_Entity_Name (N) | |
5523 | and then Overflow_Checks_Suppressed (Entity (N))) | |
5524 | then | |
5525 | Activate_Overflow_Check (N); | |
5526 | end if; | |
3cce7f32 | 5527 | |
5528 | if Debug_Flag_CC then | |
5529 | w ("Minimized/Eliminated mode"); | |
5530 | end if; | |
5531 | ||
5532 | return; | |
5533 | end if; | |
5534 | ||
0df9d43f | 5535 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 5536 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 5537 | |
feff2f05 | 5538 | -- Nothing to do if the range of the result is known OK. We skip this |
5539 | -- for conversions, since the caller already did the check, and in any | |
5540 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 5541 | -- different. |
ee6ba406 | 5542 | |
3cce7f32 | 5543 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 5544 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 5545 | |
cc60bd16 | 5546 | -- Note in the test below that we assume that the range is not OK |
5547 | -- if a bound of the range is equal to that of the type. That's not | |
5548 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 5549 | |
9dfe12ae | 5550 | -- a) The way that Determine_Range works, it will typically report |
5551 | -- the bounds of the value as being equal to the bounds of the | |
5552 | -- type, because it either can't tell anything more precise, or | |
5553 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 5554 | |
9dfe12ae | 5555 | -- b) It is very unusual to have a situation in which this would |
5556 | -- generate an unnecessary overflow check (an example would be | |
5557 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 5558 | -- literal value one is added). |
ee6ba406 | 5559 | |
9dfe12ae | 5560 | -- c) The alternative is a lot of special casing in this routine |
5561 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 5562 | |
f3ccbbb3 | 5563 | if OK then |
5564 | Do_Ovflow_Check := True; | |
5565 | ||
5566 | -- Note that the following checks are quite deliberately > and < | |
5567 | -- rather than >= and <= as explained above. | |
5568 | ||
5569 | if Lo > Expr_Value (Type_Low_Bound (Typ)) | |
5570 | and then | |
5571 | Hi < Expr_Value (Type_High_Bound (Typ)) | |
5572 | then | |
5573 | Do_Ovflow_Check := False; | |
5574 | ||
5575 | -- Despite the comments above, it is worth dealing specially with | |
5576 | -- division specially. The only case where integer division can | |
5577 | -- overflow is (largest negative number) / (-1). So we will do | |
5578 | -- an extra range analysis to see if this is possible. | |
5579 | ||
5580 | elsif Nkind (N) = N_Op_Divide then | |
5581 | Determine_Range | |
5582 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5583 | ||
5584 | if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then | |
5585 | Do_Ovflow_Check := False; | |
5586 | ||
5587 | else | |
5588 | Determine_Range | |
5589 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5590 | ||
5591 | if OK and then (Lo > Uint_Minus_1 | |
5592 | or else | |
5593 | Hi < Uint_Minus_1) | |
5594 | then | |
5595 | Do_Ovflow_Check := False; | |
5596 | end if; | |
5597 | end if; | |
9dfe12ae | 5598 | end if; |
5599 | ||
f3ccbbb3 | 5600 | -- If no overflow check required, we are done |
5601 | ||
5602 | if not Do_Ovflow_Check then | |
5603 | if Debug_Flag_CC then | |
5604 | w ("No overflow check required"); | |
5605 | end if; | |
5606 | ||
5607 | return; | |
5608 | end if; | |
9dfe12ae | 5609 | end if; |
5610 | end if; | |
5611 | ||
feff2f05 | 5612 | -- If not in optimizing mode, set flag and we are done. We are also done |
5613 | -- (and just set the flag) if the type is not a discrete type, since it | |
5614 | -- is not worth the effort to eliminate checks for other than discrete | |
5615 | -- types. In addition, we take this same path if we have stored the | |
5616 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5617 | -- but we do not want to blow up). |
9dfe12ae | 5618 | |
5619 | if Optimization_Level = 0 | |
5620 | or else not Is_Discrete_Type (Etype (N)) | |
5621 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5622 | then |
00c403ee | 5623 | Activate_Overflow_Check (N); |
9dfe12ae | 5624 | |
5625 | if Debug_Flag_CC then | |
5626 | w ("Optimization off"); | |
5627 | end if; | |
5628 | ||
ee6ba406 | 5629 | return; |
9dfe12ae | 5630 | end if; |
ee6ba406 | 5631 | |
9dfe12ae | 5632 | -- Otherwise evaluate and check the expression |
5633 | ||
5634 | Find_Check | |
5635 | (Expr => N, | |
5636 | Check_Type => 'O', | |
5637 | Target_Type => Empty, | |
5638 | Entry_OK => OK, | |
5639 | Check_Num => Chk, | |
5640 | Ent => Ent, | |
5641 | Ofs => Ofs); | |
5642 | ||
5643 | if Debug_Flag_CC then | |
5644 | w ("Called Find_Check"); | |
5645 | w (" OK = ", OK); | |
5646 | ||
5647 | if OK then | |
5648 | w (" Check_Num = ", Chk); | |
5649 | w (" Ent = ", Int (Ent)); | |
5650 | Write_Str (" Ofs = "); | |
5651 | pid (Ofs); | |
5652 | end if; | |
5653 | end if; | |
ee6ba406 | 5654 | |
9dfe12ae | 5655 | -- If check is not of form to optimize, then set flag and we are done |
5656 | ||
5657 | if not OK then | |
00c403ee | 5658 | Activate_Overflow_Check (N); |
ee6ba406 | 5659 | return; |
9dfe12ae | 5660 | end if; |
ee6ba406 | 5661 | |
9dfe12ae | 5662 | -- If check is already performed, then return without setting flag |
5663 | ||
5664 | if Chk /= 0 then | |
5665 | if Debug_Flag_CC then | |
5666 | w ("Check suppressed!"); | |
5667 | end if; | |
ee6ba406 | 5668 | |
ee6ba406 | 5669 | return; |
9dfe12ae | 5670 | end if; |
ee6ba406 | 5671 | |
9dfe12ae | 5672 | -- Here we will make a new entry for the new check |
5673 | ||
00c403ee | 5674 | Activate_Overflow_Check (N); |
9dfe12ae | 5675 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5676 | Saved_Checks (Num_Saved_Checks) := | |
5677 | (Killed => False, | |
5678 | Entity => Ent, | |
5679 | Offset => Ofs, | |
5680 | Check_Type => 'O', | |
5681 | Target_Type => Empty); | |
5682 | ||
5683 | if Debug_Flag_CC then | |
5684 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5685 | w (" Entity = ", Int (Ent)); | |
5686 | Write_Str (" Offset = "); | |
5687 | pid (Ofs); | |
5688 | w (" Check_Type = O"); | |
5689 | w (" Target_Type = Empty"); | |
5690 | end if; | |
ee6ba406 | 5691 | |
feff2f05 | 5692 | -- If we get an exception, then something went wrong, probably because of |
20cf157b | 5693 | -- an error in the structure of the tree due to an incorrect program. Or |
5694 | -- it may be a bug in the optimization circuit. In either case the safest | |
feff2f05 | 5695 | -- thing is simply to set the check flag unconditionally. |
9dfe12ae | 5696 | |
5697 | exception | |
5698 | when others => | |
00c403ee | 5699 | Activate_Overflow_Check (N); |
9dfe12ae | 5700 | |
5701 | if Debug_Flag_CC then | |
5702 | w (" exception occurred, overflow flag set"); | |
5703 | end if; | |
5704 | ||
5705 | return; | |
5706 | end Enable_Overflow_Check; | |
5707 | ||
5708 | ------------------------ | |
5709 | -- Enable_Range_Check -- | |
5710 | ------------------------ | |
5711 | ||
5712 | procedure Enable_Range_Check (N : Node_Id) is | |
5713 | Chk : Nat; | |
5714 | OK : Boolean; | |
5715 | Ent : Entity_Id; | |
5716 | Ofs : Uint; | |
5717 | Ttyp : Entity_Id; | |
5718 | P : Node_Id; | |
5719 | ||
5720 | begin | |
feff2f05 | 5721 | -- Return if unchecked type conversion with range check killed. In this |
39a0c1d3 | 5722 | -- case we never set the flag (that's what Kill_Range_Check is about). |
9dfe12ae | 5723 | |
5724 | if Nkind (N) = N_Unchecked_Type_Conversion | |
5725 | and then Kill_Range_Check (N) | |
ee6ba406 | 5726 | then |
5727 | return; | |
9dfe12ae | 5728 | end if; |
ee6ba406 | 5729 | |
55e8372b | 5730 | -- Do not set range check flag if parent is assignment statement or |
5731 | -- object declaration with Suppress_Assignment_Checks flag set | |
5732 | ||
5733 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
5734 | and then Suppress_Assignment_Checks (Parent (N)) | |
5735 | then | |
5736 | return; | |
5737 | end if; | |
5738 | ||
0577b0b1 | 5739 | -- Check for various cases where we should suppress the range check |
5740 | ||
5741 | -- No check if range checks suppressed for type of node | |
5742 | ||
20cf157b | 5743 | if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
0577b0b1 | 5744 | return; |
5745 | ||
5746 | -- No check if node is an entity name, and range checks are suppressed | |
5747 | -- for this entity, or for the type of this entity. | |
5748 | ||
5749 | elsif Is_Entity_Name (N) | |
5750 | and then (Range_Checks_Suppressed (Entity (N)) | |
20cf157b | 5751 | or else Range_Checks_Suppressed (Etype (Entity (N)))) |
0577b0b1 | 5752 | then |
5753 | return; | |
5754 | ||
5755 | -- No checks if index of array, and index checks are suppressed for | |
5756 | -- the array object or the type of the array. | |
5757 | ||
5758 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
5759 | declare | |
5760 | Pref : constant Node_Id := Prefix (Parent (N)); | |
5761 | begin | |
5762 | if Is_Entity_Name (Pref) | |
5763 | and then Index_Checks_Suppressed (Entity (Pref)) | |
5764 | then | |
5765 | return; | |
5766 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
5767 | return; | |
5768 | end if; | |
5769 | end; | |
5770 | end if; | |
5771 | ||
9dfe12ae | 5772 | -- Debug trace output |
ee6ba406 | 5773 | |
9dfe12ae | 5774 | if Debug_Flag_CC then |
5775 | w ("Enable_Range_Check for node ", Int (N)); | |
5776 | Write_Str (" Source location = "); | |
5777 | wl (Sloc (N)); | |
00c403ee | 5778 | pg (Union_Id (N)); |
9dfe12ae | 5779 | end if; |
5780 | ||
feff2f05 | 5781 | -- If not in optimizing mode, set flag and we are done. We are also done |
5782 | -- (and just set the flag) if the type is not a discrete type, since it | |
5783 | -- is not worth the effort to eliminate checks for other than discrete | |
5784 | -- types. In addition, we take this same path if we have stored the | |
5785 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5786 | -- but we do not want to blow up). |
9dfe12ae | 5787 | |
5788 | if Optimization_Level = 0 | |
5789 | or else No (Etype (N)) | |
5790 | or else not Is_Discrete_Type (Etype (N)) | |
5791 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5792 | then |
00c403ee | 5793 | Activate_Range_Check (N); |
9dfe12ae | 5794 | |
5795 | if Debug_Flag_CC then | |
5796 | w ("Optimization off"); | |
5797 | end if; | |
5798 | ||
ee6ba406 | 5799 | return; |
9dfe12ae | 5800 | end if; |
ee6ba406 | 5801 | |
9dfe12ae | 5802 | -- Otherwise find out the target type |
ee6ba406 | 5803 | |
9dfe12ae | 5804 | P := Parent (N); |
ee6ba406 | 5805 | |
9dfe12ae | 5806 | -- For assignment, use left side subtype |
5807 | ||
5808 | if Nkind (P) = N_Assignment_Statement | |
5809 | and then Expression (P) = N | |
5810 | then | |
5811 | Ttyp := Etype (Name (P)); | |
5812 | ||
5813 | -- For indexed component, use subscript subtype | |
5814 | ||
5815 | elsif Nkind (P) = N_Indexed_Component then | |
5816 | declare | |
5817 | Atyp : Entity_Id; | |
5818 | Indx : Node_Id; | |
5819 | Subs : Node_Id; | |
5820 | ||
5821 | begin | |
5822 | Atyp := Etype (Prefix (P)); | |
5823 | ||
5824 | if Is_Access_Type (Atyp) then | |
5825 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 5826 | |
5827 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 5828 | -- perform check unconditionally: it depends on the bounds of |
5829 | -- an object and we cannot currently recognize whether the test | |
5830 | -- may be redundant. | |
f07ea091 | 5831 | |
5832 | if not Is_Constrained (Atyp) then | |
00c403ee | 5833 | Activate_Range_Check (N); |
f07ea091 | 5834 | return; |
5835 | end if; | |
7189d17f | 5836 | |
31831d39 | 5837 | -- Ditto if prefix is simply an unconstrained array. We used |
5838 | -- to think this case was OK, if the prefix was not an explicit | |
5839 | -- dereference, but we have now seen a case where this is not | |
5840 | -- true, so it is safer to just suppress the optimization in this | |
5841 | -- case. The back end is getting better at eliminating redundant | |
5842 | -- checks in any case, so the loss won't be important. | |
7189d17f | 5843 | |
31831d39 | 5844 | elsif Is_Array_Type (Atyp) |
7189d17f | 5845 | and then not Is_Constrained (Atyp) |
5846 | then | |
00c403ee | 5847 | Activate_Range_Check (N); |
7189d17f | 5848 | return; |
9dfe12ae | 5849 | end if; |
5850 | ||
5851 | Indx := First_Index (Atyp); | |
5852 | Subs := First (Expressions (P)); | |
5853 | loop | |
5854 | if Subs = N then | |
5855 | Ttyp := Etype (Indx); | |
5856 | exit; | |
5857 | end if; | |
5858 | ||
5859 | Next_Index (Indx); | |
5860 | Next (Subs); | |
5861 | end loop; | |
5862 | end; | |
5863 | ||
5864 | -- For now, ignore all other cases, they are not so interesting | |
5865 | ||
5866 | else | |
5867 | if Debug_Flag_CC then | |
5868 | w (" target type not found, flag set"); | |
5869 | end if; | |
5870 | ||
00c403ee | 5871 | Activate_Range_Check (N); |
9dfe12ae | 5872 | return; |
5873 | end if; | |
5874 | ||
5875 | -- Evaluate and check the expression | |
5876 | ||
5877 | Find_Check | |
5878 | (Expr => N, | |
5879 | Check_Type => 'R', | |
5880 | Target_Type => Ttyp, | |
5881 | Entry_OK => OK, | |
5882 | Check_Num => Chk, | |
5883 | Ent => Ent, | |
5884 | Ofs => Ofs); | |
5885 | ||
5886 | if Debug_Flag_CC then | |
5887 | w ("Called Find_Check"); | |
5888 | w ("Target_Typ = ", Int (Ttyp)); | |
5889 | w (" OK = ", OK); | |
5890 | ||
5891 | if OK then | |
5892 | w (" Check_Num = ", Chk); | |
5893 | w (" Ent = ", Int (Ent)); | |
5894 | Write_Str (" Ofs = "); | |
5895 | pid (Ofs); | |
5896 | end if; | |
5897 | end if; | |
5898 | ||
5899 | -- If check is not of form to optimize, then set flag and we are done | |
5900 | ||
5901 | if not OK then | |
5902 | if Debug_Flag_CC then | |
5903 | w (" expression not of optimizable type, flag set"); | |
5904 | end if; | |
5905 | ||
00c403ee | 5906 | Activate_Range_Check (N); |
9dfe12ae | 5907 | return; |
5908 | end if; | |
5909 | ||
5910 | -- If check is already performed, then return without setting flag | |
5911 | ||
5912 | if Chk /= 0 then | |
5913 | if Debug_Flag_CC then | |
5914 | w ("Check suppressed!"); | |
5915 | end if; | |
5916 | ||
5917 | return; | |
5918 | end if; | |
5919 | ||
5920 | -- Here we will make a new entry for the new check | |
5921 | ||
00c403ee | 5922 | Activate_Range_Check (N); |
9dfe12ae | 5923 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5924 | Saved_Checks (Num_Saved_Checks) := | |
5925 | (Killed => False, | |
5926 | Entity => Ent, | |
5927 | Offset => Ofs, | |
5928 | Check_Type => 'R', | |
5929 | Target_Type => Ttyp); | |
5930 | ||
5931 | if Debug_Flag_CC then | |
5932 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5933 | w (" Entity = ", Int (Ent)); | |
5934 | Write_Str (" Offset = "); | |
5935 | pid (Ofs); | |
5936 | w (" Check_Type = R"); | |
5937 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 5938 | pg (Union_Id (Ttyp)); |
9dfe12ae | 5939 | end if; |
5940 | ||
feff2f05 | 5941 | -- If we get an exception, then something went wrong, probably because of |
5942 | -- an error in the structure of the tree due to an incorrect program. Or | |
5943 | -- it may be a bug in the optimization circuit. In either case the safest | |
5944 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 5945 | |
5946 | exception | |
5947 | when others => | |
00c403ee | 5948 | Activate_Range_Check (N); |
9dfe12ae | 5949 | |
5950 | if Debug_Flag_CC then | |
5951 | w (" exception occurred, range flag set"); | |
5952 | end if; | |
5953 | ||
5954 | return; | |
5955 | end Enable_Range_Check; | |
5956 | ||
5957 | ------------------ | |
5958 | -- Ensure_Valid -- | |
5959 | ------------------ | |
5960 | ||
aaec8d13 | 5961 | procedure Ensure_Valid |
5962 | (Expr : Node_Id; | |
5963 | Holes_OK : Boolean := False; | |
5964 | Related_Id : Entity_Id := Empty; | |
5965 | Is_Low_Bound : Boolean := False; | |
5966 | Is_High_Bound : Boolean := False) | |
5967 | is | |
9dfe12ae | 5968 | Typ : constant Entity_Id := Etype (Expr); |
5969 | ||
5970 | begin | |
5971 | -- Ignore call if we are not doing any validity checking | |
5972 | ||
5973 | if not Validity_Checks_On then | |
5974 | return; | |
5975 | ||
0577b0b1 | 5976 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 5977 | |
0577b0b1 | 5978 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 5979 | return; |
5980 | ||
feff2f05 | 5981 | -- No check required if expression is from the expander, we assume the |
5982 | -- expander will generate whatever checks are needed. Note that this is | |
39a0c1d3 | 5983 | -- not just an optimization, it avoids infinite recursions. |
9dfe12ae | 5984 | |
5985 | -- Unchecked conversions must be checked, unless they are initialized | |
5986 | -- scalar values, as in a component assignment in an init proc. | |
5987 | ||
5988 | -- In addition, we force a check if Force_Validity_Checks is set | |
5989 | ||
5990 | elsif not Comes_From_Source (Expr) | |
404eb7da | 5991 | and then not |
5992 | (Nkind (Expr) = N_Identifier | |
5993 | and then Present (Renamed_Object (Entity (Expr))) | |
5994 | and then Comes_From_Source (Renamed_Object (Entity (Expr)))) | |
9dfe12ae | 5995 | and then not Force_Validity_Checks |
5996 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
5997 | or else Kill_Range_Check (Expr)) | |
5998 | then | |
5999 | return; | |
6000 | ||
6001 | -- No check required if expression is known to have valid value | |
6002 | ||
6003 | elsif Expr_Known_Valid (Expr) then | |
6004 | return; | |
6005 | ||
ea1f4b87 | 6006 | -- No check needed within a generated predicate function. Validity |
6007 | -- of input value will have been checked earlier. | |
6008 | ||
6009 | elsif Ekind (Current_Scope) = E_Function | |
6010 | and then Is_Predicate_Function (Current_Scope) | |
6011 | then | |
6012 | return; | |
6013 | ||
feff2f05 | 6014 | -- Ignore case of enumeration with holes where the flag is set not to |
6015 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 6016 | |
6017 | elsif Is_Enumeration_Type (Typ) | |
6018 | and then Has_Non_Standard_Rep (Typ) | |
6019 | and then Holes_OK | |
6020 | then | |
6021 | return; | |
6022 | ||
f2a06be9 | 6023 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 6024 | |
6025 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
6026 | and then Expr = Name (Parent (Expr)) | |
6027 | then | |
6028 | return; | |
6029 | ||
6fb3c314 | 6030 | -- No check on a universal real constant. The context will eventually |
38f5559f | 6031 | -- convert it to a machine number for some target type, or report an |
6032 | -- illegality. | |
6033 | ||
6034 | elsif Nkind (Expr) = N_Real_Literal | |
6035 | and then Etype (Expr) = Universal_Real | |
6036 | then | |
6037 | return; | |
6038 | ||
6fb3c314 | 6039 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 6040 | -- no possible check applies. We ignore the old ACATS chestnuts that |
6041 | -- involve Boolean range True..True. | |
6042 | ||
6043 | -- Note: validity checks are generated for expressions that yield a | |
6044 | -- scalar type, when it is possible to create a value that is outside of | |
6045 | -- the type. If this is a one-bit boolean no such value exists. This is | |
6046 | -- an optimization, and it also prevents compiler blowing up during the | |
6047 | -- elaboration of improperly expanded packed array references. | |
6048 | ||
6049 | elsif Nkind (Expr) = N_Indexed_Component | |
6050 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
6051 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
6052 | then | |
6053 | return; | |
6054 | ||
737e8460 | 6055 | -- For an expression with actions, we want to insert the validity check |
6056 | -- on the final Expression. | |
6057 | ||
6058 | elsif Nkind (Expr) = N_Expression_With_Actions then | |
6059 | Ensure_Valid (Expression (Expr)); | |
6060 | return; | |
6061 | ||
9dfe12ae | 6062 | -- An annoying special case. If this is an out parameter of a scalar |
6063 | -- type, then the value is not going to be accessed, therefore it is | |
6064 | -- inappropriate to do any validity check at the call site. | |
6065 | ||
6066 | else | |
6067 | -- Only need to worry about scalar types | |
6068 | ||
6069 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 6070 | declare |
6071 | P : Node_Id; | |
6072 | N : Node_Id; | |
6073 | E : Entity_Id; | |
6074 | F : Entity_Id; | |
6075 | A : Node_Id; | |
6076 | L : List_Id; | |
6077 | ||
6078 | begin | |
6079 | -- Find actual argument (which may be a parameter association) | |
6080 | -- and the parent of the actual argument (the call statement) | |
6081 | ||
6082 | N := Expr; | |
6083 | P := Parent (Expr); | |
6084 | ||
6085 | if Nkind (P) = N_Parameter_Association then | |
6086 | N := P; | |
6087 | P := Parent (N); | |
6088 | end if; | |
6089 | ||
feff2f05 | 6090 | -- Only need to worry if we are argument of a procedure call |
6091 | -- since functions don't have out parameters. If this is an | |
6092 | -- indirect or dispatching call, get signature from the | |
6093 | -- subprogram type. | |
ee6ba406 | 6094 | |
6095 | if Nkind (P) = N_Procedure_Call_Statement then | |
6096 | L := Parameter_Associations (P); | |
9dfe12ae | 6097 | |
6098 | if Is_Entity_Name (Name (P)) then | |
6099 | E := Entity (Name (P)); | |
6100 | else | |
6101 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
6102 | E := Etype (Name (P)); | |
6103 | end if; | |
ee6ba406 | 6104 | |
feff2f05 | 6105 | -- Only need to worry if there are indeed actuals, and if |
6106 | -- this could be a procedure call, otherwise we cannot get a | |
6107 | -- match (either we are not an argument, or the mode of the | |
6108 | -- formal is not OUT). This test also filters out the | |
6109 | -- generic case. | |
ee6ba406 | 6110 | |
20cf157b | 6111 | if Is_Non_Empty_List (L) and then Is_Subprogram (E) then |
6112 | ||
feff2f05 | 6113 | -- This is the loop through parameters, looking for an |
6114 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 6115 | |
6116 | F := First_Formal (E); | |
6117 | A := First (L); | |
ee6ba406 | 6118 | while Present (F) loop |
6119 | if Ekind (F) = E_Out_Parameter and then A = N then | |
6120 | return; | |
6121 | end if; | |
6122 | ||
6123 | Next_Formal (F); | |
6124 | Next (A); | |
6125 | end loop; | |
6126 | end if; | |
6127 | end if; | |
6128 | end; | |
6129 | end if; | |
6130 | end if; | |
6131 | ||
fa6a6949 | 6132 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 6133 | -- checking: if they are valid, a boolean or short-circuit operation |
6134 | -- with them will be valid as well. | |
784d4230 | 6135 | |
6136 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 6137 | and then |
fa6a6949 | 6138 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 6139 | then |
6140 | return; | |
6141 | end if; | |
6142 | ||
0577b0b1 | 6143 | -- If we fall through, a validity check is required |
ee6ba406 | 6144 | |
aaec8d13 | 6145 | Insert_Valid_Check (Expr, Related_Id, Is_Low_Bound, Is_High_Bound); |
ce7498d3 | 6146 | |
6147 | if Is_Entity_Name (Expr) | |
6148 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
6149 | then | |
6150 | Set_Is_Known_Valid (Entity (Expr)); | |
6151 | end if; | |
ee6ba406 | 6152 | end Ensure_Valid; |
6153 | ||
6154 | ---------------------- | |
6155 | -- Expr_Known_Valid -- | |
6156 | ---------------------- | |
6157 | ||
6158 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
6159 | Typ : constant Entity_Id := Etype (Expr); | |
6160 | ||
6161 | begin | |
feff2f05 | 6162 | -- Non-scalar types are always considered valid, since they never give |
6163 | -- rise to the issues of erroneous or bounded error behavior that are | |
6164 | -- the concern. In formal reference manual terms the notion of validity | |
6165 | -- only applies to scalar types. Note that even when packed arrays are | |
6166 | -- represented using modular types, they are still arrays semantically, | |
6167 | -- so they are also always valid (in particular, the unused bits can be | |
6168 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 6169 | |
a88a5773 | 6170 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then |
ee6ba406 | 6171 | return True; |
6172 | ||
6173 | -- If no validity checking, then everything is considered valid | |
6174 | ||
6175 | elsif not Validity_Checks_On then | |
6176 | return True; | |
6177 | ||
6178 | -- Floating-point types are considered valid unless floating-point | |
6179 | -- validity checks have been specifically turned on. | |
6180 | ||
6181 | elsif Is_Floating_Point_Type (Typ) | |
6182 | and then not Validity_Check_Floating_Point | |
6183 | then | |
6184 | return True; | |
6185 | ||
feff2f05 | 6186 | -- If the expression is the value of an object that is known to be |
6187 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 6188 | |
6189 | elsif Is_Entity_Name (Expr) | |
6190 | and then Is_Known_Valid (Entity (Expr)) | |
985fe5d6 | 6191 | |
6192 | -- Exclude volatile variables | |
6193 | ||
6194 | and then not Treat_As_Volatile (Entity (Expr)) | |
ee6ba406 | 6195 | then |
6196 | return True; | |
6197 | ||
0577b0b1 | 6198 | -- References to discriminants are always considered valid. The value |
6199 | -- of a discriminant gets checked when the object is built. Within the | |
6200 | -- record, we consider it valid, and it is important to do so, since | |
6201 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 6202 | -- reference discriminants out of scope. Discriminants of concurrent |
6203 | -- types are excluded for the same reason. | |
0577b0b1 | 6204 | |
6205 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 6206 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 6207 | then |
6208 | return True; | |
6209 | ||
feff2f05 | 6210 | -- If the type is one for which all values are known valid, then we are |
6211 | -- sure that the value is valid except in the slightly odd case where | |
6212 | -- the expression is a reference to a variable whose size has been | |
6213 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 6214 | |
6215 | elsif Is_Known_Valid (Typ) then | |
6216 | if Is_Entity_Name (Expr) | |
6217 | and then Ekind (Entity (Expr)) = E_Variable | |
6218 | and then Esize (Entity (Expr)) > Esize (Typ) | |
6219 | then | |
6220 | return False; | |
6221 | else | |
6222 | return True; | |
6223 | end if; | |
6224 | ||
6225 | -- Integer and character literals always have valid values, where | |
6226 | -- appropriate these will be range checked in any case. | |
6227 | ||
20cf157b | 6228 | elsif Nkind_In (Expr, N_Integer_Literal, N_Character_Literal) then |
ee6ba406 | 6229 | return True; |
91e47010 | 6230 | |
ee6ba406 | 6231 | -- If we have a type conversion or a qualification of a known valid |
6232 | -- value, then the result will always be valid. | |
6233 | ||
20cf157b | 6234 | elsif Nkind_In (Expr, N_Type_Conversion, N_Qualified_Expression) then |
ee6ba406 | 6235 | return Expr_Known_Valid (Expression (Expr)); |
6236 | ||
1eb1395f | 6237 | -- Case of expression is a non-floating-point operator. In this case we |
6238 | -- can assume the result is valid the generated code for the operator | |
6239 | -- will include whatever checks are needed (e.g. range checks) to ensure | |
6240 | -- validity. This assumption does not hold for the floating-point case, | |
6241 | -- since floating-point operators can generate Infinite or NaN results | |
6242 | -- which are considered invalid. | |
6243 | ||
6244 | -- Historical note: in older versions, the exemption of floating-point | |
6245 | -- types from this assumption was done only in cases where the parent | |
6246 | -- was an assignment, function call or parameter association. Presumably | |
6247 | -- the idea was that in other contexts, the result would be checked | |
6248 | -- elsewhere, but this list of cases was missing tests (at least the | |
6249 | -- N_Object_Declaration case, as shown by a reported missing validity | |
6250 | -- check), and it is not clear why function calls but not procedure | |
6251 | -- calls were tested for. It really seems more accurate and much | |
6252 | -- safer to recognize that expressions which are the result of a | |
6253 | -- floating-point operator can never be assumed to be valid. | |
6254 | ||
6255 | elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then | |
6256 | return True; | |
1d90d657 | 6257 | |
feff2f05 | 6258 | -- The result of a membership test is always valid, since it is true or |
6259 | -- false, there are no other possibilities. | |
0577b0b1 | 6260 | |
6261 | elsif Nkind (Expr) in N_Membership_Test then | |
6262 | return True; | |
6263 | ||
ee6ba406 | 6264 | -- For all other cases, we do not know the expression is valid |
6265 | ||
6266 | else | |
6267 | return False; | |
6268 | end if; | |
6269 | end Expr_Known_Valid; | |
6270 | ||
9dfe12ae | 6271 | ---------------- |
6272 | -- Find_Check -- | |
6273 | ---------------- | |
6274 | ||
6275 | procedure Find_Check | |
6276 | (Expr : Node_Id; | |
6277 | Check_Type : Character; | |
6278 | Target_Type : Entity_Id; | |
6279 | Entry_OK : out Boolean; | |
6280 | Check_Num : out Nat; | |
6281 | Ent : out Entity_Id; | |
6282 | Ofs : out Uint) | |
6283 | is | |
6284 | function Within_Range_Of | |
6285 | (Target_Type : Entity_Id; | |
314a23b6 | 6286 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 6287 | -- Given a requirement for checking a range against Target_Type, and |
6288 | -- and a range Check_Type against which a check has already been made, | |
6289 | -- determines if the check against check type is sufficient to ensure | |
6290 | -- that no check against Target_Type is required. | |
6291 | ||
6292 | --------------------- | |
6293 | -- Within_Range_Of -- | |
6294 | --------------------- | |
6295 | ||
6296 | function Within_Range_Of | |
6297 | (Target_Type : Entity_Id; | |
314a23b6 | 6298 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 6299 | is |
6300 | begin | |
6301 | if Target_Type = Check_Type then | |
6302 | return True; | |
6303 | ||
6304 | else | |
6305 | declare | |
6306 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
6307 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
6308 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
6309 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
6310 | ||
6311 | begin | |
6312 | if (Tlo = Clo | |
6313 | or else (Compile_Time_Known_Value (Tlo) | |
6314 | and then | |
6315 | Compile_Time_Known_Value (Clo) | |
6316 | and then | |
6317 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
6318 | and then | |
6319 | (Thi = Chi | |
6320 | or else (Compile_Time_Known_Value (Thi) | |
6321 | and then | |
6322 | Compile_Time_Known_Value (Chi) | |
6323 | and then | |
6324 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
6325 | then | |
6326 | return True; | |
6327 | else | |
6328 | return False; | |
6329 | end if; | |
6330 | end; | |
6331 | end if; | |
6332 | end Within_Range_Of; | |
6333 | ||
6334 | -- Start of processing for Find_Check | |
6335 | ||
6336 | begin | |
ed195555 | 6337 | -- Establish default, in case no entry is found |
9dfe12ae | 6338 | |
6339 | Check_Num := 0; | |
6340 | ||
6341 | -- Case of expression is simple entity reference | |
6342 | ||
6343 | if Is_Entity_Name (Expr) then | |
6344 | Ent := Entity (Expr); | |
6345 | Ofs := Uint_0; | |
6346 | ||
6347 | -- Case of expression is entity + known constant | |
6348 | ||
6349 | elsif Nkind (Expr) = N_Op_Add | |
6350 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
6351 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
6352 | then | |
6353 | Ent := Entity (Left_Opnd (Expr)); | |
6354 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
6355 | ||
6356 | -- Case of expression is entity - known constant | |
6357 | ||
6358 | elsif Nkind (Expr) = N_Op_Subtract | |
6359 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
6360 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
6361 | then | |
6362 | Ent := Entity (Left_Opnd (Expr)); | |
6363 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
6364 | ||
6365 | -- Any other expression is not of the right form | |
6366 | ||
6367 | else | |
6368 | Ent := Empty; | |
6369 | Ofs := Uint_0; | |
6370 | Entry_OK := False; | |
6371 | return; | |
6372 | end if; | |
6373 | ||
feff2f05 | 6374 | -- Come here with expression of appropriate form, check if entity is an |
6375 | -- appropriate one for our purposes. | |
9dfe12ae | 6376 | |
6377 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 6378 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 6379 | and then not Is_Library_Level_Entity (Ent) |
6380 | then | |
6381 | Entry_OK := True; | |
6382 | else | |
6383 | Entry_OK := False; | |
6384 | return; | |
6385 | end if; | |
6386 | ||
6387 | -- See if there is matching check already | |
6388 | ||
6389 | for J in reverse 1 .. Num_Saved_Checks loop | |
6390 | declare | |
6391 | SC : Saved_Check renames Saved_Checks (J); | |
9dfe12ae | 6392 | begin |
6393 | if SC.Killed = False | |
6394 | and then SC.Entity = Ent | |
6395 | and then SC.Offset = Ofs | |
6396 | and then SC.Check_Type = Check_Type | |
6397 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
6398 | then | |
6399 | Check_Num := J; | |
6400 | return; | |
6401 | end if; | |
6402 | end; | |
6403 | end loop; | |
6404 | ||
6405 | -- If we fall through entry was not found | |
6406 | ||
9dfe12ae | 6407 | return; |
6408 | end Find_Check; | |
6409 | ||
6410 | --------------------------------- | |
6411 | -- Generate_Discriminant_Check -- | |
6412 | --------------------------------- | |
6413 | ||
6414 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 6415 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 6416 | |
6417 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
6418 | Loc : constant Source_Ptr := Sloc (N); | |
6419 | Pref : constant Node_Id := Prefix (N); | |
6420 | Sel : constant Node_Id := Selector_Name (N); | |
6421 | ||
6422 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 6423 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 6424 | -- The original component to be checked |
6425 | ||
6426 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 6427 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 6428 | -- The discriminant checking function |
6429 | ||
6430 | Discr : Entity_Id; | |
6431 | -- One discriminant to be checked in the type | |
6432 | ||
6433 | Real_Discr : Entity_Id; | |
6434 | -- Actual discriminant in the call | |
6435 | ||
6436 | Pref_Type : Entity_Id; | |
6437 | -- Type of relevant prefix (ignoring private/access stuff) | |
6438 | ||
6439 | Args : List_Id; | |
6440 | -- List of arguments for function call | |
6441 | ||
6442 | Formal : Entity_Id; | |
feff2f05 | 6443 | -- Keep track of the formal corresponding to the actual we build for |
6444 | -- each discriminant, in order to be able to perform the necessary type | |
6445 | -- conversions. | |
9dfe12ae | 6446 | |
6447 | Scomp : Node_Id; | |
6448 | -- Selected component reference for checking function argument | |
6449 | ||
6450 | begin | |
6451 | Pref_Type := Etype (Pref); | |
6452 | ||
6453 | -- Force evaluation of the prefix, so that it does not get evaluated | |
6454 | -- twice (once for the check, once for the actual reference). Such a | |
20cf157b | 6455 | -- double evaluation is always a potential source of inefficiency, and |
6456 | -- is functionally incorrect in the volatile case, or when the prefix | |
f9906591 | 6457 | -- may have side effects. A nonvolatile entity or a component of a |
6458 | -- nonvolatile entity requires no evaluation. | |
9dfe12ae | 6459 | |
6460 | if Is_Entity_Name (Pref) then | |
6461 | if Treat_As_Volatile (Entity (Pref)) then | |
6462 | Force_Evaluation (Pref, Name_Req => True); | |
6463 | end if; | |
6464 | ||
6465 | elsif Treat_As_Volatile (Etype (Pref)) then | |
20cf157b | 6466 | Force_Evaluation (Pref, Name_Req => True); |
9dfe12ae | 6467 | |
6468 | elsif Nkind (Pref) = N_Selected_Component | |
6469 | and then Is_Entity_Name (Prefix (Pref)) | |
6470 | then | |
6471 | null; | |
6472 | ||
6473 | else | |
6474 | Force_Evaluation (Pref, Name_Req => True); | |
6475 | end if; | |
6476 | ||
6477 | -- For a tagged type, use the scope of the original component to | |
6478 | -- obtain the type, because ??? | |
6479 | ||
6480 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
6481 | Pref_Type := Scope (Orig_Comp); | |
6482 | ||
feff2f05 | 6483 | -- For an untagged derived type, use the discriminants of the parent |
6484 | -- which have been renamed in the derivation, possibly by a one-to-many | |
d1a2e31b | 6485 | -- discriminant constraint. For untagged type, initially get the Etype |
feff2f05 | 6486 | -- of the prefix |
9dfe12ae | 6487 | |
6488 | else | |
6489 | if Is_Derived_Type (Pref_Type) | |
6490 | and then Number_Discriminants (Pref_Type) /= | |
6491 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
6492 | then | |
6493 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
6494 | end if; | |
6495 | end if; | |
6496 | ||
6497 | -- We definitely should have a checking function, This routine should | |
6498 | -- not be called if no discriminant checking function is present. | |
6499 | ||
6500 | pragma Assert (Present (Discr_Fct)); | |
6501 | ||
6502 | -- Create the list of the actual parameters for the call. This list | |
6503 | -- is the list of the discriminant fields of the record expression to | |
6504 | -- be discriminant checked. | |
6505 | ||
6506 | Args := New_List; | |
6507 | Formal := First_Formal (Discr_Fct); | |
6508 | Discr := First_Discriminant (Pref_Type); | |
6509 | while Present (Discr) loop | |
6510 | ||
6511 | -- If we have a corresponding discriminant field, and a parent | |
6512 | -- subtype is present, then we want to use the corresponding | |
6513 | -- discriminant since this is the one with the useful value. | |
6514 | ||
6515 | if Present (Corresponding_Discriminant (Discr)) | |
6516 | and then Ekind (Pref_Type) = E_Record_Type | |
6517 | and then Present (Parent_Subtype (Pref_Type)) | |
6518 | then | |
6519 | Real_Discr := Corresponding_Discriminant (Discr); | |
6520 | else | |
6521 | Real_Discr := Discr; | |
6522 | end if; | |
6523 | ||
6524 | -- Construct the reference to the discriminant | |
6525 | ||
6526 | Scomp := | |
6527 | Make_Selected_Component (Loc, | |
6528 | Prefix => | |
6529 | Unchecked_Convert_To (Pref_Type, | |
6530 | Duplicate_Subexpr (Pref)), | |
6531 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
6532 | ||
6533 | -- Manually analyze and resolve this selected component. We really | |
6534 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 6535 | -- playing discriminal games etc with this reference. Then we append |
6536 | -- the argument to the list we are gathering. | |
9dfe12ae | 6537 | |
6538 | Set_Etype (Scomp, Etype (Real_Discr)); | |
6539 | Set_Analyzed (Scomp, True); | |
6540 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
6541 | ||
6542 | Next_Formal_With_Extras (Formal); | |
6543 | Next_Discriminant (Discr); | |
6544 | end loop; | |
6545 | ||
6546 | -- Now build and insert the call | |
6547 | ||
6548 | Insert_Action (N, | |
6549 | Make_Raise_Constraint_Error (Loc, | |
6550 | Condition => | |
6551 | Make_Function_Call (Loc, | |
20cf157b | 6552 | Name => New_Occurrence_Of (Discr_Fct, Loc), |
9dfe12ae | 6553 | Parameter_Associations => Args), |
6554 | Reason => CE_Discriminant_Check_Failed)); | |
6555 | end Generate_Discriminant_Check; | |
6556 | ||
5c99c290 | 6557 | --------------------------- |
6558 | -- Generate_Index_Checks -- | |
6559 | --------------------------- | |
9dfe12ae | 6560 | |
6561 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 6562 | |
6563 | function Entity_Of_Prefix return Entity_Id; | |
6564 | -- Returns the entity of the prefix of N (or Empty if not found) | |
6565 | ||
3f42e2a7 | 6566 | ---------------------- |
6567 | -- Entity_Of_Prefix -- | |
6568 | ---------------------- | |
6569 | ||
05f3e139 | 6570 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 6571 | P : Node_Id; |
6572 | ||
05f3e139 | 6573 | begin |
e5d38095 | 6574 | P := Prefix (N); |
05f3e139 | 6575 | while not Is_Entity_Name (P) loop |
6576 | if not Nkind_In (P, N_Selected_Component, | |
6577 | N_Indexed_Component) | |
6578 | then | |
6579 | return Empty; | |
6580 | end if; | |
6581 | ||
6582 | P := Prefix (P); | |
6583 | end loop; | |
6584 | ||
6585 | return Entity (P); | |
6586 | end Entity_Of_Prefix; | |
6587 | ||
6588 | -- Local variables | |
6589 | ||
6590 | Loc : constant Source_Ptr := Sloc (N); | |
6591 | A : constant Node_Id := Prefix (N); | |
6592 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
6593 | Sub : Node_Id; | |
9dfe12ae | 6594 | |
3f42e2a7 | 6595 | -- Start of processing for Generate_Index_Checks |
6596 | ||
9dfe12ae | 6597 | begin |
05f3e139 | 6598 | -- Ignore call if the prefix is not an array since we have a serious |
6599 | -- error in the sources. Ignore it also if index checks are suppressed | |
6600 | -- for array object or type. | |
0577b0b1 | 6601 | |
05f3e139 | 6602 | if not Is_Array_Type (Etype (A)) |
20cf157b | 6603 | or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 6604 | or else Index_Checks_Suppressed (Etype (A)) |
6605 | then | |
6606 | return; | |
df9fba45 | 6607 | |
6608 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
6609 | -- prefix. This case arises when analysis has determined that constructs | |
6610 | -- such as | |
6611 | ||
6612 | -- Prefix'Loop_Entry (Expr) | |
6613 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
6614 | ||
6615 | -- require rewriting for error detection purposes. A side effect of this | |
6616 | -- action is the generation of index checks that mention 'Loop_Entry. | |
6617 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
6618 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
6619 | ||
6620 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
6621 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
6622 | then | |
6623 | return; | |
0577b0b1 | 6624 | end if; |
6625 | ||
05f3e139 | 6626 | -- Generate a raise of constraint error with the appropriate reason and |
6627 | -- a condition of the form: | |
6628 | ||
3f42e2a7 | 6629 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 6630 | |
6631 | -- Note that the reason we generate the conversion to the base type here | |
6632 | -- is that we definitely want the range check to take place, even if it | |
6633 | -- looks like the subtype is OK. Optimization considerations that allow | |
6634 | -- us to omit the check have already been taken into account in the | |
6635 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 6636 | |
9dfe12ae | 6637 | Sub := First (Expressions (N)); |
05f3e139 | 6638 | |
6639 | -- Handle string literals | |
6640 | ||
6641 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 6642 | if Do_Range_Check (Sub) then |
6643 | Set_Do_Range_Check (Sub, False); | |
6644 | ||
05f3e139 | 6645 | -- For string literals we obtain the bounds of the string from the |
6646 | -- associated subtype. | |
9dfe12ae | 6647 | |
05f3e139 | 6648 | Insert_Action (N, |
094ed68e | 6649 | Make_Raise_Constraint_Error (Loc, |
6650 | Condition => | |
6651 | Make_Not_In (Loc, | |
6652 | Left_Opnd => | |
6653 | Convert_To (Base_Type (Etype (Sub)), | |
6654 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6655 | Right_Opnd => | |
6656 | Make_Attribute_Reference (Loc, | |
83c6c069 | 6657 | Prefix => New_Occurrence_Of (Etype (A), Loc), |
094ed68e | 6658 | Attribute_Name => Name_Range)), |
6659 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6660 | end if; |
9dfe12ae | 6661 | |
05f3e139 | 6662 | -- General case |
9dfe12ae | 6663 | |
05f3e139 | 6664 | else |
6665 | declare | |
6666 | A_Idx : Node_Id := Empty; | |
6667 | A_Range : Node_Id; | |
6668 | Ind : Nat; | |
6669 | Num : List_Id; | |
6670 | Range_N : Node_Id; | |
9dfe12ae | 6671 | |
05f3e139 | 6672 | begin |
6673 | A_Idx := First_Index (Etype (A)); | |
6674 | Ind := 1; | |
6675 | while Present (Sub) loop | |
6676 | if Do_Range_Check (Sub) then | |
6677 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 6678 | |
05f3e139 | 6679 | -- Force evaluation except for the case of a simple name of |
f9906591 | 6680 | -- a nonvolatile entity. |
9dfe12ae | 6681 | |
05f3e139 | 6682 | if not Is_Entity_Name (Sub) |
6683 | or else Treat_As_Volatile (Entity (Sub)) | |
6684 | then | |
6685 | Force_Evaluation (Sub); | |
6686 | end if; | |
9dfe12ae | 6687 | |
05f3e139 | 6688 | if Nkind (A_Idx) = N_Range then |
6689 | A_Range := A_Idx; | |
6690 | ||
6691 | elsif Nkind (A_Idx) = N_Identifier | |
6692 | or else Nkind (A_Idx) = N_Expanded_Name | |
6693 | then | |
6694 | A_Range := Scalar_Range (Entity (A_Idx)); | |
6695 | ||
6696 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
6697 | A_Range := Range_Expression (Constraint (A_Idx)); | |
6698 | end if; | |
6699 | ||
6700 | -- For array objects with constant bounds we can generate | |
6701 | -- the index check using the bounds of the type of the index | |
6702 | ||
6703 | if Present (A_Ent) | |
6704 | and then Ekind (A_Ent) = E_Variable | |
6705 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
6706 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
6707 | then | |
6708 | Range_N := | |
6709 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 6710 | Prefix => |
83c6c069 | 6711 | New_Occurrence_Of (Etype (A_Idx), Loc), |
05f3e139 | 6712 | Attribute_Name => Name_Range); |
6713 | ||
6714 | -- For arrays with non-constant bounds we cannot generate | |
6715 | -- the index check using the bounds of the type of the index | |
6716 | -- since it may reference discriminants of some enclosing | |
6717 | -- type. We obtain the bounds directly from the prefix | |
6718 | -- object. | |
6719 | ||
6720 | else | |
6721 | if Ind = 1 then | |
6722 | Num := No_List; | |
6723 | else | |
6724 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
6725 | end if; | |
6726 | ||
6727 | Range_N := | |
6728 | Make_Attribute_Reference (Loc, | |
6729 | Prefix => | |
6730 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
6731 | Attribute_Name => Name_Range, | |
6732 | Expressions => Num); | |
6733 | end if; | |
6734 | ||
6735 | Insert_Action (N, | |
094ed68e | 6736 | Make_Raise_Constraint_Error (Loc, |
6737 | Condition => | |
6738 | Make_Not_In (Loc, | |
6739 | Left_Opnd => | |
6740 | Convert_To (Base_Type (Etype (Sub)), | |
6741 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6742 | Right_Opnd => Range_N), | |
6743 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6744 | end if; |
6745 | ||
6746 | A_Idx := Next_Index (A_Idx); | |
6747 | Ind := Ind + 1; | |
6748 | Next (Sub); | |
6749 | end loop; | |
6750 | end; | |
6751 | end if; | |
9dfe12ae | 6752 | end Generate_Index_Checks; |
6753 | ||
6754 | -------------------------- | |
6755 | -- Generate_Range_Check -- | |
6756 | -------------------------- | |
6757 | ||
6758 | procedure Generate_Range_Check | |
6759 | (N : Node_Id; | |
6760 | Target_Type : Entity_Id; | |
6761 | Reason : RT_Exception_Code) | |
6762 | is | |
6763 | Loc : constant Source_Ptr := Sloc (N); | |
6764 | Source_Type : constant Entity_Id := Etype (N); | |
6765 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
6766 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
6767 | ||
1f5d83cf | 6768 | procedure Convert_And_Check_Range; |
6769 | -- Convert the conversion operand to the target base type and save in | |
6770 | -- a temporary. Then check the converted value against the range of the | |
6771 | -- target subtype. | |
6772 | ||
124f1911 | 6773 | ----------------------------- |
6774 | -- Convert_And_Check_Range -- | |
6775 | ----------------------------- | |
1f5d83cf | 6776 | |
124f1911 | 6777 | procedure Convert_And_Check_Range is |
cdfda0e3 | 6778 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
6779 | Conv_Node : Node_Id; | |
1f5d83cf | 6780 | |
124f1911 | 6781 | begin |
cdfda0e3 | 6782 | -- For enumeration types with non-standard representation this is a |
6783 | -- direct conversion from the enumeration type to the target integer | |
6784 | -- type, which is treated by the back end as a normal integer type | |
6785 | -- conversion, treating the enumeration type as an integer, which is | |
6786 | -- exactly what we want. We set Conversion_OK to make sure that the | |
6787 | -- analyzer does not complain about what otherwise might be an | |
6788 | -- illegal conversion. | |
6789 | ||
6790 | if Is_Enumeration_Type (Source_Base_Type) | |
6791 | and then Present (Enum_Pos_To_Rep (Source_Base_Type)) | |
6792 | and then Is_Integer_Type (Target_Base_Type) | |
6793 | then | |
6794 | Conv_Node := | |
96cb18c0 | 6795 | OK_Convert_To |
6796 | (Typ => Target_Base_Type, | |
6797 | Expr => Duplicate_Subexpr (N)); | |
cdfda0e3 | 6798 | |
6799 | -- Common case | |
6800 | ||
6801 | else | |
6802 | Conv_Node := | |
6803 | Make_Type_Conversion (Loc, | |
6804 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
6805 | Expression => Duplicate_Subexpr (N)); | |
6806 | end if; | |
6807 | ||
124f1911 | 6808 | -- We make a temporary to hold the value of the converted value |
6809 | -- (converted to the base type), and then do the test against this | |
6810 | -- temporary. The conversion itself is replaced by an occurrence of | |
6811 | -- Tnn and followed by the explicit range check. Note that checks | |
6812 | -- are suppressed for this code, since we don't want a recursive | |
1f5d83cf | 6813 | -- range check popping up. |
6814 | ||
124f1911 | 6815 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); |
6816 | -- [constraint_error when Tnn not in Target_Type] | |
6817 | ||
1f5d83cf | 6818 | Insert_Actions (N, New_List ( |
6819 | Make_Object_Declaration (Loc, | |
6820 | Defining_Identifier => Tnn, | |
6821 | Object_Definition => New_Occurrence_Of (Target_Base_Type, Loc), | |
6822 | Constant_Present => True, | |
cdfda0e3 | 6823 | Expression => Conv_Node), |
1f5d83cf | 6824 | |
6825 | Make_Raise_Constraint_Error (Loc, | |
6826 | Condition => | |
6827 | Make_Not_In (Loc, | |
6828 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6829 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
6830 | Reason => Reason)), | |
6831 | Suppress => All_Checks); | |
6832 | ||
6833 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6834 | ||
6835 | -- Set the type of N, because the declaration for Tnn might not | |
6836 | -- be analyzed yet, as is the case if N appears within a record | |
6837 | -- declaration, as a discriminant constraint or expression. | |
6838 | ||
6839 | Set_Etype (N, Target_Base_Type); | |
6840 | end Convert_And_Check_Range; | |
6841 | ||
6842 | -- Start of processing for Generate_Range_Check | |
6843 | ||
9dfe12ae | 6844 | begin |
feff2f05 | 6845 | -- First special case, if the source type is already within the range |
6846 | -- of the target type, then no check is needed (probably we should have | |
6847 | -- stopped Do_Range_Check from being set in the first place, but better | |
cda40848 | 6848 | -- late than never in preventing junk code and junk flag settings. |
9dfe12ae | 6849 | |
7a1dabb3 | 6850 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 6851 | |
6852 | -- We do NOT apply this if the source node is a literal, since in this | |
6853 | -- case the literal has already been labeled as having the subtype of | |
6854 | -- the target. | |
6855 | ||
9dfe12ae | 6856 | and then not |
b40670e1 | 6857 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 6858 | or else |
b40670e1 | 6859 | (Is_Entity_Name (N) |
6860 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
9dfe12ae | 6861 | then |
cda40848 | 6862 | Set_Do_Range_Check (N, False); |
9dfe12ae | 6863 | return; |
6864 | end if; | |
6865 | ||
cda40848 | 6866 | -- Here a check is needed. If the expander is not active, or if we are |
6867 | -- in GNATProve mode, then simply set the Do_Range_Check flag and we | |
6868 | -- are done. In both these cases, we just want to see the range check | |
6869 | -- flag set, we do not want to generate the explicit range check code. | |
6870 | ||
6871 | if GNATprove_Mode or else not Expander_Active then | |
6872 | Set_Do_Range_Check (N, True); | |
6873 | return; | |
6874 | end if; | |
6875 | ||
6876 | -- Here we will generate an explicit range check, so we don't want to | |
6877 | -- set the Do_Range check flag, since the range check is taken care of | |
6878 | -- by the code we will generate. | |
6879 | ||
6880 | Set_Do_Range_Check (N, False); | |
6881 | ||
6882 | -- Force evaluation of the node, so that it does not get evaluated twice | |
6883 | -- (once for the check, once for the actual reference). Such a double | |
6884 | -- evaluation is always a potential source of inefficiency, and is | |
6885 | -- functionally incorrect in the volatile case. | |
9dfe12ae | 6886 | |
f47b9548 | 6887 | -- We skip the evaluation of attribute references because, after these |
6888 | -- runtime checks are generated, the expander may need to rewrite this | |
6889 | -- node (for example, see Attribute_Max_Size_In_Storage_Elements in | |
6890 | -- Expand_N_Attribute_Reference). | |
6891 | ||
6892 | if Nkind (N) /= N_Attribute_Reference | |
6893 | and then (not Is_Entity_Name (N) | |
ca09bfdb | 6894 | or else Treat_As_Volatile (Entity (N))) |
f47b9548 | 6895 | then |
6896 | Force_Evaluation (N, Mode => Strict); | |
9dfe12ae | 6897 | end if; |
6898 | ||
feff2f05 | 6899 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
6900 | -- the same since in this case we can simply do a direct check of the | |
6901 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 6902 | |
6903 | -- [constraint_error when N not in Target_Type] | |
6904 | ||
6905 | -- Note: this is by far the most common case, for example all cases of | |
6906 | -- checks on the RHS of assignments are in this category, but not all | |
6907 | -- cases are like this. Notably conversions can involve two types. | |
6908 | ||
6909 | if Source_Base_Type = Target_Base_Type then | |
99ed4b4c | 6910 | |
6911 | -- Insert the explicit range check. Note that we suppress checks for | |
6912 | -- this code, since we don't want a recursive range check popping up. | |
6913 | ||
9dfe12ae | 6914 | Insert_Action (N, |
6915 | Make_Raise_Constraint_Error (Loc, | |
6916 | Condition => | |
6917 | Make_Not_In (Loc, | |
6918 | Left_Opnd => Duplicate_Subexpr (N), | |
6919 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
99ed4b4c | 6920 | Reason => Reason), |
6921 | Suppress => All_Checks); | |
9dfe12ae | 6922 | |
6923 | -- Next test for the case where the target type is within the bounds | |
6924 | -- of the base type of the source type, since in this case we can | |
6925 | -- simply convert these bounds to the base type of T to do the test. | |
6926 | ||
6927 | -- [constraint_error when N not in | |
6928 | -- Source_Base_Type (Target_Type'First) | |
6929 | -- .. | |
6930 | -- Source_Base_Type(Target_Type'Last))] | |
6931 | ||
f2a06be9 | 6932 | -- The conversions will always work and need no check |
9dfe12ae | 6933 | |
a9b57347 | 6934 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
6935 | -- of converting from an enumeration value to an integer type, such as | |
6936 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
6937 | -- (which used to be handled by gigi). This is OK, since the conversion | |
6938 | -- itself does not require a check. | |
6939 | ||
7a1dabb3 | 6940 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
99ed4b4c | 6941 | |
6942 | -- Insert the explicit range check. Note that we suppress checks for | |
6943 | -- this code, since we don't want a recursive range check popping up. | |
6944 | ||
1f5d83cf | 6945 | if Is_Discrete_Type (Source_Base_Type) |
6946 | and then | |
6947 | Is_Discrete_Type (Target_Base_Type) | |
6948 | then | |
6949 | Insert_Action (N, | |
6950 | Make_Raise_Constraint_Error (Loc, | |
6951 | Condition => | |
6952 | Make_Not_In (Loc, | |
6953 | Left_Opnd => Duplicate_Subexpr (N), | |
6954 | ||
6955 | Right_Opnd => | |
6956 | Make_Range (Loc, | |
6957 | Low_Bound => | |
6958 | Unchecked_Convert_To (Source_Base_Type, | |
6959 | Make_Attribute_Reference (Loc, | |
6960 | Prefix => | |
6961 | New_Occurrence_Of (Target_Type, Loc), | |
6962 | Attribute_Name => Name_First)), | |
6963 | ||
6964 | High_Bound => | |
6965 | Unchecked_Convert_To (Source_Base_Type, | |
6966 | Make_Attribute_Reference (Loc, | |
6967 | Prefix => | |
6968 | New_Occurrence_Of (Target_Type, Loc), | |
6969 | Attribute_Name => Name_Last)))), | |
6970 | Reason => Reason), | |
6971 | Suppress => All_Checks); | |
9dfe12ae | 6972 | |
1f5d83cf | 6973 | -- For conversions involving at least one type that is not discrete, |
6974 | -- first convert to target type and then generate the range check. | |
6975 | -- This avoids problems with values that are close to a bound of the | |
6976 | -- target type that would fail a range check when done in a larger | |
6977 | -- source type before converting but would pass if converted with | |
6978 | -- rounding and then checked (such as in float-to-float conversions). | |
6979 | ||
6980 | else | |
6981 | Convert_And_Check_Range; | |
6982 | end if; | |
9dfe12ae | 6983 | |
feff2f05 | 6984 | -- Note that at this stage we now that the Target_Base_Type is not in |
6985 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
6986 | -- is not in this range). It could still be the case that Source_Type is | |
6987 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 6988 | |
feff2f05 | 6989 | -- If that is the case, we can freely convert the source to the target, |
6990 | -- and then test the target result against the bounds. | |
9dfe12ae | 6991 | |
7a1dabb3 | 6992 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
1f5d83cf | 6993 | Convert_And_Check_Range; |
9dfe12ae | 6994 | |
6995 | -- At this stage, we know that we have two scalar types, which are | |
6996 | -- directly convertible, and where neither scalar type has a base | |
6997 | -- range that is in the range of the other scalar type. | |
6998 | ||
6999 | -- The only way this can happen is with a signed and unsigned type. | |
7000 | -- So test for these two cases: | |
7001 | ||
7002 | else | |
7003 | -- Case of the source is unsigned and the target is signed | |
7004 | ||
7005 | if Is_Unsigned_Type (Source_Base_Type) | |
7006 | and then not Is_Unsigned_Type (Target_Base_Type) | |
7007 | then | |
7008 | -- If the source is unsigned and the target is signed, then we | |
7009 | -- know that the source is not shorter than the target (otherwise | |
7010 | -- the source base type would be in the target base type range). | |
7011 | ||
feff2f05 | 7012 | -- In other words, the unsigned type is either the same size as |
7013 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 7014 | |
7015 | pragma Assert | |
7016 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
7017 | ||
7018 | -- We only need to check the low bound if the low bound of the | |
7019 | -- target type is non-negative. If the low bound of the target | |
7020 | -- type is negative, then we know that we will fit fine. | |
7021 | ||
7022 | -- If the high bound of the target type is negative, then we | |
7023 | -- know we have a constraint error, since we can't possibly | |
7024 | -- have a negative source. | |
7025 | ||
7026 | -- With these two checks out of the way, we can do the check | |
7027 | -- using the source type safely | |
7028 | ||
39a0c1d3 | 7029 | -- This is definitely the most annoying case. |
9dfe12ae | 7030 | |
7031 | -- [constraint_error | |
7032 | -- when (Target_Type'First >= 0 | |
7033 | -- and then | |
7034 | -- N < Source_Base_Type (Target_Type'First)) | |
7035 | -- or else Target_Type'Last < 0 | |
7036 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
7037 | ||
7038 | -- We turn off all checks since we know that the conversions | |
7039 | -- will work fine, given the guards for negative values. | |
7040 | ||
7041 | Insert_Action (N, | |
7042 | Make_Raise_Constraint_Error (Loc, | |
7043 | Condition => | |
7044 | Make_Or_Else (Loc, | |
7045 | Make_Or_Else (Loc, | |
7046 | Left_Opnd => | |
7047 | Make_And_Then (Loc, | |
7048 | Left_Opnd => Make_Op_Ge (Loc, | |
7049 | Left_Opnd => | |
7050 | Make_Attribute_Reference (Loc, | |
7051 | Prefix => | |
7052 | New_Occurrence_Of (Target_Type, Loc), | |
7053 | Attribute_Name => Name_First), | |
7054 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7055 | ||
7056 | Right_Opnd => | |
7057 | Make_Op_Lt (Loc, | |
7058 | Left_Opnd => Duplicate_Subexpr (N), | |
7059 | Right_Opnd => | |
7060 | Convert_To (Source_Base_Type, | |
7061 | Make_Attribute_Reference (Loc, | |
7062 | Prefix => | |
7063 | New_Occurrence_Of (Target_Type, Loc), | |
7064 | Attribute_Name => Name_First)))), | |
7065 | ||
7066 | Right_Opnd => | |
7067 | Make_Op_Lt (Loc, | |
7068 | Left_Opnd => | |
7069 | Make_Attribute_Reference (Loc, | |
7070 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7071 | Attribute_Name => Name_Last), | |
7072 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
7073 | ||
7074 | Right_Opnd => | |
7075 | Make_Op_Gt (Loc, | |
7076 | Left_Opnd => Duplicate_Subexpr (N), | |
7077 | Right_Opnd => | |
7078 | Convert_To (Source_Base_Type, | |
7079 | Make_Attribute_Reference (Loc, | |
7080 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7081 | Attribute_Name => Name_Last)))), | |
7082 | ||
7083 | Reason => Reason), | |
7084 | Suppress => All_Checks); | |
7085 | ||
7086 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 7087 | -- the target is unsigned. |
9dfe12ae | 7088 | |
7089 | else | |
7090 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
20cf157b | 7091 | and then Is_Unsigned_Type (Target_Base_Type)); |
9dfe12ae | 7092 | |
feff2f05 | 7093 | -- If the source is signed and the target is unsigned, then we |
7094 | -- know that the target is not shorter than the source (otherwise | |
7095 | -- the target base type would be in the source base type range). | |
9dfe12ae | 7096 | |
feff2f05 | 7097 | -- In other words, the unsigned type is either the same size as |
7098 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 7099 | |
feff2f05 | 7100 | -- Clearly we have an error if the source value is negative since |
7101 | -- no unsigned type can have negative values. If the source type | |
7102 | -- is non-negative, then the check can be done using the target | |
7103 | -- type. | |
9dfe12ae | 7104 | |
7105 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
7106 | ||
7107 | -- [constraint_error | |
7108 | -- when N < 0 or else Tnn not in Target_Type]; | |
7109 | ||
feff2f05 | 7110 | -- We turn off all checks for the conversion of N to the target |
7111 | -- base type, since we generate the explicit check to ensure that | |
7112 | -- the value is non-negative | |
9dfe12ae | 7113 | |
7114 | declare | |
46eb6933 | 7115 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 7116 | |
7117 | begin | |
7118 | Insert_Actions (N, New_List ( | |
7119 | Make_Object_Declaration (Loc, | |
7120 | Defining_Identifier => Tnn, | |
7121 | Object_Definition => | |
7122 | New_Occurrence_Of (Target_Base_Type, Loc), | |
7123 | Constant_Present => True, | |
7124 | Expression => | |
a9b57347 | 7125 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 7126 | Subtype_Mark => |
7127 | New_Occurrence_Of (Target_Base_Type, Loc), | |
7128 | Expression => Duplicate_Subexpr (N))), | |
7129 | ||
7130 | Make_Raise_Constraint_Error (Loc, | |
7131 | Condition => | |
7132 | Make_Or_Else (Loc, | |
7133 | Left_Opnd => | |
7134 | Make_Op_Lt (Loc, | |
7135 | Left_Opnd => Duplicate_Subexpr (N), | |
7136 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7137 | ||
7138 | Right_Opnd => | |
7139 | Make_Not_In (Loc, | |
7140 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7141 | Right_Opnd => | |
7142 | New_Occurrence_Of (Target_Type, Loc))), | |
7143 | ||
20cf157b | 7144 | Reason => Reason)), |
9dfe12ae | 7145 | Suppress => All_Checks); |
7146 | ||
feff2f05 | 7147 | -- Set the Etype explicitly, because Insert_Actions may have |
7148 | -- placed the declaration in the freeze list for an enclosing | |
7149 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 7150 | |
7151 | Set_Etype (Tnn, Target_Base_Type); | |
7152 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
7153 | end; | |
7154 | end if; | |
7155 | end if; | |
7156 | end Generate_Range_Check; | |
7157 | ||
2af58f67 | 7158 | ------------------ |
7159 | -- Get_Check_Id -- | |
7160 | ------------------ | |
7161 | ||
7162 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
7163 | begin | |
7164 | -- For standard check name, we can do a direct computation | |
7165 | ||
7166 | if N in First_Check_Name .. Last_Check_Name then | |
7167 | return Check_Id (N - (First_Check_Name - 1)); | |
7168 | ||
7169 | -- For non-standard names added by pragma Check_Name, search table | |
7170 | ||
7171 | else | |
7172 | for J in All_Checks + 1 .. Check_Names.Last loop | |
7173 | if Check_Names.Table (J) = N then | |
7174 | return J; | |
7175 | end if; | |
7176 | end loop; | |
7177 | end if; | |
7178 | ||
7179 | -- No matching name found | |
7180 | ||
7181 | return No_Check_Id; | |
7182 | end Get_Check_Id; | |
7183 | ||
ee6ba406 | 7184 | --------------------- |
7185 | -- Get_Discriminal -- | |
7186 | --------------------- | |
7187 | ||
7188 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
7189 | Loc : constant Source_Ptr := Sloc (E); | |
7190 | D : Entity_Id; | |
7191 | Sc : Entity_Id; | |
7192 | ||
7193 | begin | |
0577b0b1 | 7194 | -- The bound can be a bona fide parameter of a protected operation, |
7195 | -- rather than a prival encoded as an in-parameter. | |
7196 | ||
7197 | if No (Discriminal_Link (Entity (Bound))) then | |
7198 | return Bound; | |
7199 | end if; | |
7200 | ||
2af58f67 | 7201 | -- Climb the scope stack looking for an enclosing protected type. If |
7202 | -- we run out of scopes, return the bound itself. | |
7203 | ||
7204 | Sc := Scope (E); | |
7205 | while Present (Sc) loop | |
7206 | if Sc = Standard_Standard then | |
7207 | return Bound; | |
2af58f67 | 7208 | elsif Ekind (Sc) = E_Protected_Type then |
7209 | exit; | |
7210 | end if; | |
7211 | ||
7212 | Sc := Scope (Sc); | |
7213 | end loop; | |
7214 | ||
ee6ba406 | 7215 | D := First_Discriminant (Sc); |
2af58f67 | 7216 | while Present (D) loop |
7217 | if Chars (D) = Chars (Bound) then | |
7218 | return New_Occurrence_Of (Discriminal (D), Loc); | |
7219 | end if; | |
ee6ba406 | 7220 | |
ee6ba406 | 7221 | Next_Discriminant (D); |
7222 | end loop; | |
7223 | ||
2af58f67 | 7224 | return Bound; |
ee6ba406 | 7225 | end Get_Discriminal; |
7226 | ||
2af58f67 | 7227 | ---------------------- |
7228 | -- Get_Range_Checks -- | |
7229 | ---------------------- | |
7230 | ||
7231 | function Get_Range_Checks | |
7232 | (Ck_Node : Node_Id; | |
7233 | Target_Typ : Entity_Id; | |
7234 | Source_Typ : Entity_Id := Empty; | |
7235 | Warn_Node : Node_Id := Empty) return Check_Result | |
7236 | is | |
7237 | begin | |
20cf157b | 7238 | return |
7239 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
2af58f67 | 7240 | end Get_Range_Checks; |
7241 | ||
ee6ba406 | 7242 | ------------------ |
7243 | -- Guard_Access -- | |
7244 | ------------------ | |
7245 | ||
7246 | function Guard_Access | |
7247 | (Cond : Node_Id; | |
7248 | Loc : Source_Ptr; | |
314a23b6 | 7249 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 7250 | is |
7251 | begin | |
7252 | if Nkind (Cond) = N_Or_Else then | |
7253 | Set_Paren_Count (Cond, 1); | |
7254 | end if; | |
7255 | ||
7256 | if Nkind (Ck_Node) = N_Allocator then | |
7257 | return Cond; | |
20cf157b | 7258 | |
ee6ba406 | 7259 | else |
7260 | return | |
7261 | Make_And_Then (Loc, | |
7262 | Left_Opnd => | |
7263 | Make_Op_Ne (Loc, | |
9dfe12ae | 7264 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 7265 | Right_Opnd => Make_Null (Loc)), |
7266 | Right_Opnd => Cond); | |
7267 | end if; | |
7268 | end Guard_Access; | |
7269 | ||
7270 | ----------------------------- | |
7271 | -- Index_Checks_Suppressed -- | |
7272 | ----------------------------- | |
7273 | ||
7274 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7275 | begin | |
9dfe12ae | 7276 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7277 | return Is_Check_Suppressed (E, Index_Check); | |
7278 | else | |
fafc6b97 | 7279 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 7280 | end if; |
ee6ba406 | 7281 | end Index_Checks_Suppressed; |
7282 | ||
7283 | ---------------- | |
7284 | -- Initialize -- | |
7285 | ---------------- | |
7286 | ||
7287 | procedure Initialize is | |
7288 | begin | |
7289 | for J in Determine_Range_Cache_N'Range loop | |
7290 | Determine_Range_Cache_N (J) := Empty; | |
7291 | end loop; | |
2af58f67 | 7292 | |
7293 | Check_Names.Init; | |
7294 | ||
7295 | for J in Int range 1 .. All_Checks loop | |
7296 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
7297 | end loop; | |
ee6ba406 | 7298 | end Initialize; |
7299 | ||
7300 | ------------------------- | |
7301 | -- Insert_Range_Checks -- | |
7302 | ------------------------- | |
7303 | ||
7304 | procedure Insert_Range_Checks | |
7305 | (Checks : Check_Result; | |
7306 | Node : Node_Id; | |
7307 | Suppress_Typ : Entity_Id; | |
7308 | Static_Sloc : Source_Ptr := No_Location; | |
7309 | Flag_Node : Node_Id := Empty; | |
7310 | Do_Before : Boolean := False) | |
7311 | is | |
2b4f2458 | 7312 | Checks_On : constant Boolean := |
7313 | not Index_Checks_Suppressed (Suppress_Typ) | |
7314 | or else | |
7315 | not Range_Checks_Suppressed (Suppress_Typ); | |
7316 | ||
7317 | Check_Node : Node_Id; | |
ee6ba406 | 7318 | Internal_Flag_Node : Node_Id := Flag_Node; |
7319 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
7320 | ||
ee6ba406 | 7321 | begin |
feff2f05 | 7322 | -- For now we just return if Checks_On is false, however this should be |
7323 | -- enhanced to check for an always True value in the condition and to | |
7324 | -- generate a compilation warning??? | |
ee6ba406 | 7325 | |
ac9184ed | 7326 | if not Expander_Active or not Checks_On then |
ee6ba406 | 7327 | return; |
7328 | end if; | |
7329 | ||
7330 | if Static_Sloc = No_Location then | |
7331 | Internal_Static_Sloc := Sloc (Node); | |
7332 | end if; | |
7333 | ||
7334 | if No (Flag_Node) then | |
7335 | Internal_Flag_Node := Node; | |
7336 | end if; | |
7337 | ||
7338 | for J in 1 .. 2 loop | |
7339 | exit when No (Checks (J)); | |
7340 | ||
7341 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
7342 | and then Present (Condition (Checks (J))) | |
7343 | then | |
7344 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
7345 | Check_Node := Checks (J); | |
7346 | Mark_Rewrite_Insertion (Check_Node); | |
7347 | ||
7348 | if Do_Before then | |
7349 | Insert_Before_And_Analyze (Node, Check_Node); | |
7350 | else | |
7351 | Insert_After_And_Analyze (Node, Check_Node); | |
7352 | end if; | |
7353 | ||
7354 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
7355 | end if; | |
7356 | ||
7357 | else | |
7358 | Check_Node := | |
f15731c4 | 7359 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
7360 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 7361 | Mark_Rewrite_Insertion (Check_Node); |
7362 | ||
7363 | if Do_Before then | |
7364 | Insert_Before_And_Analyze (Node, Check_Node); | |
7365 | else | |
7366 | Insert_After_And_Analyze (Node, Check_Node); | |
7367 | end if; | |
7368 | end if; | |
7369 | end loop; | |
7370 | end Insert_Range_Checks; | |
7371 | ||
7372 | ------------------------ | |
7373 | -- Insert_Valid_Check -- | |
7374 | ------------------------ | |
7375 | ||
aaec8d13 | 7376 | procedure Insert_Valid_Check |
7377 | (Expr : Node_Id; | |
7378 | Related_Id : Entity_Id := Empty; | |
7379 | Is_Low_Bound : Boolean := False; | |
7380 | Is_High_Bound : Boolean := False) | |
7381 | is | |
ee6ba406 | 7382 | Loc : constant Source_Ptr := Sloc (Expr); |
70580828 | 7383 | Typ : constant Entity_Id := Etype (Expr); |
8b718dab | 7384 | Exp : Node_Id; |
ee6ba406 | 7385 | |
7386 | begin | |
aaec8d13 | 7387 | -- Do not insert if checks off, or if not checking validity or if |
7388 | -- expression is known to be valid. | |
ee6ba406 | 7389 | |
0577b0b1 | 7390 | if not Validity_Checks_On |
7391 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 7392 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 7393 | then |
8b718dab | 7394 | return; |
ee6ba406 | 7395 | |
42c57d55 | 7396 | -- Do not insert checks within a predicate function. This will arise |
7397 | -- if the current unit and the predicate function are being compiled | |
7398 | -- with validity checks enabled. | |
70580828 | 7399 | |
89e864b9 | 7400 | elsif Present (Predicate_Function (Typ)) |
70580828 | 7401 | and then Current_Scope = Predicate_Function (Typ) |
7402 | then | |
7403 | return; | |
70580828 | 7404 | |
310c1cde | 7405 | -- If the expression is a packed component of a modular type of the |
7406 | -- right size, the data is always valid. | |
7407 | ||
89e864b9 | 7408 | elsif Nkind (Expr) = N_Selected_Component |
310c1cde | 7409 | and then Present (Component_Clause (Entity (Selector_Name (Expr)))) |
7410 | and then Is_Modular_Integer_Type (Typ) | |
7411 | and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) | |
7412 | then | |
7413 | return; | |
89e864b9 | 7414 | |
7415 | -- Do not generate a validity check when inside a generic unit as this | |
7416 | -- is an expansion activity. | |
7417 | ||
7418 | elsif Inside_A_Generic then | |
7419 | return; | |
310c1cde | 7420 | end if; |
7421 | ||
8b718dab | 7422 | -- If we have a checked conversion, then validity check applies to |
7423 | -- the expression inside the conversion, not the result, since if | |
7424 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 7425 | |
8b718dab | 7426 | Exp := Expr; |
7427 | while Nkind (Exp) = N_Type_Conversion loop | |
7428 | Exp := Expression (Exp); | |
7429 | end loop; | |
7430 | ||
fdb8488b | 7431 | -- Do not generate a check for a variable which already validates the |
7432 | -- value of an assignable object. | |
7433 | ||
7434 | if Is_Validation_Variable_Reference (Exp) then | |
7435 | return; | |
7436 | end if; | |
7437 | ||
0577b0b1 | 7438 | declare |
49adf385 | 7439 | CE : Node_Id; |
49adf385 | 7440 | PV : Node_Id; |
7441 | Var_Id : Entity_Id; | |
05fcfafb | 7442 | |
0577b0b1 | 7443 | begin |
fdb8488b | 7444 | -- If the expression denotes an assignable object, capture its value |
7445 | -- in a variable and replace the original expression by the variable. | |
7446 | -- This approach has several effects: | |
06ad5813 | 7447 | |
fdb8488b | 7448 | -- 1) The evaluation of the object results in only one read in the |
7449 | -- case where the object is atomic or volatile. | |
fa771c05 | 7450 | |
49adf385 | 7451 | -- Var ... := Object; -- read |
fa771c05 | 7452 | |
fdb8488b | 7453 | -- 2) The captured value is the one verified by attribute 'Valid. |
7454 | -- As a result the object is not evaluated again, which would | |
7455 | -- result in an unwanted read in the case where the object is | |
7456 | -- atomic or volatile. | |
7457 | ||
49adf385 | 7458 | -- if not Var'Valid then -- OK, no read of Object |
fdb8488b | 7459 | |
7460 | -- if not Object'Valid then -- Wrong, extra read of Object | |
7461 | ||
7462 | -- 3) The captured value replaces the original object reference. | |
7463 | -- As a result the object is not evaluated again, in the same | |
7464 | -- vein as 2). | |
7465 | ||
49adf385 | 7466 | -- ... Var ... -- OK, no read of Object |
fdb8488b | 7467 | |
7468 | -- ... Object ... -- Wrong, extra read of Object | |
06ad5813 | 7469 | |
fdb8488b | 7470 | -- 4) The use of a variable to capture the value of the object |
7471 | -- allows the propagation of any changes back to the original | |
7472 | -- object. | |
7473 | ||
7474 | -- procedure Call (Val : in out ...); | |
7475 | ||
49adf385 | 7476 | -- Var : ... := Object; -- read Object |
7477 | -- if not Var'Valid then -- validity check | |
7478 | -- Call (Var); -- modify Var | |
7479 | -- Object := Var; -- update Object | |
fdb8488b | 7480 | |
7481 | if Is_Variable (Exp) then | |
49adf385 | 7482 | Var_Id := Make_Temporary (Loc, 'T', Exp); |
fdb8488b | 7483 | |
4cb8adff | 7484 | -- Because we could be dealing with a transient scope which would |
7485 | -- cause our object declaration to remain unanalyzed we must do | |
7486 | -- some manual decoration. | |
7487 | ||
7488 | Set_Ekind (Var_Id, E_Variable); | |
7489 | Set_Etype (Var_Id, Typ); | |
7490 | ||
fdb8488b | 7491 | Insert_Action (Exp, |
7492 | Make_Object_Declaration (Loc, | |
49adf385 | 7493 | Defining_Identifier => Var_Id, |
fdb8488b | 7494 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
bf5f89dc | 7495 | Expression => New_Copy_Tree (Exp)), |
7496 | Suppress => Validity_Check); | |
7497 | ||
7498 | Set_Validated_Object (Var_Id, New_Copy_Tree (Exp)); | |
49adf385 | 7499 | Rewrite (Exp, New_Occurrence_Of (Var_Id, Loc)); |
7500 | PV := New_Occurrence_Of (Var_Id, Loc); | |
fdb8488b | 7501 | |
02585eb0 | 7502 | -- Copy the Do_Range_Check flag over to the new Exp, so it doesn't |
7503 | -- get lost. Floating point types are handled elsewhere. | |
7504 | ||
7505 | if not Is_Floating_Point_Type (Typ) then | |
7506 | Set_Do_Range_Check (Exp, Do_Range_Check (Original_Node (Exp))); | |
7507 | end if; | |
7508 | ||
fdb8488b | 7509 | -- Otherwise the expression does not denote a variable. Force its |
7510 | -- evaluation by capturing its value in a constant. Generate: | |
7511 | ||
7512 | -- Temp : constant ... := Exp; | |
7513 | ||
7514 | else | |
7515 | Force_Evaluation | |
7516 | (Exp => Exp, | |
7517 | Related_Id => Related_Id, | |
7518 | Is_Low_Bound => Is_Low_Bound, | |
7519 | Is_High_Bound => Is_High_Bound); | |
7520 | ||
7521 | PV := New_Copy_Tree (Exp); | |
7522 | end if; | |
0577b0b1 | 7523 | |
443bdccb | 7524 | -- A rather specialized test. If PV is an analyzed expression which |
7525 | -- is an indexed component of a packed array that has not been | |
7526 | -- properly expanded, turn off its Analyzed flag to make sure it | |
5f46de53 | 7527 | -- gets properly reexpanded. If the prefix is an access value, |
7528 | -- the dereference will be added later. | |
23abd64d | 7529 | |
7530 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
7531 | -- an analyze with the old parent pointer. This may point e.g. to | |
7532 | -- a subprogram call, which deactivates this expansion. | |
7533 | ||
7534 | if Analyzed (PV) | |
7535 | and then Nkind (PV) = N_Indexed_Component | |
5f46de53 | 7536 | and then Is_Array_Type (Etype (Prefix (PV))) |
a88a5773 | 7537 | and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV)))) |
23abd64d | 7538 | then |
7539 | Set_Analyzed (PV, False); | |
7540 | end if; | |
7541 | ||
fa771c05 | 7542 | -- Build the raise CE node to check for validity. We build a type |
7543 | -- qualification for the prefix, since it may not be of the form of | |
7544 | -- a name, and we don't care in this context! | |
23abd64d | 7545 | |
7546 | CE := | |
aaec8d13 | 7547 | Make_Raise_Constraint_Error (Loc, |
7548 | Condition => | |
7549 | Make_Op_Not (Loc, | |
7550 | Right_Opnd => | |
7551 | Make_Attribute_Reference (Loc, | |
7552 | Prefix => PV, | |
7553 | Attribute_Name => Name_Valid)), | |
7554 | Reason => CE_Invalid_Data); | |
23abd64d | 7555 | |
7556 | -- Insert the validity check. Note that we do this with validity | |
7557 | -- checks turned off, to avoid recursion, we do not want validity | |
39a0c1d3 | 7558 | -- checks on the validity checking code itself. |
23abd64d | 7559 | |
7560 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 7561 | |
6fb3c314 | 7562 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 7563 | -- array, then it is rewritten as a renaming declaration. If the |
7564 | -- expression is an actual in a call, it has not been expanded, | |
7565 | -- waiting for the proper point at which to do it. The same happens | |
7566 | -- with renamings, so that we have to force the expansion now. This | |
7567 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
7568 | -- and exp_ch6.adb. | |
7569 | ||
7570 | if Is_Entity_Name (Exp) | |
7571 | and then Nkind (Parent (Entity (Exp))) = | |
20cf157b | 7572 | N_Object_Renaming_Declaration |
0577b0b1 | 7573 | then |
7574 | declare | |
7575 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
7576 | begin | |
7577 | if Nkind (Old_Exp) = N_Indexed_Component | |
7578 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
7579 | then | |
7580 | Expand_Packed_Element_Reference (Old_Exp); | |
7581 | end if; | |
7582 | end; | |
7583 | end if; | |
0577b0b1 | 7584 | end; |
ee6ba406 | 7585 | end Insert_Valid_Check; |
7586 | ||
3cce7f32 | 7587 | ------------------------------------- |
7588 | -- Is_Signed_Integer_Arithmetic_Op -- | |
7589 | ------------------------------------- | |
7590 | ||
7591 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
7592 | begin | |
7593 | case Nkind (N) is | |
99378362 | 7594 | when N_Op_Abs |
7595 | | N_Op_Add | |
7596 | | N_Op_Divide | |
7597 | | N_Op_Expon | |
7598 | | N_Op_Minus | |
7599 | | N_Op_Mod | |
7600 | | N_Op_Multiply | |
7601 | | N_Op_Plus | |
7602 | | N_Op_Rem | |
7603 | | N_Op_Subtract | |
7604 | => | |
3cce7f32 | 7605 | return Is_Signed_Integer_Type (Etype (N)); |
7606 | ||
99378362 | 7607 | when N_Case_Expression |
7608 | | N_If_Expression | |
7609 | => | |
0326b4d4 | 7610 | return Is_Signed_Integer_Type (Etype (N)); |
7611 | ||
3cce7f32 | 7612 | when others => |
7613 | return False; | |
7614 | end case; | |
7615 | end Is_Signed_Integer_Arithmetic_Op; | |
7616 | ||
fa7497e8 | 7617 | ---------------------------------- |
7618 | -- Install_Null_Excluding_Check -- | |
7619 | ---------------------------------- | |
7620 | ||
7621 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 7622 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 7623 | Typ : constant Entity_Id := Etype (N); |
7624 | ||
7b31b357 | 7625 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
7626 | -- Determines if it is safe to capture Known_Non_Null status for an | |
7627 | -- the entity referenced by node N. The caller ensures that N is indeed | |
7628 | -- an entity name. It is safe to capture the non-null status for an IN | |
7629 | -- parameter when the reference occurs within a declaration that is sure | |
7630 | -- to be executed as part of the declarative region. | |
7870823d | 7631 | |
84d0d4a5 | 7632 | procedure Mark_Non_Null; |
7870823d | 7633 | -- After installation of check, if the node in question is an entity |
7634 | -- name, then mark this entity as non-null if possible. | |
7635 | ||
7b31b357 | 7636 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 7637 | E : constant Entity_Id := Entity (N); |
7638 | S : constant Entity_Id := Current_Scope; | |
7639 | S_Par : Node_Id; | |
7640 | ||
7641 | begin | |
7b31b357 | 7642 | if Ekind (E) /= E_In_Parameter then |
7643 | return False; | |
7644 | end if; | |
7870823d | 7645 | |
7646 | -- Two initial context checks. We must be inside a subprogram body | |
7647 | -- with declarations and reference must not appear in nested scopes. | |
7648 | ||
7b31b357 | 7649 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 7650 | or else Scope (E) /= S |
7651 | then | |
7652 | return False; | |
7653 | end if; | |
7654 | ||
7655 | S_Par := Parent (Parent (S)); | |
7656 | ||
7657 | if Nkind (S_Par) /= N_Subprogram_Body | |
7658 | or else No (Declarations (S_Par)) | |
7659 | then | |
7660 | return False; | |
7661 | end if; | |
7662 | ||
7663 | declare | |
7664 | N_Decl : Node_Id; | |
7665 | P : Node_Id; | |
7666 | ||
7667 | begin | |
7668 | -- Retrieve the declaration node of N (if any). Note that N | |
7669 | -- may be a part of a complex initialization expression. | |
7670 | ||
7671 | P := Parent (N); | |
7672 | N_Decl := Empty; | |
7673 | while Present (P) loop | |
7674 | ||
7b31b357 | 7675 | -- If we have a short circuit form, and we are within the right |
7676 | -- hand expression, we return false, since the right hand side | |
7677 | -- is not guaranteed to be elaborated. | |
7678 | ||
7679 | if Nkind (P) in N_Short_Circuit | |
7680 | and then N = Right_Opnd (P) | |
7681 | then | |
7682 | return False; | |
7683 | end if; | |
7684 | ||
92f1631f | 7685 | -- Similarly, if we are in an if expression and not part of the |
7686 | -- condition, then we return False, since neither the THEN or | |
7687 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 7688 | |
92f1631f | 7689 | if Nkind (P) = N_If_Expression |
7b31b357 | 7690 | and then N /= First (Expressions (P)) |
7691 | then | |
7692 | return False; | |
e977c0cf | 7693 | end if; |
7694 | ||
20cf157b | 7695 | -- If within a case expression, and not part of the expression, |
7696 | -- then return False, since a particular dependent expression | |
7697 | -- may not always be elaborated | |
e977c0cf | 7698 | |
7699 | if Nkind (P) = N_Case_Expression | |
7700 | and then N /= Expression (P) | |
7701 | then | |
7702 | return False; | |
7b31b357 | 7703 | end if; |
7704 | ||
20cf157b | 7705 | -- While traversing the parent chain, if node N belongs to a |
7706 | -- statement, then it may never appear in a declarative region. | |
7870823d | 7707 | |
7708 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
7709 | or else Nkind (P) = N_Procedure_Call_Statement | |
7710 | then | |
7711 | return False; | |
7712 | end if; | |
7713 | ||
7b31b357 | 7714 | -- If we are at a declaration, record it and exit |
7715 | ||
7870823d | 7716 | if Nkind (P) in N_Declaration |
7717 | and then Nkind (P) not in N_Subprogram_Specification | |
7718 | then | |
7719 | N_Decl := P; | |
7720 | exit; | |
7721 | end if; | |
7722 | ||
7723 | P := Parent (P); | |
7724 | end loop; | |
7725 | ||
7726 | if No (N_Decl) then | |
7727 | return False; | |
7728 | end if; | |
7729 | ||
7730 | return List_Containing (N_Decl) = Declarations (S_Par); | |
7731 | end; | |
7b31b357 | 7732 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 7733 | |
7734 | ------------------- | |
7735 | -- Mark_Non_Null -- | |
7736 | ------------------- | |
7737 | ||
7738 | procedure Mark_Non_Null is | |
7739 | begin | |
7870823d | 7740 | -- Only case of interest is if node N is an entity name |
7741 | ||
84d0d4a5 | 7742 | if Is_Entity_Name (N) then |
7870823d | 7743 | |
7744 | -- For sure, we want to clear an indication that this is known to | |
39a0c1d3 | 7745 | -- be null, since if we get past this check, it definitely is not. |
7870823d | 7746 | |
84d0d4a5 | 7747 | Set_Is_Known_Null (Entity (N), False); |
7748 | ||
7870823d | 7749 | -- We can mark the entity as known to be non-null if either it is |
7750 | -- safe to capture the value, or in the case of an IN parameter, | |
7751 | -- which is a constant, if the check we just installed is in the | |
7752 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 7753 | -- a check is decisive for the rest of the body if the expression |
7754 | -- is sure to be elaborated, since we know we have to elaborate | |
7755 | -- all declarations before executing the body. | |
7756 | ||
7757 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 7758 | |
7759 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 7760 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 7761 | then |
7762 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 7763 | end if; |
7764 | end if; | |
7765 | end Mark_Non_Null; | |
7766 | ||
7767 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 7768 | |
7769 | begin | |
c9bbc06b | 7770 | -- No need to add null-excluding checks when the tree may not be fully |
7771 | -- decorated. | |
7772 | ||
7773 | if Serious_Errors_Detected > 0 then | |
7774 | return; | |
7775 | end if; | |
7776 | ||
84d0d4a5 | 7777 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 7778 | |
46e32b5e | 7779 | -- No check inside a generic, check will be emitted in instance |
fa7497e8 | 7780 | |
84d0d4a5 | 7781 | if Inside_A_Generic then |
fa7497e8 | 7782 | return; |
84d0d4a5 | 7783 | end if; |
7784 | ||
7785 | -- No check needed if known to be non-null | |
7786 | ||
7787 | if Known_Non_Null (N) then | |
05fcfafb | 7788 | return; |
84d0d4a5 | 7789 | end if; |
fa7497e8 | 7790 | |
84d0d4a5 | 7791 | -- If known to be null, here is where we generate a compile time check |
7792 | ||
7793 | if Known_Null (N) then | |
d16989f1 | 7794 | |
20cf157b | 7795 | -- Avoid generating warning message inside init procs. In SPARK mode |
7796 | -- we can go ahead and call Apply_Compile_Time_Constraint_Error | |
46e32b5e | 7797 | -- since it will be turned into an error in any case. |
d16989f1 | 7798 | |
46e32b5e | 7799 | if (not Inside_Init_Proc or else SPARK_Mode = On) |
7800 | ||
28d5d68f | 7801 | -- Do not emit the warning within a conditional expression, |
7802 | -- where the expression might not be evaluated, and the warning | |
7803 | -- appear as extraneous noise. | |
46e32b5e | 7804 | |
7805 | and then not Within_Case_Or_If_Expression (N) | |
7806 | then | |
d16989f1 | 7807 | Apply_Compile_Time_Constraint_Error |
4098232e | 7808 | (N, "null value not allowed here??", CE_Access_Check_Failed); |
46e32b5e | 7809 | |
7810 | -- Remaining cases, where we silently insert the raise | |
7811 | ||
d16989f1 | 7812 | else |
7813 | Insert_Action (N, | |
7814 | Make_Raise_Constraint_Error (Loc, | |
7815 | Reason => CE_Access_Check_Failed)); | |
7816 | end if; | |
7817 | ||
84d0d4a5 | 7818 | Mark_Non_Null; |
7819 | return; | |
7820 | end if; | |
7821 | ||
7822 | -- If entity is never assigned, for sure a warning is appropriate | |
7823 | ||
7824 | if Is_Entity_Name (N) then | |
7825 | Check_Unset_Reference (N); | |
fa7497e8 | 7826 | end if; |
84d0d4a5 | 7827 | |
7828 | -- No check needed if checks are suppressed on the range. Note that we | |
7829 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
7830 | -- so, since the program is erroneous, but we don't like to casually | |
7831 | -- propagate such conclusions from erroneosity). | |
7832 | ||
7833 | if Access_Checks_Suppressed (Typ) then | |
7834 | return; | |
7835 | end if; | |
7836 | ||
2af58f67 | 7837 | -- No check needed for access to concurrent record types generated by |
7838 | -- the expander. This is not just an optimization (though it does indeed | |
7839 | -- remove junk checks). It also avoids generation of junk warnings. | |
7840 | ||
7841 | if Nkind (N) in N_Has_Chars | |
7842 | and then Chars (N) = Name_uObject | |
7843 | and then Is_Concurrent_Record_Type | |
7844 | (Directly_Designated_Type (Etype (N))) | |
7845 | then | |
7846 | return; | |
7847 | end if; | |
7848 | ||
228836e8 | 7849 | -- No check needed in interface thunks since the runtime check is |
7850 | -- already performed at the caller side. | |
7851 | ||
7852 | if Is_Thunk (Current_Scope) then | |
7853 | return; | |
7854 | end if; | |
7855 | ||
472ea160 | 7856 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
7857 | -- the expander within exception handlers, since we know that the value | |
7858 | -- can never be null. | |
7859 | ||
7860 | -- Is this really the right way to do this? Normally we generate such | |
7861 | -- code in the expander with checks off, and that's how we suppress this | |
7862 | -- kind of junk check ??? | |
7863 | ||
7864 | if Nkind (N) = N_Function_Call | |
7865 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
7866 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
7867 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
7868 | then | |
7869 | return; | |
7870 | end if; | |
7871 | ||
84d0d4a5 | 7872 | -- Otherwise install access check |
7873 | ||
7874 | Insert_Action (N, | |
7875 | Make_Raise_Constraint_Error (Loc, | |
7876 | Condition => | |
7877 | Make_Op_Eq (Loc, | |
7878 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
7879 | Right_Opnd => Make_Null (Loc)), | |
7880 | Reason => CE_Access_Check_Failed)); | |
7881 | ||
7882 | Mark_Non_Null; | |
fa7497e8 | 7883 | end Install_Null_Excluding_Check; |
7884 | ||
7e933b61 | 7885 | ----------------------------------------- |
7886 | -- Install_Primitive_Elaboration_Check -- | |
7887 | ----------------------------------------- | |
7888 | ||
7889 | procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id) is | |
7e933b61 | 7890 | function Within_Compilation_Unit_Instance |
7891 | (Subp_Id : Entity_Id) return Boolean; | |
7892 | -- Determine whether subprogram Subp_Id appears within an instance which | |
7893 | -- acts as a compilation unit. | |
7894 | ||
7895 | -------------------------------------- | |
7896 | -- Within_Compilation_Unit_Instance -- | |
7897 | -------------------------------------- | |
7898 | ||
7899 | function Within_Compilation_Unit_Instance | |
7900 | (Subp_Id : Entity_Id) return Boolean | |
7901 | is | |
7902 | Pack : Entity_Id; | |
7903 | ||
7904 | begin | |
7905 | -- Examine the scope chain looking for a compilation-unit-level | |
7906 | -- instance. | |
7907 | ||
7908 | Pack := Scope (Subp_Id); | |
7909 | while Present (Pack) and then Pack /= Standard_Standard loop | |
7910 | if Ekind (Pack) = E_Package | |
7911 | and then Is_Generic_Instance (Pack) | |
7912 | and then Nkind (Parent (Unit_Declaration_Node (Pack))) = | |
7913 | N_Compilation_Unit | |
7914 | then | |
7915 | return True; | |
7916 | end if; | |
7917 | ||
7918 | Pack := Scope (Pack); | |
7919 | end loop; | |
7920 | ||
7921 | return False; | |
7922 | end Within_Compilation_Unit_Instance; | |
7923 | ||
7924 | -- Local declarations | |
7925 | ||
7926 | Context : constant Node_Id := Parent (Subp_Body); | |
7927 | Loc : constant Source_Ptr := Sloc (Subp_Body); | |
7928 | Subp_Id : constant Entity_Id := Unique_Defining_Entity (Subp_Body); | |
7929 | Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id); | |
7930 | ||
e2293a63 | 7931 | Decls : List_Id; |
7932 | Flag_Id : Entity_Id; | |
7933 | Set_Ins : Node_Id; | |
7934 | Set_Stmt : Node_Id; | |
7935 | Tag_Typ : Entity_Id; | |
7e933b61 | 7936 | |
7937 | -- Start of processing for Install_Primitive_Elaboration_Check | |
7938 | ||
7939 | begin | |
7940 | -- Do not generate an elaboration check in compilation modes where | |
7941 | -- expansion is not desirable. | |
7942 | ||
7943 | if ASIS_Mode or GNATprove_Mode then | |
7944 | return; | |
7945 | ||
f0e731f2 | 7946 | -- Do not generate an elaboration check if all checks have been |
7947 | -- suppressed. | |
62c62e4b | 7948 | |
f0e731f2 | 7949 | elsif Suppress_Checks then |
62c62e4b | 7950 | return; |
7951 | ||
7e933b61 | 7952 | -- Do not generate an elaboration check if the related subprogram is |
7953 | -- not subjected to accessibility checks. | |
7954 | ||
7955 | elsif Elaboration_Checks_Suppressed (Subp_Id) then | |
7956 | return; | |
f0e731f2 | 7957 | |
7958 | -- Do not generate an elaboration check if such code is not desirable | |
7959 | ||
7960 | elsif Restriction_Active (No_Elaboration_Code) then | |
7961 | return; | |
7e933b61 | 7962 | |
77b577f9 | 7963 | -- Do not generate an elaboration check if exceptions cannot be used, |
7964 | -- caught, or propagated. | |
7965 | ||
7966 | elsif not Exceptions_OK then | |
7967 | return; | |
7968 | ||
7e933b61 | 7969 | -- Do not consider subprograms which act as compilation units, because |
7970 | -- they cannot be the target of a dispatching call. | |
7971 | ||
7972 | elsif Nkind (Context) = N_Compilation_Unit then | |
7973 | return; | |
7974 | ||
e2293a63 | 7975 | -- Do not consider anything other than nonabstract library-level source |
7976 | -- primitives. | |
7e933b61 | 7977 | |
7978 | elsif not | |
7979 | (Comes_From_Source (Subp_Id) | |
7980 | and then Is_Library_Level_Entity (Subp_Id) | |
7981 | and then Is_Primitive (Subp_Id) | |
7982 | and then not Is_Abstract_Subprogram (Subp_Id)) | |
7983 | then | |
7984 | return; | |
7985 | ||
7986 | -- Do not consider inlined primitives, because once the body is inlined | |
7987 | -- the reference to the elaboration flag will be out of place and will | |
7988 | -- result in an undefined symbol. | |
7989 | ||
7990 | elsif Is_Inlined (Subp_Id) or else Has_Pragma_Inline (Subp_Id) then | |
7991 | return; | |
7992 | ||
7993 | -- Do not generate a duplicate elaboration check. This happens only in | |
7994 | -- the case of primitives completed by an expression function, as the | |
7995 | -- corresponding body is apparently analyzed and expanded twice. | |
7996 | ||
7997 | elsif Analyzed (Subp_Body) then | |
7998 | return; | |
7999 | ||
8000 | -- Do not consider primitives which occur within an instance that acts | |
8001 | -- as a compilation unit. Such an instance defines its spec and body out | |
8002 | -- of order (body is first) within the tree, which causes the reference | |
8003 | -- to the elaboration flag to appear as an undefined symbol. | |
8004 | ||
8005 | elsif Within_Compilation_Unit_Instance (Subp_Id) then | |
8006 | return; | |
8007 | end if; | |
8008 | ||
8009 | Tag_Typ := Find_Dispatching_Type (Subp_Id); | |
8010 | ||
8011 | -- Only tagged primitives may be the target of a dispatching call | |
8012 | ||
8013 | if No (Tag_Typ) then | |
8014 | return; | |
8015 | ||
8016 | -- Do not consider finalization-related primitives, because they may | |
8017 | -- need to be called while elaboration is taking place. | |
8018 | ||
8019 | elsif Is_Controlled (Tag_Typ) | |
8020 | and then Nam_In (Chars (Subp_Id), Name_Adjust, | |
8021 | Name_Finalize, | |
8022 | Name_Initialize) | |
8023 | then | |
8024 | return; | |
8025 | end if; | |
8026 | ||
8027 | -- Create the declaration of the elaboration flag. The name carries a | |
8028 | -- unique counter in case of name overloading. | |
8029 | ||
8030 | Flag_Id := | |
8031 | Make_Defining_Identifier (Loc, | |
fe48a434 | 8032 | Chars => New_External_Name (Chars (Subp_Id), 'E', -1)); |
7e933b61 | 8033 | Set_Is_Frozen (Flag_Id); |
8034 | ||
8035 | -- Insert the declaration of the elaboration flag in front of the | |
8036 | -- primitive spec and analyze it in the proper context. | |
8037 | ||
8038 | Push_Scope (Scope (Subp_Id)); | |
8039 | ||
8040 | -- Generate: | |
fe48a434 | 8041 | -- E : Boolean := False; |
7e933b61 | 8042 | |
8043 | Insert_Action (Subp_Decl, | |
8044 | Make_Object_Declaration (Loc, | |
8045 | Defining_Identifier => Flag_Id, | |
8046 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), | |
8047 | Expression => New_Occurrence_Of (Standard_False, Loc))); | |
8048 | Pop_Scope; | |
8049 | ||
8050 | -- Prevent the compiler from optimizing the elaboration check by killing | |
8051 | -- the current value of the flag and the associated assignment. | |
8052 | ||
8053 | Set_Current_Value (Flag_Id, Empty); | |
8054 | Set_Last_Assignment (Flag_Id, Empty); | |
8055 | ||
8056 | -- Add a check at the top of the body declarations to ensure that the | |
8057 | -- elaboration flag has been set. | |
8058 | ||
8059 | Decls := Declarations (Subp_Body); | |
8060 | ||
8061 | if No (Decls) then | |
8062 | Decls := New_List; | |
8063 | Set_Declarations (Subp_Body, Decls); | |
8064 | end if; | |
8065 | ||
8066 | -- Generate: | |
8067 | -- if not F then | |
8068 | -- raise Program_Error with "access before elaboration"; | |
8069 | -- end if; | |
8070 | ||
8071 | Prepend_To (Decls, | |
8072 | Make_Raise_Program_Error (Loc, | |
8073 | Condition => | |
8074 | Make_Op_Not (Loc, | |
8075 | Right_Opnd => New_Occurrence_Of (Flag_Id, Loc)), | |
8076 | Reason => PE_Access_Before_Elaboration)); | |
8077 | ||
8078 | Analyze (First (Decls)); | |
8079 | ||
8080 | -- Set the elaboration flag once the body has been elaborated. Insert | |
8081 | -- the statement after the subprogram stub when the primitive body is | |
8082 | -- a subunit. | |
8083 | ||
8084 | if Nkind (Context) = N_Subunit then | |
8085 | Set_Ins := Corresponding_Stub (Context); | |
8086 | else | |
8087 | Set_Ins := Subp_Body; | |
8088 | end if; | |
8089 | ||
8090 | -- Generate: | |
fe48a434 | 8091 | -- E := True; |
7e933b61 | 8092 | |
e2293a63 | 8093 | Set_Stmt := |
7e933b61 | 8094 | Make_Assignment_Statement (Loc, |
8095 | Name => New_Occurrence_Of (Flag_Id, Loc), | |
e2293a63 | 8096 | Expression => New_Occurrence_Of (Standard_True, Loc)); |
8097 | ||
8098 | -- Mark the assignment statement as elaboration code. This allows the | |
8099 | -- early call region mechanism (see Sem_Elab) to properly ignore such | |
8100 | -- assignments even though they are non-preelaborable code. | |
8101 | ||
8102 | Set_Is_Elaboration_Code (Set_Stmt); | |
8103 | ||
8104 | Insert_After_And_Analyze (Set_Ins, Set_Stmt); | |
7e933b61 | 8105 | end Install_Primitive_Elaboration_Check; |
8106 | ||
ee6ba406 | 8107 | -------------------------- |
8108 | -- Install_Static_Check -- | |
8109 | -------------------------- | |
8110 | ||
8111 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
cda40848 | 8112 | Stat : constant Boolean := Is_OK_Static_Expression (R_Cno); |
ee6ba406 | 8113 | Typ : constant Entity_Id := Etype (R_Cno); |
8114 | ||
8115 | begin | |
f15731c4 | 8116 | Rewrite (R_Cno, |
8117 | Make_Raise_Constraint_Error (Loc, | |
8118 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 8119 | Set_Analyzed (R_Cno); |
8120 | Set_Etype (R_Cno, Typ); | |
8121 | Set_Raises_Constraint_Error (R_Cno); | |
8122 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 8123 | |
8124 | -- Now deal with possible local raise handling | |
8125 | ||
8126 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 8127 | end Install_Static_Check; |
8128 | ||
3cce7f32 | 8129 | ------------------------- |
8130 | -- Is_Check_Suppressed -- | |
8131 | ------------------------- | |
8132 | ||
8133 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
8134 | Ptr : Suppress_Stack_Entry_Ptr; | |
8135 | ||
8136 | begin | |
8137 | -- First search the local entity suppress stack. We search this from the | |
8138 | -- top of the stack down so that we get the innermost entry that applies | |
8139 | -- to this case if there are nested entries. | |
8140 | ||
8141 | Ptr := Local_Suppress_Stack_Top; | |
8142 | while Ptr /= null loop | |
8143 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8144 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8145 | then | |
8146 | return Ptr.Suppress; | |
8147 | end if; | |
8148 | ||
8149 | Ptr := Ptr.Prev; | |
8150 | end loop; | |
8151 | ||
8152 | -- Now search the global entity suppress table for a matching entry. | |
8153 | -- We also search this from the top down so that if there are multiple | |
8154 | -- pragmas for the same entity, the last one applies (not clear what | |
8155 | -- or whether the RM specifies this handling, but it seems reasonable). | |
8156 | ||
8157 | Ptr := Global_Suppress_Stack_Top; | |
8158 | while Ptr /= null loop | |
8159 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8160 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8161 | then | |
8162 | return Ptr.Suppress; | |
8163 | end if; | |
8164 | ||
8165 | Ptr := Ptr.Prev; | |
8166 | end loop; | |
8167 | ||
8168 | -- If we did not find a matching entry, then use the normal scope | |
8169 | -- suppress value after all (actually this will be the global setting | |
8170 | -- since it clearly was not overridden at any point). For a predefined | |
8171 | -- check, we test the specific flag. For a user defined check, we check | |
8172 | -- the All_Checks flag. The Overflow flag requires special handling to | |
fe48a434 | 8173 | -- deal with the General vs Assertion case. |
3cce7f32 | 8174 | |
8175 | if C = Overflow_Check then | |
8176 | return Overflow_Checks_Suppressed (Empty); | |
fe48a434 | 8177 | |
3cce7f32 | 8178 | elsif C in Predefined_Check_Id then |
8179 | return Scope_Suppress.Suppress (C); | |
fe48a434 | 8180 | |
3cce7f32 | 8181 | else |
8182 | return Scope_Suppress.Suppress (All_Checks); | |
8183 | end if; | |
8184 | end Is_Check_Suppressed; | |
8185 | ||
9dfe12ae | 8186 | --------------------- |
8187 | -- Kill_All_Checks -- | |
8188 | --------------------- | |
8189 | ||
8190 | procedure Kill_All_Checks is | |
8191 | begin | |
8192 | if Debug_Flag_CC then | |
8193 | w ("Kill_All_Checks"); | |
8194 | end if; | |
8195 | ||
feff2f05 | 8196 | -- We reset the number of saved checks to zero, and also modify all |
8197 | -- stack entries for statement ranges to indicate that the number of | |
8198 | -- checks at each level is now zero. | |
9dfe12ae | 8199 | |
8200 | Num_Saved_Checks := 0; | |
8201 | ||
96da3284 | 8202 | -- Note: the Int'Min here avoids any possibility of J being out of |
8203 | -- range when called from e.g. Conditional_Statements_Begin. | |
8204 | ||
8205 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 8206 | Saved_Checks_Stack (J) := 0; |
8207 | end loop; | |
8208 | end Kill_All_Checks; | |
8209 | ||
8210 | ----------------- | |
8211 | -- Kill_Checks -- | |
8212 | ----------------- | |
8213 | ||
8214 | procedure Kill_Checks (V : Entity_Id) is | |
8215 | begin | |
8216 | if Debug_Flag_CC then | |
8217 | w ("Kill_Checks for entity", Int (V)); | |
8218 | end if; | |
8219 | ||
8220 | for J in 1 .. Num_Saved_Checks loop | |
8221 | if Saved_Checks (J).Entity = V then | |
8222 | if Debug_Flag_CC then | |
8223 | w (" Checks killed for saved check ", J); | |
8224 | end if; | |
8225 | ||
8226 | Saved_Checks (J).Killed := True; | |
8227 | end if; | |
8228 | end loop; | |
8229 | end Kill_Checks; | |
8230 | ||
ee6ba406 | 8231 | ------------------------------ |
8232 | -- Length_Checks_Suppressed -- | |
8233 | ------------------------------ | |
8234 | ||
8235 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8236 | begin | |
9dfe12ae | 8237 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
8238 | return Is_Check_Suppressed (E, Length_Check); | |
8239 | else | |
fafc6b97 | 8240 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 8241 | end if; |
ee6ba406 | 8242 | end Length_Checks_Suppressed; |
8243 | ||
3cce7f32 | 8244 | ----------------------- |
8245 | -- Make_Bignum_Block -- | |
8246 | ----------------------- | |
8247 | ||
8248 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
8249 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
3cce7f32 | 8250 | begin |
8251 | return | |
8252 | Make_Block_Statement (Loc, | |
97c15ab0 | 8253 | Declarations => |
8254 | New_List (Build_SS_Mark_Call (Loc, M)), | |
3cce7f32 | 8255 | Handled_Statement_Sequence => |
8256 | Make_Handled_Sequence_Of_Statements (Loc, | |
97c15ab0 | 8257 | Statements => New_List (Build_SS_Release_Call (Loc, M)))); |
3cce7f32 | 8258 | end Make_Bignum_Block; |
8259 | ||
0df9d43f | 8260 | ---------------------------------- |
8261 | -- Minimize_Eliminate_Overflows -- | |
8262 | ---------------------------------- | |
3cce7f32 | 8263 | |
f32c377d | 8264 | -- This is a recursive routine that is called at the top of an expression |
8265 | -- tree to properly process overflow checking for a whole subtree by making | |
8266 | -- recursive calls to process operands. This processing may involve the use | |
8267 | -- of bignum or long long integer arithmetic, which will change the types | |
8268 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 8269 | -- it would interfere with semantic analysis). |
f32c377d | 8270 | |
21a55437 | 8271 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 8272 | -- the operator expansion routines, as well as the expansion routines for |
8273 | -- if/case expression, do nothing (for the moment) except call the routine | |
8274 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
8275 | -- routine does nothing for non top-level nodes, so at the point where the | |
8276 | -- call is made for the top level node, the entire expression subtree has | |
8277 | -- not been expanded, or processed for overflow. All that has to happen as | |
8278 | -- a result of the top level call to this routine. | |
f32c377d | 8279 | |
8280 | -- As noted above, the overflow processing works by making recursive calls | |
8281 | -- for the operands, and figuring out what to do, based on the processing | |
8282 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
8283 | -- to be done in bignum mode), and the determined ranges of the operands. | |
8284 | ||
8285 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 8286 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 8287 | -- the node (if it has been modified by the overflow check processing). The |
8288 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
8289 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 8290 | -- for this call is that the overflow handling mode must be temporarily set |
8291 | -- to STRICT. | |
f32c377d | 8292 | |
0df9d43f | 8293 | procedure Minimize_Eliminate_Overflows |
61016a7a | 8294 | (N : Node_Id; |
8295 | Lo : out Uint; | |
8296 | Hi : out Uint; | |
8297 | Top_Level : Boolean) | |
3cce7f32 | 8298 | is |
0326b4d4 | 8299 | Rtyp : constant Entity_Id := Etype (N); |
8300 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
8301 | -- Result type, must be a signed integer type | |
3cce7f32 | 8302 | |
db415383 | 8303 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 8304 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
8305 | ||
8306 | Loc : constant Source_Ptr := Sloc (N); | |
8307 | ||
8308 | Rlo, Rhi : Uint; | |
0326b4d4 | 8309 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 8310 | |
16149377 | 8311 | Llo : Uint := No_Uint; -- initialize to prevent warning |
8312 | Lhi : Uint := No_Uint; -- initialize to prevent warning | |
0326b4d4 | 8313 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 8314 | |
49b3a812 | 8315 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
8316 | -- Operands and results are of this type when we convert | |
8317 | ||
0326b4d4 | 8318 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
8319 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 8320 | -- Bounds of Long_Long_Integer |
8321 | ||
8322 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
8323 | -- Indicates binary operator case | |
8324 | ||
8325 | OK : Boolean; | |
8326 | -- Used in call to Determine_Range | |
8327 | ||
61016a7a | 8328 | Bignum_Operands : Boolean; |
8329 | -- Set True if one or more operands is already of type Bignum, meaning | |
8330 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 8331 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 8332 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 8333 | |
8334 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 8335 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 8336 | -- which means that if the result is known to be in the result type |
8337 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 8338 | |
8339 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
8340 | -- This is called when we have modified the node and we therefore need | |
8341 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
8342 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
39a0c1d3 | 8343 | -- we would reenter this routine recursively which would not be good. |
0df9d43f | 8344 | -- The argument Suppress is set True if we also want to suppress |
8345 | -- overflow checking for the reexpansion (this is set when we know | |
8346 | -- overflow is not possible). Typ is the type for the reanalysis. | |
8347 | ||
8348 | procedure Reexpand (Suppress : Boolean := False); | |
8349 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
8350 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
8351 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
8352 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
8353 | -- Note that skipping reanalysis is not just an optimization, testing | |
8354 | -- has showed up several complex cases in which reanalyzing an already | |
8355 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 8356 | |
0326b4d4 | 8357 | function In_Result_Range return Boolean; |
8358 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 8359 | |
2fe22c69 | 8360 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 8361 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 8362 | |
8363 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 8364 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 8365 | |
0326b4d4 | 8366 | --------------------- |
8367 | -- In_Result_Range -- | |
8368 | --------------------- | |
8369 | ||
8370 | function In_Result_Range return Boolean is | |
8371 | begin | |
f32c377d | 8372 | if Lo = No_Uint or else Hi = No_Uint then |
8373 | return False; | |
8374 | ||
cda40848 | 8375 | elsif Is_OK_Static_Subtype (Etype (N)) then |
0326b4d4 | 8376 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
8377 | and then | |
8378 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 8379 | |
0326b4d4 | 8380 | else |
8381 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
8382 | and then | |
8383 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
8384 | end if; | |
8385 | end In_Result_Range; | |
8386 | ||
2fe22c69 | 8387 | --------- |
8388 | -- Max -- | |
8389 | --------- | |
8390 | ||
8391 | procedure Max (A : in out Uint; B : Uint) is | |
8392 | begin | |
8393 | if A = No_Uint or else B > A then | |
8394 | A := B; | |
8395 | end if; | |
8396 | end Max; | |
8397 | ||
8398 | --------- | |
8399 | -- Min -- | |
8400 | --------- | |
8401 | ||
8402 | procedure Min (A : in out Uint; B : Uint) is | |
8403 | begin | |
8404 | if A = No_Uint or else B < A then | |
8405 | A := B; | |
8406 | end if; | |
8407 | end Min; | |
8408 | ||
0df9d43f | 8409 | --------------- |
8410 | -- Reanalyze -- | |
8411 | --------------- | |
8412 | ||
8413 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 8414 | Svg : constant Overflow_Mode_Type := |
8415 | Scope_Suppress.Overflow_Mode_General; | |
8416 | Sva : constant Overflow_Mode_Type := | |
8417 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 8418 | Svo : constant Boolean := |
8419 | Scope_Suppress.Suppress (Overflow_Check); | |
8420 | ||
8421 | begin | |
db415383 | 8422 | Scope_Suppress.Overflow_Mode_General := Strict; |
8423 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 8424 | |
8425 | if Suppress then | |
8426 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
8427 | end if; | |
8428 | ||
8429 | Analyze_And_Resolve (N, Typ); | |
8430 | ||
2b108e18 | 8431 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
8432 | Scope_Suppress.Overflow_Mode_General := Svg; | |
8433 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 8434 | end Reanalyze; |
8435 | ||
4fb5f0a0 | 8436 | -------------- |
8437 | -- Reexpand -- | |
8438 | -------------- | |
8439 | ||
0df9d43f | 8440 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 8441 | Svg : constant Overflow_Mode_Type := |
8442 | Scope_Suppress.Overflow_Mode_General; | |
8443 | Sva : constant Overflow_Mode_Type := | |
8444 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 8445 | Svo : constant Boolean := |
8446 | Scope_Suppress.Suppress (Overflow_Check); | |
8447 | ||
4fb5f0a0 | 8448 | begin |
db415383 | 8449 | Scope_Suppress.Overflow_Mode_General := Strict; |
8450 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 8451 | Set_Analyzed (N, False); |
0df9d43f | 8452 | |
8453 | if Suppress then | |
8454 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
8455 | end if; | |
8456 | ||
4fb5f0a0 | 8457 | Expand (N); |
0df9d43f | 8458 | |
2b108e18 | 8459 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
8460 | Scope_Suppress.Overflow_Mode_General := Svg; | |
8461 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 8462 | end Reexpand; |
8463 | ||
0df9d43f | 8464 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 8465 | |
3cce7f32 | 8466 | begin |
02038e4e | 8467 | -- Default initialize Lo and Hi since these are not guaranteed to be |
8468 | -- set otherwise. | |
8469 | ||
8470 | Lo := No_Uint; | |
8471 | Hi := No_Uint; | |
8472 | ||
0326b4d4 | 8473 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 8474 | |
8475 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
8476 | ||
8477 | -- Use the normal Determine_Range routine to get the range. We | |
8478 | -- don't require operands to be valid, invalid values may result in | |
8479 | -- rubbish results where the result has not been properly checked for | |
39a0c1d3 | 8480 | -- overflow, that's fine. |
3cce7f32 | 8481 | |
8482 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
8483 | ||
21a55437 | 8484 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 8485 | -- clear but might as well protect), use type bounds. |
8486 | ||
8487 | if not OK then | |
8488 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
8489 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
8490 | end if; | |
8491 | ||
8492 | -- If we don't have a binary operator, all we have to do is to set | |
20cf157b | 8493 | -- the Hi/Lo range, so we are done. |
3cce7f32 | 8494 | |
8495 | return; | |
8496 | ||
0326b4d4 | 8497 | -- Processing for if expression |
8498 | ||
92f1631f | 8499 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 8500 | declare |
8501 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
8502 | Else_DE : constant Node_Id := Next (Then_DE); | |
8503 | ||
8504 | begin | |
8505 | Bignum_Operands := False; | |
8506 | ||
0df9d43f | 8507 | Minimize_Eliminate_Overflows |
0326b4d4 | 8508 | (Then_DE, Lo, Hi, Top_Level => False); |
8509 | ||
8510 | if Lo = No_Uint then | |
8511 | Bignum_Operands := True; | |
8512 | end if; | |
8513 | ||
0df9d43f | 8514 | Minimize_Eliminate_Overflows |
0326b4d4 | 8515 | (Else_DE, Rlo, Rhi, Top_Level => False); |
8516 | ||
8517 | if Rlo = No_Uint then | |
8518 | Bignum_Operands := True; | |
8519 | else | |
8520 | Long_Long_Integer_Operands := | |
8521 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
8522 | ||
8523 | Min (Lo, Rlo); | |
8524 | Max (Hi, Rhi); | |
8525 | end if; | |
8526 | ||
21a55437 | 8527 | -- If at least one of our operands is now Bignum, we must rebuild |
8528 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 8529 | -- rebuilt if expression with overflow checks off, since once we |
39a0c1d3 | 8530 | -- are in bignum mode, we are all done with overflow checks. |
0326b4d4 | 8531 | |
8532 | if Bignum_Operands then | |
8533 | Rewrite (N, | |
92f1631f | 8534 | Make_If_Expression (Loc, |
0326b4d4 | 8535 | Expressions => New_List ( |
8536 | Remove_Head (Expressions (N)), | |
8537 | Convert_To_Bignum (Then_DE), | |
8538 | Convert_To_Bignum (Else_DE)), | |
8539 | Is_Elsif => Is_Elsif (N))); | |
8540 | ||
0df9d43f | 8541 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 8542 | |
8543 | -- If we have no Long_Long_Integer operands, then we are in result | |
8544 | -- range, since it means that none of our operands felt the need | |
8545 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 8546 | -- converted to long long integer or bignum). We reexpand to |
8547 | -- complete the expansion of the if expression (but we do not | |
8548 | -- need to reanalyze). | |
0326b4d4 | 8549 | |
8550 | elsif not Long_Long_Integer_Operands then | |
8551 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 8552 | Reexpand; |
0326b4d4 | 8553 | |
8554 | -- Otherwise convert us to long long integer mode. Note that we | |
8555 | -- don't need any further overflow checking at this level. | |
8556 | ||
8557 | else | |
8558 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
8559 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
8560 | Set_Etype (N, LLIB); | |
f32c377d | 8561 | |
8562 | -- Now reanalyze with overflow checks off | |
8563 | ||
0326b4d4 | 8564 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 8565 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 8566 | end if; |
8567 | end; | |
8568 | ||
8569 | return; | |
8570 | ||
8571 | -- Here for case expression | |
8572 | ||
8573 | elsif Nkind (N) = N_Case_Expression then | |
8574 | Bignum_Operands := False; | |
8575 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 8576 | |
8577 | declare | |
f32c377d | 8578 | Alt : Node_Id; |
0326b4d4 | 8579 | |
8580 | begin | |
8581 | -- Loop through expressions applying recursive call | |
8582 | ||
8583 | Alt := First (Alternatives (N)); | |
8584 | while Present (Alt) loop | |
8585 | declare | |
8586 | Aexp : constant Node_Id := Expression (Alt); | |
8587 | ||
8588 | begin | |
0df9d43f | 8589 | Minimize_Eliminate_Overflows |
0326b4d4 | 8590 | (Aexp, Lo, Hi, Top_Level => False); |
8591 | ||
8592 | if Lo = No_Uint then | |
8593 | Bignum_Operands := True; | |
8594 | elsif Etype (Aexp) = LLIB then | |
8595 | Long_Long_Integer_Operands := True; | |
8596 | end if; | |
8597 | end; | |
8598 | ||
8599 | Next (Alt); | |
8600 | end loop; | |
8601 | ||
8602 | -- If we have no bignum or long long integer operands, it means | |
8603 | -- that none of our dependent expressions could raise overflow. | |
8604 | -- In this case, we simply return with no changes except for | |
8605 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 8606 | -- checks for this node. We will reexpand to get the needed |
8607 | -- expansion for the case expression, but we do not need to | |
21a55437 | 8608 | -- reanalyze, since nothing has changed. |
0326b4d4 | 8609 | |
f32c377d | 8610 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 8611 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 8612 | Reexpand (Suppress => True); |
0326b4d4 | 8613 | |
8614 | -- Otherwise we are going to rebuild the case expression using | |
8615 | -- either bignum or long long integer operands throughout. | |
8616 | ||
8617 | else | |
f32c377d | 8618 | declare |
8619 | Rtype : Entity_Id; | |
16149377 | 8620 | pragma Warnings (Off, Rtype); |
f32c377d | 8621 | New_Alts : List_Id; |
8622 | New_Exp : Node_Id; | |
8623 | ||
8624 | begin | |
8625 | New_Alts := New_List; | |
8626 | Alt := First (Alternatives (N)); | |
8627 | while Present (Alt) loop | |
8628 | if Bignum_Operands then | |
8629 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
8630 | Rtype := RTE (RE_Bignum); | |
8631 | else | |
8632 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
8633 | Rtype := LLIB; | |
8634 | end if; | |
0326b4d4 | 8635 | |
f32c377d | 8636 | Append_To (New_Alts, |
8637 | Make_Case_Expression_Alternative (Sloc (Alt), | |
8638 | Actions => No_List, | |
8639 | Discrete_Choices => Discrete_Choices (Alt), | |
8640 | Expression => New_Exp)); | |
0326b4d4 | 8641 | |
f32c377d | 8642 | Next (Alt); |
8643 | end loop; | |
0326b4d4 | 8644 | |
f32c377d | 8645 | Rewrite (N, |
8646 | Make_Case_Expression (Loc, | |
8647 | Expression => Expression (N), | |
8648 | Alternatives => New_Alts)); | |
0326b4d4 | 8649 | |
0df9d43f | 8650 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 8651 | end; |
0326b4d4 | 8652 | end if; |
8653 | end; | |
8654 | ||
8655 | return; | |
8656 | end if; | |
8657 | ||
8658 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 8659 | -- operands to get the ranges (and to properly process the subtree |
20cf157b | 8660 | -- that lies below us). |
3cce7f32 | 8661 | |
0df9d43f | 8662 | Minimize_Eliminate_Overflows |
0326b4d4 | 8663 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 8664 | |
0326b4d4 | 8665 | if Binary then |
0df9d43f | 8666 | Minimize_Eliminate_Overflows |
0326b4d4 | 8667 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 8668 | end if; |
8669 | ||
f32c377d | 8670 | -- Record if we have Long_Long_Integer operands |
8671 | ||
8672 | Long_Long_Integer_Operands := | |
8673 | Etype (Right_Opnd (N)) = LLIB | |
8674 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
8675 | ||
8676 | -- If either operand is a bignum, then result will be a bignum and we | |
8677 | -- don't need to do any range analysis. As previously discussed we could | |
8678 | -- do range analysis in such cases, but it could mean working with giant | |
8679 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 8680 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 8681 | |
8682 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
8683 | Lo := No_Uint; | |
8684 | Hi := No_Uint; | |
61016a7a | 8685 | Bignum_Operands := True; |
3cce7f32 | 8686 | |
8687 | -- Otherwise compute result range | |
8688 | ||
8689 | else | |
61016a7a | 8690 | Bignum_Operands := False; |
8691 | ||
3cce7f32 | 8692 | case Nkind (N) is |
8693 | ||
8694 | -- Absolute value | |
8695 | ||
8696 | when N_Op_Abs => | |
8697 | Lo := Uint_0; | |
de922300 | 8698 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 8699 | |
8700 | -- Addition | |
8701 | ||
8702 | when N_Op_Add => | |
8703 | Lo := Llo + Rlo; | |
8704 | Hi := Lhi + Rhi; | |
8705 | ||
8706 | -- Division | |
8707 | ||
8708 | when N_Op_Divide => | |
2fe22c69 | 8709 | |
5f4275e1 | 8710 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 8711 | |
5f4275e1 | 8712 | if Rlo = 0 and then Rhi = 0 then |
8713 | Lo := Uint_0; | |
8714 | Hi := Uint_0; | |
2fe22c69 | 8715 | |
5f4275e1 | 8716 | -- Possible bounds of division must come from dividing end |
8717 | -- values of the input ranges (four possibilities), provided | |
8718 | -- zero is not included in the possible values of the right | |
8719 | -- operand. | |
8720 | ||
8721 | -- Otherwise, we just consider two intervals of values for | |
8722 | -- the right operand: the interval of negative values (up to | |
8723 | -- -1) and the interval of positive values (starting at 1). | |
8724 | -- Since division by 1 is the identity, and division by -1 | |
8725 | -- is negation, we get all possible bounds of division in that | |
8726 | -- case by considering: | |
8727 | -- - all values from the division of end values of input | |
8728 | -- ranges; | |
8729 | -- - the end values of the left operand; | |
8730 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 8731 | |
5f4275e1 | 8732 | else |
8733 | declare | |
8734 | Mrk : constant Uintp.Save_Mark := Mark; | |
8735 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 8736 | |
5f4275e1 | 8737 | Ev1 : Uint; |
8738 | Ev2 : Uint; | |
8739 | Ev3 : Uint; | |
8740 | Ev4 : Uint; | |
2fe22c69 | 8741 | |
5f4275e1 | 8742 | begin |
8743 | -- Discard extreme values of zero for the divisor, since | |
8744 | -- they will simply result in an exception in any case. | |
2fe22c69 | 8745 | |
5f4275e1 | 8746 | if Rlo = 0 then |
8747 | Rlo := Uint_1; | |
8748 | elsif Rhi = 0 then | |
8749 | Rhi := -Uint_1; | |
2fe22c69 | 8750 | end if; |
2fe22c69 | 8751 | |
5f4275e1 | 8752 | -- Compute possible bounds coming from dividing end |
8753 | -- values of the input ranges. | |
2fe22c69 | 8754 | |
5f4275e1 | 8755 | Ev1 := Llo / Rlo; |
8756 | Ev2 := Llo / Rhi; | |
8757 | Ev3 := Lhi / Rlo; | |
8758 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 8759 | |
5f4275e1 | 8760 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
8761 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 8762 | |
5f4275e1 | 8763 | -- If the right operand can be both negative or positive, |
8764 | -- include the end values of the left operand in the | |
8765 | -- extreme values, as well as their negation. | |
2fe22c69 | 8766 | |
5f4275e1 | 8767 | if Rlo < 0 and then Rhi > 0 then |
8768 | Ev1 := Llo; | |
8769 | Ev2 := -Llo; | |
8770 | Ev3 := Lhi; | |
8771 | Ev4 := -Lhi; | |
2fe22c69 | 8772 | |
5f4275e1 | 8773 | Min (Lo, |
8774 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
8775 | Max (Hi, | |
8776 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 8777 | end if; |
2fe22c69 | 8778 | |
5f4275e1 | 8779 | -- Release the RR and Ev values |
2fe22c69 | 8780 | |
5f4275e1 | 8781 | Release_And_Save (Mrk, Lo, Hi); |
8782 | end; | |
8783 | end if; | |
3cce7f32 | 8784 | |
8785 | -- Exponentiation | |
8786 | ||
8787 | when N_Op_Expon => | |
de922300 | 8788 | |
8789 | -- Discard negative values for the exponent, since they will | |
8790 | -- simply result in an exception in any case. | |
8791 | ||
8792 | if Rhi < 0 then | |
8793 | Rhi := Uint_0; | |
8794 | elsif Rlo < 0 then | |
8795 | Rlo := Uint_0; | |
8796 | end if; | |
8797 | ||
8798 | -- Estimate number of bits in result before we go computing | |
8799 | -- giant useless bounds. Basically the number of bits in the | |
8800 | -- result is the number of bits in the base multiplied by the | |
8801 | -- value of the exponent. If this is big enough that the result | |
8802 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
8803 | -- mode immediately, and avoid computing giant bounds. | |
8804 | ||
8805 | -- The comparison here is approximate, but conservative, it | |
8806 | -- only clicks on cases that are sure to exceed the bounds. | |
8807 | ||
8808 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
8809 | Lo := No_Uint; | |
8810 | Hi := No_Uint; | |
8811 | ||
8812 | -- If right operand is zero then result is 1 | |
8813 | ||
8814 | elsif Rhi = 0 then | |
8815 | Lo := Uint_1; | |
8816 | Hi := Uint_1; | |
8817 | ||
8818 | else | |
8819 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 8820 | -- positive value to largest exponent value, or from |
8821 | -- the exponentiation of most negative value to an | |
8822 | -- even exponent. | |
de922300 | 8823 | |
8824 | declare | |
8825 | Hi1, Hi2 : Uint; | |
8826 | ||
8827 | begin | |
5f4275e1 | 8828 | if Lhi > 0 then |
de922300 | 8829 | Hi1 := Lhi ** Rhi; |
8830 | else | |
8831 | Hi1 := Uint_0; | |
8832 | end if; | |
8833 | ||
8834 | if Llo < 0 then | |
8835 | if Rhi mod 2 = 0 then | |
de922300 | 8836 | Hi2 := Llo ** Rhi; |
5f4275e1 | 8837 | else |
8838 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 8839 | end if; |
8840 | else | |
8841 | Hi2 := Uint_0; | |
8842 | end if; | |
8843 | ||
8844 | Hi := UI_Max (Hi1, Hi2); | |
8845 | end; | |
8846 | ||
8847 | -- Result can only be negative if base can be negative | |
8848 | ||
8849 | if Llo < 0 then | |
21a55437 | 8850 | if Rhi mod 2 = 0 then |
de922300 | 8851 | Lo := Llo ** (Rhi - 1); |
8852 | else | |
8853 | Lo := Llo ** Rhi; | |
8854 | end if; | |
8855 | ||
21a55437 | 8856 | -- Otherwise low bound is minimum ** minimum |
de922300 | 8857 | |
8858 | else | |
8859 | Lo := Llo ** Rlo; | |
8860 | end if; | |
8861 | end if; | |
3cce7f32 | 8862 | |
8863 | -- Negation | |
8864 | ||
8865 | when N_Op_Minus => | |
8866 | Lo := -Rhi; | |
8867 | Hi := -Rlo; | |
8868 | ||
8869 | -- Mod | |
8870 | ||
8871 | when N_Op_Mod => | |
2fe22c69 | 8872 | declare |
5f4275e1 | 8873 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 8874 | -- This is the maximum absolute value of the result |
8875 | ||
8876 | begin | |
8877 | Lo := Uint_0; | |
8878 | Hi := Uint_0; | |
8879 | ||
8880 | -- The result depends only on the sign and magnitude of | |
8881 | -- the right operand, it does not depend on the sign or | |
8882 | -- magnitude of the left operand. | |
8883 | ||
8884 | if Rlo < 0 then | |
8885 | Lo := -Maxabs; | |
8886 | end if; | |
8887 | ||
8888 | if Rhi > 0 then | |
8889 | Hi := Maxabs; | |
8890 | end if; | |
8891 | end; | |
3cce7f32 | 8892 | |
8893 | -- Multiplication | |
8894 | ||
8895 | when N_Op_Multiply => | |
49b3a812 | 8896 | |
8897 | -- Possible bounds of multiplication must come from multiplying | |
8898 | -- end values of the input ranges (four possibilities). | |
8899 | ||
8900 | declare | |
8901 | Mrk : constant Uintp.Save_Mark := Mark; | |
8902 | -- Mark so we can release the Ev values | |
8903 | ||
8904 | Ev1 : constant Uint := Llo * Rlo; | |
8905 | Ev2 : constant Uint := Llo * Rhi; | |
8906 | Ev3 : constant Uint := Lhi * Rlo; | |
8907 | Ev4 : constant Uint := Lhi * Rhi; | |
8908 | ||
8909 | begin | |
8910 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
8911 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
8912 | ||
8913 | -- Release the Ev values | |
8914 | ||
8915 | Release_And_Save (Mrk, Lo, Hi); | |
8916 | end; | |
3cce7f32 | 8917 | |
8918 | -- Plus operator (affirmation) | |
8919 | ||
8920 | when N_Op_Plus => | |
8921 | Lo := Rlo; | |
8922 | Hi := Rhi; | |
8923 | ||
8924 | -- Remainder | |
8925 | ||
8926 | when N_Op_Rem => | |
2fe22c69 | 8927 | declare |
5f4275e1 | 8928 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 8929 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 8930 | -- that the result range does not depend on the sign of the |
8931 | -- right operand. | |
2fe22c69 | 8932 | |
8933 | begin | |
8934 | Lo := Uint_0; | |
8935 | Hi := Uint_0; | |
8936 | ||
8937 | -- Case of left operand negative, which results in a range | |
8938 | -- of -Maxabs .. 0 for those negative values. If there are | |
8939 | -- no negative values then Lo value of result is always 0. | |
8940 | ||
8941 | if Llo < 0 then | |
8942 | Lo := -Maxabs; | |
8943 | end if; | |
8944 | ||
8945 | -- Case of left operand positive | |
8946 | ||
8947 | if Lhi > 0 then | |
8948 | Hi := Maxabs; | |
8949 | end if; | |
8950 | end; | |
3cce7f32 | 8951 | |
8952 | -- Subtract | |
8953 | ||
8954 | when N_Op_Subtract => | |
8955 | Lo := Llo - Rhi; | |
8956 | Hi := Lhi - Rlo; | |
8957 | ||
8958 | -- Nothing else should be possible | |
8959 | ||
8960 | when others => | |
8961 | raise Program_Error; | |
3cce7f32 | 8962 | end case; |
8963 | end if; | |
8964 | ||
4fb5f0a0 | 8965 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 8966 | -- operands or long long integer operands), and we know the result. |
8967 | -- If we know we are in the result range, and we do not have Bignum | |
8968 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
8969 | -- overflow checks turned off (since we know we cannot have overflow). | |
8970 | -- As always the reexpansion is required to complete expansion of the | |
8971 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
8972 | -- by suppressing the check. | |
f32c377d | 8973 | |
8974 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
8975 | and then In_Result_Range | |
8976 | then | |
8977 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 8978 | Reexpand (Suppress => True); |
f32c377d | 8979 | return; |
8980 | ||
8981 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 8982 | -- case we will move into either the Bignum or Long_Long_Integer domain |
8983 | -- to compute the result. However, there is one exception. If we are | |
8984 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
8985 | -- operands, we will have to immediately convert the result back to | |
8986 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
8987 | -- fiddling. | |
f32c377d | 8988 | |
8989 | elsif Top_Level | |
8990 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 8991 | |
8992 | -- One further refinement. If we are at the top level, but our parent | |
8993 | -- is a type conversion, then go into bignum or long long integer node | |
8994 | -- since the result will be converted to that type directly without | |
8995 | -- going through the result type, and we may avoid an overflow. This | |
8996 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
8997 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
8998 | -- but does not fit in Integer. | |
8999 | ||
9000 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 9001 | then |
0df9d43f | 9002 | -- Here keep original types, but we need to complete analysis |
f32c377d | 9003 | |
9004 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 9005 | -- here because it will cause recursion into the whole MINIMIZED/ |
9006 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 9007 | -- we are at the top level, and we need a check against the result |
39a0c1d3 | 9008 | -- mode (i.e. we want to use STRICT mode). So do exactly that. |
4fb5f0a0 | 9009 | -- Also, we have not modified the node, so this is a case where |
9010 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 9011 | |
0df9d43f | 9012 | Reexpand; |
f32c377d | 9013 | return; |
9014 | ||
9015 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 9016 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 9017 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
9018 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 9019 | |
9020 | -- Note: we could do better here and in some cases switch back from | |
9021 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
9022 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
9023 | -- Failing to do this switching back is only an efficiency issue. | |
9024 | ||
f32c377d | 9025 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 9026 | |
61016a7a | 9027 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 9028 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 9029 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
9030 | ||
9031 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
9032 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
9033 | -- into Bignum mode, but there is an exception if neither of our | |
9034 | -- operands is Bignum now, and we are at the top level (Top_Level | |
9035 | -- set True). In this case, there is no point in moving into Bignum | |
9036 | -- mode to prevent overflow if the caller will immediately convert | |
9037 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 9038 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 9039 | |
9040 | if Check_Mode = Minimized | |
9041 | or else (Top_Level and not Bignum_Operands) | |
9042 | then | |
0df9d43f | 9043 | if Do_Overflow_Check (N) then |
9044 | Enable_Overflow_Check (N); | |
9045 | end if; | |
3cce7f32 | 9046 | |
0df9d43f | 9047 | -- The result now has to be in Long_Long_Integer mode, so adjust |
9048 | -- the possible range to reflect this. Note these calls also | |
9049 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 9050 | |
9051 | Max (Lo, LLLo); | |
9052 | Min (Hi, LLHi); | |
9053 | ||
9054 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 9055 | |
9056 | else | |
9057 | pragma Assert (Check_Mode = Eliminated); | |
9058 | ||
9059 | declare | |
9060 | Fent : Entity_Id; | |
9061 | Args : List_Id; | |
9062 | ||
9063 | begin | |
9064 | case Nkind (N) is | |
99378362 | 9065 | when N_Op_Abs => |
3cce7f32 | 9066 | Fent := RTE (RE_Big_Abs); |
9067 | ||
99378362 | 9068 | when N_Op_Add => |
3cce7f32 | 9069 | Fent := RTE (RE_Big_Add); |
9070 | ||
99378362 | 9071 | when N_Op_Divide => |
3cce7f32 | 9072 | Fent := RTE (RE_Big_Div); |
9073 | ||
99378362 | 9074 | when N_Op_Expon => |
3cce7f32 | 9075 | Fent := RTE (RE_Big_Exp); |
9076 | ||
99378362 | 9077 | when N_Op_Minus => |
3cce7f32 | 9078 | Fent := RTE (RE_Big_Neg); |
9079 | ||
99378362 | 9080 | when N_Op_Mod => |
3cce7f32 | 9081 | Fent := RTE (RE_Big_Mod); |
9082 | ||
9083 | when N_Op_Multiply => | |
9084 | Fent := RTE (RE_Big_Mul); | |
9085 | ||
99378362 | 9086 | when N_Op_Rem => |
3cce7f32 | 9087 | Fent := RTE (RE_Big_Rem); |
9088 | ||
9089 | when N_Op_Subtract => | |
9090 | Fent := RTE (RE_Big_Sub); | |
9091 | ||
9092 | -- Anything else is an internal error, this includes the | |
9093 | -- N_Op_Plus case, since how can plus cause the result | |
9094 | -- to be out of range if the operand is in range? | |
9095 | ||
9096 | when others => | |
9097 | raise Program_Error; | |
9098 | end case; | |
9099 | ||
9100 | -- Construct argument list for Bignum call, converting our | |
9101 | -- operands to Bignum form if they are not already there. | |
9102 | ||
9103 | Args := New_List; | |
9104 | ||
9105 | if Binary then | |
9106 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
9107 | end if; | |
9108 | ||
9109 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
9110 | ||
9111 | -- Now rewrite the arithmetic operator with a call to the | |
9112 | -- corresponding bignum function. | |
9113 | ||
9114 | Rewrite (N, | |
9115 | Make_Function_Call (Loc, | |
9116 | Name => New_Occurrence_Of (Fent, Loc), | |
9117 | Parameter_Associations => Args)); | |
0df9d43f | 9118 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 9119 | |
9120 | -- Indicate result is Bignum mode | |
9121 | ||
9122 | Lo := No_Uint; | |
9123 | Hi := No_Uint; | |
de922300 | 9124 | return; |
3cce7f32 | 9125 | end; |
9126 | end if; | |
9127 | ||
9128 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 9129 | -- check is required, at least not yet. |
3cce7f32 | 9130 | |
9131 | else | |
de922300 | 9132 | Set_Do_Overflow_Check (N, False); |
9133 | end if; | |
3cce7f32 | 9134 | |
f32c377d | 9135 | -- Here we are not in Bignum territory, but we may have long long |
9136 | -- integer operands that need special handling. First a special check: | |
9137 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
9138 | -- it means we converted it to prevent overflow, but exponentiation | |
9139 | -- requires a Natural right operand, so convert it back to Natural. | |
9140 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 9141 | |
f32c377d | 9142 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
9143 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 9144 | end if; |
9145 | ||
de922300 | 9146 | -- Here we will do the operation in Long_Long_Integer. We do this even |
9147 | -- if we know an overflow check is required, better to do this in long | |
39a0c1d3 | 9148 | -- long integer mode, since we are less likely to overflow. |
3cce7f32 | 9149 | |
de922300 | 9150 | -- Convert right or only operand to Long_Long_Integer, except that |
9151 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 9152 | |
de922300 | 9153 | if Nkind (N) /= N_Op_Expon then |
9154 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
9155 | end if; | |
3cce7f32 | 9156 | |
de922300 | 9157 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 9158 | |
de922300 | 9159 | if Binary then |
9160 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
9161 | end if; | |
9162 | ||
9163 | -- Reset node to unanalyzed | |
9164 | ||
9165 | Set_Analyzed (N, False); | |
9166 | Set_Etype (N, Empty); | |
9167 | Set_Entity (N, Empty); | |
9168 | ||
2fe22c69 | 9169 | -- Now analyze this new node. This reanalysis will complete processing |
9170 | -- for the node. In particular we will complete the expansion of an | |
9171 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
9172 | -- we will complete any division checks (since we have not changed the | |
9173 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 9174 | |
0df9d43f | 9175 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
39a0c1d3 | 9176 | -- MINIMIZED/ELIMINATED handling, since we are now done with that. |
3cce7f32 | 9177 | |
0df9d43f | 9178 | declare |
db415383 | 9179 | SG : constant Overflow_Mode_Type := |
9180 | Scope_Suppress.Overflow_Mode_General; | |
9181 | SA : constant Overflow_Mode_Type := | |
9182 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 9183 | |
0df9d43f | 9184 | begin |
db415383 | 9185 | Scope_Suppress.Overflow_Mode_General := Strict; |
9186 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 9187 | |
0df9d43f | 9188 | if not Do_Overflow_Check (N) then |
9189 | Reanalyze (LLIB, Suppress => True); | |
9190 | else | |
9191 | Reanalyze (LLIB); | |
9192 | end if; | |
9193 | ||
db415383 | 9194 | Scope_Suppress.Overflow_Mode_General := SG; |
9195 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 9196 | end; |
9197 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 9198 | |
9199 | ------------------------- | |
9200 | -- Overflow_Check_Mode -- | |
9201 | ------------------------- | |
9202 | ||
db415383 | 9203 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 9204 | begin |
724d2bd8 | 9205 | if In_Assertion_Expr = 0 then |
db415383 | 9206 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 9207 | else |
db415383 | 9208 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 9209 | end if; |
3cce7f32 | 9210 | end Overflow_Check_Mode; |
9211 | ||
9212 | -------------------------------- | |
9213 | -- Overflow_Checks_Suppressed -- | |
9214 | -------------------------------- | |
9215 | ||
9216 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9217 | begin | |
0df9d43f | 9218 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9219 | return Is_Check_Suppressed (E, Overflow_Check); | |
9220 | else | |
9221 | return Scope_Suppress.Suppress (Overflow_Check); | |
9222 | end if; | |
ee6ba406 | 9223 | end Overflow_Checks_Suppressed; |
fc75802a | 9224 | |
37baba83 | 9225 | --------------------------------- |
9226 | -- Predicate_Checks_Suppressed -- | |
9227 | --------------------------------- | |
9228 | ||
9229 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9230 | begin | |
9231 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9232 | return Is_Check_Suppressed (E, Predicate_Check); | |
9233 | else | |
9234 | return Scope_Suppress.Suppress (Predicate_Check); | |
9235 | end if; | |
9236 | end Predicate_Checks_Suppressed; | |
9237 | ||
ee6ba406 | 9238 | ----------------------------- |
9239 | -- Range_Checks_Suppressed -- | |
9240 | ----------------------------- | |
9241 | ||
9242 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9243 | begin | |
9dfe12ae | 9244 | if Present (E) then |
ce4d14c4 | 9245 | if Kill_Range_Checks (E) then |
9dfe12ae | 9246 | return True; |
5f46de53 | 9247 | |
9dfe12ae | 9248 | elsif Checks_May_Be_Suppressed (E) then |
9249 | return Is_Check_Suppressed (E, Range_Check); | |
9250 | end if; | |
9251 | end if; | |
ee6ba406 | 9252 | |
fafc6b97 | 9253 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 9254 | end Range_Checks_Suppressed; |
9255 | ||
0577b0b1 | 9256 | ----------------------------------------- |
9257 | -- Range_Or_Validity_Checks_Suppressed -- | |
9258 | ----------------------------------------- | |
9259 | ||
9260 | -- Note: the coding would be simpler here if we simply made appropriate | |
9261 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
9262 | -- duplicated checks which we prefer to avoid. | |
9263 | ||
9264 | function Range_Or_Validity_Checks_Suppressed | |
9265 | (Expr : Node_Id) return Boolean | |
9266 | is | |
9267 | begin | |
9268 | -- Immediate return if scope checks suppressed for either check | |
9269 | ||
fafc6b97 | 9270 | if Scope_Suppress.Suppress (Range_Check) |
9271 | or | |
9272 | Scope_Suppress.Suppress (Validity_Check) | |
9273 | then | |
0577b0b1 | 9274 | return True; |
9275 | end if; | |
9276 | ||
9277 | -- If no expression, that's odd, decide that checks are suppressed, | |
9278 | -- since we don't want anyone trying to do checks in this case, which | |
9279 | -- is most likely the result of some other error. | |
9280 | ||
9281 | if No (Expr) then | |
9282 | return True; | |
9283 | end if; | |
9284 | ||
9285 | -- Expression is present, so perform suppress checks on type | |
9286 | ||
9287 | declare | |
9288 | Typ : constant Entity_Id := Etype (Expr); | |
9289 | begin | |
ce4d14c4 | 9290 | if Checks_May_Be_Suppressed (Typ) |
0577b0b1 | 9291 | and then (Is_Check_Suppressed (Typ, Range_Check) |
9292 | or else | |
9293 | Is_Check_Suppressed (Typ, Validity_Check)) | |
9294 | then | |
9295 | return True; | |
9296 | end if; | |
9297 | end; | |
9298 | ||
9299 | -- If expression is an entity name, perform checks on this entity | |
9300 | ||
9301 | if Is_Entity_Name (Expr) then | |
9302 | declare | |
9303 | Ent : constant Entity_Id := Entity (Expr); | |
9304 | begin | |
9305 | if Checks_May_Be_Suppressed (Ent) then | |
9306 | return Is_Check_Suppressed (Ent, Range_Check) | |
9307 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
9308 | end if; | |
9309 | end; | |
9310 | end if; | |
9311 | ||
9312 | -- If we fall through, no checks suppressed | |
9313 | ||
9314 | return False; | |
9315 | end Range_Or_Validity_Checks_Suppressed; | |
9316 | ||
226494a3 | 9317 | ------------------- |
9318 | -- Remove_Checks -- | |
9319 | ------------------- | |
9320 | ||
9321 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 9322 | function Process (N : Node_Id) return Traverse_Result; |
9323 | -- Process a single node during the traversal | |
9324 | ||
8f6e4fd5 | 9325 | procedure Traverse is new Traverse_Proc (Process); |
9326 | -- The traversal procedure itself | |
226494a3 | 9327 | |
9328 | ------------- | |
9329 | -- Process -- | |
9330 | ------------- | |
9331 | ||
9332 | function Process (N : Node_Id) return Traverse_Result is | |
9333 | begin | |
9334 | if Nkind (N) not in N_Subexpr then | |
9335 | return Skip; | |
9336 | end if; | |
9337 | ||
9338 | Set_Do_Range_Check (N, False); | |
9339 | ||
9340 | case Nkind (N) is | |
9341 | when N_And_Then => | |
8f6e4fd5 | 9342 | Traverse (Left_Opnd (N)); |
226494a3 | 9343 | return Skip; |
9344 | ||
9345 | when N_Attribute_Reference => | |
226494a3 | 9346 | Set_Do_Overflow_Check (N, False); |
9347 | ||
226494a3 | 9348 | when N_Function_Call => |
9349 | Set_Do_Tag_Check (N, False); | |
9350 | ||
226494a3 | 9351 | when N_Op => |
9352 | Set_Do_Overflow_Check (N, False); | |
9353 | ||
9354 | case Nkind (N) is | |
9355 | when N_Op_Divide => | |
9356 | Set_Do_Division_Check (N, False); | |
9357 | ||
9358 | when N_Op_And => | |
9359 | Set_Do_Length_Check (N, False); | |
9360 | ||
9361 | when N_Op_Mod => | |
9362 | Set_Do_Division_Check (N, False); | |
9363 | ||
9364 | when N_Op_Or => | |
9365 | Set_Do_Length_Check (N, False); | |
9366 | ||
9367 | when N_Op_Rem => | |
9368 | Set_Do_Division_Check (N, False); | |
9369 | ||
9370 | when N_Op_Xor => | |
9371 | Set_Do_Length_Check (N, False); | |
9372 | ||
9373 | when others => | |
9374 | null; | |
9375 | end case; | |
9376 | ||
9377 | when N_Or_Else => | |
8f6e4fd5 | 9378 | Traverse (Left_Opnd (N)); |
226494a3 | 9379 | return Skip; |
9380 | ||
9381 | when N_Selected_Component => | |
226494a3 | 9382 | Set_Do_Discriminant_Check (N, False); |
9383 | ||
226494a3 | 9384 | when N_Type_Conversion => |
9dfe12ae | 9385 | Set_Do_Length_Check (N, False); |
9386 | Set_Do_Tag_Check (N, False); | |
226494a3 | 9387 | Set_Do_Overflow_Check (N, False); |
226494a3 | 9388 | |
9389 | when others => | |
9390 | null; | |
9391 | end case; | |
9392 | ||
9393 | return OK; | |
9394 | end Process; | |
9395 | ||
9396 | -- Start of processing for Remove_Checks | |
9397 | ||
9398 | begin | |
8f6e4fd5 | 9399 | Traverse (Expr); |
226494a3 | 9400 | end Remove_Checks; |
9401 | ||
ee6ba406 | 9402 | ---------------------------- |
9403 | -- Selected_Length_Checks -- | |
9404 | ---------------------------- | |
9405 | ||
9406 | function Selected_Length_Checks | |
9407 | (Ck_Node : Node_Id; | |
9408 | Target_Typ : Entity_Id; | |
9409 | Source_Typ : Entity_Id; | |
314a23b6 | 9410 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 9411 | is |
9412 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
9413 | S_Typ : Entity_Id; | |
9414 | T_Typ : Entity_Id; | |
9415 | Expr_Actual : Node_Id; | |
9416 | Exptyp : Entity_Id; | |
9417 | Cond : Node_Id := Empty; | |
9418 | Do_Access : Boolean := False; | |
9419 | Wnode : Node_Id := Warn_Node; | |
9420 | Ret_Result : Check_Result := (Empty, Empty); | |
9421 | Num_Checks : Natural := 0; | |
9422 | ||
9423 | procedure Add_Check (N : Node_Id); | |
9424 | -- Adds the action given to Ret_Result if N is non-Empty | |
9425 | ||
9426 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
9427 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 9428 | -- Comments required ??? |
ee6ba406 | 9429 | |
9430 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
9431 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 9432 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 9433 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 9434 | -- obviously superfluous checks. |
ee6ba406 | 9435 | |
9436 | function Length_E_Cond | |
9437 | (Exptyp : Entity_Id; | |
9438 | Typ : Entity_Id; | |
314a23b6 | 9439 | Indx : Nat) return Node_Id; |
ee6ba406 | 9440 | -- Returns expression to compute: |
9441 | -- Typ'Length /= Exptyp'Length | |
9442 | ||
9443 | function Length_N_Cond | |
9444 | (Expr : Node_Id; | |
9445 | Typ : Entity_Id; | |
314a23b6 | 9446 | Indx : Nat) return Node_Id; |
ee6ba406 | 9447 | -- Returns expression to compute: |
9448 | -- Typ'Length /= Expr'Length | |
9449 | ||
9450 | --------------- | |
9451 | -- Add_Check -- | |
9452 | --------------- | |
9453 | ||
9454 | procedure Add_Check (N : Node_Id) is | |
9455 | begin | |
9456 | if Present (N) then | |
9457 | ||
20cf157b | 9458 | -- For now, ignore attempt to place more than two checks ??? |
9459 | -- This is really worrisome, are we really discarding checks ??? | |
ee6ba406 | 9460 | |
9461 | if Num_Checks = 2 then | |
9462 | return; | |
9463 | end if; | |
9464 | ||
9465 | pragma Assert (Num_Checks <= 1); | |
9466 | Num_Checks := Num_Checks + 1; | |
9467 | Ret_Result (Num_Checks) := N; | |
9468 | end if; | |
9469 | end Add_Check; | |
9470 | ||
9471 | ------------------ | |
9472 | -- Get_E_Length -- | |
9473 | ------------------ | |
9474 | ||
9475 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 9476 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 9477 | N : Node_Id; |
9478 | E1 : Entity_Id := E; | |
ee6ba406 | 9479 | |
9480 | begin | |
9481 | if Ekind (Scope (E)) = E_Record_Type | |
9482 | and then Has_Discriminants (Scope (E)) | |
9483 | then | |
9484 | N := Build_Discriminal_Subtype_Of_Component (E); | |
9485 | ||
9486 | if Present (N) then | |
9487 | Insert_Action (Ck_Node, N); | |
9488 | E1 := Defining_Identifier (N); | |
9489 | end if; | |
9490 | end if; | |
9491 | ||
9492 | if Ekind (E1) = E_String_Literal_Subtype then | |
9493 | return | |
9494 | Make_Integer_Literal (Loc, | |
9495 | Intval => String_Literal_Length (E1)); | |
9496 | ||
00c403ee | 9497 | elsif SE /= Standard_Standard |
9498 | and then Ekind (Scope (SE)) = E_Protected_Type | |
9499 | and then Has_Discriminants (Scope (SE)) | |
9500 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 9501 | and then not Inside_Init_Proc |
9502 | then | |
ee6ba406 | 9503 | -- If the type whose length is needed is a private component |
9504 | -- constrained by a discriminant, we must expand the 'Length | |
9505 | -- attribute into an explicit computation, using the discriminal | |
9506 | -- of the current protected operation. This is because the actual | |
9507 | -- type of the prival is constructed after the protected opera- | |
9508 | -- tion has been fully expanded. | |
9509 | ||
9510 | declare | |
9511 | Indx_Type : Node_Id; | |
9512 | Lo : Node_Id; | |
9513 | Hi : Node_Id; | |
9514 | Do_Expand : Boolean := False; | |
9515 | ||
9516 | begin | |
9517 | Indx_Type := First_Index (E); | |
9518 | ||
9519 | for J in 1 .. Indx - 1 loop | |
9520 | Next_Index (Indx_Type); | |
9521 | end loop; | |
9522 | ||
2af58f67 | 9523 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 9524 | |
9525 | if Nkind (Lo) = N_Identifier | |
9526 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
9527 | then | |
9528 | Lo := Get_Discriminal (E, Lo); | |
9529 | Do_Expand := True; | |
9530 | end if; | |
9531 | ||
9532 | if Nkind (Hi) = N_Identifier | |
9533 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
9534 | then | |
9535 | Hi := Get_Discriminal (E, Hi); | |
9536 | Do_Expand := True; | |
9537 | end if; | |
9538 | ||
9539 | if Do_Expand then | |
9540 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 9541 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 9542 | end if; |
9543 | ||
9544 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 9545 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 9546 | end if; |
9547 | ||
9548 | N := | |
9549 | Make_Op_Add (Loc, | |
9550 | Left_Opnd => | |
9551 | Make_Op_Subtract (Loc, | |
9552 | Left_Opnd => Hi, | |
9553 | Right_Opnd => Lo), | |
9554 | ||
9555 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
9556 | return N; | |
9557 | ||
9558 | else | |
9559 | N := | |
9560 | Make_Attribute_Reference (Loc, | |
9561 | Attribute_Name => Name_Length, | |
9562 | Prefix => | |
9563 | New_Occurrence_Of (E1, Loc)); | |
9564 | ||
9565 | if Indx > 1 then | |
9566 | Set_Expressions (N, New_List ( | |
9567 | Make_Integer_Literal (Loc, Indx))); | |
9568 | end if; | |
9569 | ||
9570 | return N; | |
9571 | end if; | |
9572 | end; | |
9573 | ||
9574 | else | |
9575 | N := | |
9576 | Make_Attribute_Reference (Loc, | |
9577 | Attribute_Name => Name_Length, | |
9578 | Prefix => | |
9579 | New_Occurrence_Of (E1, Loc)); | |
9580 | ||
9581 | if Indx > 1 then | |
9582 | Set_Expressions (N, New_List ( | |
9583 | Make_Integer_Literal (Loc, Indx))); | |
9584 | end if; | |
9585 | ||
9586 | return N; | |
ee6ba406 | 9587 | end if; |
9588 | end Get_E_Length; | |
9589 | ||
9590 | ------------------ | |
9591 | -- Get_N_Length -- | |
9592 | ------------------ | |
9593 | ||
9594 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
9595 | begin | |
9596 | return | |
9597 | Make_Attribute_Reference (Loc, | |
9598 | Attribute_Name => Name_Length, | |
9599 | Prefix => | |
9dfe12ae | 9600 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 9601 | Expressions => New_List ( |
9602 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 9603 | end Get_N_Length; |
9604 | ||
9605 | ------------------- | |
9606 | -- Length_E_Cond -- | |
9607 | ------------------- | |
9608 | ||
9609 | function Length_E_Cond | |
9610 | (Exptyp : Entity_Id; | |
9611 | Typ : Entity_Id; | |
314a23b6 | 9612 | Indx : Nat) return Node_Id |
ee6ba406 | 9613 | is |
9614 | begin | |
9615 | return | |
9616 | Make_Op_Ne (Loc, | |
9617 | Left_Opnd => Get_E_Length (Typ, Indx), | |
9618 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 9619 | end Length_E_Cond; |
9620 | ||
9621 | ------------------- | |
9622 | -- Length_N_Cond -- | |
9623 | ------------------- | |
9624 | ||
9625 | function Length_N_Cond | |
9626 | (Expr : Node_Id; | |
9627 | Typ : Entity_Id; | |
314a23b6 | 9628 | Indx : Nat) return Node_Id |
ee6ba406 | 9629 | is |
9630 | begin | |
9631 | return | |
9632 | Make_Op_Ne (Loc, | |
9633 | Left_Opnd => Get_E_Length (Typ, Indx), | |
9634 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 9635 | end Length_N_Cond; |
9636 | ||
feff2f05 | 9637 | ----------------- |
9638 | -- Same_Bounds -- | |
9639 | ----------------- | |
9640 | ||
ee6ba406 | 9641 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
9642 | begin | |
9643 | return | |
9644 | (Nkind (L) = N_Integer_Literal | |
9645 | and then Nkind (R) = N_Integer_Literal | |
9646 | and then Intval (L) = Intval (R)) | |
9647 | ||
9648 | or else | |
9649 | (Is_Entity_Name (L) | |
9650 | and then Ekind (Entity (L)) = E_Constant | |
9651 | and then ((Is_Entity_Name (R) | |
9652 | and then Entity (L) = Entity (R)) | |
9653 | or else | |
9654 | (Nkind (R) = N_Type_Conversion | |
9655 | and then Is_Entity_Name (Expression (R)) | |
9656 | and then Entity (L) = Entity (Expression (R))))) | |
9657 | ||
9658 | or else | |
9659 | (Is_Entity_Name (R) | |
9660 | and then Ekind (Entity (R)) = E_Constant | |
9661 | and then Nkind (L) = N_Type_Conversion | |
9662 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 9663 | and then Entity (R) = Entity (Expression (L))) |
9664 | ||
9665 | or else | |
9666 | (Is_Entity_Name (L) | |
9667 | and then Is_Entity_Name (R) | |
9668 | and then Entity (L) = Entity (R) | |
9669 | and then Ekind (Entity (L)) = E_In_Parameter | |
9670 | and then Inside_Init_Proc); | |
ee6ba406 | 9671 | end Same_Bounds; |
9672 | ||
9673 | -- Start of processing for Selected_Length_Checks | |
9674 | ||
9675 | begin | |
18cb6d78 | 9676 | -- Checks will be applied only when generating code |
f0d65dae | 9677 | |
18cb6d78 | 9678 | if not Expander_Active then |
ee6ba406 | 9679 | return Ret_Result; |
9680 | end if; | |
9681 | ||
9682 | if Target_Typ = Any_Type | |
9683 | or else Target_Typ = Any_Composite | |
9684 | or else Raises_Constraint_Error (Ck_Node) | |
9685 | then | |
9686 | return Ret_Result; | |
9687 | end if; | |
9688 | ||
9689 | if No (Wnode) then | |
9690 | Wnode := Ck_Node; | |
9691 | end if; | |
9692 | ||
9693 | T_Typ := Target_Typ; | |
9694 | ||
9695 | if No (Source_Typ) then | |
9696 | S_Typ := Etype (Ck_Node); | |
9697 | else | |
9698 | S_Typ := Source_Typ; | |
9699 | end if; | |
9700 | ||
9701 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
9702 | return Ret_Result; | |
9703 | end if; | |
9704 | ||
9705 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
9706 | S_Typ := Designated_Type (S_Typ); | |
9707 | T_Typ := Designated_Type (T_Typ); | |
9708 | Do_Access := True; | |
9709 | ||
2af58f67 | 9710 | -- A simple optimization for the null case |
ee6ba406 | 9711 | |
2af58f67 | 9712 | if Known_Null (Ck_Node) then |
ee6ba406 | 9713 | return Ret_Result; |
9714 | end if; | |
9715 | end if; | |
9716 | ||
9717 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9718 | if Is_Constrained (T_Typ) then | |
9719 | ||
92f1631f | 9720 | -- The checking code to be generated will freeze the corresponding |
9721 | -- array type. However, we must freeze the type now, so that the | |
9722 | -- freeze node does not appear within the generated if expression, | |
9723 | -- but ahead of it. | |
ee6ba406 | 9724 | |
9725 | Freeze_Before (Ck_Node, T_Typ); | |
9726 | ||
9727 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 9728 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 9729 | |
9730 | if Is_Access_Type (Exptyp) then | |
9731 | Exptyp := Designated_Type (Exptyp); | |
9732 | end if; | |
9733 | ||
9734 | -- String_Literal case. This needs to be handled specially be- | |
9735 | -- cause no index types are available for string literals. The | |
9736 | -- condition is simply: | |
9737 | ||
9738 | -- T_Typ'Length = string-literal-length | |
9739 | ||
9dfe12ae | 9740 | if Nkind (Expr_Actual) = N_String_Literal |
9741 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
9742 | then | |
ee6ba406 | 9743 | Cond := |
9744 | Make_Op_Ne (Loc, | |
9745 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
9746 | Right_Opnd => | |
9747 | Make_Integer_Literal (Loc, | |
9748 | Intval => | |
9749 | String_Literal_Length (Etype (Expr_Actual)))); | |
9750 | ||
9751 | -- General array case. Here we have a usable actual subtype for | |
9752 | -- the expression, and the condition is built from the two types | |
9753 | -- (Do_Length): | |
9754 | ||
9755 | -- T_Typ'Length /= Exptyp'Length or else | |
9756 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
9757 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
9758 | -- ... | |
9759 | ||
9760 | elsif Is_Constrained (Exptyp) then | |
9761 | declare | |
9dfe12ae | 9762 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9763 | ||
9764 | L_Index : Node_Id; | |
9765 | R_Index : Node_Id; | |
9766 | L_Low : Node_Id; | |
9767 | L_High : Node_Id; | |
9768 | R_Low : Node_Id; | |
9769 | R_High : Node_Id; | |
ee6ba406 | 9770 | L_Length : Uint; |
9771 | R_Length : Uint; | |
9dfe12ae | 9772 | Ref_Node : Node_Id; |
ee6ba406 | 9773 | |
9774 | begin | |
feff2f05 | 9775 | -- At the library level, we need to ensure that the type of |
9776 | -- the object is elaborated before the check itself is | |
9777 | -- emitted. This is only done if the object is in the | |
9778 | -- current compilation unit, otherwise the type is frozen | |
9779 | -- and elaborated in its unit. | |
9dfe12ae | 9780 | |
9781 | if Is_Itype (Exptyp) | |
9782 | and then | |
9783 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
9784 | and then | |
9785 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 9786 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 9787 | then |
9788 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
9789 | Set_Itype (Ref_Node, Exptyp); | |
9790 | Insert_Action (Ck_Node, Ref_Node); | |
9791 | end if; | |
9792 | ||
ee6ba406 | 9793 | L_Index := First_Index (T_Typ); |
9794 | R_Index := First_Index (Exptyp); | |
9795 | ||
9796 | for Indx in 1 .. Ndims loop | |
9797 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9798 | or else |
9799 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9800 | then |
9801 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
9802 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
9803 | ||
9804 | -- Deal with compile time length check. Note that we | |
9805 | -- skip this in the access case, because the access | |
9806 | -- value may be null, so we cannot know statically. | |
9807 | ||
9808 | if not Do_Access | |
9809 | and then Compile_Time_Known_Value (L_Low) | |
9810 | and then Compile_Time_Known_Value (L_High) | |
9811 | and then Compile_Time_Known_Value (R_Low) | |
9812 | and then Compile_Time_Known_Value (R_High) | |
9813 | then | |
9814 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
9815 | L_Length := Expr_Value (L_High) - | |
9816 | Expr_Value (L_Low) + 1; | |
9817 | else | |
9818 | L_Length := UI_From_Int (0); | |
9819 | end if; | |
9820 | ||
9821 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
9822 | R_Length := Expr_Value (R_High) - | |
9823 | Expr_Value (R_Low) + 1; | |
9824 | else | |
9825 | R_Length := UI_From_Int (0); | |
9826 | end if; | |
9827 | ||
9828 | if L_Length > R_Length then | |
9829 | Add_Check | |
9830 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9831 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 9832 | |
b8eacb12 | 9833 | elsif L_Length < R_Length then |
ee6ba406 | 9834 | Add_Check |
9835 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9836 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 9837 | end if; |
9838 | ||
9839 | -- The comparison for an individual index subtype | |
9840 | -- is omitted if the corresponding index subtypes | |
9841 | -- statically match, since the result is known to | |
9842 | -- be true. Note that this test is worth while even | |
9843 | -- though we do static evaluation, because non-static | |
9844 | -- subtypes can statically match. | |
9845 | ||
9846 | elsif not | |
9847 | Subtypes_Statically_Match | |
9848 | (Etype (L_Index), Etype (R_Index)) | |
9849 | ||
9850 | and then not | |
9851 | (Same_Bounds (L_Low, R_Low) | |
9852 | and then Same_Bounds (L_High, R_High)) | |
9853 | then | |
9854 | Evolve_Or_Else | |
9855 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
9856 | end if; | |
9857 | ||
9858 | Next (L_Index); | |
9859 | Next (R_Index); | |
9860 | end if; | |
9861 | end loop; | |
9862 | end; | |
9863 | ||
9864 | -- Handle cases where we do not get a usable actual subtype that | |
9865 | -- is constrained. This happens for example in the function call | |
9866 | -- and explicit dereference cases. In these cases, we have to get | |
9867 | -- the length or range from the expression itself, making sure we | |
9868 | -- do not evaluate it more than once. | |
9869 | ||
9870 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 9871 | -- result of applying Duplicate_Expr to the original tree, forcing |
9872 | -- the result to be a name. | |
ee6ba406 | 9873 | |
9874 | else | |
9875 | declare | |
9dfe12ae | 9876 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9877 | |
9878 | begin | |
9879 | -- Build the condition for the explicit dereference case | |
9880 | ||
9881 | for Indx in 1 .. Ndims loop | |
9882 | Evolve_Or_Else | |
9883 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
9884 | end loop; | |
9885 | end; | |
9886 | end if; | |
9887 | end if; | |
9888 | end if; | |
9889 | ||
9890 | -- Construct the test and insert into the tree | |
9891 | ||
9892 | if Present (Cond) then | |
9893 | if Do_Access then | |
9894 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9895 | end if; | |
9896 | ||
f15731c4 | 9897 | Add_Check |
9898 | (Make_Raise_Constraint_Error (Loc, | |
9899 | Condition => Cond, | |
9900 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 9901 | end if; |
9902 | ||
9903 | return Ret_Result; | |
ee6ba406 | 9904 | end Selected_Length_Checks; |
9905 | ||
9906 | --------------------------- | |
9907 | -- Selected_Range_Checks -- | |
9908 | --------------------------- | |
9909 | ||
9910 | function Selected_Range_Checks | |
9911 | (Ck_Node : Node_Id; | |
9912 | Target_Typ : Entity_Id; | |
9913 | Source_Typ : Entity_Id; | |
314a23b6 | 9914 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 9915 | is |
9916 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
9917 | S_Typ : Entity_Id; | |
9918 | T_Typ : Entity_Id; | |
9919 | Expr_Actual : Node_Id; | |
9920 | Exptyp : Entity_Id; | |
9921 | Cond : Node_Id := Empty; | |
9922 | Do_Access : Boolean := False; | |
9923 | Wnode : Node_Id := Warn_Node; | |
9924 | Ret_Result : Check_Result := (Empty, Empty); | |
5bb74b99 | 9925 | Num_Checks : Natural := 0; |
ee6ba406 | 9926 | |
9927 | procedure Add_Check (N : Node_Id); | |
9928 | -- Adds the action given to Ret_Result if N is non-Empty | |
9929 | ||
9930 | function Discrete_Range_Cond | |
9931 | (Expr : Node_Id; | |
314a23b6 | 9932 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 9933 | -- Returns expression to compute: |
9934 | -- Low_Bound (Expr) < Typ'First | |
9935 | -- or else | |
9936 | -- High_Bound (Expr) > Typ'Last | |
9937 | ||
9938 | function Discrete_Expr_Cond | |
9939 | (Expr : Node_Id; | |
314a23b6 | 9940 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 9941 | -- Returns expression to compute: |
9942 | -- Expr < Typ'First | |
9943 | -- or else | |
9944 | -- Expr > Typ'Last | |
9945 | ||
9946 | function Get_E_First_Or_Last | |
3cb12758 | 9947 | (Loc : Source_Ptr; |
9948 | E : Entity_Id; | |
ee6ba406 | 9949 | Indx : Nat; |
314a23b6 | 9950 | Nam : Name_Id) return Node_Id; |
79212397 | 9951 | -- Returns an attribute reference |
ee6ba406 | 9952 | -- E'First or E'Last |
79212397 | 9953 | -- with a source location of Loc. |
f73ee678 | 9954 | -- |
79212397 | 9955 | -- Nam is Name_First or Name_Last, according to which attribute is |
9956 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
9957 | -- Expressions of the attribute reference (identifying the desired | |
9958 | -- array dimension). | |
ee6ba406 | 9959 | |
9960 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
9961 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
9962 | -- Returns expression to compute: | |
9dfe12ae | 9963 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 9964 | |
9965 | function Range_E_Cond | |
9966 | (Exptyp : Entity_Id; | |
9967 | Typ : Entity_Id; | |
9968 | Indx : Nat) | |
9969 | return Node_Id; | |
9970 | -- Returns expression to compute: | |
9971 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
9972 | ||
9973 | function Range_Equal_E_Cond | |
9974 | (Exptyp : Entity_Id; | |
9975 | Typ : Entity_Id; | |
314a23b6 | 9976 | Indx : Nat) return Node_Id; |
ee6ba406 | 9977 | -- Returns expression to compute: |
9978 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
9979 | ||
9980 | function Range_N_Cond | |
9981 | (Expr : Node_Id; | |
9982 | Typ : Entity_Id; | |
314a23b6 | 9983 | Indx : Nat) return Node_Id; |
ee6ba406 | 9984 | -- Return expression to compute: |
9985 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
9986 | ||
9987 | --------------- | |
9988 | -- Add_Check -- | |
9989 | --------------- | |
9990 | ||
9991 | procedure Add_Check (N : Node_Id) is | |
9992 | begin | |
9993 | if Present (N) then | |
9994 | ||
9995 | -- For now, ignore attempt to place more than 2 checks ??? | |
9996 | ||
9997 | if Num_Checks = 2 then | |
9998 | return; | |
9999 | end if; | |
10000 | ||
10001 | pragma Assert (Num_Checks <= 1); | |
10002 | Num_Checks := Num_Checks + 1; | |
10003 | Ret_Result (Num_Checks) := N; | |
10004 | end if; | |
10005 | end Add_Check; | |
10006 | ||
10007 | ------------------------- | |
10008 | -- Discrete_Expr_Cond -- | |
10009 | ------------------------- | |
10010 | ||
10011 | function Discrete_Expr_Cond | |
10012 | (Expr : Node_Id; | |
314a23b6 | 10013 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 10014 | is |
10015 | begin | |
10016 | return | |
10017 | Make_Or_Else (Loc, | |
10018 | Left_Opnd => | |
10019 | Make_Op_Lt (Loc, | |
10020 | Left_Opnd => | |
9dfe12ae | 10021 | Convert_To (Base_Type (Typ), |
10022 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 10023 | Right_Opnd => |
10024 | Convert_To (Base_Type (Typ), | |
3cb12758 | 10025 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 10026 | |
10027 | Right_Opnd => | |
10028 | Make_Op_Gt (Loc, | |
10029 | Left_Opnd => | |
9dfe12ae | 10030 | Convert_To (Base_Type (Typ), |
10031 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 10032 | Right_Opnd => |
10033 | Convert_To | |
10034 | (Base_Type (Typ), | |
3cb12758 | 10035 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 10036 | end Discrete_Expr_Cond; |
10037 | ||
10038 | ------------------------- | |
10039 | -- Discrete_Range_Cond -- | |
10040 | ------------------------- | |
10041 | ||
10042 | function Discrete_Range_Cond | |
10043 | (Expr : Node_Id; | |
314a23b6 | 10044 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 10045 | is |
10046 | LB : Node_Id := Low_Bound (Expr); | |
10047 | HB : Node_Id := High_Bound (Expr); | |
10048 | ||
10049 | Left_Opnd : Node_Id; | |
10050 | Right_Opnd : Node_Id; | |
10051 | ||
10052 | begin | |
10053 | if Nkind (LB) = N_Identifier | |
feff2f05 | 10054 | and then Ekind (Entity (LB)) = E_Discriminant |
10055 | then | |
ee6ba406 | 10056 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
10057 | end if; | |
10058 | ||
ee6ba406 | 10059 | Left_Opnd := |
10060 | Make_Op_Lt (Loc, | |
10061 | Left_Opnd => | |
10062 | Convert_To | |
9dfe12ae | 10063 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 10064 | |
10065 | Right_Opnd => | |
10066 | Convert_To | |
3cb12758 | 10067 | (Base_Type (Typ), |
10068 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 10069 | |
ba9b1a39 | 10070 | if Nkind (HB) = N_Identifier |
10071 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 10072 | then |
ba9b1a39 | 10073 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 10074 | end if; |
10075 | ||
10076 | Right_Opnd := | |
10077 | Make_Op_Gt (Loc, | |
10078 | Left_Opnd => | |
10079 | Convert_To | |
9dfe12ae | 10080 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 10081 | |
10082 | Right_Opnd => | |
10083 | Convert_To | |
10084 | (Base_Type (Typ), | |
3cb12758 | 10085 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 10086 | |
10087 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
10088 | end Discrete_Range_Cond; | |
10089 | ||
10090 | ------------------------- | |
10091 | -- Get_E_First_Or_Last -- | |
10092 | ------------------------- | |
10093 | ||
10094 | function Get_E_First_Or_Last | |
3cb12758 | 10095 | (Loc : Source_Ptr; |
10096 | E : Entity_Id; | |
ee6ba406 | 10097 | Indx : Nat; |
314a23b6 | 10098 | Nam : Name_Id) return Node_Id |
ee6ba406 | 10099 | is |
3cb12758 | 10100 | Exprs : List_Id; |
ee6ba406 | 10101 | begin |
3cb12758 | 10102 | if Indx > 0 then |
10103 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 10104 | else |
3cb12758 | 10105 | Exprs := No_List; |
ee6ba406 | 10106 | end if; |
10107 | ||
3cb12758 | 10108 | return Make_Attribute_Reference (Loc, |
10109 | Prefix => New_Occurrence_Of (E, Loc), | |
10110 | Attribute_Name => Nam, | |
10111 | Expressions => Exprs); | |
ee6ba406 | 10112 | end Get_E_First_Or_Last; |
10113 | ||
10114 | ----------------- | |
10115 | -- Get_N_First -- | |
10116 | ----------------- | |
10117 | ||
10118 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
10119 | begin | |
10120 | return | |
10121 | Make_Attribute_Reference (Loc, | |
10122 | Attribute_Name => Name_First, | |
10123 | Prefix => | |
9dfe12ae | 10124 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 10125 | Expressions => New_List ( |
10126 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 10127 | end Get_N_First; |
10128 | ||
10129 | ---------------- | |
10130 | -- Get_N_Last -- | |
10131 | ---------------- | |
10132 | ||
10133 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
10134 | begin | |
10135 | return | |
10136 | Make_Attribute_Reference (Loc, | |
10137 | Attribute_Name => Name_Last, | |
10138 | Prefix => | |
9dfe12ae | 10139 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 10140 | Expressions => New_List ( |
10141 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 10142 | end Get_N_Last; |
10143 | ||
10144 | ------------------ | |
10145 | -- Range_E_Cond -- | |
10146 | ------------------ | |
10147 | ||
10148 | function Range_E_Cond | |
10149 | (Exptyp : Entity_Id; | |
10150 | Typ : Entity_Id; | |
314a23b6 | 10151 | Indx : Nat) return Node_Id |
ee6ba406 | 10152 | is |
10153 | begin | |
10154 | return | |
10155 | Make_Or_Else (Loc, | |
10156 | Left_Opnd => | |
10157 | Make_Op_Lt (Loc, | |
3cb12758 | 10158 | Left_Opnd => |
10159 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10160 | Right_Opnd => | |
10161 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 10162 | |
10163 | Right_Opnd => | |
10164 | Make_Op_Gt (Loc, | |
3cb12758 | 10165 | Left_Opnd => |
10166 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10167 | Right_Opnd => | |
10168 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10169 | end Range_E_Cond; |
10170 | ||
10171 | ------------------------ | |
10172 | -- Range_Equal_E_Cond -- | |
10173 | ------------------------ | |
10174 | ||
10175 | function Range_Equal_E_Cond | |
10176 | (Exptyp : Entity_Id; | |
10177 | Typ : Entity_Id; | |
314a23b6 | 10178 | Indx : Nat) return Node_Id |
ee6ba406 | 10179 | is |
10180 | begin | |
10181 | return | |
10182 | Make_Or_Else (Loc, | |
10183 | Left_Opnd => | |
10184 | Make_Op_Ne (Loc, | |
3cb12758 | 10185 | Left_Opnd => |
10186 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10187 | Right_Opnd => | |
10188 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
10189 | ||
ee6ba406 | 10190 | Right_Opnd => |
10191 | Make_Op_Ne (Loc, | |
3cb12758 | 10192 | Left_Opnd => |
10193 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10194 | Right_Opnd => | |
10195 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10196 | end Range_Equal_E_Cond; |
10197 | ||
10198 | ------------------ | |
10199 | -- Range_N_Cond -- | |
10200 | ------------------ | |
10201 | ||
10202 | function Range_N_Cond | |
10203 | (Expr : Node_Id; | |
10204 | Typ : Entity_Id; | |
314a23b6 | 10205 | Indx : Nat) return Node_Id |
ee6ba406 | 10206 | is |
10207 | begin | |
10208 | return | |
10209 | Make_Or_Else (Loc, | |
10210 | Left_Opnd => | |
10211 | Make_Op_Lt (Loc, | |
3cb12758 | 10212 | Left_Opnd => |
10213 | Get_N_First (Expr, Indx), | |
10214 | Right_Opnd => | |
10215 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 10216 | |
10217 | Right_Opnd => | |
10218 | Make_Op_Gt (Loc, | |
3cb12758 | 10219 | Left_Opnd => |
10220 | Get_N_Last (Expr, Indx), | |
10221 | Right_Opnd => | |
10222 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10223 | end Range_N_Cond; |
10224 | ||
10225 | -- Start of processing for Selected_Range_Checks | |
10226 | ||
10227 | begin | |
f0d65dae | 10228 | -- Checks will be applied only when generating code. In GNATprove mode, |
10229 | -- we do not apply the checks, but we still call Selected_Range_Checks | |
10230 | -- to possibly issue errors on SPARK code when a run-time error can be | |
10231 | -- detected at compile time. | |
10232 | ||
10233 | if not Expander_Active and not GNATprove_Mode then | |
ee6ba406 | 10234 | return Ret_Result; |
10235 | end if; | |
10236 | ||
10237 | if Target_Typ = Any_Type | |
10238 | or else Target_Typ = Any_Composite | |
10239 | or else Raises_Constraint_Error (Ck_Node) | |
10240 | then | |
10241 | return Ret_Result; | |
10242 | end if; | |
10243 | ||
10244 | if No (Wnode) then | |
10245 | Wnode := Ck_Node; | |
10246 | end if; | |
10247 | ||
10248 | T_Typ := Target_Typ; | |
10249 | ||
10250 | if No (Source_Typ) then | |
10251 | S_Typ := Etype (Ck_Node); | |
10252 | else | |
10253 | S_Typ := Source_Typ; | |
10254 | end if; | |
10255 | ||
10256 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
10257 | return Ret_Result; | |
10258 | end if; | |
10259 | ||
10260 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
10261 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
10262 | -- in, and since Node can be an N_Range node, it might be invalid. | |
10263 | -- Should there be an assert check somewhere for taking the Etype of | |
10264 | -- an N_Range node ??? | |
10265 | ||
10266 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
10267 | S_Typ := Designated_Type (S_Typ); | |
10268 | T_Typ := Designated_Type (T_Typ); | |
10269 | Do_Access := True; | |
10270 | ||
2af58f67 | 10271 | -- A simple optimization for the null case |
ee6ba406 | 10272 | |
2af58f67 | 10273 | if Known_Null (Ck_Node) then |
ee6ba406 | 10274 | return Ret_Result; |
10275 | end if; | |
10276 | end if; | |
10277 | ||
10278 | -- For an N_Range Node, check for a null range and then if not | |
10279 | -- null generate a range check action. | |
10280 | ||
10281 | if Nkind (Ck_Node) = N_Range then | |
10282 | ||
10283 | -- There's no point in checking a range against itself | |
10284 | ||
10285 | if Ck_Node = Scalar_Range (T_Typ) then | |
10286 | return Ret_Result; | |
10287 | end if; | |
10288 | ||
10289 | declare | |
10290 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
10291 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 10292 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
10293 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 10294 | |
eefa141b | 10295 | LB : Node_Id := Low_Bound (Ck_Node); |
10296 | HB : Node_Id := High_Bound (Ck_Node); | |
3fabf0ca | 10297 | Known_LB : Boolean := False; |
10298 | Known_HB : Boolean := False; | |
eefa141b | 10299 | |
10300 | Null_Range : Boolean; | |
ee6ba406 | 10301 | Out_Of_Range_L : Boolean; |
10302 | Out_Of_Range_H : Boolean; | |
10303 | ||
10304 | begin | |
eefa141b | 10305 | -- Compute what is known at compile time |
10306 | ||
10307 | if Known_T_LB and Known_T_HB then | |
10308 | if Compile_Time_Known_Value (LB) then | |
10309 | Known_LB := True; | |
10310 | ||
10311 | -- There's no point in checking that a bound is within its | |
10312 | -- own range so pretend that it is known in this case. First | |
10313 | -- deal with low bound. | |
10314 | ||
10315 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
10316 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
10317 | then | |
10318 | LB := T_LB; | |
10319 | Known_LB := True; | |
eefa141b | 10320 | end if; |
10321 | ||
10322 | -- Likewise for the high bound | |
10323 | ||
10324 | if Compile_Time_Known_Value (HB) then | |
10325 | Known_HB := True; | |
10326 | ||
10327 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
10328 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
10329 | then | |
10330 | HB := T_HB; | |
10331 | Known_HB := True; | |
eefa141b | 10332 | end if; |
10333 | end if; | |
10334 | ||
10335 | -- Check for case where everything is static and we can do the | |
10336 | -- check at compile time. This is skipped if we have an access | |
10337 | -- type, since the access value may be null. | |
10338 | ||
10339 | -- ??? This code can be improved since you only need to know that | |
10340 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
10341 | -- compile time to emit pertinent messages. | |
10342 | ||
10343 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
10344 | and not Do_Access | |
ee6ba406 | 10345 | then |
10346 | -- Floating-point case | |
10347 | ||
10348 | if Is_Floating_Point_Type (S_Typ) then | |
10349 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
10350 | Out_Of_Range_L := | |
10351 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 10352 | or else |
ee6ba406 | 10353 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
10354 | ||
10355 | Out_Of_Range_H := | |
10356 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 10357 | or else |
ee6ba406 | 10358 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
10359 | ||
10360 | -- Fixed or discrete type case | |
10361 | ||
10362 | else | |
10363 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
10364 | Out_Of_Range_L := | |
10365 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 10366 | or else |
ee6ba406 | 10367 | (Expr_Value (LB) > Expr_Value (T_HB)); |
10368 | ||
10369 | Out_Of_Range_H := | |
10370 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 10371 | or else |
ee6ba406 | 10372 | (Expr_Value (HB) < Expr_Value (T_LB)); |
10373 | end if; | |
10374 | ||
10375 | if not Null_Range then | |
10376 | if Out_Of_Range_L then | |
10377 | if No (Warn_Node) then | |
10378 | Add_Check | |
10379 | (Compile_Time_Constraint_Error | |
10380 | (Low_Bound (Ck_Node), | |
cb97ae5c | 10381 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10382 | |
10383 | else | |
10384 | Add_Check | |
10385 | (Compile_Time_Constraint_Error | |
10386 | (Wnode, | |
cb97ae5c | 10387 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 10388 | end if; |
10389 | end if; | |
10390 | ||
10391 | if Out_Of_Range_H then | |
10392 | if No (Warn_Node) then | |
10393 | Add_Check | |
10394 | (Compile_Time_Constraint_Error | |
10395 | (High_Bound (Ck_Node), | |
cb97ae5c | 10396 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10397 | |
10398 | else | |
10399 | Add_Check | |
10400 | (Compile_Time_Constraint_Error | |
10401 | (Wnode, | |
cb97ae5c | 10402 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 10403 | end if; |
10404 | end if; | |
ee6ba406 | 10405 | end if; |
10406 | ||
10407 | else | |
10408 | declare | |
10409 | LB : Node_Id := Low_Bound (Ck_Node); | |
10410 | HB : Node_Id := High_Bound (Ck_Node); | |
10411 | ||
10412 | begin | |
feff2f05 | 10413 | -- If either bound is a discriminant and we are within the |
10414 | -- record declaration, it is a use of the discriminant in a | |
10415 | -- constraint of a component, and nothing can be checked | |
10416 | -- here. The check will be emitted within the init proc. | |
10417 | -- Before then, the discriminal has no real meaning. | |
10418 | -- Similarly, if the entity is a discriminal, there is no | |
10419 | -- check to perform yet. | |
10420 | ||
10421 | -- The same holds within a discriminated synchronized type, | |
10422 | -- where the discriminant may constrain a component or an | |
10423 | -- entry family. | |
ee6ba406 | 10424 | |
10425 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 10426 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 10427 | then |
0577b0b1 | 10428 | if Current_Scope = Scope (Entity (LB)) |
10429 | or else Is_Concurrent_Type (Current_Scope) | |
10430 | or else Ekind (Entity (LB)) /= E_Discriminant | |
10431 | then | |
ee6ba406 | 10432 | return Ret_Result; |
10433 | else | |
10434 | LB := | |
10435 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
10436 | end if; | |
10437 | end if; | |
10438 | ||
10439 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 10440 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 10441 | then |
0577b0b1 | 10442 | if Current_Scope = Scope (Entity (HB)) |
10443 | or else Is_Concurrent_Type (Current_Scope) | |
10444 | or else Ekind (Entity (HB)) /= E_Discriminant | |
10445 | then | |
ee6ba406 | 10446 | return Ret_Result; |
10447 | else | |
10448 | HB := | |
10449 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
10450 | end if; | |
10451 | end if; | |
10452 | ||
10453 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
10454 | Set_Paren_Count (Cond, 1); | |
10455 | ||
10456 | Cond := | |
10457 | Make_And_Then (Loc, | |
10458 | Left_Opnd => | |
10459 | Make_Op_Ge (Loc, | |
86d32751 | 10460 | Left_Opnd => |
10461 | Convert_To (Base_Type (Etype (HB)), | |
10462 | Duplicate_Subexpr_No_Checks (HB)), | |
10463 | Right_Opnd => | |
10464 | Convert_To (Base_Type (Etype (LB)), | |
10465 | Duplicate_Subexpr_No_Checks (LB))), | |
ee6ba406 | 10466 | Right_Opnd => Cond); |
10467 | end; | |
ee6ba406 | 10468 | end if; |
10469 | end; | |
10470 | ||
10471 | elsif Is_Scalar_Type (S_Typ) then | |
10472 | ||
10473 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
10474 | -- except the above simply sets a flag in the node and lets | |
10475 | -- gigi generate the check base on the Etype of the expression. | |
10476 | -- Sometimes, however we want to do a dynamic check against an | |
10477 | -- arbitrary target type, so we do that here. | |
10478 | ||
10479 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
10480 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
10481 | ||
10482 | -- For literals, we can tell if the constraint error will be | |
10483 | -- raised at compile time, so we never need a dynamic check, but | |
10484 | -- if the exception will be raised, then post the usual warning, | |
10485 | -- and replace the literal with a raise constraint error | |
10486 | -- expression. As usual, skip this for access types | |
10487 | ||
20cf157b | 10488 | elsif Compile_Time_Known_Value (Ck_Node) and then not Do_Access then |
ee6ba406 | 10489 | declare |
10490 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
10491 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
10492 | ||
10493 | Out_Of_Range : Boolean; | |
10494 | Static_Bounds : constant Boolean := | |
b6341c67 | 10495 | Compile_Time_Known_Value (LB) |
10496 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 10497 | |
10498 | begin | |
10499 | -- Following range tests should use Sem_Eval routine ??? | |
10500 | ||
10501 | if Static_Bounds then | |
10502 | if Is_Floating_Point_Type (S_Typ) then | |
10503 | Out_Of_Range := | |
10504 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
10505 | or else | |
10506 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
10507 | ||
eefa141b | 10508 | -- Fixed or discrete type |
10509 | ||
10510 | else | |
ee6ba406 | 10511 | Out_Of_Range := |
10512 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
10513 | or else | |
10514 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
10515 | end if; | |
10516 | ||
eefa141b | 10517 | -- Bounds of the type are static and the literal is out of |
10518 | -- range so output a warning message. | |
ee6ba406 | 10519 | |
10520 | if Out_Of_Range then | |
10521 | if No (Warn_Node) then | |
10522 | Add_Check | |
10523 | (Compile_Time_Constraint_Error | |
10524 | (Ck_Node, | |
cb97ae5c | 10525 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10526 | |
10527 | else | |
10528 | Add_Check | |
10529 | (Compile_Time_Constraint_Error | |
10530 | (Wnode, | |
cb97ae5c | 10531 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10532 | end if; |
10533 | end if; | |
10534 | ||
10535 | else | |
10536 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
10537 | end if; | |
10538 | end; | |
10539 | ||
10540 | -- Here for the case of a non-static expression, we need a runtime | |
10541 | -- check unless the source type range is guaranteed to be in the | |
10542 | -- range of the target type. | |
10543 | ||
10544 | else | |
7a1dabb3 | 10545 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 10546 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
10547 | end if; | |
10548 | end if; | |
10549 | end if; | |
10550 | ||
10551 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
10552 | if Is_Constrained (T_Typ) then | |
10553 | ||
10554 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
10555 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
10556 | ||
10557 | if Is_Access_Type (Exptyp) then | |
10558 | Exptyp := Designated_Type (Exptyp); | |
10559 | end if; | |
10560 | ||
10561 | -- String_Literal case. This needs to be handled specially be- | |
10562 | -- cause no index types are available for string literals. The | |
10563 | -- condition is simply: | |
10564 | ||
10565 | -- T_Typ'Length = string-literal-length | |
10566 | ||
10567 | if Nkind (Expr_Actual) = N_String_Literal then | |
10568 | null; | |
10569 | ||
10570 | -- General array case. Here we have a usable actual subtype for | |
10571 | -- the expression, and the condition is built from the two types | |
10572 | ||
10573 | -- T_Typ'First < Exptyp'First or else | |
10574 | -- T_Typ'Last > Exptyp'Last or else | |
10575 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
10576 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
10577 | -- ... | |
10578 | ||
10579 | elsif Is_Constrained (Exptyp) then | |
10580 | declare | |
9dfe12ae | 10581 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
10582 | ||
ee6ba406 | 10583 | L_Index : Node_Id; |
10584 | R_Index : Node_Id; | |
ee6ba406 | 10585 | |
10586 | begin | |
10587 | L_Index := First_Index (T_Typ); | |
10588 | R_Index := First_Index (Exptyp); | |
10589 | ||
10590 | for Indx in 1 .. Ndims loop | |
10591 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 10592 | or else |
10593 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 10594 | then |
ee6ba406 | 10595 | -- Deal with compile time length check. Note that we |
10596 | -- skip this in the access case, because the access | |
10597 | -- value may be null, so we cannot know statically. | |
10598 | ||
10599 | if not | |
10600 | Subtypes_Statically_Match | |
10601 | (Etype (L_Index), Etype (R_Index)) | |
10602 | then | |
10603 | -- If the target type is constrained then we | |
10604 | -- have to check for exact equality of bounds | |
10605 | -- (required for qualified expressions). | |
10606 | ||
10607 | if Is_Constrained (T_Typ) then | |
10608 | Evolve_Or_Else | |
10609 | (Cond, | |
10610 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 10611 | else |
10612 | Evolve_Or_Else | |
10613 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
10614 | end if; | |
10615 | end if; | |
10616 | ||
10617 | Next (L_Index); | |
10618 | Next (R_Index); | |
ee6ba406 | 10619 | end if; |
10620 | end loop; | |
10621 | end; | |
10622 | ||
10623 | -- Handle cases where we do not get a usable actual subtype that | |
10624 | -- is constrained. This happens for example in the function call | |
10625 | -- and explicit dereference cases. In these cases, we have to get | |
10626 | -- the length or range from the expression itself, making sure we | |
10627 | -- do not evaluate it more than once. | |
10628 | ||
10629 | -- Here Ck_Node is the original expression, or more properly the | |
10630 | -- result of applying Duplicate_Expr to the original tree, | |
10631 | -- forcing the result to be a name. | |
10632 | ||
10633 | else | |
10634 | declare | |
9dfe12ae | 10635 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 10636 | |
10637 | begin | |
10638 | -- Build the condition for the explicit dereference case | |
10639 | ||
10640 | for Indx in 1 .. Ndims loop | |
10641 | Evolve_Or_Else | |
10642 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
10643 | end loop; | |
10644 | end; | |
ee6ba406 | 10645 | end if; |
10646 | ||
10647 | else | |
feff2f05 | 10648 | -- For a conversion to an unconstrained array type, generate an |
10649 | -- Action to check that the bounds of the source value are within | |
10650 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
10651 | -- check is needed for a conversion to an access to unconstrained | |
10652 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
10653 | -- of the two access types to statically match. | |
10654 | ||
10655 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
10656 | and then not Do_Access | |
10657 | then | |
ee6ba406 | 10658 | declare |
10659 | Opnd_Index : Node_Id; | |
10660 | Targ_Index : Node_Id; | |
00c403ee | 10661 | Opnd_Range : Node_Id; |
ee6ba406 | 10662 | |
10663 | begin | |
feff2f05 | 10664 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 10665 | Targ_Index := First_Index (T_Typ); |
00c403ee | 10666 | while Present (Opnd_Index) loop |
10667 | ||
10668 | -- If the index is a range, use its bounds. If it is an | |
10669 | -- entity (as will be the case if it is a named subtype | |
10670 | -- or an itype created for a slice) retrieve its range. | |
10671 | ||
10672 | if Is_Entity_Name (Opnd_Index) | |
10673 | and then Is_Type (Entity (Opnd_Index)) | |
10674 | then | |
10675 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
10676 | else | |
10677 | Opnd_Range := Opnd_Index; | |
10678 | end if; | |
10679 | ||
10680 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 10681 | if Is_In_Range |
10682 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
10683 | Assume_Valid => True) | |
ee6ba406 | 10684 | and then |
10685 | Is_In_Range | |
9c486805 | 10686 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
10687 | Assume_Valid => True) | |
ee6ba406 | 10688 | then |
10689 | null; | |
10690 | ||
feff2f05 | 10691 | -- If null range, no check needed |
f2a06be9 | 10692 | |
9dfe12ae | 10693 | elsif |
00c403ee | 10694 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 10695 | and then |
00c403ee | 10696 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 10697 | and then |
00c403ee | 10698 | Expr_Value (High_Bound (Opnd_Range)) < |
10699 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 10700 | then |
10701 | null; | |
10702 | ||
ee6ba406 | 10703 | elsif Is_Out_Of_Range |
9c486805 | 10704 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
10705 | Assume_Valid => True) | |
ee6ba406 | 10706 | or else |
10707 | Is_Out_Of_Range | |
9c486805 | 10708 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
10709 | Assume_Valid => True) | |
ee6ba406 | 10710 | then |
10711 | Add_Check | |
10712 | (Compile_Time_Constraint_Error | |
cb97ae5c | 10713 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 10714 | |
10715 | else | |
10716 | Evolve_Or_Else | |
10717 | (Cond, | |
10718 | Discrete_Range_Cond | |
00c403ee | 10719 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 10720 | end if; |
10721 | end if; | |
10722 | ||
10723 | Next_Index (Opnd_Index); | |
10724 | Next_Index (Targ_Index); | |
10725 | end loop; | |
10726 | end; | |
10727 | end if; | |
10728 | end if; | |
10729 | end if; | |
10730 | ||
10731 | -- Construct the test and insert into the tree | |
10732 | ||
10733 | if Present (Cond) then | |
10734 | if Do_Access then | |
10735 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
10736 | end if; | |
10737 | ||
f15731c4 | 10738 | Add_Check |
10739 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 10740 | Condition => Cond, |
22d3a5a3 | 10741 | Reason => CE_Range_Check_Failed)); |
ee6ba406 | 10742 | end if; |
10743 | ||
10744 | return Ret_Result; | |
ee6ba406 | 10745 | end Selected_Range_Checks; |
10746 | ||
10747 | ------------------------------- | |
10748 | -- Storage_Checks_Suppressed -- | |
10749 | ------------------------------- | |
10750 | ||
10751 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10752 | begin | |
9dfe12ae | 10753 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
10754 | return Is_Check_Suppressed (E, Storage_Check); | |
10755 | else | |
fafc6b97 | 10756 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 10757 | end if; |
ee6ba406 | 10758 | end Storage_Checks_Suppressed; |
10759 | ||
10760 | --------------------------- | |
10761 | -- Tag_Checks_Suppressed -- | |
10762 | --------------------------- | |
10763 | ||
10764 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10765 | begin | |
89f1e35c | 10766 | if Present (E) |
10767 | and then Checks_May_Be_Suppressed (E) | |
10768 | then | |
10769 | return Is_Check_Suppressed (E, Tag_Check); | |
20cf157b | 10770 | else |
10771 | return Scope_Suppress.Suppress (Tag_Check); | |
9dfe12ae | 10772 | end if; |
ee6ba406 | 10773 | end Tag_Checks_Suppressed; |
10774 | ||
7b8fa048 | 10775 | --------------------------------------- |
10776 | -- Validate_Alignment_Check_Warnings -- | |
10777 | --------------------------------------- | |
10778 | ||
10779 | procedure Validate_Alignment_Check_Warnings is | |
10780 | begin | |
10781 | for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop | |
10782 | declare | |
10783 | AWR : Alignment_Warnings_Record | |
10784 | renames Alignment_Warnings.Table (J); | |
10785 | begin | |
10786 | if Known_Alignment (AWR.E) | |
10787 | and then AWR.A mod Alignment (AWR.E) = 0 | |
10788 | then | |
10789 | Delete_Warning_And_Continuations (AWR.W); | |
10790 | end if; | |
10791 | end; | |
10792 | end loop; | |
10793 | end Validate_Alignment_Check_Warnings; | |
10794 | ||
0577b0b1 | 10795 | -------------------------- |
10796 | -- Validity_Check_Range -- | |
10797 | -------------------------- | |
10798 | ||
aaec8d13 | 10799 | procedure Validity_Check_Range |
10800 | (N : Node_Id; | |
10801 | Related_Id : Entity_Id := Empty) | |
10802 | is | |
0577b0b1 | 10803 | begin |
10804 | if Validity_Checks_On and Validity_Check_Operands then | |
10805 | if Nkind (N) = N_Range then | |
aaec8d13 | 10806 | Ensure_Valid |
10807 | (Expr => Low_Bound (N), | |
10808 | Related_Id => Related_Id, | |
10809 | Is_Low_Bound => True); | |
10810 | ||
10811 | Ensure_Valid | |
10812 | (Expr => High_Bound (N), | |
10813 | Related_Id => Related_Id, | |
10814 | Is_High_Bound => True); | |
0577b0b1 | 10815 | end if; |
10816 | end if; | |
10817 | end Validity_Check_Range; | |
10818 | ||
10819 | -------------------------------- | |
10820 | -- Validity_Checks_Suppressed -- | |
10821 | -------------------------------- | |
10822 | ||
10823 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10824 | begin | |
10825 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
10826 | return Is_Check_Suppressed (E, Validity_Check); | |
10827 | else | |
fafc6b97 | 10828 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 10829 | end if; |
10830 | end Validity_Checks_Suppressed; | |
10831 | ||
ee6ba406 | 10832 | end Checks; |