]>
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 | -- -- | |
e9c75a1a | 9 | -- Copyright (C) 1992-2019, 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; |
becb6111 | 53 | with Sem_Mech; use Sem_Mech; |
ee6ba406 | 54 | with Sem_Res; use Sem_Res; |
55 | with Sem_Util; use Sem_Util; | |
56 | with Sem_Warn; use Sem_Warn; | |
57 | with Sinfo; use Sinfo; | |
9dfe12ae | 58 | with Sinput; use Sinput; |
ee6ba406 | 59 | with Snames; use Snames; |
9dfe12ae | 60 | with Sprint; use Sprint; |
ee6ba406 | 61 | with Stand; use Stand; |
bb569db0 | 62 | with Stringt; use Stringt; |
f15731c4 | 63 | with Targparm; use Targparm; |
ee6ba406 | 64 | with Tbuild; use Tbuild; |
65 | with Ttypes; use Ttypes; | |
ee6ba406 | 66 | with Validsw; use Validsw; |
67 | ||
68 | package body Checks is | |
69 | ||
70 | -- General note: many of these routines are concerned with generating | |
71 | -- checking code to make sure that constraint error is raised at runtime. | |
72 | -- Clearly this code is only needed if the expander is active, since | |
73 | -- otherwise we will not be generating code or going into the runtime | |
74 | -- execution anyway. | |
75 | ||
76 | -- We therefore disconnect most of these checks if the expander is | |
77 | -- inactive. This has the additional benefit that we do not need to | |
78 | -- worry about the tree being messed up by previous errors (since errors | |
79 | -- turn off expansion anyway). | |
80 | ||
81 | -- There are a few exceptions to the above rule. For instance routines | |
82 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
83 | -- safely called even when the Expander is inactive (but Errors_Detected | |
84 | -- is 0). The benefit of executing this code when expansion is off, is | |
85 | -- the ability to emit constraint error warning for static expressions | |
86 | -- even when we are not generating code. | |
87 | ||
20cf157b | 88 | -- The above is modified in gnatprove mode to ensure that proper check |
89 | -- flags are always placed, even if expansion is off. | |
90 | ||
9dfe12ae | 91 | ------------------------------------- |
92 | -- Suppression of Redundant Checks -- | |
93 | ------------------------------------- | |
94 | ||
95 | -- This unit implements a limited circuit for removal of redundant | |
96 | -- checks. The processing is based on a tracing of simple sequential | |
97 | -- flow. For any sequence of statements, we save expressions that are | |
98 | -- marked to be checked, and then if the same expression appears later | |
99 | -- with the same check, then under certain circumstances, the second | |
100 | -- check can be suppressed. | |
101 | ||
102 | -- Basically, we can suppress the check if we know for certain that | |
103 | -- the previous expression has been elaborated (together with its | |
104 | -- check), and we know that the exception frame is the same, and that | |
105 | -- nothing has happened to change the result of the exception. | |
106 | ||
107 | -- Let us examine each of these three conditions in turn to describe | |
108 | -- how we ensure that this condition is met. | |
109 | ||
110 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 111 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 112 | -- Conditional_Statements_Begin at the start of any statement sequence |
113 | -- and Conditional_Statements_End at the end. The End call causes all | |
114 | -- checks remembered since the Begin call to be discarded. This does | |
115 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
116 | -- no exception handlers. But the important thing is to be conservative. | |
117 | -- The other protection is that all checks are discarded if a label | |
118 | -- is encountered, since then the assumption of sequential execution | |
119 | -- is violated, and we don't know enough about the flow. | |
120 | ||
121 | -- Second, we need to know that the exception frame is the same. We | |
122 | -- do this by killing all remembered checks when we enter a new frame. | |
123 | -- Again, that's over-conservative, but generally the cases we can help | |
124 | -- with are pretty local anyway (like the body of a loop for example). | |
125 | ||
126 | -- Third, we must be sure to forget any checks which are no longer valid. | |
127 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
128 | -- used to note any changes to local variables. We only attempt to deal | |
129 | -- with checks involving local variables, so we do not need to worry | |
130 | -- about global variables. Second, a call to any non-global procedure | |
131 | -- causes us to abandon all stored checks, since such a all may affect | |
132 | -- the values of any local variables. | |
133 | ||
134 | -- The following define the data structures used to deal with remembering | |
135 | -- checks so that redundant checks can be eliminated as described above. | |
136 | ||
137 | -- Right now, the only expressions that we deal with are of the form of | |
138 | -- simple local objects (either declared locally, or IN parameters) or | |
139 | -- such objects plus/minus a compile time known constant. We can do | |
140 | -- more later on if it seems worthwhile, but this catches many simple | |
141 | -- cases in practice. | |
142 | ||
143 | -- The following record type reflects a single saved check. An entry | |
144 | -- is made in the stack of saved checks if and only if the expression | |
145 | -- has been elaborated with the indicated checks. | |
146 | ||
147 | type Saved_Check is record | |
148 | Killed : Boolean; | |
149 | -- Set True if entry is killed by Kill_Checks | |
150 | ||
151 | Entity : Entity_Id; | |
152 | -- The entity involved in the expression that is checked | |
153 | ||
154 | Offset : Uint; | |
155 | -- A compile time value indicating the result of adding or | |
156 | -- subtracting a compile time value. This value is to be | |
157 | -- added to the value of the Entity. A value of zero is | |
158 | -- used for the case of a simple entity reference. | |
159 | ||
160 | Check_Type : Character; | |
161 | -- This is set to 'R' for a range check (in which case Target_Type | |
162 | -- is set to the target type for the range check) or to 'O' for an | |
163 | -- overflow check (in which case Target_Type is set to Empty). | |
164 | ||
165 | Target_Type : Entity_Id; | |
166 | -- Used only if Do_Range_Check is set. Records the target type for | |
167 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 168 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 169 | -- range that is smaller or equal to the stored target type of a |
170 | -- saved check). | |
171 | end record; | |
172 | ||
173 | -- The following table keeps track of saved checks. Rather than use an | |
bbbfe30c | 174 | -- extensible table, we just use a table of fixed size, and we discard |
9dfe12ae | 175 | -- any saved checks that do not fit. That's very unlikely to happen and |
176 | -- this is only an optimization in any case. | |
177 | ||
178 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
179 | -- Array of saved checks | |
180 | ||
181 | Num_Saved_Checks : Nat := 0; | |
182 | -- Number of saved checks | |
183 | ||
184 | -- The following stack keeps track of statement ranges. It is treated | |
185 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
186 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
187 | -- at the time of the call. Then when Conditional_Statements_End is | |
188 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
189 | ||
190 | -- Note: again, this is a fixed length stack with a size that should | |
191 | -- always be fine. If the value of the stack pointer goes above the | |
192 | -- limit, then we just forget all saved checks. | |
193 | ||
194 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
195 | Saved_Checks_TOS : Nat := 0; | |
196 | ||
197 | ----------------------- | |
198 | -- Local Subprograms -- | |
199 | ----------------------- | |
ee6ba406 | 200 | |
0df9d43f | 201 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 202 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 203 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 204 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
205 | -- signed integer arithmetic operator (but not an if or case expression). | |
206 | -- It is also called for types other than signed integers. | |
3cce7f32 | 207 | |
208 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
209 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 210 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
211 | -- arithmetic op (which includes the case of if and case expressions). Note | |
212 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
213 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 214 | |
2fe22c69 | 215 | procedure Apply_Division_Check |
216 | (N : Node_Id; | |
217 | Rlo : Uint; | |
218 | Rhi : Uint; | |
219 | ROK : Boolean); | |
220 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
221 | -- division checks as required if the Do_Division_Check flag is set. | |
222 | -- Rlo and Rhi give the possible range of the right operand, these values | |
223 | -- can be referenced and trusted only if ROK is set True. | |
224 | ||
225 | procedure Apply_Float_Conversion_Check | |
226 | (Ck_Node : Node_Id; | |
227 | Target_Typ : Entity_Id); | |
228 | -- The checks on a conversion from a floating-point type to an integer | |
229 | -- type are delicate. They have to be performed before conversion, they | |
230 | -- have to raise an exception when the operand is a NaN, and rounding must | |
231 | -- be taken into account to determine the safe bounds of the operand. | |
232 | ||
ee6ba406 | 233 | procedure Apply_Selected_Length_Checks |
234 | (Ck_Node : Node_Id; | |
235 | Target_Typ : Entity_Id; | |
236 | Source_Typ : Entity_Id; | |
237 | Do_Static : Boolean); | |
238 | -- This is the subprogram that does all the work for Apply_Length_Check | |
239 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
240 | -- described for the above routines. The Do_Static flag indicates that | |
241 | -- only a static check is to be done. | |
242 | ||
243 | procedure Apply_Selected_Range_Checks | |
244 | (Ck_Node : Node_Id; | |
245 | Target_Typ : Entity_Id; | |
246 | Source_Typ : Entity_Id; | |
247 | Do_Static : Boolean); | |
248 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
249 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
250 | -- routine. The Do_Static flag indicates that only a static check is | |
251 | -- to be done. | |
252 | ||
2af58f67 | 253 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 254 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
255 | -- This function is used to see if an access or division by zero check is | |
256 | -- needed. The check is to be applied to a single variable appearing in the | |
257 | -- source, and N is the node for the reference. If N is not of this form, | |
258 | -- True is returned with no further processing. If N is of the right form, | |
259 | -- then further processing determines if the given Check is needed. | |
260 | -- | |
261 | -- The particular circuit is to see if we have the case of a check that is | |
262 | -- not needed because it appears in the right operand of a short circuited | |
263 | -- conditional where the left operand guards the check. For example: | |
264 | -- | |
265 | -- if Var = 0 or else Q / Var > 12 then | |
266 | -- ... | |
267 | -- end if; | |
268 | -- | |
269 | -- In this example, the division check is not required. At the same time | |
270 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
271 | -- such as: | |
272 | -- | |
273 | -- if Var = 0 or Q / Var > 12 then | |
274 | -- ... | |
275 | -- end if; | |
276 | ||
9dfe12ae | 277 | procedure Find_Check |
278 | (Expr : Node_Id; | |
279 | Check_Type : Character; | |
280 | Target_Type : Entity_Id; | |
281 | Entry_OK : out Boolean; | |
282 | Check_Num : out Nat; | |
283 | Ent : out Entity_Id; | |
284 | Ofs : out Uint); | |
285 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
286 | -- to see if a check is of the form for optimization, and if so, to see | |
287 | -- if it has already been performed. Expr is the expression to check, | |
288 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
289 | -- Target_Type is the target type for a range check, and Empty for an | |
290 | -- overflow check. If the entry is not of the form for optimization, | |
291 | -- then Entry_OK is set to False, and the remaining out parameters | |
292 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
293 | -- entity and offset from the expression. Check_Num is the number of | |
294 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
295 | -- is located. | |
296 | ||
ee6ba406 | 297 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
298 | -- If a discriminal is used in constraining a prival, Return reference | |
299 | -- to the discriminal of the protected body (which renames the parameter | |
300 | -- of the enclosing protected operation). This clumsy transformation is | |
301 | -- needed because privals are created too late and their actual subtypes | |
302 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 303 | -- This function is called whenever the bound is an entity and the scope |
304 | -- indicates a protected operation. If the bound is an in-parameter of | |
305 | -- a protected operation that is not a prival, the function returns the | |
306 | -- bound itself. | |
ee6ba406 | 307 | -- To be cleaned up??? |
308 | ||
309 | function Guard_Access | |
310 | (Cond : Node_Id; | |
311 | Loc : Source_Ptr; | |
314a23b6 | 312 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 313 | -- In the access type case, guard the test with a test to ensure |
314 | -- that the access value is non-null, since the checks do not | |
315 | -- not apply to null access values. | |
316 | ||
317 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
318 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
319 | -- Constraint_Error node. | |
320 | ||
3cce7f32 | 321 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
322 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 323 | -- integer operands. This includes unary and binary operators, and also |
324 | -- if and case expression nodes where the dependent expressions are of | |
325 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 326 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 327 | |
0577b0b1 | 328 | function Range_Or_Validity_Checks_Suppressed |
329 | (Expr : Node_Id) return Boolean; | |
330 | -- Returns True if either range or validity checks or both are suppressed | |
331 | -- for the type of the given expression, or, if the expression is the name | |
332 | -- of an entity, if these checks are suppressed for the entity. | |
333 | ||
ee6ba406 | 334 | function Selected_Length_Checks |
335 | (Ck_Node : Node_Id; | |
336 | Target_Typ : Entity_Id; | |
337 | Source_Typ : Entity_Id; | |
314a23b6 | 338 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 339 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
340 | -- anything, just returns a list of nodes as described in the spec of | |
341 | -- this package for the Range_Check function. | |
18cb6d78 | 342 | -- ??? In fact it does construct the test and insert it into the tree, |
343 | -- and insert actions in various ways (calling Insert_Action directly | |
344 | -- in particular) so we do not call it in GNATprove mode, contrary to | |
345 | -- Selected_Range_Checks. | |
ee6ba406 | 346 | |
347 | function Selected_Range_Checks | |
348 | (Ck_Node : Node_Id; | |
349 | Target_Typ : Entity_Id; | |
350 | Source_Typ : Entity_Id; | |
314a23b6 | 351 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 352 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
353 | -- just returns a list of nodes as described in the spec of this package | |
354 | -- for the Range_Check function. | |
355 | ||
356 | ------------------------------ | |
357 | -- Access_Checks_Suppressed -- | |
358 | ------------------------------ | |
359 | ||
360 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
361 | begin | |
9dfe12ae | 362 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
363 | return Is_Check_Suppressed (E, Access_Check); | |
364 | else | |
fafc6b97 | 365 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 366 | end if; |
ee6ba406 | 367 | end Access_Checks_Suppressed; |
368 | ||
369 | ------------------------------------- | |
370 | -- Accessibility_Checks_Suppressed -- | |
371 | ------------------------------------- | |
372 | ||
373 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
374 | begin | |
9dfe12ae | 375 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
376 | return Is_Check_Suppressed (E, Accessibility_Check); | |
377 | else | |
fafc6b97 | 378 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 379 | end if; |
ee6ba406 | 380 | end Accessibility_Checks_Suppressed; |
381 | ||
00c403ee | 382 | ----------------------------- |
383 | -- Activate_Division_Check -- | |
384 | ----------------------------- | |
385 | ||
386 | procedure Activate_Division_Check (N : Node_Id) is | |
387 | begin | |
388 | Set_Do_Division_Check (N, True); | |
389 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
390 | end Activate_Division_Check; | |
391 | ||
392 | ----------------------------- | |
393 | -- Activate_Overflow_Check -- | |
394 | ----------------------------- | |
395 | ||
396 | procedure Activate_Overflow_Check (N : Node_Id) is | |
c8e92b5f | 397 | Typ : constant Entity_Id := Etype (N); |
398 | ||
00c403ee | 399 | begin |
c8e92b5f | 400 | -- Floating-point case. If Etype is not set (this can happen when we |
401 | -- activate a check on a node that has not yet been analyzed), then | |
402 | -- we assume we do not have a floating-point type (as per our spec). | |
403 | ||
404 | if Present (Typ) and then Is_Floating_Point_Type (Typ) then | |
405 | ||
406 | -- Ignore call if we have no automatic overflow checks on the target | |
407 | -- and Check_Float_Overflow mode is not set. These are the cases in | |
408 | -- which we expect to generate infinities and NaN's with no check. | |
409 | ||
410 | if not (Machine_Overflows_On_Target or Check_Float_Overflow) then | |
411 | return; | |
412 | ||
413 | -- Ignore for unary operations ("+", "-", abs) since these can never | |
414 | -- result in overflow for floating-point cases. | |
b8446e0d | 415 | |
c8e92b5f | 416 | elsif Nkind (N) in N_Unary_Op then |
417 | return; | |
418 | ||
419 | -- Otherwise we will set the flag | |
420 | ||
421 | else | |
422 | null; | |
423 | end if; | |
424 | ||
425 | -- Discrete case | |
426 | ||
427 | else | |
428 | -- Nothing to do for Rem/Mod/Plus (overflow not possible, the check | |
429 | -- for zero-divide is a divide check, not an overflow check). | |
b8446e0d | 430 | |
c8e92b5f | 431 | if Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then |
432 | return; | |
433 | end if; | |
b8446e0d | 434 | end if; |
435 | ||
c8e92b5f | 436 | -- Fall through for cases where we do set the flag |
b8446e0d | 437 | |
438 | Set_Do_Overflow_Check (N, True); | |
439 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
00c403ee | 440 | end Activate_Overflow_Check; |
441 | ||
442 | -------------------------- | |
443 | -- Activate_Range_Check -- | |
444 | -------------------------- | |
445 | ||
446 | procedure Activate_Range_Check (N : Node_Id) is | |
447 | begin | |
448 | Set_Do_Range_Check (N, True); | |
449 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
450 | end Activate_Range_Check; | |
451 | ||
0577b0b1 | 452 | --------------------------------- |
453 | -- Alignment_Checks_Suppressed -- | |
454 | --------------------------------- | |
455 | ||
456 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
457 | begin | |
458 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
459 | return Is_Check_Suppressed (E, Alignment_Check); | |
460 | else | |
fafc6b97 | 461 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 462 | end if; |
463 | end Alignment_Checks_Suppressed; | |
464 | ||
2d70530c | 465 | ---------------------------------- |
466 | -- Allocation_Checks_Suppressed -- | |
467 | ---------------------------------- | |
468 | ||
fa771c05 | 469 | -- Note: at the current time there are no calls to this function, because |
470 | -- the relevant check is in the run-time, so it is not a check that the | |
471 | -- compiler can suppress anyway, but we still have to recognize the check | |
472 | -- name Allocation_Check since it is part of the standard. | |
473 | ||
2d70530c | 474 | function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
475 | begin | |
476 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
477 | return Is_Check_Suppressed (E, Allocation_Check); | |
478 | else | |
479 | return Scope_Suppress.Suppress (Allocation_Check); | |
480 | end if; | |
481 | end Allocation_Checks_Suppressed; | |
482 | ||
ee6ba406 | 483 | ------------------------- |
484 | -- Append_Range_Checks -- | |
485 | ------------------------- | |
486 | ||
487 | procedure Append_Range_Checks | |
488 | (Checks : Check_Result; | |
489 | Stmts : List_Id; | |
490 | Suppress_Typ : Entity_Id; | |
491 | Static_Sloc : Source_Ptr; | |
492 | Flag_Node : Node_Id) | |
493 | is | |
2b4f2458 | 494 | Checks_On : constant Boolean := |
495 | not Index_Checks_Suppressed (Suppress_Typ) | |
496 | or else | |
497 | not Range_Checks_Suppressed (Suppress_Typ); | |
498 | ||
9dfe12ae | 499 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
500 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
501 | ||
ee6ba406 | 502 | begin |
2b4f2458 | 503 | -- For now we just return if Checks_On is false, however this should be |
504 | -- enhanced to check for an always True value in the condition and to | |
505 | -- generate a compilation warning??? | |
ee6ba406 | 506 | |
507 | if not Checks_On then | |
508 | return; | |
509 | end if; | |
510 | ||
511 | for J in 1 .. 2 loop | |
512 | exit when No (Checks (J)); | |
513 | ||
514 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
515 | and then Present (Condition (Checks (J))) | |
516 | then | |
517 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
518 | Append_To (Stmts, Checks (J)); | |
519 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
520 | end if; | |
521 | ||
522 | else | |
523 | Append_To | |
f15731c4 | 524 | (Stmts, |
525 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
526 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 527 | end if; |
528 | end loop; | |
529 | end Append_Range_Checks; | |
530 | ||
531 | ------------------------ | |
532 | -- Apply_Access_Check -- | |
533 | ------------------------ | |
534 | ||
535 | procedure Apply_Access_Check (N : Node_Id) is | |
536 | P : constant Node_Id := Prefix (N); | |
537 | ||
538 | begin | |
13dbf220 | 539 | -- We do not need checks if we are not generating code (i.e. the |
540 | -- expander is not active). This is not just an optimization, there | |
541 | -- are cases (e.g. with pragma Debug) where generating the checks | |
542 | -- can cause real trouble). | |
284faf8b | 543 | |
a33565dd | 544 | if not Expander_Active then |
13dbf220 | 545 | return; |
9dfe12ae | 546 | end if; |
ee6ba406 | 547 | |
84d0d4a5 | 548 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 549 | |
84d0d4a5 | 550 | if not Check_Needed (P, Access_Check) then |
551 | return; | |
ee6ba406 | 552 | end if; |
9dfe12ae | 553 | |
cc60bd16 | 554 | -- No check if accessing the Offset_To_Top component of a dispatch |
555 | -- table. They are safe by construction. | |
556 | ||
040277b1 | 557 | if Tagged_Type_Expansion |
558 | and then Present (Etype (P)) | |
cc60bd16 | 559 | and then RTU_Loaded (Ada_Tags) |
560 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
561 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
562 | then | |
563 | return; | |
564 | end if; | |
565 | ||
84d0d4a5 | 566 | -- Otherwise go ahead and install the check |
9dfe12ae | 567 | |
fa7497e8 | 568 | Install_Null_Excluding_Check (P); |
ee6ba406 | 569 | end Apply_Access_Check; |
570 | ||
571 | ------------------------------- | |
572 | -- Apply_Accessibility_Check -- | |
573 | ------------------------------- | |
574 | ||
55dc6dc2 | 575 | procedure Apply_Accessibility_Check |
576 | (N : Node_Id; | |
577 | Typ : Entity_Id; | |
578 | Insert_Node : Node_Id) | |
579 | is | |
b1118eb8 | 580 | Check_Cond : Node_Id; |
ee6ba406 | 581 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 582 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 583 | Param_Level : Node_Id; |
584 | Type_Level : Node_Id; | |
585 | ||
586 | begin | |
47d210a3 | 587 | if Ada_Version >= Ada_2012 |
588 | and then not Present (Param_Ent) | |
589 | and then Is_Entity_Name (N) | |
590 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
591 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
592 | then | |
593 | Param_Ent := Entity (N); | |
594 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 595 | |
47d210a3 | 596 | -- Renamed_Object must return an Entity_Name here |
597 | -- because of preceding "Present (E_E_A (...))" test. | |
598 | ||
599 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
600 | end loop; | |
601 | end if; | |
602 | ||
ee6ba406 | 603 | if Inside_A_Generic then |
604 | return; | |
605 | ||
6ffc64fc | 606 | -- Only apply the run-time check if the access parameter has an |
607 | -- associated extra access level parameter and when the level of the | |
608 | -- type is less deep than the level of the access parameter, and | |
609 | -- accessibility checks are not suppressed. | |
ee6ba406 | 610 | |
611 | elsif Present (Param_Ent) | |
612 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 613 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 614 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 615 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
616 | and then not Accessibility_Checks_Suppressed (Typ) | |
617 | then | |
618 | Param_Level := | |
619 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
620 | ||
e1415398 | 621 | -- Use the dynamic accessibility parameter for the function's result |
622 | -- when one has been created instead of statically referring to the | |
623 | -- deepest type level so as to appropriatly handle the rules for | |
624 | -- RM 3.10.2 (10.1/3). | |
625 | ||
626 | if Ekind_In (Scope (Param_Ent), E_Function, | |
627 | E_Operator, | |
628 | E_Subprogram_Type) | |
629 | and then Present (Extra_Accessibility_Of_Result (Scope (Param_Ent))) | |
630 | then | |
631 | Type_Level := | |
632 | New_Occurrence_Of | |
633 | (Extra_Accessibility_Of_Result (Scope (Param_Ent)), Loc); | |
634 | else | |
635 | Type_Level := | |
636 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
637 | end if; | |
ee6ba406 | 638 | |
bf3e1520 | 639 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 640 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 641 | |
b1118eb8 | 642 | Check_Cond := Make_Op_Gt (Loc, |
643 | Left_Opnd => Param_Level, | |
644 | Right_Opnd => Type_Level); | |
645 | ||
55dc6dc2 | 646 | Insert_Action (Insert_Node, |
ee6ba406 | 647 | Make_Raise_Program_Error (Loc, |
b1118eb8 | 648 | Condition => Check_Cond, |
649 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 650 | |
651 | Analyze_And_Resolve (N); | |
b1118eb8 | 652 | |
653 | -- If constant folding has happened on the condition for the | |
654 | -- generated error, then warn about it being unconditional. | |
655 | ||
656 | if Nkind (Check_Cond) = N_Identifier | |
657 | and then Entity (Check_Cond) = Standard_True | |
658 | then | |
659 | Error_Msg_Warn := SPARK_Mode /= On; | |
660 | Error_Msg_N | |
661 | ("accessibility check fails<<", N); | |
662 | Error_Msg_N | |
663 | ("\Program_Error [<<", N); | |
664 | end if; | |
ee6ba406 | 665 | end if; |
666 | end Apply_Accessibility_Check; | |
667 | ||
0577b0b1 | 668 | -------------------------------- |
669 | -- Apply_Address_Clause_Check -- | |
670 | -------------------------------- | |
671 | ||
672 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 673 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
674 | ||
8650387e | 675 | AC : constant Node_Id := Address_Clause (E); |
676 | Loc : constant Source_Ptr := Sloc (AC); | |
677 | Typ : constant Entity_Id := Etype (E); | |
c2b56224 | 678 | |
c2b56224 | 679 | Expr : Node_Id; |
0577b0b1 | 680 | -- Address expression (not necessarily the same as Aexp, for example |
681 | -- when Aexp is a reference to a constant, in which case Expr gets | |
7b8fa048 | 682 | -- reset to reference the value expression of the constant). |
0577b0b1 | 683 | |
c2b56224 | 684 | begin |
d6da7448 | 685 | -- See if alignment check needed. Note that we never need a check if the |
686 | -- maximum alignment is one, since the check will always succeed. | |
687 | ||
688 | -- Note: we do not check for checks suppressed here, since that check | |
689 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
690 | -- only called if checks were not suppressed. The reason for this is | |
691 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
692 | -- time (so that all types etc are elaborated), but we have to check | |
693 | -- the status of check suppressing at the point of the address clause. | |
694 | ||
695 | if No (AC) | |
696 | or else not Check_Address_Alignment (AC) | |
697 | or else Maximum_Alignment = 1 | |
698 | then | |
699 | return; | |
700 | end if; | |
701 | ||
702 | -- Obtain expression from address clause | |
9dfe12ae | 703 | |
514a5555 | 704 | Expr := Address_Value (Expression (AC)); |
0577b0b1 | 705 | |
514a5555 | 706 | -- See if we know that Expr has an acceptable value at compile time. If |
707 | -- it hasn't or we don't know, we defer issuing the warning until the | |
708 | -- end of the compilation to take into account back end annotations. | |
c2b56224 | 709 | |
710 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 711 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 712 | then |
f2a06be9 | 713 | declare |
714 | AL : Uint := Alignment (Typ); | |
715 | ||
716 | begin | |
8650387e | 717 | -- The object alignment might be more restrictive than the type |
718 | -- alignment. | |
f2a06be9 | 719 | |
720 | if Known_Alignment (E) then | |
721 | AL := Alignment (E); | |
722 | end if; | |
723 | ||
514a5555 | 724 | if Expr_Value (Expr) mod AL = 0 then |
0577b0b1 | 725 | return; |
f2a06be9 | 726 | end if; |
727 | end; | |
c2b56224 | 728 | |
7161e166 | 729 | -- If the expression has the form X'Address, then we can find out if the |
730 | -- object X has an alignment that is compatible with the object E. If it | |
731 | -- hasn't or we don't know, we defer issuing the warning until the end | |
732 | -- of the compilation to take into account back end annotations. | |
c2b56224 | 733 | |
0577b0b1 | 734 | elsif Nkind (Expr) = N_Attribute_Reference |
735 | and then Attribute_Name (Expr) = Name_Address | |
7161e166 | 736 | and then |
737 | Has_Compatible_Alignment (E, Prefix (Expr), False) = Known_Compatible | |
0577b0b1 | 738 | then |
d6da7448 | 739 | return; |
0577b0b1 | 740 | end if; |
c2b56224 | 741 | |
6fb3c314 | 742 | -- Here we do not know if the value is acceptable. Strictly we don't |
743 | -- have to do anything, since if the alignment is bad, we have an | |
744 | -- erroneous program. However we are allowed to check for erroneous | |
745 | -- conditions and we decide to do this by default if the check is not | |
746 | -- suppressed. | |
0577b0b1 | 747 | |
748 | -- However, don't do the check if elaboration code is unwanted | |
749 | ||
750 | if Restriction_Active (No_Elaboration_Code) then | |
751 | return; | |
752 | ||
753 | -- Generate a check to raise PE if alignment may be inappropriate | |
754 | ||
755 | else | |
1916d94e | 756 | -- If the original expression is a nonstatic constant, use the name |
8650387e | 757 | -- of the constant itself rather than duplicating its initialization |
758 | -- expression, which was extracted above. | |
0577b0b1 | 759 | |
00c403ee | 760 | -- Note: Expr is empty if the address-clause is applied to in-mode |
761 | -- actuals (allowed by 13.1(22)). | |
762 | ||
763 | if not Present (Expr) | |
764 | or else | |
765 | (Is_Entity_Name (Expression (AC)) | |
766 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
8650387e | 767 | and then Nkind (Parent (Entity (Expression (AC)))) = |
768 | N_Object_Declaration) | |
0577b0b1 | 769 | then |
770 | Expr := New_Copy_Tree (Expression (AC)); | |
771 | else | |
772 | Remove_Side_Effects (Expr); | |
c2b56224 | 773 | end if; |
c2b56224 | 774 | |
d950dc79 | 775 | if No (Actions (N)) then |
776 | Set_Actions (N, New_List); | |
777 | end if; | |
778 | ||
779 | Prepend_To (Actions (N), | |
0577b0b1 | 780 | Make_Raise_Program_Error (Loc, |
781 | Condition => | |
782 | Make_Op_Ne (Loc, | |
8650387e | 783 | Left_Opnd => |
0577b0b1 | 784 | Make_Op_Mod (Loc, |
8650387e | 785 | Left_Opnd => |
0577b0b1 | 786 | Unchecked_Convert_To |
787 | (RTE (RE_Integer_Address), Expr), | |
788 | Right_Opnd => | |
789 | Make_Attribute_Reference (Loc, | |
d950dc79 | 790 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 791 | Attribute_Name => Name_Alignment)), |
792 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
8650387e | 793 | Reason => PE_Misaligned_Address_Value)); |
7b8fa048 | 794 | |
795 | Warning_Msg := No_Error_Msg; | |
d950dc79 | 796 | Analyze (First (Actions (N)), Suppress => All_Checks); |
cd309f05 | 797 | |
514a5555 | 798 | -- If the above raise action generated a warning message (for example |
7b8fa048 | 799 | -- from Warn_On_Non_Local_Exception mode with the active restriction |
800 | -- No_Exception_Propagation). | |
801 | ||
802 | if Warning_Msg /= No_Error_Msg then | |
8650387e | 803 | |
7b8fa048 | 804 | -- If the expression has a known at compile time value, then |
805 | -- once we know the alignment of the type, we can check if the | |
806 | -- exception will be raised or not, and if not, we don't need | |
807 | -- the warning so we will kill the warning later on. | |
808 | ||
809 | if Compile_Time_Known_Value (Expr) then | |
810 | Alignment_Warnings.Append | |
811 | ((E => E, A => Expr_Value (Expr), W => Warning_Msg)); | |
cd309f05 | 812 | |
8650387e | 813 | -- Add explanation of the warning generated by the check |
814 | ||
815 | else | |
514a5555 | 816 | Error_Msg_N |
8650387e | 817 | ("\address value may be incompatible with alignment of " |
818 | & "object?X?", AC); | |
514a5555 | 819 | end if; |
cd309f05 | 820 | end if; |
78be29d1 | 821 | |
0577b0b1 | 822 | return; |
823 | end if; | |
9dfe12ae | 824 | |
825 | exception | |
8650387e | 826 | |
0577b0b1 | 827 | -- If we have some missing run time component in configurable run time |
828 | -- mode then just skip the check (it is not required in any case). | |
829 | ||
9dfe12ae | 830 | when RE_Not_Available => |
831 | return; | |
0577b0b1 | 832 | end Apply_Address_Clause_Check; |
c2b56224 | 833 | |
ee6ba406 | 834 | ------------------------------------- |
835 | -- Apply_Arithmetic_Overflow_Check -- | |
836 | ------------------------------------- | |
837 | ||
3cce7f32 | 838 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
839 | begin | |
840 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 841 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 842 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 843 | |
0df9d43f | 844 | if Overflow_Check_Mode = Strict |
3cce7f32 | 845 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
846 | then | |
0df9d43f | 847 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 848 | |
21a55437 | 849 | -- Otherwise use the new routine for the case of a signed integer |
850 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
851 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 852 | |
853 | else | |
854 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
855 | end if; | |
856 | end Apply_Arithmetic_Overflow_Check; | |
857 | ||
0df9d43f | 858 | -------------------------------------- |
859 | -- Apply_Arithmetic_Overflow_Strict -- | |
860 | -------------------------------------- | |
3cce7f32 | 861 | |
fc1dbe36 | 862 | -- This routine is called only if the type is an integer type and an |
863 | -- arithmetic overflow check may be needed for op (add, subtract, or | |
864 | -- multiply). This check is performed if Backend_Overflow_Checks_On_Target | |
865 | -- is not enabled and Do_Overflow_Check is set. In this case we expand the | |
f40f9731 | 866 | -- operation into a more complex sequence of tests that ensures that |
867 | -- overflow is properly caught. | |
ee6ba406 | 868 | |
0df9d43f | 869 | -- This is used in CHECKED modes. It is identical to the code for this |
870 | -- cases before the big overflow earthquake, thus ensuring that in this | |
871 | -- modes we have compatible behavior (and reliability) to what was there | |
872 | -- before. It is also called for types other than signed integers, and if | |
873 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 874 | |
875 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
876 | -- to give up and just generate an overflow check without any fuss. | |
877 | ||
0df9d43f | 878 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 879 | Loc : constant Source_Ptr := Sloc (N); |
880 | Typ : constant Entity_Id := Etype (N); | |
881 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 882 | |
883 | begin | |
0df9d43f | 884 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
885 | -- suppressed. | |
886 | ||
887 | if not Do_Overflow_Check (N) then | |
888 | return; | |
889 | end if; | |
890 | ||
f40f9731 | 891 | -- An interesting special case. If the arithmetic operation appears as |
892 | -- the operand of a type conversion: | |
893 | ||
894 | -- type1 (x op y) | |
895 | ||
896 | -- and all the following conditions apply: | |
897 | ||
898 | -- arithmetic operation is for a signed integer type | |
899 | -- target type type1 is a static integer subtype | |
900 | -- range of x and y are both included in the range of type1 | |
901 | -- range of x op y is included in the range of type1 | |
902 | -- size of type1 is at least twice the result size of op | |
903 | ||
a36d34e4 | 904 | -- then we don't do an overflow check in any case. Instead, we transform |
f40f9731 | 905 | -- the operation so that we end up with: |
906 | ||
907 | -- type1 (type1 (x) op type1 (y)) | |
908 | ||
909 | -- This avoids intermediate overflow before the conversion. It is | |
910 | -- explicitly permitted by RM 3.5.4(24): | |
911 | ||
912 | -- For the execution of a predefined operation of a signed integer | |
913 | -- type, the implementation need not raise Constraint_Error if the | |
914 | -- result is outside the base range of the type, so long as the | |
915 | -- correct result is produced. | |
916 | ||
917 | -- It's hard to imagine that any programmer counts on the exception | |
918 | -- being raised in this case, and in any case it's wrong coding to | |
919 | -- have this expectation, given the RM permission. Furthermore, other | |
920 | -- Ada compilers do allow such out of range results. | |
921 | ||
922 | -- Note that we do this transformation even if overflow checking is | |
923 | -- off, since this is precisely about giving the "right" result and | |
924 | -- avoiding the need for an overflow check. | |
925 | ||
8eb4a5eb | 926 | -- Note: this circuit is partially redundant with respect to the similar |
927 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
928 | -- with cases that do not come through here. We still need the following | |
929 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
930 | -- sure not to generate the arithmetic overflow check in these cases | |
931 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
932 | ||
f40f9731 | 933 | if Is_Signed_Integer_Type (Typ) |
934 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 935 | then |
f32c377d | 936 | Conversion_Optimization : declare |
f40f9731 | 937 | Target_Type : constant Entity_Id := |
b6341c67 | 938 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 939 | |
940 | Llo, Lhi : Uint; | |
941 | Rlo, Rhi : Uint; | |
942 | LOK, ROK : Boolean; | |
943 | ||
944 | Vlo : Uint; | |
945 | Vhi : Uint; | |
946 | VOK : Boolean; | |
947 | ||
948 | Tlo : Uint; | |
949 | Thi : Uint; | |
950 | ||
951 | begin | |
952 | if Is_Integer_Type (Target_Type) | |
953 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
954 | then | |
955 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
956 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
957 | ||
9c486805 | 958 | Determine_Range |
959 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
960 | Determine_Range | |
961 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 962 | |
963 | if (LOK and ROK) | |
964 | and then Tlo <= Llo and then Lhi <= Thi | |
965 | and then Tlo <= Rlo and then Rhi <= Thi | |
966 | then | |
9c486805 | 967 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 968 | |
969 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
970 | Rewrite (Left_Opnd (N), | |
971 | Make_Type_Conversion (Loc, | |
972 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
973 | Expression => Relocate_Node (Left_Opnd (N)))); | |
974 | ||
975 | Rewrite (Right_Opnd (N), | |
976 | Make_Type_Conversion (Loc, | |
977 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
978 | Expression => Relocate_Node (Right_Opnd (N)))); | |
979 | ||
780bfb21 | 980 | -- Rewrite the conversion operand so that the original |
981 | -- node is retained, in order to avoid the warning for | |
982 | -- redundant conversions in Resolve_Type_Conversion. | |
983 | ||
984 | Rewrite (N, Relocate_Node (N)); | |
985 | ||
f40f9731 | 986 | Set_Etype (N, Target_Type); |
780bfb21 | 987 | |
f40f9731 | 988 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
989 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
990 | ||
991 | -- Given that the target type is twice the size of the | |
992 | -- source type, overflow is now impossible, so we can | |
993 | -- safely kill the overflow check and return. | |
994 | ||
995 | Set_Do_Overflow_Check (N, False); | |
996 | return; | |
997 | end if; | |
998 | end if; | |
999 | end if; | |
f32c377d | 1000 | end Conversion_Optimization; |
ee6ba406 | 1001 | end if; |
1002 | ||
f40f9731 | 1003 | -- Now see if an overflow check is required |
1004 | ||
1005 | declare | |
1006 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
1007 | Dsiz : constant Int := Siz * 2; | |
1008 | Opnod : Node_Id; | |
1009 | Ctyp : Entity_Id; | |
1010 | Opnd : Node_Id; | |
1011 | Cent : RE_Id; | |
ee6ba406 | 1012 | |
f40f9731 | 1013 | begin |
1014 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 1015 | -- is not set anyway, or we are not doing code expansion, or the |
1016 | -- parent node is a type conversion whose operand is an arithmetic | |
1017 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 1018 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 1019 | |
f40f9731 | 1020 | if Backend_Overflow_Checks_On_Target |
1021 | or else not Do_Overflow_Check (N) | |
a33565dd | 1022 | or else not Expander_Active |
df40eeb0 | 1023 | or else (Present (Parent (N)) |
1024 | and then Nkind (Parent (N)) = N_Type_Conversion | |
1025 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 1026 | then |
1027 | return; | |
1028 | end if; | |
ee6ba406 | 1029 | |
f40f9731 | 1030 | -- Otherwise, generate the full general code for front end overflow |
1031 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 1032 | |
f40f9731 | 1033 | -- x op y |
ee6ba406 | 1034 | |
f40f9731 | 1035 | -- is expanded into |
ee6ba406 | 1036 | |
f40f9731 | 1037 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 1038 | |
f40f9731 | 1039 | -- where Typ is the type of the original expression, and Checktyp is |
1040 | -- an integer type of sufficient length to hold the largest possible | |
1041 | -- result. | |
ee6ba406 | 1042 | |
f40f9731 | 1043 | -- If the size of check type exceeds the size of Long_Long_Integer, |
1044 | -- we use a different approach, expanding to: | |
ee6ba406 | 1045 | |
f40f9731 | 1046 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 1047 | |
f40f9731 | 1048 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 1049 | |
f40f9731 | 1050 | -- Find check type if one exists |
1051 | ||
1052 | if Dsiz <= Standard_Integer_Size then | |
1053 | Ctyp := Standard_Integer; | |
ee6ba406 | 1054 | |
f40f9731 | 1055 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
1056 | Ctyp := Standard_Long_Long_Integer; | |
1057 | ||
c9f84db7 | 1058 | -- No check type exists, use runtime call |
ee6ba406 | 1059 | |
1060 | else | |
f40f9731 | 1061 | if Nkind (N) = N_Op_Add then |
1062 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1063 | |
f40f9731 | 1064 | elsif Nkind (N) = N_Op_Multiply then |
1065 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1066 | |
f40f9731 | 1067 | else |
1068 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1069 | Cent := RE_Subtract_With_Ovflo_Check; | |
1070 | end if; | |
1071 | ||
1072 | Rewrite (N, | |
1073 | OK_Convert_To (Typ, | |
1074 | Make_Function_Call (Loc, | |
83c6c069 | 1075 | Name => New_Occurrence_Of (RTE (Cent), Loc), |
f40f9731 | 1076 | Parameter_Associations => New_List ( |
1077 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1078 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1079 | |
f40f9731 | 1080 | Analyze_And_Resolve (N, Typ); |
1081 | return; | |
1082 | end if; | |
ee6ba406 | 1083 | |
f40f9731 | 1084 | -- If we fall through, we have the case where we do the arithmetic |
1085 | -- in the next higher type and get the check by conversion. In these | |
1086 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1087 | |
f40f9731 | 1088 | Opnod := Relocate_Node (N); |
ee6ba406 | 1089 | |
f40f9731 | 1090 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1091 | |
f40f9731 | 1092 | Analyze (Opnd); |
1093 | Set_Etype (Opnd, Ctyp); | |
1094 | Set_Analyzed (Opnd, True); | |
1095 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1096 | |
f40f9731 | 1097 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1098 | |
f40f9731 | 1099 | Analyze (Opnd); |
1100 | Set_Etype (Opnd, Ctyp); | |
1101 | Set_Analyzed (Opnd, True); | |
1102 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1103 | |
f40f9731 | 1104 | -- The type of the operation changes to the base type of the check |
1105 | -- type, and we reset the overflow check indication, since clearly no | |
1106 | -- overflow is possible now that we are using a double length type. | |
1107 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1108 | -- expand the node. | |
ee6ba406 | 1109 | |
f40f9731 | 1110 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1111 | Set_Do_Overflow_Check (Opnod, False); | |
1112 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1113 | |
f40f9731 | 1114 | -- Now build the outer conversion |
ee6ba406 | 1115 | |
f40f9731 | 1116 | Opnd := OK_Convert_To (Typ, Opnod); |
1117 | Analyze (Opnd); | |
1118 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1119 | |
f40f9731 | 1120 | -- In the discrete type case, we directly generate the range check |
1121 | -- for the outer operand. This range check will implement the | |
1122 | -- required overflow check. | |
9dfe12ae | 1123 | |
f40f9731 | 1124 | if Is_Discrete_Type (Typ) then |
1125 | Rewrite (N, Opnd); | |
1126 | Generate_Range_Check | |
1127 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1128 | |
f40f9731 | 1129 | -- For other types, we enable overflow checking on the conversion, |
1130 | -- after setting the node as analyzed to prevent recursive attempts | |
1131 | -- to expand the conversion node. | |
9dfe12ae | 1132 | |
f40f9731 | 1133 | else |
1134 | Set_Analyzed (Opnd, True); | |
1135 | Enable_Overflow_Check (Opnd); | |
1136 | Rewrite (N, Opnd); | |
1137 | end if; | |
1138 | ||
1139 | exception | |
1140 | when RE_Not_Available => | |
1141 | return; | |
1142 | end; | |
0df9d43f | 1143 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1144 | |
1145 | ---------------------------------------------------- | |
1146 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1147 | ---------------------------------------------------- | |
1148 | ||
1149 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1150 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1151 | |
1152 | Loc : constant Source_Ptr := Sloc (Op); | |
1153 | P : constant Node_Id := Parent (Op); | |
1154 | ||
49b3a812 | 1155 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1156 | -- Operands and results are of this type when we convert | |
1157 | ||
3cce7f32 | 1158 | Result_Type : constant Entity_Id := Etype (Op); |
1159 | -- Original result type | |
1160 | ||
db415383 | 1161 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1162 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1163 | ||
1164 | Lo, Hi : Uint; | |
1165 | -- Ranges of values for result | |
1166 | ||
1167 | begin | |
1168 | -- Nothing to do if our parent is one of the following: | |
1169 | ||
0326b4d4 | 1170 | -- Another signed integer arithmetic op |
3cce7f32 | 1171 | -- A membership operation |
1172 | -- A comparison operation | |
1173 | ||
1174 | -- In all these cases, we will process at the higher level (and then | |
1175 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1176 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1177 | |
1178 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1179 | or else Nkind (P) in N_Membership_Test |
1180 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1181 | |
70a2dff4 | 1182 | -- This is also true for an alternative in a case expression |
1183 | ||
1184 | or else Nkind (P) = N_Case_Expression_Alternative | |
1185 | ||
1186 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1187 | |
b8a17a21 | 1188 | or else (Nkind (P) = N_Range |
1189 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1190 | then |
af90720d | 1191 | -- If_Expressions and Case_Expressions are treated as arithmetic |
1192 | -- ops, but if they appear in an assignment or similar contexts | |
1193 | -- there is no overflow check that starts from that parent node, | |
1194 | -- so apply check now. | |
1195 | ||
1196 | if Nkind_In (P, N_If_Expression, N_Case_Expression) | |
1197 | and then not Is_Signed_Integer_Arithmetic_Op (Parent (P)) | |
1198 | then | |
1199 | null; | |
1200 | else | |
1201 | return; | |
1202 | end if; | |
3cce7f32 | 1203 | end if; |
1204 | ||
0326b4d4 | 1205 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1206 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1207 | -- modes. This is the case where we tell the machinery not to move into | |
1208 | -- Bignum mode at this top level (of course the top level operation | |
1209 | -- will still be in Bignum mode if either of its operands are of type | |
1210 | -- Bignum). | |
3cce7f32 | 1211 | |
0df9d43f | 1212 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1213 | |
1214 | -- That call may but does not necessarily change the result type of Op. | |
1215 | -- It is the job of this routine to undo such changes, so that at the | |
1216 | -- top level, we have the proper type. This "undoing" is a point at | |
1217 | -- which a final overflow check may be applied. | |
1218 | ||
f32c377d | 1219 | -- If the result type was not fiddled we are all set. We go to base |
1220 | -- types here because things may have been rewritten to generate the | |
1221 | -- base type of the operand types. | |
3cce7f32 | 1222 | |
f32c377d | 1223 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1224 | return; |
1225 | ||
1226 | -- Bignum case | |
1227 | ||
49b3a812 | 1228 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1229 | |
d94b5da2 | 1230 | -- We need a sequence that looks like: |
3cce7f32 | 1231 | |
1232 | -- Rnn : Result_Type; | |
1233 | ||
1234 | -- declare | |
d94b5da2 | 1235 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1236 | -- begin |
49b3a812 | 1237 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1238 | -- SS_Release (M); |
1239 | -- end; | |
1240 | ||
1241 | -- This block is inserted (using Insert_Actions), and then the node | |
1242 | -- is replaced with a reference to Rnn. | |
1243 | ||
75b45a21 | 1244 | -- If our parent is a conversion node then there is no point in |
a36d34e4 | 1245 | -- generating a conversion to Result_Type. Instead, we let the parent |
75b45a21 | 1246 | -- handle this. Note that this special case is not just about |
1247 | -- optimization. Consider | |
3cce7f32 | 1248 | |
1249 | -- A,B,C : Integer; | |
1250 | -- ... | |
49b3a812 | 1251 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1252 | |
1253 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1254 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1255 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1256 | |
1257 | declare | |
49b3a812 | 1258 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1259 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1260 | RHS : Node_Id; | |
1261 | ||
1262 | Rtype : Entity_Id; | |
1263 | ||
1264 | begin | |
1265 | RHS := Convert_From_Bignum (Op); | |
1266 | ||
1267 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1268 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1269 | Rtype := Result_Type; |
1270 | ||
1271 | -- Interesting question, do we need a check on that conversion | |
1272 | -- operation. Answer, not if we know the result is in range. | |
1273 | -- At the moment we are not taking advantage of this. To be | |
1274 | -- looked at later ??? | |
1275 | ||
1276 | else | |
49b3a812 | 1277 | Rtype := LLIB; |
3cce7f32 | 1278 | end if; |
1279 | ||
1280 | Insert_Before | |
1281 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1282 | Make_Assignment_Statement (Loc, | |
1283 | Name => New_Occurrence_Of (Rnn, Loc), | |
1284 | Expression => RHS)); | |
1285 | ||
1286 | Insert_Actions (Op, New_List ( | |
1287 | Make_Object_Declaration (Loc, | |
1288 | Defining_Identifier => Rnn, | |
1289 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1290 | Blk)); | |
1291 | ||
1292 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1293 | Analyze_And_Resolve (Op); | |
1294 | end; | |
1295 | ||
af90720d | 1296 | -- Here we know the result is Long_Long_Integer'Base, or that it has |
412f75eb | 1297 | -- been rewritten because the parent operation is a conversion. See |
0df9d43f | 1298 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1299 | |
1300 | else | |
f32c377d | 1301 | pragma Assert |
1302 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1303 | |
1304 | -- All we need to do here is to convert the result to the proper | |
1305 | -- result type. As explained above for the Bignum case, we can | |
1306 | -- omit this if our parent is a type conversion. | |
1307 | ||
1308 | if Nkind (P) /= N_Type_Conversion then | |
1309 | Convert_To_And_Rewrite (Result_Type, Op); | |
1310 | end if; | |
1311 | ||
1312 | Analyze_And_Resolve (Op); | |
1313 | end if; | |
1314 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1315 | |
ee6ba406 | 1316 | ---------------------------- |
1317 | -- Apply_Constraint_Check -- | |
1318 | ---------------------------- | |
1319 | ||
1320 | procedure Apply_Constraint_Check | |
1321 | (N : Node_Id; | |
1322 | Typ : Entity_Id; | |
1323 | No_Sliding : Boolean := False) | |
1324 | is | |
1325 | Desig_Typ : Entity_Id; | |
1326 | ||
1327 | begin | |
7aafae1c | 1328 | -- No checks inside a generic (check the instantiations) |
1329 | ||
ee6ba406 | 1330 | if Inside_A_Generic then |
1331 | return; | |
7aafae1c | 1332 | end if; |
ee6ba406 | 1333 | |
6fb3c314 | 1334 | -- Apply required constraint checks |
7aafae1c | 1335 | |
1336 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1337 | Apply_Scalar_Range_Check (N, Typ); |
1338 | ||
1339 | elsif Is_Array_Type (Typ) then | |
1340 | ||
05fcfafb | 1341 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1342 | -- always has the right bounds. |
1343 | ||
1344 | if Nkind (N) = N_Aggregate | |
1345 | and then No (Expressions (N)) | |
1346 | and then Nkind | |
1347 | (First (Choices (First (Component_Associations (N))))) | |
1348 | = N_Others_Choice | |
1349 | then | |
1350 | return; | |
1351 | end if; | |
1352 | ||
ee6ba406 | 1353 | if Is_Constrained (Typ) then |
1354 | Apply_Length_Check (N, Typ); | |
1355 | ||
1356 | if No_Sliding then | |
1357 | Apply_Range_Check (N, Typ); | |
1358 | end if; | |
1359 | else | |
1360 | Apply_Range_Check (N, Typ); | |
1361 | end if; | |
1362 | ||
4fb5f0a0 | 1363 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1364 | and then Has_Discriminants (Base_Type (Typ)) |
1365 | and then Is_Constrained (Typ) | |
1366 | then | |
1367 | Apply_Discriminant_Check (N, Typ); | |
1368 | ||
1369 | elsif Is_Access_Type (Typ) then | |
1370 | ||
1371 | Desig_Typ := Designated_Type (Typ); | |
1372 | ||
1373 | -- No checks necessary if expression statically null | |
1374 | ||
2af58f67 | 1375 | if Known_Null (N) then |
00c403ee | 1376 | if Can_Never_Be_Null (Typ) then |
1377 | Install_Null_Excluding_Check (N); | |
1378 | end if; | |
ee6ba406 | 1379 | |
1380 | -- No sliding possible on access to arrays | |
1381 | ||
1382 | elsif Is_Array_Type (Desig_Typ) then | |
1383 | if Is_Constrained (Desig_Typ) then | |
1384 | Apply_Length_Check (N, Typ); | |
1385 | end if; | |
1386 | ||
1387 | Apply_Range_Check (N, Typ); | |
1388 | ||
a63a0aad | 1389 | -- Do not install a discriminant check for a constrained subtype |
1390 | -- created for an unconstrained nominal type because the subtype | |
1391 | -- has the correct constraints by construction. | |
1392 | ||
ee6ba406 | 1393 | elsif Has_Discriminants (Base_Type (Desig_Typ)) |
a63a0aad | 1394 | and then Is_Constrained (Desig_Typ) |
1395 | and then not Is_Constr_Subt_For_U_Nominal (Desig_Typ) | |
ee6ba406 | 1396 | then |
1397 | Apply_Discriminant_Check (N, Typ); | |
1398 | end if; | |
fa7497e8 | 1399 | |
bf3e1520 | 1400 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1401 | -- this check if the constraint node is illegal, as shown by having |
1402 | -- an error posted. This additional guard prevents cascaded errors | |
1403 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1404 | ||
fa7497e8 | 1405 | if Can_Never_Be_Null (Typ) |
1406 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1407 | and then not Error_Posted (N) |
fa7497e8 | 1408 | then |
1409 | Install_Null_Excluding_Check (N); | |
1410 | end if; | |
ee6ba406 | 1411 | end if; |
1412 | end Apply_Constraint_Check; | |
1413 | ||
1414 | ------------------------------ | |
1415 | -- Apply_Discriminant_Check -- | |
1416 | ------------------------------ | |
1417 | ||
1418 | procedure Apply_Discriminant_Check | |
1419 | (N : Node_Id; | |
1420 | Typ : Entity_Id; | |
1421 | Lhs : Node_Id := Empty) | |
1422 | is | |
1423 | Loc : constant Source_Ptr := Sloc (N); | |
1424 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1425 | S_Typ : Entity_Id := Etype (N); | |
1426 | Cond : Node_Id; | |
1427 | T_Typ : Entity_Id; | |
1428 | ||
7be5088a | 1429 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1430 | -- A heap object with an indefinite subtype is constrained by its | |
1431 | -- initial value, and assigning to it requires a constraint_check. | |
1432 | -- The target may be an explicit dereference, or a renaming of one. | |
1433 | ||
ee6ba406 | 1434 | function Is_Aliased_Unconstrained_Component return Boolean; |
1435 | -- It is possible for an aliased component to have a nominal | |
1436 | -- unconstrained subtype (through instantiation). If this is a | |
1437 | -- discriminated component assigned in the expansion of an aggregate | |
1438 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1439 | -- situation requires a predicate of its own. |
ee6ba406 | 1440 | |
7be5088a | 1441 | ---------------------------------- |
1442 | -- Denotes_Explicit_Dereference -- | |
1443 | ---------------------------------- | |
1444 | ||
1445 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1446 | begin | |
1447 | return | |
1448 | Nkind (Obj) = N_Explicit_Dereference | |
1449 | or else | |
1450 | (Is_Entity_Name (Obj) | |
1451 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1452 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1453 | N_Explicit_Dereference); | |
7be5088a | 1454 | end Denotes_Explicit_Dereference; |
1455 | ||
ee6ba406 | 1456 | ---------------------------------------- |
1457 | -- Is_Aliased_Unconstrained_Component -- | |
1458 | ---------------------------------------- | |
1459 | ||
1460 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1461 | Comp : Entity_Id; | |
1462 | Pref : Node_Id; | |
1463 | ||
1464 | begin | |
1465 | if Nkind (Lhs) /= N_Selected_Component then | |
1466 | return False; | |
1467 | else | |
1468 | Comp := Entity (Selector_Name (Lhs)); | |
1469 | Pref := Prefix (Lhs); | |
1470 | end if; | |
1471 | ||
1472 | if Ekind (Comp) /= E_Component | |
1473 | or else not Is_Aliased (Comp) | |
1474 | then | |
1475 | return False; | |
1476 | end if; | |
1477 | ||
1478 | return not Comes_From_Source (Pref) | |
1479 | and then In_Instance | |
1480 | and then not Is_Constrained (Etype (Comp)); | |
1481 | end Is_Aliased_Unconstrained_Component; | |
1482 | ||
1483 | -- Start of processing for Apply_Discriminant_Check | |
1484 | ||
1485 | begin | |
1486 | if Do_Access then | |
1487 | T_Typ := Designated_Type (Typ); | |
1488 | else | |
1489 | T_Typ := Typ; | |
1490 | end if; | |
1491 | ||
105fa703 | 1492 | -- If the expression is a function call that returns a limited object |
1493 | -- it cannot be copied. It is not clear how to perform the proper | |
1494 | -- discriminant check in this case because the discriminant value must | |
1495 | -- be retrieved from the constructed object itself. | |
1496 | ||
1497 | if Nkind (N) = N_Function_Call | |
1498 | and then Is_Limited_Type (Typ) | |
1499 | and then Is_Entity_Name (Name (N)) | |
1500 | and then Returns_By_Ref (Entity (Name (N))) | |
1501 | then | |
1502 | return; | |
1503 | end if; | |
1504 | ||
f0d65dae | 1505 | -- Only apply checks when generating code and discriminant checks are |
1506 | -- not suppressed. In GNATprove mode, we do not apply the checks, but we | |
1507 | -- still analyze the expression to possibly issue errors on SPARK code | |
1508 | -- when a run-time error can be detected at compile time. | |
1509 | ||
1510 | if not GNATprove_Mode then | |
1511 | if not Expander_Active | |
1512 | or else Discriminant_Checks_Suppressed (T_Typ) | |
1513 | then | |
1514 | return; | |
1515 | end if; | |
ee6ba406 | 1516 | end if; |
1517 | ||
feff2f05 | 1518 | -- No discriminant checks necessary for an access when expression is |
1519 | -- statically Null. This is not only an optimization, it is fundamental | |
1520 | -- because otherwise discriminant checks may be generated in init procs | |
1521 | -- for types containing an access to a not-yet-frozen record, causing a | |
1522 | -- deadly forward reference. | |
ee6ba406 | 1523 | |
feff2f05 | 1524 | -- Also, if the expression is of an access type whose designated type is |
1525 | -- incomplete, then the access value must be null and we suppress the | |
1526 | -- check. | |
ee6ba406 | 1527 | |
2af58f67 | 1528 | if Known_Null (N) then |
ee6ba406 | 1529 | return; |
1530 | ||
1531 | elsif Is_Access_Type (S_Typ) then | |
1532 | S_Typ := Designated_Type (S_Typ); | |
1533 | ||
1534 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1535 | return; | |
1536 | end if; | |
1537 | end if; | |
1538 | ||
0577b0b1 | 1539 | -- If an assignment target is present, then we need to generate the |
1540 | -- actual subtype if the target is a parameter or aliased object with | |
1541 | -- an unconstrained nominal subtype. | |
1542 | ||
1543 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1544 | -- subtype to the parameter and dereference cases, since other aliased | |
1545 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1546 | -- constrained). |
ee6ba406 | 1547 | |
1548 | if Present (Lhs) | |
1549 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1550 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1551 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1552 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1553 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1554 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1555 | and then not Is_Constrained (T_Typ) |
7be5088a | 1556 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1557 | and then Nkind (Original_Node (Lhs)) /= |
1558 | N_Function_Call)) | |
ee6ba406 | 1559 | then |
1560 | T_Typ := Get_Actual_Subtype (Lhs); | |
1561 | end if; | |
1562 | ||
feff2f05 | 1563 | -- Nothing to do if the type is unconstrained (this is the case where |
1564 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1565 | -- is required). | |
ee6ba406 | 1566 | |
1567 | if not Is_Constrained (T_Typ) then | |
1568 | return; | |
05fcfafb | 1569 | |
1570 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1571 | -- partial view that is constrained. | |
1572 | ||
de54c5ab | 1573 | elsif Ada_Version >= Ada_2005 |
0d78d2d4 | 1574 | and then Object_Type_Has_Constrained_Partial_View |
d41a3f41 | 1575 | (Typ => Base_Type (T_Typ), |
1576 | Scop => Current_Scope) | |
05fcfafb | 1577 | then |
1578 | return; | |
ee6ba406 | 1579 | end if; |
1580 | ||
00f91aef | 1581 | -- Nothing to do if the type is an Unchecked_Union |
1582 | ||
1583 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1584 | return; | |
1585 | end if; | |
1586 | ||
8d11916f | 1587 | -- Suppress checks if the subtypes are the same. The check must be |
feff2f05 | 1588 | -- preserved in an assignment to a formal, because the constraint is |
1589 | -- given by the actual. | |
ee6ba406 | 1590 | |
1591 | if Nkind (Original_Node (N)) /= N_Allocator | |
1592 | and then (No (Lhs) | |
8143bf7c | 1593 | or else not Is_Entity_Name (Lhs) |
1594 | or else No (Param_Entity (Lhs))) | |
ee6ba406 | 1595 | then |
1596 | if (Etype (N) = Typ | |
1597 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1598 | and then not Is_Aliased_View (Lhs) | |
1599 | then | |
1600 | return; | |
1601 | end if; | |
1602 | ||
feff2f05 | 1603 | -- We can also eliminate checks on allocators with a subtype mark that |
1604 | -- coincides with the context type. The context type may be a subtype | |
1605 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1606 | |
1607 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1608 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1609 | then | |
1610 | declare | |
9dfe12ae | 1611 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1612 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1613 | |
1614 | begin | |
1615 | if Alloc_Typ = T_Typ | |
1616 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1617 | and then Is_Entity_Name ( | |
1618 | Subtype_Indication (Parent (T_Typ))) | |
1619 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1620 | ||
1621 | then | |
1622 | return; | |
1623 | end if; | |
1624 | end; | |
1625 | end if; | |
1626 | ||
feff2f05 | 1627 | -- See if we have a case where the types are both constrained, and all |
1628 | -- the constraints are constants. In this case, we can do the check | |
1629 | -- successfully at compile time. | |
ee6ba406 | 1630 | |
8d11916f | 1631 | -- We skip this check for the case where the node is rewritten as |
d7ec9a29 | 1632 | -- an allocator, because it already carries the context subtype, |
1633 | -- and extracting the discriminants from the aggregate is messy. | |
ee6ba406 | 1634 | |
1635 | if Is_Constrained (S_Typ) | |
1636 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1637 | then | |
1638 | declare | |
1639 | DconT : Elmt_Id; | |
1640 | Discr : Entity_Id; | |
1641 | DconS : Elmt_Id; | |
1642 | ItemS : Node_Id; | |
1643 | ItemT : Node_Id; | |
1644 | ||
1645 | begin | |
1646 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1647 | -- private type completed by a default discriminated type. In that |
8d11916f | 1648 | -- case, we need to get the constraints from the underlying type. |
feff2f05 | 1649 | -- If the underlying type is unconstrained (i.e. has no default |
1650 | -- discriminants) no check is needed. | |
ee6ba406 | 1651 | |
1652 | if Has_Discriminants (S_Typ) then | |
1653 | Discr := First_Discriminant (S_Typ); | |
1654 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1655 | ||
1656 | else | |
1657 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1658 | DconS := | |
1659 | First_Elmt | |
1660 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1661 | ||
1662 | if No (DconS) then | |
1663 | return; | |
1664 | end if; | |
fccb5da7 | 1665 | |
1666 | -- A further optimization: if T_Typ is derived from S_Typ | |
1667 | -- without imposing a constraint, no check is needed. | |
1668 | ||
1669 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1670 | N_Full_Type_Declaration | |
1671 | then | |
1672 | declare | |
5c61a0ff | 1673 | Type_Def : constant Node_Id := |
b6341c67 | 1674 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1675 | begin |
1676 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1677 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1678 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1679 | then | |
1680 | return; | |
1681 | end if; | |
1682 | end; | |
1683 | end if; | |
ee6ba406 | 1684 | end if; |
1685 | ||
86594966 | 1686 | -- Constraint may appear in full view of type |
1687 | ||
1688 | if Ekind (T_Typ) = E_Private_Subtype | |
1689 | and then Present (Full_View (T_Typ)) | |
1690 | then | |
d7ec9a29 | 1691 | DconT := |
86594966 | 1692 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
86594966 | 1693 | else |
d7ec9a29 | 1694 | DconT := |
1695 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
86594966 | 1696 | end if; |
ee6ba406 | 1697 | |
1698 | while Present (Discr) loop | |
1699 | ItemS := Node (DconS); | |
1700 | ItemT := Node (DconT); | |
1701 | ||
00c403ee | 1702 | -- For a discriminated component type constrained by the |
1703 | -- current instance of an enclosing type, there is no | |
1704 | -- applicable discriminant check. | |
1705 | ||
1706 | if Nkind (ItemT) = N_Attribute_Reference | |
1707 | and then Is_Access_Type (Etype (ItemT)) | |
1708 | and then Is_Entity_Name (Prefix (ItemT)) | |
1709 | and then Is_Type (Entity (Prefix (ItemT))) | |
1710 | then | |
1711 | return; | |
1712 | end if; | |
1713 | ||
cc60bd16 | 1714 | -- If the expressions for the discriminants are identical |
1715 | -- and it is side-effect free (for now just an entity), | |
1716 | -- this may be a shared constraint, e.g. from a subtype | |
1717 | -- without a constraint introduced as a generic actual. | |
1718 | -- Examine other discriminants if any. | |
1719 | ||
1720 | if ItemS = ItemT | |
1721 | and then Is_Entity_Name (ItemS) | |
1722 | then | |
1723 | null; | |
1724 | ||
1725 | elsif not Is_OK_Static_Expression (ItemS) | |
1726 | or else not Is_OK_Static_Expression (ItemT) | |
1727 | then | |
1728 | exit; | |
ee6ba406 | 1729 | |
cc60bd16 | 1730 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1731 | if Do_Access then -- needs run-time check. |
1732 | exit; | |
1733 | else | |
1734 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1735 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1736 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1737 | return; |
1738 | end if; | |
1739 | end if; | |
1740 | ||
1741 | Next_Elmt (DconS); | |
1742 | Next_Elmt (DconT); | |
1743 | Next_Discriminant (Discr); | |
1744 | end loop; | |
1745 | ||
1746 | if No (Discr) then | |
1747 | return; | |
1748 | end if; | |
1749 | end; | |
1750 | end if; | |
1751 | ||
f0d65dae | 1752 | -- In GNATprove mode, we do not apply the checks |
1753 | ||
1754 | if GNATprove_Mode then | |
1755 | return; | |
1756 | end if; | |
1757 | ||
ee6ba406 | 1758 | -- Here we need a discriminant check. First build the expression |
1759 | -- for the comparisons of the discriminants: | |
1760 | ||
1761 | -- (n.disc1 /= typ.disc1) or else | |
1762 | -- (n.disc2 /= typ.disc2) or else | |
1763 | -- ... | |
1764 | -- (n.discn /= typ.discn) | |
1765 | ||
1766 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1767 | ||
3cce7f32 | 1768 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1769 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1770 | |
1771 | if Present (Param_Entity (Lhs)) then | |
1772 | Cond := | |
1773 | Make_And_Then (Loc, | |
1774 | Left_Opnd => | |
1775 | Make_Attribute_Reference (Loc, | |
1776 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1777 | Attribute_Name => Name_Constrained), | |
1778 | Right_Opnd => Cond); | |
1779 | end if; | |
1780 | ||
1781 | if Do_Access then | |
1782 | Cond := Guard_Access (Cond, Loc, N); | |
1783 | end if; | |
1784 | ||
1785 | Insert_Action (N, | |
f15731c4 | 1786 | Make_Raise_Constraint_Error (Loc, |
1787 | Condition => Cond, | |
1788 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1789 | end Apply_Discriminant_Check; |
1790 | ||
2fe22c69 | 1791 | ------------------------- |
1792 | -- Apply_Divide_Checks -- | |
1793 | ------------------------- | |
ee6ba406 | 1794 | |
2fe22c69 | 1795 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1796 | Loc : constant Source_Ptr := Sloc (N); |
1797 | Typ : constant Entity_Id := Etype (N); | |
1798 | Left : constant Node_Id := Left_Opnd (N); | |
1799 | Right : constant Node_Id := Right_Opnd (N); | |
1800 | ||
db415383 | 1801 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1802 | -- Current overflow checking mode |
1803 | ||
ee6ba406 | 1804 | LLB : Uint; |
1805 | Llo : Uint; | |
1806 | Lhi : Uint; | |
1807 | LOK : Boolean; | |
1808 | Rlo : Uint; | |
1809 | Rhi : Uint; | |
2fe22c69 | 1810 | ROK : Boolean; |
96da3284 | 1811 | |
1812 | pragma Warnings (Off, Lhi); | |
1813 | -- Don't actually use this value | |
ee6ba406 | 1814 | |
1815 | begin | |
0df9d43f | 1816 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1817 | -- operating on signed integer types, then the only thing this routine | |
1818 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1819 | -- procedure will (possibly later on during recursive downward calls), | |
1820 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1821 | |
1822 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1823 | and then Is_Signed_Integer_Type (Typ) |
1824 | then | |
1825 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1826 | return; | |
1827 | end if; | |
1828 | ||
1829 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1830 | ||
a33565dd | 1831 | if Expander_Active |
13dbf220 | 1832 | and then not Backend_Divide_Checks_On_Target |
1833 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1834 | then |
9c486805 | 1835 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1836 | |
2fe22c69 | 1837 | -- Deal with division check |
ee6ba406 | 1838 | |
2fe22c69 | 1839 | if Do_Division_Check (N) |
1840 | and then not Division_Checks_Suppressed (Typ) | |
1841 | then | |
1842 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1843 | end if; |
1844 | ||
2fe22c69 | 1845 | -- Deal with overflow check |
1846 | ||
0df9d43f | 1847 | if Do_Overflow_Check (N) |
1848 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1849 | then | |
f3ccbbb3 | 1850 | Set_Do_Overflow_Check (N, False); |
1851 | ||
2fe22c69 | 1852 | -- Test for extremely annoying case of xxx'First divided by -1 |
1853 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1854 | |
ee6ba406 | 1855 | if Nkind (N) = N_Op_Divide |
1856 | and then Is_Signed_Integer_Type (Typ) | |
1857 | then | |
9c486805 | 1858 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1859 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1860 | ||
1861 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1862 | and then |
1863 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1864 | then |
f72e7b2a | 1865 | -- Ensure that expressions are not evaluated twice (once |
1866 | -- for their runtime checks and once for their regular | |
1867 | -- computation). | |
1868 | ||
1869 | Force_Evaluation (Left, Mode => Strict); | |
1870 | Force_Evaluation (Right, Mode => Strict); | |
1871 | ||
ee6ba406 | 1872 | Insert_Action (N, |
1873 | Make_Raise_Constraint_Error (Loc, | |
1874 | Condition => | |
1875 | Make_And_Then (Loc, | |
2fe22c69 | 1876 | Left_Opnd => |
1877 | Make_Op_Eq (Loc, | |
1878 | Left_Opnd => | |
1879 | Duplicate_Subexpr_Move_Checks (Left), | |
1880 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1881 | |
2fe22c69 | 1882 | Right_Opnd => |
1883 | Make_Op_Eq (Loc, | |
1884 | Left_Opnd => Duplicate_Subexpr (Right), | |
1885 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1886 | |
f15731c4 | 1887 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1888 | end if; |
1889 | end if; | |
1890 | end if; | |
1891 | end if; | |
2fe22c69 | 1892 | end Apply_Divide_Checks; |
1893 | ||
1894 | -------------------------- | |
1895 | -- Apply_Division_Check -- | |
1896 | -------------------------- | |
1897 | ||
1898 | procedure Apply_Division_Check | |
1899 | (N : Node_Id; | |
1900 | Rlo : Uint; | |
1901 | Rhi : Uint; | |
1902 | ROK : Boolean) | |
1903 | is | |
1904 | pragma Assert (Do_Division_Check (N)); | |
1905 | ||
1906 | Loc : constant Source_Ptr := Sloc (N); | |
f81a201b | 1907 | Right : constant Node_Id := Right_Opnd (N); |
1908 | Opnd : Node_Id; | |
2fe22c69 | 1909 | |
1910 | begin | |
a33565dd | 1911 | if Expander_Active |
2fe22c69 | 1912 | and then not Backend_Divide_Checks_On_Target |
1913 | and then Check_Needed (Right, Division_Check) | |
2fe22c69 | 1914 | |
f81a201b | 1915 | -- See if division by zero possible, and if so generate test. This |
1916 | -- part of the test is not controlled by the -gnato switch, since it | |
1917 | -- is a Division_Check and not an Overflow_Check. | |
f3ccbbb3 | 1918 | |
f81a201b | 1919 | and then Do_Division_Check (N) |
1920 | then | |
1921 | Set_Do_Division_Check (N, False); | |
75f4b34c | 1922 | |
f81a201b | 1923 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then |
1924 | if Is_Floating_Point_Type (Etype (N)) then | |
1925 | Opnd := Make_Real_Literal (Loc, Ureal_0); | |
1926 | else | |
1927 | Opnd := Make_Integer_Literal (Loc, 0); | |
2fe22c69 | 1928 | end if; |
f81a201b | 1929 | |
1930 | Insert_Action (N, | |
1931 | Make_Raise_Constraint_Error (Loc, | |
1932 | Condition => | |
1933 | Make_Op_Eq (Loc, | |
1934 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1935 | Right_Opnd => Opnd), | |
1936 | Reason => CE_Divide_By_Zero)); | |
2fe22c69 | 1937 | end if; |
1938 | end if; | |
1939 | end Apply_Division_Check; | |
ee6ba406 | 1940 | |
5329ca64 | 1941 | ---------------------------------- |
1942 | -- Apply_Float_Conversion_Check -- | |
1943 | ---------------------------------- | |
1944 | ||
feff2f05 | 1945 | -- Let F and I be the source and target types of the conversion. The RM |
1946 | -- specifies that a floating-point value X is rounded to the nearest | |
1947 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1948 | -- value of X is checked against I'Range. | |
1949 | ||
1950 | -- The catch in the above paragraph is that there is no good way to know | |
1951 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1952 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1953 | |
5329ca64 | 1954 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1955 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1956 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1957 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1958 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1959 | -- sign of I'First and I'Last. | |
5329ca64 | 1960 | -- (5) X may be a NaN, which will fail any comparison |
1961 | ||
2af58f67 | 1962 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1963 | |
5329ca64 | 1964 | -- (1) If either I'First or I'Last is not known at compile time, use |
1965 | -- I'Base instead of I in the next three steps and perform a | |
1966 | -- regular range check against I'Range after conversion. | |
1967 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1968 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1969 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1970 | -- In other words, take one of the closest floating-point numbers | |
1971 | -- (which is an integer value) to I'First, and see if it is in | |
1972 | -- range or not. | |
5329ca64 | 1973 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1974 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1975 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1976 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1977 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1978 | ||
2af58f67 | 1979 | -- For the truncating case, replace steps (2) and (3) as follows: |
1980 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1981 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1982 | -- Lo_OK be True. | |
1983 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1984 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1985 | -- Hi_OK be True. |
2af58f67 | 1986 | |
5329ca64 | 1987 | procedure Apply_Float_Conversion_Check |
1988 | (Ck_Node : Node_Id; | |
1989 | Target_Typ : Entity_Id) | |
1990 | is | |
feff2f05 | 1991 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1992 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1993 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1994 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1995 | Target_Base : constant Entity_Id := |
b6341c67 | 1996 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1997 | |
2af58f67 | 1998 | Par : constant Node_Id := Parent (Ck_Node); |
1999 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
2000 | -- Parent of check node, must be a type conversion | |
2001 | ||
2002 | Truncate : constant Boolean := Float_Truncate (Par); | |
2003 | Max_Bound : constant Uint := | |
b6341c67 | 2004 | UI_Expon |
2005 | (Machine_Radix_Value (Expr_Type), | |
2006 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 2007 | |
5329ca64 | 2008 | -- Largest bound, so bound plus or minus half is a machine number of F |
2009 | ||
feff2f05 | 2010 | Ifirst, Ilast : Uint; |
2011 | -- Bounds of integer type | |
2012 | ||
2013 | Lo, Hi : Ureal; | |
2014 | -- Bounds to check in floating-point domain | |
5329ca64 | 2015 | |
feff2f05 | 2016 | Lo_OK, Hi_OK : Boolean; |
2017 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 2018 | |
feff2f05 | 2019 | Lo_Chk, Hi_Chk : Node_Id; |
2020 | -- Expressions that are False iff check fails | |
2021 | ||
2022 | Reason : RT_Exception_Code; | |
5329ca64 | 2023 | |
2024 | begin | |
41f06abf | 2025 | -- We do not need checks if we are not generating code (i.e. the full |
2026 | -- expander is not active). In SPARK mode, we specifically don't want | |
2027 | -- the frontend to expand these checks, which are dealt with directly | |
2028 | -- in the formal verification backend. | |
2029 | ||
a33565dd | 2030 | if not Expander_Active then |
41f06abf | 2031 | return; |
2032 | end if; | |
2033 | ||
5329ca64 | 2034 | if not Compile_Time_Known_Value (LB) |
2035 | or not Compile_Time_Known_Value (HB) | |
2036 | then | |
2037 | declare | |
feff2f05 | 2038 | -- First check that the value falls in the range of the base type, |
2039 | -- to prevent overflow during conversion and then perform a | |
2040 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 2041 | |
5329ca64 | 2042 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 2043 | |
46eb6933 | 2044 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 2045 | |
2046 | begin | |
2047 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
2048 | Set_Etype (Temp, Target_Base); | |
2049 | ||
2050 | Insert_Action (Parent (Par), | |
2051 | Make_Object_Declaration (Loc, | |
2052 | Defining_Identifier => Temp, | |
2053 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
2054 | Expression => New_Copy_Tree (Par)), | |
2055 | Suppress => All_Checks); | |
2056 | ||
2057 | Insert_Action (Par, | |
2058 | Make_Raise_Constraint_Error (Loc, | |
2059 | Condition => | |
2060 | Make_Not_In (Loc, | |
2061 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
2062 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
2063 | Reason => CE_Range_Check_Failed)); | |
2064 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
2065 | ||
2066 | return; | |
2067 | end; | |
2068 | end if; | |
2069 | ||
7d86aa98 | 2070 | -- Get the (static) bounds of the target type |
5329ca64 | 2071 | |
2072 | Ifirst := Expr_Value (LB); | |
2073 | Ilast := Expr_Value (HB); | |
2074 | ||
7d86aa98 | 2075 | -- A simple optimization: if the expression is a universal literal, |
2076 | -- we can do the comparison with the bounds and the conversion to | |
2077 | -- an integer type statically. The range checks are unchanged. | |
2078 | ||
2079 | if Nkind (Ck_Node) = N_Real_Literal | |
2080 | and then Etype (Ck_Node) = Universal_Real | |
2081 | and then Is_Integer_Type (Target_Typ) | |
2082 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
2083 | then | |
2084 | declare | |
2085 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
2086 | ||
2087 | begin | |
2088 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
2089 | ||
4309515d | 2090 | -- Conversion is safe |
7d86aa98 | 2091 | |
2092 | Rewrite (Parent (Ck_Node), | |
2093 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
2094 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
2095 | return; | |
2096 | end if; | |
2097 | end; | |
2098 | end if; | |
2099 | ||
5329ca64 | 2100 | -- Check against lower bound |
2101 | ||
2af58f67 | 2102 | if Truncate and then Ifirst > 0 then |
2103 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
2104 | Lo_OK := False; | |
2105 | ||
2106 | elsif Truncate then | |
2107 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
2108 | Lo_OK := True; | |
2109 | ||
2110 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 2111 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
2112 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 2113 | |
5329ca64 | 2114 | else |
2115 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
2116 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
2117 | end if; | |
2118 | ||
2119 | if Lo_OK then | |
2120 | ||
2121 | -- Lo_Chk := (X >= Lo) | |
2122 | ||
2123 | Lo_Chk := Make_Op_Ge (Loc, | |
2124 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2125 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2126 | ||
2127 | else | |
2128 | -- Lo_Chk := (X > Lo) | |
2129 | ||
2130 | Lo_Chk := Make_Op_Gt (Loc, | |
2131 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2132 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2133 | end if; | |
2134 | ||
2135 | -- Check against higher bound | |
2136 | ||
2af58f67 | 2137 | if Truncate and then Ilast < 0 then |
2138 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2139 | Hi_OK := False; |
2af58f67 | 2140 | |
2141 | elsif Truncate then | |
2142 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2143 | Hi_OK := True; | |
2144 | ||
2145 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2146 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2147 | Hi_OK := (Ilast < 0); | |
2148 | else | |
2149 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2150 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2151 | end if; | |
2152 | ||
2153 | if Hi_OK then | |
2154 | ||
2155 | -- Hi_Chk := (X <= Hi) | |
2156 | ||
2157 | Hi_Chk := Make_Op_Le (Loc, | |
2158 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2159 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2160 | ||
2161 | else | |
2162 | -- Hi_Chk := (X < Hi) | |
2163 | ||
2164 | Hi_Chk := Make_Op_Lt (Loc, | |
2165 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2166 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2167 | end if; | |
2168 | ||
feff2f05 | 2169 | -- If the bounds of the target type are the same as those of the base |
2170 | -- type, the check is an overflow check as a range check is not | |
2171 | -- performed in these cases. | |
5329ca64 | 2172 | |
2173 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2174 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2175 | then | |
2176 | Reason := CE_Overflow_Check_Failed; | |
2177 | else | |
2178 | Reason := CE_Range_Check_Failed; | |
2179 | end if; | |
2180 | ||
2181 | -- Raise CE if either conditions does not hold | |
2182 | ||
2183 | Insert_Action (Ck_Node, | |
2184 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2185 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2186 | Reason => Reason)); |
2187 | end Apply_Float_Conversion_Check; | |
2188 | ||
ee6ba406 | 2189 | ------------------------ |
2190 | -- Apply_Length_Check -- | |
2191 | ------------------------ | |
2192 | ||
2193 | procedure Apply_Length_Check | |
2194 | (Ck_Node : Node_Id; | |
2195 | Target_Typ : Entity_Id; | |
2196 | Source_Typ : Entity_Id := Empty) | |
2197 | is | |
2198 | begin | |
2199 | Apply_Selected_Length_Checks | |
2200 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2201 | end Apply_Length_Check; | |
2202 | ||
3b045963 | 2203 | ------------------------------------- |
2204 | -- Apply_Parameter_Aliasing_Checks -- | |
2205 | ------------------------------------- | |
b73adb97 | 2206 | |
3b045963 | 2207 | procedure Apply_Parameter_Aliasing_Checks |
2208 | (Call : Node_Id; | |
2209 | Subp : Entity_Id) | |
2210 | is | |
bb569db0 | 2211 | Loc : constant Source_Ptr := Sloc (Call); |
2212 | ||
3b045963 | 2213 | function May_Cause_Aliasing |
2214 | (Formal_1 : Entity_Id; | |
2215 | Formal_2 : Entity_Id) return Boolean; | |
2216 | -- Determine whether two formal parameters can alias each other | |
2217 | -- depending on their modes. | |
2218 | ||
2219 | function Original_Actual (N : Node_Id) return Node_Id; | |
2220 | -- The expander may replace an actual with a temporary for the sake of | |
2221 | -- side effect removal. The temporary may hide a potential aliasing as | |
2222 | -- it does not share the address of the actual. This routine attempts | |
2223 | -- to retrieve the original actual. | |
2224 | ||
bb569db0 | 2225 | procedure Overlap_Check |
2226 | (Actual_1 : Node_Id; | |
2227 | Actual_2 : Node_Id; | |
2228 | Formal_1 : Entity_Id; | |
2229 | Formal_2 : Entity_Id; | |
2230 | Check : in out Node_Id); | |
2231 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2232 | -- If detailed exception messages are enabled, the check is augmented to | |
2233 | -- provide information about the names of the corresponding formals. See | |
2234 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2235 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2236 | -- Check contains all and-ed simple tests generated so far or remains | |
2237 | -- unchanged in the case of detailed exception messaged. | |
2238 | ||
3b045963 | 2239 | ------------------------ |
2240 | -- May_Cause_Aliasing -- | |
2241 | ------------------------ | |
b73adb97 | 2242 | |
3b045963 | 2243 | function May_Cause_Aliasing |
4a9e7f0c | 2244 | (Formal_1 : Entity_Id; |
3b045963 | 2245 | Formal_2 : Entity_Id) return Boolean |
2246 | is | |
2247 | begin | |
2248 | -- The following combination cannot lead to aliasing | |
2249 | ||
2250 | -- Formal 1 Formal 2 | |
2251 | -- IN IN | |
2252 | ||
2253 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2254 | and then |
2255 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2256 | then |
2257 | return False; | |
2258 | ||
2259 | -- The following combinations may lead to aliasing | |
2260 | ||
2261 | -- Formal 1 Formal 2 | |
2262 | -- IN OUT | |
2263 | -- IN IN OUT | |
2264 | -- OUT IN | |
2265 | -- OUT IN OUT | |
2266 | -- OUT OUT | |
2267 | ||
2268 | else | |
2269 | return True; | |
2270 | end if; | |
2271 | end May_Cause_Aliasing; | |
2272 | ||
2273 | --------------------- | |
2274 | -- Original_Actual -- | |
2275 | --------------------- | |
2276 | ||
2277 | function Original_Actual (N : Node_Id) return Node_Id is | |
2278 | begin | |
2279 | if Nkind (N) = N_Type_Conversion then | |
2280 | return Expression (N); | |
2281 | ||
2282 | -- The expander created a temporary to capture the result of a type | |
2283 | -- conversion where the expression is the real actual. | |
2284 | ||
2285 | elsif Nkind (N) = N_Identifier | |
2286 | and then Present (Original_Node (N)) | |
2287 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2288 | then | |
2289 | return Expression (Original_Node (N)); | |
2290 | end if; | |
2291 | ||
2292 | return N; | |
2293 | end Original_Actual; | |
2294 | ||
bb569db0 | 2295 | ------------------- |
2296 | -- Overlap_Check -- | |
2297 | ------------------- | |
2298 | ||
2299 | procedure Overlap_Check | |
2300 | (Actual_1 : Node_Id; | |
2301 | Actual_2 : Node_Id; | |
2302 | Formal_1 : Entity_Id; | |
2303 | Formal_2 : Entity_Id; | |
2304 | Check : in out Node_Id) | |
2305 | is | |
29448168 | 2306 | Cond : Node_Id; |
2307 | ID_Casing : constant Casing_Type := | |
2308 | Identifier_Casing (Source_Index (Current_Sem_Unit)); | |
bb569db0 | 2309 | |
2310 | begin | |
2311 | -- Generate: | |
2312 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2313 | ||
2314 | Cond := | |
2315 | Make_Attribute_Reference (Loc, | |
2316 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2317 | Attribute_Name => Name_Overlaps_Storage, | |
2318 | Expressions => | |
2319 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2320 | ||
2321 | -- Generate the following check when detailed exception messages are | |
2322 | -- enabled: | |
2323 | ||
2324 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2325 | -- raise Program_Error with <detailed message>; | |
2326 | -- end if; | |
2327 | ||
2328 | if Exception_Extra_Info then | |
2329 | Start_String; | |
2330 | ||
2331 | -- Do not generate location information for internal calls | |
2332 | ||
2333 | if Comes_From_Source (Call) then | |
2334 | Store_String_Chars (Build_Location_String (Loc)); | |
2335 | Store_String_Char (' '); | |
2336 | end if; | |
2337 | ||
2338 | Store_String_Chars ("aliased parameters, actuals for """); | |
29448168 | 2339 | |
2340 | Get_Name_String (Chars (Formal_1)); | |
2341 | Set_Casing (ID_Casing); | |
2342 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2343 | ||
bb569db0 | 2344 | Store_String_Chars (""" and """); |
29448168 | 2345 | |
2346 | Get_Name_String (Chars (Formal_2)); | |
2347 | Set_Casing (ID_Casing); | |
2348 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2349 | ||
bb569db0 | 2350 | Store_String_Chars (""" overlap"); |
2351 | ||
2352 | Insert_Action (Call, | |
2353 | Make_If_Statement (Loc, | |
2354 | Condition => Cond, | |
2355 | Then_Statements => New_List ( | |
2356 | Make_Raise_Statement (Loc, | |
2357 | Name => | |
83c6c069 | 2358 | New_Occurrence_Of (Standard_Program_Error, Loc), |
bb569db0 | 2359 | Expression => Make_String_Literal (Loc, End_String))))); |
2360 | ||
2361 | -- Create a sequence of overlapping checks by and-ing them all | |
2362 | -- together. | |
2363 | ||
2364 | else | |
2365 | if No (Check) then | |
2366 | Check := Cond; | |
2367 | else | |
2368 | Check := | |
2369 | Make_And_Then (Loc, | |
2370 | Left_Opnd => Check, | |
2371 | Right_Opnd => Cond); | |
2372 | end if; | |
2373 | end if; | |
2374 | end Overlap_Check; | |
2375 | ||
3b045963 | 2376 | -- Local variables |
2377 | ||
15fca308 | 2378 | Actual_1 : Node_Id; |
2379 | Actual_2 : Node_Id; | |
2380 | Check : Node_Id; | |
2381 | Formal_1 : Entity_Id; | |
2382 | Formal_2 : Entity_Id; | |
2383 | Orig_Act_1 : Node_Id; | |
2384 | Orig_Act_2 : Node_Id; | |
3b045963 | 2385 | |
2386 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2387 | ||
2388 | begin | |
bb569db0 | 2389 | Check := Empty; |
3b045963 | 2390 | |
2391 | Actual_1 := First_Actual (Call); | |
2392 | Formal_1 := First_Formal (Subp); | |
2393 | while Present (Actual_1) and then Present (Formal_1) loop | |
15fca308 | 2394 | Orig_Act_1 := Original_Actual (Actual_1); |
3b045963 | 2395 | |
2396 | -- Ensure that the actual is an object that is not passed by value. | |
2397 | -- Elementary types are always passed by value, therefore actuals of | |
747426db | 2398 | -- such types cannot lead to aliasing. An aggregate is an object in |
2399 | -- Ada 2012, but an actual that is an aggregate cannot overlap with | |
4dc3174c | 2400 | -- another actual. A type that is By_Reference (such as an array of |
2401 | -- controlled types) is not subject to the check because any update | |
2402 | -- will be done in place and a subsequent read will always see the | |
2403 | -- correct value, see RM 6.2 (12/3). | |
3b045963 | 2404 | |
15fca308 | 2405 | if Nkind (Orig_Act_1) = N_Aggregate |
2406 | or else (Nkind (Orig_Act_1) = N_Qualified_Expression | |
2407 | and then Nkind (Expression (Orig_Act_1)) = N_Aggregate) | |
747426db | 2408 | then |
2409 | null; | |
2410 | ||
15fca308 | 2411 | elsif Is_Object_Reference (Orig_Act_1) |
2412 | and then not Is_Elementary_Type (Etype (Orig_Act_1)) | |
2413 | and then not Is_By_Reference_Type (Etype (Orig_Act_1)) | |
3b045963 | 2414 | then |
2415 | Actual_2 := Next_Actual (Actual_1); | |
2416 | Formal_2 := Next_Formal (Formal_1); | |
2417 | while Present (Actual_2) and then Present (Formal_2) loop | |
15fca308 | 2418 | Orig_Act_2 := Original_Actual (Actual_2); |
3b045963 | 2419 | |
2420 | -- The other actual we are testing against must also denote | |
2421 | -- a non pass-by-value object. Generate the check only when | |
2422 | -- the mode of the two formals may lead to aliasing. | |
2423 | ||
15fca308 | 2424 | if Is_Object_Reference (Orig_Act_2) |
2425 | and then not Is_Elementary_Type (Etype (Orig_Act_2)) | |
3b045963 | 2426 | and then May_Cause_Aliasing (Formal_1, Formal_2) |
2427 | then | |
72f889fa | 2428 | Remove_Side_Effects (Actual_1); |
2429 | Remove_Side_Effects (Actual_2); | |
2430 | ||
bb569db0 | 2431 | Overlap_Check |
2432 | (Actual_1 => Actual_1, | |
2433 | Actual_2 => Actual_2, | |
2434 | Formal_1 => Formal_1, | |
2435 | Formal_2 => Formal_2, | |
2436 | Check => Check); | |
3b045963 | 2437 | end if; |
2438 | ||
2439 | Next_Actual (Actual_2); | |
2440 | Next_Formal (Formal_2); | |
2441 | end loop; | |
2442 | end if; | |
2443 | ||
2444 | Next_Actual (Actual_1); | |
2445 | Next_Formal (Formal_1); | |
2446 | end loop; | |
2447 | ||
bb569db0 | 2448 | -- Place a simple check right before the call |
3b045963 | 2449 | |
bb569db0 | 2450 | if Present (Check) and then not Exception_Extra_Info then |
3b045963 | 2451 | Insert_Action (Call, |
2452 | Make_Raise_Program_Error (Loc, | |
bb569db0 | 2453 | Condition => Check, |
2454 | Reason => PE_Aliased_Parameters)); | |
3b045963 | 2455 | end if; |
2456 | end Apply_Parameter_Aliasing_Checks; | |
2457 | ||
2458 | ------------------------------------- | |
2459 | -- Apply_Parameter_Validity_Checks -- | |
2460 | ------------------------------------- | |
2461 | ||
2462 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2463 | Subp_Decl : Node_Id; | |
b73adb97 | 2464 | |
4a9e7f0c | 2465 | procedure Add_Validity_Check |
ed695684 | 2466 | (Formal : Entity_Id; |
2467 | Prag_Nam : Name_Id; | |
4a9e7f0c | 2468 | For_Result : Boolean := False); |
2469 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
ed695684 | 2470 | -- of Formal. Prag_Nam denotes the pre or post condition pragma name. |
4a9e7f0c | 2471 | -- Set flag For_Result when to verify the result of a function. |
b73adb97 | 2472 | |
b73adb97 | 2473 | ------------------------ |
2474 | -- Add_Validity_Check -- | |
2475 | ------------------------ | |
2476 | ||
2477 | procedure Add_Validity_Check | |
ed695684 | 2478 | (Formal : Entity_Id; |
2479 | Prag_Nam : Name_Id; | |
b73adb97 | 2480 | For_Result : Boolean := False) |
2481 | is | |
ed695684 | 2482 | procedure Build_Pre_Post_Condition (Expr : Node_Id); |
2483 | -- Create a pre/postcondition pragma that tests expression Expr | |
2484 | ||
2485 | ------------------------------ | |
2486 | -- Build_Pre_Post_Condition -- | |
2487 | ------------------------------ | |
2488 | ||
2489 | procedure Build_Pre_Post_Condition (Expr : Node_Id) is | |
2490 | Loc : constant Source_Ptr := Sloc (Subp); | |
2491 | Decls : List_Id; | |
2492 | Prag : Node_Id; | |
2493 | ||
2494 | begin | |
2495 | Prag := | |
2496 | Make_Pragma (Loc, | |
c33cb5ff | 2497 | Chars => Prag_Nam, |
ed695684 | 2498 | Pragma_Argument_Associations => New_List ( |
2499 | Make_Pragma_Argument_Association (Loc, | |
2500 | Chars => Name_Check, | |
2501 | Expression => Expr))); | |
2502 | ||
2503 | -- Add a message unless exception messages are suppressed | |
2504 | ||
2505 | if not Exception_Locations_Suppressed then | |
2506 | Append_To (Pragma_Argument_Associations (Prag), | |
2507 | Make_Pragma_Argument_Association (Loc, | |
2508 | Chars => Name_Message, | |
2509 | Expression => | |
2510 | Make_String_Literal (Loc, | |
2511 | Strval => "failed " | |
2512 | & Get_Name_String (Prag_Nam) | |
2513 | & " from " | |
2514 | & Build_Location_String (Loc)))); | |
2515 | end if; | |
2516 | ||
2517 | -- Insert the pragma in the tree | |
2518 | ||
2519 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2520 | Add_Global_Declaration (Prag); | |
2521 | Analyze (Prag); | |
2522 | ||
2523 | -- PPC pragmas associated with subprogram bodies must be inserted | |
2524 | -- in the declarative part of the body. | |
2525 | ||
2526 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2527 | Decls := Declarations (Subp_Decl); | |
2528 | ||
2529 | if No (Decls) then | |
2530 | Decls := New_List; | |
2531 | Set_Declarations (Subp_Decl, Decls); | |
2532 | end if; | |
2533 | ||
2534 | Prepend_To (Decls, Prag); | |
2535 | Analyze (Prag); | |
2536 | ||
2537 | -- For subprogram declarations insert the PPC pragma right after | |
2538 | -- the declarative node. | |
2539 | ||
2540 | else | |
2541 | Insert_After_And_Analyze (Subp_Decl, Prag); | |
2542 | end if; | |
2543 | end Build_Pre_Post_Condition; | |
2544 | ||
2545 | -- Local variables | |
2546 | ||
4a9e7f0c | 2547 | Loc : constant Source_Ptr := Sloc (Subp); |
ed695684 | 2548 | Typ : constant Entity_Id := Etype (Formal); |
b73adb97 | 2549 | Check : Node_Id; |
2550 | Nam : Name_Id; | |
2551 | ||
ed695684 | 2552 | -- Start of processing for Add_Validity_Check |
2553 | ||
b73adb97 | 2554 | begin |
9916a361 | 2555 | -- For scalars, generate 'Valid test |
b73adb97 | 2556 | |
2557 | if Is_Scalar_Type (Typ) then | |
2558 | Nam := Name_Valid; | |
9916a361 | 2559 | |
2560 | -- For any non-scalar with scalar parts, generate 'Valid_Scalars test | |
2561 | ||
2562 | elsif Scalar_Part_Present (Typ) then | |
b73adb97 | 2563 | Nam := Name_Valid_Scalars; |
9916a361 | 2564 | |
2565 | -- No test needed for other cases (no scalars to test) | |
2566 | ||
b73adb97 | 2567 | else |
2568 | return; | |
2569 | end if; | |
2570 | ||
2571 | -- Step 1: Create the expression to verify the validity of the | |
2572 | -- context. | |
2573 | ||
ed695684 | 2574 | Check := New_Occurrence_Of (Formal, Loc); |
b73adb97 | 2575 | |
2576 | -- When processing a function result, use 'Result. Generate | |
2577 | -- Context'Result | |
2578 | ||
2579 | if For_Result then | |
2580 | Check := | |
2581 | Make_Attribute_Reference (Loc, | |
2582 | Prefix => Check, | |
2583 | Attribute_Name => Name_Result); | |
2584 | end if; | |
2585 | ||
2586 | -- Generate: | |
2587 | -- Context['Result]'Valid[_Scalars] | |
2588 | ||
2589 | Check := | |
2590 | Make_Attribute_Reference (Loc, | |
2591 | Prefix => Check, | |
2592 | Attribute_Name => Nam); | |
2593 | ||
4a9e7f0c | 2594 | -- Step 2: Create a pre or post condition pragma |
2595 | ||
ed695684 | 2596 | Build_Pre_Post_Condition (Check); |
4a9e7f0c | 2597 | end Add_Validity_Check; |
2598 | ||
4a9e7f0c | 2599 | -- Local variables |
2600 | ||
2601 | Formal : Entity_Id; | |
4a9e7f0c | 2602 | Subp_Spec : Node_Id; |
2603 | ||
3b045963 | 2604 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2605 | |
2606 | begin | |
4a9e7f0c | 2607 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2608 | |
4a9e7f0c | 2609 | Subp_Spec := Parent (Subp); |
a45d946f | 2610 | |
4a9e7f0c | 2611 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2612 | Subp_Spec := Parent (Subp_Spec); | |
2613 | end if; | |
a45d946f | 2614 | |
4a9e7f0c | 2615 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2616 | |
b73adb97 | 2617 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2618 | |
2619 | -- Do not process formal subprograms because the corresponding actual | |
2620 | -- will receive the proper checks when the instance is analyzed. | |
2621 | ||
2622 | or else Is_Formal_Subprogram (Subp) | |
2623 | ||
95ac2d90 | 2624 | -- Do not process imported subprograms since pre and postconditions |
a45d946f | 2625 | -- are never verified on routines coming from a different language. |
4a9e7f0c | 2626 | |
b73adb97 | 2627 | or else Is_Imported (Subp) |
2628 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2629 | |
a45d946f | 2630 | -- The PPC pragmas generated by this routine do not correspond to |
2631 | -- source aspects, therefore they cannot be applied to abstract | |
2632 | -- subprograms. | |
4a9e7f0c | 2633 | |
7c443ae8 | 2634 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2635 | |
a45d946f | 2636 | -- Do not consider subprogram renaminds because the renamed entity |
2637 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2638 | |
2639 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2640 | ||
a45d946f | 2641 | -- Do not process null procedures because there is no benefit of |
2642 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2643 | |
2644 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2645 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2646 | then |
2647 | return; | |
2648 | end if; | |
2649 | ||
4a9e7f0c | 2650 | -- Inspect all the formals applying aliasing and scalar initialization |
2651 | -- checks where applicable. | |
b73adb97 | 2652 | |
2653 | Formal := First_Formal (Subp); | |
2654 | while Present (Formal) loop | |
4a9e7f0c | 2655 | |
2656 | -- Generate the following scalar initialization checks for each | |
2657 | -- formal parameter: | |
2658 | ||
2659 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2660 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2661 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2662 | ||
2663 | if Check_Validity_Of_Parameters then | |
2664 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2665 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2666 | end if; | |
2667 | ||
2668 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2669 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2670 | end if; | |
b73adb97 | 2671 | end if; |
2672 | ||
b73adb97 | 2673 | Next_Formal (Formal); |
2674 | end loop; | |
2675 | ||
a45d946f | 2676 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2677 | |
2678 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2679 | |
a45d946f | 2680 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2681 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2682 | end if; |
3b045963 | 2683 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2684 | |
7aafae1c | 2685 | --------------------------- |
2686 | -- Apply_Predicate_Check -- | |
2687 | --------------------------- | |
2688 | ||
e60047e5 | 2689 | procedure Apply_Predicate_Check |
2690 | (N : Node_Id; | |
2691 | Typ : Entity_Id; | |
2692 | Fun : Entity_Id := Empty) | |
2693 | is | |
301d5ec3 | 2694 | S : Entity_Id; |
9e58d7ed | 2695 | |
7aafae1c | 2696 | begin |
da2270e7 | 2697 | if Predicate_Checks_Suppressed (Empty) then |
2698 | return; | |
301d5ec3 | 2699 | |
2c011bc5 | 2700 | elsif Predicates_Ignored (Typ) then |
2701 | return; | |
2702 | ||
da2270e7 | 2703 | elsif Present (Predicate_Function (Typ)) then |
301d5ec3 | 2704 | S := Current_Scope; |
9e58d7ed | 2705 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2706 | S := Scope (S); |
2707 | end loop; | |
2708 | ||
ea822fd4 | 2709 | -- A predicate check does not apply within internally generated |
2710 | -- subprograms, such as TSS functions. | |
2711 | ||
2712 | if Within_Internal_Subprogram then | |
301d5ec3 | 2713 | return; |
22631b41 | 2714 | |
96a2d100 | 2715 | -- If the check appears within the predicate function itself, it |
2716 | -- means that the user specified a check whose formal is the | |
2717 | -- predicated subtype itself, rather than some covering type. This | |
2718 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2719 | |
0e9014a7 | 2720 | elsif Present (S) and then S = Predicate_Function (Typ) then |
e60047e5 | 2721 | Error_Msg_NE |
0c4abd51 | 2722 | ("predicate check includes a call to& that requires a " |
2723 | & "predicate check??", Parent (N), Fun); | |
96a2d100 | 2724 | Error_Msg_N |
cb97ae5c | 2725 | ("\this will result in infinite recursion??", Parent (N)); |
e60047e5 | 2726 | |
2727 | if Is_First_Subtype (Typ) then | |
2728 | Error_Msg_NE | |
0c4abd51 | 2729 | ("\use an explicit subtype of& to carry the predicate", |
e60047e5 | 2730 | Parent (N), Typ); |
2731 | end if; | |
2732 | ||
96a2d100 | 2733 | Insert_Action (N, |
61016a7a | 2734 | Make_Raise_Storage_Error (Sloc (N), |
2735 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2736 | |
64cc9e5d | 2737 | -- Here for normal case of predicate active |
e6281d47 | 2738 | |
61016a7a | 2739 | else |
164597c5 | 2740 | -- If the expression is an IN parameter, the predicate will have |
2741 | -- been applied at the point of call. An additional check would | |
2742 | -- be redundant, or will lead to out-of-scope references if the | |
2743 | -- call appears within an aspect specification for a precondition. | |
2744 | ||
2745 | -- However, if the reference is within the body of the subprogram | |
2746 | -- that declares the formal, the predicate can safely be applied, | |
2747 | -- which may be necessary for a nested call whose formal has a | |
2748 | -- different predicate. | |
2749 | ||
2750 | if Is_Entity_Name (N) | |
2751 | and then Ekind (Entity (N)) = E_In_Parameter | |
2752 | then | |
2753 | declare | |
2754 | In_Body : Boolean := False; | |
50948c8a | 2755 | P : Node_Id := Parent (N); |
164597c5 | 2756 | |
2757 | begin | |
2758 | while Present (P) loop | |
2759 | if Nkind (P) = N_Subprogram_Body | |
2760 | and then Corresponding_Spec (P) = Scope (Entity (N)) | |
2761 | then | |
2762 | In_Body := True; | |
2763 | exit; | |
2764 | end if; | |
2765 | ||
2766 | P := Parent (P); | |
2767 | end loop; | |
2768 | ||
2769 | if not In_Body then | |
2770 | return; | |
2771 | end if; | |
2772 | end; | |
2773 | end if; | |
2774 | ||
b04165c4 | 2775 | -- If the type has a static predicate and the expression is known |
2776 | -- at compile time, see if the expression satisfies the predicate. | |
3a75f20b | 2777 | |
2778 | Check_Expression_Against_Static_Predicate (N, Typ); | |
e6281d47 | 2779 | |
e34cd69d | 2780 | if not Expander_Active then |
2781 | return; | |
2782 | end if; | |
2783 | ||
2784 | -- For an entity of the type, generate a call to the predicate | |
2785 | -- function, unless its type is an actual subtype, which is not | |
2786 | -- visible outside of the enclosing subprogram. | |
2787 | ||
2788 | if Is_Entity_Name (N) | |
2789 | and then not Is_Actual_Subtype (Typ) | |
2790 | then | |
da2270e7 | 2791 | Insert_Action (N, |
2792 | Make_Predicate_Check | |
2793 | (Typ, New_Occurrence_Of (Entity (N), Sloc (N)))); | |
2794 | ||
f9906591 | 2795 | -- If the expression is not an entity it may have side effects, |
bc885df9 | 2796 | -- and the following call will create an object declaration for |
2797 | -- it. We disable checks during its analysis, to prevent an | |
2798 | -- infinite recursion. | |
da2270e7 | 2799 | |
e3b910d8 | 2800 | -- If the prefix is an aggregate in an assignment, apply the |
2801 | -- check to the LHS after assignment, rather than create a | |
2802 | -- redundant temporary. This is only necessary in rare cases | |
2803 | -- of array types (including strings) initialized with an | |
2804 | -- aggregate with an "others" clause, either coming from source | |
2805 | -- or generated by an Initialize_Scalars pragma. | |
2806 | ||
2807 | elsif Nkind (N) = N_Aggregate | |
2808 | and then Nkind (Parent (N)) = N_Assignment_Statement | |
2809 | then | |
2810 | Insert_Action_After (Parent (N), | |
2811 | Make_Predicate_Check | |
2812 | (Typ, Duplicate_Subexpr (Name (Parent (N))))); | |
2813 | ||
da2270e7 | 2814 | else |
2815 | Insert_Action (N, | |
bc885df9 | 2816 | Make_Predicate_Check |
2817 | (Typ, Duplicate_Subexpr (N)), Suppress => All_Checks); | |
da2270e7 | 2818 | end if; |
301d5ec3 | 2819 | end if; |
7aafae1c | 2820 | end if; |
2821 | end Apply_Predicate_Check; | |
2822 | ||
ee6ba406 | 2823 | ----------------------- |
2824 | -- Apply_Range_Check -- | |
2825 | ----------------------- | |
2826 | ||
2827 | procedure Apply_Range_Check | |
2828 | (Ck_Node : Node_Id; | |
2829 | Target_Typ : Entity_Id; | |
2830 | Source_Typ : Entity_Id := Empty) | |
2831 | is | |
2832 | begin | |
2833 | Apply_Selected_Range_Checks | |
2834 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2835 | end Apply_Range_Check; | |
2836 | ||
2837 | ------------------------------ | |
2838 | -- Apply_Scalar_Range_Check -- | |
2839 | ------------------------------ | |
2840 | ||
feff2f05 | 2841 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2842 | -- off if it is already set on. | |
ee6ba406 | 2843 | |
2844 | procedure Apply_Scalar_Range_Check | |
2845 | (Expr : Node_Id; | |
2846 | Target_Typ : Entity_Id; | |
2847 | Source_Typ : Entity_Id := Empty; | |
2848 | Fixed_Int : Boolean := False) | |
2849 | is | |
2850 | Parnt : constant Node_Id := Parent (Expr); | |
2851 | S_Typ : Entity_Id; | |
2852 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2853 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
ee6ba406 | 2854 | |
2855 | Is_Subscr_Ref : Boolean; | |
2856 | -- Set true if Expr is a subscript | |
2857 | ||
2858 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2859 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2860 | -- case we do not attempt to do an analysis of the value against the | |
2861 | -- range of the subscript, since we don't know the actual subtype. | |
2862 | ||
2863 | Int_Real : Boolean; | |
feff2f05 | 2864 | -- Set to True if Expr should be regarded as a real value even though |
2865 | -- the type of Expr might be discrete. | |
ee6ba406 | 2866 | |
a0a15971 | 2867 | procedure Bad_Value (Warn : Boolean := False); |
2868 | -- Procedure called if value is determined to be out of range. Warn is | |
2869 | -- True to force a warning instead of an error, even when SPARK_Mode is | |
2870 | -- On. | |
ee6ba406 | 2871 | |
9dfe12ae | 2872 | --------------- |
2873 | -- Bad_Value -- | |
2874 | --------------- | |
2875 | ||
a0a15971 | 2876 | procedure Bad_Value (Warn : Boolean := False) is |
ee6ba406 | 2877 | begin |
2878 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2879 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
a0a15971 | 2880 | Ent => Target_Typ, |
2881 | Typ => Target_Typ, | |
2882 | Warn => Warn); | |
ee6ba406 | 2883 | end Bad_Value; |
2884 | ||
9dfe12ae | 2885 | -- Start of processing for Apply_Scalar_Range_Check |
2886 | ||
ee6ba406 | 2887 | begin |
2af58f67 | 2888 | -- Return if check obviously not needed |
ee6ba406 | 2889 | |
2af58f67 | 2890 | if |
2891 | -- Not needed inside generic | |
ee6ba406 | 2892 | |
2af58f67 | 2893 | Inside_A_Generic |
2894 | ||
2895 | -- Not needed if previous error | |
2896 | ||
2897 | or else Target_Typ = Any_Type | |
2898 | or else Nkind (Expr) = N_Error | |
2899 | ||
2900 | -- Not needed for non-scalar type | |
2901 | ||
2902 | or else not Is_Scalar_Type (Target_Typ) | |
2903 | ||
2904 | -- Not needed if we know node raises CE already | |
2905 | ||
2906 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2907 | then |
2908 | return; | |
2909 | end if; | |
2910 | ||
2911 | -- Now, see if checks are suppressed | |
2912 | ||
2913 | Is_Subscr_Ref := | |
2914 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2915 | ||
2916 | if Is_Subscr_Ref then | |
2917 | Arr := Prefix (Parnt); | |
2918 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2919 | |
a3a76ccc | 2920 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2921 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2922 | end if; |
ee6ba406 | 2923 | end if; |
2924 | ||
2925 | if not Do_Range_Check (Expr) then | |
2926 | ||
2927 | -- Subscript reference. Check for Index_Checks suppressed | |
2928 | ||
2929 | if Is_Subscr_Ref then | |
2930 | ||
2931 | -- Check array type and its base type | |
2932 | ||
2933 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2934 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2935 | then |
2936 | return; | |
2937 | ||
2938 | -- Check array itself if it is an entity name | |
2939 | ||
2940 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2941 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2942 | then |
2943 | return; | |
2944 | ||
2945 | -- Check expression itself if it is an entity name | |
2946 | ||
2947 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2948 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2949 | then |
2950 | return; | |
2951 | end if; | |
2952 | ||
2953 | -- All other cases, check for Range_Checks suppressed | |
2954 | ||
2955 | else | |
2956 | -- Check target type and its base type | |
2957 | ||
2958 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2959 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2960 | then |
2961 | return; | |
2962 | ||
2963 | -- Check expression itself if it is an entity name | |
2964 | ||
2965 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2966 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2967 | then |
2968 | return; | |
2969 | ||
feff2f05 | 2970 | -- If Expr is part of an assignment statement, then check left |
2971 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2972 | |
2973 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2974 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2975 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2976 | then |
2977 | return; | |
2978 | end if; | |
2979 | end if; | |
2980 | end if; | |
2981 | ||
9dfe12ae | 2982 | -- Do not set range checks if they are killed |
2983 | ||
2984 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2985 | and then Kill_Range_Check (Expr) | |
2986 | then | |
2987 | return; | |
2988 | end if; | |
2989 | ||
2990 | -- Do not set range checks for any values from System.Scalar_Values | |
39a0c1d3 | 2991 | -- since the whole idea of such values is to avoid checking them. |
9dfe12ae | 2992 | |
2993 | if Is_Entity_Name (Expr) | |
2994 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2995 | then | |
2996 | return; | |
2997 | end if; | |
2998 | ||
ee6ba406 | 2999 | -- Now see if we need a check |
3000 | ||
3001 | if No (Source_Typ) then | |
3002 | S_Typ := Etype (Expr); | |
3003 | else | |
3004 | S_Typ := Source_Typ; | |
3005 | end if; | |
3006 | ||
3007 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
3008 | return; | |
3009 | end if; | |
3010 | ||
3011 | Is_Unconstrained_Subscr_Ref := | |
3012 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
3013 | ||
b40670e1 | 3014 | -- Special checks for floating-point type |
ee6ba406 | 3015 | |
b40670e1 | 3016 | if Is_Floating_Point_Type (S_Typ) then |
3017 | ||
3018 | -- Always do a range check if the source type includes infinities and | |
3019 | -- the target type does not include infinities. We do not do this if | |
3020 | -- range checks are killed. | |
cb388b10 | 3021 | -- If the expression is a literal and the bounds of the type are |
3022 | -- static constants it may be possible to optimize the check. | |
b40670e1 | 3023 | |
3024 | if Has_Infinities (S_Typ) | |
3025 | and then not Has_Infinities (Target_Typ) | |
3026 | then | |
cb388b10 | 3027 | -- If the expression is a literal and the bounds of the type are |
3028 | -- static constants it may be possible to optimize the check. | |
3029 | ||
3030 | if Nkind (Expr) = N_Real_Literal then | |
3031 | declare | |
3032 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
3033 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
3034 | ||
3035 | begin | |
3036 | if Compile_Time_Known_Value (Tlo) | |
3037 | and then Compile_Time_Known_Value (Thi) | |
3038 | and then Expr_Value_R (Expr) >= Expr_Value_R (Tlo) | |
3039 | and then Expr_Value_R (Expr) <= Expr_Value_R (Thi) | |
3040 | then | |
3041 | return; | |
3042 | else | |
3043 | Enable_Range_Check (Expr); | |
3044 | end if; | |
3045 | end; | |
3046 | ||
3047 | else | |
3048 | Enable_Range_Check (Expr); | |
3049 | end if; | |
b40670e1 | 3050 | end if; |
ee6ba406 | 3051 | end if; |
3052 | ||
feff2f05 | 3053 | -- Return if we know expression is definitely in the range of the target |
3054 | -- type as determined by Determine_Range. Right now we only do this for | |
3055 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 3056 | |
f2a06be9 | 3057 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 3058 | |
54022749 | 3059 | -- In GNATprove_Mode, also deal with the case of a conversion from |
3060 | -- floating-point to integer. It is only possible because analysis | |
3061 | -- in GNATprove rules out the possibility of a NaN or infinite value. | |
3062 | ||
feff2f05 | 3063 | -- Note: skip this if we are given a source_typ, since the point of |
3064 | -- supplying a Source_Typ is to stop us looking at the expression. | |
3065 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 3066 | |
3067 | if Is_Discrete_Type (Target_Typ) | |
54022749 | 3068 | and then (Is_Discrete_Type (Etype (Expr)) |
3069 | or else (GNATprove_Mode | |
3070 | and then Is_Floating_Point_Type (Etype (Expr)))) | |
ee6ba406 | 3071 | and then not Is_Unconstrained_Subscr_Ref |
3072 | and then No (Source_Typ) | |
3073 | then | |
3074 | declare | |
ee6ba406 | 3075 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); |
552d7cbc | 3076 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); |
ee6ba406 | 3077 | |
3078 | begin | |
3079 | if Compile_Time_Known_Value (Tlo) | |
3080 | and then Compile_Time_Known_Value (Thi) | |
3081 | then | |
9dfe12ae | 3082 | declare |
5bb74b99 | 3083 | OK : Boolean := False; -- initialize to prevent warning |
9dfe12ae | 3084 | Hiv : constant Uint := Expr_Value (Thi); |
552d7cbc | 3085 | Lov : constant Uint := Expr_Value (Tlo); |
5bb74b99 | 3086 | Hi : Uint := No_Uint; |
3087 | Lo : Uint := No_Uint; | |
ee6ba406 | 3088 | |
9dfe12ae | 3089 | begin |
552d7cbc | 3090 | -- If range is null, we for sure have a constraint error (we |
3091 | -- don't even need to look at the value involved, since all | |
3092 | -- possible values will raise CE). | |
9dfe12ae | 3093 | |
3094 | if Lov > Hiv then | |
f4f2bf51 | 3095 | |
a0a15971 | 3096 | -- When SPARK_Mode is On, force a warning instead of |
3097 | -- an error in that case, as this likely corresponds | |
3098 | -- to deactivated code. | |
3099 | ||
3100 | Bad_Value (Warn => SPARK_Mode = On); | |
3101 | ||
3102 | -- In GNATprove mode, we enable the range check so that | |
3103 | -- GNATprove will issue a message if it cannot be proved. | |
f4f2bf51 | 3104 | |
3105 | if GNATprove_Mode then | |
3106 | Enable_Range_Check (Expr); | |
f4f2bf51 | 3107 | end if; |
3108 | ||
9dfe12ae | 3109 | return; |
3110 | end if; | |
3111 | ||
3112 | -- Otherwise determine range of value | |
3113 | ||
54022749 | 3114 | if Is_Discrete_Type (Etype (Expr)) then |
552d7cbc | 3115 | Determine_Range |
3116 | (Expr, OK, Lo, Hi, Assume_Valid => True); | |
54022749 | 3117 | |
3118 | -- When converting a float to an integer type, determine the | |
3119 | -- range in real first, and then convert the bounds using | |
3120 | -- UR_To_Uint which correctly rounds away from zero when | |
3121 | -- half way between two integers, as required by normal | |
3122 | -- Ada 95 rounding semantics. It is only possible because | |
3123 | -- analysis in GNATprove rules out the possibility of a NaN | |
3124 | -- or infinite value. | |
3125 | ||
3126 | elsif GNATprove_Mode | |
3127 | and then Is_Floating_Point_Type (Etype (Expr)) | |
3128 | then | |
3129 | declare | |
54022749 | 3130 | Hir : Ureal; |
552d7cbc | 3131 | Lor : Ureal; |
3132 | ||
54022749 | 3133 | begin |
552d7cbc | 3134 | Determine_Range_R |
3135 | (Expr, OK, Lor, Hir, Assume_Valid => True); | |
54022749 | 3136 | |
3137 | if OK then | |
3138 | Lo := UR_To_Uint (Lor); | |
3139 | Hi := UR_To_Uint (Hir); | |
3140 | end if; | |
3141 | end; | |
3142 | end if; | |
9dfe12ae | 3143 | |
3144 | if OK then | |
3145 | ||
3146 | -- If definitely in range, all OK | |
ee6ba406 | 3147 | |
ee6ba406 | 3148 | if Lo >= Lov and then Hi <= Hiv then |
3149 | return; | |
3150 | ||
9dfe12ae | 3151 | -- If definitely not in range, warn |
3152 | ||
ee6ba406 | 3153 | elsif Lov > Hi or else Hiv < Lo then |
e9dae6c3 | 3154 | |
c59854a0 | 3155 | -- Ignore out of range values for System.Priority in |
3156 | -- CodePeer mode since the actual target compiler may | |
3157 | -- provide a wider range. | |
3158 | ||
3159 | if not CodePeer_Mode | |
3160 | or else Target_Typ /= RTE (RE_Priority) | |
3161 | then | |
3162 | Bad_Value; | |
3163 | end if; | |
3164 | ||
ee6ba406 | 3165 | return; |
9dfe12ae | 3166 | |
3167 | -- Otherwise we don't know | |
3168 | ||
3169 | else | |
3170 | null; | |
ee6ba406 | 3171 | end if; |
9dfe12ae | 3172 | end if; |
3173 | end; | |
ee6ba406 | 3174 | end if; |
3175 | end; | |
3176 | end if; | |
3177 | ||
3178 | Int_Real := | |
3179 | Is_Floating_Point_Type (S_Typ) | |
3180 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
3181 | ||
3182 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 3183 | -- range of the target type. Note that if S_Typ is within the bounds |
3184 | -- of Target_Typ then this must be the case. This check is meaningful | |
3185 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 3186 | |
3187 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 3188 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 3189 | and then |
7a1dabb3 | 3190 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ac5f5168 | 3191 | |
3192 | -- Also check if the expression itself is in the range of the | |
3193 | -- target type if it is a known at compile time value. We skip | |
3194 | -- this test if S_Typ is set since for OUT and IN OUT parameters | |
3195 | -- the Expr itself is not relevant to the checking. | |
3196 | ||
ee6ba406 | 3197 | or else |
ac5f5168 | 3198 | (No (Source_Typ) |
3199 | and then Is_In_Range (Expr, Target_Typ, | |
3200 | Assume_Valid => True, | |
3201 | Fixed_Int => Fixed_Int, | |
3202 | Int_Real => Int_Real))) | |
ee6ba406 | 3203 | then |
3204 | return; | |
3205 | ||
9c486805 | 3206 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
3207 | Assume_Valid => True, | |
3208 | Fixed_Int => Fixed_Int, | |
3209 | Int_Real => Int_Real) | |
3210 | then | |
ee6ba406 | 3211 | Bad_Value; |
3212 | return; | |
3213 | ||
b40670e1 | 3214 | -- Floating-point case |
feff2f05 | 3215 | -- In the floating-point case, we only do range checks if the type is |
3216 | -- constrained. We definitely do NOT want range checks for unconstrained | |
29d958a7 | 3217 | -- types, since we want to have infinities, except when |
3218 | -- Check_Float_Overflow is set. | |
ee6ba406 | 3219 | |
9dfe12ae | 3220 | elsif Is_Floating_Point_Type (S_Typ) then |
29d958a7 | 3221 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then |
9dfe12ae | 3222 | Enable_Range_Check (Expr); |
3223 | end if; | |
ee6ba406 | 3224 | |
9dfe12ae | 3225 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 3226 | |
3227 | else | |
3228 | Enable_Range_Check (Expr); | |
3229 | return; | |
3230 | end if; | |
ee6ba406 | 3231 | end Apply_Scalar_Range_Check; |
3232 | ||
3233 | ---------------------------------- | |
3234 | -- Apply_Selected_Length_Checks -- | |
3235 | ---------------------------------- | |
3236 | ||
3237 | procedure Apply_Selected_Length_Checks | |
3238 | (Ck_Node : Node_Id; | |
3239 | Target_Typ : Entity_Id; | |
3240 | Source_Typ : Entity_Id; | |
3241 | Do_Static : Boolean) | |
3242 | is | |
2b4f2458 | 3243 | Checks_On : constant Boolean := |
3244 | not Index_Checks_Suppressed (Target_Typ) | |
3245 | or else | |
3246 | not Length_Checks_Suppressed (Target_Typ); | |
3247 | ||
3248 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
3249 | ||
ee6ba406 | 3250 | Cond : Node_Id; |
ee6ba406 | 3251 | R_Cno : Node_Id; |
2b4f2458 | 3252 | R_Result : Check_Result; |
ee6ba406 | 3253 | |
3254 | begin | |
18cb6d78 | 3255 | -- Only apply checks when generating code |
f0d65dae | 3256 | |
4098232e | 3257 | -- Note: this means that we lose some useful warnings if the expander |
f0d65dae | 3258 | -- is not active. |
4098232e | 3259 | |
18cb6d78 | 3260 | if not Expander_Active then |
ee6ba406 | 3261 | return; |
3262 | end if; | |
3263 | ||
3264 | R_Result := | |
3265 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3266 | ||
3267 | for J in 1 .. 2 loop | |
ee6ba406 | 3268 | R_Cno := R_Result (J); |
3269 | exit when No (R_Cno); | |
3270 | ||
3271 | -- A length check may mention an Itype which is attached to a | |
3272 | -- subsequent node. At the top level in a package this can cause | |
3273 | -- an order-of-elaboration problem, so we make sure that the itype | |
3274 | -- is referenced now. | |
3275 | ||
3276 | if Ekind (Current_Scope) = E_Package | |
3277 | and then Is_Compilation_Unit (Current_Scope) | |
3278 | then | |
3279 | Ensure_Defined (Target_Typ, Ck_Node); | |
3280 | ||
3281 | if Present (Source_Typ) then | |
3282 | Ensure_Defined (Source_Typ, Ck_Node); | |
3283 | ||
3284 | elsif Is_Itype (Etype (Ck_Node)) then | |
3285 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
3286 | end if; | |
3287 | end if; | |
3288 | ||
feff2f05 | 3289 | -- If the item is a conditional raise of constraint error, then have |
3290 | -- a look at what check is being performed and ??? | |
ee6ba406 | 3291 | |
3292 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3293 | and then Present (Condition (R_Cno)) | |
3294 | then | |
3295 | Cond := Condition (R_Cno); | |
3296 | ||
0577b0b1 | 3297 | -- Case where node does not now have a dynamic check |
ee6ba406 | 3298 | |
0577b0b1 | 3299 | if not Has_Dynamic_Length_Check (Ck_Node) then |
3300 | ||
3301 | -- If checks are on, just insert the check | |
3302 | ||
3303 | if Checks_On then | |
3304 | Insert_Action (Ck_Node, R_Cno); | |
3305 | ||
3306 | if not Do_Static then | |
3307 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
3308 | end if; | |
3309 | ||
3310 | -- If checks are off, then analyze the length check after | |
3311 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 3312 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 3313 | -- compile time warning in this case. |
3314 | ||
3315 | else | |
3316 | Set_Parent (R_Cno, Ck_Node); | |
3317 | Analyze (R_Cno); | |
ee6ba406 | 3318 | end if; |
ee6ba406 | 3319 | end if; |
3320 | ||
3321 | -- Output a warning if the condition is known to be True | |
3322 | ||
3323 | if Is_Entity_Name (Cond) | |
3324 | and then Entity (Cond) = Standard_True | |
3325 | then | |
3326 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3327 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 3328 | CE_Length_Check_Failed, |
ee6ba406 | 3329 | Ent => Target_Typ, |
3330 | Typ => Target_Typ); | |
3331 | ||
3332 | -- If we were only doing a static check, or if checks are not | |
3333 | -- on, then we want to delete the check, since it is not needed. | |
3334 | -- We do this by replacing the if statement by a null statement | |
3335 | ||
3336 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3337 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3338 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3339 | end if; | |
3340 | ||
3341 | else | |
3342 | Install_Static_Check (R_Cno, Loc); | |
3343 | end if; | |
ee6ba406 | 3344 | end loop; |
ee6ba406 | 3345 | end Apply_Selected_Length_Checks; |
3346 | ||
3347 | --------------------------------- | |
3348 | -- Apply_Selected_Range_Checks -- | |
3349 | --------------------------------- | |
3350 | ||
3351 | procedure Apply_Selected_Range_Checks | |
3352 | (Ck_Node : Node_Id; | |
3353 | Target_Typ : Entity_Id; | |
3354 | Source_Typ : Entity_Id; | |
3355 | Do_Static : Boolean) | |
3356 | is | |
ee6ba406 | 3357 | Checks_On : constant Boolean := |
f9bcba0d | 3358 | not Index_Checks_Suppressed (Target_Typ) |
5372d110 | 3359 | or else |
3360 | not Range_Checks_Suppressed (Target_Typ); | |
f9bcba0d | 3361 | |
2b4f2458 | 3362 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
3363 | ||
f9bcba0d | 3364 | Cond : Node_Id; |
3365 | R_Cno : Node_Id; | |
3366 | R_Result : Check_Result; | |
ee6ba406 | 3367 | |
3368 | begin | |
f0d65dae | 3369 | -- Only apply checks when generating code. In GNATprove mode, we do not |
3370 | -- apply the checks, but we still call Selected_Range_Checks to possibly | |
3371 | -- issue errors on SPARK code when a run-time error can be detected at | |
3372 | -- compile time. | |
3373 | ||
3374 | if not GNATprove_Mode then | |
3375 | if not Expander_Active or not Checks_On then | |
3376 | return; | |
3377 | end if; | |
ee6ba406 | 3378 | end if; |
3379 | ||
3380 | R_Result := | |
3381 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3382 | ||
f0d65dae | 3383 | if GNATprove_Mode then |
3384 | return; | |
3385 | end if; | |
3386 | ||
ee6ba406 | 3387 | for J in 1 .. 2 loop |
ee6ba406 | 3388 | R_Cno := R_Result (J); |
3389 | exit when No (R_Cno); | |
3390 | ||
f9bcba0d | 3391 | -- The range check requires runtime evaluation. Depending on what its |
3392 | -- triggering condition is, the check may be converted into a compile | |
3393 | -- time constraint check. | |
ee6ba406 | 3394 | |
3395 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3396 | and then Present (Condition (R_Cno)) | |
3397 | then | |
3398 | Cond := Condition (R_Cno); | |
3399 | ||
f9bcba0d | 3400 | -- Insert the range check before the related context. Note that |
3401 | -- this action analyses the triggering condition. | |
ee6ba406 | 3402 | |
f9bcba0d | 3403 | Insert_Action (Ck_Node, R_Cno); |
3404 | ||
3405 | -- This old code doesn't make sense, why is the context flagged as | |
3406 | -- requiring dynamic range checks now in the middle of generating | |
3407 | -- them ??? | |
3408 | ||
3409 | if not Do_Static then | |
3410 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
ee6ba406 | 3411 | end if; |
3412 | ||
f9bcba0d | 3413 | -- The triggering condition evaluates to True, the range check |
3414 | -- can be converted into a compile time constraint check. | |
ee6ba406 | 3415 | |
3416 | if Is_Entity_Name (Cond) | |
3417 | and then Entity (Cond) = Standard_True | |
3418 | then | |
feff2f05 | 3419 | -- Since an N_Range is technically not an expression, we have |
3420 | -- to set one of the bounds to C_E and then just flag the | |
3421 | -- N_Range. The warning message will point to the lower bound | |
3422 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3423 | |
3424 | if Nkind (Ck_Node) = N_Range then | |
3425 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3426 | (Low_Bound (Ck_Node), |
3427 | "static range out of bounds of}??", | |
f15731c4 | 3428 | CE_Range_Check_Failed, |
ee6ba406 | 3429 | Ent => Target_Typ, |
3430 | Typ => Target_Typ); | |
3431 | ||
3432 | Set_Raises_Constraint_Error (Ck_Node); | |
3433 | ||
3434 | else | |
3435 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3436 | (Ck_Node, |
1581f2d7 | 3437 | "static value out of range of}??", |
f15731c4 | 3438 | CE_Range_Check_Failed, |
ee6ba406 | 3439 | Ent => Target_Typ, |
3440 | Typ => Target_Typ); | |
3441 | end if; | |
3442 | ||
3443 | -- If we were only doing a static check, or if checks are not | |
3444 | -- on, then we want to delete the check, since it is not needed. | |
3445 | -- We do this by replacing the if statement by a null statement | |
3446 | ||
3fabf0ca | 3447 | elsif Do_Static then |
00c403ee | 3448 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3449 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3450 | end if; | |
3451 | ||
4ee78e36 | 3452 | -- The range check raises Constraint_Error explicitly |
f9bcba0d | 3453 | |
ee6ba406 | 3454 | else |
3455 | Install_Static_Check (R_Cno, Loc); | |
3456 | end if; | |
ee6ba406 | 3457 | end loop; |
ee6ba406 | 3458 | end Apply_Selected_Range_Checks; |
3459 | ||
3460 | ------------------------------- | |
3461 | -- Apply_Static_Length_Check -- | |
3462 | ------------------------------- | |
3463 | ||
3464 | procedure Apply_Static_Length_Check | |
3465 | (Expr : Node_Id; | |
3466 | Target_Typ : Entity_Id; | |
3467 | Source_Typ : Entity_Id := Empty) | |
3468 | is | |
3469 | begin | |
3470 | Apply_Selected_Length_Checks | |
3471 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3472 | end Apply_Static_Length_Check; | |
3473 | ||
3474 | ------------------------------------- | |
3475 | -- Apply_Subscript_Validity_Checks -- | |
3476 | ------------------------------------- | |
3477 | ||
3478 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3479 | Sub : Node_Id; | |
3480 | ||
3481 | begin | |
3482 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3483 | ||
3484 | -- Loop through subscripts | |
3485 | ||
3486 | Sub := First (Expressions (Expr)); | |
3487 | while Present (Sub) loop | |
3488 | ||
feff2f05 | 3489 | -- Check one subscript. Note that we do not worry about enumeration |
3490 | -- type with holes, since we will convert the value to a Pos value | |
3491 | -- for the subscript, and that convert will do the necessary validity | |
3492 | -- check. | |
ee6ba406 | 3493 | |
3494 | Ensure_Valid (Sub, Holes_OK => True); | |
3495 | ||
3496 | -- Move to next subscript | |
3497 | ||
3498 | Sub := Next (Sub); | |
3499 | end loop; | |
3500 | end Apply_Subscript_Validity_Checks; | |
3501 | ||
3502 | ---------------------------------- | |
3503 | -- Apply_Type_Conversion_Checks -- | |
3504 | ---------------------------------- | |
3505 | ||
3506 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3507 | Target_Type : constant Entity_Id := Etype (N); | |
3508 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3509 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3510 | |
3511 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3512 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3513 | -- full view might have discriminants with defaults, so we need the | |
3514 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3515 | |
3516 | begin | |
3517 | if Inside_A_Generic then | |
3518 | return; | |
3519 | ||
f15731c4 | 3520 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3521 | -- situations of incomplete trees that blow things up. |
3522 | ||
f15731c4 | 3523 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3524 | return; |
3525 | ||
ea822fd4 | 3526 | -- Never generate discriminant checks for Unchecked_Union types |
3527 | ||
3528 | elsif Present (Expr_Type) | |
3529 | and then Is_Unchecked_Union (Expr_Type) | |
3530 | then | |
3531 | return; | |
3532 | ||
feff2f05 | 3533 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3534 | -- range check if we cannot be sure that Expr is in the base type of | |
3535 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3536 | -- are not quite the same condition from an implementation point of | |
3537 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3538 | |
3539 | elsif Is_Scalar_Type (Target_Type) then | |
3540 | declare | |
3541 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3542 | -- If the Conversion_OK flag on the type conversion is set and no |
ea822fd4 | 3543 | -- floating-point type is involved in the type conversion then |
3544 | -- fixed-point values must be read as integral values. | |
ee6ba406 | 3545 | |
5329ca64 | 3546 | Float_To_Int : constant Boolean := |
b6341c67 | 3547 | Is_Floating_Point_Type (Expr_Type) |
3548 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3549 | |
ee6ba406 | 3550 | begin |
ee6ba406 | 3551 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3552 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3553 | and then not |
7a1dabb3 | 3554 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3555 | and then not Float_To_Int |
ee6ba406 | 3556 | then |
4642b679 | 3557 | -- A small optimization: the attribute 'Pos applied to an |
c59f2b2d | 3558 | -- enumeration type has a known range, even though its type is |
3559 | -- Universal_Integer. So in numeric conversions it is usually | |
3560 | -- within range of the target integer type. Use the static | |
3561 | -- bounds of the base types to check. Disable this optimization | |
3562 | -- in case of a generic formal discrete type, because we don't | |
3563 | -- necessarily know the upper bound yet. | |
259716a0 | 3564 | |
3565 | if Nkind (Expr) = N_Attribute_Reference | |
3566 | and then Attribute_Name (Expr) = Name_Pos | |
3567 | and then Is_Enumeration_Type (Etype (Prefix (Expr))) | |
c59f2b2d | 3568 | and then not Is_Generic_Type (Etype (Prefix (Expr))) |
259716a0 | 3569 | and then Is_Integer_Type (Target_Type) |
3570 | then | |
3571 | declare | |
4642b679 | 3572 | Enum_T : constant Entity_Id := |
3573 | Root_Type (Etype (Prefix (Expr))); | |
3574 | Int_T : constant Entity_Id := Base_Type (Target_Type); | |
3575 | Last_I : constant Uint := | |
3576 | Intval (High_Bound (Scalar_Range (Int_T))); | |
3577 | Last_E : Uint; | |
259716a0 | 3578 | |
3579 | begin | |
4642b679 | 3580 | -- Character types have no explicit literals, so we use |
259716a0 | 3581 | -- the known number of characters in the type. |
3582 | ||
3583 | if Root_Type (Enum_T) = Standard_Character then | |
3584 | Last_E := UI_From_Int (255); | |
3585 | ||
3586 | elsif Enum_T = Standard_Wide_Character | |
3587 | or else Enum_T = Standard_Wide_Wide_Character | |
3588 | then | |
3589 | Last_E := UI_From_Int (65535); | |
3590 | ||
3591 | else | |
4642b679 | 3592 | Last_E := |
3593 | Enumeration_Pos | |
259716a0 | 3594 | (Entity (High_Bound (Scalar_Range (Enum_T)))); |
3595 | end if; | |
3596 | ||
3597 | if Last_E <= Last_I then | |
3598 | null; | |
3599 | ||
3600 | else | |
3601 | Activate_Overflow_Check (N); | |
3602 | end if; | |
3603 | end; | |
3604 | ||
3605 | else | |
3606 | Activate_Overflow_Check (N); | |
3607 | end if; | |
ee6ba406 | 3608 | end if; |
3609 | ||
3610 | if not Range_Checks_Suppressed (Target_Type) | |
3611 | and then not Range_Checks_Suppressed (Expr_Type) | |
3612 | then | |
54022749 | 3613 | if Float_To_Int |
3614 | and then not GNATprove_Mode | |
3615 | then | |
5329ca64 | 3616 | Apply_Float_Conversion_Check (Expr, Target_Type); |
f81a201b | 3617 | |
5329ca64 | 3618 | else |
7d97dbc9 | 3619 | -- Conversions involving fixed-point types are expanded |
3620 | -- separately, and do not need a Range_Check flag, except | |
c6056dd1 | 3621 | -- in GNATprove_Mode, where the explicit constraint check |
3622 | -- will not be generated. | |
7d97dbc9 | 3623 | |
95c16286 | 3624 | if GNATprove_Mode |
3625 | or else not Is_Fixed_Point_Type (Expr_Type) | |
7d97dbc9 | 3626 | then |
3627 | Apply_Scalar_Range_Check | |
3628 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
3629 | ||
3630 | else | |
3631 | Set_Do_Range_Check (Expression (N), False); | |
3632 | end if; | |
798afddc | 3633 | |
3634 | -- If the target type has predicates, we need to indicate | |
ea822fd4 | 3635 | -- the need for a check, even if Determine_Range finds that |
3636 | -- the value is within bounds. This may be the case e.g for | |
3637 | -- a division with a constant denominator. | |
798afddc | 3638 | |
3639 | if Has_Predicates (Target_Type) then | |
3640 | Enable_Range_Check (Expr); | |
3641 | end if; | |
5329ca64 | 3642 | end if; |
ee6ba406 | 3643 | end if; |
3644 | end; | |
3645 | ||
3646 | elsif Comes_From_Source (N) | |
f40f9731 | 3647 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3648 | and then Is_Record_Type (Target_Type) |
3649 | and then Is_Derived_Type (Target_Type) | |
3650 | and then not Is_Tagged_Type (Target_Type) | |
3651 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3652 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3653 | then |
141d591a | 3654 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3655 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3656 | -- constraint, to verify that the expression of the parent type |
ea822fd4 | 3657 | -- satisfies the constraints imposed by the (unconstrained) derived |
3658 | -- type. This applies to value conversions, not to view conversions | |
3659 | -- of tagged types. | |
ee6ba406 | 3660 | |
3661 | declare | |
9dfe12ae | 3662 | Loc : constant Source_Ptr := Sloc (N); |
3663 | Cond : Node_Id; | |
3664 | Constraint : Elmt_Id; | |
3665 | Discr_Value : Node_Id; | |
3666 | Discr : Entity_Id; | |
3667 | ||
3668 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3669 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3670 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3671 | |
3672 | begin | |
9dfe12ae | 3673 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3674 | while Present (Constraint) loop |
3675 | Discr_Value := Node (Constraint); | |
3676 | ||
3677 | if Is_Entity_Name (Discr_Value) | |
3678 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3679 | then | |
3680 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3681 | ||
3682 | if Present (Discr) | |
3683 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3684 | then | |
3685 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3686 | -- Value of original discriminant in expression. If the |
3687 | -- new discriminant has been used to constrain more than | |
3688 | -- one of the stored discriminants, this will provide the | |
3689 | -- required consistency check. | |
ee6ba406 | 3690 | |
55868293 | 3691 | Append_Elmt |
3692 | (Make_Selected_Component (Loc, | |
3693 | Prefix => | |
9dfe12ae | 3694 | Duplicate_Subexpr_No_Checks |
3695 | (Expr, Name_Req => True), | |
ee6ba406 | 3696 | Selector_Name => |
3697 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3698 | New_Constraints); |
ee6ba406 | 3699 | |
3700 | else | |
3701 | -- Discriminant of more remote ancestor ??? | |
3702 | ||
3703 | return; | |
3704 | end if; | |
3705 | ||
feff2f05 | 3706 | -- Derived type definition has an explicit value for this |
3707 | -- stored discriminant. | |
ee6ba406 | 3708 | |
3709 | else | |
3710 | Append_Elmt | |
9dfe12ae | 3711 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3712 | New_Constraints); | |
ee6ba406 | 3713 | end if; |
3714 | ||
3715 | Next_Elmt (Constraint); | |
3716 | end loop; | |
3717 | ||
3718 | -- Use the unconstrained expression type to retrieve the | |
3719 | -- discriminants of the parent, and apply momentarily the | |
3720 | -- discriminant constraint synthesized above. | |
3721 | ||
3722 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3723 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3724 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3725 | ||
3726 | Insert_Action (N, | |
f15731c4 | 3727 | Make_Raise_Constraint_Error (Loc, |
3728 | Condition => Cond, | |
3729 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3730 | end; |
3731 | ||
175a6969 | 3732 | -- For arrays, checks are set now, but conversions are applied during |
3733 | -- expansion, to take into accounts changes of representation. The | |
3734 | -- checks become range checks on the base type or length checks on the | |
3735 | -- subtype, depending on whether the target type is unconstrained or | |
8e802312 | 3736 | -- constrained. Note that the range check is put on the expression of a |
3737 | -- type conversion, while the length check is put on the type conversion | |
3738 | -- itself. | |
175a6969 | 3739 | |
3740 | elsif Is_Array_Type (Target_Type) then | |
3741 | if Is_Constrained (Target_Type) then | |
3742 | Set_Do_Length_Check (N); | |
3743 | else | |
3744 | Set_Do_Range_Check (Expr); | |
3745 | end if; | |
ee6ba406 | 3746 | end if; |
ee6ba406 | 3747 | end Apply_Type_Conversion_Checks; |
3748 | ||
3749 | ---------------------------------------------- | |
3750 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3751 | ---------------------------------------------- | |
3752 | ||
3753 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3754 | Loc : constant Source_Ptr := Sloc (N); | |
3755 | Typ : constant Entity_Id := Etype (N); | |
3756 | ||
3757 | begin | |
3758 | if Inside_A_Generic then | |
3759 | return; | |
3760 | ||
3761 | -- Nothing to do if checks are suppressed | |
3762 | ||
3763 | elsif Range_Checks_Suppressed (Typ) | |
3764 | and then Overflow_Checks_Suppressed (Typ) | |
3765 | then | |
3766 | return; | |
3767 | ||
3768 | -- Nothing to do if the attribute does not come from source. The | |
3769 | -- internal attributes we generate of this type do not need checks, | |
3770 | -- and furthermore the attempt to check them causes some circular | |
3771 | -- elaboration orders when dealing with packed types. | |
3772 | ||
3773 | elsif not Comes_From_Source (N) then | |
3774 | return; | |
3775 | ||
9dfe12ae | 3776 | -- If the prefix is a selected component that depends on a discriminant |
3777 | -- the check may improperly expose a discriminant instead of using | |
3778 | -- the bounds of the object itself. Set the type of the attribute to | |
3779 | -- the base type of the context, so that a check will be imposed when | |
3780 | -- needed (e.g. if the node appears as an index). | |
3781 | ||
3782 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3783 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3784 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3785 | then | |
3786 | Set_Etype (N, Base_Type (Typ)); | |
3787 | ||
feff2f05 | 3788 | -- Otherwise, replace the attribute node with a type conversion node |
3789 | -- whose expression is the attribute, retyped to universal integer, and | |
3790 | -- whose subtype mark is the target type. The call to analyze this | |
3791 | -- conversion will set range and overflow checks as required for proper | |
3792 | -- detection of an out of range value. | |
ee6ba406 | 3793 | |
3794 | else | |
3795 | Set_Etype (N, Universal_Integer); | |
3796 | Set_Analyzed (N, True); | |
3797 | ||
3798 | Rewrite (N, | |
3799 | Make_Type_Conversion (Loc, | |
3800 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3801 | Expression => Relocate_Node (N))); | |
3802 | ||
3803 | Analyze_And_Resolve (N, Typ); | |
3804 | return; | |
3805 | end if; | |
ee6ba406 | 3806 | end Apply_Universal_Integer_Attribute_Checks; |
3807 | ||
07c191b0 | 3808 | ------------------------------------- |
3809 | -- Atomic_Synchronization_Disabled -- | |
3810 | ------------------------------------- | |
3811 | ||
3812 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3813 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3814 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3815 | ||
3816 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3817 | begin | |
b444f81d | 3818 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3819 | -- looks enabled, since it is never disabled. | |
3820 | ||
3821 | if Debug_Flag_Dot_E then | |
3822 | return False; | |
3823 | ||
3824 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3825 | -- sync looks disabled, since it always tests True. | |
3826 | ||
3827 | elsif Debug_Flag_Dot_D then | |
3828 | return True; | |
3829 | ||
3830 | -- If entity present, then check result for that entity | |
3831 | ||
3832 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3833 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3834 | |
3835 | -- Otherwise result depends on current scope setting | |
3836 | ||
07c191b0 | 3837 | else |
fafc6b97 | 3838 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3839 | end if; |
3840 | end Atomic_Synchronization_Disabled; | |
3841 | ||
ee6ba406 | 3842 | ------------------------------- |
3843 | -- Build_Discriminant_Checks -- | |
3844 | ------------------------------- | |
3845 | ||
3846 | function Build_Discriminant_Checks | |
3847 | (N : Node_Id; | |
314a23b6 | 3848 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3849 | is |
3850 | Loc : constant Source_Ptr := Sloc (N); | |
3851 | Cond : Node_Id; | |
3852 | Disc : Elmt_Id; | |
3853 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3854 | Dref : Node_Id; |
ee6ba406 | 3855 | Dval : Node_Id; |
3856 | ||
84d0d4a5 | 3857 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3858 | ||
bacd5059 | 3859 | -------------------------------- |
3860 | -- Aggregate_Discriminant_Val -- | |
3861 | -------------------------------- | |
84d0d4a5 | 3862 | |
3863 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3864 | Assoc : Node_Id; | |
3865 | ||
3866 | begin | |
feff2f05 | 3867 | -- The aggregate has been normalized with named associations. We use |
3868 | -- the Chars field to locate the discriminant to take into account | |
3869 | -- discriminants in derived types, which carry the same name as those | |
3870 | -- in the parent. | |
84d0d4a5 | 3871 | |
3872 | Assoc := First (Component_Associations (N)); | |
3873 | while Present (Assoc) loop | |
3874 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3875 | return Expression (Assoc); | |
3876 | else | |
3877 | Next (Assoc); | |
3878 | end if; | |
3879 | end loop; | |
3880 | ||
3881 | -- Discriminant must have been found in the loop above | |
3882 | ||
3883 | raise Program_Error; | |
3884 | end Aggregate_Discriminant_Val; | |
3885 | ||
3886 | -- Start of processing for Build_Discriminant_Checks | |
3887 | ||
ee6ba406 | 3888 | begin |
84d0d4a5 | 3889 | -- Loop through discriminants evolving the condition |
3890 | ||
ee6ba406 | 3891 | Cond := Empty; |
3892 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3893 | ||
9dfe12ae | 3894 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3895 | |
3896 | if Is_Private_Type (T_Typ) | |
3897 | and then No (Full_View (T_Typ)) | |
3898 | then | |
3899 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3900 | else | |
3901 | Disc_Ent := First_Discriminant (T_Typ); | |
3902 | end if; | |
3903 | ||
3904 | while Present (Disc) loop | |
ee6ba406 | 3905 | Dval := Node (Disc); |
3906 | ||
3907 | if Nkind (Dval) = N_Identifier | |
3908 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3909 | then | |
3910 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3911 | else | |
9dfe12ae | 3912 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3913 | end if; |
3914 | ||
00f91aef | 3915 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3916 | -- of the node. | |
9dfe12ae | 3917 | |
00f91aef | 3918 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3919 | Dref := New_Copy ( | |
3920 | Get_Discriminant_Value ( | |
3921 | First_Discriminant (T_Typ), | |
3922 | T_Typ, | |
3923 | Stored_Constraint (T_Typ))); | |
3924 | ||
84d0d4a5 | 3925 | elsif Nkind (N) = N_Aggregate then |
3926 | Dref := | |
3927 | Duplicate_Subexpr_No_Checks | |
3928 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3929 | ||
00f91aef | 3930 | else |
3931 | Dref := | |
3932 | Make_Selected_Component (Loc, | |
20cf157b | 3933 | Prefix => |
00f91aef | 3934 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
20cf157b | 3935 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
00f91aef | 3936 | |
3937 | Set_Is_In_Discriminant_Check (Dref); | |
3938 | end if; | |
9dfe12ae | 3939 | |
ee6ba406 | 3940 | Evolve_Or_Else (Cond, |
3941 | Make_Op_Ne (Loc, | |
20cf157b | 3942 | Left_Opnd => Dref, |
ee6ba406 | 3943 | Right_Opnd => Dval)); |
3944 | ||
3945 | Next_Elmt (Disc); | |
3946 | Next_Discriminant (Disc_Ent); | |
3947 | end loop; | |
3948 | ||
3949 | return Cond; | |
3950 | end Build_Discriminant_Checks; | |
3951 | ||
13dbf220 | 3952 | ------------------ |
3953 | -- Check_Needed -- | |
3954 | ------------------ | |
3955 | ||
3956 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3957 | N : Node_Id; | |
3958 | P : Node_Id; | |
3959 | K : Node_Kind; | |
3960 | L : Node_Id; | |
3961 | R : Node_Id; | |
3962 | ||
9b2068d4 | 3963 | function Left_Expression (Op : Node_Id) return Node_Id; |
3964 | -- Return the relevant expression from the left operand of the given | |
3965 | -- short circuit form: this is LO itself, except if LO is a qualified | |
3966 | -- expression, a type conversion, or an expression with actions, in | |
3967 | -- which case this is Left_Expression (Expression (LO)). | |
3968 | ||
3969 | --------------------- | |
3970 | -- Left_Expression -- | |
3971 | --------------------- | |
3972 | ||
3973 | function Left_Expression (Op : Node_Id) return Node_Id is | |
3974 | LE : Node_Id := Left_Opnd (Op); | |
3975 | begin | |
20cf157b | 3976 | while Nkind_In (LE, N_Qualified_Expression, |
3977 | N_Type_Conversion, | |
3978 | N_Expression_With_Actions) | |
9b2068d4 | 3979 | loop |
3980 | LE := Expression (LE); | |
3981 | end loop; | |
3982 | ||
3983 | return LE; | |
3984 | end Left_Expression; | |
3985 | ||
3986 | -- Start of processing for Check_Needed | |
3987 | ||
13dbf220 | 3988 | begin |
3989 | -- Always check if not simple entity | |
3990 | ||
3991 | if Nkind (Nod) not in N_Has_Entity | |
3992 | or else not Comes_From_Source (Nod) | |
3993 | then | |
3994 | return True; | |
3995 | end if; | |
3996 | ||
3997 | -- Look up tree for short circuit | |
3998 | ||
3999 | N := Nod; | |
4000 | loop | |
4001 | P := Parent (N); | |
4002 | K := Nkind (P); | |
4003 | ||
7b17e51b | 4004 | -- Done if out of subexpression (note that we allow generated stuff |
4005 | -- such as itype declarations in this context, to keep the loop going | |
4006 | -- since we may well have generated such stuff in complex situations. | |
4007 | -- Also done if no parent (probably an error condition, but no point | |
39a0c1d3 | 4008 | -- in behaving nasty if we find it). |
7b17e51b | 4009 | |
4010 | if No (P) | |
4011 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
4012 | then | |
13dbf220 | 4013 | return True; |
4014 | ||
7b17e51b | 4015 | -- Or/Or Else case, where test is part of the right operand, or is |
4016 | -- part of one of the actions associated with the right operand, and | |
4017 | -- the left operand is an equality test. | |
13dbf220 | 4018 | |
7b17e51b | 4019 | elsif K = N_Op_Or then |
13dbf220 | 4020 | exit when N = Right_Opnd (P) |
9b2068d4 | 4021 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 4022 | |
7b17e51b | 4023 | elsif K = N_Or_Else then |
4024 | exit when (N = Right_Opnd (P) | |
4025 | or else | |
4026 | (Is_List_Member (N) | |
4027 | and then List_Containing (N) = Actions (P))) | |
9b2068d4 | 4028 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 4029 | |
7b17e51b | 4030 | -- Similar test for the And/And then case, where the left operand |
4031 | -- is an inequality test. | |
4032 | ||
4033 | elsif K = N_Op_And then | |
13dbf220 | 4034 | exit when N = Right_Opnd (P) |
9b2068d4 | 4035 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
7b17e51b | 4036 | |
4037 | elsif K = N_And_Then then | |
4038 | exit when (N = Right_Opnd (P) | |
4039 | or else | |
4040 | (Is_List_Member (N) | |
20cf157b | 4041 | and then List_Containing (N) = Actions (P))) |
9b2068d4 | 4042 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
13dbf220 | 4043 | end if; |
4044 | ||
4045 | N := P; | |
4046 | end loop; | |
4047 | ||
4048 | -- If we fall through the loop, then we have a conditional with an | |
9b2068d4 | 4049 | -- appropriate test as its left operand, so look further. |
4050 | ||
4051 | L := Left_Expression (P); | |
4052 | ||
4053 | -- L is an "=" or "/=" operator: extract its operands | |
13dbf220 | 4054 | |
13dbf220 | 4055 | R := Right_Opnd (L); |
4056 | L := Left_Opnd (L); | |
4057 | ||
4058 | -- Left operand of test must match original variable | |
4059 | ||
20cf157b | 4060 | if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
13dbf220 | 4061 | return True; |
4062 | end if; | |
4063 | ||
2af58f67 | 4064 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 4065 | |
4066 | case Check is | |
4067 | when Access_Check => | |
2af58f67 | 4068 | if not Known_Null (R) then |
13dbf220 | 4069 | return True; |
4070 | end if; | |
4071 | ||
4072 | when Division_Check => | |
4073 | if not Compile_Time_Known_Value (R) | |
4074 | or else Expr_Value (R) /= Uint_0 | |
4075 | then | |
4076 | return True; | |
4077 | end if; | |
2af58f67 | 4078 | |
4079 | when others => | |
4080 | raise Program_Error; | |
13dbf220 | 4081 | end case; |
4082 | ||
4083 | -- Here we have the optimizable case, warn if not short-circuited | |
4084 | ||
4085 | if K = N_Op_And or else K = N_Op_Or then | |
c4968aa2 | 4086 | Error_Msg_Warn := SPARK_Mode /= On; |
4098232e | 4087 | |
13dbf220 | 4088 | case Check is |
4089 | when Access_Check => | |
4098232e | 4090 | if GNATprove_Mode then |
4091 | Error_Msg_N | |
4092 | ("Constraint_Error might have been raised (access check)", | |
4093 | Parent (Nod)); | |
4094 | else | |
4095 | Error_Msg_N | |
4096 | ("Constraint_Error may be raised (access check)??", | |
4097 | Parent (Nod)); | |
4098 | end if; | |
4099 | ||
13dbf220 | 4100 | when Division_Check => |
4098232e | 4101 | if GNATprove_Mode then |
4102 | Error_Msg_N | |
4103 | ("Constraint_Error might have been raised (zero divide)", | |
4104 | Parent (Nod)); | |
4105 | else | |
4106 | Error_Msg_N | |
4107 | ("Constraint_Error may be raised (zero divide)??", | |
4108 | Parent (Nod)); | |
4109 | end if; | |
2af58f67 | 4110 | |
4111 | when others => | |
4112 | raise Program_Error; | |
13dbf220 | 4113 | end case; |
4114 | ||
4115 | if K = N_Op_And then | |
e977c0cf | 4116 | Error_Msg_N -- CODEFIX |
cb97ae5c | 4117 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 4118 | else |
e977c0cf | 4119 | Error_Msg_N -- CODEFIX |
cb97ae5c | 4120 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 4121 | end if; |
4122 | ||
6fb3c314 | 4123 | -- If not short-circuited, we need the check |
13dbf220 | 4124 | |
4125 | return True; | |
4126 | ||
4127 | -- If short-circuited, we can omit the check | |
4128 | ||
4129 | else | |
4130 | return False; | |
4131 | end if; | |
4132 | end Check_Needed; | |
4133 | ||
ee6ba406 | 4134 | ----------------------------------- |
4135 | -- Check_Valid_Lvalue_Subscripts -- | |
4136 | ----------------------------------- | |
4137 | ||
4138 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
4139 | begin | |
4140 | -- Skip this if range checks are suppressed | |
4141 | ||
4142 | if Range_Checks_Suppressed (Etype (Expr)) then | |
4143 | return; | |
4144 | ||
feff2f05 | 4145 | -- Only do this check for expressions that come from source. We assume |
4146 | -- that expander generated assignments explicitly include any necessary | |
4147 | -- checks. Note that this is not just an optimization, it avoids | |
39a0c1d3 | 4148 | -- infinite recursions. |
ee6ba406 | 4149 | |
4150 | elsif not Comes_From_Source (Expr) then | |
4151 | return; | |
4152 | ||
4153 | -- For a selected component, check the prefix | |
4154 | ||
4155 | elsif Nkind (Expr) = N_Selected_Component then | |
4156 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4157 | return; | |
4158 | ||
4159 | -- Case of indexed component | |
4160 | ||
4161 | elsif Nkind (Expr) = N_Indexed_Component then | |
4162 | Apply_Subscript_Validity_Checks (Expr); | |
4163 | ||
feff2f05 | 4164 | -- Prefix may itself be or contain an indexed component, and these |
4165 | -- subscripts need checking as well. | |
ee6ba406 | 4166 | |
4167 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4168 | end if; | |
4169 | end Check_Valid_Lvalue_Subscripts; | |
4170 | ||
fa7497e8 | 4171 | ---------------------------------- |
4172 | -- Null_Exclusion_Static_Checks -- | |
4173 | ---------------------------------- | |
4174 | ||
e9998840 | 4175 | procedure Null_Exclusion_Static_Checks |
69733a9a | 4176 | (N : Node_Id; |
4177 | Comp : Node_Id := Empty; | |
4178 | Array_Comp : Boolean := False) | |
e9998840 | 4179 | is |
b1e656fb | 4180 | Has_Null : constant Boolean := Has_Null_Exclusion (N); |
4181 | Kind : constant Node_Kind := Nkind (N); | |
4182 | Error_Nod : Node_Id; | |
4183 | Expr : Node_Id; | |
4184 | Typ : Entity_Id; | |
fa7497e8 | 4185 | |
13dbf220 | 4186 | begin |
0577b0b1 | 4187 | pragma Assert |
b1e656fb | 4188 | (Nkind_In (Kind, N_Component_Declaration, |
4189 | N_Discriminant_Specification, | |
4190 | N_Function_Specification, | |
4191 | N_Object_Declaration, | |
4192 | N_Parameter_Specification)); | |
0577b0b1 | 4193 | |
b1e656fb | 4194 | if Kind = N_Function_Specification then |
0577b0b1 | 4195 | Typ := Etype (Defining_Entity (N)); |
4196 | else | |
4197 | Typ := Etype (Defining_Identifier (N)); | |
4198 | end if; | |
fa7497e8 | 4199 | |
b1e656fb | 4200 | case Kind is |
13dbf220 | 4201 | when N_Component_Declaration => |
4202 | if Present (Access_Definition (Component_Definition (N))) then | |
b1e656fb | 4203 | Error_Nod := Component_Definition (N); |
13dbf220 | 4204 | else |
b1e656fb | 4205 | Error_Nod := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 4206 | end if; |
5329ca64 | 4207 | |
0577b0b1 | 4208 | when N_Discriminant_Specification => |
b1e656fb | 4209 | Error_Nod := Discriminant_Type (N); |
0577b0b1 | 4210 | |
4211 | when N_Function_Specification => | |
b1e656fb | 4212 | Error_Nod := Result_Definition (N); |
0577b0b1 | 4213 | |
4214 | when N_Object_Declaration => | |
b1e656fb | 4215 | Error_Nod := Object_Definition (N); |
0577b0b1 | 4216 | |
4217 | when N_Parameter_Specification => | |
b1e656fb | 4218 | Error_Nod := Parameter_Type (N); |
0577b0b1 | 4219 | |
13dbf220 | 4220 | when others => |
4221 | raise Program_Error; | |
4222 | end case; | |
5329ca64 | 4223 | |
0577b0b1 | 4224 | if Has_Null then |
5329ca64 | 4225 | |
0577b0b1 | 4226 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
4227 | -- applied to an access [sub]type. | |
5329ca64 | 4228 | |
0577b0b1 | 4229 | if not Is_Access_Type (Typ) then |
503f7fd3 | 4230 | Error_Msg_N |
b1e656fb | 4231 | ("`NOT NULL` allowed only for an access type", Error_Nod); |
5329ca64 | 4232 | |
feff2f05 | 4233 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 4234 | -- be applied to a [sub]type that does not exclude null already. |
4235 | ||
b1e656fb | 4236 | elsif Can_Never_Be_Null (Typ) and then Comes_From_Source (Typ) then |
503f7fd3 | 4237 | Error_Msg_NE |
00c403ee | 4238 | ("`NOT NULL` not allowed (& already excludes null)", |
b1e656fb | 4239 | Error_Nod, Typ); |
0577b0b1 | 4240 | end if; |
13dbf220 | 4241 | end if; |
5329ca64 | 4242 | |
cc60bd16 | 4243 | -- Check that null-excluding objects are always initialized, except for |
4244 | -- deferred constants, for which the expression will appear in the full | |
4245 | -- declaration. | |
13dbf220 | 4246 | |
b1e656fb | 4247 | if Kind = N_Object_Declaration |
84d0d4a5 | 4248 | and then No (Expression (N)) |
cc60bd16 | 4249 | and then not Constant_Present (N) |
feff2f05 | 4250 | and then not No_Initialization (N) |
13dbf220 | 4251 | then |
e9998840 | 4252 | if Present (Comp) then |
4253 | ||
b1ff36e7 | 4254 | -- Specialize the warning message to indicate that we are dealing |
e9998840 | 4255 | -- with an uninitialized composite object that has a defaulted |
4256 | -- null-excluding component. | |
4257 | ||
4258 | Error_Msg_Name_1 := Chars (Defining_Identifier (Comp)); | |
4259 | Error_Msg_Name_2 := Chars (Defining_Identifier (N)); | |
4260 | ||
69733a9a | 4261 | Discard_Node |
4262 | (Compile_Time_Constraint_Error | |
4263 | (N => N, | |
4264 | Msg => | |
4265 | "(Ada 2005) null-excluding component % of object % must " | |
4266 | & "be initialized??", | |
4267 | Ent => Defining_Identifier (Comp))); | |
4268 | ||
4269 | -- This is a case of an array with null-excluding components, so | |
4270 | -- indicate that in the warning. | |
4271 | ||
4272 | elsif Array_Comp then | |
4273 | Discard_Node | |
4274 | (Compile_Time_Constraint_Error | |
4275 | (N => N, | |
4276 | Msg => | |
4277 | "(Ada 2005) null-excluding array components must " | |
4278 | & "be initialized??", | |
4279 | Ent => Defining_Identifier (N))); | |
4280 | ||
4281 | -- Normal case of object of a null-excluding access type | |
b1ff36e7 | 4282 | |
e9998840 | 4283 | else |
69733a9a | 4284 | -- Add an expression that assigns null. This node is needed by |
4285 | -- Apply_Compile_Time_Constraint_Error, which will replace this | |
4286 | -- with a Constraint_Error node. | |
4287 | ||
4288 | Set_Expression (N, Make_Null (Sloc (N))); | |
4289 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
4290 | ||
e9998840 | 4291 | Apply_Compile_Time_Constraint_Error |
4292 | (N => Expression (N), | |
4293 | Msg => | |
4294 | "(Ada 2005) null-excluding objects must be initialized??", | |
4295 | Reason => CE_Null_Not_Allowed); | |
4296 | end if; | |
13dbf220 | 4297 | end if; |
5329ca64 | 4298 | |
cc60bd16 | 4299 | -- Check that a null-excluding component, formal or object is not being |
4300 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 4301 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 4302 | |
b1e656fb | 4303 | if Kind /= N_Function_Specification then |
0577b0b1 | 4304 | Expr := Expression (N); |
5329ca64 | 4305 | |
2af58f67 | 4306 | if Present (Expr) and then Known_Null (Expr) then |
b1e656fb | 4307 | case Kind is |
99378362 | 4308 | when N_Component_Declaration |
4309 | | N_Discriminant_Specification | |
4310 | => | |
7189d17f | 4311 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4312 | (N => Expr, |
99378362 | 4313 | Msg => |
4314 | "(Ada 2005) null not allowed in null-excluding " | |
4315 | & "components??", | |
0577b0b1 | 4316 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 4317 | |
0577b0b1 | 4318 | when N_Object_Declaration => |
7189d17f | 4319 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4320 | (N => Expr, |
99378362 | 4321 | Msg => |
4322 | "(Ada 2005) null not allowed in null-excluding " | |
4323 | & "objects??", | |
0577b0b1 | 4324 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 4325 | |
0577b0b1 | 4326 | when N_Parameter_Specification => |
7189d17f | 4327 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 4328 | (N => Expr, |
99378362 | 4329 | Msg => |
4330 | "(Ada 2005) null not allowed in null-excluding " | |
4331 | & "formals??", | |
0577b0b1 | 4332 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 4333 | |
4334 | when others => | |
4335 | null; | |
5329ca64 | 4336 | end case; |
4337 | end if; | |
0577b0b1 | 4338 | end if; |
fa7497e8 | 4339 | end Null_Exclusion_Static_Checks; |
4340 | ||
9dfe12ae | 4341 | ---------------------------------- |
4342 | -- Conditional_Statements_Begin -- | |
4343 | ---------------------------------- | |
4344 | ||
4345 | procedure Conditional_Statements_Begin is | |
4346 | begin | |
4347 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
4348 | ||
feff2f05 | 4349 | -- If stack overflows, kill all checks, that way we know to simply reset |
4350 | -- the number of saved checks to zero on return. This should never occur | |
4351 | -- in practice. | |
9dfe12ae | 4352 | |
4353 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4354 | Kill_All_Checks; | |
4355 | ||
feff2f05 | 4356 | -- In the normal case, we just make a new stack entry saving the current |
4357 | -- number of saved checks for a later restore. | |
9dfe12ae | 4358 | |
4359 | else | |
4360 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
4361 | ||
4362 | if Debug_Flag_CC then | |
4363 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
4364 | Num_Saved_Checks); | |
4365 | end if; | |
4366 | end if; | |
4367 | end Conditional_Statements_Begin; | |
4368 | ||
4369 | -------------------------------- | |
4370 | -- Conditional_Statements_End -- | |
4371 | -------------------------------- | |
4372 | ||
4373 | procedure Conditional_Statements_End is | |
4374 | begin | |
4375 | pragma Assert (Saved_Checks_TOS > 0); | |
4376 | ||
feff2f05 | 4377 | -- If the saved checks stack overflowed, then we killed all checks, so |
4378 | -- setting the number of saved checks back to zero is correct. This | |
4379 | -- should never occur in practice. | |
9dfe12ae | 4380 | |
4381 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4382 | Num_Saved_Checks := 0; | |
4383 | ||
feff2f05 | 4384 | -- In the normal case, restore the number of saved checks from the top |
4385 | -- stack entry. | |
9dfe12ae | 4386 | |
4387 | else | |
4388 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
20cf157b | 4389 | |
9dfe12ae | 4390 | if Debug_Flag_CC then |
4391 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
4392 | Num_Saved_Checks); | |
4393 | end if; | |
4394 | end if; | |
4395 | ||
4396 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
4397 | end Conditional_Statements_End; | |
4398 | ||
3cce7f32 | 4399 | ------------------------- |
4400 | -- Convert_From_Bignum -- | |
4401 | ------------------------- | |
4402 | ||
4403 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
4404 | Loc : constant Source_Ptr := Sloc (N); | |
4405 | ||
4406 | begin | |
4407 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
4408 | ||
4409 | -- Construct call From Bignum | |
4410 | ||
4411 | return | |
4412 | Make_Function_Call (Loc, | |
4413 | Name => | |
4414 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4415 | Parameter_Associations => New_List (Relocate_Node (N))); | |
4416 | end Convert_From_Bignum; | |
4417 | ||
4418 | ----------------------- | |
4419 | -- Convert_To_Bignum -- | |
4420 | ----------------------- | |
4421 | ||
4422 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
4423 | Loc : constant Source_Ptr := Sloc (N); | |
4424 | ||
4425 | begin | |
0326b4d4 | 4426 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 4427 | |
4428 | if Is_RTE (Etype (N), RE_Bignum) then | |
4429 | return Relocate_Node (N); | |
4430 | ||
21a55437 | 4431 | -- Otherwise construct call to To_Bignum, converting the operand to the |
4432 | -- required Long_Long_Integer form. | |
3cce7f32 | 4433 | |
4434 | else | |
4435 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
4436 | return | |
4437 | Make_Function_Call (Loc, | |
4438 | Name => | |
4439 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
4440 | Parameter_Associations => New_List ( | |
4441 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
4442 | end if; | |
4443 | end Convert_To_Bignum; | |
4444 | ||
ee6ba406 | 4445 | --------------------- |
4446 | -- Determine_Range -- | |
4447 | --------------------- | |
4448 | ||
6af1bdbc | 4449 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 4450 | type Cache_Index is range 0 .. Cache_Size - 1; |
39a0c1d3 | 4451 | -- Determine size of below cache (power of 2 is more efficient) |
ee6ba406 | 4452 | |
7ac8c2b1 | 4453 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; |
4454 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; | |
4455 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; | |
4456 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
4457 | Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal; | |
4458 | Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal; | |
feff2f05 | 4459 | -- The above arrays are used to implement a small direct cache for |
7ac8c2b1 | 4460 | -- Determine_Range and Determine_Range_R calls. Because of the way these |
4461 | -- subprograms recursively traces subexpressions, and because overflow | |
4462 | -- checking calls the routine on the way up the tree, a quadratic behavior | |
4463 | -- can otherwise be encountered in large expressions. The cache entry for | |
4464 | -- node N is stored in the (N mod Cache_Size) entry, and can be validated | |
4465 | -- by checking the actual node value stored there. The Range_Cache_V array | |
4466 | -- records the setting of Assume_Valid for the cache entry. | |
ee6ba406 | 4467 | |
4468 | procedure Determine_Range | |
9c486805 | 4469 | (N : Node_Id; |
4470 | OK : out Boolean; | |
4471 | Lo : out Uint; | |
4472 | Hi : out Uint; | |
4473 | Assume_Valid : Boolean := False) | |
ee6ba406 | 4474 | is |
e254d721 | 4475 | Typ : Entity_Id := Etype (N); |
4476 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 4477 | |
4478 | Lo_Left : Uint; | |
4479 | Hi_Left : Uint; | |
4480 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 4481 | |
5bb74b99 | 4482 | Lo_Right : Uint := No_Uint; |
4483 | Hi_Right : Uint := No_Uint; | |
8880be85 | 4484 | -- Lo and Hi bounds of right (or only) operand |
4485 | ||
4486 | Bound : Node_Id; | |
4487 | -- Temp variable used to hold a bound node | |
4488 | ||
4489 | Hbound : Uint; | |
4490 | -- High bound of base type of expression | |
4491 | ||
4492 | Lor : Uint; | |
4493 | Hir : Uint; | |
4494 | -- Refined values for low and high bounds, after tightening | |
4495 | ||
4496 | OK1 : Boolean; | |
4497 | -- Used in lower level calls to indicate if call succeeded | |
4498 | ||
4499 | Cindex : Cache_Index; | |
4500 | -- Used to search cache | |
ee6ba406 | 4501 | |
094ed68e | 4502 | Btyp : Entity_Id; |
4503 | -- Base type | |
4504 | ||
ee6ba406 | 4505 | function OK_Operands return Boolean; |
4506 | -- Used for binary operators. Determines the ranges of the left and | |
4507 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 4508 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 4509 | |
4510 | ----------------- | |
4511 | -- OK_Operands -- | |
4512 | ----------------- | |
4513 | ||
4514 | function OK_Operands return Boolean is | |
4515 | begin | |
9c486805 | 4516 | Determine_Range |
4517 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 4518 | |
4519 | if not OK1 then | |
4520 | return False; | |
4521 | end if; | |
4522 | ||
9c486805 | 4523 | Determine_Range |
4524 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4525 | return OK1; |
4526 | end OK_Operands; | |
4527 | ||
4528 | -- Start of processing for Determine_Range | |
4529 | ||
4530 | begin | |
e12b2502 | 4531 | -- Prevent junk warnings by initializing range variables |
4532 | ||
4533 | Lo := No_Uint; | |
4534 | Hi := No_Uint; | |
4535 | Lor := No_Uint; | |
4536 | Hir := No_Uint; | |
4537 | ||
87bdc21d | 4538 | -- For temporary constants internally generated to remove side effects |
4539 | -- we must use the corresponding expression to determine the range of | |
e12b2502 | 4540 | -- the expression. But note that the expander can also generate |
4541 | -- constants in other cases, including deferred constants. | |
87bdc21d | 4542 | |
4543 | if Is_Entity_Name (N) | |
4544 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4545 | and then Ekind (Entity (N)) = E_Constant | |
4546 | and then Is_Internal_Name (Chars (Entity (N))) | |
4547 | then | |
e12b2502 | 4548 | if Present (Expression (Parent (Entity (N)))) then |
4549 | Determine_Range | |
4550 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
87bdc21d | 4551 | |
e12b2502 | 4552 | elsif Present (Full_View (Entity (N))) then |
4553 | Determine_Range | |
4554 | (Expression (Parent (Full_View (Entity (N)))), | |
4555 | OK, Lo, Hi, Assume_Valid); | |
ee6ba406 | 4556 | |
e12b2502 | 4557 | else |
4558 | OK := False; | |
4559 | end if; | |
4560 | return; | |
4561 | end if; | |
ee6ba406 | 4562 | |
a781c0fc | 4563 | -- If type is not defined, we can't determine its range |
ee6ba406 | 4564 | |
a781c0fc | 4565 | if No (Typ) |
4566 | ||
4567 | -- We don't deal with anything except discrete types | |
4568 | ||
4569 | or else not Is_Discrete_Type (Typ) | |
4570 | ||
cdfda0e3 | 4571 | -- Don't deal with enumerated types with non-standard representation |
4572 | ||
4573 | or else (Is_Enumeration_Type (Typ) | |
4574 | and then Present (Enum_Pos_To_Rep (Base_Type (Typ)))) | |
4575 | ||
a781c0fc | 4576 | -- Ignore type for which an error has been posted, since range in |
4577 | -- this case may well be a bogosity deriving from the error. Also | |
4578 | -- ignore if error posted on the reference node. | |
4579 | ||
4580 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 4581 | then |
4582 | OK := False; | |
4583 | return; | |
4584 | end if; | |
4585 | ||
4586 | -- For all other cases, we can determine the range | |
4587 | ||
4588 | OK := True; | |
4589 | ||
feff2f05 | 4590 | -- If value is compile time known, then the possible range is the one |
39a0c1d3 | 4591 | -- value that we know this expression definitely has. |
ee6ba406 | 4592 | |
4593 | if Compile_Time_Known_Value (N) then | |
4594 | Lo := Expr_Value (N); | |
4595 | Hi := Lo; | |
4596 | return; | |
4597 | end if; | |
4598 | ||
4599 | -- Return if already in the cache | |
4600 | ||
4601 | Cindex := Cache_Index (N mod Cache_Size); | |
4602 | ||
9c486805 | 4603 | if Determine_Range_Cache_N (Cindex) = N |
4604 | and then | |
4605 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4606 | then | |
ee6ba406 | 4607 | Lo := Determine_Range_Cache_Lo (Cindex); |
4608 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4609 | return; | |
4610 | end if; | |
4611 | ||
feff2f05 | 4612 | -- Otherwise, start by finding the bounds of the type of the expression, |
4613 | -- the value cannot be outside this range (if it is, then we have an | |
4614 | -- overflow situation, which is a separate check, we are talking here | |
4615 | -- only about the expression value). | |
ee6ba406 | 4616 | |
341bd953 | 4617 | -- First a check, never try to find the bounds of a generic type, since |
4618 | -- these bounds are always junk values, and it is only valid to look at | |
4619 | -- the bounds in an instance. | |
4620 | ||
4621 | if Is_Generic_Type (Typ) then | |
4622 | OK := False; | |
4623 | return; | |
4624 | end if; | |
4625 | ||
9c486805 | 4626 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4627 | |
9c486805 | 4628 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4629 | or else Assume_No_Invalid_Values | |
4630 | or else Assume_Valid | |
e254d721 | 4631 | then |
1916d94e | 4632 | -- If this is a known valid constant with a nonstatic value, it may |
4633 | -- have inherited a narrower subtype from its initial value; use this | |
4634 | -- saved subtype (see sem_ch3.adb). | |
4635 | ||
4636 | if Is_Entity_Name (N) | |
4637 | and then Ekind (Entity (N)) = E_Constant | |
4638 | and then Present (Actual_Subtype (Entity (N))) | |
4639 | then | |
4640 | Typ := Actual_Subtype (Entity (N)); | |
4641 | end if; | |
4642 | ||
9c486805 | 4643 | else |
4644 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4645 | end if; |
4646 | ||
094ed68e | 4647 | -- Retrieve the base type. Handle the case where the base type is a |
4648 | -- private enumeration type. | |
4649 | ||
4650 | Btyp := Base_Type (Typ); | |
4651 | ||
4652 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4653 | Btyp := Full_View (Btyp); | |
4654 | end if; | |
4655 | ||
feff2f05 | 4656 | -- We use the actual bound unless it is dynamic, in which case use the |
4657 | -- corresponding base type bound if possible. If we can't get a bound | |
4658 | -- then we figure we can't determine the range (a peculiar case, that | |
4659 | -- perhaps cannot happen, but there is no point in bombing in this | |
4660 | -- optimization circuit. | |
8880be85 | 4661 | |
4662 | -- First the low bound | |
ee6ba406 | 4663 | |
4664 | Bound := Type_Low_Bound (Typ); | |
4665 | ||
4666 | if Compile_Time_Known_Value (Bound) then | |
4667 | Lo := Expr_Value (Bound); | |
4668 | ||
094ed68e | 4669 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4670 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4671 | |
4672 | else | |
4673 | OK := False; | |
4674 | return; | |
4675 | end if; | |
4676 | ||
8880be85 | 4677 | -- Now the high bound |
4678 | ||
ee6ba406 | 4679 | Bound := Type_High_Bound (Typ); |
4680 | ||
8880be85 | 4681 | -- We need the high bound of the base type later on, and this should |
4682 | -- always be compile time known. Again, it is not clear that this | |
4683 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4684 | |
094ed68e | 4685 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4686 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4687 | Hi := Hbound; |
4688 | ||
4689 | else | |
4690 | OK := False; | |
4691 | return; | |
4692 | end if; | |
4693 | ||
feff2f05 | 4694 | -- If we have a static subtype, then that may have a tighter bound so |
4695 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4696 | |
4697 | if Compile_Time_Known_Value (Bound) then | |
4698 | Hi := Expr_Value (Bound); | |
4699 | end if; | |
4700 | ||
feff2f05 | 4701 | -- We may be able to refine this value in certain situations. If any |
4702 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4703 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4704 | |
4705 | case Nkind (N) is | |
4706 | ||
4707 | -- For unary plus, result is limited by range of operand | |
4708 | ||
4709 | when N_Op_Plus => | |
9c486805 | 4710 | Determine_Range |
4711 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4712 | |
4713 | -- For unary minus, determine range of operand, and negate it | |
4714 | ||
4715 | when N_Op_Minus => | |
9c486805 | 4716 | Determine_Range |
4717 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4718 | |
4719 | if OK1 then | |
4720 | Lor := -Hi_Right; | |
4721 | Hir := -Lo_Right; | |
4722 | end if; | |
4723 | ||
4724 | -- For binary addition, get range of each operand and do the | |
4725 | -- addition to get the result range. | |
4726 | ||
4727 | when N_Op_Add => | |
4728 | if OK_Operands then | |
4729 | Lor := Lo_Left + Lo_Right; | |
4730 | Hir := Hi_Left + Hi_Right; | |
4731 | end if; | |
4732 | ||
feff2f05 | 4733 | -- Division is tricky. The only case we consider is where the right |
4734 | -- operand is a positive constant, and in this case we simply divide | |
4735 | -- the bounds of the left operand | |
ee6ba406 | 4736 | |
4737 | when N_Op_Divide => | |
4738 | if OK_Operands then | |
4739 | if Lo_Right = Hi_Right | |
4740 | and then Lo_Right > 0 | |
4741 | then | |
4742 | Lor := Lo_Left / Lo_Right; | |
4743 | Hir := Hi_Left / Lo_Right; | |
ee6ba406 | 4744 | else |
4745 | OK1 := False; | |
4746 | end if; | |
4747 | end if; | |
4748 | ||
feff2f05 | 4749 | -- For binary subtraction, get range of each operand and do the worst |
4750 | -- case subtraction to get the result range. | |
ee6ba406 | 4751 | |
4752 | when N_Op_Subtract => | |
4753 | if OK_Operands then | |
4754 | Lor := Lo_Left - Hi_Right; | |
4755 | Hir := Hi_Left - Lo_Right; | |
4756 | end if; | |
4757 | ||
feff2f05 | 4758 | -- For MOD, if right operand is a positive constant, then result must |
4759 | -- be in the allowable range of mod results. | |
ee6ba406 | 4760 | |
4761 | when N_Op_Mod => | |
4762 | if OK_Operands then | |
9dfe12ae | 4763 | if Lo_Right = Hi_Right |
4764 | and then Lo_Right /= 0 | |
4765 | then | |
ee6ba406 | 4766 | if Lo_Right > 0 then |
4767 | Lor := Uint_0; | |
4768 | Hir := Lo_Right - 1; | |
4769 | ||
9dfe12ae | 4770 | else -- Lo_Right < 0 |
ee6ba406 | 4771 | Lor := Lo_Right + 1; |
4772 | Hir := Uint_0; | |
4773 | end if; | |
4774 | ||
4775 | else | |
4776 | OK1 := False; | |
4777 | end if; | |
4778 | end if; | |
4779 | ||
feff2f05 | 4780 | -- For REM, if right operand is a positive constant, then result must |
4781 | -- be in the allowable range of mod results. | |
ee6ba406 | 4782 | |
4783 | when N_Op_Rem => | |
4784 | if OK_Operands then | |
99378362 | 4785 | if Lo_Right = Hi_Right and then Lo_Right /= 0 then |
ee6ba406 | 4786 | declare |
4787 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4788 | ||
4789 | begin | |
4790 | -- The sign of the result depends on the sign of the | |
4791 | -- dividend (but not on the sign of the divisor, hence | |
4792 | -- the abs operation above). | |
4793 | ||
4794 | if Lo_Left < 0 then | |
4795 | Lor := -Dval; | |
4796 | else | |
4797 | Lor := Uint_0; | |
4798 | end if; | |
4799 | ||
4800 | if Hi_Left < 0 then | |
4801 | Hir := Uint_0; | |
4802 | else | |
4803 | Hir := Dval; | |
4804 | end if; | |
4805 | end; | |
4806 | ||
4807 | else | |
4808 | OK1 := False; | |
4809 | end if; | |
4810 | end if; | |
4811 | ||
4812 | -- Attribute reference cases | |
4813 | ||
4814 | when N_Attribute_Reference => | |
4815 | case Attribute_Name (N) is | |
4816 | ||
4817 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4818 | -- possible range of values of the attribute expression. |
ee6ba406 | 4819 | |
99378362 | 4820 | when Name_Pos |
4821 | | Name_Val | |
4822 | => | |
9c486805 | 4823 | Determine_Range |
4824 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4825 | |
4826 | -- For Length attribute, use the bounds of the corresponding | |
4827 | -- index type to refine the range. | |
4828 | ||
4829 | when Name_Length => | |
4830 | declare | |
4831 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4832 | Inum : Nat; | |
4833 | Indx : Node_Id; | |
4834 | ||
4835 | LL, LU : Uint; | |
4836 | UL, UU : Uint; | |
4837 | ||
4838 | begin | |
4839 | if Is_Access_Type (Atyp) then | |
4840 | Atyp := Designated_Type (Atyp); | |
4841 | end if; | |
4842 | ||
4843 | -- For string literal, we know exact value | |
4844 | ||
4845 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4846 | OK := True; | |
4847 | Lo := String_Literal_Length (Atyp); | |
4848 | Hi := String_Literal_Length (Atyp); | |
4849 | return; | |
4850 | end if; | |
4851 | ||
4852 | -- Otherwise check for expression given | |
4853 | ||
4854 | if No (Expressions (N)) then | |
4855 | Inum := 1; | |
4856 | else | |
4857 | Inum := | |
4858 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4859 | end if; | |
4860 | ||
4861 | Indx := First_Index (Atyp); | |
4862 | for J in 2 .. Inum loop | |
4863 | Indx := Next_Index (Indx); | |
4864 | end loop; | |
4865 | ||
9116df93 | 4866 | -- If the index type is a formal type or derived from |
c8da6114 | 4867 | -- one, the bounds are not static. |
4868 | ||
4869 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4870 | OK := False; | |
4871 | return; | |
4872 | end if; | |
4873 | ||
ee6ba406 | 4874 | Determine_Range |
9c486805 | 4875 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4876 | Assume_Valid); | |
ee6ba406 | 4877 | |
4878 | if OK1 then | |
4879 | Determine_Range | |
9c486805 | 4880 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4881 | Assume_Valid); | |
ee6ba406 | 4882 | |
4883 | if OK1 then | |
4884 | ||
4885 | -- The maximum value for Length is the biggest | |
4886 | -- possible gap between the values of the bounds. | |
4887 | -- But of course, this value cannot be negative. | |
4888 | ||
9c486805 | 4889 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4890 | |
4891 | -- For constrained arrays, the minimum value for | |
4892 | -- Length is taken from the actual value of the | |
9116df93 | 4893 | -- bounds, since the index will be exactly of this |
4894 | -- subtype. | |
ee6ba406 | 4895 | |
4896 | if Is_Constrained (Atyp) then | |
9c486805 | 4897 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4898 | |
4899 | -- For an unconstrained array, the minimum value | |
4900 | -- for length is always zero. | |
4901 | ||
4902 | else | |
4903 | Lor := Uint_0; | |
4904 | end if; | |
4905 | end if; | |
4906 | end if; | |
4907 | end; | |
4908 | ||
4909 | -- No special handling for other attributes | |
9116df93 | 4910 | -- Probably more opportunities exist here??? |
ee6ba406 | 4911 | |
4912 | when others => | |
4913 | OK1 := False; | |
4914 | ||
4915 | end case; | |
4916 | ||
ee6ba406 | 4917 | when N_Type_Conversion => |
54022749 | 4918 | |
4919 | -- For type conversion from one discrete type to another, we can | |
4920 | -- refine the range using the converted value. | |
4921 | ||
4922 | if Is_Discrete_Type (Etype (Expression (N))) then | |
4923 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
4924 | ||
4925 | -- When converting a float to an integer type, determine the range | |
4926 | -- in real first, and then convert the bounds using UR_To_Uint | |
4927 | -- which correctly rounds away from zero when half way between two | |
4928 | -- integers, as required by normal Ada 95 rounding semantics. It | |
4929 | -- is only possible because analysis in GNATprove rules out the | |
4930 | -- possibility of a NaN or infinite value. | |
4931 | ||
4932 | elsif GNATprove_Mode | |
4933 | and then Is_Floating_Point_Type (Etype (Expression (N))) | |
4934 | then | |
4935 | declare | |
4936 | Lor_Real, Hir_Real : Ureal; | |
4937 | begin | |
4938 | Determine_Range_R (Expression (N), OK1, Lor_Real, Hir_Real, | |
4939 | Assume_Valid); | |
4940 | ||
4941 | if OK1 then | |
4942 | Lor := UR_To_Uint (Lor_Real); | |
4943 | Hir := UR_To_Uint (Hir_Real); | |
4944 | end if; | |
4945 | end; | |
4946 | ||
4947 | else | |
4948 | OK1 := False; | |
4949 | end if; | |
ee6ba406 | 4950 | |
4951 | -- Nothing special to do for all other expression kinds | |
4952 | ||
4953 | when others => | |
4954 | OK1 := False; | |
4955 | Lor := No_Uint; | |
4956 | Hir := No_Uint; | |
4957 | end case; | |
4958 | ||
9116df93 | 4959 | -- At this stage, if OK1 is true, then we know that the actual result of |
4960 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4961 | -- to restrict the possible range of results. | |
ee6ba406 | 4962 | |
4963 | if OK1 then | |
4964 | ||
9116df93 | 4965 | -- If the refined value of the low bound is greater than the type |
7ac8c2b1 | 4966 | -- low bound, then reset it to the more restrictive value. However, |
9116df93 | 4967 | -- we do NOT do this for the case of a modular type where the |
4968 | -- possible upper bound on the value is above the base type high | |
4969 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4970 | |
4971 | if Lor > Lo | |
9116df93 | 4972 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4973 | then |
4974 | Lo := Lor; | |
4975 | end if; | |
4976 | ||
9116df93 | 4977 | -- Similarly, if the refined value of the high bound is less than the |
4978 | -- value so far, then reset it to the more restrictive value. Again, | |
4979 | -- we do not do this if the refined low bound is negative for a | |
4980 | -- modular type, since this would wrap. | |
ee6ba406 | 4981 | |
4982 | if Hir < Hi | |
9116df93 | 4983 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4984 | then |
4985 | Hi := Hir; | |
4986 | end if; | |
4987 | end if; | |
4988 | ||
4989 | -- Set cache entry for future call and we are all done | |
4990 | ||
4991 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4992 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4993 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4994 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4995 | return; | |
4996 | ||
9116df93 | 4997 | -- If any exception occurs, it means that we have some bug in the compiler, |
4998 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4999 | -- occurrence. However, this is only an optimization attempt, so there is |
5000 | -- really no point in crashing the compiler. Instead we just decide, too | |
5001 | -- bad, we can't figure out a range in this case after all. | |
5002 | ||
5003 | exception | |
5004 | when others => | |
5005 | ||
5006 | -- Debug flag K disables this behavior (useful for debugging) | |
5007 | ||
5008 | if Debug_Flag_K then | |
5009 | raise; | |
5010 | else | |
5011 | OK := False; | |
5012 | Lo := No_Uint; | |
5013 | Hi := No_Uint; | |
5014 | return; | |
5015 | end if; | |
ee6ba406 | 5016 | end Determine_Range; |
5017 | ||
7ac8c2b1 | 5018 | ----------------------- |
5019 | -- Determine_Range_R -- | |
5020 | ----------------------- | |
5021 | ||
5022 | procedure Determine_Range_R | |
5023 | (N : Node_Id; | |
5024 | OK : out Boolean; | |
5025 | Lo : out Ureal; | |
5026 | Hi : out Ureal; | |
5027 | Assume_Valid : Boolean := False) | |
5028 | is | |
5029 | Typ : Entity_Id := Etype (N); | |
5030 | -- Type to use, may get reset to base type for possibly invalid entity | |
5031 | ||
5032 | Lo_Left : Ureal; | |
5033 | Hi_Left : Ureal; | |
5034 | -- Lo and Hi bounds of left operand | |
5035 | ||
5bb74b99 | 5036 | Lo_Right : Ureal := No_Ureal; |
5037 | Hi_Right : Ureal := No_Ureal; | |
7ac8c2b1 | 5038 | -- Lo and Hi bounds of right (or only) operand |
5039 | ||
5040 | Bound : Node_Id; | |
5041 | -- Temp variable used to hold a bound node | |
5042 | ||
5043 | Hbound : Ureal; | |
5044 | -- High bound of base type of expression | |
5045 | ||
5046 | Lor : Ureal; | |
5047 | Hir : Ureal; | |
5048 | -- Refined values for low and high bounds, after tightening | |
5049 | ||
5050 | OK1 : Boolean; | |
5051 | -- Used in lower level calls to indicate if call succeeded | |
5052 | ||
5053 | Cindex : Cache_Index; | |
5054 | -- Used to search cache | |
5055 | ||
5056 | Btyp : Entity_Id; | |
5057 | -- Base type | |
5058 | ||
5059 | function OK_Operands return Boolean; | |
5060 | -- Used for binary operators. Determines the ranges of the left and | |
5061 | -- right operands, and if they are both OK, returns True, and puts | |
5062 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. | |
5063 | ||
5064 | function Round_Machine (B : Ureal) return Ureal; | |
5065 | -- B is a real bound. Round it using mode Round_Even. | |
5066 | ||
5067 | ----------------- | |
5068 | -- OK_Operands -- | |
5069 | ----------------- | |
5070 | ||
5071 | function OK_Operands return Boolean is | |
5072 | begin | |
5073 | Determine_Range_R | |
5074 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
5075 | ||
5076 | if not OK1 then | |
5077 | return False; | |
5078 | end if; | |
5079 | ||
5080 | Determine_Range_R | |
5081 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5082 | return OK1; | |
5083 | end OK_Operands; | |
5084 | ||
5085 | ------------------- | |
5086 | -- Round_Machine -- | |
5087 | ------------------- | |
5088 | ||
5089 | function Round_Machine (B : Ureal) return Ureal is | |
5090 | begin | |
5091 | return Machine (Typ, B, Round_Even, N); | |
5092 | end Round_Machine; | |
5093 | ||
5094 | -- Start of processing for Determine_Range_R | |
5095 | ||
5096 | begin | |
5097 | -- Prevent junk warnings by initializing range variables | |
5098 | ||
5099 | Lo := No_Ureal; | |
5100 | Hi := No_Ureal; | |
5101 | Lor := No_Ureal; | |
5102 | Hir := No_Ureal; | |
5103 | ||
5104 | -- For temporary constants internally generated to remove side effects | |
5105 | -- we must use the corresponding expression to determine the range of | |
5106 | -- the expression. But note that the expander can also generate | |
5107 | -- constants in other cases, including deferred constants. | |
5108 | ||
5109 | if Is_Entity_Name (N) | |
5110 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
5111 | and then Ekind (Entity (N)) = E_Constant | |
5112 | and then Is_Internal_Name (Chars (Entity (N))) | |
5113 | then | |
5114 | if Present (Expression (Parent (Entity (N)))) then | |
5115 | Determine_Range_R | |
5116 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
5117 | ||
5118 | elsif Present (Full_View (Entity (N))) then | |
5119 | Determine_Range_R | |
5120 | (Expression (Parent (Full_View (Entity (N)))), | |
5121 | OK, Lo, Hi, Assume_Valid); | |
5122 | ||
5123 | else | |
5124 | OK := False; | |
5125 | end if; | |
4c1c7f3f | 5126 | |
7ac8c2b1 | 5127 | return; |
5128 | end if; | |
5129 | ||
5130 | -- If type is not defined, we can't determine its range | |
5131 | ||
5132 | if No (Typ) | |
5133 | ||
5134 | -- We don't deal with anything except IEEE floating-point types | |
5135 | ||
5136 | or else not Is_Floating_Point_Type (Typ) | |
5137 | or else Float_Rep (Typ) /= IEEE_Binary | |
5138 | ||
5139 | -- Ignore type for which an error has been posted, since range in | |
5140 | -- this case may well be a bogosity deriving from the error. Also | |
5141 | -- ignore if error posted on the reference node. | |
5142 | ||
5143 | or else Error_Posted (N) or else Error_Posted (Typ) | |
5144 | then | |
5145 | OK := False; | |
5146 | return; | |
5147 | end if; | |
5148 | ||
5149 | -- For all other cases, we can determine the range | |
5150 | ||
5151 | OK := True; | |
5152 | ||
5153 | -- If value is compile time known, then the possible range is the one | |
5154 | -- value that we know this expression definitely has. | |
5155 | ||
5156 | if Compile_Time_Known_Value (N) then | |
5157 | Lo := Expr_Value_R (N); | |
5158 | Hi := Lo; | |
5159 | return; | |
5160 | end if; | |
5161 | ||
5162 | -- Return if already in the cache | |
5163 | ||
5164 | Cindex := Cache_Index (N mod Cache_Size); | |
5165 | ||
5166 | if Determine_Range_Cache_N (Cindex) = N | |
5167 | and then | |
5168 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
5169 | then | |
5170 | Lo := Determine_Range_Cache_Lo_R (Cindex); | |
5171 | Hi := Determine_Range_Cache_Hi_R (Cindex); | |
5172 | return; | |
5173 | end if; | |
5174 | ||
5175 | -- Otherwise, start by finding the bounds of the type of the expression, | |
5176 | -- the value cannot be outside this range (if it is, then we have an | |
5177 | -- overflow situation, which is a separate check, we are talking here | |
5178 | -- only about the expression value). | |
5179 | ||
5180 | -- First a check, never try to find the bounds of a generic type, since | |
5181 | -- these bounds are always junk values, and it is only valid to look at | |
5182 | -- the bounds in an instance. | |
5183 | ||
5184 | if Is_Generic_Type (Typ) then | |
5185 | OK := False; | |
5186 | return; | |
5187 | end if; | |
5188 | ||
5189 | -- First step, change to use base type unless we know the value is valid | |
5190 | ||
5191 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) | |
5192 | or else Assume_No_Invalid_Values | |
5193 | or else Assume_Valid | |
5194 | then | |
5195 | null; | |
5196 | else | |
5197 | Typ := Underlying_Type (Base_Type (Typ)); | |
5198 | end if; | |
5199 | ||
5200 | -- Retrieve the base type. Handle the case where the base type is a | |
5201 | -- private type. | |
5202 | ||
5203 | Btyp := Base_Type (Typ); | |
5204 | ||
5205 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
5206 | Btyp := Full_View (Btyp); | |
5207 | end if; | |
5208 | ||
5209 | -- We use the actual bound unless it is dynamic, in which case use the | |
5210 | -- corresponding base type bound if possible. If we can't get a bound | |
5211 | -- then we figure we can't determine the range (a peculiar case, that | |
5212 | -- perhaps cannot happen, but there is no point in bombing in this | |
5213 | -- optimization circuit). | |
5214 | ||
5215 | -- First the low bound | |
5216 | ||
5217 | Bound := Type_Low_Bound (Typ); | |
5218 | ||
5219 | if Compile_Time_Known_Value (Bound) then | |
5220 | Lo := Expr_Value_R (Bound); | |
5221 | ||
5222 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then | |
5223 | Lo := Expr_Value_R (Type_Low_Bound (Btyp)); | |
5224 | ||
5225 | else | |
5226 | OK := False; | |
5227 | return; | |
5228 | end if; | |
5229 | ||
5230 | -- Now the high bound | |
5231 | ||
5232 | Bound := Type_High_Bound (Typ); | |
5233 | ||
5234 | -- We need the high bound of the base type later on, and this should | |
5235 | -- always be compile time known. Again, it is not clear that this | |
5236 | -- can ever be false, but no point in bombing. | |
5237 | ||
5238 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then | |
5239 | Hbound := Expr_Value_R (Type_High_Bound (Btyp)); | |
5240 | Hi := Hbound; | |
5241 | ||
5242 | else | |
5243 | OK := False; | |
5244 | return; | |
5245 | end if; | |
5246 | ||
5247 | -- If we have a static subtype, then that may have a tighter bound so | |
5248 | -- use the upper bound of the subtype instead in this case. | |
5249 | ||
5250 | if Compile_Time_Known_Value (Bound) then | |
5251 | Hi := Expr_Value_R (Bound); | |
5252 | end if; | |
5253 | ||
5254 | -- We may be able to refine this value in certain situations. If any | |
5255 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
5256 | -- bounds, and OK1 is set to True. | |
5257 | ||
5258 | case Nkind (N) is | |
5259 | ||
5260 | -- For unary plus, result is limited by range of operand | |
5261 | ||
5262 | when N_Op_Plus => | |
5263 | Determine_Range_R | |
5264 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
5265 | ||
5266 | -- For unary minus, determine range of operand, and negate it | |
5267 | ||
5268 | when N_Op_Minus => | |
5269 | Determine_Range_R | |
5270 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5271 | ||
5272 | if OK1 then | |
5273 | Lor := -Hi_Right; | |
5274 | Hir := -Lo_Right; | |
5275 | end if; | |
5276 | ||
5277 | -- For binary addition, get range of each operand and do the | |
5278 | -- addition to get the result range. | |
5279 | ||
5280 | when N_Op_Add => | |
5281 | if OK_Operands then | |
5282 | Lor := Round_Machine (Lo_Left + Lo_Right); | |
5283 | Hir := Round_Machine (Hi_Left + Hi_Right); | |
5284 | end if; | |
5285 | ||
5286 | -- For binary subtraction, get range of each operand and do the worst | |
5287 | -- case subtraction to get the result range. | |
5288 | ||
5289 | when N_Op_Subtract => | |
5290 | if OK_Operands then | |
5291 | Lor := Round_Machine (Lo_Left - Hi_Right); | |
5292 | Hir := Round_Machine (Hi_Left - Lo_Right); | |
5293 | end if; | |
5294 | ||
5295 | -- For multiplication, get range of each operand and do the | |
5296 | -- four multiplications to get the result range. | |
5297 | ||
5298 | when N_Op_Multiply => | |
5299 | if OK_Operands then | |
5300 | declare | |
5301 | M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right); | |
5302 | M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right); | |
5303 | M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right); | |
5304 | M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right); | |
552d7cbc | 5305 | |
7ac8c2b1 | 5306 | begin |
5307 | Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4)); | |
5308 | Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4)); | |
5309 | end; | |
5310 | end if; | |
5311 | ||
5312 | -- For division, consider separately the cases where the right | |
5313 | -- operand is positive or negative. Otherwise, the right operand | |
5314 | -- can be arbitrarily close to zero, so the result is likely to | |
5315 | -- be unbounded in one direction, do not attempt to compute it. | |
5316 | ||
5317 | when N_Op_Divide => | |
5318 | if OK_Operands then | |
5319 | ||
5320 | -- Right operand is positive | |
5321 | ||
5322 | if Lo_Right > Ureal_0 then | |
5323 | ||
5324 | -- If the low bound of the left operand is negative, obtain | |
5325 | -- the overall low bound by dividing it by the smallest | |
5326 | -- value of the right operand, and otherwise by the largest | |
5327 | -- value of the right operand. | |
5328 | ||
5329 | if Lo_Left < Ureal_0 then | |
5330 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5331 | else | |
5332 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5333 | end if; | |
5334 | ||
5335 | -- If the high bound of the left operand is negative, obtain | |
5336 | -- the overall high bound by dividing it by the largest | |
5337 | -- value of the right operand, and otherwise by the | |
5338 | -- smallest value of the right operand. | |
5339 | ||
5340 | if Hi_Left < Ureal_0 then | |
5341 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5342 | else | |
5343 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5344 | end if; | |
5345 | ||
5346 | -- Right operand is negative | |
5347 | ||
5348 | elsif Hi_Right < Ureal_0 then | |
5349 | ||
5350 | -- If the low bound of the left operand is negative, obtain | |
5351 | -- the overall low bound by dividing it by the largest | |
5352 | -- value of the right operand, and otherwise by the smallest | |
5353 | -- value of the right operand. | |
5354 | ||
5355 | if Lo_Left < Ureal_0 then | |
5356 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5357 | else | |
5358 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5359 | end if; | |
5360 | ||
5361 | -- If the high bound of the left operand is negative, obtain | |
5362 | -- the overall high bound by dividing it by the smallest | |
5363 | -- value of the right operand, and otherwise by the | |
5364 | -- largest value of the right operand. | |
5365 | ||
5366 | if Hi_Left < Ureal_0 then | |
5367 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5368 | else | |
5369 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5370 | end if; | |
5371 | ||
5372 | else | |
5373 | OK1 := False; | |
5374 | end if; | |
5375 | end if; | |
5376 | ||
7ac8c2b1 | 5377 | when N_Type_Conversion => |
3c5c732f | 5378 | |
5379 | -- For type conversion from one floating-point type to another, we | |
5380 | -- can refine the range using the converted value. | |
5381 | ||
5382 | if Is_Floating_Point_Type (Etype (Expression (N))) then | |
5383 | Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
5384 | ||
5385 | -- When converting an integer to a floating-point type, determine | |
5386 | -- the range in integer first, and then convert the bounds. | |
5387 | ||
5388 | elsif Is_Discrete_Type (Etype (Expression (N))) then | |
5389 | declare | |
552d7cbc | 5390 | Hir_Int : Uint; |
5391 | Lor_Int : Uint; | |
5392 | ||
3c5c732f | 5393 | begin |
552d7cbc | 5394 | Determine_Range |
5395 | (Expression (N), OK1, Lor_Int, Hir_Int, Assume_Valid); | |
3c5c732f | 5396 | |
5397 | if OK1 then | |
5398 | Lor := Round_Machine (UR_From_Uint (Lor_Int)); | |
5399 | Hir := Round_Machine (UR_From_Uint (Hir_Int)); | |
5400 | end if; | |
5401 | end; | |
5402 | ||
5403 | else | |
5404 | OK1 := False; | |
5405 | end if; | |
7ac8c2b1 | 5406 | |
5407 | -- Nothing special to do for all other expression kinds | |
5408 | ||
5409 | when others => | |
5410 | OK1 := False; | |
5411 | Lor := No_Ureal; | |
5412 | Hir := No_Ureal; | |
5413 | end case; | |
5414 | ||
5415 | -- At this stage, if OK1 is true, then we know that the actual result of | |
5416 | -- the computed expression is in the range Lor .. Hir. We can use this | |
5417 | -- to restrict the possible range of results. | |
5418 | ||
5419 | if OK1 then | |
5420 | ||
5421 | -- If the refined value of the low bound is greater than the type | |
5422 | -- low bound, then reset it to the more restrictive value. | |
5423 | ||
5424 | if Lor > Lo then | |
5425 | Lo := Lor; | |
5426 | end if; | |
5427 | ||
5428 | -- Similarly, if the refined value of the high bound is less than the | |
5429 | -- value so far, then reset it to the more restrictive value. | |
5430 | ||
5431 | if Hir < Hi then | |
5432 | Hi := Hir; | |
5433 | end if; | |
5434 | end if; | |
5435 | ||
5436 | -- Set cache entry for future call and we are all done | |
5437 | ||
5438 | Determine_Range_Cache_N (Cindex) := N; | |
5439 | Determine_Range_Cache_V (Cindex) := Assume_Valid; | |
5440 | Determine_Range_Cache_Lo_R (Cindex) := Lo; | |
5441 | Determine_Range_Cache_Hi_R (Cindex) := Hi; | |
5442 | return; | |
5443 | ||
5444 | -- If any exception occurs, it means that we have some bug in the compiler, | |
5445 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
5446 | -- occurrence. However, this is only an optimization attempt, so there is | |
5447 | -- really no point in crashing the compiler. Instead we just decide, too | |
5448 | -- bad, we can't figure out a range in this case after all. | |
5449 | ||
5450 | exception | |
5451 | when others => | |
5452 | ||
5453 | -- Debug flag K disables this behavior (useful for debugging) | |
5454 | ||
5455 | if Debug_Flag_K then | |
5456 | raise; | |
5457 | else | |
5458 | OK := False; | |
5459 | Lo := No_Ureal; | |
5460 | Hi := No_Ureal; | |
5461 | return; | |
5462 | end if; | |
5463 | end Determine_Range_R; | |
5464 | ||
ee6ba406 | 5465 | ------------------------------------ |
5466 | -- Discriminant_Checks_Suppressed -- | |
5467 | ------------------------------------ | |
5468 | ||
5469 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5470 | begin | |
9dfe12ae | 5471 | if Present (E) then |
5472 | if Is_Unchecked_Union (E) then | |
5473 | return True; | |
5474 | elsif Checks_May_Be_Suppressed (E) then | |
5475 | return Is_Check_Suppressed (E, Discriminant_Check); | |
5476 | end if; | |
5477 | end if; | |
5478 | ||
fafc6b97 | 5479 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 5480 | end Discriminant_Checks_Suppressed; |
5481 | ||
5482 | -------------------------------- | |
5483 | -- Division_Checks_Suppressed -- | |
5484 | -------------------------------- | |
5485 | ||
5486 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5487 | begin | |
9dfe12ae | 5488 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5489 | return Is_Check_Suppressed (E, Division_Check); | |
5490 | else | |
fafc6b97 | 5491 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 5492 | end if; |
ee6ba406 | 5493 | end Division_Checks_Suppressed; |
5494 | ||
fa771c05 | 5495 | -------------------------------------- |
5496 | -- Duplicated_Tag_Checks_Suppressed -- | |
5497 | -------------------------------------- | |
5498 | ||
5499 | function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5500 | begin | |
5501 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
5502 | return Is_Check_Suppressed (E, Duplicated_Tag_Check); | |
5503 | else | |
5504 | return Scope_Suppress.Suppress (Duplicated_Tag_Check); | |
5505 | end if; | |
5506 | end Duplicated_Tag_Checks_Suppressed; | |
5507 | ||
ee6ba406 | 5508 | ----------------------------------- |
5509 | -- Elaboration_Checks_Suppressed -- | |
5510 | ----------------------------------- | |
5511 | ||
5512 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5513 | begin | |
38f5559f | 5514 | -- The complication in this routine is that if we are in the dynamic |
5515 | -- model of elaboration, we also check All_Checks, since All_Checks | |
5516 | -- does not set Elaboration_Check explicitly. | |
5517 | ||
9dfe12ae | 5518 | if Present (E) then |
5519 | if Kill_Elaboration_Checks (E) then | |
5520 | return True; | |
38f5559f | 5521 | |
9dfe12ae | 5522 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 5523 | if Is_Check_Suppressed (E, Elaboration_Check) then |
5524 | return True; | |
fe48a434 | 5525 | |
38f5559f | 5526 | elsif Dynamic_Elaboration_Checks then |
5527 | return Is_Check_Suppressed (E, All_Checks); | |
fe48a434 | 5528 | |
38f5559f | 5529 | else |
5530 | return False; | |
5531 | end if; | |
9dfe12ae | 5532 | end if; |
5533 | end if; | |
5534 | ||
fafc6b97 | 5535 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 5536 | return True; |
fe48a434 | 5537 | |
38f5559f | 5538 | elsif Dynamic_Elaboration_Checks then |
fafc6b97 | 5539 | return Scope_Suppress.Suppress (All_Checks); |
fe48a434 | 5540 | |
38f5559f | 5541 | else |
5542 | return False; | |
5543 | end if; | |
ee6ba406 | 5544 | end Elaboration_Checks_Suppressed; |
5545 | ||
9dfe12ae | 5546 | --------------------------- |
5547 | -- Enable_Overflow_Check -- | |
5548 | --------------------------- | |
5549 | ||
5550 | procedure Enable_Overflow_Check (N : Node_Id) is | |
4c1c7f3f | 5551 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 5552 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 5553 | Chk : Nat; |
5554 | OK : Boolean; | |
5555 | Ent : Entity_Id; | |
5556 | Ofs : Uint; | |
5557 | Lo : Uint; | |
5558 | Hi : Uint; | |
ee6ba406 | 5559 | |
f3ccbbb3 | 5560 | Do_Ovflow_Check : Boolean; |
5561 | ||
ee6ba406 | 5562 | begin |
9dfe12ae | 5563 | if Debug_Flag_CC then |
5564 | w ("Enable_Overflow_Check for node ", Int (N)); | |
5565 | Write_Str (" Source location = "); | |
5566 | wl (Sloc (N)); | |
00c403ee | 5567 | pg (Union_Id (N)); |
ee6ba406 | 5568 | end if; |
ee6ba406 | 5569 | |
75209ec5 | 5570 | -- No check if overflow checks suppressed for type of node |
5571 | ||
0df9d43f | 5572 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 5573 | return; |
5574 | ||
49260fa5 | 5575 | -- Nothing to do for unsigned integer types, which do not overflow |
5576 | ||
5577 | elsif Is_Modular_Integer_Type (Typ) then | |
5578 | return; | |
3cce7f32 | 5579 | end if; |
5580 | ||
0df9d43f | 5581 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 5582 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
5583 | -- probably more extreme that it needs to be, but what is going on here | |
5584 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 5585 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 5586 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 5587 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 5588 | -- legacy reliable. |
3cce7f32 | 5589 | |
0df9d43f | 5590 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 5591 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
5592 | -- know that no check is needed. We skip all that in the two new modes, | |
5593 | -- since really overflow checking happens over a whole subtree, and we | |
5594 | -- do the corresponding optimizations later on when applying the checks. | |
5595 | ||
5596 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 5597 | if not (Overflow_Checks_Suppressed (Etype (N))) |
5598 | and then not (Is_Entity_Name (N) | |
5599 | and then Overflow_Checks_Suppressed (Entity (N))) | |
5600 | then | |
5601 | Activate_Overflow_Check (N); | |
5602 | end if; | |
3cce7f32 | 5603 | |
5604 | if Debug_Flag_CC then | |
5605 | w ("Minimized/Eliminated mode"); | |
5606 | end if; | |
5607 | ||
5608 | return; | |
5609 | end if; | |
5610 | ||
0df9d43f | 5611 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 5612 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 5613 | |
feff2f05 | 5614 | -- Nothing to do if the range of the result is known OK. We skip this |
5615 | -- for conversions, since the caller already did the check, and in any | |
5616 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 5617 | -- different. |
ee6ba406 | 5618 | |
3cce7f32 | 5619 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 5620 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 5621 | |
cc60bd16 | 5622 | -- Note in the test below that we assume that the range is not OK |
5623 | -- if a bound of the range is equal to that of the type. That's not | |
5624 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 5625 | |
9dfe12ae | 5626 | -- a) The way that Determine_Range works, it will typically report |
5627 | -- the bounds of the value as being equal to the bounds of the | |
5628 | -- type, because it either can't tell anything more precise, or | |
5629 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 5630 | |
9dfe12ae | 5631 | -- b) It is very unusual to have a situation in which this would |
5632 | -- generate an unnecessary overflow check (an example would be | |
5633 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 5634 | -- literal value one is added). |
ee6ba406 | 5635 | |
9dfe12ae | 5636 | -- c) The alternative is a lot of special casing in this routine |
5637 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 5638 | |
f3ccbbb3 | 5639 | if OK then |
5640 | Do_Ovflow_Check := True; | |
5641 | ||
5642 | -- Note that the following checks are quite deliberately > and < | |
5643 | -- rather than >= and <= as explained above. | |
5644 | ||
5645 | if Lo > Expr_Value (Type_Low_Bound (Typ)) | |
5646 | and then | |
5647 | Hi < Expr_Value (Type_High_Bound (Typ)) | |
5648 | then | |
5649 | Do_Ovflow_Check := False; | |
5650 | ||
5651 | -- Despite the comments above, it is worth dealing specially with | |
5652 | -- division specially. The only case where integer division can | |
5653 | -- overflow is (largest negative number) / (-1). So we will do | |
5654 | -- an extra range analysis to see if this is possible. | |
5655 | ||
5656 | elsif Nkind (N) = N_Op_Divide then | |
5657 | Determine_Range | |
5658 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5659 | ||
5660 | if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then | |
5661 | Do_Ovflow_Check := False; | |
5662 | ||
5663 | else | |
5664 | Determine_Range | |
5665 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5666 | ||
5667 | if OK and then (Lo > Uint_Minus_1 | |
5668 | or else | |
5669 | Hi < Uint_Minus_1) | |
5670 | then | |
5671 | Do_Ovflow_Check := False; | |
5672 | end if; | |
5673 | end if; | |
9dfe12ae | 5674 | end if; |
5675 | ||
f3ccbbb3 | 5676 | -- If no overflow check required, we are done |
5677 | ||
5678 | if not Do_Ovflow_Check then | |
5679 | if Debug_Flag_CC then | |
5680 | w ("No overflow check required"); | |
5681 | end if; | |
5682 | ||
5683 | return; | |
5684 | end if; | |
9dfe12ae | 5685 | end if; |
5686 | end if; | |
5687 | ||
feff2f05 | 5688 | -- If not in optimizing mode, set flag and we are done. We are also done |
5689 | -- (and just set the flag) if the type is not a discrete type, since it | |
5690 | -- is not worth the effort to eliminate checks for other than discrete | |
5691 | -- types. In addition, we take this same path if we have stored the | |
5692 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5693 | -- but we do not want to blow up). |
9dfe12ae | 5694 | |
5695 | if Optimization_Level = 0 | |
5696 | or else not Is_Discrete_Type (Etype (N)) | |
5697 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5698 | then |
00c403ee | 5699 | Activate_Overflow_Check (N); |
9dfe12ae | 5700 | |
5701 | if Debug_Flag_CC then | |
5702 | w ("Optimization off"); | |
5703 | end if; | |
5704 | ||
ee6ba406 | 5705 | return; |
9dfe12ae | 5706 | end if; |
ee6ba406 | 5707 | |
9dfe12ae | 5708 | -- Otherwise evaluate and check the expression |
5709 | ||
5710 | Find_Check | |
5711 | (Expr => N, | |
5712 | Check_Type => 'O', | |
5713 | Target_Type => Empty, | |
5714 | Entry_OK => OK, | |
5715 | Check_Num => Chk, | |
5716 | Ent => Ent, | |
5717 | Ofs => Ofs); | |
5718 | ||
5719 | if Debug_Flag_CC then | |
5720 | w ("Called Find_Check"); | |
5721 | w (" OK = ", OK); | |
5722 | ||
5723 | if OK then | |
5724 | w (" Check_Num = ", Chk); | |
5725 | w (" Ent = ", Int (Ent)); | |
5726 | Write_Str (" Ofs = "); | |
5727 | pid (Ofs); | |
5728 | end if; | |
5729 | end if; | |
ee6ba406 | 5730 | |
9dfe12ae | 5731 | -- If check is not of form to optimize, then set flag and we are done |
5732 | ||
5733 | if not OK then | |
00c403ee | 5734 | Activate_Overflow_Check (N); |
ee6ba406 | 5735 | return; |
9dfe12ae | 5736 | end if; |
ee6ba406 | 5737 | |
9dfe12ae | 5738 | -- If check is already performed, then return without setting flag |
5739 | ||
5740 | if Chk /= 0 then | |
5741 | if Debug_Flag_CC then | |
5742 | w ("Check suppressed!"); | |
5743 | end if; | |
ee6ba406 | 5744 | |
ee6ba406 | 5745 | return; |
9dfe12ae | 5746 | end if; |
ee6ba406 | 5747 | |
9dfe12ae | 5748 | -- Here we will make a new entry for the new check |
5749 | ||
00c403ee | 5750 | Activate_Overflow_Check (N); |
9dfe12ae | 5751 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5752 | Saved_Checks (Num_Saved_Checks) := | |
5753 | (Killed => False, | |
5754 | Entity => Ent, | |
5755 | Offset => Ofs, | |
5756 | Check_Type => 'O', | |
5757 | Target_Type => Empty); | |
5758 | ||
5759 | if Debug_Flag_CC then | |
5760 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5761 | w (" Entity = ", Int (Ent)); | |
5762 | Write_Str (" Offset = "); | |
5763 | pid (Ofs); | |
5764 | w (" Check_Type = O"); | |
5765 | w (" Target_Type = Empty"); | |
5766 | end if; | |
ee6ba406 | 5767 | |
feff2f05 | 5768 | -- If we get an exception, then something went wrong, probably because of |
20cf157b | 5769 | -- an error in the structure of the tree due to an incorrect program. Or |
5770 | -- it may be a bug in the optimization circuit. In either case the safest | |
feff2f05 | 5771 | -- thing is simply to set the check flag unconditionally. |
9dfe12ae | 5772 | |
5773 | exception | |
5774 | when others => | |
00c403ee | 5775 | Activate_Overflow_Check (N); |
9dfe12ae | 5776 | |
5777 | if Debug_Flag_CC then | |
5778 | w (" exception occurred, overflow flag set"); | |
5779 | end if; | |
5780 | ||
5781 | return; | |
5782 | end Enable_Overflow_Check; | |
5783 | ||
5784 | ------------------------ | |
5785 | -- Enable_Range_Check -- | |
5786 | ------------------------ | |
5787 | ||
5788 | procedure Enable_Range_Check (N : Node_Id) is | |
5789 | Chk : Nat; | |
5790 | OK : Boolean; | |
5791 | Ent : Entity_Id; | |
5792 | Ofs : Uint; | |
5793 | Ttyp : Entity_Id; | |
5794 | P : Node_Id; | |
5795 | ||
5796 | begin | |
feff2f05 | 5797 | -- Return if unchecked type conversion with range check killed. In this |
39a0c1d3 | 5798 | -- case we never set the flag (that's what Kill_Range_Check is about). |
9dfe12ae | 5799 | |
5800 | if Nkind (N) = N_Unchecked_Type_Conversion | |
5801 | and then Kill_Range_Check (N) | |
ee6ba406 | 5802 | then |
5803 | return; | |
9dfe12ae | 5804 | end if; |
ee6ba406 | 5805 | |
55e8372b | 5806 | -- Do not set range check flag if parent is assignment statement or |
5807 | -- object declaration with Suppress_Assignment_Checks flag set | |
5808 | ||
5809 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
5810 | and then Suppress_Assignment_Checks (Parent (N)) | |
5811 | then | |
5812 | return; | |
5813 | end if; | |
5814 | ||
0577b0b1 | 5815 | -- Check for various cases where we should suppress the range check |
5816 | ||
5817 | -- No check if range checks suppressed for type of node | |
5818 | ||
20cf157b | 5819 | if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
0577b0b1 | 5820 | return; |
5821 | ||
5822 | -- No check if node is an entity name, and range checks are suppressed | |
5823 | -- for this entity, or for the type of this entity. | |
5824 | ||
5825 | elsif Is_Entity_Name (N) | |
5826 | and then (Range_Checks_Suppressed (Entity (N)) | |
20cf157b | 5827 | or else Range_Checks_Suppressed (Etype (Entity (N)))) |
0577b0b1 | 5828 | then |
5829 | return; | |
5830 | ||
5831 | -- No checks if index of array, and index checks are suppressed for | |
5832 | -- the array object or the type of the array. | |
5833 | ||
5834 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
5835 | declare | |
5836 | Pref : constant Node_Id := Prefix (Parent (N)); | |
5837 | begin | |
5838 | if Is_Entity_Name (Pref) | |
5839 | and then Index_Checks_Suppressed (Entity (Pref)) | |
5840 | then | |
5841 | return; | |
5842 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
5843 | return; | |
5844 | end if; | |
5845 | end; | |
5846 | end if; | |
5847 | ||
9dfe12ae | 5848 | -- Debug trace output |
ee6ba406 | 5849 | |
9dfe12ae | 5850 | if Debug_Flag_CC then |
5851 | w ("Enable_Range_Check for node ", Int (N)); | |
5852 | Write_Str (" Source location = "); | |
5853 | wl (Sloc (N)); | |
00c403ee | 5854 | pg (Union_Id (N)); |
9dfe12ae | 5855 | end if; |
5856 | ||
feff2f05 | 5857 | -- If not in optimizing mode, set flag and we are done. We are also done |
5858 | -- (and just set the flag) if the type is not a discrete type, since it | |
5859 | -- is not worth the effort to eliminate checks for other than discrete | |
5860 | -- types. In addition, we take this same path if we have stored the | |
5861 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5862 | -- but we do not want to blow up). |
9dfe12ae | 5863 | |
5864 | if Optimization_Level = 0 | |
5865 | or else No (Etype (N)) | |
5866 | or else not Is_Discrete_Type (Etype (N)) | |
5867 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5868 | then |
00c403ee | 5869 | Activate_Range_Check (N); |
9dfe12ae | 5870 | |
5871 | if Debug_Flag_CC then | |
5872 | w ("Optimization off"); | |
5873 | end if; | |
5874 | ||
ee6ba406 | 5875 | return; |
9dfe12ae | 5876 | end if; |
ee6ba406 | 5877 | |
9dfe12ae | 5878 | -- Otherwise find out the target type |
ee6ba406 | 5879 | |
9dfe12ae | 5880 | P := Parent (N); |
ee6ba406 | 5881 | |
9dfe12ae | 5882 | -- For assignment, use left side subtype |
5883 | ||
5884 | if Nkind (P) = N_Assignment_Statement | |
5885 | and then Expression (P) = N | |
5886 | then | |
5887 | Ttyp := Etype (Name (P)); | |
5888 | ||
5889 | -- For indexed component, use subscript subtype | |
5890 | ||
5891 | elsif Nkind (P) = N_Indexed_Component then | |
5892 | declare | |
5893 | Atyp : Entity_Id; | |
5894 | Indx : Node_Id; | |
5895 | Subs : Node_Id; | |
5896 | ||
5897 | begin | |
5898 | Atyp := Etype (Prefix (P)); | |
5899 | ||
5900 | if Is_Access_Type (Atyp) then | |
5901 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 5902 | |
5903 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 5904 | -- perform check unconditionally: it depends on the bounds of |
5905 | -- an object and we cannot currently recognize whether the test | |
5906 | -- may be redundant. | |
f07ea091 | 5907 | |
5908 | if not Is_Constrained (Atyp) then | |
00c403ee | 5909 | Activate_Range_Check (N); |
f07ea091 | 5910 | return; |
5911 | end if; | |
7189d17f | 5912 | |
31831d39 | 5913 | -- Ditto if prefix is simply an unconstrained array. We used |
5914 | -- to think this case was OK, if the prefix was not an explicit | |
5915 | -- dereference, but we have now seen a case where this is not | |
5916 | -- true, so it is safer to just suppress the optimization in this | |
5917 | -- case. The back end is getting better at eliminating redundant | |
5918 | -- checks in any case, so the loss won't be important. | |
7189d17f | 5919 | |
31831d39 | 5920 | elsif Is_Array_Type (Atyp) |
7189d17f | 5921 | and then not Is_Constrained (Atyp) |
5922 | then | |
00c403ee | 5923 | Activate_Range_Check (N); |
7189d17f | 5924 | return; |
9dfe12ae | 5925 | end if; |
5926 | ||
5927 | Indx := First_Index (Atyp); | |
5928 | Subs := First (Expressions (P)); | |
5929 | loop | |
5930 | if Subs = N then | |
5931 | Ttyp := Etype (Indx); | |
5932 | exit; | |
5933 | end if; | |
5934 | ||
5935 | Next_Index (Indx); | |
5936 | Next (Subs); | |
5937 | end loop; | |
5938 | end; | |
5939 | ||
5940 | -- For now, ignore all other cases, they are not so interesting | |
5941 | ||
5942 | else | |
5943 | if Debug_Flag_CC then | |
5944 | w (" target type not found, flag set"); | |
5945 | end if; | |
5946 | ||
00c403ee | 5947 | Activate_Range_Check (N); |
9dfe12ae | 5948 | return; |
5949 | end if; | |
5950 | ||
5951 | -- Evaluate and check the expression | |
5952 | ||
5953 | Find_Check | |
5954 | (Expr => N, | |
5955 | Check_Type => 'R', | |
5956 | Target_Type => Ttyp, | |
5957 | Entry_OK => OK, | |
5958 | Check_Num => Chk, | |
5959 | Ent => Ent, | |
5960 | Ofs => Ofs); | |
5961 | ||
5962 | if Debug_Flag_CC then | |
5963 | w ("Called Find_Check"); | |
5964 | w ("Target_Typ = ", Int (Ttyp)); | |
5965 | w (" OK = ", OK); | |
5966 | ||
5967 | if OK then | |
5968 | w (" Check_Num = ", Chk); | |
5969 | w (" Ent = ", Int (Ent)); | |
5970 | Write_Str (" Ofs = "); | |
5971 | pid (Ofs); | |
5972 | end if; | |
5973 | end if; | |
5974 | ||
5975 | -- If check is not of form to optimize, then set flag and we are done | |
5976 | ||
5977 | if not OK then | |
5978 | if Debug_Flag_CC then | |
5979 | w (" expression not of optimizable type, flag set"); | |
5980 | end if; | |
5981 | ||
00c403ee | 5982 | Activate_Range_Check (N); |
9dfe12ae | 5983 | return; |
5984 | end if; | |
5985 | ||
5986 | -- If check is already performed, then return without setting flag | |
5987 | ||
5988 | if Chk /= 0 then | |
5989 | if Debug_Flag_CC then | |
5990 | w ("Check suppressed!"); | |
5991 | end if; | |
5992 | ||
5993 | return; | |
5994 | end if; | |
5995 | ||
5996 | -- Here we will make a new entry for the new check | |
5997 | ||
00c403ee | 5998 | Activate_Range_Check (N); |
9dfe12ae | 5999 | Num_Saved_Checks := Num_Saved_Checks + 1; |
6000 | Saved_Checks (Num_Saved_Checks) := | |
6001 | (Killed => False, | |
6002 | Entity => Ent, | |
6003 | Offset => Ofs, | |
6004 | Check_Type => 'R', | |
6005 | Target_Type => Ttyp); | |
6006 | ||
6007 | if Debug_Flag_CC then | |
6008 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
6009 | w (" Entity = ", Int (Ent)); | |
6010 | Write_Str (" Offset = "); | |
6011 | pid (Ofs); | |
6012 | w (" Check_Type = R"); | |
6013 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 6014 | pg (Union_Id (Ttyp)); |
9dfe12ae | 6015 | end if; |
6016 | ||
feff2f05 | 6017 | -- If we get an exception, then something went wrong, probably because of |
6018 | -- an error in the structure of the tree due to an incorrect program. Or | |
6019 | -- it may be a bug in the optimization circuit. In either case the safest | |
6020 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 6021 | |
6022 | exception | |
6023 | when others => | |
00c403ee | 6024 | Activate_Range_Check (N); |
9dfe12ae | 6025 | |
6026 | if Debug_Flag_CC then | |
6027 | w (" exception occurred, range flag set"); | |
6028 | end if; | |
6029 | ||
6030 | return; | |
6031 | end Enable_Range_Check; | |
6032 | ||
6033 | ------------------ | |
6034 | -- Ensure_Valid -- | |
6035 | ------------------ | |
6036 | ||
aaec8d13 | 6037 | procedure Ensure_Valid |
6038 | (Expr : Node_Id; | |
6039 | Holes_OK : Boolean := False; | |
6040 | Related_Id : Entity_Id := Empty; | |
6041 | Is_Low_Bound : Boolean := False; | |
6042 | Is_High_Bound : Boolean := False) | |
6043 | is | |
9dfe12ae | 6044 | Typ : constant Entity_Id := Etype (Expr); |
6045 | ||
6046 | begin | |
6047 | -- Ignore call if we are not doing any validity checking | |
6048 | ||
6049 | if not Validity_Checks_On then | |
6050 | return; | |
6051 | ||
0577b0b1 | 6052 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 6053 | |
0577b0b1 | 6054 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 6055 | return; |
6056 | ||
feff2f05 | 6057 | -- No check required if expression is from the expander, we assume the |
6058 | -- expander will generate whatever checks are needed. Note that this is | |
39a0c1d3 | 6059 | -- not just an optimization, it avoids infinite recursions. |
9dfe12ae | 6060 | |
6061 | -- Unchecked conversions must be checked, unless they are initialized | |
6062 | -- scalar values, as in a component assignment in an init proc. | |
6063 | ||
6064 | -- In addition, we force a check if Force_Validity_Checks is set | |
6065 | ||
6066 | elsif not Comes_From_Source (Expr) | |
404eb7da | 6067 | and then not |
6068 | (Nkind (Expr) = N_Identifier | |
6069 | and then Present (Renamed_Object (Entity (Expr))) | |
6070 | and then Comes_From_Source (Renamed_Object (Entity (Expr)))) | |
9dfe12ae | 6071 | and then not Force_Validity_Checks |
6072 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
6073 | or else Kill_Range_Check (Expr)) | |
6074 | then | |
6075 | return; | |
6076 | ||
6077 | -- No check required if expression is known to have valid value | |
6078 | ||
6079 | elsif Expr_Known_Valid (Expr) then | |
6080 | return; | |
6081 | ||
ea1f4b87 | 6082 | -- No check needed within a generated predicate function. Validity |
6083 | -- of input value will have been checked earlier. | |
6084 | ||
6085 | elsif Ekind (Current_Scope) = E_Function | |
6086 | and then Is_Predicate_Function (Current_Scope) | |
6087 | then | |
6088 | return; | |
6089 | ||
feff2f05 | 6090 | -- Ignore case of enumeration with holes where the flag is set not to |
6091 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 6092 | |
6093 | elsif Is_Enumeration_Type (Typ) | |
6094 | and then Has_Non_Standard_Rep (Typ) | |
6095 | and then Holes_OK | |
6096 | then | |
6097 | return; | |
6098 | ||
f2a06be9 | 6099 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 6100 | |
6101 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
6102 | and then Expr = Name (Parent (Expr)) | |
6103 | then | |
6104 | return; | |
6105 | ||
6fb3c314 | 6106 | -- No check on a universal real constant. The context will eventually |
38f5559f | 6107 | -- convert it to a machine number for some target type, or report an |
6108 | -- illegality. | |
6109 | ||
6110 | elsif Nkind (Expr) = N_Real_Literal | |
6111 | and then Etype (Expr) = Universal_Real | |
6112 | then | |
6113 | return; | |
6114 | ||
6fb3c314 | 6115 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 6116 | -- no possible check applies. We ignore the old ACATS chestnuts that |
6117 | -- involve Boolean range True..True. | |
6118 | ||
6119 | -- Note: validity checks are generated for expressions that yield a | |
6120 | -- scalar type, when it is possible to create a value that is outside of | |
6121 | -- the type. If this is a one-bit boolean no such value exists. This is | |
6122 | -- an optimization, and it also prevents compiler blowing up during the | |
6123 | -- elaboration of improperly expanded packed array references. | |
6124 | ||
6125 | elsif Nkind (Expr) = N_Indexed_Component | |
6126 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
6127 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
6128 | then | |
6129 | return; | |
6130 | ||
737e8460 | 6131 | -- For an expression with actions, we want to insert the validity check |
6132 | -- on the final Expression. | |
6133 | ||
6134 | elsif Nkind (Expr) = N_Expression_With_Actions then | |
6135 | Ensure_Valid (Expression (Expr)); | |
6136 | return; | |
6137 | ||
9dfe12ae | 6138 | -- An annoying special case. If this is an out parameter of a scalar |
6139 | -- type, then the value is not going to be accessed, therefore it is | |
becb6111 | 6140 | -- inappropriate to do any validity check at the call site. Likewise |
6141 | -- if the parameter is passed by reference. | |
9dfe12ae | 6142 | |
6143 | else | |
6144 | -- Only need to worry about scalar types | |
6145 | ||
6146 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 6147 | declare |
6148 | P : Node_Id; | |
6149 | N : Node_Id; | |
6150 | E : Entity_Id; | |
6151 | F : Entity_Id; | |
6152 | A : Node_Id; | |
6153 | L : List_Id; | |
6154 | ||
6155 | begin | |
6156 | -- Find actual argument (which may be a parameter association) | |
6157 | -- and the parent of the actual argument (the call statement) | |
6158 | ||
6159 | N := Expr; | |
6160 | P := Parent (Expr); | |
6161 | ||
6162 | if Nkind (P) = N_Parameter_Association then | |
6163 | N := P; | |
6164 | P := Parent (N); | |
6165 | end if; | |
6166 | ||
becb6111 | 6167 | -- If this is an indirect or dispatching call, get signature |
6168 | -- from the subprogram type. | |
ee6ba406 | 6169 | |
becb6111 | 6170 | if Nkind_In (P, N_Entry_Call_Statement, |
6171 | N_Function_Call, | |
6172 | N_Procedure_Call_Statement) | |
6173 | then | |
6174 | E := Get_Called_Entity (P); | |
ee6ba406 | 6175 | L := Parameter_Associations (P); |
9dfe12ae | 6176 | |
feff2f05 | 6177 | -- Only need to worry if there are indeed actuals, and if |
becb6111 | 6178 | -- this could be a subprogram call, otherwise we cannot get |
6179 | -- a match (either we are not an argument, or the mode of | |
6180 | -- the formal is not OUT). This test also filters out the | |
feff2f05 | 6181 | -- generic case. |
ee6ba406 | 6182 | |
20cf157b | 6183 | if Is_Non_Empty_List (L) and then Is_Subprogram (E) then |
6184 | ||
feff2f05 | 6185 | -- This is the loop through parameters, looking for an |
6186 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 6187 | |
6188 | F := First_Formal (E); | |
6189 | A := First (L); | |
ee6ba406 | 6190 | while Present (F) loop |
becb6111 | 6191 | if A = N |
6192 | and then (Ekind (F) = E_Out_Parameter | |
6193 | or else Mechanism (F) = By_Reference) | |
6194 | then | |
ee6ba406 | 6195 | return; |
6196 | end if; | |
6197 | ||
6198 | Next_Formal (F); | |
6199 | Next (A); | |
6200 | end loop; | |
6201 | end if; | |
6202 | end if; | |
6203 | end; | |
6204 | end if; | |
6205 | end if; | |
6206 | ||
fa6a6949 | 6207 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 6208 | -- checking: if they are valid, a boolean or short-circuit operation |
6209 | -- with them will be valid as well. | |
784d4230 | 6210 | |
6211 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 6212 | and then |
fa6a6949 | 6213 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 6214 | then |
6215 | return; | |
6216 | end if; | |
6217 | ||
0577b0b1 | 6218 | -- If we fall through, a validity check is required |
ee6ba406 | 6219 | |
aaec8d13 | 6220 | Insert_Valid_Check (Expr, Related_Id, Is_Low_Bound, Is_High_Bound); |
ce7498d3 | 6221 | |
6222 | if Is_Entity_Name (Expr) | |
6223 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
6224 | then | |
6225 | Set_Is_Known_Valid (Entity (Expr)); | |
6226 | end if; | |
ee6ba406 | 6227 | end Ensure_Valid; |
6228 | ||
6229 | ---------------------- | |
6230 | -- Expr_Known_Valid -- | |
6231 | ---------------------- | |
6232 | ||
6233 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
6234 | Typ : constant Entity_Id := Etype (Expr); | |
6235 | ||
6236 | begin | |
feff2f05 | 6237 | -- Non-scalar types are always considered valid, since they never give |
6238 | -- rise to the issues of erroneous or bounded error behavior that are | |
6239 | -- the concern. In formal reference manual terms the notion of validity | |
6240 | -- only applies to scalar types. Note that even when packed arrays are | |
6241 | -- represented using modular types, they are still arrays semantically, | |
6242 | -- so they are also always valid (in particular, the unused bits can be | |
6243 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 6244 | |
a88a5773 | 6245 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then |
ee6ba406 | 6246 | return True; |
6247 | ||
6248 | -- If no validity checking, then everything is considered valid | |
6249 | ||
6250 | elsif not Validity_Checks_On then | |
6251 | return True; | |
6252 | ||
6253 | -- Floating-point types are considered valid unless floating-point | |
6254 | -- validity checks have been specifically turned on. | |
6255 | ||
6256 | elsif Is_Floating_Point_Type (Typ) | |
6257 | and then not Validity_Check_Floating_Point | |
6258 | then | |
6259 | return True; | |
6260 | ||
feff2f05 | 6261 | -- If the expression is the value of an object that is known to be |
6262 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 6263 | |
6264 | elsif Is_Entity_Name (Expr) | |
6265 | and then Is_Known_Valid (Entity (Expr)) | |
985fe5d6 | 6266 | |
6267 | -- Exclude volatile variables | |
6268 | ||
6269 | and then not Treat_As_Volatile (Entity (Expr)) | |
ee6ba406 | 6270 | then |
6271 | return True; | |
6272 | ||
0577b0b1 | 6273 | -- References to discriminants are always considered valid. The value |
6274 | -- of a discriminant gets checked when the object is built. Within the | |
6275 | -- record, we consider it valid, and it is important to do so, since | |
6276 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 6277 | -- reference discriminants out of scope. Discriminants of concurrent |
6278 | -- types are excluded for the same reason. | |
0577b0b1 | 6279 | |
6280 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 6281 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 6282 | then |
6283 | return True; | |
6284 | ||
feff2f05 | 6285 | -- If the type is one for which all values are known valid, then we are |
6286 | -- sure that the value is valid except in the slightly odd case where | |
6287 | -- the expression is a reference to a variable whose size has been | |
6288 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 6289 | |
6290 | elsif Is_Known_Valid (Typ) then | |
6291 | if Is_Entity_Name (Expr) | |
6292 | and then Ekind (Entity (Expr)) = E_Variable | |
6293 | and then Esize (Entity (Expr)) > Esize (Typ) | |
6294 | then | |
6295 | return False; | |
6296 | else | |
6297 | return True; | |
6298 | end if; | |
6299 | ||
6300 | -- Integer and character literals always have valid values, where | |
6301 | -- appropriate these will be range checked in any case. | |
6302 | ||
20cf157b | 6303 | elsif Nkind_In (Expr, N_Integer_Literal, N_Character_Literal) then |
ee6ba406 | 6304 | return True; |
91e47010 | 6305 | |
ee6ba406 | 6306 | -- If we have a type conversion or a qualification of a known valid |
6307 | -- value, then the result will always be valid. | |
6308 | ||
20cf157b | 6309 | elsif Nkind_In (Expr, N_Type_Conversion, N_Qualified_Expression) then |
ee6ba406 | 6310 | return Expr_Known_Valid (Expression (Expr)); |
6311 | ||
1eb1395f | 6312 | -- Case of expression is a non-floating-point operator. In this case we |
6313 | -- can assume the result is valid the generated code for the operator | |
6314 | -- will include whatever checks are needed (e.g. range checks) to ensure | |
6315 | -- validity. This assumption does not hold for the floating-point case, | |
6316 | -- since floating-point operators can generate Infinite or NaN results | |
6317 | -- which are considered invalid. | |
6318 | ||
6319 | -- Historical note: in older versions, the exemption of floating-point | |
6320 | -- types from this assumption was done only in cases where the parent | |
6321 | -- was an assignment, function call or parameter association. Presumably | |
6322 | -- the idea was that in other contexts, the result would be checked | |
6323 | -- elsewhere, but this list of cases was missing tests (at least the | |
6324 | -- N_Object_Declaration case, as shown by a reported missing validity | |
6325 | -- check), and it is not clear why function calls but not procedure | |
6326 | -- calls were tested for. It really seems more accurate and much | |
6327 | -- safer to recognize that expressions which are the result of a | |
6328 | -- floating-point operator can never be assumed to be valid. | |
6329 | ||
6330 | elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then | |
6331 | return True; | |
1d90d657 | 6332 | |
feff2f05 | 6333 | -- The result of a membership test is always valid, since it is true or |
6334 | -- false, there are no other possibilities. | |
0577b0b1 | 6335 | |
6336 | elsif Nkind (Expr) in N_Membership_Test then | |
6337 | return True; | |
6338 | ||
ee6ba406 | 6339 | -- For all other cases, we do not know the expression is valid |
6340 | ||
6341 | else | |
6342 | return False; | |
6343 | end if; | |
6344 | end Expr_Known_Valid; | |
6345 | ||
9dfe12ae | 6346 | ---------------- |
6347 | -- Find_Check -- | |
6348 | ---------------- | |
6349 | ||
6350 | procedure Find_Check | |
6351 | (Expr : Node_Id; | |
6352 | Check_Type : Character; | |
6353 | Target_Type : Entity_Id; | |
6354 | Entry_OK : out Boolean; | |
6355 | Check_Num : out Nat; | |
6356 | Ent : out Entity_Id; | |
6357 | Ofs : out Uint) | |
6358 | is | |
6359 | function Within_Range_Of | |
6360 | (Target_Type : Entity_Id; | |
314a23b6 | 6361 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 6362 | -- Given a requirement for checking a range against Target_Type, and |
6363 | -- and a range Check_Type against which a check has already been made, | |
6364 | -- determines if the check against check type is sufficient to ensure | |
6365 | -- that no check against Target_Type is required. | |
6366 | ||
6367 | --------------------- | |
6368 | -- Within_Range_Of -- | |
6369 | --------------------- | |
6370 | ||
6371 | function Within_Range_Of | |
6372 | (Target_Type : Entity_Id; | |
314a23b6 | 6373 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 6374 | is |
6375 | begin | |
6376 | if Target_Type = Check_Type then | |
6377 | return True; | |
6378 | ||
6379 | else | |
6380 | declare | |
6381 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
6382 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
6383 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
6384 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
6385 | ||
6386 | begin | |
6387 | if (Tlo = Clo | |
6388 | or else (Compile_Time_Known_Value (Tlo) | |
6389 | and then | |
6390 | Compile_Time_Known_Value (Clo) | |
6391 | and then | |
6392 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
6393 | and then | |
6394 | (Thi = Chi | |
6395 | or else (Compile_Time_Known_Value (Thi) | |
6396 | and then | |
6397 | Compile_Time_Known_Value (Chi) | |
6398 | and then | |
6399 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
6400 | then | |
6401 | return True; | |
6402 | else | |
6403 | return False; | |
6404 | end if; | |
6405 | end; | |
6406 | end if; | |
6407 | end Within_Range_Of; | |
6408 | ||
6409 | -- Start of processing for Find_Check | |
6410 | ||
6411 | begin | |
ed195555 | 6412 | -- Establish default, in case no entry is found |
9dfe12ae | 6413 | |
6414 | Check_Num := 0; | |
6415 | ||
6416 | -- Case of expression is simple entity reference | |
6417 | ||
6418 | if Is_Entity_Name (Expr) then | |
6419 | Ent := Entity (Expr); | |
6420 | Ofs := Uint_0; | |
6421 | ||
6422 | -- Case of expression is entity + known constant | |
6423 | ||
6424 | elsif Nkind (Expr) = N_Op_Add | |
6425 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
6426 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
6427 | then | |
6428 | Ent := Entity (Left_Opnd (Expr)); | |
6429 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
6430 | ||
6431 | -- Case of expression is entity - known constant | |
6432 | ||
6433 | elsif Nkind (Expr) = N_Op_Subtract | |
6434 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
6435 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
6436 | then | |
6437 | Ent := Entity (Left_Opnd (Expr)); | |
6438 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
6439 | ||
6440 | -- Any other expression is not of the right form | |
6441 | ||
6442 | else | |
6443 | Ent := Empty; | |
6444 | Ofs := Uint_0; | |
6445 | Entry_OK := False; | |
6446 | return; | |
6447 | end if; | |
6448 | ||
feff2f05 | 6449 | -- Come here with expression of appropriate form, check if entity is an |
6450 | -- appropriate one for our purposes. | |
9dfe12ae | 6451 | |
6452 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 6453 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 6454 | and then not Is_Library_Level_Entity (Ent) |
6455 | then | |
6456 | Entry_OK := True; | |
6457 | else | |
6458 | Entry_OK := False; | |
6459 | return; | |
6460 | end if; | |
6461 | ||
6462 | -- See if there is matching check already | |
6463 | ||
6464 | for J in reverse 1 .. Num_Saved_Checks loop | |
6465 | declare | |
6466 | SC : Saved_Check renames Saved_Checks (J); | |
9dfe12ae | 6467 | begin |
6468 | if SC.Killed = False | |
6469 | and then SC.Entity = Ent | |
6470 | and then SC.Offset = Ofs | |
6471 | and then SC.Check_Type = Check_Type | |
6472 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
6473 | then | |
6474 | Check_Num := J; | |
6475 | return; | |
6476 | end if; | |
6477 | end; | |
6478 | end loop; | |
6479 | ||
6480 | -- If we fall through entry was not found | |
6481 | ||
9dfe12ae | 6482 | return; |
6483 | end Find_Check; | |
6484 | ||
6485 | --------------------------------- | |
6486 | -- Generate_Discriminant_Check -- | |
6487 | --------------------------------- | |
6488 | ||
6489 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 6490 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 6491 | |
6492 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
6493 | Loc : constant Source_Ptr := Sloc (N); | |
6494 | Pref : constant Node_Id := Prefix (N); | |
6495 | Sel : constant Node_Id := Selector_Name (N); | |
6496 | ||
6497 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 6498 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 6499 | -- The original component to be checked |
6500 | ||
6501 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 6502 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 6503 | -- The discriminant checking function |
6504 | ||
6505 | Discr : Entity_Id; | |
6506 | -- One discriminant to be checked in the type | |
6507 | ||
6508 | Real_Discr : Entity_Id; | |
6509 | -- Actual discriminant in the call | |
6510 | ||
6511 | Pref_Type : Entity_Id; | |
6512 | -- Type of relevant prefix (ignoring private/access stuff) | |
6513 | ||
6514 | Args : List_Id; | |
6515 | -- List of arguments for function call | |
6516 | ||
6517 | Formal : Entity_Id; | |
feff2f05 | 6518 | -- Keep track of the formal corresponding to the actual we build for |
6519 | -- each discriminant, in order to be able to perform the necessary type | |
6520 | -- conversions. | |
9dfe12ae | 6521 | |
6522 | Scomp : Node_Id; | |
6523 | -- Selected component reference for checking function argument | |
6524 | ||
6525 | begin | |
6526 | Pref_Type := Etype (Pref); | |
6527 | ||
6528 | -- Force evaluation of the prefix, so that it does not get evaluated | |
6529 | -- twice (once for the check, once for the actual reference). Such a | |
20cf157b | 6530 | -- double evaluation is always a potential source of inefficiency, and |
6531 | -- is functionally incorrect in the volatile case, or when the prefix | |
f9906591 | 6532 | -- may have side effects. A nonvolatile entity or a component of a |
6533 | -- nonvolatile entity requires no evaluation. | |
9dfe12ae | 6534 | |
6535 | if Is_Entity_Name (Pref) then | |
6536 | if Treat_As_Volatile (Entity (Pref)) then | |
6537 | Force_Evaluation (Pref, Name_Req => True); | |
6538 | end if; | |
6539 | ||
6540 | elsif Treat_As_Volatile (Etype (Pref)) then | |
20cf157b | 6541 | Force_Evaluation (Pref, Name_Req => True); |
9dfe12ae | 6542 | |
6543 | elsif Nkind (Pref) = N_Selected_Component | |
6544 | and then Is_Entity_Name (Prefix (Pref)) | |
6545 | then | |
6546 | null; | |
6547 | ||
6548 | else | |
6549 | Force_Evaluation (Pref, Name_Req => True); | |
6550 | end if; | |
6551 | ||
6552 | -- For a tagged type, use the scope of the original component to | |
6553 | -- obtain the type, because ??? | |
6554 | ||
6555 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
6556 | Pref_Type := Scope (Orig_Comp); | |
6557 | ||
feff2f05 | 6558 | -- For an untagged derived type, use the discriminants of the parent |
6559 | -- which have been renamed in the derivation, possibly by a one-to-many | |
d1a2e31b | 6560 | -- discriminant constraint. For untagged type, initially get the Etype |
feff2f05 | 6561 | -- of the prefix |
9dfe12ae | 6562 | |
6563 | else | |
6564 | if Is_Derived_Type (Pref_Type) | |
6565 | and then Number_Discriminants (Pref_Type) /= | |
6566 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
6567 | then | |
6568 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
6569 | end if; | |
6570 | end if; | |
6571 | ||
6572 | -- We definitely should have a checking function, This routine should | |
6573 | -- not be called if no discriminant checking function is present. | |
6574 | ||
6575 | pragma Assert (Present (Discr_Fct)); | |
6576 | ||
6577 | -- Create the list of the actual parameters for the call. This list | |
6578 | -- is the list of the discriminant fields of the record expression to | |
6579 | -- be discriminant checked. | |
6580 | ||
6581 | Args := New_List; | |
6582 | Formal := First_Formal (Discr_Fct); | |
6583 | Discr := First_Discriminant (Pref_Type); | |
6584 | while Present (Discr) loop | |
6585 | ||
6586 | -- If we have a corresponding discriminant field, and a parent | |
6587 | -- subtype is present, then we want to use the corresponding | |
6588 | -- discriminant since this is the one with the useful value. | |
6589 | ||
6590 | if Present (Corresponding_Discriminant (Discr)) | |
6591 | and then Ekind (Pref_Type) = E_Record_Type | |
6592 | and then Present (Parent_Subtype (Pref_Type)) | |
6593 | then | |
6594 | Real_Discr := Corresponding_Discriminant (Discr); | |
6595 | else | |
6596 | Real_Discr := Discr; | |
6597 | end if; | |
6598 | ||
6599 | -- Construct the reference to the discriminant | |
6600 | ||
6601 | Scomp := | |
6602 | Make_Selected_Component (Loc, | |
6603 | Prefix => | |
6604 | Unchecked_Convert_To (Pref_Type, | |
6605 | Duplicate_Subexpr (Pref)), | |
6606 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
6607 | ||
6608 | -- Manually analyze and resolve this selected component. We really | |
6609 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 6610 | -- playing discriminal games etc with this reference. Then we append |
6611 | -- the argument to the list we are gathering. | |
9dfe12ae | 6612 | |
6613 | Set_Etype (Scomp, Etype (Real_Discr)); | |
6614 | Set_Analyzed (Scomp, True); | |
6615 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
6616 | ||
6617 | Next_Formal_With_Extras (Formal); | |
6618 | Next_Discriminant (Discr); | |
6619 | end loop; | |
6620 | ||
6621 | -- Now build and insert the call | |
6622 | ||
6623 | Insert_Action (N, | |
6624 | Make_Raise_Constraint_Error (Loc, | |
6625 | Condition => | |
6626 | Make_Function_Call (Loc, | |
20cf157b | 6627 | Name => New_Occurrence_Of (Discr_Fct, Loc), |
9dfe12ae | 6628 | Parameter_Associations => Args), |
6629 | Reason => CE_Discriminant_Check_Failed)); | |
6630 | end Generate_Discriminant_Check; | |
6631 | ||
5c99c290 | 6632 | --------------------------- |
6633 | -- Generate_Index_Checks -- | |
6634 | --------------------------- | |
9dfe12ae | 6635 | |
6636 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 6637 | |
6638 | function Entity_Of_Prefix return Entity_Id; | |
6639 | -- Returns the entity of the prefix of N (or Empty if not found) | |
6640 | ||
3f42e2a7 | 6641 | ---------------------- |
6642 | -- Entity_Of_Prefix -- | |
6643 | ---------------------- | |
6644 | ||
05f3e139 | 6645 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 6646 | P : Node_Id; |
6647 | ||
05f3e139 | 6648 | begin |
e5d38095 | 6649 | P := Prefix (N); |
05f3e139 | 6650 | while not Is_Entity_Name (P) loop |
6651 | if not Nkind_In (P, N_Selected_Component, | |
6652 | N_Indexed_Component) | |
6653 | then | |
6654 | return Empty; | |
6655 | end if; | |
6656 | ||
6657 | P := Prefix (P); | |
6658 | end loop; | |
6659 | ||
6660 | return Entity (P); | |
6661 | end Entity_Of_Prefix; | |
6662 | ||
6663 | -- Local variables | |
6664 | ||
6665 | Loc : constant Source_Ptr := Sloc (N); | |
6666 | A : constant Node_Id := Prefix (N); | |
6667 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
6668 | Sub : Node_Id; | |
9dfe12ae | 6669 | |
3f42e2a7 | 6670 | -- Start of processing for Generate_Index_Checks |
6671 | ||
9dfe12ae | 6672 | begin |
05f3e139 | 6673 | -- Ignore call if the prefix is not an array since we have a serious |
6674 | -- error in the sources. Ignore it also if index checks are suppressed | |
6675 | -- for array object or type. | |
0577b0b1 | 6676 | |
05f3e139 | 6677 | if not Is_Array_Type (Etype (A)) |
20cf157b | 6678 | or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 6679 | or else Index_Checks_Suppressed (Etype (A)) |
6680 | then | |
6681 | return; | |
df9fba45 | 6682 | |
6683 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
6684 | -- prefix. This case arises when analysis has determined that constructs | |
6685 | -- such as | |
6686 | ||
6687 | -- Prefix'Loop_Entry (Expr) | |
6688 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
6689 | ||
6690 | -- require rewriting for error detection purposes. A side effect of this | |
6691 | -- action is the generation of index checks that mention 'Loop_Entry. | |
6692 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
6693 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
6694 | ||
6695 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
6696 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
6697 | then | |
6698 | return; | |
0577b0b1 | 6699 | end if; |
6700 | ||
05f3e139 | 6701 | -- Generate a raise of constraint error with the appropriate reason and |
6702 | -- a condition of the form: | |
6703 | ||
3f42e2a7 | 6704 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 6705 | |
6706 | -- Note that the reason we generate the conversion to the base type here | |
6707 | -- is that we definitely want the range check to take place, even if it | |
6708 | -- looks like the subtype is OK. Optimization considerations that allow | |
6709 | -- us to omit the check have already been taken into account in the | |
6710 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 6711 | |
9dfe12ae | 6712 | Sub := First (Expressions (N)); |
05f3e139 | 6713 | |
6714 | -- Handle string literals | |
6715 | ||
6716 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 6717 | if Do_Range_Check (Sub) then |
6718 | Set_Do_Range_Check (Sub, False); | |
6719 | ||
05f3e139 | 6720 | -- For string literals we obtain the bounds of the string from the |
6721 | -- associated subtype. | |
9dfe12ae | 6722 | |
05f3e139 | 6723 | Insert_Action (N, |
094ed68e | 6724 | Make_Raise_Constraint_Error (Loc, |
6725 | Condition => | |
6726 | Make_Not_In (Loc, | |
6727 | Left_Opnd => | |
6728 | Convert_To (Base_Type (Etype (Sub)), | |
6729 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6730 | Right_Opnd => | |
6731 | Make_Attribute_Reference (Loc, | |
83c6c069 | 6732 | Prefix => New_Occurrence_Of (Etype (A), Loc), |
094ed68e | 6733 | Attribute_Name => Name_Range)), |
6734 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6735 | end if; |
9dfe12ae | 6736 | |
05f3e139 | 6737 | -- General case |
9dfe12ae | 6738 | |
05f3e139 | 6739 | else |
6740 | declare | |
6741 | A_Idx : Node_Id := Empty; | |
6742 | A_Range : Node_Id; | |
6743 | Ind : Nat; | |
6744 | Num : List_Id; | |
6745 | Range_N : Node_Id; | |
9dfe12ae | 6746 | |
05f3e139 | 6747 | begin |
6748 | A_Idx := First_Index (Etype (A)); | |
6749 | Ind := 1; | |
6750 | while Present (Sub) loop | |
6751 | if Do_Range_Check (Sub) then | |
6752 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 6753 | |
05f3e139 | 6754 | -- Force evaluation except for the case of a simple name of |
f9906591 | 6755 | -- a nonvolatile entity. |
9dfe12ae | 6756 | |
05f3e139 | 6757 | if not Is_Entity_Name (Sub) |
6758 | or else Treat_As_Volatile (Entity (Sub)) | |
6759 | then | |
6760 | Force_Evaluation (Sub); | |
6761 | end if; | |
9dfe12ae | 6762 | |
05f3e139 | 6763 | if Nkind (A_Idx) = N_Range then |
6764 | A_Range := A_Idx; | |
6765 | ||
6766 | elsif Nkind (A_Idx) = N_Identifier | |
6767 | or else Nkind (A_Idx) = N_Expanded_Name | |
6768 | then | |
6769 | A_Range := Scalar_Range (Entity (A_Idx)); | |
6770 | ||
6771 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
6772 | A_Range := Range_Expression (Constraint (A_Idx)); | |
6773 | end if; | |
6774 | ||
6775 | -- For array objects with constant bounds we can generate | |
6776 | -- the index check using the bounds of the type of the index | |
6777 | ||
6778 | if Present (A_Ent) | |
6779 | and then Ekind (A_Ent) = E_Variable | |
6780 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
6781 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
6782 | then | |
6783 | Range_N := | |
6784 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 6785 | Prefix => |
83c6c069 | 6786 | New_Occurrence_Of (Etype (A_Idx), Loc), |
05f3e139 | 6787 | Attribute_Name => Name_Range); |
6788 | ||
6789 | -- For arrays with non-constant bounds we cannot generate | |
6790 | -- the index check using the bounds of the type of the index | |
6791 | -- since it may reference discriminants of some enclosing | |
6792 | -- type. We obtain the bounds directly from the prefix | |
6793 | -- object. | |
6794 | ||
6795 | else | |
6796 | if Ind = 1 then | |
6797 | Num := No_List; | |
6798 | else | |
6799 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
6800 | end if; | |
6801 | ||
6802 | Range_N := | |
6803 | Make_Attribute_Reference (Loc, | |
6804 | Prefix => | |
6805 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
6806 | Attribute_Name => Name_Range, | |
6807 | Expressions => Num); | |
6808 | end if; | |
6809 | ||
6810 | Insert_Action (N, | |
094ed68e | 6811 | Make_Raise_Constraint_Error (Loc, |
6812 | Condition => | |
6813 | Make_Not_In (Loc, | |
6814 | Left_Opnd => | |
6815 | Convert_To (Base_Type (Etype (Sub)), | |
6816 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6817 | Right_Opnd => Range_N), | |
6818 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6819 | end if; |
6820 | ||
6821 | A_Idx := Next_Index (A_Idx); | |
6822 | Ind := Ind + 1; | |
6823 | Next (Sub); | |
6824 | end loop; | |
6825 | end; | |
6826 | end if; | |
9dfe12ae | 6827 | end Generate_Index_Checks; |
6828 | ||
6829 | -------------------------- | |
6830 | -- Generate_Range_Check -- | |
6831 | -------------------------- | |
6832 | ||
6833 | procedure Generate_Range_Check | |
6834 | (N : Node_Id; | |
6835 | Target_Type : Entity_Id; | |
6836 | Reason : RT_Exception_Code) | |
6837 | is | |
6838 | Loc : constant Source_Ptr := Sloc (N); | |
6839 | Source_Type : constant Entity_Id := Etype (N); | |
6840 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
6841 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
6842 | ||
1f5d83cf | 6843 | procedure Convert_And_Check_Range; |
6844 | -- Convert the conversion operand to the target base type and save in | |
6845 | -- a temporary. Then check the converted value against the range of the | |
6846 | -- target subtype. | |
6847 | ||
124f1911 | 6848 | ----------------------------- |
6849 | -- Convert_And_Check_Range -- | |
6850 | ----------------------------- | |
1f5d83cf | 6851 | |
124f1911 | 6852 | procedure Convert_And_Check_Range is |
cdfda0e3 | 6853 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
6854 | Conv_Node : Node_Id; | |
1f5d83cf | 6855 | |
124f1911 | 6856 | begin |
cdfda0e3 | 6857 | -- For enumeration types with non-standard representation this is a |
6858 | -- direct conversion from the enumeration type to the target integer | |
6859 | -- type, which is treated by the back end as a normal integer type | |
6860 | -- conversion, treating the enumeration type as an integer, which is | |
6861 | -- exactly what we want. We set Conversion_OK to make sure that the | |
6862 | -- analyzer does not complain about what otherwise might be an | |
6863 | -- illegal conversion. | |
6864 | ||
6865 | if Is_Enumeration_Type (Source_Base_Type) | |
6866 | and then Present (Enum_Pos_To_Rep (Source_Base_Type)) | |
6867 | and then Is_Integer_Type (Target_Base_Type) | |
6868 | then | |
6869 | Conv_Node := | |
96cb18c0 | 6870 | OK_Convert_To |
6871 | (Typ => Target_Base_Type, | |
6872 | Expr => Duplicate_Subexpr (N)); | |
cdfda0e3 | 6873 | |
6874 | -- Common case | |
6875 | ||
6876 | else | |
6877 | Conv_Node := | |
6878 | Make_Type_Conversion (Loc, | |
6879 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
6880 | Expression => Duplicate_Subexpr (N)); | |
6881 | end if; | |
6882 | ||
124f1911 | 6883 | -- We make a temporary to hold the value of the converted value |
6884 | -- (converted to the base type), and then do the test against this | |
6885 | -- temporary. The conversion itself is replaced by an occurrence of | |
6886 | -- Tnn and followed by the explicit range check. Note that checks | |
6887 | -- are suppressed for this code, since we don't want a recursive | |
1f5d83cf | 6888 | -- range check popping up. |
6889 | ||
124f1911 | 6890 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); |
6891 | -- [constraint_error when Tnn not in Target_Type] | |
6892 | ||
1f5d83cf | 6893 | Insert_Actions (N, New_List ( |
6894 | Make_Object_Declaration (Loc, | |
6895 | Defining_Identifier => Tnn, | |
6896 | Object_Definition => New_Occurrence_Of (Target_Base_Type, Loc), | |
6897 | Constant_Present => True, | |
cdfda0e3 | 6898 | Expression => Conv_Node), |
1f5d83cf | 6899 | |
6900 | Make_Raise_Constraint_Error (Loc, | |
6901 | Condition => | |
6902 | Make_Not_In (Loc, | |
6903 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6904 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
6905 | Reason => Reason)), | |
6906 | Suppress => All_Checks); | |
6907 | ||
6908 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6909 | ||
6910 | -- Set the type of N, because the declaration for Tnn might not | |
6911 | -- be analyzed yet, as is the case if N appears within a record | |
6912 | -- declaration, as a discriminant constraint or expression. | |
6913 | ||
6914 | Set_Etype (N, Target_Base_Type); | |
6915 | end Convert_And_Check_Range; | |
6916 | ||
6917 | -- Start of processing for Generate_Range_Check | |
6918 | ||
9dfe12ae | 6919 | begin |
feff2f05 | 6920 | -- First special case, if the source type is already within the range |
6921 | -- of the target type, then no check is needed (probably we should have | |
6922 | -- stopped Do_Range_Check from being set in the first place, but better | |
cda40848 | 6923 | -- late than never in preventing junk code and junk flag settings. |
9dfe12ae | 6924 | |
7a1dabb3 | 6925 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 6926 | |
6927 | -- We do NOT apply this if the source node is a literal, since in this | |
6928 | -- case the literal has already been labeled as having the subtype of | |
6929 | -- the target. | |
6930 | ||
9dfe12ae | 6931 | and then not |
b40670e1 | 6932 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 6933 | or else |
b40670e1 | 6934 | (Is_Entity_Name (N) |
6935 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
9dfe12ae | 6936 | then |
cda40848 | 6937 | Set_Do_Range_Check (N, False); |
9dfe12ae | 6938 | return; |
6939 | end if; | |
6940 | ||
cda40848 | 6941 | -- Here a check is needed. If the expander is not active, or if we are |
6942 | -- in GNATProve mode, then simply set the Do_Range_Check flag and we | |
6943 | -- are done. In both these cases, we just want to see the range check | |
6944 | -- flag set, we do not want to generate the explicit range check code. | |
6945 | ||
6946 | if GNATprove_Mode or else not Expander_Active then | |
6947 | Set_Do_Range_Check (N, True); | |
6948 | return; | |
6949 | end if; | |
6950 | ||
6951 | -- Here we will generate an explicit range check, so we don't want to | |
6952 | -- set the Do_Range check flag, since the range check is taken care of | |
6953 | -- by the code we will generate. | |
6954 | ||
6955 | Set_Do_Range_Check (N, False); | |
6956 | ||
6957 | -- Force evaluation of the node, so that it does not get evaluated twice | |
6958 | -- (once for the check, once for the actual reference). Such a double | |
6959 | -- evaluation is always a potential source of inefficiency, and is | |
6960 | -- functionally incorrect in the volatile case. | |
9dfe12ae | 6961 | |
f47b9548 | 6962 | -- We skip the evaluation of attribute references because, after these |
6963 | -- runtime checks are generated, the expander may need to rewrite this | |
6964 | -- node (for example, see Attribute_Max_Size_In_Storage_Elements in | |
6965 | -- Expand_N_Attribute_Reference). | |
6966 | ||
6967 | if Nkind (N) /= N_Attribute_Reference | |
6968 | and then (not Is_Entity_Name (N) | |
ca09bfdb | 6969 | or else Treat_As_Volatile (Entity (N))) |
f47b9548 | 6970 | then |
6971 | Force_Evaluation (N, Mode => Strict); | |
9dfe12ae | 6972 | end if; |
6973 | ||
feff2f05 | 6974 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
6975 | -- the same since in this case we can simply do a direct check of the | |
6976 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 6977 | |
6978 | -- [constraint_error when N not in Target_Type] | |
6979 | ||
6980 | -- Note: this is by far the most common case, for example all cases of | |
6981 | -- checks on the RHS of assignments are in this category, but not all | |
6982 | -- cases are like this. Notably conversions can involve two types. | |
6983 | ||
6984 | if Source_Base_Type = Target_Base_Type then | |
99ed4b4c | 6985 | |
6986 | -- Insert the explicit range check. Note that we suppress checks for | |
6987 | -- this code, since we don't want a recursive range check popping up. | |
6988 | ||
9dfe12ae | 6989 | Insert_Action (N, |
6990 | Make_Raise_Constraint_Error (Loc, | |
6991 | Condition => | |
6992 | Make_Not_In (Loc, | |
6993 | Left_Opnd => Duplicate_Subexpr (N), | |
6994 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
99ed4b4c | 6995 | Reason => Reason), |
6996 | Suppress => All_Checks); | |
9dfe12ae | 6997 | |
6998 | -- Next test for the case where the target type is within the bounds | |
6999 | -- of the base type of the source type, since in this case we can | |
7000 | -- simply convert these bounds to the base type of T to do the test. | |
7001 | ||
7002 | -- [constraint_error when N not in | |
7003 | -- Source_Base_Type (Target_Type'First) | |
7004 | -- .. | |
7005 | -- Source_Base_Type(Target_Type'Last))] | |
7006 | ||
f2a06be9 | 7007 | -- The conversions will always work and need no check |
9dfe12ae | 7008 | |
a9b57347 | 7009 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
7010 | -- of converting from an enumeration value to an integer type, such as | |
7011 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
7012 | -- (which used to be handled by gigi). This is OK, since the conversion | |
7013 | -- itself does not require a check. | |
7014 | ||
7a1dabb3 | 7015 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
99ed4b4c | 7016 | |
7017 | -- Insert the explicit range check. Note that we suppress checks for | |
7018 | -- this code, since we don't want a recursive range check popping up. | |
7019 | ||
1f5d83cf | 7020 | if Is_Discrete_Type (Source_Base_Type) |
7021 | and then | |
7022 | Is_Discrete_Type (Target_Base_Type) | |
7023 | then | |
7024 | Insert_Action (N, | |
7025 | Make_Raise_Constraint_Error (Loc, | |
7026 | Condition => | |
7027 | Make_Not_In (Loc, | |
7028 | Left_Opnd => Duplicate_Subexpr (N), | |
7029 | ||
7030 | Right_Opnd => | |
7031 | Make_Range (Loc, | |
7032 | Low_Bound => | |
7033 | Unchecked_Convert_To (Source_Base_Type, | |
7034 | Make_Attribute_Reference (Loc, | |
7035 | Prefix => | |
7036 | New_Occurrence_Of (Target_Type, Loc), | |
7037 | Attribute_Name => Name_First)), | |
7038 | ||
7039 | High_Bound => | |
7040 | Unchecked_Convert_To (Source_Base_Type, | |
7041 | Make_Attribute_Reference (Loc, | |
7042 | Prefix => | |
7043 | New_Occurrence_Of (Target_Type, Loc), | |
7044 | Attribute_Name => Name_Last)))), | |
7045 | Reason => Reason), | |
7046 | Suppress => All_Checks); | |
9dfe12ae | 7047 | |
1f5d83cf | 7048 | -- For conversions involving at least one type that is not discrete, |
7049 | -- first convert to target type and then generate the range check. | |
7050 | -- This avoids problems with values that are close to a bound of the | |
7051 | -- target type that would fail a range check when done in a larger | |
7052 | -- source type before converting but would pass if converted with | |
7053 | -- rounding and then checked (such as in float-to-float conversions). | |
7054 | ||
7055 | else | |
7056 | Convert_And_Check_Range; | |
7057 | end if; | |
9dfe12ae | 7058 | |
feff2f05 | 7059 | -- Note that at this stage we now that the Target_Base_Type is not in |
7060 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
7061 | -- is not in this range). It could still be the case that Source_Type is | |
7062 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 7063 | |
feff2f05 | 7064 | -- If that is the case, we can freely convert the source to the target, |
7065 | -- and then test the target result against the bounds. | |
9dfe12ae | 7066 | |
7a1dabb3 | 7067 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
1f5d83cf | 7068 | Convert_And_Check_Range; |
9dfe12ae | 7069 | |
7070 | -- At this stage, we know that we have two scalar types, which are | |
7071 | -- directly convertible, and where neither scalar type has a base | |
7072 | -- range that is in the range of the other scalar type. | |
7073 | ||
7074 | -- The only way this can happen is with a signed and unsigned type. | |
7075 | -- So test for these two cases: | |
7076 | ||
7077 | else | |
7078 | -- Case of the source is unsigned and the target is signed | |
7079 | ||
7080 | if Is_Unsigned_Type (Source_Base_Type) | |
7081 | and then not Is_Unsigned_Type (Target_Base_Type) | |
7082 | then | |
7083 | -- If the source is unsigned and the target is signed, then we | |
7084 | -- know that the source is not shorter than the target (otherwise | |
7085 | -- the source base type would be in the target base type range). | |
7086 | ||
feff2f05 | 7087 | -- In other words, the unsigned type is either the same size as |
7088 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 7089 | |
7090 | pragma Assert | |
7091 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
7092 | ||
7093 | -- We only need to check the low bound if the low bound of the | |
7094 | -- target type is non-negative. If the low bound of the target | |
7095 | -- type is negative, then we know that we will fit fine. | |
7096 | ||
7097 | -- If the high bound of the target type is negative, then we | |
7098 | -- know we have a constraint error, since we can't possibly | |
7099 | -- have a negative source. | |
7100 | ||
7101 | -- With these two checks out of the way, we can do the check | |
7102 | -- using the source type safely | |
7103 | ||
39a0c1d3 | 7104 | -- This is definitely the most annoying case. |
9dfe12ae | 7105 | |
7106 | -- [constraint_error | |
7107 | -- when (Target_Type'First >= 0 | |
7108 | -- and then | |
7109 | -- N < Source_Base_Type (Target_Type'First)) | |
7110 | -- or else Target_Type'Last < 0 | |
7111 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
7112 | ||
7113 | -- We turn off all checks since we know that the conversions | |
7114 | -- will work fine, given the guards for negative values. | |
7115 | ||
7116 | Insert_Action (N, | |
7117 | Make_Raise_Constraint_Error (Loc, | |
7118 | Condition => | |
7119 | Make_Or_Else (Loc, | |
7120 | Make_Or_Else (Loc, | |
7121 | Left_Opnd => | |
7122 | Make_And_Then (Loc, | |
7123 | Left_Opnd => Make_Op_Ge (Loc, | |
7124 | Left_Opnd => | |
7125 | Make_Attribute_Reference (Loc, | |
7126 | Prefix => | |
7127 | New_Occurrence_Of (Target_Type, Loc), | |
7128 | Attribute_Name => Name_First), | |
7129 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7130 | ||
7131 | Right_Opnd => | |
7132 | Make_Op_Lt (Loc, | |
7133 | Left_Opnd => Duplicate_Subexpr (N), | |
7134 | Right_Opnd => | |
7135 | Convert_To (Source_Base_Type, | |
7136 | Make_Attribute_Reference (Loc, | |
7137 | Prefix => | |
7138 | New_Occurrence_Of (Target_Type, Loc), | |
7139 | Attribute_Name => Name_First)))), | |
7140 | ||
7141 | Right_Opnd => | |
7142 | Make_Op_Lt (Loc, | |
7143 | Left_Opnd => | |
7144 | Make_Attribute_Reference (Loc, | |
7145 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7146 | Attribute_Name => Name_Last), | |
7147 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
7148 | ||
7149 | Right_Opnd => | |
7150 | Make_Op_Gt (Loc, | |
7151 | Left_Opnd => Duplicate_Subexpr (N), | |
7152 | Right_Opnd => | |
7153 | Convert_To (Source_Base_Type, | |
7154 | Make_Attribute_Reference (Loc, | |
7155 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7156 | Attribute_Name => Name_Last)))), | |
7157 | ||
7158 | Reason => Reason), | |
7159 | Suppress => All_Checks); | |
7160 | ||
7161 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 7162 | -- the target is unsigned. |
9dfe12ae | 7163 | |
7164 | else | |
7165 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
20cf157b | 7166 | and then Is_Unsigned_Type (Target_Base_Type)); |
9dfe12ae | 7167 | |
feff2f05 | 7168 | -- If the source is signed and the target is unsigned, then we |
7169 | -- know that the target is not shorter than the source (otherwise | |
7170 | -- the target base type would be in the source base type range). | |
9dfe12ae | 7171 | |
feff2f05 | 7172 | -- In other words, the unsigned type is either the same size as |
7173 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 7174 | |
feff2f05 | 7175 | -- Clearly we have an error if the source value is negative since |
7176 | -- no unsigned type can have negative values. If the source type | |
7177 | -- is non-negative, then the check can be done using the target | |
7178 | -- type. | |
9dfe12ae | 7179 | |
7180 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
7181 | ||
7182 | -- [constraint_error | |
7183 | -- when N < 0 or else Tnn not in Target_Type]; | |
7184 | ||
feff2f05 | 7185 | -- We turn off all checks for the conversion of N to the target |
7186 | -- base type, since we generate the explicit check to ensure that | |
7187 | -- the value is non-negative | |
9dfe12ae | 7188 | |
7189 | declare | |
46eb6933 | 7190 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 7191 | |
7192 | begin | |
7193 | Insert_Actions (N, New_List ( | |
7194 | Make_Object_Declaration (Loc, | |
7195 | Defining_Identifier => Tnn, | |
7196 | Object_Definition => | |
7197 | New_Occurrence_Of (Target_Base_Type, Loc), | |
7198 | Constant_Present => True, | |
7199 | Expression => | |
a9b57347 | 7200 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 7201 | Subtype_Mark => |
7202 | New_Occurrence_Of (Target_Base_Type, Loc), | |
7203 | Expression => Duplicate_Subexpr (N))), | |
7204 | ||
7205 | Make_Raise_Constraint_Error (Loc, | |
7206 | Condition => | |
7207 | Make_Or_Else (Loc, | |
7208 | Left_Opnd => | |
7209 | Make_Op_Lt (Loc, | |
7210 | Left_Opnd => Duplicate_Subexpr (N), | |
7211 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7212 | ||
7213 | Right_Opnd => | |
7214 | Make_Not_In (Loc, | |
7215 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7216 | Right_Opnd => | |
7217 | New_Occurrence_Of (Target_Type, Loc))), | |
7218 | ||
20cf157b | 7219 | Reason => Reason)), |
9dfe12ae | 7220 | Suppress => All_Checks); |
7221 | ||
feff2f05 | 7222 | -- Set the Etype explicitly, because Insert_Actions may have |
7223 | -- placed the declaration in the freeze list for an enclosing | |
7224 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 7225 | |
7226 | Set_Etype (Tnn, Target_Base_Type); | |
7227 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
7228 | end; | |
7229 | end if; | |
7230 | end if; | |
7231 | end Generate_Range_Check; | |
7232 | ||
2af58f67 | 7233 | ------------------ |
7234 | -- Get_Check_Id -- | |
7235 | ------------------ | |
7236 | ||
7237 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
7238 | begin | |
7239 | -- For standard check name, we can do a direct computation | |
7240 | ||
7241 | if N in First_Check_Name .. Last_Check_Name then | |
7242 | return Check_Id (N - (First_Check_Name - 1)); | |
7243 | ||
7244 | -- For non-standard names added by pragma Check_Name, search table | |
7245 | ||
7246 | else | |
7247 | for J in All_Checks + 1 .. Check_Names.Last loop | |
7248 | if Check_Names.Table (J) = N then | |
7249 | return J; | |
7250 | end if; | |
7251 | end loop; | |
7252 | end if; | |
7253 | ||
7254 | -- No matching name found | |
7255 | ||
7256 | return No_Check_Id; | |
7257 | end Get_Check_Id; | |
7258 | ||
ee6ba406 | 7259 | --------------------- |
7260 | -- Get_Discriminal -- | |
7261 | --------------------- | |
7262 | ||
7263 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
7264 | Loc : constant Source_Ptr := Sloc (E); | |
7265 | D : Entity_Id; | |
7266 | Sc : Entity_Id; | |
7267 | ||
7268 | begin | |
0577b0b1 | 7269 | -- The bound can be a bona fide parameter of a protected operation, |
7270 | -- rather than a prival encoded as an in-parameter. | |
7271 | ||
7272 | if No (Discriminal_Link (Entity (Bound))) then | |
7273 | return Bound; | |
7274 | end if; | |
7275 | ||
2af58f67 | 7276 | -- Climb the scope stack looking for an enclosing protected type. If |
7277 | -- we run out of scopes, return the bound itself. | |
7278 | ||
7279 | Sc := Scope (E); | |
7280 | while Present (Sc) loop | |
7281 | if Sc = Standard_Standard then | |
7282 | return Bound; | |
2af58f67 | 7283 | elsif Ekind (Sc) = E_Protected_Type then |
7284 | exit; | |
7285 | end if; | |
7286 | ||
7287 | Sc := Scope (Sc); | |
7288 | end loop; | |
7289 | ||
ee6ba406 | 7290 | D := First_Discriminant (Sc); |
2af58f67 | 7291 | while Present (D) loop |
7292 | if Chars (D) = Chars (Bound) then | |
7293 | return New_Occurrence_Of (Discriminal (D), Loc); | |
7294 | end if; | |
ee6ba406 | 7295 | |
ee6ba406 | 7296 | Next_Discriminant (D); |
7297 | end loop; | |
7298 | ||
2af58f67 | 7299 | return Bound; |
ee6ba406 | 7300 | end Get_Discriminal; |
7301 | ||
2af58f67 | 7302 | ---------------------- |
7303 | -- Get_Range_Checks -- | |
7304 | ---------------------- | |
7305 | ||
7306 | function Get_Range_Checks | |
7307 | (Ck_Node : Node_Id; | |
7308 | Target_Typ : Entity_Id; | |
7309 | Source_Typ : Entity_Id := Empty; | |
7310 | Warn_Node : Node_Id := Empty) return Check_Result | |
7311 | is | |
7312 | begin | |
20cf157b | 7313 | return |
7314 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
2af58f67 | 7315 | end Get_Range_Checks; |
7316 | ||
ee6ba406 | 7317 | ------------------ |
7318 | -- Guard_Access -- | |
7319 | ------------------ | |
7320 | ||
7321 | function Guard_Access | |
7322 | (Cond : Node_Id; | |
7323 | Loc : Source_Ptr; | |
314a23b6 | 7324 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 7325 | is |
7326 | begin | |
7327 | if Nkind (Cond) = N_Or_Else then | |
7328 | Set_Paren_Count (Cond, 1); | |
7329 | end if; | |
7330 | ||
7331 | if Nkind (Ck_Node) = N_Allocator then | |
7332 | return Cond; | |
20cf157b | 7333 | |
ee6ba406 | 7334 | else |
7335 | return | |
7336 | Make_And_Then (Loc, | |
7337 | Left_Opnd => | |
7338 | Make_Op_Ne (Loc, | |
9dfe12ae | 7339 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 7340 | Right_Opnd => Make_Null (Loc)), |
7341 | Right_Opnd => Cond); | |
7342 | end if; | |
7343 | end Guard_Access; | |
7344 | ||
7345 | ----------------------------- | |
7346 | -- Index_Checks_Suppressed -- | |
7347 | ----------------------------- | |
7348 | ||
7349 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7350 | begin | |
9dfe12ae | 7351 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7352 | return Is_Check_Suppressed (E, Index_Check); | |
7353 | else | |
fafc6b97 | 7354 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 7355 | end if; |
ee6ba406 | 7356 | end Index_Checks_Suppressed; |
7357 | ||
7358 | ---------------- | |
7359 | -- Initialize -- | |
7360 | ---------------- | |
7361 | ||
7362 | procedure Initialize is | |
7363 | begin | |
7364 | for J in Determine_Range_Cache_N'Range loop | |
7365 | Determine_Range_Cache_N (J) := Empty; | |
7366 | end loop; | |
2af58f67 | 7367 | |
7368 | Check_Names.Init; | |
7369 | ||
7370 | for J in Int range 1 .. All_Checks loop | |
7371 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
7372 | end loop; | |
ee6ba406 | 7373 | end Initialize; |
7374 | ||
7375 | ------------------------- | |
7376 | -- Insert_Range_Checks -- | |
7377 | ------------------------- | |
7378 | ||
7379 | procedure Insert_Range_Checks | |
7380 | (Checks : Check_Result; | |
7381 | Node : Node_Id; | |
7382 | Suppress_Typ : Entity_Id; | |
7383 | Static_Sloc : Source_Ptr := No_Location; | |
7384 | Flag_Node : Node_Id := Empty; | |
7385 | Do_Before : Boolean := False) | |
7386 | is | |
2b4f2458 | 7387 | Checks_On : constant Boolean := |
7388 | not Index_Checks_Suppressed (Suppress_Typ) | |
7389 | or else | |
7390 | not Range_Checks_Suppressed (Suppress_Typ); | |
7391 | ||
7392 | Check_Node : Node_Id; | |
ee6ba406 | 7393 | Internal_Flag_Node : Node_Id := Flag_Node; |
7394 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
7395 | ||
ee6ba406 | 7396 | begin |
feff2f05 | 7397 | -- For now we just return if Checks_On is false, however this should be |
7398 | -- enhanced to check for an always True value in the condition and to | |
7399 | -- generate a compilation warning??? | |
ee6ba406 | 7400 | |
ac9184ed | 7401 | if not Expander_Active or not Checks_On then |
ee6ba406 | 7402 | return; |
7403 | end if; | |
7404 | ||
7405 | if Static_Sloc = No_Location then | |
7406 | Internal_Static_Sloc := Sloc (Node); | |
7407 | end if; | |
7408 | ||
7409 | if No (Flag_Node) then | |
7410 | Internal_Flag_Node := Node; | |
7411 | end if; | |
7412 | ||
7413 | for J in 1 .. 2 loop | |
7414 | exit when No (Checks (J)); | |
7415 | ||
7416 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
7417 | and then Present (Condition (Checks (J))) | |
7418 | then | |
7419 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
7420 | Check_Node := Checks (J); | |
7421 | Mark_Rewrite_Insertion (Check_Node); | |
7422 | ||
7423 | if Do_Before then | |
7424 | Insert_Before_And_Analyze (Node, Check_Node); | |
7425 | else | |
7426 | Insert_After_And_Analyze (Node, Check_Node); | |
7427 | end if; | |
7428 | ||
7429 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
7430 | end if; | |
7431 | ||
7432 | else | |
7433 | Check_Node := | |
f15731c4 | 7434 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
7435 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 7436 | Mark_Rewrite_Insertion (Check_Node); |
7437 | ||
7438 | if Do_Before then | |
7439 | Insert_Before_And_Analyze (Node, Check_Node); | |
7440 | else | |
7441 | Insert_After_And_Analyze (Node, Check_Node); | |
7442 | end if; | |
7443 | end if; | |
7444 | end loop; | |
7445 | end Insert_Range_Checks; | |
7446 | ||
7447 | ------------------------ | |
7448 | -- Insert_Valid_Check -- | |
7449 | ------------------------ | |
7450 | ||
aaec8d13 | 7451 | procedure Insert_Valid_Check |
7452 | (Expr : Node_Id; | |
7453 | Related_Id : Entity_Id := Empty; | |
7454 | Is_Low_Bound : Boolean := False; | |
7455 | Is_High_Bound : Boolean := False) | |
7456 | is | |
ee6ba406 | 7457 | Loc : constant Source_Ptr := Sloc (Expr); |
70580828 | 7458 | Typ : constant Entity_Id := Etype (Expr); |
8b718dab | 7459 | Exp : Node_Id; |
ee6ba406 | 7460 | |
7461 | begin | |
aaec8d13 | 7462 | -- Do not insert if checks off, or if not checking validity or if |
7463 | -- expression is known to be valid. | |
ee6ba406 | 7464 | |
0577b0b1 | 7465 | if not Validity_Checks_On |
7466 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 7467 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 7468 | then |
8b718dab | 7469 | return; |
ee6ba406 | 7470 | |
42c57d55 | 7471 | -- Do not insert checks within a predicate function. This will arise |
7472 | -- if the current unit and the predicate function are being compiled | |
7473 | -- with validity checks enabled. | |
70580828 | 7474 | |
89e864b9 | 7475 | elsif Present (Predicate_Function (Typ)) |
70580828 | 7476 | and then Current_Scope = Predicate_Function (Typ) |
7477 | then | |
7478 | return; | |
70580828 | 7479 | |
310c1cde | 7480 | -- If the expression is a packed component of a modular type of the |
7481 | -- right size, the data is always valid. | |
7482 | ||
89e864b9 | 7483 | elsif Nkind (Expr) = N_Selected_Component |
310c1cde | 7484 | and then Present (Component_Clause (Entity (Selector_Name (Expr)))) |
7485 | and then Is_Modular_Integer_Type (Typ) | |
7486 | and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) | |
7487 | then | |
7488 | return; | |
89e864b9 | 7489 | |
7490 | -- Do not generate a validity check when inside a generic unit as this | |
7491 | -- is an expansion activity. | |
7492 | ||
7493 | elsif Inside_A_Generic then | |
7494 | return; | |
310c1cde | 7495 | end if; |
7496 | ||
7a42b778 | 7497 | -- Entities declared in Lock_free protected types must be treated as |
7498 | -- volatile, and we must inhibit validity checks to prevent improper | |
7499 | -- constant folding. | |
866fa2d0 | 7500 | |
7501 | if Is_Entity_Name (Expr) | |
7502 | and then Is_Subprogram (Scope (Entity (Expr))) | |
7503 | and then Present (Protected_Subprogram (Scope (Entity (Expr)))) | |
7504 | and then Uses_Lock_Free | |
7a42b778 | 7505 | (Scope (Protected_Subprogram (Scope (Entity (Expr))))) |
866fa2d0 | 7506 | then |
7507 | return; | |
7508 | end if; | |
7509 | ||
8b718dab | 7510 | -- If we have a checked conversion, then validity check applies to |
7511 | -- the expression inside the conversion, not the result, since if | |
7512 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 7513 | |
8b718dab | 7514 | Exp := Expr; |
7515 | while Nkind (Exp) = N_Type_Conversion loop | |
7516 | Exp := Expression (Exp); | |
7517 | end loop; | |
7518 | ||
fdb8488b | 7519 | -- Do not generate a check for a variable which already validates the |
7520 | -- value of an assignable object. | |
7521 | ||
7522 | if Is_Validation_Variable_Reference (Exp) then | |
7523 | return; | |
7524 | end if; | |
7525 | ||
0577b0b1 | 7526 | declare |
49adf385 | 7527 | CE : Node_Id; |
49adf385 | 7528 | PV : Node_Id; |
7529 | Var_Id : Entity_Id; | |
05fcfafb | 7530 | |
0577b0b1 | 7531 | begin |
fdb8488b | 7532 | -- If the expression denotes an assignable object, capture its value |
7533 | -- in a variable and replace the original expression by the variable. | |
7534 | -- This approach has several effects: | |
06ad5813 | 7535 | |
fdb8488b | 7536 | -- 1) The evaluation of the object results in only one read in the |
7537 | -- case where the object is atomic or volatile. | |
fa771c05 | 7538 | |
49adf385 | 7539 | -- Var ... := Object; -- read |
fa771c05 | 7540 | |
fdb8488b | 7541 | -- 2) The captured value is the one verified by attribute 'Valid. |
7542 | -- As a result the object is not evaluated again, which would | |
7543 | -- result in an unwanted read in the case where the object is | |
7544 | -- atomic or volatile. | |
7545 | ||
49adf385 | 7546 | -- if not Var'Valid then -- OK, no read of Object |
fdb8488b | 7547 | |
7548 | -- if not Object'Valid then -- Wrong, extra read of Object | |
7549 | ||
7550 | -- 3) The captured value replaces the original object reference. | |
7551 | -- As a result the object is not evaluated again, in the same | |
7552 | -- vein as 2). | |
7553 | ||
49adf385 | 7554 | -- ... Var ... -- OK, no read of Object |
fdb8488b | 7555 | |
7556 | -- ... Object ... -- Wrong, extra read of Object | |
06ad5813 | 7557 | |
fdb8488b | 7558 | -- 4) The use of a variable to capture the value of the object |
7559 | -- allows the propagation of any changes back to the original | |
7560 | -- object. | |
7561 | ||
7562 | -- procedure Call (Val : in out ...); | |
7563 | ||
49adf385 | 7564 | -- Var : ... := Object; -- read Object |
7565 | -- if not Var'Valid then -- validity check | |
7566 | -- Call (Var); -- modify Var | |
7567 | -- Object := Var; -- update Object | |
fdb8488b | 7568 | |
7569 | if Is_Variable (Exp) then | |
49adf385 | 7570 | Var_Id := Make_Temporary (Loc, 'T', Exp); |
fdb8488b | 7571 | |
4cb8adff | 7572 | -- Because we could be dealing with a transient scope which would |
7573 | -- cause our object declaration to remain unanalyzed we must do | |
7574 | -- some manual decoration. | |
7575 | ||
7576 | Set_Ekind (Var_Id, E_Variable); | |
7577 | Set_Etype (Var_Id, Typ); | |
7578 | ||
fdb8488b | 7579 | Insert_Action (Exp, |
7580 | Make_Object_Declaration (Loc, | |
49adf385 | 7581 | Defining_Identifier => Var_Id, |
fdb8488b | 7582 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
bf5f89dc | 7583 | Expression => New_Copy_Tree (Exp)), |
7584 | Suppress => Validity_Check); | |
7585 | ||
7586 | Set_Validated_Object (Var_Id, New_Copy_Tree (Exp)); | |
49adf385 | 7587 | Rewrite (Exp, New_Occurrence_Of (Var_Id, Loc)); |
7588 | PV := New_Occurrence_Of (Var_Id, Loc); | |
fdb8488b | 7589 | |
02585eb0 | 7590 | -- Copy the Do_Range_Check flag over to the new Exp, so it doesn't |
7591 | -- get lost. Floating point types are handled elsewhere. | |
7592 | ||
7593 | if not Is_Floating_Point_Type (Typ) then | |
7594 | Set_Do_Range_Check (Exp, Do_Range_Check (Original_Node (Exp))); | |
7595 | end if; | |
7596 | ||
fdb8488b | 7597 | -- Otherwise the expression does not denote a variable. Force its |
7598 | -- evaluation by capturing its value in a constant. Generate: | |
7599 | ||
7600 | -- Temp : constant ... := Exp; | |
7601 | ||
7602 | else | |
7603 | Force_Evaluation | |
7604 | (Exp => Exp, | |
7605 | Related_Id => Related_Id, | |
7606 | Is_Low_Bound => Is_Low_Bound, | |
7607 | Is_High_Bound => Is_High_Bound); | |
7608 | ||
7609 | PV := New_Copy_Tree (Exp); | |
7610 | end if; | |
0577b0b1 | 7611 | |
443bdccb | 7612 | -- A rather specialized test. If PV is an analyzed expression which |
7613 | -- is an indexed component of a packed array that has not been | |
7614 | -- properly expanded, turn off its Analyzed flag to make sure it | |
5f46de53 | 7615 | -- gets properly reexpanded. If the prefix is an access value, |
7616 | -- the dereference will be added later. | |
23abd64d | 7617 | |
7618 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
7619 | -- an analyze with the old parent pointer. This may point e.g. to | |
7620 | -- a subprogram call, which deactivates this expansion. | |
7621 | ||
7622 | if Analyzed (PV) | |
7623 | and then Nkind (PV) = N_Indexed_Component | |
5f46de53 | 7624 | and then Is_Array_Type (Etype (Prefix (PV))) |
a88a5773 | 7625 | and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV)))) |
23abd64d | 7626 | then |
7627 | Set_Analyzed (PV, False); | |
7628 | end if; | |
7629 | ||
fa771c05 | 7630 | -- Build the raise CE node to check for validity. We build a type |
7631 | -- qualification for the prefix, since it may not be of the form of | |
7632 | -- a name, and we don't care in this context! | |
23abd64d | 7633 | |
7634 | CE := | |
aaec8d13 | 7635 | Make_Raise_Constraint_Error (Loc, |
7636 | Condition => | |
7637 | Make_Op_Not (Loc, | |
7638 | Right_Opnd => | |
7639 | Make_Attribute_Reference (Loc, | |
7640 | Prefix => PV, | |
7641 | Attribute_Name => Name_Valid)), | |
7642 | Reason => CE_Invalid_Data); | |
23abd64d | 7643 | |
7644 | -- Insert the validity check. Note that we do this with validity | |
7645 | -- checks turned off, to avoid recursion, we do not want validity | |
39a0c1d3 | 7646 | -- checks on the validity checking code itself. |
23abd64d | 7647 | |
7648 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 7649 | |
6fb3c314 | 7650 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 7651 | -- array, then it is rewritten as a renaming declaration. If the |
7652 | -- expression is an actual in a call, it has not been expanded, | |
7653 | -- waiting for the proper point at which to do it. The same happens | |
7654 | -- with renamings, so that we have to force the expansion now. This | |
7655 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
7656 | -- and exp_ch6.adb. | |
7657 | ||
7658 | if Is_Entity_Name (Exp) | |
7659 | and then Nkind (Parent (Entity (Exp))) = | |
20cf157b | 7660 | N_Object_Renaming_Declaration |
0577b0b1 | 7661 | then |
7662 | declare | |
7663 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
7664 | begin | |
7665 | if Nkind (Old_Exp) = N_Indexed_Component | |
7666 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
7667 | then | |
7668 | Expand_Packed_Element_Reference (Old_Exp); | |
7669 | end if; | |
7670 | end; | |
7671 | end if; | |
0577b0b1 | 7672 | end; |
ee6ba406 | 7673 | end Insert_Valid_Check; |
7674 | ||
3cce7f32 | 7675 | ------------------------------------- |
7676 | -- Is_Signed_Integer_Arithmetic_Op -- | |
7677 | ------------------------------------- | |
7678 | ||
7679 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
7680 | begin | |
7681 | case Nkind (N) is | |
99378362 | 7682 | when N_Op_Abs |
7683 | | N_Op_Add | |
7684 | | N_Op_Divide | |
7685 | | N_Op_Expon | |
7686 | | N_Op_Minus | |
7687 | | N_Op_Mod | |
7688 | | N_Op_Multiply | |
7689 | | N_Op_Plus | |
7690 | | N_Op_Rem | |
7691 | | N_Op_Subtract | |
7692 | => | |
3cce7f32 | 7693 | return Is_Signed_Integer_Type (Etype (N)); |
7694 | ||
99378362 | 7695 | when N_Case_Expression |
7696 | | N_If_Expression | |
7697 | => | |
0326b4d4 | 7698 | return Is_Signed_Integer_Type (Etype (N)); |
7699 | ||
3cce7f32 | 7700 | when others => |
7701 | return False; | |
7702 | end case; | |
7703 | end Is_Signed_Integer_Arithmetic_Op; | |
7704 | ||
fa7497e8 | 7705 | ---------------------------------- |
7706 | -- Install_Null_Excluding_Check -- | |
7707 | ---------------------------------- | |
7708 | ||
7709 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 7710 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 7711 | Typ : constant Entity_Id := Etype (N); |
7712 | ||
7b31b357 | 7713 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
7714 | -- Determines if it is safe to capture Known_Non_Null status for an | |
7715 | -- the entity referenced by node N. The caller ensures that N is indeed | |
7716 | -- an entity name. It is safe to capture the non-null status for an IN | |
7717 | -- parameter when the reference occurs within a declaration that is sure | |
7718 | -- to be executed as part of the declarative region. | |
7870823d | 7719 | |
84d0d4a5 | 7720 | procedure Mark_Non_Null; |
7870823d | 7721 | -- After installation of check, if the node in question is an entity |
7722 | -- name, then mark this entity as non-null if possible. | |
7723 | ||
7b31b357 | 7724 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 7725 | E : constant Entity_Id := Entity (N); |
7726 | S : constant Entity_Id := Current_Scope; | |
7727 | S_Par : Node_Id; | |
7728 | ||
7729 | begin | |
7b31b357 | 7730 | if Ekind (E) /= E_In_Parameter then |
7731 | return False; | |
7732 | end if; | |
7870823d | 7733 | |
7734 | -- Two initial context checks. We must be inside a subprogram body | |
7735 | -- with declarations and reference must not appear in nested scopes. | |
7736 | ||
7b31b357 | 7737 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 7738 | or else Scope (E) /= S |
7739 | then | |
7740 | return False; | |
7741 | end if; | |
7742 | ||
7743 | S_Par := Parent (Parent (S)); | |
7744 | ||
7745 | if Nkind (S_Par) /= N_Subprogram_Body | |
7746 | or else No (Declarations (S_Par)) | |
7747 | then | |
7748 | return False; | |
7749 | end if; | |
7750 | ||
7751 | declare | |
7752 | N_Decl : Node_Id; | |
7753 | P : Node_Id; | |
7754 | ||
7755 | begin | |
7756 | -- Retrieve the declaration node of N (if any). Note that N | |
7757 | -- may be a part of a complex initialization expression. | |
7758 | ||
7759 | P := Parent (N); | |
7760 | N_Decl := Empty; | |
7761 | while Present (P) loop | |
7762 | ||
7b31b357 | 7763 | -- If we have a short circuit form, and we are within the right |
7764 | -- hand expression, we return false, since the right hand side | |
7765 | -- is not guaranteed to be elaborated. | |
7766 | ||
7767 | if Nkind (P) in N_Short_Circuit | |
7768 | and then N = Right_Opnd (P) | |
7769 | then | |
7770 | return False; | |
7771 | end if; | |
7772 | ||
92f1631f | 7773 | -- Similarly, if we are in an if expression and not part of the |
7774 | -- condition, then we return False, since neither the THEN or | |
7775 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 7776 | |
92f1631f | 7777 | if Nkind (P) = N_If_Expression |
7b31b357 | 7778 | and then N /= First (Expressions (P)) |
7779 | then | |
7780 | return False; | |
e977c0cf | 7781 | end if; |
7782 | ||
20cf157b | 7783 | -- If within a case expression, and not part of the expression, |
7784 | -- then return False, since a particular dependent expression | |
7785 | -- may not always be elaborated | |
e977c0cf | 7786 | |
7787 | if Nkind (P) = N_Case_Expression | |
7788 | and then N /= Expression (P) | |
7789 | then | |
7790 | return False; | |
7b31b357 | 7791 | end if; |
7792 | ||
20cf157b | 7793 | -- While traversing the parent chain, if node N belongs to a |
7794 | -- statement, then it may never appear in a declarative region. | |
7870823d | 7795 | |
7796 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
7797 | or else Nkind (P) = N_Procedure_Call_Statement | |
7798 | then | |
7799 | return False; | |
7800 | end if; | |
7801 | ||
7b31b357 | 7802 | -- If we are at a declaration, record it and exit |
7803 | ||
7870823d | 7804 | if Nkind (P) in N_Declaration |
7805 | and then Nkind (P) not in N_Subprogram_Specification | |
7806 | then | |
7807 | N_Decl := P; | |
7808 | exit; | |
7809 | end if; | |
7810 | ||
7811 | P := Parent (P); | |
7812 | end loop; | |
7813 | ||
7814 | if No (N_Decl) then | |
7815 | return False; | |
7816 | end if; | |
7817 | ||
7818 | return List_Containing (N_Decl) = Declarations (S_Par); | |
7819 | end; | |
7b31b357 | 7820 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 7821 | |
7822 | ------------------- | |
7823 | -- Mark_Non_Null -- | |
7824 | ------------------- | |
7825 | ||
7826 | procedure Mark_Non_Null is | |
7827 | begin | |
7870823d | 7828 | -- Only case of interest is if node N is an entity name |
7829 | ||
84d0d4a5 | 7830 | if Is_Entity_Name (N) then |
7870823d | 7831 | |
7832 | -- For sure, we want to clear an indication that this is known to | |
39a0c1d3 | 7833 | -- be null, since if we get past this check, it definitely is not. |
7870823d | 7834 | |
84d0d4a5 | 7835 | Set_Is_Known_Null (Entity (N), False); |
7836 | ||
7870823d | 7837 | -- We can mark the entity as known to be non-null if either it is |
7838 | -- safe to capture the value, or in the case of an IN parameter, | |
7839 | -- which is a constant, if the check we just installed is in the | |
7840 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 7841 | -- a check is decisive for the rest of the body if the expression |
7842 | -- is sure to be elaborated, since we know we have to elaborate | |
7843 | -- all declarations before executing the body. | |
7844 | ||
7845 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 7846 | |
7847 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 7848 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 7849 | then |
7850 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 7851 | end if; |
7852 | end if; | |
7853 | end Mark_Non_Null; | |
7854 | ||
7855 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 7856 | |
7857 | begin | |
c9bbc06b | 7858 | -- No need to add null-excluding checks when the tree may not be fully |
7859 | -- decorated. | |
7860 | ||
7861 | if Serious_Errors_Detected > 0 then | |
7862 | return; | |
7863 | end if; | |
7864 | ||
84d0d4a5 | 7865 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 7866 | |
46e32b5e | 7867 | -- No check inside a generic, check will be emitted in instance |
fa7497e8 | 7868 | |
84d0d4a5 | 7869 | if Inside_A_Generic then |
fa7497e8 | 7870 | return; |
84d0d4a5 | 7871 | end if; |
7872 | ||
7873 | -- No check needed if known to be non-null | |
7874 | ||
7875 | if Known_Non_Null (N) then | |
05fcfafb | 7876 | return; |
84d0d4a5 | 7877 | end if; |
fa7497e8 | 7878 | |
84d0d4a5 | 7879 | -- If known to be null, here is where we generate a compile time check |
7880 | ||
7881 | if Known_Null (N) then | |
d16989f1 | 7882 | |
20cf157b | 7883 | -- Avoid generating warning message inside init procs. In SPARK mode |
7884 | -- we can go ahead and call Apply_Compile_Time_Constraint_Error | |
46e32b5e | 7885 | -- since it will be turned into an error in any case. |
d16989f1 | 7886 | |
46e32b5e | 7887 | if (not Inside_Init_Proc or else SPARK_Mode = On) |
7888 | ||
28d5d68f | 7889 | -- Do not emit the warning within a conditional expression, |
7890 | -- where the expression might not be evaluated, and the warning | |
7891 | -- appear as extraneous noise. | |
46e32b5e | 7892 | |
7893 | and then not Within_Case_Or_If_Expression (N) | |
7894 | then | |
d16989f1 | 7895 | Apply_Compile_Time_Constraint_Error |
4098232e | 7896 | (N, "null value not allowed here??", CE_Access_Check_Failed); |
46e32b5e | 7897 | |
7898 | -- Remaining cases, where we silently insert the raise | |
7899 | ||
d16989f1 | 7900 | else |
7901 | Insert_Action (N, | |
7902 | Make_Raise_Constraint_Error (Loc, | |
7903 | Reason => CE_Access_Check_Failed)); | |
7904 | end if; | |
7905 | ||
84d0d4a5 | 7906 | Mark_Non_Null; |
7907 | return; | |
7908 | end if; | |
7909 | ||
7910 | -- If entity is never assigned, for sure a warning is appropriate | |
7911 | ||
7912 | if Is_Entity_Name (N) then | |
7913 | Check_Unset_Reference (N); | |
fa7497e8 | 7914 | end if; |
84d0d4a5 | 7915 | |
7916 | -- No check needed if checks are suppressed on the range. Note that we | |
7917 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
7918 | -- so, since the program is erroneous, but we don't like to casually | |
7919 | -- propagate such conclusions from erroneosity). | |
7920 | ||
7921 | if Access_Checks_Suppressed (Typ) then | |
7922 | return; | |
7923 | end if; | |
7924 | ||
2af58f67 | 7925 | -- No check needed for access to concurrent record types generated by |
7926 | -- the expander. This is not just an optimization (though it does indeed | |
7927 | -- remove junk checks). It also avoids generation of junk warnings. | |
7928 | ||
7929 | if Nkind (N) in N_Has_Chars | |
7930 | and then Chars (N) = Name_uObject | |
7931 | and then Is_Concurrent_Record_Type | |
7932 | (Directly_Designated_Type (Etype (N))) | |
7933 | then | |
7934 | return; | |
7935 | end if; | |
7936 | ||
228836e8 | 7937 | -- No check needed in interface thunks since the runtime check is |
7938 | -- already performed at the caller side. | |
7939 | ||
7940 | if Is_Thunk (Current_Scope) then | |
7941 | return; | |
7942 | end if; | |
7943 | ||
472ea160 | 7944 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
7945 | -- the expander within exception handlers, since we know that the value | |
7946 | -- can never be null. | |
7947 | ||
7948 | -- Is this really the right way to do this? Normally we generate such | |
7949 | -- code in the expander with checks off, and that's how we suppress this | |
7950 | -- kind of junk check ??? | |
7951 | ||
7952 | if Nkind (N) = N_Function_Call | |
7953 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
7954 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
7955 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
7956 | then | |
7957 | return; | |
7958 | end if; | |
7959 | ||
84d0d4a5 | 7960 | -- Otherwise install access check |
7961 | ||
7962 | Insert_Action (N, | |
7963 | Make_Raise_Constraint_Error (Loc, | |
7964 | Condition => | |
7965 | Make_Op_Eq (Loc, | |
7966 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
7967 | Right_Opnd => Make_Null (Loc)), | |
7968 | Reason => CE_Access_Check_Failed)); | |
7969 | ||
7970 | Mark_Non_Null; | |
fa7497e8 | 7971 | end Install_Null_Excluding_Check; |
7972 | ||
7e933b61 | 7973 | ----------------------------------------- |
7974 | -- Install_Primitive_Elaboration_Check -- | |
7975 | ----------------------------------------- | |
7976 | ||
7977 | procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id) is | |
7e933b61 | 7978 | function Within_Compilation_Unit_Instance |
7979 | (Subp_Id : Entity_Id) return Boolean; | |
7980 | -- Determine whether subprogram Subp_Id appears within an instance which | |
7981 | -- acts as a compilation unit. | |
7982 | ||
7983 | -------------------------------------- | |
7984 | -- Within_Compilation_Unit_Instance -- | |
7985 | -------------------------------------- | |
7986 | ||
7987 | function Within_Compilation_Unit_Instance | |
7988 | (Subp_Id : Entity_Id) return Boolean | |
7989 | is | |
7990 | Pack : Entity_Id; | |
7991 | ||
7992 | begin | |
7993 | -- Examine the scope chain looking for a compilation-unit-level | |
7994 | -- instance. | |
7995 | ||
7996 | Pack := Scope (Subp_Id); | |
7997 | while Present (Pack) and then Pack /= Standard_Standard loop | |
7998 | if Ekind (Pack) = E_Package | |
7999 | and then Is_Generic_Instance (Pack) | |
8000 | and then Nkind (Parent (Unit_Declaration_Node (Pack))) = | |
8001 | N_Compilation_Unit | |
8002 | then | |
8003 | return True; | |
8004 | end if; | |
8005 | ||
8006 | Pack := Scope (Pack); | |
8007 | end loop; | |
8008 | ||
8009 | return False; | |
8010 | end Within_Compilation_Unit_Instance; | |
8011 | ||
8012 | -- Local declarations | |
8013 | ||
8014 | Context : constant Node_Id := Parent (Subp_Body); | |
8015 | Loc : constant Source_Ptr := Sloc (Subp_Body); | |
8016 | Subp_Id : constant Entity_Id := Unique_Defining_Entity (Subp_Body); | |
8017 | Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id); | |
8018 | ||
e2293a63 | 8019 | Decls : List_Id; |
8020 | Flag_Id : Entity_Id; | |
8021 | Set_Ins : Node_Id; | |
8022 | Set_Stmt : Node_Id; | |
8023 | Tag_Typ : Entity_Id; | |
7e933b61 | 8024 | |
8025 | -- Start of processing for Install_Primitive_Elaboration_Check | |
8026 | ||
8027 | begin | |
8028 | -- Do not generate an elaboration check in compilation modes where | |
8029 | -- expansion is not desirable. | |
8030 | ||
8031 | if ASIS_Mode or GNATprove_Mode then | |
8032 | return; | |
8033 | ||
f0e731f2 | 8034 | -- Do not generate an elaboration check if all checks have been |
8035 | -- suppressed. | |
62c62e4b | 8036 | |
f0e731f2 | 8037 | elsif Suppress_Checks then |
62c62e4b | 8038 | return; |
8039 | ||
7e933b61 | 8040 | -- Do not generate an elaboration check if the related subprogram is |
8041 | -- not subjected to accessibility checks. | |
8042 | ||
8043 | elsif Elaboration_Checks_Suppressed (Subp_Id) then | |
8044 | return; | |
f0e731f2 | 8045 | |
8046 | -- Do not generate an elaboration check if such code is not desirable | |
8047 | ||
8048 | elsif Restriction_Active (No_Elaboration_Code) then | |
8049 | return; | |
7e933b61 | 8050 | |
77b577f9 | 8051 | -- Do not generate an elaboration check if exceptions cannot be used, |
8052 | -- caught, or propagated. | |
8053 | ||
8054 | elsif not Exceptions_OK then | |
8055 | return; | |
8056 | ||
7e933b61 | 8057 | -- Do not consider subprograms which act as compilation units, because |
8058 | -- they cannot be the target of a dispatching call. | |
8059 | ||
8060 | elsif Nkind (Context) = N_Compilation_Unit then | |
8061 | return; | |
8062 | ||
e2293a63 | 8063 | -- Do not consider anything other than nonabstract library-level source |
8064 | -- primitives. | |
7e933b61 | 8065 | |
8066 | elsif not | |
8067 | (Comes_From_Source (Subp_Id) | |
8068 | and then Is_Library_Level_Entity (Subp_Id) | |
8069 | and then Is_Primitive (Subp_Id) | |
8070 | and then not Is_Abstract_Subprogram (Subp_Id)) | |
8071 | then | |
8072 | return; | |
8073 | ||
8074 | -- Do not consider inlined primitives, because once the body is inlined | |
8075 | -- the reference to the elaboration flag will be out of place and will | |
8076 | -- result in an undefined symbol. | |
8077 | ||
8078 | elsif Is_Inlined (Subp_Id) or else Has_Pragma_Inline (Subp_Id) then | |
8079 | return; | |
8080 | ||
8081 | -- Do not generate a duplicate elaboration check. This happens only in | |
8082 | -- the case of primitives completed by an expression function, as the | |
8083 | -- corresponding body is apparently analyzed and expanded twice. | |
8084 | ||
8085 | elsif Analyzed (Subp_Body) then | |
8086 | return; | |
8087 | ||
8088 | -- Do not consider primitives which occur within an instance that acts | |
8089 | -- as a compilation unit. Such an instance defines its spec and body out | |
8090 | -- of order (body is first) within the tree, which causes the reference | |
8091 | -- to the elaboration flag to appear as an undefined symbol. | |
8092 | ||
8093 | elsif Within_Compilation_Unit_Instance (Subp_Id) then | |
8094 | return; | |
8095 | end if; | |
8096 | ||
8097 | Tag_Typ := Find_Dispatching_Type (Subp_Id); | |
8098 | ||
8099 | -- Only tagged primitives may be the target of a dispatching call | |
8100 | ||
8101 | if No (Tag_Typ) then | |
8102 | return; | |
8103 | ||
8104 | -- Do not consider finalization-related primitives, because they may | |
8105 | -- need to be called while elaboration is taking place. | |
8106 | ||
8107 | elsif Is_Controlled (Tag_Typ) | |
8108 | and then Nam_In (Chars (Subp_Id), Name_Adjust, | |
8109 | Name_Finalize, | |
8110 | Name_Initialize) | |
8111 | then | |
8112 | return; | |
8113 | end if; | |
8114 | ||
8115 | -- Create the declaration of the elaboration flag. The name carries a | |
8116 | -- unique counter in case of name overloading. | |
8117 | ||
8118 | Flag_Id := | |
8119 | Make_Defining_Identifier (Loc, | |
fe48a434 | 8120 | Chars => New_External_Name (Chars (Subp_Id), 'E', -1)); |
7e933b61 | 8121 | Set_Is_Frozen (Flag_Id); |
8122 | ||
8123 | -- Insert the declaration of the elaboration flag in front of the | |
8124 | -- primitive spec and analyze it in the proper context. | |
8125 | ||
8126 | Push_Scope (Scope (Subp_Id)); | |
8127 | ||
8128 | -- Generate: | |
fe48a434 | 8129 | -- E : Boolean := False; |
7e933b61 | 8130 | |
8131 | Insert_Action (Subp_Decl, | |
8132 | Make_Object_Declaration (Loc, | |
8133 | Defining_Identifier => Flag_Id, | |
8134 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), | |
8135 | Expression => New_Occurrence_Of (Standard_False, Loc))); | |
8136 | Pop_Scope; | |
8137 | ||
8138 | -- Prevent the compiler from optimizing the elaboration check by killing | |
8139 | -- the current value of the flag and the associated assignment. | |
8140 | ||
8141 | Set_Current_Value (Flag_Id, Empty); | |
8142 | Set_Last_Assignment (Flag_Id, Empty); | |
8143 | ||
8144 | -- Add a check at the top of the body declarations to ensure that the | |
8145 | -- elaboration flag has been set. | |
8146 | ||
8147 | Decls := Declarations (Subp_Body); | |
8148 | ||
8149 | if No (Decls) then | |
8150 | Decls := New_List; | |
8151 | Set_Declarations (Subp_Body, Decls); | |
8152 | end if; | |
8153 | ||
8154 | -- Generate: | |
8155 | -- if not F then | |
8156 | -- raise Program_Error with "access before elaboration"; | |
8157 | -- end if; | |
8158 | ||
8159 | Prepend_To (Decls, | |
8160 | Make_Raise_Program_Error (Loc, | |
8161 | Condition => | |
8162 | Make_Op_Not (Loc, | |
8163 | Right_Opnd => New_Occurrence_Of (Flag_Id, Loc)), | |
8164 | Reason => PE_Access_Before_Elaboration)); | |
8165 | ||
8166 | Analyze (First (Decls)); | |
8167 | ||
8168 | -- Set the elaboration flag once the body has been elaborated. Insert | |
8169 | -- the statement after the subprogram stub when the primitive body is | |
8170 | -- a subunit. | |
8171 | ||
8172 | if Nkind (Context) = N_Subunit then | |
8173 | Set_Ins := Corresponding_Stub (Context); | |
8174 | else | |
8175 | Set_Ins := Subp_Body; | |
8176 | end if; | |
8177 | ||
8178 | -- Generate: | |
fe48a434 | 8179 | -- E := True; |
7e933b61 | 8180 | |
e2293a63 | 8181 | Set_Stmt := |
7e933b61 | 8182 | Make_Assignment_Statement (Loc, |
8183 | Name => New_Occurrence_Of (Flag_Id, Loc), | |
e2293a63 | 8184 | Expression => New_Occurrence_Of (Standard_True, Loc)); |
8185 | ||
8186 | -- Mark the assignment statement as elaboration code. This allows the | |
8187 | -- early call region mechanism (see Sem_Elab) to properly ignore such | |
8188 | -- assignments even though they are non-preelaborable code. | |
8189 | ||
8190 | Set_Is_Elaboration_Code (Set_Stmt); | |
8191 | ||
8192 | Insert_After_And_Analyze (Set_Ins, Set_Stmt); | |
7e933b61 | 8193 | end Install_Primitive_Elaboration_Check; |
8194 | ||
ee6ba406 | 8195 | -------------------------- |
8196 | -- Install_Static_Check -- | |
8197 | -------------------------- | |
8198 | ||
8199 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
cda40848 | 8200 | Stat : constant Boolean := Is_OK_Static_Expression (R_Cno); |
ee6ba406 | 8201 | Typ : constant Entity_Id := Etype (R_Cno); |
8202 | ||
8203 | begin | |
f15731c4 | 8204 | Rewrite (R_Cno, |
8205 | Make_Raise_Constraint_Error (Loc, | |
8206 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 8207 | Set_Analyzed (R_Cno); |
8208 | Set_Etype (R_Cno, Typ); | |
8209 | Set_Raises_Constraint_Error (R_Cno); | |
8210 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 8211 | |
8212 | -- Now deal with possible local raise handling | |
8213 | ||
8214 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 8215 | end Install_Static_Check; |
8216 | ||
3cce7f32 | 8217 | ------------------------- |
8218 | -- Is_Check_Suppressed -- | |
8219 | ------------------------- | |
8220 | ||
8221 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
8222 | Ptr : Suppress_Stack_Entry_Ptr; | |
8223 | ||
8224 | begin | |
8225 | -- First search the local entity suppress stack. We search this from the | |
8226 | -- top of the stack down so that we get the innermost entry that applies | |
8227 | -- to this case if there are nested entries. | |
8228 | ||
8229 | Ptr := Local_Suppress_Stack_Top; | |
8230 | while Ptr /= null loop | |
8231 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8232 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8233 | then | |
8234 | return Ptr.Suppress; | |
8235 | end if; | |
8236 | ||
8237 | Ptr := Ptr.Prev; | |
8238 | end loop; | |
8239 | ||
8240 | -- Now search the global entity suppress table for a matching entry. | |
8241 | -- We also search this from the top down so that if there are multiple | |
8242 | -- pragmas for the same entity, the last one applies (not clear what | |
8243 | -- or whether the RM specifies this handling, but it seems reasonable). | |
8244 | ||
8245 | Ptr := Global_Suppress_Stack_Top; | |
8246 | while Ptr /= null loop | |
8247 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8248 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8249 | then | |
8250 | return Ptr.Suppress; | |
8251 | end if; | |
8252 | ||
8253 | Ptr := Ptr.Prev; | |
8254 | end loop; | |
8255 | ||
8256 | -- If we did not find a matching entry, then use the normal scope | |
8257 | -- suppress value after all (actually this will be the global setting | |
8258 | -- since it clearly was not overridden at any point). For a predefined | |
8259 | -- check, we test the specific flag. For a user defined check, we check | |
8260 | -- the All_Checks flag. The Overflow flag requires special handling to | |
fe48a434 | 8261 | -- deal with the General vs Assertion case. |
3cce7f32 | 8262 | |
8263 | if C = Overflow_Check then | |
8264 | return Overflow_Checks_Suppressed (Empty); | |
fe48a434 | 8265 | |
3cce7f32 | 8266 | elsif C in Predefined_Check_Id then |
8267 | return Scope_Suppress.Suppress (C); | |
fe48a434 | 8268 | |
3cce7f32 | 8269 | else |
8270 | return Scope_Suppress.Suppress (All_Checks); | |
8271 | end if; | |
8272 | end Is_Check_Suppressed; | |
8273 | ||
9dfe12ae | 8274 | --------------------- |
8275 | -- Kill_All_Checks -- | |
8276 | --------------------- | |
8277 | ||
8278 | procedure Kill_All_Checks is | |
8279 | begin | |
8280 | if Debug_Flag_CC then | |
8281 | w ("Kill_All_Checks"); | |
8282 | end if; | |
8283 | ||
feff2f05 | 8284 | -- We reset the number of saved checks to zero, and also modify all |
8285 | -- stack entries for statement ranges to indicate that the number of | |
8286 | -- checks at each level is now zero. | |
9dfe12ae | 8287 | |
8288 | Num_Saved_Checks := 0; | |
8289 | ||
96da3284 | 8290 | -- Note: the Int'Min here avoids any possibility of J being out of |
8291 | -- range when called from e.g. Conditional_Statements_Begin. | |
8292 | ||
8293 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 8294 | Saved_Checks_Stack (J) := 0; |
8295 | end loop; | |
8296 | end Kill_All_Checks; | |
8297 | ||
8298 | ----------------- | |
8299 | -- Kill_Checks -- | |
8300 | ----------------- | |
8301 | ||
8302 | procedure Kill_Checks (V : Entity_Id) is | |
8303 | begin | |
8304 | if Debug_Flag_CC then | |
8305 | w ("Kill_Checks for entity", Int (V)); | |
8306 | end if; | |
8307 | ||
8308 | for J in 1 .. Num_Saved_Checks loop | |
8309 | if Saved_Checks (J).Entity = V then | |
8310 | if Debug_Flag_CC then | |
8311 | w (" Checks killed for saved check ", J); | |
8312 | end if; | |
8313 | ||
8314 | Saved_Checks (J).Killed := True; | |
8315 | end if; | |
8316 | end loop; | |
8317 | end Kill_Checks; | |
8318 | ||
ee6ba406 | 8319 | ------------------------------ |
8320 | -- Length_Checks_Suppressed -- | |
8321 | ------------------------------ | |
8322 | ||
8323 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8324 | begin | |
9dfe12ae | 8325 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
8326 | return Is_Check_Suppressed (E, Length_Check); | |
8327 | else | |
fafc6b97 | 8328 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 8329 | end if; |
ee6ba406 | 8330 | end Length_Checks_Suppressed; |
8331 | ||
3cce7f32 | 8332 | ----------------------- |
8333 | -- Make_Bignum_Block -- | |
8334 | ----------------------- | |
8335 | ||
8336 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
8337 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
3cce7f32 | 8338 | begin |
8339 | return | |
8340 | Make_Block_Statement (Loc, | |
97c15ab0 | 8341 | Declarations => |
8342 | New_List (Build_SS_Mark_Call (Loc, M)), | |
3cce7f32 | 8343 | Handled_Statement_Sequence => |
8344 | Make_Handled_Sequence_Of_Statements (Loc, | |
97c15ab0 | 8345 | Statements => New_List (Build_SS_Release_Call (Loc, M)))); |
3cce7f32 | 8346 | end Make_Bignum_Block; |
8347 | ||
0df9d43f | 8348 | ---------------------------------- |
8349 | -- Minimize_Eliminate_Overflows -- | |
8350 | ---------------------------------- | |
3cce7f32 | 8351 | |
f32c377d | 8352 | -- This is a recursive routine that is called at the top of an expression |
8353 | -- tree to properly process overflow checking for a whole subtree by making | |
8354 | -- recursive calls to process operands. This processing may involve the use | |
8355 | -- of bignum or long long integer arithmetic, which will change the types | |
8356 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 8357 | -- it would interfere with semantic analysis). |
f32c377d | 8358 | |
21a55437 | 8359 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 8360 | -- the operator expansion routines, as well as the expansion routines for |
8361 | -- if/case expression, do nothing (for the moment) except call the routine | |
8362 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
8363 | -- routine does nothing for non top-level nodes, so at the point where the | |
8364 | -- call is made for the top level node, the entire expression subtree has | |
8365 | -- not been expanded, or processed for overflow. All that has to happen as | |
8366 | -- a result of the top level call to this routine. | |
f32c377d | 8367 | |
8368 | -- As noted above, the overflow processing works by making recursive calls | |
8369 | -- for the operands, and figuring out what to do, based on the processing | |
8370 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
8371 | -- to be done in bignum mode), and the determined ranges of the operands. | |
8372 | ||
8373 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 8374 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 8375 | -- the node (if it has been modified by the overflow check processing). The |
8376 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
8377 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 8378 | -- for this call is that the overflow handling mode must be temporarily set |
8379 | -- to STRICT. | |
f32c377d | 8380 | |
0df9d43f | 8381 | procedure Minimize_Eliminate_Overflows |
61016a7a | 8382 | (N : Node_Id; |
8383 | Lo : out Uint; | |
8384 | Hi : out Uint; | |
8385 | Top_Level : Boolean) | |
3cce7f32 | 8386 | is |
0326b4d4 | 8387 | Rtyp : constant Entity_Id := Etype (N); |
8388 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
8389 | -- Result type, must be a signed integer type | |
3cce7f32 | 8390 | |
db415383 | 8391 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 8392 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
8393 | ||
8394 | Loc : constant Source_Ptr := Sloc (N); | |
8395 | ||
8396 | Rlo, Rhi : Uint; | |
0326b4d4 | 8397 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 8398 | |
16149377 | 8399 | Llo : Uint := No_Uint; -- initialize to prevent warning |
8400 | Lhi : Uint := No_Uint; -- initialize to prevent warning | |
0326b4d4 | 8401 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 8402 | |
49b3a812 | 8403 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
8404 | -- Operands and results are of this type when we convert | |
8405 | ||
0326b4d4 | 8406 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
8407 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 8408 | -- Bounds of Long_Long_Integer |
8409 | ||
8410 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
8411 | -- Indicates binary operator case | |
8412 | ||
8413 | OK : Boolean; | |
8414 | -- Used in call to Determine_Range | |
8415 | ||
61016a7a | 8416 | Bignum_Operands : Boolean; |
8417 | -- Set True if one or more operands is already of type Bignum, meaning | |
8418 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 8419 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 8420 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 8421 | |
8422 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 8423 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 8424 | -- which means that if the result is known to be in the result type |
8425 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 8426 | |
8427 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
8428 | -- This is called when we have modified the node and we therefore need | |
8429 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
8430 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
39a0c1d3 | 8431 | -- we would reenter this routine recursively which would not be good. |
0df9d43f | 8432 | -- The argument Suppress is set True if we also want to suppress |
8433 | -- overflow checking for the reexpansion (this is set when we know | |
8434 | -- overflow is not possible). Typ is the type for the reanalysis. | |
8435 | ||
8436 | procedure Reexpand (Suppress : Boolean := False); | |
8437 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
8438 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
8439 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
8440 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
8441 | -- Note that skipping reanalysis is not just an optimization, testing | |
8442 | -- has showed up several complex cases in which reanalyzing an already | |
8443 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 8444 | |
0326b4d4 | 8445 | function In_Result_Range return Boolean; |
8446 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 8447 | |
2fe22c69 | 8448 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 8449 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 8450 | |
8451 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 8452 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 8453 | |
0326b4d4 | 8454 | --------------------- |
8455 | -- In_Result_Range -- | |
8456 | --------------------- | |
8457 | ||
8458 | function In_Result_Range return Boolean is | |
8459 | begin | |
f32c377d | 8460 | if Lo = No_Uint or else Hi = No_Uint then |
8461 | return False; | |
8462 | ||
cda40848 | 8463 | elsif Is_OK_Static_Subtype (Etype (N)) then |
0326b4d4 | 8464 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
8465 | and then | |
8466 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 8467 | |
0326b4d4 | 8468 | else |
8469 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
8470 | and then | |
8471 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
8472 | end if; | |
8473 | end In_Result_Range; | |
8474 | ||
2fe22c69 | 8475 | --------- |
8476 | -- Max -- | |
8477 | --------- | |
8478 | ||
8479 | procedure Max (A : in out Uint; B : Uint) is | |
8480 | begin | |
8481 | if A = No_Uint or else B > A then | |
8482 | A := B; | |
8483 | end if; | |
8484 | end Max; | |
8485 | ||
8486 | --------- | |
8487 | -- Min -- | |
8488 | --------- | |
8489 | ||
8490 | procedure Min (A : in out Uint; B : Uint) is | |
8491 | begin | |
8492 | if A = No_Uint or else B < A then | |
8493 | A := B; | |
8494 | end if; | |
8495 | end Min; | |
8496 | ||
0df9d43f | 8497 | --------------- |
8498 | -- Reanalyze -- | |
8499 | --------------- | |
8500 | ||
8501 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 8502 | Svg : constant Overflow_Mode_Type := |
8503 | Scope_Suppress.Overflow_Mode_General; | |
8504 | Sva : constant Overflow_Mode_Type := | |
8505 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 8506 | Svo : constant Boolean := |
8507 | Scope_Suppress.Suppress (Overflow_Check); | |
8508 | ||
8509 | begin | |
db415383 | 8510 | Scope_Suppress.Overflow_Mode_General := Strict; |
8511 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 8512 | |
8513 | if Suppress then | |
8514 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
8515 | end if; | |
8516 | ||
8517 | Analyze_And_Resolve (N, Typ); | |
8518 | ||
2b108e18 | 8519 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
8520 | Scope_Suppress.Overflow_Mode_General := Svg; | |
8521 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 8522 | end Reanalyze; |
8523 | ||
4fb5f0a0 | 8524 | -------------- |
8525 | -- Reexpand -- | |
8526 | -------------- | |
8527 | ||
0df9d43f | 8528 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 8529 | Svg : constant Overflow_Mode_Type := |
8530 | Scope_Suppress.Overflow_Mode_General; | |
8531 | Sva : constant Overflow_Mode_Type := | |
8532 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 8533 | Svo : constant Boolean := |
8534 | Scope_Suppress.Suppress (Overflow_Check); | |
8535 | ||
4fb5f0a0 | 8536 | begin |
db415383 | 8537 | Scope_Suppress.Overflow_Mode_General := Strict; |
8538 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 8539 | Set_Analyzed (N, False); |
0df9d43f | 8540 | |
8541 | if Suppress then | |
8542 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
8543 | end if; | |
8544 | ||
4fb5f0a0 | 8545 | Expand (N); |
0df9d43f | 8546 | |
2b108e18 | 8547 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
8548 | Scope_Suppress.Overflow_Mode_General := Svg; | |
8549 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 8550 | end Reexpand; |
8551 | ||
0df9d43f | 8552 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 8553 | |
3cce7f32 | 8554 | begin |
02038e4e | 8555 | -- Default initialize Lo and Hi since these are not guaranteed to be |
8556 | -- set otherwise. | |
8557 | ||
8558 | Lo := No_Uint; | |
8559 | Hi := No_Uint; | |
8560 | ||
0326b4d4 | 8561 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 8562 | |
8563 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
8564 | ||
8565 | -- Use the normal Determine_Range routine to get the range. We | |
8566 | -- don't require operands to be valid, invalid values may result in | |
8567 | -- rubbish results where the result has not been properly checked for | |
39a0c1d3 | 8568 | -- overflow, that's fine. |
3cce7f32 | 8569 | |
8570 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
8571 | ||
21a55437 | 8572 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 8573 | -- clear but might as well protect), use type bounds. |
8574 | ||
8575 | if not OK then | |
8576 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
8577 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
8578 | end if; | |
8579 | ||
8580 | -- If we don't have a binary operator, all we have to do is to set | |
20cf157b | 8581 | -- the Hi/Lo range, so we are done. |
3cce7f32 | 8582 | |
8583 | return; | |
8584 | ||
0326b4d4 | 8585 | -- Processing for if expression |
8586 | ||
92f1631f | 8587 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 8588 | declare |
8589 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
8590 | Else_DE : constant Node_Id := Next (Then_DE); | |
8591 | ||
8592 | begin | |
8593 | Bignum_Operands := False; | |
8594 | ||
0df9d43f | 8595 | Minimize_Eliminate_Overflows |
0326b4d4 | 8596 | (Then_DE, Lo, Hi, Top_Level => False); |
8597 | ||
8598 | if Lo = No_Uint then | |
8599 | Bignum_Operands := True; | |
8600 | end if; | |
8601 | ||
0df9d43f | 8602 | Minimize_Eliminate_Overflows |
0326b4d4 | 8603 | (Else_DE, Rlo, Rhi, Top_Level => False); |
8604 | ||
8605 | if Rlo = No_Uint then | |
8606 | Bignum_Operands := True; | |
8607 | else | |
8608 | Long_Long_Integer_Operands := | |
8609 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
8610 | ||
8611 | Min (Lo, Rlo); | |
8612 | Max (Hi, Rhi); | |
8613 | end if; | |
8614 | ||
21a55437 | 8615 | -- If at least one of our operands is now Bignum, we must rebuild |
8616 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 8617 | -- rebuilt if expression with overflow checks off, since once we |
39a0c1d3 | 8618 | -- are in bignum mode, we are all done with overflow checks. |
0326b4d4 | 8619 | |
8620 | if Bignum_Operands then | |
8621 | Rewrite (N, | |
92f1631f | 8622 | Make_If_Expression (Loc, |
0326b4d4 | 8623 | Expressions => New_List ( |
8624 | Remove_Head (Expressions (N)), | |
8625 | Convert_To_Bignum (Then_DE), | |
8626 | Convert_To_Bignum (Else_DE)), | |
8627 | Is_Elsif => Is_Elsif (N))); | |
8628 | ||
0df9d43f | 8629 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 8630 | |
8631 | -- If we have no Long_Long_Integer operands, then we are in result | |
8632 | -- range, since it means that none of our operands felt the need | |
8633 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 8634 | -- converted to long long integer or bignum). We reexpand to |
8635 | -- complete the expansion of the if expression (but we do not | |
8636 | -- need to reanalyze). | |
0326b4d4 | 8637 | |
8638 | elsif not Long_Long_Integer_Operands then | |
8639 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 8640 | Reexpand; |
0326b4d4 | 8641 | |
8642 | -- Otherwise convert us to long long integer mode. Note that we | |
8643 | -- don't need any further overflow checking at this level. | |
8644 | ||
8645 | else | |
8646 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
8647 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
8648 | Set_Etype (N, LLIB); | |
f32c377d | 8649 | |
8650 | -- Now reanalyze with overflow checks off | |
8651 | ||
0326b4d4 | 8652 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 8653 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 8654 | end if; |
8655 | end; | |
8656 | ||
8657 | return; | |
8658 | ||
8659 | -- Here for case expression | |
8660 | ||
8661 | elsif Nkind (N) = N_Case_Expression then | |
8662 | Bignum_Operands := False; | |
8663 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 8664 | |
8665 | declare | |
f32c377d | 8666 | Alt : Node_Id; |
0326b4d4 | 8667 | |
8668 | begin | |
8669 | -- Loop through expressions applying recursive call | |
8670 | ||
8671 | Alt := First (Alternatives (N)); | |
8672 | while Present (Alt) loop | |
8673 | declare | |
8674 | Aexp : constant Node_Id := Expression (Alt); | |
8675 | ||
8676 | begin | |
0df9d43f | 8677 | Minimize_Eliminate_Overflows |
0326b4d4 | 8678 | (Aexp, Lo, Hi, Top_Level => False); |
8679 | ||
8680 | if Lo = No_Uint then | |
8681 | Bignum_Operands := True; | |
8682 | elsif Etype (Aexp) = LLIB then | |
8683 | Long_Long_Integer_Operands := True; | |
8684 | end if; | |
8685 | end; | |
8686 | ||
8687 | Next (Alt); | |
8688 | end loop; | |
8689 | ||
8690 | -- If we have no bignum or long long integer operands, it means | |
8691 | -- that none of our dependent expressions could raise overflow. | |
8692 | -- In this case, we simply return with no changes except for | |
8693 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 8694 | -- checks for this node. We will reexpand to get the needed |
8695 | -- expansion for the case expression, but we do not need to | |
21a55437 | 8696 | -- reanalyze, since nothing has changed. |
0326b4d4 | 8697 | |
f32c377d | 8698 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 8699 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 8700 | Reexpand (Suppress => True); |
0326b4d4 | 8701 | |
8702 | -- Otherwise we are going to rebuild the case expression using | |
8703 | -- either bignum or long long integer operands throughout. | |
8704 | ||
8705 | else | |
f32c377d | 8706 | declare |
8707 | Rtype : Entity_Id; | |
16149377 | 8708 | pragma Warnings (Off, Rtype); |
f32c377d | 8709 | New_Alts : List_Id; |
8710 | New_Exp : Node_Id; | |
8711 | ||
8712 | begin | |
8713 | New_Alts := New_List; | |
8714 | Alt := First (Alternatives (N)); | |
8715 | while Present (Alt) loop | |
8716 | if Bignum_Operands then | |
8717 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
8718 | Rtype := RTE (RE_Bignum); | |
8719 | else | |
8720 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
8721 | Rtype := LLIB; | |
8722 | end if; | |
0326b4d4 | 8723 | |
f32c377d | 8724 | Append_To (New_Alts, |
8725 | Make_Case_Expression_Alternative (Sloc (Alt), | |
8726 | Actions => No_List, | |
8727 | Discrete_Choices => Discrete_Choices (Alt), | |
8728 | Expression => New_Exp)); | |
0326b4d4 | 8729 | |
f32c377d | 8730 | Next (Alt); |
8731 | end loop; | |
0326b4d4 | 8732 | |
f32c377d | 8733 | Rewrite (N, |
8734 | Make_Case_Expression (Loc, | |
8735 | Expression => Expression (N), | |
8736 | Alternatives => New_Alts)); | |
0326b4d4 | 8737 | |
0df9d43f | 8738 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 8739 | end; |
0326b4d4 | 8740 | end if; |
8741 | end; | |
8742 | ||
8743 | return; | |
8744 | end if; | |
8745 | ||
8746 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 8747 | -- operands to get the ranges (and to properly process the subtree |
20cf157b | 8748 | -- that lies below us). |
3cce7f32 | 8749 | |
0df9d43f | 8750 | Minimize_Eliminate_Overflows |
0326b4d4 | 8751 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 8752 | |
0326b4d4 | 8753 | if Binary then |
0df9d43f | 8754 | Minimize_Eliminate_Overflows |
0326b4d4 | 8755 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 8756 | end if; |
8757 | ||
f32c377d | 8758 | -- Record if we have Long_Long_Integer operands |
8759 | ||
8760 | Long_Long_Integer_Operands := | |
8761 | Etype (Right_Opnd (N)) = LLIB | |
8762 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
8763 | ||
8764 | -- If either operand is a bignum, then result will be a bignum and we | |
8765 | -- don't need to do any range analysis. As previously discussed we could | |
8766 | -- do range analysis in such cases, but it could mean working with giant | |
8767 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 8768 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 8769 | |
8770 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
8771 | Lo := No_Uint; | |
8772 | Hi := No_Uint; | |
61016a7a | 8773 | Bignum_Operands := True; |
3cce7f32 | 8774 | |
8775 | -- Otherwise compute result range | |
8776 | ||
8777 | else | |
61016a7a | 8778 | Bignum_Operands := False; |
8779 | ||
3cce7f32 | 8780 | case Nkind (N) is |
8781 | ||
8782 | -- Absolute value | |
8783 | ||
8784 | when N_Op_Abs => | |
8785 | Lo := Uint_0; | |
de922300 | 8786 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 8787 | |
8788 | -- Addition | |
8789 | ||
8790 | when N_Op_Add => | |
8791 | Lo := Llo + Rlo; | |
8792 | Hi := Lhi + Rhi; | |
8793 | ||
8794 | -- Division | |
8795 | ||
8796 | when N_Op_Divide => | |
2fe22c69 | 8797 | |
5f4275e1 | 8798 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 8799 | |
5f4275e1 | 8800 | if Rlo = 0 and then Rhi = 0 then |
8801 | Lo := Uint_0; | |
8802 | Hi := Uint_0; | |
2fe22c69 | 8803 | |
5f4275e1 | 8804 | -- Possible bounds of division must come from dividing end |
8805 | -- values of the input ranges (four possibilities), provided | |
8806 | -- zero is not included in the possible values of the right | |
8807 | -- operand. | |
8808 | ||
8809 | -- Otherwise, we just consider two intervals of values for | |
8810 | -- the right operand: the interval of negative values (up to | |
8811 | -- -1) and the interval of positive values (starting at 1). | |
8812 | -- Since division by 1 is the identity, and division by -1 | |
8813 | -- is negation, we get all possible bounds of division in that | |
8814 | -- case by considering: | |
8815 | -- - all values from the division of end values of input | |
8816 | -- ranges; | |
8817 | -- - the end values of the left operand; | |
8818 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 8819 | |
5f4275e1 | 8820 | else |
8821 | declare | |
8822 | Mrk : constant Uintp.Save_Mark := Mark; | |
8823 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 8824 | |
5f4275e1 | 8825 | Ev1 : Uint; |
8826 | Ev2 : Uint; | |
8827 | Ev3 : Uint; | |
8828 | Ev4 : Uint; | |
2fe22c69 | 8829 | |
5f4275e1 | 8830 | begin |
8831 | -- Discard extreme values of zero for the divisor, since | |
8832 | -- they will simply result in an exception in any case. | |
2fe22c69 | 8833 | |
5f4275e1 | 8834 | if Rlo = 0 then |
8835 | Rlo := Uint_1; | |
8836 | elsif Rhi = 0 then | |
8837 | Rhi := -Uint_1; | |
2fe22c69 | 8838 | end if; |
2fe22c69 | 8839 | |
5f4275e1 | 8840 | -- Compute possible bounds coming from dividing end |
8841 | -- values of the input ranges. | |
2fe22c69 | 8842 | |
5f4275e1 | 8843 | Ev1 := Llo / Rlo; |
8844 | Ev2 := Llo / Rhi; | |
8845 | Ev3 := Lhi / Rlo; | |
8846 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 8847 | |
5f4275e1 | 8848 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
8849 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 8850 | |
5f4275e1 | 8851 | -- If the right operand can be both negative or positive, |
8852 | -- include the end values of the left operand in the | |
8853 | -- extreme values, as well as their negation. | |
2fe22c69 | 8854 | |
5f4275e1 | 8855 | if Rlo < 0 and then Rhi > 0 then |
8856 | Ev1 := Llo; | |
8857 | Ev2 := -Llo; | |
8858 | Ev3 := Lhi; | |
8859 | Ev4 := -Lhi; | |
2fe22c69 | 8860 | |
5f4275e1 | 8861 | Min (Lo, |
8862 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
8863 | Max (Hi, | |
8864 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 8865 | end if; |
2fe22c69 | 8866 | |
5f4275e1 | 8867 | -- Release the RR and Ev values |
2fe22c69 | 8868 | |
5f4275e1 | 8869 | Release_And_Save (Mrk, Lo, Hi); |
8870 | end; | |
8871 | end if; | |
3cce7f32 | 8872 | |
8873 | -- Exponentiation | |
8874 | ||
8875 | when N_Op_Expon => | |
de922300 | 8876 | |
8877 | -- Discard negative values for the exponent, since they will | |
8878 | -- simply result in an exception in any case. | |
8879 | ||
8880 | if Rhi < 0 then | |
8881 | Rhi := Uint_0; | |
8882 | elsif Rlo < 0 then | |
8883 | Rlo := Uint_0; | |
8884 | end if; | |
8885 | ||
8886 | -- Estimate number of bits in result before we go computing | |
8887 | -- giant useless bounds. Basically the number of bits in the | |
8888 | -- result is the number of bits in the base multiplied by the | |
8889 | -- value of the exponent. If this is big enough that the result | |
8890 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
8891 | -- mode immediately, and avoid computing giant bounds. | |
8892 | ||
8893 | -- The comparison here is approximate, but conservative, it | |
8894 | -- only clicks on cases that are sure to exceed the bounds. | |
8895 | ||
8896 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
8897 | Lo := No_Uint; | |
8898 | Hi := No_Uint; | |
8899 | ||
8900 | -- If right operand is zero then result is 1 | |
8901 | ||
8902 | elsif Rhi = 0 then | |
8903 | Lo := Uint_1; | |
8904 | Hi := Uint_1; | |
8905 | ||
8906 | else | |
8907 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 8908 | -- positive value to largest exponent value, or from |
8909 | -- the exponentiation of most negative value to an | |
8910 | -- even exponent. | |
de922300 | 8911 | |
8912 | declare | |
8913 | Hi1, Hi2 : Uint; | |
8914 | ||
8915 | begin | |
5f4275e1 | 8916 | if Lhi > 0 then |
de922300 | 8917 | Hi1 := Lhi ** Rhi; |
8918 | else | |
8919 | Hi1 := Uint_0; | |
8920 | end if; | |
8921 | ||
8922 | if Llo < 0 then | |
8923 | if Rhi mod 2 = 0 then | |
de922300 | 8924 | Hi2 := Llo ** Rhi; |
5f4275e1 | 8925 | else |
8926 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 8927 | end if; |
8928 | else | |
8929 | Hi2 := Uint_0; | |
8930 | end if; | |
8931 | ||
8932 | Hi := UI_Max (Hi1, Hi2); | |
8933 | end; | |
8934 | ||
8935 | -- Result can only be negative if base can be negative | |
8936 | ||
8937 | if Llo < 0 then | |
21a55437 | 8938 | if Rhi mod 2 = 0 then |
de922300 | 8939 | Lo := Llo ** (Rhi - 1); |
8940 | else | |
8941 | Lo := Llo ** Rhi; | |
8942 | end if; | |
8943 | ||
21a55437 | 8944 | -- Otherwise low bound is minimum ** minimum |
de922300 | 8945 | |
8946 | else | |
8947 | Lo := Llo ** Rlo; | |
8948 | end if; | |
8949 | end if; | |
3cce7f32 | 8950 | |
8951 | -- Negation | |
8952 | ||
8953 | when N_Op_Minus => | |
8954 | Lo := -Rhi; | |
8955 | Hi := -Rlo; | |
8956 | ||
8957 | -- Mod | |
8958 | ||
8959 | when N_Op_Mod => | |
2fe22c69 | 8960 | declare |
5f4275e1 | 8961 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 8962 | -- This is the maximum absolute value of the result |
8963 | ||
8964 | begin | |
8965 | Lo := Uint_0; | |
8966 | Hi := Uint_0; | |
8967 | ||
8968 | -- The result depends only on the sign and magnitude of | |
8969 | -- the right operand, it does not depend on the sign or | |
8970 | -- magnitude of the left operand. | |
8971 | ||
8972 | if Rlo < 0 then | |
8973 | Lo := -Maxabs; | |
8974 | end if; | |
8975 | ||
8976 | if Rhi > 0 then | |
8977 | Hi := Maxabs; | |
8978 | end if; | |
8979 | end; | |
3cce7f32 | 8980 | |
8981 | -- Multiplication | |
8982 | ||
8983 | when N_Op_Multiply => | |
49b3a812 | 8984 | |
8985 | -- Possible bounds of multiplication must come from multiplying | |
8986 | -- end values of the input ranges (four possibilities). | |
8987 | ||
8988 | declare | |
8989 | Mrk : constant Uintp.Save_Mark := Mark; | |
8990 | -- Mark so we can release the Ev values | |
8991 | ||
8992 | Ev1 : constant Uint := Llo * Rlo; | |
8993 | Ev2 : constant Uint := Llo * Rhi; | |
8994 | Ev3 : constant Uint := Lhi * Rlo; | |
8995 | Ev4 : constant Uint := Lhi * Rhi; | |
8996 | ||
8997 | begin | |
8998 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
8999 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
9000 | ||
9001 | -- Release the Ev values | |
9002 | ||
9003 | Release_And_Save (Mrk, Lo, Hi); | |
9004 | end; | |
3cce7f32 | 9005 | |
9006 | -- Plus operator (affirmation) | |
9007 | ||
9008 | when N_Op_Plus => | |
9009 | Lo := Rlo; | |
9010 | Hi := Rhi; | |
9011 | ||
9012 | -- Remainder | |
9013 | ||
9014 | when N_Op_Rem => | |
2fe22c69 | 9015 | declare |
5f4275e1 | 9016 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 9017 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 9018 | -- that the result range does not depend on the sign of the |
9019 | -- right operand. | |
2fe22c69 | 9020 | |
9021 | begin | |
9022 | Lo := Uint_0; | |
9023 | Hi := Uint_0; | |
9024 | ||
9025 | -- Case of left operand negative, which results in a range | |
9026 | -- of -Maxabs .. 0 for those negative values. If there are | |
9027 | -- no negative values then Lo value of result is always 0. | |
9028 | ||
9029 | if Llo < 0 then | |
9030 | Lo := -Maxabs; | |
9031 | end if; | |
9032 | ||
9033 | -- Case of left operand positive | |
9034 | ||
9035 | if Lhi > 0 then | |
9036 | Hi := Maxabs; | |
9037 | end if; | |
9038 | end; | |
3cce7f32 | 9039 | |
9040 | -- Subtract | |
9041 | ||
9042 | when N_Op_Subtract => | |
9043 | Lo := Llo - Rhi; | |
9044 | Hi := Lhi - Rlo; | |
9045 | ||
9046 | -- Nothing else should be possible | |
9047 | ||
9048 | when others => | |
9049 | raise Program_Error; | |
3cce7f32 | 9050 | end case; |
9051 | end if; | |
9052 | ||
4fb5f0a0 | 9053 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 9054 | -- operands or long long integer operands), and we know the result. |
9055 | -- If we know we are in the result range, and we do not have Bignum | |
9056 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
9057 | -- overflow checks turned off (since we know we cannot have overflow). | |
9058 | -- As always the reexpansion is required to complete expansion of the | |
9059 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
9060 | -- by suppressing the check. | |
f32c377d | 9061 | |
9062 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
9063 | and then In_Result_Range | |
9064 | then | |
9065 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 9066 | Reexpand (Suppress => True); |
f32c377d | 9067 | return; |
9068 | ||
9069 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 9070 | -- case we will move into either the Bignum or Long_Long_Integer domain |
9071 | -- to compute the result. However, there is one exception. If we are | |
9072 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
9073 | -- operands, we will have to immediately convert the result back to | |
9074 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
9075 | -- fiddling. | |
f32c377d | 9076 | |
9077 | elsif Top_Level | |
9078 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 9079 | |
9080 | -- One further refinement. If we are at the top level, but our parent | |
9081 | -- is a type conversion, then go into bignum or long long integer node | |
9082 | -- since the result will be converted to that type directly without | |
9083 | -- going through the result type, and we may avoid an overflow. This | |
9084 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
9085 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
9086 | -- but does not fit in Integer. | |
9087 | ||
9088 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 9089 | then |
0df9d43f | 9090 | -- Here keep original types, but we need to complete analysis |
f32c377d | 9091 | |
9092 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 9093 | -- here because it will cause recursion into the whole MINIMIZED/ |
9094 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 9095 | -- we are at the top level, and we need a check against the result |
39a0c1d3 | 9096 | -- mode (i.e. we want to use STRICT mode). So do exactly that. |
4fb5f0a0 | 9097 | -- Also, we have not modified the node, so this is a case where |
9098 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 9099 | |
0df9d43f | 9100 | Reexpand; |
f32c377d | 9101 | return; |
9102 | ||
9103 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 9104 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 9105 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
9106 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 9107 | |
9108 | -- Note: we could do better here and in some cases switch back from | |
9109 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
9110 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
9111 | -- Failing to do this switching back is only an efficiency issue. | |
9112 | ||
f32c377d | 9113 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 9114 | |
61016a7a | 9115 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 9116 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 9117 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
9118 | ||
9119 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
9120 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
9121 | -- into Bignum mode, but there is an exception if neither of our | |
9122 | -- operands is Bignum now, and we are at the top level (Top_Level | |
9123 | -- set True). In this case, there is no point in moving into Bignum | |
9124 | -- mode to prevent overflow if the caller will immediately convert | |
9125 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 9126 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 9127 | |
9128 | if Check_Mode = Minimized | |
9129 | or else (Top_Level and not Bignum_Operands) | |
9130 | then | |
0df9d43f | 9131 | if Do_Overflow_Check (N) then |
9132 | Enable_Overflow_Check (N); | |
9133 | end if; | |
3cce7f32 | 9134 | |
0df9d43f | 9135 | -- The result now has to be in Long_Long_Integer mode, so adjust |
9136 | -- the possible range to reflect this. Note these calls also | |
9137 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 9138 | |
9139 | Max (Lo, LLLo); | |
9140 | Min (Hi, LLHi); | |
9141 | ||
9142 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 9143 | |
9144 | else | |
9145 | pragma Assert (Check_Mode = Eliminated); | |
9146 | ||
9147 | declare | |
9148 | Fent : Entity_Id; | |
9149 | Args : List_Id; | |
9150 | ||
9151 | begin | |
9152 | case Nkind (N) is | |
99378362 | 9153 | when N_Op_Abs => |
3cce7f32 | 9154 | Fent := RTE (RE_Big_Abs); |
9155 | ||
99378362 | 9156 | when N_Op_Add => |
3cce7f32 | 9157 | Fent := RTE (RE_Big_Add); |
9158 | ||
99378362 | 9159 | when N_Op_Divide => |
3cce7f32 | 9160 | Fent := RTE (RE_Big_Div); |
9161 | ||
99378362 | 9162 | when N_Op_Expon => |
3cce7f32 | 9163 | Fent := RTE (RE_Big_Exp); |
9164 | ||
99378362 | 9165 | when N_Op_Minus => |
3cce7f32 | 9166 | Fent := RTE (RE_Big_Neg); |
9167 | ||
99378362 | 9168 | when N_Op_Mod => |
3cce7f32 | 9169 | Fent := RTE (RE_Big_Mod); |
9170 | ||
9171 | when N_Op_Multiply => | |
9172 | Fent := RTE (RE_Big_Mul); | |
9173 | ||
99378362 | 9174 | when N_Op_Rem => |
3cce7f32 | 9175 | Fent := RTE (RE_Big_Rem); |
9176 | ||
9177 | when N_Op_Subtract => | |
9178 | Fent := RTE (RE_Big_Sub); | |
9179 | ||
9180 | -- Anything else is an internal error, this includes the | |
9181 | -- N_Op_Plus case, since how can plus cause the result | |
9182 | -- to be out of range if the operand is in range? | |
9183 | ||
9184 | when others => | |
9185 | raise Program_Error; | |
9186 | end case; | |
9187 | ||
9188 | -- Construct argument list for Bignum call, converting our | |
9189 | -- operands to Bignum form if they are not already there. | |
9190 | ||
9191 | Args := New_List; | |
9192 | ||
9193 | if Binary then | |
9194 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
9195 | end if; | |
9196 | ||
9197 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
9198 | ||
9199 | -- Now rewrite the arithmetic operator with a call to the | |
9200 | -- corresponding bignum function. | |
9201 | ||
9202 | Rewrite (N, | |
9203 | Make_Function_Call (Loc, | |
9204 | Name => New_Occurrence_Of (Fent, Loc), | |
9205 | Parameter_Associations => Args)); | |
0df9d43f | 9206 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 9207 | |
9208 | -- Indicate result is Bignum mode | |
9209 | ||
9210 | Lo := No_Uint; | |
9211 | Hi := No_Uint; | |
de922300 | 9212 | return; |
3cce7f32 | 9213 | end; |
9214 | end if; | |
9215 | ||
9216 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 9217 | -- check is required, at least not yet. |
3cce7f32 | 9218 | |
9219 | else | |
de922300 | 9220 | Set_Do_Overflow_Check (N, False); |
9221 | end if; | |
3cce7f32 | 9222 | |
f32c377d | 9223 | -- Here we are not in Bignum territory, but we may have long long |
9224 | -- integer operands that need special handling. First a special check: | |
9225 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
9226 | -- it means we converted it to prevent overflow, but exponentiation | |
9227 | -- requires a Natural right operand, so convert it back to Natural. | |
9228 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 9229 | |
f32c377d | 9230 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
9231 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 9232 | end if; |
9233 | ||
de922300 | 9234 | -- Here we will do the operation in Long_Long_Integer. We do this even |
9235 | -- if we know an overflow check is required, better to do this in long | |
39a0c1d3 | 9236 | -- long integer mode, since we are less likely to overflow. |
3cce7f32 | 9237 | |
de922300 | 9238 | -- Convert right or only operand to Long_Long_Integer, except that |
9239 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 9240 | |
de922300 | 9241 | if Nkind (N) /= N_Op_Expon then |
9242 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
9243 | end if; | |
3cce7f32 | 9244 | |
de922300 | 9245 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 9246 | |
de922300 | 9247 | if Binary then |
9248 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
9249 | end if; | |
9250 | ||
9251 | -- Reset node to unanalyzed | |
9252 | ||
9253 | Set_Analyzed (N, False); | |
9254 | Set_Etype (N, Empty); | |
9255 | Set_Entity (N, Empty); | |
9256 | ||
2fe22c69 | 9257 | -- Now analyze this new node. This reanalysis will complete processing |
9258 | -- for the node. In particular we will complete the expansion of an | |
9259 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
9260 | -- we will complete any division checks (since we have not changed the | |
9261 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 9262 | |
0df9d43f | 9263 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
39a0c1d3 | 9264 | -- MINIMIZED/ELIMINATED handling, since we are now done with that. |
3cce7f32 | 9265 | |
0df9d43f | 9266 | declare |
db415383 | 9267 | SG : constant Overflow_Mode_Type := |
9268 | Scope_Suppress.Overflow_Mode_General; | |
9269 | SA : constant Overflow_Mode_Type := | |
9270 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 9271 | |
0df9d43f | 9272 | begin |
db415383 | 9273 | Scope_Suppress.Overflow_Mode_General := Strict; |
9274 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 9275 | |
0df9d43f | 9276 | if not Do_Overflow_Check (N) then |
9277 | Reanalyze (LLIB, Suppress => True); | |
9278 | else | |
9279 | Reanalyze (LLIB); | |
9280 | end if; | |
9281 | ||
db415383 | 9282 | Scope_Suppress.Overflow_Mode_General := SG; |
9283 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 9284 | end; |
9285 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 9286 | |
9287 | ------------------------- | |
9288 | -- Overflow_Check_Mode -- | |
9289 | ------------------------- | |
9290 | ||
db415383 | 9291 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 9292 | begin |
724d2bd8 | 9293 | if In_Assertion_Expr = 0 then |
db415383 | 9294 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 9295 | else |
db415383 | 9296 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 9297 | end if; |
3cce7f32 | 9298 | end Overflow_Check_Mode; |
9299 | ||
9300 | -------------------------------- | |
9301 | -- Overflow_Checks_Suppressed -- | |
9302 | -------------------------------- | |
9303 | ||
9304 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9305 | begin | |
0df9d43f | 9306 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9307 | return Is_Check_Suppressed (E, Overflow_Check); | |
9308 | else | |
9309 | return Scope_Suppress.Suppress (Overflow_Check); | |
9310 | end if; | |
ee6ba406 | 9311 | end Overflow_Checks_Suppressed; |
fc75802a | 9312 | |
37baba83 | 9313 | --------------------------------- |
9314 | -- Predicate_Checks_Suppressed -- | |
9315 | --------------------------------- | |
9316 | ||
9317 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9318 | begin | |
9319 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9320 | return Is_Check_Suppressed (E, Predicate_Check); | |
9321 | else | |
9322 | return Scope_Suppress.Suppress (Predicate_Check); | |
9323 | end if; | |
9324 | end Predicate_Checks_Suppressed; | |
9325 | ||
ee6ba406 | 9326 | ----------------------------- |
9327 | -- Range_Checks_Suppressed -- | |
9328 | ----------------------------- | |
9329 | ||
9330 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9331 | begin | |
9dfe12ae | 9332 | if Present (E) then |
ce4d14c4 | 9333 | if Kill_Range_Checks (E) then |
9dfe12ae | 9334 | return True; |
5f46de53 | 9335 | |
9dfe12ae | 9336 | elsif Checks_May_Be_Suppressed (E) then |
9337 | return Is_Check_Suppressed (E, Range_Check); | |
9338 | end if; | |
9339 | end if; | |
ee6ba406 | 9340 | |
fafc6b97 | 9341 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 9342 | end Range_Checks_Suppressed; |
9343 | ||
0577b0b1 | 9344 | ----------------------------------------- |
9345 | -- Range_Or_Validity_Checks_Suppressed -- | |
9346 | ----------------------------------------- | |
9347 | ||
9348 | -- Note: the coding would be simpler here if we simply made appropriate | |
9349 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
9350 | -- duplicated checks which we prefer to avoid. | |
9351 | ||
9352 | function Range_Or_Validity_Checks_Suppressed | |
9353 | (Expr : Node_Id) return Boolean | |
9354 | is | |
9355 | begin | |
9356 | -- Immediate return if scope checks suppressed for either check | |
9357 | ||
fafc6b97 | 9358 | if Scope_Suppress.Suppress (Range_Check) |
9359 | or | |
9360 | Scope_Suppress.Suppress (Validity_Check) | |
9361 | then | |
0577b0b1 | 9362 | return True; |
9363 | end if; | |
9364 | ||
9365 | -- If no expression, that's odd, decide that checks are suppressed, | |
9366 | -- since we don't want anyone trying to do checks in this case, which | |
9367 | -- is most likely the result of some other error. | |
9368 | ||
9369 | if No (Expr) then | |
9370 | return True; | |
9371 | end if; | |
9372 | ||
9373 | -- Expression is present, so perform suppress checks on type | |
9374 | ||
9375 | declare | |
9376 | Typ : constant Entity_Id := Etype (Expr); | |
9377 | begin | |
ce4d14c4 | 9378 | if Checks_May_Be_Suppressed (Typ) |
0577b0b1 | 9379 | and then (Is_Check_Suppressed (Typ, Range_Check) |
9380 | or else | |
9381 | Is_Check_Suppressed (Typ, Validity_Check)) | |
9382 | then | |
9383 | return True; | |
9384 | end if; | |
9385 | end; | |
9386 | ||
9387 | -- If expression is an entity name, perform checks on this entity | |
9388 | ||
9389 | if Is_Entity_Name (Expr) then | |
9390 | declare | |
9391 | Ent : constant Entity_Id := Entity (Expr); | |
9392 | begin | |
9393 | if Checks_May_Be_Suppressed (Ent) then | |
9394 | return Is_Check_Suppressed (Ent, Range_Check) | |
9395 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
9396 | end if; | |
9397 | end; | |
9398 | end if; | |
9399 | ||
9400 | -- If we fall through, no checks suppressed | |
9401 | ||
9402 | return False; | |
9403 | end Range_Or_Validity_Checks_Suppressed; | |
9404 | ||
226494a3 | 9405 | ------------------- |
9406 | -- Remove_Checks -- | |
9407 | ------------------- | |
9408 | ||
9409 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 9410 | function Process (N : Node_Id) return Traverse_Result; |
9411 | -- Process a single node during the traversal | |
9412 | ||
8f6e4fd5 | 9413 | procedure Traverse is new Traverse_Proc (Process); |
9414 | -- The traversal procedure itself | |
226494a3 | 9415 | |
9416 | ------------- | |
9417 | -- Process -- | |
9418 | ------------- | |
9419 | ||
9420 | function Process (N : Node_Id) return Traverse_Result is | |
9421 | begin | |
9422 | if Nkind (N) not in N_Subexpr then | |
9423 | return Skip; | |
9424 | end if; | |
9425 | ||
9426 | Set_Do_Range_Check (N, False); | |
9427 | ||
9428 | case Nkind (N) is | |
9429 | when N_And_Then => | |
8f6e4fd5 | 9430 | Traverse (Left_Opnd (N)); |
226494a3 | 9431 | return Skip; |
9432 | ||
9433 | when N_Attribute_Reference => | |
226494a3 | 9434 | Set_Do_Overflow_Check (N, False); |
9435 | ||
226494a3 | 9436 | when N_Function_Call => |
9437 | Set_Do_Tag_Check (N, False); | |
9438 | ||
226494a3 | 9439 | when N_Op => |
9440 | Set_Do_Overflow_Check (N, False); | |
9441 | ||
9442 | case Nkind (N) is | |
9443 | when N_Op_Divide => | |
9444 | Set_Do_Division_Check (N, False); | |
9445 | ||
9446 | when N_Op_And => | |
9447 | Set_Do_Length_Check (N, False); | |
9448 | ||
9449 | when N_Op_Mod => | |
9450 | Set_Do_Division_Check (N, False); | |
9451 | ||
9452 | when N_Op_Or => | |
9453 | Set_Do_Length_Check (N, False); | |
9454 | ||
9455 | when N_Op_Rem => | |
9456 | Set_Do_Division_Check (N, False); | |
9457 | ||
9458 | when N_Op_Xor => | |
9459 | Set_Do_Length_Check (N, False); | |
9460 | ||
9461 | when others => | |
9462 | null; | |
9463 | end case; | |
9464 | ||
9465 | when N_Or_Else => | |
8f6e4fd5 | 9466 | Traverse (Left_Opnd (N)); |
226494a3 | 9467 | return Skip; |
9468 | ||
9469 | when N_Selected_Component => | |
226494a3 | 9470 | Set_Do_Discriminant_Check (N, False); |
9471 | ||
226494a3 | 9472 | when N_Type_Conversion => |
9dfe12ae | 9473 | Set_Do_Length_Check (N, False); |
9474 | Set_Do_Tag_Check (N, False); | |
226494a3 | 9475 | Set_Do_Overflow_Check (N, False); |
226494a3 | 9476 | |
9477 | when others => | |
9478 | null; | |
9479 | end case; | |
9480 | ||
9481 | return OK; | |
9482 | end Process; | |
9483 | ||
9484 | -- Start of processing for Remove_Checks | |
9485 | ||
9486 | begin | |
8f6e4fd5 | 9487 | Traverse (Expr); |
226494a3 | 9488 | end Remove_Checks; |
9489 | ||
ee6ba406 | 9490 | ---------------------------- |
9491 | -- Selected_Length_Checks -- | |
9492 | ---------------------------- | |
9493 | ||
9494 | function Selected_Length_Checks | |
9495 | (Ck_Node : Node_Id; | |
9496 | Target_Typ : Entity_Id; | |
9497 | Source_Typ : Entity_Id; | |
314a23b6 | 9498 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 9499 | is |
9500 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
9501 | S_Typ : Entity_Id; | |
9502 | T_Typ : Entity_Id; | |
9503 | Expr_Actual : Node_Id; | |
9504 | Exptyp : Entity_Id; | |
9505 | Cond : Node_Id := Empty; | |
9506 | Do_Access : Boolean := False; | |
9507 | Wnode : Node_Id := Warn_Node; | |
9508 | Ret_Result : Check_Result := (Empty, Empty); | |
9509 | Num_Checks : Natural := 0; | |
9510 | ||
9511 | procedure Add_Check (N : Node_Id); | |
9512 | -- Adds the action given to Ret_Result if N is non-Empty | |
9513 | ||
9514 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
9515 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 9516 | -- Comments required ??? |
ee6ba406 | 9517 | |
9518 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
9519 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 9520 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 9521 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 9522 | -- obviously superfluous checks. |
ee6ba406 | 9523 | |
9524 | function Length_E_Cond | |
9525 | (Exptyp : Entity_Id; | |
9526 | Typ : Entity_Id; | |
314a23b6 | 9527 | Indx : Nat) return Node_Id; |
ee6ba406 | 9528 | -- Returns expression to compute: |
9529 | -- Typ'Length /= Exptyp'Length | |
9530 | ||
9531 | function Length_N_Cond | |
9532 | (Expr : Node_Id; | |
9533 | Typ : Entity_Id; | |
314a23b6 | 9534 | Indx : Nat) return Node_Id; |
ee6ba406 | 9535 | -- Returns expression to compute: |
9536 | -- Typ'Length /= Expr'Length | |
9537 | ||
9538 | --------------- | |
9539 | -- Add_Check -- | |
9540 | --------------- | |
9541 | ||
9542 | procedure Add_Check (N : Node_Id) is | |
9543 | begin | |
9544 | if Present (N) then | |
9545 | ||
20cf157b | 9546 | -- For now, ignore attempt to place more than two checks ??? |
9547 | -- This is really worrisome, are we really discarding checks ??? | |
ee6ba406 | 9548 | |
9549 | if Num_Checks = 2 then | |
9550 | return; | |
9551 | end if; | |
9552 | ||
9553 | pragma Assert (Num_Checks <= 1); | |
9554 | Num_Checks := Num_Checks + 1; | |
9555 | Ret_Result (Num_Checks) := N; | |
9556 | end if; | |
9557 | end Add_Check; | |
9558 | ||
9559 | ------------------ | |
9560 | -- Get_E_Length -- | |
9561 | ------------------ | |
9562 | ||
9563 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 9564 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 9565 | N : Node_Id; |
9566 | E1 : Entity_Id := E; | |
ee6ba406 | 9567 | |
9568 | begin | |
9569 | if Ekind (Scope (E)) = E_Record_Type | |
9570 | and then Has_Discriminants (Scope (E)) | |
9571 | then | |
9572 | N := Build_Discriminal_Subtype_Of_Component (E); | |
9573 | ||
9574 | if Present (N) then | |
9575 | Insert_Action (Ck_Node, N); | |
9576 | E1 := Defining_Identifier (N); | |
9577 | end if; | |
9578 | end if; | |
9579 | ||
9580 | if Ekind (E1) = E_String_Literal_Subtype then | |
9581 | return | |
9582 | Make_Integer_Literal (Loc, | |
9583 | Intval => String_Literal_Length (E1)); | |
9584 | ||
00c403ee | 9585 | elsif SE /= Standard_Standard |
9586 | and then Ekind (Scope (SE)) = E_Protected_Type | |
9587 | and then Has_Discriminants (Scope (SE)) | |
9588 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 9589 | and then not Inside_Init_Proc |
9590 | then | |
ee6ba406 | 9591 | -- If the type whose length is needed is a private component |
9592 | -- constrained by a discriminant, we must expand the 'Length | |
9593 | -- attribute into an explicit computation, using the discriminal | |
9594 | -- of the current protected operation. This is because the actual | |
9595 | -- type of the prival is constructed after the protected opera- | |
9596 | -- tion has been fully expanded. | |
9597 | ||
9598 | declare | |
9599 | Indx_Type : Node_Id; | |
9600 | Lo : Node_Id; | |
9601 | Hi : Node_Id; | |
9602 | Do_Expand : Boolean := False; | |
9603 | ||
9604 | begin | |
9605 | Indx_Type := First_Index (E); | |
9606 | ||
9607 | for J in 1 .. Indx - 1 loop | |
9608 | Next_Index (Indx_Type); | |
9609 | end loop; | |
9610 | ||
2af58f67 | 9611 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 9612 | |
9613 | if Nkind (Lo) = N_Identifier | |
9614 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
9615 | then | |
9616 | Lo := Get_Discriminal (E, Lo); | |
9617 | Do_Expand := True; | |
9618 | end if; | |
9619 | ||
9620 | if Nkind (Hi) = N_Identifier | |
9621 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
9622 | then | |
9623 | Hi := Get_Discriminal (E, Hi); | |
9624 | Do_Expand := True; | |
9625 | end if; | |
9626 | ||
9627 | if Do_Expand then | |
9628 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 9629 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 9630 | end if; |
9631 | ||
9632 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 9633 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 9634 | end if; |
9635 | ||
9636 | N := | |
9637 | Make_Op_Add (Loc, | |
9638 | Left_Opnd => | |
9639 | Make_Op_Subtract (Loc, | |
9640 | Left_Opnd => Hi, | |
9641 | Right_Opnd => Lo), | |
9642 | ||
9643 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
9644 | return N; | |
9645 | ||
9646 | else | |
9647 | N := | |
9648 | Make_Attribute_Reference (Loc, | |
9649 | Attribute_Name => Name_Length, | |
9650 | Prefix => | |
9651 | New_Occurrence_Of (E1, Loc)); | |
9652 | ||
9653 | if Indx > 1 then | |
9654 | Set_Expressions (N, New_List ( | |
9655 | Make_Integer_Literal (Loc, Indx))); | |
9656 | end if; | |
9657 | ||
9658 | return N; | |
9659 | end if; | |
9660 | end; | |
9661 | ||
9662 | else | |
9663 | N := | |
9664 | Make_Attribute_Reference (Loc, | |
9665 | Attribute_Name => Name_Length, | |
9666 | Prefix => | |
9667 | New_Occurrence_Of (E1, Loc)); | |
9668 | ||
9669 | if Indx > 1 then | |
9670 | Set_Expressions (N, New_List ( | |
9671 | Make_Integer_Literal (Loc, Indx))); | |
9672 | end if; | |
9673 | ||
9674 | return N; | |
ee6ba406 | 9675 | end if; |
9676 | end Get_E_Length; | |
9677 | ||
9678 | ------------------ | |
9679 | -- Get_N_Length -- | |
9680 | ------------------ | |
9681 | ||
9682 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
9683 | begin | |
9684 | return | |
9685 | Make_Attribute_Reference (Loc, | |
9686 | Attribute_Name => Name_Length, | |
9687 | Prefix => | |
9dfe12ae | 9688 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 9689 | Expressions => New_List ( |
9690 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 9691 | end Get_N_Length; |
9692 | ||
9693 | ------------------- | |
9694 | -- Length_E_Cond -- | |
9695 | ------------------- | |
9696 | ||
9697 | function Length_E_Cond | |
9698 | (Exptyp : Entity_Id; | |
9699 | Typ : Entity_Id; | |
314a23b6 | 9700 | Indx : Nat) return Node_Id |
ee6ba406 | 9701 | is |
9702 | begin | |
9703 | return | |
9704 | Make_Op_Ne (Loc, | |
9705 | Left_Opnd => Get_E_Length (Typ, Indx), | |
9706 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 9707 | end Length_E_Cond; |
9708 | ||
9709 | ------------------- | |
9710 | -- Length_N_Cond -- | |
9711 | ------------------- | |
9712 | ||
9713 | function Length_N_Cond | |
9714 | (Expr : Node_Id; | |
9715 | Typ : Entity_Id; | |
314a23b6 | 9716 | Indx : Nat) return Node_Id |
ee6ba406 | 9717 | is |
9718 | begin | |
9719 | return | |
9720 | Make_Op_Ne (Loc, | |
9721 | Left_Opnd => Get_E_Length (Typ, Indx), | |
9722 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 9723 | end Length_N_Cond; |
9724 | ||
feff2f05 | 9725 | ----------------- |
9726 | -- Same_Bounds -- | |
9727 | ----------------- | |
9728 | ||
ee6ba406 | 9729 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
9730 | begin | |
9731 | return | |
9732 | (Nkind (L) = N_Integer_Literal | |
9733 | and then Nkind (R) = N_Integer_Literal | |
9734 | and then Intval (L) = Intval (R)) | |
9735 | ||
9736 | or else | |
9737 | (Is_Entity_Name (L) | |
9738 | and then Ekind (Entity (L)) = E_Constant | |
9739 | and then ((Is_Entity_Name (R) | |
9740 | and then Entity (L) = Entity (R)) | |
9741 | or else | |
9742 | (Nkind (R) = N_Type_Conversion | |
9743 | and then Is_Entity_Name (Expression (R)) | |
9744 | and then Entity (L) = Entity (Expression (R))))) | |
9745 | ||
9746 | or else | |
9747 | (Is_Entity_Name (R) | |
9748 | and then Ekind (Entity (R)) = E_Constant | |
9749 | and then Nkind (L) = N_Type_Conversion | |
9750 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 9751 | and then Entity (R) = Entity (Expression (L))) |
9752 | ||
9753 | or else | |
9754 | (Is_Entity_Name (L) | |
9755 | and then Is_Entity_Name (R) | |
9756 | and then Entity (L) = Entity (R) | |
9757 | and then Ekind (Entity (L)) = E_In_Parameter | |
9758 | and then Inside_Init_Proc); | |
ee6ba406 | 9759 | end Same_Bounds; |
9760 | ||
9761 | -- Start of processing for Selected_Length_Checks | |
9762 | ||
9763 | begin | |
18cb6d78 | 9764 | -- Checks will be applied only when generating code |
f0d65dae | 9765 | |
18cb6d78 | 9766 | if not Expander_Active then |
ee6ba406 | 9767 | return Ret_Result; |
9768 | end if; | |
9769 | ||
9770 | if Target_Typ = Any_Type | |
9771 | or else Target_Typ = Any_Composite | |
9772 | or else Raises_Constraint_Error (Ck_Node) | |
9773 | then | |
9774 | return Ret_Result; | |
9775 | end if; | |
9776 | ||
9777 | if No (Wnode) then | |
9778 | Wnode := Ck_Node; | |
9779 | end if; | |
9780 | ||
9781 | T_Typ := Target_Typ; | |
9782 | ||
9783 | if No (Source_Typ) then | |
9784 | S_Typ := Etype (Ck_Node); | |
9785 | else | |
9786 | S_Typ := Source_Typ; | |
9787 | end if; | |
9788 | ||
9789 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
9790 | return Ret_Result; | |
9791 | end if; | |
9792 | ||
9793 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
9794 | S_Typ := Designated_Type (S_Typ); | |
9795 | T_Typ := Designated_Type (T_Typ); | |
9796 | Do_Access := True; | |
9797 | ||
2af58f67 | 9798 | -- A simple optimization for the null case |
ee6ba406 | 9799 | |
2af58f67 | 9800 | if Known_Null (Ck_Node) then |
ee6ba406 | 9801 | return Ret_Result; |
9802 | end if; | |
9803 | end if; | |
9804 | ||
9805 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9806 | if Is_Constrained (T_Typ) then | |
9807 | ||
92f1631f | 9808 | -- The checking code to be generated will freeze the corresponding |
9809 | -- array type. However, we must freeze the type now, so that the | |
9810 | -- freeze node does not appear within the generated if expression, | |
9811 | -- but ahead of it. | |
ee6ba406 | 9812 | |
9813 | Freeze_Before (Ck_Node, T_Typ); | |
9814 | ||
9815 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 9816 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 9817 | |
9818 | if Is_Access_Type (Exptyp) then | |
9819 | Exptyp := Designated_Type (Exptyp); | |
9820 | end if; | |
9821 | ||
9822 | -- String_Literal case. This needs to be handled specially be- | |
9823 | -- cause no index types are available for string literals. The | |
9824 | -- condition is simply: | |
9825 | ||
9826 | -- T_Typ'Length = string-literal-length | |
9827 | ||
9dfe12ae | 9828 | if Nkind (Expr_Actual) = N_String_Literal |
9829 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
9830 | then | |
ee6ba406 | 9831 | Cond := |
9832 | Make_Op_Ne (Loc, | |
9833 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
9834 | Right_Opnd => | |
9835 | Make_Integer_Literal (Loc, | |
9836 | Intval => | |
9837 | String_Literal_Length (Etype (Expr_Actual)))); | |
9838 | ||
9839 | -- General array case. Here we have a usable actual subtype for | |
9840 | -- the expression, and the condition is built from the two types | |
9841 | -- (Do_Length): | |
9842 | ||
9843 | -- T_Typ'Length /= Exptyp'Length or else | |
9844 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
9845 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
9846 | -- ... | |
9847 | ||
9848 | elsif Is_Constrained (Exptyp) then | |
9849 | declare | |
9dfe12ae | 9850 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9851 | ||
9852 | L_Index : Node_Id; | |
9853 | R_Index : Node_Id; | |
9854 | L_Low : Node_Id; | |
9855 | L_High : Node_Id; | |
9856 | R_Low : Node_Id; | |
9857 | R_High : Node_Id; | |
ee6ba406 | 9858 | L_Length : Uint; |
9859 | R_Length : Uint; | |
9dfe12ae | 9860 | Ref_Node : Node_Id; |
ee6ba406 | 9861 | |
9862 | begin | |
feff2f05 | 9863 | -- At the library level, we need to ensure that the type of |
9864 | -- the object is elaborated before the check itself is | |
9865 | -- emitted. This is only done if the object is in the | |
9866 | -- current compilation unit, otherwise the type is frozen | |
9867 | -- and elaborated in its unit. | |
9dfe12ae | 9868 | |
9869 | if Is_Itype (Exptyp) | |
9870 | and then | |
9871 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
9872 | and then | |
9873 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 9874 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 9875 | then |
9876 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
9877 | Set_Itype (Ref_Node, Exptyp); | |
9878 | Insert_Action (Ck_Node, Ref_Node); | |
9879 | end if; | |
9880 | ||
ee6ba406 | 9881 | L_Index := First_Index (T_Typ); |
9882 | R_Index := First_Index (Exptyp); | |
9883 | ||
9884 | for Indx in 1 .. Ndims loop | |
9885 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9886 | or else |
9887 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9888 | then |
9889 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
9890 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
9891 | ||
9892 | -- Deal with compile time length check. Note that we | |
9893 | -- skip this in the access case, because the access | |
9894 | -- value may be null, so we cannot know statically. | |
9895 | ||
9896 | if not Do_Access | |
9897 | and then Compile_Time_Known_Value (L_Low) | |
9898 | and then Compile_Time_Known_Value (L_High) | |
9899 | and then Compile_Time_Known_Value (R_Low) | |
9900 | and then Compile_Time_Known_Value (R_High) | |
9901 | then | |
9902 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
9903 | L_Length := Expr_Value (L_High) - | |
9904 | Expr_Value (L_Low) + 1; | |
9905 | else | |
9906 | L_Length := UI_From_Int (0); | |
9907 | end if; | |
9908 | ||
9909 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
9910 | R_Length := Expr_Value (R_High) - | |
9911 | Expr_Value (R_Low) + 1; | |
9912 | else | |
9913 | R_Length := UI_From_Int (0); | |
9914 | end if; | |
9915 | ||
9916 | if L_Length > R_Length then | |
9917 | Add_Check | |
9918 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9919 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 9920 | |
b8eacb12 | 9921 | elsif L_Length < R_Length then |
ee6ba406 | 9922 | Add_Check |
9923 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9924 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 9925 | end if; |
9926 | ||
9927 | -- The comparison for an individual index subtype | |
9928 | -- is omitted if the corresponding index subtypes | |
9929 | -- statically match, since the result is known to | |
9930 | -- be true. Note that this test is worth while even | |
9931 | -- though we do static evaluation, because non-static | |
9932 | -- subtypes can statically match. | |
9933 | ||
9934 | elsif not | |
9935 | Subtypes_Statically_Match | |
9936 | (Etype (L_Index), Etype (R_Index)) | |
9937 | ||
9938 | and then not | |
9939 | (Same_Bounds (L_Low, R_Low) | |
9940 | and then Same_Bounds (L_High, R_High)) | |
9941 | then | |
9942 | Evolve_Or_Else | |
9943 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
9944 | end if; | |
9945 | ||
9946 | Next (L_Index); | |
9947 | Next (R_Index); | |
9948 | end if; | |
9949 | end loop; | |
9950 | end; | |
9951 | ||
9952 | -- Handle cases where we do not get a usable actual subtype that | |
9953 | -- is constrained. This happens for example in the function call | |
9954 | -- and explicit dereference cases. In these cases, we have to get | |
9955 | -- the length or range from the expression itself, making sure we | |
9956 | -- do not evaluate it more than once. | |
9957 | ||
9958 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 9959 | -- result of applying Duplicate_Expr to the original tree, forcing |
9960 | -- the result to be a name. | |
ee6ba406 | 9961 | |
9962 | else | |
9963 | declare | |
9dfe12ae | 9964 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9965 | |
9966 | begin | |
9967 | -- Build the condition for the explicit dereference case | |
9968 | ||
9969 | for Indx in 1 .. Ndims loop | |
9970 | Evolve_Or_Else | |
9971 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
9972 | end loop; | |
9973 | end; | |
9974 | end if; | |
9975 | end if; | |
9976 | end if; | |
9977 | ||
9978 | -- Construct the test and insert into the tree | |
9979 | ||
9980 | if Present (Cond) then | |
9981 | if Do_Access then | |
9982 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9983 | end if; | |
9984 | ||
f15731c4 | 9985 | Add_Check |
9986 | (Make_Raise_Constraint_Error (Loc, | |
9987 | Condition => Cond, | |
9988 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 9989 | end if; |
9990 | ||
9991 | return Ret_Result; | |
ee6ba406 | 9992 | end Selected_Length_Checks; |
9993 | ||
9994 | --------------------------- | |
9995 | -- Selected_Range_Checks -- | |
9996 | --------------------------- | |
9997 | ||
9998 | function Selected_Range_Checks | |
9999 | (Ck_Node : Node_Id; | |
10000 | Target_Typ : Entity_Id; | |
10001 | Source_Typ : Entity_Id; | |
314a23b6 | 10002 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 10003 | is |
10004 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
10005 | S_Typ : Entity_Id; | |
10006 | T_Typ : Entity_Id; | |
10007 | Expr_Actual : Node_Id; | |
10008 | Exptyp : Entity_Id; | |
10009 | Cond : Node_Id := Empty; | |
10010 | Do_Access : Boolean := False; | |
10011 | Wnode : Node_Id := Warn_Node; | |
10012 | Ret_Result : Check_Result := (Empty, Empty); | |
5bb74b99 | 10013 | Num_Checks : Natural := 0; |
ee6ba406 | 10014 | |
10015 | procedure Add_Check (N : Node_Id); | |
10016 | -- Adds the action given to Ret_Result if N is non-Empty | |
10017 | ||
10018 | function Discrete_Range_Cond | |
10019 | (Expr : Node_Id; | |
314a23b6 | 10020 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 10021 | -- Returns expression to compute: |
10022 | -- Low_Bound (Expr) < Typ'First | |
10023 | -- or else | |
10024 | -- High_Bound (Expr) > Typ'Last | |
10025 | ||
10026 | function Discrete_Expr_Cond | |
10027 | (Expr : Node_Id; | |
314a23b6 | 10028 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 10029 | -- Returns expression to compute: |
10030 | -- Expr < Typ'First | |
10031 | -- or else | |
10032 | -- Expr > Typ'Last | |
10033 | ||
10034 | function Get_E_First_Or_Last | |
3cb12758 | 10035 | (Loc : Source_Ptr; |
10036 | E : Entity_Id; | |
ee6ba406 | 10037 | Indx : Nat; |
314a23b6 | 10038 | Nam : Name_Id) return Node_Id; |
79212397 | 10039 | -- Returns an attribute reference |
ee6ba406 | 10040 | -- E'First or E'Last |
79212397 | 10041 | -- with a source location of Loc. |
f73ee678 | 10042 | -- |
79212397 | 10043 | -- Nam is Name_First or Name_Last, according to which attribute is |
10044 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
10045 | -- Expressions of the attribute reference (identifying the desired | |
10046 | -- array dimension). | |
ee6ba406 | 10047 | |
10048 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
10049 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
10050 | -- Returns expression to compute: | |
9dfe12ae | 10051 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 10052 | |
10053 | function Range_E_Cond | |
10054 | (Exptyp : Entity_Id; | |
10055 | Typ : Entity_Id; | |
10056 | Indx : Nat) | |
10057 | return Node_Id; | |
10058 | -- Returns expression to compute: | |
10059 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
10060 | ||
10061 | function Range_Equal_E_Cond | |
10062 | (Exptyp : Entity_Id; | |
10063 | Typ : Entity_Id; | |
314a23b6 | 10064 | Indx : Nat) return Node_Id; |
ee6ba406 | 10065 | -- Returns expression to compute: |
10066 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
10067 | ||
10068 | function Range_N_Cond | |
10069 | (Expr : Node_Id; | |
10070 | Typ : Entity_Id; | |
314a23b6 | 10071 | Indx : Nat) return Node_Id; |
ee6ba406 | 10072 | -- Return expression to compute: |
10073 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
10074 | ||
10075 | --------------- | |
10076 | -- Add_Check -- | |
10077 | --------------- | |
10078 | ||
10079 | procedure Add_Check (N : Node_Id) is | |
10080 | begin | |
10081 | if Present (N) then | |
10082 | ||
10083 | -- For now, ignore attempt to place more than 2 checks ??? | |
10084 | ||
10085 | if Num_Checks = 2 then | |
10086 | return; | |
10087 | end if; | |
10088 | ||
10089 | pragma Assert (Num_Checks <= 1); | |
10090 | Num_Checks := Num_Checks + 1; | |
10091 | Ret_Result (Num_Checks) := N; | |
10092 | end if; | |
10093 | end Add_Check; | |
10094 | ||
10095 | ------------------------- | |
10096 | -- Discrete_Expr_Cond -- | |
10097 | ------------------------- | |
10098 | ||
10099 | function Discrete_Expr_Cond | |
10100 | (Expr : Node_Id; | |
314a23b6 | 10101 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 10102 | is |
10103 | begin | |
10104 | return | |
10105 | Make_Or_Else (Loc, | |
10106 | Left_Opnd => | |
10107 | Make_Op_Lt (Loc, | |
10108 | Left_Opnd => | |
9dfe12ae | 10109 | Convert_To (Base_Type (Typ), |
10110 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 10111 | Right_Opnd => |
10112 | Convert_To (Base_Type (Typ), | |
3cb12758 | 10113 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 10114 | |
10115 | Right_Opnd => | |
10116 | Make_Op_Gt (Loc, | |
10117 | Left_Opnd => | |
9dfe12ae | 10118 | Convert_To (Base_Type (Typ), |
10119 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 10120 | Right_Opnd => |
10121 | Convert_To | |
10122 | (Base_Type (Typ), | |
3cb12758 | 10123 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 10124 | end Discrete_Expr_Cond; |
10125 | ||
10126 | ------------------------- | |
10127 | -- Discrete_Range_Cond -- | |
10128 | ------------------------- | |
10129 | ||
10130 | function Discrete_Range_Cond | |
10131 | (Expr : Node_Id; | |
314a23b6 | 10132 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 10133 | is |
10134 | LB : Node_Id := Low_Bound (Expr); | |
10135 | HB : Node_Id := High_Bound (Expr); | |
10136 | ||
10137 | Left_Opnd : Node_Id; | |
10138 | Right_Opnd : Node_Id; | |
10139 | ||
10140 | begin | |
10141 | if Nkind (LB) = N_Identifier | |
feff2f05 | 10142 | and then Ekind (Entity (LB)) = E_Discriminant |
10143 | then | |
ee6ba406 | 10144 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
10145 | end if; | |
10146 | ||
ee6ba406 | 10147 | Left_Opnd := |
10148 | Make_Op_Lt (Loc, | |
10149 | Left_Opnd => | |
10150 | Convert_To | |
9dfe12ae | 10151 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 10152 | |
10153 | Right_Opnd => | |
10154 | Convert_To | |
3cb12758 | 10155 | (Base_Type (Typ), |
10156 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 10157 | |
ba9b1a39 | 10158 | if Nkind (HB) = N_Identifier |
10159 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 10160 | then |
ba9b1a39 | 10161 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 10162 | end if; |
10163 | ||
10164 | Right_Opnd := | |
10165 | Make_Op_Gt (Loc, | |
10166 | Left_Opnd => | |
10167 | Convert_To | |
9dfe12ae | 10168 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 10169 | |
10170 | Right_Opnd => | |
10171 | Convert_To | |
10172 | (Base_Type (Typ), | |
3cb12758 | 10173 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 10174 | |
10175 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
10176 | end Discrete_Range_Cond; | |
10177 | ||
10178 | ------------------------- | |
10179 | -- Get_E_First_Or_Last -- | |
10180 | ------------------------- | |
10181 | ||
10182 | function Get_E_First_Or_Last | |
3cb12758 | 10183 | (Loc : Source_Ptr; |
10184 | E : Entity_Id; | |
ee6ba406 | 10185 | Indx : Nat; |
314a23b6 | 10186 | Nam : Name_Id) return Node_Id |
ee6ba406 | 10187 | is |
3cb12758 | 10188 | Exprs : List_Id; |
ee6ba406 | 10189 | begin |
3cb12758 | 10190 | if Indx > 0 then |
10191 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 10192 | else |
3cb12758 | 10193 | Exprs := No_List; |
ee6ba406 | 10194 | end if; |
10195 | ||
3cb12758 | 10196 | return Make_Attribute_Reference (Loc, |
10197 | Prefix => New_Occurrence_Of (E, Loc), | |
10198 | Attribute_Name => Nam, | |
10199 | Expressions => Exprs); | |
ee6ba406 | 10200 | end Get_E_First_Or_Last; |
10201 | ||
10202 | ----------------- | |
10203 | -- Get_N_First -- | |
10204 | ----------------- | |
10205 | ||
10206 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
10207 | begin | |
10208 | return | |
10209 | Make_Attribute_Reference (Loc, | |
10210 | Attribute_Name => Name_First, | |
10211 | Prefix => | |
9dfe12ae | 10212 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 10213 | Expressions => New_List ( |
10214 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 10215 | end Get_N_First; |
10216 | ||
10217 | ---------------- | |
10218 | -- Get_N_Last -- | |
10219 | ---------------- | |
10220 | ||
10221 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
10222 | begin | |
10223 | return | |
10224 | Make_Attribute_Reference (Loc, | |
10225 | Attribute_Name => Name_Last, | |
10226 | Prefix => | |
9dfe12ae | 10227 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 10228 | Expressions => New_List ( |
10229 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 10230 | end Get_N_Last; |
10231 | ||
10232 | ------------------ | |
10233 | -- Range_E_Cond -- | |
10234 | ------------------ | |
10235 | ||
10236 | function Range_E_Cond | |
10237 | (Exptyp : Entity_Id; | |
10238 | Typ : Entity_Id; | |
314a23b6 | 10239 | Indx : Nat) return Node_Id |
ee6ba406 | 10240 | is |
10241 | begin | |
10242 | return | |
10243 | Make_Or_Else (Loc, | |
10244 | Left_Opnd => | |
10245 | Make_Op_Lt (Loc, | |
3cb12758 | 10246 | Left_Opnd => |
10247 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10248 | Right_Opnd => | |
10249 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 10250 | |
10251 | Right_Opnd => | |
10252 | Make_Op_Gt (Loc, | |
3cb12758 | 10253 | Left_Opnd => |
10254 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10255 | Right_Opnd => | |
10256 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10257 | end Range_E_Cond; |
10258 | ||
10259 | ------------------------ | |
10260 | -- Range_Equal_E_Cond -- | |
10261 | ------------------------ | |
10262 | ||
10263 | function Range_Equal_E_Cond | |
10264 | (Exptyp : Entity_Id; | |
10265 | Typ : Entity_Id; | |
314a23b6 | 10266 | Indx : Nat) return Node_Id |
ee6ba406 | 10267 | is |
10268 | begin | |
10269 | return | |
10270 | Make_Or_Else (Loc, | |
10271 | Left_Opnd => | |
10272 | Make_Op_Ne (Loc, | |
3cb12758 | 10273 | Left_Opnd => |
10274 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10275 | Right_Opnd => | |
10276 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
10277 | ||
ee6ba406 | 10278 | Right_Opnd => |
10279 | Make_Op_Ne (Loc, | |
3cb12758 | 10280 | Left_Opnd => |
10281 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10282 | Right_Opnd => | |
10283 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10284 | end Range_Equal_E_Cond; |
10285 | ||
10286 | ------------------ | |
10287 | -- Range_N_Cond -- | |
10288 | ------------------ | |
10289 | ||
10290 | function Range_N_Cond | |
10291 | (Expr : Node_Id; | |
10292 | Typ : Entity_Id; | |
314a23b6 | 10293 | Indx : Nat) return Node_Id |
ee6ba406 | 10294 | is |
10295 | begin | |
10296 | return | |
10297 | Make_Or_Else (Loc, | |
10298 | Left_Opnd => | |
10299 | Make_Op_Lt (Loc, | |
3cb12758 | 10300 | Left_Opnd => |
10301 | Get_N_First (Expr, Indx), | |
10302 | Right_Opnd => | |
10303 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 10304 | |
10305 | Right_Opnd => | |
10306 | Make_Op_Gt (Loc, | |
3cb12758 | 10307 | Left_Opnd => |
10308 | Get_N_Last (Expr, Indx), | |
10309 | Right_Opnd => | |
10310 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 10311 | end Range_N_Cond; |
10312 | ||
10313 | -- Start of processing for Selected_Range_Checks | |
10314 | ||
10315 | begin | |
f0d65dae | 10316 | -- Checks will be applied only when generating code. In GNATprove mode, |
10317 | -- we do not apply the checks, but we still call Selected_Range_Checks | |
10318 | -- to possibly issue errors on SPARK code when a run-time error can be | |
10319 | -- detected at compile time. | |
10320 | ||
10321 | if not Expander_Active and not GNATprove_Mode then | |
ee6ba406 | 10322 | return Ret_Result; |
10323 | end if; | |
10324 | ||
10325 | if Target_Typ = Any_Type | |
10326 | or else Target_Typ = Any_Composite | |
10327 | or else Raises_Constraint_Error (Ck_Node) | |
10328 | then | |
10329 | return Ret_Result; | |
10330 | end if; | |
10331 | ||
10332 | if No (Wnode) then | |
10333 | Wnode := Ck_Node; | |
10334 | end if; | |
10335 | ||
10336 | T_Typ := Target_Typ; | |
10337 | ||
10338 | if No (Source_Typ) then | |
10339 | S_Typ := Etype (Ck_Node); | |
10340 | else | |
10341 | S_Typ := Source_Typ; | |
10342 | end if; | |
10343 | ||
10344 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
10345 | return Ret_Result; | |
10346 | end if; | |
10347 | ||
10348 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
10349 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
10350 | -- in, and since Node can be an N_Range node, it might be invalid. | |
10351 | -- Should there be an assert check somewhere for taking the Etype of | |
10352 | -- an N_Range node ??? | |
10353 | ||
10354 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
10355 | S_Typ := Designated_Type (S_Typ); | |
10356 | T_Typ := Designated_Type (T_Typ); | |
10357 | Do_Access := True; | |
10358 | ||
2af58f67 | 10359 | -- A simple optimization for the null case |
ee6ba406 | 10360 | |
2af58f67 | 10361 | if Known_Null (Ck_Node) then |
ee6ba406 | 10362 | return Ret_Result; |
10363 | end if; | |
10364 | end if; | |
10365 | ||
10366 | -- For an N_Range Node, check for a null range and then if not | |
10367 | -- null generate a range check action. | |
10368 | ||
10369 | if Nkind (Ck_Node) = N_Range then | |
10370 | ||
10371 | -- There's no point in checking a range against itself | |
10372 | ||
10373 | if Ck_Node = Scalar_Range (T_Typ) then | |
10374 | return Ret_Result; | |
10375 | end if; | |
10376 | ||
10377 | declare | |
10378 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
10379 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 10380 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
10381 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 10382 | |
eefa141b | 10383 | LB : Node_Id := Low_Bound (Ck_Node); |
10384 | HB : Node_Id := High_Bound (Ck_Node); | |
3fabf0ca | 10385 | Known_LB : Boolean := False; |
10386 | Known_HB : Boolean := False; | |
eefa141b | 10387 | |
10388 | Null_Range : Boolean; | |
ee6ba406 | 10389 | Out_Of_Range_L : Boolean; |
10390 | Out_Of_Range_H : Boolean; | |
10391 | ||
10392 | begin | |
eefa141b | 10393 | -- Compute what is known at compile time |
10394 | ||
10395 | if Known_T_LB and Known_T_HB then | |
10396 | if Compile_Time_Known_Value (LB) then | |
10397 | Known_LB := True; | |
10398 | ||
10399 | -- There's no point in checking that a bound is within its | |
10400 | -- own range so pretend that it is known in this case. First | |
10401 | -- deal with low bound. | |
10402 | ||
10403 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
10404 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
10405 | then | |
10406 | LB := T_LB; | |
10407 | Known_LB := True; | |
eefa141b | 10408 | end if; |
10409 | ||
10410 | -- Likewise for the high bound | |
10411 | ||
10412 | if Compile_Time_Known_Value (HB) then | |
10413 | Known_HB := True; | |
10414 | ||
10415 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
10416 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
10417 | then | |
10418 | HB := T_HB; | |
10419 | Known_HB := True; | |
eefa141b | 10420 | end if; |
10421 | end if; | |
10422 | ||
10423 | -- Check for case where everything is static and we can do the | |
10424 | -- check at compile time. This is skipped if we have an access | |
10425 | -- type, since the access value may be null. | |
10426 | ||
10427 | -- ??? This code can be improved since you only need to know that | |
10428 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
10429 | -- compile time to emit pertinent messages. | |
10430 | ||
10431 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
10432 | and not Do_Access | |
ee6ba406 | 10433 | then |
10434 | -- Floating-point case | |
10435 | ||
10436 | if Is_Floating_Point_Type (S_Typ) then | |
10437 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
10438 | Out_Of_Range_L := | |
10439 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 10440 | or else |
ee6ba406 | 10441 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
10442 | ||
10443 | Out_Of_Range_H := | |
10444 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 10445 | or else |
ee6ba406 | 10446 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
10447 | ||
10448 | -- Fixed or discrete type case | |
10449 | ||
10450 | else | |
10451 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
10452 | Out_Of_Range_L := | |
10453 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 10454 | or else |
ee6ba406 | 10455 | (Expr_Value (LB) > Expr_Value (T_HB)); |
10456 | ||
10457 | Out_Of_Range_H := | |
10458 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 10459 | or else |
ee6ba406 | 10460 | (Expr_Value (HB) < Expr_Value (T_LB)); |
10461 | end if; | |
10462 | ||
10463 | if not Null_Range then | |
10464 | if Out_Of_Range_L then | |
10465 | if No (Warn_Node) then | |
10466 | Add_Check | |
10467 | (Compile_Time_Constraint_Error | |
10468 | (Low_Bound (Ck_Node), | |
cb97ae5c | 10469 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10470 | |
10471 | else | |
10472 | Add_Check | |
10473 | (Compile_Time_Constraint_Error | |
10474 | (Wnode, | |
cb97ae5c | 10475 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 10476 | end if; |
10477 | end if; | |
10478 | ||
10479 | if Out_Of_Range_H then | |
10480 | if No (Warn_Node) then | |
10481 | Add_Check | |
10482 | (Compile_Time_Constraint_Error | |
10483 | (High_Bound (Ck_Node), | |
cb97ae5c | 10484 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10485 | |
10486 | else | |
10487 | Add_Check | |
10488 | (Compile_Time_Constraint_Error | |
10489 | (Wnode, | |
cb97ae5c | 10490 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 10491 | end if; |
10492 | end if; | |
ee6ba406 | 10493 | end if; |
10494 | ||
10495 | else | |
10496 | declare | |
10497 | LB : Node_Id := Low_Bound (Ck_Node); | |
10498 | HB : Node_Id := High_Bound (Ck_Node); | |
10499 | ||
10500 | begin | |
feff2f05 | 10501 | -- If either bound is a discriminant and we are within the |
10502 | -- record declaration, it is a use of the discriminant in a | |
10503 | -- constraint of a component, and nothing can be checked | |
10504 | -- here. The check will be emitted within the init proc. | |
10505 | -- Before then, the discriminal has no real meaning. | |
10506 | -- Similarly, if the entity is a discriminal, there is no | |
10507 | -- check to perform yet. | |
10508 | ||
10509 | -- The same holds within a discriminated synchronized type, | |
10510 | -- where the discriminant may constrain a component or an | |
10511 | -- entry family. | |
ee6ba406 | 10512 | |
10513 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 10514 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 10515 | then |
0577b0b1 | 10516 | if Current_Scope = Scope (Entity (LB)) |
10517 | or else Is_Concurrent_Type (Current_Scope) | |
10518 | or else Ekind (Entity (LB)) /= E_Discriminant | |
10519 | then | |
ee6ba406 | 10520 | return Ret_Result; |
10521 | else | |
10522 | LB := | |
10523 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
10524 | end if; | |
10525 | end if; | |
10526 | ||
10527 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 10528 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 10529 | then |
0577b0b1 | 10530 | if Current_Scope = Scope (Entity (HB)) |
10531 | or else Is_Concurrent_Type (Current_Scope) | |
10532 | or else Ekind (Entity (HB)) /= E_Discriminant | |
10533 | then | |
ee6ba406 | 10534 | return Ret_Result; |
10535 | else | |
10536 | HB := | |
10537 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
10538 | end if; | |
10539 | end if; | |
10540 | ||
10541 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
10542 | Set_Paren_Count (Cond, 1); | |
10543 | ||
10544 | Cond := | |
10545 | Make_And_Then (Loc, | |
10546 | Left_Opnd => | |
10547 | Make_Op_Ge (Loc, | |
86d32751 | 10548 | Left_Opnd => |
10549 | Convert_To (Base_Type (Etype (HB)), | |
10550 | Duplicate_Subexpr_No_Checks (HB)), | |
10551 | Right_Opnd => | |
10552 | Convert_To (Base_Type (Etype (LB)), | |
10553 | Duplicate_Subexpr_No_Checks (LB))), | |
ee6ba406 | 10554 | Right_Opnd => Cond); |
10555 | end; | |
ee6ba406 | 10556 | end if; |
10557 | end; | |
10558 | ||
10559 | elsif Is_Scalar_Type (S_Typ) then | |
10560 | ||
10561 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
10562 | -- except the above simply sets a flag in the node and lets | |
10563 | -- gigi generate the check base on the Etype of the expression. | |
10564 | -- Sometimes, however we want to do a dynamic check against an | |
10565 | -- arbitrary target type, so we do that here. | |
10566 | ||
10567 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
10568 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
10569 | ||
10570 | -- For literals, we can tell if the constraint error will be | |
10571 | -- raised at compile time, so we never need a dynamic check, but | |
10572 | -- if the exception will be raised, then post the usual warning, | |
10573 | -- and replace the literal with a raise constraint error | |
10574 | -- expression. As usual, skip this for access types | |
10575 | ||
20cf157b | 10576 | elsif Compile_Time_Known_Value (Ck_Node) and then not Do_Access then |
ee6ba406 | 10577 | declare |
10578 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
10579 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
10580 | ||
10581 | Out_Of_Range : Boolean; | |
10582 | Static_Bounds : constant Boolean := | |
b6341c67 | 10583 | Compile_Time_Known_Value (LB) |
10584 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 10585 | |
10586 | begin | |
10587 | -- Following range tests should use Sem_Eval routine ??? | |
10588 | ||
10589 | if Static_Bounds then | |
10590 | if Is_Floating_Point_Type (S_Typ) then | |
10591 | Out_Of_Range := | |
10592 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
10593 | or else | |
10594 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
10595 | ||
eefa141b | 10596 | -- Fixed or discrete type |
10597 | ||
10598 | else | |
ee6ba406 | 10599 | Out_Of_Range := |
10600 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
10601 | or else | |
10602 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
10603 | end if; | |
10604 | ||
eefa141b | 10605 | -- Bounds of the type are static and the literal is out of |
10606 | -- range so output a warning message. | |
ee6ba406 | 10607 | |
10608 | if Out_Of_Range then | |
10609 | if No (Warn_Node) then | |
10610 | Add_Check | |
10611 | (Compile_Time_Constraint_Error | |
10612 | (Ck_Node, | |
cb97ae5c | 10613 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10614 | |
10615 | else | |
10616 | Add_Check | |
10617 | (Compile_Time_Constraint_Error | |
10618 | (Wnode, | |
cb97ae5c | 10619 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 10620 | end if; |
10621 | end if; | |
10622 | ||
10623 | else | |
10624 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
10625 | end if; | |
10626 | end; | |
10627 | ||
10628 | -- Here for the case of a non-static expression, we need a runtime | |
10629 | -- check unless the source type range is guaranteed to be in the | |
10630 | -- range of the target type. | |
10631 | ||
10632 | else | |
7a1dabb3 | 10633 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 10634 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
10635 | end if; | |
10636 | end if; | |
10637 | end if; | |
10638 | ||
10639 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
10640 | if Is_Constrained (T_Typ) then | |
10641 | ||
10642 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
10643 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
10644 | ||
10645 | if Is_Access_Type (Exptyp) then | |
10646 | Exptyp := Designated_Type (Exptyp); | |
10647 | end if; | |
10648 | ||
10649 | -- String_Literal case. This needs to be handled specially be- | |
10650 | -- cause no index types are available for string literals. The | |
10651 | -- condition is simply: | |
10652 | ||
10653 | -- T_Typ'Length = string-literal-length | |
10654 | ||
10655 | if Nkind (Expr_Actual) = N_String_Literal then | |
10656 | null; | |
10657 | ||
10658 | -- General array case. Here we have a usable actual subtype for | |
10659 | -- the expression, and the condition is built from the two types | |
10660 | ||
10661 | -- T_Typ'First < Exptyp'First or else | |
10662 | -- T_Typ'Last > Exptyp'Last or else | |
10663 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
10664 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
10665 | -- ... | |
10666 | ||
10667 | elsif Is_Constrained (Exptyp) then | |
10668 | declare | |
9dfe12ae | 10669 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
10670 | ||
ee6ba406 | 10671 | L_Index : Node_Id; |
10672 | R_Index : Node_Id; | |
ee6ba406 | 10673 | |
10674 | begin | |
10675 | L_Index := First_Index (T_Typ); | |
10676 | R_Index := First_Index (Exptyp); | |
10677 | ||
10678 | for Indx in 1 .. Ndims loop | |
10679 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 10680 | or else |
10681 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 10682 | then |
ee6ba406 | 10683 | -- Deal with compile time length check. Note that we |
10684 | -- skip this in the access case, because the access | |
10685 | -- value may be null, so we cannot know statically. | |
10686 | ||
10687 | if not | |
10688 | Subtypes_Statically_Match | |
10689 | (Etype (L_Index), Etype (R_Index)) | |
10690 | then | |
10691 | -- If the target type is constrained then we | |
10692 | -- have to check for exact equality of bounds | |
10693 | -- (required for qualified expressions). | |
10694 | ||
10695 | if Is_Constrained (T_Typ) then | |
10696 | Evolve_Or_Else | |
10697 | (Cond, | |
10698 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 10699 | else |
10700 | Evolve_Or_Else | |
10701 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
10702 | end if; | |
10703 | end if; | |
10704 | ||
10705 | Next (L_Index); | |
10706 | Next (R_Index); | |
ee6ba406 | 10707 | end if; |
10708 | end loop; | |
10709 | end; | |
10710 | ||
10711 | -- Handle cases where we do not get a usable actual subtype that | |
10712 | -- is constrained. This happens for example in the function call | |
10713 | -- and explicit dereference cases. In these cases, we have to get | |
10714 | -- the length or range from the expression itself, making sure we | |
10715 | -- do not evaluate it more than once. | |
10716 | ||
10717 | -- Here Ck_Node is the original expression, or more properly the | |
10718 | -- result of applying Duplicate_Expr to the original tree, | |
10719 | -- forcing the result to be a name. | |
10720 | ||
10721 | else | |
10722 | declare | |
9dfe12ae | 10723 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 10724 | |
10725 | begin | |
10726 | -- Build the condition for the explicit dereference case | |
10727 | ||
10728 | for Indx in 1 .. Ndims loop | |
10729 | Evolve_Or_Else | |
10730 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
10731 | end loop; | |
10732 | end; | |
ee6ba406 | 10733 | end if; |
10734 | ||
10735 | else | |
feff2f05 | 10736 | -- For a conversion to an unconstrained array type, generate an |
10737 | -- Action to check that the bounds of the source value are within | |
10738 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
10739 | -- check is needed for a conversion to an access to unconstrained | |
10740 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
10741 | -- of the two access types to statically match. | |
10742 | ||
10743 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
10744 | and then not Do_Access | |
10745 | then | |
ee6ba406 | 10746 | declare |
10747 | Opnd_Index : Node_Id; | |
10748 | Targ_Index : Node_Id; | |
00c403ee | 10749 | Opnd_Range : Node_Id; |
ee6ba406 | 10750 | |
10751 | begin | |
feff2f05 | 10752 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 10753 | Targ_Index := First_Index (T_Typ); |
00c403ee | 10754 | while Present (Opnd_Index) loop |
10755 | ||
10756 | -- If the index is a range, use its bounds. If it is an | |
10757 | -- entity (as will be the case if it is a named subtype | |
10758 | -- or an itype created for a slice) retrieve its range. | |
10759 | ||
10760 | if Is_Entity_Name (Opnd_Index) | |
10761 | and then Is_Type (Entity (Opnd_Index)) | |
10762 | then | |
10763 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
10764 | else | |
10765 | Opnd_Range := Opnd_Index; | |
10766 | end if; | |
10767 | ||
10768 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 10769 | if Is_In_Range |
10770 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
10771 | Assume_Valid => True) | |
ee6ba406 | 10772 | and then |
10773 | Is_In_Range | |
9c486805 | 10774 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
10775 | Assume_Valid => True) | |
ee6ba406 | 10776 | then |
10777 | null; | |
10778 | ||
feff2f05 | 10779 | -- If null range, no check needed |
f2a06be9 | 10780 | |
9dfe12ae | 10781 | elsif |
00c403ee | 10782 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 10783 | and then |
00c403ee | 10784 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 10785 | and then |
00c403ee | 10786 | Expr_Value (High_Bound (Opnd_Range)) < |
10787 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 10788 | then |
10789 | null; | |
10790 | ||
ee6ba406 | 10791 | elsif Is_Out_Of_Range |
9c486805 | 10792 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
10793 | Assume_Valid => True) | |
ee6ba406 | 10794 | or else |
10795 | Is_Out_Of_Range | |
9c486805 | 10796 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
10797 | Assume_Valid => True) | |
ee6ba406 | 10798 | then |
10799 | Add_Check | |
10800 | (Compile_Time_Constraint_Error | |
cb97ae5c | 10801 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 10802 | |
10803 | else | |
10804 | Evolve_Or_Else | |
10805 | (Cond, | |
10806 | Discrete_Range_Cond | |
00c403ee | 10807 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 10808 | end if; |
10809 | end if; | |
10810 | ||
10811 | Next_Index (Opnd_Index); | |
10812 | Next_Index (Targ_Index); | |
10813 | end loop; | |
10814 | end; | |
10815 | end if; | |
10816 | end if; | |
10817 | end if; | |
10818 | ||
10819 | -- Construct the test and insert into the tree | |
10820 | ||
10821 | if Present (Cond) then | |
10822 | if Do_Access then | |
10823 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
10824 | end if; | |
10825 | ||
f15731c4 | 10826 | Add_Check |
10827 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 10828 | Condition => Cond, |
22d3a5a3 | 10829 | Reason => CE_Range_Check_Failed)); |
ee6ba406 | 10830 | end if; |
10831 | ||
10832 | return Ret_Result; | |
ee6ba406 | 10833 | end Selected_Range_Checks; |
10834 | ||
10835 | ------------------------------- | |
10836 | -- Storage_Checks_Suppressed -- | |
10837 | ------------------------------- | |
10838 | ||
10839 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10840 | begin | |
9dfe12ae | 10841 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
10842 | return Is_Check_Suppressed (E, Storage_Check); | |
10843 | else | |
fafc6b97 | 10844 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 10845 | end if; |
ee6ba406 | 10846 | end Storage_Checks_Suppressed; |
10847 | ||
10848 | --------------------------- | |
10849 | -- Tag_Checks_Suppressed -- | |
10850 | --------------------------- | |
10851 | ||
10852 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10853 | begin | |
89f1e35c | 10854 | if Present (E) |
10855 | and then Checks_May_Be_Suppressed (E) | |
10856 | then | |
10857 | return Is_Check_Suppressed (E, Tag_Check); | |
20cf157b | 10858 | else |
10859 | return Scope_Suppress.Suppress (Tag_Check); | |
9dfe12ae | 10860 | end if; |
ee6ba406 | 10861 | end Tag_Checks_Suppressed; |
10862 | ||
7b8fa048 | 10863 | --------------------------------------- |
10864 | -- Validate_Alignment_Check_Warnings -- | |
10865 | --------------------------------------- | |
10866 | ||
10867 | procedure Validate_Alignment_Check_Warnings is | |
10868 | begin | |
10869 | for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop | |
10870 | declare | |
10871 | AWR : Alignment_Warnings_Record | |
10872 | renames Alignment_Warnings.Table (J); | |
10873 | begin | |
10874 | if Known_Alignment (AWR.E) | |
10875 | and then AWR.A mod Alignment (AWR.E) = 0 | |
10876 | then | |
10877 | Delete_Warning_And_Continuations (AWR.W); | |
10878 | end if; | |
10879 | end; | |
10880 | end loop; | |
10881 | end Validate_Alignment_Check_Warnings; | |
10882 | ||
0577b0b1 | 10883 | -------------------------- |
10884 | -- Validity_Check_Range -- | |
10885 | -------------------------- | |
10886 | ||
aaec8d13 | 10887 | procedure Validity_Check_Range |
10888 | (N : Node_Id; | |
10889 | Related_Id : Entity_Id := Empty) | |
10890 | is | |
0577b0b1 | 10891 | begin |
10892 | if Validity_Checks_On and Validity_Check_Operands then | |
10893 | if Nkind (N) = N_Range then | |
aaec8d13 | 10894 | Ensure_Valid |
10895 | (Expr => Low_Bound (N), | |
10896 | Related_Id => Related_Id, | |
10897 | Is_Low_Bound => True); | |
10898 | ||
10899 | Ensure_Valid | |
10900 | (Expr => High_Bound (N), | |
10901 | Related_Id => Related_Id, | |
10902 | Is_High_Bound => True); | |
0577b0b1 | 10903 | end if; |
10904 | end if; | |
10905 | end Validity_Check_Range; | |
10906 | ||
10907 | -------------------------------- | |
10908 | -- Validity_Checks_Suppressed -- | |
10909 | -------------------------------- | |
10910 | ||
10911 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10912 | begin | |
10913 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
10914 | return Is_Check_Suppressed (E, Validity_Check); | |
10915 | else | |
fafc6b97 | 10916 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 10917 | end if; |
10918 | end Validity_Checks_Suppressed; | |
10919 | ||
ee6ba406 | 10920 | end Checks; |