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