]>
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 | -- -- | |
1581f2d7 | 9 | -- Copyright (C) 1992-2014, 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; |
7b8fa048 | 51 | with Sem_Eval; use Sem_Eval; |
ee6ba406 | 52 | with Sem_Res; use Sem_Res; |
53 | with Sem_Util; use Sem_Util; | |
54 | with Sem_Warn; use Sem_Warn; | |
55 | with Sinfo; use Sinfo; | |
9dfe12ae | 56 | with Sinput; use Sinput; |
ee6ba406 | 57 | with Snames; use Snames; |
9dfe12ae | 58 | with Sprint; use Sprint; |
ee6ba406 | 59 | with Stand; use Stand; |
bb569db0 | 60 | with Stringt; use Stringt; |
f15731c4 | 61 | with Targparm; use Targparm; |
ee6ba406 | 62 | with Tbuild; use Tbuild; |
63 | with Ttypes; use Ttypes; | |
ee6ba406 | 64 | with Validsw; use Validsw; |
65 | ||
66 | package body Checks is | |
67 | ||
68 | -- General note: many of these routines are concerned with generating | |
69 | -- checking code to make sure that constraint error is raised at runtime. | |
70 | -- Clearly this code is only needed if the expander is active, since | |
71 | -- otherwise we will not be generating code or going into the runtime | |
72 | -- execution anyway. | |
73 | ||
74 | -- We therefore disconnect most of these checks if the expander is | |
75 | -- inactive. This has the additional benefit that we do not need to | |
76 | -- worry about the tree being messed up by previous errors (since errors | |
77 | -- turn off expansion anyway). | |
78 | ||
79 | -- There are a few exceptions to the above rule. For instance routines | |
80 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
81 | -- safely called even when the Expander is inactive (but Errors_Detected | |
82 | -- is 0). The benefit of executing this code when expansion is off, is | |
83 | -- the ability to emit constraint error warning for static expressions | |
84 | -- even when we are not generating code. | |
85 | ||
20cf157b | 86 | -- The above is modified in gnatprove mode to ensure that proper check |
87 | -- flags are always placed, even if expansion is off. | |
88 | ||
9dfe12ae | 89 | ------------------------------------- |
90 | -- Suppression of Redundant Checks -- | |
91 | ------------------------------------- | |
92 | ||
93 | -- This unit implements a limited circuit for removal of redundant | |
94 | -- checks. The processing is based on a tracing of simple sequential | |
95 | -- flow. For any sequence of statements, we save expressions that are | |
96 | -- marked to be checked, and then if the same expression appears later | |
97 | -- with the same check, then under certain circumstances, the second | |
98 | -- check can be suppressed. | |
99 | ||
100 | -- Basically, we can suppress the check if we know for certain that | |
101 | -- the previous expression has been elaborated (together with its | |
102 | -- check), and we know that the exception frame is the same, and that | |
103 | -- nothing has happened to change the result of the exception. | |
104 | ||
105 | -- Let us examine each of these three conditions in turn to describe | |
106 | -- how we ensure that this condition is met. | |
107 | ||
108 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 109 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 110 | -- Conditional_Statements_Begin at the start of any statement sequence |
111 | -- and Conditional_Statements_End at the end. The End call causes all | |
112 | -- checks remembered since the Begin call to be discarded. This does | |
113 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
114 | -- no exception handlers. But the important thing is to be conservative. | |
115 | -- The other protection is that all checks are discarded if a label | |
116 | -- is encountered, since then the assumption of sequential execution | |
117 | -- is violated, and we don't know enough about the flow. | |
118 | ||
119 | -- Second, we need to know that the exception frame is the same. We | |
120 | -- do this by killing all remembered checks when we enter a new frame. | |
121 | -- Again, that's over-conservative, but generally the cases we can help | |
122 | -- with are pretty local anyway (like the body of a loop for example). | |
123 | ||
124 | -- Third, we must be sure to forget any checks which are no longer valid. | |
125 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
126 | -- used to note any changes to local variables. We only attempt to deal | |
127 | -- with checks involving local variables, so we do not need to worry | |
128 | -- about global variables. Second, a call to any non-global procedure | |
129 | -- causes us to abandon all stored checks, since such a all may affect | |
130 | -- the values of any local variables. | |
131 | ||
132 | -- The following define the data structures used to deal with remembering | |
133 | -- checks so that redundant checks can be eliminated as described above. | |
134 | ||
135 | -- Right now, the only expressions that we deal with are of the form of | |
136 | -- simple local objects (either declared locally, or IN parameters) or | |
137 | -- such objects plus/minus a compile time known constant. We can do | |
138 | -- more later on if it seems worthwhile, but this catches many simple | |
139 | -- cases in practice. | |
140 | ||
141 | -- The following record type reflects a single saved check. An entry | |
142 | -- is made in the stack of saved checks if and only if the expression | |
143 | -- has been elaborated with the indicated checks. | |
144 | ||
145 | type Saved_Check is record | |
146 | Killed : Boolean; | |
147 | -- Set True if entry is killed by Kill_Checks | |
148 | ||
149 | Entity : Entity_Id; | |
150 | -- The entity involved in the expression that is checked | |
151 | ||
152 | Offset : Uint; | |
153 | -- A compile time value indicating the result of adding or | |
154 | -- subtracting a compile time value. This value is to be | |
155 | -- added to the value of the Entity. A value of zero is | |
156 | -- used for the case of a simple entity reference. | |
157 | ||
158 | Check_Type : Character; | |
159 | -- This is set to 'R' for a range check (in which case Target_Type | |
160 | -- is set to the target type for the range check) or to 'O' for an | |
161 | -- overflow check (in which case Target_Type is set to Empty). | |
162 | ||
163 | Target_Type : Entity_Id; | |
164 | -- Used only if Do_Range_Check is set. Records the target type for | |
165 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 166 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 167 | -- range that is smaller or equal to the stored target type of a |
168 | -- saved check). | |
169 | end record; | |
170 | ||
171 | -- The following table keeps track of saved checks. Rather than use an | |
172 | -- extensible table. We just use a table of fixed size, and we discard | |
173 | -- any saved checks that do not fit. That's very unlikely to happen and | |
174 | -- this is only an optimization in any case. | |
175 | ||
176 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
177 | -- Array of saved checks | |
178 | ||
179 | Num_Saved_Checks : Nat := 0; | |
180 | -- Number of saved checks | |
181 | ||
182 | -- The following stack keeps track of statement ranges. It is treated | |
183 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
184 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
185 | -- at the time of the call. Then when Conditional_Statements_End is | |
186 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
187 | ||
188 | -- Note: again, this is a fixed length stack with a size that should | |
189 | -- always be fine. If the value of the stack pointer goes above the | |
190 | -- limit, then we just forget all saved checks. | |
191 | ||
192 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
193 | Saved_Checks_TOS : Nat := 0; | |
194 | ||
195 | ----------------------- | |
196 | -- Local Subprograms -- | |
197 | ----------------------- | |
ee6ba406 | 198 | |
0df9d43f | 199 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 200 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 201 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 202 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
203 | -- signed integer arithmetic operator (but not an if or case expression). | |
204 | -- It is also called for types other than signed integers. | |
3cce7f32 | 205 | |
206 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
207 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 208 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
209 | -- arithmetic op (which includes the case of if and case expressions). Note | |
210 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
211 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 212 | |
2fe22c69 | 213 | procedure Apply_Division_Check |
214 | (N : Node_Id; | |
215 | Rlo : Uint; | |
216 | Rhi : Uint; | |
217 | ROK : Boolean); | |
218 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
219 | -- division checks as required if the Do_Division_Check flag is set. | |
220 | -- Rlo and Rhi give the possible range of the right operand, these values | |
221 | -- can be referenced and trusted only if ROK is set True. | |
222 | ||
223 | procedure Apply_Float_Conversion_Check | |
224 | (Ck_Node : Node_Id; | |
225 | Target_Typ : Entity_Id); | |
226 | -- The checks on a conversion from a floating-point type to an integer | |
227 | -- type are delicate. They have to be performed before conversion, they | |
228 | -- have to raise an exception when the operand is a NaN, and rounding must | |
229 | -- be taken into account to determine the safe bounds of the operand. | |
230 | ||
ee6ba406 | 231 | procedure Apply_Selected_Length_Checks |
232 | (Ck_Node : Node_Id; | |
233 | Target_Typ : Entity_Id; | |
234 | Source_Typ : Entity_Id; | |
235 | Do_Static : Boolean); | |
236 | -- This is the subprogram that does all the work for Apply_Length_Check | |
237 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
238 | -- described for the above routines. The Do_Static flag indicates that | |
239 | -- only a static check is to be done. | |
240 | ||
241 | procedure Apply_Selected_Range_Checks | |
242 | (Ck_Node : Node_Id; | |
243 | Target_Typ : Entity_Id; | |
244 | Source_Typ : Entity_Id; | |
245 | Do_Static : Boolean); | |
246 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
247 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
248 | -- routine. The Do_Static flag indicates that only a static check is | |
249 | -- to be done. | |
250 | ||
2af58f67 | 251 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 252 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
253 | -- This function is used to see if an access or division by zero check is | |
254 | -- needed. The check is to be applied to a single variable appearing in the | |
255 | -- source, and N is the node for the reference. If N is not of this form, | |
256 | -- True is returned with no further processing. If N is of the right form, | |
257 | -- then further processing determines if the given Check is needed. | |
258 | -- | |
259 | -- The particular circuit is to see if we have the case of a check that is | |
260 | -- not needed because it appears in the right operand of a short circuited | |
261 | -- conditional where the left operand guards the check. For example: | |
262 | -- | |
263 | -- if Var = 0 or else Q / Var > 12 then | |
264 | -- ... | |
265 | -- end if; | |
266 | -- | |
267 | -- In this example, the division check is not required. At the same time | |
268 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
269 | -- such as: | |
270 | -- | |
271 | -- if Var = 0 or Q / Var > 12 then | |
272 | -- ... | |
273 | -- end if; | |
274 | ||
9dfe12ae | 275 | procedure Find_Check |
276 | (Expr : Node_Id; | |
277 | Check_Type : Character; | |
278 | Target_Type : Entity_Id; | |
279 | Entry_OK : out Boolean; | |
280 | Check_Num : out Nat; | |
281 | Ent : out Entity_Id; | |
282 | Ofs : out Uint); | |
283 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
284 | -- to see if a check is of the form for optimization, and if so, to see | |
285 | -- if it has already been performed. Expr is the expression to check, | |
286 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
287 | -- Target_Type is the target type for a range check, and Empty for an | |
288 | -- overflow check. If the entry is not of the form for optimization, | |
289 | -- then Entry_OK is set to False, and the remaining out parameters | |
290 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
291 | -- entity and offset from the expression. Check_Num is the number of | |
292 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
293 | -- is located. | |
294 | ||
ee6ba406 | 295 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
296 | -- If a discriminal is used in constraining a prival, Return reference | |
297 | -- to the discriminal of the protected body (which renames the parameter | |
298 | -- of the enclosing protected operation). This clumsy transformation is | |
299 | -- needed because privals are created too late and their actual subtypes | |
300 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 301 | -- This function is called whenever the bound is an entity and the scope |
302 | -- indicates a protected operation. If the bound is an in-parameter of | |
303 | -- a protected operation that is not a prival, the function returns the | |
304 | -- bound itself. | |
ee6ba406 | 305 | -- To be cleaned up??? |
306 | ||
307 | function Guard_Access | |
308 | (Cond : Node_Id; | |
309 | Loc : Source_Ptr; | |
314a23b6 | 310 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 311 | -- In the access type case, guard the test with a test to ensure |
312 | -- that the access value is non-null, since the checks do not | |
313 | -- not apply to null access values. | |
314 | ||
315 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
316 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
317 | -- Constraint_Error node. | |
318 | ||
3cce7f32 | 319 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
320 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 321 | -- integer operands. This includes unary and binary operators, and also |
322 | -- if and case expression nodes where the dependent expressions are of | |
323 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 324 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 325 | |
0577b0b1 | 326 | function Range_Or_Validity_Checks_Suppressed |
327 | (Expr : Node_Id) return Boolean; | |
328 | -- Returns True if either range or validity checks or both are suppressed | |
329 | -- for the type of the given expression, or, if the expression is the name | |
330 | -- of an entity, if these checks are suppressed for the entity. | |
331 | ||
ee6ba406 | 332 | function Selected_Length_Checks |
333 | (Ck_Node : Node_Id; | |
334 | Target_Typ : Entity_Id; | |
335 | Source_Typ : Entity_Id; | |
314a23b6 | 336 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 337 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
338 | -- anything, just returns a list of nodes as described in the spec of | |
339 | -- this package for the Range_Check function. | |
340 | ||
341 | function Selected_Range_Checks | |
342 | (Ck_Node : Node_Id; | |
343 | Target_Typ : Entity_Id; | |
344 | Source_Typ : Entity_Id; | |
314a23b6 | 345 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 346 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
347 | -- just returns a list of nodes as described in the spec of this package | |
348 | -- for the Range_Check function. | |
349 | ||
350 | ------------------------------ | |
351 | -- Access_Checks_Suppressed -- | |
352 | ------------------------------ | |
353 | ||
354 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
355 | begin | |
9dfe12ae | 356 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
357 | return Is_Check_Suppressed (E, Access_Check); | |
358 | else | |
fafc6b97 | 359 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 360 | end if; |
ee6ba406 | 361 | end Access_Checks_Suppressed; |
362 | ||
363 | ------------------------------------- | |
364 | -- Accessibility_Checks_Suppressed -- | |
365 | ------------------------------------- | |
366 | ||
367 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
368 | begin | |
9dfe12ae | 369 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
370 | return Is_Check_Suppressed (E, Accessibility_Check); | |
371 | else | |
fafc6b97 | 372 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 373 | end if; |
ee6ba406 | 374 | end Accessibility_Checks_Suppressed; |
375 | ||
00c403ee | 376 | ----------------------------- |
377 | -- Activate_Division_Check -- | |
378 | ----------------------------- | |
379 | ||
380 | procedure Activate_Division_Check (N : Node_Id) is | |
381 | begin | |
382 | Set_Do_Division_Check (N, True); | |
383 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
384 | end Activate_Division_Check; | |
385 | ||
386 | ----------------------------- | |
387 | -- Activate_Overflow_Check -- | |
388 | ----------------------------- | |
389 | ||
390 | procedure Activate_Overflow_Check (N : Node_Id) is | |
391 | begin | |
b8446e0d | 392 | -- Nothing to do for unconstrained floating-point types (the test for |
393 | -- Etype (N) being present seems necessary in some cases, should be | |
394 | -- tracked down, but for now just ignore the check in this case ???) | |
395 | ||
396 | if Present (Etype (N)) | |
397 | and then Is_Floating_Point_Type (Etype (N)) | |
398 | and then not Is_Constrained (Etype (N)) | |
399 | ||
400 | -- But do the check after all if float overflow checking enforced | |
401 | ||
402 | and then not Check_Float_Overflow | |
403 | then | |
404 | return; | |
d32ceaf3 | 405 | end if; |
b8446e0d | 406 | |
407 | -- Nothing to do for Rem/Mod/Plus (overflow not possible) | |
408 | ||
409 | if Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then | |
410 | return; | |
411 | end if; | |
412 | ||
413 | -- Otherwise set the flag | |
414 | ||
415 | Set_Do_Overflow_Check (N, True); | |
416 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
00c403ee | 417 | end Activate_Overflow_Check; |
418 | ||
419 | -------------------------- | |
420 | -- Activate_Range_Check -- | |
421 | -------------------------- | |
422 | ||
423 | procedure Activate_Range_Check (N : Node_Id) is | |
424 | begin | |
425 | Set_Do_Range_Check (N, True); | |
426 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
427 | end Activate_Range_Check; | |
428 | ||
0577b0b1 | 429 | --------------------------------- |
430 | -- Alignment_Checks_Suppressed -- | |
431 | --------------------------------- | |
432 | ||
433 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
434 | begin | |
435 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
436 | return Is_Check_Suppressed (E, Alignment_Check); | |
437 | else | |
fafc6b97 | 438 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 439 | end if; |
440 | end Alignment_Checks_Suppressed; | |
441 | ||
2d70530c | 442 | ---------------------------------- |
443 | -- Allocation_Checks_Suppressed -- | |
444 | ---------------------------------- | |
445 | ||
fa771c05 | 446 | -- Note: at the current time there are no calls to this function, because |
447 | -- the relevant check is in the run-time, so it is not a check that the | |
448 | -- compiler can suppress anyway, but we still have to recognize the check | |
449 | -- name Allocation_Check since it is part of the standard. | |
450 | ||
2d70530c | 451 | function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
452 | begin | |
453 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
454 | return Is_Check_Suppressed (E, Allocation_Check); | |
455 | else | |
456 | return Scope_Suppress.Suppress (Allocation_Check); | |
457 | end if; | |
458 | end Allocation_Checks_Suppressed; | |
459 | ||
ee6ba406 | 460 | ------------------------- |
461 | -- Append_Range_Checks -- | |
462 | ------------------------- | |
463 | ||
464 | procedure Append_Range_Checks | |
465 | (Checks : Check_Result; | |
466 | Stmts : List_Id; | |
467 | Suppress_Typ : Entity_Id; | |
468 | Static_Sloc : Source_Ptr; | |
469 | Flag_Node : Node_Id) | |
470 | is | |
9dfe12ae | 471 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
472 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
473 | ||
ee6ba406 | 474 | Checks_On : constant Boolean := |
b6341c67 | 475 | (not Index_Checks_Suppressed (Suppress_Typ)) |
476 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 477 | |
478 | begin | |
479 | -- For now we just return if Checks_On is false, however this should | |
480 | -- be enhanced to check for an always True value in the condition | |
481 | -- and to generate a compilation warning??? | |
482 | ||
483 | if not Checks_On then | |
484 | return; | |
485 | end if; | |
486 | ||
487 | for J in 1 .. 2 loop | |
488 | exit when No (Checks (J)); | |
489 | ||
490 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
491 | and then Present (Condition (Checks (J))) | |
492 | then | |
493 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
494 | Append_To (Stmts, Checks (J)); | |
495 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
496 | end if; | |
497 | ||
498 | else | |
499 | Append_To | |
f15731c4 | 500 | (Stmts, |
501 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
502 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 503 | end if; |
504 | end loop; | |
505 | end Append_Range_Checks; | |
506 | ||
507 | ------------------------ | |
508 | -- Apply_Access_Check -- | |
509 | ------------------------ | |
510 | ||
511 | procedure Apply_Access_Check (N : Node_Id) is | |
512 | P : constant Node_Id := Prefix (N); | |
513 | ||
514 | begin | |
13dbf220 | 515 | -- We do not need checks if we are not generating code (i.e. the |
516 | -- expander is not active). This is not just an optimization, there | |
517 | -- are cases (e.g. with pragma Debug) where generating the checks | |
518 | -- can cause real trouble). | |
284faf8b | 519 | |
a33565dd | 520 | if not Expander_Active then |
13dbf220 | 521 | return; |
9dfe12ae | 522 | end if; |
ee6ba406 | 523 | |
84d0d4a5 | 524 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 525 | |
84d0d4a5 | 526 | if not Check_Needed (P, Access_Check) then |
527 | return; | |
ee6ba406 | 528 | end if; |
9dfe12ae | 529 | |
cc60bd16 | 530 | -- No check if accessing the Offset_To_Top component of a dispatch |
531 | -- table. They are safe by construction. | |
532 | ||
040277b1 | 533 | if Tagged_Type_Expansion |
534 | and then Present (Etype (P)) | |
cc60bd16 | 535 | and then RTU_Loaded (Ada_Tags) |
536 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
537 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
538 | then | |
539 | return; | |
540 | end if; | |
541 | ||
84d0d4a5 | 542 | -- Otherwise go ahead and install the check |
9dfe12ae | 543 | |
fa7497e8 | 544 | Install_Null_Excluding_Check (P); |
ee6ba406 | 545 | end Apply_Access_Check; |
546 | ||
547 | ------------------------------- | |
548 | -- Apply_Accessibility_Check -- | |
549 | ------------------------------- | |
550 | ||
55dc6dc2 | 551 | procedure Apply_Accessibility_Check |
552 | (N : Node_Id; | |
553 | Typ : Entity_Id; | |
554 | Insert_Node : Node_Id) | |
555 | is | |
ee6ba406 | 556 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 557 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 558 | Param_Level : Node_Id; |
559 | Type_Level : Node_Id; | |
560 | ||
561 | begin | |
47d210a3 | 562 | if Ada_Version >= Ada_2012 |
563 | and then not Present (Param_Ent) | |
564 | and then Is_Entity_Name (N) | |
565 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
566 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
567 | then | |
568 | Param_Ent := Entity (N); | |
569 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 570 | |
47d210a3 | 571 | -- Renamed_Object must return an Entity_Name here |
572 | -- because of preceding "Present (E_E_A (...))" test. | |
573 | ||
574 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
575 | end loop; | |
576 | end if; | |
577 | ||
ee6ba406 | 578 | if Inside_A_Generic then |
579 | return; | |
580 | ||
6ffc64fc | 581 | -- Only apply the run-time check if the access parameter has an |
582 | -- associated extra access level parameter and when the level of the | |
583 | -- type is less deep than the level of the access parameter, and | |
584 | -- accessibility checks are not suppressed. | |
ee6ba406 | 585 | |
586 | elsif Present (Param_Ent) | |
587 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 588 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 589 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 590 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
591 | and then not Accessibility_Checks_Suppressed (Typ) | |
592 | then | |
593 | Param_Level := | |
594 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
595 | ||
1a9cc6cd | 596 | Type_Level := |
597 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
ee6ba406 | 598 | |
bf3e1520 | 599 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 600 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 601 | |
55dc6dc2 | 602 | Insert_Action (Insert_Node, |
ee6ba406 | 603 | Make_Raise_Program_Error (Loc, |
604 | Condition => | |
605 | Make_Op_Gt (Loc, | |
606 | Left_Opnd => Param_Level, | |
f15731c4 | 607 | Right_Opnd => Type_Level), |
608 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 609 | |
610 | Analyze_And_Resolve (N); | |
611 | end if; | |
612 | end Apply_Accessibility_Check; | |
613 | ||
0577b0b1 | 614 | -------------------------------- |
615 | -- Apply_Address_Clause_Check -- | |
616 | -------------------------------- | |
617 | ||
618 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 619 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
620 | ||
0577b0b1 | 621 | AC : constant Node_Id := Address_Clause (E); |
622 | Loc : constant Source_Ptr := Sloc (AC); | |
623 | Typ : constant Entity_Id := Etype (E); | |
624 | Aexp : constant Node_Id := Expression (AC); | |
c2b56224 | 625 | |
c2b56224 | 626 | Expr : Node_Id; |
0577b0b1 | 627 | -- Address expression (not necessarily the same as Aexp, for example |
628 | -- when Aexp is a reference to a constant, in which case Expr gets | |
7b8fa048 | 629 | -- reset to reference the value expression of the constant). |
0577b0b1 | 630 | |
0577b0b1 | 631 | procedure Compile_Time_Bad_Alignment; |
632 | -- Post error warnings when alignment is known to be incompatible. Note | |
633 | -- that we do not go as far as inserting a raise of Program_Error since | |
634 | -- this is an erroneous case, and it may happen that we are lucky and an | |
d6da7448 | 635 | -- underaligned address turns out to be OK after all. |
0577b0b1 | 636 | |
637 | -------------------------------- | |
638 | -- Compile_Time_Bad_Alignment -- | |
639 | -------------------------------- | |
640 | ||
641 | procedure Compile_Time_Bad_Alignment is | |
642 | begin | |
d6da7448 | 643 | if Address_Clause_Overlay_Warnings then |
0577b0b1 | 644 | Error_Msg_FE |
cb97ae5c | 645 | ("?o?specified address for& may be inconsistent with alignment", |
0577b0b1 | 646 | Aexp, E); |
647 | Error_Msg_FE | |
cb97ae5c | 648 | ("\?o?program execution may be erroneous (RM 13.3(27))", |
0577b0b1 | 649 | Aexp, E); |
83f8f0a6 | 650 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 651 | end if; |
652 | end Compile_Time_Bad_Alignment; | |
c2b56224 | 653 | |
2af58f67 | 654 | -- Start of processing for Apply_Address_Clause_Check |
5c61a0ff | 655 | |
c2b56224 | 656 | begin |
d6da7448 | 657 | -- See if alignment check needed. Note that we never need a check if the |
658 | -- maximum alignment is one, since the check will always succeed. | |
659 | ||
660 | -- Note: we do not check for checks suppressed here, since that check | |
661 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
662 | -- only called if checks were not suppressed. The reason for this is | |
663 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
664 | -- time (so that all types etc are elaborated), but we have to check | |
665 | -- the status of check suppressing at the point of the address clause. | |
666 | ||
667 | if No (AC) | |
668 | or else not Check_Address_Alignment (AC) | |
669 | or else Maximum_Alignment = 1 | |
670 | then | |
671 | return; | |
672 | end if; | |
673 | ||
674 | -- Obtain expression from address clause | |
9dfe12ae | 675 | |
0577b0b1 | 676 | Expr := Expression (AC); |
677 | ||
678 | -- The following loop digs for the real expression to use in the check | |
679 | ||
680 | loop | |
681 | -- For constant, get constant expression | |
682 | ||
683 | if Is_Entity_Name (Expr) | |
684 | and then Ekind (Entity (Expr)) = E_Constant | |
685 | then | |
686 | Expr := Constant_Value (Entity (Expr)); | |
687 | ||
688 | -- For unchecked conversion, get result to convert | |
689 | ||
690 | elsif Nkind (Expr) = N_Unchecked_Type_Conversion then | |
691 | Expr := Expression (Expr); | |
692 | ||
693 | -- For (common case) of To_Address call, get argument | |
694 | ||
695 | elsif Nkind (Expr) = N_Function_Call | |
696 | and then Is_Entity_Name (Name (Expr)) | |
697 | and then Is_RTE (Entity (Name (Expr)), RE_To_Address) | |
698 | then | |
699 | Expr := First (Parameter_Associations (Expr)); | |
700 | ||
701 | if Nkind (Expr) = N_Parameter_Association then | |
702 | Expr := Explicit_Actual_Parameter (Expr); | |
703 | end if; | |
704 | ||
705 | -- We finally have the real expression | |
706 | ||
707 | else | |
708 | exit; | |
709 | end if; | |
710 | end loop; | |
711 | ||
d6da7448 | 712 | -- See if we know that Expr has a bad alignment at compile time |
c2b56224 | 713 | |
714 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 715 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 716 | then |
f2a06be9 | 717 | declare |
718 | AL : Uint := Alignment (Typ); | |
719 | ||
720 | begin | |
721 | -- The object alignment might be more restrictive than the | |
722 | -- type alignment. | |
723 | ||
724 | if Known_Alignment (E) then | |
725 | AL := Alignment (E); | |
726 | end if; | |
727 | ||
728 | if Expr_Value (Expr) mod AL /= 0 then | |
0577b0b1 | 729 | Compile_Time_Bad_Alignment; |
730 | else | |
731 | return; | |
f2a06be9 | 732 | end if; |
733 | end; | |
c2b56224 | 734 | |
0577b0b1 | 735 | -- If the expression has the form X'Address, then we can find out if |
736 | -- the object X has an alignment that is compatible with the object E. | |
d6da7448 | 737 | -- If it hasn't or we don't know, we defer issuing the warning until |
738 | -- the end of the compilation to take into account back end annotations. | |
c2b56224 | 739 | |
0577b0b1 | 740 | elsif Nkind (Expr) = N_Attribute_Reference |
741 | and then Attribute_Name (Expr) = Name_Address | |
d6da7448 | 742 | and then Has_Compatible_Alignment (E, Prefix (Expr)) = Known_Compatible |
0577b0b1 | 743 | then |
d6da7448 | 744 | return; |
0577b0b1 | 745 | end if; |
c2b56224 | 746 | |
6fb3c314 | 747 | -- Here we do not know if the value is acceptable. Strictly we don't |
748 | -- have to do anything, since if the alignment is bad, we have an | |
749 | -- erroneous program. However we are allowed to check for erroneous | |
750 | -- conditions and we decide to do this by default if the check is not | |
751 | -- suppressed. | |
0577b0b1 | 752 | |
753 | -- However, don't do the check if elaboration code is unwanted | |
754 | ||
755 | if Restriction_Active (No_Elaboration_Code) then | |
756 | return; | |
757 | ||
758 | -- Generate a check to raise PE if alignment may be inappropriate | |
759 | ||
760 | else | |
761 | -- If the original expression is a non-static constant, use the | |
762 | -- name of the constant itself rather than duplicating its | |
00c403ee | 763 | -- defining expression, which was extracted above. |
0577b0b1 | 764 | |
00c403ee | 765 | -- Note: Expr is empty if the address-clause is applied to in-mode |
766 | -- actuals (allowed by 13.1(22)). | |
767 | ||
768 | if not Present (Expr) | |
769 | or else | |
770 | (Is_Entity_Name (Expression (AC)) | |
771 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
772 | and then Nkind (Parent (Entity (Expression (AC)))) | |
773 | = N_Object_Declaration) | |
0577b0b1 | 774 | then |
775 | Expr := New_Copy_Tree (Expression (AC)); | |
776 | else | |
777 | Remove_Side_Effects (Expr); | |
c2b56224 | 778 | end if; |
c2b56224 | 779 | |
d950dc79 | 780 | if No (Actions (N)) then |
781 | Set_Actions (N, New_List); | |
782 | end if; | |
783 | ||
784 | Prepend_To (Actions (N), | |
0577b0b1 | 785 | Make_Raise_Program_Error (Loc, |
786 | Condition => | |
787 | Make_Op_Ne (Loc, | |
788 | Left_Opnd => | |
789 | Make_Op_Mod (Loc, | |
790 | Left_Opnd => | |
791 | Unchecked_Convert_To | |
792 | (RTE (RE_Integer_Address), Expr), | |
793 | Right_Opnd => | |
794 | Make_Attribute_Reference (Loc, | |
d950dc79 | 795 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 796 | Attribute_Name => Name_Alignment)), |
797 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7b8fa048 | 798 | Reason => PE_Misaligned_Address_Value)); |
799 | ||
800 | Warning_Msg := No_Error_Msg; | |
d950dc79 | 801 | Analyze (First (Actions (N)), Suppress => All_Checks); |
cd309f05 | 802 | |
7b8fa048 | 803 | -- If the address clause generated a warning message (for example, |
804 | -- from Warn_On_Non_Local_Exception mode with the active restriction | |
805 | -- No_Exception_Propagation). | |
806 | ||
807 | if Warning_Msg /= No_Error_Msg then | |
808 | ||
809 | -- If the expression has a known at compile time value, then | |
810 | -- once we know the alignment of the type, we can check if the | |
811 | -- exception will be raised or not, and if not, we don't need | |
812 | -- the warning so we will kill the warning later on. | |
813 | ||
814 | if Compile_Time_Known_Value (Expr) then | |
815 | Alignment_Warnings.Append | |
816 | ((E => E, A => Expr_Value (Expr), W => Warning_Msg)); | |
817 | end if; | |
818 | ||
819 | -- Add explanation of the warning that is generated by the check | |
cd309f05 | 820 | |
78be29d1 | 821 | Error_Msg_N |
7b8fa048 | 822 | ("\address value may be incompatible with alignment " |
823 | & "of object?X?", AC); | |
cd309f05 | 824 | end if; |
78be29d1 | 825 | |
0577b0b1 | 826 | return; |
827 | end if; | |
9dfe12ae | 828 | |
829 | exception | |
0577b0b1 | 830 | -- If we have some missing run time component in configurable run time |
831 | -- mode then just skip the check (it is not required in any case). | |
832 | ||
9dfe12ae | 833 | when RE_Not_Available => |
834 | return; | |
0577b0b1 | 835 | end Apply_Address_Clause_Check; |
c2b56224 | 836 | |
ee6ba406 | 837 | ------------------------------------- |
838 | -- Apply_Arithmetic_Overflow_Check -- | |
839 | ------------------------------------- | |
840 | ||
3cce7f32 | 841 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
842 | begin | |
843 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 844 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 845 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 846 | |
0df9d43f | 847 | if Overflow_Check_Mode = Strict |
3cce7f32 | 848 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
849 | then | |
0df9d43f | 850 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 851 | |
21a55437 | 852 | -- Otherwise use the new routine for the case of a signed integer |
853 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
854 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 855 | |
856 | else | |
857 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
858 | end if; | |
859 | end Apply_Arithmetic_Overflow_Check; | |
860 | ||
0df9d43f | 861 | -------------------------------------- |
862 | -- Apply_Arithmetic_Overflow_Strict -- | |
863 | -------------------------------------- | |
3cce7f32 | 864 | |
f40f9731 | 865 | -- This routine is called only if the type is an integer type, and a |
866 | -- software arithmetic overflow check may be needed for op (add, subtract, | |
867 | -- or multiply). This check is performed only if Software_Overflow_Checking | |
868 | -- is enabled and Do_Overflow_Check is set. In this case we expand the | |
869 | -- operation into a more complex sequence of tests that ensures that | |
870 | -- overflow is properly caught. | |
ee6ba406 | 871 | |
0df9d43f | 872 | -- This is used in CHECKED modes. It is identical to the code for this |
873 | -- cases before the big overflow earthquake, thus ensuring that in this | |
874 | -- modes we have compatible behavior (and reliability) to what was there | |
875 | -- before. It is also called for types other than signed integers, and if | |
876 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 877 | |
878 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
879 | -- to give up and just generate an overflow check without any fuss. | |
880 | ||
0df9d43f | 881 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 882 | Loc : constant Source_Ptr := Sloc (N); |
883 | Typ : constant Entity_Id := Etype (N); | |
884 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 885 | |
886 | begin | |
0df9d43f | 887 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
888 | -- suppressed. | |
889 | ||
890 | if not Do_Overflow_Check (N) then | |
891 | return; | |
892 | end if; | |
893 | ||
f40f9731 | 894 | -- An interesting special case. If the arithmetic operation appears as |
895 | -- the operand of a type conversion: | |
896 | ||
897 | -- type1 (x op y) | |
898 | ||
899 | -- and all the following conditions apply: | |
900 | ||
901 | -- arithmetic operation is for a signed integer type | |
902 | -- target type type1 is a static integer subtype | |
903 | -- range of x and y are both included in the range of type1 | |
904 | -- range of x op y is included in the range of type1 | |
905 | -- size of type1 is at least twice the result size of op | |
906 | ||
907 | -- then we don't do an overflow check in any case, instead we transform | |
908 | -- the operation so that we end up with: | |
909 | ||
910 | -- type1 (type1 (x) op type1 (y)) | |
911 | ||
912 | -- This avoids intermediate overflow before the conversion. It is | |
913 | -- explicitly permitted by RM 3.5.4(24): | |
914 | ||
915 | -- For the execution of a predefined operation of a signed integer | |
916 | -- type, the implementation need not raise Constraint_Error if the | |
917 | -- result is outside the base range of the type, so long as the | |
918 | -- correct result is produced. | |
919 | ||
920 | -- It's hard to imagine that any programmer counts on the exception | |
921 | -- being raised in this case, and in any case it's wrong coding to | |
922 | -- have this expectation, given the RM permission. Furthermore, other | |
923 | -- Ada compilers do allow such out of range results. | |
924 | ||
925 | -- Note that we do this transformation even if overflow checking is | |
926 | -- off, since this is precisely about giving the "right" result and | |
927 | -- avoiding the need for an overflow check. | |
928 | ||
8eb4a5eb | 929 | -- Note: this circuit is partially redundant with respect to the similar |
930 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
931 | -- with cases that do not come through here. We still need the following | |
932 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
933 | -- sure not to generate the arithmetic overflow check in these cases | |
934 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
935 | ||
f40f9731 | 936 | if Is_Signed_Integer_Type (Typ) |
937 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 938 | then |
f32c377d | 939 | Conversion_Optimization : declare |
f40f9731 | 940 | Target_Type : constant Entity_Id := |
b6341c67 | 941 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 942 | |
943 | Llo, Lhi : Uint; | |
944 | Rlo, Rhi : Uint; | |
945 | LOK, ROK : Boolean; | |
946 | ||
947 | Vlo : Uint; | |
948 | Vhi : Uint; | |
949 | VOK : Boolean; | |
950 | ||
951 | Tlo : Uint; | |
952 | Thi : Uint; | |
953 | ||
954 | begin | |
955 | if Is_Integer_Type (Target_Type) | |
956 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
957 | then | |
958 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
959 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
960 | ||
9c486805 | 961 | Determine_Range |
962 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
963 | Determine_Range | |
964 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 965 | |
966 | if (LOK and ROK) | |
967 | and then Tlo <= Llo and then Lhi <= Thi | |
968 | and then Tlo <= Rlo and then Rhi <= Thi | |
969 | then | |
9c486805 | 970 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 971 | |
972 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
973 | Rewrite (Left_Opnd (N), | |
974 | Make_Type_Conversion (Loc, | |
975 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
976 | Expression => Relocate_Node (Left_Opnd (N)))); | |
977 | ||
978 | Rewrite (Right_Opnd (N), | |
979 | Make_Type_Conversion (Loc, | |
980 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
981 | Expression => Relocate_Node (Right_Opnd (N)))); | |
982 | ||
780bfb21 | 983 | -- Rewrite the conversion operand so that the original |
984 | -- node is retained, in order to avoid the warning for | |
985 | -- redundant conversions in Resolve_Type_Conversion. | |
986 | ||
987 | Rewrite (N, Relocate_Node (N)); | |
988 | ||
f40f9731 | 989 | Set_Etype (N, Target_Type); |
780bfb21 | 990 | |
f40f9731 | 991 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
992 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
993 | ||
994 | -- Given that the target type is twice the size of the | |
995 | -- source type, overflow is now impossible, so we can | |
996 | -- safely kill the overflow check and return. | |
997 | ||
998 | Set_Do_Overflow_Check (N, False); | |
999 | return; | |
1000 | end if; | |
1001 | end if; | |
1002 | end if; | |
f32c377d | 1003 | end Conversion_Optimization; |
ee6ba406 | 1004 | end if; |
1005 | ||
f40f9731 | 1006 | -- Now see if an overflow check is required |
1007 | ||
1008 | declare | |
1009 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
1010 | Dsiz : constant Int := Siz * 2; | |
1011 | Opnod : Node_Id; | |
1012 | Ctyp : Entity_Id; | |
1013 | Opnd : Node_Id; | |
1014 | Cent : RE_Id; | |
ee6ba406 | 1015 | |
f40f9731 | 1016 | begin |
1017 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 1018 | -- is not set anyway, or we are not doing code expansion, or the |
1019 | -- parent node is a type conversion whose operand is an arithmetic | |
1020 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 1021 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 1022 | |
f40f9731 | 1023 | -- Special case CLI target, where arithmetic overflow checks can be |
1024 | -- performed for integer and long_integer | |
ee6ba406 | 1025 | |
f40f9731 | 1026 | if Backend_Overflow_Checks_On_Target |
1027 | or else not Do_Overflow_Check (N) | |
a33565dd | 1028 | or else not Expander_Active |
df40eeb0 | 1029 | or else (Present (Parent (N)) |
1030 | and then Nkind (Parent (N)) = N_Type_Conversion | |
1031 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 1032 | or else |
1033 | (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size) | |
1034 | then | |
1035 | return; | |
1036 | end if; | |
ee6ba406 | 1037 | |
f40f9731 | 1038 | -- Otherwise, generate the full general code for front end overflow |
1039 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 1040 | |
f40f9731 | 1041 | -- x op y |
ee6ba406 | 1042 | |
f40f9731 | 1043 | -- is expanded into |
ee6ba406 | 1044 | |
f40f9731 | 1045 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 1046 | |
f40f9731 | 1047 | -- where Typ is the type of the original expression, and Checktyp is |
1048 | -- an integer type of sufficient length to hold the largest possible | |
1049 | -- result. | |
ee6ba406 | 1050 | |
f40f9731 | 1051 | -- If the size of check type exceeds the size of Long_Long_Integer, |
1052 | -- we use a different approach, expanding to: | |
ee6ba406 | 1053 | |
f40f9731 | 1054 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 1055 | |
f40f9731 | 1056 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 1057 | |
f40f9731 | 1058 | -- Find check type if one exists |
1059 | ||
1060 | if Dsiz <= Standard_Integer_Size then | |
1061 | Ctyp := Standard_Integer; | |
ee6ba406 | 1062 | |
f40f9731 | 1063 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
1064 | Ctyp := Standard_Long_Long_Integer; | |
1065 | ||
c9f84db7 | 1066 | -- No check type exists, use runtime call |
ee6ba406 | 1067 | |
1068 | else | |
f40f9731 | 1069 | if Nkind (N) = N_Op_Add then |
1070 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1071 | |
f40f9731 | 1072 | elsif Nkind (N) = N_Op_Multiply then |
1073 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1074 | |
f40f9731 | 1075 | else |
1076 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1077 | Cent := RE_Subtract_With_Ovflo_Check; | |
1078 | end if; | |
1079 | ||
1080 | Rewrite (N, | |
1081 | OK_Convert_To (Typ, | |
1082 | Make_Function_Call (Loc, | |
83c6c069 | 1083 | Name => New_Occurrence_Of (RTE (Cent), Loc), |
f40f9731 | 1084 | Parameter_Associations => New_List ( |
1085 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1086 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1087 | |
f40f9731 | 1088 | Analyze_And_Resolve (N, Typ); |
1089 | return; | |
1090 | end if; | |
ee6ba406 | 1091 | |
f40f9731 | 1092 | -- If we fall through, we have the case where we do the arithmetic |
1093 | -- in the next higher type and get the check by conversion. In these | |
1094 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1095 | |
f40f9731 | 1096 | Opnod := Relocate_Node (N); |
ee6ba406 | 1097 | |
f40f9731 | 1098 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1099 | |
f40f9731 | 1100 | Analyze (Opnd); |
1101 | Set_Etype (Opnd, Ctyp); | |
1102 | Set_Analyzed (Opnd, True); | |
1103 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1104 | |
f40f9731 | 1105 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1106 | |
f40f9731 | 1107 | Analyze (Opnd); |
1108 | Set_Etype (Opnd, Ctyp); | |
1109 | Set_Analyzed (Opnd, True); | |
1110 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1111 | |
f40f9731 | 1112 | -- The type of the operation changes to the base type of the check |
1113 | -- type, and we reset the overflow check indication, since clearly no | |
1114 | -- overflow is possible now that we are using a double length type. | |
1115 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1116 | -- expand the node. | |
ee6ba406 | 1117 | |
f40f9731 | 1118 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1119 | Set_Do_Overflow_Check (Opnod, False); | |
1120 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1121 | |
f40f9731 | 1122 | -- Now build the outer conversion |
ee6ba406 | 1123 | |
f40f9731 | 1124 | Opnd := OK_Convert_To (Typ, Opnod); |
1125 | Analyze (Opnd); | |
1126 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1127 | |
f40f9731 | 1128 | -- In the discrete type case, we directly generate the range check |
1129 | -- for the outer operand. This range check will implement the | |
1130 | -- required overflow check. | |
9dfe12ae | 1131 | |
f40f9731 | 1132 | if Is_Discrete_Type (Typ) then |
1133 | Rewrite (N, Opnd); | |
1134 | Generate_Range_Check | |
1135 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1136 | |
f40f9731 | 1137 | -- For other types, we enable overflow checking on the conversion, |
1138 | -- after setting the node as analyzed to prevent recursive attempts | |
1139 | -- to expand the conversion node. | |
9dfe12ae | 1140 | |
f40f9731 | 1141 | else |
1142 | Set_Analyzed (Opnd, True); | |
1143 | Enable_Overflow_Check (Opnd); | |
1144 | Rewrite (N, Opnd); | |
1145 | end if; | |
1146 | ||
1147 | exception | |
1148 | when RE_Not_Available => | |
1149 | return; | |
1150 | end; | |
0df9d43f | 1151 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1152 | |
1153 | ---------------------------------------------------- | |
1154 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1155 | ---------------------------------------------------- | |
1156 | ||
1157 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1158 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1159 | |
1160 | Loc : constant Source_Ptr := Sloc (Op); | |
1161 | P : constant Node_Id := Parent (Op); | |
1162 | ||
49b3a812 | 1163 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1164 | -- Operands and results are of this type when we convert | |
1165 | ||
3cce7f32 | 1166 | Result_Type : constant Entity_Id := Etype (Op); |
1167 | -- Original result type | |
1168 | ||
db415383 | 1169 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1170 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1171 | ||
1172 | Lo, Hi : Uint; | |
1173 | -- Ranges of values for result | |
1174 | ||
1175 | begin | |
1176 | -- Nothing to do if our parent is one of the following: | |
1177 | ||
0326b4d4 | 1178 | -- Another signed integer arithmetic op |
3cce7f32 | 1179 | -- A membership operation |
1180 | -- A comparison operation | |
1181 | ||
1182 | -- In all these cases, we will process at the higher level (and then | |
1183 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1184 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1185 | |
1186 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1187 | or else Nkind (P) in N_Membership_Test |
1188 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1189 | |
70a2dff4 | 1190 | -- This is also true for an alternative in a case expression |
1191 | ||
1192 | or else Nkind (P) = N_Case_Expression_Alternative | |
1193 | ||
1194 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1195 | |
b8a17a21 | 1196 | or else (Nkind (P) = N_Range |
1197 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1198 | then |
1199 | return; | |
1200 | end if; | |
1201 | ||
0326b4d4 | 1202 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1203 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1204 | -- modes. This is the case where we tell the machinery not to move into | |
1205 | -- Bignum mode at this top level (of course the top level operation | |
1206 | -- will still be in Bignum mode if either of its operands are of type | |
1207 | -- Bignum). | |
3cce7f32 | 1208 | |
0df9d43f | 1209 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1210 | |
1211 | -- That call may but does not necessarily change the result type of Op. | |
1212 | -- It is the job of this routine to undo such changes, so that at the | |
1213 | -- top level, we have the proper type. This "undoing" is a point at | |
1214 | -- which a final overflow check may be applied. | |
1215 | ||
f32c377d | 1216 | -- If the result type was not fiddled we are all set. We go to base |
1217 | -- types here because things may have been rewritten to generate the | |
1218 | -- base type of the operand types. | |
3cce7f32 | 1219 | |
f32c377d | 1220 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1221 | return; |
1222 | ||
1223 | -- Bignum case | |
1224 | ||
49b3a812 | 1225 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1226 | |
d94b5da2 | 1227 | -- We need a sequence that looks like: |
3cce7f32 | 1228 | |
1229 | -- Rnn : Result_Type; | |
1230 | ||
1231 | -- declare | |
d94b5da2 | 1232 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1233 | -- begin |
49b3a812 | 1234 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1235 | -- SS_Release (M); |
1236 | -- end; | |
1237 | ||
1238 | -- This block is inserted (using Insert_Actions), and then the node | |
1239 | -- is replaced with a reference to Rnn. | |
1240 | ||
1241 | -- A special case arises if our parent is a conversion node. In this | |
1242 | -- case no point in generating a conversion to Result_Type, we will | |
1243 | -- let the parent handle this. Note that this special case is not | |
1244 | -- just about optimization. Consider | |
1245 | ||
1246 | -- A,B,C : Integer; | |
1247 | -- ... | |
49b3a812 | 1248 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1249 | |
1250 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1251 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1252 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1253 | |
1254 | declare | |
49b3a812 | 1255 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1256 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1257 | RHS : Node_Id; | |
1258 | ||
1259 | Rtype : Entity_Id; | |
1260 | ||
1261 | begin | |
1262 | RHS := Convert_From_Bignum (Op); | |
1263 | ||
1264 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1265 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1266 | Rtype := Result_Type; |
1267 | ||
1268 | -- Interesting question, do we need a check on that conversion | |
1269 | -- operation. Answer, not if we know the result is in range. | |
1270 | -- At the moment we are not taking advantage of this. To be | |
1271 | -- looked at later ??? | |
1272 | ||
1273 | else | |
49b3a812 | 1274 | Rtype := LLIB; |
3cce7f32 | 1275 | end if; |
1276 | ||
1277 | Insert_Before | |
1278 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1279 | Make_Assignment_Statement (Loc, | |
1280 | Name => New_Occurrence_Of (Rnn, Loc), | |
1281 | Expression => RHS)); | |
1282 | ||
1283 | Insert_Actions (Op, New_List ( | |
1284 | Make_Object_Declaration (Loc, | |
1285 | Defining_Identifier => Rnn, | |
1286 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1287 | Blk)); | |
1288 | ||
1289 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1290 | Analyze_And_Resolve (Op); | |
1291 | end; | |
1292 | ||
412f75eb | 1293 | -- Here we know the result is Long_Long_Integer'Base, of that it has |
1294 | -- been rewritten because the parent operation is a conversion. See | |
0df9d43f | 1295 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1296 | |
1297 | else | |
f32c377d | 1298 | pragma Assert |
1299 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1300 | |
1301 | -- All we need to do here is to convert the result to the proper | |
1302 | -- result type. As explained above for the Bignum case, we can | |
1303 | -- omit this if our parent is a type conversion. | |
1304 | ||
1305 | if Nkind (P) /= N_Type_Conversion then | |
1306 | Convert_To_And_Rewrite (Result_Type, Op); | |
1307 | end if; | |
1308 | ||
1309 | Analyze_And_Resolve (Op); | |
1310 | end if; | |
1311 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1312 | |
ee6ba406 | 1313 | ---------------------------- |
1314 | -- Apply_Constraint_Check -- | |
1315 | ---------------------------- | |
1316 | ||
1317 | procedure Apply_Constraint_Check | |
1318 | (N : Node_Id; | |
1319 | Typ : Entity_Id; | |
1320 | No_Sliding : Boolean := False) | |
1321 | is | |
1322 | Desig_Typ : Entity_Id; | |
1323 | ||
1324 | begin | |
7aafae1c | 1325 | -- No checks inside a generic (check the instantiations) |
1326 | ||
ee6ba406 | 1327 | if Inside_A_Generic then |
1328 | return; | |
7aafae1c | 1329 | end if; |
ee6ba406 | 1330 | |
6fb3c314 | 1331 | -- Apply required constraint checks |
7aafae1c | 1332 | |
1333 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1334 | Apply_Scalar_Range_Check (N, Typ); |
1335 | ||
1336 | elsif Is_Array_Type (Typ) then | |
1337 | ||
05fcfafb | 1338 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1339 | -- always has the right bounds. |
1340 | ||
1341 | if Nkind (N) = N_Aggregate | |
1342 | and then No (Expressions (N)) | |
1343 | and then Nkind | |
1344 | (First (Choices (First (Component_Associations (N))))) | |
1345 | = N_Others_Choice | |
1346 | then | |
1347 | return; | |
1348 | end if; | |
1349 | ||
ee6ba406 | 1350 | if Is_Constrained (Typ) then |
1351 | Apply_Length_Check (N, Typ); | |
1352 | ||
1353 | if No_Sliding then | |
1354 | Apply_Range_Check (N, Typ); | |
1355 | end if; | |
1356 | else | |
1357 | Apply_Range_Check (N, Typ); | |
1358 | end if; | |
1359 | ||
4fb5f0a0 | 1360 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1361 | and then Has_Discriminants (Base_Type (Typ)) |
1362 | and then Is_Constrained (Typ) | |
1363 | then | |
1364 | Apply_Discriminant_Check (N, Typ); | |
1365 | ||
1366 | elsif Is_Access_Type (Typ) then | |
1367 | ||
1368 | Desig_Typ := Designated_Type (Typ); | |
1369 | ||
1370 | -- No checks necessary if expression statically null | |
1371 | ||
2af58f67 | 1372 | if Known_Null (N) then |
00c403ee | 1373 | if Can_Never_Be_Null (Typ) then |
1374 | Install_Null_Excluding_Check (N); | |
1375 | end if; | |
ee6ba406 | 1376 | |
1377 | -- No sliding possible on access to arrays | |
1378 | ||
1379 | elsif Is_Array_Type (Desig_Typ) then | |
1380 | if Is_Constrained (Desig_Typ) then | |
1381 | Apply_Length_Check (N, Typ); | |
1382 | end if; | |
1383 | ||
1384 | Apply_Range_Check (N, Typ); | |
1385 | ||
1386 | elsif Has_Discriminants (Base_Type (Desig_Typ)) | |
1387 | and then Is_Constrained (Desig_Typ) | |
1388 | then | |
1389 | Apply_Discriminant_Check (N, Typ); | |
1390 | end if; | |
fa7497e8 | 1391 | |
bf3e1520 | 1392 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1393 | -- this check if the constraint node is illegal, as shown by having |
1394 | -- an error posted. This additional guard prevents cascaded errors | |
1395 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1396 | ||
fa7497e8 | 1397 | if Can_Never_Be_Null (Typ) |
1398 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1399 | and then not Error_Posted (N) |
fa7497e8 | 1400 | then |
1401 | Install_Null_Excluding_Check (N); | |
1402 | end if; | |
ee6ba406 | 1403 | end if; |
1404 | end Apply_Constraint_Check; | |
1405 | ||
1406 | ------------------------------ | |
1407 | -- Apply_Discriminant_Check -- | |
1408 | ------------------------------ | |
1409 | ||
1410 | procedure Apply_Discriminant_Check | |
1411 | (N : Node_Id; | |
1412 | Typ : Entity_Id; | |
1413 | Lhs : Node_Id := Empty) | |
1414 | is | |
1415 | Loc : constant Source_Ptr := Sloc (N); | |
1416 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1417 | S_Typ : Entity_Id := Etype (N); | |
1418 | Cond : Node_Id; | |
1419 | T_Typ : Entity_Id; | |
1420 | ||
7be5088a | 1421 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1422 | -- A heap object with an indefinite subtype is constrained by its | |
1423 | -- initial value, and assigning to it requires a constraint_check. | |
1424 | -- The target may be an explicit dereference, or a renaming of one. | |
1425 | ||
ee6ba406 | 1426 | function Is_Aliased_Unconstrained_Component return Boolean; |
1427 | -- It is possible for an aliased component to have a nominal | |
1428 | -- unconstrained subtype (through instantiation). If this is a | |
1429 | -- discriminated component assigned in the expansion of an aggregate | |
1430 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1431 | -- situation requires a predicate of its own. |
ee6ba406 | 1432 | |
7be5088a | 1433 | ---------------------------------- |
1434 | -- Denotes_Explicit_Dereference -- | |
1435 | ---------------------------------- | |
1436 | ||
1437 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1438 | begin | |
1439 | return | |
1440 | Nkind (Obj) = N_Explicit_Dereference | |
1441 | or else | |
1442 | (Is_Entity_Name (Obj) | |
1443 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1444 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1445 | N_Explicit_Dereference); | |
7be5088a | 1446 | end Denotes_Explicit_Dereference; |
1447 | ||
ee6ba406 | 1448 | ---------------------------------------- |
1449 | -- Is_Aliased_Unconstrained_Component -- | |
1450 | ---------------------------------------- | |
1451 | ||
1452 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1453 | Comp : Entity_Id; | |
1454 | Pref : Node_Id; | |
1455 | ||
1456 | begin | |
1457 | if Nkind (Lhs) /= N_Selected_Component then | |
1458 | return False; | |
1459 | else | |
1460 | Comp := Entity (Selector_Name (Lhs)); | |
1461 | Pref := Prefix (Lhs); | |
1462 | end if; | |
1463 | ||
1464 | if Ekind (Comp) /= E_Component | |
1465 | or else not Is_Aliased (Comp) | |
1466 | then | |
1467 | return False; | |
1468 | end if; | |
1469 | ||
1470 | return not Comes_From_Source (Pref) | |
1471 | and then In_Instance | |
1472 | and then not Is_Constrained (Etype (Comp)); | |
1473 | end Is_Aliased_Unconstrained_Component; | |
1474 | ||
1475 | -- Start of processing for Apply_Discriminant_Check | |
1476 | ||
1477 | begin | |
1478 | if Do_Access then | |
1479 | T_Typ := Designated_Type (Typ); | |
1480 | else | |
1481 | T_Typ := Typ; | |
1482 | end if; | |
1483 | ||
1484 | -- Nothing to do if discriminant checks are suppressed or else no code | |
1485 | -- is to be generated | |
1486 | ||
a33565dd | 1487 | if not Expander_Active |
ee6ba406 | 1488 | or else Discriminant_Checks_Suppressed (T_Typ) |
1489 | then | |
1490 | return; | |
1491 | end if; | |
1492 | ||
feff2f05 | 1493 | -- No discriminant checks necessary for an access when expression is |
1494 | -- statically Null. This is not only an optimization, it is fundamental | |
1495 | -- because otherwise discriminant checks may be generated in init procs | |
1496 | -- for types containing an access to a not-yet-frozen record, causing a | |
1497 | -- deadly forward reference. | |
ee6ba406 | 1498 | |
feff2f05 | 1499 | -- Also, if the expression is of an access type whose designated type is |
1500 | -- incomplete, then the access value must be null and we suppress the | |
1501 | -- check. | |
ee6ba406 | 1502 | |
2af58f67 | 1503 | if Known_Null (N) then |
ee6ba406 | 1504 | return; |
1505 | ||
1506 | elsif Is_Access_Type (S_Typ) then | |
1507 | S_Typ := Designated_Type (S_Typ); | |
1508 | ||
1509 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1510 | return; | |
1511 | end if; | |
1512 | end if; | |
1513 | ||
0577b0b1 | 1514 | -- If an assignment target is present, then we need to generate the |
1515 | -- actual subtype if the target is a parameter or aliased object with | |
1516 | -- an unconstrained nominal subtype. | |
1517 | ||
1518 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1519 | -- subtype to the parameter and dereference cases, since other aliased | |
1520 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1521 | -- constrained). |
ee6ba406 | 1522 | |
1523 | if Present (Lhs) | |
1524 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1525 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1526 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1527 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1528 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1529 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1530 | and then not Is_Constrained (T_Typ) |
7be5088a | 1531 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1532 | and then Nkind (Original_Node (Lhs)) /= |
1533 | N_Function_Call)) | |
ee6ba406 | 1534 | then |
1535 | T_Typ := Get_Actual_Subtype (Lhs); | |
1536 | end if; | |
1537 | ||
feff2f05 | 1538 | -- Nothing to do if the type is unconstrained (this is the case where |
1539 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1540 | -- is required). | |
ee6ba406 | 1541 | |
1542 | if not Is_Constrained (T_Typ) then | |
1543 | return; | |
05fcfafb | 1544 | |
1545 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1546 | -- partial view that is constrained. | |
1547 | ||
de54c5ab | 1548 | elsif Ada_Version >= Ada_2005 |
0d78d2d4 | 1549 | and then Object_Type_Has_Constrained_Partial_View |
d41a3f41 | 1550 | (Typ => Base_Type (T_Typ), |
1551 | Scop => Current_Scope) | |
05fcfafb | 1552 | then |
1553 | return; | |
ee6ba406 | 1554 | end if; |
1555 | ||
00f91aef | 1556 | -- Nothing to do if the type is an Unchecked_Union |
1557 | ||
1558 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1559 | return; | |
1560 | end if; | |
1561 | ||
8d11916f | 1562 | -- Suppress checks if the subtypes are the same. The check must be |
feff2f05 | 1563 | -- preserved in an assignment to a formal, because the constraint is |
1564 | -- given by the actual. | |
ee6ba406 | 1565 | |
1566 | if Nkind (Original_Node (N)) /= N_Allocator | |
1567 | and then (No (Lhs) | |
8143bf7c | 1568 | or else not Is_Entity_Name (Lhs) |
1569 | or else No (Param_Entity (Lhs))) | |
ee6ba406 | 1570 | then |
1571 | if (Etype (N) = Typ | |
1572 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1573 | and then not Is_Aliased_View (Lhs) | |
1574 | then | |
1575 | return; | |
1576 | end if; | |
1577 | ||
feff2f05 | 1578 | -- We can also eliminate checks on allocators with a subtype mark that |
1579 | -- coincides with the context type. The context type may be a subtype | |
1580 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1581 | |
1582 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1583 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1584 | then | |
1585 | declare | |
9dfe12ae | 1586 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1587 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1588 | |
1589 | begin | |
1590 | if Alloc_Typ = T_Typ | |
1591 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1592 | and then Is_Entity_Name ( | |
1593 | Subtype_Indication (Parent (T_Typ))) | |
1594 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1595 | ||
1596 | then | |
1597 | return; | |
1598 | end if; | |
1599 | end; | |
1600 | end if; | |
1601 | ||
feff2f05 | 1602 | -- See if we have a case where the types are both constrained, and all |
1603 | -- the constraints are constants. In this case, we can do the check | |
1604 | -- successfully at compile time. | |
ee6ba406 | 1605 | |
8d11916f | 1606 | -- We skip this check for the case where the node is rewritten as |
d7ec9a29 | 1607 | -- an allocator, because it already carries the context subtype, |
1608 | -- and extracting the discriminants from the aggregate is messy. | |
ee6ba406 | 1609 | |
1610 | if Is_Constrained (S_Typ) | |
1611 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1612 | then | |
1613 | declare | |
1614 | DconT : Elmt_Id; | |
1615 | Discr : Entity_Id; | |
1616 | DconS : Elmt_Id; | |
1617 | ItemS : Node_Id; | |
1618 | ItemT : Node_Id; | |
1619 | ||
1620 | begin | |
1621 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1622 | -- private type completed by a default discriminated type. In that |
8d11916f | 1623 | -- case, we need to get the constraints from the underlying type. |
feff2f05 | 1624 | -- If the underlying type is unconstrained (i.e. has no default |
1625 | -- discriminants) no check is needed. | |
ee6ba406 | 1626 | |
1627 | if Has_Discriminants (S_Typ) then | |
1628 | Discr := First_Discriminant (S_Typ); | |
1629 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1630 | ||
1631 | else | |
1632 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1633 | DconS := | |
1634 | First_Elmt | |
1635 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1636 | ||
1637 | if No (DconS) then | |
1638 | return; | |
1639 | end if; | |
fccb5da7 | 1640 | |
1641 | -- A further optimization: if T_Typ is derived from S_Typ | |
1642 | -- without imposing a constraint, no check is needed. | |
1643 | ||
1644 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1645 | N_Full_Type_Declaration | |
1646 | then | |
1647 | declare | |
5c61a0ff | 1648 | Type_Def : constant Node_Id := |
b6341c67 | 1649 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1650 | begin |
1651 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1652 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1653 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1654 | then | |
1655 | return; | |
1656 | end if; | |
1657 | end; | |
1658 | end if; | |
ee6ba406 | 1659 | end if; |
1660 | ||
86594966 | 1661 | -- Constraint may appear in full view of type |
1662 | ||
1663 | if Ekind (T_Typ) = E_Private_Subtype | |
1664 | and then Present (Full_View (T_Typ)) | |
1665 | then | |
d7ec9a29 | 1666 | DconT := |
86594966 | 1667 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
86594966 | 1668 | else |
d7ec9a29 | 1669 | DconT := |
1670 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
86594966 | 1671 | end if; |
ee6ba406 | 1672 | |
1673 | while Present (Discr) loop | |
1674 | ItemS := Node (DconS); | |
1675 | ItemT := Node (DconT); | |
1676 | ||
00c403ee | 1677 | -- For a discriminated component type constrained by the |
1678 | -- current instance of an enclosing type, there is no | |
1679 | -- applicable discriminant check. | |
1680 | ||
1681 | if Nkind (ItemT) = N_Attribute_Reference | |
1682 | and then Is_Access_Type (Etype (ItemT)) | |
1683 | and then Is_Entity_Name (Prefix (ItemT)) | |
1684 | and then Is_Type (Entity (Prefix (ItemT))) | |
1685 | then | |
1686 | return; | |
1687 | end if; | |
1688 | ||
cc60bd16 | 1689 | -- If the expressions for the discriminants are identical |
1690 | -- and it is side-effect free (for now just an entity), | |
1691 | -- this may be a shared constraint, e.g. from a subtype | |
1692 | -- without a constraint introduced as a generic actual. | |
1693 | -- Examine other discriminants if any. | |
1694 | ||
1695 | if ItemS = ItemT | |
1696 | and then Is_Entity_Name (ItemS) | |
1697 | then | |
1698 | null; | |
1699 | ||
1700 | elsif not Is_OK_Static_Expression (ItemS) | |
1701 | or else not Is_OK_Static_Expression (ItemT) | |
1702 | then | |
1703 | exit; | |
ee6ba406 | 1704 | |
cc60bd16 | 1705 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1706 | if Do_Access then -- needs run-time check. |
1707 | exit; | |
1708 | else | |
1709 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1710 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1711 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1712 | return; |
1713 | end if; | |
1714 | end if; | |
1715 | ||
1716 | Next_Elmt (DconS); | |
1717 | Next_Elmt (DconT); | |
1718 | Next_Discriminant (Discr); | |
1719 | end loop; | |
1720 | ||
1721 | if No (Discr) then | |
1722 | return; | |
1723 | end if; | |
1724 | end; | |
1725 | end if; | |
1726 | ||
1727 | -- Here we need a discriminant check. First build the expression | |
1728 | -- for the comparisons of the discriminants: | |
1729 | ||
1730 | -- (n.disc1 /= typ.disc1) or else | |
1731 | -- (n.disc2 /= typ.disc2) or else | |
1732 | -- ... | |
1733 | -- (n.discn /= typ.discn) | |
1734 | ||
1735 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1736 | ||
3cce7f32 | 1737 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1738 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1739 | |
1740 | if Present (Param_Entity (Lhs)) then | |
1741 | Cond := | |
1742 | Make_And_Then (Loc, | |
1743 | Left_Opnd => | |
1744 | Make_Attribute_Reference (Loc, | |
1745 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1746 | Attribute_Name => Name_Constrained), | |
1747 | Right_Opnd => Cond); | |
1748 | end if; | |
1749 | ||
1750 | if Do_Access then | |
1751 | Cond := Guard_Access (Cond, Loc, N); | |
1752 | end if; | |
1753 | ||
1754 | Insert_Action (N, | |
f15731c4 | 1755 | Make_Raise_Constraint_Error (Loc, |
1756 | Condition => Cond, | |
1757 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1758 | end Apply_Discriminant_Check; |
1759 | ||
2fe22c69 | 1760 | ------------------------- |
1761 | -- Apply_Divide_Checks -- | |
1762 | ------------------------- | |
ee6ba406 | 1763 | |
2fe22c69 | 1764 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1765 | Loc : constant Source_Ptr := Sloc (N); |
1766 | Typ : constant Entity_Id := Etype (N); | |
1767 | Left : constant Node_Id := Left_Opnd (N); | |
1768 | Right : constant Node_Id := Right_Opnd (N); | |
1769 | ||
db415383 | 1770 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1771 | -- Current overflow checking mode |
1772 | ||
ee6ba406 | 1773 | LLB : Uint; |
1774 | Llo : Uint; | |
1775 | Lhi : Uint; | |
1776 | LOK : Boolean; | |
1777 | Rlo : Uint; | |
1778 | Rhi : Uint; | |
2fe22c69 | 1779 | ROK : Boolean; |
96da3284 | 1780 | |
1781 | pragma Warnings (Off, Lhi); | |
1782 | -- Don't actually use this value | |
ee6ba406 | 1783 | |
1784 | begin | |
0df9d43f | 1785 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1786 | -- operating on signed integer types, then the only thing this routine | |
1787 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1788 | -- procedure will (possibly later on during recursive downward calls), | |
1789 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1790 | |
1791 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1792 | and then Is_Signed_Integer_Type (Typ) |
1793 | then | |
1794 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1795 | return; | |
1796 | end if; | |
1797 | ||
1798 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1799 | ||
a33565dd | 1800 | if Expander_Active |
13dbf220 | 1801 | and then not Backend_Divide_Checks_On_Target |
1802 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1803 | then |
9c486805 | 1804 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1805 | |
2fe22c69 | 1806 | -- Deal with division check |
ee6ba406 | 1807 | |
2fe22c69 | 1808 | if Do_Division_Check (N) |
1809 | and then not Division_Checks_Suppressed (Typ) | |
1810 | then | |
1811 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1812 | end if; |
1813 | ||
2fe22c69 | 1814 | -- Deal with overflow check |
1815 | ||
0df9d43f | 1816 | if Do_Overflow_Check (N) |
1817 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1818 | then | |
f3ccbbb3 | 1819 | Set_Do_Overflow_Check (N, False); |
1820 | ||
2fe22c69 | 1821 | -- Test for extremely annoying case of xxx'First divided by -1 |
1822 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1823 | |
ee6ba406 | 1824 | if Nkind (N) = N_Op_Divide |
1825 | and then Is_Signed_Integer_Type (Typ) | |
1826 | then | |
9c486805 | 1827 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1828 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1829 | ||
1830 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1831 | and then |
1832 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1833 | then |
1834 | Insert_Action (N, | |
1835 | Make_Raise_Constraint_Error (Loc, | |
1836 | Condition => | |
1837 | Make_And_Then (Loc, | |
2fe22c69 | 1838 | Left_Opnd => |
1839 | Make_Op_Eq (Loc, | |
1840 | Left_Opnd => | |
1841 | Duplicate_Subexpr_Move_Checks (Left), | |
1842 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1843 | |
2fe22c69 | 1844 | Right_Opnd => |
1845 | Make_Op_Eq (Loc, | |
1846 | Left_Opnd => Duplicate_Subexpr (Right), | |
1847 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1848 | |
f15731c4 | 1849 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1850 | end if; |
1851 | end if; | |
1852 | end if; | |
1853 | end if; | |
2fe22c69 | 1854 | end Apply_Divide_Checks; |
1855 | ||
1856 | -------------------------- | |
1857 | -- Apply_Division_Check -- | |
1858 | -------------------------- | |
1859 | ||
1860 | procedure Apply_Division_Check | |
1861 | (N : Node_Id; | |
1862 | Rlo : Uint; | |
1863 | Rhi : Uint; | |
1864 | ROK : Boolean) | |
1865 | is | |
1866 | pragma Assert (Do_Division_Check (N)); | |
1867 | ||
1868 | Loc : constant Source_Ptr := Sloc (N); | |
1869 | Right : constant Node_Id := Right_Opnd (N); | |
1870 | ||
1871 | begin | |
a33565dd | 1872 | if Expander_Active |
2fe22c69 | 1873 | and then not Backend_Divide_Checks_On_Target |
1874 | and then Check_Needed (Right, Division_Check) | |
1875 | then | |
1876 | -- See if division by zero possible, and if so generate test. This | |
1877 | -- part of the test is not controlled by the -gnato switch, since | |
1878 | -- it is a Division_Check and not an Overflow_Check. | |
1879 | ||
1880 | if Do_Division_Check (N) then | |
f3ccbbb3 | 1881 | Set_Do_Division_Check (N, False); |
1882 | ||
2fe22c69 | 1883 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then |
1884 | Insert_Action (N, | |
1885 | Make_Raise_Constraint_Error (Loc, | |
1886 | Condition => | |
1887 | Make_Op_Eq (Loc, | |
1888 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1889 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
1890 | Reason => CE_Divide_By_Zero)); | |
1891 | end if; | |
1892 | end if; | |
1893 | end if; | |
1894 | end Apply_Division_Check; | |
ee6ba406 | 1895 | |
5329ca64 | 1896 | ---------------------------------- |
1897 | -- Apply_Float_Conversion_Check -- | |
1898 | ---------------------------------- | |
1899 | ||
feff2f05 | 1900 | -- Let F and I be the source and target types of the conversion. The RM |
1901 | -- specifies that a floating-point value X is rounded to the nearest | |
1902 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1903 | -- value of X is checked against I'Range. | |
1904 | ||
1905 | -- The catch in the above paragraph is that there is no good way to know | |
1906 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1907 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1908 | |
5329ca64 | 1909 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1910 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1911 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1912 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1913 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1914 | -- sign of I'First and I'Last. | |
5329ca64 | 1915 | -- (5) X may be a NaN, which will fail any comparison |
1916 | ||
2af58f67 | 1917 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1918 | |
5329ca64 | 1919 | -- (1) If either I'First or I'Last is not known at compile time, use |
1920 | -- I'Base instead of I in the next three steps and perform a | |
1921 | -- regular range check against I'Range after conversion. | |
1922 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1923 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1924 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1925 | -- In other words, take one of the closest floating-point numbers | |
1926 | -- (which is an integer value) to I'First, and see if it is in | |
1927 | -- range or not. | |
5329ca64 | 1928 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1929 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1930 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1931 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1932 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1933 | ||
2af58f67 | 1934 | -- For the truncating case, replace steps (2) and (3) as follows: |
1935 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1936 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1937 | -- Lo_OK be True. | |
1938 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1939 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1940 | -- Hi_OK be True. |
2af58f67 | 1941 | |
5329ca64 | 1942 | procedure Apply_Float_Conversion_Check |
1943 | (Ck_Node : Node_Id; | |
1944 | Target_Typ : Entity_Id) | |
1945 | is | |
feff2f05 | 1946 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1947 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1948 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1949 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1950 | Target_Base : constant Entity_Id := |
b6341c67 | 1951 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1952 | |
2af58f67 | 1953 | Par : constant Node_Id := Parent (Ck_Node); |
1954 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1955 | -- Parent of check node, must be a type conversion | |
1956 | ||
1957 | Truncate : constant Boolean := Float_Truncate (Par); | |
1958 | Max_Bound : constant Uint := | |
b6341c67 | 1959 | UI_Expon |
1960 | (Machine_Radix_Value (Expr_Type), | |
1961 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 1962 | |
5329ca64 | 1963 | -- Largest bound, so bound plus or minus half is a machine number of F |
1964 | ||
feff2f05 | 1965 | Ifirst, Ilast : Uint; |
1966 | -- Bounds of integer type | |
1967 | ||
1968 | Lo, Hi : Ureal; | |
1969 | -- Bounds to check in floating-point domain | |
5329ca64 | 1970 | |
feff2f05 | 1971 | Lo_OK, Hi_OK : Boolean; |
1972 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1973 | |
feff2f05 | 1974 | Lo_Chk, Hi_Chk : Node_Id; |
1975 | -- Expressions that are False iff check fails | |
1976 | ||
1977 | Reason : RT_Exception_Code; | |
5329ca64 | 1978 | |
1979 | begin | |
41f06abf | 1980 | -- We do not need checks if we are not generating code (i.e. the full |
1981 | -- expander is not active). In SPARK mode, we specifically don't want | |
1982 | -- the frontend to expand these checks, which are dealt with directly | |
1983 | -- in the formal verification backend. | |
1984 | ||
a33565dd | 1985 | if not Expander_Active then |
41f06abf | 1986 | return; |
1987 | end if; | |
1988 | ||
5329ca64 | 1989 | if not Compile_Time_Known_Value (LB) |
1990 | or not Compile_Time_Known_Value (HB) | |
1991 | then | |
1992 | declare | |
feff2f05 | 1993 | -- First check that the value falls in the range of the base type, |
1994 | -- to prevent overflow during conversion and then perform a | |
1995 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 1996 | |
5329ca64 | 1997 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 1998 | |
46eb6933 | 1999 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 2000 | |
2001 | begin | |
2002 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
2003 | Set_Etype (Temp, Target_Base); | |
2004 | ||
2005 | Insert_Action (Parent (Par), | |
2006 | Make_Object_Declaration (Loc, | |
2007 | Defining_Identifier => Temp, | |
2008 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
2009 | Expression => New_Copy_Tree (Par)), | |
2010 | Suppress => All_Checks); | |
2011 | ||
2012 | Insert_Action (Par, | |
2013 | Make_Raise_Constraint_Error (Loc, | |
2014 | Condition => | |
2015 | Make_Not_In (Loc, | |
2016 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
2017 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
2018 | Reason => CE_Range_Check_Failed)); | |
2019 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
2020 | ||
2021 | return; | |
2022 | end; | |
2023 | end if; | |
2024 | ||
7d86aa98 | 2025 | -- Get the (static) bounds of the target type |
5329ca64 | 2026 | |
2027 | Ifirst := Expr_Value (LB); | |
2028 | Ilast := Expr_Value (HB); | |
2029 | ||
7d86aa98 | 2030 | -- A simple optimization: if the expression is a universal literal, |
2031 | -- we can do the comparison with the bounds and the conversion to | |
2032 | -- an integer type statically. The range checks are unchanged. | |
2033 | ||
2034 | if Nkind (Ck_Node) = N_Real_Literal | |
2035 | and then Etype (Ck_Node) = Universal_Real | |
2036 | and then Is_Integer_Type (Target_Typ) | |
2037 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
2038 | then | |
2039 | declare | |
2040 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
2041 | ||
2042 | begin | |
2043 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
2044 | ||
4309515d | 2045 | -- Conversion is safe |
7d86aa98 | 2046 | |
2047 | Rewrite (Parent (Ck_Node), | |
2048 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
2049 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
2050 | return; | |
2051 | end if; | |
2052 | end; | |
2053 | end if; | |
2054 | ||
5329ca64 | 2055 | -- Check against lower bound |
2056 | ||
2af58f67 | 2057 | if Truncate and then Ifirst > 0 then |
2058 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
2059 | Lo_OK := False; | |
2060 | ||
2061 | elsif Truncate then | |
2062 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
2063 | Lo_OK := True; | |
2064 | ||
2065 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 2066 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
2067 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 2068 | |
5329ca64 | 2069 | else |
2070 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
2071 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
2072 | end if; | |
2073 | ||
2074 | if Lo_OK then | |
2075 | ||
2076 | -- Lo_Chk := (X >= Lo) | |
2077 | ||
2078 | Lo_Chk := Make_Op_Ge (Loc, | |
2079 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2080 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2081 | ||
2082 | else | |
2083 | -- Lo_Chk := (X > Lo) | |
2084 | ||
2085 | Lo_Chk := Make_Op_Gt (Loc, | |
2086 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2087 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2088 | end if; | |
2089 | ||
2090 | -- Check against higher bound | |
2091 | ||
2af58f67 | 2092 | if Truncate and then Ilast < 0 then |
2093 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2094 | Hi_OK := False; |
2af58f67 | 2095 | |
2096 | elsif Truncate then | |
2097 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2098 | Hi_OK := True; | |
2099 | ||
2100 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2101 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2102 | Hi_OK := (Ilast < 0); | |
2103 | else | |
2104 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2105 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2106 | end if; | |
2107 | ||
2108 | if Hi_OK then | |
2109 | ||
2110 | -- Hi_Chk := (X <= Hi) | |
2111 | ||
2112 | Hi_Chk := Make_Op_Le (Loc, | |
2113 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2114 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2115 | ||
2116 | else | |
2117 | -- Hi_Chk := (X < Hi) | |
2118 | ||
2119 | Hi_Chk := Make_Op_Lt (Loc, | |
2120 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2121 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2122 | end if; | |
2123 | ||
feff2f05 | 2124 | -- If the bounds of the target type are the same as those of the base |
2125 | -- type, the check is an overflow check as a range check is not | |
2126 | -- performed in these cases. | |
5329ca64 | 2127 | |
2128 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2129 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2130 | then | |
2131 | Reason := CE_Overflow_Check_Failed; | |
2132 | else | |
2133 | Reason := CE_Range_Check_Failed; | |
2134 | end if; | |
2135 | ||
2136 | -- Raise CE if either conditions does not hold | |
2137 | ||
2138 | Insert_Action (Ck_Node, | |
2139 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2140 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2141 | Reason => Reason)); |
2142 | end Apply_Float_Conversion_Check; | |
2143 | ||
ee6ba406 | 2144 | ------------------------ |
2145 | -- Apply_Length_Check -- | |
2146 | ------------------------ | |
2147 | ||
2148 | procedure Apply_Length_Check | |
2149 | (Ck_Node : Node_Id; | |
2150 | Target_Typ : Entity_Id; | |
2151 | Source_Typ : Entity_Id := Empty) | |
2152 | is | |
2153 | begin | |
2154 | Apply_Selected_Length_Checks | |
2155 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2156 | end Apply_Length_Check; | |
2157 | ||
3b045963 | 2158 | ------------------------------------- |
2159 | -- Apply_Parameter_Aliasing_Checks -- | |
2160 | ------------------------------------- | |
b73adb97 | 2161 | |
3b045963 | 2162 | procedure Apply_Parameter_Aliasing_Checks |
2163 | (Call : Node_Id; | |
2164 | Subp : Entity_Id) | |
2165 | is | |
bb569db0 | 2166 | Loc : constant Source_Ptr := Sloc (Call); |
2167 | ||
3b045963 | 2168 | function May_Cause_Aliasing |
2169 | (Formal_1 : Entity_Id; | |
2170 | Formal_2 : Entity_Id) return Boolean; | |
2171 | -- Determine whether two formal parameters can alias each other | |
2172 | -- depending on their modes. | |
2173 | ||
2174 | function Original_Actual (N : Node_Id) return Node_Id; | |
2175 | -- The expander may replace an actual with a temporary for the sake of | |
2176 | -- side effect removal. The temporary may hide a potential aliasing as | |
2177 | -- it does not share the address of the actual. This routine attempts | |
2178 | -- to retrieve the original actual. | |
2179 | ||
bb569db0 | 2180 | procedure Overlap_Check |
2181 | (Actual_1 : Node_Id; | |
2182 | Actual_2 : Node_Id; | |
2183 | Formal_1 : Entity_Id; | |
2184 | Formal_2 : Entity_Id; | |
2185 | Check : in out Node_Id); | |
2186 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2187 | -- If detailed exception messages are enabled, the check is augmented to | |
2188 | -- provide information about the names of the corresponding formals. See | |
2189 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2190 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2191 | -- Check contains all and-ed simple tests generated so far or remains | |
2192 | -- unchanged in the case of detailed exception messaged. | |
2193 | ||
3b045963 | 2194 | ------------------------ |
2195 | -- May_Cause_Aliasing -- | |
2196 | ------------------------ | |
b73adb97 | 2197 | |
3b045963 | 2198 | function May_Cause_Aliasing |
4a9e7f0c | 2199 | (Formal_1 : Entity_Id; |
3b045963 | 2200 | Formal_2 : Entity_Id) return Boolean |
2201 | is | |
2202 | begin | |
2203 | -- The following combination cannot lead to aliasing | |
2204 | ||
2205 | -- Formal 1 Formal 2 | |
2206 | -- IN IN | |
2207 | ||
2208 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2209 | and then |
2210 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2211 | then |
2212 | return False; | |
2213 | ||
2214 | -- The following combinations may lead to aliasing | |
2215 | ||
2216 | -- Formal 1 Formal 2 | |
2217 | -- IN OUT | |
2218 | -- IN IN OUT | |
2219 | -- OUT IN | |
2220 | -- OUT IN OUT | |
2221 | -- OUT OUT | |
2222 | ||
2223 | else | |
2224 | return True; | |
2225 | end if; | |
2226 | end May_Cause_Aliasing; | |
2227 | ||
2228 | --------------------- | |
2229 | -- Original_Actual -- | |
2230 | --------------------- | |
2231 | ||
2232 | function Original_Actual (N : Node_Id) return Node_Id is | |
2233 | begin | |
2234 | if Nkind (N) = N_Type_Conversion then | |
2235 | return Expression (N); | |
2236 | ||
2237 | -- The expander created a temporary to capture the result of a type | |
2238 | -- conversion where the expression is the real actual. | |
2239 | ||
2240 | elsif Nkind (N) = N_Identifier | |
2241 | and then Present (Original_Node (N)) | |
2242 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2243 | then | |
2244 | return Expression (Original_Node (N)); | |
2245 | end if; | |
2246 | ||
2247 | return N; | |
2248 | end Original_Actual; | |
2249 | ||
bb569db0 | 2250 | ------------------- |
2251 | -- Overlap_Check -- | |
2252 | ------------------- | |
2253 | ||
2254 | procedure Overlap_Check | |
2255 | (Actual_1 : Node_Id; | |
2256 | Actual_2 : Node_Id; | |
2257 | Formal_1 : Entity_Id; | |
2258 | Formal_2 : Entity_Id; | |
2259 | Check : in out Node_Id) | |
2260 | is | |
29448168 | 2261 | Cond : Node_Id; |
2262 | ID_Casing : constant Casing_Type := | |
2263 | Identifier_Casing (Source_Index (Current_Sem_Unit)); | |
bb569db0 | 2264 | |
2265 | begin | |
2266 | -- Generate: | |
2267 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2268 | ||
2269 | Cond := | |
2270 | Make_Attribute_Reference (Loc, | |
2271 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2272 | Attribute_Name => Name_Overlaps_Storage, | |
2273 | Expressions => | |
2274 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2275 | ||
2276 | -- Generate the following check when detailed exception messages are | |
2277 | -- enabled: | |
2278 | ||
2279 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2280 | -- raise Program_Error with <detailed message>; | |
2281 | -- end if; | |
2282 | ||
2283 | if Exception_Extra_Info then | |
2284 | Start_String; | |
2285 | ||
2286 | -- Do not generate location information for internal calls | |
2287 | ||
2288 | if Comes_From_Source (Call) then | |
2289 | Store_String_Chars (Build_Location_String (Loc)); | |
2290 | Store_String_Char (' '); | |
2291 | end if; | |
2292 | ||
2293 | Store_String_Chars ("aliased parameters, actuals for """); | |
29448168 | 2294 | |
2295 | Get_Name_String (Chars (Formal_1)); | |
2296 | Set_Casing (ID_Casing); | |
2297 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2298 | ||
bb569db0 | 2299 | Store_String_Chars (""" and """); |
29448168 | 2300 | |
2301 | Get_Name_String (Chars (Formal_2)); | |
2302 | Set_Casing (ID_Casing); | |
2303 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2304 | ||
bb569db0 | 2305 | Store_String_Chars (""" overlap"); |
2306 | ||
2307 | Insert_Action (Call, | |
2308 | Make_If_Statement (Loc, | |
2309 | Condition => Cond, | |
2310 | Then_Statements => New_List ( | |
2311 | Make_Raise_Statement (Loc, | |
2312 | Name => | |
83c6c069 | 2313 | New_Occurrence_Of (Standard_Program_Error, Loc), |
bb569db0 | 2314 | Expression => Make_String_Literal (Loc, End_String))))); |
2315 | ||
2316 | -- Create a sequence of overlapping checks by and-ing them all | |
2317 | -- together. | |
2318 | ||
2319 | else | |
2320 | if No (Check) then | |
2321 | Check := Cond; | |
2322 | else | |
2323 | Check := | |
2324 | Make_And_Then (Loc, | |
2325 | Left_Opnd => Check, | |
2326 | Right_Opnd => Cond); | |
2327 | end if; | |
2328 | end if; | |
2329 | end Overlap_Check; | |
2330 | ||
3b045963 | 2331 | -- Local variables |
2332 | ||
3b045963 | 2333 | Actual_1 : Node_Id; |
2334 | Actual_2 : Node_Id; | |
2335 | Check : Node_Id; | |
3b045963 | 2336 | Formal_1 : Entity_Id; |
2337 | Formal_2 : Entity_Id; | |
2338 | ||
2339 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2340 | ||
2341 | begin | |
bb569db0 | 2342 | Check := Empty; |
3b045963 | 2343 | |
2344 | Actual_1 := First_Actual (Call); | |
2345 | Formal_1 := First_Formal (Subp); | |
2346 | while Present (Actual_1) and then Present (Formal_1) loop | |
2347 | ||
2348 | -- Ensure that the actual is an object that is not passed by value. | |
2349 | -- Elementary types are always passed by value, therefore actuals of | |
2350 | -- such types cannot lead to aliasing. | |
2351 | ||
2352 | if Is_Object_Reference (Original_Actual (Actual_1)) | |
2353 | and then not Is_Elementary_Type (Etype (Original_Actual (Actual_1))) | |
2354 | then | |
2355 | Actual_2 := Next_Actual (Actual_1); | |
2356 | Formal_2 := Next_Formal (Formal_1); | |
2357 | while Present (Actual_2) and then Present (Formal_2) loop | |
2358 | ||
2359 | -- The other actual we are testing against must also denote | |
2360 | -- a non pass-by-value object. Generate the check only when | |
2361 | -- the mode of the two formals may lead to aliasing. | |
2362 | ||
2363 | if Is_Object_Reference (Original_Actual (Actual_2)) | |
2364 | and then not | |
2365 | Is_Elementary_Type (Etype (Original_Actual (Actual_2))) | |
2366 | and then May_Cause_Aliasing (Formal_1, Formal_2) | |
2367 | then | |
bb569db0 | 2368 | Overlap_Check |
2369 | (Actual_1 => Actual_1, | |
2370 | Actual_2 => Actual_2, | |
2371 | Formal_1 => Formal_1, | |
2372 | Formal_2 => Formal_2, | |
2373 | Check => Check); | |
3b045963 | 2374 | end if; |
2375 | ||
2376 | Next_Actual (Actual_2); | |
2377 | Next_Formal (Formal_2); | |
2378 | end loop; | |
2379 | end if; | |
2380 | ||
2381 | Next_Actual (Actual_1); | |
2382 | Next_Formal (Formal_1); | |
2383 | end loop; | |
2384 | ||
bb569db0 | 2385 | -- Place a simple check right before the call |
3b045963 | 2386 | |
bb569db0 | 2387 | if Present (Check) and then not Exception_Extra_Info then |
3b045963 | 2388 | Insert_Action (Call, |
2389 | Make_Raise_Program_Error (Loc, | |
bb569db0 | 2390 | Condition => Check, |
2391 | Reason => PE_Aliased_Parameters)); | |
3b045963 | 2392 | end if; |
2393 | end Apply_Parameter_Aliasing_Checks; | |
2394 | ||
2395 | ------------------------------------- | |
2396 | -- Apply_Parameter_Validity_Checks -- | |
2397 | ------------------------------------- | |
2398 | ||
2399 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2400 | Subp_Decl : Node_Id; | |
b73adb97 | 2401 | |
4a9e7f0c | 2402 | procedure Add_Validity_Check |
2403 | (Context : Entity_Id; | |
2404 | PPC_Nam : Name_Id; | |
2405 | For_Result : Boolean := False); | |
2406 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
2407 | -- of Context. PPC_Nam denotes the pre or post condition pragma name. | |
2408 | -- Set flag For_Result when to verify the result of a function. | |
b73adb97 | 2409 | |
4a9e7f0c | 2410 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id); |
2411 | -- Create a pre or post condition pragma with name PPC_Nam which | |
2412 | -- tests expression Check. | |
b73adb97 | 2413 | |
b73adb97 | 2414 | ------------------------ |
2415 | -- Add_Validity_Check -- | |
2416 | ------------------------ | |
2417 | ||
2418 | procedure Add_Validity_Check | |
2419 | (Context : Entity_Id; | |
4a9e7f0c | 2420 | PPC_Nam : Name_Id; |
b73adb97 | 2421 | For_Result : Boolean := False) |
2422 | is | |
4a9e7f0c | 2423 | Loc : constant Source_Ptr := Sloc (Subp); |
2424 | Typ : constant Entity_Id := Etype (Context); | |
b73adb97 | 2425 | Check : Node_Id; |
2426 | Nam : Name_Id; | |
2427 | ||
2428 | begin | |
9916a361 | 2429 | -- For scalars, generate 'Valid test |
b73adb97 | 2430 | |
2431 | if Is_Scalar_Type (Typ) then | |
2432 | Nam := Name_Valid; | |
9916a361 | 2433 | |
2434 | -- For any non-scalar with scalar parts, generate 'Valid_Scalars test | |
2435 | ||
2436 | elsif Scalar_Part_Present (Typ) then | |
b73adb97 | 2437 | Nam := Name_Valid_Scalars; |
9916a361 | 2438 | |
2439 | -- No test needed for other cases (no scalars to test) | |
2440 | ||
b73adb97 | 2441 | else |
2442 | return; | |
2443 | end if; | |
2444 | ||
2445 | -- Step 1: Create the expression to verify the validity of the | |
2446 | -- context. | |
2447 | ||
83c6c069 | 2448 | Check := New_Occurrence_Of (Context, Loc); |
b73adb97 | 2449 | |
2450 | -- When processing a function result, use 'Result. Generate | |
2451 | -- Context'Result | |
2452 | ||
2453 | if For_Result then | |
2454 | Check := | |
2455 | Make_Attribute_Reference (Loc, | |
2456 | Prefix => Check, | |
2457 | Attribute_Name => Name_Result); | |
2458 | end if; | |
2459 | ||
2460 | -- Generate: | |
2461 | -- Context['Result]'Valid[_Scalars] | |
2462 | ||
2463 | Check := | |
2464 | Make_Attribute_Reference (Loc, | |
2465 | Prefix => Check, | |
2466 | Attribute_Name => Nam); | |
2467 | ||
4a9e7f0c | 2468 | -- Step 2: Create a pre or post condition pragma |
2469 | ||
2470 | Build_PPC_Pragma (PPC_Nam, Check); | |
2471 | end Add_Validity_Check; | |
2472 | ||
2473 | ---------------------- | |
2474 | -- Build_PPC_Pragma -- | |
2475 | ---------------------- | |
b73adb97 | 2476 | |
4a9e7f0c | 2477 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id) is |
7c443ae8 | 2478 | Loc : constant Source_Ptr := Sloc (Subp); |
2479 | Decls : List_Id; | |
2480 | Prag : Node_Id; | |
4a9e7f0c | 2481 | |
2482 | begin | |
2483 | Prag := | |
2484 | Make_Pragma (Loc, | |
2485 | Pragma_Identifier => Make_Identifier (Loc, PPC_Nam), | |
2486 | Pragma_Argument_Associations => New_List ( | |
2487 | Make_Pragma_Argument_Association (Loc, | |
2488 | Chars => Name_Check, | |
2489 | Expression => Check))); | |
2490 | ||
2491 | -- Add a message unless exception messages are suppressed | |
2492 | ||
2493 | if not Exception_Locations_Suppressed then | |
2494 | Append_To (Pragma_Argument_Associations (Prag), | |
2495 | Make_Pragma_Argument_Association (Loc, | |
2496 | Chars => Name_Message, | |
2497 | Expression => | |
2498 | Make_String_Literal (Loc, | |
2499 | Strval => "failed " & Get_Name_String (PPC_Nam) & | |
2500 | " from " & Build_Location_String (Loc)))); | |
2501 | end if; | |
2502 | ||
2503 | -- Insert the pragma in the tree | |
2504 | ||
2505 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2506 | Add_Global_Declaration (Prag); | |
7c443ae8 | 2507 | Analyze (Prag); |
2508 | ||
2509 | -- PPC pragmas associated with subprogram bodies must be inserted in | |
2510 | -- the declarative part of the body. | |
2511 | ||
2512 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2513 | Decls := Declarations (Subp_Decl); | |
2514 | ||
2515 | if No (Decls) then | |
2516 | Decls := New_List; | |
2517 | Set_Declarations (Subp_Decl, Decls); | |
2518 | end if; | |
2519 | ||
1bd93de5 | 2520 | Prepend_To (Decls, Prag); |
7c443ae8 | 2521 | |
2522 | -- Ensure the proper visibility of the subprogram body and its | |
2523 | -- parameters. | |
2524 | ||
2525 | Push_Scope (Subp); | |
2526 | Analyze (Prag); | |
2527 | Pop_Scope; | |
2528 | ||
2529 | -- For subprogram declarations insert the PPC pragma right after the | |
2530 | -- declarative node. | |
2531 | ||
b73adb97 | 2532 | else |
7c443ae8 | 2533 | Insert_After_And_Analyze (Subp_Decl, Prag); |
b73adb97 | 2534 | end if; |
4a9e7f0c | 2535 | end Build_PPC_Pragma; |
2536 | ||
2537 | -- Local variables | |
2538 | ||
2539 | Formal : Entity_Id; | |
4a9e7f0c | 2540 | Subp_Spec : Node_Id; |
2541 | ||
3b045963 | 2542 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2543 | |
2544 | begin | |
4a9e7f0c | 2545 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2546 | |
4a9e7f0c | 2547 | Subp_Spec := Parent (Subp); |
a45d946f | 2548 | |
4a9e7f0c | 2549 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2550 | Subp_Spec := Parent (Subp_Spec); | |
2551 | end if; | |
a45d946f | 2552 | |
4a9e7f0c | 2553 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2554 | |
b73adb97 | 2555 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2556 | |
2557 | -- Do not process formal subprograms because the corresponding actual | |
2558 | -- will receive the proper checks when the instance is analyzed. | |
2559 | ||
2560 | or else Is_Formal_Subprogram (Subp) | |
2561 | ||
a45d946f | 2562 | -- Do not process imported subprograms since pre and post conditions |
2563 | -- are never verified on routines coming from a different language. | |
4a9e7f0c | 2564 | |
b73adb97 | 2565 | or else Is_Imported (Subp) |
2566 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2567 | |
a45d946f | 2568 | -- The PPC pragmas generated by this routine do not correspond to |
2569 | -- source aspects, therefore they cannot be applied to abstract | |
2570 | -- subprograms. | |
4a9e7f0c | 2571 | |
7c443ae8 | 2572 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2573 | |
a45d946f | 2574 | -- Do not consider subprogram renaminds because the renamed entity |
2575 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2576 | |
2577 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2578 | ||
a45d946f | 2579 | -- Do not process null procedures because there is no benefit of |
2580 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2581 | |
2582 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2583 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2584 | then |
2585 | return; | |
2586 | end if; | |
2587 | ||
4a9e7f0c | 2588 | -- Inspect all the formals applying aliasing and scalar initialization |
2589 | -- checks where applicable. | |
b73adb97 | 2590 | |
2591 | Formal := First_Formal (Subp); | |
2592 | while Present (Formal) loop | |
4a9e7f0c | 2593 | |
2594 | -- Generate the following scalar initialization checks for each | |
2595 | -- formal parameter: | |
2596 | ||
2597 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2598 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2599 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2600 | ||
2601 | if Check_Validity_Of_Parameters then | |
2602 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2603 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2604 | end if; | |
2605 | ||
2606 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2607 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2608 | end if; | |
b73adb97 | 2609 | end if; |
2610 | ||
b73adb97 | 2611 | Next_Formal (Formal); |
2612 | end loop; | |
2613 | ||
a45d946f | 2614 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2615 | |
2616 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2617 | |
a45d946f | 2618 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2619 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2620 | end if; |
3b045963 | 2621 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2622 | |
7aafae1c | 2623 | --------------------------- |
2624 | -- Apply_Predicate_Check -- | |
2625 | --------------------------- | |
2626 | ||
2627 | procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id) is | |
301d5ec3 | 2628 | S : Entity_Id; |
9e58d7ed | 2629 | |
7aafae1c | 2630 | begin |
701d57a4 | 2631 | if Present (Predicate_Function (Typ)) then |
301d5ec3 | 2632 | |
301d5ec3 | 2633 | S := Current_Scope; |
9e58d7ed | 2634 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2635 | S := Scope (S); |
2636 | end loop; | |
2637 | ||
ea822fd4 | 2638 | -- A predicate check does not apply within internally generated |
2639 | -- subprograms, such as TSS functions. | |
2640 | ||
2641 | if Within_Internal_Subprogram then | |
301d5ec3 | 2642 | return; |
22631b41 | 2643 | |
96a2d100 | 2644 | -- If the check appears within the predicate function itself, it |
2645 | -- means that the user specified a check whose formal is the | |
2646 | -- predicated subtype itself, rather than some covering type. This | |
2647 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2648 | |
0e9014a7 | 2649 | elsif Present (S) and then S = Predicate_Function (Typ) then |
96a2d100 | 2650 | Error_Msg_N |
2651 | ("predicate check includes a function call that " | |
cb97ae5c | 2652 | & "requires a predicate check??", Parent (N)); |
96a2d100 | 2653 | Error_Msg_N |
cb97ae5c | 2654 | ("\this will result in infinite recursion??", Parent (N)); |
96a2d100 | 2655 | Insert_Action (N, |
61016a7a | 2656 | Make_Raise_Storage_Error (Sloc (N), |
2657 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2658 | |
64cc9e5d | 2659 | -- Here for normal case of predicate active |
e6281d47 | 2660 | |
61016a7a | 2661 | else |
b04165c4 | 2662 | -- If the type has a static predicate and the expression is known |
2663 | -- at compile time, see if the expression satisfies the predicate. | |
3a75f20b | 2664 | |
2665 | Check_Expression_Against_Static_Predicate (N, Typ); | |
e6281d47 | 2666 | |
301d5ec3 | 2667 | Insert_Action (N, |
2668 | Make_Predicate_Check (Typ, Duplicate_Subexpr (N))); | |
2669 | end if; | |
7aafae1c | 2670 | end if; |
2671 | end Apply_Predicate_Check; | |
2672 | ||
ee6ba406 | 2673 | ----------------------- |
2674 | -- Apply_Range_Check -- | |
2675 | ----------------------- | |
2676 | ||
2677 | procedure Apply_Range_Check | |
2678 | (Ck_Node : Node_Id; | |
2679 | Target_Typ : Entity_Id; | |
2680 | Source_Typ : Entity_Id := Empty) | |
2681 | is | |
2682 | begin | |
2683 | Apply_Selected_Range_Checks | |
2684 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2685 | end Apply_Range_Check; | |
2686 | ||
2687 | ------------------------------ | |
2688 | -- Apply_Scalar_Range_Check -- | |
2689 | ------------------------------ | |
2690 | ||
feff2f05 | 2691 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2692 | -- off if it is already set on. | |
ee6ba406 | 2693 | |
2694 | procedure Apply_Scalar_Range_Check | |
2695 | (Expr : Node_Id; | |
2696 | Target_Typ : Entity_Id; | |
2697 | Source_Typ : Entity_Id := Empty; | |
2698 | Fixed_Int : Boolean := False) | |
2699 | is | |
2700 | Parnt : constant Node_Id := Parent (Expr); | |
2701 | S_Typ : Entity_Id; | |
2702 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2703 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
2704 | OK : Boolean; | |
2705 | ||
2706 | Is_Subscr_Ref : Boolean; | |
2707 | -- Set true if Expr is a subscript | |
2708 | ||
2709 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2710 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2711 | -- case we do not attempt to do an analysis of the value against the | |
2712 | -- range of the subscript, since we don't know the actual subtype. | |
2713 | ||
2714 | Int_Real : Boolean; | |
feff2f05 | 2715 | -- Set to True if Expr should be regarded as a real value even though |
2716 | -- the type of Expr might be discrete. | |
ee6ba406 | 2717 | |
2718 | procedure Bad_Value; | |
2719 | -- Procedure called if value is determined to be out of range | |
2720 | ||
9dfe12ae | 2721 | --------------- |
2722 | -- Bad_Value -- | |
2723 | --------------- | |
2724 | ||
ee6ba406 | 2725 | procedure Bad_Value is |
2726 | begin | |
2727 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2728 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
ee6ba406 | 2729 | Ent => Target_Typ, |
2730 | Typ => Target_Typ); | |
2731 | end Bad_Value; | |
2732 | ||
9dfe12ae | 2733 | -- Start of processing for Apply_Scalar_Range_Check |
2734 | ||
ee6ba406 | 2735 | begin |
2af58f67 | 2736 | -- Return if check obviously not needed |
ee6ba406 | 2737 | |
2af58f67 | 2738 | if |
2739 | -- Not needed inside generic | |
ee6ba406 | 2740 | |
2af58f67 | 2741 | Inside_A_Generic |
2742 | ||
2743 | -- Not needed if previous error | |
2744 | ||
2745 | or else Target_Typ = Any_Type | |
2746 | or else Nkind (Expr) = N_Error | |
2747 | ||
2748 | -- Not needed for non-scalar type | |
2749 | ||
2750 | or else not Is_Scalar_Type (Target_Typ) | |
2751 | ||
2752 | -- Not needed if we know node raises CE already | |
2753 | ||
2754 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2755 | then |
2756 | return; | |
2757 | end if; | |
2758 | ||
2759 | -- Now, see if checks are suppressed | |
2760 | ||
2761 | Is_Subscr_Ref := | |
2762 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2763 | ||
2764 | if Is_Subscr_Ref then | |
2765 | Arr := Prefix (Parnt); | |
2766 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2767 | |
a3a76ccc | 2768 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2769 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2770 | end if; |
ee6ba406 | 2771 | end if; |
2772 | ||
2773 | if not Do_Range_Check (Expr) then | |
2774 | ||
2775 | -- Subscript reference. Check for Index_Checks suppressed | |
2776 | ||
2777 | if Is_Subscr_Ref then | |
2778 | ||
2779 | -- Check array type and its base type | |
2780 | ||
2781 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2782 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2783 | then |
2784 | return; | |
2785 | ||
2786 | -- Check array itself if it is an entity name | |
2787 | ||
2788 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2789 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2790 | then |
2791 | return; | |
2792 | ||
2793 | -- Check expression itself if it is an entity name | |
2794 | ||
2795 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2796 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2797 | then |
2798 | return; | |
2799 | end if; | |
2800 | ||
2801 | -- All other cases, check for Range_Checks suppressed | |
2802 | ||
2803 | else | |
2804 | -- Check target type and its base type | |
2805 | ||
2806 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2807 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2808 | then |
2809 | return; | |
2810 | ||
2811 | -- Check expression itself if it is an entity name | |
2812 | ||
2813 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2814 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2815 | then |
2816 | return; | |
2817 | ||
feff2f05 | 2818 | -- If Expr is part of an assignment statement, then check left |
2819 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2820 | |
2821 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2822 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2823 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2824 | then |
2825 | return; | |
2826 | end if; | |
2827 | end if; | |
2828 | end if; | |
2829 | ||
9dfe12ae | 2830 | -- Do not set range checks if they are killed |
2831 | ||
2832 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2833 | and then Kill_Range_Check (Expr) | |
2834 | then | |
2835 | return; | |
2836 | end if; | |
2837 | ||
2838 | -- Do not set range checks for any values from System.Scalar_Values | |
39a0c1d3 | 2839 | -- since the whole idea of such values is to avoid checking them. |
9dfe12ae | 2840 | |
2841 | if Is_Entity_Name (Expr) | |
2842 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2843 | then | |
2844 | return; | |
2845 | end if; | |
2846 | ||
ee6ba406 | 2847 | -- Now see if we need a check |
2848 | ||
2849 | if No (Source_Typ) then | |
2850 | S_Typ := Etype (Expr); | |
2851 | else | |
2852 | S_Typ := Source_Typ; | |
2853 | end if; | |
2854 | ||
2855 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
2856 | return; | |
2857 | end if; | |
2858 | ||
2859 | Is_Unconstrained_Subscr_Ref := | |
2860 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
2861 | ||
b40670e1 | 2862 | -- Special checks for floating-point type |
ee6ba406 | 2863 | |
b40670e1 | 2864 | if Is_Floating_Point_Type (S_Typ) then |
2865 | ||
2866 | -- Always do a range check if the source type includes infinities and | |
2867 | -- the target type does not include infinities. We do not do this if | |
2868 | -- range checks are killed. | |
2869 | ||
2870 | if Has_Infinities (S_Typ) | |
2871 | and then not Has_Infinities (Target_Typ) | |
2872 | then | |
2873 | Enable_Range_Check (Expr); | |
2874 | ||
2875 | -- Always do a range check for operators if option set | |
2876 | ||
2877 | elsif Check_Float_Overflow and then Nkind (Expr) in N_Op then | |
2878 | Enable_Range_Check (Expr); | |
2879 | end if; | |
ee6ba406 | 2880 | end if; |
2881 | ||
feff2f05 | 2882 | -- Return if we know expression is definitely in the range of the target |
2883 | -- type as determined by Determine_Range. Right now we only do this for | |
2884 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 2885 | |
f2a06be9 | 2886 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 2887 | |
feff2f05 | 2888 | -- Note: skip this if we are given a source_typ, since the point of |
2889 | -- supplying a Source_Typ is to stop us looking at the expression. | |
2890 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 2891 | |
2892 | if Is_Discrete_Type (Target_Typ) | |
2893 | and then Is_Discrete_Type (Etype (Expr)) | |
2894 | and then not Is_Unconstrained_Subscr_Ref | |
2895 | and then No (Source_Typ) | |
2896 | then | |
2897 | declare | |
2898 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
2899 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
2900 | Lo : Uint; | |
2901 | Hi : Uint; | |
2902 | ||
2903 | begin | |
2904 | if Compile_Time_Known_Value (Tlo) | |
2905 | and then Compile_Time_Known_Value (Thi) | |
2906 | then | |
9dfe12ae | 2907 | declare |
2908 | Lov : constant Uint := Expr_Value (Tlo); | |
2909 | Hiv : constant Uint := Expr_Value (Thi); | |
ee6ba406 | 2910 | |
9dfe12ae | 2911 | begin |
2912 | -- If range is null, we for sure have a constraint error | |
2913 | -- (we don't even need to look at the value involved, | |
2914 | -- since all possible values will raise CE). | |
2915 | ||
2916 | if Lov > Hiv then | |
2917 | Bad_Value; | |
2918 | return; | |
2919 | end if; | |
2920 | ||
2921 | -- Otherwise determine range of value | |
2922 | ||
9c486805 | 2923 | Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True); |
9dfe12ae | 2924 | |
2925 | if OK then | |
2926 | ||
2927 | -- If definitely in range, all OK | |
ee6ba406 | 2928 | |
ee6ba406 | 2929 | if Lo >= Lov and then Hi <= Hiv then |
2930 | return; | |
2931 | ||
9dfe12ae | 2932 | -- If definitely not in range, warn |
2933 | ||
ee6ba406 | 2934 | elsif Lov > Hi or else Hiv < Lo then |
2935 | Bad_Value; | |
2936 | return; | |
9dfe12ae | 2937 | |
2938 | -- Otherwise we don't know | |
2939 | ||
2940 | else | |
2941 | null; | |
ee6ba406 | 2942 | end if; |
9dfe12ae | 2943 | end if; |
2944 | end; | |
ee6ba406 | 2945 | end if; |
2946 | end; | |
2947 | end if; | |
2948 | ||
2949 | Int_Real := | |
2950 | Is_Floating_Point_Type (S_Typ) | |
2951 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
2952 | ||
2953 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 2954 | -- range of the target type. Note that if S_Typ is within the bounds |
2955 | -- of Target_Typ then this must be the case. This check is meaningful | |
2956 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 2957 | |
2958 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 2959 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 2960 | and then |
7a1dabb3 | 2961 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ee6ba406 | 2962 | or else |
9c486805 | 2963 | Is_In_Range (Expr, Target_Typ, |
2964 | Assume_Valid => True, | |
b40670e1 | 2965 | Fixed_Int => Fixed_Int, |
2966 | Int_Real => Int_Real)) | |
ee6ba406 | 2967 | then |
2968 | return; | |
2969 | ||
9c486805 | 2970 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
2971 | Assume_Valid => True, | |
2972 | Fixed_Int => Fixed_Int, | |
2973 | Int_Real => Int_Real) | |
2974 | then | |
ee6ba406 | 2975 | Bad_Value; |
2976 | return; | |
2977 | ||
b40670e1 | 2978 | -- Floating-point case |
feff2f05 | 2979 | -- In the floating-point case, we only do range checks if the type is |
2980 | -- constrained. We definitely do NOT want range checks for unconstrained | |
2981 | -- types, since we want to have infinities | |
ee6ba406 | 2982 | |
9dfe12ae | 2983 | elsif Is_Floating_Point_Type (S_Typ) then |
b40670e1 | 2984 | |
2985 | -- Normally, we only do range checks if the type is constrained. We do | |
2986 | -- NOT want range checks for unconstrained types, since we want to have | |
2987 | -- infinities. Override this decision in Check_Float_Overflow mode. | |
2988 | ||
2989 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then | |
9dfe12ae | 2990 | Enable_Range_Check (Expr); |
2991 | end if; | |
ee6ba406 | 2992 | |
9dfe12ae | 2993 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 2994 | |
2995 | else | |
2996 | Enable_Range_Check (Expr); | |
2997 | return; | |
2998 | end if; | |
ee6ba406 | 2999 | end Apply_Scalar_Range_Check; |
3000 | ||
3001 | ---------------------------------- | |
3002 | -- Apply_Selected_Length_Checks -- | |
3003 | ---------------------------------- | |
3004 | ||
3005 | procedure Apply_Selected_Length_Checks | |
3006 | (Ck_Node : Node_Id; | |
3007 | Target_Typ : Entity_Id; | |
3008 | Source_Typ : Entity_Id; | |
3009 | Do_Static : Boolean) | |
3010 | is | |
3011 | Cond : Node_Id; | |
3012 | R_Result : Check_Result; | |
3013 | R_Cno : Node_Id; | |
3014 | ||
3015 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
3016 | Checks_On : constant Boolean := | |
b6341c67 | 3017 | (not Index_Checks_Suppressed (Target_Typ)) |
4098232e | 3018 | or else (not Length_Checks_Suppressed (Target_Typ)); |
ee6ba406 | 3019 | |
3020 | begin | |
4098232e | 3021 | -- Note: this means that we lose some useful warnings if the expander |
3022 | -- is not active, and we also lose these warnings in SPARK mode ??? | |
3023 | ||
a33565dd | 3024 | if not Expander_Active then |
ee6ba406 | 3025 | return; |
3026 | end if; | |
3027 | ||
3028 | R_Result := | |
3029 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3030 | ||
3031 | for J in 1 .. 2 loop | |
ee6ba406 | 3032 | R_Cno := R_Result (J); |
3033 | exit when No (R_Cno); | |
3034 | ||
3035 | -- A length check may mention an Itype which is attached to a | |
3036 | -- subsequent node. At the top level in a package this can cause | |
3037 | -- an order-of-elaboration problem, so we make sure that the itype | |
3038 | -- is referenced now. | |
3039 | ||
3040 | if Ekind (Current_Scope) = E_Package | |
3041 | and then Is_Compilation_Unit (Current_Scope) | |
3042 | then | |
3043 | Ensure_Defined (Target_Typ, Ck_Node); | |
3044 | ||
3045 | if Present (Source_Typ) then | |
3046 | Ensure_Defined (Source_Typ, Ck_Node); | |
3047 | ||
3048 | elsif Is_Itype (Etype (Ck_Node)) then | |
3049 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
3050 | end if; | |
3051 | end if; | |
3052 | ||
feff2f05 | 3053 | -- If the item is a conditional raise of constraint error, then have |
3054 | -- a look at what check is being performed and ??? | |
ee6ba406 | 3055 | |
3056 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3057 | and then Present (Condition (R_Cno)) | |
3058 | then | |
3059 | Cond := Condition (R_Cno); | |
3060 | ||
0577b0b1 | 3061 | -- Case where node does not now have a dynamic check |
ee6ba406 | 3062 | |
0577b0b1 | 3063 | if not Has_Dynamic_Length_Check (Ck_Node) then |
3064 | ||
3065 | -- If checks are on, just insert the check | |
3066 | ||
3067 | if Checks_On then | |
3068 | Insert_Action (Ck_Node, R_Cno); | |
3069 | ||
3070 | if not Do_Static then | |
3071 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
3072 | end if; | |
3073 | ||
3074 | -- If checks are off, then analyze the length check after | |
3075 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 3076 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 3077 | -- compile time warning in this case. |
3078 | ||
3079 | else | |
3080 | Set_Parent (R_Cno, Ck_Node); | |
3081 | Analyze (R_Cno); | |
ee6ba406 | 3082 | end if; |
ee6ba406 | 3083 | end if; |
3084 | ||
3085 | -- Output a warning if the condition is known to be True | |
3086 | ||
3087 | if Is_Entity_Name (Cond) | |
3088 | and then Entity (Cond) = Standard_True | |
3089 | then | |
3090 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3091 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 3092 | CE_Length_Check_Failed, |
ee6ba406 | 3093 | Ent => Target_Typ, |
3094 | Typ => Target_Typ); | |
3095 | ||
3096 | -- If we were only doing a static check, or if checks are not | |
3097 | -- on, then we want to delete the check, since it is not needed. | |
3098 | -- We do this by replacing the if statement by a null statement | |
3099 | ||
3100 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3101 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3102 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3103 | end if; | |
3104 | ||
3105 | else | |
3106 | Install_Static_Check (R_Cno, Loc); | |
3107 | end if; | |
ee6ba406 | 3108 | end loop; |
ee6ba406 | 3109 | end Apply_Selected_Length_Checks; |
3110 | ||
3111 | --------------------------------- | |
3112 | -- Apply_Selected_Range_Checks -- | |
3113 | --------------------------------- | |
3114 | ||
3115 | procedure Apply_Selected_Range_Checks | |
3116 | (Ck_Node : Node_Id; | |
3117 | Target_Typ : Entity_Id; | |
3118 | Source_Typ : Entity_Id; | |
3119 | Do_Static : Boolean) | |
3120 | is | |
ee6ba406 | 3121 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
3122 | Checks_On : constant Boolean := | |
f9bcba0d | 3123 | not Index_Checks_Suppressed (Target_Typ) |
5372d110 | 3124 | or else |
3125 | not Range_Checks_Suppressed (Target_Typ); | |
f9bcba0d | 3126 | |
3127 | Cond : Node_Id; | |
3128 | R_Cno : Node_Id; | |
3129 | R_Result : Check_Result; | |
ee6ba406 | 3130 | |
3131 | begin | |
5372d110 | 3132 | if not Expander_Active or not Checks_On then |
ee6ba406 | 3133 | return; |
3134 | end if; | |
3135 | ||
3136 | R_Result := | |
3137 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3138 | ||
3139 | for J in 1 .. 2 loop | |
ee6ba406 | 3140 | R_Cno := R_Result (J); |
3141 | exit when No (R_Cno); | |
3142 | ||
f9bcba0d | 3143 | -- The range check requires runtime evaluation. Depending on what its |
3144 | -- triggering condition is, the check may be converted into a compile | |
3145 | -- time constraint check. | |
ee6ba406 | 3146 | |
3147 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3148 | and then Present (Condition (R_Cno)) | |
3149 | then | |
3150 | Cond := Condition (R_Cno); | |
3151 | ||
f9bcba0d | 3152 | -- Insert the range check before the related context. Note that |
3153 | -- this action analyses the triggering condition. | |
ee6ba406 | 3154 | |
f9bcba0d | 3155 | Insert_Action (Ck_Node, R_Cno); |
3156 | ||
3157 | -- This old code doesn't make sense, why is the context flagged as | |
3158 | -- requiring dynamic range checks now in the middle of generating | |
3159 | -- them ??? | |
3160 | ||
3161 | if not Do_Static then | |
3162 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
ee6ba406 | 3163 | end if; |
3164 | ||
f9bcba0d | 3165 | -- The triggering condition evaluates to True, the range check |
3166 | -- can be converted into a compile time constraint check. | |
ee6ba406 | 3167 | |
3168 | if Is_Entity_Name (Cond) | |
3169 | and then Entity (Cond) = Standard_True | |
3170 | then | |
feff2f05 | 3171 | -- Since an N_Range is technically not an expression, we have |
3172 | -- to set one of the bounds to C_E and then just flag the | |
3173 | -- N_Range. The warning message will point to the lower bound | |
3174 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3175 | |
3176 | if Nkind (Ck_Node) = N_Range then | |
3177 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3178 | (Low_Bound (Ck_Node), |
3179 | "static range out of bounds of}??", | |
f15731c4 | 3180 | CE_Range_Check_Failed, |
ee6ba406 | 3181 | Ent => Target_Typ, |
3182 | Typ => Target_Typ); | |
3183 | ||
3184 | Set_Raises_Constraint_Error (Ck_Node); | |
3185 | ||
3186 | else | |
3187 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3188 | (Ck_Node, |
1581f2d7 | 3189 | "static value out of range of}??", |
f15731c4 | 3190 | CE_Range_Check_Failed, |
ee6ba406 | 3191 | Ent => Target_Typ, |
3192 | Typ => Target_Typ); | |
3193 | end if; | |
3194 | ||
3195 | -- If we were only doing a static check, or if checks are not | |
3196 | -- on, then we want to delete the check, since it is not needed. | |
3197 | -- We do this by replacing the if statement by a null statement | |
3198 | ||
f9bcba0d | 3199 | -- Why are we even generating checks if checks are turned off ??? |
3200 | ||
ee6ba406 | 3201 | elsif Do_Static or else not Checks_On then |
00c403ee | 3202 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3203 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3204 | end if; | |
3205 | ||
f9bcba0d | 3206 | -- The range check raises Constrant_Error explicitly |
3207 | ||
ee6ba406 | 3208 | else |
3209 | Install_Static_Check (R_Cno, Loc); | |
3210 | end if; | |
ee6ba406 | 3211 | end loop; |
ee6ba406 | 3212 | end Apply_Selected_Range_Checks; |
3213 | ||
3214 | ------------------------------- | |
3215 | -- Apply_Static_Length_Check -- | |
3216 | ------------------------------- | |
3217 | ||
3218 | procedure Apply_Static_Length_Check | |
3219 | (Expr : Node_Id; | |
3220 | Target_Typ : Entity_Id; | |
3221 | Source_Typ : Entity_Id := Empty) | |
3222 | is | |
3223 | begin | |
3224 | Apply_Selected_Length_Checks | |
3225 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3226 | end Apply_Static_Length_Check; | |
3227 | ||
3228 | ------------------------------------- | |
3229 | -- Apply_Subscript_Validity_Checks -- | |
3230 | ------------------------------------- | |
3231 | ||
3232 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3233 | Sub : Node_Id; | |
3234 | ||
3235 | begin | |
3236 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3237 | ||
3238 | -- Loop through subscripts | |
3239 | ||
3240 | Sub := First (Expressions (Expr)); | |
3241 | while Present (Sub) loop | |
3242 | ||
feff2f05 | 3243 | -- Check one subscript. Note that we do not worry about enumeration |
3244 | -- type with holes, since we will convert the value to a Pos value | |
3245 | -- for the subscript, and that convert will do the necessary validity | |
3246 | -- check. | |
ee6ba406 | 3247 | |
3248 | Ensure_Valid (Sub, Holes_OK => True); | |
3249 | ||
3250 | -- Move to next subscript | |
3251 | ||
3252 | Sub := Next (Sub); | |
3253 | end loop; | |
3254 | end Apply_Subscript_Validity_Checks; | |
3255 | ||
3256 | ---------------------------------- | |
3257 | -- Apply_Type_Conversion_Checks -- | |
3258 | ---------------------------------- | |
3259 | ||
3260 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3261 | Target_Type : constant Entity_Id := Etype (N); | |
3262 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3263 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3264 | |
3265 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3266 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3267 | -- full view might have discriminants with defaults, so we need the | |
3268 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3269 | |
3270 | begin | |
3271 | if Inside_A_Generic then | |
3272 | return; | |
3273 | ||
f15731c4 | 3274 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3275 | -- situations of incomplete trees that blow things up. |
3276 | ||
f15731c4 | 3277 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3278 | return; |
3279 | ||
ea822fd4 | 3280 | -- Never generate discriminant checks for Unchecked_Union types |
3281 | ||
3282 | elsif Present (Expr_Type) | |
3283 | and then Is_Unchecked_Union (Expr_Type) | |
3284 | then | |
3285 | return; | |
3286 | ||
feff2f05 | 3287 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3288 | -- range check if we cannot be sure that Expr is in the base type of | |
3289 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3290 | -- are not quite the same condition from an implementation point of | |
3291 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3292 | |
3293 | elsif Is_Scalar_Type (Target_Type) then | |
3294 | declare | |
3295 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3296 | -- If the Conversion_OK flag on the type conversion is set and no |
ea822fd4 | 3297 | -- floating-point type is involved in the type conversion then |
3298 | -- fixed-point values must be read as integral values. | |
ee6ba406 | 3299 | |
5329ca64 | 3300 | Float_To_Int : constant Boolean := |
b6341c67 | 3301 | Is_Floating_Point_Type (Expr_Type) |
3302 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3303 | |
ee6ba406 | 3304 | begin |
ee6ba406 | 3305 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3306 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3307 | and then not |
7a1dabb3 | 3308 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3309 | and then not Float_To_Int |
ee6ba406 | 3310 | then |
00c403ee | 3311 | Activate_Overflow_Check (N); |
ee6ba406 | 3312 | end if; |
3313 | ||
3314 | if not Range_Checks_Suppressed (Target_Type) | |
3315 | and then not Range_Checks_Suppressed (Expr_Type) | |
3316 | then | |
5329ca64 | 3317 | if Float_To_Int then |
3318 | Apply_Float_Conversion_Check (Expr, Target_Type); | |
3319 | else | |
3320 | Apply_Scalar_Range_Check | |
3321 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
798afddc | 3322 | |
3323 | -- If the target type has predicates, we need to indicate | |
ea822fd4 | 3324 | -- the need for a check, even if Determine_Range finds that |
3325 | -- the value is within bounds. This may be the case e.g for | |
3326 | -- a division with a constant denominator. | |
798afddc | 3327 | |
3328 | if Has_Predicates (Target_Type) then | |
3329 | Enable_Range_Check (Expr); | |
3330 | end if; | |
5329ca64 | 3331 | end if; |
ee6ba406 | 3332 | end if; |
3333 | end; | |
3334 | ||
3335 | elsif Comes_From_Source (N) | |
f40f9731 | 3336 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3337 | and then Is_Record_Type (Target_Type) |
3338 | and then Is_Derived_Type (Target_Type) | |
3339 | and then not Is_Tagged_Type (Target_Type) | |
3340 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3341 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3342 | then |
141d591a | 3343 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3344 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3345 | -- constraint, to verify that the expression of the parent type |
ea822fd4 | 3346 | -- satisfies the constraints imposed by the (unconstrained) derived |
3347 | -- type. This applies to value conversions, not to view conversions | |
3348 | -- of tagged types. | |
ee6ba406 | 3349 | |
3350 | declare | |
9dfe12ae | 3351 | Loc : constant Source_Ptr := Sloc (N); |
3352 | Cond : Node_Id; | |
3353 | Constraint : Elmt_Id; | |
3354 | Discr_Value : Node_Id; | |
3355 | Discr : Entity_Id; | |
3356 | ||
3357 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3358 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3359 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3360 | |
3361 | begin | |
9dfe12ae | 3362 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3363 | while Present (Constraint) loop |
3364 | Discr_Value := Node (Constraint); | |
3365 | ||
3366 | if Is_Entity_Name (Discr_Value) | |
3367 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3368 | then | |
3369 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3370 | ||
3371 | if Present (Discr) | |
3372 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3373 | then | |
3374 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3375 | -- Value of original discriminant in expression. If the |
3376 | -- new discriminant has been used to constrain more than | |
3377 | -- one of the stored discriminants, this will provide the | |
3378 | -- required consistency check. | |
ee6ba406 | 3379 | |
55868293 | 3380 | Append_Elmt |
3381 | (Make_Selected_Component (Loc, | |
3382 | Prefix => | |
9dfe12ae | 3383 | Duplicate_Subexpr_No_Checks |
3384 | (Expr, Name_Req => True), | |
ee6ba406 | 3385 | Selector_Name => |
3386 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3387 | New_Constraints); |
ee6ba406 | 3388 | |
3389 | else | |
3390 | -- Discriminant of more remote ancestor ??? | |
3391 | ||
3392 | return; | |
3393 | end if; | |
3394 | ||
feff2f05 | 3395 | -- Derived type definition has an explicit value for this |
3396 | -- stored discriminant. | |
ee6ba406 | 3397 | |
3398 | else | |
3399 | Append_Elmt | |
9dfe12ae | 3400 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3401 | New_Constraints); | |
ee6ba406 | 3402 | end if; |
3403 | ||
3404 | Next_Elmt (Constraint); | |
3405 | end loop; | |
3406 | ||
3407 | -- Use the unconstrained expression type to retrieve the | |
3408 | -- discriminants of the parent, and apply momentarily the | |
3409 | -- discriminant constraint synthesized above. | |
3410 | ||
3411 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3412 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3413 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3414 | ||
3415 | Insert_Action (N, | |
f15731c4 | 3416 | Make_Raise_Constraint_Error (Loc, |
3417 | Condition => Cond, | |
3418 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3419 | end; |
3420 | ||
175a6969 | 3421 | -- For arrays, checks are set now, but conversions are applied during |
3422 | -- expansion, to take into accounts changes of representation. The | |
3423 | -- checks become range checks on the base type or length checks on the | |
3424 | -- subtype, depending on whether the target type is unconstrained or | |
8e802312 | 3425 | -- constrained. Note that the range check is put on the expression of a |
3426 | -- type conversion, while the length check is put on the type conversion | |
3427 | -- itself. | |
175a6969 | 3428 | |
3429 | elsif Is_Array_Type (Target_Type) then | |
3430 | if Is_Constrained (Target_Type) then | |
3431 | Set_Do_Length_Check (N); | |
3432 | else | |
3433 | Set_Do_Range_Check (Expr); | |
3434 | end if; | |
ee6ba406 | 3435 | end if; |
ee6ba406 | 3436 | end Apply_Type_Conversion_Checks; |
3437 | ||
3438 | ---------------------------------------------- | |
3439 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3440 | ---------------------------------------------- | |
3441 | ||
3442 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3443 | Loc : constant Source_Ptr := Sloc (N); | |
3444 | Typ : constant Entity_Id := Etype (N); | |
3445 | ||
3446 | begin | |
3447 | if Inside_A_Generic then | |
3448 | return; | |
3449 | ||
3450 | -- Nothing to do if checks are suppressed | |
3451 | ||
3452 | elsif Range_Checks_Suppressed (Typ) | |
3453 | and then Overflow_Checks_Suppressed (Typ) | |
3454 | then | |
3455 | return; | |
3456 | ||
3457 | -- Nothing to do if the attribute does not come from source. The | |
3458 | -- internal attributes we generate of this type do not need checks, | |
3459 | -- and furthermore the attempt to check them causes some circular | |
3460 | -- elaboration orders when dealing with packed types. | |
3461 | ||
3462 | elsif not Comes_From_Source (N) then | |
3463 | return; | |
3464 | ||
9dfe12ae | 3465 | -- If the prefix is a selected component that depends on a discriminant |
3466 | -- the check may improperly expose a discriminant instead of using | |
3467 | -- the bounds of the object itself. Set the type of the attribute to | |
3468 | -- the base type of the context, so that a check will be imposed when | |
3469 | -- needed (e.g. if the node appears as an index). | |
3470 | ||
3471 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3472 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3473 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3474 | then | |
3475 | Set_Etype (N, Base_Type (Typ)); | |
3476 | ||
feff2f05 | 3477 | -- Otherwise, replace the attribute node with a type conversion node |
3478 | -- whose expression is the attribute, retyped to universal integer, and | |
3479 | -- whose subtype mark is the target type. The call to analyze this | |
3480 | -- conversion will set range and overflow checks as required for proper | |
3481 | -- detection of an out of range value. | |
ee6ba406 | 3482 | |
3483 | else | |
3484 | Set_Etype (N, Universal_Integer); | |
3485 | Set_Analyzed (N, True); | |
3486 | ||
3487 | Rewrite (N, | |
3488 | Make_Type_Conversion (Loc, | |
3489 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3490 | Expression => Relocate_Node (N))); | |
3491 | ||
3492 | Analyze_And_Resolve (N, Typ); | |
3493 | return; | |
3494 | end if; | |
ee6ba406 | 3495 | end Apply_Universal_Integer_Attribute_Checks; |
3496 | ||
07c191b0 | 3497 | ------------------------------------- |
3498 | -- Atomic_Synchronization_Disabled -- | |
3499 | ------------------------------------- | |
3500 | ||
3501 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3502 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3503 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3504 | ||
3505 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3506 | begin | |
b444f81d | 3507 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3508 | -- looks enabled, since it is never disabled. | |
3509 | ||
3510 | if Debug_Flag_Dot_E then | |
3511 | return False; | |
3512 | ||
3513 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3514 | -- sync looks disabled, since it always tests True. | |
3515 | ||
3516 | elsif Debug_Flag_Dot_D then | |
3517 | return True; | |
3518 | ||
3519 | -- If entity present, then check result for that entity | |
3520 | ||
3521 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3522 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3523 | |
3524 | -- Otherwise result depends on current scope setting | |
3525 | ||
07c191b0 | 3526 | else |
fafc6b97 | 3527 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3528 | end if; |
3529 | end Atomic_Synchronization_Disabled; | |
3530 | ||
ee6ba406 | 3531 | ------------------------------- |
3532 | -- Build_Discriminant_Checks -- | |
3533 | ------------------------------- | |
3534 | ||
3535 | function Build_Discriminant_Checks | |
3536 | (N : Node_Id; | |
314a23b6 | 3537 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3538 | is |
3539 | Loc : constant Source_Ptr := Sloc (N); | |
3540 | Cond : Node_Id; | |
3541 | Disc : Elmt_Id; | |
3542 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3543 | Dref : Node_Id; |
ee6ba406 | 3544 | Dval : Node_Id; |
3545 | ||
84d0d4a5 | 3546 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3547 | ||
3548 | ---------------------------------- | |
3549 | -- Aggregate_Discriminant_Value -- | |
3550 | ---------------------------------- | |
3551 | ||
3552 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3553 | Assoc : Node_Id; | |
3554 | ||
3555 | begin | |
feff2f05 | 3556 | -- The aggregate has been normalized with named associations. We use |
3557 | -- the Chars field to locate the discriminant to take into account | |
3558 | -- discriminants in derived types, which carry the same name as those | |
3559 | -- in the parent. | |
84d0d4a5 | 3560 | |
3561 | Assoc := First (Component_Associations (N)); | |
3562 | while Present (Assoc) loop | |
3563 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3564 | return Expression (Assoc); | |
3565 | else | |
3566 | Next (Assoc); | |
3567 | end if; | |
3568 | end loop; | |
3569 | ||
3570 | -- Discriminant must have been found in the loop above | |
3571 | ||
3572 | raise Program_Error; | |
3573 | end Aggregate_Discriminant_Val; | |
3574 | ||
3575 | -- Start of processing for Build_Discriminant_Checks | |
3576 | ||
ee6ba406 | 3577 | begin |
84d0d4a5 | 3578 | -- Loop through discriminants evolving the condition |
3579 | ||
ee6ba406 | 3580 | Cond := Empty; |
3581 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3582 | ||
9dfe12ae | 3583 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3584 | |
3585 | if Is_Private_Type (T_Typ) | |
3586 | and then No (Full_View (T_Typ)) | |
3587 | then | |
3588 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3589 | else | |
3590 | Disc_Ent := First_Discriminant (T_Typ); | |
3591 | end if; | |
3592 | ||
3593 | while Present (Disc) loop | |
ee6ba406 | 3594 | Dval := Node (Disc); |
3595 | ||
3596 | if Nkind (Dval) = N_Identifier | |
3597 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3598 | then | |
3599 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3600 | else | |
9dfe12ae | 3601 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3602 | end if; |
3603 | ||
00f91aef | 3604 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3605 | -- of the node. | |
9dfe12ae | 3606 | |
00f91aef | 3607 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3608 | Dref := New_Copy ( | |
3609 | Get_Discriminant_Value ( | |
3610 | First_Discriminant (T_Typ), | |
3611 | T_Typ, | |
3612 | Stored_Constraint (T_Typ))); | |
3613 | ||
84d0d4a5 | 3614 | elsif Nkind (N) = N_Aggregate then |
3615 | Dref := | |
3616 | Duplicate_Subexpr_No_Checks | |
3617 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3618 | ||
00f91aef | 3619 | else |
3620 | Dref := | |
3621 | Make_Selected_Component (Loc, | |
20cf157b | 3622 | Prefix => |
00f91aef | 3623 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
20cf157b | 3624 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
00f91aef | 3625 | |
3626 | Set_Is_In_Discriminant_Check (Dref); | |
3627 | end if; | |
9dfe12ae | 3628 | |
ee6ba406 | 3629 | Evolve_Or_Else (Cond, |
3630 | Make_Op_Ne (Loc, | |
20cf157b | 3631 | Left_Opnd => Dref, |
ee6ba406 | 3632 | Right_Opnd => Dval)); |
3633 | ||
3634 | Next_Elmt (Disc); | |
3635 | Next_Discriminant (Disc_Ent); | |
3636 | end loop; | |
3637 | ||
3638 | return Cond; | |
3639 | end Build_Discriminant_Checks; | |
3640 | ||
13dbf220 | 3641 | ------------------ |
3642 | -- Check_Needed -- | |
3643 | ------------------ | |
3644 | ||
3645 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3646 | N : Node_Id; | |
3647 | P : Node_Id; | |
3648 | K : Node_Kind; | |
3649 | L : Node_Id; | |
3650 | R : Node_Id; | |
3651 | ||
9b2068d4 | 3652 | function Left_Expression (Op : Node_Id) return Node_Id; |
3653 | -- Return the relevant expression from the left operand of the given | |
3654 | -- short circuit form: this is LO itself, except if LO is a qualified | |
3655 | -- expression, a type conversion, or an expression with actions, in | |
3656 | -- which case this is Left_Expression (Expression (LO)). | |
3657 | ||
3658 | --------------------- | |
3659 | -- Left_Expression -- | |
3660 | --------------------- | |
3661 | ||
3662 | function Left_Expression (Op : Node_Id) return Node_Id is | |
3663 | LE : Node_Id := Left_Opnd (Op); | |
3664 | begin | |
20cf157b | 3665 | while Nkind_In (LE, N_Qualified_Expression, |
3666 | N_Type_Conversion, | |
3667 | N_Expression_With_Actions) | |
9b2068d4 | 3668 | loop |
3669 | LE := Expression (LE); | |
3670 | end loop; | |
3671 | ||
3672 | return LE; | |
3673 | end Left_Expression; | |
3674 | ||
3675 | -- Start of processing for Check_Needed | |
3676 | ||
13dbf220 | 3677 | begin |
3678 | -- Always check if not simple entity | |
3679 | ||
3680 | if Nkind (Nod) not in N_Has_Entity | |
3681 | or else not Comes_From_Source (Nod) | |
3682 | then | |
3683 | return True; | |
3684 | end if; | |
3685 | ||
3686 | -- Look up tree for short circuit | |
3687 | ||
3688 | N := Nod; | |
3689 | loop | |
3690 | P := Parent (N); | |
3691 | K := Nkind (P); | |
3692 | ||
7b17e51b | 3693 | -- Done if out of subexpression (note that we allow generated stuff |
3694 | -- such as itype declarations in this context, to keep the loop going | |
3695 | -- since we may well have generated such stuff in complex situations. | |
3696 | -- Also done if no parent (probably an error condition, but no point | |
39a0c1d3 | 3697 | -- in behaving nasty if we find it). |
7b17e51b | 3698 | |
3699 | if No (P) | |
3700 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
3701 | then | |
13dbf220 | 3702 | return True; |
3703 | ||
7b17e51b | 3704 | -- Or/Or Else case, where test is part of the right operand, or is |
3705 | -- part of one of the actions associated with the right operand, and | |
3706 | -- the left operand is an equality test. | |
13dbf220 | 3707 | |
7b17e51b | 3708 | elsif K = N_Op_Or then |
13dbf220 | 3709 | exit when N = Right_Opnd (P) |
9b2068d4 | 3710 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3711 | |
7b17e51b | 3712 | elsif K = N_Or_Else then |
3713 | exit when (N = Right_Opnd (P) | |
3714 | or else | |
3715 | (Is_List_Member (N) | |
3716 | and then List_Containing (N) = Actions (P))) | |
9b2068d4 | 3717 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3718 | |
7b17e51b | 3719 | -- Similar test for the And/And then case, where the left operand |
3720 | -- is an inequality test. | |
3721 | ||
3722 | elsif K = N_Op_And then | |
13dbf220 | 3723 | exit when N = Right_Opnd (P) |
9b2068d4 | 3724 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
7b17e51b | 3725 | |
3726 | elsif K = N_And_Then then | |
3727 | exit when (N = Right_Opnd (P) | |
3728 | or else | |
3729 | (Is_List_Member (N) | |
20cf157b | 3730 | and then List_Containing (N) = Actions (P))) |
9b2068d4 | 3731 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
13dbf220 | 3732 | end if; |
3733 | ||
3734 | N := P; | |
3735 | end loop; | |
3736 | ||
3737 | -- If we fall through the loop, then we have a conditional with an | |
9b2068d4 | 3738 | -- appropriate test as its left operand, so look further. |
3739 | ||
3740 | L := Left_Expression (P); | |
3741 | ||
3742 | -- L is an "=" or "/=" operator: extract its operands | |
13dbf220 | 3743 | |
13dbf220 | 3744 | R := Right_Opnd (L); |
3745 | L := Left_Opnd (L); | |
3746 | ||
3747 | -- Left operand of test must match original variable | |
3748 | ||
20cf157b | 3749 | if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
13dbf220 | 3750 | return True; |
3751 | end if; | |
3752 | ||
2af58f67 | 3753 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 3754 | |
3755 | case Check is | |
3756 | when Access_Check => | |
2af58f67 | 3757 | if not Known_Null (R) then |
13dbf220 | 3758 | return True; |
3759 | end if; | |
3760 | ||
3761 | when Division_Check => | |
3762 | if not Compile_Time_Known_Value (R) | |
3763 | or else Expr_Value (R) /= Uint_0 | |
3764 | then | |
3765 | return True; | |
3766 | end if; | |
2af58f67 | 3767 | |
3768 | when others => | |
3769 | raise Program_Error; | |
13dbf220 | 3770 | end case; |
3771 | ||
3772 | -- Here we have the optimizable case, warn if not short-circuited | |
3773 | ||
3774 | if K = N_Op_And or else K = N_Op_Or then | |
c4968aa2 | 3775 | Error_Msg_Warn := SPARK_Mode /= On; |
4098232e | 3776 | |
13dbf220 | 3777 | case Check is |
3778 | when Access_Check => | |
4098232e | 3779 | if GNATprove_Mode then |
3780 | Error_Msg_N | |
3781 | ("Constraint_Error might have been raised (access check)", | |
3782 | Parent (Nod)); | |
3783 | else | |
3784 | Error_Msg_N | |
3785 | ("Constraint_Error may be raised (access check)??", | |
3786 | Parent (Nod)); | |
3787 | end if; | |
3788 | ||
13dbf220 | 3789 | when Division_Check => |
4098232e | 3790 | if GNATprove_Mode then |
3791 | Error_Msg_N | |
3792 | ("Constraint_Error might have been raised (zero divide)", | |
3793 | Parent (Nod)); | |
3794 | else | |
3795 | Error_Msg_N | |
3796 | ("Constraint_Error may be raised (zero divide)??", | |
3797 | Parent (Nod)); | |
3798 | end if; | |
2af58f67 | 3799 | |
3800 | when others => | |
3801 | raise Program_Error; | |
13dbf220 | 3802 | end case; |
3803 | ||
3804 | if K = N_Op_And then | |
e977c0cf | 3805 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3806 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 3807 | else |
e977c0cf | 3808 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3809 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 3810 | end if; |
3811 | ||
6fb3c314 | 3812 | -- If not short-circuited, we need the check |
13dbf220 | 3813 | |
3814 | return True; | |
3815 | ||
3816 | -- If short-circuited, we can omit the check | |
3817 | ||
3818 | else | |
3819 | return False; | |
3820 | end if; | |
3821 | end Check_Needed; | |
3822 | ||
ee6ba406 | 3823 | ----------------------------------- |
3824 | -- Check_Valid_Lvalue_Subscripts -- | |
3825 | ----------------------------------- | |
3826 | ||
3827 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
3828 | begin | |
3829 | -- Skip this if range checks are suppressed | |
3830 | ||
3831 | if Range_Checks_Suppressed (Etype (Expr)) then | |
3832 | return; | |
3833 | ||
feff2f05 | 3834 | -- Only do this check for expressions that come from source. We assume |
3835 | -- that expander generated assignments explicitly include any necessary | |
3836 | -- checks. Note that this is not just an optimization, it avoids | |
39a0c1d3 | 3837 | -- infinite recursions. |
ee6ba406 | 3838 | |
3839 | elsif not Comes_From_Source (Expr) then | |
3840 | return; | |
3841 | ||
3842 | -- For a selected component, check the prefix | |
3843 | ||
3844 | elsif Nkind (Expr) = N_Selected_Component then | |
3845 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3846 | return; | |
3847 | ||
3848 | -- Case of indexed component | |
3849 | ||
3850 | elsif Nkind (Expr) = N_Indexed_Component then | |
3851 | Apply_Subscript_Validity_Checks (Expr); | |
3852 | ||
feff2f05 | 3853 | -- Prefix may itself be or contain an indexed component, and these |
3854 | -- subscripts need checking as well. | |
ee6ba406 | 3855 | |
3856 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3857 | end if; | |
3858 | end Check_Valid_Lvalue_Subscripts; | |
3859 | ||
fa7497e8 | 3860 | ---------------------------------- |
3861 | -- Null_Exclusion_Static_Checks -- | |
3862 | ---------------------------------- | |
3863 | ||
3864 | procedure Null_Exclusion_Static_Checks (N : Node_Id) is | |
0577b0b1 | 3865 | Error_Node : Node_Id; |
3866 | Expr : Node_Id; | |
3867 | Has_Null : constant Boolean := Has_Null_Exclusion (N); | |
3868 | K : constant Node_Kind := Nkind (N); | |
3869 | Typ : Entity_Id; | |
fa7497e8 | 3870 | |
13dbf220 | 3871 | begin |
0577b0b1 | 3872 | pragma Assert |
ea822fd4 | 3873 | (Nkind_In (K, N_Component_Declaration, |
3874 | N_Discriminant_Specification, | |
3875 | N_Function_Specification, | |
3876 | N_Object_Declaration, | |
3877 | N_Parameter_Specification)); | |
0577b0b1 | 3878 | |
3879 | if K = N_Function_Specification then | |
3880 | Typ := Etype (Defining_Entity (N)); | |
3881 | else | |
3882 | Typ := Etype (Defining_Identifier (N)); | |
3883 | end if; | |
fa7497e8 | 3884 | |
13dbf220 | 3885 | case K is |
13dbf220 | 3886 | when N_Component_Declaration => |
3887 | if Present (Access_Definition (Component_Definition (N))) then | |
0577b0b1 | 3888 | Error_Node := Component_Definition (N); |
13dbf220 | 3889 | else |
0577b0b1 | 3890 | Error_Node := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 3891 | end if; |
5329ca64 | 3892 | |
0577b0b1 | 3893 | when N_Discriminant_Specification => |
3894 | Error_Node := Discriminant_Type (N); | |
3895 | ||
3896 | when N_Function_Specification => | |
3897 | Error_Node := Result_Definition (N); | |
3898 | ||
3899 | when N_Object_Declaration => | |
3900 | Error_Node := Object_Definition (N); | |
3901 | ||
3902 | when N_Parameter_Specification => | |
3903 | Error_Node := Parameter_Type (N); | |
3904 | ||
13dbf220 | 3905 | when others => |
3906 | raise Program_Error; | |
3907 | end case; | |
5329ca64 | 3908 | |
0577b0b1 | 3909 | if Has_Null then |
5329ca64 | 3910 | |
0577b0b1 | 3911 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
3912 | -- applied to an access [sub]type. | |
5329ca64 | 3913 | |
0577b0b1 | 3914 | if not Is_Access_Type (Typ) then |
503f7fd3 | 3915 | Error_Msg_N |
00c403ee | 3916 | ("`NOT NULL` allowed only for an access type", Error_Node); |
5329ca64 | 3917 | |
feff2f05 | 3918 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 3919 | -- be applied to a [sub]type that does not exclude null already. |
3920 | ||
3921 | elsif Can_Never_Be_Null (Typ) | |
d16989f1 | 3922 | and then Comes_From_Source (Typ) |
0577b0b1 | 3923 | then |
503f7fd3 | 3924 | Error_Msg_NE |
00c403ee | 3925 | ("`NOT NULL` not allowed (& already excludes null)", |
3926 | Error_Node, Typ); | |
0577b0b1 | 3927 | end if; |
13dbf220 | 3928 | end if; |
5329ca64 | 3929 | |
cc60bd16 | 3930 | -- Check that null-excluding objects are always initialized, except for |
3931 | -- deferred constants, for which the expression will appear in the full | |
3932 | -- declaration. | |
13dbf220 | 3933 | |
3934 | if K = N_Object_Declaration | |
84d0d4a5 | 3935 | and then No (Expression (N)) |
cc60bd16 | 3936 | and then not Constant_Present (N) |
feff2f05 | 3937 | and then not No_Initialization (N) |
13dbf220 | 3938 | then |
feff2f05 | 3939 | -- Add an expression that assigns null. This node is needed by |
3940 | -- Apply_Compile_Time_Constraint_Error, which will replace this with | |
3941 | -- a Constraint_Error node. | |
13dbf220 | 3942 | |
3943 | Set_Expression (N, Make_Null (Sloc (N))); | |
3944 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
5329ca64 | 3945 | |
13dbf220 | 3946 | Apply_Compile_Time_Constraint_Error |
3947 | (N => Expression (N), | |
cb97ae5c | 3948 | Msg => |
3949 | "(Ada 2005) null-excluding objects must be initialized??", | |
13dbf220 | 3950 | Reason => CE_Null_Not_Allowed); |
3951 | end if; | |
5329ca64 | 3952 | |
cc60bd16 | 3953 | -- Check that a null-excluding component, formal or object is not being |
3954 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 3955 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 3956 | |
0577b0b1 | 3957 | if K /= N_Function_Specification then |
3958 | Expr := Expression (N); | |
5329ca64 | 3959 | |
2af58f67 | 3960 | if Present (Expr) and then Known_Null (Expr) then |
13dbf220 | 3961 | case K is |
0577b0b1 | 3962 | when N_Component_Declaration | |
3963 | N_Discriminant_Specification => | |
7189d17f | 3964 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3965 | (N => Expr, |
4098232e | 3966 | Msg => "(Ada 2005) null not allowed " |
3967 | & "in null-excluding components??", | |
0577b0b1 | 3968 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3969 | |
0577b0b1 | 3970 | when N_Object_Declaration => |
7189d17f | 3971 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3972 | (N => Expr, |
4098232e | 3973 | Msg => "(Ada 2005) null not allowed " |
1581f2d7 | 3974 | & "in null-excluding objects??", |
0577b0b1 | 3975 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3976 | |
0577b0b1 | 3977 | when N_Parameter_Specification => |
7189d17f | 3978 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3979 | (N => Expr, |
4098232e | 3980 | Msg => "(Ada 2005) null not allowed " |
3981 | & "in null-excluding formals??", | |
0577b0b1 | 3982 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 3983 | |
3984 | when others => | |
3985 | null; | |
5329ca64 | 3986 | end case; |
3987 | end if; | |
0577b0b1 | 3988 | end if; |
fa7497e8 | 3989 | end Null_Exclusion_Static_Checks; |
3990 | ||
9dfe12ae | 3991 | ---------------------------------- |
3992 | -- Conditional_Statements_Begin -- | |
3993 | ---------------------------------- | |
3994 | ||
3995 | procedure Conditional_Statements_Begin is | |
3996 | begin | |
3997 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
3998 | ||
feff2f05 | 3999 | -- If stack overflows, kill all checks, that way we know to simply reset |
4000 | -- the number of saved checks to zero on return. This should never occur | |
4001 | -- in practice. | |
9dfe12ae | 4002 | |
4003 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4004 | Kill_All_Checks; | |
4005 | ||
feff2f05 | 4006 | -- In the normal case, we just make a new stack entry saving the current |
4007 | -- number of saved checks for a later restore. | |
9dfe12ae | 4008 | |
4009 | else | |
4010 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
4011 | ||
4012 | if Debug_Flag_CC then | |
4013 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
4014 | Num_Saved_Checks); | |
4015 | end if; | |
4016 | end if; | |
4017 | end Conditional_Statements_Begin; | |
4018 | ||
4019 | -------------------------------- | |
4020 | -- Conditional_Statements_End -- | |
4021 | -------------------------------- | |
4022 | ||
4023 | procedure Conditional_Statements_End is | |
4024 | begin | |
4025 | pragma Assert (Saved_Checks_TOS > 0); | |
4026 | ||
feff2f05 | 4027 | -- If the saved checks stack overflowed, then we killed all checks, so |
4028 | -- setting the number of saved checks back to zero is correct. This | |
4029 | -- should never occur in practice. | |
9dfe12ae | 4030 | |
4031 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4032 | Num_Saved_Checks := 0; | |
4033 | ||
feff2f05 | 4034 | -- In the normal case, restore the number of saved checks from the top |
4035 | -- stack entry. | |
9dfe12ae | 4036 | |
4037 | else | |
4038 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
20cf157b | 4039 | |
9dfe12ae | 4040 | if Debug_Flag_CC then |
4041 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
4042 | Num_Saved_Checks); | |
4043 | end if; | |
4044 | end if; | |
4045 | ||
4046 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
4047 | end Conditional_Statements_End; | |
4048 | ||
3cce7f32 | 4049 | ------------------------- |
4050 | -- Convert_From_Bignum -- | |
4051 | ------------------------- | |
4052 | ||
4053 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
4054 | Loc : constant Source_Ptr := Sloc (N); | |
4055 | ||
4056 | begin | |
4057 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
4058 | ||
4059 | -- Construct call From Bignum | |
4060 | ||
4061 | return | |
4062 | Make_Function_Call (Loc, | |
4063 | Name => | |
4064 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4065 | Parameter_Associations => New_List (Relocate_Node (N))); | |
4066 | end Convert_From_Bignum; | |
4067 | ||
4068 | ----------------------- | |
4069 | -- Convert_To_Bignum -- | |
4070 | ----------------------- | |
4071 | ||
4072 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
4073 | Loc : constant Source_Ptr := Sloc (N); | |
4074 | ||
4075 | begin | |
0326b4d4 | 4076 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 4077 | |
4078 | if Is_RTE (Etype (N), RE_Bignum) then | |
4079 | return Relocate_Node (N); | |
4080 | ||
21a55437 | 4081 | -- Otherwise construct call to To_Bignum, converting the operand to the |
4082 | -- required Long_Long_Integer form. | |
3cce7f32 | 4083 | |
4084 | else | |
4085 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
4086 | return | |
4087 | Make_Function_Call (Loc, | |
4088 | Name => | |
4089 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
4090 | Parameter_Associations => New_List ( | |
4091 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
4092 | end if; | |
4093 | end Convert_To_Bignum; | |
4094 | ||
ee6ba406 | 4095 | --------------------- |
4096 | -- Determine_Range -- | |
4097 | --------------------- | |
4098 | ||
6af1bdbc | 4099 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 4100 | type Cache_Index is range 0 .. Cache_Size - 1; |
39a0c1d3 | 4101 | -- Determine size of below cache (power of 2 is more efficient) |
ee6ba406 | 4102 | |
7ac8c2b1 | 4103 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; |
4104 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; | |
4105 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; | |
4106 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
4107 | Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal; | |
4108 | Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal; | |
feff2f05 | 4109 | -- The above arrays are used to implement a small direct cache for |
7ac8c2b1 | 4110 | -- Determine_Range and Determine_Range_R calls. Because of the way these |
4111 | -- subprograms recursively traces subexpressions, and because overflow | |
4112 | -- checking calls the routine on the way up the tree, a quadratic behavior | |
4113 | -- can otherwise be encountered in large expressions. The cache entry for | |
4114 | -- node N is stored in the (N mod Cache_Size) entry, and can be validated | |
4115 | -- by checking the actual node value stored there. The Range_Cache_V array | |
4116 | -- records the setting of Assume_Valid for the cache entry. | |
ee6ba406 | 4117 | |
4118 | procedure Determine_Range | |
9c486805 | 4119 | (N : Node_Id; |
4120 | OK : out Boolean; | |
4121 | Lo : out Uint; | |
4122 | Hi : out Uint; | |
4123 | Assume_Valid : Boolean := False) | |
ee6ba406 | 4124 | is |
e254d721 | 4125 | Typ : Entity_Id := Etype (N); |
4126 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 4127 | |
4128 | Lo_Left : Uint; | |
4129 | Hi_Left : Uint; | |
4130 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 4131 | |
ee6ba406 | 4132 | Lo_Right : Uint; |
ee6ba406 | 4133 | Hi_Right : Uint; |
8880be85 | 4134 | -- Lo and Hi bounds of right (or only) operand |
4135 | ||
4136 | Bound : Node_Id; | |
4137 | -- Temp variable used to hold a bound node | |
4138 | ||
4139 | Hbound : Uint; | |
4140 | -- High bound of base type of expression | |
4141 | ||
4142 | Lor : Uint; | |
4143 | Hir : Uint; | |
4144 | -- Refined values for low and high bounds, after tightening | |
4145 | ||
4146 | OK1 : Boolean; | |
4147 | -- Used in lower level calls to indicate if call succeeded | |
4148 | ||
4149 | Cindex : Cache_Index; | |
4150 | -- Used to search cache | |
ee6ba406 | 4151 | |
094ed68e | 4152 | Btyp : Entity_Id; |
4153 | -- Base type | |
4154 | ||
ee6ba406 | 4155 | function OK_Operands return Boolean; |
4156 | -- Used for binary operators. Determines the ranges of the left and | |
4157 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 4158 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 4159 | |
4160 | ----------------- | |
4161 | -- OK_Operands -- | |
4162 | ----------------- | |
4163 | ||
4164 | function OK_Operands return Boolean is | |
4165 | begin | |
9c486805 | 4166 | Determine_Range |
4167 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 4168 | |
4169 | if not OK1 then | |
4170 | return False; | |
4171 | end if; | |
4172 | ||
9c486805 | 4173 | Determine_Range |
4174 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4175 | return OK1; |
4176 | end OK_Operands; | |
4177 | ||
4178 | -- Start of processing for Determine_Range | |
4179 | ||
4180 | begin | |
e12b2502 | 4181 | -- Prevent junk warnings by initializing range variables |
4182 | ||
4183 | Lo := No_Uint; | |
4184 | Hi := No_Uint; | |
4185 | Lor := No_Uint; | |
4186 | Hir := No_Uint; | |
4187 | ||
87bdc21d | 4188 | -- For temporary constants internally generated to remove side effects |
4189 | -- we must use the corresponding expression to determine the range of | |
e12b2502 | 4190 | -- the expression. But note that the expander can also generate |
4191 | -- constants in other cases, including deferred constants. | |
87bdc21d | 4192 | |
4193 | if Is_Entity_Name (N) | |
4194 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4195 | and then Ekind (Entity (N)) = E_Constant | |
4196 | and then Is_Internal_Name (Chars (Entity (N))) | |
4197 | then | |
e12b2502 | 4198 | if Present (Expression (Parent (Entity (N)))) then |
4199 | Determine_Range | |
4200 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
87bdc21d | 4201 | |
e12b2502 | 4202 | elsif Present (Full_View (Entity (N))) then |
4203 | Determine_Range | |
4204 | (Expression (Parent (Full_View (Entity (N)))), | |
4205 | OK, Lo, Hi, Assume_Valid); | |
ee6ba406 | 4206 | |
e12b2502 | 4207 | else |
4208 | OK := False; | |
4209 | end if; | |
4210 | return; | |
4211 | end if; | |
ee6ba406 | 4212 | |
a781c0fc | 4213 | -- If type is not defined, we can't determine its range |
ee6ba406 | 4214 | |
a781c0fc | 4215 | if No (Typ) |
4216 | ||
4217 | -- We don't deal with anything except discrete types | |
4218 | ||
4219 | or else not Is_Discrete_Type (Typ) | |
4220 | ||
4221 | -- Ignore type for which an error has been posted, since range in | |
4222 | -- this case may well be a bogosity deriving from the error. Also | |
4223 | -- ignore if error posted on the reference node. | |
4224 | ||
4225 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 4226 | then |
4227 | OK := False; | |
4228 | return; | |
4229 | end if; | |
4230 | ||
4231 | -- For all other cases, we can determine the range | |
4232 | ||
4233 | OK := True; | |
4234 | ||
feff2f05 | 4235 | -- If value is compile time known, then the possible range is the one |
39a0c1d3 | 4236 | -- value that we know this expression definitely has. |
ee6ba406 | 4237 | |
4238 | if Compile_Time_Known_Value (N) then | |
4239 | Lo := Expr_Value (N); | |
4240 | Hi := Lo; | |
4241 | return; | |
4242 | end if; | |
4243 | ||
4244 | -- Return if already in the cache | |
4245 | ||
4246 | Cindex := Cache_Index (N mod Cache_Size); | |
4247 | ||
9c486805 | 4248 | if Determine_Range_Cache_N (Cindex) = N |
4249 | and then | |
4250 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4251 | then | |
ee6ba406 | 4252 | Lo := Determine_Range_Cache_Lo (Cindex); |
4253 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4254 | return; | |
4255 | end if; | |
4256 | ||
feff2f05 | 4257 | -- Otherwise, start by finding the bounds of the type of the expression, |
4258 | -- the value cannot be outside this range (if it is, then we have an | |
4259 | -- overflow situation, which is a separate check, we are talking here | |
4260 | -- only about the expression value). | |
ee6ba406 | 4261 | |
341bd953 | 4262 | -- First a check, never try to find the bounds of a generic type, since |
4263 | -- these bounds are always junk values, and it is only valid to look at | |
4264 | -- the bounds in an instance. | |
4265 | ||
4266 | if Is_Generic_Type (Typ) then | |
4267 | OK := False; | |
4268 | return; | |
4269 | end if; | |
4270 | ||
9c486805 | 4271 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4272 | |
9c486805 | 4273 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4274 | or else Assume_No_Invalid_Values | |
4275 | or else Assume_Valid | |
e254d721 | 4276 | then |
9c486805 | 4277 | null; |
4278 | else | |
4279 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4280 | end if; |
4281 | ||
094ed68e | 4282 | -- Retrieve the base type. Handle the case where the base type is a |
4283 | -- private enumeration type. | |
4284 | ||
4285 | Btyp := Base_Type (Typ); | |
4286 | ||
4287 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4288 | Btyp := Full_View (Btyp); | |
4289 | end if; | |
4290 | ||
feff2f05 | 4291 | -- We use the actual bound unless it is dynamic, in which case use the |
4292 | -- corresponding base type bound if possible. If we can't get a bound | |
4293 | -- then we figure we can't determine the range (a peculiar case, that | |
4294 | -- perhaps cannot happen, but there is no point in bombing in this | |
4295 | -- optimization circuit. | |
8880be85 | 4296 | |
4297 | -- First the low bound | |
ee6ba406 | 4298 | |
4299 | Bound := Type_Low_Bound (Typ); | |
4300 | ||
4301 | if Compile_Time_Known_Value (Bound) then | |
4302 | Lo := Expr_Value (Bound); | |
4303 | ||
094ed68e | 4304 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4305 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4306 | |
4307 | else | |
4308 | OK := False; | |
4309 | return; | |
4310 | end if; | |
4311 | ||
8880be85 | 4312 | -- Now the high bound |
4313 | ||
ee6ba406 | 4314 | Bound := Type_High_Bound (Typ); |
4315 | ||
8880be85 | 4316 | -- We need the high bound of the base type later on, and this should |
4317 | -- always be compile time known. Again, it is not clear that this | |
4318 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4319 | |
094ed68e | 4320 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4321 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4322 | Hi := Hbound; |
4323 | ||
4324 | else | |
4325 | OK := False; | |
4326 | return; | |
4327 | end if; | |
4328 | ||
feff2f05 | 4329 | -- If we have a static subtype, then that may have a tighter bound so |
4330 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4331 | |
4332 | if Compile_Time_Known_Value (Bound) then | |
4333 | Hi := Expr_Value (Bound); | |
4334 | end if; | |
4335 | ||
feff2f05 | 4336 | -- We may be able to refine this value in certain situations. If any |
4337 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4338 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4339 | |
4340 | case Nkind (N) is | |
4341 | ||
4342 | -- For unary plus, result is limited by range of operand | |
4343 | ||
4344 | when N_Op_Plus => | |
9c486805 | 4345 | Determine_Range |
4346 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4347 | |
4348 | -- For unary minus, determine range of operand, and negate it | |
4349 | ||
4350 | when N_Op_Minus => | |
9c486805 | 4351 | Determine_Range |
4352 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4353 | |
4354 | if OK1 then | |
4355 | Lor := -Hi_Right; | |
4356 | Hir := -Lo_Right; | |
4357 | end if; | |
4358 | ||
4359 | -- For binary addition, get range of each operand and do the | |
4360 | -- addition to get the result range. | |
4361 | ||
4362 | when N_Op_Add => | |
4363 | if OK_Operands then | |
4364 | Lor := Lo_Left + Lo_Right; | |
4365 | Hir := Hi_Left + Hi_Right; | |
4366 | end if; | |
4367 | ||
feff2f05 | 4368 | -- Division is tricky. The only case we consider is where the right |
4369 | -- operand is a positive constant, and in this case we simply divide | |
4370 | -- the bounds of the left operand | |
ee6ba406 | 4371 | |
4372 | when N_Op_Divide => | |
4373 | if OK_Operands then | |
4374 | if Lo_Right = Hi_Right | |
4375 | and then Lo_Right > 0 | |
4376 | then | |
4377 | Lor := Lo_Left / Lo_Right; | |
4378 | Hir := Hi_Left / Lo_Right; | |
ee6ba406 | 4379 | else |
4380 | OK1 := False; | |
4381 | end if; | |
4382 | end if; | |
4383 | ||
feff2f05 | 4384 | -- For binary subtraction, get range of each operand and do the worst |
4385 | -- case subtraction to get the result range. | |
ee6ba406 | 4386 | |
4387 | when N_Op_Subtract => | |
4388 | if OK_Operands then | |
4389 | Lor := Lo_Left - Hi_Right; | |
4390 | Hir := Hi_Left - Lo_Right; | |
4391 | end if; | |
4392 | ||
feff2f05 | 4393 | -- For MOD, if right operand is a positive constant, then result must |
4394 | -- be in the allowable range of mod results. | |
ee6ba406 | 4395 | |
4396 | when N_Op_Mod => | |
4397 | if OK_Operands then | |
9dfe12ae | 4398 | if Lo_Right = Hi_Right |
4399 | and then Lo_Right /= 0 | |
4400 | then | |
ee6ba406 | 4401 | if Lo_Right > 0 then |
4402 | Lor := Uint_0; | |
4403 | Hir := Lo_Right - 1; | |
4404 | ||
9dfe12ae | 4405 | else -- Lo_Right < 0 |
ee6ba406 | 4406 | Lor := Lo_Right + 1; |
4407 | Hir := Uint_0; | |
4408 | end if; | |
4409 | ||
4410 | else | |
4411 | OK1 := False; | |
4412 | end if; | |
4413 | end if; | |
4414 | ||
feff2f05 | 4415 | -- For REM, if right operand is a positive constant, then result must |
4416 | -- be in the allowable range of mod results. | |
ee6ba406 | 4417 | |
4418 | when N_Op_Rem => | |
4419 | if OK_Operands then | |
9dfe12ae | 4420 | if Lo_Right = Hi_Right |
4421 | and then Lo_Right /= 0 | |
4422 | then | |
ee6ba406 | 4423 | declare |
4424 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4425 | ||
4426 | begin | |
4427 | -- The sign of the result depends on the sign of the | |
4428 | -- dividend (but not on the sign of the divisor, hence | |
4429 | -- the abs operation above). | |
4430 | ||
4431 | if Lo_Left < 0 then | |
4432 | Lor := -Dval; | |
4433 | else | |
4434 | Lor := Uint_0; | |
4435 | end if; | |
4436 | ||
4437 | if Hi_Left < 0 then | |
4438 | Hir := Uint_0; | |
4439 | else | |
4440 | Hir := Dval; | |
4441 | end if; | |
4442 | end; | |
4443 | ||
4444 | else | |
4445 | OK1 := False; | |
4446 | end if; | |
4447 | end if; | |
4448 | ||
4449 | -- Attribute reference cases | |
4450 | ||
4451 | when N_Attribute_Reference => | |
4452 | case Attribute_Name (N) is | |
4453 | ||
4454 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4455 | -- possible range of values of the attribute expression. |
ee6ba406 | 4456 | |
4457 | when Name_Pos | Name_Val => | |
9c486805 | 4458 | Determine_Range |
4459 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4460 | |
4461 | -- For Length attribute, use the bounds of the corresponding | |
4462 | -- index type to refine the range. | |
4463 | ||
4464 | when Name_Length => | |
4465 | declare | |
4466 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4467 | Inum : Nat; | |
4468 | Indx : Node_Id; | |
4469 | ||
4470 | LL, LU : Uint; | |
4471 | UL, UU : Uint; | |
4472 | ||
4473 | begin | |
4474 | if Is_Access_Type (Atyp) then | |
4475 | Atyp := Designated_Type (Atyp); | |
4476 | end if; | |
4477 | ||
4478 | -- For string literal, we know exact value | |
4479 | ||
4480 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4481 | OK := True; | |
4482 | Lo := String_Literal_Length (Atyp); | |
4483 | Hi := String_Literal_Length (Atyp); | |
4484 | return; | |
4485 | end if; | |
4486 | ||
4487 | -- Otherwise check for expression given | |
4488 | ||
4489 | if No (Expressions (N)) then | |
4490 | Inum := 1; | |
4491 | else | |
4492 | Inum := | |
4493 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4494 | end if; | |
4495 | ||
4496 | Indx := First_Index (Atyp); | |
4497 | for J in 2 .. Inum loop | |
4498 | Indx := Next_Index (Indx); | |
4499 | end loop; | |
4500 | ||
9116df93 | 4501 | -- If the index type is a formal type or derived from |
c8da6114 | 4502 | -- one, the bounds are not static. |
4503 | ||
4504 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4505 | OK := False; | |
4506 | return; | |
4507 | end if; | |
4508 | ||
ee6ba406 | 4509 | Determine_Range |
9c486805 | 4510 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4511 | Assume_Valid); | |
ee6ba406 | 4512 | |
4513 | if OK1 then | |
4514 | Determine_Range | |
9c486805 | 4515 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4516 | Assume_Valid); | |
ee6ba406 | 4517 | |
4518 | if OK1 then | |
4519 | ||
4520 | -- The maximum value for Length is the biggest | |
4521 | -- possible gap between the values of the bounds. | |
4522 | -- But of course, this value cannot be negative. | |
4523 | ||
9c486805 | 4524 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4525 | |
4526 | -- For constrained arrays, the minimum value for | |
4527 | -- Length is taken from the actual value of the | |
9116df93 | 4528 | -- bounds, since the index will be exactly of this |
4529 | -- subtype. | |
ee6ba406 | 4530 | |
4531 | if Is_Constrained (Atyp) then | |
9c486805 | 4532 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4533 | |
4534 | -- For an unconstrained array, the minimum value | |
4535 | -- for length is always zero. | |
4536 | ||
4537 | else | |
4538 | Lor := Uint_0; | |
4539 | end if; | |
4540 | end if; | |
4541 | end if; | |
4542 | end; | |
4543 | ||
4544 | -- No special handling for other attributes | |
9116df93 | 4545 | -- Probably more opportunities exist here??? |
ee6ba406 | 4546 | |
4547 | when others => | |
4548 | OK1 := False; | |
4549 | ||
4550 | end case; | |
4551 | ||
feff2f05 | 4552 | -- For type conversion from one discrete type to another, we can |
4553 | -- refine the range using the converted value. | |
ee6ba406 | 4554 | |
4555 | when N_Type_Conversion => | |
9c486805 | 4556 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); |
ee6ba406 | 4557 | |
4558 | -- Nothing special to do for all other expression kinds | |
4559 | ||
4560 | when others => | |
4561 | OK1 := False; | |
4562 | Lor := No_Uint; | |
4563 | Hir := No_Uint; | |
4564 | end case; | |
4565 | ||
9116df93 | 4566 | -- At this stage, if OK1 is true, then we know that the actual result of |
4567 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4568 | -- to restrict the possible range of results. | |
ee6ba406 | 4569 | |
4570 | if OK1 then | |
4571 | ||
9116df93 | 4572 | -- If the refined value of the low bound is greater than the type |
7ac8c2b1 | 4573 | -- low bound, then reset it to the more restrictive value. However, |
9116df93 | 4574 | -- we do NOT do this for the case of a modular type where the |
4575 | -- possible upper bound on the value is above the base type high | |
4576 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4577 | |
4578 | if Lor > Lo | |
9116df93 | 4579 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4580 | then |
4581 | Lo := Lor; | |
4582 | end if; | |
4583 | ||
9116df93 | 4584 | -- Similarly, if the refined value of the high bound is less than the |
4585 | -- value so far, then reset it to the more restrictive value. Again, | |
4586 | -- we do not do this if the refined low bound is negative for a | |
4587 | -- modular type, since this would wrap. | |
ee6ba406 | 4588 | |
4589 | if Hir < Hi | |
9116df93 | 4590 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4591 | then |
4592 | Hi := Hir; | |
4593 | end if; | |
4594 | end if; | |
4595 | ||
4596 | -- Set cache entry for future call and we are all done | |
4597 | ||
4598 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4599 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4600 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4601 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4602 | return; | |
4603 | ||
9116df93 | 4604 | -- If any exception occurs, it means that we have some bug in the compiler, |
4605 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4606 | -- occurrence. However, this is only an optimization attempt, so there is |
4607 | -- really no point in crashing the compiler. Instead we just decide, too | |
4608 | -- bad, we can't figure out a range in this case after all. | |
4609 | ||
4610 | exception | |
4611 | when others => | |
4612 | ||
4613 | -- Debug flag K disables this behavior (useful for debugging) | |
4614 | ||
4615 | if Debug_Flag_K then | |
4616 | raise; | |
4617 | else | |
4618 | OK := False; | |
4619 | Lo := No_Uint; | |
4620 | Hi := No_Uint; | |
4621 | return; | |
4622 | end if; | |
ee6ba406 | 4623 | end Determine_Range; |
4624 | ||
7ac8c2b1 | 4625 | ----------------------- |
4626 | -- Determine_Range_R -- | |
4627 | ----------------------- | |
4628 | ||
4629 | procedure Determine_Range_R | |
4630 | (N : Node_Id; | |
4631 | OK : out Boolean; | |
4632 | Lo : out Ureal; | |
4633 | Hi : out Ureal; | |
4634 | Assume_Valid : Boolean := False) | |
4635 | is | |
4636 | Typ : Entity_Id := Etype (N); | |
4637 | -- Type to use, may get reset to base type for possibly invalid entity | |
4638 | ||
4639 | Lo_Left : Ureal; | |
4640 | Hi_Left : Ureal; | |
4641 | -- Lo and Hi bounds of left operand | |
4642 | ||
4643 | Lo_Right : Ureal; | |
4644 | Hi_Right : Ureal; | |
4645 | -- Lo and Hi bounds of right (or only) operand | |
4646 | ||
4647 | Bound : Node_Id; | |
4648 | -- Temp variable used to hold a bound node | |
4649 | ||
4650 | Hbound : Ureal; | |
4651 | -- High bound of base type of expression | |
4652 | ||
4653 | Lor : Ureal; | |
4654 | Hir : Ureal; | |
4655 | -- Refined values for low and high bounds, after tightening | |
4656 | ||
4657 | OK1 : Boolean; | |
4658 | -- Used in lower level calls to indicate if call succeeded | |
4659 | ||
4660 | Cindex : Cache_Index; | |
4661 | -- Used to search cache | |
4662 | ||
4663 | Btyp : Entity_Id; | |
4664 | -- Base type | |
4665 | ||
4666 | function OK_Operands return Boolean; | |
4667 | -- Used for binary operators. Determines the ranges of the left and | |
4668 | -- right operands, and if they are both OK, returns True, and puts | |
4669 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. | |
4670 | ||
4671 | function Round_Machine (B : Ureal) return Ureal; | |
4672 | -- B is a real bound. Round it using mode Round_Even. | |
4673 | ||
4674 | ----------------- | |
4675 | -- OK_Operands -- | |
4676 | ----------------- | |
4677 | ||
4678 | function OK_Operands return Boolean is | |
4679 | begin | |
4680 | Determine_Range_R | |
4681 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
4682 | ||
4683 | if not OK1 then | |
4684 | return False; | |
4685 | end if; | |
4686 | ||
4687 | Determine_Range_R | |
4688 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
4689 | return OK1; | |
4690 | end OK_Operands; | |
4691 | ||
4692 | ------------------- | |
4693 | -- Round_Machine -- | |
4694 | ------------------- | |
4695 | ||
4696 | function Round_Machine (B : Ureal) return Ureal is | |
4697 | begin | |
4698 | return Machine (Typ, B, Round_Even, N); | |
4699 | end Round_Machine; | |
4700 | ||
4701 | -- Start of processing for Determine_Range_R | |
4702 | ||
4703 | begin | |
4704 | -- Prevent junk warnings by initializing range variables | |
4705 | ||
4706 | Lo := No_Ureal; | |
4707 | Hi := No_Ureal; | |
4708 | Lor := No_Ureal; | |
4709 | Hir := No_Ureal; | |
4710 | ||
4711 | -- For temporary constants internally generated to remove side effects | |
4712 | -- we must use the corresponding expression to determine the range of | |
4713 | -- the expression. But note that the expander can also generate | |
4714 | -- constants in other cases, including deferred constants. | |
4715 | ||
4716 | if Is_Entity_Name (N) | |
4717 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4718 | and then Ekind (Entity (N)) = E_Constant | |
4719 | and then Is_Internal_Name (Chars (Entity (N))) | |
4720 | then | |
4721 | if Present (Expression (Parent (Entity (N)))) then | |
4722 | Determine_Range_R | |
4723 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
4724 | ||
4725 | elsif Present (Full_View (Entity (N))) then | |
4726 | Determine_Range_R | |
4727 | (Expression (Parent (Full_View (Entity (N)))), | |
4728 | OK, Lo, Hi, Assume_Valid); | |
4729 | ||
4730 | else | |
4731 | OK := False; | |
4732 | end if; | |
4c1c7f3f | 4733 | |
7ac8c2b1 | 4734 | return; |
4735 | end if; | |
4736 | ||
4737 | -- If type is not defined, we can't determine its range | |
4738 | ||
4739 | if No (Typ) | |
4740 | ||
4741 | -- We don't deal with anything except IEEE floating-point types | |
4742 | ||
4743 | or else not Is_Floating_Point_Type (Typ) | |
4744 | or else Float_Rep (Typ) /= IEEE_Binary | |
4745 | ||
4746 | -- Ignore type for which an error has been posted, since range in | |
4747 | -- this case may well be a bogosity deriving from the error. Also | |
4748 | -- ignore if error posted on the reference node. | |
4749 | ||
4750 | or else Error_Posted (N) or else Error_Posted (Typ) | |
4751 | then | |
4752 | OK := False; | |
4753 | return; | |
4754 | end if; | |
4755 | ||
4756 | -- For all other cases, we can determine the range | |
4757 | ||
4758 | OK := True; | |
4759 | ||
4760 | -- If value is compile time known, then the possible range is the one | |
4761 | -- value that we know this expression definitely has. | |
4762 | ||
4763 | if Compile_Time_Known_Value (N) then | |
4764 | Lo := Expr_Value_R (N); | |
4765 | Hi := Lo; | |
4766 | return; | |
4767 | end if; | |
4768 | ||
4769 | -- Return if already in the cache | |
4770 | ||
4771 | Cindex := Cache_Index (N mod Cache_Size); | |
4772 | ||
4773 | if Determine_Range_Cache_N (Cindex) = N | |
4774 | and then | |
4775 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4776 | then | |
4777 | Lo := Determine_Range_Cache_Lo_R (Cindex); | |
4778 | Hi := Determine_Range_Cache_Hi_R (Cindex); | |
4779 | return; | |
4780 | end if; | |
4781 | ||
4782 | -- Otherwise, start by finding the bounds of the type of the expression, | |
4783 | -- the value cannot be outside this range (if it is, then we have an | |
4784 | -- overflow situation, which is a separate check, we are talking here | |
4785 | -- only about the expression value). | |
4786 | ||
4787 | -- First a check, never try to find the bounds of a generic type, since | |
4788 | -- these bounds are always junk values, and it is only valid to look at | |
4789 | -- the bounds in an instance. | |
4790 | ||
4791 | if Is_Generic_Type (Typ) then | |
4792 | OK := False; | |
4793 | return; | |
4794 | end if; | |
4795 | ||
4796 | -- First step, change to use base type unless we know the value is valid | |
4797 | ||
4798 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) | |
4799 | or else Assume_No_Invalid_Values | |
4800 | or else Assume_Valid | |
4801 | then | |
4802 | null; | |
4803 | else | |
4804 | Typ := Underlying_Type (Base_Type (Typ)); | |
4805 | end if; | |
4806 | ||
4807 | -- Retrieve the base type. Handle the case where the base type is a | |
4808 | -- private type. | |
4809 | ||
4810 | Btyp := Base_Type (Typ); | |
4811 | ||
4812 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4813 | Btyp := Full_View (Btyp); | |
4814 | end if; | |
4815 | ||
4816 | -- We use the actual bound unless it is dynamic, in which case use the | |
4817 | -- corresponding base type bound if possible. If we can't get a bound | |
4818 | -- then we figure we can't determine the range (a peculiar case, that | |
4819 | -- perhaps cannot happen, but there is no point in bombing in this | |
4820 | -- optimization circuit). | |
4821 | ||
4822 | -- First the low bound | |
4823 | ||
4824 | Bound := Type_Low_Bound (Typ); | |
4825 | ||
4826 | if Compile_Time_Known_Value (Bound) then | |
4827 | Lo := Expr_Value_R (Bound); | |
4828 | ||
4829 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then | |
4830 | Lo := Expr_Value_R (Type_Low_Bound (Btyp)); | |
4831 | ||
4832 | else | |
4833 | OK := False; | |
4834 | return; | |
4835 | end if; | |
4836 | ||
4837 | -- Now the high bound | |
4838 | ||
4839 | Bound := Type_High_Bound (Typ); | |
4840 | ||
4841 | -- We need the high bound of the base type later on, and this should | |
4842 | -- always be compile time known. Again, it is not clear that this | |
4843 | -- can ever be false, but no point in bombing. | |
4844 | ||
4845 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then | |
4846 | Hbound := Expr_Value_R (Type_High_Bound (Btyp)); | |
4847 | Hi := Hbound; | |
4848 | ||
4849 | else | |
4850 | OK := False; | |
4851 | return; | |
4852 | end if; | |
4853 | ||
4854 | -- If we have a static subtype, then that may have a tighter bound so | |
4855 | -- use the upper bound of the subtype instead in this case. | |
4856 | ||
4857 | if Compile_Time_Known_Value (Bound) then | |
4858 | Hi := Expr_Value_R (Bound); | |
4859 | end if; | |
4860 | ||
4861 | -- We may be able to refine this value in certain situations. If any | |
4862 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4863 | -- bounds, and OK1 is set to True. | |
4864 | ||
4865 | case Nkind (N) is | |
4866 | ||
4867 | -- For unary plus, result is limited by range of operand | |
4868 | ||
4869 | when N_Op_Plus => | |
4870 | Determine_Range_R | |
4871 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
4872 | ||
4873 | -- For unary minus, determine range of operand, and negate it | |
4874 | ||
4875 | when N_Op_Minus => | |
4876 | Determine_Range_R | |
4877 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
4878 | ||
4879 | if OK1 then | |
4880 | Lor := -Hi_Right; | |
4881 | Hir := -Lo_Right; | |
4882 | end if; | |
4883 | ||
4884 | -- For binary addition, get range of each operand and do the | |
4885 | -- addition to get the result range. | |
4886 | ||
4887 | when N_Op_Add => | |
4888 | if OK_Operands then | |
4889 | Lor := Round_Machine (Lo_Left + Lo_Right); | |
4890 | Hir := Round_Machine (Hi_Left + Hi_Right); | |
4891 | end if; | |
4892 | ||
4893 | -- For binary subtraction, get range of each operand and do the worst | |
4894 | -- case subtraction to get the result range. | |
4895 | ||
4896 | when N_Op_Subtract => | |
4897 | if OK_Operands then | |
4898 | Lor := Round_Machine (Lo_Left - Hi_Right); | |
4899 | Hir := Round_Machine (Hi_Left - Lo_Right); | |
4900 | end if; | |
4901 | ||
4902 | -- For multiplication, get range of each operand and do the | |
4903 | -- four multiplications to get the result range. | |
4904 | ||
4905 | when N_Op_Multiply => | |
4906 | if OK_Operands then | |
4907 | declare | |
4908 | M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right); | |
4909 | M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right); | |
4910 | M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right); | |
4911 | M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right); | |
4912 | begin | |
4913 | Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4)); | |
4914 | Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4)); | |
4915 | end; | |
4916 | end if; | |
4917 | ||
4918 | -- For division, consider separately the cases where the right | |
4919 | -- operand is positive or negative. Otherwise, the right operand | |
4920 | -- can be arbitrarily close to zero, so the result is likely to | |
4921 | -- be unbounded in one direction, do not attempt to compute it. | |
4922 | ||
4923 | when N_Op_Divide => | |
4924 | if OK_Operands then | |
4925 | ||
4926 | -- Right operand is positive | |
4927 | ||
4928 | if Lo_Right > Ureal_0 then | |
4929 | ||
4930 | -- If the low bound of the left operand is negative, obtain | |
4931 | -- the overall low bound by dividing it by the smallest | |
4932 | -- value of the right operand, and otherwise by the largest | |
4933 | -- value of the right operand. | |
4934 | ||
4935 | if Lo_Left < Ureal_0 then | |
4936 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
4937 | else | |
4938 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
4939 | end if; | |
4940 | ||
4941 | -- If the high bound of the left operand is negative, obtain | |
4942 | -- the overall high bound by dividing it by the largest | |
4943 | -- value of the right operand, and otherwise by the | |
4944 | -- smallest value of the right operand. | |
4945 | ||
4946 | if Hi_Left < Ureal_0 then | |
4947 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
4948 | else | |
4949 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
4950 | end if; | |
4951 | ||
4952 | -- Right operand is negative | |
4953 | ||
4954 | elsif Hi_Right < Ureal_0 then | |
4955 | ||
4956 | -- If the low bound of the left operand is negative, obtain | |
4957 | -- the overall low bound by dividing it by the largest | |
4958 | -- value of the right operand, and otherwise by the smallest | |
4959 | -- value of the right operand. | |
4960 | ||
4961 | if Lo_Left < Ureal_0 then | |
4962 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
4963 | else | |
4964 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
4965 | end if; | |
4966 | ||
4967 | -- If the high bound of the left operand is negative, obtain | |
4968 | -- the overall high bound by dividing it by the smallest | |
4969 | -- value of the right operand, and otherwise by the | |
4970 | -- largest value of the right operand. | |
4971 | ||
4972 | if Hi_Left < Ureal_0 then | |
4973 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
4974 | else | |
4975 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
4976 | end if; | |
4977 | ||
4978 | else | |
4979 | OK1 := False; | |
4980 | end if; | |
4981 | end if; | |
4982 | ||
4983 | -- For type conversion from one floating-point type to another, we | |
4984 | -- can refine the range using the converted value. | |
4985 | ||
4986 | when N_Type_Conversion => | |
4987 | Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
4988 | ||
4989 | -- Nothing special to do for all other expression kinds | |
4990 | ||
4991 | when others => | |
4992 | OK1 := False; | |
4993 | Lor := No_Ureal; | |
4994 | Hir := No_Ureal; | |
4995 | end case; | |
4996 | ||
4997 | -- At this stage, if OK1 is true, then we know that the actual result of | |
4998 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4999 | -- to restrict the possible range of results. | |
5000 | ||
5001 | if OK1 then | |
5002 | ||
5003 | -- If the refined value of the low bound is greater than the type | |
5004 | -- low bound, then reset it to the more restrictive value. | |
5005 | ||
5006 | if Lor > Lo then | |
5007 | Lo := Lor; | |
5008 | end if; | |
5009 | ||
5010 | -- Similarly, if the refined value of the high bound is less than the | |
5011 | -- value so far, then reset it to the more restrictive value. | |
5012 | ||
5013 | if Hir < Hi then | |
5014 | Hi := Hir; | |
5015 | end if; | |
5016 | end if; | |
5017 | ||
5018 | -- Set cache entry for future call and we are all done | |
5019 | ||
5020 | Determine_Range_Cache_N (Cindex) := N; | |
5021 | Determine_Range_Cache_V (Cindex) := Assume_Valid; | |
5022 | Determine_Range_Cache_Lo_R (Cindex) := Lo; | |
5023 | Determine_Range_Cache_Hi_R (Cindex) := Hi; | |
5024 | return; | |
5025 | ||
5026 | -- If any exception occurs, it means that we have some bug in the compiler, | |
5027 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
5028 | -- occurrence. However, this is only an optimization attempt, so there is | |
5029 | -- really no point in crashing the compiler. Instead we just decide, too | |
5030 | -- bad, we can't figure out a range in this case after all. | |
5031 | ||
5032 | exception | |
5033 | when others => | |
5034 | ||
5035 | -- Debug flag K disables this behavior (useful for debugging) | |
5036 | ||
5037 | if Debug_Flag_K then | |
5038 | raise; | |
5039 | else | |
5040 | OK := False; | |
5041 | Lo := No_Ureal; | |
5042 | Hi := No_Ureal; | |
5043 | return; | |
5044 | end if; | |
5045 | end Determine_Range_R; | |
5046 | ||
ee6ba406 | 5047 | ------------------------------------ |
5048 | -- Discriminant_Checks_Suppressed -- | |
5049 | ------------------------------------ | |
5050 | ||
5051 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5052 | begin | |
9dfe12ae | 5053 | if Present (E) then |
5054 | if Is_Unchecked_Union (E) then | |
5055 | return True; | |
5056 | elsif Checks_May_Be_Suppressed (E) then | |
5057 | return Is_Check_Suppressed (E, Discriminant_Check); | |
5058 | end if; | |
5059 | end if; | |
5060 | ||
fafc6b97 | 5061 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 5062 | end Discriminant_Checks_Suppressed; |
5063 | ||
5064 | -------------------------------- | |
5065 | -- Division_Checks_Suppressed -- | |
5066 | -------------------------------- | |
5067 | ||
5068 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5069 | begin | |
9dfe12ae | 5070 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5071 | return Is_Check_Suppressed (E, Division_Check); | |
5072 | else | |
fafc6b97 | 5073 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 5074 | end if; |
ee6ba406 | 5075 | end Division_Checks_Suppressed; |
5076 | ||
fa771c05 | 5077 | -------------------------------------- |
5078 | -- Duplicated_Tag_Checks_Suppressed -- | |
5079 | -------------------------------------- | |
5080 | ||
5081 | function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5082 | begin | |
5083 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
5084 | return Is_Check_Suppressed (E, Duplicated_Tag_Check); | |
5085 | else | |
5086 | return Scope_Suppress.Suppress (Duplicated_Tag_Check); | |
5087 | end if; | |
5088 | end Duplicated_Tag_Checks_Suppressed; | |
5089 | ||
ee6ba406 | 5090 | ----------------------------------- |
5091 | -- Elaboration_Checks_Suppressed -- | |
5092 | ----------------------------------- | |
5093 | ||
5094 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5095 | begin | |
38f5559f | 5096 | -- The complication in this routine is that if we are in the dynamic |
5097 | -- model of elaboration, we also check All_Checks, since All_Checks | |
5098 | -- does not set Elaboration_Check explicitly. | |
5099 | ||
9dfe12ae | 5100 | if Present (E) then |
5101 | if Kill_Elaboration_Checks (E) then | |
5102 | return True; | |
38f5559f | 5103 | |
9dfe12ae | 5104 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 5105 | if Is_Check_Suppressed (E, Elaboration_Check) then |
5106 | return True; | |
5107 | elsif Dynamic_Elaboration_Checks then | |
5108 | return Is_Check_Suppressed (E, All_Checks); | |
5109 | else | |
5110 | return False; | |
5111 | end if; | |
9dfe12ae | 5112 | end if; |
5113 | end if; | |
5114 | ||
fafc6b97 | 5115 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 5116 | return True; |
5117 | elsif Dynamic_Elaboration_Checks then | |
fafc6b97 | 5118 | return Scope_Suppress.Suppress (All_Checks); |
38f5559f | 5119 | else |
5120 | return False; | |
5121 | end if; | |
ee6ba406 | 5122 | end Elaboration_Checks_Suppressed; |
5123 | ||
9dfe12ae | 5124 | --------------------------- |
5125 | -- Enable_Overflow_Check -- | |
5126 | --------------------------- | |
5127 | ||
5128 | procedure Enable_Overflow_Check (N : Node_Id) is | |
4c1c7f3f | 5129 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 5130 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 5131 | Chk : Nat; |
5132 | OK : Boolean; | |
5133 | Ent : Entity_Id; | |
5134 | Ofs : Uint; | |
5135 | Lo : Uint; | |
5136 | Hi : Uint; | |
ee6ba406 | 5137 | |
f3ccbbb3 | 5138 | Do_Ovflow_Check : Boolean; |
5139 | ||
ee6ba406 | 5140 | begin |
9dfe12ae | 5141 | if Debug_Flag_CC then |
5142 | w ("Enable_Overflow_Check for node ", Int (N)); | |
5143 | Write_Str (" Source location = "); | |
5144 | wl (Sloc (N)); | |
00c403ee | 5145 | pg (Union_Id (N)); |
ee6ba406 | 5146 | end if; |
ee6ba406 | 5147 | |
75209ec5 | 5148 | -- No check if overflow checks suppressed for type of node |
5149 | ||
0df9d43f | 5150 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 5151 | return; |
5152 | ||
49260fa5 | 5153 | -- Nothing to do for unsigned integer types, which do not overflow |
5154 | ||
5155 | elsif Is_Modular_Integer_Type (Typ) then | |
5156 | return; | |
3cce7f32 | 5157 | end if; |
5158 | ||
0df9d43f | 5159 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 5160 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
5161 | -- probably more extreme that it needs to be, but what is going on here | |
5162 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 5163 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 5164 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 5165 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 5166 | -- legacy reliable. |
3cce7f32 | 5167 | |
0df9d43f | 5168 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 5169 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
5170 | -- know that no check is needed. We skip all that in the two new modes, | |
5171 | -- since really overflow checking happens over a whole subtree, and we | |
5172 | -- do the corresponding optimizations later on when applying the checks. | |
5173 | ||
5174 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 5175 | if not (Overflow_Checks_Suppressed (Etype (N))) |
5176 | and then not (Is_Entity_Name (N) | |
5177 | and then Overflow_Checks_Suppressed (Entity (N))) | |
5178 | then | |
5179 | Activate_Overflow_Check (N); | |
5180 | end if; | |
3cce7f32 | 5181 | |
5182 | if Debug_Flag_CC then | |
5183 | w ("Minimized/Eliminated mode"); | |
5184 | end if; | |
5185 | ||
5186 | return; | |
5187 | end if; | |
5188 | ||
0df9d43f | 5189 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 5190 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 5191 | |
feff2f05 | 5192 | -- Nothing to do if the range of the result is known OK. We skip this |
5193 | -- for conversions, since the caller already did the check, and in any | |
5194 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 5195 | -- different. |
ee6ba406 | 5196 | |
3cce7f32 | 5197 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 5198 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 5199 | |
cc60bd16 | 5200 | -- Note in the test below that we assume that the range is not OK |
5201 | -- if a bound of the range is equal to that of the type. That's not | |
5202 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 5203 | |
9dfe12ae | 5204 | -- a) The way that Determine_Range works, it will typically report |
5205 | -- the bounds of the value as being equal to the bounds of the | |
5206 | -- type, because it either can't tell anything more precise, or | |
5207 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 5208 | |
9dfe12ae | 5209 | -- b) It is very unusual to have a situation in which this would |
5210 | -- generate an unnecessary overflow check (an example would be | |
5211 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 5212 | -- literal value one is added). |
ee6ba406 | 5213 | |
9dfe12ae | 5214 | -- c) The alternative is a lot of special casing in this routine |
5215 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 5216 | |
f3ccbbb3 | 5217 | if OK then |
5218 | Do_Ovflow_Check := True; | |
5219 | ||
5220 | -- Note that the following checks are quite deliberately > and < | |
5221 | -- rather than >= and <= as explained above. | |
5222 | ||
5223 | if Lo > Expr_Value (Type_Low_Bound (Typ)) | |
5224 | and then | |
5225 | Hi < Expr_Value (Type_High_Bound (Typ)) | |
5226 | then | |
5227 | Do_Ovflow_Check := False; | |
5228 | ||
5229 | -- Despite the comments above, it is worth dealing specially with | |
5230 | -- division specially. The only case where integer division can | |
5231 | -- overflow is (largest negative number) / (-1). So we will do | |
5232 | -- an extra range analysis to see if this is possible. | |
5233 | ||
5234 | elsif Nkind (N) = N_Op_Divide then | |
5235 | Determine_Range | |
5236 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5237 | ||
5238 | if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then | |
5239 | Do_Ovflow_Check := False; | |
5240 | ||
5241 | else | |
5242 | Determine_Range | |
5243 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
5244 | ||
5245 | if OK and then (Lo > Uint_Minus_1 | |
5246 | or else | |
5247 | Hi < Uint_Minus_1) | |
5248 | then | |
5249 | Do_Ovflow_Check := False; | |
5250 | end if; | |
5251 | end if; | |
9dfe12ae | 5252 | end if; |
5253 | ||
f3ccbbb3 | 5254 | -- If no overflow check required, we are done |
5255 | ||
5256 | if not Do_Ovflow_Check then | |
5257 | if Debug_Flag_CC then | |
5258 | w ("No overflow check required"); | |
5259 | end if; | |
5260 | ||
5261 | return; | |
5262 | end if; | |
9dfe12ae | 5263 | end if; |
5264 | end if; | |
5265 | ||
feff2f05 | 5266 | -- If not in optimizing mode, set flag and we are done. We are also done |
5267 | -- (and just set the flag) if the type is not a discrete type, since it | |
5268 | -- is not worth the effort to eliminate checks for other than discrete | |
5269 | -- types. In addition, we take this same path if we have stored the | |
5270 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5271 | -- but we do not want to blow up). |
9dfe12ae | 5272 | |
5273 | if Optimization_Level = 0 | |
5274 | or else not Is_Discrete_Type (Etype (N)) | |
5275 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5276 | then |
00c403ee | 5277 | Activate_Overflow_Check (N); |
9dfe12ae | 5278 | |
5279 | if Debug_Flag_CC then | |
5280 | w ("Optimization off"); | |
5281 | end if; | |
5282 | ||
ee6ba406 | 5283 | return; |
9dfe12ae | 5284 | end if; |
ee6ba406 | 5285 | |
9dfe12ae | 5286 | -- Otherwise evaluate and check the expression |
5287 | ||
5288 | Find_Check | |
5289 | (Expr => N, | |
5290 | Check_Type => 'O', | |
5291 | Target_Type => Empty, | |
5292 | Entry_OK => OK, | |
5293 | Check_Num => Chk, | |
5294 | Ent => Ent, | |
5295 | Ofs => Ofs); | |
5296 | ||
5297 | if Debug_Flag_CC then | |
5298 | w ("Called Find_Check"); | |
5299 | w (" OK = ", OK); | |
5300 | ||
5301 | if OK then | |
5302 | w (" Check_Num = ", Chk); | |
5303 | w (" Ent = ", Int (Ent)); | |
5304 | Write_Str (" Ofs = "); | |
5305 | pid (Ofs); | |
5306 | end if; | |
5307 | end if; | |
ee6ba406 | 5308 | |
9dfe12ae | 5309 | -- If check is not of form to optimize, then set flag and we are done |
5310 | ||
5311 | if not OK then | |
00c403ee | 5312 | Activate_Overflow_Check (N); |
ee6ba406 | 5313 | return; |
9dfe12ae | 5314 | end if; |
ee6ba406 | 5315 | |
9dfe12ae | 5316 | -- If check is already performed, then return without setting flag |
5317 | ||
5318 | if Chk /= 0 then | |
5319 | if Debug_Flag_CC then | |
5320 | w ("Check suppressed!"); | |
5321 | end if; | |
ee6ba406 | 5322 | |
ee6ba406 | 5323 | return; |
9dfe12ae | 5324 | end if; |
ee6ba406 | 5325 | |
9dfe12ae | 5326 | -- Here we will make a new entry for the new check |
5327 | ||
00c403ee | 5328 | Activate_Overflow_Check (N); |
9dfe12ae | 5329 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5330 | Saved_Checks (Num_Saved_Checks) := | |
5331 | (Killed => False, | |
5332 | Entity => Ent, | |
5333 | Offset => Ofs, | |
5334 | Check_Type => 'O', | |
5335 | Target_Type => Empty); | |
5336 | ||
5337 | if Debug_Flag_CC then | |
5338 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5339 | w (" Entity = ", Int (Ent)); | |
5340 | Write_Str (" Offset = "); | |
5341 | pid (Ofs); | |
5342 | w (" Check_Type = O"); | |
5343 | w (" Target_Type = Empty"); | |
5344 | end if; | |
ee6ba406 | 5345 | |
feff2f05 | 5346 | -- If we get an exception, then something went wrong, probably because of |
20cf157b | 5347 | -- an error in the structure of the tree due to an incorrect program. Or |
5348 | -- it may be a bug in the optimization circuit. In either case the safest | |
feff2f05 | 5349 | -- thing is simply to set the check flag unconditionally. |
9dfe12ae | 5350 | |
5351 | exception | |
5352 | when others => | |
00c403ee | 5353 | Activate_Overflow_Check (N); |
9dfe12ae | 5354 | |
5355 | if Debug_Flag_CC then | |
5356 | w (" exception occurred, overflow flag set"); | |
5357 | end if; | |
5358 | ||
5359 | return; | |
5360 | end Enable_Overflow_Check; | |
5361 | ||
5362 | ------------------------ | |
5363 | -- Enable_Range_Check -- | |
5364 | ------------------------ | |
5365 | ||
5366 | procedure Enable_Range_Check (N : Node_Id) is | |
5367 | Chk : Nat; | |
5368 | OK : Boolean; | |
5369 | Ent : Entity_Id; | |
5370 | Ofs : Uint; | |
5371 | Ttyp : Entity_Id; | |
5372 | P : Node_Id; | |
5373 | ||
5374 | begin | |
feff2f05 | 5375 | -- Return if unchecked type conversion with range check killed. In this |
39a0c1d3 | 5376 | -- case we never set the flag (that's what Kill_Range_Check is about). |
9dfe12ae | 5377 | |
5378 | if Nkind (N) = N_Unchecked_Type_Conversion | |
5379 | and then Kill_Range_Check (N) | |
ee6ba406 | 5380 | then |
5381 | return; | |
9dfe12ae | 5382 | end if; |
ee6ba406 | 5383 | |
55e8372b | 5384 | -- Do not set range check flag if parent is assignment statement or |
5385 | -- object declaration with Suppress_Assignment_Checks flag set | |
5386 | ||
5387 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
5388 | and then Suppress_Assignment_Checks (Parent (N)) | |
5389 | then | |
5390 | return; | |
5391 | end if; | |
5392 | ||
0577b0b1 | 5393 | -- Check for various cases where we should suppress the range check |
5394 | ||
5395 | -- No check if range checks suppressed for type of node | |
5396 | ||
20cf157b | 5397 | if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
0577b0b1 | 5398 | return; |
5399 | ||
5400 | -- No check if node is an entity name, and range checks are suppressed | |
5401 | -- for this entity, or for the type of this entity. | |
5402 | ||
5403 | elsif Is_Entity_Name (N) | |
5404 | and then (Range_Checks_Suppressed (Entity (N)) | |
20cf157b | 5405 | or else Range_Checks_Suppressed (Etype (Entity (N)))) |
0577b0b1 | 5406 | then |
5407 | return; | |
5408 | ||
5409 | -- No checks if index of array, and index checks are suppressed for | |
5410 | -- the array object or the type of the array. | |
5411 | ||
5412 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
5413 | declare | |
5414 | Pref : constant Node_Id := Prefix (Parent (N)); | |
5415 | begin | |
5416 | if Is_Entity_Name (Pref) | |
5417 | and then Index_Checks_Suppressed (Entity (Pref)) | |
5418 | then | |
5419 | return; | |
5420 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
5421 | return; | |
5422 | end if; | |
5423 | end; | |
5424 | end if; | |
5425 | ||
9dfe12ae | 5426 | -- Debug trace output |
ee6ba406 | 5427 | |
9dfe12ae | 5428 | if Debug_Flag_CC then |
5429 | w ("Enable_Range_Check for node ", Int (N)); | |
5430 | Write_Str (" Source location = "); | |
5431 | wl (Sloc (N)); | |
00c403ee | 5432 | pg (Union_Id (N)); |
9dfe12ae | 5433 | end if; |
5434 | ||
feff2f05 | 5435 | -- If not in optimizing mode, set flag and we are done. We are also done |
5436 | -- (and just set the flag) if the type is not a discrete type, since it | |
5437 | -- is not worth the effort to eliminate checks for other than discrete | |
5438 | -- types. In addition, we take this same path if we have stored the | |
5439 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 5440 | -- but we do not want to blow up). |
9dfe12ae | 5441 | |
5442 | if Optimization_Level = 0 | |
5443 | or else No (Etype (N)) | |
5444 | or else not Is_Discrete_Type (Etype (N)) | |
5445 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 5446 | then |
00c403ee | 5447 | Activate_Range_Check (N); |
9dfe12ae | 5448 | |
5449 | if Debug_Flag_CC then | |
5450 | w ("Optimization off"); | |
5451 | end if; | |
5452 | ||
ee6ba406 | 5453 | return; |
9dfe12ae | 5454 | end if; |
ee6ba406 | 5455 | |
9dfe12ae | 5456 | -- Otherwise find out the target type |
ee6ba406 | 5457 | |
9dfe12ae | 5458 | P := Parent (N); |
ee6ba406 | 5459 | |
9dfe12ae | 5460 | -- For assignment, use left side subtype |
5461 | ||
5462 | if Nkind (P) = N_Assignment_Statement | |
5463 | and then Expression (P) = N | |
5464 | then | |
5465 | Ttyp := Etype (Name (P)); | |
5466 | ||
5467 | -- For indexed component, use subscript subtype | |
5468 | ||
5469 | elsif Nkind (P) = N_Indexed_Component then | |
5470 | declare | |
5471 | Atyp : Entity_Id; | |
5472 | Indx : Node_Id; | |
5473 | Subs : Node_Id; | |
5474 | ||
5475 | begin | |
5476 | Atyp := Etype (Prefix (P)); | |
5477 | ||
5478 | if Is_Access_Type (Atyp) then | |
5479 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 5480 | |
5481 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 5482 | -- perform check unconditionally: it depends on the bounds of |
5483 | -- an object and we cannot currently recognize whether the test | |
5484 | -- may be redundant. | |
f07ea091 | 5485 | |
5486 | if not Is_Constrained (Atyp) then | |
00c403ee | 5487 | Activate_Range_Check (N); |
f07ea091 | 5488 | return; |
5489 | end if; | |
7189d17f | 5490 | |
feff2f05 | 5491 | -- Ditto if the prefix is an explicit dereference whose designated |
5492 | -- type is unconstrained. | |
7189d17f | 5493 | |
5494 | elsif Nkind (Prefix (P)) = N_Explicit_Dereference | |
5495 | and then not Is_Constrained (Atyp) | |
5496 | then | |
00c403ee | 5497 | Activate_Range_Check (N); |
7189d17f | 5498 | return; |
9dfe12ae | 5499 | end if; |
5500 | ||
5501 | Indx := First_Index (Atyp); | |
5502 | Subs := First (Expressions (P)); | |
5503 | loop | |
5504 | if Subs = N then | |
5505 | Ttyp := Etype (Indx); | |
5506 | exit; | |
5507 | end if; | |
5508 | ||
5509 | Next_Index (Indx); | |
5510 | Next (Subs); | |
5511 | end loop; | |
5512 | end; | |
5513 | ||
5514 | -- For now, ignore all other cases, they are not so interesting | |
5515 | ||
5516 | else | |
5517 | if Debug_Flag_CC then | |
5518 | w (" target type not found, flag set"); | |
5519 | end if; | |
5520 | ||
00c403ee | 5521 | Activate_Range_Check (N); |
9dfe12ae | 5522 | return; |
5523 | end if; | |
5524 | ||
5525 | -- Evaluate and check the expression | |
5526 | ||
5527 | Find_Check | |
5528 | (Expr => N, | |
5529 | Check_Type => 'R', | |
5530 | Target_Type => Ttyp, | |
5531 | Entry_OK => OK, | |
5532 | Check_Num => Chk, | |
5533 | Ent => Ent, | |
5534 | Ofs => Ofs); | |
5535 | ||
5536 | if Debug_Flag_CC then | |
5537 | w ("Called Find_Check"); | |
5538 | w ("Target_Typ = ", Int (Ttyp)); | |
5539 | w (" OK = ", OK); | |
5540 | ||
5541 | if OK then | |
5542 | w (" Check_Num = ", Chk); | |
5543 | w (" Ent = ", Int (Ent)); | |
5544 | Write_Str (" Ofs = "); | |
5545 | pid (Ofs); | |
5546 | end if; | |
5547 | end if; | |
5548 | ||
5549 | -- If check is not of form to optimize, then set flag and we are done | |
5550 | ||
5551 | if not OK then | |
5552 | if Debug_Flag_CC then | |
5553 | w (" expression not of optimizable type, flag set"); | |
5554 | end if; | |
5555 | ||
00c403ee | 5556 | Activate_Range_Check (N); |
9dfe12ae | 5557 | return; |
5558 | end if; | |
5559 | ||
5560 | -- If check is already performed, then return without setting flag | |
5561 | ||
5562 | if Chk /= 0 then | |
5563 | if Debug_Flag_CC then | |
5564 | w ("Check suppressed!"); | |
5565 | end if; | |
5566 | ||
5567 | return; | |
5568 | end if; | |
5569 | ||
5570 | -- Here we will make a new entry for the new check | |
5571 | ||
00c403ee | 5572 | Activate_Range_Check (N); |
9dfe12ae | 5573 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5574 | Saved_Checks (Num_Saved_Checks) := | |
5575 | (Killed => False, | |
5576 | Entity => Ent, | |
5577 | Offset => Ofs, | |
5578 | Check_Type => 'R', | |
5579 | Target_Type => Ttyp); | |
5580 | ||
5581 | if Debug_Flag_CC then | |
5582 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5583 | w (" Entity = ", Int (Ent)); | |
5584 | Write_Str (" Offset = "); | |
5585 | pid (Ofs); | |
5586 | w (" Check_Type = R"); | |
5587 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 5588 | pg (Union_Id (Ttyp)); |
9dfe12ae | 5589 | end if; |
5590 | ||
feff2f05 | 5591 | -- If we get an exception, then something went wrong, probably because of |
5592 | -- an error in the structure of the tree due to an incorrect program. Or | |
5593 | -- it may be a bug in the optimization circuit. In either case the safest | |
5594 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 5595 | |
5596 | exception | |
5597 | when others => | |
00c403ee | 5598 | Activate_Range_Check (N); |
9dfe12ae | 5599 | |
5600 | if Debug_Flag_CC then | |
5601 | w (" exception occurred, range flag set"); | |
5602 | end if; | |
5603 | ||
5604 | return; | |
5605 | end Enable_Range_Check; | |
5606 | ||
5607 | ------------------ | |
5608 | -- Ensure_Valid -- | |
5609 | ------------------ | |
5610 | ||
5611 | procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is | |
5612 | Typ : constant Entity_Id := Etype (Expr); | |
5613 | ||
5614 | begin | |
5615 | -- Ignore call if we are not doing any validity checking | |
5616 | ||
5617 | if not Validity_Checks_On then | |
5618 | return; | |
5619 | ||
0577b0b1 | 5620 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 5621 | |
0577b0b1 | 5622 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 5623 | return; |
5624 | ||
feff2f05 | 5625 | -- No check required if expression is from the expander, we assume the |
5626 | -- expander will generate whatever checks are needed. Note that this is | |
39a0c1d3 | 5627 | -- not just an optimization, it avoids infinite recursions. |
9dfe12ae | 5628 | |
5629 | -- Unchecked conversions must be checked, unless they are initialized | |
5630 | -- scalar values, as in a component assignment in an init proc. | |
5631 | ||
5632 | -- In addition, we force a check if Force_Validity_Checks is set | |
5633 | ||
5634 | elsif not Comes_From_Source (Expr) | |
5635 | and then not Force_Validity_Checks | |
5636 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
5637 | or else Kill_Range_Check (Expr)) | |
5638 | then | |
5639 | return; | |
5640 | ||
5641 | -- No check required if expression is known to have valid value | |
5642 | ||
5643 | elsif Expr_Known_Valid (Expr) then | |
5644 | return; | |
5645 | ||
feff2f05 | 5646 | -- Ignore case of enumeration with holes where the flag is set not to |
5647 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 5648 | |
5649 | elsif Is_Enumeration_Type (Typ) | |
5650 | and then Has_Non_Standard_Rep (Typ) | |
5651 | and then Holes_OK | |
5652 | then | |
5653 | return; | |
5654 | ||
f2a06be9 | 5655 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 5656 | |
5657 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
5658 | and then Expr = Name (Parent (Expr)) | |
5659 | then | |
5660 | return; | |
5661 | ||
6fb3c314 | 5662 | -- No check on a universal real constant. The context will eventually |
38f5559f | 5663 | -- convert it to a machine number for some target type, or report an |
5664 | -- illegality. | |
5665 | ||
5666 | elsif Nkind (Expr) = N_Real_Literal | |
5667 | and then Etype (Expr) = Universal_Real | |
5668 | then | |
5669 | return; | |
5670 | ||
6fb3c314 | 5671 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 5672 | -- no possible check applies. We ignore the old ACATS chestnuts that |
5673 | -- involve Boolean range True..True. | |
5674 | ||
5675 | -- Note: validity checks are generated for expressions that yield a | |
5676 | -- scalar type, when it is possible to create a value that is outside of | |
5677 | -- the type. If this is a one-bit boolean no such value exists. This is | |
5678 | -- an optimization, and it also prevents compiler blowing up during the | |
5679 | -- elaboration of improperly expanded packed array references. | |
5680 | ||
5681 | elsif Nkind (Expr) = N_Indexed_Component | |
5682 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
5683 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
5684 | then | |
5685 | return; | |
5686 | ||
737e8460 | 5687 | -- For an expression with actions, we want to insert the validity check |
5688 | -- on the final Expression. | |
5689 | ||
5690 | elsif Nkind (Expr) = N_Expression_With_Actions then | |
5691 | Ensure_Valid (Expression (Expr)); | |
5692 | return; | |
5693 | ||
9dfe12ae | 5694 | -- An annoying special case. If this is an out parameter of a scalar |
5695 | -- type, then the value is not going to be accessed, therefore it is | |
5696 | -- inappropriate to do any validity check at the call site. | |
5697 | ||
5698 | else | |
5699 | -- Only need to worry about scalar types | |
5700 | ||
5701 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 5702 | declare |
5703 | P : Node_Id; | |
5704 | N : Node_Id; | |
5705 | E : Entity_Id; | |
5706 | F : Entity_Id; | |
5707 | A : Node_Id; | |
5708 | L : List_Id; | |
5709 | ||
5710 | begin | |
5711 | -- Find actual argument (which may be a parameter association) | |
5712 | -- and the parent of the actual argument (the call statement) | |
5713 | ||
5714 | N := Expr; | |
5715 | P := Parent (Expr); | |
5716 | ||
5717 | if Nkind (P) = N_Parameter_Association then | |
5718 | N := P; | |
5719 | P := Parent (N); | |
5720 | end if; | |
5721 | ||
feff2f05 | 5722 | -- Only need to worry if we are argument of a procedure call |
5723 | -- since functions don't have out parameters. If this is an | |
5724 | -- indirect or dispatching call, get signature from the | |
5725 | -- subprogram type. | |
ee6ba406 | 5726 | |
5727 | if Nkind (P) = N_Procedure_Call_Statement then | |
5728 | L := Parameter_Associations (P); | |
9dfe12ae | 5729 | |
5730 | if Is_Entity_Name (Name (P)) then | |
5731 | E := Entity (Name (P)); | |
5732 | else | |
5733 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
5734 | E := Etype (Name (P)); | |
5735 | end if; | |
ee6ba406 | 5736 | |
feff2f05 | 5737 | -- Only need to worry if there are indeed actuals, and if |
5738 | -- this could be a procedure call, otherwise we cannot get a | |
5739 | -- match (either we are not an argument, or the mode of the | |
5740 | -- formal is not OUT). This test also filters out the | |
5741 | -- generic case. | |
ee6ba406 | 5742 | |
20cf157b | 5743 | if Is_Non_Empty_List (L) and then Is_Subprogram (E) then |
5744 | ||
feff2f05 | 5745 | -- This is the loop through parameters, looking for an |
5746 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 5747 | |
5748 | F := First_Formal (E); | |
5749 | A := First (L); | |
ee6ba406 | 5750 | while Present (F) loop |
5751 | if Ekind (F) = E_Out_Parameter and then A = N then | |
5752 | return; | |
5753 | end if; | |
5754 | ||
5755 | Next_Formal (F); | |
5756 | Next (A); | |
5757 | end loop; | |
5758 | end if; | |
5759 | end if; | |
5760 | end; | |
5761 | end if; | |
5762 | end if; | |
5763 | ||
fa6a6949 | 5764 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 5765 | -- checking: if they are valid, a boolean or short-circuit operation |
5766 | -- with them will be valid as well. | |
784d4230 | 5767 | |
5768 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 5769 | and then |
fa6a6949 | 5770 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 5771 | then |
5772 | return; | |
5773 | end if; | |
5774 | ||
0577b0b1 | 5775 | -- If we fall through, a validity check is required |
ee6ba406 | 5776 | |
5777 | Insert_Valid_Check (Expr); | |
ce7498d3 | 5778 | |
5779 | if Is_Entity_Name (Expr) | |
5780 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
5781 | then | |
5782 | Set_Is_Known_Valid (Entity (Expr)); | |
5783 | end if; | |
ee6ba406 | 5784 | end Ensure_Valid; |
5785 | ||
5786 | ---------------------- | |
5787 | -- Expr_Known_Valid -- | |
5788 | ---------------------- | |
5789 | ||
5790 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
5791 | Typ : constant Entity_Id := Etype (Expr); | |
5792 | ||
5793 | begin | |
feff2f05 | 5794 | -- Non-scalar types are always considered valid, since they never give |
5795 | -- rise to the issues of erroneous or bounded error behavior that are | |
5796 | -- the concern. In formal reference manual terms the notion of validity | |
5797 | -- only applies to scalar types. Note that even when packed arrays are | |
5798 | -- represented using modular types, they are still arrays semantically, | |
5799 | -- so they are also always valid (in particular, the unused bits can be | |
5800 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 5801 | |
a88a5773 | 5802 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then |
ee6ba406 | 5803 | return True; |
5804 | ||
5805 | -- If no validity checking, then everything is considered valid | |
5806 | ||
5807 | elsif not Validity_Checks_On then | |
5808 | return True; | |
5809 | ||
5810 | -- Floating-point types are considered valid unless floating-point | |
5811 | -- validity checks have been specifically turned on. | |
5812 | ||
5813 | elsif Is_Floating_Point_Type (Typ) | |
5814 | and then not Validity_Check_Floating_Point | |
5815 | then | |
5816 | return True; | |
5817 | ||
feff2f05 | 5818 | -- If the expression is the value of an object that is known to be |
5819 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 5820 | |
5821 | elsif Is_Entity_Name (Expr) | |
5822 | and then Is_Known_Valid (Entity (Expr)) | |
985fe5d6 | 5823 | |
5824 | -- Exclude volatile variables | |
5825 | ||
5826 | and then not Treat_As_Volatile (Entity (Expr)) | |
ee6ba406 | 5827 | then |
5828 | return True; | |
5829 | ||
0577b0b1 | 5830 | -- References to discriminants are always considered valid. The value |
5831 | -- of a discriminant gets checked when the object is built. Within the | |
5832 | -- record, we consider it valid, and it is important to do so, since | |
5833 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 5834 | -- reference discriminants out of scope. Discriminants of concurrent |
5835 | -- types are excluded for the same reason. | |
0577b0b1 | 5836 | |
5837 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 5838 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 5839 | then |
5840 | return True; | |
5841 | ||
feff2f05 | 5842 | -- If the type is one for which all values are known valid, then we are |
5843 | -- sure that the value is valid except in the slightly odd case where | |
5844 | -- the expression is a reference to a variable whose size has been | |
5845 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 5846 | |
5847 | elsif Is_Known_Valid (Typ) then | |
5848 | if Is_Entity_Name (Expr) | |
5849 | and then Ekind (Entity (Expr)) = E_Variable | |
5850 | and then Esize (Entity (Expr)) > Esize (Typ) | |
5851 | then | |
5852 | return False; | |
5853 | else | |
5854 | return True; | |
5855 | end if; | |
5856 | ||
5857 | -- Integer and character literals always have valid values, where | |
5858 | -- appropriate these will be range checked in any case. | |
5859 | ||
20cf157b | 5860 | elsif Nkind_In (Expr, N_Integer_Literal, N_Character_Literal) then |
ee6ba406 | 5861 | return True; |
5862 | ||
91e47010 | 5863 | -- Real literals are assumed to be valid in VM targets |
5864 | ||
20cf157b | 5865 | elsif VM_Target /= No_VM and then Nkind (Expr) = N_Real_Literal then |
91e47010 | 5866 | return True; |
5867 | ||
ee6ba406 | 5868 | -- If we have a type conversion or a qualification of a known valid |
5869 | -- value, then the result will always be valid. | |
5870 | ||
20cf157b | 5871 | elsif Nkind_In (Expr, N_Type_Conversion, N_Qualified_Expression) then |
ee6ba406 | 5872 | return Expr_Known_Valid (Expression (Expr)); |
5873 | ||
1eb1395f | 5874 | -- Case of expression is a non-floating-point operator. In this case we |
5875 | -- can assume the result is valid the generated code for the operator | |
5876 | -- will include whatever checks are needed (e.g. range checks) to ensure | |
5877 | -- validity. This assumption does not hold for the floating-point case, | |
5878 | -- since floating-point operators can generate Infinite or NaN results | |
5879 | -- which are considered invalid. | |
5880 | ||
5881 | -- Historical note: in older versions, the exemption of floating-point | |
5882 | -- types from this assumption was done only in cases where the parent | |
5883 | -- was an assignment, function call or parameter association. Presumably | |
5884 | -- the idea was that in other contexts, the result would be checked | |
5885 | -- elsewhere, but this list of cases was missing tests (at least the | |
5886 | -- N_Object_Declaration case, as shown by a reported missing validity | |
5887 | -- check), and it is not clear why function calls but not procedure | |
5888 | -- calls were tested for. It really seems more accurate and much | |
5889 | -- safer to recognize that expressions which are the result of a | |
5890 | -- floating-point operator can never be assumed to be valid. | |
5891 | ||
5892 | elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then | |
5893 | return True; | |
1d90d657 | 5894 | |
feff2f05 | 5895 | -- The result of a membership test is always valid, since it is true or |
5896 | -- false, there are no other possibilities. | |
0577b0b1 | 5897 | |
5898 | elsif Nkind (Expr) in N_Membership_Test then | |
5899 | return True; | |
5900 | ||
ee6ba406 | 5901 | -- For all other cases, we do not know the expression is valid |
5902 | ||
5903 | else | |
5904 | return False; | |
5905 | end if; | |
5906 | end Expr_Known_Valid; | |
5907 | ||
9dfe12ae | 5908 | ---------------- |
5909 | -- Find_Check -- | |
5910 | ---------------- | |
5911 | ||
5912 | procedure Find_Check | |
5913 | (Expr : Node_Id; | |
5914 | Check_Type : Character; | |
5915 | Target_Type : Entity_Id; | |
5916 | Entry_OK : out Boolean; | |
5917 | Check_Num : out Nat; | |
5918 | Ent : out Entity_Id; | |
5919 | Ofs : out Uint) | |
5920 | is | |
5921 | function Within_Range_Of | |
5922 | (Target_Type : Entity_Id; | |
314a23b6 | 5923 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 5924 | -- Given a requirement for checking a range against Target_Type, and |
5925 | -- and a range Check_Type against which a check has already been made, | |
5926 | -- determines if the check against check type is sufficient to ensure | |
5927 | -- that no check against Target_Type is required. | |
5928 | ||
5929 | --------------------- | |
5930 | -- Within_Range_Of -- | |
5931 | --------------------- | |
5932 | ||
5933 | function Within_Range_Of | |
5934 | (Target_Type : Entity_Id; | |
314a23b6 | 5935 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 5936 | is |
5937 | begin | |
5938 | if Target_Type = Check_Type then | |
5939 | return True; | |
5940 | ||
5941 | else | |
5942 | declare | |
5943 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
5944 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
5945 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
5946 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
5947 | ||
5948 | begin | |
5949 | if (Tlo = Clo | |
5950 | or else (Compile_Time_Known_Value (Tlo) | |
5951 | and then | |
5952 | Compile_Time_Known_Value (Clo) | |
5953 | and then | |
5954 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
5955 | and then | |
5956 | (Thi = Chi | |
5957 | or else (Compile_Time_Known_Value (Thi) | |
5958 | and then | |
5959 | Compile_Time_Known_Value (Chi) | |
5960 | and then | |
5961 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
5962 | then | |
5963 | return True; | |
5964 | else | |
5965 | return False; | |
5966 | end if; | |
5967 | end; | |
5968 | end if; | |
5969 | end Within_Range_Of; | |
5970 | ||
5971 | -- Start of processing for Find_Check | |
5972 | ||
5973 | begin | |
ed195555 | 5974 | -- Establish default, in case no entry is found |
9dfe12ae | 5975 | |
5976 | Check_Num := 0; | |
5977 | ||
5978 | -- Case of expression is simple entity reference | |
5979 | ||
5980 | if Is_Entity_Name (Expr) then | |
5981 | Ent := Entity (Expr); | |
5982 | Ofs := Uint_0; | |
5983 | ||
5984 | -- Case of expression is entity + known constant | |
5985 | ||
5986 | elsif Nkind (Expr) = N_Op_Add | |
5987 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5988 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5989 | then | |
5990 | Ent := Entity (Left_Opnd (Expr)); | |
5991 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
5992 | ||
5993 | -- Case of expression is entity - known constant | |
5994 | ||
5995 | elsif Nkind (Expr) = N_Op_Subtract | |
5996 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5997 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5998 | then | |
5999 | Ent := Entity (Left_Opnd (Expr)); | |
6000 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
6001 | ||
6002 | -- Any other expression is not of the right form | |
6003 | ||
6004 | else | |
6005 | Ent := Empty; | |
6006 | Ofs := Uint_0; | |
6007 | Entry_OK := False; | |
6008 | return; | |
6009 | end if; | |
6010 | ||
feff2f05 | 6011 | -- Come here with expression of appropriate form, check if entity is an |
6012 | -- appropriate one for our purposes. | |
9dfe12ae | 6013 | |
6014 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 6015 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 6016 | and then not Is_Library_Level_Entity (Ent) |
6017 | then | |
6018 | Entry_OK := True; | |
6019 | else | |
6020 | Entry_OK := False; | |
6021 | return; | |
6022 | end if; | |
6023 | ||
6024 | -- See if there is matching check already | |
6025 | ||
6026 | for J in reverse 1 .. Num_Saved_Checks loop | |
6027 | declare | |
6028 | SC : Saved_Check renames Saved_Checks (J); | |
9dfe12ae | 6029 | begin |
6030 | if SC.Killed = False | |
6031 | and then SC.Entity = Ent | |
6032 | and then SC.Offset = Ofs | |
6033 | and then SC.Check_Type = Check_Type | |
6034 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
6035 | then | |
6036 | Check_Num := J; | |
6037 | return; | |
6038 | end if; | |
6039 | end; | |
6040 | end loop; | |
6041 | ||
6042 | -- If we fall through entry was not found | |
6043 | ||
9dfe12ae | 6044 | return; |
6045 | end Find_Check; | |
6046 | ||
6047 | --------------------------------- | |
6048 | -- Generate_Discriminant_Check -- | |
6049 | --------------------------------- | |
6050 | ||
6051 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 6052 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 6053 | |
6054 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
6055 | Loc : constant Source_Ptr := Sloc (N); | |
6056 | Pref : constant Node_Id := Prefix (N); | |
6057 | Sel : constant Node_Id := Selector_Name (N); | |
6058 | ||
6059 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 6060 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 6061 | -- The original component to be checked |
6062 | ||
6063 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 6064 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 6065 | -- The discriminant checking function |
6066 | ||
6067 | Discr : Entity_Id; | |
6068 | -- One discriminant to be checked in the type | |
6069 | ||
6070 | Real_Discr : Entity_Id; | |
6071 | -- Actual discriminant in the call | |
6072 | ||
6073 | Pref_Type : Entity_Id; | |
6074 | -- Type of relevant prefix (ignoring private/access stuff) | |
6075 | ||
6076 | Args : List_Id; | |
6077 | -- List of arguments for function call | |
6078 | ||
6079 | Formal : Entity_Id; | |
feff2f05 | 6080 | -- Keep track of the formal corresponding to the actual we build for |
6081 | -- each discriminant, in order to be able to perform the necessary type | |
6082 | -- conversions. | |
9dfe12ae | 6083 | |
6084 | Scomp : Node_Id; | |
6085 | -- Selected component reference for checking function argument | |
6086 | ||
6087 | begin | |
6088 | Pref_Type := Etype (Pref); | |
6089 | ||
6090 | -- Force evaluation of the prefix, so that it does not get evaluated | |
6091 | -- twice (once for the check, once for the actual reference). Such a | |
20cf157b | 6092 | -- double evaluation is always a potential source of inefficiency, and |
6093 | -- is functionally incorrect in the volatile case, or when the prefix | |
6094 | -- may have side-effects. A non-volatile entity or a component of a | |
6095 | -- non-volatile entity requires no evaluation. | |
9dfe12ae | 6096 | |
6097 | if Is_Entity_Name (Pref) then | |
6098 | if Treat_As_Volatile (Entity (Pref)) then | |
6099 | Force_Evaluation (Pref, Name_Req => True); | |
6100 | end if; | |
6101 | ||
6102 | elsif Treat_As_Volatile (Etype (Pref)) then | |
20cf157b | 6103 | Force_Evaluation (Pref, Name_Req => True); |
9dfe12ae | 6104 | |
6105 | elsif Nkind (Pref) = N_Selected_Component | |
6106 | and then Is_Entity_Name (Prefix (Pref)) | |
6107 | then | |
6108 | null; | |
6109 | ||
6110 | else | |
6111 | Force_Evaluation (Pref, Name_Req => True); | |
6112 | end if; | |
6113 | ||
6114 | -- For a tagged type, use the scope of the original component to | |
6115 | -- obtain the type, because ??? | |
6116 | ||
6117 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
6118 | Pref_Type := Scope (Orig_Comp); | |
6119 | ||
feff2f05 | 6120 | -- For an untagged derived type, use the discriminants of the parent |
6121 | -- which have been renamed in the derivation, possibly by a one-to-many | |
d1a2e31b | 6122 | -- discriminant constraint. For untagged type, initially get the Etype |
feff2f05 | 6123 | -- of the prefix |
9dfe12ae | 6124 | |
6125 | else | |
6126 | if Is_Derived_Type (Pref_Type) | |
6127 | and then Number_Discriminants (Pref_Type) /= | |
6128 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
6129 | then | |
6130 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
6131 | end if; | |
6132 | end if; | |
6133 | ||
6134 | -- We definitely should have a checking function, This routine should | |
6135 | -- not be called if no discriminant checking function is present. | |
6136 | ||
6137 | pragma Assert (Present (Discr_Fct)); | |
6138 | ||
6139 | -- Create the list of the actual parameters for the call. This list | |
6140 | -- is the list of the discriminant fields of the record expression to | |
6141 | -- be discriminant checked. | |
6142 | ||
6143 | Args := New_List; | |
6144 | Formal := First_Formal (Discr_Fct); | |
6145 | Discr := First_Discriminant (Pref_Type); | |
6146 | while Present (Discr) loop | |
6147 | ||
6148 | -- If we have a corresponding discriminant field, and a parent | |
6149 | -- subtype is present, then we want to use the corresponding | |
6150 | -- discriminant since this is the one with the useful value. | |
6151 | ||
6152 | if Present (Corresponding_Discriminant (Discr)) | |
6153 | and then Ekind (Pref_Type) = E_Record_Type | |
6154 | and then Present (Parent_Subtype (Pref_Type)) | |
6155 | then | |
6156 | Real_Discr := Corresponding_Discriminant (Discr); | |
6157 | else | |
6158 | Real_Discr := Discr; | |
6159 | end if; | |
6160 | ||
6161 | -- Construct the reference to the discriminant | |
6162 | ||
6163 | Scomp := | |
6164 | Make_Selected_Component (Loc, | |
6165 | Prefix => | |
6166 | Unchecked_Convert_To (Pref_Type, | |
6167 | Duplicate_Subexpr (Pref)), | |
6168 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
6169 | ||
6170 | -- Manually analyze and resolve this selected component. We really | |
6171 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 6172 | -- playing discriminal games etc with this reference. Then we append |
6173 | -- the argument to the list we are gathering. | |
9dfe12ae | 6174 | |
6175 | Set_Etype (Scomp, Etype (Real_Discr)); | |
6176 | Set_Analyzed (Scomp, True); | |
6177 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
6178 | ||
6179 | Next_Formal_With_Extras (Formal); | |
6180 | Next_Discriminant (Discr); | |
6181 | end loop; | |
6182 | ||
6183 | -- Now build and insert the call | |
6184 | ||
6185 | Insert_Action (N, | |
6186 | Make_Raise_Constraint_Error (Loc, | |
6187 | Condition => | |
6188 | Make_Function_Call (Loc, | |
20cf157b | 6189 | Name => New_Occurrence_Of (Discr_Fct, Loc), |
9dfe12ae | 6190 | Parameter_Associations => Args), |
6191 | Reason => CE_Discriminant_Check_Failed)); | |
6192 | end Generate_Discriminant_Check; | |
6193 | ||
5c99c290 | 6194 | --------------------------- |
6195 | -- Generate_Index_Checks -- | |
6196 | --------------------------- | |
9dfe12ae | 6197 | |
6198 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 6199 | |
6200 | function Entity_Of_Prefix return Entity_Id; | |
6201 | -- Returns the entity of the prefix of N (or Empty if not found) | |
6202 | ||
3f42e2a7 | 6203 | ---------------------- |
6204 | -- Entity_Of_Prefix -- | |
6205 | ---------------------- | |
6206 | ||
05f3e139 | 6207 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 6208 | P : Node_Id; |
6209 | ||
05f3e139 | 6210 | begin |
e5d38095 | 6211 | P := Prefix (N); |
05f3e139 | 6212 | while not Is_Entity_Name (P) loop |
6213 | if not Nkind_In (P, N_Selected_Component, | |
6214 | N_Indexed_Component) | |
6215 | then | |
6216 | return Empty; | |
6217 | end if; | |
6218 | ||
6219 | P := Prefix (P); | |
6220 | end loop; | |
6221 | ||
6222 | return Entity (P); | |
6223 | end Entity_Of_Prefix; | |
6224 | ||
6225 | -- Local variables | |
6226 | ||
6227 | Loc : constant Source_Ptr := Sloc (N); | |
6228 | A : constant Node_Id := Prefix (N); | |
6229 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
6230 | Sub : Node_Id; | |
9dfe12ae | 6231 | |
3f42e2a7 | 6232 | -- Start of processing for Generate_Index_Checks |
6233 | ||
9dfe12ae | 6234 | begin |
05f3e139 | 6235 | -- Ignore call if the prefix is not an array since we have a serious |
6236 | -- error in the sources. Ignore it also if index checks are suppressed | |
6237 | -- for array object or type. | |
0577b0b1 | 6238 | |
05f3e139 | 6239 | if not Is_Array_Type (Etype (A)) |
20cf157b | 6240 | or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 6241 | or else Index_Checks_Suppressed (Etype (A)) |
6242 | then | |
6243 | return; | |
df9fba45 | 6244 | |
6245 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
6246 | -- prefix. This case arises when analysis has determined that constructs | |
6247 | -- such as | |
6248 | ||
6249 | -- Prefix'Loop_Entry (Expr) | |
6250 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
6251 | ||
6252 | -- require rewriting for error detection purposes. A side effect of this | |
6253 | -- action is the generation of index checks that mention 'Loop_Entry. | |
6254 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
6255 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
6256 | ||
6257 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
6258 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
6259 | then | |
6260 | return; | |
0577b0b1 | 6261 | end if; |
6262 | ||
05f3e139 | 6263 | -- Generate a raise of constraint error with the appropriate reason and |
6264 | -- a condition of the form: | |
6265 | ||
3f42e2a7 | 6266 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 6267 | |
6268 | -- Note that the reason we generate the conversion to the base type here | |
6269 | -- is that we definitely want the range check to take place, even if it | |
6270 | -- looks like the subtype is OK. Optimization considerations that allow | |
6271 | -- us to omit the check have already been taken into account in the | |
6272 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 6273 | |
9dfe12ae | 6274 | Sub := First (Expressions (N)); |
05f3e139 | 6275 | |
6276 | -- Handle string literals | |
6277 | ||
6278 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 6279 | if Do_Range_Check (Sub) then |
6280 | Set_Do_Range_Check (Sub, False); | |
6281 | ||
05f3e139 | 6282 | -- For string literals we obtain the bounds of the string from the |
6283 | -- associated subtype. | |
9dfe12ae | 6284 | |
05f3e139 | 6285 | Insert_Action (N, |
094ed68e | 6286 | Make_Raise_Constraint_Error (Loc, |
6287 | Condition => | |
6288 | Make_Not_In (Loc, | |
6289 | Left_Opnd => | |
6290 | Convert_To (Base_Type (Etype (Sub)), | |
6291 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6292 | Right_Opnd => | |
6293 | Make_Attribute_Reference (Loc, | |
83c6c069 | 6294 | Prefix => New_Occurrence_Of (Etype (A), Loc), |
094ed68e | 6295 | Attribute_Name => Name_Range)), |
6296 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6297 | end if; |
9dfe12ae | 6298 | |
05f3e139 | 6299 | -- General case |
9dfe12ae | 6300 | |
05f3e139 | 6301 | else |
6302 | declare | |
6303 | A_Idx : Node_Id := Empty; | |
6304 | A_Range : Node_Id; | |
6305 | Ind : Nat; | |
6306 | Num : List_Id; | |
6307 | Range_N : Node_Id; | |
9dfe12ae | 6308 | |
05f3e139 | 6309 | begin |
6310 | A_Idx := First_Index (Etype (A)); | |
6311 | Ind := 1; | |
6312 | while Present (Sub) loop | |
6313 | if Do_Range_Check (Sub) then | |
6314 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 6315 | |
05f3e139 | 6316 | -- Force evaluation except for the case of a simple name of |
6317 | -- a non-volatile entity. | |
9dfe12ae | 6318 | |
05f3e139 | 6319 | if not Is_Entity_Name (Sub) |
6320 | or else Treat_As_Volatile (Entity (Sub)) | |
6321 | then | |
6322 | Force_Evaluation (Sub); | |
6323 | end if; | |
9dfe12ae | 6324 | |
05f3e139 | 6325 | if Nkind (A_Idx) = N_Range then |
6326 | A_Range := A_Idx; | |
6327 | ||
6328 | elsif Nkind (A_Idx) = N_Identifier | |
6329 | or else Nkind (A_Idx) = N_Expanded_Name | |
6330 | then | |
6331 | A_Range := Scalar_Range (Entity (A_Idx)); | |
6332 | ||
6333 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
6334 | A_Range := Range_Expression (Constraint (A_Idx)); | |
6335 | end if; | |
6336 | ||
6337 | -- For array objects with constant bounds we can generate | |
6338 | -- the index check using the bounds of the type of the index | |
6339 | ||
6340 | if Present (A_Ent) | |
6341 | and then Ekind (A_Ent) = E_Variable | |
6342 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
6343 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
6344 | then | |
6345 | Range_N := | |
6346 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 6347 | Prefix => |
83c6c069 | 6348 | New_Occurrence_Of (Etype (A_Idx), Loc), |
05f3e139 | 6349 | Attribute_Name => Name_Range); |
6350 | ||
6351 | -- For arrays with non-constant bounds we cannot generate | |
6352 | -- the index check using the bounds of the type of the index | |
6353 | -- since it may reference discriminants of some enclosing | |
6354 | -- type. We obtain the bounds directly from the prefix | |
6355 | -- object. | |
6356 | ||
6357 | else | |
6358 | if Ind = 1 then | |
6359 | Num := No_List; | |
6360 | else | |
6361 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
6362 | end if; | |
6363 | ||
6364 | Range_N := | |
6365 | Make_Attribute_Reference (Loc, | |
6366 | Prefix => | |
6367 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
6368 | Attribute_Name => Name_Range, | |
6369 | Expressions => Num); | |
6370 | end if; | |
6371 | ||
6372 | Insert_Action (N, | |
094ed68e | 6373 | Make_Raise_Constraint_Error (Loc, |
6374 | Condition => | |
6375 | Make_Not_In (Loc, | |
6376 | Left_Opnd => | |
6377 | Convert_To (Base_Type (Etype (Sub)), | |
6378 | Duplicate_Subexpr_Move_Checks (Sub)), | |
6379 | Right_Opnd => Range_N), | |
6380 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 6381 | end if; |
6382 | ||
6383 | A_Idx := Next_Index (A_Idx); | |
6384 | Ind := Ind + 1; | |
6385 | Next (Sub); | |
6386 | end loop; | |
6387 | end; | |
6388 | end if; | |
9dfe12ae | 6389 | end Generate_Index_Checks; |
6390 | ||
6391 | -------------------------- | |
6392 | -- Generate_Range_Check -- | |
6393 | -------------------------- | |
6394 | ||
6395 | procedure Generate_Range_Check | |
6396 | (N : Node_Id; | |
6397 | Target_Type : Entity_Id; | |
6398 | Reason : RT_Exception_Code) | |
6399 | is | |
6400 | Loc : constant Source_Ptr := Sloc (N); | |
6401 | Source_Type : constant Entity_Id := Etype (N); | |
6402 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
6403 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
6404 | ||
6405 | begin | |
feff2f05 | 6406 | -- First special case, if the source type is already within the range |
6407 | -- of the target type, then no check is needed (probably we should have | |
6408 | -- stopped Do_Range_Check from being set in the first place, but better | |
cda40848 | 6409 | -- late than never in preventing junk code and junk flag settings. |
9dfe12ae | 6410 | |
7a1dabb3 | 6411 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 6412 | |
6413 | -- We do NOT apply this if the source node is a literal, since in this | |
6414 | -- case the literal has already been labeled as having the subtype of | |
6415 | -- the target. | |
6416 | ||
9dfe12ae | 6417 | and then not |
b40670e1 | 6418 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 6419 | or else |
b40670e1 | 6420 | (Is_Entity_Name (N) |
6421 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
6422 | ||
6423 | -- Also do not apply this for floating-point if Check_Float_Overflow | |
6424 | ||
6425 | and then not | |
6426 | (Is_Floating_Point_Type (Source_Type) and Check_Float_Overflow) | |
9dfe12ae | 6427 | then |
cda40848 | 6428 | Set_Do_Range_Check (N, False); |
9dfe12ae | 6429 | return; |
6430 | end if; | |
6431 | ||
cda40848 | 6432 | -- Here a check is needed. If the expander is not active, or if we are |
6433 | -- in GNATProve mode, then simply set the Do_Range_Check flag and we | |
6434 | -- are done. In both these cases, we just want to see the range check | |
6435 | -- flag set, we do not want to generate the explicit range check code. | |
6436 | ||
6437 | if GNATprove_Mode or else not Expander_Active then | |
6438 | Set_Do_Range_Check (N, True); | |
6439 | return; | |
6440 | end if; | |
6441 | ||
6442 | -- Here we will generate an explicit range check, so we don't want to | |
6443 | -- set the Do_Range check flag, since the range check is taken care of | |
6444 | -- by the code we will generate. | |
6445 | ||
6446 | Set_Do_Range_Check (N, False); | |
6447 | ||
6448 | -- Force evaluation of the node, so that it does not get evaluated twice | |
6449 | -- (once for the check, once for the actual reference). Such a double | |
6450 | -- evaluation is always a potential source of inefficiency, and is | |
6451 | -- functionally incorrect in the volatile case. | |
9dfe12ae | 6452 | |
b40670e1 | 6453 | if not Is_Entity_Name (N) or else Treat_As_Volatile (Entity (N)) then |
9dfe12ae | 6454 | Force_Evaluation (N); |
6455 | end if; | |
6456 | ||
feff2f05 | 6457 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
6458 | -- the same since in this case we can simply do a direct check of the | |
6459 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 6460 | |
6461 | -- [constraint_error when N not in Target_Type] | |
6462 | ||
6463 | -- Note: this is by far the most common case, for example all cases of | |
6464 | -- checks on the RHS of assignments are in this category, but not all | |
6465 | -- cases are like this. Notably conversions can involve two types. | |
6466 | ||
6467 | if Source_Base_Type = Target_Base_Type then | |
99ed4b4c | 6468 | |
6469 | -- Insert the explicit range check. Note that we suppress checks for | |
6470 | -- this code, since we don't want a recursive range check popping up. | |
6471 | ||
9dfe12ae | 6472 | Insert_Action (N, |
6473 | Make_Raise_Constraint_Error (Loc, | |
6474 | Condition => | |
6475 | Make_Not_In (Loc, | |
6476 | Left_Opnd => Duplicate_Subexpr (N), | |
6477 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
99ed4b4c | 6478 | Reason => Reason), |
6479 | Suppress => All_Checks); | |
9dfe12ae | 6480 | |
6481 | -- Next test for the case where the target type is within the bounds | |
6482 | -- of the base type of the source type, since in this case we can | |
6483 | -- simply convert these bounds to the base type of T to do the test. | |
6484 | ||
6485 | -- [constraint_error when N not in | |
6486 | -- Source_Base_Type (Target_Type'First) | |
6487 | -- .. | |
6488 | -- Source_Base_Type(Target_Type'Last))] | |
6489 | ||
f2a06be9 | 6490 | -- The conversions will always work and need no check |
9dfe12ae | 6491 | |
a9b57347 | 6492 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
6493 | -- of converting from an enumeration value to an integer type, such as | |
6494 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
6495 | -- (which used to be handled by gigi). This is OK, since the conversion | |
6496 | -- itself does not require a check. | |
6497 | ||
7a1dabb3 | 6498 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
99ed4b4c | 6499 | |
6500 | -- Insert the explicit range check. Note that we suppress checks for | |
6501 | -- this code, since we don't want a recursive range check popping up. | |
6502 | ||
9dfe12ae | 6503 | Insert_Action (N, |
6504 | Make_Raise_Constraint_Error (Loc, | |
6505 | Condition => | |
6506 | Make_Not_In (Loc, | |
6507 | Left_Opnd => Duplicate_Subexpr (N), | |
6508 | ||
6509 | Right_Opnd => | |
6510 | Make_Range (Loc, | |
6511 | Low_Bound => | |
a9b57347 | 6512 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 6513 | Make_Attribute_Reference (Loc, |
6514 | Prefix => | |
6515 | New_Occurrence_Of (Target_Type, Loc), | |
6516 | Attribute_Name => Name_First)), | |
6517 | ||
6518 | High_Bound => | |
a9b57347 | 6519 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 6520 | Make_Attribute_Reference (Loc, |
6521 | Prefix => | |
6522 | New_Occurrence_Of (Target_Type, Loc), | |
6523 | Attribute_Name => Name_Last)))), | |
99ed4b4c | 6524 | Reason => Reason), |
6525 | Suppress => All_Checks); | |
9dfe12ae | 6526 | |
feff2f05 | 6527 | -- Note that at this stage we now that the Target_Base_Type is not in |
6528 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
6529 | -- is not in this range). It could still be the case that Source_Type is | |
6530 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 6531 | |
feff2f05 | 6532 | -- If that is the case, we can freely convert the source to the target, |
6533 | -- and then test the target result against the bounds. | |
9dfe12ae | 6534 | |
7a1dabb3 | 6535 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
9dfe12ae | 6536 | |
feff2f05 | 6537 | -- We make a temporary to hold the value of the converted value |
6538 | -- (converted to the base type), and then we will do the test against | |
6539 | -- this temporary. | |
9dfe12ae | 6540 | |
6541 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); | |
6542 | -- [constraint_error when Tnn not in Target_Type] | |
6543 | ||
6544 | -- Then the conversion itself is replaced by an occurrence of Tnn | |
6545 | ||
99ed4b4c | 6546 | -- Insert the explicit range check. Note that we suppress checks for |
6547 | -- this code, since we don't want a recursive range check popping up. | |
6548 | ||
9dfe12ae | 6549 | declare |
46eb6933 | 6550 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 6551 | |
6552 | begin | |
6553 | Insert_Actions (N, New_List ( | |
6554 | Make_Object_Declaration (Loc, | |
6555 | Defining_Identifier => Tnn, | |
6556 | Object_Definition => | |
6557 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6558 | Constant_Present => True, | |
6559 | Expression => | |
6560 | Make_Type_Conversion (Loc, | |
6561 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
6562 | Expression => Duplicate_Subexpr (N))), | |
6563 | ||
6564 | Make_Raise_Constraint_Error (Loc, | |
6565 | Condition => | |
6566 | Make_Not_In (Loc, | |
6567 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6568 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
6569 | ||
99ed4b4c | 6570 | Reason => Reason)), |
6571 | Suppress => All_Checks); | |
9dfe12ae | 6572 | |
6573 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
2af58f67 | 6574 | |
6575 | -- Set the type of N, because the declaration for Tnn might not | |
6576 | -- be analyzed yet, as is the case if N appears within a record | |
6577 | -- declaration, as a discriminant constraint or expression. | |
6578 | ||
6579 | Set_Etype (N, Target_Base_Type); | |
9dfe12ae | 6580 | end; |
6581 | ||
6582 | -- At this stage, we know that we have two scalar types, which are | |
6583 | -- directly convertible, and where neither scalar type has a base | |
6584 | -- range that is in the range of the other scalar type. | |
6585 | ||
6586 | -- The only way this can happen is with a signed and unsigned type. | |
6587 | -- So test for these two cases: | |
6588 | ||
6589 | else | |
6590 | -- Case of the source is unsigned and the target is signed | |
6591 | ||
6592 | if Is_Unsigned_Type (Source_Base_Type) | |
6593 | and then not Is_Unsigned_Type (Target_Base_Type) | |
6594 | then | |
6595 | -- If the source is unsigned and the target is signed, then we | |
6596 | -- know that the source is not shorter than the target (otherwise | |
6597 | -- the source base type would be in the target base type range). | |
6598 | ||
feff2f05 | 6599 | -- In other words, the unsigned type is either the same size as |
6600 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 6601 | |
6602 | pragma Assert | |
6603 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
6604 | ||
6605 | -- We only need to check the low bound if the low bound of the | |
6606 | -- target type is non-negative. If the low bound of the target | |
6607 | -- type is negative, then we know that we will fit fine. | |
6608 | ||
6609 | -- If the high bound of the target type is negative, then we | |
6610 | -- know we have a constraint error, since we can't possibly | |
6611 | -- have a negative source. | |
6612 | ||
6613 | -- With these two checks out of the way, we can do the check | |
6614 | -- using the source type safely | |
6615 | ||
39a0c1d3 | 6616 | -- This is definitely the most annoying case. |
9dfe12ae | 6617 | |
6618 | -- [constraint_error | |
6619 | -- when (Target_Type'First >= 0 | |
6620 | -- and then | |
6621 | -- N < Source_Base_Type (Target_Type'First)) | |
6622 | -- or else Target_Type'Last < 0 | |
6623 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
6624 | ||
6625 | -- We turn off all checks since we know that the conversions | |
6626 | -- will work fine, given the guards for negative values. | |
6627 | ||
6628 | Insert_Action (N, | |
6629 | Make_Raise_Constraint_Error (Loc, | |
6630 | Condition => | |
6631 | Make_Or_Else (Loc, | |
6632 | Make_Or_Else (Loc, | |
6633 | Left_Opnd => | |
6634 | Make_And_Then (Loc, | |
6635 | Left_Opnd => Make_Op_Ge (Loc, | |
6636 | Left_Opnd => | |
6637 | Make_Attribute_Reference (Loc, | |
6638 | Prefix => | |
6639 | New_Occurrence_Of (Target_Type, Loc), | |
6640 | Attribute_Name => Name_First), | |
6641 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
6642 | ||
6643 | Right_Opnd => | |
6644 | Make_Op_Lt (Loc, | |
6645 | Left_Opnd => Duplicate_Subexpr (N), | |
6646 | Right_Opnd => | |
6647 | Convert_To (Source_Base_Type, | |
6648 | Make_Attribute_Reference (Loc, | |
6649 | Prefix => | |
6650 | New_Occurrence_Of (Target_Type, Loc), | |
6651 | Attribute_Name => Name_First)))), | |
6652 | ||
6653 | Right_Opnd => | |
6654 | Make_Op_Lt (Loc, | |
6655 | Left_Opnd => | |
6656 | Make_Attribute_Reference (Loc, | |
6657 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6658 | Attribute_Name => Name_Last), | |
6659 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
6660 | ||
6661 | Right_Opnd => | |
6662 | Make_Op_Gt (Loc, | |
6663 | Left_Opnd => Duplicate_Subexpr (N), | |
6664 | Right_Opnd => | |
6665 | Convert_To (Source_Base_Type, | |
6666 | Make_Attribute_Reference (Loc, | |
6667 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6668 | Attribute_Name => Name_Last)))), | |
6669 | ||
6670 | Reason => Reason), | |
6671 | Suppress => All_Checks); | |
6672 | ||
6673 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 6674 | -- the target is unsigned. |
9dfe12ae | 6675 | |
6676 | else | |
6677 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
20cf157b | 6678 | and then Is_Unsigned_Type (Target_Base_Type)); |
9dfe12ae | 6679 | |
feff2f05 | 6680 | -- If the source is signed and the target is unsigned, then we |
6681 | -- know that the target is not shorter than the source (otherwise | |
6682 | -- the target base type would be in the source base type range). | |
9dfe12ae | 6683 | |
feff2f05 | 6684 | -- In other words, the unsigned type is either the same size as |
6685 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 6686 | |
feff2f05 | 6687 | -- Clearly we have an error if the source value is negative since |
6688 | -- no unsigned type can have negative values. If the source type | |
6689 | -- is non-negative, then the check can be done using the target | |
6690 | -- type. | |
9dfe12ae | 6691 | |
6692 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
6693 | ||
6694 | -- [constraint_error | |
6695 | -- when N < 0 or else Tnn not in Target_Type]; | |
6696 | ||
feff2f05 | 6697 | -- We turn off all checks for the conversion of N to the target |
6698 | -- base type, since we generate the explicit check to ensure that | |
6699 | -- the value is non-negative | |
9dfe12ae | 6700 | |
6701 | declare | |
46eb6933 | 6702 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 6703 | |
6704 | begin | |
6705 | Insert_Actions (N, New_List ( | |
6706 | Make_Object_Declaration (Loc, | |
6707 | Defining_Identifier => Tnn, | |
6708 | Object_Definition => | |
6709 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6710 | Constant_Present => True, | |
6711 | Expression => | |
a9b57347 | 6712 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 6713 | Subtype_Mark => |
6714 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6715 | Expression => Duplicate_Subexpr (N))), | |
6716 | ||
6717 | Make_Raise_Constraint_Error (Loc, | |
6718 | Condition => | |
6719 | Make_Or_Else (Loc, | |
6720 | Left_Opnd => | |
6721 | Make_Op_Lt (Loc, | |
6722 | Left_Opnd => Duplicate_Subexpr (N), | |
6723 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
6724 | ||
6725 | Right_Opnd => | |
6726 | Make_Not_In (Loc, | |
6727 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6728 | Right_Opnd => | |
6729 | New_Occurrence_Of (Target_Type, Loc))), | |
6730 | ||
20cf157b | 6731 | Reason => Reason)), |
9dfe12ae | 6732 | Suppress => All_Checks); |
6733 | ||
feff2f05 | 6734 | -- Set the Etype explicitly, because Insert_Actions may have |
6735 | -- placed the declaration in the freeze list for an enclosing | |
6736 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 6737 | |
6738 | Set_Etype (Tnn, Target_Base_Type); | |
6739 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6740 | end; | |
6741 | end if; | |
6742 | end if; | |
6743 | end Generate_Range_Check; | |
6744 | ||
2af58f67 | 6745 | ------------------ |
6746 | -- Get_Check_Id -- | |
6747 | ------------------ | |
6748 | ||
6749 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
6750 | begin | |
6751 | -- For standard check name, we can do a direct computation | |
6752 | ||
6753 | if N in First_Check_Name .. Last_Check_Name then | |
6754 | return Check_Id (N - (First_Check_Name - 1)); | |
6755 | ||
6756 | -- For non-standard names added by pragma Check_Name, search table | |
6757 | ||
6758 | else | |
6759 | for J in All_Checks + 1 .. Check_Names.Last loop | |
6760 | if Check_Names.Table (J) = N then | |
6761 | return J; | |
6762 | end if; | |
6763 | end loop; | |
6764 | end if; | |
6765 | ||
6766 | -- No matching name found | |
6767 | ||
6768 | return No_Check_Id; | |
6769 | end Get_Check_Id; | |
6770 | ||
ee6ba406 | 6771 | --------------------- |
6772 | -- Get_Discriminal -- | |
6773 | --------------------- | |
6774 | ||
6775 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
6776 | Loc : constant Source_Ptr := Sloc (E); | |
6777 | D : Entity_Id; | |
6778 | Sc : Entity_Id; | |
6779 | ||
6780 | begin | |
0577b0b1 | 6781 | -- The bound can be a bona fide parameter of a protected operation, |
6782 | -- rather than a prival encoded as an in-parameter. | |
6783 | ||
6784 | if No (Discriminal_Link (Entity (Bound))) then | |
6785 | return Bound; | |
6786 | end if; | |
6787 | ||
2af58f67 | 6788 | -- Climb the scope stack looking for an enclosing protected type. If |
6789 | -- we run out of scopes, return the bound itself. | |
6790 | ||
6791 | Sc := Scope (E); | |
6792 | while Present (Sc) loop | |
6793 | if Sc = Standard_Standard then | |
6794 | return Bound; | |
2af58f67 | 6795 | elsif Ekind (Sc) = E_Protected_Type then |
6796 | exit; | |
6797 | end if; | |
6798 | ||
6799 | Sc := Scope (Sc); | |
6800 | end loop; | |
6801 | ||
ee6ba406 | 6802 | D := First_Discriminant (Sc); |
2af58f67 | 6803 | while Present (D) loop |
6804 | if Chars (D) = Chars (Bound) then | |
6805 | return New_Occurrence_Of (Discriminal (D), Loc); | |
6806 | end if; | |
ee6ba406 | 6807 | |
ee6ba406 | 6808 | Next_Discriminant (D); |
6809 | end loop; | |
6810 | ||
2af58f67 | 6811 | return Bound; |
ee6ba406 | 6812 | end Get_Discriminal; |
6813 | ||
2af58f67 | 6814 | ---------------------- |
6815 | -- Get_Range_Checks -- | |
6816 | ---------------------- | |
6817 | ||
6818 | function Get_Range_Checks | |
6819 | (Ck_Node : Node_Id; | |
6820 | Target_Typ : Entity_Id; | |
6821 | Source_Typ : Entity_Id := Empty; | |
6822 | Warn_Node : Node_Id := Empty) return Check_Result | |
6823 | is | |
6824 | begin | |
20cf157b | 6825 | return |
6826 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
2af58f67 | 6827 | end Get_Range_Checks; |
6828 | ||
ee6ba406 | 6829 | ------------------ |
6830 | -- Guard_Access -- | |
6831 | ------------------ | |
6832 | ||
6833 | function Guard_Access | |
6834 | (Cond : Node_Id; | |
6835 | Loc : Source_Ptr; | |
314a23b6 | 6836 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 6837 | is |
6838 | begin | |
6839 | if Nkind (Cond) = N_Or_Else then | |
6840 | Set_Paren_Count (Cond, 1); | |
6841 | end if; | |
6842 | ||
6843 | if Nkind (Ck_Node) = N_Allocator then | |
6844 | return Cond; | |
20cf157b | 6845 | |
ee6ba406 | 6846 | else |
6847 | return | |
6848 | Make_And_Then (Loc, | |
6849 | Left_Opnd => | |
6850 | Make_Op_Ne (Loc, | |
9dfe12ae | 6851 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 6852 | Right_Opnd => Make_Null (Loc)), |
6853 | Right_Opnd => Cond); | |
6854 | end if; | |
6855 | end Guard_Access; | |
6856 | ||
6857 | ----------------------------- | |
6858 | -- Index_Checks_Suppressed -- | |
6859 | ----------------------------- | |
6860 | ||
6861 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6862 | begin | |
9dfe12ae | 6863 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6864 | return Is_Check_Suppressed (E, Index_Check); | |
6865 | else | |
fafc6b97 | 6866 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 6867 | end if; |
ee6ba406 | 6868 | end Index_Checks_Suppressed; |
6869 | ||
6870 | ---------------- | |
6871 | -- Initialize -- | |
6872 | ---------------- | |
6873 | ||
6874 | procedure Initialize is | |
6875 | begin | |
6876 | for J in Determine_Range_Cache_N'Range loop | |
6877 | Determine_Range_Cache_N (J) := Empty; | |
6878 | end loop; | |
2af58f67 | 6879 | |
6880 | Check_Names.Init; | |
6881 | ||
6882 | for J in Int range 1 .. All_Checks loop | |
6883 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
6884 | end loop; | |
ee6ba406 | 6885 | end Initialize; |
6886 | ||
6887 | ------------------------- | |
6888 | -- Insert_Range_Checks -- | |
6889 | ------------------------- | |
6890 | ||
6891 | procedure Insert_Range_Checks | |
6892 | (Checks : Check_Result; | |
6893 | Node : Node_Id; | |
6894 | Suppress_Typ : Entity_Id; | |
6895 | Static_Sloc : Source_Ptr := No_Location; | |
6896 | Flag_Node : Node_Id := Empty; | |
6897 | Do_Before : Boolean := False) | |
6898 | is | |
6899 | Internal_Flag_Node : Node_Id := Flag_Node; | |
6900 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
6901 | ||
6902 | Check_Node : Node_Id; | |
6903 | Checks_On : constant Boolean := | |
b6341c67 | 6904 | (not Index_Checks_Suppressed (Suppress_Typ)) |
6905 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 6906 | |
6907 | begin | |
feff2f05 | 6908 | -- For now we just return if Checks_On is false, however this should be |
6909 | -- enhanced to check for an always True value in the condition and to | |
6910 | -- generate a compilation warning??? | |
ee6ba406 | 6911 | |
ac9184ed | 6912 | if not Expander_Active or not Checks_On then |
ee6ba406 | 6913 | return; |
6914 | end if; | |
6915 | ||
6916 | if Static_Sloc = No_Location then | |
6917 | Internal_Static_Sloc := Sloc (Node); | |
6918 | end if; | |
6919 | ||
6920 | if No (Flag_Node) then | |
6921 | Internal_Flag_Node := Node; | |
6922 | end if; | |
6923 | ||
6924 | for J in 1 .. 2 loop | |
6925 | exit when No (Checks (J)); | |
6926 | ||
6927 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
6928 | and then Present (Condition (Checks (J))) | |
6929 | then | |
6930 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
6931 | Check_Node := Checks (J); | |
6932 | Mark_Rewrite_Insertion (Check_Node); | |
6933 | ||
6934 | if Do_Before then | |
6935 | Insert_Before_And_Analyze (Node, Check_Node); | |
6936 | else | |
6937 | Insert_After_And_Analyze (Node, Check_Node); | |
6938 | end if; | |
6939 | ||
6940 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
6941 | end if; | |
6942 | ||
6943 | else | |
6944 | Check_Node := | |
f15731c4 | 6945 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
6946 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 6947 | Mark_Rewrite_Insertion (Check_Node); |
6948 | ||
6949 | if Do_Before then | |
6950 | Insert_Before_And_Analyze (Node, Check_Node); | |
6951 | else | |
6952 | Insert_After_And_Analyze (Node, Check_Node); | |
6953 | end if; | |
6954 | end if; | |
6955 | end loop; | |
6956 | end Insert_Range_Checks; | |
6957 | ||
6958 | ------------------------ | |
6959 | -- Insert_Valid_Check -- | |
6960 | ------------------------ | |
6961 | ||
6962 | procedure Insert_Valid_Check (Expr : Node_Id) is | |
6963 | Loc : constant Source_Ptr := Sloc (Expr); | |
70580828 | 6964 | Typ : constant Entity_Id := Etype (Expr); |
8b718dab | 6965 | Exp : Node_Id; |
ee6ba406 | 6966 | |
6967 | begin | |
06ad5813 | 6968 | -- Do not insert if checks off, or if not checking validity or |
6969 | -- if expression is known to be valid | |
ee6ba406 | 6970 | |
0577b0b1 | 6971 | if not Validity_Checks_On |
6972 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 6973 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 6974 | then |
8b718dab | 6975 | return; |
6976 | end if; | |
ee6ba406 | 6977 | |
42c57d55 | 6978 | -- Do not insert checks within a predicate function. This will arise |
6979 | -- if the current unit and the predicate function are being compiled | |
6980 | -- with validity checks enabled. | |
70580828 | 6981 | |
6982 | if Present (Predicate_Function (Typ)) | |
6983 | and then Current_Scope = Predicate_Function (Typ) | |
6984 | then | |
6985 | return; | |
6986 | end if; | |
6987 | ||
310c1cde | 6988 | -- If the expression is a packed component of a modular type of the |
6989 | -- right size, the data is always valid. | |
6990 | ||
6991 | if Nkind (Expr) = N_Selected_Component | |
6992 | and then Present (Component_Clause (Entity (Selector_Name (Expr)))) | |
6993 | and then Is_Modular_Integer_Type (Typ) | |
6994 | and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) | |
6995 | then | |
6996 | return; | |
6997 | end if; | |
6998 | ||
8b718dab | 6999 | -- If we have a checked conversion, then validity check applies to |
7000 | -- the expression inside the conversion, not the result, since if | |
7001 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 7002 | |
8b718dab | 7003 | Exp := Expr; |
7004 | while Nkind (Exp) = N_Type_Conversion loop | |
7005 | Exp := Expression (Exp); | |
7006 | end loop; | |
7007 | ||
0577b0b1 | 7008 | -- We are about to insert the validity check for Exp. We save and |
7009 | -- reset the Do_Range_Check flag over this validity check, and then | |
7010 | -- put it back for the final original reference (Exp may be rewritten). | |
7011 | ||
7012 | declare | |
7013 | DRC : constant Boolean := Do_Range_Check (Exp); | |
23abd64d | 7014 | PV : Node_Id; |
7015 | CE : Node_Id; | |
05fcfafb | 7016 | |
0577b0b1 | 7017 | begin |
7018 | Set_Do_Range_Check (Exp, False); | |
7019 | ||
06ad5813 | 7020 | -- Force evaluation to avoid multiple reads for atomic/volatile |
7021 | ||
fa771c05 | 7022 | -- Note: we set Name_Req to False. We used to set it to True, with |
7023 | -- the thinking that a name is required as the prefix of the 'Valid | |
7024 | -- call, but in fact the check that the prefix of an attribute is | |
7025 | -- a name is in the parser, and we just don't require it here. | |
7026 | -- Moreover, when we set Name_Req to True, that interfered with the | |
7027 | -- checking for Volatile, since we couldn't just capture the value. | |
7028 | ||
06ad5813 | 7029 | if Is_Entity_Name (Exp) |
7030 | and then Is_Volatile (Entity (Exp)) | |
7031 | then | |
fa771c05 | 7032 | -- Same reasoning as above for setting Name_Req to False |
7033 | ||
7034 | Force_Evaluation (Exp, Name_Req => False); | |
06ad5813 | 7035 | end if; |
7036 | ||
23abd64d | 7037 | -- Build the prefix for the 'Valid call |
7038 | ||
fa771c05 | 7039 | PV := Duplicate_Subexpr_No_Checks (Exp, Name_Req => False); |
0577b0b1 | 7040 | |
443bdccb | 7041 | -- A rather specialized test. If PV is an analyzed expression which |
7042 | -- is an indexed component of a packed array that has not been | |
7043 | -- properly expanded, turn off its Analyzed flag to make sure it | |
5f46de53 | 7044 | -- gets properly reexpanded. If the prefix is an access value, |
7045 | -- the dereference will be added later. | |
23abd64d | 7046 | |
7047 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
7048 | -- an analyze with the old parent pointer. This may point e.g. to | |
7049 | -- a subprogram call, which deactivates this expansion. | |
7050 | ||
7051 | if Analyzed (PV) | |
7052 | and then Nkind (PV) = N_Indexed_Component | |
5f46de53 | 7053 | and then Is_Array_Type (Etype (Prefix (PV))) |
a88a5773 | 7054 | and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV)))) |
23abd64d | 7055 | then |
7056 | Set_Analyzed (PV, False); | |
7057 | end if; | |
7058 | ||
fa771c05 | 7059 | -- Build the raise CE node to check for validity. We build a type |
7060 | -- qualification for the prefix, since it may not be of the form of | |
7061 | -- a name, and we don't care in this context! | |
23abd64d | 7062 | |
7063 | CE := | |
0577b0b1 | 7064 | Make_Raise_Constraint_Error (Loc, |
7065 | Condition => | |
7066 | Make_Op_Not (Loc, | |
7067 | Right_Opnd => | |
7068 | Make_Attribute_Reference (Loc, | |
23abd64d | 7069 | Prefix => PV, |
0577b0b1 | 7070 | Attribute_Name => Name_Valid)), |
23abd64d | 7071 | Reason => CE_Invalid_Data); |
7072 | ||
7073 | -- Insert the validity check. Note that we do this with validity | |
7074 | -- checks turned off, to avoid recursion, we do not want validity | |
39a0c1d3 | 7075 | -- checks on the validity checking code itself. |
23abd64d | 7076 | |
7077 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 7078 | |
6fb3c314 | 7079 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 7080 | -- array, then it is rewritten as a renaming declaration. If the |
7081 | -- expression is an actual in a call, it has not been expanded, | |
7082 | -- waiting for the proper point at which to do it. The same happens | |
7083 | -- with renamings, so that we have to force the expansion now. This | |
7084 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
7085 | -- and exp_ch6.adb. | |
7086 | ||
7087 | if Is_Entity_Name (Exp) | |
7088 | and then Nkind (Parent (Entity (Exp))) = | |
20cf157b | 7089 | N_Object_Renaming_Declaration |
0577b0b1 | 7090 | then |
7091 | declare | |
7092 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
7093 | begin | |
7094 | if Nkind (Old_Exp) = N_Indexed_Component | |
7095 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
7096 | then | |
7097 | Expand_Packed_Element_Reference (Old_Exp); | |
7098 | end if; | |
7099 | end; | |
7100 | end if; | |
7101 | ||
7102 | -- Put back the Do_Range_Check flag on the resulting (possibly | |
7103 | -- rewritten) expression. | |
7104 | ||
7105 | -- Note: it might be thought that a validity check is not required | |
7106 | -- when a range check is present, but that's not the case, because | |
7107 | -- the back end is allowed to assume for the range check that the | |
7108 | -- operand is within its declared range (an assumption that validity | |
39a0c1d3 | 7109 | -- checking is all about NOT assuming). |
0577b0b1 | 7110 | |
00c403ee | 7111 | -- Note: no need to worry about Possible_Local_Raise here, it will |
7112 | -- already have been called if original node has Do_Range_Check set. | |
7113 | ||
0577b0b1 | 7114 | Set_Do_Range_Check (Exp, DRC); |
7115 | end; | |
ee6ba406 | 7116 | end Insert_Valid_Check; |
7117 | ||
3cce7f32 | 7118 | ------------------------------------- |
7119 | -- Is_Signed_Integer_Arithmetic_Op -- | |
7120 | ------------------------------------- | |
7121 | ||
7122 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
7123 | begin | |
7124 | case Nkind (N) is | |
7125 | when N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon | | |
7126 | N_Op_Minus | N_Op_Mod | N_Op_Multiply | N_Op_Plus | | |
7127 | N_Op_Rem | N_Op_Subtract => | |
7128 | return Is_Signed_Integer_Type (Etype (N)); | |
7129 | ||
92f1631f | 7130 | when N_If_Expression | N_Case_Expression => |
0326b4d4 | 7131 | return Is_Signed_Integer_Type (Etype (N)); |
7132 | ||
3cce7f32 | 7133 | when others => |
7134 | return False; | |
7135 | end case; | |
7136 | end Is_Signed_Integer_Arithmetic_Op; | |
7137 | ||
fa7497e8 | 7138 | ---------------------------------- |
7139 | -- Install_Null_Excluding_Check -- | |
7140 | ---------------------------------- | |
7141 | ||
7142 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 7143 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 7144 | Typ : constant Entity_Id := Etype (N); |
7145 | ||
7b31b357 | 7146 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
7147 | -- Determines if it is safe to capture Known_Non_Null status for an | |
7148 | -- the entity referenced by node N. The caller ensures that N is indeed | |
7149 | -- an entity name. It is safe to capture the non-null status for an IN | |
7150 | -- parameter when the reference occurs within a declaration that is sure | |
7151 | -- to be executed as part of the declarative region. | |
7870823d | 7152 | |
84d0d4a5 | 7153 | procedure Mark_Non_Null; |
7870823d | 7154 | -- After installation of check, if the node in question is an entity |
7155 | -- name, then mark this entity as non-null if possible. | |
7156 | ||
7b31b357 | 7157 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 7158 | E : constant Entity_Id := Entity (N); |
7159 | S : constant Entity_Id := Current_Scope; | |
7160 | S_Par : Node_Id; | |
7161 | ||
7162 | begin | |
7b31b357 | 7163 | if Ekind (E) /= E_In_Parameter then |
7164 | return False; | |
7165 | end if; | |
7870823d | 7166 | |
7167 | -- Two initial context checks. We must be inside a subprogram body | |
7168 | -- with declarations and reference must not appear in nested scopes. | |
7169 | ||
7b31b357 | 7170 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 7171 | or else Scope (E) /= S |
7172 | then | |
7173 | return False; | |
7174 | end if; | |
7175 | ||
7176 | S_Par := Parent (Parent (S)); | |
7177 | ||
7178 | if Nkind (S_Par) /= N_Subprogram_Body | |
7179 | or else No (Declarations (S_Par)) | |
7180 | then | |
7181 | return False; | |
7182 | end if; | |
7183 | ||
7184 | declare | |
7185 | N_Decl : Node_Id; | |
7186 | P : Node_Id; | |
7187 | ||
7188 | begin | |
7189 | -- Retrieve the declaration node of N (if any). Note that N | |
7190 | -- may be a part of a complex initialization expression. | |
7191 | ||
7192 | P := Parent (N); | |
7193 | N_Decl := Empty; | |
7194 | while Present (P) loop | |
7195 | ||
7b31b357 | 7196 | -- If we have a short circuit form, and we are within the right |
7197 | -- hand expression, we return false, since the right hand side | |
7198 | -- is not guaranteed to be elaborated. | |
7199 | ||
7200 | if Nkind (P) in N_Short_Circuit | |
7201 | and then N = Right_Opnd (P) | |
7202 | then | |
7203 | return False; | |
7204 | end if; | |
7205 | ||
92f1631f | 7206 | -- Similarly, if we are in an if expression and not part of the |
7207 | -- condition, then we return False, since neither the THEN or | |
7208 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 7209 | |
92f1631f | 7210 | if Nkind (P) = N_If_Expression |
7b31b357 | 7211 | and then N /= First (Expressions (P)) |
7212 | then | |
7213 | return False; | |
e977c0cf | 7214 | end if; |
7215 | ||
20cf157b | 7216 | -- If within a case expression, and not part of the expression, |
7217 | -- then return False, since a particular dependent expression | |
7218 | -- may not always be elaborated | |
e977c0cf | 7219 | |
7220 | if Nkind (P) = N_Case_Expression | |
7221 | and then N /= Expression (P) | |
7222 | then | |
7223 | return False; | |
7b31b357 | 7224 | end if; |
7225 | ||
20cf157b | 7226 | -- While traversing the parent chain, if node N belongs to a |
7227 | -- statement, then it may never appear in a declarative region. | |
7870823d | 7228 | |
7229 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
7230 | or else Nkind (P) = N_Procedure_Call_Statement | |
7231 | then | |
7232 | return False; | |
7233 | end if; | |
7234 | ||
7b31b357 | 7235 | -- If we are at a declaration, record it and exit |
7236 | ||
7870823d | 7237 | if Nkind (P) in N_Declaration |
7238 | and then Nkind (P) not in N_Subprogram_Specification | |
7239 | then | |
7240 | N_Decl := P; | |
7241 | exit; | |
7242 | end if; | |
7243 | ||
7244 | P := Parent (P); | |
7245 | end loop; | |
7246 | ||
7247 | if No (N_Decl) then | |
7248 | return False; | |
7249 | end if; | |
7250 | ||
7251 | return List_Containing (N_Decl) = Declarations (S_Par); | |
7252 | end; | |
7b31b357 | 7253 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 7254 | |
7255 | ------------------- | |
7256 | -- Mark_Non_Null -- | |
7257 | ------------------- | |
7258 | ||
7259 | procedure Mark_Non_Null is | |
7260 | begin | |
7870823d | 7261 | -- Only case of interest is if node N is an entity name |
7262 | ||
84d0d4a5 | 7263 | if Is_Entity_Name (N) then |
7870823d | 7264 | |
7265 | -- For sure, we want to clear an indication that this is known to | |
39a0c1d3 | 7266 | -- be null, since if we get past this check, it definitely is not. |
7870823d | 7267 | |
84d0d4a5 | 7268 | Set_Is_Known_Null (Entity (N), False); |
7269 | ||
7870823d | 7270 | -- We can mark the entity as known to be non-null if either it is |
7271 | -- safe to capture the value, or in the case of an IN parameter, | |
7272 | -- which is a constant, if the check we just installed is in the | |
7273 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 7274 | -- a check is decisive for the rest of the body if the expression |
7275 | -- is sure to be elaborated, since we know we have to elaborate | |
7276 | -- all declarations before executing the body. | |
7277 | ||
7278 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 7279 | |
7280 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 7281 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 7282 | then |
7283 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 7284 | end if; |
7285 | end if; | |
7286 | end Mark_Non_Null; | |
7287 | ||
7288 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 7289 | |
7290 | begin | |
84d0d4a5 | 7291 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 7292 | |
46e32b5e | 7293 | -- No check inside a generic, check will be emitted in instance |
fa7497e8 | 7294 | |
84d0d4a5 | 7295 | if Inside_A_Generic then |
fa7497e8 | 7296 | return; |
84d0d4a5 | 7297 | end if; |
7298 | ||
7299 | -- No check needed if known to be non-null | |
7300 | ||
7301 | if Known_Non_Null (N) then | |
05fcfafb | 7302 | return; |
84d0d4a5 | 7303 | end if; |
fa7497e8 | 7304 | |
84d0d4a5 | 7305 | -- If known to be null, here is where we generate a compile time check |
7306 | ||
7307 | if Known_Null (N) then | |
d16989f1 | 7308 | |
20cf157b | 7309 | -- Avoid generating warning message inside init procs. In SPARK mode |
7310 | -- we can go ahead and call Apply_Compile_Time_Constraint_Error | |
46e32b5e | 7311 | -- since it will be turned into an error in any case. |
d16989f1 | 7312 | |
46e32b5e | 7313 | if (not Inside_Init_Proc or else SPARK_Mode = On) |
7314 | ||
28d5d68f | 7315 | -- Do not emit the warning within a conditional expression, |
7316 | -- where the expression might not be evaluated, and the warning | |
7317 | -- appear as extraneous noise. | |
46e32b5e | 7318 | |
7319 | and then not Within_Case_Or_If_Expression (N) | |
7320 | then | |
d16989f1 | 7321 | Apply_Compile_Time_Constraint_Error |
4098232e | 7322 | (N, "null value not allowed here??", CE_Access_Check_Failed); |
46e32b5e | 7323 | |
7324 | -- Remaining cases, where we silently insert the raise | |
7325 | ||
d16989f1 | 7326 | else |
7327 | Insert_Action (N, | |
7328 | Make_Raise_Constraint_Error (Loc, | |
7329 | Reason => CE_Access_Check_Failed)); | |
7330 | end if; | |
7331 | ||
84d0d4a5 | 7332 | Mark_Non_Null; |
7333 | return; | |
7334 | end if; | |
7335 | ||
7336 | -- If entity is never assigned, for sure a warning is appropriate | |
7337 | ||
7338 | if Is_Entity_Name (N) then | |
7339 | Check_Unset_Reference (N); | |
fa7497e8 | 7340 | end if; |
84d0d4a5 | 7341 | |
7342 | -- No check needed if checks are suppressed on the range. Note that we | |
7343 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
7344 | -- so, since the program is erroneous, but we don't like to casually | |
7345 | -- propagate such conclusions from erroneosity). | |
7346 | ||
7347 | if Access_Checks_Suppressed (Typ) then | |
7348 | return; | |
7349 | end if; | |
7350 | ||
2af58f67 | 7351 | -- No check needed for access to concurrent record types generated by |
7352 | -- the expander. This is not just an optimization (though it does indeed | |
7353 | -- remove junk checks). It also avoids generation of junk warnings. | |
7354 | ||
7355 | if Nkind (N) in N_Has_Chars | |
7356 | and then Chars (N) = Name_uObject | |
7357 | and then Is_Concurrent_Record_Type | |
7358 | (Directly_Designated_Type (Etype (N))) | |
7359 | then | |
7360 | return; | |
7361 | end if; | |
7362 | ||
228836e8 | 7363 | -- No check needed in interface thunks since the runtime check is |
7364 | -- already performed at the caller side. | |
7365 | ||
7366 | if Is_Thunk (Current_Scope) then | |
7367 | return; | |
7368 | end if; | |
7369 | ||
472ea160 | 7370 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
7371 | -- the expander within exception handlers, since we know that the value | |
7372 | -- can never be null. | |
7373 | ||
7374 | -- Is this really the right way to do this? Normally we generate such | |
7375 | -- code in the expander with checks off, and that's how we suppress this | |
7376 | -- kind of junk check ??? | |
7377 | ||
7378 | if Nkind (N) = N_Function_Call | |
7379 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
7380 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
7381 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
7382 | then | |
7383 | return; | |
7384 | end if; | |
7385 | ||
84d0d4a5 | 7386 | -- Otherwise install access check |
7387 | ||
7388 | Insert_Action (N, | |
7389 | Make_Raise_Constraint_Error (Loc, | |
7390 | Condition => | |
7391 | Make_Op_Eq (Loc, | |
7392 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
7393 | Right_Opnd => Make_Null (Loc)), | |
7394 | Reason => CE_Access_Check_Failed)); | |
7395 | ||
7396 | Mark_Non_Null; | |
fa7497e8 | 7397 | end Install_Null_Excluding_Check; |
7398 | ||
ee6ba406 | 7399 | -------------------------- |
7400 | -- Install_Static_Check -- | |
7401 | -------------------------- | |
7402 | ||
7403 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
cda40848 | 7404 | Stat : constant Boolean := Is_OK_Static_Expression (R_Cno); |
ee6ba406 | 7405 | Typ : constant Entity_Id := Etype (R_Cno); |
7406 | ||
7407 | begin | |
f15731c4 | 7408 | Rewrite (R_Cno, |
7409 | Make_Raise_Constraint_Error (Loc, | |
7410 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 7411 | Set_Analyzed (R_Cno); |
7412 | Set_Etype (R_Cno, Typ); | |
7413 | Set_Raises_Constraint_Error (R_Cno); | |
7414 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 7415 | |
7416 | -- Now deal with possible local raise handling | |
7417 | ||
7418 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 7419 | end Install_Static_Check; |
7420 | ||
3cce7f32 | 7421 | ------------------------- |
7422 | -- Is_Check_Suppressed -- | |
7423 | ------------------------- | |
7424 | ||
7425 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
7426 | Ptr : Suppress_Stack_Entry_Ptr; | |
7427 | ||
7428 | begin | |
7429 | -- First search the local entity suppress stack. We search this from the | |
7430 | -- top of the stack down so that we get the innermost entry that applies | |
7431 | -- to this case if there are nested entries. | |
7432 | ||
7433 | Ptr := Local_Suppress_Stack_Top; | |
7434 | while Ptr /= null loop | |
7435 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
7436 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
7437 | then | |
7438 | return Ptr.Suppress; | |
7439 | end if; | |
7440 | ||
7441 | Ptr := Ptr.Prev; | |
7442 | end loop; | |
7443 | ||
7444 | -- Now search the global entity suppress table for a matching entry. | |
7445 | -- We also search this from the top down so that if there are multiple | |
7446 | -- pragmas for the same entity, the last one applies (not clear what | |
7447 | -- or whether the RM specifies this handling, but it seems reasonable). | |
7448 | ||
7449 | Ptr := Global_Suppress_Stack_Top; | |
7450 | while Ptr /= null loop | |
7451 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
7452 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
7453 | then | |
7454 | return Ptr.Suppress; | |
7455 | end if; | |
7456 | ||
7457 | Ptr := Ptr.Prev; | |
7458 | end loop; | |
7459 | ||
7460 | -- If we did not find a matching entry, then use the normal scope | |
7461 | -- suppress value after all (actually this will be the global setting | |
7462 | -- since it clearly was not overridden at any point). For a predefined | |
7463 | -- check, we test the specific flag. For a user defined check, we check | |
7464 | -- the All_Checks flag. The Overflow flag requires special handling to | |
7465 | -- deal with the General vs Assertion case | |
7466 | ||
7467 | if C = Overflow_Check then | |
7468 | return Overflow_Checks_Suppressed (Empty); | |
7469 | elsif C in Predefined_Check_Id then | |
7470 | return Scope_Suppress.Suppress (C); | |
7471 | else | |
7472 | return Scope_Suppress.Suppress (All_Checks); | |
7473 | end if; | |
7474 | end Is_Check_Suppressed; | |
7475 | ||
9dfe12ae | 7476 | --------------------- |
7477 | -- Kill_All_Checks -- | |
7478 | --------------------- | |
7479 | ||
7480 | procedure Kill_All_Checks is | |
7481 | begin | |
7482 | if Debug_Flag_CC then | |
7483 | w ("Kill_All_Checks"); | |
7484 | end if; | |
7485 | ||
feff2f05 | 7486 | -- We reset the number of saved checks to zero, and also modify all |
7487 | -- stack entries for statement ranges to indicate that the number of | |
7488 | -- checks at each level is now zero. | |
9dfe12ae | 7489 | |
7490 | Num_Saved_Checks := 0; | |
7491 | ||
96da3284 | 7492 | -- Note: the Int'Min here avoids any possibility of J being out of |
7493 | -- range when called from e.g. Conditional_Statements_Begin. | |
7494 | ||
7495 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 7496 | Saved_Checks_Stack (J) := 0; |
7497 | end loop; | |
7498 | end Kill_All_Checks; | |
7499 | ||
7500 | ----------------- | |
7501 | -- Kill_Checks -- | |
7502 | ----------------- | |
7503 | ||
7504 | procedure Kill_Checks (V : Entity_Id) is | |
7505 | begin | |
7506 | if Debug_Flag_CC then | |
7507 | w ("Kill_Checks for entity", Int (V)); | |
7508 | end if; | |
7509 | ||
7510 | for J in 1 .. Num_Saved_Checks loop | |
7511 | if Saved_Checks (J).Entity = V then | |
7512 | if Debug_Flag_CC then | |
7513 | w (" Checks killed for saved check ", J); | |
7514 | end if; | |
7515 | ||
7516 | Saved_Checks (J).Killed := True; | |
7517 | end if; | |
7518 | end loop; | |
7519 | end Kill_Checks; | |
7520 | ||
ee6ba406 | 7521 | ------------------------------ |
7522 | -- Length_Checks_Suppressed -- | |
7523 | ------------------------------ | |
7524 | ||
7525 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7526 | begin | |
9dfe12ae | 7527 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7528 | return Is_Check_Suppressed (E, Length_Check); | |
7529 | else | |
fafc6b97 | 7530 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 7531 | end if; |
ee6ba406 | 7532 | end Length_Checks_Suppressed; |
7533 | ||
3cce7f32 | 7534 | ----------------------- |
7535 | -- Make_Bignum_Block -- | |
7536 | ----------------------- | |
7537 | ||
7538 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
7539 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
3cce7f32 | 7540 | begin |
7541 | return | |
7542 | Make_Block_Statement (Loc, | |
97c15ab0 | 7543 | Declarations => |
7544 | New_List (Build_SS_Mark_Call (Loc, M)), | |
3cce7f32 | 7545 | Handled_Statement_Sequence => |
7546 | Make_Handled_Sequence_Of_Statements (Loc, | |
97c15ab0 | 7547 | Statements => New_List (Build_SS_Release_Call (Loc, M)))); |
3cce7f32 | 7548 | end Make_Bignum_Block; |
7549 | ||
0df9d43f | 7550 | ---------------------------------- |
7551 | -- Minimize_Eliminate_Overflows -- | |
7552 | ---------------------------------- | |
3cce7f32 | 7553 | |
f32c377d | 7554 | -- This is a recursive routine that is called at the top of an expression |
7555 | -- tree to properly process overflow checking for a whole subtree by making | |
7556 | -- recursive calls to process operands. This processing may involve the use | |
7557 | -- of bignum or long long integer arithmetic, which will change the types | |
7558 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 7559 | -- it would interfere with semantic analysis). |
f32c377d | 7560 | |
21a55437 | 7561 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 7562 | -- the operator expansion routines, as well as the expansion routines for |
7563 | -- if/case expression, do nothing (for the moment) except call the routine | |
7564 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
7565 | -- routine does nothing for non top-level nodes, so at the point where the | |
7566 | -- call is made for the top level node, the entire expression subtree has | |
7567 | -- not been expanded, or processed for overflow. All that has to happen as | |
7568 | -- a result of the top level call to this routine. | |
f32c377d | 7569 | |
7570 | -- As noted above, the overflow processing works by making recursive calls | |
7571 | -- for the operands, and figuring out what to do, based on the processing | |
7572 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
7573 | -- to be done in bignum mode), and the determined ranges of the operands. | |
7574 | ||
7575 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 7576 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 7577 | -- the node (if it has been modified by the overflow check processing). The |
7578 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
7579 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 7580 | -- for this call is that the overflow handling mode must be temporarily set |
7581 | -- to STRICT. | |
f32c377d | 7582 | |
0df9d43f | 7583 | procedure Minimize_Eliminate_Overflows |
61016a7a | 7584 | (N : Node_Id; |
7585 | Lo : out Uint; | |
7586 | Hi : out Uint; | |
7587 | Top_Level : Boolean) | |
3cce7f32 | 7588 | is |
0326b4d4 | 7589 | Rtyp : constant Entity_Id := Etype (N); |
7590 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
7591 | -- Result type, must be a signed integer type | |
3cce7f32 | 7592 | |
db415383 | 7593 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 7594 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
7595 | ||
7596 | Loc : constant Source_Ptr := Sloc (N); | |
7597 | ||
7598 | Rlo, Rhi : Uint; | |
0326b4d4 | 7599 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 7600 | |
7601 | Llo, Lhi : Uint; | |
0326b4d4 | 7602 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 7603 | |
49b3a812 | 7604 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
7605 | -- Operands and results are of this type when we convert | |
7606 | ||
0326b4d4 | 7607 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
7608 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 7609 | -- Bounds of Long_Long_Integer |
7610 | ||
7611 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
7612 | -- Indicates binary operator case | |
7613 | ||
7614 | OK : Boolean; | |
7615 | -- Used in call to Determine_Range | |
7616 | ||
61016a7a | 7617 | Bignum_Operands : Boolean; |
7618 | -- Set True if one or more operands is already of type Bignum, meaning | |
7619 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 7620 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 7621 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 7622 | |
7623 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 7624 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 7625 | -- which means that if the result is known to be in the result type |
7626 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 7627 | |
7628 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
7629 | -- This is called when we have modified the node and we therefore need | |
7630 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
7631 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
39a0c1d3 | 7632 | -- we would reenter this routine recursively which would not be good. |
0df9d43f | 7633 | -- The argument Suppress is set True if we also want to suppress |
7634 | -- overflow checking for the reexpansion (this is set when we know | |
7635 | -- overflow is not possible). Typ is the type for the reanalysis. | |
7636 | ||
7637 | procedure Reexpand (Suppress : Boolean := False); | |
7638 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
7639 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
7640 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
7641 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
7642 | -- Note that skipping reanalysis is not just an optimization, testing | |
7643 | -- has showed up several complex cases in which reanalyzing an already | |
7644 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 7645 | |
0326b4d4 | 7646 | function In_Result_Range return Boolean; |
7647 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 7648 | |
2fe22c69 | 7649 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 7650 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 7651 | |
7652 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 7653 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 7654 | |
0326b4d4 | 7655 | --------------------- |
7656 | -- In_Result_Range -- | |
7657 | --------------------- | |
7658 | ||
7659 | function In_Result_Range return Boolean is | |
7660 | begin | |
f32c377d | 7661 | if Lo = No_Uint or else Hi = No_Uint then |
7662 | return False; | |
7663 | ||
cda40848 | 7664 | elsif Is_OK_Static_Subtype (Etype (N)) then |
0326b4d4 | 7665 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
7666 | and then | |
7667 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 7668 | |
0326b4d4 | 7669 | else |
7670 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
7671 | and then | |
7672 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
7673 | end if; | |
7674 | end In_Result_Range; | |
7675 | ||
2fe22c69 | 7676 | --------- |
7677 | -- Max -- | |
7678 | --------- | |
7679 | ||
7680 | procedure Max (A : in out Uint; B : Uint) is | |
7681 | begin | |
7682 | if A = No_Uint or else B > A then | |
7683 | A := B; | |
7684 | end if; | |
7685 | end Max; | |
7686 | ||
7687 | --------- | |
7688 | -- Min -- | |
7689 | --------- | |
7690 | ||
7691 | procedure Min (A : in out Uint; B : Uint) is | |
7692 | begin | |
7693 | if A = No_Uint or else B < A then | |
7694 | A := B; | |
7695 | end if; | |
7696 | end Min; | |
7697 | ||
0df9d43f | 7698 | --------------- |
7699 | -- Reanalyze -- | |
7700 | --------------- | |
7701 | ||
7702 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 7703 | Svg : constant Overflow_Mode_Type := |
7704 | Scope_Suppress.Overflow_Mode_General; | |
7705 | Sva : constant Overflow_Mode_Type := | |
7706 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7707 | Svo : constant Boolean := |
7708 | Scope_Suppress.Suppress (Overflow_Check); | |
7709 | ||
7710 | begin | |
db415383 | 7711 | Scope_Suppress.Overflow_Mode_General := Strict; |
7712 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 7713 | |
7714 | if Suppress then | |
7715 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7716 | end if; | |
7717 | ||
7718 | Analyze_And_Resolve (N, Typ); | |
7719 | ||
7720 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7721 | Scope_Suppress.Overflow_Mode_General := Svg; |
7722 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 7723 | end Reanalyze; |
7724 | ||
4fb5f0a0 | 7725 | -------------- |
7726 | -- Reexpand -- | |
7727 | -------------- | |
7728 | ||
0df9d43f | 7729 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 7730 | Svg : constant Overflow_Mode_Type := |
7731 | Scope_Suppress.Overflow_Mode_General; | |
7732 | Sva : constant Overflow_Mode_Type := | |
7733 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7734 | Svo : constant Boolean := |
7735 | Scope_Suppress.Suppress (Overflow_Check); | |
7736 | ||
4fb5f0a0 | 7737 | begin |
db415383 | 7738 | Scope_Suppress.Overflow_Mode_General := Strict; |
7739 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 7740 | Set_Analyzed (N, False); |
0df9d43f | 7741 | |
7742 | if Suppress then | |
7743 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7744 | end if; | |
7745 | ||
4fb5f0a0 | 7746 | Expand (N); |
0df9d43f | 7747 | |
7748 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7749 | Scope_Suppress.Overflow_Mode_General := Svg; |
7750 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 7751 | end Reexpand; |
7752 | ||
0df9d43f | 7753 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 7754 | |
3cce7f32 | 7755 | begin |
0326b4d4 | 7756 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 7757 | |
7758 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
7759 | ||
7760 | -- Use the normal Determine_Range routine to get the range. We | |
7761 | -- don't require operands to be valid, invalid values may result in | |
7762 | -- rubbish results where the result has not been properly checked for | |
39a0c1d3 | 7763 | -- overflow, that's fine. |
3cce7f32 | 7764 | |
7765 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
7766 | ||
21a55437 | 7767 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 7768 | -- clear but might as well protect), use type bounds. |
7769 | ||
7770 | if not OK then | |
7771 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
7772 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
7773 | end if; | |
7774 | ||
7775 | -- If we don't have a binary operator, all we have to do is to set | |
20cf157b | 7776 | -- the Hi/Lo range, so we are done. |
3cce7f32 | 7777 | |
7778 | return; | |
7779 | ||
0326b4d4 | 7780 | -- Processing for if expression |
7781 | ||
92f1631f | 7782 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 7783 | declare |
7784 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
7785 | Else_DE : constant Node_Id := Next (Then_DE); | |
7786 | ||
7787 | begin | |
7788 | Bignum_Operands := False; | |
7789 | ||
0df9d43f | 7790 | Minimize_Eliminate_Overflows |
0326b4d4 | 7791 | (Then_DE, Lo, Hi, Top_Level => False); |
7792 | ||
7793 | if Lo = No_Uint then | |
7794 | Bignum_Operands := True; | |
7795 | end if; | |
7796 | ||
0df9d43f | 7797 | Minimize_Eliminate_Overflows |
0326b4d4 | 7798 | (Else_DE, Rlo, Rhi, Top_Level => False); |
7799 | ||
7800 | if Rlo = No_Uint then | |
7801 | Bignum_Operands := True; | |
7802 | else | |
7803 | Long_Long_Integer_Operands := | |
7804 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
7805 | ||
7806 | Min (Lo, Rlo); | |
7807 | Max (Hi, Rhi); | |
7808 | end if; | |
7809 | ||
21a55437 | 7810 | -- If at least one of our operands is now Bignum, we must rebuild |
7811 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 7812 | -- rebuilt if expression with overflow checks off, since once we |
39a0c1d3 | 7813 | -- are in bignum mode, we are all done with overflow checks. |
0326b4d4 | 7814 | |
7815 | if Bignum_Operands then | |
7816 | Rewrite (N, | |
92f1631f | 7817 | Make_If_Expression (Loc, |
0326b4d4 | 7818 | Expressions => New_List ( |
7819 | Remove_Head (Expressions (N)), | |
7820 | Convert_To_Bignum (Then_DE), | |
7821 | Convert_To_Bignum (Else_DE)), | |
7822 | Is_Elsif => Is_Elsif (N))); | |
7823 | ||
0df9d43f | 7824 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 7825 | |
7826 | -- If we have no Long_Long_Integer operands, then we are in result | |
7827 | -- range, since it means that none of our operands felt the need | |
7828 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 7829 | -- converted to long long integer or bignum). We reexpand to |
7830 | -- complete the expansion of the if expression (but we do not | |
7831 | -- need to reanalyze). | |
0326b4d4 | 7832 | |
7833 | elsif not Long_Long_Integer_Operands then | |
7834 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7835 | Reexpand; |
0326b4d4 | 7836 | |
7837 | -- Otherwise convert us to long long integer mode. Note that we | |
7838 | -- don't need any further overflow checking at this level. | |
7839 | ||
7840 | else | |
7841 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
7842 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
7843 | Set_Etype (N, LLIB); | |
f32c377d | 7844 | |
7845 | -- Now reanalyze with overflow checks off | |
7846 | ||
0326b4d4 | 7847 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7848 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 7849 | end if; |
7850 | end; | |
7851 | ||
7852 | return; | |
7853 | ||
7854 | -- Here for case expression | |
7855 | ||
7856 | elsif Nkind (N) = N_Case_Expression then | |
7857 | Bignum_Operands := False; | |
7858 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 7859 | |
7860 | declare | |
f32c377d | 7861 | Alt : Node_Id; |
0326b4d4 | 7862 | |
7863 | begin | |
7864 | -- Loop through expressions applying recursive call | |
7865 | ||
7866 | Alt := First (Alternatives (N)); | |
7867 | while Present (Alt) loop | |
7868 | declare | |
7869 | Aexp : constant Node_Id := Expression (Alt); | |
7870 | ||
7871 | begin | |
0df9d43f | 7872 | Minimize_Eliminate_Overflows |
0326b4d4 | 7873 | (Aexp, Lo, Hi, Top_Level => False); |
7874 | ||
7875 | if Lo = No_Uint then | |
7876 | Bignum_Operands := True; | |
7877 | elsif Etype (Aexp) = LLIB then | |
7878 | Long_Long_Integer_Operands := True; | |
7879 | end if; | |
7880 | end; | |
7881 | ||
7882 | Next (Alt); | |
7883 | end loop; | |
7884 | ||
7885 | -- If we have no bignum or long long integer operands, it means | |
7886 | -- that none of our dependent expressions could raise overflow. | |
7887 | -- In this case, we simply return with no changes except for | |
7888 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 7889 | -- checks for this node. We will reexpand to get the needed |
7890 | -- expansion for the case expression, but we do not need to | |
21a55437 | 7891 | -- reanalyze, since nothing has changed. |
0326b4d4 | 7892 | |
f32c377d | 7893 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 7894 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7895 | Reexpand (Suppress => True); |
0326b4d4 | 7896 | |
7897 | -- Otherwise we are going to rebuild the case expression using | |
7898 | -- either bignum or long long integer operands throughout. | |
7899 | ||
7900 | else | |
f32c377d | 7901 | declare |
7902 | Rtype : Entity_Id; | |
7903 | New_Alts : List_Id; | |
7904 | New_Exp : Node_Id; | |
7905 | ||
7906 | begin | |
7907 | New_Alts := New_List; | |
7908 | Alt := First (Alternatives (N)); | |
7909 | while Present (Alt) loop | |
7910 | if Bignum_Operands then | |
7911 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
7912 | Rtype := RTE (RE_Bignum); | |
7913 | else | |
7914 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
7915 | Rtype := LLIB; | |
7916 | end if; | |
0326b4d4 | 7917 | |
f32c377d | 7918 | Append_To (New_Alts, |
7919 | Make_Case_Expression_Alternative (Sloc (Alt), | |
7920 | Actions => No_List, | |
7921 | Discrete_Choices => Discrete_Choices (Alt), | |
7922 | Expression => New_Exp)); | |
0326b4d4 | 7923 | |
f32c377d | 7924 | Next (Alt); |
7925 | end loop; | |
0326b4d4 | 7926 | |
f32c377d | 7927 | Rewrite (N, |
7928 | Make_Case_Expression (Loc, | |
7929 | Expression => Expression (N), | |
7930 | Alternatives => New_Alts)); | |
0326b4d4 | 7931 | |
0df9d43f | 7932 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 7933 | end; |
0326b4d4 | 7934 | end if; |
7935 | end; | |
7936 | ||
7937 | return; | |
7938 | end if; | |
7939 | ||
7940 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 7941 | -- operands to get the ranges (and to properly process the subtree |
20cf157b | 7942 | -- that lies below us). |
3cce7f32 | 7943 | |
0df9d43f | 7944 | Minimize_Eliminate_Overflows |
0326b4d4 | 7945 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 7946 | |
0326b4d4 | 7947 | if Binary then |
0df9d43f | 7948 | Minimize_Eliminate_Overflows |
0326b4d4 | 7949 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 7950 | end if; |
7951 | ||
f32c377d | 7952 | -- Record if we have Long_Long_Integer operands |
7953 | ||
7954 | Long_Long_Integer_Operands := | |
7955 | Etype (Right_Opnd (N)) = LLIB | |
7956 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
7957 | ||
7958 | -- If either operand is a bignum, then result will be a bignum and we | |
7959 | -- don't need to do any range analysis. As previously discussed we could | |
7960 | -- do range analysis in such cases, but it could mean working with giant | |
7961 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 7962 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 7963 | |
7964 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
7965 | Lo := No_Uint; | |
7966 | Hi := No_Uint; | |
61016a7a | 7967 | Bignum_Operands := True; |
3cce7f32 | 7968 | |
7969 | -- Otherwise compute result range | |
7970 | ||
7971 | else | |
61016a7a | 7972 | Bignum_Operands := False; |
7973 | ||
3cce7f32 | 7974 | case Nkind (N) is |
7975 | ||
7976 | -- Absolute value | |
7977 | ||
7978 | when N_Op_Abs => | |
7979 | Lo := Uint_0; | |
de922300 | 7980 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 7981 | |
7982 | -- Addition | |
7983 | ||
7984 | when N_Op_Add => | |
7985 | Lo := Llo + Rlo; | |
7986 | Hi := Lhi + Rhi; | |
7987 | ||
7988 | -- Division | |
7989 | ||
7990 | when N_Op_Divide => | |
2fe22c69 | 7991 | |
5f4275e1 | 7992 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 7993 | |
5f4275e1 | 7994 | if Rlo = 0 and then Rhi = 0 then |
7995 | Lo := Uint_0; | |
7996 | Hi := Uint_0; | |
2fe22c69 | 7997 | |
5f4275e1 | 7998 | -- Possible bounds of division must come from dividing end |
7999 | -- values of the input ranges (four possibilities), provided | |
8000 | -- zero is not included in the possible values of the right | |
8001 | -- operand. | |
8002 | ||
8003 | -- Otherwise, we just consider two intervals of values for | |
8004 | -- the right operand: the interval of negative values (up to | |
8005 | -- -1) and the interval of positive values (starting at 1). | |
8006 | -- Since division by 1 is the identity, and division by -1 | |
8007 | -- is negation, we get all possible bounds of division in that | |
8008 | -- case by considering: | |
8009 | -- - all values from the division of end values of input | |
8010 | -- ranges; | |
8011 | -- - the end values of the left operand; | |
8012 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 8013 | |
5f4275e1 | 8014 | else |
8015 | declare | |
8016 | Mrk : constant Uintp.Save_Mark := Mark; | |
8017 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 8018 | |
5f4275e1 | 8019 | Ev1 : Uint; |
8020 | Ev2 : Uint; | |
8021 | Ev3 : Uint; | |
8022 | Ev4 : Uint; | |
2fe22c69 | 8023 | |
5f4275e1 | 8024 | begin |
8025 | -- Discard extreme values of zero for the divisor, since | |
8026 | -- they will simply result in an exception in any case. | |
2fe22c69 | 8027 | |
5f4275e1 | 8028 | if Rlo = 0 then |
8029 | Rlo := Uint_1; | |
8030 | elsif Rhi = 0 then | |
8031 | Rhi := -Uint_1; | |
2fe22c69 | 8032 | end if; |
2fe22c69 | 8033 | |
5f4275e1 | 8034 | -- Compute possible bounds coming from dividing end |
8035 | -- values of the input ranges. | |
2fe22c69 | 8036 | |
5f4275e1 | 8037 | Ev1 := Llo / Rlo; |
8038 | Ev2 := Llo / Rhi; | |
8039 | Ev3 := Lhi / Rlo; | |
8040 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 8041 | |
5f4275e1 | 8042 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
8043 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 8044 | |
5f4275e1 | 8045 | -- If the right operand can be both negative or positive, |
8046 | -- include the end values of the left operand in the | |
8047 | -- extreme values, as well as their negation. | |
2fe22c69 | 8048 | |
5f4275e1 | 8049 | if Rlo < 0 and then Rhi > 0 then |
8050 | Ev1 := Llo; | |
8051 | Ev2 := -Llo; | |
8052 | Ev3 := Lhi; | |
8053 | Ev4 := -Lhi; | |
2fe22c69 | 8054 | |
5f4275e1 | 8055 | Min (Lo, |
8056 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
8057 | Max (Hi, | |
8058 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 8059 | end if; |
2fe22c69 | 8060 | |
5f4275e1 | 8061 | -- Release the RR and Ev values |
2fe22c69 | 8062 | |
5f4275e1 | 8063 | Release_And_Save (Mrk, Lo, Hi); |
8064 | end; | |
8065 | end if; | |
3cce7f32 | 8066 | |
8067 | -- Exponentiation | |
8068 | ||
8069 | when N_Op_Expon => | |
de922300 | 8070 | |
8071 | -- Discard negative values for the exponent, since they will | |
8072 | -- simply result in an exception in any case. | |
8073 | ||
8074 | if Rhi < 0 then | |
8075 | Rhi := Uint_0; | |
8076 | elsif Rlo < 0 then | |
8077 | Rlo := Uint_0; | |
8078 | end if; | |
8079 | ||
8080 | -- Estimate number of bits in result before we go computing | |
8081 | -- giant useless bounds. Basically the number of bits in the | |
8082 | -- result is the number of bits in the base multiplied by the | |
8083 | -- value of the exponent. If this is big enough that the result | |
8084 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
8085 | -- mode immediately, and avoid computing giant bounds. | |
8086 | ||
8087 | -- The comparison here is approximate, but conservative, it | |
8088 | -- only clicks on cases that are sure to exceed the bounds. | |
8089 | ||
8090 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
8091 | Lo := No_Uint; | |
8092 | Hi := No_Uint; | |
8093 | ||
8094 | -- If right operand is zero then result is 1 | |
8095 | ||
8096 | elsif Rhi = 0 then | |
8097 | Lo := Uint_1; | |
8098 | Hi := Uint_1; | |
8099 | ||
8100 | else | |
8101 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 8102 | -- positive value to largest exponent value, or from |
8103 | -- the exponentiation of most negative value to an | |
8104 | -- even exponent. | |
de922300 | 8105 | |
8106 | declare | |
8107 | Hi1, Hi2 : Uint; | |
8108 | ||
8109 | begin | |
5f4275e1 | 8110 | if Lhi > 0 then |
de922300 | 8111 | Hi1 := Lhi ** Rhi; |
8112 | else | |
8113 | Hi1 := Uint_0; | |
8114 | end if; | |
8115 | ||
8116 | if Llo < 0 then | |
8117 | if Rhi mod 2 = 0 then | |
de922300 | 8118 | Hi2 := Llo ** Rhi; |
5f4275e1 | 8119 | else |
8120 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 8121 | end if; |
8122 | else | |
8123 | Hi2 := Uint_0; | |
8124 | end if; | |
8125 | ||
8126 | Hi := UI_Max (Hi1, Hi2); | |
8127 | end; | |
8128 | ||
8129 | -- Result can only be negative if base can be negative | |
8130 | ||
8131 | if Llo < 0 then | |
21a55437 | 8132 | if Rhi mod 2 = 0 then |
de922300 | 8133 | Lo := Llo ** (Rhi - 1); |
8134 | else | |
8135 | Lo := Llo ** Rhi; | |
8136 | end if; | |
8137 | ||
21a55437 | 8138 | -- Otherwise low bound is minimum ** minimum |
de922300 | 8139 | |
8140 | else | |
8141 | Lo := Llo ** Rlo; | |
8142 | end if; | |
8143 | end if; | |
3cce7f32 | 8144 | |
8145 | -- Negation | |
8146 | ||
8147 | when N_Op_Minus => | |
8148 | Lo := -Rhi; | |
8149 | Hi := -Rlo; | |
8150 | ||
8151 | -- Mod | |
8152 | ||
8153 | when N_Op_Mod => | |
2fe22c69 | 8154 | declare |
5f4275e1 | 8155 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 8156 | -- This is the maximum absolute value of the result |
8157 | ||
8158 | begin | |
8159 | Lo := Uint_0; | |
8160 | Hi := Uint_0; | |
8161 | ||
8162 | -- The result depends only on the sign and magnitude of | |
8163 | -- the right operand, it does not depend on the sign or | |
8164 | -- magnitude of the left operand. | |
8165 | ||
8166 | if Rlo < 0 then | |
8167 | Lo := -Maxabs; | |
8168 | end if; | |
8169 | ||
8170 | if Rhi > 0 then | |
8171 | Hi := Maxabs; | |
8172 | end if; | |
8173 | end; | |
3cce7f32 | 8174 | |
8175 | -- Multiplication | |
8176 | ||
8177 | when N_Op_Multiply => | |
49b3a812 | 8178 | |
8179 | -- Possible bounds of multiplication must come from multiplying | |
8180 | -- end values of the input ranges (four possibilities). | |
8181 | ||
8182 | declare | |
8183 | Mrk : constant Uintp.Save_Mark := Mark; | |
8184 | -- Mark so we can release the Ev values | |
8185 | ||
8186 | Ev1 : constant Uint := Llo * Rlo; | |
8187 | Ev2 : constant Uint := Llo * Rhi; | |
8188 | Ev3 : constant Uint := Lhi * Rlo; | |
8189 | Ev4 : constant Uint := Lhi * Rhi; | |
8190 | ||
8191 | begin | |
8192 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
8193 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
8194 | ||
8195 | -- Release the Ev values | |
8196 | ||
8197 | Release_And_Save (Mrk, Lo, Hi); | |
8198 | end; | |
3cce7f32 | 8199 | |
8200 | -- Plus operator (affirmation) | |
8201 | ||
8202 | when N_Op_Plus => | |
8203 | Lo := Rlo; | |
8204 | Hi := Rhi; | |
8205 | ||
8206 | -- Remainder | |
8207 | ||
8208 | when N_Op_Rem => | |
2fe22c69 | 8209 | declare |
5f4275e1 | 8210 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 8211 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 8212 | -- that the result range does not depend on the sign of the |
8213 | -- right operand. | |
2fe22c69 | 8214 | |
8215 | begin | |
8216 | Lo := Uint_0; | |
8217 | Hi := Uint_0; | |
8218 | ||
8219 | -- Case of left operand negative, which results in a range | |
8220 | -- of -Maxabs .. 0 for those negative values. If there are | |
8221 | -- no negative values then Lo value of result is always 0. | |
8222 | ||
8223 | if Llo < 0 then | |
8224 | Lo := -Maxabs; | |
8225 | end if; | |
8226 | ||
8227 | -- Case of left operand positive | |
8228 | ||
8229 | if Lhi > 0 then | |
8230 | Hi := Maxabs; | |
8231 | end if; | |
8232 | end; | |
3cce7f32 | 8233 | |
8234 | -- Subtract | |
8235 | ||
8236 | when N_Op_Subtract => | |
8237 | Lo := Llo - Rhi; | |
8238 | Hi := Lhi - Rlo; | |
8239 | ||
8240 | -- Nothing else should be possible | |
8241 | ||
8242 | when others => | |
8243 | raise Program_Error; | |
3cce7f32 | 8244 | end case; |
8245 | end if; | |
8246 | ||
4fb5f0a0 | 8247 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 8248 | -- operands or long long integer operands), and we know the result. |
8249 | -- If we know we are in the result range, and we do not have Bignum | |
8250 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
8251 | -- overflow checks turned off (since we know we cannot have overflow). | |
8252 | -- As always the reexpansion is required to complete expansion of the | |
8253 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
8254 | -- by suppressing the check. | |
f32c377d | 8255 | |
8256 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
8257 | and then In_Result_Range | |
8258 | then | |
8259 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 8260 | Reexpand (Suppress => True); |
f32c377d | 8261 | return; |
8262 | ||
8263 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 8264 | -- case we will move into either the Bignum or Long_Long_Integer domain |
8265 | -- to compute the result. However, there is one exception. If we are | |
8266 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
8267 | -- operands, we will have to immediately convert the result back to | |
8268 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
8269 | -- fiddling. | |
f32c377d | 8270 | |
8271 | elsif Top_Level | |
8272 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 8273 | |
8274 | -- One further refinement. If we are at the top level, but our parent | |
8275 | -- is a type conversion, then go into bignum or long long integer node | |
8276 | -- since the result will be converted to that type directly without | |
8277 | -- going through the result type, and we may avoid an overflow. This | |
8278 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
8279 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
8280 | -- but does not fit in Integer. | |
8281 | ||
8282 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 8283 | then |
0df9d43f | 8284 | -- Here keep original types, but we need to complete analysis |
f32c377d | 8285 | |
8286 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 8287 | -- here because it will cause recursion into the whole MINIMIZED/ |
8288 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 8289 | -- we are at the top level, and we need a check against the result |
39a0c1d3 | 8290 | -- mode (i.e. we want to use STRICT mode). So do exactly that. |
4fb5f0a0 | 8291 | -- Also, we have not modified the node, so this is a case where |
8292 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 8293 | |
0df9d43f | 8294 | Reexpand; |
f32c377d | 8295 | return; |
8296 | ||
8297 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 8298 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 8299 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
8300 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 8301 | |
8302 | -- Note: we could do better here and in some cases switch back from | |
8303 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
8304 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
8305 | -- Failing to do this switching back is only an efficiency issue. | |
8306 | ||
f32c377d | 8307 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 8308 | |
61016a7a | 8309 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 8310 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 8311 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
8312 | ||
8313 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
8314 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
8315 | -- into Bignum mode, but there is an exception if neither of our | |
8316 | -- operands is Bignum now, and we are at the top level (Top_Level | |
8317 | -- set True). In this case, there is no point in moving into Bignum | |
8318 | -- mode to prevent overflow if the caller will immediately convert | |
8319 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 8320 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 8321 | |
8322 | if Check_Mode = Minimized | |
8323 | or else (Top_Level and not Bignum_Operands) | |
8324 | then | |
0df9d43f | 8325 | if Do_Overflow_Check (N) then |
8326 | Enable_Overflow_Check (N); | |
8327 | end if; | |
3cce7f32 | 8328 | |
0df9d43f | 8329 | -- The result now has to be in Long_Long_Integer mode, so adjust |
8330 | -- the possible range to reflect this. Note these calls also | |
8331 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 8332 | |
8333 | Max (Lo, LLLo); | |
8334 | Min (Hi, LLHi); | |
8335 | ||
8336 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 8337 | |
8338 | else | |
8339 | pragma Assert (Check_Mode = Eliminated); | |
8340 | ||
8341 | declare | |
8342 | Fent : Entity_Id; | |
8343 | Args : List_Id; | |
8344 | ||
8345 | begin | |
8346 | case Nkind (N) is | |
8347 | when N_Op_Abs => | |
8348 | Fent := RTE (RE_Big_Abs); | |
8349 | ||
8350 | when N_Op_Add => | |
8351 | Fent := RTE (RE_Big_Add); | |
8352 | ||
8353 | when N_Op_Divide => | |
8354 | Fent := RTE (RE_Big_Div); | |
8355 | ||
8356 | when N_Op_Expon => | |
8357 | Fent := RTE (RE_Big_Exp); | |
8358 | ||
8359 | when N_Op_Minus => | |
8360 | Fent := RTE (RE_Big_Neg); | |
8361 | ||
8362 | when N_Op_Mod => | |
8363 | Fent := RTE (RE_Big_Mod); | |
8364 | ||
8365 | when N_Op_Multiply => | |
8366 | Fent := RTE (RE_Big_Mul); | |
8367 | ||
8368 | when N_Op_Rem => | |
8369 | Fent := RTE (RE_Big_Rem); | |
8370 | ||
8371 | when N_Op_Subtract => | |
8372 | Fent := RTE (RE_Big_Sub); | |
8373 | ||
8374 | -- Anything else is an internal error, this includes the | |
8375 | -- N_Op_Plus case, since how can plus cause the result | |
8376 | -- to be out of range if the operand is in range? | |
8377 | ||
8378 | when others => | |
8379 | raise Program_Error; | |
8380 | end case; | |
8381 | ||
8382 | -- Construct argument list for Bignum call, converting our | |
8383 | -- operands to Bignum form if they are not already there. | |
8384 | ||
8385 | Args := New_List; | |
8386 | ||
8387 | if Binary then | |
8388 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
8389 | end if; | |
8390 | ||
8391 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
8392 | ||
8393 | -- Now rewrite the arithmetic operator with a call to the | |
8394 | -- corresponding bignum function. | |
8395 | ||
8396 | Rewrite (N, | |
8397 | Make_Function_Call (Loc, | |
8398 | Name => New_Occurrence_Of (Fent, Loc), | |
8399 | Parameter_Associations => Args)); | |
0df9d43f | 8400 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 8401 | |
8402 | -- Indicate result is Bignum mode | |
8403 | ||
8404 | Lo := No_Uint; | |
8405 | Hi := No_Uint; | |
de922300 | 8406 | return; |
3cce7f32 | 8407 | end; |
8408 | end if; | |
8409 | ||
8410 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 8411 | -- check is required, at least not yet. |
3cce7f32 | 8412 | |
8413 | else | |
de922300 | 8414 | Set_Do_Overflow_Check (N, False); |
8415 | end if; | |
3cce7f32 | 8416 | |
f32c377d | 8417 | -- Here we are not in Bignum territory, but we may have long long |
8418 | -- integer operands that need special handling. First a special check: | |
8419 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
8420 | -- it means we converted it to prevent overflow, but exponentiation | |
8421 | -- requires a Natural right operand, so convert it back to Natural. | |
8422 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 8423 | |
f32c377d | 8424 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
8425 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 8426 | end if; |
8427 | ||
de922300 | 8428 | -- Here we will do the operation in Long_Long_Integer. We do this even |
8429 | -- if we know an overflow check is required, better to do this in long | |
39a0c1d3 | 8430 | -- long integer mode, since we are less likely to overflow. |
3cce7f32 | 8431 | |
de922300 | 8432 | -- Convert right or only operand to Long_Long_Integer, except that |
8433 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 8434 | |
de922300 | 8435 | if Nkind (N) /= N_Op_Expon then |
8436 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
8437 | end if; | |
3cce7f32 | 8438 | |
de922300 | 8439 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 8440 | |
de922300 | 8441 | if Binary then |
8442 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
8443 | end if; | |
8444 | ||
8445 | -- Reset node to unanalyzed | |
8446 | ||
8447 | Set_Analyzed (N, False); | |
8448 | Set_Etype (N, Empty); | |
8449 | Set_Entity (N, Empty); | |
8450 | ||
2fe22c69 | 8451 | -- Now analyze this new node. This reanalysis will complete processing |
8452 | -- for the node. In particular we will complete the expansion of an | |
8453 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
8454 | -- we will complete any division checks (since we have not changed the | |
8455 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 8456 | |
0df9d43f | 8457 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
39a0c1d3 | 8458 | -- MINIMIZED/ELIMINATED handling, since we are now done with that. |
3cce7f32 | 8459 | |
0df9d43f | 8460 | declare |
db415383 | 8461 | SG : constant Overflow_Mode_Type := |
8462 | Scope_Suppress.Overflow_Mode_General; | |
8463 | SA : constant Overflow_Mode_Type := | |
8464 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 8465 | |
0df9d43f | 8466 | begin |
db415383 | 8467 | Scope_Suppress.Overflow_Mode_General := Strict; |
8468 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 8469 | |
0df9d43f | 8470 | if not Do_Overflow_Check (N) then |
8471 | Reanalyze (LLIB, Suppress => True); | |
8472 | else | |
8473 | Reanalyze (LLIB); | |
8474 | end if; | |
8475 | ||
db415383 | 8476 | Scope_Suppress.Overflow_Mode_General := SG; |
8477 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 8478 | end; |
8479 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 8480 | |
8481 | ------------------------- | |
8482 | -- Overflow_Check_Mode -- | |
8483 | ------------------------- | |
8484 | ||
db415383 | 8485 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 8486 | begin |
724d2bd8 | 8487 | if In_Assertion_Expr = 0 then |
db415383 | 8488 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 8489 | else |
db415383 | 8490 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 8491 | end if; |
3cce7f32 | 8492 | end Overflow_Check_Mode; |
8493 | ||
8494 | -------------------------------- | |
8495 | -- Overflow_Checks_Suppressed -- | |
8496 | -------------------------------- | |
8497 | ||
8498 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8499 | begin | |
0df9d43f | 8500 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
8501 | return Is_Check_Suppressed (E, Overflow_Check); | |
8502 | else | |
8503 | return Scope_Suppress.Suppress (Overflow_Check); | |
8504 | end if; | |
ee6ba406 | 8505 | end Overflow_Checks_Suppressed; |
fc75802a | 8506 | |
37baba83 | 8507 | --------------------------------- |
8508 | -- Predicate_Checks_Suppressed -- | |
8509 | --------------------------------- | |
8510 | ||
8511 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8512 | begin | |
8513 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
8514 | return Is_Check_Suppressed (E, Predicate_Check); | |
8515 | else | |
8516 | return Scope_Suppress.Suppress (Predicate_Check); | |
8517 | end if; | |
8518 | end Predicate_Checks_Suppressed; | |
8519 | ||
ee6ba406 | 8520 | ----------------------------- |
8521 | -- Range_Checks_Suppressed -- | |
8522 | ----------------------------- | |
8523 | ||
8524 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8525 | begin | |
9dfe12ae | 8526 | if Present (E) then |
ce4d14c4 | 8527 | if Kill_Range_Checks (E) then |
9dfe12ae | 8528 | return True; |
5f46de53 | 8529 | |
9dfe12ae | 8530 | elsif Checks_May_Be_Suppressed (E) then |
8531 | return Is_Check_Suppressed (E, Range_Check); | |
8532 | end if; | |
8533 | end if; | |
ee6ba406 | 8534 | |
fafc6b97 | 8535 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 8536 | end Range_Checks_Suppressed; |
8537 | ||
0577b0b1 | 8538 | ----------------------------------------- |
8539 | -- Range_Or_Validity_Checks_Suppressed -- | |
8540 | ----------------------------------------- | |
8541 | ||
8542 | -- Note: the coding would be simpler here if we simply made appropriate | |
8543 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
8544 | -- duplicated checks which we prefer to avoid. | |
8545 | ||
8546 | function Range_Or_Validity_Checks_Suppressed | |
8547 | (Expr : Node_Id) return Boolean | |
8548 | is | |
8549 | begin | |
8550 | -- Immediate return if scope checks suppressed for either check | |
8551 | ||
fafc6b97 | 8552 | if Scope_Suppress.Suppress (Range_Check) |
8553 | or | |
8554 | Scope_Suppress.Suppress (Validity_Check) | |
8555 | then | |
0577b0b1 | 8556 | return True; |
8557 | end if; | |
8558 | ||
8559 | -- If no expression, that's odd, decide that checks are suppressed, | |
8560 | -- since we don't want anyone trying to do checks in this case, which | |
8561 | -- is most likely the result of some other error. | |
8562 | ||
8563 | if No (Expr) then | |
8564 | return True; | |
8565 | end if; | |
8566 | ||
8567 | -- Expression is present, so perform suppress checks on type | |
8568 | ||
8569 | declare | |
8570 | Typ : constant Entity_Id := Etype (Expr); | |
8571 | begin | |
ce4d14c4 | 8572 | if Checks_May_Be_Suppressed (Typ) |
0577b0b1 | 8573 | and then (Is_Check_Suppressed (Typ, Range_Check) |
8574 | or else | |
8575 | Is_Check_Suppressed (Typ, Validity_Check)) | |
8576 | then | |
8577 | return True; | |
8578 | end if; | |
8579 | end; | |
8580 | ||
8581 | -- If expression is an entity name, perform checks on this entity | |
8582 | ||
8583 | if Is_Entity_Name (Expr) then | |
8584 | declare | |
8585 | Ent : constant Entity_Id := Entity (Expr); | |
8586 | begin | |
8587 | if Checks_May_Be_Suppressed (Ent) then | |
8588 | return Is_Check_Suppressed (Ent, Range_Check) | |
8589 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
8590 | end if; | |
8591 | end; | |
8592 | end if; | |
8593 | ||
8594 | -- If we fall through, no checks suppressed | |
8595 | ||
8596 | return False; | |
8597 | end Range_Or_Validity_Checks_Suppressed; | |
8598 | ||
226494a3 | 8599 | ------------------- |
8600 | -- Remove_Checks -- | |
8601 | ------------------- | |
8602 | ||
8603 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 8604 | function Process (N : Node_Id) return Traverse_Result; |
8605 | -- Process a single node during the traversal | |
8606 | ||
8f6e4fd5 | 8607 | procedure Traverse is new Traverse_Proc (Process); |
8608 | -- The traversal procedure itself | |
226494a3 | 8609 | |
8610 | ------------- | |
8611 | -- Process -- | |
8612 | ------------- | |
8613 | ||
8614 | function Process (N : Node_Id) return Traverse_Result is | |
8615 | begin | |
8616 | if Nkind (N) not in N_Subexpr then | |
8617 | return Skip; | |
8618 | end if; | |
8619 | ||
8620 | Set_Do_Range_Check (N, False); | |
8621 | ||
8622 | case Nkind (N) is | |
8623 | when N_And_Then => | |
8f6e4fd5 | 8624 | Traverse (Left_Opnd (N)); |
226494a3 | 8625 | return Skip; |
8626 | ||
8627 | when N_Attribute_Reference => | |
226494a3 | 8628 | Set_Do_Overflow_Check (N, False); |
8629 | ||
226494a3 | 8630 | when N_Function_Call => |
8631 | Set_Do_Tag_Check (N, False); | |
8632 | ||
226494a3 | 8633 | when N_Op => |
8634 | Set_Do_Overflow_Check (N, False); | |
8635 | ||
8636 | case Nkind (N) is | |
8637 | when N_Op_Divide => | |
8638 | Set_Do_Division_Check (N, False); | |
8639 | ||
8640 | when N_Op_And => | |
8641 | Set_Do_Length_Check (N, False); | |
8642 | ||
8643 | when N_Op_Mod => | |
8644 | Set_Do_Division_Check (N, False); | |
8645 | ||
8646 | when N_Op_Or => | |
8647 | Set_Do_Length_Check (N, False); | |
8648 | ||
8649 | when N_Op_Rem => | |
8650 | Set_Do_Division_Check (N, False); | |
8651 | ||
8652 | when N_Op_Xor => | |
8653 | Set_Do_Length_Check (N, False); | |
8654 | ||
8655 | when others => | |
8656 | null; | |
8657 | end case; | |
8658 | ||
8659 | when N_Or_Else => | |
8f6e4fd5 | 8660 | Traverse (Left_Opnd (N)); |
226494a3 | 8661 | return Skip; |
8662 | ||
8663 | when N_Selected_Component => | |
226494a3 | 8664 | Set_Do_Discriminant_Check (N, False); |
8665 | ||
226494a3 | 8666 | when N_Type_Conversion => |
9dfe12ae | 8667 | Set_Do_Length_Check (N, False); |
8668 | Set_Do_Tag_Check (N, False); | |
226494a3 | 8669 | Set_Do_Overflow_Check (N, False); |
226494a3 | 8670 | |
8671 | when others => | |
8672 | null; | |
8673 | end case; | |
8674 | ||
8675 | return OK; | |
8676 | end Process; | |
8677 | ||
8678 | -- Start of processing for Remove_Checks | |
8679 | ||
8680 | begin | |
8f6e4fd5 | 8681 | Traverse (Expr); |
226494a3 | 8682 | end Remove_Checks; |
8683 | ||
ee6ba406 | 8684 | ---------------------------- |
8685 | -- Selected_Length_Checks -- | |
8686 | ---------------------------- | |
8687 | ||
8688 | function Selected_Length_Checks | |
8689 | (Ck_Node : Node_Id; | |
8690 | Target_Typ : Entity_Id; | |
8691 | Source_Typ : Entity_Id; | |
314a23b6 | 8692 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8693 | is |
8694 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8695 | S_Typ : Entity_Id; | |
8696 | T_Typ : Entity_Id; | |
8697 | Expr_Actual : Node_Id; | |
8698 | Exptyp : Entity_Id; | |
8699 | Cond : Node_Id := Empty; | |
8700 | Do_Access : Boolean := False; | |
8701 | Wnode : Node_Id := Warn_Node; | |
8702 | Ret_Result : Check_Result := (Empty, Empty); | |
8703 | Num_Checks : Natural := 0; | |
8704 | ||
8705 | procedure Add_Check (N : Node_Id); | |
8706 | -- Adds the action given to Ret_Result if N is non-Empty | |
8707 | ||
8708 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
8709 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 8710 | -- Comments required ??? |
ee6ba406 | 8711 | |
8712 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
8713 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 8714 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 8715 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 8716 | -- obviously superfluous checks. |
ee6ba406 | 8717 | |
8718 | function Length_E_Cond | |
8719 | (Exptyp : Entity_Id; | |
8720 | Typ : Entity_Id; | |
314a23b6 | 8721 | Indx : Nat) return Node_Id; |
ee6ba406 | 8722 | -- Returns expression to compute: |
8723 | -- Typ'Length /= Exptyp'Length | |
8724 | ||
8725 | function Length_N_Cond | |
8726 | (Expr : Node_Id; | |
8727 | Typ : Entity_Id; | |
314a23b6 | 8728 | Indx : Nat) return Node_Id; |
ee6ba406 | 8729 | -- Returns expression to compute: |
8730 | -- Typ'Length /= Expr'Length | |
8731 | ||
8732 | --------------- | |
8733 | -- Add_Check -- | |
8734 | --------------- | |
8735 | ||
8736 | procedure Add_Check (N : Node_Id) is | |
8737 | begin | |
8738 | if Present (N) then | |
8739 | ||
20cf157b | 8740 | -- For now, ignore attempt to place more than two checks ??? |
8741 | -- This is really worrisome, are we really discarding checks ??? | |
ee6ba406 | 8742 | |
8743 | if Num_Checks = 2 then | |
8744 | return; | |
8745 | end if; | |
8746 | ||
8747 | pragma Assert (Num_Checks <= 1); | |
8748 | Num_Checks := Num_Checks + 1; | |
8749 | Ret_Result (Num_Checks) := N; | |
8750 | end if; | |
8751 | end Add_Check; | |
8752 | ||
8753 | ------------------ | |
8754 | -- Get_E_Length -- | |
8755 | ------------------ | |
8756 | ||
8757 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 8758 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 8759 | N : Node_Id; |
8760 | E1 : Entity_Id := E; | |
ee6ba406 | 8761 | |
8762 | begin | |
8763 | if Ekind (Scope (E)) = E_Record_Type | |
8764 | and then Has_Discriminants (Scope (E)) | |
8765 | then | |
8766 | N := Build_Discriminal_Subtype_Of_Component (E); | |
8767 | ||
8768 | if Present (N) then | |
8769 | Insert_Action (Ck_Node, N); | |
8770 | E1 := Defining_Identifier (N); | |
8771 | end if; | |
8772 | end if; | |
8773 | ||
8774 | if Ekind (E1) = E_String_Literal_Subtype then | |
8775 | return | |
8776 | Make_Integer_Literal (Loc, | |
8777 | Intval => String_Literal_Length (E1)); | |
8778 | ||
00c403ee | 8779 | elsif SE /= Standard_Standard |
8780 | and then Ekind (Scope (SE)) = E_Protected_Type | |
8781 | and then Has_Discriminants (Scope (SE)) | |
8782 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 8783 | and then not Inside_Init_Proc |
8784 | then | |
ee6ba406 | 8785 | -- If the type whose length is needed is a private component |
8786 | -- constrained by a discriminant, we must expand the 'Length | |
8787 | -- attribute into an explicit computation, using the discriminal | |
8788 | -- of the current protected operation. This is because the actual | |
8789 | -- type of the prival is constructed after the protected opera- | |
8790 | -- tion has been fully expanded. | |
8791 | ||
8792 | declare | |
8793 | Indx_Type : Node_Id; | |
8794 | Lo : Node_Id; | |
8795 | Hi : Node_Id; | |
8796 | Do_Expand : Boolean := False; | |
8797 | ||
8798 | begin | |
8799 | Indx_Type := First_Index (E); | |
8800 | ||
8801 | for J in 1 .. Indx - 1 loop | |
8802 | Next_Index (Indx_Type); | |
8803 | end loop; | |
8804 | ||
2af58f67 | 8805 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 8806 | |
8807 | if Nkind (Lo) = N_Identifier | |
8808 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
8809 | then | |
8810 | Lo := Get_Discriminal (E, Lo); | |
8811 | Do_Expand := True; | |
8812 | end if; | |
8813 | ||
8814 | if Nkind (Hi) = N_Identifier | |
8815 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
8816 | then | |
8817 | Hi := Get_Discriminal (E, Hi); | |
8818 | Do_Expand := True; | |
8819 | end if; | |
8820 | ||
8821 | if Do_Expand then | |
8822 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 8823 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 8824 | end if; |
8825 | ||
8826 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 8827 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 8828 | end if; |
8829 | ||
8830 | N := | |
8831 | Make_Op_Add (Loc, | |
8832 | Left_Opnd => | |
8833 | Make_Op_Subtract (Loc, | |
8834 | Left_Opnd => Hi, | |
8835 | Right_Opnd => Lo), | |
8836 | ||
8837 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
8838 | return N; | |
8839 | ||
8840 | else | |
8841 | N := | |
8842 | Make_Attribute_Reference (Loc, | |
8843 | Attribute_Name => Name_Length, | |
8844 | Prefix => | |
8845 | New_Occurrence_Of (E1, Loc)); | |
8846 | ||
8847 | if Indx > 1 then | |
8848 | Set_Expressions (N, New_List ( | |
8849 | Make_Integer_Literal (Loc, Indx))); | |
8850 | end if; | |
8851 | ||
8852 | return N; | |
8853 | end if; | |
8854 | end; | |
8855 | ||
8856 | else | |
8857 | N := | |
8858 | Make_Attribute_Reference (Loc, | |
8859 | Attribute_Name => Name_Length, | |
8860 | Prefix => | |
8861 | New_Occurrence_Of (E1, Loc)); | |
8862 | ||
8863 | if Indx > 1 then | |
8864 | Set_Expressions (N, New_List ( | |
8865 | Make_Integer_Literal (Loc, Indx))); | |
8866 | end if; | |
8867 | ||
8868 | return N; | |
ee6ba406 | 8869 | end if; |
8870 | end Get_E_Length; | |
8871 | ||
8872 | ------------------ | |
8873 | -- Get_N_Length -- | |
8874 | ------------------ | |
8875 | ||
8876 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
8877 | begin | |
8878 | return | |
8879 | Make_Attribute_Reference (Loc, | |
8880 | Attribute_Name => Name_Length, | |
8881 | Prefix => | |
9dfe12ae | 8882 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8883 | Expressions => New_List ( |
8884 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8885 | end Get_N_Length; |
8886 | ||
8887 | ------------------- | |
8888 | -- Length_E_Cond -- | |
8889 | ------------------- | |
8890 | ||
8891 | function Length_E_Cond | |
8892 | (Exptyp : Entity_Id; | |
8893 | Typ : Entity_Id; | |
314a23b6 | 8894 | Indx : Nat) return Node_Id |
ee6ba406 | 8895 | is |
8896 | begin | |
8897 | return | |
8898 | Make_Op_Ne (Loc, | |
8899 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8900 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 8901 | end Length_E_Cond; |
8902 | ||
8903 | ------------------- | |
8904 | -- Length_N_Cond -- | |
8905 | ------------------- | |
8906 | ||
8907 | function Length_N_Cond | |
8908 | (Expr : Node_Id; | |
8909 | Typ : Entity_Id; | |
314a23b6 | 8910 | Indx : Nat) return Node_Id |
ee6ba406 | 8911 | is |
8912 | begin | |
8913 | return | |
8914 | Make_Op_Ne (Loc, | |
8915 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8916 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 8917 | end Length_N_Cond; |
8918 | ||
feff2f05 | 8919 | ----------------- |
8920 | -- Same_Bounds -- | |
8921 | ----------------- | |
8922 | ||
ee6ba406 | 8923 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
8924 | begin | |
8925 | return | |
8926 | (Nkind (L) = N_Integer_Literal | |
8927 | and then Nkind (R) = N_Integer_Literal | |
8928 | and then Intval (L) = Intval (R)) | |
8929 | ||
8930 | or else | |
8931 | (Is_Entity_Name (L) | |
8932 | and then Ekind (Entity (L)) = E_Constant | |
8933 | and then ((Is_Entity_Name (R) | |
8934 | and then Entity (L) = Entity (R)) | |
8935 | or else | |
8936 | (Nkind (R) = N_Type_Conversion | |
8937 | and then Is_Entity_Name (Expression (R)) | |
8938 | and then Entity (L) = Entity (Expression (R))))) | |
8939 | ||
8940 | or else | |
8941 | (Is_Entity_Name (R) | |
8942 | and then Ekind (Entity (R)) = E_Constant | |
8943 | and then Nkind (L) = N_Type_Conversion | |
8944 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 8945 | and then Entity (R) = Entity (Expression (L))) |
8946 | ||
8947 | or else | |
8948 | (Is_Entity_Name (L) | |
8949 | and then Is_Entity_Name (R) | |
8950 | and then Entity (L) = Entity (R) | |
8951 | and then Ekind (Entity (L)) = E_In_Parameter | |
8952 | and then Inside_Init_Proc); | |
ee6ba406 | 8953 | end Same_Bounds; |
8954 | ||
8955 | -- Start of processing for Selected_Length_Checks | |
8956 | ||
8957 | begin | |
a33565dd | 8958 | if not Expander_Active then |
ee6ba406 | 8959 | return Ret_Result; |
8960 | end if; | |
8961 | ||
8962 | if Target_Typ = Any_Type | |
8963 | or else Target_Typ = Any_Composite | |
8964 | or else Raises_Constraint_Error (Ck_Node) | |
8965 | then | |
8966 | return Ret_Result; | |
8967 | end if; | |
8968 | ||
8969 | if No (Wnode) then | |
8970 | Wnode := Ck_Node; | |
8971 | end if; | |
8972 | ||
8973 | T_Typ := Target_Typ; | |
8974 | ||
8975 | if No (Source_Typ) then | |
8976 | S_Typ := Etype (Ck_Node); | |
8977 | else | |
8978 | S_Typ := Source_Typ; | |
8979 | end if; | |
8980 | ||
8981 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8982 | return Ret_Result; | |
8983 | end if; | |
8984 | ||
8985 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8986 | S_Typ := Designated_Type (S_Typ); | |
8987 | T_Typ := Designated_Type (T_Typ); | |
8988 | Do_Access := True; | |
8989 | ||
2af58f67 | 8990 | -- A simple optimization for the null case |
ee6ba406 | 8991 | |
2af58f67 | 8992 | if Known_Null (Ck_Node) then |
ee6ba406 | 8993 | return Ret_Result; |
8994 | end if; | |
8995 | end if; | |
8996 | ||
8997 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
8998 | if Is_Constrained (T_Typ) then | |
8999 | ||
92f1631f | 9000 | -- The checking code to be generated will freeze the corresponding |
9001 | -- array type. However, we must freeze the type now, so that the | |
9002 | -- freeze node does not appear within the generated if expression, | |
9003 | -- but ahead of it. | |
ee6ba406 | 9004 | |
9005 | Freeze_Before (Ck_Node, T_Typ); | |
9006 | ||
9007 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 9008 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 9009 | |
9010 | if Is_Access_Type (Exptyp) then | |
9011 | Exptyp := Designated_Type (Exptyp); | |
9012 | end if; | |
9013 | ||
9014 | -- String_Literal case. This needs to be handled specially be- | |
9015 | -- cause no index types are available for string literals. The | |
9016 | -- condition is simply: | |
9017 | ||
9018 | -- T_Typ'Length = string-literal-length | |
9019 | ||
9dfe12ae | 9020 | if Nkind (Expr_Actual) = N_String_Literal |
9021 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
9022 | then | |
ee6ba406 | 9023 | Cond := |
9024 | Make_Op_Ne (Loc, | |
9025 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
9026 | Right_Opnd => | |
9027 | Make_Integer_Literal (Loc, | |
9028 | Intval => | |
9029 | String_Literal_Length (Etype (Expr_Actual)))); | |
9030 | ||
9031 | -- General array case. Here we have a usable actual subtype for | |
9032 | -- the expression, and the condition is built from the two types | |
9033 | -- (Do_Length): | |
9034 | ||
9035 | -- T_Typ'Length /= Exptyp'Length or else | |
9036 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
9037 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
9038 | -- ... | |
9039 | ||
9040 | elsif Is_Constrained (Exptyp) then | |
9041 | declare | |
9dfe12ae | 9042 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9043 | ||
9044 | L_Index : Node_Id; | |
9045 | R_Index : Node_Id; | |
9046 | L_Low : Node_Id; | |
9047 | L_High : Node_Id; | |
9048 | R_Low : Node_Id; | |
9049 | R_High : Node_Id; | |
ee6ba406 | 9050 | L_Length : Uint; |
9051 | R_Length : Uint; | |
9dfe12ae | 9052 | Ref_Node : Node_Id; |
ee6ba406 | 9053 | |
9054 | begin | |
feff2f05 | 9055 | -- At the library level, we need to ensure that the type of |
9056 | -- the object is elaborated before the check itself is | |
9057 | -- emitted. This is only done if the object is in the | |
9058 | -- current compilation unit, otherwise the type is frozen | |
9059 | -- and elaborated in its unit. | |
9dfe12ae | 9060 | |
9061 | if Is_Itype (Exptyp) | |
9062 | and then | |
9063 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
9064 | and then | |
9065 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 9066 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 9067 | then |
9068 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
9069 | Set_Itype (Ref_Node, Exptyp); | |
9070 | Insert_Action (Ck_Node, Ref_Node); | |
9071 | end if; | |
9072 | ||
ee6ba406 | 9073 | L_Index := First_Index (T_Typ); |
9074 | R_Index := First_Index (Exptyp); | |
9075 | ||
9076 | for Indx in 1 .. Ndims loop | |
9077 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9078 | or else |
9079 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9080 | then |
9081 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
9082 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
9083 | ||
9084 | -- Deal with compile time length check. Note that we | |
9085 | -- skip this in the access case, because the access | |
9086 | -- value may be null, so we cannot know statically. | |
9087 | ||
9088 | if not Do_Access | |
9089 | and then Compile_Time_Known_Value (L_Low) | |
9090 | and then Compile_Time_Known_Value (L_High) | |
9091 | and then Compile_Time_Known_Value (R_Low) | |
9092 | and then Compile_Time_Known_Value (R_High) | |
9093 | then | |
9094 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
9095 | L_Length := Expr_Value (L_High) - | |
9096 | Expr_Value (L_Low) + 1; | |
9097 | else | |
9098 | L_Length := UI_From_Int (0); | |
9099 | end if; | |
9100 | ||
9101 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
9102 | R_Length := Expr_Value (R_High) - | |
9103 | Expr_Value (R_Low) + 1; | |
9104 | else | |
9105 | R_Length := UI_From_Int (0); | |
9106 | end if; | |
9107 | ||
9108 | if L_Length > R_Length then | |
9109 | Add_Check | |
9110 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9111 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 9112 | |
9113 | elsif L_Length < R_Length then | |
9114 | Add_Check | |
9115 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9116 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 9117 | end if; |
9118 | ||
9119 | -- The comparison for an individual index subtype | |
9120 | -- is omitted if the corresponding index subtypes | |
9121 | -- statically match, since the result is known to | |
9122 | -- be true. Note that this test is worth while even | |
9123 | -- though we do static evaluation, because non-static | |
9124 | -- subtypes can statically match. | |
9125 | ||
9126 | elsif not | |
9127 | Subtypes_Statically_Match | |
9128 | (Etype (L_Index), Etype (R_Index)) | |
9129 | ||
9130 | and then not | |
9131 | (Same_Bounds (L_Low, R_Low) | |
9132 | and then Same_Bounds (L_High, R_High)) | |
9133 | then | |
9134 | Evolve_Or_Else | |
9135 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
9136 | end if; | |
9137 | ||
9138 | Next (L_Index); | |
9139 | Next (R_Index); | |
9140 | end if; | |
9141 | end loop; | |
9142 | end; | |
9143 | ||
9144 | -- Handle cases where we do not get a usable actual subtype that | |
9145 | -- is constrained. This happens for example in the function call | |
9146 | -- and explicit dereference cases. In these cases, we have to get | |
9147 | -- the length or range from the expression itself, making sure we | |
9148 | -- do not evaluate it more than once. | |
9149 | ||
9150 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 9151 | -- result of applying Duplicate_Expr to the original tree, forcing |
9152 | -- the result to be a name. | |
ee6ba406 | 9153 | |
9154 | else | |
9155 | declare | |
9dfe12ae | 9156 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9157 | |
9158 | begin | |
9159 | -- Build the condition for the explicit dereference case | |
9160 | ||
9161 | for Indx in 1 .. Ndims loop | |
9162 | Evolve_Or_Else | |
9163 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
9164 | end loop; | |
9165 | end; | |
9166 | end if; | |
9167 | end if; | |
9168 | end if; | |
9169 | ||
9170 | -- Construct the test and insert into the tree | |
9171 | ||
9172 | if Present (Cond) then | |
9173 | if Do_Access then | |
9174 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9175 | end if; | |
9176 | ||
f15731c4 | 9177 | Add_Check |
9178 | (Make_Raise_Constraint_Error (Loc, | |
9179 | Condition => Cond, | |
9180 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 9181 | end if; |
9182 | ||
9183 | return Ret_Result; | |
ee6ba406 | 9184 | end Selected_Length_Checks; |
9185 | ||
9186 | --------------------------- | |
9187 | -- Selected_Range_Checks -- | |
9188 | --------------------------- | |
9189 | ||
9190 | function Selected_Range_Checks | |
9191 | (Ck_Node : Node_Id; | |
9192 | Target_Typ : Entity_Id; | |
9193 | Source_Typ : Entity_Id; | |
314a23b6 | 9194 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 9195 | is |
9196 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
9197 | S_Typ : Entity_Id; | |
9198 | T_Typ : Entity_Id; | |
9199 | Expr_Actual : Node_Id; | |
9200 | Exptyp : Entity_Id; | |
9201 | Cond : Node_Id := Empty; | |
9202 | Do_Access : Boolean := False; | |
9203 | Wnode : Node_Id := Warn_Node; | |
9204 | Ret_Result : Check_Result := (Empty, Empty); | |
9205 | Num_Checks : Integer := 0; | |
9206 | ||
9207 | procedure Add_Check (N : Node_Id); | |
9208 | -- Adds the action given to Ret_Result if N is non-Empty | |
9209 | ||
9210 | function Discrete_Range_Cond | |
9211 | (Expr : Node_Id; | |
314a23b6 | 9212 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 9213 | -- Returns expression to compute: |
9214 | -- Low_Bound (Expr) < Typ'First | |
9215 | -- or else | |
9216 | -- High_Bound (Expr) > Typ'Last | |
9217 | ||
9218 | function Discrete_Expr_Cond | |
9219 | (Expr : Node_Id; | |
314a23b6 | 9220 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 9221 | -- Returns expression to compute: |
9222 | -- Expr < Typ'First | |
9223 | -- or else | |
9224 | -- Expr > Typ'Last | |
9225 | ||
9226 | function Get_E_First_Or_Last | |
3cb12758 | 9227 | (Loc : Source_Ptr; |
9228 | E : Entity_Id; | |
ee6ba406 | 9229 | Indx : Nat; |
314a23b6 | 9230 | Nam : Name_Id) return Node_Id; |
79212397 | 9231 | -- Returns an attribute reference |
ee6ba406 | 9232 | -- E'First or E'Last |
79212397 | 9233 | -- with a source location of Loc. |
f73ee678 | 9234 | -- |
79212397 | 9235 | -- Nam is Name_First or Name_Last, according to which attribute is |
9236 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
9237 | -- Expressions of the attribute reference (identifying the desired | |
9238 | -- array dimension). | |
ee6ba406 | 9239 | |
9240 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
9241 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
9242 | -- Returns expression to compute: | |
9dfe12ae | 9243 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 9244 | |
9245 | function Range_E_Cond | |
9246 | (Exptyp : Entity_Id; | |
9247 | Typ : Entity_Id; | |
9248 | Indx : Nat) | |
9249 | return Node_Id; | |
9250 | -- Returns expression to compute: | |
9251 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
9252 | ||
9253 | function Range_Equal_E_Cond | |
9254 | (Exptyp : Entity_Id; | |
9255 | Typ : Entity_Id; | |
314a23b6 | 9256 | Indx : Nat) return Node_Id; |
ee6ba406 | 9257 | -- Returns expression to compute: |
9258 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
9259 | ||
9260 | function Range_N_Cond | |
9261 | (Expr : Node_Id; | |
9262 | Typ : Entity_Id; | |
314a23b6 | 9263 | Indx : Nat) return Node_Id; |
ee6ba406 | 9264 | -- Return expression to compute: |
9265 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
9266 | ||
9267 | --------------- | |
9268 | -- Add_Check -- | |
9269 | --------------- | |
9270 | ||
9271 | procedure Add_Check (N : Node_Id) is | |
9272 | begin | |
9273 | if Present (N) then | |
9274 | ||
9275 | -- For now, ignore attempt to place more than 2 checks ??? | |
9276 | ||
9277 | if Num_Checks = 2 then | |
9278 | return; | |
9279 | end if; | |
9280 | ||
9281 | pragma Assert (Num_Checks <= 1); | |
9282 | Num_Checks := Num_Checks + 1; | |
9283 | Ret_Result (Num_Checks) := N; | |
9284 | end if; | |
9285 | end Add_Check; | |
9286 | ||
9287 | ------------------------- | |
9288 | -- Discrete_Expr_Cond -- | |
9289 | ------------------------- | |
9290 | ||
9291 | function Discrete_Expr_Cond | |
9292 | (Expr : Node_Id; | |
314a23b6 | 9293 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 9294 | is |
9295 | begin | |
9296 | return | |
9297 | Make_Or_Else (Loc, | |
9298 | Left_Opnd => | |
9299 | Make_Op_Lt (Loc, | |
9300 | Left_Opnd => | |
9dfe12ae | 9301 | Convert_To (Base_Type (Typ), |
9302 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 9303 | Right_Opnd => |
9304 | Convert_To (Base_Type (Typ), | |
3cb12758 | 9305 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 9306 | |
9307 | Right_Opnd => | |
9308 | Make_Op_Gt (Loc, | |
9309 | Left_Opnd => | |
9dfe12ae | 9310 | Convert_To (Base_Type (Typ), |
9311 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 9312 | Right_Opnd => |
9313 | Convert_To | |
9314 | (Base_Type (Typ), | |
3cb12758 | 9315 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 9316 | end Discrete_Expr_Cond; |
9317 | ||
9318 | ------------------------- | |
9319 | -- Discrete_Range_Cond -- | |
9320 | ------------------------- | |
9321 | ||
9322 | function Discrete_Range_Cond | |
9323 | (Expr : Node_Id; | |
314a23b6 | 9324 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 9325 | is |
9326 | LB : Node_Id := Low_Bound (Expr); | |
9327 | HB : Node_Id := High_Bound (Expr); | |
9328 | ||
9329 | Left_Opnd : Node_Id; | |
9330 | Right_Opnd : Node_Id; | |
9331 | ||
9332 | begin | |
9333 | if Nkind (LB) = N_Identifier | |
feff2f05 | 9334 | and then Ekind (Entity (LB)) = E_Discriminant |
9335 | then | |
ee6ba406 | 9336 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
9337 | end if; | |
9338 | ||
ee6ba406 | 9339 | Left_Opnd := |
9340 | Make_Op_Lt (Loc, | |
9341 | Left_Opnd => | |
9342 | Convert_To | |
9dfe12ae | 9343 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 9344 | |
9345 | Right_Opnd => | |
9346 | Convert_To | |
3cb12758 | 9347 | (Base_Type (Typ), |
9348 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 9349 | |
ba9b1a39 | 9350 | if Nkind (HB) = N_Identifier |
9351 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 9352 | then |
ba9b1a39 | 9353 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 9354 | end if; |
9355 | ||
9356 | Right_Opnd := | |
9357 | Make_Op_Gt (Loc, | |
9358 | Left_Opnd => | |
9359 | Convert_To | |
9dfe12ae | 9360 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 9361 | |
9362 | Right_Opnd => | |
9363 | Convert_To | |
9364 | (Base_Type (Typ), | |
3cb12758 | 9365 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 9366 | |
9367 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
9368 | end Discrete_Range_Cond; | |
9369 | ||
9370 | ------------------------- | |
9371 | -- Get_E_First_Or_Last -- | |
9372 | ------------------------- | |
9373 | ||
9374 | function Get_E_First_Or_Last | |
3cb12758 | 9375 | (Loc : Source_Ptr; |
9376 | E : Entity_Id; | |
ee6ba406 | 9377 | Indx : Nat; |
314a23b6 | 9378 | Nam : Name_Id) return Node_Id |
ee6ba406 | 9379 | is |
3cb12758 | 9380 | Exprs : List_Id; |
ee6ba406 | 9381 | begin |
3cb12758 | 9382 | if Indx > 0 then |
9383 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 9384 | else |
3cb12758 | 9385 | Exprs := No_List; |
ee6ba406 | 9386 | end if; |
9387 | ||
3cb12758 | 9388 | return Make_Attribute_Reference (Loc, |
9389 | Prefix => New_Occurrence_Of (E, Loc), | |
9390 | Attribute_Name => Nam, | |
9391 | Expressions => Exprs); | |
ee6ba406 | 9392 | end Get_E_First_Or_Last; |
9393 | ||
9394 | ----------------- | |
9395 | -- Get_N_First -- | |
9396 | ----------------- | |
9397 | ||
9398 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
9399 | begin | |
9400 | return | |
9401 | Make_Attribute_Reference (Loc, | |
9402 | Attribute_Name => Name_First, | |
9403 | Prefix => | |
9dfe12ae | 9404 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 9405 | Expressions => New_List ( |
9406 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 9407 | end Get_N_First; |
9408 | ||
9409 | ---------------- | |
9410 | -- Get_N_Last -- | |
9411 | ---------------- | |
9412 | ||
9413 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
9414 | begin | |
9415 | return | |
9416 | Make_Attribute_Reference (Loc, | |
9417 | Attribute_Name => Name_Last, | |
9418 | Prefix => | |
9dfe12ae | 9419 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 9420 | Expressions => New_List ( |
9421 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 9422 | end Get_N_Last; |
9423 | ||
9424 | ------------------ | |
9425 | -- Range_E_Cond -- | |
9426 | ------------------ | |
9427 | ||
9428 | function Range_E_Cond | |
9429 | (Exptyp : Entity_Id; | |
9430 | Typ : Entity_Id; | |
314a23b6 | 9431 | Indx : Nat) return Node_Id |
ee6ba406 | 9432 | is |
9433 | begin | |
9434 | return | |
9435 | Make_Or_Else (Loc, | |
9436 | Left_Opnd => | |
9437 | Make_Op_Lt (Loc, | |
3cb12758 | 9438 | Left_Opnd => |
9439 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
9440 | Right_Opnd => | |
9441 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 9442 | |
9443 | Right_Opnd => | |
9444 | Make_Op_Gt (Loc, | |
3cb12758 | 9445 | Left_Opnd => |
9446 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
9447 | Right_Opnd => | |
9448 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 9449 | end Range_E_Cond; |
9450 | ||
9451 | ------------------------ | |
9452 | -- Range_Equal_E_Cond -- | |
9453 | ------------------------ | |
9454 | ||
9455 | function Range_Equal_E_Cond | |
9456 | (Exptyp : Entity_Id; | |
9457 | Typ : Entity_Id; | |
314a23b6 | 9458 | Indx : Nat) return Node_Id |
ee6ba406 | 9459 | is |
9460 | begin | |
9461 | return | |
9462 | Make_Or_Else (Loc, | |
9463 | Left_Opnd => | |
9464 | Make_Op_Ne (Loc, | |
3cb12758 | 9465 | Left_Opnd => |
9466 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
9467 | Right_Opnd => | |
9468 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
9469 | ||
ee6ba406 | 9470 | Right_Opnd => |
9471 | Make_Op_Ne (Loc, | |
3cb12758 | 9472 | Left_Opnd => |
9473 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
9474 | Right_Opnd => | |
9475 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 9476 | end Range_Equal_E_Cond; |
9477 | ||
9478 | ------------------ | |
9479 | -- Range_N_Cond -- | |
9480 | ------------------ | |
9481 | ||
9482 | function Range_N_Cond | |
9483 | (Expr : Node_Id; | |
9484 | Typ : Entity_Id; | |
314a23b6 | 9485 | Indx : Nat) return Node_Id |
ee6ba406 | 9486 | is |
9487 | begin | |
9488 | return | |
9489 | Make_Or_Else (Loc, | |
9490 | Left_Opnd => | |
9491 | Make_Op_Lt (Loc, | |
3cb12758 | 9492 | Left_Opnd => |
9493 | Get_N_First (Expr, Indx), | |
9494 | Right_Opnd => | |
9495 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 9496 | |
9497 | Right_Opnd => | |
9498 | Make_Op_Gt (Loc, | |
3cb12758 | 9499 | Left_Opnd => |
9500 | Get_N_Last (Expr, Indx), | |
9501 | Right_Opnd => | |
9502 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 9503 | end Range_N_Cond; |
9504 | ||
9505 | -- Start of processing for Selected_Range_Checks | |
9506 | ||
9507 | begin | |
a33565dd | 9508 | if not Expander_Active then |
ee6ba406 | 9509 | return Ret_Result; |
9510 | end if; | |
9511 | ||
9512 | if Target_Typ = Any_Type | |
9513 | or else Target_Typ = Any_Composite | |
9514 | or else Raises_Constraint_Error (Ck_Node) | |
9515 | then | |
9516 | return Ret_Result; | |
9517 | end if; | |
9518 | ||
9519 | if No (Wnode) then | |
9520 | Wnode := Ck_Node; | |
9521 | end if; | |
9522 | ||
9523 | T_Typ := Target_Typ; | |
9524 | ||
9525 | if No (Source_Typ) then | |
9526 | S_Typ := Etype (Ck_Node); | |
9527 | else | |
9528 | S_Typ := Source_Typ; | |
9529 | end if; | |
9530 | ||
9531 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
9532 | return Ret_Result; | |
9533 | end if; | |
9534 | ||
9535 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
9536 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
9537 | -- in, and since Node can be an N_Range node, it might be invalid. | |
9538 | -- Should there be an assert check somewhere for taking the Etype of | |
9539 | -- an N_Range node ??? | |
9540 | ||
9541 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
9542 | S_Typ := Designated_Type (S_Typ); | |
9543 | T_Typ := Designated_Type (T_Typ); | |
9544 | Do_Access := True; | |
9545 | ||
2af58f67 | 9546 | -- A simple optimization for the null case |
ee6ba406 | 9547 | |
2af58f67 | 9548 | if Known_Null (Ck_Node) then |
ee6ba406 | 9549 | return Ret_Result; |
9550 | end if; | |
9551 | end if; | |
9552 | ||
9553 | -- For an N_Range Node, check for a null range and then if not | |
9554 | -- null generate a range check action. | |
9555 | ||
9556 | if Nkind (Ck_Node) = N_Range then | |
9557 | ||
9558 | -- There's no point in checking a range against itself | |
9559 | ||
9560 | if Ck_Node = Scalar_Range (T_Typ) then | |
9561 | return Ret_Result; | |
9562 | end if; | |
9563 | ||
9564 | declare | |
9565 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
9566 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 9567 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
9568 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 9569 | |
eefa141b | 9570 | LB : Node_Id := Low_Bound (Ck_Node); |
9571 | HB : Node_Id := High_Bound (Ck_Node); | |
9572 | Known_LB : Boolean; | |
9573 | Known_HB : Boolean; | |
9574 | ||
9575 | Null_Range : Boolean; | |
ee6ba406 | 9576 | Out_Of_Range_L : Boolean; |
9577 | Out_Of_Range_H : Boolean; | |
9578 | ||
9579 | begin | |
eefa141b | 9580 | -- Compute what is known at compile time |
9581 | ||
9582 | if Known_T_LB and Known_T_HB then | |
9583 | if Compile_Time_Known_Value (LB) then | |
9584 | Known_LB := True; | |
9585 | ||
9586 | -- There's no point in checking that a bound is within its | |
9587 | -- own range so pretend that it is known in this case. First | |
9588 | -- deal with low bound. | |
9589 | ||
9590 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
9591 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
9592 | then | |
9593 | LB := T_LB; | |
9594 | Known_LB := True; | |
9595 | ||
9596 | else | |
9597 | Known_LB := False; | |
9598 | end if; | |
9599 | ||
9600 | -- Likewise for the high bound | |
9601 | ||
9602 | if Compile_Time_Known_Value (HB) then | |
9603 | Known_HB := True; | |
9604 | ||
9605 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
9606 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
9607 | then | |
9608 | HB := T_HB; | |
9609 | Known_HB := True; | |
eefa141b | 9610 | else |
9611 | Known_HB := False; | |
9612 | end if; | |
9613 | end if; | |
9614 | ||
9615 | -- Check for case where everything is static and we can do the | |
9616 | -- check at compile time. This is skipped if we have an access | |
9617 | -- type, since the access value may be null. | |
9618 | ||
9619 | -- ??? This code can be improved since you only need to know that | |
9620 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
9621 | -- compile time to emit pertinent messages. | |
9622 | ||
9623 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
9624 | and not Do_Access | |
ee6ba406 | 9625 | then |
9626 | -- Floating-point case | |
9627 | ||
9628 | if Is_Floating_Point_Type (S_Typ) then | |
9629 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
9630 | Out_Of_Range_L := | |
9631 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 9632 | or else |
ee6ba406 | 9633 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
9634 | ||
9635 | Out_Of_Range_H := | |
9636 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 9637 | or else |
ee6ba406 | 9638 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
9639 | ||
9640 | -- Fixed or discrete type case | |
9641 | ||
9642 | else | |
9643 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
9644 | Out_Of_Range_L := | |
9645 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 9646 | or else |
ee6ba406 | 9647 | (Expr_Value (LB) > Expr_Value (T_HB)); |
9648 | ||
9649 | Out_Of_Range_H := | |
9650 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 9651 | or else |
ee6ba406 | 9652 | (Expr_Value (HB) < Expr_Value (T_LB)); |
9653 | end if; | |
9654 | ||
9655 | if not Null_Range then | |
9656 | if Out_Of_Range_L then | |
9657 | if No (Warn_Node) then | |
9658 | Add_Check | |
9659 | (Compile_Time_Constraint_Error | |
9660 | (Low_Bound (Ck_Node), | |
cb97ae5c | 9661 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9662 | |
9663 | else | |
9664 | Add_Check | |
9665 | (Compile_Time_Constraint_Error | |
9666 | (Wnode, | |
cb97ae5c | 9667 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 9668 | end if; |
9669 | end if; | |
9670 | ||
9671 | if Out_Of_Range_H then | |
9672 | if No (Warn_Node) then | |
9673 | Add_Check | |
9674 | (Compile_Time_Constraint_Error | |
9675 | (High_Bound (Ck_Node), | |
cb97ae5c | 9676 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9677 | |
9678 | else | |
9679 | Add_Check | |
9680 | (Compile_Time_Constraint_Error | |
9681 | (Wnode, | |
cb97ae5c | 9682 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 9683 | end if; |
9684 | end if; | |
ee6ba406 | 9685 | end if; |
9686 | ||
9687 | else | |
9688 | declare | |
9689 | LB : Node_Id := Low_Bound (Ck_Node); | |
9690 | HB : Node_Id := High_Bound (Ck_Node); | |
9691 | ||
9692 | begin | |
feff2f05 | 9693 | -- If either bound is a discriminant and we are within the |
9694 | -- record declaration, it is a use of the discriminant in a | |
9695 | -- constraint of a component, and nothing can be checked | |
9696 | -- here. The check will be emitted within the init proc. | |
9697 | -- Before then, the discriminal has no real meaning. | |
9698 | -- Similarly, if the entity is a discriminal, there is no | |
9699 | -- check to perform yet. | |
9700 | ||
9701 | -- The same holds within a discriminated synchronized type, | |
9702 | -- where the discriminant may constrain a component or an | |
9703 | -- entry family. | |
ee6ba406 | 9704 | |
9705 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 9706 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 9707 | then |
0577b0b1 | 9708 | if Current_Scope = Scope (Entity (LB)) |
9709 | or else Is_Concurrent_Type (Current_Scope) | |
9710 | or else Ekind (Entity (LB)) /= E_Discriminant | |
9711 | then | |
ee6ba406 | 9712 | return Ret_Result; |
9713 | else | |
9714 | LB := | |
9715 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
9716 | end if; | |
9717 | end if; | |
9718 | ||
9719 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 9720 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 9721 | then |
0577b0b1 | 9722 | if Current_Scope = Scope (Entity (HB)) |
9723 | or else Is_Concurrent_Type (Current_Scope) | |
9724 | or else Ekind (Entity (HB)) /= E_Discriminant | |
9725 | then | |
ee6ba406 | 9726 | return Ret_Result; |
9727 | else | |
9728 | HB := | |
9729 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
9730 | end if; | |
9731 | end if; | |
9732 | ||
9733 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
9734 | Set_Paren_Count (Cond, 1); | |
9735 | ||
9736 | Cond := | |
9737 | Make_And_Then (Loc, | |
9738 | Left_Opnd => | |
9739 | Make_Op_Ge (Loc, | |
86d32751 | 9740 | Left_Opnd => |
9741 | Convert_To (Base_Type (Etype (HB)), | |
9742 | Duplicate_Subexpr_No_Checks (HB)), | |
9743 | Right_Opnd => | |
9744 | Convert_To (Base_Type (Etype (LB)), | |
9745 | Duplicate_Subexpr_No_Checks (LB))), | |
ee6ba406 | 9746 | Right_Opnd => Cond); |
9747 | end; | |
ee6ba406 | 9748 | end if; |
9749 | end; | |
9750 | ||
9751 | elsif Is_Scalar_Type (S_Typ) then | |
9752 | ||
9753 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
9754 | -- except the above simply sets a flag in the node and lets | |
9755 | -- gigi generate the check base on the Etype of the expression. | |
9756 | -- Sometimes, however we want to do a dynamic check against an | |
9757 | -- arbitrary target type, so we do that here. | |
9758 | ||
9759 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
9760 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9761 | ||
9762 | -- For literals, we can tell if the constraint error will be | |
9763 | -- raised at compile time, so we never need a dynamic check, but | |
9764 | -- if the exception will be raised, then post the usual warning, | |
9765 | -- and replace the literal with a raise constraint error | |
9766 | -- expression. As usual, skip this for access types | |
9767 | ||
20cf157b | 9768 | elsif Compile_Time_Known_Value (Ck_Node) and then not Do_Access then |
ee6ba406 | 9769 | declare |
9770 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
9771 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
9772 | ||
9773 | Out_Of_Range : Boolean; | |
9774 | Static_Bounds : constant Boolean := | |
b6341c67 | 9775 | Compile_Time_Known_Value (LB) |
9776 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 9777 | |
9778 | begin | |
9779 | -- Following range tests should use Sem_Eval routine ??? | |
9780 | ||
9781 | if Static_Bounds then | |
9782 | if Is_Floating_Point_Type (S_Typ) then | |
9783 | Out_Of_Range := | |
9784 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
9785 | or else | |
9786 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
9787 | ||
eefa141b | 9788 | -- Fixed or discrete type |
9789 | ||
9790 | else | |
ee6ba406 | 9791 | Out_Of_Range := |
9792 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
9793 | or else | |
9794 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
9795 | end if; | |
9796 | ||
eefa141b | 9797 | -- Bounds of the type are static and the literal is out of |
9798 | -- range so output a warning message. | |
ee6ba406 | 9799 | |
9800 | if Out_Of_Range then | |
9801 | if No (Warn_Node) then | |
9802 | Add_Check | |
9803 | (Compile_Time_Constraint_Error | |
9804 | (Ck_Node, | |
cb97ae5c | 9805 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9806 | |
9807 | else | |
9808 | Add_Check | |
9809 | (Compile_Time_Constraint_Error | |
9810 | (Wnode, | |
cb97ae5c | 9811 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9812 | end if; |
9813 | end if; | |
9814 | ||
9815 | else | |
9816 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9817 | end if; | |
9818 | end; | |
9819 | ||
9820 | -- Here for the case of a non-static expression, we need a runtime | |
9821 | -- check unless the source type range is guaranteed to be in the | |
9822 | -- range of the target type. | |
9823 | ||
9824 | else | |
7a1dabb3 | 9825 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 9826 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
9827 | end if; | |
9828 | end if; | |
9829 | end if; | |
9830 | ||
9831 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9832 | if Is_Constrained (T_Typ) then | |
9833 | ||
9834 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
9835 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
9836 | ||
9837 | if Is_Access_Type (Exptyp) then | |
9838 | Exptyp := Designated_Type (Exptyp); | |
9839 | end if; | |
9840 | ||
9841 | -- String_Literal case. This needs to be handled specially be- | |
9842 | -- cause no index types are available for string literals. The | |
9843 | -- condition is simply: | |
9844 | ||
9845 | -- T_Typ'Length = string-literal-length | |
9846 | ||
9847 | if Nkind (Expr_Actual) = N_String_Literal then | |
9848 | null; | |
9849 | ||
9850 | -- General array case. Here we have a usable actual subtype for | |
9851 | -- the expression, and the condition is built from the two types | |
9852 | ||
9853 | -- T_Typ'First < Exptyp'First or else | |
9854 | -- T_Typ'Last > Exptyp'Last or else | |
9855 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
9856 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
9857 | -- ... | |
9858 | ||
9859 | elsif Is_Constrained (Exptyp) then | |
9860 | declare | |
9dfe12ae | 9861 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9862 | ||
ee6ba406 | 9863 | L_Index : Node_Id; |
9864 | R_Index : Node_Id; | |
ee6ba406 | 9865 | |
9866 | begin | |
9867 | L_Index := First_Index (T_Typ); | |
9868 | R_Index := First_Index (Exptyp); | |
9869 | ||
9870 | for Indx in 1 .. Ndims loop | |
9871 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9872 | or else |
9873 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9874 | then |
ee6ba406 | 9875 | -- Deal with compile time length check. Note that we |
9876 | -- skip this in the access case, because the access | |
9877 | -- value may be null, so we cannot know statically. | |
9878 | ||
9879 | if not | |
9880 | Subtypes_Statically_Match | |
9881 | (Etype (L_Index), Etype (R_Index)) | |
9882 | then | |
9883 | -- If the target type is constrained then we | |
9884 | -- have to check for exact equality of bounds | |
9885 | -- (required for qualified expressions). | |
9886 | ||
9887 | if Is_Constrained (T_Typ) then | |
9888 | Evolve_Or_Else | |
9889 | (Cond, | |
9890 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 9891 | else |
9892 | Evolve_Or_Else | |
9893 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
9894 | end if; | |
9895 | end if; | |
9896 | ||
9897 | Next (L_Index); | |
9898 | Next (R_Index); | |
ee6ba406 | 9899 | end if; |
9900 | end loop; | |
9901 | end; | |
9902 | ||
9903 | -- Handle cases where we do not get a usable actual subtype that | |
9904 | -- is constrained. This happens for example in the function call | |
9905 | -- and explicit dereference cases. In these cases, we have to get | |
9906 | -- the length or range from the expression itself, making sure we | |
9907 | -- do not evaluate it more than once. | |
9908 | ||
9909 | -- Here Ck_Node is the original expression, or more properly the | |
9910 | -- result of applying Duplicate_Expr to the original tree, | |
9911 | -- forcing the result to be a name. | |
9912 | ||
9913 | else | |
9914 | declare | |
9dfe12ae | 9915 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9916 | |
9917 | begin | |
9918 | -- Build the condition for the explicit dereference case | |
9919 | ||
9920 | for Indx in 1 .. Ndims loop | |
9921 | Evolve_Or_Else | |
9922 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
9923 | end loop; | |
9924 | end; | |
ee6ba406 | 9925 | end if; |
9926 | ||
9927 | else | |
feff2f05 | 9928 | -- For a conversion to an unconstrained array type, generate an |
9929 | -- Action to check that the bounds of the source value are within | |
9930 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
9931 | -- check is needed for a conversion to an access to unconstrained | |
9932 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
9933 | -- of the two access types to statically match. | |
9934 | ||
9935 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
9936 | and then not Do_Access | |
9937 | then | |
ee6ba406 | 9938 | declare |
9939 | Opnd_Index : Node_Id; | |
9940 | Targ_Index : Node_Id; | |
00c403ee | 9941 | Opnd_Range : Node_Id; |
ee6ba406 | 9942 | |
9943 | begin | |
feff2f05 | 9944 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 9945 | Targ_Index := First_Index (T_Typ); |
00c403ee | 9946 | while Present (Opnd_Index) loop |
9947 | ||
9948 | -- If the index is a range, use its bounds. If it is an | |
9949 | -- entity (as will be the case if it is a named subtype | |
9950 | -- or an itype created for a slice) retrieve its range. | |
9951 | ||
9952 | if Is_Entity_Name (Opnd_Index) | |
9953 | and then Is_Type (Entity (Opnd_Index)) | |
9954 | then | |
9955 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
9956 | else | |
9957 | Opnd_Range := Opnd_Index; | |
9958 | end if; | |
9959 | ||
9960 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 9961 | if Is_In_Range |
9962 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
9963 | Assume_Valid => True) | |
ee6ba406 | 9964 | and then |
9965 | Is_In_Range | |
9c486805 | 9966 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9967 | Assume_Valid => True) | |
ee6ba406 | 9968 | then |
9969 | null; | |
9970 | ||
feff2f05 | 9971 | -- If null range, no check needed |
f2a06be9 | 9972 | |
9dfe12ae | 9973 | elsif |
00c403ee | 9974 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 9975 | and then |
00c403ee | 9976 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 9977 | and then |
00c403ee | 9978 | Expr_Value (High_Bound (Opnd_Range)) < |
9979 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 9980 | then |
9981 | null; | |
9982 | ||
ee6ba406 | 9983 | elsif Is_Out_Of_Range |
9c486805 | 9984 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
9985 | Assume_Valid => True) | |
ee6ba406 | 9986 | or else |
9987 | Is_Out_Of_Range | |
9c486805 | 9988 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9989 | Assume_Valid => True) | |
ee6ba406 | 9990 | then |
9991 | Add_Check | |
9992 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9993 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 9994 | |
9995 | else | |
9996 | Evolve_Or_Else | |
9997 | (Cond, | |
9998 | Discrete_Range_Cond | |
00c403ee | 9999 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 10000 | end if; |
10001 | end if; | |
10002 | ||
10003 | Next_Index (Opnd_Index); | |
10004 | Next_Index (Targ_Index); | |
10005 | end loop; | |
10006 | end; | |
10007 | end if; | |
10008 | end if; | |
10009 | end if; | |
10010 | ||
10011 | -- Construct the test and insert into the tree | |
10012 | ||
10013 | if Present (Cond) then | |
10014 | if Do_Access then | |
10015 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
10016 | end if; | |
10017 | ||
f15731c4 | 10018 | Add_Check |
10019 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 10020 | Condition => Cond, |
10021 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 10022 | end if; |
10023 | ||
10024 | return Ret_Result; | |
ee6ba406 | 10025 | end Selected_Range_Checks; |
10026 | ||
10027 | ------------------------------- | |
10028 | -- Storage_Checks_Suppressed -- | |
10029 | ------------------------------- | |
10030 | ||
10031 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10032 | begin | |
9dfe12ae | 10033 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
10034 | return Is_Check_Suppressed (E, Storage_Check); | |
10035 | else | |
fafc6b97 | 10036 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 10037 | end if; |
ee6ba406 | 10038 | end Storage_Checks_Suppressed; |
10039 | ||
10040 | --------------------------- | |
10041 | -- Tag_Checks_Suppressed -- | |
10042 | --------------------------- | |
10043 | ||
10044 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10045 | begin | |
89f1e35c | 10046 | if Present (E) |
10047 | and then Checks_May_Be_Suppressed (E) | |
10048 | then | |
10049 | return Is_Check_Suppressed (E, Tag_Check); | |
20cf157b | 10050 | else |
10051 | return Scope_Suppress.Suppress (Tag_Check); | |
9dfe12ae | 10052 | end if; |
ee6ba406 | 10053 | end Tag_Checks_Suppressed; |
10054 | ||
7b8fa048 | 10055 | --------------------------------------- |
10056 | -- Validate_Alignment_Check_Warnings -- | |
10057 | --------------------------------------- | |
10058 | ||
10059 | procedure Validate_Alignment_Check_Warnings is | |
10060 | begin | |
10061 | for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop | |
10062 | declare | |
10063 | AWR : Alignment_Warnings_Record | |
10064 | renames Alignment_Warnings.Table (J); | |
10065 | begin | |
10066 | if Known_Alignment (AWR.E) | |
10067 | and then AWR.A mod Alignment (AWR.E) = 0 | |
10068 | then | |
10069 | Delete_Warning_And_Continuations (AWR.W); | |
10070 | end if; | |
10071 | end; | |
10072 | end loop; | |
10073 | end Validate_Alignment_Check_Warnings; | |
10074 | ||
0577b0b1 | 10075 | -------------------------- |
10076 | -- Validity_Check_Range -- | |
10077 | -------------------------- | |
10078 | ||
10079 | procedure Validity_Check_Range (N : Node_Id) is | |
10080 | begin | |
10081 | if Validity_Checks_On and Validity_Check_Operands then | |
10082 | if Nkind (N) = N_Range then | |
10083 | Ensure_Valid (Low_Bound (N)); | |
10084 | Ensure_Valid (High_Bound (N)); | |
10085 | end if; | |
10086 | end if; | |
10087 | end Validity_Check_Range; | |
10088 | ||
10089 | -------------------------------- | |
10090 | -- Validity_Checks_Suppressed -- | |
10091 | -------------------------------- | |
10092 | ||
10093 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
10094 | begin | |
10095 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
10096 | return Is_Check_Suppressed (E, Validity_Check); | |
10097 | else | |
fafc6b97 | 10098 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 10099 | end if; |
10100 | end Validity_Checks_Suppressed; | |
10101 | ||
ee6ba406 | 10102 | end Checks; |