]>
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 | -- -- | |
86594966 | 9 | -- Copyright (C) 1992-2013, 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; | |
27 | with Debug; use Debug; | |
28 | with Einfo; use Einfo; | |
29 | with Errout; use Errout; | |
30 | with Exp_Ch2; use Exp_Ch2; | |
df40eeb0 | 31 | with Exp_Ch4; use Exp_Ch4; |
00c403ee | 32 | with Exp_Ch11; use Exp_Ch11; |
05fcfafb | 33 | with Exp_Pakd; use Exp_Pakd; |
301d5ec3 | 34 | with Exp_Tss; use Exp_Tss; |
ee6ba406 | 35 | with Exp_Util; use Exp_Util; |
36 | with Elists; use Elists; | |
4fb5f0a0 | 37 | with Expander; use Expander; |
5329ca64 | 38 | with Eval_Fat; use Eval_Fat; |
ee6ba406 | 39 | with Freeze; use Freeze; |
9dfe12ae | 40 | with Lib; use Lib; |
ee6ba406 | 41 | with Nlists; use Nlists; |
42 | with Nmake; use Nmake; | |
43 | with Opt; use Opt; | |
9dfe12ae | 44 | with Output; use Output; |
c2b56224 | 45 | with Restrict; use Restrict; |
1e16c51c | 46 | with Rident; use Rident; |
ee6ba406 | 47 | with Rtsfind; use Rtsfind; |
48 | with Sem; use Sem; | |
d60c9ff7 | 49 | with Sem_Aux; use Sem_Aux; |
ee6ba406 | 50 | with Sem_Eval; use Sem_Eval; |
00f91aef | 51 | with Sem_Ch3; use Sem_Ch3; |
9dfe12ae | 52 | with Sem_Ch8; use Sem_Ch8; |
ee6ba406 | 53 | with Sem_Res; use Sem_Res; |
54 | with Sem_Util; use Sem_Util; | |
55 | with Sem_Warn; use Sem_Warn; | |
56 | with Sinfo; use Sinfo; | |
9dfe12ae | 57 | with Sinput; use Sinput; |
ee6ba406 | 58 | with Snames; use Snames; |
9dfe12ae | 59 | with Sprint; use Sprint; |
ee6ba406 | 60 | with Stand; use Stand; |
bb569db0 | 61 | with Stringt; use Stringt; |
f15731c4 | 62 | with Targparm; use Targparm; |
ee6ba406 | 63 | with Tbuild; use Tbuild; |
64 | with Ttypes; use Ttypes; | |
65 | with Urealp; use Urealp; | |
66 | with Validsw; use Validsw; | |
67 | ||
68 | package body Checks is | |
69 | ||
70 | -- General note: many of these routines are concerned with generating | |
71 | -- checking code to make sure that constraint error is raised at runtime. | |
72 | -- Clearly this code is only needed if the expander is active, since | |
73 | -- otherwise we will not be generating code or going into the runtime | |
74 | -- execution anyway. | |
75 | ||
76 | -- We therefore disconnect most of these checks if the expander is | |
77 | -- inactive. This has the additional benefit that we do not need to | |
78 | -- worry about the tree being messed up by previous errors (since errors | |
79 | -- turn off expansion anyway). | |
80 | ||
81 | -- There are a few exceptions to the above rule. For instance routines | |
82 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
83 | -- safely called even when the Expander is inactive (but Errors_Detected | |
84 | -- is 0). The benefit of executing this code when expansion is off, is | |
85 | -- the ability to emit constraint error warning for static expressions | |
86 | -- even when we are not generating code. | |
87 | ||
9dfe12ae | 88 | ------------------------------------- |
89 | -- Suppression of Redundant Checks -- | |
90 | ------------------------------------- | |
91 | ||
92 | -- This unit implements a limited circuit for removal of redundant | |
93 | -- checks. The processing is based on a tracing of simple sequential | |
94 | -- flow. For any sequence of statements, we save expressions that are | |
95 | -- marked to be checked, and then if the same expression appears later | |
96 | -- with the same check, then under certain circumstances, the second | |
97 | -- check can be suppressed. | |
98 | ||
99 | -- Basically, we can suppress the check if we know for certain that | |
100 | -- the previous expression has been elaborated (together with its | |
101 | -- check), and we know that the exception frame is the same, and that | |
102 | -- nothing has happened to change the result of the exception. | |
103 | ||
104 | -- Let us examine each of these three conditions in turn to describe | |
105 | -- how we ensure that this condition is met. | |
106 | ||
107 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 108 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 109 | -- Conditional_Statements_Begin at the start of any statement sequence |
110 | -- and Conditional_Statements_End at the end. The End call causes all | |
111 | -- checks remembered since the Begin call to be discarded. This does | |
112 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
113 | -- no exception handlers. But the important thing is to be conservative. | |
114 | -- The other protection is that all checks are discarded if a label | |
115 | -- is encountered, since then the assumption of sequential execution | |
116 | -- is violated, and we don't know enough about the flow. | |
117 | ||
118 | -- Second, we need to know that the exception frame is the same. We | |
119 | -- do this by killing all remembered checks when we enter a new frame. | |
120 | -- Again, that's over-conservative, but generally the cases we can help | |
121 | -- with are pretty local anyway (like the body of a loop for example). | |
122 | ||
123 | -- Third, we must be sure to forget any checks which are no longer valid. | |
124 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
125 | -- used to note any changes to local variables. We only attempt to deal | |
126 | -- with checks involving local variables, so we do not need to worry | |
127 | -- about global variables. Second, a call to any non-global procedure | |
128 | -- causes us to abandon all stored checks, since such a all may affect | |
129 | -- the values of any local variables. | |
130 | ||
131 | -- The following define the data structures used to deal with remembering | |
132 | -- checks so that redundant checks can be eliminated as described above. | |
133 | ||
134 | -- Right now, the only expressions that we deal with are of the form of | |
135 | -- simple local objects (either declared locally, or IN parameters) or | |
136 | -- such objects plus/minus a compile time known constant. We can do | |
137 | -- more later on if it seems worthwhile, but this catches many simple | |
138 | -- cases in practice. | |
139 | ||
140 | -- The following record type reflects a single saved check. An entry | |
141 | -- is made in the stack of saved checks if and only if the expression | |
142 | -- has been elaborated with the indicated checks. | |
143 | ||
144 | type Saved_Check is record | |
145 | Killed : Boolean; | |
146 | -- Set True if entry is killed by Kill_Checks | |
147 | ||
148 | Entity : Entity_Id; | |
149 | -- The entity involved in the expression that is checked | |
150 | ||
151 | Offset : Uint; | |
152 | -- A compile time value indicating the result of adding or | |
153 | -- subtracting a compile time value. This value is to be | |
154 | -- added to the value of the Entity. A value of zero is | |
155 | -- used for the case of a simple entity reference. | |
156 | ||
157 | Check_Type : Character; | |
158 | -- This is set to 'R' for a range check (in which case Target_Type | |
159 | -- is set to the target type for the range check) or to 'O' for an | |
160 | -- overflow check (in which case Target_Type is set to Empty). | |
161 | ||
162 | Target_Type : Entity_Id; | |
163 | -- Used only if Do_Range_Check is set. Records the target type for | |
164 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 165 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 166 | -- range that is smaller or equal to the stored target type of a |
167 | -- saved check). | |
168 | end record; | |
169 | ||
170 | -- The following table keeps track of saved checks. Rather than use an | |
171 | -- extensible table. We just use a table of fixed size, and we discard | |
172 | -- any saved checks that do not fit. That's very unlikely to happen and | |
173 | -- this is only an optimization in any case. | |
174 | ||
175 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
176 | -- Array of saved checks | |
177 | ||
178 | Num_Saved_Checks : Nat := 0; | |
179 | -- Number of saved checks | |
180 | ||
181 | -- The following stack keeps track of statement ranges. It is treated | |
182 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
183 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
184 | -- at the time of the call. Then when Conditional_Statements_End is | |
185 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
186 | ||
187 | -- Note: again, this is a fixed length stack with a size that should | |
188 | -- always be fine. If the value of the stack pointer goes above the | |
189 | -- limit, then we just forget all saved checks. | |
190 | ||
191 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
192 | Saved_Checks_TOS : Nat := 0; | |
193 | ||
194 | ----------------------- | |
195 | -- Local Subprograms -- | |
196 | ----------------------- | |
ee6ba406 | 197 | |
0df9d43f | 198 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 199 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 200 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 201 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
202 | -- signed integer arithmetic operator (but not an if or case expression). | |
203 | -- It is also called for types other than signed integers. | |
3cce7f32 | 204 | |
205 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
206 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 207 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
208 | -- arithmetic op (which includes the case of if and case expressions). Note | |
209 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
210 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 211 | |
2fe22c69 | 212 | procedure Apply_Division_Check |
213 | (N : Node_Id; | |
214 | Rlo : Uint; | |
215 | Rhi : Uint; | |
216 | ROK : Boolean); | |
217 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
218 | -- division checks as required if the Do_Division_Check flag is set. | |
219 | -- Rlo and Rhi give the possible range of the right operand, these values | |
220 | -- can be referenced and trusted only if ROK is set True. | |
221 | ||
222 | procedure Apply_Float_Conversion_Check | |
223 | (Ck_Node : Node_Id; | |
224 | Target_Typ : Entity_Id); | |
225 | -- The checks on a conversion from a floating-point type to an integer | |
226 | -- type are delicate. They have to be performed before conversion, they | |
227 | -- have to raise an exception when the operand is a NaN, and rounding must | |
228 | -- be taken into account to determine the safe bounds of the operand. | |
229 | ||
ee6ba406 | 230 | procedure Apply_Selected_Length_Checks |
231 | (Ck_Node : Node_Id; | |
232 | Target_Typ : Entity_Id; | |
233 | Source_Typ : Entity_Id; | |
234 | Do_Static : Boolean); | |
235 | -- This is the subprogram that does all the work for Apply_Length_Check | |
236 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
237 | -- described for the above routines. The Do_Static flag indicates that | |
238 | -- only a static check is to be done. | |
239 | ||
240 | procedure Apply_Selected_Range_Checks | |
241 | (Ck_Node : Node_Id; | |
242 | Target_Typ : Entity_Id; | |
243 | Source_Typ : Entity_Id; | |
244 | Do_Static : Boolean); | |
245 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
246 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
247 | -- routine. The Do_Static flag indicates that only a static check is | |
248 | -- to be done. | |
249 | ||
2af58f67 | 250 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 251 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
252 | -- This function is used to see if an access or division by zero check is | |
253 | -- needed. The check is to be applied to a single variable appearing in the | |
254 | -- source, and N is the node for the reference. If N is not of this form, | |
255 | -- True is returned with no further processing. If N is of the right form, | |
256 | -- then further processing determines if the given Check is needed. | |
257 | -- | |
258 | -- The particular circuit is to see if we have the case of a check that is | |
259 | -- not needed because it appears in the right operand of a short circuited | |
260 | -- conditional where the left operand guards the check. For example: | |
261 | -- | |
262 | -- if Var = 0 or else Q / Var > 12 then | |
263 | -- ... | |
264 | -- end if; | |
265 | -- | |
266 | -- In this example, the division check is not required. At the same time | |
267 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
268 | -- such as: | |
269 | -- | |
270 | -- if Var = 0 or Q / Var > 12 then | |
271 | -- ... | |
272 | -- end if; | |
273 | ||
9dfe12ae | 274 | procedure Find_Check |
275 | (Expr : Node_Id; | |
276 | Check_Type : Character; | |
277 | Target_Type : Entity_Id; | |
278 | Entry_OK : out Boolean; | |
279 | Check_Num : out Nat; | |
280 | Ent : out Entity_Id; | |
281 | Ofs : out Uint); | |
282 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
283 | -- to see if a check is of the form for optimization, and if so, to see | |
284 | -- if it has already been performed. Expr is the expression to check, | |
285 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
286 | -- Target_Type is the target type for a range check, and Empty for an | |
287 | -- overflow check. If the entry is not of the form for optimization, | |
288 | -- then Entry_OK is set to False, and the remaining out parameters | |
289 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
290 | -- entity and offset from the expression. Check_Num is the number of | |
291 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
292 | -- is located. | |
293 | ||
ee6ba406 | 294 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
295 | -- If a discriminal is used in constraining a prival, Return reference | |
296 | -- to the discriminal of the protected body (which renames the parameter | |
297 | -- of the enclosing protected operation). This clumsy transformation is | |
298 | -- needed because privals are created too late and their actual subtypes | |
299 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 300 | -- This function is called whenever the bound is an entity and the scope |
301 | -- indicates a protected operation. If the bound is an in-parameter of | |
302 | -- a protected operation that is not a prival, the function returns the | |
303 | -- bound itself. | |
ee6ba406 | 304 | -- To be cleaned up??? |
305 | ||
306 | function Guard_Access | |
307 | (Cond : Node_Id; | |
308 | Loc : Source_Ptr; | |
314a23b6 | 309 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 310 | -- In the access type case, guard the test with a test to ensure |
311 | -- that the access value is non-null, since the checks do not | |
312 | -- not apply to null access values. | |
313 | ||
314 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
315 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
316 | -- Constraint_Error node. | |
317 | ||
3cce7f32 | 318 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
319 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 320 | -- integer operands. This includes unary and binary operators, and also |
321 | -- if and case expression nodes where the dependent expressions are of | |
322 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 323 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 324 | |
0577b0b1 | 325 | function Range_Or_Validity_Checks_Suppressed |
326 | (Expr : Node_Id) return Boolean; | |
327 | -- Returns True if either range or validity checks or both are suppressed | |
328 | -- for the type of the given expression, or, if the expression is the name | |
329 | -- of an entity, if these checks are suppressed for the entity. | |
330 | ||
ee6ba406 | 331 | function Selected_Length_Checks |
332 | (Ck_Node : Node_Id; | |
333 | Target_Typ : Entity_Id; | |
334 | Source_Typ : Entity_Id; | |
314a23b6 | 335 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 336 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
337 | -- anything, just returns a list of nodes as described in the spec of | |
338 | -- this package for the Range_Check function. | |
339 | ||
340 | function Selected_Range_Checks | |
341 | (Ck_Node : Node_Id; | |
342 | Target_Typ : Entity_Id; | |
343 | Source_Typ : Entity_Id; | |
314a23b6 | 344 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 345 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
346 | -- just returns a list of nodes as described in the spec of this package | |
347 | -- for the Range_Check function. | |
348 | ||
349 | ------------------------------ | |
350 | -- Access_Checks_Suppressed -- | |
351 | ------------------------------ | |
352 | ||
353 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
354 | begin | |
9dfe12ae | 355 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
356 | return Is_Check_Suppressed (E, Access_Check); | |
357 | else | |
fafc6b97 | 358 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 359 | end if; |
ee6ba406 | 360 | end Access_Checks_Suppressed; |
361 | ||
362 | ------------------------------------- | |
363 | -- Accessibility_Checks_Suppressed -- | |
364 | ------------------------------------- | |
365 | ||
366 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
367 | begin | |
9dfe12ae | 368 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
369 | return Is_Check_Suppressed (E, Accessibility_Check); | |
370 | else | |
fafc6b97 | 371 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 372 | end if; |
ee6ba406 | 373 | end Accessibility_Checks_Suppressed; |
374 | ||
00c403ee | 375 | ----------------------------- |
376 | -- Activate_Division_Check -- | |
377 | ----------------------------- | |
378 | ||
379 | procedure Activate_Division_Check (N : Node_Id) is | |
380 | begin | |
381 | Set_Do_Division_Check (N, True); | |
382 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
383 | end Activate_Division_Check; | |
384 | ||
385 | ----------------------------- | |
386 | -- Activate_Overflow_Check -- | |
387 | ----------------------------- | |
388 | ||
389 | procedure Activate_Overflow_Check (N : Node_Id) is | |
390 | begin | |
d32ceaf3 | 391 | if not Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then |
392 | Set_Do_Overflow_Check (N, True); | |
393 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
394 | end if; | |
00c403ee | 395 | end Activate_Overflow_Check; |
396 | ||
397 | -------------------------- | |
398 | -- Activate_Range_Check -- | |
399 | -------------------------- | |
400 | ||
401 | procedure Activate_Range_Check (N : Node_Id) is | |
402 | begin | |
403 | Set_Do_Range_Check (N, True); | |
404 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
405 | end Activate_Range_Check; | |
406 | ||
0577b0b1 | 407 | --------------------------------- |
408 | -- Alignment_Checks_Suppressed -- | |
409 | --------------------------------- | |
410 | ||
411 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
412 | begin | |
413 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
414 | return Is_Check_Suppressed (E, Alignment_Check); | |
415 | else | |
fafc6b97 | 416 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 417 | end if; |
418 | end Alignment_Checks_Suppressed; | |
419 | ||
ee6ba406 | 420 | ------------------------- |
421 | -- Append_Range_Checks -- | |
422 | ------------------------- | |
423 | ||
424 | procedure Append_Range_Checks | |
425 | (Checks : Check_Result; | |
426 | Stmts : List_Id; | |
427 | Suppress_Typ : Entity_Id; | |
428 | Static_Sloc : Source_Ptr; | |
429 | Flag_Node : Node_Id) | |
430 | is | |
9dfe12ae | 431 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
432 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
433 | ||
ee6ba406 | 434 | Checks_On : constant Boolean := |
b6341c67 | 435 | (not Index_Checks_Suppressed (Suppress_Typ)) |
436 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 437 | |
438 | begin | |
439 | -- For now we just return if Checks_On is false, however this should | |
440 | -- be enhanced to check for an always True value in the condition | |
441 | -- and to generate a compilation warning??? | |
442 | ||
443 | if not Checks_On then | |
444 | return; | |
445 | end if; | |
446 | ||
447 | for J in 1 .. 2 loop | |
448 | exit when No (Checks (J)); | |
449 | ||
450 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
451 | and then Present (Condition (Checks (J))) | |
452 | then | |
453 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
454 | Append_To (Stmts, Checks (J)); | |
455 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
456 | end if; | |
457 | ||
458 | else | |
459 | Append_To | |
f15731c4 | 460 | (Stmts, |
461 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
462 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 463 | end if; |
464 | end loop; | |
465 | end Append_Range_Checks; | |
466 | ||
467 | ------------------------ | |
468 | -- Apply_Access_Check -- | |
469 | ------------------------ | |
470 | ||
471 | procedure Apply_Access_Check (N : Node_Id) is | |
472 | P : constant Node_Id := Prefix (N); | |
473 | ||
474 | begin | |
13dbf220 | 475 | -- We do not need checks if we are not generating code (i.e. the |
476 | -- expander is not active). This is not just an optimization, there | |
477 | -- are cases (e.g. with pragma Debug) where generating the checks | |
478 | -- can cause real trouble). | |
284faf8b | 479 | |
6dbcfcd9 | 480 | if not Full_Expander_Active then |
13dbf220 | 481 | return; |
9dfe12ae | 482 | end if; |
ee6ba406 | 483 | |
84d0d4a5 | 484 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 485 | |
84d0d4a5 | 486 | if not Check_Needed (P, Access_Check) then |
487 | return; | |
ee6ba406 | 488 | end if; |
9dfe12ae | 489 | |
cc60bd16 | 490 | -- No check if accessing the Offset_To_Top component of a dispatch |
491 | -- table. They are safe by construction. | |
492 | ||
040277b1 | 493 | if Tagged_Type_Expansion |
494 | and then Present (Etype (P)) | |
cc60bd16 | 495 | and then RTU_Loaded (Ada_Tags) |
496 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
497 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
498 | then | |
499 | return; | |
500 | end if; | |
501 | ||
84d0d4a5 | 502 | -- Otherwise go ahead and install the check |
9dfe12ae | 503 | |
fa7497e8 | 504 | Install_Null_Excluding_Check (P); |
ee6ba406 | 505 | end Apply_Access_Check; |
506 | ||
507 | ------------------------------- | |
508 | -- Apply_Accessibility_Check -- | |
509 | ------------------------------- | |
510 | ||
55dc6dc2 | 511 | procedure Apply_Accessibility_Check |
512 | (N : Node_Id; | |
513 | Typ : Entity_Id; | |
514 | Insert_Node : Node_Id) | |
515 | is | |
ee6ba406 | 516 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 517 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 518 | Param_Level : Node_Id; |
519 | Type_Level : Node_Id; | |
520 | ||
521 | begin | |
47d210a3 | 522 | if Ada_Version >= Ada_2012 |
523 | and then not Present (Param_Ent) | |
524 | and then Is_Entity_Name (N) | |
525 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
526 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
527 | then | |
528 | Param_Ent := Entity (N); | |
529 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 530 | |
47d210a3 | 531 | -- Renamed_Object must return an Entity_Name here |
532 | -- because of preceding "Present (E_E_A (...))" test. | |
533 | ||
534 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
535 | end loop; | |
536 | end if; | |
537 | ||
ee6ba406 | 538 | if Inside_A_Generic then |
539 | return; | |
540 | ||
6ffc64fc | 541 | -- Only apply the run-time check if the access parameter has an |
542 | -- associated extra access level parameter and when the level of the | |
543 | -- type is less deep than the level of the access parameter, and | |
544 | -- accessibility checks are not suppressed. | |
ee6ba406 | 545 | |
546 | elsif Present (Param_Ent) | |
547 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 548 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 549 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 550 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
551 | and then not Accessibility_Checks_Suppressed (Typ) | |
552 | then | |
553 | Param_Level := | |
554 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
555 | ||
1a9cc6cd | 556 | Type_Level := |
557 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
ee6ba406 | 558 | |
bf3e1520 | 559 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 560 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 561 | |
55dc6dc2 | 562 | Insert_Action (Insert_Node, |
ee6ba406 | 563 | Make_Raise_Program_Error (Loc, |
564 | Condition => | |
565 | Make_Op_Gt (Loc, | |
566 | Left_Opnd => Param_Level, | |
f15731c4 | 567 | Right_Opnd => Type_Level), |
568 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 569 | |
570 | Analyze_And_Resolve (N); | |
571 | end if; | |
572 | end Apply_Accessibility_Check; | |
573 | ||
0577b0b1 | 574 | -------------------------------- |
575 | -- Apply_Address_Clause_Check -- | |
576 | -------------------------------- | |
577 | ||
578 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 579 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
580 | ||
0577b0b1 | 581 | AC : constant Node_Id := Address_Clause (E); |
582 | Loc : constant Source_Ptr := Sloc (AC); | |
583 | Typ : constant Entity_Id := Etype (E); | |
584 | Aexp : constant Node_Id := Expression (AC); | |
c2b56224 | 585 | |
c2b56224 | 586 | Expr : Node_Id; |
0577b0b1 | 587 | -- Address expression (not necessarily the same as Aexp, for example |
588 | -- when Aexp is a reference to a constant, in which case Expr gets | |
589 | -- reset to reference the value expression of the constant. | |
590 | ||
0577b0b1 | 591 | procedure Compile_Time_Bad_Alignment; |
592 | -- Post error warnings when alignment is known to be incompatible. Note | |
593 | -- that we do not go as far as inserting a raise of Program_Error since | |
594 | -- this is an erroneous case, and it may happen that we are lucky and an | |
d6da7448 | 595 | -- underaligned address turns out to be OK after all. |
0577b0b1 | 596 | |
597 | -------------------------------- | |
598 | -- Compile_Time_Bad_Alignment -- | |
599 | -------------------------------- | |
600 | ||
601 | procedure Compile_Time_Bad_Alignment is | |
602 | begin | |
d6da7448 | 603 | if Address_Clause_Overlay_Warnings then |
0577b0b1 | 604 | Error_Msg_FE |
cb97ae5c | 605 | ("?o?specified address for& may be inconsistent with alignment", |
0577b0b1 | 606 | Aexp, E); |
607 | Error_Msg_FE | |
cb97ae5c | 608 | ("\?o?program execution may be erroneous (RM 13.3(27))", |
0577b0b1 | 609 | Aexp, E); |
83f8f0a6 | 610 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 611 | end if; |
612 | end Compile_Time_Bad_Alignment; | |
c2b56224 | 613 | |
2af58f67 | 614 | -- Start of processing for Apply_Address_Clause_Check |
5c61a0ff | 615 | |
c2b56224 | 616 | begin |
d6da7448 | 617 | -- See if alignment check needed. Note that we never need a check if the |
618 | -- maximum alignment is one, since the check will always succeed. | |
619 | ||
620 | -- Note: we do not check for checks suppressed here, since that check | |
621 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
622 | -- only called if checks were not suppressed. The reason for this is | |
623 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
624 | -- time (so that all types etc are elaborated), but we have to check | |
625 | -- the status of check suppressing at the point of the address clause. | |
626 | ||
627 | if No (AC) | |
628 | or else not Check_Address_Alignment (AC) | |
629 | or else Maximum_Alignment = 1 | |
630 | then | |
631 | return; | |
632 | end if; | |
633 | ||
634 | -- Obtain expression from address clause | |
9dfe12ae | 635 | |
0577b0b1 | 636 | Expr := Expression (AC); |
637 | ||
638 | -- The following loop digs for the real expression to use in the check | |
639 | ||
640 | loop | |
641 | -- For constant, get constant expression | |
642 | ||
643 | if Is_Entity_Name (Expr) | |
644 | and then Ekind (Entity (Expr)) = E_Constant | |
645 | then | |
646 | Expr := Constant_Value (Entity (Expr)); | |
647 | ||
648 | -- For unchecked conversion, get result to convert | |
649 | ||
650 | elsif Nkind (Expr) = N_Unchecked_Type_Conversion then | |
651 | Expr := Expression (Expr); | |
652 | ||
653 | -- For (common case) of To_Address call, get argument | |
654 | ||
655 | elsif Nkind (Expr) = N_Function_Call | |
656 | and then Is_Entity_Name (Name (Expr)) | |
657 | and then Is_RTE (Entity (Name (Expr)), RE_To_Address) | |
658 | then | |
659 | Expr := First (Parameter_Associations (Expr)); | |
660 | ||
661 | if Nkind (Expr) = N_Parameter_Association then | |
662 | Expr := Explicit_Actual_Parameter (Expr); | |
663 | end if; | |
664 | ||
665 | -- We finally have the real expression | |
666 | ||
667 | else | |
668 | exit; | |
669 | end if; | |
670 | end loop; | |
671 | ||
d6da7448 | 672 | -- See if we know that Expr has a bad alignment at compile time |
c2b56224 | 673 | |
674 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 675 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 676 | then |
f2a06be9 | 677 | declare |
678 | AL : Uint := Alignment (Typ); | |
679 | ||
680 | begin | |
681 | -- The object alignment might be more restrictive than the | |
682 | -- type alignment. | |
683 | ||
684 | if Known_Alignment (E) then | |
685 | AL := Alignment (E); | |
686 | end if; | |
687 | ||
688 | if Expr_Value (Expr) mod AL /= 0 then | |
0577b0b1 | 689 | Compile_Time_Bad_Alignment; |
690 | else | |
691 | return; | |
f2a06be9 | 692 | end if; |
693 | end; | |
c2b56224 | 694 | |
0577b0b1 | 695 | -- If the expression has the form X'Address, then we can find out if |
696 | -- the object X has an alignment that is compatible with the object E. | |
d6da7448 | 697 | -- If it hasn't or we don't know, we defer issuing the warning until |
698 | -- the end of the compilation to take into account back end annotations. | |
c2b56224 | 699 | |
0577b0b1 | 700 | elsif Nkind (Expr) = N_Attribute_Reference |
701 | and then Attribute_Name (Expr) = Name_Address | |
d6da7448 | 702 | and then Has_Compatible_Alignment (E, Prefix (Expr)) = Known_Compatible |
0577b0b1 | 703 | then |
d6da7448 | 704 | return; |
0577b0b1 | 705 | end if; |
c2b56224 | 706 | |
6fb3c314 | 707 | -- Here we do not know if the value is acceptable. Strictly we don't |
708 | -- have to do anything, since if the alignment is bad, we have an | |
709 | -- erroneous program. However we are allowed to check for erroneous | |
710 | -- conditions and we decide to do this by default if the check is not | |
711 | -- suppressed. | |
0577b0b1 | 712 | |
713 | -- However, don't do the check if elaboration code is unwanted | |
714 | ||
715 | if Restriction_Active (No_Elaboration_Code) then | |
716 | return; | |
717 | ||
718 | -- Generate a check to raise PE if alignment may be inappropriate | |
719 | ||
720 | else | |
721 | -- If the original expression is a non-static constant, use the | |
722 | -- name of the constant itself rather than duplicating its | |
00c403ee | 723 | -- defining expression, which was extracted above. |
0577b0b1 | 724 | |
00c403ee | 725 | -- Note: Expr is empty if the address-clause is applied to in-mode |
726 | -- actuals (allowed by 13.1(22)). | |
727 | ||
728 | if not Present (Expr) | |
729 | or else | |
730 | (Is_Entity_Name (Expression (AC)) | |
731 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
732 | and then Nkind (Parent (Entity (Expression (AC)))) | |
733 | = N_Object_Declaration) | |
0577b0b1 | 734 | then |
735 | Expr := New_Copy_Tree (Expression (AC)); | |
736 | else | |
737 | Remove_Side_Effects (Expr); | |
c2b56224 | 738 | end if; |
c2b56224 | 739 | |
d950dc79 | 740 | if No (Actions (N)) then |
741 | Set_Actions (N, New_List); | |
742 | end if; | |
743 | ||
744 | Prepend_To (Actions (N), | |
0577b0b1 | 745 | Make_Raise_Program_Error (Loc, |
746 | Condition => | |
747 | Make_Op_Ne (Loc, | |
748 | Left_Opnd => | |
749 | Make_Op_Mod (Loc, | |
750 | Left_Opnd => | |
751 | Unchecked_Convert_To | |
752 | (RTE (RE_Integer_Address), Expr), | |
753 | Right_Opnd => | |
754 | Make_Attribute_Reference (Loc, | |
d950dc79 | 755 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 756 | Attribute_Name => Name_Alignment)), |
757 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
d950dc79 | 758 | Reason => PE_Misaligned_Address_Value)); |
759 | Analyze (First (Actions (N)), Suppress => All_Checks); | |
0577b0b1 | 760 | return; |
761 | end if; | |
9dfe12ae | 762 | |
763 | exception | |
0577b0b1 | 764 | -- If we have some missing run time component in configurable run time |
765 | -- mode then just skip the check (it is not required in any case). | |
766 | ||
9dfe12ae | 767 | when RE_Not_Available => |
768 | return; | |
0577b0b1 | 769 | end Apply_Address_Clause_Check; |
c2b56224 | 770 | |
ee6ba406 | 771 | ------------------------------------- |
772 | -- Apply_Arithmetic_Overflow_Check -- | |
773 | ------------------------------------- | |
774 | ||
3cce7f32 | 775 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
776 | begin | |
777 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 778 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 779 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 780 | |
0df9d43f | 781 | if Overflow_Check_Mode = Strict |
3cce7f32 | 782 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
783 | then | |
0df9d43f | 784 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 785 | |
21a55437 | 786 | -- Otherwise use the new routine for the case of a signed integer |
787 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
788 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 789 | |
790 | else | |
791 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
792 | end if; | |
793 | end Apply_Arithmetic_Overflow_Check; | |
794 | ||
0df9d43f | 795 | -------------------------------------- |
796 | -- Apply_Arithmetic_Overflow_Strict -- | |
797 | -------------------------------------- | |
3cce7f32 | 798 | |
f40f9731 | 799 | -- This routine is called only if the type is an integer type, and a |
800 | -- software arithmetic overflow check may be needed for op (add, subtract, | |
801 | -- or multiply). This check is performed only if Software_Overflow_Checking | |
802 | -- is enabled and Do_Overflow_Check is set. In this case we expand the | |
803 | -- operation into a more complex sequence of tests that ensures that | |
804 | -- overflow is properly caught. | |
ee6ba406 | 805 | |
0df9d43f | 806 | -- This is used in CHECKED modes. It is identical to the code for this |
807 | -- cases before the big overflow earthquake, thus ensuring that in this | |
808 | -- modes we have compatible behavior (and reliability) to what was there | |
809 | -- before. It is also called for types other than signed integers, and if | |
810 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 811 | |
812 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
813 | -- to give up and just generate an overflow check without any fuss. | |
814 | ||
0df9d43f | 815 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 816 | Loc : constant Source_Ptr := Sloc (N); |
817 | Typ : constant Entity_Id := Etype (N); | |
818 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 819 | |
820 | begin | |
0df9d43f | 821 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
822 | -- suppressed. | |
823 | ||
824 | if not Do_Overflow_Check (N) then | |
825 | return; | |
826 | end if; | |
827 | ||
f40f9731 | 828 | -- An interesting special case. If the arithmetic operation appears as |
829 | -- the operand of a type conversion: | |
830 | ||
831 | -- type1 (x op y) | |
832 | ||
833 | -- and all the following conditions apply: | |
834 | ||
835 | -- arithmetic operation is for a signed integer type | |
836 | -- target type type1 is a static integer subtype | |
837 | -- range of x and y are both included in the range of type1 | |
838 | -- range of x op y is included in the range of type1 | |
839 | -- size of type1 is at least twice the result size of op | |
840 | ||
841 | -- then we don't do an overflow check in any case, instead we transform | |
842 | -- the operation so that we end up with: | |
843 | ||
844 | -- type1 (type1 (x) op type1 (y)) | |
845 | ||
846 | -- This avoids intermediate overflow before the conversion. It is | |
847 | -- explicitly permitted by RM 3.5.4(24): | |
848 | ||
849 | -- For the execution of a predefined operation of a signed integer | |
850 | -- type, the implementation need not raise Constraint_Error if the | |
851 | -- result is outside the base range of the type, so long as the | |
852 | -- correct result is produced. | |
853 | ||
854 | -- It's hard to imagine that any programmer counts on the exception | |
855 | -- being raised in this case, and in any case it's wrong coding to | |
856 | -- have this expectation, given the RM permission. Furthermore, other | |
857 | -- Ada compilers do allow such out of range results. | |
858 | ||
859 | -- Note that we do this transformation even if overflow checking is | |
860 | -- off, since this is precisely about giving the "right" result and | |
861 | -- avoiding the need for an overflow check. | |
862 | ||
8eb4a5eb | 863 | -- Note: this circuit is partially redundant with respect to the similar |
864 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
865 | -- with cases that do not come through here. We still need the following | |
866 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
867 | -- sure not to generate the arithmetic overflow check in these cases | |
868 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
869 | ||
f40f9731 | 870 | if Is_Signed_Integer_Type (Typ) |
871 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 872 | then |
f32c377d | 873 | Conversion_Optimization : declare |
f40f9731 | 874 | Target_Type : constant Entity_Id := |
b6341c67 | 875 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 876 | |
877 | Llo, Lhi : Uint; | |
878 | Rlo, Rhi : Uint; | |
879 | LOK, ROK : Boolean; | |
880 | ||
881 | Vlo : Uint; | |
882 | Vhi : Uint; | |
883 | VOK : Boolean; | |
884 | ||
885 | Tlo : Uint; | |
886 | Thi : Uint; | |
887 | ||
888 | begin | |
889 | if Is_Integer_Type (Target_Type) | |
890 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
891 | then | |
892 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
893 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
894 | ||
9c486805 | 895 | Determine_Range |
896 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
897 | Determine_Range | |
898 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 899 | |
900 | if (LOK and ROK) | |
901 | and then Tlo <= Llo and then Lhi <= Thi | |
902 | and then Tlo <= Rlo and then Rhi <= Thi | |
903 | then | |
9c486805 | 904 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 905 | |
906 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
907 | Rewrite (Left_Opnd (N), | |
908 | Make_Type_Conversion (Loc, | |
909 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
910 | Expression => Relocate_Node (Left_Opnd (N)))); | |
911 | ||
912 | Rewrite (Right_Opnd (N), | |
913 | Make_Type_Conversion (Loc, | |
914 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
915 | Expression => Relocate_Node (Right_Opnd (N)))); | |
916 | ||
780bfb21 | 917 | -- Rewrite the conversion operand so that the original |
918 | -- node is retained, in order to avoid the warning for | |
919 | -- redundant conversions in Resolve_Type_Conversion. | |
920 | ||
921 | Rewrite (N, Relocate_Node (N)); | |
922 | ||
f40f9731 | 923 | Set_Etype (N, Target_Type); |
780bfb21 | 924 | |
f40f9731 | 925 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
926 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
927 | ||
928 | -- Given that the target type is twice the size of the | |
929 | -- source type, overflow is now impossible, so we can | |
930 | -- safely kill the overflow check and return. | |
931 | ||
932 | Set_Do_Overflow_Check (N, False); | |
933 | return; | |
934 | end if; | |
935 | end if; | |
936 | end if; | |
f32c377d | 937 | end Conversion_Optimization; |
ee6ba406 | 938 | end if; |
939 | ||
f40f9731 | 940 | -- Now see if an overflow check is required |
941 | ||
942 | declare | |
943 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
944 | Dsiz : constant Int := Siz * 2; | |
945 | Opnod : Node_Id; | |
946 | Ctyp : Entity_Id; | |
947 | Opnd : Node_Id; | |
948 | Cent : RE_Id; | |
ee6ba406 | 949 | |
f40f9731 | 950 | begin |
951 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 952 | -- is not set anyway, or we are not doing code expansion, or the |
953 | -- parent node is a type conversion whose operand is an arithmetic | |
954 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 955 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 956 | |
f40f9731 | 957 | -- Special case CLI target, where arithmetic overflow checks can be |
958 | -- performed for integer and long_integer | |
ee6ba406 | 959 | |
f40f9731 | 960 | if Backend_Overflow_Checks_On_Target |
961 | or else not Do_Overflow_Check (N) | |
6dbcfcd9 | 962 | or else not Full_Expander_Active |
df40eeb0 | 963 | or else (Present (Parent (N)) |
964 | and then Nkind (Parent (N)) = N_Type_Conversion | |
965 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 966 | or else |
967 | (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size) | |
968 | then | |
969 | return; | |
970 | end if; | |
ee6ba406 | 971 | |
f40f9731 | 972 | -- Otherwise, generate the full general code for front end overflow |
973 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 974 | |
f40f9731 | 975 | -- x op y |
ee6ba406 | 976 | |
f40f9731 | 977 | -- is expanded into |
ee6ba406 | 978 | |
f40f9731 | 979 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 980 | |
f40f9731 | 981 | -- where Typ is the type of the original expression, and Checktyp is |
982 | -- an integer type of sufficient length to hold the largest possible | |
983 | -- result. | |
ee6ba406 | 984 | |
f40f9731 | 985 | -- If the size of check type exceeds the size of Long_Long_Integer, |
986 | -- we use a different approach, expanding to: | |
ee6ba406 | 987 | |
f40f9731 | 988 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 989 | |
f40f9731 | 990 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 991 | |
f40f9731 | 992 | -- Find check type if one exists |
993 | ||
994 | if Dsiz <= Standard_Integer_Size then | |
995 | Ctyp := Standard_Integer; | |
ee6ba406 | 996 | |
f40f9731 | 997 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
998 | Ctyp := Standard_Long_Long_Integer; | |
999 | ||
c9f84db7 | 1000 | -- No check type exists, use runtime call |
ee6ba406 | 1001 | |
1002 | else | |
f40f9731 | 1003 | if Nkind (N) = N_Op_Add then |
1004 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1005 | |
f40f9731 | 1006 | elsif Nkind (N) = N_Op_Multiply then |
1007 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1008 | |
f40f9731 | 1009 | else |
1010 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1011 | Cent := RE_Subtract_With_Ovflo_Check; | |
1012 | end if; | |
1013 | ||
1014 | Rewrite (N, | |
1015 | OK_Convert_To (Typ, | |
1016 | Make_Function_Call (Loc, | |
1017 | Name => New_Reference_To (RTE (Cent), Loc), | |
1018 | Parameter_Associations => New_List ( | |
1019 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1020 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1021 | |
f40f9731 | 1022 | Analyze_And_Resolve (N, Typ); |
1023 | return; | |
1024 | end if; | |
ee6ba406 | 1025 | |
f40f9731 | 1026 | -- If we fall through, we have the case where we do the arithmetic |
1027 | -- in the next higher type and get the check by conversion. In these | |
1028 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1029 | |
f40f9731 | 1030 | Opnod := Relocate_Node (N); |
ee6ba406 | 1031 | |
f40f9731 | 1032 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1033 | |
f40f9731 | 1034 | Analyze (Opnd); |
1035 | Set_Etype (Opnd, Ctyp); | |
1036 | Set_Analyzed (Opnd, True); | |
1037 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1038 | |
f40f9731 | 1039 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1040 | |
f40f9731 | 1041 | Analyze (Opnd); |
1042 | Set_Etype (Opnd, Ctyp); | |
1043 | Set_Analyzed (Opnd, True); | |
1044 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1045 | |
f40f9731 | 1046 | -- The type of the operation changes to the base type of the check |
1047 | -- type, and we reset the overflow check indication, since clearly no | |
1048 | -- overflow is possible now that we are using a double length type. | |
1049 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1050 | -- expand the node. | |
ee6ba406 | 1051 | |
f40f9731 | 1052 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1053 | Set_Do_Overflow_Check (Opnod, False); | |
1054 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1055 | |
f40f9731 | 1056 | -- Now build the outer conversion |
ee6ba406 | 1057 | |
f40f9731 | 1058 | Opnd := OK_Convert_To (Typ, Opnod); |
1059 | Analyze (Opnd); | |
1060 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1061 | |
f40f9731 | 1062 | -- In the discrete type case, we directly generate the range check |
1063 | -- for the outer operand. This range check will implement the | |
1064 | -- required overflow check. | |
9dfe12ae | 1065 | |
f40f9731 | 1066 | if Is_Discrete_Type (Typ) then |
1067 | Rewrite (N, Opnd); | |
1068 | Generate_Range_Check | |
1069 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1070 | |
f40f9731 | 1071 | -- For other types, we enable overflow checking on the conversion, |
1072 | -- after setting the node as analyzed to prevent recursive attempts | |
1073 | -- to expand the conversion node. | |
9dfe12ae | 1074 | |
f40f9731 | 1075 | else |
1076 | Set_Analyzed (Opnd, True); | |
1077 | Enable_Overflow_Check (Opnd); | |
1078 | Rewrite (N, Opnd); | |
1079 | end if; | |
1080 | ||
1081 | exception | |
1082 | when RE_Not_Available => | |
1083 | return; | |
1084 | end; | |
0df9d43f | 1085 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1086 | |
1087 | ---------------------------------------------------- | |
1088 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1089 | ---------------------------------------------------- | |
1090 | ||
1091 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1092 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1093 | |
1094 | Loc : constant Source_Ptr := Sloc (Op); | |
1095 | P : constant Node_Id := Parent (Op); | |
1096 | ||
49b3a812 | 1097 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1098 | -- Operands and results are of this type when we convert | |
1099 | ||
3cce7f32 | 1100 | Result_Type : constant Entity_Id := Etype (Op); |
1101 | -- Original result type | |
1102 | ||
db415383 | 1103 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1104 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1105 | ||
1106 | Lo, Hi : Uint; | |
1107 | -- Ranges of values for result | |
1108 | ||
1109 | begin | |
1110 | -- Nothing to do if our parent is one of the following: | |
1111 | ||
0326b4d4 | 1112 | -- Another signed integer arithmetic op |
3cce7f32 | 1113 | -- A membership operation |
1114 | -- A comparison operation | |
1115 | ||
1116 | -- In all these cases, we will process at the higher level (and then | |
1117 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1118 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1119 | |
1120 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1121 | or else Nkind (P) in N_Membership_Test |
1122 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1123 | |
70a2dff4 | 1124 | -- This is also true for an alternative in a case expression |
1125 | ||
1126 | or else Nkind (P) = N_Case_Expression_Alternative | |
1127 | ||
1128 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1129 | |
b8a17a21 | 1130 | or else (Nkind (P) = N_Range |
1131 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1132 | then |
1133 | return; | |
1134 | end if; | |
1135 | ||
0326b4d4 | 1136 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1137 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1138 | -- modes. This is the case where we tell the machinery not to move into | |
1139 | -- Bignum mode at this top level (of course the top level operation | |
1140 | -- will still be in Bignum mode if either of its operands are of type | |
1141 | -- Bignum). | |
3cce7f32 | 1142 | |
0df9d43f | 1143 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1144 | |
1145 | -- That call may but does not necessarily change the result type of Op. | |
1146 | -- It is the job of this routine to undo such changes, so that at the | |
1147 | -- top level, we have the proper type. This "undoing" is a point at | |
1148 | -- which a final overflow check may be applied. | |
1149 | ||
f32c377d | 1150 | -- If the result type was not fiddled we are all set. We go to base |
1151 | -- types here because things may have been rewritten to generate the | |
1152 | -- base type of the operand types. | |
3cce7f32 | 1153 | |
f32c377d | 1154 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1155 | return; |
1156 | ||
1157 | -- Bignum case | |
1158 | ||
49b3a812 | 1159 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1160 | |
d94b5da2 | 1161 | -- We need a sequence that looks like: |
3cce7f32 | 1162 | |
1163 | -- Rnn : Result_Type; | |
1164 | ||
1165 | -- declare | |
d94b5da2 | 1166 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1167 | -- begin |
49b3a812 | 1168 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1169 | -- SS_Release (M); |
1170 | -- end; | |
1171 | ||
1172 | -- This block is inserted (using Insert_Actions), and then the node | |
1173 | -- is replaced with a reference to Rnn. | |
1174 | ||
1175 | -- A special case arises if our parent is a conversion node. In this | |
1176 | -- case no point in generating a conversion to Result_Type, we will | |
1177 | -- let the parent handle this. Note that this special case is not | |
1178 | -- just about optimization. Consider | |
1179 | ||
1180 | -- A,B,C : Integer; | |
1181 | -- ... | |
49b3a812 | 1182 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1183 | |
1184 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1185 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1186 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1187 | |
1188 | declare | |
49b3a812 | 1189 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1190 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1191 | RHS : Node_Id; | |
1192 | ||
1193 | Rtype : Entity_Id; | |
1194 | ||
1195 | begin | |
1196 | RHS := Convert_From_Bignum (Op); | |
1197 | ||
1198 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1199 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1200 | Rtype := Result_Type; |
1201 | ||
1202 | -- Interesting question, do we need a check on that conversion | |
1203 | -- operation. Answer, not if we know the result is in range. | |
1204 | -- At the moment we are not taking advantage of this. To be | |
1205 | -- looked at later ??? | |
1206 | ||
1207 | else | |
49b3a812 | 1208 | Rtype := LLIB; |
3cce7f32 | 1209 | end if; |
1210 | ||
1211 | Insert_Before | |
1212 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1213 | Make_Assignment_Statement (Loc, | |
1214 | Name => New_Occurrence_Of (Rnn, Loc), | |
1215 | Expression => RHS)); | |
1216 | ||
1217 | Insert_Actions (Op, New_List ( | |
1218 | Make_Object_Declaration (Loc, | |
1219 | Defining_Identifier => Rnn, | |
1220 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1221 | Blk)); | |
1222 | ||
1223 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1224 | Analyze_And_Resolve (Op); | |
1225 | end; | |
1226 | ||
412f75eb | 1227 | -- Here we know the result is Long_Long_Integer'Base, of that it has |
1228 | -- been rewritten because the parent operation is a conversion. See | |
0df9d43f | 1229 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1230 | |
1231 | else | |
f32c377d | 1232 | pragma Assert |
1233 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1234 | |
1235 | -- All we need to do here is to convert the result to the proper | |
1236 | -- result type. As explained above for the Bignum case, we can | |
1237 | -- omit this if our parent is a type conversion. | |
1238 | ||
1239 | if Nkind (P) /= N_Type_Conversion then | |
1240 | Convert_To_And_Rewrite (Result_Type, Op); | |
1241 | end if; | |
1242 | ||
1243 | Analyze_And_Resolve (Op); | |
1244 | end if; | |
1245 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1246 | |
ee6ba406 | 1247 | ---------------------------- |
1248 | -- Apply_Constraint_Check -- | |
1249 | ---------------------------- | |
1250 | ||
1251 | procedure Apply_Constraint_Check | |
1252 | (N : Node_Id; | |
1253 | Typ : Entity_Id; | |
1254 | No_Sliding : Boolean := False) | |
1255 | is | |
1256 | Desig_Typ : Entity_Id; | |
1257 | ||
1258 | begin | |
7aafae1c | 1259 | -- No checks inside a generic (check the instantiations) |
1260 | ||
ee6ba406 | 1261 | if Inside_A_Generic then |
1262 | return; | |
7aafae1c | 1263 | end if; |
ee6ba406 | 1264 | |
6fb3c314 | 1265 | -- Apply required constraint checks |
7aafae1c | 1266 | |
1267 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1268 | Apply_Scalar_Range_Check (N, Typ); |
1269 | ||
1270 | elsif Is_Array_Type (Typ) then | |
1271 | ||
05fcfafb | 1272 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1273 | -- always has the right bounds. |
1274 | ||
1275 | if Nkind (N) = N_Aggregate | |
1276 | and then No (Expressions (N)) | |
1277 | and then Nkind | |
1278 | (First (Choices (First (Component_Associations (N))))) | |
1279 | = N_Others_Choice | |
1280 | then | |
1281 | return; | |
1282 | end if; | |
1283 | ||
ee6ba406 | 1284 | if Is_Constrained (Typ) then |
1285 | Apply_Length_Check (N, Typ); | |
1286 | ||
1287 | if No_Sliding then | |
1288 | Apply_Range_Check (N, Typ); | |
1289 | end if; | |
1290 | else | |
1291 | Apply_Range_Check (N, Typ); | |
1292 | end if; | |
1293 | ||
4fb5f0a0 | 1294 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1295 | and then Has_Discriminants (Base_Type (Typ)) |
1296 | and then Is_Constrained (Typ) | |
1297 | then | |
1298 | Apply_Discriminant_Check (N, Typ); | |
1299 | ||
1300 | elsif Is_Access_Type (Typ) then | |
1301 | ||
1302 | Desig_Typ := Designated_Type (Typ); | |
1303 | ||
1304 | -- No checks necessary if expression statically null | |
1305 | ||
2af58f67 | 1306 | if Known_Null (N) then |
00c403ee | 1307 | if Can_Never_Be_Null (Typ) then |
1308 | Install_Null_Excluding_Check (N); | |
1309 | end if; | |
ee6ba406 | 1310 | |
1311 | -- No sliding possible on access to arrays | |
1312 | ||
1313 | elsif Is_Array_Type (Desig_Typ) then | |
1314 | if Is_Constrained (Desig_Typ) then | |
1315 | Apply_Length_Check (N, Typ); | |
1316 | end if; | |
1317 | ||
1318 | Apply_Range_Check (N, Typ); | |
1319 | ||
1320 | elsif Has_Discriminants (Base_Type (Desig_Typ)) | |
1321 | and then Is_Constrained (Desig_Typ) | |
1322 | then | |
1323 | Apply_Discriminant_Check (N, Typ); | |
1324 | end if; | |
fa7497e8 | 1325 | |
bf3e1520 | 1326 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1327 | -- this check if the constraint node is illegal, as shown by having |
1328 | -- an error posted. This additional guard prevents cascaded errors | |
1329 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1330 | ||
fa7497e8 | 1331 | if Can_Never_Be_Null (Typ) |
1332 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1333 | and then not Error_Posted (N) |
fa7497e8 | 1334 | then |
1335 | Install_Null_Excluding_Check (N); | |
1336 | end if; | |
ee6ba406 | 1337 | end if; |
1338 | end Apply_Constraint_Check; | |
1339 | ||
1340 | ------------------------------ | |
1341 | -- Apply_Discriminant_Check -- | |
1342 | ------------------------------ | |
1343 | ||
1344 | procedure Apply_Discriminant_Check | |
1345 | (N : Node_Id; | |
1346 | Typ : Entity_Id; | |
1347 | Lhs : Node_Id := Empty) | |
1348 | is | |
1349 | Loc : constant Source_Ptr := Sloc (N); | |
1350 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1351 | S_Typ : Entity_Id := Etype (N); | |
1352 | Cond : Node_Id; | |
1353 | T_Typ : Entity_Id; | |
1354 | ||
7be5088a | 1355 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1356 | -- A heap object with an indefinite subtype is constrained by its | |
1357 | -- initial value, and assigning to it requires a constraint_check. | |
1358 | -- The target may be an explicit dereference, or a renaming of one. | |
1359 | ||
ee6ba406 | 1360 | function Is_Aliased_Unconstrained_Component return Boolean; |
1361 | -- It is possible for an aliased component to have a nominal | |
1362 | -- unconstrained subtype (through instantiation). If this is a | |
1363 | -- discriminated component assigned in the expansion of an aggregate | |
1364 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1365 | -- situation requires a predicate of its own. |
ee6ba406 | 1366 | |
7be5088a | 1367 | ---------------------------------- |
1368 | -- Denotes_Explicit_Dereference -- | |
1369 | ---------------------------------- | |
1370 | ||
1371 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1372 | begin | |
1373 | return | |
1374 | Nkind (Obj) = N_Explicit_Dereference | |
1375 | or else | |
1376 | (Is_Entity_Name (Obj) | |
1377 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1378 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1379 | N_Explicit_Dereference); | |
7be5088a | 1380 | end Denotes_Explicit_Dereference; |
1381 | ||
ee6ba406 | 1382 | ---------------------------------------- |
1383 | -- Is_Aliased_Unconstrained_Component -- | |
1384 | ---------------------------------------- | |
1385 | ||
1386 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1387 | Comp : Entity_Id; | |
1388 | Pref : Node_Id; | |
1389 | ||
1390 | begin | |
1391 | if Nkind (Lhs) /= N_Selected_Component then | |
1392 | return False; | |
1393 | else | |
1394 | Comp := Entity (Selector_Name (Lhs)); | |
1395 | Pref := Prefix (Lhs); | |
1396 | end if; | |
1397 | ||
1398 | if Ekind (Comp) /= E_Component | |
1399 | or else not Is_Aliased (Comp) | |
1400 | then | |
1401 | return False; | |
1402 | end if; | |
1403 | ||
1404 | return not Comes_From_Source (Pref) | |
1405 | and then In_Instance | |
1406 | and then not Is_Constrained (Etype (Comp)); | |
1407 | end Is_Aliased_Unconstrained_Component; | |
1408 | ||
1409 | -- Start of processing for Apply_Discriminant_Check | |
1410 | ||
1411 | begin | |
1412 | if Do_Access then | |
1413 | T_Typ := Designated_Type (Typ); | |
1414 | else | |
1415 | T_Typ := Typ; | |
1416 | end if; | |
1417 | ||
1418 | -- Nothing to do if discriminant checks are suppressed or else no code | |
1419 | -- is to be generated | |
1420 | ||
6dbcfcd9 | 1421 | if not Full_Expander_Active |
ee6ba406 | 1422 | or else Discriminant_Checks_Suppressed (T_Typ) |
1423 | then | |
1424 | return; | |
1425 | end if; | |
1426 | ||
feff2f05 | 1427 | -- No discriminant checks necessary for an access when expression is |
1428 | -- statically Null. This is not only an optimization, it is fundamental | |
1429 | -- because otherwise discriminant checks may be generated in init procs | |
1430 | -- for types containing an access to a not-yet-frozen record, causing a | |
1431 | -- deadly forward reference. | |
ee6ba406 | 1432 | |
feff2f05 | 1433 | -- Also, if the expression is of an access type whose designated type is |
1434 | -- incomplete, then the access value must be null and we suppress the | |
1435 | -- check. | |
ee6ba406 | 1436 | |
2af58f67 | 1437 | if Known_Null (N) then |
ee6ba406 | 1438 | return; |
1439 | ||
1440 | elsif Is_Access_Type (S_Typ) then | |
1441 | S_Typ := Designated_Type (S_Typ); | |
1442 | ||
1443 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1444 | return; | |
1445 | end if; | |
1446 | end if; | |
1447 | ||
0577b0b1 | 1448 | -- If an assignment target is present, then we need to generate the |
1449 | -- actual subtype if the target is a parameter or aliased object with | |
1450 | -- an unconstrained nominal subtype. | |
1451 | ||
1452 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1453 | -- subtype to the parameter and dereference cases, since other aliased | |
1454 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1455 | -- constrained). |
ee6ba406 | 1456 | |
1457 | if Present (Lhs) | |
1458 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1459 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1460 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1461 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1462 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1463 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1464 | and then not Is_Constrained (T_Typ) |
7be5088a | 1465 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1466 | and then Nkind (Original_Node (Lhs)) /= |
1467 | N_Function_Call)) | |
ee6ba406 | 1468 | then |
1469 | T_Typ := Get_Actual_Subtype (Lhs); | |
1470 | end if; | |
1471 | ||
feff2f05 | 1472 | -- Nothing to do if the type is unconstrained (this is the case where |
1473 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1474 | -- is required). | |
ee6ba406 | 1475 | |
1476 | if not Is_Constrained (T_Typ) then | |
1477 | return; | |
05fcfafb | 1478 | |
1479 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1480 | -- partial view that is constrained. | |
1481 | ||
de54c5ab | 1482 | elsif Ada_Version >= Ada_2005 |
0d78d2d4 | 1483 | and then Object_Type_Has_Constrained_Partial_View |
d41a3f41 | 1484 | (Typ => Base_Type (T_Typ), |
1485 | Scop => Current_Scope) | |
05fcfafb | 1486 | then |
1487 | return; | |
ee6ba406 | 1488 | end if; |
1489 | ||
00f91aef | 1490 | -- Nothing to do if the type is an Unchecked_Union |
1491 | ||
1492 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1493 | return; | |
1494 | end if; | |
1495 | ||
feff2f05 | 1496 | -- Suppress checks if the subtypes are the same. the check must be |
1497 | -- preserved in an assignment to a formal, because the constraint is | |
1498 | -- given by the actual. | |
ee6ba406 | 1499 | |
1500 | if Nkind (Original_Node (N)) /= N_Allocator | |
1501 | and then (No (Lhs) | |
1502 | or else not Is_Entity_Name (Lhs) | |
9dfe12ae | 1503 | or else No (Param_Entity (Lhs))) |
ee6ba406 | 1504 | then |
1505 | if (Etype (N) = Typ | |
1506 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1507 | and then not Is_Aliased_View (Lhs) | |
1508 | then | |
1509 | return; | |
1510 | end if; | |
1511 | ||
feff2f05 | 1512 | -- We can also eliminate checks on allocators with a subtype mark that |
1513 | -- coincides with the context type. The context type may be a subtype | |
1514 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1515 | |
1516 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1517 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1518 | then | |
1519 | declare | |
9dfe12ae | 1520 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1521 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1522 | |
1523 | begin | |
1524 | if Alloc_Typ = T_Typ | |
1525 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1526 | and then Is_Entity_Name ( | |
1527 | Subtype_Indication (Parent (T_Typ))) | |
1528 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1529 | ||
1530 | then | |
1531 | return; | |
1532 | end if; | |
1533 | end; | |
1534 | end if; | |
1535 | ||
feff2f05 | 1536 | -- See if we have a case where the types are both constrained, and all |
1537 | -- the constraints are constants. In this case, we can do the check | |
1538 | -- successfully at compile time. | |
ee6ba406 | 1539 | |
d7ec9a29 | 1540 | -- We skip this check for the case where the node is rewritten`as |
1541 | -- an allocator, because it already carries the context subtype, | |
1542 | -- and extracting the discriminants from the aggregate is messy. | |
ee6ba406 | 1543 | |
1544 | if Is_Constrained (S_Typ) | |
1545 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1546 | then | |
1547 | declare | |
1548 | DconT : Elmt_Id; | |
1549 | Discr : Entity_Id; | |
1550 | DconS : Elmt_Id; | |
1551 | ItemS : Node_Id; | |
1552 | ItemT : Node_Id; | |
1553 | ||
1554 | begin | |
1555 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1556 | -- private type completed by a default discriminated type. In that |
1557 | -- case, we need to get the constraints from the underlying_type. | |
1558 | -- If the underlying type is unconstrained (i.e. has no default | |
1559 | -- discriminants) no check is needed. | |
ee6ba406 | 1560 | |
1561 | if Has_Discriminants (S_Typ) then | |
1562 | Discr := First_Discriminant (S_Typ); | |
1563 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1564 | ||
1565 | else | |
1566 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1567 | DconS := | |
1568 | First_Elmt | |
1569 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1570 | ||
1571 | if No (DconS) then | |
1572 | return; | |
1573 | end if; | |
fccb5da7 | 1574 | |
1575 | -- A further optimization: if T_Typ is derived from S_Typ | |
1576 | -- without imposing a constraint, no check is needed. | |
1577 | ||
1578 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1579 | N_Full_Type_Declaration | |
1580 | then | |
1581 | declare | |
5c61a0ff | 1582 | Type_Def : constant Node_Id := |
b6341c67 | 1583 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1584 | begin |
1585 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1586 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1587 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1588 | then | |
1589 | return; | |
1590 | end if; | |
1591 | end; | |
1592 | end if; | |
ee6ba406 | 1593 | end if; |
1594 | ||
86594966 | 1595 | -- Constraint may appear in full view of type |
1596 | ||
1597 | if Ekind (T_Typ) = E_Private_Subtype | |
1598 | and then Present (Full_View (T_Typ)) | |
1599 | then | |
d7ec9a29 | 1600 | DconT := |
86594966 | 1601 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
86594966 | 1602 | else |
d7ec9a29 | 1603 | DconT := |
1604 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
86594966 | 1605 | end if; |
ee6ba406 | 1606 | |
1607 | while Present (Discr) loop | |
1608 | ItemS := Node (DconS); | |
1609 | ItemT := Node (DconT); | |
1610 | ||
00c403ee | 1611 | -- For a discriminated component type constrained by the |
1612 | -- current instance of an enclosing type, there is no | |
1613 | -- applicable discriminant check. | |
1614 | ||
1615 | if Nkind (ItemT) = N_Attribute_Reference | |
1616 | and then Is_Access_Type (Etype (ItemT)) | |
1617 | and then Is_Entity_Name (Prefix (ItemT)) | |
1618 | and then Is_Type (Entity (Prefix (ItemT))) | |
1619 | then | |
1620 | return; | |
1621 | end if; | |
1622 | ||
cc60bd16 | 1623 | -- If the expressions for the discriminants are identical |
1624 | -- and it is side-effect free (for now just an entity), | |
1625 | -- this may be a shared constraint, e.g. from a subtype | |
1626 | -- without a constraint introduced as a generic actual. | |
1627 | -- Examine other discriminants if any. | |
1628 | ||
1629 | if ItemS = ItemT | |
1630 | and then Is_Entity_Name (ItemS) | |
1631 | then | |
1632 | null; | |
1633 | ||
1634 | elsif not Is_OK_Static_Expression (ItemS) | |
1635 | or else not Is_OK_Static_Expression (ItemT) | |
1636 | then | |
1637 | exit; | |
ee6ba406 | 1638 | |
cc60bd16 | 1639 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1640 | if Do_Access then -- needs run-time check. |
1641 | exit; | |
1642 | else | |
1643 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1644 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1645 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1646 | return; |
1647 | end if; | |
1648 | end if; | |
1649 | ||
1650 | Next_Elmt (DconS); | |
1651 | Next_Elmt (DconT); | |
1652 | Next_Discriminant (Discr); | |
1653 | end loop; | |
1654 | ||
1655 | if No (Discr) then | |
1656 | return; | |
1657 | end if; | |
1658 | end; | |
1659 | end if; | |
1660 | ||
1661 | -- Here we need a discriminant check. First build the expression | |
1662 | -- for the comparisons of the discriminants: | |
1663 | ||
1664 | -- (n.disc1 /= typ.disc1) or else | |
1665 | -- (n.disc2 /= typ.disc2) or else | |
1666 | -- ... | |
1667 | -- (n.discn /= typ.discn) | |
1668 | ||
1669 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1670 | ||
3cce7f32 | 1671 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1672 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1673 | |
1674 | if Present (Param_Entity (Lhs)) then | |
1675 | Cond := | |
1676 | Make_And_Then (Loc, | |
1677 | Left_Opnd => | |
1678 | Make_Attribute_Reference (Loc, | |
1679 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1680 | Attribute_Name => Name_Constrained), | |
1681 | Right_Opnd => Cond); | |
1682 | end if; | |
1683 | ||
1684 | if Do_Access then | |
1685 | Cond := Guard_Access (Cond, Loc, N); | |
1686 | end if; | |
1687 | ||
1688 | Insert_Action (N, | |
f15731c4 | 1689 | Make_Raise_Constraint_Error (Loc, |
1690 | Condition => Cond, | |
1691 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1692 | end Apply_Discriminant_Check; |
1693 | ||
2fe22c69 | 1694 | ------------------------- |
1695 | -- Apply_Divide_Checks -- | |
1696 | ------------------------- | |
ee6ba406 | 1697 | |
2fe22c69 | 1698 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1699 | Loc : constant Source_Ptr := Sloc (N); |
1700 | Typ : constant Entity_Id := Etype (N); | |
1701 | Left : constant Node_Id := Left_Opnd (N); | |
1702 | Right : constant Node_Id := Right_Opnd (N); | |
1703 | ||
db415383 | 1704 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1705 | -- Current overflow checking mode |
1706 | ||
ee6ba406 | 1707 | LLB : Uint; |
1708 | Llo : Uint; | |
1709 | Lhi : Uint; | |
1710 | LOK : Boolean; | |
1711 | Rlo : Uint; | |
1712 | Rhi : Uint; | |
2fe22c69 | 1713 | ROK : Boolean; |
96da3284 | 1714 | |
1715 | pragma Warnings (Off, Lhi); | |
1716 | -- Don't actually use this value | |
ee6ba406 | 1717 | |
1718 | begin | |
0df9d43f | 1719 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1720 | -- operating on signed integer types, then the only thing this routine | |
1721 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1722 | -- procedure will (possibly later on during recursive downward calls), | |
1723 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1724 | |
1725 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1726 | and then Is_Signed_Integer_Type (Typ) |
1727 | then | |
1728 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1729 | return; | |
1730 | end if; | |
1731 | ||
1732 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1733 | ||
6dbcfcd9 | 1734 | if Full_Expander_Active |
13dbf220 | 1735 | and then not Backend_Divide_Checks_On_Target |
1736 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1737 | then |
9c486805 | 1738 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1739 | |
2fe22c69 | 1740 | -- Deal with division check |
ee6ba406 | 1741 | |
2fe22c69 | 1742 | if Do_Division_Check (N) |
1743 | and then not Division_Checks_Suppressed (Typ) | |
1744 | then | |
1745 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1746 | end if; |
1747 | ||
2fe22c69 | 1748 | -- Deal with overflow check |
1749 | ||
0df9d43f | 1750 | if Do_Overflow_Check (N) |
1751 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1752 | then | |
2fe22c69 | 1753 | |
1754 | -- Test for extremely annoying case of xxx'First divided by -1 | |
1755 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1756 | |
ee6ba406 | 1757 | if Nkind (N) = N_Op_Divide |
1758 | and then Is_Signed_Integer_Type (Typ) | |
1759 | then | |
9c486805 | 1760 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1761 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1762 | ||
1763 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1764 | and then |
1765 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1766 | then |
1767 | Insert_Action (N, | |
1768 | Make_Raise_Constraint_Error (Loc, | |
1769 | Condition => | |
1770 | Make_And_Then (Loc, | |
2fe22c69 | 1771 | Left_Opnd => |
1772 | Make_Op_Eq (Loc, | |
1773 | Left_Opnd => | |
1774 | Duplicate_Subexpr_Move_Checks (Left), | |
1775 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1776 | |
2fe22c69 | 1777 | Right_Opnd => |
1778 | Make_Op_Eq (Loc, | |
1779 | Left_Opnd => Duplicate_Subexpr (Right), | |
1780 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1781 | |
f15731c4 | 1782 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1783 | end if; |
1784 | end if; | |
1785 | end if; | |
1786 | end if; | |
2fe22c69 | 1787 | end Apply_Divide_Checks; |
1788 | ||
1789 | -------------------------- | |
1790 | -- Apply_Division_Check -- | |
1791 | -------------------------- | |
1792 | ||
1793 | procedure Apply_Division_Check | |
1794 | (N : Node_Id; | |
1795 | Rlo : Uint; | |
1796 | Rhi : Uint; | |
1797 | ROK : Boolean) | |
1798 | is | |
1799 | pragma Assert (Do_Division_Check (N)); | |
1800 | ||
1801 | Loc : constant Source_Ptr := Sloc (N); | |
1802 | Right : constant Node_Id := Right_Opnd (N); | |
1803 | ||
1804 | begin | |
1805 | if Full_Expander_Active | |
1806 | and then not Backend_Divide_Checks_On_Target | |
1807 | and then Check_Needed (Right, Division_Check) | |
1808 | then | |
1809 | -- See if division by zero possible, and if so generate test. This | |
1810 | -- part of the test is not controlled by the -gnato switch, since | |
1811 | -- it is a Division_Check and not an Overflow_Check. | |
1812 | ||
1813 | if Do_Division_Check (N) then | |
1814 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then | |
1815 | Insert_Action (N, | |
1816 | Make_Raise_Constraint_Error (Loc, | |
1817 | Condition => | |
1818 | Make_Op_Eq (Loc, | |
1819 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1820 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
1821 | Reason => CE_Divide_By_Zero)); | |
1822 | end if; | |
1823 | end if; | |
1824 | end if; | |
1825 | end Apply_Division_Check; | |
ee6ba406 | 1826 | |
5329ca64 | 1827 | ---------------------------------- |
1828 | -- Apply_Float_Conversion_Check -- | |
1829 | ---------------------------------- | |
1830 | ||
feff2f05 | 1831 | -- Let F and I be the source and target types of the conversion. The RM |
1832 | -- specifies that a floating-point value X is rounded to the nearest | |
1833 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1834 | -- value of X is checked against I'Range. | |
1835 | ||
1836 | -- The catch in the above paragraph is that there is no good way to know | |
1837 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1838 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1839 | |
5329ca64 | 1840 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1841 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1842 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1843 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1844 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1845 | -- sign of I'First and I'Last. | |
5329ca64 | 1846 | -- (5) X may be a NaN, which will fail any comparison |
1847 | ||
2af58f67 | 1848 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1849 | |
5329ca64 | 1850 | -- (1) If either I'First or I'Last is not known at compile time, use |
1851 | -- I'Base instead of I in the next three steps and perform a | |
1852 | -- regular range check against I'Range after conversion. | |
1853 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1854 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1855 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1856 | -- In other words, take one of the closest floating-point numbers | |
1857 | -- (which is an integer value) to I'First, and see if it is in | |
1858 | -- range or not. | |
5329ca64 | 1859 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1860 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1861 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1862 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1863 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1864 | ||
2af58f67 | 1865 | -- For the truncating case, replace steps (2) and (3) as follows: |
1866 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1867 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1868 | -- Lo_OK be True. | |
1869 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1870 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1871 | -- Hi_OK be True. |
2af58f67 | 1872 | |
5329ca64 | 1873 | procedure Apply_Float_Conversion_Check |
1874 | (Ck_Node : Node_Id; | |
1875 | Target_Typ : Entity_Id) | |
1876 | is | |
feff2f05 | 1877 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1878 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1879 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1880 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1881 | Target_Base : constant Entity_Id := |
b6341c67 | 1882 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1883 | |
2af58f67 | 1884 | Par : constant Node_Id := Parent (Ck_Node); |
1885 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1886 | -- Parent of check node, must be a type conversion | |
1887 | ||
1888 | Truncate : constant Boolean := Float_Truncate (Par); | |
1889 | Max_Bound : constant Uint := | |
b6341c67 | 1890 | UI_Expon |
1891 | (Machine_Radix_Value (Expr_Type), | |
1892 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 1893 | |
5329ca64 | 1894 | -- Largest bound, so bound plus or minus half is a machine number of F |
1895 | ||
feff2f05 | 1896 | Ifirst, Ilast : Uint; |
1897 | -- Bounds of integer type | |
1898 | ||
1899 | Lo, Hi : Ureal; | |
1900 | -- Bounds to check in floating-point domain | |
5329ca64 | 1901 | |
feff2f05 | 1902 | Lo_OK, Hi_OK : Boolean; |
1903 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1904 | |
feff2f05 | 1905 | Lo_Chk, Hi_Chk : Node_Id; |
1906 | -- Expressions that are False iff check fails | |
1907 | ||
1908 | Reason : RT_Exception_Code; | |
5329ca64 | 1909 | |
1910 | begin | |
41f06abf | 1911 | -- We do not need checks if we are not generating code (i.e. the full |
1912 | -- expander is not active). In SPARK mode, we specifically don't want | |
1913 | -- the frontend to expand these checks, which are dealt with directly | |
1914 | -- in the formal verification backend. | |
1915 | ||
1916 | if not Full_Expander_Active then | |
1917 | return; | |
1918 | end if; | |
1919 | ||
5329ca64 | 1920 | if not Compile_Time_Known_Value (LB) |
1921 | or not Compile_Time_Known_Value (HB) | |
1922 | then | |
1923 | declare | |
feff2f05 | 1924 | -- First check that the value falls in the range of the base type, |
1925 | -- to prevent overflow during conversion and then perform a | |
1926 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 1927 | |
5329ca64 | 1928 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 1929 | |
46eb6933 | 1930 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 1931 | |
1932 | begin | |
1933 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
1934 | Set_Etype (Temp, Target_Base); | |
1935 | ||
1936 | Insert_Action (Parent (Par), | |
1937 | Make_Object_Declaration (Loc, | |
1938 | Defining_Identifier => Temp, | |
1939 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
1940 | Expression => New_Copy_Tree (Par)), | |
1941 | Suppress => All_Checks); | |
1942 | ||
1943 | Insert_Action (Par, | |
1944 | Make_Raise_Constraint_Error (Loc, | |
1945 | Condition => | |
1946 | Make_Not_In (Loc, | |
1947 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
1948 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
1949 | Reason => CE_Range_Check_Failed)); | |
1950 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
1951 | ||
1952 | return; | |
1953 | end; | |
1954 | end if; | |
1955 | ||
7d86aa98 | 1956 | -- Get the (static) bounds of the target type |
5329ca64 | 1957 | |
1958 | Ifirst := Expr_Value (LB); | |
1959 | Ilast := Expr_Value (HB); | |
1960 | ||
7d86aa98 | 1961 | -- A simple optimization: if the expression is a universal literal, |
1962 | -- we can do the comparison with the bounds and the conversion to | |
1963 | -- an integer type statically. The range checks are unchanged. | |
1964 | ||
1965 | if Nkind (Ck_Node) = N_Real_Literal | |
1966 | and then Etype (Ck_Node) = Universal_Real | |
1967 | and then Is_Integer_Type (Target_Typ) | |
1968 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
1969 | then | |
1970 | declare | |
1971 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
1972 | ||
1973 | begin | |
1974 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
1975 | ||
4309515d | 1976 | -- Conversion is safe |
7d86aa98 | 1977 | |
1978 | Rewrite (Parent (Ck_Node), | |
1979 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
1980 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
1981 | return; | |
1982 | end if; | |
1983 | end; | |
1984 | end if; | |
1985 | ||
5329ca64 | 1986 | -- Check against lower bound |
1987 | ||
2af58f67 | 1988 | if Truncate and then Ifirst > 0 then |
1989 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
1990 | Lo_OK := False; | |
1991 | ||
1992 | elsif Truncate then | |
1993 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
1994 | Lo_OK := True; | |
1995 | ||
1996 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 1997 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
1998 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 1999 | |
5329ca64 | 2000 | else |
2001 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
2002 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
2003 | end if; | |
2004 | ||
2005 | if Lo_OK then | |
2006 | ||
2007 | -- Lo_Chk := (X >= Lo) | |
2008 | ||
2009 | Lo_Chk := Make_Op_Ge (Loc, | |
2010 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2011 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2012 | ||
2013 | else | |
2014 | -- Lo_Chk := (X > Lo) | |
2015 | ||
2016 | Lo_Chk := Make_Op_Gt (Loc, | |
2017 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2018 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2019 | end if; | |
2020 | ||
2021 | -- Check against higher bound | |
2022 | ||
2af58f67 | 2023 | if Truncate and then Ilast < 0 then |
2024 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2025 | Hi_OK := False; |
2af58f67 | 2026 | |
2027 | elsif Truncate then | |
2028 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2029 | Hi_OK := True; | |
2030 | ||
2031 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2032 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2033 | Hi_OK := (Ilast < 0); | |
2034 | else | |
2035 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2036 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2037 | end if; | |
2038 | ||
2039 | if Hi_OK then | |
2040 | ||
2041 | -- Hi_Chk := (X <= Hi) | |
2042 | ||
2043 | Hi_Chk := Make_Op_Le (Loc, | |
2044 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2045 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2046 | ||
2047 | else | |
2048 | -- Hi_Chk := (X < Hi) | |
2049 | ||
2050 | Hi_Chk := Make_Op_Lt (Loc, | |
2051 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2052 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2053 | end if; | |
2054 | ||
feff2f05 | 2055 | -- If the bounds of the target type are the same as those of the base |
2056 | -- type, the check is an overflow check as a range check is not | |
2057 | -- performed in these cases. | |
5329ca64 | 2058 | |
2059 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2060 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2061 | then | |
2062 | Reason := CE_Overflow_Check_Failed; | |
2063 | else | |
2064 | Reason := CE_Range_Check_Failed; | |
2065 | end if; | |
2066 | ||
2067 | -- Raise CE if either conditions does not hold | |
2068 | ||
2069 | Insert_Action (Ck_Node, | |
2070 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2071 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2072 | Reason => Reason)); |
2073 | end Apply_Float_Conversion_Check; | |
2074 | ||
ee6ba406 | 2075 | ------------------------ |
2076 | -- Apply_Length_Check -- | |
2077 | ------------------------ | |
2078 | ||
2079 | procedure Apply_Length_Check | |
2080 | (Ck_Node : Node_Id; | |
2081 | Target_Typ : Entity_Id; | |
2082 | Source_Typ : Entity_Id := Empty) | |
2083 | is | |
2084 | begin | |
2085 | Apply_Selected_Length_Checks | |
2086 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2087 | end Apply_Length_Check; | |
2088 | ||
3b045963 | 2089 | ------------------------------------- |
2090 | -- Apply_Parameter_Aliasing_Checks -- | |
2091 | ------------------------------------- | |
b73adb97 | 2092 | |
3b045963 | 2093 | procedure Apply_Parameter_Aliasing_Checks |
2094 | (Call : Node_Id; | |
2095 | Subp : Entity_Id) | |
2096 | is | |
bb569db0 | 2097 | Loc : constant Source_Ptr := Sloc (Call); |
2098 | ||
3b045963 | 2099 | function May_Cause_Aliasing |
2100 | (Formal_1 : Entity_Id; | |
2101 | Formal_2 : Entity_Id) return Boolean; | |
2102 | -- Determine whether two formal parameters can alias each other | |
2103 | -- depending on their modes. | |
2104 | ||
2105 | function Original_Actual (N : Node_Id) return Node_Id; | |
2106 | -- The expander may replace an actual with a temporary for the sake of | |
2107 | -- side effect removal. The temporary may hide a potential aliasing as | |
2108 | -- it does not share the address of the actual. This routine attempts | |
2109 | -- to retrieve the original actual. | |
2110 | ||
bb569db0 | 2111 | procedure Overlap_Check |
2112 | (Actual_1 : Node_Id; | |
2113 | Actual_2 : Node_Id; | |
2114 | Formal_1 : Entity_Id; | |
2115 | Formal_2 : Entity_Id; | |
2116 | Check : in out Node_Id); | |
2117 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2118 | -- If detailed exception messages are enabled, the check is augmented to | |
2119 | -- provide information about the names of the corresponding formals. See | |
2120 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2121 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2122 | -- Check contains all and-ed simple tests generated so far or remains | |
2123 | -- unchanged in the case of detailed exception messaged. | |
2124 | ||
3b045963 | 2125 | ------------------------ |
2126 | -- May_Cause_Aliasing -- | |
2127 | ------------------------ | |
b73adb97 | 2128 | |
3b045963 | 2129 | function May_Cause_Aliasing |
4a9e7f0c | 2130 | (Formal_1 : Entity_Id; |
3b045963 | 2131 | Formal_2 : Entity_Id) return Boolean |
2132 | is | |
2133 | begin | |
2134 | -- The following combination cannot lead to aliasing | |
2135 | ||
2136 | -- Formal 1 Formal 2 | |
2137 | -- IN IN | |
2138 | ||
2139 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2140 | and then |
2141 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2142 | then |
2143 | return False; | |
2144 | ||
2145 | -- The following combinations may lead to aliasing | |
2146 | ||
2147 | -- Formal 1 Formal 2 | |
2148 | -- IN OUT | |
2149 | -- IN IN OUT | |
2150 | -- OUT IN | |
2151 | -- OUT IN OUT | |
2152 | -- OUT OUT | |
2153 | ||
2154 | else | |
2155 | return True; | |
2156 | end if; | |
2157 | end May_Cause_Aliasing; | |
2158 | ||
2159 | --------------------- | |
2160 | -- Original_Actual -- | |
2161 | --------------------- | |
2162 | ||
2163 | function Original_Actual (N : Node_Id) return Node_Id is | |
2164 | begin | |
2165 | if Nkind (N) = N_Type_Conversion then | |
2166 | return Expression (N); | |
2167 | ||
2168 | -- The expander created a temporary to capture the result of a type | |
2169 | -- conversion where the expression is the real actual. | |
2170 | ||
2171 | elsif Nkind (N) = N_Identifier | |
2172 | and then Present (Original_Node (N)) | |
2173 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2174 | then | |
2175 | return Expression (Original_Node (N)); | |
2176 | end if; | |
2177 | ||
2178 | return N; | |
2179 | end Original_Actual; | |
2180 | ||
bb569db0 | 2181 | ------------------- |
2182 | -- Overlap_Check -- | |
2183 | ------------------- | |
2184 | ||
2185 | procedure Overlap_Check | |
2186 | (Actual_1 : Node_Id; | |
2187 | Actual_2 : Node_Id; | |
2188 | Formal_1 : Entity_Id; | |
2189 | Formal_2 : Entity_Id; | |
2190 | Check : in out Node_Id) | |
2191 | is | |
2192 | Cond : Node_Id; | |
2193 | ||
2194 | begin | |
2195 | -- Generate: | |
2196 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2197 | ||
2198 | Cond := | |
2199 | Make_Attribute_Reference (Loc, | |
2200 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2201 | Attribute_Name => Name_Overlaps_Storage, | |
2202 | Expressions => | |
2203 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2204 | ||
2205 | -- Generate the following check when detailed exception messages are | |
2206 | -- enabled: | |
2207 | ||
2208 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2209 | -- raise Program_Error with <detailed message>; | |
2210 | -- end if; | |
2211 | ||
2212 | if Exception_Extra_Info then | |
2213 | Start_String; | |
2214 | ||
2215 | -- Do not generate location information for internal calls | |
2216 | ||
2217 | if Comes_From_Source (Call) then | |
2218 | Store_String_Chars (Build_Location_String (Loc)); | |
2219 | Store_String_Char (' '); | |
2220 | end if; | |
2221 | ||
2222 | Store_String_Chars ("aliased parameters, actuals for """); | |
2223 | Store_String_Chars (Get_Name_String (Chars (Formal_1))); | |
2224 | Store_String_Chars (""" and """); | |
2225 | Store_String_Chars (Get_Name_String (Chars (Formal_2))); | |
2226 | Store_String_Chars (""" overlap"); | |
2227 | ||
2228 | Insert_Action (Call, | |
2229 | Make_If_Statement (Loc, | |
2230 | Condition => Cond, | |
2231 | Then_Statements => New_List ( | |
2232 | Make_Raise_Statement (Loc, | |
2233 | Name => | |
2234 | New_Reference_To (Standard_Program_Error, Loc), | |
2235 | Expression => Make_String_Literal (Loc, End_String))))); | |
2236 | ||
2237 | -- Create a sequence of overlapping checks by and-ing them all | |
2238 | -- together. | |
2239 | ||
2240 | else | |
2241 | if No (Check) then | |
2242 | Check := Cond; | |
2243 | else | |
2244 | Check := | |
2245 | Make_And_Then (Loc, | |
2246 | Left_Opnd => Check, | |
2247 | Right_Opnd => Cond); | |
2248 | end if; | |
2249 | end if; | |
2250 | end Overlap_Check; | |
2251 | ||
3b045963 | 2252 | -- Local variables |
2253 | ||
3b045963 | 2254 | Actual_1 : Node_Id; |
2255 | Actual_2 : Node_Id; | |
2256 | Check : Node_Id; | |
3b045963 | 2257 | Formal_1 : Entity_Id; |
2258 | Formal_2 : Entity_Id; | |
2259 | ||
2260 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2261 | ||
2262 | begin | |
bb569db0 | 2263 | Check := Empty; |
3b045963 | 2264 | |
2265 | Actual_1 := First_Actual (Call); | |
2266 | Formal_1 := First_Formal (Subp); | |
2267 | while Present (Actual_1) and then Present (Formal_1) loop | |
2268 | ||
2269 | -- Ensure that the actual is an object that is not passed by value. | |
2270 | -- Elementary types are always passed by value, therefore actuals of | |
2271 | -- such types cannot lead to aliasing. | |
2272 | ||
2273 | if Is_Object_Reference (Original_Actual (Actual_1)) | |
2274 | and then not Is_Elementary_Type (Etype (Original_Actual (Actual_1))) | |
2275 | then | |
2276 | Actual_2 := Next_Actual (Actual_1); | |
2277 | Formal_2 := Next_Formal (Formal_1); | |
2278 | while Present (Actual_2) and then Present (Formal_2) loop | |
2279 | ||
2280 | -- The other actual we are testing against must also denote | |
2281 | -- a non pass-by-value object. Generate the check only when | |
2282 | -- the mode of the two formals may lead to aliasing. | |
2283 | ||
2284 | if Is_Object_Reference (Original_Actual (Actual_2)) | |
2285 | and then not | |
2286 | Is_Elementary_Type (Etype (Original_Actual (Actual_2))) | |
2287 | and then May_Cause_Aliasing (Formal_1, Formal_2) | |
2288 | then | |
bb569db0 | 2289 | Overlap_Check |
2290 | (Actual_1 => Actual_1, | |
2291 | Actual_2 => Actual_2, | |
2292 | Formal_1 => Formal_1, | |
2293 | Formal_2 => Formal_2, | |
2294 | Check => Check); | |
3b045963 | 2295 | end if; |
2296 | ||
2297 | Next_Actual (Actual_2); | |
2298 | Next_Formal (Formal_2); | |
2299 | end loop; | |
2300 | end if; | |
2301 | ||
2302 | Next_Actual (Actual_1); | |
2303 | Next_Formal (Formal_1); | |
2304 | end loop; | |
2305 | ||
bb569db0 | 2306 | -- Place a simple check right before the call |
3b045963 | 2307 | |
bb569db0 | 2308 | if Present (Check) and then not Exception_Extra_Info then |
3b045963 | 2309 | Insert_Action (Call, |
2310 | Make_Raise_Program_Error (Loc, | |
bb569db0 | 2311 | Condition => Check, |
2312 | Reason => PE_Aliased_Parameters)); | |
3b045963 | 2313 | end if; |
2314 | end Apply_Parameter_Aliasing_Checks; | |
2315 | ||
2316 | ------------------------------------- | |
2317 | -- Apply_Parameter_Validity_Checks -- | |
2318 | ------------------------------------- | |
2319 | ||
2320 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2321 | Subp_Decl : Node_Id; | |
b73adb97 | 2322 | |
4a9e7f0c | 2323 | procedure Add_Validity_Check |
2324 | (Context : Entity_Id; | |
2325 | PPC_Nam : Name_Id; | |
2326 | For_Result : Boolean := False); | |
2327 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
2328 | -- of Context. PPC_Nam denotes the pre or post condition pragma name. | |
2329 | -- Set flag For_Result when to verify the result of a function. | |
b73adb97 | 2330 | |
4a9e7f0c | 2331 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id); |
2332 | -- Create a pre or post condition pragma with name PPC_Nam which | |
2333 | -- tests expression Check. | |
b73adb97 | 2334 | |
b73adb97 | 2335 | ------------------------ |
2336 | -- Add_Validity_Check -- | |
2337 | ------------------------ | |
2338 | ||
2339 | procedure Add_Validity_Check | |
2340 | (Context : Entity_Id; | |
4a9e7f0c | 2341 | PPC_Nam : Name_Id; |
b73adb97 | 2342 | For_Result : Boolean := False) |
2343 | is | |
4a9e7f0c | 2344 | Loc : constant Source_Ptr := Sloc (Subp); |
2345 | Typ : constant Entity_Id := Etype (Context); | |
b73adb97 | 2346 | Check : Node_Id; |
2347 | Nam : Name_Id; | |
2348 | ||
2349 | begin | |
2350 | -- Pick the proper version of 'Valid depending on the type of the | |
2351 | -- context. If the context is not eligible for such a check, return. | |
2352 | ||
2353 | if Is_Scalar_Type (Typ) then | |
2354 | Nam := Name_Valid; | |
2355 | elsif not No_Scalar_Parts (Typ) then | |
2356 | Nam := Name_Valid_Scalars; | |
2357 | else | |
2358 | return; | |
2359 | end if; | |
2360 | ||
2361 | -- Step 1: Create the expression to verify the validity of the | |
2362 | -- context. | |
2363 | ||
2364 | Check := New_Reference_To (Context, Loc); | |
2365 | ||
2366 | -- When processing a function result, use 'Result. Generate | |
2367 | -- Context'Result | |
2368 | ||
2369 | if For_Result then | |
2370 | Check := | |
2371 | Make_Attribute_Reference (Loc, | |
2372 | Prefix => Check, | |
2373 | Attribute_Name => Name_Result); | |
2374 | end if; | |
2375 | ||
2376 | -- Generate: | |
2377 | -- Context['Result]'Valid[_Scalars] | |
2378 | ||
2379 | Check := | |
2380 | Make_Attribute_Reference (Loc, | |
2381 | Prefix => Check, | |
2382 | Attribute_Name => Nam); | |
2383 | ||
4a9e7f0c | 2384 | -- Step 2: Create a pre or post condition pragma |
2385 | ||
2386 | Build_PPC_Pragma (PPC_Nam, Check); | |
2387 | end Add_Validity_Check; | |
2388 | ||
2389 | ---------------------- | |
2390 | -- Build_PPC_Pragma -- | |
2391 | ---------------------- | |
b73adb97 | 2392 | |
4a9e7f0c | 2393 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id) is |
7c443ae8 | 2394 | Loc : constant Source_Ptr := Sloc (Subp); |
2395 | Decls : List_Id; | |
2396 | Prag : Node_Id; | |
4a9e7f0c | 2397 | |
2398 | begin | |
2399 | Prag := | |
2400 | Make_Pragma (Loc, | |
2401 | Pragma_Identifier => Make_Identifier (Loc, PPC_Nam), | |
2402 | Pragma_Argument_Associations => New_List ( | |
2403 | Make_Pragma_Argument_Association (Loc, | |
2404 | Chars => Name_Check, | |
2405 | Expression => Check))); | |
2406 | ||
2407 | -- Add a message unless exception messages are suppressed | |
2408 | ||
2409 | if not Exception_Locations_Suppressed then | |
2410 | Append_To (Pragma_Argument_Associations (Prag), | |
2411 | Make_Pragma_Argument_Association (Loc, | |
2412 | Chars => Name_Message, | |
2413 | Expression => | |
2414 | Make_String_Literal (Loc, | |
2415 | Strval => "failed " & Get_Name_String (PPC_Nam) & | |
2416 | " from " & Build_Location_String (Loc)))); | |
2417 | end if; | |
2418 | ||
2419 | -- Insert the pragma in the tree | |
2420 | ||
2421 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2422 | Add_Global_Declaration (Prag); | |
7c443ae8 | 2423 | Analyze (Prag); |
2424 | ||
2425 | -- PPC pragmas associated with subprogram bodies must be inserted in | |
2426 | -- the declarative part of the body. | |
2427 | ||
2428 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2429 | Decls := Declarations (Subp_Decl); | |
2430 | ||
2431 | if No (Decls) then | |
2432 | Decls := New_List; | |
2433 | Set_Declarations (Subp_Decl, Decls); | |
2434 | end if; | |
2435 | ||
1bd93de5 | 2436 | Prepend_To (Decls, Prag); |
7c443ae8 | 2437 | |
2438 | -- Ensure the proper visibility of the subprogram body and its | |
2439 | -- parameters. | |
2440 | ||
2441 | Push_Scope (Subp); | |
2442 | Analyze (Prag); | |
2443 | Pop_Scope; | |
2444 | ||
2445 | -- For subprogram declarations insert the PPC pragma right after the | |
2446 | -- declarative node. | |
2447 | ||
b73adb97 | 2448 | else |
7c443ae8 | 2449 | Insert_After_And_Analyze (Subp_Decl, Prag); |
b73adb97 | 2450 | end if; |
4a9e7f0c | 2451 | end Build_PPC_Pragma; |
2452 | ||
2453 | -- Local variables | |
2454 | ||
2455 | Formal : Entity_Id; | |
4a9e7f0c | 2456 | Subp_Spec : Node_Id; |
2457 | ||
3b045963 | 2458 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2459 | |
2460 | begin | |
4a9e7f0c | 2461 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2462 | |
4a9e7f0c | 2463 | Subp_Spec := Parent (Subp); |
a45d946f | 2464 | |
4a9e7f0c | 2465 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2466 | Subp_Spec := Parent (Subp_Spec); | |
2467 | end if; | |
a45d946f | 2468 | |
4a9e7f0c | 2469 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2470 | |
b73adb97 | 2471 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2472 | |
2473 | -- Do not process formal subprograms because the corresponding actual | |
2474 | -- will receive the proper checks when the instance is analyzed. | |
2475 | ||
2476 | or else Is_Formal_Subprogram (Subp) | |
2477 | ||
a45d946f | 2478 | -- Do not process imported subprograms since pre and post conditions |
2479 | -- are never verified on routines coming from a different language. | |
4a9e7f0c | 2480 | |
b73adb97 | 2481 | or else Is_Imported (Subp) |
2482 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2483 | |
a45d946f | 2484 | -- The PPC pragmas generated by this routine do not correspond to |
2485 | -- source aspects, therefore they cannot be applied to abstract | |
2486 | -- subprograms. | |
4a9e7f0c | 2487 | |
7c443ae8 | 2488 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2489 | |
a45d946f | 2490 | -- Do not consider subprogram renaminds because the renamed entity |
2491 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2492 | |
2493 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2494 | ||
a45d946f | 2495 | -- Do not process null procedures because there is no benefit of |
2496 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2497 | |
2498 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2499 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2500 | then |
2501 | return; | |
2502 | end if; | |
2503 | ||
4a9e7f0c | 2504 | -- Inspect all the formals applying aliasing and scalar initialization |
2505 | -- checks where applicable. | |
b73adb97 | 2506 | |
2507 | Formal := First_Formal (Subp); | |
2508 | while Present (Formal) loop | |
4a9e7f0c | 2509 | |
2510 | -- Generate the following scalar initialization checks for each | |
2511 | -- formal parameter: | |
2512 | ||
2513 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2514 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2515 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2516 | ||
2517 | if Check_Validity_Of_Parameters then | |
2518 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2519 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2520 | end if; | |
2521 | ||
2522 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2523 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2524 | end if; | |
b73adb97 | 2525 | end if; |
2526 | ||
b73adb97 | 2527 | Next_Formal (Formal); |
2528 | end loop; | |
2529 | ||
a45d946f | 2530 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2531 | |
2532 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2533 | |
a45d946f | 2534 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2535 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2536 | end if; |
3b045963 | 2537 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2538 | |
7aafae1c | 2539 | --------------------------- |
2540 | -- Apply_Predicate_Check -- | |
2541 | --------------------------- | |
2542 | ||
2543 | procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id) is | |
301d5ec3 | 2544 | S : Entity_Id; |
9e58d7ed | 2545 | |
7aafae1c | 2546 | begin |
701d57a4 | 2547 | if Present (Predicate_Function (Typ)) then |
301d5ec3 | 2548 | |
2549 | -- A predicate check does not apply within internally generated | |
2550 | -- subprograms, such as TSS functions. | |
2551 | ||
2552 | S := Current_Scope; | |
9e58d7ed | 2553 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2554 | S := Scope (S); |
2555 | end loop; | |
2556 | ||
9e58d7ed | 2557 | if Present (S) and then Get_TSS_Name (S) /= TSS_Null then |
301d5ec3 | 2558 | return; |
22631b41 | 2559 | |
96a2d100 | 2560 | -- If the check appears within the predicate function itself, it |
2561 | -- means that the user specified a check whose formal is the | |
2562 | -- predicated subtype itself, rather than some covering type. This | |
2563 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2564 | |
2565 | elsif S = Predicate_Function (Typ) then | |
96a2d100 | 2566 | Error_Msg_N |
2567 | ("predicate check includes a function call that " | |
cb97ae5c | 2568 | & "requires a predicate check??", Parent (N)); |
96a2d100 | 2569 | Error_Msg_N |
cb97ae5c | 2570 | ("\this will result in infinite recursion??", Parent (N)); |
96a2d100 | 2571 | Insert_Action (N, |
61016a7a | 2572 | Make_Raise_Storage_Error (Sloc (N), |
2573 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2574 | |
64cc9e5d | 2575 | -- Here for normal case of predicate active |
e6281d47 | 2576 | |
61016a7a | 2577 | else |
b04165c4 | 2578 | -- If the type has a static predicate and the expression is known |
2579 | -- at compile time, see if the expression satisfies the predicate. | |
3a75f20b | 2580 | |
2581 | Check_Expression_Against_Static_Predicate (N, Typ); | |
e6281d47 | 2582 | |
301d5ec3 | 2583 | Insert_Action (N, |
2584 | Make_Predicate_Check (Typ, Duplicate_Subexpr (N))); | |
2585 | end if; | |
7aafae1c | 2586 | end if; |
2587 | end Apply_Predicate_Check; | |
2588 | ||
ee6ba406 | 2589 | ----------------------- |
2590 | -- Apply_Range_Check -- | |
2591 | ----------------------- | |
2592 | ||
2593 | procedure Apply_Range_Check | |
2594 | (Ck_Node : Node_Id; | |
2595 | Target_Typ : Entity_Id; | |
2596 | Source_Typ : Entity_Id := Empty) | |
2597 | is | |
2598 | begin | |
2599 | Apply_Selected_Range_Checks | |
2600 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2601 | end Apply_Range_Check; | |
2602 | ||
2603 | ------------------------------ | |
2604 | -- Apply_Scalar_Range_Check -- | |
2605 | ------------------------------ | |
2606 | ||
feff2f05 | 2607 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2608 | -- off if it is already set on. | |
ee6ba406 | 2609 | |
2610 | procedure Apply_Scalar_Range_Check | |
2611 | (Expr : Node_Id; | |
2612 | Target_Typ : Entity_Id; | |
2613 | Source_Typ : Entity_Id := Empty; | |
2614 | Fixed_Int : Boolean := False) | |
2615 | is | |
2616 | Parnt : constant Node_Id := Parent (Expr); | |
2617 | S_Typ : Entity_Id; | |
2618 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2619 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
2620 | OK : Boolean; | |
2621 | ||
2622 | Is_Subscr_Ref : Boolean; | |
2623 | -- Set true if Expr is a subscript | |
2624 | ||
2625 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2626 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2627 | -- case we do not attempt to do an analysis of the value against the | |
2628 | -- range of the subscript, since we don't know the actual subtype. | |
2629 | ||
2630 | Int_Real : Boolean; | |
feff2f05 | 2631 | -- Set to True if Expr should be regarded as a real value even though |
2632 | -- the type of Expr might be discrete. | |
ee6ba406 | 2633 | |
2634 | procedure Bad_Value; | |
2635 | -- Procedure called if value is determined to be out of range | |
2636 | ||
9dfe12ae | 2637 | --------------- |
2638 | -- Bad_Value -- | |
2639 | --------------- | |
2640 | ||
ee6ba406 | 2641 | procedure Bad_Value is |
2642 | begin | |
2643 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2644 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
ee6ba406 | 2645 | Ent => Target_Typ, |
2646 | Typ => Target_Typ); | |
2647 | end Bad_Value; | |
2648 | ||
9dfe12ae | 2649 | -- Start of processing for Apply_Scalar_Range_Check |
2650 | ||
ee6ba406 | 2651 | begin |
2af58f67 | 2652 | -- Return if check obviously not needed |
ee6ba406 | 2653 | |
2af58f67 | 2654 | if |
2655 | -- Not needed inside generic | |
ee6ba406 | 2656 | |
2af58f67 | 2657 | Inside_A_Generic |
2658 | ||
2659 | -- Not needed if previous error | |
2660 | ||
2661 | or else Target_Typ = Any_Type | |
2662 | or else Nkind (Expr) = N_Error | |
2663 | ||
2664 | -- Not needed for non-scalar type | |
2665 | ||
2666 | or else not Is_Scalar_Type (Target_Typ) | |
2667 | ||
2668 | -- Not needed if we know node raises CE already | |
2669 | ||
2670 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2671 | then |
2672 | return; | |
2673 | end if; | |
2674 | ||
2675 | -- Now, see if checks are suppressed | |
2676 | ||
2677 | Is_Subscr_Ref := | |
2678 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2679 | ||
2680 | if Is_Subscr_Ref then | |
2681 | Arr := Prefix (Parnt); | |
2682 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2683 | |
a3a76ccc | 2684 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2685 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2686 | end if; |
ee6ba406 | 2687 | end if; |
2688 | ||
2689 | if not Do_Range_Check (Expr) then | |
2690 | ||
2691 | -- Subscript reference. Check for Index_Checks suppressed | |
2692 | ||
2693 | if Is_Subscr_Ref then | |
2694 | ||
2695 | -- Check array type and its base type | |
2696 | ||
2697 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2698 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2699 | then |
2700 | return; | |
2701 | ||
2702 | -- Check array itself if it is an entity name | |
2703 | ||
2704 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2705 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2706 | then |
2707 | return; | |
2708 | ||
2709 | -- Check expression itself if it is an entity name | |
2710 | ||
2711 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2712 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2713 | then |
2714 | return; | |
2715 | end if; | |
2716 | ||
2717 | -- All other cases, check for Range_Checks suppressed | |
2718 | ||
2719 | else | |
2720 | -- Check target type and its base type | |
2721 | ||
2722 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2723 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2724 | then |
2725 | return; | |
2726 | ||
2727 | -- Check expression itself if it is an entity name | |
2728 | ||
2729 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2730 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2731 | then |
2732 | return; | |
2733 | ||
feff2f05 | 2734 | -- If Expr is part of an assignment statement, then check left |
2735 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2736 | |
2737 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2738 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2739 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2740 | then |
2741 | return; | |
2742 | end if; | |
2743 | end if; | |
2744 | end if; | |
2745 | ||
9dfe12ae | 2746 | -- Do not set range checks if they are killed |
2747 | ||
2748 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2749 | and then Kill_Range_Check (Expr) | |
2750 | then | |
2751 | return; | |
2752 | end if; | |
2753 | ||
2754 | -- Do not set range checks for any values from System.Scalar_Values | |
2755 | -- since the whole idea of such values is to avoid checking them! | |
2756 | ||
2757 | if Is_Entity_Name (Expr) | |
2758 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2759 | then | |
2760 | return; | |
2761 | end if; | |
2762 | ||
ee6ba406 | 2763 | -- Now see if we need a check |
2764 | ||
2765 | if No (Source_Typ) then | |
2766 | S_Typ := Etype (Expr); | |
2767 | else | |
2768 | S_Typ := Source_Typ; | |
2769 | end if; | |
2770 | ||
2771 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
2772 | return; | |
2773 | end if; | |
2774 | ||
2775 | Is_Unconstrained_Subscr_Ref := | |
2776 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
2777 | ||
b40670e1 | 2778 | -- Special checks for floating-point type |
ee6ba406 | 2779 | |
b40670e1 | 2780 | if Is_Floating_Point_Type (S_Typ) then |
2781 | ||
2782 | -- Always do a range check if the source type includes infinities and | |
2783 | -- the target type does not include infinities. We do not do this if | |
2784 | -- range checks are killed. | |
2785 | ||
2786 | if Has_Infinities (S_Typ) | |
2787 | and then not Has_Infinities (Target_Typ) | |
2788 | then | |
2789 | Enable_Range_Check (Expr); | |
2790 | ||
2791 | -- Always do a range check for operators if option set | |
2792 | ||
2793 | elsif Check_Float_Overflow and then Nkind (Expr) in N_Op then | |
2794 | Enable_Range_Check (Expr); | |
2795 | end if; | |
ee6ba406 | 2796 | end if; |
2797 | ||
feff2f05 | 2798 | -- Return if we know expression is definitely in the range of the target |
2799 | -- type as determined by Determine_Range. Right now we only do this for | |
2800 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 2801 | |
f2a06be9 | 2802 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 2803 | |
feff2f05 | 2804 | -- Note: skip this if we are given a source_typ, since the point of |
2805 | -- supplying a Source_Typ is to stop us looking at the expression. | |
2806 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 2807 | |
2808 | if Is_Discrete_Type (Target_Typ) | |
2809 | and then Is_Discrete_Type (Etype (Expr)) | |
2810 | and then not Is_Unconstrained_Subscr_Ref | |
2811 | and then No (Source_Typ) | |
2812 | then | |
2813 | declare | |
2814 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
2815 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
2816 | Lo : Uint; | |
2817 | Hi : Uint; | |
2818 | ||
2819 | begin | |
2820 | if Compile_Time_Known_Value (Tlo) | |
2821 | and then Compile_Time_Known_Value (Thi) | |
2822 | then | |
9dfe12ae | 2823 | declare |
2824 | Lov : constant Uint := Expr_Value (Tlo); | |
2825 | Hiv : constant Uint := Expr_Value (Thi); | |
ee6ba406 | 2826 | |
9dfe12ae | 2827 | begin |
2828 | -- If range is null, we for sure have a constraint error | |
2829 | -- (we don't even need to look at the value involved, | |
2830 | -- since all possible values will raise CE). | |
2831 | ||
2832 | if Lov > Hiv then | |
2833 | Bad_Value; | |
2834 | return; | |
2835 | end if; | |
2836 | ||
2837 | -- Otherwise determine range of value | |
2838 | ||
9c486805 | 2839 | Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True); |
9dfe12ae | 2840 | |
2841 | if OK then | |
2842 | ||
2843 | -- If definitely in range, all OK | |
ee6ba406 | 2844 | |
ee6ba406 | 2845 | if Lo >= Lov and then Hi <= Hiv then |
2846 | return; | |
2847 | ||
9dfe12ae | 2848 | -- If definitely not in range, warn |
2849 | ||
ee6ba406 | 2850 | elsif Lov > Hi or else Hiv < Lo then |
2851 | Bad_Value; | |
2852 | return; | |
9dfe12ae | 2853 | |
2854 | -- Otherwise we don't know | |
2855 | ||
2856 | else | |
2857 | null; | |
ee6ba406 | 2858 | end if; |
9dfe12ae | 2859 | end if; |
2860 | end; | |
ee6ba406 | 2861 | end if; |
2862 | end; | |
2863 | end if; | |
2864 | ||
2865 | Int_Real := | |
2866 | Is_Floating_Point_Type (S_Typ) | |
2867 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
2868 | ||
2869 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 2870 | -- range of the target type. Note that if S_Typ is within the bounds |
2871 | -- of Target_Typ then this must be the case. This check is meaningful | |
2872 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 2873 | |
2874 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 2875 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 2876 | and then |
7a1dabb3 | 2877 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ee6ba406 | 2878 | or else |
9c486805 | 2879 | Is_In_Range (Expr, Target_Typ, |
2880 | Assume_Valid => True, | |
b40670e1 | 2881 | Fixed_Int => Fixed_Int, |
2882 | Int_Real => Int_Real)) | |
ee6ba406 | 2883 | then |
2884 | return; | |
2885 | ||
9c486805 | 2886 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
2887 | Assume_Valid => True, | |
2888 | Fixed_Int => Fixed_Int, | |
2889 | Int_Real => Int_Real) | |
2890 | then | |
ee6ba406 | 2891 | Bad_Value; |
2892 | return; | |
2893 | ||
b40670e1 | 2894 | -- Floating-point case |
feff2f05 | 2895 | -- In the floating-point case, we only do range checks if the type is |
2896 | -- constrained. We definitely do NOT want range checks for unconstrained | |
2897 | -- types, since we want to have infinities | |
ee6ba406 | 2898 | |
9dfe12ae | 2899 | elsif Is_Floating_Point_Type (S_Typ) then |
b40670e1 | 2900 | |
2901 | -- Normally, we only do range checks if the type is constrained. We do | |
2902 | -- NOT want range checks for unconstrained types, since we want to have | |
2903 | -- infinities. Override this decision in Check_Float_Overflow mode. | |
2904 | ||
2905 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then | |
9dfe12ae | 2906 | Enable_Range_Check (Expr); |
2907 | end if; | |
ee6ba406 | 2908 | |
9dfe12ae | 2909 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 2910 | |
2911 | else | |
2912 | Enable_Range_Check (Expr); | |
2913 | return; | |
2914 | end if; | |
ee6ba406 | 2915 | end Apply_Scalar_Range_Check; |
2916 | ||
2917 | ---------------------------------- | |
2918 | -- Apply_Selected_Length_Checks -- | |
2919 | ---------------------------------- | |
2920 | ||
2921 | procedure Apply_Selected_Length_Checks | |
2922 | (Ck_Node : Node_Id; | |
2923 | Target_Typ : Entity_Id; | |
2924 | Source_Typ : Entity_Id; | |
2925 | Do_Static : Boolean) | |
2926 | is | |
2927 | Cond : Node_Id; | |
2928 | R_Result : Check_Result; | |
2929 | R_Cno : Node_Id; | |
2930 | ||
2931 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2932 | Checks_On : constant Boolean := | |
b6341c67 | 2933 | (not Index_Checks_Suppressed (Target_Typ)) |
2934 | or else (not Length_Checks_Suppressed (Target_Typ)); | |
ee6ba406 | 2935 | |
2936 | begin | |
6dbcfcd9 | 2937 | if not Full_Expander_Active then |
ee6ba406 | 2938 | return; |
2939 | end if; | |
2940 | ||
2941 | R_Result := | |
2942 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
2943 | ||
2944 | for J in 1 .. 2 loop | |
ee6ba406 | 2945 | R_Cno := R_Result (J); |
2946 | exit when No (R_Cno); | |
2947 | ||
2948 | -- A length check may mention an Itype which is attached to a | |
2949 | -- subsequent node. At the top level in a package this can cause | |
2950 | -- an order-of-elaboration problem, so we make sure that the itype | |
2951 | -- is referenced now. | |
2952 | ||
2953 | if Ekind (Current_Scope) = E_Package | |
2954 | and then Is_Compilation_Unit (Current_Scope) | |
2955 | then | |
2956 | Ensure_Defined (Target_Typ, Ck_Node); | |
2957 | ||
2958 | if Present (Source_Typ) then | |
2959 | Ensure_Defined (Source_Typ, Ck_Node); | |
2960 | ||
2961 | elsif Is_Itype (Etype (Ck_Node)) then | |
2962 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
2963 | end if; | |
2964 | end if; | |
2965 | ||
feff2f05 | 2966 | -- If the item is a conditional raise of constraint error, then have |
2967 | -- a look at what check is being performed and ??? | |
ee6ba406 | 2968 | |
2969 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2970 | and then Present (Condition (R_Cno)) | |
2971 | then | |
2972 | Cond := Condition (R_Cno); | |
2973 | ||
0577b0b1 | 2974 | -- Case where node does not now have a dynamic check |
ee6ba406 | 2975 | |
0577b0b1 | 2976 | if not Has_Dynamic_Length_Check (Ck_Node) then |
2977 | ||
2978 | -- If checks are on, just insert the check | |
2979 | ||
2980 | if Checks_On then | |
2981 | Insert_Action (Ck_Node, R_Cno); | |
2982 | ||
2983 | if not Do_Static then | |
2984 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
2985 | end if; | |
2986 | ||
2987 | -- If checks are off, then analyze the length check after | |
2988 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 2989 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 2990 | -- compile time warning in this case. |
2991 | ||
2992 | else | |
2993 | Set_Parent (R_Cno, Ck_Node); | |
2994 | Analyze (R_Cno); | |
ee6ba406 | 2995 | end if; |
ee6ba406 | 2996 | end if; |
2997 | ||
2998 | -- Output a warning if the condition is known to be True | |
2999 | ||
3000 | if Is_Entity_Name (Cond) | |
3001 | and then Entity (Cond) = Standard_True | |
3002 | then | |
3003 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3004 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 3005 | CE_Length_Check_Failed, |
ee6ba406 | 3006 | Ent => Target_Typ, |
3007 | Typ => Target_Typ); | |
3008 | ||
3009 | -- If we were only doing a static check, or if checks are not | |
3010 | -- on, then we want to delete the check, since it is not needed. | |
3011 | -- We do this by replacing the if statement by a null statement | |
3012 | ||
3013 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3014 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3015 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3016 | end if; | |
3017 | ||
3018 | else | |
3019 | Install_Static_Check (R_Cno, Loc); | |
3020 | end if; | |
ee6ba406 | 3021 | end loop; |
ee6ba406 | 3022 | end Apply_Selected_Length_Checks; |
3023 | ||
3024 | --------------------------------- | |
3025 | -- Apply_Selected_Range_Checks -- | |
3026 | --------------------------------- | |
3027 | ||
3028 | procedure Apply_Selected_Range_Checks | |
3029 | (Ck_Node : Node_Id; | |
3030 | Target_Typ : Entity_Id; | |
3031 | Source_Typ : Entity_Id; | |
3032 | Do_Static : Boolean) | |
3033 | is | |
3034 | Cond : Node_Id; | |
3035 | R_Result : Check_Result; | |
3036 | R_Cno : Node_Id; | |
3037 | ||
3038 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
3039 | Checks_On : constant Boolean := | |
b6341c67 | 3040 | (not Index_Checks_Suppressed (Target_Typ)) |
3041 | or else (not Range_Checks_Suppressed (Target_Typ)); | |
ee6ba406 | 3042 | |
3043 | begin | |
6dbcfcd9 | 3044 | if not Full_Expander_Active or else not Checks_On then |
ee6ba406 | 3045 | return; |
3046 | end if; | |
3047 | ||
3048 | R_Result := | |
3049 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3050 | ||
3051 | for J in 1 .. 2 loop | |
3052 | ||
3053 | R_Cno := R_Result (J); | |
3054 | exit when No (R_Cno); | |
3055 | ||
feff2f05 | 3056 | -- If the item is a conditional raise of constraint error, then have |
3057 | -- a look at what check is being performed and ??? | |
ee6ba406 | 3058 | |
3059 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3060 | and then Present (Condition (R_Cno)) | |
3061 | then | |
3062 | Cond := Condition (R_Cno); | |
3063 | ||
3064 | if not Has_Dynamic_Range_Check (Ck_Node) then | |
3065 | Insert_Action (Ck_Node, R_Cno); | |
3066 | ||
3067 | if not Do_Static then | |
3068 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
3069 | end if; | |
3070 | end if; | |
3071 | ||
3072 | -- Output a warning if the condition is known to be True | |
3073 | ||
3074 | if Is_Entity_Name (Cond) | |
3075 | and then Entity (Cond) = Standard_True | |
3076 | then | |
feff2f05 | 3077 | -- Since an N_Range is technically not an expression, we have |
3078 | -- to set one of the bounds to C_E and then just flag the | |
3079 | -- N_Range. The warning message will point to the lower bound | |
3080 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3081 | |
3082 | if Nkind (Ck_Node) = N_Range then | |
3083 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3084 | (Low_Bound (Ck_Node), "static range out of bounds of}??", |
f15731c4 | 3085 | CE_Range_Check_Failed, |
ee6ba406 | 3086 | Ent => Target_Typ, |
3087 | Typ => Target_Typ); | |
3088 | ||
3089 | Set_Raises_Constraint_Error (Ck_Node); | |
3090 | ||
3091 | else | |
3092 | Apply_Compile_Time_Constraint_Error | |
3093 | (Ck_Node, "static value out of range of}?", | |
f15731c4 | 3094 | CE_Range_Check_Failed, |
ee6ba406 | 3095 | Ent => Target_Typ, |
3096 | Typ => Target_Typ); | |
3097 | end if; | |
3098 | ||
3099 | -- If we were only doing a static check, or if checks are not | |
3100 | -- on, then we want to delete the check, since it is not needed. | |
3101 | -- We do this by replacing the if statement by a null statement | |
3102 | ||
3103 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3104 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3105 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3106 | end if; | |
3107 | ||
3108 | else | |
3109 | Install_Static_Check (R_Cno, Loc); | |
3110 | end if; | |
ee6ba406 | 3111 | end loop; |
ee6ba406 | 3112 | end Apply_Selected_Range_Checks; |
3113 | ||
3114 | ------------------------------- | |
3115 | -- Apply_Static_Length_Check -- | |
3116 | ------------------------------- | |
3117 | ||
3118 | procedure Apply_Static_Length_Check | |
3119 | (Expr : Node_Id; | |
3120 | Target_Typ : Entity_Id; | |
3121 | Source_Typ : Entity_Id := Empty) | |
3122 | is | |
3123 | begin | |
3124 | Apply_Selected_Length_Checks | |
3125 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3126 | end Apply_Static_Length_Check; | |
3127 | ||
3128 | ------------------------------------- | |
3129 | -- Apply_Subscript_Validity_Checks -- | |
3130 | ------------------------------------- | |
3131 | ||
3132 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3133 | Sub : Node_Id; | |
3134 | ||
3135 | begin | |
3136 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3137 | ||
3138 | -- Loop through subscripts | |
3139 | ||
3140 | Sub := First (Expressions (Expr)); | |
3141 | while Present (Sub) loop | |
3142 | ||
feff2f05 | 3143 | -- Check one subscript. Note that we do not worry about enumeration |
3144 | -- type with holes, since we will convert the value to a Pos value | |
3145 | -- for the subscript, and that convert will do the necessary validity | |
3146 | -- check. | |
ee6ba406 | 3147 | |
3148 | Ensure_Valid (Sub, Holes_OK => True); | |
3149 | ||
3150 | -- Move to next subscript | |
3151 | ||
3152 | Sub := Next (Sub); | |
3153 | end loop; | |
3154 | end Apply_Subscript_Validity_Checks; | |
3155 | ||
3156 | ---------------------------------- | |
3157 | -- Apply_Type_Conversion_Checks -- | |
3158 | ---------------------------------- | |
3159 | ||
3160 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3161 | Target_Type : constant Entity_Id := Etype (N); | |
3162 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3163 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3164 | |
3165 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3166 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3167 | -- full view might have discriminants with defaults, so we need the | |
3168 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3169 | |
3170 | begin | |
3171 | if Inside_A_Generic then | |
3172 | return; | |
3173 | ||
f15731c4 | 3174 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3175 | -- situations of incomplete trees that blow things up. |
3176 | ||
f15731c4 | 3177 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3178 | return; |
3179 | ||
feff2f05 | 3180 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3181 | -- range check if we cannot be sure that Expr is in the base type of | |
3182 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3183 | -- are not quite the same condition from an implementation point of | |
3184 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3185 | |
3186 | elsif Is_Scalar_Type (Target_Type) then | |
3187 | declare | |
3188 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3189 | -- If the Conversion_OK flag on the type conversion is set and no |
3190 | -- floating point type is involved in the type conversion then | |
3191 | -- fixed point values must be read as integral values. | |
ee6ba406 | 3192 | |
5329ca64 | 3193 | Float_To_Int : constant Boolean := |
b6341c67 | 3194 | Is_Floating_Point_Type (Expr_Type) |
3195 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3196 | |
ee6ba406 | 3197 | begin |
ee6ba406 | 3198 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3199 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3200 | and then not |
7a1dabb3 | 3201 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3202 | and then not Float_To_Int |
ee6ba406 | 3203 | then |
00c403ee | 3204 | Activate_Overflow_Check (N); |
ee6ba406 | 3205 | end if; |
3206 | ||
3207 | if not Range_Checks_Suppressed (Target_Type) | |
3208 | and then not Range_Checks_Suppressed (Expr_Type) | |
3209 | then | |
5329ca64 | 3210 | if Float_To_Int then |
3211 | Apply_Float_Conversion_Check (Expr, Target_Type); | |
3212 | else | |
3213 | Apply_Scalar_Range_Check | |
3214 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
798afddc | 3215 | |
3216 | -- If the target type has predicates, we need to indicate | |
3217 | -- the need for a check, even if Determine_Range finds | |
3218 | -- that the value is within bounds. This may be the case | |
3219 | -- e.g for a division with a constant denominator. | |
3220 | ||
3221 | if Has_Predicates (Target_Type) then | |
3222 | Enable_Range_Check (Expr); | |
3223 | end if; | |
5329ca64 | 3224 | end if; |
ee6ba406 | 3225 | end if; |
3226 | end; | |
3227 | ||
3228 | elsif Comes_From_Source (N) | |
f40f9731 | 3229 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3230 | and then Is_Record_Type (Target_Type) |
3231 | and then Is_Derived_Type (Target_Type) | |
3232 | and then not Is_Tagged_Type (Target_Type) | |
3233 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3234 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3235 | then |
141d591a | 3236 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3237 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3238 | -- constraint, to verify that the expression of the parent type |
3239 | -- satisfies the constraints imposed by the (unconstrained!) | |
3240 | -- derived type. This applies to value conversions, not to view | |
3241 | -- conversions of tagged types. | |
3242 | ||
3243 | declare | |
9dfe12ae | 3244 | Loc : constant Source_Ptr := Sloc (N); |
3245 | Cond : Node_Id; | |
3246 | Constraint : Elmt_Id; | |
3247 | Discr_Value : Node_Id; | |
3248 | Discr : Entity_Id; | |
3249 | ||
3250 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3251 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3252 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3253 | |
3254 | begin | |
9dfe12ae | 3255 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3256 | while Present (Constraint) loop |
3257 | Discr_Value := Node (Constraint); | |
3258 | ||
3259 | if Is_Entity_Name (Discr_Value) | |
3260 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3261 | then | |
3262 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3263 | ||
3264 | if Present (Discr) | |
3265 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3266 | then | |
3267 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3268 | -- Value of original discriminant in expression. If the |
3269 | -- new discriminant has been used to constrain more than | |
3270 | -- one of the stored discriminants, this will provide the | |
3271 | -- required consistency check. | |
ee6ba406 | 3272 | |
55868293 | 3273 | Append_Elmt |
3274 | (Make_Selected_Component (Loc, | |
3275 | Prefix => | |
9dfe12ae | 3276 | Duplicate_Subexpr_No_Checks |
3277 | (Expr, Name_Req => True), | |
ee6ba406 | 3278 | Selector_Name => |
3279 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3280 | New_Constraints); |
ee6ba406 | 3281 | |
3282 | else | |
3283 | -- Discriminant of more remote ancestor ??? | |
3284 | ||
3285 | return; | |
3286 | end if; | |
3287 | ||
feff2f05 | 3288 | -- Derived type definition has an explicit value for this |
3289 | -- stored discriminant. | |
ee6ba406 | 3290 | |
3291 | else | |
3292 | Append_Elmt | |
9dfe12ae | 3293 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3294 | New_Constraints); | |
ee6ba406 | 3295 | end if; |
3296 | ||
3297 | Next_Elmt (Constraint); | |
3298 | end loop; | |
3299 | ||
3300 | -- Use the unconstrained expression type to retrieve the | |
3301 | -- discriminants of the parent, and apply momentarily the | |
3302 | -- discriminant constraint synthesized above. | |
3303 | ||
3304 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3305 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3306 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3307 | ||
3308 | Insert_Action (N, | |
f15731c4 | 3309 | Make_Raise_Constraint_Error (Loc, |
3310 | Condition => Cond, | |
3311 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3312 | end; |
3313 | ||
175a6969 | 3314 | -- For arrays, checks are set now, but conversions are applied during |
3315 | -- expansion, to take into accounts changes of representation. The | |
3316 | -- checks become range checks on the base type or length checks on the | |
3317 | -- subtype, depending on whether the target type is unconstrained or | |
8e802312 | 3318 | -- constrained. Note that the range check is put on the expression of a |
3319 | -- type conversion, while the length check is put on the type conversion | |
3320 | -- itself. | |
175a6969 | 3321 | |
3322 | elsif Is_Array_Type (Target_Type) then | |
3323 | if Is_Constrained (Target_Type) then | |
3324 | Set_Do_Length_Check (N); | |
3325 | else | |
3326 | Set_Do_Range_Check (Expr); | |
3327 | end if; | |
ee6ba406 | 3328 | end if; |
ee6ba406 | 3329 | end Apply_Type_Conversion_Checks; |
3330 | ||
3331 | ---------------------------------------------- | |
3332 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3333 | ---------------------------------------------- | |
3334 | ||
3335 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3336 | Loc : constant Source_Ptr := Sloc (N); | |
3337 | Typ : constant Entity_Id := Etype (N); | |
3338 | ||
3339 | begin | |
3340 | if Inside_A_Generic then | |
3341 | return; | |
3342 | ||
3343 | -- Nothing to do if checks are suppressed | |
3344 | ||
3345 | elsif Range_Checks_Suppressed (Typ) | |
3346 | and then Overflow_Checks_Suppressed (Typ) | |
3347 | then | |
3348 | return; | |
3349 | ||
3350 | -- Nothing to do if the attribute does not come from source. The | |
3351 | -- internal attributes we generate of this type do not need checks, | |
3352 | -- and furthermore the attempt to check them causes some circular | |
3353 | -- elaboration orders when dealing with packed types. | |
3354 | ||
3355 | elsif not Comes_From_Source (N) then | |
3356 | return; | |
3357 | ||
9dfe12ae | 3358 | -- If the prefix is a selected component that depends on a discriminant |
3359 | -- the check may improperly expose a discriminant instead of using | |
3360 | -- the bounds of the object itself. Set the type of the attribute to | |
3361 | -- the base type of the context, so that a check will be imposed when | |
3362 | -- needed (e.g. if the node appears as an index). | |
3363 | ||
3364 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3365 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3366 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3367 | then | |
3368 | Set_Etype (N, Base_Type (Typ)); | |
3369 | ||
feff2f05 | 3370 | -- Otherwise, replace the attribute node with a type conversion node |
3371 | -- whose expression is the attribute, retyped to universal integer, and | |
3372 | -- whose subtype mark is the target type. The call to analyze this | |
3373 | -- conversion will set range and overflow checks as required for proper | |
3374 | -- detection of an out of range value. | |
ee6ba406 | 3375 | |
3376 | else | |
3377 | Set_Etype (N, Universal_Integer); | |
3378 | Set_Analyzed (N, True); | |
3379 | ||
3380 | Rewrite (N, | |
3381 | Make_Type_Conversion (Loc, | |
3382 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3383 | Expression => Relocate_Node (N))); | |
3384 | ||
3385 | Analyze_And_Resolve (N, Typ); | |
3386 | return; | |
3387 | end if; | |
ee6ba406 | 3388 | end Apply_Universal_Integer_Attribute_Checks; |
3389 | ||
07c191b0 | 3390 | ------------------------------------- |
3391 | -- Atomic_Synchronization_Disabled -- | |
3392 | ------------------------------------- | |
3393 | ||
3394 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3395 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3396 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3397 | ||
3398 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3399 | begin | |
b444f81d | 3400 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3401 | -- looks enabled, since it is never disabled. | |
3402 | ||
3403 | if Debug_Flag_Dot_E then | |
3404 | return False; | |
3405 | ||
3406 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3407 | -- sync looks disabled, since it always tests True. | |
3408 | ||
3409 | elsif Debug_Flag_Dot_D then | |
3410 | return True; | |
3411 | ||
3412 | -- If entity present, then check result for that entity | |
3413 | ||
3414 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3415 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3416 | |
3417 | -- Otherwise result depends on current scope setting | |
3418 | ||
07c191b0 | 3419 | else |
fafc6b97 | 3420 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3421 | end if; |
3422 | end Atomic_Synchronization_Disabled; | |
3423 | ||
ee6ba406 | 3424 | ------------------------------- |
3425 | -- Build_Discriminant_Checks -- | |
3426 | ------------------------------- | |
3427 | ||
3428 | function Build_Discriminant_Checks | |
3429 | (N : Node_Id; | |
314a23b6 | 3430 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3431 | is |
3432 | Loc : constant Source_Ptr := Sloc (N); | |
3433 | Cond : Node_Id; | |
3434 | Disc : Elmt_Id; | |
3435 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3436 | Dref : Node_Id; |
ee6ba406 | 3437 | Dval : Node_Id; |
3438 | ||
84d0d4a5 | 3439 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3440 | ||
3441 | ---------------------------------- | |
3442 | -- Aggregate_Discriminant_Value -- | |
3443 | ---------------------------------- | |
3444 | ||
3445 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3446 | Assoc : Node_Id; | |
3447 | ||
3448 | begin | |
feff2f05 | 3449 | -- The aggregate has been normalized with named associations. We use |
3450 | -- the Chars field to locate the discriminant to take into account | |
3451 | -- discriminants in derived types, which carry the same name as those | |
3452 | -- in the parent. | |
84d0d4a5 | 3453 | |
3454 | Assoc := First (Component_Associations (N)); | |
3455 | while Present (Assoc) loop | |
3456 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3457 | return Expression (Assoc); | |
3458 | else | |
3459 | Next (Assoc); | |
3460 | end if; | |
3461 | end loop; | |
3462 | ||
3463 | -- Discriminant must have been found in the loop above | |
3464 | ||
3465 | raise Program_Error; | |
3466 | end Aggregate_Discriminant_Val; | |
3467 | ||
3468 | -- Start of processing for Build_Discriminant_Checks | |
3469 | ||
ee6ba406 | 3470 | begin |
84d0d4a5 | 3471 | -- Loop through discriminants evolving the condition |
3472 | ||
ee6ba406 | 3473 | Cond := Empty; |
3474 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3475 | ||
9dfe12ae | 3476 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3477 | |
3478 | if Is_Private_Type (T_Typ) | |
3479 | and then No (Full_View (T_Typ)) | |
3480 | then | |
3481 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3482 | else | |
3483 | Disc_Ent := First_Discriminant (T_Typ); | |
3484 | end if; | |
3485 | ||
3486 | while Present (Disc) loop | |
ee6ba406 | 3487 | Dval := Node (Disc); |
3488 | ||
3489 | if Nkind (Dval) = N_Identifier | |
3490 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3491 | then | |
3492 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3493 | else | |
9dfe12ae | 3494 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3495 | end if; |
3496 | ||
00f91aef | 3497 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3498 | -- of the node. | |
9dfe12ae | 3499 | |
00f91aef | 3500 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3501 | Dref := New_Copy ( | |
3502 | Get_Discriminant_Value ( | |
3503 | First_Discriminant (T_Typ), | |
3504 | T_Typ, | |
3505 | Stored_Constraint (T_Typ))); | |
3506 | ||
84d0d4a5 | 3507 | elsif Nkind (N) = N_Aggregate then |
3508 | Dref := | |
3509 | Duplicate_Subexpr_No_Checks | |
3510 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3511 | ||
00f91aef | 3512 | else |
3513 | Dref := | |
3514 | Make_Selected_Component (Loc, | |
3515 | Prefix => | |
3516 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), | |
3517 | Selector_Name => | |
3518 | Make_Identifier (Loc, Chars (Disc_Ent))); | |
3519 | ||
3520 | Set_Is_In_Discriminant_Check (Dref); | |
3521 | end if; | |
9dfe12ae | 3522 | |
ee6ba406 | 3523 | Evolve_Or_Else (Cond, |
3524 | Make_Op_Ne (Loc, | |
9dfe12ae | 3525 | Left_Opnd => Dref, |
ee6ba406 | 3526 | Right_Opnd => Dval)); |
3527 | ||
3528 | Next_Elmt (Disc); | |
3529 | Next_Discriminant (Disc_Ent); | |
3530 | end loop; | |
3531 | ||
3532 | return Cond; | |
3533 | end Build_Discriminant_Checks; | |
3534 | ||
13dbf220 | 3535 | ------------------ |
3536 | -- Check_Needed -- | |
3537 | ------------------ | |
3538 | ||
3539 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3540 | N : Node_Id; | |
3541 | P : Node_Id; | |
3542 | K : Node_Kind; | |
3543 | L : Node_Id; | |
3544 | R : Node_Id; | |
3545 | ||
3546 | begin | |
3547 | -- Always check if not simple entity | |
3548 | ||
3549 | if Nkind (Nod) not in N_Has_Entity | |
3550 | or else not Comes_From_Source (Nod) | |
3551 | then | |
3552 | return True; | |
3553 | end if; | |
3554 | ||
3555 | -- Look up tree for short circuit | |
3556 | ||
3557 | N := Nod; | |
3558 | loop | |
3559 | P := Parent (N); | |
3560 | K := Nkind (P); | |
3561 | ||
7b17e51b | 3562 | -- Done if out of subexpression (note that we allow generated stuff |
3563 | -- such as itype declarations in this context, to keep the loop going | |
3564 | -- since we may well have generated such stuff in complex situations. | |
3565 | -- Also done if no parent (probably an error condition, but no point | |
3566 | -- in behaving nasty if we find it!) | |
3567 | ||
3568 | if No (P) | |
3569 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
3570 | then | |
13dbf220 | 3571 | return True; |
3572 | ||
7b17e51b | 3573 | -- Or/Or Else case, where test is part of the right operand, or is |
3574 | -- part of one of the actions associated with the right operand, and | |
3575 | -- the left operand is an equality test. | |
13dbf220 | 3576 | |
7b17e51b | 3577 | elsif K = N_Op_Or then |
13dbf220 | 3578 | exit when N = Right_Opnd (P) |
3579 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
3580 | ||
7b17e51b | 3581 | elsif K = N_Or_Else then |
3582 | exit when (N = Right_Opnd (P) | |
3583 | or else | |
3584 | (Is_List_Member (N) | |
3585 | and then List_Containing (N) = Actions (P))) | |
3586 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
13dbf220 | 3587 | |
7b17e51b | 3588 | -- Similar test for the And/And then case, where the left operand |
3589 | -- is an inequality test. | |
3590 | ||
3591 | elsif K = N_Op_And then | |
13dbf220 | 3592 | exit when N = Right_Opnd (P) |
38f5559f | 3593 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; |
7b17e51b | 3594 | |
3595 | elsif K = N_And_Then then | |
3596 | exit when (N = Right_Opnd (P) | |
3597 | or else | |
3598 | (Is_List_Member (N) | |
3599 | and then List_Containing (N) = Actions (P))) | |
3600 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; | |
13dbf220 | 3601 | end if; |
3602 | ||
3603 | N := P; | |
3604 | end loop; | |
3605 | ||
3606 | -- If we fall through the loop, then we have a conditional with an | |
3607 | -- appropriate test as its left operand. So test further. | |
3608 | ||
3609 | L := Left_Opnd (P); | |
13dbf220 | 3610 | R := Right_Opnd (L); |
3611 | L := Left_Opnd (L); | |
3612 | ||
3613 | -- Left operand of test must match original variable | |
3614 | ||
3615 | if Nkind (L) not in N_Has_Entity | |
3616 | or else Entity (L) /= Entity (Nod) | |
3617 | then | |
3618 | return True; | |
3619 | end if; | |
3620 | ||
2af58f67 | 3621 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 3622 | |
3623 | case Check is | |
3624 | when Access_Check => | |
2af58f67 | 3625 | if not Known_Null (R) then |
13dbf220 | 3626 | return True; |
3627 | end if; | |
3628 | ||
3629 | when Division_Check => | |
3630 | if not Compile_Time_Known_Value (R) | |
3631 | or else Expr_Value (R) /= Uint_0 | |
3632 | then | |
3633 | return True; | |
3634 | end if; | |
2af58f67 | 3635 | |
3636 | when others => | |
3637 | raise Program_Error; | |
13dbf220 | 3638 | end case; |
3639 | ||
3640 | -- Here we have the optimizable case, warn if not short-circuited | |
3641 | ||
3642 | if K = N_Op_And or else K = N_Op_Or then | |
3643 | case Check is | |
3644 | when Access_Check => | |
3645 | Error_Msg_N | |
cb97ae5c | 3646 | ("Constraint_Error may be raised (access check)??", |
13dbf220 | 3647 | Parent (Nod)); |
3648 | when Division_Check => | |
3649 | Error_Msg_N | |
cb97ae5c | 3650 | ("Constraint_Error may be raised (zero divide)??", |
13dbf220 | 3651 | Parent (Nod)); |
2af58f67 | 3652 | |
3653 | when others => | |
3654 | raise Program_Error; | |
13dbf220 | 3655 | end case; |
3656 | ||
3657 | if K = N_Op_And then | |
e977c0cf | 3658 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3659 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 3660 | else |
e977c0cf | 3661 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3662 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 3663 | end if; |
3664 | ||
6fb3c314 | 3665 | -- If not short-circuited, we need the check |
13dbf220 | 3666 | |
3667 | return True; | |
3668 | ||
3669 | -- If short-circuited, we can omit the check | |
3670 | ||
3671 | else | |
3672 | return False; | |
3673 | end if; | |
3674 | end Check_Needed; | |
3675 | ||
ee6ba406 | 3676 | ----------------------------------- |
3677 | -- Check_Valid_Lvalue_Subscripts -- | |
3678 | ----------------------------------- | |
3679 | ||
3680 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
3681 | begin | |
3682 | -- Skip this if range checks are suppressed | |
3683 | ||
3684 | if Range_Checks_Suppressed (Etype (Expr)) then | |
3685 | return; | |
3686 | ||
feff2f05 | 3687 | -- Only do this check for expressions that come from source. We assume |
3688 | -- that expander generated assignments explicitly include any necessary | |
3689 | -- checks. Note that this is not just an optimization, it avoids | |
3690 | -- infinite recursions! | |
ee6ba406 | 3691 | |
3692 | elsif not Comes_From_Source (Expr) then | |
3693 | return; | |
3694 | ||
3695 | -- For a selected component, check the prefix | |
3696 | ||
3697 | elsif Nkind (Expr) = N_Selected_Component then | |
3698 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3699 | return; | |
3700 | ||
3701 | -- Case of indexed component | |
3702 | ||
3703 | elsif Nkind (Expr) = N_Indexed_Component then | |
3704 | Apply_Subscript_Validity_Checks (Expr); | |
3705 | ||
feff2f05 | 3706 | -- Prefix may itself be or contain an indexed component, and these |
3707 | -- subscripts need checking as well. | |
ee6ba406 | 3708 | |
3709 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3710 | end if; | |
3711 | end Check_Valid_Lvalue_Subscripts; | |
3712 | ||
fa7497e8 | 3713 | ---------------------------------- |
3714 | -- Null_Exclusion_Static_Checks -- | |
3715 | ---------------------------------- | |
3716 | ||
3717 | procedure Null_Exclusion_Static_Checks (N : Node_Id) is | |
0577b0b1 | 3718 | Error_Node : Node_Id; |
3719 | Expr : Node_Id; | |
3720 | Has_Null : constant Boolean := Has_Null_Exclusion (N); | |
3721 | K : constant Node_Kind := Nkind (N); | |
3722 | Typ : Entity_Id; | |
fa7497e8 | 3723 | |
13dbf220 | 3724 | begin |
0577b0b1 | 3725 | pragma Assert |
3726 | (K = N_Component_Declaration | |
3727 | or else K = N_Discriminant_Specification | |
3728 | or else K = N_Function_Specification | |
3729 | or else K = N_Object_Declaration | |
3730 | or else K = N_Parameter_Specification); | |
3731 | ||
3732 | if K = N_Function_Specification then | |
3733 | Typ := Etype (Defining_Entity (N)); | |
3734 | else | |
3735 | Typ := Etype (Defining_Identifier (N)); | |
3736 | end if; | |
fa7497e8 | 3737 | |
13dbf220 | 3738 | case K is |
13dbf220 | 3739 | when N_Component_Declaration => |
3740 | if Present (Access_Definition (Component_Definition (N))) then | |
0577b0b1 | 3741 | Error_Node := Component_Definition (N); |
13dbf220 | 3742 | else |
0577b0b1 | 3743 | Error_Node := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 3744 | end if; |
5329ca64 | 3745 | |
0577b0b1 | 3746 | when N_Discriminant_Specification => |
3747 | Error_Node := Discriminant_Type (N); | |
3748 | ||
3749 | when N_Function_Specification => | |
3750 | Error_Node := Result_Definition (N); | |
3751 | ||
3752 | when N_Object_Declaration => | |
3753 | Error_Node := Object_Definition (N); | |
3754 | ||
3755 | when N_Parameter_Specification => | |
3756 | Error_Node := Parameter_Type (N); | |
3757 | ||
13dbf220 | 3758 | when others => |
3759 | raise Program_Error; | |
3760 | end case; | |
5329ca64 | 3761 | |
0577b0b1 | 3762 | if Has_Null then |
5329ca64 | 3763 | |
0577b0b1 | 3764 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
3765 | -- applied to an access [sub]type. | |
5329ca64 | 3766 | |
0577b0b1 | 3767 | if not Is_Access_Type (Typ) then |
503f7fd3 | 3768 | Error_Msg_N |
00c403ee | 3769 | ("`NOT NULL` allowed only for an access type", Error_Node); |
5329ca64 | 3770 | |
feff2f05 | 3771 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 3772 | -- be applied to a [sub]type that does not exclude null already. |
3773 | ||
3774 | elsif Can_Never_Be_Null (Typ) | |
d16989f1 | 3775 | and then Comes_From_Source (Typ) |
0577b0b1 | 3776 | then |
503f7fd3 | 3777 | Error_Msg_NE |
00c403ee | 3778 | ("`NOT NULL` not allowed (& already excludes null)", |
3779 | Error_Node, Typ); | |
0577b0b1 | 3780 | end if; |
13dbf220 | 3781 | end if; |
5329ca64 | 3782 | |
cc60bd16 | 3783 | -- Check that null-excluding objects are always initialized, except for |
3784 | -- deferred constants, for which the expression will appear in the full | |
3785 | -- declaration. | |
13dbf220 | 3786 | |
3787 | if K = N_Object_Declaration | |
84d0d4a5 | 3788 | and then No (Expression (N)) |
cc60bd16 | 3789 | and then not Constant_Present (N) |
feff2f05 | 3790 | and then not No_Initialization (N) |
13dbf220 | 3791 | then |
feff2f05 | 3792 | -- Add an expression that assigns null. This node is needed by |
3793 | -- Apply_Compile_Time_Constraint_Error, which will replace this with | |
3794 | -- a Constraint_Error node. | |
13dbf220 | 3795 | |
3796 | Set_Expression (N, Make_Null (Sloc (N))); | |
3797 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
5329ca64 | 3798 | |
13dbf220 | 3799 | Apply_Compile_Time_Constraint_Error |
3800 | (N => Expression (N), | |
cb97ae5c | 3801 | Msg => |
3802 | "(Ada 2005) null-excluding objects must be initialized??", | |
13dbf220 | 3803 | Reason => CE_Null_Not_Allowed); |
3804 | end if; | |
5329ca64 | 3805 | |
cc60bd16 | 3806 | -- Check that a null-excluding component, formal or object is not being |
3807 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 3808 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 3809 | |
0577b0b1 | 3810 | if K /= N_Function_Specification then |
3811 | Expr := Expression (N); | |
5329ca64 | 3812 | |
2af58f67 | 3813 | if Present (Expr) and then Known_Null (Expr) then |
13dbf220 | 3814 | case K is |
0577b0b1 | 3815 | when N_Component_Declaration | |
3816 | N_Discriminant_Specification => | |
7189d17f | 3817 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3818 | (N => Expr, |
2af58f67 | 3819 | Msg => "(Ada 2005) null not allowed " & |
cb97ae5c | 3820 | "in null-excluding components??", |
0577b0b1 | 3821 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3822 | |
0577b0b1 | 3823 | when N_Object_Declaration => |
7189d17f | 3824 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3825 | (N => Expr, |
2af58f67 | 3826 | Msg => "(Ada 2005) null not allowed " & |
0577b0b1 | 3827 | "in null-excluding objects?", |
3828 | Reason => CE_Null_Not_Allowed); | |
5329ca64 | 3829 | |
0577b0b1 | 3830 | when N_Parameter_Specification => |
7189d17f | 3831 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3832 | (N => Expr, |
2af58f67 | 3833 | Msg => "(Ada 2005) null not allowed " & |
cb97ae5c | 3834 | "in null-excluding formals??", |
0577b0b1 | 3835 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 3836 | |
3837 | when others => | |
3838 | null; | |
5329ca64 | 3839 | end case; |
3840 | end if; | |
0577b0b1 | 3841 | end if; |
fa7497e8 | 3842 | end Null_Exclusion_Static_Checks; |
3843 | ||
9dfe12ae | 3844 | ---------------------------------- |
3845 | -- Conditional_Statements_Begin -- | |
3846 | ---------------------------------- | |
3847 | ||
3848 | procedure Conditional_Statements_Begin is | |
3849 | begin | |
3850 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
3851 | ||
feff2f05 | 3852 | -- If stack overflows, kill all checks, that way we know to simply reset |
3853 | -- the number of saved checks to zero on return. This should never occur | |
3854 | -- in practice. | |
9dfe12ae | 3855 | |
3856 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
3857 | Kill_All_Checks; | |
3858 | ||
feff2f05 | 3859 | -- In the normal case, we just make a new stack entry saving the current |
3860 | -- number of saved checks for a later restore. | |
9dfe12ae | 3861 | |
3862 | else | |
3863 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
3864 | ||
3865 | if Debug_Flag_CC then | |
3866 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
3867 | Num_Saved_Checks); | |
3868 | end if; | |
3869 | end if; | |
3870 | end Conditional_Statements_Begin; | |
3871 | ||
3872 | -------------------------------- | |
3873 | -- Conditional_Statements_End -- | |
3874 | -------------------------------- | |
3875 | ||
3876 | procedure Conditional_Statements_End is | |
3877 | begin | |
3878 | pragma Assert (Saved_Checks_TOS > 0); | |
3879 | ||
feff2f05 | 3880 | -- If the saved checks stack overflowed, then we killed all checks, so |
3881 | -- setting the number of saved checks back to zero is correct. This | |
3882 | -- should never occur in practice. | |
9dfe12ae | 3883 | |
3884 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
3885 | Num_Saved_Checks := 0; | |
3886 | ||
feff2f05 | 3887 | -- In the normal case, restore the number of saved checks from the top |
3888 | -- stack entry. | |
9dfe12ae | 3889 | |
3890 | else | |
3891 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
3892 | if Debug_Flag_CC then | |
3893 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
3894 | Num_Saved_Checks); | |
3895 | end if; | |
3896 | end if; | |
3897 | ||
3898 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
3899 | end Conditional_Statements_End; | |
3900 | ||
3cce7f32 | 3901 | ------------------------- |
3902 | -- Convert_From_Bignum -- | |
3903 | ------------------------- | |
3904 | ||
3905 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
3906 | Loc : constant Source_Ptr := Sloc (N); | |
3907 | ||
3908 | begin | |
3909 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
3910 | ||
3911 | -- Construct call From Bignum | |
3912 | ||
3913 | return | |
3914 | Make_Function_Call (Loc, | |
3915 | Name => | |
3916 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
3917 | Parameter_Associations => New_List (Relocate_Node (N))); | |
3918 | end Convert_From_Bignum; | |
3919 | ||
3920 | ----------------------- | |
3921 | -- Convert_To_Bignum -- | |
3922 | ----------------------- | |
3923 | ||
3924 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
3925 | Loc : constant Source_Ptr := Sloc (N); | |
3926 | ||
3927 | begin | |
0326b4d4 | 3928 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 3929 | |
3930 | if Is_RTE (Etype (N), RE_Bignum) then | |
3931 | return Relocate_Node (N); | |
3932 | ||
21a55437 | 3933 | -- Otherwise construct call to To_Bignum, converting the operand to the |
3934 | -- required Long_Long_Integer form. | |
3cce7f32 | 3935 | |
3936 | else | |
3937 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
3938 | return | |
3939 | Make_Function_Call (Loc, | |
3940 | Name => | |
3941 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
3942 | Parameter_Associations => New_List ( | |
3943 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
3944 | end if; | |
3945 | end Convert_To_Bignum; | |
3946 | ||
ee6ba406 | 3947 | --------------------- |
3948 | -- Determine_Range -- | |
3949 | --------------------- | |
3950 | ||
6af1bdbc | 3951 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 3952 | type Cache_Index is range 0 .. Cache_Size - 1; |
3953 | -- Determine size of below cache (power of 2 is more efficient!) | |
3954 | ||
3955 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; | |
9c486805 | 3956 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
ee6ba406 | 3957 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; |
3958 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
feff2f05 | 3959 | -- The above arrays are used to implement a small direct cache for |
3960 | -- Determine_Range calls. Because of the way Determine_Range recursively | |
3961 | -- traces subexpressions, and because overflow checking calls the routine | |
3962 | -- on the way up the tree, a quadratic behavior can otherwise be | |
3963 | -- encountered in large expressions. The cache entry for node N is stored | |
3964 | -- in the (N mod Cache_Size) entry, and can be validated by checking the | |
9c486805 | 3965 | -- actual node value stored there. The Range_Cache_V array records the |
3966 | -- setting of Assume_Valid for the cache entry. | |
ee6ba406 | 3967 | |
3968 | procedure Determine_Range | |
9c486805 | 3969 | (N : Node_Id; |
3970 | OK : out Boolean; | |
3971 | Lo : out Uint; | |
3972 | Hi : out Uint; | |
3973 | Assume_Valid : Boolean := False) | |
ee6ba406 | 3974 | is |
e254d721 | 3975 | Typ : Entity_Id := Etype (N); |
3976 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 3977 | |
3978 | Lo_Left : Uint; | |
3979 | Hi_Left : Uint; | |
3980 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 3981 | |
ee6ba406 | 3982 | Lo_Right : Uint; |
ee6ba406 | 3983 | Hi_Right : Uint; |
8880be85 | 3984 | -- Lo and Hi bounds of right (or only) operand |
3985 | ||
3986 | Bound : Node_Id; | |
3987 | -- Temp variable used to hold a bound node | |
3988 | ||
3989 | Hbound : Uint; | |
3990 | -- High bound of base type of expression | |
3991 | ||
3992 | Lor : Uint; | |
3993 | Hir : Uint; | |
3994 | -- Refined values for low and high bounds, after tightening | |
3995 | ||
3996 | OK1 : Boolean; | |
3997 | -- Used in lower level calls to indicate if call succeeded | |
3998 | ||
3999 | Cindex : Cache_Index; | |
4000 | -- Used to search cache | |
ee6ba406 | 4001 | |
094ed68e | 4002 | Btyp : Entity_Id; |
4003 | -- Base type | |
4004 | ||
ee6ba406 | 4005 | function OK_Operands return Boolean; |
4006 | -- Used for binary operators. Determines the ranges of the left and | |
4007 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 4008 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 4009 | |
4010 | ----------------- | |
4011 | -- OK_Operands -- | |
4012 | ----------------- | |
4013 | ||
4014 | function OK_Operands return Boolean is | |
4015 | begin | |
9c486805 | 4016 | Determine_Range |
4017 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 4018 | |
4019 | if not OK1 then | |
4020 | return False; | |
4021 | end if; | |
4022 | ||
9c486805 | 4023 | Determine_Range |
4024 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4025 | return OK1; |
4026 | end OK_Operands; | |
4027 | ||
4028 | -- Start of processing for Determine_Range | |
4029 | ||
4030 | begin | |
87bdc21d | 4031 | -- For temporary constants internally generated to remove side effects |
4032 | -- we must use the corresponding expression to determine the range of | |
4033 | -- the expression. | |
4034 | ||
4035 | if Is_Entity_Name (N) | |
4036 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4037 | and then Ekind (Entity (N)) = E_Constant | |
4038 | and then Is_Internal_Name (Chars (Entity (N))) | |
4039 | then | |
4040 | Determine_Range | |
4041 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
4042 | return; | |
4043 | end if; | |
4044 | ||
ee6ba406 | 4045 | -- Prevent junk warnings by initializing range variables |
4046 | ||
4047 | Lo := No_Uint; | |
4048 | Hi := No_Uint; | |
4049 | Lor := No_Uint; | |
4050 | Hir := No_Uint; | |
4051 | ||
a781c0fc | 4052 | -- If type is not defined, we can't determine its range |
ee6ba406 | 4053 | |
a781c0fc | 4054 | if No (Typ) |
4055 | ||
4056 | -- We don't deal with anything except discrete types | |
4057 | ||
4058 | or else not Is_Discrete_Type (Typ) | |
4059 | ||
4060 | -- Ignore type for which an error has been posted, since range in | |
4061 | -- this case may well be a bogosity deriving from the error. Also | |
4062 | -- ignore if error posted on the reference node. | |
4063 | ||
4064 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 4065 | then |
4066 | OK := False; | |
4067 | return; | |
4068 | end if; | |
4069 | ||
4070 | -- For all other cases, we can determine the range | |
4071 | ||
4072 | OK := True; | |
4073 | ||
feff2f05 | 4074 | -- If value is compile time known, then the possible range is the one |
4075 | -- value that we know this expression definitely has! | |
ee6ba406 | 4076 | |
4077 | if Compile_Time_Known_Value (N) then | |
4078 | Lo := Expr_Value (N); | |
4079 | Hi := Lo; | |
4080 | return; | |
4081 | end if; | |
4082 | ||
4083 | -- Return if already in the cache | |
4084 | ||
4085 | Cindex := Cache_Index (N mod Cache_Size); | |
4086 | ||
9c486805 | 4087 | if Determine_Range_Cache_N (Cindex) = N |
4088 | and then | |
4089 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4090 | then | |
ee6ba406 | 4091 | Lo := Determine_Range_Cache_Lo (Cindex); |
4092 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4093 | return; | |
4094 | end if; | |
4095 | ||
feff2f05 | 4096 | -- Otherwise, start by finding the bounds of the type of the expression, |
4097 | -- the value cannot be outside this range (if it is, then we have an | |
4098 | -- overflow situation, which is a separate check, we are talking here | |
4099 | -- only about the expression value). | |
ee6ba406 | 4100 | |
341bd953 | 4101 | -- First a check, never try to find the bounds of a generic type, since |
4102 | -- these bounds are always junk values, and it is only valid to look at | |
4103 | -- the bounds in an instance. | |
4104 | ||
4105 | if Is_Generic_Type (Typ) then | |
4106 | OK := False; | |
4107 | return; | |
4108 | end if; | |
4109 | ||
9c486805 | 4110 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4111 | |
9c486805 | 4112 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4113 | or else Assume_No_Invalid_Values | |
4114 | or else Assume_Valid | |
e254d721 | 4115 | then |
9c486805 | 4116 | null; |
4117 | else | |
4118 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4119 | end if; |
4120 | ||
094ed68e | 4121 | -- Retrieve the base type. Handle the case where the base type is a |
4122 | -- private enumeration type. | |
4123 | ||
4124 | Btyp := Base_Type (Typ); | |
4125 | ||
4126 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4127 | Btyp := Full_View (Btyp); | |
4128 | end if; | |
4129 | ||
feff2f05 | 4130 | -- We use the actual bound unless it is dynamic, in which case use the |
4131 | -- corresponding base type bound if possible. If we can't get a bound | |
4132 | -- then we figure we can't determine the range (a peculiar case, that | |
4133 | -- perhaps cannot happen, but there is no point in bombing in this | |
4134 | -- optimization circuit. | |
8880be85 | 4135 | |
4136 | -- First the low bound | |
ee6ba406 | 4137 | |
4138 | Bound := Type_Low_Bound (Typ); | |
4139 | ||
4140 | if Compile_Time_Known_Value (Bound) then | |
4141 | Lo := Expr_Value (Bound); | |
4142 | ||
094ed68e | 4143 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4144 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4145 | |
4146 | else | |
4147 | OK := False; | |
4148 | return; | |
4149 | end if; | |
4150 | ||
8880be85 | 4151 | -- Now the high bound |
4152 | ||
ee6ba406 | 4153 | Bound := Type_High_Bound (Typ); |
4154 | ||
8880be85 | 4155 | -- We need the high bound of the base type later on, and this should |
4156 | -- always be compile time known. Again, it is not clear that this | |
4157 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4158 | |
094ed68e | 4159 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4160 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4161 | Hi := Hbound; |
4162 | ||
4163 | else | |
4164 | OK := False; | |
4165 | return; | |
4166 | end if; | |
4167 | ||
feff2f05 | 4168 | -- If we have a static subtype, then that may have a tighter bound so |
4169 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4170 | |
4171 | if Compile_Time_Known_Value (Bound) then | |
4172 | Hi := Expr_Value (Bound); | |
4173 | end if; | |
4174 | ||
feff2f05 | 4175 | -- We may be able to refine this value in certain situations. If any |
4176 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4177 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4178 | |
4179 | case Nkind (N) is | |
4180 | ||
4181 | -- For unary plus, result is limited by range of operand | |
4182 | ||
4183 | when N_Op_Plus => | |
9c486805 | 4184 | Determine_Range |
4185 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4186 | |
4187 | -- For unary minus, determine range of operand, and negate it | |
4188 | ||
4189 | when N_Op_Minus => | |
9c486805 | 4190 | Determine_Range |
4191 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4192 | |
4193 | if OK1 then | |
4194 | Lor := -Hi_Right; | |
4195 | Hir := -Lo_Right; | |
4196 | end if; | |
4197 | ||
4198 | -- For binary addition, get range of each operand and do the | |
4199 | -- addition to get the result range. | |
4200 | ||
4201 | when N_Op_Add => | |
4202 | if OK_Operands then | |
4203 | Lor := Lo_Left + Lo_Right; | |
4204 | Hir := Hi_Left + Hi_Right; | |
4205 | end if; | |
4206 | ||
feff2f05 | 4207 | -- Division is tricky. The only case we consider is where the right |
4208 | -- operand is a positive constant, and in this case we simply divide | |
4209 | -- the bounds of the left operand | |
ee6ba406 | 4210 | |
4211 | when N_Op_Divide => | |
4212 | if OK_Operands then | |
4213 | if Lo_Right = Hi_Right | |
4214 | and then Lo_Right > 0 | |
4215 | then | |
4216 | Lor := Lo_Left / Lo_Right; | |
4217 | Hir := Hi_Left / Lo_Right; | |
4218 | ||
4219 | else | |
4220 | OK1 := False; | |
4221 | end if; | |
4222 | end if; | |
4223 | ||
feff2f05 | 4224 | -- For binary subtraction, get range of each operand and do the worst |
4225 | -- case subtraction to get the result range. | |
ee6ba406 | 4226 | |
4227 | when N_Op_Subtract => | |
4228 | if OK_Operands then | |
4229 | Lor := Lo_Left - Hi_Right; | |
4230 | Hir := Hi_Left - Lo_Right; | |
4231 | end if; | |
4232 | ||
feff2f05 | 4233 | -- For MOD, if right operand is a positive constant, then result must |
4234 | -- be in the allowable range of mod results. | |
ee6ba406 | 4235 | |
4236 | when N_Op_Mod => | |
4237 | if OK_Operands then | |
9dfe12ae | 4238 | if Lo_Right = Hi_Right |
4239 | and then Lo_Right /= 0 | |
4240 | then | |
ee6ba406 | 4241 | if Lo_Right > 0 then |
4242 | Lor := Uint_0; | |
4243 | Hir := Lo_Right - 1; | |
4244 | ||
9dfe12ae | 4245 | else -- Lo_Right < 0 |
ee6ba406 | 4246 | Lor := Lo_Right + 1; |
4247 | Hir := Uint_0; | |
4248 | end if; | |
4249 | ||
4250 | else | |
4251 | OK1 := False; | |
4252 | end if; | |
4253 | end if; | |
4254 | ||
feff2f05 | 4255 | -- For REM, if right operand is a positive constant, then result must |
4256 | -- be in the allowable range of mod results. | |
ee6ba406 | 4257 | |
4258 | when N_Op_Rem => | |
4259 | if OK_Operands then | |
9dfe12ae | 4260 | if Lo_Right = Hi_Right |
4261 | and then Lo_Right /= 0 | |
4262 | then | |
ee6ba406 | 4263 | declare |
4264 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4265 | ||
4266 | begin | |
4267 | -- The sign of the result depends on the sign of the | |
4268 | -- dividend (but not on the sign of the divisor, hence | |
4269 | -- the abs operation above). | |
4270 | ||
4271 | if Lo_Left < 0 then | |
4272 | Lor := -Dval; | |
4273 | else | |
4274 | Lor := Uint_0; | |
4275 | end if; | |
4276 | ||
4277 | if Hi_Left < 0 then | |
4278 | Hir := Uint_0; | |
4279 | else | |
4280 | Hir := Dval; | |
4281 | end if; | |
4282 | end; | |
4283 | ||
4284 | else | |
4285 | OK1 := False; | |
4286 | end if; | |
4287 | end if; | |
4288 | ||
4289 | -- Attribute reference cases | |
4290 | ||
4291 | when N_Attribute_Reference => | |
4292 | case Attribute_Name (N) is | |
4293 | ||
4294 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4295 | -- possible range of values of the attribute expression. |
ee6ba406 | 4296 | |
4297 | when Name_Pos | Name_Val => | |
9c486805 | 4298 | Determine_Range |
4299 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4300 | |
4301 | -- For Length attribute, use the bounds of the corresponding | |
4302 | -- index type to refine the range. | |
4303 | ||
4304 | when Name_Length => | |
4305 | declare | |
4306 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4307 | Inum : Nat; | |
4308 | Indx : Node_Id; | |
4309 | ||
4310 | LL, LU : Uint; | |
4311 | UL, UU : Uint; | |
4312 | ||
4313 | begin | |
4314 | if Is_Access_Type (Atyp) then | |
4315 | Atyp := Designated_Type (Atyp); | |
4316 | end if; | |
4317 | ||
4318 | -- For string literal, we know exact value | |
4319 | ||
4320 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4321 | OK := True; | |
4322 | Lo := String_Literal_Length (Atyp); | |
4323 | Hi := String_Literal_Length (Atyp); | |
4324 | return; | |
4325 | end if; | |
4326 | ||
4327 | -- Otherwise check for expression given | |
4328 | ||
4329 | if No (Expressions (N)) then | |
4330 | Inum := 1; | |
4331 | else | |
4332 | Inum := | |
4333 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4334 | end if; | |
4335 | ||
4336 | Indx := First_Index (Atyp); | |
4337 | for J in 2 .. Inum loop | |
4338 | Indx := Next_Index (Indx); | |
4339 | end loop; | |
4340 | ||
9116df93 | 4341 | -- If the index type is a formal type or derived from |
c8da6114 | 4342 | -- one, the bounds are not static. |
4343 | ||
4344 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4345 | OK := False; | |
4346 | return; | |
4347 | end if; | |
4348 | ||
ee6ba406 | 4349 | Determine_Range |
9c486805 | 4350 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4351 | Assume_Valid); | |
ee6ba406 | 4352 | |
4353 | if OK1 then | |
4354 | Determine_Range | |
9c486805 | 4355 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4356 | Assume_Valid); | |
ee6ba406 | 4357 | |
4358 | if OK1 then | |
4359 | ||
4360 | -- The maximum value for Length is the biggest | |
4361 | -- possible gap between the values of the bounds. | |
4362 | -- But of course, this value cannot be negative. | |
4363 | ||
9c486805 | 4364 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4365 | |
4366 | -- For constrained arrays, the minimum value for | |
4367 | -- Length is taken from the actual value of the | |
9116df93 | 4368 | -- bounds, since the index will be exactly of this |
4369 | -- subtype. | |
ee6ba406 | 4370 | |
4371 | if Is_Constrained (Atyp) then | |
9c486805 | 4372 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4373 | |
4374 | -- For an unconstrained array, the minimum value | |
4375 | -- for length is always zero. | |
4376 | ||
4377 | else | |
4378 | Lor := Uint_0; | |
4379 | end if; | |
4380 | end if; | |
4381 | end if; | |
4382 | end; | |
4383 | ||
4384 | -- No special handling for other attributes | |
9116df93 | 4385 | -- Probably more opportunities exist here??? |
ee6ba406 | 4386 | |
4387 | when others => | |
4388 | OK1 := False; | |
4389 | ||
4390 | end case; | |
4391 | ||
feff2f05 | 4392 | -- For type conversion from one discrete type to another, we can |
4393 | -- refine the range using the converted value. | |
ee6ba406 | 4394 | |
4395 | when N_Type_Conversion => | |
9c486805 | 4396 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); |
ee6ba406 | 4397 | |
4398 | -- Nothing special to do for all other expression kinds | |
4399 | ||
4400 | when others => | |
4401 | OK1 := False; | |
4402 | Lor := No_Uint; | |
4403 | Hir := No_Uint; | |
4404 | end case; | |
4405 | ||
9116df93 | 4406 | -- At this stage, if OK1 is true, then we know that the actual result of |
4407 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4408 | -- to restrict the possible range of results. | |
ee6ba406 | 4409 | |
4410 | if OK1 then | |
4411 | ||
9116df93 | 4412 | -- If the refined value of the low bound is greater than the type |
4413 | -- high bound, then reset it to the more restrictive value. However, | |
4414 | -- we do NOT do this for the case of a modular type where the | |
4415 | -- possible upper bound on the value is above the base type high | |
4416 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4417 | |
4418 | if Lor > Lo | |
9116df93 | 4419 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4420 | then |
4421 | Lo := Lor; | |
4422 | end if; | |
4423 | ||
9116df93 | 4424 | -- Similarly, if the refined value of the high bound is less than the |
4425 | -- value so far, then reset it to the more restrictive value. Again, | |
4426 | -- we do not do this if the refined low bound is negative for a | |
4427 | -- modular type, since this would wrap. | |
ee6ba406 | 4428 | |
4429 | if Hir < Hi | |
9116df93 | 4430 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4431 | then |
4432 | Hi := Hir; | |
4433 | end if; | |
4434 | end if; | |
4435 | ||
4436 | -- Set cache entry for future call and we are all done | |
4437 | ||
4438 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4439 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4440 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4441 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4442 | return; | |
4443 | ||
9116df93 | 4444 | -- If any exception occurs, it means that we have some bug in the compiler, |
4445 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4446 | -- occurrence. However, this is only an optimization attempt, so there is |
4447 | -- really no point in crashing the compiler. Instead we just decide, too | |
4448 | -- bad, we can't figure out a range in this case after all. | |
4449 | ||
4450 | exception | |
4451 | when others => | |
4452 | ||
4453 | -- Debug flag K disables this behavior (useful for debugging) | |
4454 | ||
4455 | if Debug_Flag_K then | |
4456 | raise; | |
4457 | else | |
4458 | OK := False; | |
4459 | Lo := No_Uint; | |
4460 | Hi := No_Uint; | |
4461 | return; | |
4462 | end if; | |
ee6ba406 | 4463 | end Determine_Range; |
4464 | ||
4465 | ------------------------------------ | |
4466 | -- Discriminant_Checks_Suppressed -- | |
4467 | ------------------------------------ | |
4468 | ||
4469 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4470 | begin | |
9dfe12ae | 4471 | if Present (E) then |
4472 | if Is_Unchecked_Union (E) then | |
4473 | return True; | |
4474 | elsif Checks_May_Be_Suppressed (E) then | |
4475 | return Is_Check_Suppressed (E, Discriminant_Check); | |
4476 | end if; | |
4477 | end if; | |
4478 | ||
fafc6b97 | 4479 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 4480 | end Discriminant_Checks_Suppressed; |
4481 | ||
4482 | -------------------------------- | |
4483 | -- Division_Checks_Suppressed -- | |
4484 | -------------------------------- | |
4485 | ||
4486 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4487 | begin | |
9dfe12ae | 4488 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
4489 | return Is_Check_Suppressed (E, Division_Check); | |
4490 | else | |
fafc6b97 | 4491 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 4492 | end if; |
ee6ba406 | 4493 | end Division_Checks_Suppressed; |
4494 | ||
4495 | ----------------------------------- | |
4496 | -- Elaboration_Checks_Suppressed -- | |
4497 | ----------------------------------- | |
4498 | ||
4499 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4500 | begin | |
38f5559f | 4501 | -- The complication in this routine is that if we are in the dynamic |
4502 | -- model of elaboration, we also check All_Checks, since All_Checks | |
4503 | -- does not set Elaboration_Check explicitly. | |
4504 | ||
9dfe12ae | 4505 | if Present (E) then |
4506 | if Kill_Elaboration_Checks (E) then | |
4507 | return True; | |
38f5559f | 4508 | |
9dfe12ae | 4509 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 4510 | if Is_Check_Suppressed (E, Elaboration_Check) then |
4511 | return True; | |
4512 | elsif Dynamic_Elaboration_Checks then | |
4513 | return Is_Check_Suppressed (E, All_Checks); | |
4514 | else | |
4515 | return False; | |
4516 | end if; | |
9dfe12ae | 4517 | end if; |
4518 | end if; | |
4519 | ||
fafc6b97 | 4520 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 4521 | return True; |
4522 | elsif Dynamic_Elaboration_Checks then | |
fafc6b97 | 4523 | return Scope_Suppress.Suppress (All_Checks); |
38f5559f | 4524 | else |
4525 | return False; | |
4526 | end if; | |
ee6ba406 | 4527 | end Elaboration_Checks_Suppressed; |
4528 | ||
9dfe12ae | 4529 | --------------------------- |
4530 | -- Enable_Overflow_Check -- | |
4531 | --------------------------- | |
4532 | ||
4533 | procedure Enable_Overflow_Check (N : Node_Id) is | |
3cce7f32 | 4534 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 4535 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 4536 | Chk : Nat; |
4537 | OK : Boolean; | |
4538 | Ent : Entity_Id; | |
4539 | Ofs : Uint; | |
4540 | Lo : Uint; | |
4541 | Hi : Uint; | |
ee6ba406 | 4542 | |
ee6ba406 | 4543 | begin |
9dfe12ae | 4544 | if Debug_Flag_CC then |
4545 | w ("Enable_Overflow_Check for node ", Int (N)); | |
4546 | Write_Str (" Source location = "); | |
4547 | wl (Sloc (N)); | |
00c403ee | 4548 | pg (Union_Id (N)); |
ee6ba406 | 4549 | end if; |
ee6ba406 | 4550 | |
75209ec5 | 4551 | -- No check if overflow checks suppressed for type of node |
4552 | ||
0df9d43f | 4553 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 4554 | return; |
4555 | ||
49260fa5 | 4556 | -- Nothing to do for unsigned integer types, which do not overflow |
4557 | ||
4558 | elsif Is_Modular_Integer_Type (Typ) then | |
4559 | return; | |
3cce7f32 | 4560 | end if; |
4561 | ||
0df9d43f | 4562 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 4563 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
4564 | -- probably more extreme that it needs to be, but what is going on here | |
4565 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 4566 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 4567 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 4568 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 4569 | -- legacy reliable. |
3cce7f32 | 4570 | |
0df9d43f | 4571 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 4572 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
4573 | -- know that no check is needed. We skip all that in the two new modes, | |
4574 | -- since really overflow checking happens over a whole subtree, and we | |
4575 | -- do the corresponding optimizations later on when applying the checks. | |
4576 | ||
4577 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 4578 | if not (Overflow_Checks_Suppressed (Etype (N))) |
4579 | and then not (Is_Entity_Name (N) | |
4580 | and then Overflow_Checks_Suppressed (Entity (N))) | |
4581 | then | |
4582 | Activate_Overflow_Check (N); | |
4583 | end if; | |
3cce7f32 | 4584 | |
4585 | if Debug_Flag_CC then | |
4586 | w ("Minimized/Eliminated mode"); | |
4587 | end if; | |
4588 | ||
4589 | return; | |
4590 | end if; | |
4591 | ||
0df9d43f | 4592 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 4593 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 4594 | |
feff2f05 | 4595 | -- Nothing to do if the range of the result is known OK. We skip this |
4596 | -- for conversions, since the caller already did the check, and in any | |
4597 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 4598 | -- different. |
ee6ba406 | 4599 | |
3cce7f32 | 4600 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 4601 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 4602 | |
cc60bd16 | 4603 | -- Note in the test below that we assume that the range is not OK |
4604 | -- if a bound of the range is equal to that of the type. That's not | |
4605 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 4606 | |
9dfe12ae | 4607 | -- a) The way that Determine_Range works, it will typically report |
4608 | -- the bounds of the value as being equal to the bounds of the | |
4609 | -- type, because it either can't tell anything more precise, or | |
4610 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 4611 | |
9dfe12ae | 4612 | -- b) It is very unusual to have a situation in which this would |
4613 | -- generate an unnecessary overflow check (an example would be | |
4614 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 4615 | -- literal value one is added). |
ee6ba406 | 4616 | |
9dfe12ae | 4617 | -- c) The alternative is a lot of special casing in this routine |
4618 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 4619 | |
9dfe12ae | 4620 | if OK |
4621 | and then Lo > Expr_Value (Type_Low_Bound (Typ)) | |
4622 | and then Hi < Expr_Value (Type_High_Bound (Typ)) | |
4623 | then | |
4624 | if Debug_Flag_CC then | |
4625 | w ("No overflow check required"); | |
4626 | end if; | |
4627 | ||
4628 | return; | |
4629 | end if; | |
4630 | end if; | |
4631 | ||
feff2f05 | 4632 | -- If not in optimizing mode, set flag and we are done. We are also done |
4633 | -- (and just set the flag) if the type is not a discrete type, since it | |
4634 | -- is not worth the effort to eliminate checks for other than discrete | |
4635 | -- types. In addition, we take this same path if we have stored the | |
4636 | -- maximum number of checks possible already (a very unlikely situation, | |
4637 | -- but we do not want to blow up!) | |
9dfe12ae | 4638 | |
4639 | if Optimization_Level = 0 | |
4640 | or else not Is_Discrete_Type (Etype (N)) | |
4641 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4642 | then |
00c403ee | 4643 | Activate_Overflow_Check (N); |
9dfe12ae | 4644 | |
4645 | if Debug_Flag_CC then | |
4646 | w ("Optimization off"); | |
4647 | end if; | |
4648 | ||
ee6ba406 | 4649 | return; |
9dfe12ae | 4650 | end if; |
ee6ba406 | 4651 | |
9dfe12ae | 4652 | -- Otherwise evaluate and check the expression |
4653 | ||
4654 | Find_Check | |
4655 | (Expr => N, | |
4656 | Check_Type => 'O', | |
4657 | Target_Type => Empty, | |
4658 | Entry_OK => OK, | |
4659 | Check_Num => Chk, | |
4660 | Ent => Ent, | |
4661 | Ofs => Ofs); | |
4662 | ||
4663 | if Debug_Flag_CC then | |
4664 | w ("Called Find_Check"); | |
4665 | w (" OK = ", OK); | |
4666 | ||
4667 | if OK then | |
4668 | w (" Check_Num = ", Chk); | |
4669 | w (" Ent = ", Int (Ent)); | |
4670 | Write_Str (" Ofs = "); | |
4671 | pid (Ofs); | |
4672 | end if; | |
4673 | end if; | |
ee6ba406 | 4674 | |
9dfe12ae | 4675 | -- If check is not of form to optimize, then set flag and we are done |
4676 | ||
4677 | if not OK then | |
00c403ee | 4678 | Activate_Overflow_Check (N); |
ee6ba406 | 4679 | return; |
9dfe12ae | 4680 | end if; |
ee6ba406 | 4681 | |
9dfe12ae | 4682 | -- If check is already performed, then return without setting flag |
4683 | ||
4684 | if Chk /= 0 then | |
4685 | if Debug_Flag_CC then | |
4686 | w ("Check suppressed!"); | |
4687 | end if; | |
ee6ba406 | 4688 | |
ee6ba406 | 4689 | return; |
9dfe12ae | 4690 | end if; |
ee6ba406 | 4691 | |
9dfe12ae | 4692 | -- Here we will make a new entry for the new check |
4693 | ||
00c403ee | 4694 | Activate_Overflow_Check (N); |
9dfe12ae | 4695 | Num_Saved_Checks := Num_Saved_Checks + 1; |
4696 | Saved_Checks (Num_Saved_Checks) := | |
4697 | (Killed => False, | |
4698 | Entity => Ent, | |
4699 | Offset => Ofs, | |
4700 | Check_Type => 'O', | |
4701 | Target_Type => Empty); | |
4702 | ||
4703 | if Debug_Flag_CC then | |
4704 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
4705 | w (" Entity = ", Int (Ent)); | |
4706 | Write_Str (" Offset = "); | |
4707 | pid (Ofs); | |
4708 | w (" Check_Type = O"); | |
4709 | w (" Target_Type = Empty"); | |
4710 | end if; | |
ee6ba406 | 4711 | |
feff2f05 | 4712 | -- If we get an exception, then something went wrong, probably because of |
4713 | -- an error in the structure of the tree due to an incorrect program. Or it | |
4714 | -- may be a bug in the optimization circuit. In either case the safest | |
4715 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 4716 | |
4717 | exception | |
4718 | when others => | |
00c403ee | 4719 | Activate_Overflow_Check (N); |
9dfe12ae | 4720 | |
4721 | if Debug_Flag_CC then | |
4722 | w (" exception occurred, overflow flag set"); | |
4723 | end if; | |
4724 | ||
4725 | return; | |
4726 | end Enable_Overflow_Check; | |
4727 | ||
4728 | ------------------------ | |
4729 | -- Enable_Range_Check -- | |
4730 | ------------------------ | |
4731 | ||
4732 | procedure Enable_Range_Check (N : Node_Id) is | |
4733 | Chk : Nat; | |
4734 | OK : Boolean; | |
4735 | Ent : Entity_Id; | |
4736 | Ofs : Uint; | |
4737 | Ttyp : Entity_Id; | |
4738 | P : Node_Id; | |
4739 | ||
4740 | begin | |
feff2f05 | 4741 | -- Return if unchecked type conversion with range check killed. In this |
4742 | -- case we never set the flag (that's what Kill_Range_Check is about!) | |
9dfe12ae | 4743 | |
4744 | if Nkind (N) = N_Unchecked_Type_Conversion | |
4745 | and then Kill_Range_Check (N) | |
ee6ba406 | 4746 | then |
4747 | return; | |
9dfe12ae | 4748 | end if; |
ee6ba406 | 4749 | |
55e8372b | 4750 | -- Do not set range check flag if parent is assignment statement or |
4751 | -- object declaration with Suppress_Assignment_Checks flag set | |
4752 | ||
4753 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
4754 | and then Suppress_Assignment_Checks (Parent (N)) | |
4755 | then | |
4756 | return; | |
4757 | end if; | |
4758 | ||
0577b0b1 | 4759 | -- Check for various cases where we should suppress the range check |
4760 | ||
4761 | -- No check if range checks suppressed for type of node | |
4762 | ||
4763 | if Present (Etype (N)) | |
4764 | and then Range_Checks_Suppressed (Etype (N)) | |
4765 | then | |
4766 | return; | |
4767 | ||
4768 | -- No check if node is an entity name, and range checks are suppressed | |
4769 | -- for this entity, or for the type of this entity. | |
4770 | ||
4771 | elsif Is_Entity_Name (N) | |
4772 | and then (Range_Checks_Suppressed (Entity (N)) | |
4773 | or else Range_Checks_Suppressed (Etype (Entity (N)))) | |
4774 | then | |
4775 | return; | |
4776 | ||
4777 | -- No checks if index of array, and index checks are suppressed for | |
4778 | -- the array object or the type of the array. | |
4779 | ||
4780 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
4781 | declare | |
4782 | Pref : constant Node_Id := Prefix (Parent (N)); | |
4783 | begin | |
4784 | if Is_Entity_Name (Pref) | |
4785 | and then Index_Checks_Suppressed (Entity (Pref)) | |
4786 | then | |
4787 | return; | |
4788 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
4789 | return; | |
4790 | end if; | |
4791 | end; | |
4792 | end if; | |
4793 | ||
9dfe12ae | 4794 | -- Debug trace output |
ee6ba406 | 4795 | |
9dfe12ae | 4796 | if Debug_Flag_CC then |
4797 | w ("Enable_Range_Check for node ", Int (N)); | |
4798 | Write_Str (" Source location = "); | |
4799 | wl (Sloc (N)); | |
00c403ee | 4800 | pg (Union_Id (N)); |
9dfe12ae | 4801 | end if; |
4802 | ||
feff2f05 | 4803 | -- If not in optimizing mode, set flag and we are done. We are also done |
4804 | -- (and just set the flag) if the type is not a discrete type, since it | |
4805 | -- is not worth the effort to eliminate checks for other than discrete | |
4806 | -- types. In addition, we take this same path if we have stored the | |
4807 | -- maximum number of checks possible already (a very unlikely situation, | |
4808 | -- but we do not want to blow up!) | |
9dfe12ae | 4809 | |
4810 | if Optimization_Level = 0 | |
4811 | or else No (Etype (N)) | |
4812 | or else not Is_Discrete_Type (Etype (N)) | |
4813 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4814 | then |
00c403ee | 4815 | Activate_Range_Check (N); |
9dfe12ae | 4816 | |
4817 | if Debug_Flag_CC then | |
4818 | w ("Optimization off"); | |
4819 | end if; | |
4820 | ||
ee6ba406 | 4821 | return; |
9dfe12ae | 4822 | end if; |
ee6ba406 | 4823 | |
9dfe12ae | 4824 | -- Otherwise find out the target type |
ee6ba406 | 4825 | |
9dfe12ae | 4826 | P := Parent (N); |
ee6ba406 | 4827 | |
9dfe12ae | 4828 | -- For assignment, use left side subtype |
4829 | ||
4830 | if Nkind (P) = N_Assignment_Statement | |
4831 | and then Expression (P) = N | |
4832 | then | |
4833 | Ttyp := Etype (Name (P)); | |
4834 | ||
4835 | -- For indexed component, use subscript subtype | |
4836 | ||
4837 | elsif Nkind (P) = N_Indexed_Component then | |
4838 | declare | |
4839 | Atyp : Entity_Id; | |
4840 | Indx : Node_Id; | |
4841 | Subs : Node_Id; | |
4842 | ||
4843 | begin | |
4844 | Atyp := Etype (Prefix (P)); | |
4845 | ||
4846 | if Is_Access_Type (Atyp) then | |
4847 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 4848 | |
4849 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 4850 | -- perform check unconditionally: it depends on the bounds of |
4851 | -- an object and we cannot currently recognize whether the test | |
4852 | -- may be redundant. | |
f07ea091 | 4853 | |
4854 | if not Is_Constrained (Atyp) then | |
00c403ee | 4855 | Activate_Range_Check (N); |
f07ea091 | 4856 | return; |
4857 | end if; | |
7189d17f | 4858 | |
feff2f05 | 4859 | -- Ditto if the prefix is an explicit dereference whose designated |
4860 | -- type is unconstrained. | |
7189d17f | 4861 | |
4862 | elsif Nkind (Prefix (P)) = N_Explicit_Dereference | |
4863 | and then not Is_Constrained (Atyp) | |
4864 | then | |
00c403ee | 4865 | Activate_Range_Check (N); |
7189d17f | 4866 | return; |
9dfe12ae | 4867 | end if; |
4868 | ||
4869 | Indx := First_Index (Atyp); | |
4870 | Subs := First (Expressions (P)); | |
4871 | loop | |
4872 | if Subs = N then | |
4873 | Ttyp := Etype (Indx); | |
4874 | exit; | |
4875 | end if; | |
4876 | ||
4877 | Next_Index (Indx); | |
4878 | Next (Subs); | |
4879 | end loop; | |
4880 | end; | |
4881 | ||
4882 | -- For now, ignore all other cases, they are not so interesting | |
4883 | ||
4884 | else | |
4885 | if Debug_Flag_CC then | |
4886 | w (" target type not found, flag set"); | |
4887 | end if; | |
4888 | ||
00c403ee | 4889 | Activate_Range_Check (N); |
9dfe12ae | 4890 | return; |
4891 | end if; | |
4892 | ||
4893 | -- Evaluate and check the expression | |
4894 | ||
4895 | Find_Check | |
4896 | (Expr => N, | |
4897 | Check_Type => 'R', | |
4898 | Target_Type => Ttyp, | |
4899 | Entry_OK => OK, | |
4900 | Check_Num => Chk, | |
4901 | Ent => Ent, | |
4902 | Ofs => Ofs); | |
4903 | ||
4904 | if Debug_Flag_CC then | |
4905 | w ("Called Find_Check"); | |
4906 | w ("Target_Typ = ", Int (Ttyp)); | |
4907 | w (" OK = ", OK); | |
4908 | ||
4909 | if OK then | |
4910 | w (" Check_Num = ", Chk); | |
4911 | w (" Ent = ", Int (Ent)); | |
4912 | Write_Str (" Ofs = "); | |
4913 | pid (Ofs); | |
4914 | end if; | |
4915 | end if; | |
4916 | ||
4917 | -- If check is not of form to optimize, then set flag and we are done | |
4918 | ||
4919 | if not OK then | |
4920 | if Debug_Flag_CC then | |
4921 | w (" expression not of optimizable type, flag set"); | |
4922 | end if; | |
4923 | ||
00c403ee | 4924 | Activate_Range_Check (N); |
9dfe12ae | 4925 | return; |
4926 | end if; | |
4927 | ||
4928 | -- If check is already performed, then return without setting flag | |
4929 | ||
4930 | if Chk /= 0 then | |
4931 | if Debug_Flag_CC then | |
4932 | w ("Check suppressed!"); | |
4933 | end if; | |
4934 | ||
4935 | return; | |
4936 | end if; | |
4937 | ||
4938 | -- Here we will make a new entry for the new check | |
4939 | ||
00c403ee | 4940 | Activate_Range_Check (N); |
9dfe12ae | 4941 | Num_Saved_Checks := Num_Saved_Checks + 1; |
4942 | Saved_Checks (Num_Saved_Checks) := | |
4943 | (Killed => False, | |
4944 | Entity => Ent, | |
4945 | Offset => Ofs, | |
4946 | Check_Type => 'R', | |
4947 | Target_Type => Ttyp); | |
4948 | ||
4949 | if Debug_Flag_CC then | |
4950 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
4951 | w (" Entity = ", Int (Ent)); | |
4952 | Write_Str (" Offset = "); | |
4953 | pid (Ofs); | |
4954 | w (" Check_Type = R"); | |
4955 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 4956 | pg (Union_Id (Ttyp)); |
9dfe12ae | 4957 | end if; |
4958 | ||
feff2f05 | 4959 | -- If we get an exception, then something went wrong, probably because of |
4960 | -- an error in the structure of the tree due to an incorrect program. Or | |
4961 | -- it may be a bug in the optimization circuit. In either case the safest | |
4962 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 4963 | |
4964 | exception | |
4965 | when others => | |
00c403ee | 4966 | Activate_Range_Check (N); |
9dfe12ae | 4967 | |
4968 | if Debug_Flag_CC then | |
4969 | w (" exception occurred, range flag set"); | |
4970 | end if; | |
4971 | ||
4972 | return; | |
4973 | end Enable_Range_Check; | |
4974 | ||
4975 | ------------------ | |
4976 | -- Ensure_Valid -- | |
4977 | ------------------ | |
4978 | ||
4979 | procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is | |
4980 | Typ : constant Entity_Id := Etype (Expr); | |
4981 | ||
4982 | begin | |
4983 | -- Ignore call if we are not doing any validity checking | |
4984 | ||
4985 | if not Validity_Checks_On then | |
4986 | return; | |
4987 | ||
0577b0b1 | 4988 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 4989 | |
0577b0b1 | 4990 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 4991 | return; |
4992 | ||
feff2f05 | 4993 | -- No check required if expression is from the expander, we assume the |
4994 | -- expander will generate whatever checks are needed. Note that this is | |
4995 | -- not just an optimization, it avoids infinite recursions! | |
9dfe12ae | 4996 | |
4997 | -- Unchecked conversions must be checked, unless they are initialized | |
4998 | -- scalar values, as in a component assignment in an init proc. | |
4999 | ||
5000 | -- In addition, we force a check if Force_Validity_Checks is set | |
5001 | ||
5002 | elsif not Comes_From_Source (Expr) | |
5003 | and then not Force_Validity_Checks | |
5004 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
5005 | or else Kill_Range_Check (Expr)) | |
5006 | then | |
5007 | return; | |
5008 | ||
5009 | -- No check required if expression is known to have valid value | |
5010 | ||
5011 | elsif Expr_Known_Valid (Expr) then | |
5012 | return; | |
5013 | ||
feff2f05 | 5014 | -- Ignore case of enumeration with holes where the flag is set not to |
5015 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 5016 | |
5017 | elsif Is_Enumeration_Type (Typ) | |
5018 | and then Has_Non_Standard_Rep (Typ) | |
5019 | and then Holes_OK | |
5020 | then | |
5021 | return; | |
5022 | ||
f2a06be9 | 5023 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 5024 | |
5025 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
5026 | and then Expr = Name (Parent (Expr)) | |
5027 | then | |
5028 | return; | |
5029 | ||
6fb3c314 | 5030 | -- No check on a universal real constant. The context will eventually |
38f5559f | 5031 | -- convert it to a machine number for some target type, or report an |
5032 | -- illegality. | |
5033 | ||
5034 | elsif Nkind (Expr) = N_Real_Literal | |
5035 | and then Etype (Expr) = Universal_Real | |
5036 | then | |
5037 | return; | |
5038 | ||
6fb3c314 | 5039 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 5040 | -- no possible check applies. We ignore the old ACATS chestnuts that |
5041 | -- involve Boolean range True..True. | |
5042 | ||
5043 | -- Note: validity checks are generated for expressions that yield a | |
5044 | -- scalar type, when it is possible to create a value that is outside of | |
5045 | -- the type. If this is a one-bit boolean no such value exists. This is | |
5046 | -- an optimization, and it also prevents compiler blowing up during the | |
5047 | -- elaboration of improperly expanded packed array references. | |
5048 | ||
5049 | elsif Nkind (Expr) = N_Indexed_Component | |
5050 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
5051 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
5052 | then | |
5053 | return; | |
5054 | ||
9dfe12ae | 5055 | -- An annoying special case. If this is an out parameter of a scalar |
5056 | -- type, then the value is not going to be accessed, therefore it is | |
5057 | -- inappropriate to do any validity check at the call site. | |
5058 | ||
5059 | else | |
5060 | -- Only need to worry about scalar types | |
5061 | ||
5062 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 5063 | declare |
5064 | P : Node_Id; | |
5065 | N : Node_Id; | |
5066 | E : Entity_Id; | |
5067 | F : Entity_Id; | |
5068 | A : Node_Id; | |
5069 | L : List_Id; | |
5070 | ||
5071 | begin | |
5072 | -- Find actual argument (which may be a parameter association) | |
5073 | -- and the parent of the actual argument (the call statement) | |
5074 | ||
5075 | N := Expr; | |
5076 | P := Parent (Expr); | |
5077 | ||
5078 | if Nkind (P) = N_Parameter_Association then | |
5079 | N := P; | |
5080 | P := Parent (N); | |
5081 | end if; | |
5082 | ||
feff2f05 | 5083 | -- Only need to worry if we are argument of a procedure call |
5084 | -- since functions don't have out parameters. If this is an | |
5085 | -- indirect or dispatching call, get signature from the | |
5086 | -- subprogram type. | |
ee6ba406 | 5087 | |
5088 | if Nkind (P) = N_Procedure_Call_Statement then | |
5089 | L := Parameter_Associations (P); | |
9dfe12ae | 5090 | |
5091 | if Is_Entity_Name (Name (P)) then | |
5092 | E := Entity (Name (P)); | |
5093 | else | |
5094 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
5095 | E := Etype (Name (P)); | |
5096 | end if; | |
ee6ba406 | 5097 | |
feff2f05 | 5098 | -- Only need to worry if there are indeed actuals, and if |
5099 | -- this could be a procedure call, otherwise we cannot get a | |
5100 | -- match (either we are not an argument, or the mode of the | |
5101 | -- formal is not OUT). This test also filters out the | |
5102 | -- generic case. | |
ee6ba406 | 5103 | |
5104 | if Is_Non_Empty_List (L) | |
5105 | and then Is_Subprogram (E) | |
5106 | then | |
feff2f05 | 5107 | -- This is the loop through parameters, looking for an |
5108 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 5109 | |
5110 | F := First_Formal (E); | |
5111 | A := First (L); | |
ee6ba406 | 5112 | while Present (F) loop |
5113 | if Ekind (F) = E_Out_Parameter and then A = N then | |
5114 | return; | |
5115 | end if; | |
5116 | ||
5117 | Next_Formal (F); | |
5118 | Next (A); | |
5119 | end loop; | |
5120 | end if; | |
5121 | end if; | |
5122 | end; | |
5123 | end if; | |
5124 | end if; | |
5125 | ||
fa6a6949 | 5126 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 5127 | -- checking: if they are valid, a boolean or short-circuit operation |
5128 | -- with them will be valid as well. | |
784d4230 | 5129 | |
5130 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 5131 | and then |
fa6a6949 | 5132 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 5133 | then |
5134 | return; | |
5135 | end if; | |
5136 | ||
0577b0b1 | 5137 | -- If we fall through, a validity check is required |
ee6ba406 | 5138 | |
5139 | Insert_Valid_Check (Expr); | |
ce7498d3 | 5140 | |
5141 | if Is_Entity_Name (Expr) | |
5142 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
5143 | then | |
5144 | Set_Is_Known_Valid (Entity (Expr)); | |
5145 | end if; | |
ee6ba406 | 5146 | end Ensure_Valid; |
5147 | ||
5148 | ---------------------- | |
5149 | -- Expr_Known_Valid -- | |
5150 | ---------------------- | |
5151 | ||
5152 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
5153 | Typ : constant Entity_Id := Etype (Expr); | |
5154 | ||
5155 | begin | |
feff2f05 | 5156 | -- Non-scalar types are always considered valid, since they never give |
5157 | -- rise to the issues of erroneous or bounded error behavior that are | |
5158 | -- the concern. In formal reference manual terms the notion of validity | |
5159 | -- only applies to scalar types. Note that even when packed arrays are | |
5160 | -- represented using modular types, they are still arrays semantically, | |
5161 | -- so they are also always valid (in particular, the unused bits can be | |
5162 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 5163 | |
fa814356 | 5164 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then |
ee6ba406 | 5165 | return True; |
5166 | ||
5167 | -- If no validity checking, then everything is considered valid | |
5168 | ||
5169 | elsif not Validity_Checks_On then | |
5170 | return True; | |
5171 | ||
5172 | -- Floating-point types are considered valid unless floating-point | |
5173 | -- validity checks have been specifically turned on. | |
5174 | ||
5175 | elsif Is_Floating_Point_Type (Typ) | |
5176 | and then not Validity_Check_Floating_Point | |
5177 | then | |
5178 | return True; | |
5179 | ||
feff2f05 | 5180 | -- If the expression is the value of an object that is known to be |
5181 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 5182 | |
5183 | elsif Is_Entity_Name (Expr) | |
5184 | and then Is_Known_Valid (Entity (Expr)) | |
5185 | then | |
5186 | return True; | |
5187 | ||
0577b0b1 | 5188 | -- References to discriminants are always considered valid. The value |
5189 | -- of a discriminant gets checked when the object is built. Within the | |
5190 | -- record, we consider it valid, and it is important to do so, since | |
5191 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 5192 | -- reference discriminants out of scope. Discriminants of concurrent |
5193 | -- types are excluded for the same reason. | |
0577b0b1 | 5194 | |
5195 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 5196 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 5197 | then |
5198 | return True; | |
5199 | ||
feff2f05 | 5200 | -- If the type is one for which all values are known valid, then we are |
5201 | -- sure that the value is valid except in the slightly odd case where | |
5202 | -- the expression is a reference to a variable whose size has been | |
5203 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 5204 | |
5205 | elsif Is_Known_Valid (Typ) then | |
5206 | if Is_Entity_Name (Expr) | |
5207 | and then Ekind (Entity (Expr)) = E_Variable | |
5208 | and then Esize (Entity (Expr)) > Esize (Typ) | |
5209 | then | |
5210 | return False; | |
5211 | else | |
5212 | return True; | |
5213 | end if; | |
5214 | ||
5215 | -- Integer and character literals always have valid values, where | |
5216 | -- appropriate these will be range checked in any case. | |
5217 | ||
5218 | elsif Nkind (Expr) = N_Integer_Literal | |
5219 | or else | |
5220 | Nkind (Expr) = N_Character_Literal | |
5221 | then | |
5222 | return True; | |
5223 | ||
91e47010 | 5224 | -- Real literals are assumed to be valid in VM targets |
5225 | ||
5226 | elsif VM_Target /= No_VM | |
5227 | and then Nkind (Expr) = N_Real_Literal | |
5228 | then | |
5229 | return True; | |
5230 | ||
ee6ba406 | 5231 | -- If we have a type conversion or a qualification of a known valid |
5232 | -- value, then the result will always be valid. | |
5233 | ||
5234 | elsif Nkind (Expr) = N_Type_Conversion | |
5235 | or else | |
5236 | Nkind (Expr) = N_Qualified_Expression | |
5237 | then | |
5238 | return Expr_Known_Valid (Expression (Expr)); | |
5239 | ||
38f5559f | 5240 | -- The result of any operator is always considered valid, since we |
5241 | -- assume the necessary checks are done by the operator. For operators | |
5242 | -- on floating-point operations, we must also check when the operation | |
5243 | -- is the right-hand side of an assignment, or is an actual in a call. | |
ee6ba406 | 5244 | |
0577b0b1 | 5245 | elsif Nkind (Expr) in N_Op then |
1d90d657 | 5246 | if Is_Floating_Point_Type (Typ) |
5247 | and then Validity_Check_Floating_Point | |
5248 | and then | |
5249 | (Nkind (Parent (Expr)) = N_Assignment_Statement | |
5250 | or else Nkind (Parent (Expr)) = N_Function_Call | |
5251 | or else Nkind (Parent (Expr)) = N_Parameter_Association) | |
5252 | then | |
5253 | return False; | |
5254 | else | |
5255 | return True; | |
5256 | end if; | |
5257 | ||
feff2f05 | 5258 | -- The result of a membership test is always valid, since it is true or |
5259 | -- false, there are no other possibilities. | |
0577b0b1 | 5260 | |
5261 | elsif Nkind (Expr) in N_Membership_Test then | |
5262 | return True; | |
5263 | ||
ee6ba406 | 5264 | -- For all other cases, we do not know the expression is valid |
5265 | ||
5266 | else | |
5267 | return False; | |
5268 | end if; | |
5269 | end Expr_Known_Valid; | |
5270 | ||
9dfe12ae | 5271 | ---------------- |
5272 | -- Find_Check -- | |
5273 | ---------------- | |
5274 | ||
5275 | procedure Find_Check | |
5276 | (Expr : Node_Id; | |
5277 | Check_Type : Character; | |
5278 | Target_Type : Entity_Id; | |
5279 | Entry_OK : out Boolean; | |
5280 | Check_Num : out Nat; | |
5281 | Ent : out Entity_Id; | |
5282 | Ofs : out Uint) | |
5283 | is | |
5284 | function Within_Range_Of | |
5285 | (Target_Type : Entity_Id; | |
314a23b6 | 5286 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 5287 | -- Given a requirement for checking a range against Target_Type, and |
5288 | -- and a range Check_Type against which a check has already been made, | |
5289 | -- determines if the check against check type is sufficient to ensure | |
5290 | -- that no check against Target_Type is required. | |
5291 | ||
5292 | --------------------- | |
5293 | -- Within_Range_Of -- | |
5294 | --------------------- | |
5295 | ||
5296 | function Within_Range_Of | |
5297 | (Target_Type : Entity_Id; | |
314a23b6 | 5298 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 5299 | is |
5300 | begin | |
5301 | if Target_Type = Check_Type then | |
5302 | return True; | |
5303 | ||
5304 | else | |
5305 | declare | |
5306 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
5307 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
5308 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
5309 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
5310 | ||
5311 | begin | |
5312 | if (Tlo = Clo | |
5313 | or else (Compile_Time_Known_Value (Tlo) | |
5314 | and then | |
5315 | Compile_Time_Known_Value (Clo) | |
5316 | and then | |
5317 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
5318 | and then | |
5319 | (Thi = Chi | |
5320 | or else (Compile_Time_Known_Value (Thi) | |
5321 | and then | |
5322 | Compile_Time_Known_Value (Chi) | |
5323 | and then | |
5324 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
5325 | then | |
5326 | return True; | |
5327 | else | |
5328 | return False; | |
5329 | end if; | |
5330 | end; | |
5331 | end if; | |
5332 | end Within_Range_Of; | |
5333 | ||
5334 | -- Start of processing for Find_Check | |
5335 | ||
5336 | begin | |
ed195555 | 5337 | -- Establish default, in case no entry is found |
9dfe12ae | 5338 | |
5339 | Check_Num := 0; | |
5340 | ||
5341 | -- Case of expression is simple entity reference | |
5342 | ||
5343 | if Is_Entity_Name (Expr) then | |
5344 | Ent := Entity (Expr); | |
5345 | Ofs := Uint_0; | |
5346 | ||
5347 | -- Case of expression is entity + known constant | |
5348 | ||
5349 | elsif Nkind (Expr) = N_Op_Add | |
5350 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5351 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5352 | then | |
5353 | Ent := Entity (Left_Opnd (Expr)); | |
5354 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
5355 | ||
5356 | -- Case of expression is entity - known constant | |
5357 | ||
5358 | elsif Nkind (Expr) = N_Op_Subtract | |
5359 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5360 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5361 | then | |
5362 | Ent := Entity (Left_Opnd (Expr)); | |
5363 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
5364 | ||
5365 | -- Any other expression is not of the right form | |
5366 | ||
5367 | else | |
5368 | Ent := Empty; | |
5369 | Ofs := Uint_0; | |
5370 | Entry_OK := False; | |
5371 | return; | |
5372 | end if; | |
5373 | ||
feff2f05 | 5374 | -- Come here with expression of appropriate form, check if entity is an |
5375 | -- appropriate one for our purposes. | |
9dfe12ae | 5376 | |
5377 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 5378 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 5379 | and then not Is_Library_Level_Entity (Ent) |
5380 | then | |
5381 | Entry_OK := True; | |
5382 | else | |
5383 | Entry_OK := False; | |
5384 | return; | |
5385 | end if; | |
5386 | ||
5387 | -- See if there is matching check already | |
5388 | ||
5389 | for J in reverse 1 .. Num_Saved_Checks loop | |
5390 | declare | |
5391 | SC : Saved_Check renames Saved_Checks (J); | |
5392 | ||
5393 | begin | |
5394 | if SC.Killed = False | |
5395 | and then SC.Entity = Ent | |
5396 | and then SC.Offset = Ofs | |
5397 | and then SC.Check_Type = Check_Type | |
5398 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
5399 | then | |
5400 | Check_Num := J; | |
5401 | return; | |
5402 | end if; | |
5403 | end; | |
5404 | end loop; | |
5405 | ||
5406 | -- If we fall through entry was not found | |
5407 | ||
9dfe12ae | 5408 | return; |
5409 | end Find_Check; | |
5410 | ||
5411 | --------------------------------- | |
5412 | -- Generate_Discriminant_Check -- | |
5413 | --------------------------------- | |
5414 | ||
5415 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 5416 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 5417 | |
5418 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
5419 | Loc : constant Source_Ptr := Sloc (N); | |
5420 | Pref : constant Node_Id := Prefix (N); | |
5421 | Sel : constant Node_Id := Selector_Name (N); | |
5422 | ||
5423 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 5424 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 5425 | -- The original component to be checked |
5426 | ||
5427 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 5428 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 5429 | -- The discriminant checking function |
5430 | ||
5431 | Discr : Entity_Id; | |
5432 | -- One discriminant to be checked in the type | |
5433 | ||
5434 | Real_Discr : Entity_Id; | |
5435 | -- Actual discriminant in the call | |
5436 | ||
5437 | Pref_Type : Entity_Id; | |
5438 | -- Type of relevant prefix (ignoring private/access stuff) | |
5439 | ||
5440 | Args : List_Id; | |
5441 | -- List of arguments for function call | |
5442 | ||
5443 | Formal : Entity_Id; | |
feff2f05 | 5444 | -- Keep track of the formal corresponding to the actual we build for |
5445 | -- each discriminant, in order to be able to perform the necessary type | |
5446 | -- conversions. | |
9dfe12ae | 5447 | |
5448 | Scomp : Node_Id; | |
5449 | -- Selected component reference for checking function argument | |
5450 | ||
5451 | begin | |
5452 | Pref_Type := Etype (Pref); | |
5453 | ||
5454 | -- Force evaluation of the prefix, so that it does not get evaluated | |
5455 | -- twice (once for the check, once for the actual reference). Such a | |
5456 | -- double evaluation is always a potential source of inefficiency, | |
5457 | -- and is functionally incorrect in the volatile case, or when the | |
5458 | -- prefix may have side-effects. An entity or a component of an | |
5459 | -- entity requires no evaluation. | |
5460 | ||
5461 | if Is_Entity_Name (Pref) then | |
5462 | if Treat_As_Volatile (Entity (Pref)) then | |
5463 | Force_Evaluation (Pref, Name_Req => True); | |
5464 | end if; | |
5465 | ||
5466 | elsif Treat_As_Volatile (Etype (Pref)) then | |
5467 | Force_Evaluation (Pref, Name_Req => True); | |
5468 | ||
5469 | elsif Nkind (Pref) = N_Selected_Component | |
5470 | and then Is_Entity_Name (Prefix (Pref)) | |
5471 | then | |
5472 | null; | |
5473 | ||
5474 | else | |
5475 | Force_Evaluation (Pref, Name_Req => True); | |
5476 | end if; | |
5477 | ||
5478 | -- For a tagged type, use the scope of the original component to | |
5479 | -- obtain the type, because ??? | |
5480 | ||
5481 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
5482 | Pref_Type := Scope (Orig_Comp); | |
5483 | ||
feff2f05 | 5484 | -- For an untagged derived type, use the discriminants of the parent |
5485 | -- which have been renamed in the derivation, possibly by a one-to-many | |
5486 | -- discriminant constraint. For non-tagged type, initially get the Etype | |
5487 | -- of the prefix | |
9dfe12ae | 5488 | |
5489 | else | |
5490 | if Is_Derived_Type (Pref_Type) | |
5491 | and then Number_Discriminants (Pref_Type) /= | |
5492 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
5493 | then | |
5494 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
5495 | end if; | |
5496 | end if; | |
5497 | ||
5498 | -- We definitely should have a checking function, This routine should | |
5499 | -- not be called if no discriminant checking function is present. | |
5500 | ||
5501 | pragma Assert (Present (Discr_Fct)); | |
5502 | ||
5503 | -- Create the list of the actual parameters for the call. This list | |
5504 | -- is the list of the discriminant fields of the record expression to | |
5505 | -- be discriminant checked. | |
5506 | ||
5507 | Args := New_List; | |
5508 | Formal := First_Formal (Discr_Fct); | |
5509 | Discr := First_Discriminant (Pref_Type); | |
5510 | while Present (Discr) loop | |
5511 | ||
5512 | -- If we have a corresponding discriminant field, and a parent | |
5513 | -- subtype is present, then we want to use the corresponding | |
5514 | -- discriminant since this is the one with the useful value. | |
5515 | ||
5516 | if Present (Corresponding_Discriminant (Discr)) | |
5517 | and then Ekind (Pref_Type) = E_Record_Type | |
5518 | and then Present (Parent_Subtype (Pref_Type)) | |
5519 | then | |
5520 | Real_Discr := Corresponding_Discriminant (Discr); | |
5521 | else | |
5522 | Real_Discr := Discr; | |
5523 | end if; | |
5524 | ||
5525 | -- Construct the reference to the discriminant | |
5526 | ||
5527 | Scomp := | |
5528 | Make_Selected_Component (Loc, | |
5529 | Prefix => | |
5530 | Unchecked_Convert_To (Pref_Type, | |
5531 | Duplicate_Subexpr (Pref)), | |
5532 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
5533 | ||
5534 | -- Manually analyze and resolve this selected component. We really | |
5535 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 5536 | -- playing discriminal games etc with this reference. Then we append |
5537 | -- the argument to the list we are gathering. | |
9dfe12ae | 5538 | |
5539 | Set_Etype (Scomp, Etype (Real_Discr)); | |
5540 | Set_Analyzed (Scomp, True); | |
5541 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
5542 | ||
5543 | Next_Formal_With_Extras (Formal); | |
5544 | Next_Discriminant (Discr); | |
5545 | end loop; | |
5546 | ||
5547 | -- Now build and insert the call | |
5548 | ||
5549 | Insert_Action (N, | |
5550 | Make_Raise_Constraint_Error (Loc, | |
5551 | Condition => | |
5552 | Make_Function_Call (Loc, | |
5553 | Name => New_Occurrence_Of (Discr_Fct, Loc), | |
5554 | Parameter_Associations => Args), | |
5555 | Reason => CE_Discriminant_Check_Failed)); | |
5556 | end Generate_Discriminant_Check; | |
5557 | ||
5c99c290 | 5558 | --------------------------- |
5559 | -- Generate_Index_Checks -- | |
5560 | --------------------------- | |
9dfe12ae | 5561 | |
5562 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 5563 | |
5564 | function Entity_Of_Prefix return Entity_Id; | |
5565 | -- Returns the entity of the prefix of N (or Empty if not found) | |
5566 | ||
3f42e2a7 | 5567 | ---------------------- |
5568 | -- Entity_Of_Prefix -- | |
5569 | ---------------------- | |
5570 | ||
05f3e139 | 5571 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 5572 | P : Node_Id; |
5573 | ||
05f3e139 | 5574 | begin |
e5d38095 | 5575 | P := Prefix (N); |
05f3e139 | 5576 | while not Is_Entity_Name (P) loop |
5577 | if not Nkind_In (P, N_Selected_Component, | |
5578 | N_Indexed_Component) | |
5579 | then | |
5580 | return Empty; | |
5581 | end if; | |
5582 | ||
5583 | P := Prefix (P); | |
5584 | end loop; | |
5585 | ||
5586 | return Entity (P); | |
5587 | end Entity_Of_Prefix; | |
5588 | ||
5589 | -- Local variables | |
5590 | ||
5591 | Loc : constant Source_Ptr := Sloc (N); | |
5592 | A : constant Node_Id := Prefix (N); | |
5593 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
5594 | Sub : Node_Id; | |
9dfe12ae | 5595 | |
3f42e2a7 | 5596 | -- Start of processing for Generate_Index_Checks |
5597 | ||
9dfe12ae | 5598 | begin |
05f3e139 | 5599 | -- Ignore call if the prefix is not an array since we have a serious |
5600 | -- error in the sources. Ignore it also if index checks are suppressed | |
5601 | -- for array object or type. | |
0577b0b1 | 5602 | |
05f3e139 | 5603 | if not Is_Array_Type (Etype (A)) |
5604 | or else (Present (A_Ent) | |
e5d38095 | 5605 | and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 5606 | or else Index_Checks_Suppressed (Etype (A)) |
5607 | then | |
5608 | return; | |
df9fba45 | 5609 | |
5610 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
5611 | -- prefix. This case arises when analysis has determined that constructs | |
5612 | -- such as | |
5613 | ||
5614 | -- Prefix'Loop_Entry (Expr) | |
5615 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
5616 | ||
5617 | -- require rewriting for error detection purposes. A side effect of this | |
5618 | -- action is the generation of index checks that mention 'Loop_Entry. | |
5619 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
5620 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
5621 | ||
5622 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
5623 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
5624 | then | |
5625 | return; | |
0577b0b1 | 5626 | end if; |
5627 | ||
05f3e139 | 5628 | -- Generate a raise of constraint error with the appropriate reason and |
5629 | -- a condition of the form: | |
5630 | ||
3f42e2a7 | 5631 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 5632 | |
5633 | -- Note that the reason we generate the conversion to the base type here | |
5634 | -- is that we definitely want the range check to take place, even if it | |
5635 | -- looks like the subtype is OK. Optimization considerations that allow | |
5636 | -- us to omit the check have already been taken into account in the | |
5637 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 5638 | |
9dfe12ae | 5639 | Sub := First (Expressions (N)); |
05f3e139 | 5640 | |
5641 | -- Handle string literals | |
5642 | ||
5643 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 5644 | if Do_Range_Check (Sub) then |
5645 | Set_Do_Range_Check (Sub, False); | |
5646 | ||
05f3e139 | 5647 | -- For string literals we obtain the bounds of the string from the |
5648 | -- associated subtype. | |
9dfe12ae | 5649 | |
05f3e139 | 5650 | Insert_Action (N, |
094ed68e | 5651 | Make_Raise_Constraint_Error (Loc, |
5652 | Condition => | |
5653 | Make_Not_In (Loc, | |
5654 | Left_Opnd => | |
5655 | Convert_To (Base_Type (Etype (Sub)), | |
5656 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5657 | Right_Opnd => | |
5658 | Make_Attribute_Reference (Loc, | |
5659 | Prefix => New_Reference_To (Etype (A), Loc), | |
5660 | Attribute_Name => Name_Range)), | |
5661 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5662 | end if; |
9dfe12ae | 5663 | |
05f3e139 | 5664 | -- General case |
9dfe12ae | 5665 | |
05f3e139 | 5666 | else |
5667 | declare | |
5668 | A_Idx : Node_Id := Empty; | |
5669 | A_Range : Node_Id; | |
5670 | Ind : Nat; | |
5671 | Num : List_Id; | |
5672 | Range_N : Node_Id; | |
9dfe12ae | 5673 | |
05f3e139 | 5674 | begin |
5675 | A_Idx := First_Index (Etype (A)); | |
5676 | Ind := 1; | |
5677 | while Present (Sub) loop | |
5678 | if Do_Range_Check (Sub) then | |
5679 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 5680 | |
05f3e139 | 5681 | -- Force evaluation except for the case of a simple name of |
5682 | -- a non-volatile entity. | |
9dfe12ae | 5683 | |
05f3e139 | 5684 | if not Is_Entity_Name (Sub) |
5685 | or else Treat_As_Volatile (Entity (Sub)) | |
5686 | then | |
5687 | Force_Evaluation (Sub); | |
5688 | end if; | |
9dfe12ae | 5689 | |
05f3e139 | 5690 | if Nkind (A_Idx) = N_Range then |
5691 | A_Range := A_Idx; | |
5692 | ||
5693 | elsif Nkind (A_Idx) = N_Identifier | |
5694 | or else Nkind (A_Idx) = N_Expanded_Name | |
5695 | then | |
5696 | A_Range := Scalar_Range (Entity (A_Idx)); | |
5697 | ||
5698 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
5699 | A_Range := Range_Expression (Constraint (A_Idx)); | |
5700 | end if; | |
5701 | ||
5702 | -- For array objects with constant bounds we can generate | |
5703 | -- the index check using the bounds of the type of the index | |
5704 | ||
5705 | if Present (A_Ent) | |
5706 | and then Ekind (A_Ent) = E_Variable | |
5707 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
5708 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
5709 | then | |
5710 | Range_N := | |
5711 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 5712 | Prefix => |
5713 | New_Reference_To (Etype (A_Idx), Loc), | |
05f3e139 | 5714 | Attribute_Name => Name_Range); |
5715 | ||
5716 | -- For arrays with non-constant bounds we cannot generate | |
5717 | -- the index check using the bounds of the type of the index | |
5718 | -- since it may reference discriminants of some enclosing | |
5719 | -- type. We obtain the bounds directly from the prefix | |
5720 | -- object. | |
5721 | ||
5722 | else | |
5723 | if Ind = 1 then | |
5724 | Num := No_List; | |
5725 | else | |
5726 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
5727 | end if; | |
5728 | ||
5729 | Range_N := | |
5730 | Make_Attribute_Reference (Loc, | |
5731 | Prefix => | |
5732 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
5733 | Attribute_Name => Name_Range, | |
5734 | Expressions => Num); | |
5735 | end if; | |
5736 | ||
5737 | Insert_Action (N, | |
094ed68e | 5738 | Make_Raise_Constraint_Error (Loc, |
5739 | Condition => | |
5740 | Make_Not_In (Loc, | |
5741 | Left_Opnd => | |
5742 | Convert_To (Base_Type (Etype (Sub)), | |
5743 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5744 | Right_Opnd => Range_N), | |
5745 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5746 | end if; |
5747 | ||
5748 | A_Idx := Next_Index (A_Idx); | |
5749 | Ind := Ind + 1; | |
5750 | Next (Sub); | |
5751 | end loop; | |
5752 | end; | |
5753 | end if; | |
9dfe12ae | 5754 | end Generate_Index_Checks; |
5755 | ||
5756 | -------------------------- | |
5757 | -- Generate_Range_Check -- | |
5758 | -------------------------- | |
5759 | ||
5760 | procedure Generate_Range_Check | |
5761 | (N : Node_Id; | |
5762 | Target_Type : Entity_Id; | |
5763 | Reason : RT_Exception_Code) | |
5764 | is | |
5765 | Loc : constant Source_Ptr := Sloc (N); | |
5766 | Source_Type : constant Entity_Id := Etype (N); | |
5767 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
5768 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
5769 | ||
5770 | begin | |
feff2f05 | 5771 | -- First special case, if the source type is already within the range |
5772 | -- of the target type, then no check is needed (probably we should have | |
5773 | -- stopped Do_Range_Check from being set in the first place, but better | |
b40670e1 | 5774 | -- late than never in preventing junk code! |
9dfe12ae | 5775 | |
7a1dabb3 | 5776 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 5777 | |
5778 | -- We do NOT apply this if the source node is a literal, since in this | |
5779 | -- case the literal has already been labeled as having the subtype of | |
5780 | -- the target. | |
5781 | ||
9dfe12ae | 5782 | and then not |
b40670e1 | 5783 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 5784 | or else |
b40670e1 | 5785 | (Is_Entity_Name (N) |
5786 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
5787 | ||
5788 | -- Also do not apply this for floating-point if Check_Float_Overflow | |
5789 | ||
5790 | and then not | |
5791 | (Is_Floating_Point_Type (Source_Type) and Check_Float_Overflow) | |
9dfe12ae | 5792 | then |
5793 | return; | |
5794 | end if; | |
5795 | ||
5796 | -- We need a check, so force evaluation of the node, so that it does | |
5797 | -- not get evaluated twice (once for the check, once for the actual | |
5798 | -- reference). Such a double evaluation is always a potential source | |
5799 | -- of inefficiency, and is functionally incorrect in the volatile case. | |
5800 | ||
b40670e1 | 5801 | if not Is_Entity_Name (N) or else Treat_As_Volatile (Entity (N)) then |
9dfe12ae | 5802 | Force_Evaluation (N); |
5803 | end if; | |
5804 | ||
feff2f05 | 5805 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
5806 | -- the same since in this case we can simply do a direct check of the | |
5807 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 5808 | |
5809 | -- [constraint_error when N not in Target_Type] | |
5810 | ||
5811 | -- Note: this is by far the most common case, for example all cases of | |
5812 | -- checks on the RHS of assignments are in this category, but not all | |
5813 | -- cases are like this. Notably conversions can involve two types. | |
5814 | ||
5815 | if Source_Base_Type = Target_Base_Type then | |
5816 | Insert_Action (N, | |
5817 | Make_Raise_Constraint_Error (Loc, | |
5818 | Condition => | |
5819 | Make_Not_In (Loc, | |
5820 | Left_Opnd => Duplicate_Subexpr (N), | |
5821 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
5822 | Reason => Reason)); | |
5823 | ||
5824 | -- Next test for the case where the target type is within the bounds | |
5825 | -- of the base type of the source type, since in this case we can | |
5826 | -- simply convert these bounds to the base type of T to do the test. | |
5827 | ||
5828 | -- [constraint_error when N not in | |
5829 | -- Source_Base_Type (Target_Type'First) | |
5830 | -- .. | |
5831 | -- Source_Base_Type(Target_Type'Last))] | |
5832 | ||
f2a06be9 | 5833 | -- The conversions will always work and need no check |
9dfe12ae | 5834 | |
a9b57347 | 5835 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
5836 | -- of converting from an enumeration value to an integer type, such as | |
5837 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
5838 | -- (which used to be handled by gigi). This is OK, since the conversion | |
5839 | -- itself does not require a check. | |
5840 | ||
7a1dabb3 | 5841 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
9dfe12ae | 5842 | Insert_Action (N, |
5843 | Make_Raise_Constraint_Error (Loc, | |
5844 | Condition => | |
5845 | Make_Not_In (Loc, | |
5846 | Left_Opnd => Duplicate_Subexpr (N), | |
5847 | ||
5848 | Right_Opnd => | |
5849 | Make_Range (Loc, | |
5850 | Low_Bound => | |
a9b57347 | 5851 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 5852 | Make_Attribute_Reference (Loc, |
5853 | Prefix => | |
5854 | New_Occurrence_Of (Target_Type, Loc), | |
5855 | Attribute_Name => Name_First)), | |
5856 | ||
5857 | High_Bound => | |
a9b57347 | 5858 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 5859 | Make_Attribute_Reference (Loc, |
5860 | Prefix => | |
5861 | New_Occurrence_Of (Target_Type, Loc), | |
5862 | Attribute_Name => Name_Last)))), | |
5863 | Reason => Reason)); | |
5864 | ||
feff2f05 | 5865 | -- Note that at this stage we now that the Target_Base_Type is not in |
5866 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
5867 | -- is not in this range). It could still be the case that Source_Type is | |
5868 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 5869 | |
feff2f05 | 5870 | -- If that is the case, we can freely convert the source to the target, |
5871 | -- and then test the target result against the bounds. | |
9dfe12ae | 5872 | |
7a1dabb3 | 5873 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
9dfe12ae | 5874 | |
feff2f05 | 5875 | -- We make a temporary to hold the value of the converted value |
5876 | -- (converted to the base type), and then we will do the test against | |
5877 | -- this temporary. | |
9dfe12ae | 5878 | |
5879 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); | |
5880 | -- [constraint_error when Tnn not in Target_Type] | |
5881 | ||
5882 | -- Then the conversion itself is replaced by an occurrence of Tnn | |
5883 | ||
5884 | declare | |
46eb6933 | 5885 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 5886 | |
5887 | begin | |
5888 | Insert_Actions (N, New_List ( | |
5889 | Make_Object_Declaration (Loc, | |
5890 | Defining_Identifier => Tnn, | |
5891 | Object_Definition => | |
5892 | New_Occurrence_Of (Target_Base_Type, Loc), | |
5893 | Constant_Present => True, | |
5894 | Expression => | |
5895 | Make_Type_Conversion (Loc, | |
5896 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
5897 | Expression => Duplicate_Subexpr (N))), | |
5898 | ||
5899 | Make_Raise_Constraint_Error (Loc, | |
5900 | Condition => | |
5901 | Make_Not_In (Loc, | |
5902 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
5903 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
5904 | ||
5905 | Reason => Reason))); | |
5906 | ||
5907 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
2af58f67 | 5908 | |
5909 | -- Set the type of N, because the declaration for Tnn might not | |
5910 | -- be analyzed yet, as is the case if N appears within a record | |
5911 | -- declaration, as a discriminant constraint or expression. | |
5912 | ||
5913 | Set_Etype (N, Target_Base_Type); | |
9dfe12ae | 5914 | end; |
5915 | ||
5916 | -- At this stage, we know that we have two scalar types, which are | |
5917 | -- directly convertible, and where neither scalar type has a base | |
5918 | -- range that is in the range of the other scalar type. | |
5919 | ||
5920 | -- The only way this can happen is with a signed and unsigned type. | |
5921 | -- So test for these two cases: | |
5922 | ||
5923 | else | |
5924 | -- Case of the source is unsigned and the target is signed | |
5925 | ||
5926 | if Is_Unsigned_Type (Source_Base_Type) | |
5927 | and then not Is_Unsigned_Type (Target_Base_Type) | |
5928 | then | |
5929 | -- If the source is unsigned and the target is signed, then we | |
5930 | -- know that the source is not shorter than the target (otherwise | |
5931 | -- the source base type would be in the target base type range). | |
5932 | ||
feff2f05 | 5933 | -- In other words, the unsigned type is either the same size as |
5934 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 5935 | |
5936 | pragma Assert | |
5937 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
5938 | ||
5939 | -- We only need to check the low bound if the low bound of the | |
5940 | -- target type is non-negative. If the low bound of the target | |
5941 | -- type is negative, then we know that we will fit fine. | |
5942 | ||
5943 | -- If the high bound of the target type is negative, then we | |
5944 | -- know we have a constraint error, since we can't possibly | |
5945 | -- have a negative source. | |
5946 | ||
5947 | -- With these two checks out of the way, we can do the check | |
5948 | -- using the source type safely | |
5949 | ||
5950 | -- This is definitely the most annoying case! | |
5951 | ||
5952 | -- [constraint_error | |
5953 | -- when (Target_Type'First >= 0 | |
5954 | -- and then | |
5955 | -- N < Source_Base_Type (Target_Type'First)) | |
5956 | -- or else Target_Type'Last < 0 | |
5957 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
5958 | ||
5959 | -- We turn off all checks since we know that the conversions | |
5960 | -- will work fine, given the guards for negative values. | |
5961 | ||
5962 | Insert_Action (N, | |
5963 | Make_Raise_Constraint_Error (Loc, | |
5964 | Condition => | |
5965 | Make_Or_Else (Loc, | |
5966 | Make_Or_Else (Loc, | |
5967 | Left_Opnd => | |
5968 | Make_And_Then (Loc, | |
5969 | Left_Opnd => Make_Op_Ge (Loc, | |
5970 | Left_Opnd => | |
5971 | Make_Attribute_Reference (Loc, | |
5972 | Prefix => | |
5973 | New_Occurrence_Of (Target_Type, Loc), | |
5974 | Attribute_Name => Name_First), | |
5975 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
5976 | ||
5977 | Right_Opnd => | |
5978 | Make_Op_Lt (Loc, | |
5979 | Left_Opnd => Duplicate_Subexpr (N), | |
5980 | Right_Opnd => | |
5981 | Convert_To (Source_Base_Type, | |
5982 | Make_Attribute_Reference (Loc, | |
5983 | Prefix => | |
5984 | New_Occurrence_Of (Target_Type, Loc), | |
5985 | Attribute_Name => Name_First)))), | |
5986 | ||
5987 | Right_Opnd => | |
5988 | Make_Op_Lt (Loc, | |
5989 | Left_Opnd => | |
5990 | Make_Attribute_Reference (Loc, | |
5991 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
5992 | Attribute_Name => Name_Last), | |
5993 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
5994 | ||
5995 | Right_Opnd => | |
5996 | Make_Op_Gt (Loc, | |
5997 | Left_Opnd => Duplicate_Subexpr (N), | |
5998 | Right_Opnd => | |
5999 | Convert_To (Source_Base_Type, | |
6000 | Make_Attribute_Reference (Loc, | |
6001 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6002 | Attribute_Name => Name_Last)))), | |
6003 | ||
6004 | Reason => Reason), | |
6005 | Suppress => All_Checks); | |
6006 | ||
6007 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 6008 | -- the target is unsigned. |
9dfe12ae | 6009 | |
6010 | else | |
6011 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
6012 | and then Is_Unsigned_Type (Target_Base_Type)); | |
6013 | ||
feff2f05 | 6014 | -- If the source is signed and the target is unsigned, then we |
6015 | -- know that the target is not shorter than the source (otherwise | |
6016 | -- the target base type would be in the source base type range). | |
9dfe12ae | 6017 | |
feff2f05 | 6018 | -- In other words, the unsigned type is either the same size as |
6019 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 6020 | |
feff2f05 | 6021 | -- Clearly we have an error if the source value is negative since |
6022 | -- no unsigned type can have negative values. If the source type | |
6023 | -- is non-negative, then the check can be done using the target | |
6024 | -- type. | |
9dfe12ae | 6025 | |
6026 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
6027 | ||
6028 | -- [constraint_error | |
6029 | -- when N < 0 or else Tnn not in Target_Type]; | |
6030 | ||
feff2f05 | 6031 | -- We turn off all checks for the conversion of N to the target |
6032 | -- base type, since we generate the explicit check to ensure that | |
6033 | -- the value is non-negative | |
9dfe12ae | 6034 | |
6035 | declare | |
46eb6933 | 6036 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 6037 | |
6038 | begin | |
6039 | Insert_Actions (N, New_List ( | |
6040 | Make_Object_Declaration (Loc, | |
6041 | Defining_Identifier => Tnn, | |
6042 | Object_Definition => | |
6043 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6044 | Constant_Present => True, | |
6045 | Expression => | |
a9b57347 | 6046 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 6047 | Subtype_Mark => |
6048 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6049 | Expression => Duplicate_Subexpr (N))), | |
6050 | ||
6051 | Make_Raise_Constraint_Error (Loc, | |
6052 | Condition => | |
6053 | Make_Or_Else (Loc, | |
6054 | Left_Opnd => | |
6055 | Make_Op_Lt (Loc, | |
6056 | Left_Opnd => Duplicate_Subexpr (N), | |
6057 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
6058 | ||
6059 | Right_Opnd => | |
6060 | Make_Not_In (Loc, | |
6061 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6062 | Right_Opnd => | |
6063 | New_Occurrence_Of (Target_Type, Loc))), | |
6064 | ||
6065 | Reason => Reason)), | |
6066 | Suppress => All_Checks); | |
6067 | ||
feff2f05 | 6068 | -- Set the Etype explicitly, because Insert_Actions may have |
6069 | -- placed the declaration in the freeze list for an enclosing | |
6070 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 6071 | |
6072 | Set_Etype (Tnn, Target_Base_Type); | |
6073 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6074 | end; | |
6075 | end if; | |
6076 | end if; | |
6077 | end Generate_Range_Check; | |
6078 | ||
2af58f67 | 6079 | ------------------ |
6080 | -- Get_Check_Id -- | |
6081 | ------------------ | |
6082 | ||
6083 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
6084 | begin | |
6085 | -- For standard check name, we can do a direct computation | |
6086 | ||
6087 | if N in First_Check_Name .. Last_Check_Name then | |
6088 | return Check_Id (N - (First_Check_Name - 1)); | |
6089 | ||
6090 | -- For non-standard names added by pragma Check_Name, search table | |
6091 | ||
6092 | else | |
6093 | for J in All_Checks + 1 .. Check_Names.Last loop | |
6094 | if Check_Names.Table (J) = N then | |
6095 | return J; | |
6096 | end if; | |
6097 | end loop; | |
6098 | end if; | |
6099 | ||
6100 | -- No matching name found | |
6101 | ||
6102 | return No_Check_Id; | |
6103 | end Get_Check_Id; | |
6104 | ||
ee6ba406 | 6105 | --------------------- |
6106 | -- Get_Discriminal -- | |
6107 | --------------------- | |
6108 | ||
6109 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
6110 | Loc : constant Source_Ptr := Sloc (E); | |
6111 | D : Entity_Id; | |
6112 | Sc : Entity_Id; | |
6113 | ||
6114 | begin | |
0577b0b1 | 6115 | -- The bound can be a bona fide parameter of a protected operation, |
6116 | -- rather than a prival encoded as an in-parameter. | |
6117 | ||
6118 | if No (Discriminal_Link (Entity (Bound))) then | |
6119 | return Bound; | |
6120 | end if; | |
6121 | ||
2af58f67 | 6122 | -- Climb the scope stack looking for an enclosing protected type. If |
6123 | -- we run out of scopes, return the bound itself. | |
6124 | ||
6125 | Sc := Scope (E); | |
6126 | while Present (Sc) loop | |
6127 | if Sc = Standard_Standard then | |
6128 | return Bound; | |
6129 | ||
6130 | elsif Ekind (Sc) = E_Protected_Type then | |
6131 | exit; | |
6132 | end if; | |
6133 | ||
6134 | Sc := Scope (Sc); | |
6135 | end loop; | |
6136 | ||
ee6ba406 | 6137 | D := First_Discriminant (Sc); |
2af58f67 | 6138 | while Present (D) loop |
6139 | if Chars (D) = Chars (Bound) then | |
6140 | return New_Occurrence_Of (Discriminal (D), Loc); | |
6141 | end if; | |
ee6ba406 | 6142 | |
ee6ba406 | 6143 | Next_Discriminant (D); |
6144 | end loop; | |
6145 | ||
2af58f67 | 6146 | return Bound; |
ee6ba406 | 6147 | end Get_Discriminal; |
6148 | ||
2af58f67 | 6149 | ---------------------- |
6150 | -- Get_Range_Checks -- | |
6151 | ---------------------- | |
6152 | ||
6153 | function Get_Range_Checks | |
6154 | (Ck_Node : Node_Id; | |
6155 | Target_Typ : Entity_Id; | |
6156 | Source_Typ : Entity_Id := Empty; | |
6157 | Warn_Node : Node_Id := Empty) return Check_Result | |
6158 | is | |
6159 | begin | |
6160 | return Selected_Range_Checks | |
6161 | (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
6162 | end Get_Range_Checks; | |
6163 | ||
ee6ba406 | 6164 | ------------------ |
6165 | -- Guard_Access -- | |
6166 | ------------------ | |
6167 | ||
6168 | function Guard_Access | |
6169 | (Cond : Node_Id; | |
6170 | Loc : Source_Ptr; | |
314a23b6 | 6171 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 6172 | is |
6173 | begin | |
6174 | if Nkind (Cond) = N_Or_Else then | |
6175 | Set_Paren_Count (Cond, 1); | |
6176 | end if; | |
6177 | ||
6178 | if Nkind (Ck_Node) = N_Allocator then | |
6179 | return Cond; | |
6180 | else | |
6181 | return | |
6182 | Make_And_Then (Loc, | |
6183 | Left_Opnd => | |
6184 | Make_Op_Ne (Loc, | |
9dfe12ae | 6185 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 6186 | Right_Opnd => Make_Null (Loc)), |
6187 | Right_Opnd => Cond); | |
6188 | end if; | |
6189 | end Guard_Access; | |
6190 | ||
6191 | ----------------------------- | |
6192 | -- Index_Checks_Suppressed -- | |
6193 | ----------------------------- | |
6194 | ||
6195 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6196 | begin | |
9dfe12ae | 6197 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6198 | return Is_Check_Suppressed (E, Index_Check); | |
6199 | else | |
fafc6b97 | 6200 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 6201 | end if; |
ee6ba406 | 6202 | end Index_Checks_Suppressed; |
6203 | ||
6204 | ---------------- | |
6205 | -- Initialize -- | |
6206 | ---------------- | |
6207 | ||
6208 | procedure Initialize is | |
6209 | begin | |
6210 | for J in Determine_Range_Cache_N'Range loop | |
6211 | Determine_Range_Cache_N (J) := Empty; | |
6212 | end loop; | |
2af58f67 | 6213 | |
6214 | Check_Names.Init; | |
6215 | ||
6216 | for J in Int range 1 .. All_Checks loop | |
6217 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
6218 | end loop; | |
ee6ba406 | 6219 | end Initialize; |
6220 | ||
6221 | ------------------------- | |
6222 | -- Insert_Range_Checks -- | |
6223 | ------------------------- | |
6224 | ||
6225 | procedure Insert_Range_Checks | |
6226 | (Checks : Check_Result; | |
6227 | Node : Node_Id; | |
6228 | Suppress_Typ : Entity_Id; | |
6229 | Static_Sloc : Source_Ptr := No_Location; | |
6230 | Flag_Node : Node_Id := Empty; | |
6231 | Do_Before : Boolean := False) | |
6232 | is | |
6233 | Internal_Flag_Node : Node_Id := Flag_Node; | |
6234 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
6235 | ||
6236 | Check_Node : Node_Id; | |
6237 | Checks_On : constant Boolean := | |
b6341c67 | 6238 | (not Index_Checks_Suppressed (Suppress_Typ)) |
6239 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 6240 | |
6241 | begin | |
feff2f05 | 6242 | -- For now we just return if Checks_On is false, however this should be |
6243 | -- enhanced to check for an always True value in the condition and to | |
6244 | -- generate a compilation warning??? | |
ee6ba406 | 6245 | |
6dbcfcd9 | 6246 | if not Full_Expander_Active or else not Checks_On then |
ee6ba406 | 6247 | return; |
6248 | end if; | |
6249 | ||
6250 | if Static_Sloc = No_Location then | |
6251 | Internal_Static_Sloc := Sloc (Node); | |
6252 | end if; | |
6253 | ||
6254 | if No (Flag_Node) then | |
6255 | Internal_Flag_Node := Node; | |
6256 | end if; | |
6257 | ||
6258 | for J in 1 .. 2 loop | |
6259 | exit when No (Checks (J)); | |
6260 | ||
6261 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
6262 | and then Present (Condition (Checks (J))) | |
6263 | then | |
6264 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
6265 | Check_Node := Checks (J); | |
6266 | Mark_Rewrite_Insertion (Check_Node); | |
6267 | ||
6268 | if Do_Before then | |
6269 | Insert_Before_And_Analyze (Node, Check_Node); | |
6270 | else | |
6271 | Insert_After_And_Analyze (Node, Check_Node); | |
6272 | end if; | |
6273 | ||
6274 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
6275 | end if; | |
6276 | ||
6277 | else | |
6278 | Check_Node := | |
f15731c4 | 6279 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
6280 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 6281 | Mark_Rewrite_Insertion (Check_Node); |
6282 | ||
6283 | if Do_Before then | |
6284 | Insert_Before_And_Analyze (Node, Check_Node); | |
6285 | else | |
6286 | Insert_After_And_Analyze (Node, Check_Node); | |
6287 | end if; | |
6288 | end if; | |
6289 | end loop; | |
6290 | end Insert_Range_Checks; | |
6291 | ||
6292 | ------------------------ | |
6293 | -- Insert_Valid_Check -- | |
6294 | ------------------------ | |
6295 | ||
6296 | procedure Insert_Valid_Check (Expr : Node_Id) is | |
6297 | Loc : constant Source_Ptr := Sloc (Expr); | |
70580828 | 6298 | Typ : constant Entity_Id := Etype (Expr); |
8b718dab | 6299 | Exp : Node_Id; |
ee6ba406 | 6300 | |
6301 | begin | |
06ad5813 | 6302 | -- Do not insert if checks off, or if not checking validity or |
6303 | -- if expression is known to be valid | |
ee6ba406 | 6304 | |
0577b0b1 | 6305 | if not Validity_Checks_On |
6306 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 6307 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 6308 | then |
8b718dab | 6309 | return; |
6310 | end if; | |
ee6ba406 | 6311 | |
42c57d55 | 6312 | -- Do not insert checks within a predicate function. This will arise |
6313 | -- if the current unit and the predicate function are being compiled | |
6314 | -- with validity checks enabled. | |
70580828 | 6315 | |
6316 | if Present (Predicate_Function (Typ)) | |
6317 | and then Current_Scope = Predicate_Function (Typ) | |
6318 | then | |
6319 | return; | |
6320 | end if; | |
6321 | ||
8b718dab | 6322 | -- If we have a checked conversion, then validity check applies to |
6323 | -- the expression inside the conversion, not the result, since if | |
6324 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 6325 | |
8b718dab | 6326 | Exp := Expr; |
6327 | while Nkind (Exp) = N_Type_Conversion loop | |
6328 | Exp := Expression (Exp); | |
6329 | end loop; | |
6330 | ||
0577b0b1 | 6331 | -- We are about to insert the validity check for Exp. We save and |
6332 | -- reset the Do_Range_Check flag over this validity check, and then | |
6333 | -- put it back for the final original reference (Exp may be rewritten). | |
6334 | ||
6335 | declare | |
6336 | DRC : constant Boolean := Do_Range_Check (Exp); | |
23abd64d | 6337 | PV : Node_Id; |
6338 | CE : Node_Id; | |
05fcfafb | 6339 | |
0577b0b1 | 6340 | begin |
6341 | Set_Do_Range_Check (Exp, False); | |
6342 | ||
06ad5813 | 6343 | -- Force evaluation to avoid multiple reads for atomic/volatile |
6344 | ||
6345 | if Is_Entity_Name (Exp) | |
6346 | and then Is_Volatile (Entity (Exp)) | |
6347 | then | |
6348 | Force_Evaluation (Exp, Name_Req => True); | |
6349 | end if; | |
6350 | ||
23abd64d | 6351 | -- Build the prefix for the 'Valid call |
6352 | ||
6353 | PV := Duplicate_Subexpr_No_Checks (Exp, Name_Req => True); | |
0577b0b1 | 6354 | |
23abd64d | 6355 | -- A rather specialized kludge. If PV is an analyzed expression |
6356 | -- which is an indexed component of a packed array that has not | |
6357 | -- been properly expanded, turn off its Analyzed flag to make sure | |
6358 | -- it gets properly reexpanded. | |
6359 | ||
6360 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
6361 | -- an analyze with the old parent pointer. This may point e.g. to | |
6362 | -- a subprogram call, which deactivates this expansion. | |
6363 | ||
6364 | if Analyzed (PV) | |
6365 | and then Nkind (PV) = N_Indexed_Component | |
6366 | and then Present (Packed_Array_Type (Etype (Prefix (PV)))) | |
6367 | then | |
6368 | Set_Analyzed (PV, False); | |
6369 | end if; | |
6370 | ||
6371 | -- Build the raise CE node to check for validity | |
6372 | ||
6373 | CE := | |
0577b0b1 | 6374 | Make_Raise_Constraint_Error (Loc, |
6375 | Condition => | |
6376 | Make_Op_Not (Loc, | |
6377 | Right_Opnd => | |
6378 | Make_Attribute_Reference (Loc, | |
23abd64d | 6379 | Prefix => PV, |
0577b0b1 | 6380 | Attribute_Name => Name_Valid)), |
23abd64d | 6381 | Reason => CE_Invalid_Data); |
6382 | ||
6383 | -- Insert the validity check. Note that we do this with validity | |
6384 | -- checks turned off, to avoid recursion, we do not want validity | |
6385 | -- checks on the validity checking code itself! | |
6386 | ||
6387 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 6388 | |
6fb3c314 | 6389 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 6390 | -- array, then it is rewritten as a renaming declaration. If the |
6391 | -- expression is an actual in a call, it has not been expanded, | |
6392 | -- waiting for the proper point at which to do it. The same happens | |
6393 | -- with renamings, so that we have to force the expansion now. This | |
6394 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
6395 | -- and exp_ch6.adb. | |
6396 | ||
6397 | if Is_Entity_Name (Exp) | |
6398 | and then Nkind (Parent (Entity (Exp))) = | |
6399 | N_Object_Renaming_Declaration | |
6400 | then | |
6401 | declare | |
6402 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
6403 | begin | |
6404 | if Nkind (Old_Exp) = N_Indexed_Component | |
6405 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
6406 | then | |
6407 | Expand_Packed_Element_Reference (Old_Exp); | |
6408 | end if; | |
6409 | end; | |
6410 | end if; | |
6411 | ||
6412 | -- Put back the Do_Range_Check flag on the resulting (possibly | |
6413 | -- rewritten) expression. | |
6414 | ||
6415 | -- Note: it might be thought that a validity check is not required | |
6416 | -- when a range check is present, but that's not the case, because | |
6417 | -- the back end is allowed to assume for the range check that the | |
6418 | -- operand is within its declared range (an assumption that validity | |
6419 | -- checking is all about NOT assuming!) | |
6420 | ||
00c403ee | 6421 | -- Note: no need to worry about Possible_Local_Raise here, it will |
6422 | -- already have been called if original node has Do_Range_Check set. | |
6423 | ||
0577b0b1 | 6424 | Set_Do_Range_Check (Exp, DRC); |
6425 | end; | |
ee6ba406 | 6426 | end Insert_Valid_Check; |
6427 | ||
3cce7f32 | 6428 | ------------------------------------- |
6429 | -- Is_Signed_Integer_Arithmetic_Op -- | |
6430 | ------------------------------------- | |
6431 | ||
6432 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
6433 | begin | |
6434 | case Nkind (N) is | |
6435 | when N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon | | |
6436 | N_Op_Minus | N_Op_Mod | N_Op_Multiply | N_Op_Plus | | |
6437 | N_Op_Rem | N_Op_Subtract => | |
6438 | return Is_Signed_Integer_Type (Etype (N)); | |
6439 | ||
92f1631f | 6440 | when N_If_Expression | N_Case_Expression => |
0326b4d4 | 6441 | return Is_Signed_Integer_Type (Etype (N)); |
6442 | ||
3cce7f32 | 6443 | when others => |
6444 | return False; | |
6445 | end case; | |
6446 | end Is_Signed_Integer_Arithmetic_Op; | |
6447 | ||
fa7497e8 | 6448 | ---------------------------------- |
6449 | -- Install_Null_Excluding_Check -- | |
6450 | ---------------------------------- | |
6451 | ||
6452 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 6453 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 6454 | Typ : constant Entity_Id := Etype (N); |
6455 | ||
7b31b357 | 6456 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
6457 | -- Determines if it is safe to capture Known_Non_Null status for an | |
6458 | -- the entity referenced by node N. The caller ensures that N is indeed | |
6459 | -- an entity name. It is safe to capture the non-null status for an IN | |
6460 | -- parameter when the reference occurs within a declaration that is sure | |
6461 | -- to be executed as part of the declarative region. | |
7870823d | 6462 | |
84d0d4a5 | 6463 | procedure Mark_Non_Null; |
7870823d | 6464 | -- After installation of check, if the node in question is an entity |
6465 | -- name, then mark this entity as non-null if possible. | |
6466 | ||
7b31b357 | 6467 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 6468 | E : constant Entity_Id := Entity (N); |
6469 | S : constant Entity_Id := Current_Scope; | |
6470 | S_Par : Node_Id; | |
6471 | ||
6472 | begin | |
7b31b357 | 6473 | if Ekind (E) /= E_In_Parameter then |
6474 | return False; | |
6475 | end if; | |
7870823d | 6476 | |
6477 | -- Two initial context checks. We must be inside a subprogram body | |
6478 | -- with declarations and reference must not appear in nested scopes. | |
6479 | ||
7b31b357 | 6480 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 6481 | or else Scope (E) /= S |
6482 | then | |
6483 | return False; | |
6484 | end if; | |
6485 | ||
6486 | S_Par := Parent (Parent (S)); | |
6487 | ||
6488 | if Nkind (S_Par) /= N_Subprogram_Body | |
6489 | or else No (Declarations (S_Par)) | |
6490 | then | |
6491 | return False; | |
6492 | end if; | |
6493 | ||
6494 | declare | |
6495 | N_Decl : Node_Id; | |
6496 | P : Node_Id; | |
6497 | ||
6498 | begin | |
6499 | -- Retrieve the declaration node of N (if any). Note that N | |
6500 | -- may be a part of a complex initialization expression. | |
6501 | ||
6502 | P := Parent (N); | |
6503 | N_Decl := Empty; | |
6504 | while Present (P) loop | |
6505 | ||
7b31b357 | 6506 | -- If we have a short circuit form, and we are within the right |
6507 | -- hand expression, we return false, since the right hand side | |
6508 | -- is not guaranteed to be elaborated. | |
6509 | ||
6510 | if Nkind (P) in N_Short_Circuit | |
6511 | and then N = Right_Opnd (P) | |
6512 | then | |
6513 | return False; | |
6514 | end if; | |
6515 | ||
92f1631f | 6516 | -- Similarly, if we are in an if expression and not part of the |
6517 | -- condition, then we return False, since neither the THEN or | |
6518 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 6519 | |
92f1631f | 6520 | if Nkind (P) = N_If_Expression |
7b31b357 | 6521 | and then N /= First (Expressions (P)) |
6522 | then | |
6523 | return False; | |
e977c0cf | 6524 | end if; |
6525 | ||
6fb3c314 | 6526 | -- If we are in a case expression, and not part of the |
e977c0cf | 6527 | -- expression, then we return False, since a particular |
92f1631f | 6528 | -- dependent expression may not always be elaborated |
e977c0cf | 6529 | |
6530 | if Nkind (P) = N_Case_Expression | |
6531 | and then N /= Expression (P) | |
6532 | then | |
6533 | return False; | |
7b31b357 | 6534 | end if; |
6535 | ||
7870823d | 6536 | -- While traversing the parent chain, we find that N |
6537 | -- belongs to a statement, thus it may never appear in | |
6538 | -- a declarative region. | |
6539 | ||
6540 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
6541 | or else Nkind (P) = N_Procedure_Call_Statement | |
6542 | then | |
6543 | return False; | |
6544 | end if; | |
6545 | ||
7b31b357 | 6546 | -- If we are at a declaration, record it and exit |
6547 | ||
7870823d | 6548 | if Nkind (P) in N_Declaration |
6549 | and then Nkind (P) not in N_Subprogram_Specification | |
6550 | then | |
6551 | N_Decl := P; | |
6552 | exit; | |
6553 | end if; | |
6554 | ||
6555 | P := Parent (P); | |
6556 | end loop; | |
6557 | ||
6558 | if No (N_Decl) then | |
6559 | return False; | |
6560 | end if; | |
6561 | ||
6562 | return List_Containing (N_Decl) = Declarations (S_Par); | |
6563 | end; | |
7b31b357 | 6564 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 6565 | |
6566 | ------------------- | |
6567 | -- Mark_Non_Null -- | |
6568 | ------------------- | |
6569 | ||
6570 | procedure Mark_Non_Null is | |
6571 | begin | |
7870823d | 6572 | -- Only case of interest is if node N is an entity name |
6573 | ||
84d0d4a5 | 6574 | if Is_Entity_Name (N) then |
7870823d | 6575 | |
6576 | -- For sure, we want to clear an indication that this is known to | |
6577 | -- be null, since if we get past this check, it definitely is not! | |
6578 | ||
84d0d4a5 | 6579 | Set_Is_Known_Null (Entity (N), False); |
6580 | ||
7870823d | 6581 | -- We can mark the entity as known to be non-null if either it is |
6582 | -- safe to capture the value, or in the case of an IN parameter, | |
6583 | -- which is a constant, if the check we just installed is in the | |
6584 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 6585 | -- a check is decisive for the rest of the body if the expression |
6586 | -- is sure to be elaborated, since we know we have to elaborate | |
6587 | -- all declarations before executing the body. | |
6588 | ||
6589 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 6590 | |
6591 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 6592 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 6593 | then |
6594 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 6595 | end if; |
6596 | end if; | |
6597 | end Mark_Non_Null; | |
6598 | ||
6599 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 6600 | |
6601 | begin | |
84d0d4a5 | 6602 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 6603 | |
84d0d4a5 | 6604 | -- No check inside a generic (why not???) |
fa7497e8 | 6605 | |
84d0d4a5 | 6606 | if Inside_A_Generic then |
fa7497e8 | 6607 | return; |
84d0d4a5 | 6608 | end if; |
6609 | ||
6610 | -- No check needed if known to be non-null | |
6611 | ||
6612 | if Known_Non_Null (N) then | |
05fcfafb | 6613 | return; |
84d0d4a5 | 6614 | end if; |
fa7497e8 | 6615 | |
84d0d4a5 | 6616 | -- If known to be null, here is where we generate a compile time check |
6617 | ||
6618 | if Known_Null (N) then | |
d16989f1 | 6619 | |
6620 | -- Avoid generating warning message inside init procs | |
6621 | ||
6622 | if not Inside_Init_Proc then | |
6623 | Apply_Compile_Time_Constraint_Error | |
6624 | (N, | |
cb97ae5c | 6625 | "null value not allowed here??", |
d16989f1 | 6626 | CE_Access_Check_Failed); |
6627 | else | |
6628 | Insert_Action (N, | |
6629 | Make_Raise_Constraint_Error (Loc, | |
6630 | Reason => CE_Access_Check_Failed)); | |
6631 | end if; | |
6632 | ||
84d0d4a5 | 6633 | Mark_Non_Null; |
6634 | return; | |
6635 | end if; | |
6636 | ||
6637 | -- If entity is never assigned, for sure a warning is appropriate | |
6638 | ||
6639 | if Is_Entity_Name (N) then | |
6640 | Check_Unset_Reference (N); | |
fa7497e8 | 6641 | end if; |
84d0d4a5 | 6642 | |
6643 | -- No check needed if checks are suppressed on the range. Note that we | |
6644 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
6645 | -- so, since the program is erroneous, but we don't like to casually | |
6646 | -- propagate such conclusions from erroneosity). | |
6647 | ||
6648 | if Access_Checks_Suppressed (Typ) then | |
6649 | return; | |
6650 | end if; | |
6651 | ||
2af58f67 | 6652 | -- No check needed for access to concurrent record types generated by |
6653 | -- the expander. This is not just an optimization (though it does indeed | |
6654 | -- remove junk checks). It also avoids generation of junk warnings. | |
6655 | ||
6656 | if Nkind (N) in N_Has_Chars | |
6657 | and then Chars (N) = Name_uObject | |
6658 | and then Is_Concurrent_Record_Type | |
6659 | (Directly_Designated_Type (Etype (N))) | |
6660 | then | |
6661 | return; | |
6662 | end if; | |
6663 | ||
228836e8 | 6664 | -- No check needed in interface thunks since the runtime check is |
6665 | -- already performed at the caller side. | |
6666 | ||
6667 | if Is_Thunk (Current_Scope) then | |
6668 | return; | |
6669 | end if; | |
6670 | ||
472ea160 | 6671 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
6672 | -- the expander within exception handlers, since we know that the value | |
6673 | -- can never be null. | |
6674 | ||
6675 | -- Is this really the right way to do this? Normally we generate such | |
6676 | -- code in the expander with checks off, and that's how we suppress this | |
6677 | -- kind of junk check ??? | |
6678 | ||
6679 | if Nkind (N) = N_Function_Call | |
6680 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
6681 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
6682 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
6683 | then | |
6684 | return; | |
6685 | end if; | |
6686 | ||
84d0d4a5 | 6687 | -- Otherwise install access check |
6688 | ||
6689 | Insert_Action (N, | |
6690 | Make_Raise_Constraint_Error (Loc, | |
6691 | Condition => | |
6692 | Make_Op_Eq (Loc, | |
6693 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
6694 | Right_Opnd => Make_Null (Loc)), | |
6695 | Reason => CE_Access_Check_Failed)); | |
6696 | ||
6697 | Mark_Non_Null; | |
fa7497e8 | 6698 | end Install_Null_Excluding_Check; |
6699 | ||
ee6ba406 | 6700 | -------------------------- |
6701 | -- Install_Static_Check -- | |
6702 | -------------------------- | |
6703 | ||
6704 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
6705 | Stat : constant Boolean := Is_Static_Expression (R_Cno); | |
6706 | Typ : constant Entity_Id := Etype (R_Cno); | |
6707 | ||
6708 | begin | |
f15731c4 | 6709 | Rewrite (R_Cno, |
6710 | Make_Raise_Constraint_Error (Loc, | |
6711 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 6712 | Set_Analyzed (R_Cno); |
6713 | Set_Etype (R_Cno, Typ); | |
6714 | Set_Raises_Constraint_Error (R_Cno); | |
6715 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 6716 | |
6717 | -- Now deal with possible local raise handling | |
6718 | ||
6719 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 6720 | end Install_Static_Check; |
6721 | ||
3cce7f32 | 6722 | ------------------------- |
6723 | -- Is_Check_Suppressed -- | |
6724 | ------------------------- | |
6725 | ||
6726 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
6727 | Ptr : Suppress_Stack_Entry_Ptr; | |
6728 | ||
6729 | begin | |
6730 | -- First search the local entity suppress stack. We search this from the | |
6731 | -- top of the stack down so that we get the innermost entry that applies | |
6732 | -- to this case if there are nested entries. | |
6733 | ||
6734 | Ptr := Local_Suppress_Stack_Top; | |
6735 | while Ptr /= null loop | |
6736 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6737 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6738 | then | |
6739 | return Ptr.Suppress; | |
6740 | end if; | |
6741 | ||
6742 | Ptr := Ptr.Prev; | |
6743 | end loop; | |
6744 | ||
6745 | -- Now search the global entity suppress table for a matching entry. | |
6746 | -- We also search this from the top down so that if there are multiple | |
6747 | -- pragmas for the same entity, the last one applies (not clear what | |
6748 | -- or whether the RM specifies this handling, but it seems reasonable). | |
6749 | ||
6750 | Ptr := Global_Suppress_Stack_Top; | |
6751 | while Ptr /= null loop | |
6752 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6753 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6754 | then | |
6755 | return Ptr.Suppress; | |
6756 | end if; | |
6757 | ||
6758 | Ptr := Ptr.Prev; | |
6759 | end loop; | |
6760 | ||
6761 | -- If we did not find a matching entry, then use the normal scope | |
6762 | -- suppress value after all (actually this will be the global setting | |
6763 | -- since it clearly was not overridden at any point). For a predefined | |
6764 | -- check, we test the specific flag. For a user defined check, we check | |
6765 | -- the All_Checks flag. The Overflow flag requires special handling to | |
6766 | -- deal with the General vs Assertion case | |
6767 | ||
6768 | if C = Overflow_Check then | |
6769 | return Overflow_Checks_Suppressed (Empty); | |
6770 | elsif C in Predefined_Check_Id then | |
6771 | return Scope_Suppress.Suppress (C); | |
6772 | else | |
6773 | return Scope_Suppress.Suppress (All_Checks); | |
6774 | end if; | |
6775 | end Is_Check_Suppressed; | |
6776 | ||
9dfe12ae | 6777 | --------------------- |
6778 | -- Kill_All_Checks -- | |
6779 | --------------------- | |
6780 | ||
6781 | procedure Kill_All_Checks is | |
6782 | begin | |
6783 | if Debug_Flag_CC then | |
6784 | w ("Kill_All_Checks"); | |
6785 | end if; | |
6786 | ||
feff2f05 | 6787 | -- We reset the number of saved checks to zero, and also modify all |
6788 | -- stack entries for statement ranges to indicate that the number of | |
6789 | -- checks at each level is now zero. | |
9dfe12ae | 6790 | |
6791 | Num_Saved_Checks := 0; | |
6792 | ||
96da3284 | 6793 | -- Note: the Int'Min here avoids any possibility of J being out of |
6794 | -- range when called from e.g. Conditional_Statements_Begin. | |
6795 | ||
6796 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 6797 | Saved_Checks_Stack (J) := 0; |
6798 | end loop; | |
6799 | end Kill_All_Checks; | |
6800 | ||
6801 | ----------------- | |
6802 | -- Kill_Checks -- | |
6803 | ----------------- | |
6804 | ||
6805 | procedure Kill_Checks (V : Entity_Id) is | |
6806 | begin | |
6807 | if Debug_Flag_CC then | |
6808 | w ("Kill_Checks for entity", Int (V)); | |
6809 | end if; | |
6810 | ||
6811 | for J in 1 .. Num_Saved_Checks loop | |
6812 | if Saved_Checks (J).Entity = V then | |
6813 | if Debug_Flag_CC then | |
6814 | w (" Checks killed for saved check ", J); | |
6815 | end if; | |
6816 | ||
6817 | Saved_Checks (J).Killed := True; | |
6818 | end if; | |
6819 | end loop; | |
6820 | end Kill_Checks; | |
6821 | ||
ee6ba406 | 6822 | ------------------------------ |
6823 | -- Length_Checks_Suppressed -- | |
6824 | ------------------------------ | |
6825 | ||
6826 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6827 | begin | |
9dfe12ae | 6828 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6829 | return Is_Check_Suppressed (E, Length_Check); | |
6830 | else | |
fafc6b97 | 6831 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 6832 | end if; |
ee6ba406 | 6833 | end Length_Checks_Suppressed; |
6834 | ||
3cce7f32 | 6835 | ----------------------- |
6836 | -- Make_Bignum_Block -- | |
6837 | ----------------------- | |
6838 | ||
6839 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
6840 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
ee6ba406 | 6841 | |
3cce7f32 | 6842 | begin |
6843 | return | |
6844 | Make_Block_Statement (Loc, | |
6845 | Declarations => New_List ( | |
6846 | Make_Object_Declaration (Loc, | |
6847 | Defining_Identifier => M, | |
6848 | Object_Definition => | |
6849 | New_Occurrence_Of (RTE (RE_Mark_Id), Loc), | |
6850 | Expression => | |
6851 | Make_Function_Call (Loc, | |
6852 | Name => New_Reference_To (RTE (RE_SS_Mark), Loc)))), | |
6853 | ||
6854 | Handled_Statement_Sequence => | |
6855 | Make_Handled_Sequence_Of_Statements (Loc, | |
6856 | Statements => New_List ( | |
6857 | Make_Procedure_Call_Statement (Loc, | |
6858 | Name => New_Occurrence_Of (RTE (RE_SS_Release), Loc), | |
6859 | Parameter_Associations => New_List ( | |
6860 | New_Reference_To (M, Loc)))))); | |
6861 | end Make_Bignum_Block; | |
6862 | ||
0df9d43f | 6863 | ---------------------------------- |
6864 | -- Minimize_Eliminate_Overflows -- | |
6865 | ---------------------------------- | |
3cce7f32 | 6866 | |
f32c377d | 6867 | -- This is a recursive routine that is called at the top of an expression |
6868 | -- tree to properly process overflow checking for a whole subtree by making | |
6869 | -- recursive calls to process operands. This processing may involve the use | |
6870 | -- of bignum or long long integer arithmetic, which will change the types | |
6871 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 6872 | -- it would interfere with semantic analysis). |
f32c377d | 6873 | |
21a55437 | 6874 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 6875 | -- the operator expansion routines, as well as the expansion routines for |
6876 | -- if/case expression, do nothing (for the moment) except call the routine | |
6877 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
6878 | -- routine does nothing for non top-level nodes, so at the point where the | |
6879 | -- call is made for the top level node, the entire expression subtree has | |
6880 | -- not been expanded, or processed for overflow. All that has to happen as | |
6881 | -- a result of the top level call to this routine. | |
f32c377d | 6882 | |
6883 | -- As noted above, the overflow processing works by making recursive calls | |
6884 | -- for the operands, and figuring out what to do, based on the processing | |
6885 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
6886 | -- to be done in bignum mode), and the determined ranges of the operands. | |
6887 | ||
6888 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 6889 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 6890 | -- the node (if it has been modified by the overflow check processing). The |
6891 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
6892 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 6893 | -- for this call is that the overflow handling mode must be temporarily set |
6894 | -- to STRICT. | |
f32c377d | 6895 | |
0df9d43f | 6896 | procedure Minimize_Eliminate_Overflows |
61016a7a | 6897 | (N : Node_Id; |
6898 | Lo : out Uint; | |
6899 | Hi : out Uint; | |
6900 | Top_Level : Boolean) | |
3cce7f32 | 6901 | is |
0326b4d4 | 6902 | Rtyp : constant Entity_Id := Etype (N); |
6903 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
6904 | -- Result type, must be a signed integer type | |
3cce7f32 | 6905 | |
db415383 | 6906 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 6907 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
6908 | ||
6909 | Loc : constant Source_Ptr := Sloc (N); | |
6910 | ||
6911 | Rlo, Rhi : Uint; | |
0326b4d4 | 6912 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 6913 | |
6914 | Llo, Lhi : Uint; | |
0326b4d4 | 6915 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 6916 | |
49b3a812 | 6917 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
6918 | -- Operands and results are of this type when we convert | |
6919 | ||
0326b4d4 | 6920 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
6921 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 6922 | -- Bounds of Long_Long_Integer |
6923 | ||
6924 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
6925 | -- Indicates binary operator case | |
6926 | ||
6927 | OK : Boolean; | |
6928 | -- Used in call to Determine_Range | |
6929 | ||
61016a7a | 6930 | Bignum_Operands : Boolean; |
6931 | -- Set True if one or more operands is already of type Bignum, meaning | |
6932 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 6933 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 6934 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 6935 | |
6936 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 6937 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 6938 | -- which means that if the result is known to be in the result type |
6939 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 6940 | |
6941 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
6942 | -- This is called when we have modified the node and we therefore need | |
6943 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
6944 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
6945 | -- we would reenter this routine recursively which would not be good! | |
6946 | -- The argument Suppress is set True if we also want to suppress | |
6947 | -- overflow checking for the reexpansion (this is set when we know | |
6948 | -- overflow is not possible). Typ is the type for the reanalysis. | |
6949 | ||
6950 | procedure Reexpand (Suppress : Boolean := False); | |
6951 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
6952 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
6953 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
6954 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
6955 | -- Note that skipping reanalysis is not just an optimization, testing | |
6956 | -- has showed up several complex cases in which reanalyzing an already | |
6957 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 6958 | |
0326b4d4 | 6959 | function In_Result_Range return Boolean; |
6960 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 6961 | |
2fe22c69 | 6962 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 6963 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 6964 | |
6965 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 6966 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 6967 | |
0326b4d4 | 6968 | --------------------- |
6969 | -- In_Result_Range -- | |
6970 | --------------------- | |
6971 | ||
6972 | function In_Result_Range return Boolean is | |
6973 | begin | |
f32c377d | 6974 | if Lo = No_Uint or else Hi = No_Uint then |
6975 | return False; | |
6976 | ||
6977 | elsif Is_Static_Subtype (Etype (N)) then | |
0326b4d4 | 6978 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
6979 | and then | |
6980 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 6981 | |
0326b4d4 | 6982 | else |
6983 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
6984 | and then | |
6985 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
6986 | end if; | |
6987 | end In_Result_Range; | |
6988 | ||
2fe22c69 | 6989 | --------- |
6990 | -- Max -- | |
6991 | --------- | |
6992 | ||
6993 | procedure Max (A : in out Uint; B : Uint) is | |
6994 | begin | |
6995 | if A = No_Uint or else B > A then | |
6996 | A := B; | |
6997 | end if; | |
6998 | end Max; | |
6999 | ||
7000 | --------- | |
7001 | -- Min -- | |
7002 | --------- | |
7003 | ||
7004 | procedure Min (A : in out Uint; B : Uint) is | |
7005 | begin | |
7006 | if A = No_Uint or else B < A then | |
7007 | A := B; | |
7008 | end if; | |
7009 | end Min; | |
7010 | ||
0df9d43f | 7011 | --------------- |
7012 | -- Reanalyze -- | |
7013 | --------------- | |
7014 | ||
7015 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 7016 | Svg : constant Overflow_Mode_Type := |
7017 | Scope_Suppress.Overflow_Mode_General; | |
7018 | Sva : constant Overflow_Mode_Type := | |
7019 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7020 | Svo : constant Boolean := |
7021 | Scope_Suppress.Suppress (Overflow_Check); | |
7022 | ||
7023 | begin | |
db415383 | 7024 | Scope_Suppress.Overflow_Mode_General := Strict; |
7025 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 7026 | |
7027 | if Suppress then | |
7028 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7029 | end if; | |
7030 | ||
7031 | Analyze_And_Resolve (N, Typ); | |
7032 | ||
7033 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7034 | Scope_Suppress.Overflow_Mode_General := Svg; |
7035 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 7036 | end Reanalyze; |
7037 | ||
4fb5f0a0 | 7038 | -------------- |
7039 | -- Reexpand -- | |
7040 | -------------- | |
7041 | ||
0df9d43f | 7042 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 7043 | Svg : constant Overflow_Mode_Type := |
7044 | Scope_Suppress.Overflow_Mode_General; | |
7045 | Sva : constant Overflow_Mode_Type := | |
7046 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7047 | Svo : constant Boolean := |
7048 | Scope_Suppress.Suppress (Overflow_Check); | |
7049 | ||
4fb5f0a0 | 7050 | begin |
db415383 | 7051 | Scope_Suppress.Overflow_Mode_General := Strict; |
7052 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 7053 | Set_Analyzed (N, False); |
0df9d43f | 7054 | |
7055 | if Suppress then | |
7056 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7057 | end if; | |
7058 | ||
4fb5f0a0 | 7059 | Expand (N); |
0df9d43f | 7060 | |
7061 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7062 | Scope_Suppress.Overflow_Mode_General := Svg; |
7063 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 7064 | end Reexpand; |
7065 | ||
0df9d43f | 7066 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 7067 | |
3cce7f32 | 7068 | begin |
0326b4d4 | 7069 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 7070 | |
7071 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
7072 | ||
7073 | -- Use the normal Determine_Range routine to get the range. We | |
7074 | -- don't require operands to be valid, invalid values may result in | |
7075 | -- rubbish results where the result has not been properly checked for | |
7076 | -- overflow, that's fine! | |
7077 | ||
7078 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
7079 | ||
21a55437 | 7080 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 7081 | -- clear but might as well protect), use type bounds. |
7082 | ||
7083 | if not OK then | |
7084 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
7085 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
7086 | end if; | |
7087 | ||
7088 | -- If we don't have a binary operator, all we have to do is to set | |
7089 | -- the Hi/Lo range, so we are done | |
7090 | ||
7091 | return; | |
7092 | ||
0326b4d4 | 7093 | -- Processing for if expression |
7094 | ||
92f1631f | 7095 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 7096 | declare |
7097 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
7098 | Else_DE : constant Node_Id := Next (Then_DE); | |
7099 | ||
7100 | begin | |
7101 | Bignum_Operands := False; | |
7102 | ||
0df9d43f | 7103 | Minimize_Eliminate_Overflows |
0326b4d4 | 7104 | (Then_DE, Lo, Hi, Top_Level => False); |
7105 | ||
7106 | if Lo = No_Uint then | |
7107 | Bignum_Operands := True; | |
7108 | end if; | |
7109 | ||
0df9d43f | 7110 | Minimize_Eliminate_Overflows |
0326b4d4 | 7111 | (Else_DE, Rlo, Rhi, Top_Level => False); |
7112 | ||
7113 | if Rlo = No_Uint then | |
7114 | Bignum_Operands := True; | |
7115 | else | |
7116 | Long_Long_Integer_Operands := | |
7117 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
7118 | ||
7119 | Min (Lo, Rlo); | |
7120 | Max (Hi, Rhi); | |
7121 | end if; | |
7122 | ||
21a55437 | 7123 | -- If at least one of our operands is now Bignum, we must rebuild |
7124 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 7125 | -- rebuilt if expression with overflow checks off, since once we |
7126 | -- are in bignum mode, we are all done with overflow checks! | |
7127 | ||
7128 | if Bignum_Operands then | |
7129 | Rewrite (N, | |
92f1631f | 7130 | Make_If_Expression (Loc, |
0326b4d4 | 7131 | Expressions => New_List ( |
7132 | Remove_Head (Expressions (N)), | |
7133 | Convert_To_Bignum (Then_DE), | |
7134 | Convert_To_Bignum (Else_DE)), | |
7135 | Is_Elsif => Is_Elsif (N))); | |
7136 | ||
0df9d43f | 7137 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 7138 | |
7139 | -- If we have no Long_Long_Integer operands, then we are in result | |
7140 | -- range, since it means that none of our operands felt the need | |
7141 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 7142 | -- converted to long long integer or bignum). We reexpand to |
7143 | -- complete the expansion of the if expression (but we do not | |
7144 | -- need to reanalyze). | |
0326b4d4 | 7145 | |
7146 | elsif not Long_Long_Integer_Operands then | |
7147 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7148 | Reexpand; |
0326b4d4 | 7149 | |
7150 | -- Otherwise convert us to long long integer mode. Note that we | |
7151 | -- don't need any further overflow checking at this level. | |
7152 | ||
7153 | else | |
7154 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
7155 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
7156 | Set_Etype (N, LLIB); | |
f32c377d | 7157 | |
7158 | -- Now reanalyze with overflow checks off | |
7159 | ||
0326b4d4 | 7160 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7161 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 7162 | end if; |
7163 | end; | |
7164 | ||
7165 | return; | |
7166 | ||
7167 | -- Here for case expression | |
7168 | ||
7169 | elsif Nkind (N) = N_Case_Expression then | |
7170 | Bignum_Operands := False; | |
7171 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 7172 | |
7173 | declare | |
f32c377d | 7174 | Alt : Node_Id; |
0326b4d4 | 7175 | |
7176 | begin | |
7177 | -- Loop through expressions applying recursive call | |
7178 | ||
7179 | Alt := First (Alternatives (N)); | |
7180 | while Present (Alt) loop | |
7181 | declare | |
7182 | Aexp : constant Node_Id := Expression (Alt); | |
7183 | ||
7184 | begin | |
0df9d43f | 7185 | Minimize_Eliminate_Overflows |
0326b4d4 | 7186 | (Aexp, Lo, Hi, Top_Level => False); |
7187 | ||
7188 | if Lo = No_Uint then | |
7189 | Bignum_Operands := True; | |
7190 | elsif Etype (Aexp) = LLIB then | |
7191 | Long_Long_Integer_Operands := True; | |
7192 | end if; | |
7193 | end; | |
7194 | ||
7195 | Next (Alt); | |
7196 | end loop; | |
7197 | ||
7198 | -- If we have no bignum or long long integer operands, it means | |
7199 | -- that none of our dependent expressions could raise overflow. | |
7200 | -- In this case, we simply return with no changes except for | |
7201 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 7202 | -- checks for this node. We will reexpand to get the needed |
7203 | -- expansion for the case expression, but we do not need to | |
21a55437 | 7204 | -- reanalyze, since nothing has changed. |
0326b4d4 | 7205 | |
f32c377d | 7206 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 7207 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7208 | Reexpand (Suppress => True); |
0326b4d4 | 7209 | |
7210 | -- Otherwise we are going to rebuild the case expression using | |
7211 | -- either bignum or long long integer operands throughout. | |
7212 | ||
7213 | else | |
f32c377d | 7214 | declare |
7215 | Rtype : Entity_Id; | |
7216 | New_Alts : List_Id; | |
7217 | New_Exp : Node_Id; | |
7218 | ||
7219 | begin | |
7220 | New_Alts := New_List; | |
7221 | Alt := First (Alternatives (N)); | |
7222 | while Present (Alt) loop | |
7223 | if Bignum_Operands then | |
7224 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
7225 | Rtype := RTE (RE_Bignum); | |
7226 | else | |
7227 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
7228 | Rtype := LLIB; | |
7229 | end if; | |
0326b4d4 | 7230 | |
f32c377d | 7231 | Append_To (New_Alts, |
7232 | Make_Case_Expression_Alternative (Sloc (Alt), | |
7233 | Actions => No_List, | |
7234 | Discrete_Choices => Discrete_Choices (Alt), | |
7235 | Expression => New_Exp)); | |
0326b4d4 | 7236 | |
f32c377d | 7237 | Next (Alt); |
7238 | end loop; | |
0326b4d4 | 7239 | |
f32c377d | 7240 | Rewrite (N, |
7241 | Make_Case_Expression (Loc, | |
7242 | Expression => Expression (N), | |
7243 | Alternatives => New_Alts)); | |
0326b4d4 | 7244 | |
0df9d43f | 7245 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 7246 | end; |
0326b4d4 | 7247 | end if; |
7248 | end; | |
7249 | ||
7250 | return; | |
7251 | end if; | |
7252 | ||
7253 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 7254 | -- operands to get the ranges (and to properly process the subtree |
7255 | -- that lies below us!) | |
7256 | ||
0df9d43f | 7257 | Minimize_Eliminate_Overflows |
0326b4d4 | 7258 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 7259 | |
0326b4d4 | 7260 | if Binary then |
0df9d43f | 7261 | Minimize_Eliminate_Overflows |
0326b4d4 | 7262 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 7263 | end if; |
7264 | ||
f32c377d | 7265 | -- Record if we have Long_Long_Integer operands |
7266 | ||
7267 | Long_Long_Integer_Operands := | |
7268 | Etype (Right_Opnd (N)) = LLIB | |
7269 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
7270 | ||
7271 | -- If either operand is a bignum, then result will be a bignum and we | |
7272 | -- don't need to do any range analysis. As previously discussed we could | |
7273 | -- do range analysis in such cases, but it could mean working with giant | |
7274 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 7275 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 7276 | |
7277 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
7278 | Lo := No_Uint; | |
7279 | Hi := No_Uint; | |
61016a7a | 7280 | Bignum_Operands := True; |
3cce7f32 | 7281 | |
7282 | -- Otherwise compute result range | |
7283 | ||
7284 | else | |
61016a7a | 7285 | Bignum_Operands := False; |
7286 | ||
3cce7f32 | 7287 | case Nkind (N) is |
7288 | ||
7289 | -- Absolute value | |
7290 | ||
7291 | when N_Op_Abs => | |
7292 | Lo := Uint_0; | |
de922300 | 7293 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 7294 | |
7295 | -- Addition | |
7296 | ||
7297 | when N_Op_Add => | |
7298 | Lo := Llo + Rlo; | |
7299 | Hi := Lhi + Rhi; | |
7300 | ||
7301 | -- Division | |
7302 | ||
7303 | when N_Op_Divide => | |
2fe22c69 | 7304 | |
5f4275e1 | 7305 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 7306 | |
5f4275e1 | 7307 | if Rlo = 0 and then Rhi = 0 then |
7308 | Lo := Uint_0; | |
7309 | Hi := Uint_0; | |
2fe22c69 | 7310 | |
5f4275e1 | 7311 | -- Possible bounds of division must come from dividing end |
7312 | -- values of the input ranges (four possibilities), provided | |
7313 | -- zero is not included in the possible values of the right | |
7314 | -- operand. | |
7315 | ||
7316 | -- Otherwise, we just consider two intervals of values for | |
7317 | -- the right operand: the interval of negative values (up to | |
7318 | -- -1) and the interval of positive values (starting at 1). | |
7319 | -- Since division by 1 is the identity, and division by -1 | |
7320 | -- is negation, we get all possible bounds of division in that | |
7321 | -- case by considering: | |
7322 | -- - all values from the division of end values of input | |
7323 | -- ranges; | |
7324 | -- - the end values of the left operand; | |
7325 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 7326 | |
5f4275e1 | 7327 | else |
7328 | declare | |
7329 | Mrk : constant Uintp.Save_Mark := Mark; | |
7330 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 7331 | |
5f4275e1 | 7332 | Ev1 : Uint; |
7333 | Ev2 : Uint; | |
7334 | Ev3 : Uint; | |
7335 | Ev4 : Uint; | |
2fe22c69 | 7336 | |
5f4275e1 | 7337 | begin |
7338 | -- Discard extreme values of zero for the divisor, since | |
7339 | -- they will simply result in an exception in any case. | |
2fe22c69 | 7340 | |
5f4275e1 | 7341 | if Rlo = 0 then |
7342 | Rlo := Uint_1; | |
7343 | elsif Rhi = 0 then | |
7344 | Rhi := -Uint_1; | |
2fe22c69 | 7345 | end if; |
2fe22c69 | 7346 | |
5f4275e1 | 7347 | -- Compute possible bounds coming from dividing end |
7348 | -- values of the input ranges. | |
2fe22c69 | 7349 | |
5f4275e1 | 7350 | Ev1 := Llo / Rlo; |
7351 | Ev2 := Llo / Rhi; | |
7352 | Ev3 := Lhi / Rlo; | |
7353 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 7354 | |
5f4275e1 | 7355 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
7356 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 7357 | |
5f4275e1 | 7358 | -- If the right operand can be both negative or positive, |
7359 | -- include the end values of the left operand in the | |
7360 | -- extreme values, as well as their negation. | |
2fe22c69 | 7361 | |
5f4275e1 | 7362 | if Rlo < 0 and then Rhi > 0 then |
7363 | Ev1 := Llo; | |
7364 | Ev2 := -Llo; | |
7365 | Ev3 := Lhi; | |
7366 | Ev4 := -Lhi; | |
2fe22c69 | 7367 | |
5f4275e1 | 7368 | Min (Lo, |
7369 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
7370 | Max (Hi, | |
7371 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 7372 | end if; |
2fe22c69 | 7373 | |
5f4275e1 | 7374 | -- Release the RR and Ev values |
2fe22c69 | 7375 | |
5f4275e1 | 7376 | Release_And_Save (Mrk, Lo, Hi); |
7377 | end; | |
7378 | end if; | |
3cce7f32 | 7379 | |
7380 | -- Exponentiation | |
7381 | ||
7382 | when N_Op_Expon => | |
de922300 | 7383 | |
7384 | -- Discard negative values for the exponent, since they will | |
7385 | -- simply result in an exception in any case. | |
7386 | ||
7387 | if Rhi < 0 then | |
7388 | Rhi := Uint_0; | |
7389 | elsif Rlo < 0 then | |
7390 | Rlo := Uint_0; | |
7391 | end if; | |
7392 | ||
7393 | -- Estimate number of bits in result before we go computing | |
7394 | -- giant useless bounds. Basically the number of bits in the | |
7395 | -- result is the number of bits in the base multiplied by the | |
7396 | -- value of the exponent. If this is big enough that the result | |
7397 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
7398 | -- mode immediately, and avoid computing giant bounds. | |
7399 | ||
7400 | -- The comparison here is approximate, but conservative, it | |
7401 | -- only clicks on cases that are sure to exceed the bounds. | |
7402 | ||
7403 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
7404 | Lo := No_Uint; | |
7405 | Hi := No_Uint; | |
7406 | ||
7407 | -- If right operand is zero then result is 1 | |
7408 | ||
7409 | elsif Rhi = 0 then | |
7410 | Lo := Uint_1; | |
7411 | Hi := Uint_1; | |
7412 | ||
7413 | else | |
7414 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 7415 | -- positive value to largest exponent value, or from |
7416 | -- the exponentiation of most negative value to an | |
7417 | -- even exponent. | |
de922300 | 7418 | |
7419 | declare | |
7420 | Hi1, Hi2 : Uint; | |
7421 | ||
7422 | begin | |
5f4275e1 | 7423 | if Lhi > 0 then |
de922300 | 7424 | Hi1 := Lhi ** Rhi; |
7425 | else | |
7426 | Hi1 := Uint_0; | |
7427 | end if; | |
7428 | ||
7429 | if Llo < 0 then | |
7430 | if Rhi mod 2 = 0 then | |
de922300 | 7431 | Hi2 := Llo ** Rhi; |
5f4275e1 | 7432 | else |
7433 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 7434 | end if; |
7435 | else | |
7436 | Hi2 := Uint_0; | |
7437 | end if; | |
7438 | ||
7439 | Hi := UI_Max (Hi1, Hi2); | |
7440 | end; | |
7441 | ||
7442 | -- Result can only be negative if base can be negative | |
7443 | ||
7444 | if Llo < 0 then | |
21a55437 | 7445 | if Rhi mod 2 = 0 then |
de922300 | 7446 | Lo := Llo ** (Rhi - 1); |
7447 | else | |
7448 | Lo := Llo ** Rhi; | |
7449 | end if; | |
7450 | ||
21a55437 | 7451 | -- Otherwise low bound is minimum ** minimum |
de922300 | 7452 | |
7453 | else | |
7454 | Lo := Llo ** Rlo; | |
7455 | end if; | |
7456 | end if; | |
3cce7f32 | 7457 | |
7458 | -- Negation | |
7459 | ||
7460 | when N_Op_Minus => | |
7461 | Lo := -Rhi; | |
7462 | Hi := -Rlo; | |
7463 | ||
7464 | -- Mod | |
7465 | ||
7466 | when N_Op_Mod => | |
2fe22c69 | 7467 | declare |
5f4275e1 | 7468 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7469 | -- This is the maximum absolute value of the result |
7470 | ||
7471 | begin | |
7472 | Lo := Uint_0; | |
7473 | Hi := Uint_0; | |
7474 | ||
7475 | -- The result depends only on the sign and magnitude of | |
7476 | -- the right operand, it does not depend on the sign or | |
7477 | -- magnitude of the left operand. | |
7478 | ||
7479 | if Rlo < 0 then | |
7480 | Lo := -Maxabs; | |
7481 | end if; | |
7482 | ||
7483 | if Rhi > 0 then | |
7484 | Hi := Maxabs; | |
7485 | end if; | |
7486 | end; | |
3cce7f32 | 7487 | |
7488 | -- Multiplication | |
7489 | ||
7490 | when N_Op_Multiply => | |
49b3a812 | 7491 | |
7492 | -- Possible bounds of multiplication must come from multiplying | |
7493 | -- end values of the input ranges (four possibilities). | |
7494 | ||
7495 | declare | |
7496 | Mrk : constant Uintp.Save_Mark := Mark; | |
7497 | -- Mark so we can release the Ev values | |
7498 | ||
7499 | Ev1 : constant Uint := Llo * Rlo; | |
7500 | Ev2 : constant Uint := Llo * Rhi; | |
7501 | Ev3 : constant Uint := Lhi * Rlo; | |
7502 | Ev4 : constant Uint := Lhi * Rhi; | |
7503 | ||
7504 | begin | |
7505 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
7506 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
7507 | ||
7508 | -- Release the Ev values | |
7509 | ||
7510 | Release_And_Save (Mrk, Lo, Hi); | |
7511 | end; | |
3cce7f32 | 7512 | |
7513 | -- Plus operator (affirmation) | |
7514 | ||
7515 | when N_Op_Plus => | |
7516 | Lo := Rlo; | |
7517 | Hi := Rhi; | |
7518 | ||
7519 | -- Remainder | |
7520 | ||
7521 | when N_Op_Rem => | |
2fe22c69 | 7522 | declare |
5f4275e1 | 7523 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7524 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 7525 | -- that the result range does not depend on the sign of the |
7526 | -- right operand. | |
2fe22c69 | 7527 | |
7528 | begin | |
7529 | Lo := Uint_0; | |
7530 | Hi := Uint_0; | |
7531 | ||
7532 | -- Case of left operand negative, which results in a range | |
7533 | -- of -Maxabs .. 0 for those negative values. If there are | |
7534 | -- no negative values then Lo value of result is always 0. | |
7535 | ||
7536 | if Llo < 0 then | |
7537 | Lo := -Maxabs; | |
7538 | end if; | |
7539 | ||
7540 | -- Case of left operand positive | |
7541 | ||
7542 | if Lhi > 0 then | |
7543 | Hi := Maxabs; | |
7544 | end if; | |
7545 | end; | |
3cce7f32 | 7546 | |
7547 | -- Subtract | |
7548 | ||
7549 | when N_Op_Subtract => | |
7550 | Lo := Llo - Rhi; | |
7551 | Hi := Lhi - Rlo; | |
7552 | ||
7553 | -- Nothing else should be possible | |
7554 | ||
7555 | when others => | |
7556 | raise Program_Error; | |
3cce7f32 | 7557 | end case; |
7558 | end if; | |
7559 | ||
4fb5f0a0 | 7560 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 7561 | -- operands or long long integer operands), and we know the result. |
7562 | -- If we know we are in the result range, and we do not have Bignum | |
7563 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
7564 | -- overflow checks turned off (since we know we cannot have overflow). | |
7565 | -- As always the reexpansion is required to complete expansion of the | |
7566 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
7567 | -- by suppressing the check. | |
f32c377d | 7568 | |
7569 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
7570 | and then In_Result_Range | |
7571 | then | |
7572 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7573 | Reexpand (Suppress => True); |
f32c377d | 7574 | return; |
7575 | ||
7576 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 7577 | -- case we will move into either the Bignum or Long_Long_Integer domain |
7578 | -- to compute the result. However, there is one exception. If we are | |
7579 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
7580 | -- operands, we will have to immediately convert the result back to | |
7581 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
7582 | -- fiddling. | |
f32c377d | 7583 | |
7584 | elsif Top_Level | |
7585 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 7586 | |
7587 | -- One further refinement. If we are at the top level, but our parent | |
7588 | -- is a type conversion, then go into bignum or long long integer node | |
7589 | -- since the result will be converted to that type directly without | |
7590 | -- going through the result type, and we may avoid an overflow. This | |
7591 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
7592 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
7593 | -- but does not fit in Integer. | |
7594 | ||
7595 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 7596 | then |
0df9d43f | 7597 | -- Here keep original types, but we need to complete analysis |
f32c377d | 7598 | |
7599 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 7600 | -- here because it will cause recursion into the whole MINIMIZED/ |
7601 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 7602 | -- we are at the top level, and we need a check against the result |
0df9d43f | 7603 | -- mode (i.e. we want to use STRICT mode). So do exactly that! |
4fb5f0a0 | 7604 | -- Also, we have not modified the node, so this is a case where |
7605 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 7606 | |
0df9d43f | 7607 | Reexpand; |
f32c377d | 7608 | return; |
7609 | ||
7610 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 7611 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 7612 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
7613 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 7614 | |
7615 | -- Note: we could do better here and in some cases switch back from | |
7616 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
7617 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
7618 | -- Failing to do this switching back is only an efficiency issue. | |
7619 | ||
f32c377d | 7620 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 7621 | |
61016a7a | 7622 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 7623 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 7624 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
7625 | ||
7626 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
7627 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
7628 | -- into Bignum mode, but there is an exception if neither of our | |
7629 | -- operands is Bignum now, and we are at the top level (Top_Level | |
7630 | -- set True). In this case, there is no point in moving into Bignum | |
7631 | -- mode to prevent overflow if the caller will immediately convert | |
7632 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 7633 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 7634 | |
7635 | if Check_Mode = Minimized | |
7636 | or else (Top_Level and not Bignum_Operands) | |
7637 | then | |
0df9d43f | 7638 | if Do_Overflow_Check (N) then |
7639 | Enable_Overflow_Check (N); | |
7640 | end if; | |
3cce7f32 | 7641 | |
0df9d43f | 7642 | -- The result now has to be in Long_Long_Integer mode, so adjust |
7643 | -- the possible range to reflect this. Note these calls also | |
7644 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 7645 | |
7646 | Max (Lo, LLLo); | |
7647 | Min (Hi, LLHi); | |
7648 | ||
7649 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 7650 | |
7651 | else | |
7652 | pragma Assert (Check_Mode = Eliminated); | |
7653 | ||
7654 | declare | |
7655 | Fent : Entity_Id; | |
7656 | Args : List_Id; | |
7657 | ||
7658 | begin | |
7659 | case Nkind (N) is | |
7660 | when N_Op_Abs => | |
7661 | Fent := RTE (RE_Big_Abs); | |
7662 | ||
7663 | when N_Op_Add => | |
7664 | Fent := RTE (RE_Big_Add); | |
7665 | ||
7666 | when N_Op_Divide => | |
7667 | Fent := RTE (RE_Big_Div); | |
7668 | ||
7669 | when N_Op_Expon => | |
7670 | Fent := RTE (RE_Big_Exp); | |
7671 | ||
7672 | when N_Op_Minus => | |
7673 | Fent := RTE (RE_Big_Neg); | |
7674 | ||
7675 | when N_Op_Mod => | |
7676 | Fent := RTE (RE_Big_Mod); | |
7677 | ||
7678 | when N_Op_Multiply => | |
7679 | Fent := RTE (RE_Big_Mul); | |
7680 | ||
7681 | when N_Op_Rem => | |
7682 | Fent := RTE (RE_Big_Rem); | |
7683 | ||
7684 | when N_Op_Subtract => | |
7685 | Fent := RTE (RE_Big_Sub); | |
7686 | ||
7687 | -- Anything else is an internal error, this includes the | |
7688 | -- N_Op_Plus case, since how can plus cause the result | |
7689 | -- to be out of range if the operand is in range? | |
7690 | ||
7691 | when others => | |
7692 | raise Program_Error; | |
7693 | end case; | |
7694 | ||
7695 | -- Construct argument list for Bignum call, converting our | |
7696 | -- operands to Bignum form if they are not already there. | |
7697 | ||
7698 | Args := New_List; | |
7699 | ||
7700 | if Binary then | |
7701 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
7702 | end if; | |
7703 | ||
7704 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
7705 | ||
7706 | -- Now rewrite the arithmetic operator with a call to the | |
7707 | -- corresponding bignum function. | |
7708 | ||
7709 | Rewrite (N, | |
7710 | Make_Function_Call (Loc, | |
7711 | Name => New_Occurrence_Of (Fent, Loc), | |
7712 | Parameter_Associations => Args)); | |
0df9d43f | 7713 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 7714 | |
7715 | -- Indicate result is Bignum mode | |
7716 | ||
7717 | Lo := No_Uint; | |
7718 | Hi := No_Uint; | |
de922300 | 7719 | return; |
3cce7f32 | 7720 | end; |
7721 | end if; | |
7722 | ||
7723 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 7724 | -- check is required, at least not yet. |
3cce7f32 | 7725 | |
7726 | else | |
de922300 | 7727 | Set_Do_Overflow_Check (N, False); |
7728 | end if; | |
3cce7f32 | 7729 | |
f32c377d | 7730 | -- Here we are not in Bignum territory, but we may have long long |
7731 | -- integer operands that need special handling. First a special check: | |
7732 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
7733 | -- it means we converted it to prevent overflow, but exponentiation | |
7734 | -- requires a Natural right operand, so convert it back to Natural. | |
7735 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 7736 | |
f32c377d | 7737 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
7738 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 7739 | end if; |
7740 | ||
de922300 | 7741 | -- Here we will do the operation in Long_Long_Integer. We do this even |
7742 | -- if we know an overflow check is required, better to do this in long | |
7743 | -- long integer mode, since we are less likely to overflow! | |
3cce7f32 | 7744 | |
de922300 | 7745 | -- Convert right or only operand to Long_Long_Integer, except that |
7746 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 7747 | |
de922300 | 7748 | if Nkind (N) /= N_Op_Expon then |
7749 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
7750 | end if; | |
3cce7f32 | 7751 | |
de922300 | 7752 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 7753 | |
de922300 | 7754 | if Binary then |
7755 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
7756 | end if; | |
7757 | ||
7758 | -- Reset node to unanalyzed | |
7759 | ||
7760 | Set_Analyzed (N, False); | |
7761 | Set_Etype (N, Empty); | |
7762 | Set_Entity (N, Empty); | |
7763 | ||
2fe22c69 | 7764 | -- Now analyze this new node. This reanalysis will complete processing |
7765 | -- for the node. In particular we will complete the expansion of an | |
7766 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
7767 | -- we will complete any division checks (since we have not changed the | |
7768 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 7769 | |
0df9d43f | 7770 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
7771 | -- MINIMIZED/ELIMINATED handling, since we are now done with that! | |
3cce7f32 | 7772 | |
0df9d43f | 7773 | declare |
db415383 | 7774 | SG : constant Overflow_Mode_Type := |
7775 | Scope_Suppress.Overflow_Mode_General; | |
7776 | SA : constant Overflow_Mode_Type := | |
7777 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 7778 | |
0df9d43f | 7779 | begin |
db415383 | 7780 | Scope_Suppress.Overflow_Mode_General := Strict; |
7781 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 7782 | |
0df9d43f | 7783 | if not Do_Overflow_Check (N) then |
7784 | Reanalyze (LLIB, Suppress => True); | |
7785 | else | |
7786 | Reanalyze (LLIB); | |
7787 | end if; | |
7788 | ||
db415383 | 7789 | Scope_Suppress.Overflow_Mode_General := SG; |
7790 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 7791 | end; |
7792 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 7793 | |
7794 | ------------------------- | |
7795 | -- Overflow_Check_Mode -- | |
7796 | ------------------------- | |
7797 | ||
db415383 | 7798 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 7799 | begin |
724d2bd8 | 7800 | if In_Assertion_Expr = 0 then |
db415383 | 7801 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 7802 | else |
db415383 | 7803 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 7804 | end if; |
3cce7f32 | 7805 | end Overflow_Check_Mode; |
7806 | ||
7807 | -------------------------------- | |
7808 | -- Overflow_Checks_Suppressed -- | |
7809 | -------------------------------- | |
7810 | ||
7811 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7812 | begin | |
0df9d43f | 7813 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7814 | return Is_Check_Suppressed (E, Overflow_Check); | |
7815 | else | |
7816 | return Scope_Suppress.Suppress (Overflow_Check); | |
7817 | end if; | |
ee6ba406 | 7818 | end Overflow_Checks_Suppressed; |
fc75802a | 7819 | |
37baba83 | 7820 | --------------------------------- |
7821 | -- Predicate_Checks_Suppressed -- | |
7822 | --------------------------------- | |
7823 | ||
7824 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7825 | begin | |
7826 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
7827 | return Is_Check_Suppressed (E, Predicate_Check); | |
7828 | else | |
7829 | return Scope_Suppress.Suppress (Predicate_Check); | |
7830 | end if; | |
7831 | end Predicate_Checks_Suppressed; | |
7832 | ||
ee6ba406 | 7833 | ----------------------------- |
7834 | -- Range_Checks_Suppressed -- | |
7835 | ----------------------------- | |
7836 | ||
7837 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7838 | begin | |
9dfe12ae | 7839 | if Present (E) then |
7840 | ||
7841 | -- Note: for now we always suppress range checks on Vax float types, | |
7842 | -- since Gigi does not know how to generate these checks. | |
7843 | ||
7844 | if Vax_Float (E) then | |
7845 | return True; | |
7846 | elsif Kill_Range_Checks (E) then | |
7847 | return True; | |
7848 | elsif Checks_May_Be_Suppressed (E) then | |
7849 | return Is_Check_Suppressed (E, Range_Check); | |
7850 | end if; | |
7851 | end if; | |
ee6ba406 | 7852 | |
fafc6b97 | 7853 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 7854 | end Range_Checks_Suppressed; |
7855 | ||
0577b0b1 | 7856 | ----------------------------------------- |
7857 | -- Range_Or_Validity_Checks_Suppressed -- | |
7858 | ----------------------------------------- | |
7859 | ||
7860 | -- Note: the coding would be simpler here if we simply made appropriate | |
7861 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
7862 | -- duplicated checks which we prefer to avoid. | |
7863 | ||
7864 | function Range_Or_Validity_Checks_Suppressed | |
7865 | (Expr : Node_Id) return Boolean | |
7866 | is | |
7867 | begin | |
7868 | -- Immediate return if scope checks suppressed for either check | |
7869 | ||
fafc6b97 | 7870 | if Scope_Suppress.Suppress (Range_Check) |
7871 | or | |
7872 | Scope_Suppress.Suppress (Validity_Check) | |
7873 | then | |
0577b0b1 | 7874 | return True; |
7875 | end if; | |
7876 | ||
7877 | -- If no expression, that's odd, decide that checks are suppressed, | |
7878 | -- since we don't want anyone trying to do checks in this case, which | |
7879 | -- is most likely the result of some other error. | |
7880 | ||
7881 | if No (Expr) then | |
7882 | return True; | |
7883 | end if; | |
7884 | ||
7885 | -- Expression is present, so perform suppress checks on type | |
7886 | ||
7887 | declare | |
7888 | Typ : constant Entity_Id := Etype (Expr); | |
7889 | begin | |
7890 | if Vax_Float (Typ) then | |
7891 | return True; | |
7892 | elsif Checks_May_Be_Suppressed (Typ) | |
7893 | and then (Is_Check_Suppressed (Typ, Range_Check) | |
7894 | or else | |
7895 | Is_Check_Suppressed (Typ, Validity_Check)) | |
7896 | then | |
7897 | return True; | |
7898 | end if; | |
7899 | end; | |
7900 | ||
7901 | -- If expression is an entity name, perform checks on this entity | |
7902 | ||
7903 | if Is_Entity_Name (Expr) then | |
7904 | declare | |
7905 | Ent : constant Entity_Id := Entity (Expr); | |
7906 | begin | |
7907 | if Checks_May_Be_Suppressed (Ent) then | |
7908 | return Is_Check_Suppressed (Ent, Range_Check) | |
7909 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
7910 | end if; | |
7911 | end; | |
7912 | end if; | |
7913 | ||
7914 | -- If we fall through, no checks suppressed | |
7915 | ||
7916 | return False; | |
7917 | end Range_Or_Validity_Checks_Suppressed; | |
7918 | ||
226494a3 | 7919 | ------------------- |
7920 | -- Remove_Checks -- | |
7921 | ------------------- | |
7922 | ||
7923 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 7924 | function Process (N : Node_Id) return Traverse_Result; |
7925 | -- Process a single node during the traversal | |
7926 | ||
8f6e4fd5 | 7927 | procedure Traverse is new Traverse_Proc (Process); |
7928 | -- The traversal procedure itself | |
226494a3 | 7929 | |
7930 | ------------- | |
7931 | -- Process -- | |
7932 | ------------- | |
7933 | ||
7934 | function Process (N : Node_Id) return Traverse_Result is | |
7935 | begin | |
7936 | if Nkind (N) not in N_Subexpr then | |
7937 | return Skip; | |
7938 | end if; | |
7939 | ||
7940 | Set_Do_Range_Check (N, False); | |
7941 | ||
7942 | case Nkind (N) is | |
7943 | when N_And_Then => | |
8f6e4fd5 | 7944 | Traverse (Left_Opnd (N)); |
226494a3 | 7945 | return Skip; |
7946 | ||
7947 | when N_Attribute_Reference => | |
226494a3 | 7948 | Set_Do_Overflow_Check (N, False); |
7949 | ||
226494a3 | 7950 | when N_Function_Call => |
7951 | Set_Do_Tag_Check (N, False); | |
7952 | ||
226494a3 | 7953 | when N_Op => |
7954 | Set_Do_Overflow_Check (N, False); | |
7955 | ||
7956 | case Nkind (N) is | |
7957 | when N_Op_Divide => | |
7958 | Set_Do_Division_Check (N, False); | |
7959 | ||
7960 | when N_Op_And => | |
7961 | Set_Do_Length_Check (N, False); | |
7962 | ||
7963 | when N_Op_Mod => | |
7964 | Set_Do_Division_Check (N, False); | |
7965 | ||
7966 | when N_Op_Or => | |
7967 | Set_Do_Length_Check (N, False); | |
7968 | ||
7969 | when N_Op_Rem => | |
7970 | Set_Do_Division_Check (N, False); | |
7971 | ||
7972 | when N_Op_Xor => | |
7973 | Set_Do_Length_Check (N, False); | |
7974 | ||
7975 | when others => | |
7976 | null; | |
7977 | end case; | |
7978 | ||
7979 | when N_Or_Else => | |
8f6e4fd5 | 7980 | Traverse (Left_Opnd (N)); |
226494a3 | 7981 | return Skip; |
7982 | ||
7983 | when N_Selected_Component => | |
226494a3 | 7984 | Set_Do_Discriminant_Check (N, False); |
7985 | ||
226494a3 | 7986 | when N_Type_Conversion => |
9dfe12ae | 7987 | Set_Do_Length_Check (N, False); |
7988 | Set_Do_Tag_Check (N, False); | |
226494a3 | 7989 | Set_Do_Overflow_Check (N, False); |
226494a3 | 7990 | |
7991 | when others => | |
7992 | null; | |
7993 | end case; | |
7994 | ||
7995 | return OK; | |
7996 | end Process; | |
7997 | ||
7998 | -- Start of processing for Remove_Checks | |
7999 | ||
8000 | begin | |
8f6e4fd5 | 8001 | Traverse (Expr); |
226494a3 | 8002 | end Remove_Checks; |
8003 | ||
ee6ba406 | 8004 | ---------------------------- |
8005 | -- Selected_Length_Checks -- | |
8006 | ---------------------------- | |
8007 | ||
8008 | function Selected_Length_Checks | |
8009 | (Ck_Node : Node_Id; | |
8010 | Target_Typ : Entity_Id; | |
8011 | Source_Typ : Entity_Id; | |
314a23b6 | 8012 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8013 | is |
8014 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8015 | S_Typ : Entity_Id; | |
8016 | T_Typ : Entity_Id; | |
8017 | Expr_Actual : Node_Id; | |
8018 | Exptyp : Entity_Id; | |
8019 | Cond : Node_Id := Empty; | |
8020 | Do_Access : Boolean := False; | |
8021 | Wnode : Node_Id := Warn_Node; | |
8022 | Ret_Result : Check_Result := (Empty, Empty); | |
8023 | Num_Checks : Natural := 0; | |
8024 | ||
8025 | procedure Add_Check (N : Node_Id); | |
8026 | -- Adds the action given to Ret_Result if N is non-Empty | |
8027 | ||
8028 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
8029 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 8030 | -- Comments required ??? |
ee6ba406 | 8031 | |
8032 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
8033 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 8034 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 8035 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 8036 | -- obviously superfluous checks. |
ee6ba406 | 8037 | |
8038 | function Length_E_Cond | |
8039 | (Exptyp : Entity_Id; | |
8040 | Typ : Entity_Id; | |
314a23b6 | 8041 | Indx : Nat) return Node_Id; |
ee6ba406 | 8042 | -- Returns expression to compute: |
8043 | -- Typ'Length /= Exptyp'Length | |
8044 | ||
8045 | function Length_N_Cond | |
8046 | (Expr : Node_Id; | |
8047 | Typ : Entity_Id; | |
314a23b6 | 8048 | Indx : Nat) return Node_Id; |
ee6ba406 | 8049 | -- Returns expression to compute: |
8050 | -- Typ'Length /= Expr'Length | |
8051 | ||
8052 | --------------- | |
8053 | -- Add_Check -- | |
8054 | --------------- | |
8055 | ||
8056 | procedure Add_Check (N : Node_Id) is | |
8057 | begin | |
8058 | if Present (N) then | |
8059 | ||
8060 | -- For now, ignore attempt to place more than 2 checks ??? | |
8061 | ||
8062 | if Num_Checks = 2 then | |
8063 | return; | |
8064 | end if; | |
8065 | ||
8066 | pragma Assert (Num_Checks <= 1); | |
8067 | Num_Checks := Num_Checks + 1; | |
8068 | Ret_Result (Num_Checks) := N; | |
8069 | end if; | |
8070 | end Add_Check; | |
8071 | ||
8072 | ------------------ | |
8073 | -- Get_E_Length -- | |
8074 | ------------------ | |
8075 | ||
8076 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 8077 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 8078 | N : Node_Id; |
8079 | E1 : Entity_Id := E; | |
ee6ba406 | 8080 | |
8081 | begin | |
8082 | if Ekind (Scope (E)) = E_Record_Type | |
8083 | and then Has_Discriminants (Scope (E)) | |
8084 | then | |
8085 | N := Build_Discriminal_Subtype_Of_Component (E); | |
8086 | ||
8087 | if Present (N) then | |
8088 | Insert_Action (Ck_Node, N); | |
8089 | E1 := Defining_Identifier (N); | |
8090 | end if; | |
8091 | end if; | |
8092 | ||
8093 | if Ekind (E1) = E_String_Literal_Subtype then | |
8094 | return | |
8095 | Make_Integer_Literal (Loc, | |
8096 | Intval => String_Literal_Length (E1)); | |
8097 | ||
00c403ee | 8098 | elsif SE /= Standard_Standard |
8099 | and then Ekind (Scope (SE)) = E_Protected_Type | |
8100 | and then Has_Discriminants (Scope (SE)) | |
8101 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 8102 | and then not Inside_Init_Proc |
8103 | then | |
ee6ba406 | 8104 | -- If the type whose length is needed is a private component |
8105 | -- constrained by a discriminant, we must expand the 'Length | |
8106 | -- attribute into an explicit computation, using the discriminal | |
8107 | -- of the current protected operation. This is because the actual | |
8108 | -- type of the prival is constructed after the protected opera- | |
8109 | -- tion has been fully expanded. | |
8110 | ||
8111 | declare | |
8112 | Indx_Type : Node_Id; | |
8113 | Lo : Node_Id; | |
8114 | Hi : Node_Id; | |
8115 | Do_Expand : Boolean := False; | |
8116 | ||
8117 | begin | |
8118 | Indx_Type := First_Index (E); | |
8119 | ||
8120 | for J in 1 .. Indx - 1 loop | |
8121 | Next_Index (Indx_Type); | |
8122 | end loop; | |
8123 | ||
2af58f67 | 8124 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 8125 | |
8126 | if Nkind (Lo) = N_Identifier | |
8127 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
8128 | then | |
8129 | Lo := Get_Discriminal (E, Lo); | |
8130 | Do_Expand := True; | |
8131 | end if; | |
8132 | ||
8133 | if Nkind (Hi) = N_Identifier | |
8134 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
8135 | then | |
8136 | Hi := Get_Discriminal (E, Hi); | |
8137 | Do_Expand := True; | |
8138 | end if; | |
8139 | ||
8140 | if Do_Expand then | |
8141 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 8142 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 8143 | end if; |
8144 | ||
8145 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 8146 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 8147 | end if; |
8148 | ||
8149 | N := | |
8150 | Make_Op_Add (Loc, | |
8151 | Left_Opnd => | |
8152 | Make_Op_Subtract (Loc, | |
8153 | Left_Opnd => Hi, | |
8154 | Right_Opnd => Lo), | |
8155 | ||
8156 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
8157 | return N; | |
8158 | ||
8159 | else | |
8160 | N := | |
8161 | Make_Attribute_Reference (Loc, | |
8162 | Attribute_Name => Name_Length, | |
8163 | Prefix => | |
8164 | New_Occurrence_Of (E1, Loc)); | |
8165 | ||
8166 | if Indx > 1 then | |
8167 | Set_Expressions (N, New_List ( | |
8168 | Make_Integer_Literal (Loc, Indx))); | |
8169 | end if; | |
8170 | ||
8171 | return N; | |
8172 | end if; | |
8173 | end; | |
8174 | ||
8175 | else | |
8176 | N := | |
8177 | Make_Attribute_Reference (Loc, | |
8178 | Attribute_Name => Name_Length, | |
8179 | Prefix => | |
8180 | New_Occurrence_Of (E1, Loc)); | |
8181 | ||
8182 | if Indx > 1 then | |
8183 | Set_Expressions (N, New_List ( | |
8184 | Make_Integer_Literal (Loc, Indx))); | |
8185 | end if; | |
8186 | ||
8187 | return N; | |
ee6ba406 | 8188 | end if; |
8189 | end Get_E_Length; | |
8190 | ||
8191 | ------------------ | |
8192 | -- Get_N_Length -- | |
8193 | ------------------ | |
8194 | ||
8195 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
8196 | begin | |
8197 | return | |
8198 | Make_Attribute_Reference (Loc, | |
8199 | Attribute_Name => Name_Length, | |
8200 | Prefix => | |
9dfe12ae | 8201 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8202 | Expressions => New_List ( |
8203 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8204 | end Get_N_Length; |
8205 | ||
8206 | ------------------- | |
8207 | -- Length_E_Cond -- | |
8208 | ------------------- | |
8209 | ||
8210 | function Length_E_Cond | |
8211 | (Exptyp : Entity_Id; | |
8212 | Typ : Entity_Id; | |
314a23b6 | 8213 | Indx : Nat) return Node_Id |
ee6ba406 | 8214 | is |
8215 | begin | |
8216 | return | |
8217 | Make_Op_Ne (Loc, | |
8218 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8219 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 8220 | end Length_E_Cond; |
8221 | ||
8222 | ------------------- | |
8223 | -- Length_N_Cond -- | |
8224 | ------------------- | |
8225 | ||
8226 | function Length_N_Cond | |
8227 | (Expr : Node_Id; | |
8228 | Typ : Entity_Id; | |
314a23b6 | 8229 | Indx : Nat) return Node_Id |
ee6ba406 | 8230 | is |
8231 | begin | |
8232 | return | |
8233 | Make_Op_Ne (Loc, | |
8234 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8235 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 8236 | end Length_N_Cond; |
8237 | ||
feff2f05 | 8238 | ----------------- |
8239 | -- Same_Bounds -- | |
8240 | ----------------- | |
8241 | ||
ee6ba406 | 8242 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
8243 | begin | |
8244 | return | |
8245 | (Nkind (L) = N_Integer_Literal | |
8246 | and then Nkind (R) = N_Integer_Literal | |
8247 | and then Intval (L) = Intval (R)) | |
8248 | ||
8249 | or else | |
8250 | (Is_Entity_Name (L) | |
8251 | and then Ekind (Entity (L)) = E_Constant | |
8252 | and then ((Is_Entity_Name (R) | |
8253 | and then Entity (L) = Entity (R)) | |
8254 | or else | |
8255 | (Nkind (R) = N_Type_Conversion | |
8256 | and then Is_Entity_Name (Expression (R)) | |
8257 | and then Entity (L) = Entity (Expression (R))))) | |
8258 | ||
8259 | or else | |
8260 | (Is_Entity_Name (R) | |
8261 | and then Ekind (Entity (R)) = E_Constant | |
8262 | and then Nkind (L) = N_Type_Conversion | |
8263 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 8264 | and then Entity (R) = Entity (Expression (L))) |
8265 | ||
8266 | or else | |
8267 | (Is_Entity_Name (L) | |
8268 | and then Is_Entity_Name (R) | |
8269 | and then Entity (L) = Entity (R) | |
8270 | and then Ekind (Entity (L)) = E_In_Parameter | |
8271 | and then Inside_Init_Proc); | |
ee6ba406 | 8272 | end Same_Bounds; |
8273 | ||
8274 | -- Start of processing for Selected_Length_Checks | |
8275 | ||
8276 | begin | |
6dbcfcd9 | 8277 | if not Full_Expander_Active then |
ee6ba406 | 8278 | return Ret_Result; |
8279 | end if; | |
8280 | ||
8281 | if Target_Typ = Any_Type | |
8282 | or else Target_Typ = Any_Composite | |
8283 | or else Raises_Constraint_Error (Ck_Node) | |
8284 | then | |
8285 | return Ret_Result; | |
8286 | end if; | |
8287 | ||
8288 | if No (Wnode) then | |
8289 | Wnode := Ck_Node; | |
8290 | end if; | |
8291 | ||
8292 | T_Typ := Target_Typ; | |
8293 | ||
8294 | if No (Source_Typ) then | |
8295 | S_Typ := Etype (Ck_Node); | |
8296 | else | |
8297 | S_Typ := Source_Typ; | |
8298 | end if; | |
8299 | ||
8300 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8301 | return Ret_Result; | |
8302 | end if; | |
8303 | ||
8304 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8305 | S_Typ := Designated_Type (S_Typ); | |
8306 | T_Typ := Designated_Type (T_Typ); | |
8307 | Do_Access := True; | |
8308 | ||
2af58f67 | 8309 | -- A simple optimization for the null case |
ee6ba406 | 8310 | |
2af58f67 | 8311 | if Known_Null (Ck_Node) then |
ee6ba406 | 8312 | return Ret_Result; |
8313 | end if; | |
8314 | end if; | |
8315 | ||
8316 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
8317 | if Is_Constrained (T_Typ) then | |
8318 | ||
92f1631f | 8319 | -- The checking code to be generated will freeze the corresponding |
8320 | -- array type. However, we must freeze the type now, so that the | |
8321 | -- freeze node does not appear within the generated if expression, | |
8322 | -- but ahead of it. | |
ee6ba406 | 8323 | |
8324 | Freeze_Before (Ck_Node, T_Typ); | |
8325 | ||
8326 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 8327 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 8328 | |
8329 | if Is_Access_Type (Exptyp) then | |
8330 | Exptyp := Designated_Type (Exptyp); | |
8331 | end if; | |
8332 | ||
8333 | -- String_Literal case. This needs to be handled specially be- | |
8334 | -- cause no index types are available for string literals. The | |
8335 | -- condition is simply: | |
8336 | ||
8337 | -- T_Typ'Length = string-literal-length | |
8338 | ||
9dfe12ae | 8339 | if Nkind (Expr_Actual) = N_String_Literal |
8340 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
8341 | then | |
ee6ba406 | 8342 | Cond := |
8343 | Make_Op_Ne (Loc, | |
8344 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
8345 | Right_Opnd => | |
8346 | Make_Integer_Literal (Loc, | |
8347 | Intval => | |
8348 | String_Literal_Length (Etype (Expr_Actual)))); | |
8349 | ||
8350 | -- General array case. Here we have a usable actual subtype for | |
8351 | -- the expression, and the condition is built from the two types | |
8352 | -- (Do_Length): | |
8353 | ||
8354 | -- T_Typ'Length /= Exptyp'Length or else | |
8355 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
8356 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
8357 | -- ... | |
8358 | ||
8359 | elsif Is_Constrained (Exptyp) then | |
8360 | declare | |
9dfe12ae | 8361 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
8362 | ||
8363 | L_Index : Node_Id; | |
8364 | R_Index : Node_Id; | |
8365 | L_Low : Node_Id; | |
8366 | L_High : Node_Id; | |
8367 | R_Low : Node_Id; | |
8368 | R_High : Node_Id; | |
ee6ba406 | 8369 | L_Length : Uint; |
8370 | R_Length : Uint; | |
9dfe12ae | 8371 | Ref_Node : Node_Id; |
ee6ba406 | 8372 | |
8373 | begin | |
feff2f05 | 8374 | -- At the library level, we need to ensure that the type of |
8375 | -- the object is elaborated before the check itself is | |
8376 | -- emitted. This is only done if the object is in the | |
8377 | -- current compilation unit, otherwise the type is frozen | |
8378 | -- and elaborated in its unit. | |
9dfe12ae | 8379 | |
8380 | if Is_Itype (Exptyp) | |
8381 | and then | |
8382 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
8383 | and then | |
8384 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 8385 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 8386 | then |
8387 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
8388 | Set_Itype (Ref_Node, Exptyp); | |
8389 | Insert_Action (Ck_Node, Ref_Node); | |
8390 | end if; | |
8391 | ||
ee6ba406 | 8392 | L_Index := First_Index (T_Typ); |
8393 | R_Index := First_Index (Exptyp); | |
8394 | ||
8395 | for Indx in 1 .. Ndims loop | |
8396 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 8397 | or else |
8398 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 8399 | then |
8400 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
8401 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
8402 | ||
8403 | -- Deal with compile time length check. Note that we | |
8404 | -- skip this in the access case, because the access | |
8405 | -- value may be null, so we cannot know statically. | |
8406 | ||
8407 | if not Do_Access | |
8408 | and then Compile_Time_Known_Value (L_Low) | |
8409 | and then Compile_Time_Known_Value (L_High) | |
8410 | and then Compile_Time_Known_Value (R_Low) | |
8411 | and then Compile_Time_Known_Value (R_High) | |
8412 | then | |
8413 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
8414 | L_Length := Expr_Value (L_High) - | |
8415 | Expr_Value (L_Low) + 1; | |
8416 | else | |
8417 | L_Length := UI_From_Int (0); | |
8418 | end if; | |
8419 | ||
8420 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
8421 | R_Length := Expr_Value (R_High) - | |
8422 | Expr_Value (R_Low) + 1; | |
8423 | else | |
8424 | R_Length := UI_From_Int (0); | |
8425 | end if; | |
8426 | ||
8427 | if L_Length > R_Length then | |
8428 | Add_Check | |
8429 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8430 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 8431 | |
8432 | elsif L_Length < R_Length then | |
8433 | Add_Check | |
8434 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8435 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 8436 | end if; |
8437 | ||
8438 | -- The comparison for an individual index subtype | |
8439 | -- is omitted if the corresponding index subtypes | |
8440 | -- statically match, since the result is known to | |
8441 | -- be true. Note that this test is worth while even | |
8442 | -- though we do static evaluation, because non-static | |
8443 | -- subtypes can statically match. | |
8444 | ||
8445 | elsif not | |
8446 | Subtypes_Statically_Match | |
8447 | (Etype (L_Index), Etype (R_Index)) | |
8448 | ||
8449 | and then not | |
8450 | (Same_Bounds (L_Low, R_Low) | |
8451 | and then Same_Bounds (L_High, R_High)) | |
8452 | then | |
8453 | Evolve_Or_Else | |
8454 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
8455 | end if; | |
8456 | ||
8457 | Next (L_Index); | |
8458 | Next (R_Index); | |
8459 | end if; | |
8460 | end loop; | |
8461 | end; | |
8462 | ||
8463 | -- Handle cases where we do not get a usable actual subtype that | |
8464 | -- is constrained. This happens for example in the function call | |
8465 | -- and explicit dereference cases. In these cases, we have to get | |
8466 | -- the length or range from the expression itself, making sure we | |
8467 | -- do not evaluate it more than once. | |
8468 | ||
8469 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 8470 | -- result of applying Duplicate_Expr to the original tree, forcing |
8471 | -- the result to be a name. | |
ee6ba406 | 8472 | |
8473 | else | |
8474 | declare | |
9dfe12ae | 8475 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 8476 | |
8477 | begin | |
8478 | -- Build the condition for the explicit dereference case | |
8479 | ||
8480 | for Indx in 1 .. Ndims loop | |
8481 | Evolve_Or_Else | |
8482 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
8483 | end loop; | |
8484 | end; | |
8485 | end if; | |
8486 | end if; | |
8487 | end if; | |
8488 | ||
8489 | -- Construct the test and insert into the tree | |
8490 | ||
8491 | if Present (Cond) then | |
8492 | if Do_Access then | |
8493 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
8494 | end if; | |
8495 | ||
f15731c4 | 8496 | Add_Check |
8497 | (Make_Raise_Constraint_Error (Loc, | |
8498 | Condition => Cond, | |
8499 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 8500 | end if; |
8501 | ||
8502 | return Ret_Result; | |
ee6ba406 | 8503 | end Selected_Length_Checks; |
8504 | ||
8505 | --------------------------- | |
8506 | -- Selected_Range_Checks -- | |
8507 | --------------------------- | |
8508 | ||
8509 | function Selected_Range_Checks | |
8510 | (Ck_Node : Node_Id; | |
8511 | Target_Typ : Entity_Id; | |
8512 | Source_Typ : Entity_Id; | |
314a23b6 | 8513 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8514 | is |
8515 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8516 | S_Typ : Entity_Id; | |
8517 | T_Typ : Entity_Id; | |
8518 | Expr_Actual : Node_Id; | |
8519 | Exptyp : Entity_Id; | |
8520 | Cond : Node_Id := Empty; | |
8521 | Do_Access : Boolean := False; | |
8522 | Wnode : Node_Id := Warn_Node; | |
8523 | Ret_Result : Check_Result := (Empty, Empty); | |
8524 | Num_Checks : Integer := 0; | |
8525 | ||
8526 | procedure Add_Check (N : Node_Id); | |
8527 | -- Adds the action given to Ret_Result if N is non-Empty | |
8528 | ||
8529 | function Discrete_Range_Cond | |
8530 | (Expr : Node_Id; | |
314a23b6 | 8531 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8532 | -- Returns expression to compute: |
8533 | -- Low_Bound (Expr) < Typ'First | |
8534 | -- or else | |
8535 | -- High_Bound (Expr) > Typ'Last | |
8536 | ||
8537 | function Discrete_Expr_Cond | |
8538 | (Expr : Node_Id; | |
314a23b6 | 8539 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8540 | -- Returns expression to compute: |
8541 | -- Expr < Typ'First | |
8542 | -- or else | |
8543 | -- Expr > Typ'Last | |
8544 | ||
8545 | function Get_E_First_Or_Last | |
3cb12758 | 8546 | (Loc : Source_Ptr; |
8547 | E : Entity_Id; | |
ee6ba406 | 8548 | Indx : Nat; |
314a23b6 | 8549 | Nam : Name_Id) return Node_Id; |
79212397 | 8550 | -- Returns an attribute reference |
ee6ba406 | 8551 | -- E'First or E'Last |
79212397 | 8552 | -- with a source location of Loc. |
f73ee678 | 8553 | -- |
79212397 | 8554 | -- Nam is Name_First or Name_Last, according to which attribute is |
8555 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
8556 | -- Expressions of the attribute reference (identifying the desired | |
8557 | -- array dimension). | |
ee6ba406 | 8558 | |
8559 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
8560 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
8561 | -- Returns expression to compute: | |
9dfe12ae | 8562 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 8563 | |
8564 | function Range_E_Cond | |
8565 | (Exptyp : Entity_Id; | |
8566 | Typ : Entity_Id; | |
8567 | Indx : Nat) | |
8568 | return Node_Id; | |
8569 | -- Returns expression to compute: | |
8570 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
8571 | ||
8572 | function Range_Equal_E_Cond | |
8573 | (Exptyp : Entity_Id; | |
8574 | Typ : Entity_Id; | |
314a23b6 | 8575 | Indx : Nat) return Node_Id; |
ee6ba406 | 8576 | -- Returns expression to compute: |
8577 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
8578 | ||
8579 | function Range_N_Cond | |
8580 | (Expr : Node_Id; | |
8581 | Typ : Entity_Id; | |
314a23b6 | 8582 | Indx : Nat) return Node_Id; |
ee6ba406 | 8583 | -- Return expression to compute: |
8584 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
8585 | ||
8586 | --------------- | |
8587 | -- Add_Check -- | |
8588 | --------------- | |
8589 | ||
8590 | procedure Add_Check (N : Node_Id) is | |
8591 | begin | |
8592 | if Present (N) then | |
8593 | ||
8594 | -- For now, ignore attempt to place more than 2 checks ??? | |
8595 | ||
8596 | if Num_Checks = 2 then | |
8597 | return; | |
8598 | end if; | |
8599 | ||
8600 | pragma Assert (Num_Checks <= 1); | |
8601 | Num_Checks := Num_Checks + 1; | |
8602 | Ret_Result (Num_Checks) := N; | |
8603 | end if; | |
8604 | end Add_Check; | |
8605 | ||
8606 | ------------------------- | |
8607 | -- Discrete_Expr_Cond -- | |
8608 | ------------------------- | |
8609 | ||
8610 | function Discrete_Expr_Cond | |
8611 | (Expr : Node_Id; | |
314a23b6 | 8612 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8613 | is |
8614 | begin | |
8615 | return | |
8616 | Make_Or_Else (Loc, | |
8617 | Left_Opnd => | |
8618 | Make_Op_Lt (Loc, | |
8619 | Left_Opnd => | |
9dfe12ae | 8620 | Convert_To (Base_Type (Typ), |
8621 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8622 | Right_Opnd => |
8623 | Convert_To (Base_Type (Typ), | |
3cb12758 | 8624 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 8625 | |
8626 | Right_Opnd => | |
8627 | Make_Op_Gt (Loc, | |
8628 | Left_Opnd => | |
9dfe12ae | 8629 | Convert_To (Base_Type (Typ), |
8630 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8631 | Right_Opnd => |
8632 | Convert_To | |
8633 | (Base_Type (Typ), | |
3cb12758 | 8634 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 8635 | end Discrete_Expr_Cond; |
8636 | ||
8637 | ------------------------- | |
8638 | -- Discrete_Range_Cond -- | |
8639 | ------------------------- | |
8640 | ||
8641 | function Discrete_Range_Cond | |
8642 | (Expr : Node_Id; | |
314a23b6 | 8643 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8644 | is |
8645 | LB : Node_Id := Low_Bound (Expr); | |
8646 | HB : Node_Id := High_Bound (Expr); | |
8647 | ||
8648 | Left_Opnd : Node_Id; | |
8649 | Right_Opnd : Node_Id; | |
8650 | ||
8651 | begin | |
8652 | if Nkind (LB) = N_Identifier | |
feff2f05 | 8653 | and then Ekind (Entity (LB)) = E_Discriminant |
8654 | then | |
ee6ba406 | 8655 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
8656 | end if; | |
8657 | ||
ee6ba406 | 8658 | Left_Opnd := |
8659 | Make_Op_Lt (Loc, | |
8660 | Left_Opnd => | |
8661 | Convert_To | |
9dfe12ae | 8662 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 8663 | |
8664 | Right_Opnd => | |
8665 | Convert_To | |
3cb12758 | 8666 | (Base_Type (Typ), |
8667 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 8668 | |
ba9b1a39 | 8669 | if Nkind (HB) = N_Identifier |
8670 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 8671 | then |
ba9b1a39 | 8672 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 8673 | end if; |
8674 | ||
8675 | Right_Opnd := | |
8676 | Make_Op_Gt (Loc, | |
8677 | Left_Opnd => | |
8678 | Convert_To | |
9dfe12ae | 8679 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 8680 | |
8681 | Right_Opnd => | |
8682 | Convert_To | |
8683 | (Base_Type (Typ), | |
3cb12758 | 8684 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 8685 | |
8686 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
8687 | end Discrete_Range_Cond; | |
8688 | ||
8689 | ------------------------- | |
8690 | -- Get_E_First_Or_Last -- | |
8691 | ------------------------- | |
8692 | ||
8693 | function Get_E_First_Or_Last | |
3cb12758 | 8694 | (Loc : Source_Ptr; |
8695 | E : Entity_Id; | |
ee6ba406 | 8696 | Indx : Nat; |
314a23b6 | 8697 | Nam : Name_Id) return Node_Id |
ee6ba406 | 8698 | is |
3cb12758 | 8699 | Exprs : List_Id; |
ee6ba406 | 8700 | begin |
3cb12758 | 8701 | if Indx > 0 then |
8702 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 8703 | else |
3cb12758 | 8704 | Exprs := No_List; |
ee6ba406 | 8705 | end if; |
8706 | ||
3cb12758 | 8707 | return Make_Attribute_Reference (Loc, |
8708 | Prefix => New_Occurrence_Of (E, Loc), | |
8709 | Attribute_Name => Nam, | |
8710 | Expressions => Exprs); | |
ee6ba406 | 8711 | end Get_E_First_Or_Last; |
8712 | ||
8713 | ----------------- | |
8714 | -- Get_N_First -- | |
8715 | ----------------- | |
8716 | ||
8717 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
8718 | begin | |
8719 | return | |
8720 | Make_Attribute_Reference (Loc, | |
8721 | Attribute_Name => Name_First, | |
8722 | Prefix => | |
9dfe12ae | 8723 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8724 | Expressions => New_List ( |
8725 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8726 | end Get_N_First; |
8727 | ||
8728 | ---------------- | |
8729 | -- Get_N_Last -- | |
8730 | ---------------- | |
8731 | ||
8732 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
8733 | begin | |
8734 | return | |
8735 | Make_Attribute_Reference (Loc, | |
8736 | Attribute_Name => Name_Last, | |
8737 | Prefix => | |
9dfe12ae | 8738 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8739 | Expressions => New_List ( |
8740 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8741 | end Get_N_Last; |
8742 | ||
8743 | ------------------ | |
8744 | -- Range_E_Cond -- | |
8745 | ------------------ | |
8746 | ||
8747 | function Range_E_Cond | |
8748 | (Exptyp : Entity_Id; | |
8749 | Typ : Entity_Id; | |
314a23b6 | 8750 | Indx : Nat) return Node_Id |
ee6ba406 | 8751 | is |
8752 | begin | |
8753 | return | |
8754 | Make_Or_Else (Loc, | |
8755 | Left_Opnd => | |
8756 | Make_Op_Lt (Loc, | |
3cb12758 | 8757 | Left_Opnd => |
8758 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8759 | Right_Opnd => | |
8760 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8761 | |
8762 | Right_Opnd => | |
8763 | Make_Op_Gt (Loc, | |
3cb12758 | 8764 | Left_Opnd => |
8765 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8766 | Right_Opnd => | |
8767 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8768 | end Range_E_Cond; |
8769 | ||
8770 | ------------------------ | |
8771 | -- Range_Equal_E_Cond -- | |
8772 | ------------------------ | |
8773 | ||
8774 | function Range_Equal_E_Cond | |
8775 | (Exptyp : Entity_Id; | |
8776 | Typ : Entity_Id; | |
314a23b6 | 8777 | Indx : Nat) return Node_Id |
ee6ba406 | 8778 | is |
8779 | begin | |
8780 | return | |
8781 | Make_Or_Else (Loc, | |
8782 | Left_Opnd => | |
8783 | Make_Op_Ne (Loc, | |
3cb12758 | 8784 | Left_Opnd => |
8785 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8786 | Right_Opnd => | |
8787 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
8788 | ||
ee6ba406 | 8789 | Right_Opnd => |
8790 | Make_Op_Ne (Loc, | |
3cb12758 | 8791 | Left_Opnd => |
8792 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8793 | Right_Opnd => | |
8794 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8795 | end Range_Equal_E_Cond; |
8796 | ||
8797 | ------------------ | |
8798 | -- Range_N_Cond -- | |
8799 | ------------------ | |
8800 | ||
8801 | function Range_N_Cond | |
8802 | (Expr : Node_Id; | |
8803 | Typ : Entity_Id; | |
314a23b6 | 8804 | Indx : Nat) return Node_Id |
ee6ba406 | 8805 | is |
8806 | begin | |
8807 | return | |
8808 | Make_Or_Else (Loc, | |
8809 | Left_Opnd => | |
8810 | Make_Op_Lt (Loc, | |
3cb12758 | 8811 | Left_Opnd => |
8812 | Get_N_First (Expr, Indx), | |
8813 | Right_Opnd => | |
8814 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8815 | |
8816 | Right_Opnd => | |
8817 | Make_Op_Gt (Loc, | |
3cb12758 | 8818 | Left_Opnd => |
8819 | Get_N_Last (Expr, Indx), | |
8820 | Right_Opnd => | |
8821 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8822 | end Range_N_Cond; |
8823 | ||
8824 | -- Start of processing for Selected_Range_Checks | |
8825 | ||
8826 | begin | |
6dbcfcd9 | 8827 | if not Full_Expander_Active then |
ee6ba406 | 8828 | return Ret_Result; |
8829 | end if; | |
8830 | ||
8831 | if Target_Typ = Any_Type | |
8832 | or else Target_Typ = Any_Composite | |
8833 | or else Raises_Constraint_Error (Ck_Node) | |
8834 | then | |
8835 | return Ret_Result; | |
8836 | end if; | |
8837 | ||
8838 | if No (Wnode) then | |
8839 | Wnode := Ck_Node; | |
8840 | end if; | |
8841 | ||
8842 | T_Typ := Target_Typ; | |
8843 | ||
8844 | if No (Source_Typ) then | |
8845 | S_Typ := Etype (Ck_Node); | |
8846 | else | |
8847 | S_Typ := Source_Typ; | |
8848 | end if; | |
8849 | ||
8850 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8851 | return Ret_Result; | |
8852 | end if; | |
8853 | ||
8854 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
8855 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
8856 | -- in, and since Node can be an N_Range node, it might be invalid. | |
8857 | -- Should there be an assert check somewhere for taking the Etype of | |
8858 | -- an N_Range node ??? | |
8859 | ||
8860 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8861 | S_Typ := Designated_Type (S_Typ); | |
8862 | T_Typ := Designated_Type (T_Typ); | |
8863 | Do_Access := True; | |
8864 | ||
2af58f67 | 8865 | -- A simple optimization for the null case |
ee6ba406 | 8866 | |
2af58f67 | 8867 | if Known_Null (Ck_Node) then |
ee6ba406 | 8868 | return Ret_Result; |
8869 | end if; | |
8870 | end if; | |
8871 | ||
8872 | -- For an N_Range Node, check for a null range and then if not | |
8873 | -- null generate a range check action. | |
8874 | ||
8875 | if Nkind (Ck_Node) = N_Range then | |
8876 | ||
8877 | -- There's no point in checking a range against itself | |
8878 | ||
8879 | if Ck_Node = Scalar_Range (T_Typ) then | |
8880 | return Ret_Result; | |
8881 | end if; | |
8882 | ||
8883 | declare | |
8884 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
8885 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 8886 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
8887 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 8888 | |
eefa141b | 8889 | LB : Node_Id := Low_Bound (Ck_Node); |
8890 | HB : Node_Id := High_Bound (Ck_Node); | |
8891 | Known_LB : Boolean; | |
8892 | Known_HB : Boolean; | |
8893 | ||
8894 | Null_Range : Boolean; | |
ee6ba406 | 8895 | Out_Of_Range_L : Boolean; |
8896 | Out_Of_Range_H : Boolean; | |
8897 | ||
8898 | begin | |
eefa141b | 8899 | -- Compute what is known at compile time |
8900 | ||
8901 | if Known_T_LB and Known_T_HB then | |
8902 | if Compile_Time_Known_Value (LB) then | |
8903 | Known_LB := True; | |
8904 | ||
8905 | -- There's no point in checking that a bound is within its | |
8906 | -- own range so pretend that it is known in this case. First | |
8907 | -- deal with low bound. | |
8908 | ||
8909 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
8910 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
8911 | then | |
8912 | LB := T_LB; | |
8913 | Known_LB := True; | |
8914 | ||
8915 | else | |
8916 | Known_LB := False; | |
8917 | end if; | |
8918 | ||
8919 | -- Likewise for the high bound | |
8920 | ||
8921 | if Compile_Time_Known_Value (HB) then | |
8922 | Known_HB := True; | |
8923 | ||
8924 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
8925 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
8926 | then | |
8927 | HB := T_HB; | |
8928 | Known_HB := True; | |
8929 | ||
8930 | else | |
8931 | Known_HB := False; | |
8932 | end if; | |
8933 | end if; | |
8934 | ||
8935 | -- Check for case where everything is static and we can do the | |
8936 | -- check at compile time. This is skipped if we have an access | |
8937 | -- type, since the access value may be null. | |
8938 | ||
8939 | -- ??? This code can be improved since you only need to know that | |
8940 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
8941 | -- compile time to emit pertinent messages. | |
8942 | ||
8943 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
8944 | and not Do_Access | |
ee6ba406 | 8945 | then |
8946 | -- Floating-point case | |
8947 | ||
8948 | if Is_Floating_Point_Type (S_Typ) then | |
8949 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
8950 | Out_Of_Range_L := | |
8951 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 8952 | or else |
ee6ba406 | 8953 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
8954 | ||
8955 | Out_Of_Range_H := | |
8956 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 8957 | or else |
ee6ba406 | 8958 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
8959 | ||
8960 | -- Fixed or discrete type case | |
8961 | ||
8962 | else | |
8963 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
8964 | Out_Of_Range_L := | |
8965 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 8966 | or else |
ee6ba406 | 8967 | (Expr_Value (LB) > Expr_Value (T_HB)); |
8968 | ||
8969 | Out_Of_Range_H := | |
8970 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 8971 | or else |
ee6ba406 | 8972 | (Expr_Value (HB) < Expr_Value (T_LB)); |
8973 | end if; | |
8974 | ||
8975 | if not Null_Range then | |
8976 | if Out_Of_Range_L then | |
8977 | if No (Warn_Node) then | |
8978 | Add_Check | |
8979 | (Compile_Time_Constraint_Error | |
8980 | (Low_Bound (Ck_Node), | |
cb97ae5c | 8981 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 8982 | |
8983 | else | |
8984 | Add_Check | |
8985 | (Compile_Time_Constraint_Error | |
8986 | (Wnode, | |
cb97ae5c | 8987 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 8988 | end if; |
8989 | end if; | |
8990 | ||
8991 | if Out_Of_Range_H then | |
8992 | if No (Warn_Node) then | |
8993 | Add_Check | |
8994 | (Compile_Time_Constraint_Error | |
8995 | (High_Bound (Ck_Node), | |
cb97ae5c | 8996 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 8997 | |
8998 | else | |
8999 | Add_Check | |
9000 | (Compile_Time_Constraint_Error | |
9001 | (Wnode, | |
cb97ae5c | 9002 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 9003 | end if; |
9004 | end if; | |
ee6ba406 | 9005 | end if; |
9006 | ||
9007 | else | |
9008 | declare | |
9009 | LB : Node_Id := Low_Bound (Ck_Node); | |
9010 | HB : Node_Id := High_Bound (Ck_Node); | |
9011 | ||
9012 | begin | |
feff2f05 | 9013 | -- If either bound is a discriminant and we are within the |
9014 | -- record declaration, it is a use of the discriminant in a | |
9015 | -- constraint of a component, and nothing can be checked | |
9016 | -- here. The check will be emitted within the init proc. | |
9017 | -- Before then, the discriminal has no real meaning. | |
9018 | -- Similarly, if the entity is a discriminal, there is no | |
9019 | -- check to perform yet. | |
9020 | ||
9021 | -- The same holds within a discriminated synchronized type, | |
9022 | -- where the discriminant may constrain a component or an | |
9023 | -- entry family. | |
ee6ba406 | 9024 | |
9025 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 9026 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 9027 | then |
0577b0b1 | 9028 | if Current_Scope = Scope (Entity (LB)) |
9029 | or else Is_Concurrent_Type (Current_Scope) | |
9030 | or else Ekind (Entity (LB)) /= E_Discriminant | |
9031 | then | |
ee6ba406 | 9032 | return Ret_Result; |
9033 | else | |
9034 | LB := | |
9035 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
9036 | end if; | |
9037 | end if; | |
9038 | ||
9039 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 9040 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 9041 | then |
0577b0b1 | 9042 | if Current_Scope = Scope (Entity (HB)) |
9043 | or else Is_Concurrent_Type (Current_Scope) | |
9044 | or else Ekind (Entity (HB)) /= E_Discriminant | |
9045 | then | |
ee6ba406 | 9046 | return Ret_Result; |
9047 | else | |
9048 | HB := | |
9049 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
9050 | end if; | |
9051 | end if; | |
9052 | ||
9053 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
9054 | Set_Paren_Count (Cond, 1); | |
9055 | ||
9056 | Cond := | |
9057 | Make_And_Then (Loc, | |
9058 | Left_Opnd => | |
9059 | Make_Op_Ge (Loc, | |
9dfe12ae | 9060 | Left_Opnd => Duplicate_Subexpr_No_Checks (HB), |
9061 | Right_Opnd => Duplicate_Subexpr_No_Checks (LB)), | |
ee6ba406 | 9062 | Right_Opnd => Cond); |
9063 | end; | |
ee6ba406 | 9064 | end if; |
9065 | end; | |
9066 | ||
9067 | elsif Is_Scalar_Type (S_Typ) then | |
9068 | ||
9069 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
9070 | -- except the above simply sets a flag in the node and lets | |
9071 | -- gigi generate the check base on the Etype of the expression. | |
9072 | -- Sometimes, however we want to do a dynamic check against an | |
9073 | -- arbitrary target type, so we do that here. | |
9074 | ||
9075 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
9076 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9077 | ||
9078 | -- For literals, we can tell if the constraint error will be | |
9079 | -- raised at compile time, so we never need a dynamic check, but | |
9080 | -- if the exception will be raised, then post the usual warning, | |
9081 | -- and replace the literal with a raise constraint error | |
9082 | -- expression. As usual, skip this for access types | |
9083 | ||
9084 | elsif Compile_Time_Known_Value (Ck_Node) | |
9085 | and then not Do_Access | |
9086 | then | |
9087 | declare | |
9088 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
9089 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
9090 | ||
9091 | Out_Of_Range : Boolean; | |
9092 | Static_Bounds : constant Boolean := | |
b6341c67 | 9093 | Compile_Time_Known_Value (LB) |
9094 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 9095 | |
9096 | begin | |
9097 | -- Following range tests should use Sem_Eval routine ??? | |
9098 | ||
9099 | if Static_Bounds then | |
9100 | if Is_Floating_Point_Type (S_Typ) then | |
9101 | Out_Of_Range := | |
9102 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
9103 | or else | |
9104 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
9105 | ||
eefa141b | 9106 | -- Fixed or discrete type |
9107 | ||
9108 | else | |
ee6ba406 | 9109 | Out_Of_Range := |
9110 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
9111 | or else | |
9112 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
9113 | end if; | |
9114 | ||
eefa141b | 9115 | -- Bounds of the type are static and the literal is out of |
9116 | -- range so output a warning message. | |
ee6ba406 | 9117 | |
9118 | if Out_Of_Range then | |
9119 | if No (Warn_Node) then | |
9120 | Add_Check | |
9121 | (Compile_Time_Constraint_Error | |
9122 | (Ck_Node, | |
cb97ae5c | 9123 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9124 | |
9125 | else | |
9126 | Add_Check | |
9127 | (Compile_Time_Constraint_Error | |
9128 | (Wnode, | |
cb97ae5c | 9129 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9130 | end if; |
9131 | end if; | |
9132 | ||
9133 | else | |
9134 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9135 | end if; | |
9136 | end; | |
9137 | ||
9138 | -- Here for the case of a non-static expression, we need a runtime | |
9139 | -- check unless the source type range is guaranteed to be in the | |
9140 | -- range of the target type. | |
9141 | ||
9142 | else | |
7a1dabb3 | 9143 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 9144 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
9145 | end if; | |
9146 | end if; | |
9147 | end if; | |
9148 | ||
9149 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9150 | if Is_Constrained (T_Typ) then | |
9151 | ||
9152 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
9153 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
9154 | ||
9155 | if Is_Access_Type (Exptyp) then | |
9156 | Exptyp := Designated_Type (Exptyp); | |
9157 | end if; | |
9158 | ||
9159 | -- String_Literal case. This needs to be handled specially be- | |
9160 | -- cause no index types are available for string literals. The | |
9161 | -- condition is simply: | |
9162 | ||
9163 | -- T_Typ'Length = string-literal-length | |
9164 | ||
9165 | if Nkind (Expr_Actual) = N_String_Literal then | |
9166 | null; | |
9167 | ||
9168 | -- General array case. Here we have a usable actual subtype for | |
9169 | -- the expression, and the condition is built from the two types | |
9170 | ||
9171 | -- T_Typ'First < Exptyp'First or else | |
9172 | -- T_Typ'Last > Exptyp'Last or else | |
9173 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
9174 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
9175 | -- ... | |
9176 | ||
9177 | elsif Is_Constrained (Exptyp) then | |
9178 | declare | |
9dfe12ae | 9179 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9180 | ||
ee6ba406 | 9181 | L_Index : Node_Id; |
9182 | R_Index : Node_Id; | |
ee6ba406 | 9183 | |
9184 | begin | |
9185 | L_Index := First_Index (T_Typ); | |
9186 | R_Index := First_Index (Exptyp); | |
9187 | ||
9188 | for Indx in 1 .. Ndims loop | |
9189 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9190 | or else |
9191 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9192 | then |
ee6ba406 | 9193 | -- Deal with compile time length check. Note that we |
9194 | -- skip this in the access case, because the access | |
9195 | -- value may be null, so we cannot know statically. | |
9196 | ||
9197 | if not | |
9198 | Subtypes_Statically_Match | |
9199 | (Etype (L_Index), Etype (R_Index)) | |
9200 | then | |
9201 | -- If the target type is constrained then we | |
9202 | -- have to check for exact equality of bounds | |
9203 | -- (required for qualified expressions). | |
9204 | ||
9205 | if Is_Constrained (T_Typ) then | |
9206 | Evolve_Or_Else | |
9207 | (Cond, | |
9208 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 9209 | else |
9210 | Evolve_Or_Else | |
9211 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
9212 | end if; | |
9213 | end if; | |
9214 | ||
9215 | Next (L_Index); | |
9216 | Next (R_Index); | |
ee6ba406 | 9217 | end if; |
9218 | end loop; | |
9219 | end; | |
9220 | ||
9221 | -- Handle cases where we do not get a usable actual subtype that | |
9222 | -- is constrained. This happens for example in the function call | |
9223 | -- and explicit dereference cases. In these cases, we have to get | |
9224 | -- the length or range from the expression itself, making sure we | |
9225 | -- do not evaluate it more than once. | |
9226 | ||
9227 | -- Here Ck_Node is the original expression, or more properly the | |
9228 | -- result of applying Duplicate_Expr to the original tree, | |
9229 | -- forcing the result to be a name. | |
9230 | ||
9231 | else | |
9232 | declare | |
9dfe12ae | 9233 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9234 | |
9235 | begin | |
9236 | -- Build the condition for the explicit dereference case | |
9237 | ||
9238 | for Indx in 1 .. Ndims loop | |
9239 | Evolve_Or_Else | |
9240 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
9241 | end loop; | |
9242 | end; | |
ee6ba406 | 9243 | end if; |
9244 | ||
9245 | else | |
feff2f05 | 9246 | -- For a conversion to an unconstrained array type, generate an |
9247 | -- Action to check that the bounds of the source value are within | |
9248 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
9249 | -- check is needed for a conversion to an access to unconstrained | |
9250 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
9251 | -- of the two access types to statically match. | |
9252 | ||
9253 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
9254 | and then not Do_Access | |
9255 | then | |
ee6ba406 | 9256 | declare |
9257 | Opnd_Index : Node_Id; | |
9258 | Targ_Index : Node_Id; | |
00c403ee | 9259 | Opnd_Range : Node_Id; |
ee6ba406 | 9260 | |
9261 | begin | |
feff2f05 | 9262 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 9263 | Targ_Index := First_Index (T_Typ); |
00c403ee | 9264 | while Present (Opnd_Index) loop |
9265 | ||
9266 | -- If the index is a range, use its bounds. If it is an | |
9267 | -- entity (as will be the case if it is a named subtype | |
9268 | -- or an itype created for a slice) retrieve its range. | |
9269 | ||
9270 | if Is_Entity_Name (Opnd_Index) | |
9271 | and then Is_Type (Entity (Opnd_Index)) | |
9272 | then | |
9273 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
9274 | else | |
9275 | Opnd_Range := Opnd_Index; | |
9276 | end if; | |
9277 | ||
9278 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 9279 | if Is_In_Range |
9280 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
9281 | Assume_Valid => True) | |
ee6ba406 | 9282 | and then |
9283 | Is_In_Range | |
9c486805 | 9284 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9285 | Assume_Valid => True) | |
ee6ba406 | 9286 | then |
9287 | null; | |
9288 | ||
feff2f05 | 9289 | -- If null range, no check needed |
f2a06be9 | 9290 | |
9dfe12ae | 9291 | elsif |
00c403ee | 9292 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 9293 | and then |
00c403ee | 9294 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 9295 | and then |
00c403ee | 9296 | Expr_Value (High_Bound (Opnd_Range)) < |
9297 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 9298 | then |
9299 | null; | |
9300 | ||
ee6ba406 | 9301 | elsif Is_Out_Of_Range |
9c486805 | 9302 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
9303 | Assume_Valid => True) | |
ee6ba406 | 9304 | or else |
9305 | Is_Out_Of_Range | |
9c486805 | 9306 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9307 | Assume_Valid => True) | |
ee6ba406 | 9308 | then |
9309 | Add_Check | |
9310 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9311 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 9312 | |
9313 | else | |
9314 | Evolve_Or_Else | |
9315 | (Cond, | |
9316 | Discrete_Range_Cond | |
00c403ee | 9317 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 9318 | end if; |
9319 | end if; | |
9320 | ||
9321 | Next_Index (Opnd_Index); | |
9322 | Next_Index (Targ_Index); | |
9323 | end loop; | |
9324 | end; | |
9325 | end if; | |
9326 | end if; | |
9327 | end if; | |
9328 | ||
9329 | -- Construct the test and insert into the tree | |
9330 | ||
9331 | if Present (Cond) then | |
9332 | if Do_Access then | |
9333 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9334 | end if; | |
9335 | ||
f15731c4 | 9336 | Add_Check |
9337 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 9338 | Condition => Cond, |
9339 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 9340 | end if; |
9341 | ||
9342 | return Ret_Result; | |
ee6ba406 | 9343 | end Selected_Range_Checks; |
9344 | ||
9345 | ------------------------------- | |
9346 | -- Storage_Checks_Suppressed -- | |
9347 | ------------------------------- | |
9348 | ||
9349 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9350 | begin | |
9dfe12ae | 9351 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9352 | return Is_Check_Suppressed (E, Storage_Check); | |
9353 | else | |
fafc6b97 | 9354 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 9355 | end if; |
ee6ba406 | 9356 | end Storage_Checks_Suppressed; |
9357 | ||
9358 | --------------------------- | |
9359 | -- Tag_Checks_Suppressed -- | |
9360 | --------------------------- | |
9361 | ||
9362 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9363 | begin | |
89f1e35c | 9364 | if Present (E) |
9365 | and then Checks_May_Be_Suppressed (E) | |
9366 | then | |
9367 | return Is_Check_Suppressed (E, Tag_Check); | |
9dfe12ae | 9368 | end if; |
9369 | ||
fafc6b97 | 9370 | return Scope_Suppress.Suppress (Tag_Check); |
ee6ba406 | 9371 | end Tag_Checks_Suppressed; |
9372 | ||
0577b0b1 | 9373 | -------------------------- |
9374 | -- Validity_Check_Range -- | |
9375 | -------------------------- | |
9376 | ||
9377 | procedure Validity_Check_Range (N : Node_Id) is | |
9378 | begin | |
9379 | if Validity_Checks_On and Validity_Check_Operands then | |
9380 | if Nkind (N) = N_Range then | |
9381 | Ensure_Valid (Low_Bound (N)); | |
9382 | Ensure_Valid (High_Bound (N)); | |
9383 | end if; | |
9384 | end if; | |
9385 | end Validity_Check_Range; | |
9386 | ||
9387 | -------------------------------- | |
9388 | -- Validity_Checks_Suppressed -- | |
9389 | -------------------------------- | |
9390 | ||
9391 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9392 | begin | |
9393 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9394 | return Is_Check_Suppressed (E, Validity_Check); | |
9395 | else | |
fafc6b97 | 9396 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 9397 | end if; |
9398 | end Validity_Checks_Suppressed; | |
9399 | ||
ee6ba406 | 9400 | end Checks; |