]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/checks.adb
2011-08-29 Thomas Quinot <quinot@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-- --
dd688950 9-- Copyright (C) 1992-2011, 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;
27with Debug; use Debug;
28with Einfo; use Einfo;
29with Errout; use Errout;
30with Exp_Ch2; use Exp_Ch2;
df40eeb0 31with Exp_Ch4; use Exp_Ch4;
00c403ee 32with Exp_Ch11; use Exp_Ch11;
05fcfafb 33with Exp_Pakd; use Exp_Pakd;
ee6ba406 34with Exp_Util; use Exp_Util;
35with Elists; use Elists;
5329ca64 36with Eval_Fat; use Eval_Fat;
ee6ba406 37with Freeze; use Freeze;
9dfe12ae 38with Lib; use Lib;
ee6ba406 39with Nlists; use Nlists;
40with Nmake; use Nmake;
41with Opt; use Opt;
9dfe12ae 42with Output; use Output;
c2b56224 43with Restrict; use Restrict;
1e16c51c 44with Rident; use Rident;
ee6ba406 45with Rtsfind; use Rtsfind;
46with Sem; use Sem;
d60c9ff7 47with Sem_Aux; use Sem_Aux;
ee6ba406 48with Sem_Eval; use Sem_Eval;
00f91aef 49with Sem_Ch3; use Sem_Ch3;
9dfe12ae 50with Sem_Ch8; use Sem_Ch8;
ee6ba406 51with Sem_Res; use Sem_Res;
52with Sem_Util; use Sem_Util;
53with Sem_Warn; use Sem_Warn;
54with Sinfo; use Sinfo;
9dfe12ae 55with Sinput; use Sinput;
ee6ba406 56with Snames; use Snames;
9dfe12ae 57with Sprint; use Sprint;
ee6ba406 58with Stand; use Stand;
f15731c4 59with Targparm; use Targparm;
ee6ba406 60with Tbuild; use Tbuild;
61with Ttypes; use Ttypes;
62with Urealp; use Urealp;
63with Validsw; use Validsw;
64
65package body Checks is
66
67 -- General note: many of these routines are concerned with generating
68 -- checking code to make sure that constraint error is raised at runtime.
69 -- Clearly this code is only needed if the expander is active, since
70 -- otherwise we will not be generating code or going into the runtime
71 -- execution anyway.
72
73 -- We therefore disconnect most of these checks if the expander is
74 -- inactive. This has the additional benefit that we do not need to
75 -- worry about the tree being messed up by previous errors (since errors
76 -- turn off expansion anyway).
77
78 -- There are a few exceptions to the above rule. For instance routines
79 -- such as Apply_Scalar_Range_Check that do not insert any code can be
80 -- safely called even when the Expander is inactive (but Errors_Detected
81 -- is 0). The benefit of executing this code when expansion is off, is
82 -- the ability to emit constraint error warning for static expressions
83 -- even when we are not generating code.
84
9dfe12ae 85 -------------------------------------
86 -- Suppression of Redundant Checks --
87 -------------------------------------
88
89 -- This unit implements a limited circuit for removal of redundant
90 -- checks. The processing is based on a tracing of simple sequential
91 -- flow. For any sequence of statements, we save expressions that are
92 -- marked to be checked, and then if the same expression appears later
93 -- with the same check, then under certain circumstances, the second
94 -- check can be suppressed.
95
96 -- Basically, we can suppress the check if we know for certain that
97 -- the previous expression has been elaborated (together with its
98 -- check), and we know that the exception frame is the same, and that
99 -- nothing has happened to change the result of the exception.
100
101 -- Let us examine each of these three conditions in turn to describe
102 -- how we ensure that this condition is met.
103
104 -- First, we need to know for certain that the previous expression has
6fb3c314 105 -- been executed. This is done principally by the mechanism of calling
9dfe12ae 106 -- Conditional_Statements_Begin at the start of any statement sequence
107 -- and Conditional_Statements_End at the end. The End call causes all
108 -- checks remembered since the Begin call to be discarded. This does
109 -- miss a few cases, notably the case of a nested BEGIN-END block with
110 -- no exception handlers. But the important thing is to be conservative.
111 -- The other protection is that all checks are discarded if a label
112 -- is encountered, since then the assumption of sequential execution
113 -- is violated, and we don't know enough about the flow.
114
115 -- Second, we need to know that the exception frame is the same. We
116 -- do this by killing all remembered checks when we enter a new frame.
117 -- Again, that's over-conservative, but generally the cases we can help
118 -- with are pretty local anyway (like the body of a loop for example).
119
120 -- Third, we must be sure to forget any checks which are no longer valid.
121 -- This is done by two mechanisms, first the Kill_Checks_Variable call is
122 -- used to note any changes to local variables. We only attempt to deal
123 -- with checks involving local variables, so we do not need to worry
124 -- about global variables. Second, a call to any non-global procedure
125 -- causes us to abandon all stored checks, since such a all may affect
126 -- the values of any local variables.
127
128 -- The following define the data structures used to deal with remembering
129 -- checks so that redundant checks can be eliminated as described above.
130
131 -- Right now, the only expressions that we deal with are of the form of
132 -- simple local objects (either declared locally, or IN parameters) or
133 -- such objects plus/minus a compile time known constant. We can do
134 -- more later on if it seems worthwhile, but this catches many simple
135 -- cases in practice.
136
137 -- The following record type reflects a single saved check. An entry
138 -- is made in the stack of saved checks if and only if the expression
139 -- has been elaborated with the indicated checks.
140
141 type Saved_Check is record
142 Killed : Boolean;
143 -- Set True if entry is killed by Kill_Checks
144
145 Entity : Entity_Id;
146 -- The entity involved in the expression that is checked
147
148 Offset : Uint;
149 -- A compile time value indicating the result of adding or
150 -- subtracting a compile time value. This value is to be
151 -- added to the value of the Entity. A value of zero is
152 -- used for the case of a simple entity reference.
153
154 Check_Type : Character;
155 -- This is set to 'R' for a range check (in which case Target_Type
156 -- is set to the target type for the range check) or to 'O' for an
157 -- overflow check (in which case Target_Type is set to Empty).
158
159 Target_Type : Entity_Id;
160 -- Used only if Do_Range_Check is set. Records the target type for
161 -- the check. We need this, because a check is a duplicate only if
6fb3c314 162 -- it has the same target type (or more accurately one with a
9dfe12ae 163 -- range that is smaller or equal to the stored target type of a
164 -- saved check).
165 end record;
166
167 -- The following table keeps track of saved checks. Rather than use an
168 -- extensible table. We just use a table of fixed size, and we discard
169 -- any saved checks that do not fit. That's very unlikely to happen and
170 -- this is only an optimization in any case.
171
172 Saved_Checks : array (Int range 1 .. 200) of Saved_Check;
173 -- Array of saved checks
174
175 Num_Saved_Checks : Nat := 0;
176 -- Number of saved checks
177
178 -- The following stack keeps track of statement ranges. It is treated
179 -- as a stack. When Conditional_Statements_Begin is called, an entry
180 -- is pushed onto this stack containing the value of Num_Saved_Checks
181 -- at the time of the call. Then when Conditional_Statements_End is
182 -- called, this value is popped off and used to reset Num_Saved_Checks.
183
184 -- Note: again, this is a fixed length stack with a size that should
185 -- always be fine. If the value of the stack pointer goes above the
186 -- limit, then we just forget all saved checks.
187
188 Saved_Checks_Stack : array (Int range 1 .. 100) of Nat;
189 Saved_Checks_TOS : Nat := 0;
190
191 -----------------------
192 -- Local Subprograms --
193 -----------------------
ee6ba406 194
5329ca64 195 procedure Apply_Float_Conversion_Check
196 (Ck_Node : Node_Id;
197 Target_Typ : Entity_Id);
198 -- The checks on a conversion from a floating-point type to an integer
199 -- type are delicate. They have to be performed before conversion, they
200 -- have to raise an exception when the operand is a NaN, and rounding must
201 -- be taken into account to determine the safe bounds of the operand.
202
ee6ba406 203 procedure Apply_Selected_Length_Checks
204 (Ck_Node : Node_Id;
205 Target_Typ : Entity_Id;
206 Source_Typ : Entity_Id;
207 Do_Static : Boolean);
208 -- This is the subprogram that does all the work for Apply_Length_Check
209 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
210 -- described for the above routines. The Do_Static flag indicates that
211 -- only a static check is to be done.
212
213 procedure Apply_Selected_Range_Checks
214 (Ck_Node : Node_Id;
215 Target_Typ : Entity_Id;
216 Source_Typ : Entity_Id;
217 Do_Static : Boolean);
218 -- This is the subprogram that does all the work for Apply_Range_Check.
219 -- Expr, Target_Typ and Source_Typ are as described for the above
220 -- routine. The Do_Static flag indicates that only a static check is
221 -- to be done.
222
2af58f67 223 type Check_Type is new Check_Id range Access_Check .. Division_Check;
13dbf220 224 function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean;
225 -- This function is used to see if an access or division by zero check is
226 -- needed. The check is to be applied to a single variable appearing in the
227 -- source, and N is the node for the reference. If N is not of this form,
228 -- True is returned with no further processing. If N is of the right form,
229 -- then further processing determines if the given Check is needed.
230 --
231 -- The particular circuit is to see if we have the case of a check that is
232 -- not needed because it appears in the right operand of a short circuited
233 -- conditional where the left operand guards the check. For example:
234 --
235 -- if Var = 0 or else Q / Var > 12 then
236 -- ...
237 -- end if;
238 --
239 -- In this example, the division check is not required. At the same time
240 -- we can issue warnings for suspicious use of non-short-circuited forms,
241 -- such as:
242 --
243 -- if Var = 0 or Q / Var > 12 then
244 -- ...
245 -- end if;
246
9dfe12ae 247 procedure Find_Check
248 (Expr : Node_Id;
249 Check_Type : Character;
250 Target_Type : Entity_Id;
251 Entry_OK : out Boolean;
252 Check_Num : out Nat;
253 Ent : out Entity_Id;
254 Ofs : out Uint);
255 -- This routine is used by Enable_Range_Check and Enable_Overflow_Check
256 -- to see if a check is of the form for optimization, and if so, to see
257 -- if it has already been performed. Expr is the expression to check,
258 -- and Check_Type is 'R' for a range check, 'O' for an overflow check.
259 -- Target_Type is the target type for a range check, and Empty for an
260 -- overflow check. If the entry is not of the form for optimization,
261 -- then Entry_OK is set to False, and the remaining out parameters
262 -- are undefined. If the entry is OK, then Ent/Ofs are set to the
263 -- entity and offset from the expression. Check_Num is the number of
264 -- a matching saved entry in Saved_Checks, or zero if no such entry
265 -- is located.
266
ee6ba406 267 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
268 -- If a discriminal is used in constraining a prival, Return reference
269 -- to the discriminal of the protected body (which renames the parameter
270 -- of the enclosing protected operation). This clumsy transformation is
271 -- needed because privals are created too late and their actual subtypes
272 -- are not available when analysing the bodies of the protected operations.
0577b0b1 273 -- This function is called whenever the bound is an entity and the scope
274 -- indicates a protected operation. If the bound is an in-parameter of
275 -- a protected operation that is not a prival, the function returns the
276 -- bound itself.
ee6ba406 277 -- To be cleaned up???
278
279 function Guard_Access
280 (Cond : Node_Id;
281 Loc : Source_Ptr;
314a23b6 282 Ck_Node : Node_Id) return Node_Id;
ee6ba406 283 -- In the access type case, guard the test with a test to ensure
284 -- that the access value is non-null, since the checks do not
285 -- not apply to null access values.
286
287 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
288 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
289 -- Constraint_Error node.
290
0577b0b1 291 function Range_Or_Validity_Checks_Suppressed
292 (Expr : Node_Id) return Boolean;
293 -- Returns True if either range or validity checks or both are suppressed
294 -- for the type of the given expression, or, if the expression is the name
295 -- of an entity, if these checks are suppressed for the entity.
296
ee6ba406 297 function Selected_Length_Checks
298 (Ck_Node : Node_Id;
299 Target_Typ : Entity_Id;
300 Source_Typ : Entity_Id;
314a23b6 301 Warn_Node : Node_Id) return Check_Result;
ee6ba406 302 -- Like Apply_Selected_Length_Checks, except it doesn't modify
303 -- anything, just returns a list of nodes as described in the spec of
304 -- this package for the Range_Check function.
305
306 function Selected_Range_Checks
307 (Ck_Node : Node_Id;
308 Target_Typ : Entity_Id;
309 Source_Typ : Entity_Id;
314a23b6 310 Warn_Node : Node_Id) return Check_Result;
ee6ba406 311 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
312 -- just returns a list of nodes as described in the spec of this package
313 -- for the Range_Check function.
314
315 ------------------------------
316 -- Access_Checks_Suppressed --
317 ------------------------------
318
319 function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
320 begin
9dfe12ae 321 if Present (E) and then Checks_May_Be_Suppressed (E) then
322 return Is_Check_Suppressed (E, Access_Check);
323 else
324 return Scope_Suppress (Access_Check);
325 end if;
ee6ba406 326 end Access_Checks_Suppressed;
327
328 -------------------------------------
329 -- Accessibility_Checks_Suppressed --
330 -------------------------------------
331
332 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
333 begin
9dfe12ae 334 if Present (E) and then Checks_May_Be_Suppressed (E) then
335 return Is_Check_Suppressed (E, Accessibility_Check);
336 else
337 return Scope_Suppress (Accessibility_Check);
338 end if;
ee6ba406 339 end Accessibility_Checks_Suppressed;
340
00c403ee 341 -----------------------------
342 -- Activate_Division_Check --
343 -----------------------------
344
345 procedure Activate_Division_Check (N : Node_Id) is
346 begin
347 Set_Do_Division_Check (N, True);
348 Possible_Local_Raise (N, Standard_Constraint_Error);
349 end Activate_Division_Check;
350
351 -----------------------------
352 -- Activate_Overflow_Check --
353 -----------------------------
354
355 procedure Activate_Overflow_Check (N : Node_Id) is
356 begin
357 Set_Do_Overflow_Check (N, True);
358 Possible_Local_Raise (N, Standard_Constraint_Error);
359 end Activate_Overflow_Check;
360
361 --------------------------
362 -- Activate_Range_Check --
363 --------------------------
364
365 procedure Activate_Range_Check (N : Node_Id) is
366 begin
367 Set_Do_Range_Check (N, True);
368 Possible_Local_Raise (N, Standard_Constraint_Error);
369 end Activate_Range_Check;
370
0577b0b1 371 ---------------------------------
372 -- Alignment_Checks_Suppressed --
373 ---------------------------------
374
375 function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is
376 begin
377 if Present (E) and then Checks_May_Be_Suppressed (E) then
378 return Is_Check_Suppressed (E, Alignment_Check);
379 else
380 return Scope_Suppress (Alignment_Check);
381 end if;
382 end Alignment_Checks_Suppressed;
383
ee6ba406 384 -------------------------
385 -- Append_Range_Checks --
386 -------------------------
387
388 procedure Append_Range_Checks
389 (Checks : Check_Result;
390 Stmts : List_Id;
391 Suppress_Typ : Entity_Id;
392 Static_Sloc : Source_Ptr;
393 Flag_Node : Node_Id)
394 is
9dfe12ae 395 Internal_Flag_Node : constant Node_Id := Flag_Node;
396 Internal_Static_Sloc : constant Source_Ptr := Static_Sloc;
397
ee6ba406 398 Checks_On : constant Boolean :=
399 (not Index_Checks_Suppressed (Suppress_Typ))
400 or else
401 (not Range_Checks_Suppressed (Suppress_Typ));
402
403 begin
404 -- For now we just return if Checks_On is false, however this should
405 -- be enhanced to check for an always True value in the condition
406 -- and to generate a compilation warning???
407
408 if not Checks_On then
409 return;
410 end if;
411
412 for J in 1 .. 2 loop
413 exit when No (Checks (J));
414
415 if Nkind (Checks (J)) = N_Raise_Constraint_Error
416 and then Present (Condition (Checks (J)))
417 then
418 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
419 Append_To (Stmts, Checks (J));
420 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
421 end if;
422
423 else
424 Append_To
f15731c4 425 (Stmts,
426 Make_Raise_Constraint_Error (Internal_Static_Sloc,
427 Reason => CE_Range_Check_Failed));
ee6ba406 428 end if;
429 end loop;
430 end Append_Range_Checks;
431
432 ------------------------
433 -- Apply_Access_Check --
434 ------------------------
435
436 procedure Apply_Access_Check (N : Node_Id) is
437 P : constant Node_Id := Prefix (N);
438
439 begin
13dbf220 440 -- We do not need checks if we are not generating code (i.e. the
441 -- expander is not active). This is not just an optimization, there
442 -- are cases (e.g. with pragma Debug) where generating the checks
443 -- can cause real trouble).
284faf8b 444
84d0d4a5 445 if not Expander_Active then
13dbf220 446 return;
9dfe12ae 447 end if;
ee6ba406 448
84d0d4a5 449 -- No check if short circuiting makes check unnecessary
9dfe12ae 450
84d0d4a5 451 if not Check_Needed (P, Access_Check) then
452 return;
ee6ba406 453 end if;
9dfe12ae 454
cc60bd16 455 -- No check if accessing the Offset_To_Top component of a dispatch
456 -- table. They are safe by construction.
457
040277b1 458 if Tagged_Type_Expansion
459 and then Present (Etype (P))
cc60bd16 460 and then RTU_Loaded (Ada_Tags)
461 and then RTE_Available (RE_Offset_To_Top_Ptr)
462 and then Etype (P) = RTE (RE_Offset_To_Top_Ptr)
463 then
464 return;
465 end if;
466
84d0d4a5 467 -- Otherwise go ahead and install the check
9dfe12ae 468
fa7497e8 469 Install_Null_Excluding_Check (P);
ee6ba406 470 end Apply_Access_Check;
471
472 -------------------------------
473 -- Apply_Accessibility_Check --
474 -------------------------------
475
55dc6dc2 476 procedure Apply_Accessibility_Check
477 (N : Node_Id;
478 Typ : Entity_Id;
479 Insert_Node : Node_Id)
480 is
ee6ba406 481 Loc : constant Source_Ptr := Sloc (N);
482 Param_Ent : constant Entity_Id := Param_Entity (N);
483 Param_Level : Node_Id;
484 Type_Level : Node_Id;
485
486 begin
487 if Inside_A_Generic then
488 return;
489
6ffc64fc 490 -- Only apply the run-time check if the access parameter has an
491 -- associated extra access level parameter and when the level of the
492 -- type is less deep than the level of the access parameter, and
493 -- accessibility checks are not suppressed.
ee6ba406 494
495 elsif Present (Param_Ent)
496 and then Present (Extra_Accessibility (Param_Ent))
6ffc64fc 497 and then UI_Gt (Object_Access_Level (N), Type_Access_Level (Typ))
ee6ba406 498 and then not Accessibility_Checks_Suppressed (Param_Ent)
499 and then not Accessibility_Checks_Suppressed (Typ)
500 then
501 Param_Level :=
502 New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
503
504 Type_Level :=
505 Make_Integer_Literal (Loc, Type_Access_Level (Typ));
506
bf3e1520 507 -- Raise Program_Error if the accessibility level of the access
84d0d4a5 508 -- parameter is deeper than the level of the target access type.
ee6ba406 509
55dc6dc2 510 Insert_Action (Insert_Node,
ee6ba406 511 Make_Raise_Program_Error (Loc,
512 Condition =>
513 Make_Op_Gt (Loc,
514 Left_Opnd => Param_Level,
f15731c4 515 Right_Opnd => Type_Level),
516 Reason => PE_Accessibility_Check_Failed));
ee6ba406 517
518 Analyze_And_Resolve (N);
519 end if;
520 end Apply_Accessibility_Check;
521
0577b0b1 522 --------------------------------
523 -- Apply_Address_Clause_Check --
524 --------------------------------
525
526 procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is
527 AC : constant Node_Id := Address_Clause (E);
528 Loc : constant Source_Ptr := Sloc (AC);
529 Typ : constant Entity_Id := Etype (E);
530 Aexp : constant Node_Id := Expression (AC);
c2b56224 531
c2b56224 532 Expr : Node_Id;
0577b0b1 533 -- Address expression (not necessarily the same as Aexp, for example
534 -- when Aexp is a reference to a constant, in which case Expr gets
535 -- reset to reference the value expression of the constant.
536
0577b0b1 537 procedure Compile_Time_Bad_Alignment;
538 -- Post error warnings when alignment is known to be incompatible. Note
539 -- that we do not go as far as inserting a raise of Program_Error since
540 -- this is an erroneous case, and it may happen that we are lucky and an
d6da7448 541 -- underaligned address turns out to be OK after all.
0577b0b1 542
543 --------------------------------
544 -- Compile_Time_Bad_Alignment --
545 --------------------------------
546
547 procedure Compile_Time_Bad_Alignment is
548 begin
d6da7448 549 if Address_Clause_Overlay_Warnings then
0577b0b1 550 Error_Msg_FE
551 ("?specified address for& may be inconsistent with alignment ",
552 Aexp, E);
553 Error_Msg_FE
2af58f67 554 ("\?program execution may be erroneous (RM 13.3(27))",
0577b0b1 555 Aexp, E);
83f8f0a6 556 Set_Address_Warning_Posted (AC);
0577b0b1 557 end if;
558 end Compile_Time_Bad_Alignment;
c2b56224 559
2af58f67 560 -- Start of processing for Apply_Address_Clause_Check
5c61a0ff 561
c2b56224 562 begin
d6da7448 563 -- See if alignment check needed. Note that we never need a check if the
564 -- maximum alignment is one, since the check will always succeed.
565
566 -- Note: we do not check for checks suppressed here, since that check
567 -- was done in Sem_Ch13 when the address clause was processed. We are
568 -- only called if checks were not suppressed. The reason for this is
569 -- that we have to delay the call to Apply_Alignment_Check till freeze
570 -- time (so that all types etc are elaborated), but we have to check
571 -- the status of check suppressing at the point of the address clause.
572
573 if No (AC)
574 or else not Check_Address_Alignment (AC)
575 or else Maximum_Alignment = 1
576 then
577 return;
578 end if;
579
580 -- Obtain expression from address clause
9dfe12ae 581
0577b0b1 582 Expr := Expression (AC);
583
584 -- The following loop digs for the real expression to use in the check
585
586 loop
587 -- For constant, get constant expression
588
589 if Is_Entity_Name (Expr)
590 and then Ekind (Entity (Expr)) = E_Constant
591 then
592 Expr := Constant_Value (Entity (Expr));
593
594 -- For unchecked conversion, get result to convert
595
596 elsif Nkind (Expr) = N_Unchecked_Type_Conversion then
597 Expr := Expression (Expr);
598
599 -- For (common case) of To_Address call, get argument
600
601 elsif Nkind (Expr) = N_Function_Call
602 and then Is_Entity_Name (Name (Expr))
603 and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
604 then
605 Expr := First (Parameter_Associations (Expr));
606
607 if Nkind (Expr) = N_Parameter_Association then
608 Expr := Explicit_Actual_Parameter (Expr);
609 end if;
610
611 -- We finally have the real expression
612
613 else
614 exit;
615 end if;
616 end loop;
617
d6da7448 618 -- See if we know that Expr has a bad alignment at compile time
c2b56224 619
620 if Compile_Time_Known_Value (Expr)
f2a06be9 621 and then (Known_Alignment (E) or else Known_Alignment (Typ))
c2b56224 622 then
f2a06be9 623 declare
624 AL : Uint := Alignment (Typ);
625
626 begin
627 -- The object alignment might be more restrictive than the
628 -- type alignment.
629
630 if Known_Alignment (E) then
631 AL := Alignment (E);
632 end if;
633
634 if Expr_Value (Expr) mod AL /= 0 then
0577b0b1 635 Compile_Time_Bad_Alignment;
636 else
637 return;
f2a06be9 638 end if;
639 end;
c2b56224 640
0577b0b1 641 -- If the expression has the form X'Address, then we can find out if
642 -- the object X has an alignment that is compatible with the object E.
d6da7448 643 -- If it hasn't or we don't know, we defer issuing the warning until
644 -- the end of the compilation to take into account back end annotations.
c2b56224 645
0577b0b1 646 elsif Nkind (Expr) = N_Attribute_Reference
647 and then Attribute_Name (Expr) = Name_Address
d6da7448 648 and then Has_Compatible_Alignment (E, Prefix (Expr)) = Known_Compatible
0577b0b1 649 then
d6da7448 650 return;
0577b0b1 651 end if;
c2b56224 652
6fb3c314 653 -- Here we do not know if the value is acceptable. Strictly we don't
654 -- have to do anything, since if the alignment is bad, we have an
655 -- erroneous program. However we are allowed to check for erroneous
656 -- conditions and we decide to do this by default if the check is not
657 -- suppressed.
0577b0b1 658
659 -- However, don't do the check if elaboration code is unwanted
660
661 if Restriction_Active (No_Elaboration_Code) then
662 return;
663
664 -- Generate a check to raise PE if alignment may be inappropriate
665
666 else
667 -- If the original expression is a non-static constant, use the
668 -- name of the constant itself rather than duplicating its
00c403ee 669 -- defining expression, which was extracted above.
0577b0b1 670
00c403ee 671 -- Note: Expr is empty if the address-clause is applied to in-mode
672 -- actuals (allowed by 13.1(22)).
673
674 if not Present (Expr)
675 or else
676 (Is_Entity_Name (Expression (AC))
677 and then Ekind (Entity (Expression (AC))) = E_Constant
678 and then Nkind (Parent (Entity (Expression (AC))))
679 = N_Object_Declaration)
0577b0b1 680 then
681 Expr := New_Copy_Tree (Expression (AC));
682 else
683 Remove_Side_Effects (Expr);
c2b56224 684 end if;
c2b56224 685
0577b0b1 686 Insert_After_And_Analyze (N,
687 Make_Raise_Program_Error (Loc,
688 Condition =>
689 Make_Op_Ne (Loc,
690 Left_Opnd =>
691 Make_Op_Mod (Loc,
692 Left_Opnd =>
693 Unchecked_Convert_To
694 (RTE (RE_Integer_Address), Expr),
695 Right_Opnd =>
696 Make_Attribute_Reference (Loc,
697 Prefix => New_Occurrence_Of (E, Loc),
698 Attribute_Name => Name_Alignment)),
699 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
700 Reason => PE_Misaligned_Address_Value),
701 Suppress => All_Checks);
702 return;
703 end if;
9dfe12ae 704
705 exception
0577b0b1 706 -- If we have some missing run time component in configurable run time
707 -- mode then just skip the check (it is not required in any case).
708
9dfe12ae 709 when RE_Not_Available =>
710 return;
0577b0b1 711 end Apply_Address_Clause_Check;
c2b56224 712
ee6ba406 713 -------------------------------------
714 -- Apply_Arithmetic_Overflow_Check --
715 -------------------------------------
716
f40f9731 717 -- This routine is called only if the type is an integer type, and a
718 -- software arithmetic overflow check may be needed for op (add, subtract,
719 -- or multiply). This check is performed only if Software_Overflow_Checking
720 -- is enabled and Do_Overflow_Check is set. In this case we expand the
721 -- operation into a more complex sequence of tests that ensures that
722 -- overflow is properly caught.
ee6ba406 723
724 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
725 Loc : constant Source_Ptr := Sloc (N);
780bfb21 726 Typ : constant Entity_Id := Etype (N);
727 Rtyp : constant Entity_Id := Root_Type (Typ);
ee6ba406 728
729 begin
f40f9731 730 -- An interesting special case. If the arithmetic operation appears as
731 -- the operand of a type conversion:
732
733 -- type1 (x op y)
734
735 -- and all the following conditions apply:
736
737 -- arithmetic operation is for a signed integer type
738 -- target type type1 is a static integer subtype
739 -- range of x and y are both included in the range of type1
740 -- range of x op y is included in the range of type1
741 -- size of type1 is at least twice the result size of op
742
743 -- then we don't do an overflow check in any case, instead we transform
744 -- the operation so that we end up with:
745
746 -- type1 (type1 (x) op type1 (y))
747
748 -- This avoids intermediate overflow before the conversion. It is
749 -- explicitly permitted by RM 3.5.4(24):
750
751 -- For the execution of a predefined operation of a signed integer
752 -- type, the implementation need not raise Constraint_Error if the
753 -- result is outside the base range of the type, so long as the
754 -- correct result is produced.
755
756 -- It's hard to imagine that any programmer counts on the exception
757 -- being raised in this case, and in any case it's wrong coding to
758 -- have this expectation, given the RM permission. Furthermore, other
759 -- Ada compilers do allow such out of range results.
760
761 -- Note that we do this transformation even if overflow checking is
762 -- off, since this is precisely about giving the "right" result and
763 -- avoiding the need for an overflow check.
764
8eb4a5eb 765 -- Note: this circuit is partially redundant with respect to the similar
766 -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals
767 -- with cases that do not come through here. We still need the following
768 -- processing even with the Exp_Ch4 code in place, since we want to be
769 -- sure not to generate the arithmetic overflow check in these cases
770 -- (Exp_Ch4 would have a hard time removing them once generated).
771
f40f9731 772 if Is_Signed_Integer_Type (Typ)
773 and then Nkind (Parent (N)) = N_Type_Conversion
ee6ba406 774 then
f40f9731 775 declare
776 Target_Type : constant Entity_Id :=
777 Base_Type (Entity (Subtype_Mark (Parent (N))));
778
779 Llo, Lhi : Uint;
780 Rlo, Rhi : Uint;
781 LOK, ROK : Boolean;
782
783 Vlo : Uint;
784 Vhi : Uint;
785 VOK : Boolean;
786
787 Tlo : Uint;
788 Thi : Uint;
789
790 begin
791 if Is_Integer_Type (Target_Type)
792 and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp)
793 then
794 Tlo := Expr_Value (Type_Low_Bound (Target_Type));
795 Thi := Expr_Value (Type_High_Bound (Target_Type));
796
9c486805 797 Determine_Range
798 (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True);
799 Determine_Range
800 (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True);
f40f9731 801
802 if (LOK and ROK)
803 and then Tlo <= Llo and then Lhi <= Thi
804 and then Tlo <= Rlo and then Rhi <= Thi
805 then
9c486805 806 Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True);
f40f9731 807
808 if VOK and then Tlo <= Vlo and then Vhi <= Thi then
809 Rewrite (Left_Opnd (N),
810 Make_Type_Conversion (Loc,
811 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
812 Expression => Relocate_Node (Left_Opnd (N))));
813
814 Rewrite (Right_Opnd (N),
815 Make_Type_Conversion (Loc,
816 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
817 Expression => Relocate_Node (Right_Opnd (N))));
818
780bfb21 819 -- Rewrite the conversion operand so that the original
820 -- node is retained, in order to avoid the warning for
821 -- redundant conversions in Resolve_Type_Conversion.
822
823 Rewrite (N, Relocate_Node (N));
824
f40f9731 825 Set_Etype (N, Target_Type);
780bfb21 826
f40f9731 827 Analyze_And_Resolve (Left_Opnd (N), Target_Type);
828 Analyze_And_Resolve (Right_Opnd (N), Target_Type);
829
830 -- Given that the target type is twice the size of the
831 -- source type, overflow is now impossible, so we can
832 -- safely kill the overflow check and return.
833
834 Set_Do_Overflow_Check (N, False);
835 return;
836 end if;
837 end if;
838 end if;
839 end;
ee6ba406 840 end if;
841
f40f9731 842 -- Now see if an overflow check is required
843
844 declare
845 Siz : constant Int := UI_To_Int (Esize (Rtyp));
846 Dsiz : constant Int := Siz * 2;
847 Opnod : Node_Id;
848 Ctyp : Entity_Id;
849 Opnd : Node_Id;
850 Cent : RE_Id;
ee6ba406 851
f40f9731 852 begin
853 -- Skip check if back end does overflow checks, or the overflow flag
df40eeb0 854 -- is not set anyway, or we are not doing code expansion, or the
855 -- parent node is a type conversion whose operand is an arithmetic
856 -- operation on signed integers on which the expander can promote
bbbed24b 857 -- later the operands to type Integer (see Expand_N_Type_Conversion).
ee6ba406 858
f40f9731 859 -- Special case CLI target, where arithmetic overflow checks can be
860 -- performed for integer and long_integer
ee6ba406 861
f40f9731 862 if Backend_Overflow_Checks_On_Target
863 or else not Do_Overflow_Check (N)
864 or else not Expander_Active
df40eeb0 865 or else (Present (Parent (N))
866 and then Nkind (Parent (N)) = N_Type_Conversion
867 and then Integer_Promotion_Possible (Parent (N)))
f40f9731 868 or else
869 (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size)
870 then
871 return;
872 end if;
ee6ba406 873
f40f9731 874 -- Otherwise, generate the full general code for front end overflow
875 -- detection, which works by doing arithmetic in a larger type:
ee6ba406 876
f40f9731 877 -- x op y
ee6ba406 878
f40f9731 879 -- is expanded into
ee6ba406 880
f40f9731 881 -- Typ (Checktyp (x) op Checktyp (y));
ee6ba406 882
f40f9731 883 -- where Typ is the type of the original expression, and Checktyp is
884 -- an integer type of sufficient length to hold the largest possible
885 -- result.
ee6ba406 886
f40f9731 887 -- If the size of check type exceeds the size of Long_Long_Integer,
888 -- we use a different approach, expanding to:
ee6ba406 889
f40f9731 890 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
ee6ba406 891
f40f9731 892 -- where xxx is Add, Multiply or Subtract as appropriate
ee6ba406 893
f40f9731 894 -- Find check type if one exists
895
896 if Dsiz <= Standard_Integer_Size then
897 Ctyp := Standard_Integer;
ee6ba406 898
f40f9731 899 elsif Dsiz <= Standard_Long_Long_Integer_Size then
900 Ctyp := Standard_Long_Long_Integer;
901
902 -- No check type exists, use runtime call
ee6ba406 903
904 else
f40f9731 905 if Nkind (N) = N_Op_Add then
906 Cent := RE_Add_With_Ovflo_Check;
ee6ba406 907
f40f9731 908 elsif Nkind (N) = N_Op_Multiply then
909 Cent := RE_Multiply_With_Ovflo_Check;
ee6ba406 910
f40f9731 911 else
912 pragma Assert (Nkind (N) = N_Op_Subtract);
913 Cent := RE_Subtract_With_Ovflo_Check;
914 end if;
915
916 Rewrite (N,
917 OK_Convert_To (Typ,
918 Make_Function_Call (Loc,
919 Name => New_Reference_To (RTE (Cent), Loc),
920 Parameter_Associations => New_List (
921 OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
922 OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
ee6ba406 923
f40f9731 924 Analyze_And_Resolve (N, Typ);
925 return;
926 end if;
ee6ba406 927
f40f9731 928 -- If we fall through, we have the case where we do the arithmetic
929 -- in the next higher type and get the check by conversion. In these
930 -- cases Ctyp is set to the type to be used as the check type.
ee6ba406 931
f40f9731 932 Opnod := Relocate_Node (N);
ee6ba406 933
f40f9731 934 Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
ee6ba406 935
f40f9731 936 Analyze (Opnd);
937 Set_Etype (Opnd, Ctyp);
938 Set_Analyzed (Opnd, True);
939 Set_Left_Opnd (Opnod, Opnd);
ee6ba406 940
f40f9731 941 Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
ee6ba406 942
f40f9731 943 Analyze (Opnd);
944 Set_Etype (Opnd, Ctyp);
945 Set_Analyzed (Opnd, True);
946 Set_Right_Opnd (Opnod, Opnd);
ee6ba406 947
f40f9731 948 -- The type of the operation changes to the base type of the check
949 -- type, and we reset the overflow check indication, since clearly no
950 -- overflow is possible now that we are using a double length type.
951 -- We also set the Analyzed flag to avoid a recursive attempt to
952 -- expand the node.
ee6ba406 953
f40f9731 954 Set_Etype (Opnod, Base_Type (Ctyp));
955 Set_Do_Overflow_Check (Opnod, False);
956 Set_Analyzed (Opnod, True);
ee6ba406 957
f40f9731 958 -- Now build the outer conversion
ee6ba406 959
f40f9731 960 Opnd := OK_Convert_To (Typ, Opnod);
961 Analyze (Opnd);
962 Set_Etype (Opnd, Typ);
9dfe12ae 963
f40f9731 964 -- In the discrete type case, we directly generate the range check
965 -- for the outer operand. This range check will implement the
966 -- required overflow check.
9dfe12ae 967
f40f9731 968 if Is_Discrete_Type (Typ) then
969 Rewrite (N, Opnd);
970 Generate_Range_Check
971 (Expression (N), Typ, CE_Overflow_Check_Failed);
9dfe12ae 972
f40f9731 973 -- For other types, we enable overflow checking on the conversion,
974 -- after setting the node as analyzed to prevent recursive attempts
975 -- to expand the conversion node.
9dfe12ae 976
f40f9731 977 else
978 Set_Analyzed (Opnd, True);
979 Enable_Overflow_Check (Opnd);
980 Rewrite (N, Opnd);
981 end if;
982
983 exception
984 when RE_Not_Available =>
985 return;
986 end;
ee6ba406 987 end Apply_Arithmetic_Overflow_Check;
988
ee6ba406 989 ----------------------------
990 -- Apply_Constraint_Check --
991 ----------------------------
992
993 procedure Apply_Constraint_Check
994 (N : Node_Id;
995 Typ : Entity_Id;
996 No_Sliding : Boolean := False)
997 is
998 Desig_Typ : Entity_Id;
999
1000 begin
7aafae1c 1001 -- No checks inside a generic (check the instantiations)
1002
ee6ba406 1003 if Inside_A_Generic then
1004 return;
7aafae1c 1005 end if;
ee6ba406 1006
6fb3c314 1007 -- Apply required constraint checks
7aafae1c 1008
1009 if Is_Scalar_Type (Typ) then
ee6ba406 1010 Apply_Scalar_Range_Check (N, Typ);
1011
1012 elsif Is_Array_Type (Typ) then
1013
05fcfafb 1014 -- A useful optimization: an aggregate with only an others clause
5f260d20 1015 -- always has the right bounds.
1016
1017 if Nkind (N) = N_Aggregate
1018 and then No (Expressions (N))
1019 and then Nkind
1020 (First (Choices (First (Component_Associations (N)))))
1021 = N_Others_Choice
1022 then
1023 return;
1024 end if;
1025
ee6ba406 1026 if Is_Constrained (Typ) then
1027 Apply_Length_Check (N, Typ);
1028
1029 if No_Sliding then
1030 Apply_Range_Check (N, Typ);
1031 end if;
1032 else
1033 Apply_Range_Check (N, Typ);
1034 end if;
1035
1036 elsif (Is_Record_Type (Typ)
1037 or else Is_Private_Type (Typ))
1038 and then Has_Discriminants (Base_Type (Typ))
1039 and then Is_Constrained (Typ)
1040 then
1041 Apply_Discriminant_Check (N, Typ);
1042
1043 elsif Is_Access_Type (Typ) then
1044
1045 Desig_Typ := Designated_Type (Typ);
1046
1047 -- No checks necessary if expression statically null
1048
2af58f67 1049 if Known_Null (N) then
00c403ee 1050 if Can_Never_Be_Null (Typ) then
1051 Install_Null_Excluding_Check (N);
1052 end if;
ee6ba406 1053
1054 -- No sliding possible on access to arrays
1055
1056 elsif Is_Array_Type (Desig_Typ) then
1057 if Is_Constrained (Desig_Typ) then
1058 Apply_Length_Check (N, Typ);
1059 end if;
1060
1061 Apply_Range_Check (N, Typ);
1062
1063 elsif Has_Discriminants (Base_Type (Desig_Typ))
1064 and then Is_Constrained (Desig_Typ)
1065 then
1066 Apply_Discriminant_Check (N, Typ);
1067 end if;
fa7497e8 1068
bf3e1520 1069 -- Apply the 2005 Null_Excluding check. Note that we do not apply
00c403ee 1070 -- this check if the constraint node is illegal, as shown by having
1071 -- an error posted. This additional guard prevents cascaded errors
1072 -- and compiler aborts on illegal programs involving Ada 2005 checks.
1073
fa7497e8 1074 if Can_Never_Be_Null (Typ)
1075 and then not Can_Never_Be_Null (Etype (N))
00c403ee 1076 and then not Error_Posted (N)
fa7497e8 1077 then
1078 Install_Null_Excluding_Check (N);
1079 end if;
ee6ba406 1080 end if;
1081 end Apply_Constraint_Check;
1082
1083 ------------------------------
1084 -- Apply_Discriminant_Check --
1085 ------------------------------
1086
1087 procedure Apply_Discriminant_Check
1088 (N : Node_Id;
1089 Typ : Entity_Id;
1090 Lhs : Node_Id := Empty)
1091 is
1092 Loc : constant Source_Ptr := Sloc (N);
1093 Do_Access : constant Boolean := Is_Access_Type (Typ);
1094 S_Typ : Entity_Id := Etype (N);
1095 Cond : Node_Id;
1096 T_Typ : Entity_Id;
1097
7be5088a 1098 function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean;
1099 -- A heap object with an indefinite subtype is constrained by its
1100 -- initial value, and assigning to it requires a constraint_check.
1101 -- The target may be an explicit dereference, or a renaming of one.
1102
ee6ba406 1103 function Is_Aliased_Unconstrained_Component return Boolean;
1104 -- It is possible for an aliased component to have a nominal
1105 -- unconstrained subtype (through instantiation). If this is a
1106 -- discriminated component assigned in the expansion of an aggregate
1107 -- in an initialization, the check must be suppressed. This unusual
2af58f67 1108 -- situation requires a predicate of its own.
ee6ba406 1109
7be5088a 1110 ----------------------------------
1111 -- Denotes_Explicit_Dereference --
1112 ----------------------------------
1113
1114 function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is
1115 begin
1116 return
1117 Nkind (Obj) = N_Explicit_Dereference
1118 or else
1119 (Is_Entity_Name (Obj)
1120 and then Present (Renamed_Object (Entity (Obj)))
9474aa9c 1121 and then Nkind (Renamed_Object (Entity (Obj))) =
1122 N_Explicit_Dereference);
7be5088a 1123 end Denotes_Explicit_Dereference;
1124
ee6ba406 1125 ----------------------------------------
1126 -- Is_Aliased_Unconstrained_Component --
1127 ----------------------------------------
1128
1129 function Is_Aliased_Unconstrained_Component return Boolean is
1130 Comp : Entity_Id;
1131 Pref : Node_Id;
1132
1133 begin
1134 if Nkind (Lhs) /= N_Selected_Component then
1135 return False;
1136 else
1137 Comp := Entity (Selector_Name (Lhs));
1138 Pref := Prefix (Lhs);
1139 end if;
1140
1141 if Ekind (Comp) /= E_Component
1142 or else not Is_Aliased (Comp)
1143 then
1144 return False;
1145 end if;
1146
1147 return not Comes_From_Source (Pref)
1148 and then In_Instance
1149 and then not Is_Constrained (Etype (Comp));
1150 end Is_Aliased_Unconstrained_Component;
1151
1152 -- Start of processing for Apply_Discriminant_Check
1153
1154 begin
1155 if Do_Access then
1156 T_Typ := Designated_Type (Typ);
1157 else
1158 T_Typ := Typ;
1159 end if;
1160
1161 -- Nothing to do if discriminant checks are suppressed or else no code
1162 -- is to be generated
1163
1164 if not Expander_Active
1165 or else Discriminant_Checks_Suppressed (T_Typ)
1166 then
1167 return;
1168 end if;
1169
feff2f05 1170 -- No discriminant checks necessary for an access when expression is
1171 -- statically Null. This is not only an optimization, it is fundamental
1172 -- because otherwise discriminant checks may be generated in init procs
1173 -- for types containing an access to a not-yet-frozen record, causing a
1174 -- deadly forward reference.
ee6ba406 1175
feff2f05 1176 -- Also, if the expression is of an access type whose designated type is
1177 -- incomplete, then the access value must be null and we suppress the
1178 -- check.
ee6ba406 1179
2af58f67 1180 if Known_Null (N) then
ee6ba406 1181 return;
1182
1183 elsif Is_Access_Type (S_Typ) then
1184 S_Typ := Designated_Type (S_Typ);
1185
1186 if Ekind (S_Typ) = E_Incomplete_Type then
1187 return;
1188 end if;
1189 end if;
1190
0577b0b1 1191 -- If an assignment target is present, then we need to generate the
1192 -- actual subtype if the target is a parameter or aliased object with
1193 -- an unconstrained nominal subtype.
1194
1195 -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual
1196 -- subtype to the parameter and dereference cases, since other aliased
1197 -- objects are unconstrained (unless the nominal subtype is explicitly
7be5088a 1198 -- constrained).
ee6ba406 1199
1200 if Present (Lhs)
1201 and then (Present (Param_Entity (Lhs))
de54c5ab 1202 or else (Ada_Version < Ada_2005
0577b0b1 1203 and then not Is_Constrained (T_Typ)
ee6ba406 1204 and then Is_Aliased_View (Lhs)
0577b0b1 1205 and then not Is_Aliased_Unconstrained_Component)
de54c5ab 1206 or else (Ada_Version >= Ada_2005
0577b0b1 1207 and then not Is_Constrained (T_Typ)
7be5088a 1208 and then Denotes_Explicit_Dereference (Lhs)
0577b0b1 1209 and then Nkind (Original_Node (Lhs)) /=
1210 N_Function_Call))
ee6ba406 1211 then
1212 T_Typ := Get_Actual_Subtype (Lhs);
1213 end if;
1214
feff2f05 1215 -- Nothing to do if the type is unconstrained (this is the case where
1216 -- the actual subtype in the RM sense of N is unconstrained and no check
1217 -- is required).
ee6ba406 1218
1219 if not Is_Constrained (T_Typ) then
1220 return;
05fcfafb 1221
1222 -- Ada 2005: nothing to do if the type is one for which there is a
1223 -- partial view that is constrained.
1224
de54c5ab 1225 elsif Ada_Version >= Ada_2005
05fcfafb 1226 and then Has_Constrained_Partial_View (Base_Type (T_Typ))
1227 then
1228 return;
ee6ba406 1229 end if;
1230
00f91aef 1231 -- Nothing to do if the type is an Unchecked_Union
1232
1233 if Is_Unchecked_Union (Base_Type (T_Typ)) then
1234 return;
1235 end if;
1236
feff2f05 1237 -- Suppress checks if the subtypes are the same. the check must be
1238 -- preserved in an assignment to a formal, because the constraint is
1239 -- given by the actual.
ee6ba406 1240
1241 if Nkind (Original_Node (N)) /= N_Allocator
1242 and then (No (Lhs)
1243 or else not Is_Entity_Name (Lhs)
9dfe12ae 1244 or else No (Param_Entity (Lhs)))
ee6ba406 1245 then
1246 if (Etype (N) = Typ
1247 or else (Do_Access and then Designated_Type (Typ) = S_Typ))
1248 and then not Is_Aliased_View (Lhs)
1249 then
1250 return;
1251 end if;
1252
feff2f05 1253 -- We can also eliminate checks on allocators with a subtype mark that
1254 -- coincides with the context type. The context type may be a subtype
1255 -- without a constraint (common case, a generic actual).
ee6ba406 1256
1257 elsif Nkind (Original_Node (N)) = N_Allocator
1258 and then Is_Entity_Name (Expression (Original_Node (N)))
1259 then
1260 declare
9dfe12ae 1261 Alloc_Typ : constant Entity_Id :=
1262 Entity (Expression (Original_Node (N)));
ee6ba406 1263
1264 begin
1265 if Alloc_Typ = T_Typ
1266 or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
1267 and then Is_Entity_Name (
1268 Subtype_Indication (Parent (T_Typ)))
1269 and then Alloc_Typ = Base_Type (T_Typ))
1270
1271 then
1272 return;
1273 end if;
1274 end;
1275 end if;
1276
feff2f05 1277 -- See if we have a case where the types are both constrained, and all
1278 -- the constraints are constants. In this case, we can do the check
1279 -- successfully at compile time.
ee6ba406 1280
9dfe12ae 1281 -- We skip this check for the case where the node is a rewritten`
ee6ba406 1282 -- allocator, because it already carries the context subtype, and
1283 -- extracting the discriminants from the aggregate is messy.
1284
1285 if Is_Constrained (S_Typ)
1286 and then Nkind (Original_Node (N)) /= N_Allocator
1287 then
1288 declare
1289 DconT : Elmt_Id;
1290 Discr : Entity_Id;
1291 DconS : Elmt_Id;
1292 ItemS : Node_Id;
1293 ItemT : Node_Id;
1294
1295 begin
1296 -- S_Typ may not have discriminants in the case where it is a
feff2f05 1297 -- private type completed by a default discriminated type. In that
1298 -- case, we need to get the constraints from the underlying_type.
1299 -- If the underlying type is unconstrained (i.e. has no default
1300 -- discriminants) no check is needed.
ee6ba406 1301
1302 if Has_Discriminants (S_Typ) then
1303 Discr := First_Discriminant (S_Typ);
1304 DconS := First_Elmt (Discriminant_Constraint (S_Typ));
1305
1306 else
1307 Discr := First_Discriminant (Underlying_Type (S_Typ));
1308 DconS :=
1309 First_Elmt
1310 (Discriminant_Constraint (Underlying_Type (S_Typ)));
1311
1312 if No (DconS) then
1313 return;
1314 end if;
fccb5da7 1315
1316 -- A further optimization: if T_Typ is derived from S_Typ
1317 -- without imposing a constraint, no check is needed.
1318
1319 if Nkind (Original_Node (Parent (T_Typ))) =
1320 N_Full_Type_Declaration
1321 then
1322 declare
5c61a0ff 1323 Type_Def : constant Node_Id :=
fccb5da7 1324 Type_Definition
1325 (Original_Node (Parent (T_Typ)));
1326 begin
1327 if Nkind (Type_Def) = N_Derived_Type_Definition
1328 and then Is_Entity_Name (Subtype_Indication (Type_Def))
1329 and then Entity (Subtype_Indication (Type_Def)) = S_Typ
1330 then
1331 return;
1332 end if;
1333 end;
1334 end if;
ee6ba406 1335 end if;
1336
1337 DconT := First_Elmt (Discriminant_Constraint (T_Typ));
1338
1339 while Present (Discr) loop
1340 ItemS := Node (DconS);
1341 ItemT := Node (DconT);
1342
00c403ee 1343 -- For a discriminated component type constrained by the
1344 -- current instance of an enclosing type, there is no
1345 -- applicable discriminant check.
1346
1347 if Nkind (ItemT) = N_Attribute_Reference
1348 and then Is_Access_Type (Etype (ItemT))
1349 and then Is_Entity_Name (Prefix (ItemT))
1350 and then Is_Type (Entity (Prefix (ItemT)))
1351 then
1352 return;
1353 end if;
1354
cc60bd16 1355 -- If the expressions for the discriminants are identical
1356 -- and it is side-effect free (for now just an entity),
1357 -- this may be a shared constraint, e.g. from a subtype
1358 -- without a constraint introduced as a generic actual.
1359 -- Examine other discriminants if any.
1360
1361 if ItemS = ItemT
1362 and then Is_Entity_Name (ItemS)
1363 then
1364 null;
1365
1366 elsif not Is_OK_Static_Expression (ItemS)
1367 or else not Is_OK_Static_Expression (ItemT)
1368 then
1369 exit;
ee6ba406 1370
cc60bd16 1371 elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then
ee6ba406 1372 if Do_Access then -- needs run-time check.
1373 exit;
1374 else
1375 Apply_Compile_Time_Constraint_Error
f15731c4 1376 (N, "incorrect value for discriminant&?",
1377 CE_Discriminant_Check_Failed, Ent => Discr);
ee6ba406 1378 return;
1379 end if;
1380 end if;
1381
1382 Next_Elmt (DconS);
1383 Next_Elmt (DconT);
1384 Next_Discriminant (Discr);
1385 end loop;
1386
1387 if No (Discr) then
1388 return;
1389 end if;
1390 end;
1391 end if;
1392
1393 -- Here we need a discriminant check. First build the expression
1394 -- for the comparisons of the discriminants:
1395
1396 -- (n.disc1 /= typ.disc1) or else
1397 -- (n.disc2 /= typ.disc2) or else
1398 -- ...
1399 -- (n.discn /= typ.discn)
1400
1401 Cond := Build_Discriminant_Checks (N, T_Typ);
1402
1403 -- If Lhs is set and is a parameter, then the condition is
1404 -- guarded by: lhs'constrained and then (condition built above)
1405
1406 if Present (Param_Entity (Lhs)) then
1407 Cond :=
1408 Make_And_Then (Loc,
1409 Left_Opnd =>
1410 Make_Attribute_Reference (Loc,
1411 Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
1412 Attribute_Name => Name_Constrained),
1413 Right_Opnd => Cond);
1414 end if;
1415
1416 if Do_Access then
1417 Cond := Guard_Access (Cond, Loc, N);
1418 end if;
1419
1420 Insert_Action (N,
f15731c4 1421 Make_Raise_Constraint_Error (Loc,
1422 Condition => Cond,
1423 Reason => CE_Discriminant_Check_Failed));
ee6ba406 1424 end Apply_Discriminant_Check;
1425
1426 ------------------------
1427 -- Apply_Divide_Check --
1428 ------------------------
1429
1430 procedure Apply_Divide_Check (N : Node_Id) is
1431 Loc : constant Source_Ptr := Sloc (N);
1432 Typ : constant Entity_Id := Etype (N);
1433 Left : constant Node_Id := Left_Opnd (N);
1434 Right : constant Node_Id := Right_Opnd (N);
1435
1436 LLB : Uint;
1437 Llo : Uint;
1438 Lhi : Uint;
1439 LOK : Boolean;
1440 Rlo : Uint;
1441 Rhi : Uint;
96da3284 1442 ROK : Boolean;
1443
1444 pragma Warnings (Off, Lhi);
1445 -- Don't actually use this value
ee6ba406 1446
1447 begin
1448 if Expander_Active
13dbf220 1449 and then not Backend_Divide_Checks_On_Target
1450 and then Check_Needed (Right, Division_Check)
ee6ba406 1451 then
9c486805 1452 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
ee6ba406 1453
1454 -- See if division by zero possible, and if so generate test. This
1455 -- part of the test is not controlled by the -gnato switch.
1456
1457 if Do_Division_Check (N) then
ee6ba406 1458 if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
1459 Insert_Action (N,
1460 Make_Raise_Constraint_Error (Loc,
1461 Condition =>
1462 Make_Op_Eq (Loc,
0577b0b1 1463 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
f15731c4 1464 Right_Opnd => Make_Integer_Literal (Loc, 0)),
1465 Reason => CE_Divide_By_Zero));
ee6ba406 1466 end if;
1467 end if;
1468
1469 -- Test for extremely annoying case of xxx'First divided by -1
1470
1471 if Do_Overflow_Check (N) then
ee6ba406 1472 if Nkind (N) = N_Op_Divide
1473 and then Is_Signed_Integer_Type (Typ)
1474 then
9c486805 1475 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
ee6ba406 1476 LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
1477
1478 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
1479 and then
1480 ((not LOK) or else (Llo = LLB))
1481 then
1482 Insert_Action (N,
1483 Make_Raise_Constraint_Error (Loc,
1484 Condition =>
1485 Make_And_Then (Loc,
1486
1487 Make_Op_Eq (Loc,
9dfe12ae 1488 Left_Opnd =>
1489 Duplicate_Subexpr_Move_Checks (Left),
ee6ba406 1490 Right_Opnd => Make_Integer_Literal (Loc, LLB)),
1491
1492 Make_Op_Eq (Loc,
9dfe12ae 1493 Left_Opnd =>
1494 Duplicate_Subexpr (Right),
ee6ba406 1495 Right_Opnd =>
f15731c4 1496 Make_Integer_Literal (Loc, -1))),
1497 Reason => CE_Overflow_Check_Failed));
ee6ba406 1498 end if;
1499 end if;
1500 end if;
1501 end if;
1502 end Apply_Divide_Check;
1503
5329ca64 1504 ----------------------------------
1505 -- Apply_Float_Conversion_Check --
1506 ----------------------------------
1507
feff2f05 1508 -- Let F and I be the source and target types of the conversion. The RM
1509 -- specifies that a floating-point value X is rounded to the nearest
1510 -- integer, with halfway cases being rounded away from zero. The rounded
1511 -- value of X is checked against I'Range.
1512
1513 -- The catch in the above paragraph is that there is no good way to know
1514 -- whether the round-to-integer operation resulted in overflow. A remedy is
1515 -- to perform a range check in the floating-point domain instead, however:
5329ca64 1516
5329ca64 1517 -- (1) The bounds may not be known at compile time
2af58f67 1518 -- (2) The check must take into account rounding or truncation.
5329ca64 1519 -- (3) The range of type I may not be exactly representable in F.
2af58f67 1520 -- (4) For the rounding case, The end-points I'First - 0.5 and
1521 -- I'Last + 0.5 may or may not be in range, depending on the
1522 -- sign of I'First and I'Last.
5329ca64 1523 -- (5) X may be a NaN, which will fail any comparison
1524
2af58f67 1525 -- The following steps correctly convert X with rounding:
feff2f05 1526
5329ca64 1527 -- (1) If either I'First or I'Last is not known at compile time, use
1528 -- I'Base instead of I in the next three steps and perform a
1529 -- regular range check against I'Range after conversion.
1530 -- (2) If I'First - 0.5 is representable in F then let Lo be that
1531 -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be
2af58f67 1532 -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First).
1533 -- In other words, take one of the closest floating-point numbers
1534 -- (which is an integer value) to I'First, and see if it is in
1535 -- range or not.
5329ca64 1536 -- (3) If I'Last + 0.5 is representable in F then let Hi be that value
1537 -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be
2af58f67 1538 -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last).
5329ca64 1539 -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo)
1540 -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi)
1541
2af58f67 1542 -- For the truncating case, replace steps (2) and (3) as follows:
1543 -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK
1544 -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let
1545 -- Lo_OK be True.
1546 -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK
1547 -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let
141d591a 1548 -- Hi_OK be True.
2af58f67 1549
5329ca64 1550 procedure Apply_Float_Conversion_Check
1551 (Ck_Node : Node_Id;
1552 Target_Typ : Entity_Id)
1553 is
feff2f05 1554 LB : constant Node_Id := Type_Low_Bound (Target_Typ);
1555 HB : constant Node_Id := Type_High_Bound (Target_Typ);
5329ca64 1556 Loc : constant Source_Ptr := Sloc (Ck_Node);
1557 Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node));
feff2f05 1558 Target_Base : constant Entity_Id :=
1559 Implementation_Base_Type (Target_Typ);
1560
2af58f67 1561 Par : constant Node_Id := Parent (Ck_Node);
1562 pragma Assert (Nkind (Par) = N_Type_Conversion);
1563 -- Parent of check node, must be a type conversion
1564
1565 Truncate : constant Boolean := Float_Truncate (Par);
1566 Max_Bound : constant Uint :=
1567 UI_Expon
e8548746 1568 (Machine_Radix_Value (Expr_Type),
1569 Machine_Mantissa_Value (Expr_Type) - 1) - 1;
2af58f67 1570
5329ca64 1571 -- Largest bound, so bound plus or minus half is a machine number of F
1572
feff2f05 1573 Ifirst, Ilast : Uint;
1574 -- Bounds of integer type
1575
1576 Lo, Hi : Ureal;
1577 -- Bounds to check in floating-point domain
5329ca64 1578
feff2f05 1579 Lo_OK, Hi_OK : Boolean;
1580 -- True iff Lo resp. Hi belongs to I'Range
5329ca64 1581
feff2f05 1582 Lo_Chk, Hi_Chk : Node_Id;
1583 -- Expressions that are False iff check fails
1584
1585 Reason : RT_Exception_Code;
5329ca64 1586
1587 begin
1588 if not Compile_Time_Known_Value (LB)
1589 or not Compile_Time_Known_Value (HB)
1590 then
1591 declare
feff2f05 1592 -- First check that the value falls in the range of the base type,
1593 -- to prevent overflow during conversion and then perform a
1594 -- regular range check against the (dynamic) bounds.
5329ca64 1595
5329ca64 1596 pragma Assert (Target_Base /= Target_Typ);
5329ca64 1597
46eb6933 1598 Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par);
5329ca64 1599
1600 begin
1601 Apply_Float_Conversion_Check (Ck_Node, Target_Base);
1602 Set_Etype (Temp, Target_Base);
1603
1604 Insert_Action (Parent (Par),
1605 Make_Object_Declaration (Loc,
1606 Defining_Identifier => Temp,
1607 Object_Definition => New_Occurrence_Of (Target_Typ, Loc),
1608 Expression => New_Copy_Tree (Par)),
1609 Suppress => All_Checks);
1610
1611 Insert_Action (Par,
1612 Make_Raise_Constraint_Error (Loc,
1613 Condition =>
1614 Make_Not_In (Loc,
1615 Left_Opnd => New_Occurrence_Of (Temp, Loc),
1616 Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)),
1617 Reason => CE_Range_Check_Failed));
1618 Rewrite (Par, New_Occurrence_Of (Temp, Loc));
1619
1620 return;
1621 end;
1622 end if;
1623
7d86aa98 1624 -- Get the (static) bounds of the target type
5329ca64 1625
1626 Ifirst := Expr_Value (LB);
1627 Ilast := Expr_Value (HB);
1628
7d86aa98 1629 -- A simple optimization: if the expression is a universal literal,
1630 -- we can do the comparison with the bounds and the conversion to
1631 -- an integer type statically. The range checks are unchanged.
1632
1633 if Nkind (Ck_Node) = N_Real_Literal
1634 and then Etype (Ck_Node) = Universal_Real
1635 and then Is_Integer_Type (Target_Typ)
1636 and then Nkind (Parent (Ck_Node)) = N_Type_Conversion
1637 then
1638 declare
1639 Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node));
1640
1641 begin
1642 if Int_Val <= Ilast and then Int_Val >= Ifirst then
1643
4309515d 1644 -- Conversion is safe
7d86aa98 1645
1646 Rewrite (Parent (Ck_Node),
1647 Make_Integer_Literal (Loc, UI_To_Int (Int_Val)));
1648 Analyze_And_Resolve (Parent (Ck_Node), Target_Typ);
1649 return;
1650 end if;
1651 end;
1652 end if;
1653
5329ca64 1654 -- Check against lower bound
1655
2af58f67 1656 if Truncate and then Ifirst > 0 then
1657 Lo := Pred (Expr_Type, UR_From_Uint (Ifirst));
1658 Lo_OK := False;
1659
1660 elsif Truncate then
1661 Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1));
1662 Lo_OK := True;
1663
1664 elsif abs (Ifirst) < Max_Bound then
5329ca64 1665 Lo := UR_From_Uint (Ifirst) - Ureal_Half;
1666 Lo_OK := (Ifirst > 0);
2af58f67 1667
5329ca64 1668 else
1669 Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node);
1670 Lo_OK := (Lo >= UR_From_Uint (Ifirst));
1671 end if;
1672
1673 if Lo_OK then
1674
1675 -- Lo_Chk := (X >= Lo)
1676
1677 Lo_Chk := Make_Op_Ge (Loc,
1678 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
1679 Right_Opnd => Make_Real_Literal (Loc, Lo));
1680
1681 else
1682 -- Lo_Chk := (X > Lo)
1683
1684 Lo_Chk := Make_Op_Gt (Loc,
1685 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
1686 Right_Opnd => Make_Real_Literal (Loc, Lo));
1687 end if;
1688
1689 -- Check against higher bound
1690
2af58f67 1691 if Truncate and then Ilast < 0 then
1692 Hi := Succ (Expr_Type, UR_From_Uint (Ilast));
b2c42753 1693 Hi_OK := False;
2af58f67 1694
1695 elsif Truncate then
1696 Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1));
1697 Hi_OK := True;
1698
1699 elsif abs (Ilast) < Max_Bound then
5329ca64 1700 Hi := UR_From_Uint (Ilast) + Ureal_Half;
1701 Hi_OK := (Ilast < 0);
1702 else
1703 Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node);
1704 Hi_OK := (Hi <= UR_From_Uint (Ilast));
1705 end if;
1706
1707 if Hi_OK then
1708
1709 -- Hi_Chk := (X <= Hi)
1710
1711 Hi_Chk := Make_Op_Le (Loc,
1712 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
1713 Right_Opnd => Make_Real_Literal (Loc, Hi));
1714
1715 else
1716 -- Hi_Chk := (X < Hi)
1717
1718 Hi_Chk := Make_Op_Lt (Loc,
1719 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
1720 Right_Opnd => Make_Real_Literal (Loc, Hi));
1721 end if;
1722
feff2f05 1723 -- If the bounds of the target type are the same as those of the base
1724 -- type, the check is an overflow check as a range check is not
1725 -- performed in these cases.
5329ca64 1726
1727 if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst
1728 and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast
1729 then
1730 Reason := CE_Overflow_Check_Failed;
1731 else
1732 Reason := CE_Range_Check_Failed;
1733 end if;
1734
1735 -- Raise CE if either conditions does not hold
1736
1737 Insert_Action (Ck_Node,
1738 Make_Raise_Constraint_Error (Loc,
05fcfafb 1739 Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)),
5329ca64 1740 Reason => Reason));
1741 end Apply_Float_Conversion_Check;
1742
ee6ba406 1743 ------------------------
1744 -- Apply_Length_Check --
1745 ------------------------
1746
1747 procedure Apply_Length_Check
1748 (Ck_Node : Node_Id;
1749 Target_Typ : Entity_Id;
1750 Source_Typ : Entity_Id := Empty)
1751 is
1752 begin
1753 Apply_Selected_Length_Checks
1754 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1755 end Apply_Length_Check;
1756
7aafae1c 1757 ---------------------------
1758 -- Apply_Predicate_Check --
1759 ---------------------------
1760
1761 procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id) is
1762 begin
701d57a4 1763 if Present (Predicate_Function (Typ)) then
7aafae1c 1764 Insert_Action (N,
1765 Make_Predicate_Check (Typ, Duplicate_Subexpr (N)));
1766 end if;
1767 end Apply_Predicate_Check;
1768
ee6ba406 1769 -----------------------
1770 -- Apply_Range_Check --
1771 -----------------------
1772
1773 procedure Apply_Range_Check
1774 (Ck_Node : Node_Id;
1775 Target_Typ : Entity_Id;
1776 Source_Typ : Entity_Id := Empty)
1777 is
1778 begin
1779 Apply_Selected_Range_Checks
1780 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1781 end Apply_Range_Check;
1782
1783 ------------------------------
1784 -- Apply_Scalar_Range_Check --
1785 ------------------------------
1786
feff2f05 1787 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag
1788 -- off if it is already set on.
ee6ba406 1789
1790 procedure Apply_Scalar_Range_Check
1791 (Expr : Node_Id;
1792 Target_Typ : Entity_Id;
1793 Source_Typ : Entity_Id := Empty;
1794 Fixed_Int : Boolean := False)
1795 is
1796 Parnt : constant Node_Id := Parent (Expr);
1797 S_Typ : Entity_Id;
1798 Arr : Node_Id := Empty; -- initialize to prevent warning
1799 Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning
1800 OK : Boolean;
1801
1802 Is_Subscr_Ref : Boolean;
1803 -- Set true if Expr is a subscript
1804
1805 Is_Unconstrained_Subscr_Ref : Boolean;
1806 -- Set true if Expr is a subscript of an unconstrained array. In this
1807 -- case we do not attempt to do an analysis of the value against the
1808 -- range of the subscript, since we don't know the actual subtype.
1809
1810 Int_Real : Boolean;
feff2f05 1811 -- Set to True if Expr should be regarded as a real value even though
1812 -- the type of Expr might be discrete.
ee6ba406 1813
1814 procedure Bad_Value;
1815 -- Procedure called if value is determined to be out of range
1816
9dfe12ae 1817 ---------------
1818 -- Bad_Value --
1819 ---------------
1820
ee6ba406 1821 procedure Bad_Value is
1822 begin
1823 Apply_Compile_Time_Constraint_Error
f15731c4 1824 (Expr, "value not in range of}?", CE_Range_Check_Failed,
ee6ba406 1825 Ent => Target_Typ,
1826 Typ => Target_Typ);
1827 end Bad_Value;
1828
9dfe12ae 1829 -- Start of processing for Apply_Scalar_Range_Check
1830
ee6ba406 1831 begin
2af58f67 1832 -- Return if check obviously not needed
ee6ba406 1833
2af58f67 1834 if
1835 -- Not needed inside generic
ee6ba406 1836
2af58f67 1837 Inside_A_Generic
1838
1839 -- Not needed if previous error
1840
1841 or else Target_Typ = Any_Type
1842 or else Nkind (Expr) = N_Error
1843
1844 -- Not needed for non-scalar type
1845
1846 or else not Is_Scalar_Type (Target_Typ)
1847
1848 -- Not needed if we know node raises CE already
1849
1850 or else Raises_Constraint_Error (Expr)
ee6ba406 1851 then
1852 return;
1853 end if;
1854
1855 -- Now, see if checks are suppressed
1856
1857 Is_Subscr_Ref :=
1858 Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
1859
1860 if Is_Subscr_Ref then
1861 Arr := Prefix (Parnt);
1862 Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
1863 end if;
1864
1865 if not Do_Range_Check (Expr) then
1866
1867 -- Subscript reference. Check for Index_Checks suppressed
1868
1869 if Is_Subscr_Ref then
1870
1871 -- Check array type and its base type
1872
1873 if Index_Checks_Suppressed (Arr_Typ)
9dfe12ae 1874 or else Index_Checks_Suppressed (Base_Type (Arr_Typ))
ee6ba406 1875 then
1876 return;
1877
1878 -- Check array itself if it is an entity name
1879
1880 elsif Is_Entity_Name (Arr)
9dfe12ae 1881 and then Index_Checks_Suppressed (Entity (Arr))
ee6ba406 1882 then
1883 return;
1884
1885 -- Check expression itself if it is an entity name
1886
1887 elsif Is_Entity_Name (Expr)
9dfe12ae 1888 and then Index_Checks_Suppressed (Entity (Expr))
ee6ba406 1889 then
1890 return;
1891 end if;
1892
1893 -- All other cases, check for Range_Checks suppressed
1894
1895 else
1896 -- Check target type and its base type
1897
1898 if Range_Checks_Suppressed (Target_Typ)
9dfe12ae 1899 or else Range_Checks_Suppressed (Base_Type (Target_Typ))
ee6ba406 1900 then
1901 return;
1902
1903 -- Check expression itself if it is an entity name
1904
1905 elsif Is_Entity_Name (Expr)
9dfe12ae 1906 and then Range_Checks_Suppressed (Entity (Expr))
ee6ba406 1907 then
1908 return;
1909
feff2f05 1910 -- If Expr is part of an assignment statement, then check left
1911 -- side of assignment if it is an entity name.
ee6ba406 1912
1913 elsif Nkind (Parnt) = N_Assignment_Statement
1914 and then Is_Entity_Name (Name (Parnt))
9dfe12ae 1915 and then Range_Checks_Suppressed (Entity (Name (Parnt)))
ee6ba406 1916 then
1917 return;
1918 end if;
1919 end if;
1920 end if;
1921
9dfe12ae 1922 -- Do not set range checks if they are killed
1923
1924 if Nkind (Expr) = N_Unchecked_Type_Conversion
1925 and then Kill_Range_Check (Expr)
1926 then
1927 return;
1928 end if;
1929
1930 -- Do not set range checks for any values from System.Scalar_Values
1931 -- since the whole idea of such values is to avoid checking them!
1932
1933 if Is_Entity_Name (Expr)
1934 and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values)
1935 then
1936 return;
1937 end if;
1938
ee6ba406 1939 -- Now see if we need a check
1940
1941 if No (Source_Typ) then
1942 S_Typ := Etype (Expr);
1943 else
1944 S_Typ := Source_Typ;
1945 end if;
1946
1947 if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
1948 return;
1949 end if;
1950
1951 Is_Unconstrained_Subscr_Ref :=
1952 Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
1953
feff2f05 1954 -- Always do a range check if the source type includes infinities and
1955 -- the target type does not include infinities. We do not do this if
1956 -- range checks are killed.
ee6ba406 1957
1958 if Is_Floating_Point_Type (S_Typ)
1959 and then Has_Infinities (S_Typ)
1960 and then not Has_Infinities (Target_Typ)
1961 then
1962 Enable_Range_Check (Expr);
1963 end if;
1964
feff2f05 1965 -- Return if we know expression is definitely in the range of the target
1966 -- type as determined by Determine_Range. Right now we only do this for
1967 -- discrete types, and not fixed-point or floating-point types.
ee6ba406 1968
f2a06be9 1969 -- The additional less-precise tests below catch these cases
ee6ba406 1970
feff2f05 1971 -- Note: skip this if we are given a source_typ, since the point of
1972 -- supplying a Source_Typ is to stop us looking at the expression.
1973 -- We could sharpen this test to be out parameters only ???
ee6ba406 1974
1975 if Is_Discrete_Type (Target_Typ)
1976 and then Is_Discrete_Type (Etype (Expr))
1977 and then not Is_Unconstrained_Subscr_Ref
1978 and then No (Source_Typ)
1979 then
1980 declare
1981 Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
1982 Thi : constant Node_Id := Type_High_Bound (Target_Typ);
1983 Lo : Uint;
1984 Hi : Uint;
1985
1986 begin
1987 if Compile_Time_Known_Value (Tlo)
1988 and then Compile_Time_Known_Value (Thi)
1989 then
9dfe12ae 1990 declare
1991 Lov : constant Uint := Expr_Value (Tlo);
1992 Hiv : constant Uint := Expr_Value (Thi);
ee6ba406 1993
9dfe12ae 1994 begin
1995 -- If range is null, we for sure have a constraint error
1996 -- (we don't even need to look at the value involved,
1997 -- since all possible values will raise CE).
1998
1999 if Lov > Hiv then
2000 Bad_Value;
2001 return;
2002 end if;
2003
2004 -- Otherwise determine range of value
2005
9c486805 2006 Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True);
9dfe12ae 2007
2008 if OK then
2009
2010 -- If definitely in range, all OK
ee6ba406 2011
ee6ba406 2012 if Lo >= Lov and then Hi <= Hiv then
2013 return;
2014
9dfe12ae 2015 -- If definitely not in range, warn
2016
ee6ba406 2017 elsif Lov > Hi or else Hiv < Lo then
2018 Bad_Value;
2019 return;
9dfe12ae 2020
2021 -- Otherwise we don't know
2022
2023 else
2024 null;
ee6ba406 2025 end if;
9dfe12ae 2026 end if;
2027 end;
ee6ba406 2028 end if;
2029 end;
2030 end if;
2031
2032 Int_Real :=
2033 Is_Floating_Point_Type (S_Typ)
2034 or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
2035
2036 -- Check if we can determine at compile time whether Expr is in the
9dfe12ae 2037 -- range of the target type. Note that if S_Typ is within the bounds
2038 -- of Target_Typ then this must be the case. This check is meaningful
2039 -- only if this is not a conversion between integer and real types.
ee6ba406 2040
2041 if not Is_Unconstrained_Subscr_Ref
2042 and then
2043 Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
2044 and then
7a1dabb3 2045 (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
ee6ba406 2046 or else
9c486805 2047 Is_In_Range (Expr, Target_Typ,
2048 Assume_Valid => True,
2049 Fixed_Int => Fixed_Int,
2050 Int_Real => Int_Real))
ee6ba406 2051 then
2052 return;
2053
9c486805 2054 elsif Is_Out_Of_Range (Expr, Target_Typ,
2055 Assume_Valid => True,
2056 Fixed_Int => Fixed_Int,
2057 Int_Real => Int_Real)
2058 then
ee6ba406 2059 Bad_Value;
2060 return;
2061
feff2f05 2062 -- In the floating-point case, we only do range checks if the type is
2063 -- constrained. We definitely do NOT want range checks for unconstrained
2064 -- types, since we want to have infinities
ee6ba406 2065
9dfe12ae 2066 elsif Is_Floating_Point_Type (S_Typ) then
2067 if Is_Constrained (S_Typ) then
2068 Enable_Range_Check (Expr);
2069 end if;
ee6ba406 2070
9dfe12ae 2071 -- For all other cases we enable a range check unconditionally
ee6ba406 2072
2073 else
2074 Enable_Range_Check (Expr);
2075 return;
2076 end if;
ee6ba406 2077 end Apply_Scalar_Range_Check;
2078
2079 ----------------------------------
2080 -- Apply_Selected_Length_Checks --
2081 ----------------------------------
2082
2083 procedure Apply_Selected_Length_Checks
2084 (Ck_Node : Node_Id;
2085 Target_Typ : Entity_Id;
2086 Source_Typ : Entity_Id;
2087 Do_Static : Boolean)
2088 is
2089 Cond : Node_Id;
2090 R_Result : Check_Result;
2091 R_Cno : Node_Id;
2092
2093 Loc : constant Source_Ptr := Sloc (Ck_Node);
2094 Checks_On : constant Boolean :=
2095 (not Index_Checks_Suppressed (Target_Typ))
2096 or else
2097 (not Length_Checks_Suppressed (Target_Typ));
2098
2099 begin
f15731c4 2100 if not Expander_Active then
ee6ba406 2101 return;
2102 end if;
2103
2104 R_Result :=
2105 Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
2106
2107 for J in 1 .. 2 loop
ee6ba406 2108 R_Cno := R_Result (J);
2109 exit when No (R_Cno);
2110
2111 -- A length check may mention an Itype which is attached to a
2112 -- subsequent node. At the top level in a package this can cause
2113 -- an order-of-elaboration problem, so we make sure that the itype
2114 -- is referenced now.
2115
2116 if Ekind (Current_Scope) = E_Package
2117 and then Is_Compilation_Unit (Current_Scope)
2118 then
2119 Ensure_Defined (Target_Typ, Ck_Node);
2120
2121 if Present (Source_Typ) then
2122 Ensure_Defined (Source_Typ, Ck_Node);
2123
2124 elsif Is_Itype (Etype (Ck_Node)) then
2125 Ensure_Defined (Etype (Ck_Node), Ck_Node);
2126 end if;
2127 end if;
2128
feff2f05 2129 -- If the item is a conditional raise of constraint error, then have
2130 -- a look at what check is being performed and ???
ee6ba406 2131
2132 if Nkind (R_Cno) = N_Raise_Constraint_Error
2133 and then Present (Condition (R_Cno))
2134 then
2135 Cond := Condition (R_Cno);
2136
0577b0b1 2137 -- Case where node does not now have a dynamic check
ee6ba406 2138
0577b0b1 2139 if not Has_Dynamic_Length_Check (Ck_Node) then
2140
2141 -- If checks are on, just insert the check
2142
2143 if Checks_On then
2144 Insert_Action (Ck_Node, R_Cno);
2145
2146 if not Do_Static then
2147 Set_Has_Dynamic_Length_Check (Ck_Node);
2148 end if;
2149
2150 -- If checks are off, then analyze the length check after
2151 -- temporarily attaching it to the tree in case the relevant
6fb3c314 2152 -- condition can be evaluated at compile time. We still want a
0577b0b1 2153 -- compile time warning in this case.
2154
2155 else
2156 Set_Parent (R_Cno, Ck_Node);
2157 Analyze (R_Cno);
ee6ba406 2158 end if;
ee6ba406 2159 end if;
2160
2161 -- Output a warning if the condition is known to be True
2162
2163 if Is_Entity_Name (Cond)
2164 and then Entity (Cond) = Standard_True
2165 then
2166 Apply_Compile_Time_Constraint_Error
2167 (Ck_Node, "wrong length for array of}?",
f15731c4 2168 CE_Length_Check_Failed,
ee6ba406 2169 Ent => Target_Typ,
2170 Typ => Target_Typ);
2171
2172 -- If we were only doing a static check, or if checks are not
2173 -- on, then we want to delete the check, since it is not needed.
2174 -- We do this by replacing the if statement by a null statement
2175
2176 elsif Do_Static or else not Checks_On then
00c403ee 2177 Remove_Warning_Messages (R_Cno);
ee6ba406 2178 Rewrite (R_Cno, Make_Null_Statement (Loc));
2179 end if;
2180
2181 else
2182 Install_Static_Check (R_Cno, Loc);
2183 end if;
ee6ba406 2184 end loop;
ee6ba406 2185 end Apply_Selected_Length_Checks;
2186
2187 ---------------------------------
2188 -- Apply_Selected_Range_Checks --
2189 ---------------------------------
2190
2191 procedure Apply_Selected_Range_Checks
2192 (Ck_Node : Node_Id;
2193 Target_Typ : Entity_Id;
2194 Source_Typ : Entity_Id;
2195 Do_Static : Boolean)
2196 is
2197 Cond : Node_Id;
2198 R_Result : Check_Result;
2199 R_Cno : Node_Id;
2200
2201 Loc : constant Source_Ptr := Sloc (Ck_Node);
2202 Checks_On : constant Boolean :=
2203 (not Index_Checks_Suppressed (Target_Typ))
2204 or else
2205 (not Range_Checks_Suppressed (Target_Typ));
2206
2207 begin
2208 if not Expander_Active or else not Checks_On then
2209 return;
2210 end if;
2211
2212 R_Result :=
2213 Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
2214
2215 for J in 1 .. 2 loop
2216
2217 R_Cno := R_Result (J);
2218 exit when No (R_Cno);
2219
feff2f05 2220 -- If the item is a conditional raise of constraint error, then have
2221 -- a look at what check is being performed and ???
ee6ba406 2222
2223 if Nkind (R_Cno) = N_Raise_Constraint_Error
2224 and then Present (Condition (R_Cno))
2225 then
2226 Cond := Condition (R_Cno);
2227
2228 if not Has_Dynamic_Range_Check (Ck_Node) then
2229 Insert_Action (Ck_Node, R_Cno);
2230
2231 if not Do_Static then
2232 Set_Has_Dynamic_Range_Check (Ck_Node);
2233 end if;
2234 end if;
2235
2236 -- Output a warning if the condition is known to be True
2237
2238 if Is_Entity_Name (Cond)
2239 and then Entity (Cond) = Standard_True
2240 then
feff2f05 2241 -- Since an N_Range is technically not an expression, we have
2242 -- to set one of the bounds to C_E and then just flag the
2243 -- N_Range. The warning message will point to the lower bound
2244 -- and complain about a range, which seems OK.
ee6ba406 2245
2246 if Nkind (Ck_Node) = N_Range then
2247 Apply_Compile_Time_Constraint_Error
2248 (Low_Bound (Ck_Node), "static range out of bounds of}?",
f15731c4 2249 CE_Range_Check_Failed,
ee6ba406 2250 Ent => Target_Typ,
2251 Typ => Target_Typ);
2252
2253 Set_Raises_Constraint_Error (Ck_Node);
2254
2255 else
2256 Apply_Compile_Time_Constraint_Error
2257 (Ck_Node, "static value out of range of}?",
f15731c4 2258 CE_Range_Check_Failed,
ee6ba406 2259 Ent => Target_Typ,
2260 Typ => Target_Typ);
2261 end if;
2262
2263 -- If we were only doing a static check, or if checks are not
2264 -- on, then we want to delete the check, since it is not needed.
2265 -- We do this by replacing the if statement by a null statement
2266
2267 elsif Do_Static or else not Checks_On then
00c403ee 2268 Remove_Warning_Messages (R_Cno);
ee6ba406 2269 Rewrite (R_Cno, Make_Null_Statement (Loc));
2270 end if;
2271
2272 else
2273 Install_Static_Check (R_Cno, Loc);
2274 end if;
ee6ba406 2275 end loop;
ee6ba406 2276 end Apply_Selected_Range_Checks;
2277
2278 -------------------------------
2279 -- Apply_Static_Length_Check --
2280 -------------------------------
2281
2282 procedure Apply_Static_Length_Check
2283 (Expr : Node_Id;
2284 Target_Typ : Entity_Id;
2285 Source_Typ : Entity_Id := Empty)
2286 is
2287 begin
2288 Apply_Selected_Length_Checks
2289 (Expr, Target_Typ, Source_Typ, Do_Static => True);
2290 end Apply_Static_Length_Check;
2291
2292 -------------------------------------
2293 -- Apply_Subscript_Validity_Checks --
2294 -------------------------------------
2295
2296 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
2297 Sub : Node_Id;
2298
2299 begin
2300 pragma Assert (Nkind (Expr) = N_Indexed_Component);
2301
2302 -- Loop through subscripts
2303
2304 Sub := First (Expressions (Expr));
2305 while Present (Sub) loop
2306
feff2f05 2307 -- Check one subscript. Note that we do not worry about enumeration
2308 -- type with holes, since we will convert the value to a Pos value
2309 -- for the subscript, and that convert will do the necessary validity
2310 -- check.
ee6ba406 2311
2312 Ensure_Valid (Sub, Holes_OK => True);
2313
2314 -- Move to next subscript
2315
2316 Sub := Next (Sub);
2317 end loop;
2318 end Apply_Subscript_Validity_Checks;
2319
2320 ----------------------------------
2321 -- Apply_Type_Conversion_Checks --
2322 ----------------------------------
2323
2324 procedure Apply_Type_Conversion_Checks (N : Node_Id) is
2325 Target_Type : constant Entity_Id := Etype (N);
2326 Target_Base : constant Entity_Id := Base_Type (Target_Type);
9dfe12ae 2327 Expr : constant Node_Id := Expression (N);
141d591a 2328 Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr));
2329 -- Note: if Etype (Expr) is a private type without discriminants, its
2330 -- full view might have discriminants with defaults, so we need the
2331 -- full view here to retrieve the constraints.
ee6ba406 2332
2333 begin
2334 if Inside_A_Generic then
2335 return;
2336
f15731c4 2337 -- Skip these checks if serious errors detected, there are some nasty
ee6ba406 2338 -- situations of incomplete trees that blow things up.
2339
f15731c4 2340 elsif Serious_Errors_Detected > 0 then
ee6ba406 2341 return;
2342
feff2f05 2343 -- Scalar type conversions of the form Target_Type (Expr) require a
2344 -- range check if we cannot be sure that Expr is in the base type of
2345 -- Target_Typ and also that Expr is in the range of Target_Typ. These
2346 -- are not quite the same condition from an implementation point of
2347 -- view, but clearly the second includes the first.
ee6ba406 2348
2349 elsif Is_Scalar_Type (Target_Type) then
2350 declare
2351 Conv_OK : constant Boolean := Conversion_OK (N);
feff2f05 2352 -- If the Conversion_OK flag on the type conversion is set and no
2353 -- floating point type is involved in the type conversion then
2354 -- fixed point values must be read as integral values.
ee6ba406 2355
5329ca64 2356 Float_To_Int : constant Boolean :=
2357 Is_Floating_Point_Type (Expr_Type)
2358 and then Is_Integer_Type (Target_Type);
2359
ee6ba406 2360 begin
ee6ba406 2361 if not Overflow_Checks_Suppressed (Target_Base)
e254d721 2362 and then not
7a1dabb3 2363 In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK)
5329ca64 2364 and then not Float_To_Int
ee6ba406 2365 then
00c403ee 2366 Activate_Overflow_Check (N);
ee6ba406 2367 end if;
2368
2369 if not Range_Checks_Suppressed (Target_Type)
2370 and then not Range_Checks_Suppressed (Expr_Type)
2371 then
5329ca64 2372 if Float_To_Int then
2373 Apply_Float_Conversion_Check (Expr, Target_Type);
2374 else
2375 Apply_Scalar_Range_Check
2376 (Expr, Target_Type, Fixed_Int => Conv_OK);
2377 end if;
ee6ba406 2378 end if;
2379 end;
2380
2381 elsif Comes_From_Source (N)
f40f9731 2382 and then not Discriminant_Checks_Suppressed (Target_Type)
ee6ba406 2383 and then Is_Record_Type (Target_Type)
2384 and then Is_Derived_Type (Target_Type)
2385 and then not Is_Tagged_Type (Target_Type)
2386 and then not Is_Constrained (Target_Type)
9dfe12ae 2387 and then Present (Stored_Constraint (Target_Type))
ee6ba406 2388 then
141d591a 2389 -- An unconstrained derived type may have inherited discriminant.
9dfe12ae 2390 -- Build an actual discriminant constraint list using the stored
ee6ba406 2391 -- constraint, to verify that the expression of the parent type
2392 -- satisfies the constraints imposed by the (unconstrained!)
2393 -- derived type. This applies to value conversions, not to view
2394 -- conversions of tagged types.
2395
2396 declare
9dfe12ae 2397 Loc : constant Source_Ptr := Sloc (N);
2398 Cond : Node_Id;
2399 Constraint : Elmt_Id;
2400 Discr_Value : Node_Id;
2401 Discr : Entity_Id;
2402
2403 New_Constraints : constant Elist_Id := New_Elmt_List;
2404 Old_Constraints : constant Elist_Id :=
2405 Discriminant_Constraint (Expr_Type);
ee6ba406 2406
2407 begin
9dfe12ae 2408 Constraint := First_Elmt (Stored_Constraint (Target_Type));
ee6ba406 2409 while Present (Constraint) loop
2410 Discr_Value := Node (Constraint);
2411
2412 if Is_Entity_Name (Discr_Value)
2413 and then Ekind (Entity (Discr_Value)) = E_Discriminant
2414 then
2415 Discr := Corresponding_Discriminant (Entity (Discr_Value));
2416
2417 if Present (Discr)
2418 and then Scope (Discr) = Base_Type (Expr_Type)
2419 then
2420 -- Parent is constrained by new discriminant. Obtain
feff2f05 2421 -- Value of original discriminant in expression. If the
2422 -- new discriminant has been used to constrain more than
2423 -- one of the stored discriminants, this will provide the
2424 -- required consistency check.
ee6ba406 2425
55868293 2426 Append_Elmt
2427 (Make_Selected_Component (Loc,
2428 Prefix =>
9dfe12ae 2429 Duplicate_Subexpr_No_Checks
2430 (Expr, Name_Req => True),
ee6ba406 2431 Selector_Name =>
2432 Make_Identifier (Loc, Chars (Discr))),
55868293 2433 New_Constraints);
ee6ba406 2434
2435 else
2436 -- Discriminant of more remote ancestor ???
2437
2438 return;
2439 end if;
2440
feff2f05 2441 -- Derived type definition has an explicit value for this
2442 -- stored discriminant.
ee6ba406 2443
2444 else
2445 Append_Elmt
9dfe12ae 2446 (Duplicate_Subexpr_No_Checks (Discr_Value),
2447 New_Constraints);
ee6ba406 2448 end if;
2449
2450 Next_Elmt (Constraint);
2451 end loop;
2452
2453 -- Use the unconstrained expression type to retrieve the
2454 -- discriminants of the parent, and apply momentarily the
2455 -- discriminant constraint synthesized above.
2456
2457 Set_Discriminant_Constraint (Expr_Type, New_Constraints);
2458 Cond := Build_Discriminant_Checks (Expr, Expr_Type);
2459 Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
2460
2461 Insert_Action (N,
f15731c4 2462 Make_Raise_Constraint_Error (Loc,
2463 Condition => Cond,
2464 Reason => CE_Discriminant_Check_Failed));
ee6ba406 2465 end;
2466
feff2f05 2467 -- For arrays, conversions are applied during expansion, to take into
2468 -- accounts changes of representation. The checks become range checks on
2469 -- the base type or length checks on the subtype, depending on whether
2470 -- the target type is unconstrained or constrained.
ee6ba406 2471
2472 else
2473 null;
2474 end if;
ee6ba406 2475 end Apply_Type_Conversion_Checks;
2476
2477 ----------------------------------------------
2478 -- Apply_Universal_Integer_Attribute_Checks --
2479 ----------------------------------------------
2480
2481 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
2482 Loc : constant Source_Ptr := Sloc (N);
2483 Typ : constant Entity_Id := Etype (N);
2484
2485 begin
2486 if Inside_A_Generic then
2487 return;
2488
2489 -- Nothing to do if checks are suppressed
2490
2491 elsif Range_Checks_Suppressed (Typ)
2492 and then Overflow_Checks_Suppressed (Typ)
2493 then
2494 return;
2495
2496 -- Nothing to do if the attribute does not come from source. The
2497 -- internal attributes we generate of this type do not need checks,
2498 -- and furthermore the attempt to check them causes some circular
2499 -- elaboration orders when dealing with packed types.
2500
2501 elsif not Comes_From_Source (N) then
2502 return;
2503
9dfe12ae 2504 -- If the prefix is a selected component that depends on a discriminant
2505 -- the check may improperly expose a discriminant instead of using
2506 -- the bounds of the object itself. Set the type of the attribute to
2507 -- the base type of the context, so that a check will be imposed when
2508 -- needed (e.g. if the node appears as an index).
2509
2510 elsif Nkind (Prefix (N)) = N_Selected_Component
2511 and then Ekind (Typ) = E_Signed_Integer_Subtype
2512 and then Depends_On_Discriminant (Scalar_Range (Typ))
2513 then
2514 Set_Etype (N, Base_Type (Typ));
2515
feff2f05 2516 -- Otherwise, replace the attribute node with a type conversion node
2517 -- whose expression is the attribute, retyped to universal integer, and
2518 -- whose subtype mark is the target type. The call to analyze this
2519 -- conversion will set range and overflow checks as required for proper
2520 -- detection of an out of range value.
ee6ba406 2521
2522 else
2523 Set_Etype (N, Universal_Integer);
2524 Set_Analyzed (N, True);
2525
2526 Rewrite (N,
2527 Make_Type_Conversion (Loc,
2528 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
2529 Expression => Relocate_Node (N)));
2530
2531 Analyze_And_Resolve (N, Typ);
2532 return;
2533 end if;
ee6ba406 2534 end Apply_Universal_Integer_Attribute_Checks;
2535
2536 -------------------------------
2537 -- Build_Discriminant_Checks --
2538 -------------------------------
2539
2540 function Build_Discriminant_Checks
2541 (N : Node_Id;
314a23b6 2542 T_Typ : Entity_Id) return Node_Id
ee6ba406 2543 is
2544 Loc : constant Source_Ptr := Sloc (N);
2545 Cond : Node_Id;
2546 Disc : Elmt_Id;
2547 Disc_Ent : Entity_Id;
9dfe12ae 2548 Dref : Node_Id;
ee6ba406 2549 Dval : Node_Id;
2550
84d0d4a5 2551 function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id;
2552
2553 ----------------------------------
2554 -- Aggregate_Discriminant_Value --
2555 ----------------------------------
2556
2557 function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is
2558 Assoc : Node_Id;
2559
2560 begin
feff2f05 2561 -- The aggregate has been normalized with named associations. We use
2562 -- the Chars field to locate the discriminant to take into account
2563 -- discriminants in derived types, which carry the same name as those
2564 -- in the parent.
84d0d4a5 2565
2566 Assoc := First (Component_Associations (N));
2567 while Present (Assoc) loop
2568 if Chars (First (Choices (Assoc))) = Chars (Disc) then
2569 return Expression (Assoc);
2570 else
2571 Next (Assoc);
2572 end if;
2573 end loop;
2574
2575 -- Discriminant must have been found in the loop above
2576
2577 raise Program_Error;
2578 end Aggregate_Discriminant_Val;
2579
2580 -- Start of processing for Build_Discriminant_Checks
2581
ee6ba406 2582 begin
84d0d4a5 2583 -- Loop through discriminants evolving the condition
2584
ee6ba406 2585 Cond := Empty;
2586 Disc := First_Elmt (Discriminant_Constraint (T_Typ));
2587
9dfe12ae 2588 -- For a fully private type, use the discriminants of the parent type
ee6ba406 2589
2590 if Is_Private_Type (T_Typ)
2591 and then No (Full_View (T_Typ))
2592 then
2593 Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
2594 else
2595 Disc_Ent := First_Discriminant (T_Typ);
2596 end if;
2597
2598 while Present (Disc) loop
ee6ba406 2599 Dval := Node (Disc);
2600
2601 if Nkind (Dval) = N_Identifier
2602 and then Ekind (Entity (Dval)) = E_Discriminant
2603 then
2604 Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
2605 else
9dfe12ae 2606 Dval := Duplicate_Subexpr_No_Checks (Dval);
ee6ba406 2607 end if;
2608
00f91aef 2609 -- If we have an Unchecked_Union node, we can infer the discriminants
2610 -- of the node.
9dfe12ae 2611
00f91aef 2612 if Is_Unchecked_Union (Base_Type (T_Typ)) then
2613 Dref := New_Copy (
2614 Get_Discriminant_Value (
2615 First_Discriminant (T_Typ),
2616 T_Typ,
2617 Stored_Constraint (T_Typ)));
2618
84d0d4a5 2619 elsif Nkind (N) = N_Aggregate then
2620 Dref :=
2621 Duplicate_Subexpr_No_Checks
2622 (Aggregate_Discriminant_Val (Disc_Ent));
2623
00f91aef 2624 else
2625 Dref :=
2626 Make_Selected_Component (Loc,
2627 Prefix =>
2628 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
2629 Selector_Name =>
2630 Make_Identifier (Loc, Chars (Disc_Ent)));
2631
2632 Set_Is_In_Discriminant_Check (Dref);
2633 end if;
9dfe12ae 2634
ee6ba406 2635 Evolve_Or_Else (Cond,
2636 Make_Op_Ne (Loc,
9dfe12ae 2637 Left_Opnd => Dref,
ee6ba406 2638 Right_Opnd => Dval));
2639
2640 Next_Elmt (Disc);
2641 Next_Discriminant (Disc_Ent);
2642 end loop;
2643
2644 return Cond;
2645 end Build_Discriminant_Checks;
2646
13dbf220 2647 ------------------
2648 -- Check_Needed --
2649 ------------------
2650
2651 function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is
2652 N : Node_Id;
2653 P : Node_Id;
2654 K : Node_Kind;
2655 L : Node_Id;
2656 R : Node_Id;
2657
2658 begin
2659 -- Always check if not simple entity
2660
2661 if Nkind (Nod) not in N_Has_Entity
2662 or else not Comes_From_Source (Nod)
2663 then
2664 return True;
2665 end if;
2666
2667 -- Look up tree for short circuit
2668
2669 N := Nod;
2670 loop
2671 P := Parent (N);
2672 K := Nkind (P);
2673
7b17e51b 2674 -- Done if out of subexpression (note that we allow generated stuff
2675 -- such as itype declarations in this context, to keep the loop going
2676 -- since we may well have generated such stuff in complex situations.
2677 -- Also done if no parent (probably an error condition, but no point
2678 -- in behaving nasty if we find it!)
2679
2680 if No (P)
2681 or else (K not in N_Subexpr and then Comes_From_Source (P))
2682 then
13dbf220 2683 return True;
2684
7b17e51b 2685 -- Or/Or Else case, where test is part of the right operand, or is
2686 -- part of one of the actions associated with the right operand, and
2687 -- the left operand is an equality test.
13dbf220 2688
7b17e51b 2689 elsif K = N_Op_Or then
13dbf220 2690 exit when N = Right_Opnd (P)
2691 and then Nkind (Left_Opnd (P)) = N_Op_Eq;
2692
7b17e51b 2693 elsif K = N_Or_Else then
2694 exit when (N = Right_Opnd (P)
2695 or else
2696 (Is_List_Member (N)
2697 and then List_Containing (N) = Actions (P)))
2698 and then Nkind (Left_Opnd (P)) = N_Op_Eq;
13dbf220 2699
7b17e51b 2700 -- Similar test for the And/And then case, where the left operand
2701 -- is an inequality test.
2702
2703 elsif K = N_Op_And then
13dbf220 2704 exit when N = Right_Opnd (P)
38f5559f 2705 and then Nkind (Left_Opnd (P)) = N_Op_Ne;
7b17e51b 2706
2707 elsif K = N_And_Then then
2708 exit when (N = Right_Opnd (P)
2709 or else
2710 (Is_List_Member (N)
2711 and then List_Containing (N) = Actions (P)))
2712 and then Nkind (Left_Opnd (P)) = N_Op_Ne;
13dbf220 2713 end if;
2714
2715 N := P;
2716 end loop;
2717
2718 -- If we fall through the loop, then we have a conditional with an
2719 -- appropriate test as its left operand. So test further.
2720
2721 L := Left_Opnd (P);
13dbf220 2722 R := Right_Opnd (L);
2723 L := Left_Opnd (L);
2724
2725 -- Left operand of test must match original variable
2726
2727 if Nkind (L) not in N_Has_Entity
2728 or else Entity (L) /= Entity (Nod)
2729 then
2730 return True;
2731 end if;
2732
2af58f67 2733 -- Right operand of test must be key value (zero or null)
13dbf220 2734
2735 case Check is
2736 when Access_Check =>
2af58f67 2737 if not Known_Null (R) then
13dbf220 2738 return True;
2739 end if;
2740
2741 when Division_Check =>
2742 if not Compile_Time_Known_Value (R)
2743 or else Expr_Value (R) /= Uint_0
2744 then
2745 return True;
2746 end if;
2af58f67 2747
2748 when others =>
2749 raise Program_Error;
13dbf220 2750 end case;
2751
2752 -- Here we have the optimizable case, warn if not short-circuited
2753
2754 if K = N_Op_And or else K = N_Op_Or then
2755 case Check is
2756 when Access_Check =>
2757 Error_Msg_N
2758 ("Constraint_Error may be raised (access check)?",
2759 Parent (Nod));
2760 when Division_Check =>
2761 Error_Msg_N
2762 ("Constraint_Error may be raised (zero divide)?",
2763 Parent (Nod));
2af58f67 2764
2765 when others =>
2766 raise Program_Error;
13dbf220 2767 end case;
2768
2769 if K = N_Op_And then
e977c0cf 2770 Error_Msg_N -- CODEFIX
2771 ("use `AND THEN` instead of AND?", P);
13dbf220 2772 else
e977c0cf 2773 Error_Msg_N -- CODEFIX
2774 ("use `OR ELSE` instead of OR?", P);
13dbf220 2775 end if;
2776
6fb3c314 2777 -- If not short-circuited, we need the check
13dbf220 2778
2779 return True;
2780
2781 -- If short-circuited, we can omit the check
2782
2783 else
2784 return False;
2785 end if;
2786 end Check_Needed;
2787
ee6ba406 2788 -----------------------------------
2789 -- Check_Valid_Lvalue_Subscripts --
2790 -----------------------------------
2791
2792 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
2793 begin
2794 -- Skip this if range checks are suppressed
2795
2796 if Range_Checks_Suppressed (Etype (Expr)) then
2797 return;
2798
feff2f05 2799 -- Only do this check for expressions that come from source. We assume
2800 -- that expander generated assignments explicitly include any necessary
2801 -- checks. Note that this is not just an optimization, it avoids
2802 -- infinite recursions!
ee6ba406 2803
2804 elsif not Comes_From_Source (Expr) then
2805 return;
2806
2807 -- For a selected component, check the prefix
2808
2809 elsif Nkind (Expr) = N_Selected_Component then
2810 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
2811 return;
2812
2813 -- Case of indexed component
2814
2815 elsif Nkind (Expr) = N_Indexed_Component then
2816 Apply_Subscript_Validity_Checks (Expr);
2817
feff2f05 2818 -- Prefix may itself be or contain an indexed component, and these
2819 -- subscripts need checking as well.
ee6ba406 2820
2821 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
2822 end if;
2823 end Check_Valid_Lvalue_Subscripts;
2824
fa7497e8 2825 ----------------------------------
2826 -- Null_Exclusion_Static_Checks --
2827 ----------------------------------
2828
2829 procedure Null_Exclusion_Static_Checks (N : Node_Id) is
0577b0b1 2830 Error_Node : Node_Id;
2831 Expr : Node_Id;
2832 Has_Null : constant Boolean := Has_Null_Exclusion (N);
2833 K : constant Node_Kind := Nkind (N);
2834 Typ : Entity_Id;
fa7497e8 2835
13dbf220 2836 begin
0577b0b1 2837 pragma Assert
2838 (K = N_Component_Declaration
2839 or else K = N_Discriminant_Specification
2840 or else K = N_Function_Specification
2841 or else K = N_Object_Declaration
2842 or else K = N_Parameter_Specification);
2843
2844 if K = N_Function_Specification then
2845 Typ := Etype (Defining_Entity (N));
2846 else
2847 Typ := Etype (Defining_Identifier (N));
2848 end if;
fa7497e8 2849
13dbf220 2850 case K is
13dbf220 2851 when N_Component_Declaration =>
2852 if Present (Access_Definition (Component_Definition (N))) then
0577b0b1 2853 Error_Node := Component_Definition (N);
13dbf220 2854 else
0577b0b1 2855 Error_Node := Subtype_Indication (Component_Definition (N));
13dbf220 2856 end if;
5329ca64 2857
0577b0b1 2858 when N_Discriminant_Specification =>
2859 Error_Node := Discriminant_Type (N);
2860
2861 when N_Function_Specification =>
2862 Error_Node := Result_Definition (N);
2863
2864 when N_Object_Declaration =>
2865 Error_Node := Object_Definition (N);
2866
2867 when N_Parameter_Specification =>
2868 Error_Node := Parameter_Type (N);
2869
13dbf220 2870 when others =>
2871 raise Program_Error;
2872 end case;
5329ca64 2873
0577b0b1 2874 if Has_Null then
5329ca64 2875
0577b0b1 2876 -- Enforce legality rule 3.10 (13): A null exclusion can only be
2877 -- applied to an access [sub]type.
5329ca64 2878
0577b0b1 2879 if not Is_Access_Type (Typ) then
503f7fd3 2880 Error_Msg_N
00c403ee 2881 ("`NOT NULL` allowed only for an access type", Error_Node);
5329ca64 2882
feff2f05 2883 -- Enforce legality rule RM 3.10(14/1): A null exclusion can only
0577b0b1 2884 -- be applied to a [sub]type that does not exclude null already.
2885
2886 elsif Can_Never_Be_Null (Typ)
d16989f1 2887 and then Comes_From_Source (Typ)
0577b0b1 2888 then
503f7fd3 2889 Error_Msg_NE
00c403ee 2890 ("`NOT NULL` not allowed (& already excludes null)",
2891 Error_Node, Typ);
0577b0b1 2892 end if;
13dbf220 2893 end if;
5329ca64 2894
cc60bd16 2895 -- Check that null-excluding objects are always initialized, except for
2896 -- deferred constants, for which the expression will appear in the full
2897 -- declaration.
13dbf220 2898
2899 if K = N_Object_Declaration
84d0d4a5 2900 and then No (Expression (N))
cc60bd16 2901 and then not Constant_Present (N)
feff2f05 2902 and then not No_Initialization (N)
13dbf220 2903 then
feff2f05 2904 -- Add an expression that assigns null. This node is needed by
2905 -- Apply_Compile_Time_Constraint_Error, which will replace this with
2906 -- a Constraint_Error node.
13dbf220 2907
2908 Set_Expression (N, Make_Null (Sloc (N)));
2909 Set_Etype (Expression (N), Etype (Defining_Identifier (N)));
5329ca64 2910
13dbf220 2911 Apply_Compile_Time_Constraint_Error
2912 (N => Expression (N),
2913 Msg => "(Ada 2005) null-excluding objects must be initialized?",
2914 Reason => CE_Null_Not_Allowed);
2915 end if;
5329ca64 2916
cc60bd16 2917 -- Check that a null-excluding component, formal or object is not being
2918 -- assigned a null value. Otherwise generate a warning message and
2c145f84 2919 -- replace Expression (N) by an N_Constraint_Error node.
13dbf220 2920
0577b0b1 2921 if K /= N_Function_Specification then
2922 Expr := Expression (N);
5329ca64 2923
2af58f67 2924 if Present (Expr) and then Known_Null (Expr) then
13dbf220 2925 case K is
0577b0b1 2926 when N_Component_Declaration |
2927 N_Discriminant_Specification =>
7189d17f 2928 Apply_Compile_Time_Constraint_Error
0577b0b1 2929 (N => Expr,
2af58f67 2930 Msg => "(Ada 2005) null not allowed " &
0577b0b1 2931 "in null-excluding components?",
2932 Reason => CE_Null_Not_Allowed);
5329ca64 2933
0577b0b1 2934 when N_Object_Declaration =>
7189d17f 2935 Apply_Compile_Time_Constraint_Error
0577b0b1 2936 (N => Expr,
2af58f67 2937 Msg => "(Ada 2005) null not allowed " &
0577b0b1 2938 "in null-excluding objects?",
2939 Reason => CE_Null_Not_Allowed);
5329ca64 2940
0577b0b1 2941 when N_Parameter_Specification =>
7189d17f 2942 Apply_Compile_Time_Constraint_Error
0577b0b1 2943 (N => Expr,
2af58f67 2944 Msg => "(Ada 2005) null not allowed " &
0577b0b1 2945 "in null-excluding formals?",
2946 Reason => CE_Null_Not_Allowed);
13dbf220 2947
2948 when others =>
2949 null;
5329ca64 2950 end case;
2951 end if;
0577b0b1 2952 end if;
fa7497e8 2953 end Null_Exclusion_Static_Checks;
2954
9dfe12ae 2955 ----------------------------------
2956 -- Conditional_Statements_Begin --
2957 ----------------------------------
2958
2959 procedure Conditional_Statements_Begin is
2960 begin
2961 Saved_Checks_TOS := Saved_Checks_TOS + 1;
2962
feff2f05 2963 -- If stack overflows, kill all checks, that way we know to simply reset
2964 -- the number of saved checks to zero on return. This should never occur
2965 -- in practice.
9dfe12ae 2966
2967 if Saved_Checks_TOS > Saved_Checks_Stack'Last then
2968 Kill_All_Checks;
2969
feff2f05 2970 -- In the normal case, we just make a new stack entry saving the current
2971 -- number of saved checks for a later restore.
9dfe12ae 2972
2973 else
2974 Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks;
2975
2976 if Debug_Flag_CC then
2977 w ("Conditional_Statements_Begin: Num_Saved_Checks = ",
2978 Num_Saved_Checks);
2979 end if;
2980 end if;
2981 end Conditional_Statements_Begin;
2982
2983 --------------------------------
2984 -- Conditional_Statements_End --
2985 --------------------------------
2986
2987 procedure Conditional_Statements_End is
2988 begin
2989 pragma Assert (Saved_Checks_TOS > 0);
2990
feff2f05 2991 -- If the saved checks stack overflowed, then we killed all checks, so
2992 -- setting the number of saved checks back to zero is correct. This
2993 -- should never occur in practice.
9dfe12ae 2994
2995 if Saved_Checks_TOS > Saved_Checks_Stack'Last then
2996 Num_Saved_Checks := 0;
2997
feff2f05 2998 -- In the normal case, restore the number of saved checks from the top
2999 -- stack entry.
9dfe12ae 3000
3001 else
3002 Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS);
3003 if Debug_Flag_CC then
3004 w ("Conditional_Statements_End: Num_Saved_Checks = ",
3005 Num_Saved_Checks);
3006 end if;
3007 end if;
3008
3009 Saved_Checks_TOS := Saved_Checks_TOS - 1;
3010 end Conditional_Statements_End;
3011
ee6ba406 3012 ---------------------
3013 -- Determine_Range --
3014 ---------------------
3015
6af1bdbc 3016 Cache_Size : constant := 2 ** 10;
ee6ba406 3017 type Cache_Index is range 0 .. Cache_Size - 1;
3018 -- Determine size of below cache (power of 2 is more efficient!)
3019
3020 Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
9c486805 3021 Determine_Range_Cache_V : array (Cache_Index) of Boolean;
ee6ba406 3022 Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
3023 Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
feff2f05 3024 -- The above arrays are used to implement a small direct cache for
3025 -- Determine_Range calls. Because of the way Determine_Range recursively
3026 -- traces subexpressions, and because overflow checking calls the routine
3027 -- on the way up the tree, a quadratic behavior can otherwise be
3028 -- encountered in large expressions. The cache entry for node N is stored
3029 -- in the (N mod Cache_Size) entry, and can be validated by checking the
9c486805 3030 -- actual node value stored there. The Range_Cache_V array records the
3031 -- setting of Assume_Valid for the cache entry.
ee6ba406 3032
3033 procedure Determine_Range
9c486805 3034 (N : Node_Id;
3035 OK : out Boolean;
3036 Lo : out Uint;
3037 Hi : out Uint;
3038 Assume_Valid : Boolean := False)
ee6ba406 3039 is
e254d721 3040 Typ : Entity_Id := Etype (N);
3041 -- Type to use, may get reset to base type for possibly invalid entity
8880be85 3042
3043 Lo_Left : Uint;
3044 Hi_Left : Uint;
3045 -- Lo and Hi bounds of left operand
ee6ba406 3046
ee6ba406 3047 Lo_Right : Uint;
ee6ba406 3048 Hi_Right : Uint;
8880be85 3049 -- Lo and Hi bounds of right (or only) operand
3050
3051 Bound : Node_Id;
3052 -- Temp variable used to hold a bound node
3053
3054 Hbound : Uint;
3055 -- High bound of base type of expression
3056
3057 Lor : Uint;
3058 Hir : Uint;
3059 -- Refined values for low and high bounds, after tightening
3060
3061 OK1 : Boolean;
3062 -- Used in lower level calls to indicate if call succeeded
3063
3064 Cindex : Cache_Index;
3065 -- Used to search cache
ee6ba406 3066
3067 function OK_Operands return Boolean;
3068 -- Used for binary operators. Determines the ranges of the left and
3069 -- right operands, and if they are both OK, returns True, and puts
341bd953 3070 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left.
ee6ba406 3071
3072 -----------------
3073 -- OK_Operands --
3074 -----------------
3075
3076 function OK_Operands return Boolean is
3077 begin
9c486805 3078 Determine_Range
3079 (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid);
ee6ba406 3080
3081 if not OK1 then
3082 return False;
3083 end if;
3084
9c486805 3085 Determine_Range
3086 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
ee6ba406 3087 return OK1;
3088 end OK_Operands;
3089
3090 -- Start of processing for Determine_Range
3091
3092 begin
87bdc21d 3093 -- For temporary constants internally generated to remove side effects
3094 -- we must use the corresponding expression to determine the range of
3095 -- the expression.
3096
3097 if Is_Entity_Name (N)
3098 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3099 and then Ekind (Entity (N)) = E_Constant
3100 and then Is_Internal_Name (Chars (Entity (N)))
3101 then
3102 Determine_Range
3103 (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid);
3104 return;
3105 end if;
3106
ee6ba406 3107 -- Prevent junk warnings by initializing range variables
3108
3109 Lo := No_Uint;
3110 Hi := No_Uint;
3111 Lor := No_Uint;
3112 Hir := No_Uint;
3113
a781c0fc 3114 -- If type is not defined, we can't determine its range
ee6ba406 3115
a781c0fc 3116 if No (Typ)
3117
3118 -- We don't deal with anything except discrete types
3119
3120 or else not Is_Discrete_Type (Typ)
3121
3122 -- Ignore type for which an error has been posted, since range in
3123 -- this case may well be a bogosity deriving from the error. Also
3124 -- ignore if error posted on the reference node.
3125
3126 or else Error_Posted (N) or else Error_Posted (Typ)
ee6ba406 3127 then
3128 OK := False;
3129 return;
3130 end if;
3131
3132 -- For all other cases, we can determine the range
3133
3134 OK := True;
3135
feff2f05 3136 -- If value is compile time known, then the possible range is the one
3137 -- value that we know this expression definitely has!
ee6ba406 3138
3139 if Compile_Time_Known_Value (N) then
3140 Lo := Expr_Value (N);
3141 Hi := Lo;
3142 return;
3143 end if;
3144
3145 -- Return if already in the cache
3146
3147 Cindex := Cache_Index (N mod Cache_Size);
3148
9c486805 3149 if Determine_Range_Cache_N (Cindex) = N
3150 and then
3151 Determine_Range_Cache_V (Cindex) = Assume_Valid
3152 then
ee6ba406 3153 Lo := Determine_Range_Cache_Lo (Cindex);
3154 Hi := Determine_Range_Cache_Hi (Cindex);
3155 return;
3156 end if;
3157
feff2f05 3158 -- Otherwise, start by finding the bounds of the type of the expression,
3159 -- the value cannot be outside this range (if it is, then we have an
3160 -- overflow situation, which is a separate check, we are talking here
3161 -- only about the expression value).
ee6ba406 3162
341bd953 3163 -- First a check, never try to find the bounds of a generic type, since
3164 -- these bounds are always junk values, and it is only valid to look at
3165 -- the bounds in an instance.
3166
3167 if Is_Generic_Type (Typ) then
3168 OK := False;
3169 return;
3170 end if;
3171
9c486805 3172 -- First step, change to use base type unless we know the value is valid
e254d721 3173
9c486805 3174 if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N)))
3175 or else Assume_No_Invalid_Values
3176 or else Assume_Valid
e254d721 3177 then
9c486805 3178 null;
3179 else
3180 Typ := Underlying_Type (Base_Type (Typ));
e254d721 3181 end if;
3182
feff2f05 3183 -- We use the actual bound unless it is dynamic, in which case use the
3184 -- corresponding base type bound if possible. If we can't get a bound
3185 -- then we figure we can't determine the range (a peculiar case, that
3186 -- perhaps cannot happen, but there is no point in bombing in this
3187 -- optimization circuit.
8880be85 3188
3189 -- First the low bound
ee6ba406 3190
3191 Bound := Type_Low_Bound (Typ);
3192
3193 if Compile_Time_Known_Value (Bound) then
3194 Lo := Expr_Value (Bound);
3195
3196 elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then
3197 Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
3198
3199 else
3200 OK := False;
3201 return;
3202 end if;
3203
8880be85 3204 -- Now the high bound
3205
ee6ba406 3206 Bound := Type_High_Bound (Typ);
3207
8880be85 3208 -- We need the high bound of the base type later on, and this should
3209 -- always be compile time known. Again, it is not clear that this
3210 -- can ever be false, but no point in bombing.
ee6ba406 3211
8880be85 3212 if Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then
ee6ba406 3213 Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ)));
3214 Hi := Hbound;
3215
3216 else
3217 OK := False;
3218 return;
3219 end if;
3220
feff2f05 3221 -- If we have a static subtype, then that may have a tighter bound so
3222 -- use the upper bound of the subtype instead in this case.
8880be85 3223
3224 if Compile_Time_Known_Value (Bound) then
3225 Hi := Expr_Value (Bound);
3226 end if;
3227
feff2f05 3228 -- We may be able to refine this value in certain situations. If any
3229 -- refinement is possible, then Lor and Hir are set to possibly tighter
3230 -- bounds, and OK1 is set to True.
ee6ba406 3231
3232 case Nkind (N) is
3233
3234 -- For unary plus, result is limited by range of operand
3235
3236 when N_Op_Plus =>
9c486805 3237 Determine_Range
3238 (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid);
ee6ba406 3239
3240 -- For unary minus, determine range of operand, and negate it
3241
3242 when N_Op_Minus =>
9c486805 3243 Determine_Range
3244 (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid);
ee6ba406 3245
3246 if OK1 then
3247 Lor := -Hi_Right;
3248 Hir := -Lo_Right;
3249 end if;
3250
3251 -- For binary addition, get range of each operand and do the
3252 -- addition to get the result range.
3253
3254 when N_Op_Add =>
3255 if OK_Operands then
3256 Lor := Lo_Left + Lo_Right;
3257 Hir := Hi_Left + Hi_Right;
3258 end if;
3259
feff2f05 3260 -- Division is tricky. The only case we consider is where the right
3261 -- operand is a positive constant, and in this case we simply divide
3262 -- the bounds of the left operand
ee6ba406 3263
3264 when N_Op_Divide =>
3265 if OK_Operands then
3266 if Lo_Right = Hi_Right
3267 and then Lo_Right > 0
3268 then
3269 Lor := Lo_Left / Lo_Right;
3270 Hir := Hi_Left / Lo_Right;
3271
3272 else
3273 OK1 := False;
3274 end if;
3275 end if;
3276
feff2f05 3277 -- For binary subtraction, get range of each operand and do the worst
3278 -- case subtraction to get the result range.
ee6ba406 3279
3280 when N_Op_Subtract =>
3281 if OK_Operands then
3282 Lor := Lo_Left - Hi_Right;
3283 Hir := Hi_Left - Lo_Right;
3284 end if;
3285
feff2f05 3286 -- For MOD, if right operand is a positive constant, then result must
3287 -- be in the allowable range of mod results.
ee6ba406 3288
3289 when N_Op_Mod =>
3290 if OK_Operands then
9dfe12ae 3291 if Lo_Right = Hi_Right
3292 and then Lo_Right /= 0
3293 then
ee6ba406 3294 if Lo_Right > 0 then
3295 Lor := Uint_0;
3296 Hir := Lo_Right - 1;
3297
9dfe12ae 3298 else -- Lo_Right < 0
ee6ba406 3299 Lor := Lo_Right + 1;
3300 Hir := Uint_0;
3301 end if;
3302
3303 else
3304 OK1 := False;
3305 end if;
3306 end if;
3307
feff2f05 3308 -- For REM, if right operand is a positive constant, then result must
3309 -- be in the allowable range of mod results.
ee6ba406 3310
3311 when N_Op_Rem =>
3312 if OK_Operands then
9dfe12ae 3313 if Lo_Right = Hi_Right
3314 and then Lo_Right /= 0
3315 then
ee6ba406 3316 declare
3317 Dval : constant Uint := (abs Lo_Right) - 1;
3318
3319 begin
3320 -- The sign of the result depends on the sign of the
3321 -- dividend (but not on the sign of the divisor, hence
3322 -- the abs operation above).
3323
3324 if Lo_Left < 0 then
3325 Lor := -Dval;
3326 else
3327 Lor := Uint_0;
3328 end if;
3329
3330 if Hi_Left < 0 then
3331 Hir := Uint_0;
3332 else
3333 Hir := Dval;
3334 end if;
3335 end;
3336
3337 else
3338 OK1 := False;
3339 end if;
3340 end if;
3341
3342 -- Attribute reference cases
3343
3344 when N_Attribute_Reference =>
3345 case Attribute_Name (N) is
3346
3347 -- For Pos/Val attributes, we can refine the range using the
ddbf7f2e 3348 -- possible range of values of the attribute expression.
ee6ba406 3349
3350 when Name_Pos | Name_Val =>
9c486805 3351 Determine_Range
3352 (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid);
ee6ba406 3353
3354 -- For Length attribute, use the bounds of the corresponding
3355 -- index type to refine the range.
3356
3357 when Name_Length =>
3358 declare
3359 Atyp : Entity_Id := Etype (Prefix (N));
3360 Inum : Nat;
3361 Indx : Node_Id;
3362
3363 LL, LU : Uint;
3364 UL, UU : Uint;
3365
3366 begin
3367 if Is_Access_Type (Atyp) then
3368 Atyp := Designated_Type (Atyp);
3369 end if;
3370
3371 -- For string literal, we know exact value
3372
3373 if Ekind (Atyp) = E_String_Literal_Subtype then
3374 OK := True;
3375 Lo := String_Literal_Length (Atyp);
3376 Hi := String_Literal_Length (Atyp);
3377 return;
3378 end if;
3379
3380 -- Otherwise check for expression given
3381
3382 if No (Expressions (N)) then
3383 Inum := 1;
3384 else
3385 Inum :=
3386 UI_To_Int (Expr_Value (First (Expressions (N))));
3387 end if;
3388
3389 Indx := First_Index (Atyp);
3390 for J in 2 .. Inum loop
3391 Indx := Next_Index (Indx);
3392 end loop;
3393
9116df93 3394 -- If the index type is a formal type or derived from
c8da6114 3395 -- one, the bounds are not static.
3396
3397 if Is_Generic_Type (Root_Type (Etype (Indx))) then
3398 OK := False;
3399 return;
3400 end if;
3401
ee6ba406 3402 Determine_Range
9c486805 3403 (Type_Low_Bound (Etype (Indx)), OK1, LL, LU,
3404 Assume_Valid);
ee6ba406 3405
3406 if OK1 then
3407 Determine_Range
9c486805 3408 (Type_High_Bound (Etype (Indx)), OK1, UL, UU,
3409 Assume_Valid);
ee6ba406 3410
3411 if OK1 then
3412
3413 -- The maximum value for Length is the biggest
3414 -- possible gap between the values of the bounds.
3415 -- But of course, this value cannot be negative.
3416
9c486805 3417 Hir := UI_Max (Uint_0, UU - LL + 1);
ee6ba406 3418
3419 -- For constrained arrays, the minimum value for
3420 -- Length is taken from the actual value of the
9116df93 3421 -- bounds, since the index will be exactly of this
3422 -- subtype.
ee6ba406 3423
3424 if Is_Constrained (Atyp) then
9c486805 3425 Lor := UI_Max (Uint_0, UL - LU + 1);
ee6ba406 3426
3427 -- For an unconstrained array, the minimum value
3428 -- for length is always zero.
3429
3430 else
3431 Lor := Uint_0;
3432 end if;
3433 end if;
3434 end if;
3435 end;
3436
3437 -- No special handling for other attributes
9116df93 3438 -- Probably more opportunities exist here???
ee6ba406 3439
3440 when others =>
3441 OK1 := False;
3442
3443 end case;
3444
feff2f05 3445 -- For type conversion from one discrete type to another, we can
3446 -- refine the range using the converted value.
ee6ba406 3447
3448 when N_Type_Conversion =>
9c486805 3449 Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid);
ee6ba406 3450
3451 -- Nothing special to do for all other expression kinds
3452
3453 when others =>
3454 OK1 := False;
3455 Lor := No_Uint;
3456 Hir := No_Uint;
3457 end case;
3458
9116df93 3459 -- At this stage, if OK1 is true, then we know that the actual result of
3460 -- the computed expression is in the range Lor .. Hir. We can use this
3461 -- to restrict the possible range of results.
ee6ba406 3462
dd688950 3463 -- If one of the computed bounds is outside the range of the base type,
3464 -- the expression may raise an exception and we better indicate that
3465 -- the evaluation has failed, at least if checks are enabled.
3466
3467 if Enable_Overflow_Checks
3468 and then not Is_Entity_Name (N)
c04fff3e 3469 and then (Lor < Lo or else Hir > Hi)
dd688950 3470 then
3471 OK := False;
3472 return;
3473 end if;
3474
ee6ba406 3475 if OK1 then
3476
9116df93 3477 -- If the refined value of the low bound is greater than the type
3478 -- high bound, then reset it to the more restrictive value. However,
3479 -- we do NOT do this for the case of a modular type where the
3480 -- possible upper bound on the value is above the base type high
3481 -- bound, because that means the result could wrap.
ee6ba406 3482
3483 if Lor > Lo
9116df93 3484 and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound)
ee6ba406 3485 then
3486 Lo := Lor;
3487 end if;
3488
9116df93 3489 -- Similarly, if the refined value of the high bound is less than the
3490 -- value so far, then reset it to the more restrictive value. Again,
3491 -- we do not do this if the refined low bound is negative for a
3492 -- modular type, since this would wrap.
ee6ba406 3493
3494 if Hir < Hi
9116df93 3495 and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0)
ee6ba406 3496 then
3497 Hi := Hir;
3498 end if;
3499 end if;
3500
3501 -- Set cache entry for future call and we are all done
3502
3503 Determine_Range_Cache_N (Cindex) := N;
9c486805 3504 Determine_Range_Cache_V (Cindex) := Assume_Valid;
ee6ba406 3505 Determine_Range_Cache_Lo (Cindex) := Lo;
3506 Determine_Range_Cache_Hi (Cindex) := Hi;
3507 return;
3508
9116df93 3509 -- If any exception occurs, it means that we have some bug in the compiler,
3510 -- possibly triggered by a previous error, or by some unforeseen peculiar
ee6ba406 3511 -- occurrence. However, this is only an optimization attempt, so there is
3512 -- really no point in crashing the compiler. Instead we just decide, too
3513 -- bad, we can't figure out a range in this case after all.
3514
3515 exception
3516 when others =>
3517
3518 -- Debug flag K disables this behavior (useful for debugging)
3519
3520 if Debug_Flag_K then
3521 raise;
3522 else
3523 OK := False;
3524 Lo := No_Uint;
3525 Hi := No_Uint;
3526 return;
3527 end if;
ee6ba406 3528 end Determine_Range;
3529
3530 ------------------------------------
3531 -- Discriminant_Checks_Suppressed --
3532 ------------------------------------
3533
3534 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
3535 begin
9dfe12ae 3536 if Present (E) then
3537 if Is_Unchecked_Union (E) then
3538 return True;
3539 elsif Checks_May_Be_Suppressed (E) then
3540 return Is_Check_Suppressed (E, Discriminant_Check);
3541 end if;
3542 end if;
3543
3544 return Scope_Suppress (Discriminant_Check);
ee6ba406 3545 end Discriminant_Checks_Suppressed;
3546
3547 --------------------------------
3548 -- Division_Checks_Suppressed --
3549 --------------------------------
3550
3551 function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
3552 begin
9dfe12ae 3553 if Present (E) and then Checks_May_Be_Suppressed (E) then
3554 return Is_Check_Suppressed (E, Division_Check);
3555 else
3556 return Scope_Suppress (Division_Check);
3557 end if;
ee6ba406 3558 end Division_Checks_Suppressed;
3559
3560 -----------------------------------
3561 -- Elaboration_Checks_Suppressed --
3562 -----------------------------------
3563
3564 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
3565 begin
38f5559f 3566 -- The complication in this routine is that if we are in the dynamic
3567 -- model of elaboration, we also check All_Checks, since All_Checks
3568 -- does not set Elaboration_Check explicitly.
3569
9dfe12ae 3570 if Present (E) then
3571 if Kill_Elaboration_Checks (E) then
3572 return True;
38f5559f 3573
9dfe12ae 3574 elsif Checks_May_Be_Suppressed (E) then
38f5559f 3575 if Is_Check_Suppressed (E, Elaboration_Check) then
3576 return True;
3577 elsif Dynamic_Elaboration_Checks then
3578 return Is_Check_Suppressed (E, All_Checks);
3579 else
3580 return False;
3581 end if;
9dfe12ae 3582 end if;
3583 end if;
3584
38f5559f 3585 if Scope_Suppress (Elaboration_Check) then
3586 return True;
3587 elsif Dynamic_Elaboration_Checks then
3588 return Scope_Suppress (All_Checks);
3589 else
3590 return False;
3591 end if;
ee6ba406 3592 end Elaboration_Checks_Suppressed;
3593
9dfe12ae 3594 ---------------------------
3595 -- Enable_Overflow_Check --
3596 ---------------------------
3597
3598 procedure Enable_Overflow_Check (N : Node_Id) is
3599 Typ : constant Entity_Id := Base_Type (Etype (N));
3600 Chk : Nat;
3601 OK : Boolean;
3602 Ent : Entity_Id;
3603 Ofs : Uint;
3604 Lo : Uint;
3605 Hi : Uint;
ee6ba406 3606
ee6ba406 3607 begin
9dfe12ae 3608 if Debug_Flag_CC then
3609 w ("Enable_Overflow_Check for node ", Int (N));
3610 Write_Str (" Source location = ");
3611 wl (Sloc (N));
00c403ee 3612 pg (Union_Id (N));
ee6ba406 3613 end if;
ee6ba406 3614
75209ec5 3615 -- No check if overflow checks suppressed for type of node
3616
3617 if Present (Etype (N))
3618 and then Overflow_Checks_Suppressed (Etype (N))
3619 then
3620 return;
3621
49260fa5 3622 -- Nothing to do for unsigned integer types, which do not overflow
3623
3624 elsif Is_Modular_Integer_Type (Typ) then
3625 return;
3626
feff2f05 3627 -- Nothing to do if the range of the result is known OK. We skip this
3628 -- for conversions, since the caller already did the check, and in any
3629 -- case the condition for deleting the check for a type conversion is
cc60bd16 3630 -- different.
ee6ba406 3631
75209ec5 3632 elsif Nkind (N) /= N_Type_Conversion then
9c486805 3633 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
ee6ba406 3634
cc60bd16 3635 -- Note in the test below that we assume that the range is not OK
3636 -- if a bound of the range is equal to that of the type. That's not
3637 -- quite accurate but we do this for the following reasons:
ee6ba406 3638
9dfe12ae 3639 -- a) The way that Determine_Range works, it will typically report
3640 -- the bounds of the value as being equal to the bounds of the
3641 -- type, because it either can't tell anything more precise, or
3642 -- does not think it is worth the effort to be more precise.
ee6ba406 3643
9dfe12ae 3644 -- b) It is very unusual to have a situation in which this would
3645 -- generate an unnecessary overflow check (an example would be
3646 -- a subtype with a range 0 .. Integer'Last - 1 to which the
cc60bd16 3647 -- literal value one is added).
ee6ba406 3648
9dfe12ae 3649 -- c) The alternative is a lot of special casing in this routine
3650 -- which would partially duplicate Determine_Range processing.
ee6ba406 3651
9dfe12ae 3652 if OK
3653 and then Lo > Expr_Value (Type_Low_Bound (Typ))
3654 and then Hi < Expr_Value (Type_High_Bound (Typ))
3655 then
3656 if Debug_Flag_CC then
3657 w ("No overflow check required");
3658 end if;
3659
3660 return;
3661 end if;
3662 end if;
3663
feff2f05 3664 -- If not in optimizing mode, set flag and we are done. We are also done
3665 -- (and just set the flag) if the type is not a discrete type, since it
3666 -- is not worth the effort to eliminate checks for other than discrete
3667 -- types. In addition, we take this same path if we have stored the
3668 -- maximum number of checks possible already (a very unlikely situation,
3669 -- but we do not want to blow up!)
9dfe12ae 3670
3671 if Optimization_Level = 0
3672 or else not Is_Discrete_Type (Etype (N))
3673 or else Num_Saved_Checks = Saved_Checks'Last
ee6ba406 3674 then
00c403ee 3675 Activate_Overflow_Check (N);
9dfe12ae 3676
3677 if Debug_Flag_CC then
3678 w ("Optimization off");
3679 end if;
3680
ee6ba406 3681 return;
9dfe12ae 3682 end if;
ee6ba406 3683
9dfe12ae 3684 -- Otherwise evaluate and check the expression
3685
3686 Find_Check
3687 (Expr => N,
3688 Check_Type => 'O',
3689 Target_Type => Empty,
3690 Entry_OK => OK,
3691 Check_Num => Chk,
3692 Ent => Ent,
3693 Ofs => Ofs);
3694
3695 if Debug_Flag_CC then
3696 w ("Called Find_Check");
3697 w (" OK = ", OK);
3698
3699 if OK then
3700 w (" Check_Num = ", Chk);
3701 w (" Ent = ", Int (Ent));
3702 Write_Str (" Ofs = ");
3703 pid (Ofs);
3704 end if;
3705 end if;
ee6ba406 3706
9dfe12ae 3707 -- If check is not of form to optimize, then set flag and we are done
3708
3709 if not OK then
00c403ee 3710 Activate_Overflow_Check (N);
ee6ba406 3711 return;
9dfe12ae 3712 end if;
ee6ba406 3713
9dfe12ae 3714 -- If check is already performed, then return without setting flag
3715
3716 if Chk /= 0 then
3717 if Debug_Flag_CC then
3718 w ("Check suppressed!");
3719 end if;
ee6ba406 3720
ee6ba406 3721 return;
9dfe12ae 3722 end if;
ee6ba406 3723
9dfe12ae 3724 -- Here we will make a new entry for the new check
3725
00c403ee 3726 Activate_Overflow_Check (N);
9dfe12ae 3727 Num_Saved_Checks := Num_Saved_Checks + 1;
3728 Saved_Checks (Num_Saved_Checks) :=
3729 (Killed => False,
3730 Entity => Ent,
3731 Offset => Ofs,
3732 Check_Type => 'O',
3733 Target_Type => Empty);
3734
3735 if Debug_Flag_CC then
3736 w ("Make new entry, check number = ", Num_Saved_Checks);
3737 w (" Entity = ", Int (Ent));
3738 Write_Str (" Offset = ");
3739 pid (Ofs);
3740 w (" Check_Type = O");
3741 w (" Target_Type = Empty");
3742 end if;
ee6ba406 3743
feff2f05 3744 -- If we get an exception, then something went wrong, probably because of
3745 -- an error in the structure of the tree due to an incorrect program. Or it
3746 -- may be a bug in the optimization circuit. In either case the safest
3747 -- thing is simply to set the check flag unconditionally.
9dfe12ae 3748
3749 exception
3750 when others =>
00c403ee 3751 Activate_Overflow_Check (N);
9dfe12ae 3752
3753 if Debug_Flag_CC then
3754 w (" exception occurred, overflow flag set");
3755 end if;
3756
3757 return;
3758 end Enable_Overflow_Check;
3759
3760 ------------------------
3761 -- Enable_Range_Check --
3762 ------------------------
3763
3764 procedure Enable_Range_Check (N : Node_Id) is
3765 Chk : Nat;
3766 OK : Boolean;
3767 Ent : Entity_Id;
3768 Ofs : Uint;
3769 Ttyp : Entity_Id;
3770 P : Node_Id;
3771
3772 begin
feff2f05 3773 -- Return if unchecked type conversion with range check killed. In this
3774 -- case we never set the flag (that's what Kill_Range_Check is about!)
9dfe12ae 3775
3776 if Nkind (N) = N_Unchecked_Type_Conversion
3777 and then Kill_Range_Check (N)
ee6ba406 3778 then
3779 return;
9dfe12ae 3780 end if;
ee6ba406 3781
55e8372b 3782 -- Do not set range check flag if parent is assignment statement or
3783 -- object declaration with Suppress_Assignment_Checks flag set
3784
3785 if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration)
3786 and then Suppress_Assignment_Checks (Parent (N))
3787 then
3788 return;
3789 end if;
3790
0577b0b1 3791 -- Check for various cases where we should suppress the range check
3792
3793 -- No check if range checks suppressed for type of node
3794
3795 if Present (Etype (N))
3796 and then Range_Checks_Suppressed (Etype (N))
3797 then
3798 return;
3799
3800 -- No check if node is an entity name, and range checks are suppressed
3801 -- for this entity, or for the type of this entity.
3802
3803 elsif Is_Entity_Name (N)
3804 and then (Range_Checks_Suppressed (Entity (N))
3805 or else Range_Checks_Suppressed (Etype (Entity (N))))
3806 then
3807 return;
3808
3809 -- No checks if index of array, and index checks are suppressed for
3810 -- the array object or the type of the array.
3811
3812 elsif Nkind (Parent (N)) = N_Indexed_Component then
3813 declare
3814 Pref : constant Node_Id := Prefix (Parent (N));
3815 begin
3816 if Is_Entity_Name (Pref)
3817 and then Index_Checks_Suppressed (Entity (Pref))
3818 then
3819 return;
3820 elsif Index_Checks_Suppressed (Etype (Pref)) then
3821 return;
3822 end if;
3823 end;
3824 end if;
3825
9dfe12ae 3826 -- Debug trace output
ee6ba406 3827
9dfe12ae 3828 if Debug_Flag_CC then
3829 w ("Enable_Range_Check for node ", Int (N));
3830 Write_Str (" Source location = ");
3831 wl (Sloc (N));
00c403ee 3832 pg (Union_Id (N));
9dfe12ae 3833 end if;
3834
feff2f05 3835 -- If not in optimizing mode, set flag and we are done. We are also done
3836 -- (and just set the flag) if the type is not a discrete type, since it
3837 -- is not worth the effort to eliminate checks for other than discrete
3838 -- types. In addition, we take this same path if we have stored the
3839 -- maximum number of checks possible already (a very unlikely situation,
3840 -- but we do not want to blow up!)
9dfe12ae 3841
3842 if Optimization_Level = 0
3843 or else No (Etype (N))
3844 or else not Is_Discrete_Type (Etype (N))
3845 or else Num_Saved_Checks = Saved_Checks'Last
ee6ba406 3846 then
00c403ee 3847 Activate_Range_Check (N);
9dfe12ae 3848
3849 if Debug_Flag_CC then
3850 w ("Optimization off");
3851 end if;
3852
ee6ba406 3853 return;
9dfe12ae 3854 end if;
ee6ba406 3855
9dfe12ae 3856 -- Otherwise find out the target type
ee6ba406 3857
9dfe12ae 3858 P := Parent (N);
ee6ba406 3859
9dfe12ae 3860 -- For assignment, use left side subtype
3861
3862 if Nkind (P) = N_Assignment_Statement
3863 and then Expression (P) = N
3864 then
3865 Ttyp := Etype (Name (P));
3866
3867 -- For indexed component, use subscript subtype
3868
3869 elsif Nkind (P) = N_Indexed_Component then
3870 declare
3871 Atyp : Entity_Id;
3872 Indx : Node_Id;
3873 Subs : Node_Id;
3874
3875 begin
3876 Atyp := Etype (Prefix (P));
3877
3878 if Is_Access_Type (Atyp) then
3879 Atyp := Designated_Type (Atyp);
f07ea091 3880
3881 -- If the prefix is an access to an unconstrained array,
feff2f05 3882 -- perform check unconditionally: it depends on the bounds of
3883 -- an object and we cannot currently recognize whether the test
3884 -- may be redundant.
f07ea091 3885
3886 if not Is_Constrained (Atyp) then
00c403ee 3887 Activate_Range_Check (N);
f07ea091 3888 return;
3889 end if;
7189d17f 3890
feff2f05 3891 -- Ditto if the prefix is an explicit dereference whose designated
3892 -- type is unconstrained.
7189d17f 3893
3894 elsif Nkind (Prefix (P)) = N_Explicit_Dereference
3895 and then not Is_Constrained (Atyp)
3896 then
00c403ee 3897 Activate_Range_Check (N);
7189d17f 3898 return;
9dfe12ae 3899 end if;
3900
3901 Indx := First_Index (Atyp);
3902 Subs := First (Expressions (P));
3903 loop
3904 if Subs = N then
3905 Ttyp := Etype (Indx);
3906 exit;
3907 end if;
3908
3909 Next_Index (Indx);
3910 Next (Subs);
3911 end loop;
3912 end;
3913
3914 -- For now, ignore all other cases, they are not so interesting
3915
3916 else
3917 if Debug_Flag_CC then
3918 w (" target type not found, flag set");
3919 end if;
3920
00c403ee 3921 Activate_Range_Check (N);
9dfe12ae 3922 return;
3923 end if;
3924
3925 -- Evaluate and check the expression
3926
3927 Find_Check
3928 (Expr => N,
3929 Check_Type => 'R',
3930 Target_Type => Ttyp,
3931 Entry_OK => OK,
3932 Check_Num => Chk,
3933 Ent => Ent,
3934 Ofs => Ofs);
3935
3936 if Debug_Flag_CC then
3937 w ("Called Find_Check");
3938 w ("Target_Typ = ", Int (Ttyp));
3939 w (" OK = ", OK);
3940
3941 if OK then
3942 w (" Check_Num = ", Chk);
3943 w (" Ent = ", Int (Ent));
3944 Write_Str (" Ofs = ");
3945 pid (Ofs);
3946 end if;
3947 end if;
3948
3949 -- If check is not of form to optimize, then set flag and we are done
3950
3951 if not OK then
3952 if Debug_Flag_CC then
3953 w (" expression not of optimizable type, flag set");
3954 end if;
3955
00c403ee 3956 Activate_Range_Check (N);
9dfe12ae 3957 return;
3958 end if;
3959
3960 -- If check is already performed, then return without setting flag
3961
3962 if Chk /= 0 then
3963 if Debug_Flag_CC then
3964 w ("Check suppressed!");
3965 end if;
3966
3967 return;
3968 end if;
3969
3970 -- Here we will make a new entry for the new check
3971
00c403ee 3972 Activate_Range_Check (N);
9dfe12ae 3973 Num_Saved_Checks := Num_Saved_Checks + 1;
3974 Saved_Checks (Num_Saved_Checks) :=
3975 (Killed => False,
3976 Entity => Ent,
3977 Offset => Ofs,
3978 Check_Type => 'R',
3979 Target_Type => Ttyp);
3980
3981 if Debug_Flag_CC then
3982 w ("Make new entry, check number = ", Num_Saved_Checks);
3983 w (" Entity = ", Int (Ent));
3984 Write_Str (" Offset = ");
3985 pid (Ofs);
3986 w (" Check_Type = R");
3987 w (" Target_Type = ", Int (Ttyp));
00c403ee 3988 pg (Union_Id (Ttyp));
9dfe12ae 3989 end if;
3990
feff2f05 3991 -- If we get an exception, then something went wrong, probably because of
3992 -- an error in the structure of the tree due to an incorrect program. Or
3993 -- it may be a bug in the optimization circuit. In either case the safest
3994 -- thing is simply to set the check flag unconditionally.
9dfe12ae 3995
3996 exception
3997 when others =>
00c403ee 3998 Activate_Range_Check (N);
9dfe12ae 3999
4000 if Debug_Flag_CC then
4001 w (" exception occurred, range flag set");
4002 end if;
4003
4004 return;
4005 end Enable_Range_Check;
4006
4007 ------------------
4008 -- Ensure_Valid --
4009 ------------------
4010
4011 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is
4012 Typ : constant Entity_Id := Etype (Expr);
4013
4014 begin
4015 -- Ignore call if we are not doing any validity checking
4016
4017 if not Validity_Checks_On then
4018 return;
4019
0577b0b1 4020 -- Ignore call if range or validity checks suppressed on entity or type
9dfe12ae 4021
0577b0b1 4022 elsif Range_Or_Validity_Checks_Suppressed (Expr) then
9dfe12ae 4023 return;
4024
feff2f05 4025 -- No check required if expression is from the expander, we assume the
4026 -- expander will generate whatever checks are needed. Note that this is
4027 -- not just an optimization, it avoids infinite recursions!
9dfe12ae 4028
4029 -- Unchecked conversions must be checked, unless they are initialized
4030 -- scalar values, as in a component assignment in an init proc.
4031
4032 -- In addition, we force a check if Force_Validity_Checks is set
4033
4034 elsif not Comes_From_Source (Expr)
4035 and then not Force_Validity_Checks
4036 and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
4037 or else Kill_Range_Check (Expr))
4038 then
4039 return;
4040
4041 -- No check required if expression is known to have valid value
4042
4043 elsif Expr_Known_Valid (Expr) then
4044 return;
4045
feff2f05 4046 -- Ignore case of enumeration with holes where the flag is set not to
4047 -- worry about holes, since no special validity check is needed
9dfe12ae 4048
4049 elsif Is_Enumeration_Type (Typ)
4050 and then Has_Non_Standard_Rep (Typ)
4051 and then Holes_OK
4052 then
4053 return;
4054
f2a06be9 4055 -- No check required on the left-hand side of an assignment
9dfe12ae 4056
4057 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
4058 and then Expr = Name (Parent (Expr))
4059 then
4060 return;
4061
6fb3c314 4062 -- No check on a universal real constant. The context will eventually
38f5559f 4063 -- convert it to a machine number for some target type, or report an
4064 -- illegality.
4065
4066 elsif Nkind (Expr) = N_Real_Literal
4067 and then Etype (Expr) = Universal_Real
4068 then
4069 return;
4070
6fb3c314 4071 -- If the expression denotes a component of a packed boolean array,
0577b0b1 4072 -- no possible check applies. We ignore the old ACATS chestnuts that
4073 -- involve Boolean range True..True.
4074
4075 -- Note: validity checks are generated for expressions that yield a
4076 -- scalar type, when it is possible to create a value that is outside of
4077 -- the type. If this is a one-bit boolean no such value exists. This is
4078 -- an optimization, and it also prevents compiler blowing up during the
4079 -- elaboration of improperly expanded packed array references.
4080
4081 elsif Nkind (Expr) = N_Indexed_Component
4082 and then Is_Bit_Packed_Array (Etype (Prefix (Expr)))
4083 and then Root_Type (Etype (Expr)) = Standard_Boolean
4084 then
4085 return;
4086
9dfe12ae 4087 -- An annoying special case. If this is an out parameter of a scalar
4088 -- type, then the value is not going to be accessed, therefore it is
4089 -- inappropriate to do any validity check at the call site.
4090
4091 else
4092 -- Only need to worry about scalar types
4093
4094 if Is_Scalar_Type (Typ) then
ee6ba406 4095 declare
4096 P : Node_Id;
4097 N : Node_Id;
4098 E : Entity_Id;
4099 F : Entity_Id;
4100 A : Node_Id;
4101 L : List_Id;
4102
4103 begin
4104 -- Find actual argument (which may be a parameter association)
4105 -- and the parent of the actual argument (the call statement)
4106
4107 N := Expr;
4108 P := Parent (Expr);
4109
4110 if Nkind (P) = N_Parameter_Association then
4111 N := P;
4112 P := Parent (N);
4113 end if;
4114
feff2f05 4115 -- Only need to worry if we are argument of a procedure call
4116 -- since functions don't have out parameters. If this is an
4117 -- indirect or dispatching call, get signature from the
4118 -- subprogram type.
ee6ba406 4119
4120 if Nkind (P) = N_Procedure_Call_Statement then
4121 L := Parameter_Associations (P);
9dfe12ae 4122
4123 if Is_Entity_Name (Name (P)) then
4124 E := Entity (Name (P));
4125 else
4126 pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference);
4127 E := Etype (Name (P));
4128 end if;
ee6ba406 4129
feff2f05 4130 -- Only need to worry if there are indeed actuals, and if
4131 -- this could be a procedure call, otherwise we cannot get a
4132 -- match (either we are not an argument, or the mode of the
4133 -- formal is not OUT). This test also filters out the
4134 -- generic case.
ee6ba406 4135
4136 if Is_Non_Empty_List (L)
4137 and then Is_Subprogram (E)
4138 then
feff2f05 4139 -- This is the loop through parameters, looking for an
4140 -- OUT parameter for which we are the argument.
ee6ba406 4141
4142 F := First_Formal (E);
4143 A := First (L);
ee6ba406 4144 while Present (F) loop
4145 if Ekind (F) = E_Out_Parameter and then A = N then
4146 return;
4147 end if;
4148
4149 Next_Formal (F);
4150 Next (A);
4151 end loop;
4152 end if;
4153 end if;
4154 end;
4155 end if;
4156 end if;
4157
fa6a6949 4158 -- If this is a boolean expression, only its elementary operands need
90a07d4c 4159 -- checking: if they are valid, a boolean or short-circuit operation
4160 -- with them will be valid as well.
784d4230 4161
4162 if Base_Type (Typ) = Standard_Boolean
7af38999 4163 and then
fa6a6949 4164 (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit)
784d4230 4165 then
4166 return;
4167 end if;
4168
0577b0b1 4169 -- If we fall through, a validity check is required
ee6ba406 4170
4171 Insert_Valid_Check (Expr);
ce7498d3 4172
4173 if Is_Entity_Name (Expr)
4174 and then Safe_To_Capture_Value (Expr, Entity (Expr))
4175 then
4176 Set_Is_Known_Valid (Entity (Expr));
4177 end if;
ee6ba406 4178 end Ensure_Valid;
4179
4180 ----------------------
4181 -- Expr_Known_Valid --
4182 ----------------------
4183
4184 function Expr_Known_Valid (Expr : Node_Id) return Boolean is
4185 Typ : constant Entity_Id := Etype (Expr);
4186
4187 begin
feff2f05 4188 -- Non-scalar types are always considered valid, since they never give
4189 -- rise to the issues of erroneous or bounded error behavior that are
4190 -- the concern. In formal reference manual terms the notion of validity
4191 -- only applies to scalar types. Note that even when packed arrays are
4192 -- represented using modular types, they are still arrays semantically,
4193 -- so they are also always valid (in particular, the unused bits can be
4194 -- random rubbish without affecting the validity of the array value).
ee6ba406 4195
fa814356 4196 if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then
ee6ba406 4197 return True;
4198
4199 -- If no validity checking, then everything is considered valid
4200
4201 elsif not Validity_Checks_On then
4202 return True;
4203
4204 -- Floating-point types are considered valid unless floating-point
4205 -- validity checks have been specifically turned on.
4206
4207 elsif Is_Floating_Point_Type (Typ)
4208 and then not Validity_Check_Floating_Point
4209 then
4210 return True;
4211
feff2f05 4212 -- If the expression is the value of an object that is known to be
4213 -- valid, then clearly the expression value itself is valid.
ee6ba406 4214
4215 elsif Is_Entity_Name (Expr)
4216 and then Is_Known_Valid (Entity (Expr))
4217 then
4218 return True;
4219
0577b0b1 4220 -- References to discriminants are always considered valid. The value
4221 -- of a discriminant gets checked when the object is built. Within the
4222 -- record, we consider it valid, and it is important to do so, since
4223 -- otherwise we can try to generate bogus validity checks which
feff2f05 4224 -- reference discriminants out of scope. Discriminants of concurrent
4225 -- types are excluded for the same reason.
0577b0b1 4226
4227 elsif Is_Entity_Name (Expr)
feff2f05 4228 and then Denotes_Discriminant (Expr, Check_Concurrent => True)
0577b0b1 4229 then
4230 return True;
4231
feff2f05 4232 -- If the type is one for which all values are known valid, then we are
4233 -- sure that the value is valid except in the slightly odd case where
4234 -- the expression is a reference to a variable whose size has been
4235 -- explicitly set to a value greater than the object size.
ee6ba406 4236
4237 elsif Is_Known_Valid (Typ) then
4238 if Is_Entity_Name (Expr)
4239 and then Ekind (Entity (Expr)) = E_Variable
4240 and then Esize (Entity (Expr)) > Esize (Typ)
4241 then
4242 return False;
4243 else
4244 return True;
4245 end if;
4246
4247 -- Integer and character literals always have valid values, where
4248 -- appropriate these will be range checked in any case.
4249
4250 elsif Nkind (Expr) = N_Integer_Literal
4251 or else
4252 Nkind (Expr) = N_Character_Literal
4253 then
4254 return True;
4255
4256 -- If we have a type conversion or a qualification of a known valid
4257 -- value, then the result will always be valid.
4258
4259 elsif Nkind (Expr) = N_Type_Conversion
4260 or else
4261 Nkind (Expr) = N_Qualified_Expression
4262 then
4263 return Expr_Known_Valid (Expression (Expr));
4264
38f5559f 4265 -- The result of any operator is always considered valid, since we
4266 -- assume the necessary checks are done by the operator. For operators
4267 -- on floating-point operations, we must also check when the operation
4268 -- is the right-hand side of an assignment, or is an actual in a call.
ee6ba406 4269
0577b0b1 4270 elsif Nkind (Expr) in N_Op then
1d90d657 4271 if Is_Floating_Point_Type (Typ)
4272 and then Validity_Check_Floating_Point
4273 and then
4274 (Nkind (Parent (Expr)) = N_Assignment_Statement
4275 or else Nkind (Parent (Expr)) = N_Function_Call
4276 or else Nkind (Parent (Expr)) = N_Parameter_Association)
4277 then
4278 return False;
4279 else
4280 return True;
4281 end if;
4282
feff2f05 4283 -- The result of a membership test is always valid, since it is true or
4284 -- false, there are no other possibilities.
0577b0b1 4285
4286 elsif Nkind (Expr) in N_Membership_Test then
4287 return True;
4288
ee6ba406 4289 -- For all other cases, we do not know the expression is valid
4290
4291 else
4292 return False;
4293 end if;
4294 end Expr_Known_Valid;
4295
9dfe12ae 4296 ----------------
4297 -- Find_Check --
4298 ----------------
4299
4300 procedure Find_Check
4301 (Expr : Node_Id;
4302 Check_Type : Character;
4303 Target_Type : Entity_Id;
4304 Entry_OK : out Boolean;
4305 Check_Num : out Nat;
4306 Ent : out Entity_Id;
4307 Ofs : out Uint)
4308 is
4309 function Within_Range_Of
4310 (Target_Type : Entity_Id;
314a23b6 4311 Check_Type : Entity_Id) return Boolean;
9dfe12ae 4312 -- Given a requirement for checking a range against Target_Type, and
4313 -- and a range Check_Type against which a check has already been made,
4314 -- determines if the check against check type is sufficient to ensure
4315 -- that no check against Target_Type is required.
4316
4317 ---------------------
4318 -- Within_Range_Of --
4319 ---------------------
4320
4321 function Within_Range_Of
4322 (Target_Type : Entity_Id;
314a23b6 4323 Check_Type : Entity_Id) return Boolean
9dfe12ae 4324 is
4325 begin
4326 if Target_Type = Check_Type then
4327 return True;
4328
4329 else
4330 declare
4331 Tlo : constant Node_Id := Type_Low_Bound (Target_Type);
4332 Thi : constant Node_Id := Type_High_Bound (Target_Type);
4333 Clo : constant Node_Id := Type_Low_Bound (Check_Type);
4334 Chi : constant Node_Id := Type_High_Bound (Check_Type);
4335
4336 begin
4337 if (Tlo = Clo
4338 or else (Compile_Time_Known_Value (Tlo)
4339 and then
4340 Compile_Time_Known_Value (Clo)
4341 and then
4342 Expr_Value (Clo) >= Expr_Value (Tlo)))
4343 and then
4344 (Thi = Chi
4345 or else (Compile_Time_Known_Value (Thi)
4346 and then
4347 Compile_Time_Known_Value (Chi)
4348 and then
4349 Expr_Value (Chi) <= Expr_Value (Clo)))
4350 then
4351 return True;
4352 else
4353 return False;
4354 end if;
4355 end;
4356 end if;
4357 end Within_Range_Of;
4358
4359 -- Start of processing for Find_Check
4360
4361 begin
ed195555 4362 -- Establish default, in case no entry is found
9dfe12ae 4363
4364 Check_Num := 0;
4365
4366 -- Case of expression is simple entity reference
4367
4368 if Is_Entity_Name (Expr) then
4369 Ent := Entity (Expr);
4370 Ofs := Uint_0;
4371
4372 -- Case of expression is entity + known constant
4373
4374 elsif Nkind (Expr) = N_Op_Add
4375 and then Compile_Time_Known_Value (Right_Opnd (Expr))
4376 and then Is_Entity_Name (Left_Opnd (Expr))
4377 then
4378 Ent := Entity (Left_Opnd (Expr));
4379 Ofs := Expr_Value (Right_Opnd (Expr));
4380
4381 -- Case of expression is entity - known constant
4382
4383 elsif Nkind (Expr) = N_Op_Subtract
4384 and then Compile_Time_Known_Value (Right_Opnd (Expr))
4385 and then Is_Entity_Name (Left_Opnd (Expr))
4386 then
4387 Ent := Entity (Left_Opnd (Expr));
4388 Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr)));
4389
4390 -- Any other expression is not of the right form
4391
4392 else
4393 Ent := Empty;
4394 Ofs := Uint_0;
4395 Entry_OK := False;
4396 return;
4397 end if;
4398
feff2f05 4399 -- Come here with expression of appropriate form, check if entity is an
4400 -- appropriate one for our purposes.
9dfe12ae 4401
4402 if (Ekind (Ent) = E_Variable
cc60bd16 4403 or else Is_Constant_Object (Ent))
9dfe12ae 4404 and then not Is_Library_Level_Entity (Ent)
4405 then
4406 Entry_OK := True;
4407 else
4408 Entry_OK := False;
4409 return;
4410 end if;
4411
4412 -- See if there is matching check already
4413
4414 for J in reverse 1 .. Num_Saved_Checks loop
4415 declare
4416 SC : Saved_Check renames Saved_Checks (J);
4417
4418 begin
4419 if SC.Killed = False
4420 and then SC.Entity = Ent
4421 and then SC.Offset = Ofs
4422 and then SC.Check_Type = Check_Type
4423 and then Within_Range_Of (Target_Type, SC.Target_Type)
4424 then
4425 Check_Num := J;
4426 return;
4427 end if;
4428 end;
4429 end loop;
4430
4431 -- If we fall through entry was not found
4432
9dfe12ae 4433 return;
4434 end Find_Check;
4435
4436 ---------------------------------
4437 -- Generate_Discriminant_Check --
4438 ---------------------------------
4439
4440 -- Note: the code for this procedure is derived from the
feff2f05 4441 -- Emit_Discriminant_Check Routine in trans.c.
9dfe12ae 4442
4443 procedure Generate_Discriminant_Check (N : Node_Id) is
4444 Loc : constant Source_Ptr := Sloc (N);
4445 Pref : constant Node_Id := Prefix (N);
4446 Sel : constant Node_Id := Selector_Name (N);
4447
4448 Orig_Comp : constant Entity_Id :=
4449 Original_Record_Component (Entity (Sel));
4450 -- The original component to be checked
4451
4452 Discr_Fct : constant Entity_Id :=
4453 Discriminant_Checking_Func (Orig_Comp);
4454 -- The discriminant checking function
4455
4456 Discr : Entity_Id;
4457 -- One discriminant to be checked in the type
4458
4459 Real_Discr : Entity_Id;
4460 -- Actual discriminant in the call
4461
4462 Pref_Type : Entity_Id;
4463 -- Type of relevant prefix (ignoring private/access stuff)
4464
4465 Args : List_Id;
4466 -- List of arguments for function call
4467
4468 Formal : Entity_Id;
feff2f05 4469 -- Keep track of the formal corresponding to the actual we build for
4470 -- each discriminant, in order to be able to perform the necessary type
4471 -- conversions.
9dfe12ae 4472
4473 Scomp : Node_Id;
4474 -- Selected component reference for checking function argument
4475
4476 begin
4477 Pref_Type := Etype (Pref);
4478
4479 -- Force evaluation of the prefix, so that it does not get evaluated
4480 -- twice (once for the check, once for the actual reference). Such a
4481 -- double evaluation is always a potential source of inefficiency,
4482 -- and is functionally incorrect in the volatile case, or when the
4483 -- prefix may have side-effects. An entity or a component of an
4484 -- entity requires no evaluation.
4485
4486 if Is_Entity_Name (Pref) then
4487 if Treat_As_Volatile (Entity (Pref)) then
4488 Force_Evaluation (Pref, Name_Req => True);
4489 end if;
4490
4491 elsif Treat_As_Volatile (Etype (Pref)) then
4492 Force_Evaluation (Pref, Name_Req => True);
4493
4494 elsif Nkind (Pref) = N_Selected_Component
4495 and then Is_Entity_Name (Prefix (Pref))
4496 then
4497 null;
4498
4499 else
4500 Force_Evaluation (Pref, Name_Req => True);
4501 end if;
4502
4503 -- For a tagged type, use the scope of the original component to
4504 -- obtain the type, because ???
4505
4506 if Is_Tagged_Type (Scope (Orig_Comp)) then
4507 Pref_Type := Scope (Orig_Comp);
4508
feff2f05 4509 -- For an untagged derived type, use the discriminants of the parent
4510 -- which have been renamed in the derivation, possibly by a one-to-many
4511 -- discriminant constraint. For non-tagged type, initially get the Etype
4512 -- of the prefix
9dfe12ae 4513
4514 else
4515 if Is_Derived_Type (Pref_Type)
4516 and then Number_Discriminants (Pref_Type) /=
4517 Number_Discriminants (Etype (Base_Type (Pref_Type)))
4518 then
4519 Pref_Type := Etype (Base_Type (Pref_Type));
4520 end if;
4521 end if;
4522
4523 -- We definitely should have a checking function, This routine should
4524 -- not be called if no discriminant checking function is present.
4525
4526 pragma Assert (Present (Discr_Fct));
4527
4528 -- Create the list of the actual parameters for the call. This list
4529 -- is the list of the discriminant fields of the record expression to
4530 -- be discriminant checked.
4531
4532 Args := New_List;
4533 Formal := First_Formal (Discr_Fct);
4534 Discr := First_Discriminant (Pref_Type);
4535 while Present (Discr) loop
4536
4537 -- If we have a corresponding discriminant field, and a parent
4538 -- subtype is present, then we want to use the corresponding
4539 -- discriminant since this is the one with the useful value.
4540
4541 if Present (Corresponding_Discriminant (Discr))
4542 and then Ekind (Pref_Type) = E_Record_Type
4543 and then Present (Parent_Subtype (Pref_Type))
4544 then
4545 Real_Discr := Corresponding_Discriminant (Discr);
4546 else
4547 Real_Discr := Discr;
4548 end if;
4549
4550 -- Construct the reference to the discriminant
4551
4552 Scomp :=
4553 Make_Selected_Component (Loc,
4554 Prefix =>
4555 Unchecked_Convert_To (Pref_Type,
4556 Duplicate_Subexpr (Pref)),
4557 Selector_Name => New_Occurrence_Of (Real_Discr, Loc));
4558
4559 -- Manually analyze and resolve this selected component. We really
4560 -- want it just as it appears above, and do not want the expander
feff2f05 4561 -- playing discriminal games etc with this reference. Then we append
4562 -- the argument to the list we are gathering.
9dfe12ae 4563
4564 Set_Etype (Scomp, Etype (Real_Discr));
4565 Set_Analyzed (Scomp, True);
4566 Append_To (Args, Convert_To (Etype (Formal), Scomp));
4567
4568 Next_Formal_With_Extras (Formal);
4569 Next_Discriminant (Discr);
4570 end loop;
4571
4572 -- Now build and insert the call
4573
4574 Insert_Action (N,
4575 Make_Raise_Constraint_Error (Loc,
4576 Condition =>
4577 Make_Function_Call (Loc,
4578 Name => New_Occurrence_Of (Discr_Fct, Loc),
4579 Parameter_Associations => Args),
4580 Reason => CE_Discriminant_Check_Failed));
4581 end Generate_Discriminant_Check;
4582
5c99c290 4583 ---------------------------
4584 -- Generate_Index_Checks --
4585 ---------------------------
9dfe12ae 4586
4587 procedure Generate_Index_Checks (N : Node_Id) is
05f3e139 4588
4589 function Entity_Of_Prefix return Entity_Id;
4590 -- Returns the entity of the prefix of N (or Empty if not found)
4591
3f42e2a7 4592 ----------------------
4593 -- Entity_Of_Prefix --
4594 ----------------------
4595
05f3e139 4596 function Entity_Of_Prefix return Entity_Id is
e5d38095 4597 P : Node_Id;
4598
05f3e139 4599 begin
e5d38095 4600 P := Prefix (N);
05f3e139 4601 while not Is_Entity_Name (P) loop
4602 if not Nkind_In (P, N_Selected_Component,
4603 N_Indexed_Component)
4604 then
4605 return Empty;
4606 end if;
4607
4608 P := Prefix (P);
4609 end loop;
4610
4611 return Entity (P);
4612 end Entity_Of_Prefix;
4613
4614 -- Local variables
4615
4616 Loc : constant Source_Ptr := Sloc (N);
4617 A : constant Node_Id := Prefix (N);
4618 A_Ent : constant Entity_Id := Entity_Of_Prefix;
4619 Sub : Node_Id;
9dfe12ae 4620
3f42e2a7 4621 -- Start of processing for Generate_Index_Checks
4622
9dfe12ae 4623 begin
05f3e139 4624 -- Ignore call if the prefix is not an array since we have a serious
4625 -- error in the sources. Ignore it also if index checks are suppressed
4626 -- for array object or type.
0577b0b1 4627
05f3e139 4628 if not Is_Array_Type (Etype (A))
4629 or else (Present (A_Ent)
e5d38095 4630 and then Index_Checks_Suppressed (A_Ent))
0577b0b1 4631 or else Index_Checks_Suppressed (Etype (A))
4632 then
4633 return;
4634 end if;
4635
05f3e139 4636 -- Generate a raise of constraint error with the appropriate reason and
4637 -- a condition of the form:
4638
3f42e2a7 4639 -- Base_Type (Sub) not in Array'Range (Subscript)
05f3e139 4640
4641 -- Note that the reason we generate the conversion to the base type here
4642 -- is that we definitely want the range check to take place, even if it
4643 -- looks like the subtype is OK. Optimization considerations that allow
4644 -- us to omit the check have already been taken into account in the
4645 -- setting of the Do_Range_Check flag earlier on.
0577b0b1 4646
9dfe12ae 4647 Sub := First (Expressions (N));
05f3e139 4648
4649 -- Handle string literals
4650
4651 if Ekind (Etype (A)) = E_String_Literal_Subtype then
9dfe12ae 4652 if Do_Range_Check (Sub) then
4653 Set_Do_Range_Check (Sub, False);
4654
05f3e139 4655 -- For string literals we obtain the bounds of the string from the
4656 -- associated subtype.
9dfe12ae 4657
05f3e139 4658 Insert_Action (N,
4659 Make_Raise_Constraint_Error (Loc,
4660 Condition =>
4661 Make_Not_In (Loc,
4662 Left_Opnd =>
4663 Convert_To (Base_Type (Etype (Sub)),
4664 Duplicate_Subexpr_Move_Checks (Sub)),
4665 Right_Opnd =>
4666 Make_Attribute_Reference (Loc,
3f42e2a7 4667 Prefix => New_Reference_To (Etype (A), Loc),
05f3e139 4668 Attribute_Name => Name_Range)),
4669 Reason => CE_Index_Check_Failed));
4670 end if;
9dfe12ae 4671
05f3e139 4672 -- General case
9dfe12ae 4673
05f3e139 4674 else
4675 declare
4676 A_Idx : Node_Id := Empty;
4677 A_Range : Node_Id;
4678 Ind : Nat;
4679 Num : List_Id;
4680 Range_N : Node_Id;
9dfe12ae 4681
05f3e139 4682 begin
4683 A_Idx := First_Index (Etype (A));
4684 Ind := 1;
4685 while Present (Sub) loop
4686 if Do_Range_Check (Sub) then
4687 Set_Do_Range_Check (Sub, False);
9dfe12ae 4688
05f3e139 4689 -- Force evaluation except for the case of a simple name of
4690 -- a non-volatile entity.
9dfe12ae 4691
05f3e139 4692 if not Is_Entity_Name (Sub)
4693 or else Treat_As_Volatile (Entity (Sub))
4694 then
4695 Force_Evaluation (Sub);
4696 end if;
9dfe12ae 4697
05f3e139 4698 if Nkind (A_Idx) = N_Range then
4699 A_Range := A_Idx;
4700
4701 elsif Nkind (A_Idx) = N_Identifier
4702 or else Nkind (A_Idx) = N_Expanded_Name
4703 then
4704 A_Range := Scalar_Range (Entity (A_Idx));
4705
4706 else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication);
4707 A_Range := Range_Expression (Constraint (A_Idx));
4708 end if;
4709
4710 -- For array objects with constant bounds we can generate
4711 -- the index check using the bounds of the type of the index
4712
4713 if Present (A_Ent)
4714 and then Ekind (A_Ent) = E_Variable
4715 and then Is_Constant_Bound (Low_Bound (A_Range))
4716 and then Is_Constant_Bound (High_Bound (A_Range))
4717 then
4718 Range_N :=
4719 Make_Attribute_Reference (Loc,
3f42e2a7 4720 Prefix =>
4721 New_Reference_To (Etype (A_Idx), Loc),
05f3e139 4722 Attribute_Name => Name_Range);
4723
4724 -- For arrays with non-constant bounds we cannot generate
4725 -- the index check using the bounds of the type of the index
4726 -- since it may reference discriminants of some enclosing
4727 -- type. We obtain the bounds directly from the prefix
4728 -- object.
4729
4730 else
4731 if Ind = 1 then
4732 Num := No_List;
4733 else
4734 Num := New_List (Make_Integer_Literal (Loc, Ind));
4735 end if;
4736
4737 Range_N :=
4738 Make_Attribute_Reference (Loc,
4739 Prefix =>
4740 Duplicate_Subexpr_Move_Checks (A, Name_Req => True),
4741 Attribute_Name => Name_Range,
4742 Expressions => Num);
4743 end if;
4744
4745 Insert_Action (N,
4746 Make_Raise_Constraint_Error (Loc,
4747 Condition =>
4748 Make_Not_In (Loc,
4749 Left_Opnd =>
4750 Convert_To (Base_Type (Etype (Sub)),
4751 Duplicate_Subexpr_Move_Checks (Sub)),
4752 Right_Opnd => Range_N),
4753 Reason => CE_Index_Check_Failed));
4754 end if;
4755
4756 A_Idx := Next_Index (A_Idx);
4757 Ind := Ind + 1;
4758 Next (Sub);
4759 end loop;
4760 end;
4761 end if;
9dfe12ae 4762 end Generate_Index_Checks;
4763
4764 --------------------------
4765 -- Generate_Range_Check --
4766 --------------------------
4767
4768 procedure Generate_Range_Check
4769 (N : Node_Id;
4770 Target_Type : Entity_Id;
4771 Reason : RT_Exception_Code)
4772 is
4773 Loc : constant Source_Ptr := Sloc (N);
4774 Source_Type : constant Entity_Id := Etype (N);
4775 Source_Base_Type : constant Entity_Id := Base_Type (Source_Type);
4776 Target_Base_Type : constant Entity_Id := Base_Type (Target_Type);
4777
4778 begin
feff2f05 4779 -- First special case, if the source type is already within the range
4780 -- of the target type, then no check is needed (probably we should have
4781 -- stopped Do_Range_Check from being set in the first place, but better
4782 -- late than later in preventing junk code!
9dfe12ae 4783
feff2f05 4784 -- We do NOT apply this if the source node is a literal, since in this
4785 -- case the literal has already been labeled as having the subtype of
4786 -- the target.
9dfe12ae 4787
7a1dabb3 4788 if In_Subrange_Of (Source_Type, Target_Type)
9dfe12ae 4789 and then not
4790 (Nkind (N) = N_Integer_Literal
4791 or else
4792 Nkind (N) = N_Real_Literal
4793 or else
4794 Nkind (N) = N_Character_Literal
4795 or else
4796 (Is_Entity_Name (N)
4797 and then Ekind (Entity (N)) = E_Enumeration_Literal))
4798 then
4799 return;
4800 end if;
4801
4802 -- We need a check, so force evaluation of the node, so that it does
4803 -- not get evaluated twice (once for the check, once for the actual
4804 -- reference). Such a double evaluation is always a potential source
4805 -- of inefficiency, and is functionally incorrect in the volatile case.
4806
4807 if not Is_Entity_Name (N)
4808 or else Treat_As_Volatile (Entity (N))
4809 then
4810 Force_Evaluation (N);
4811 end if;
4812
feff2f05 4813 -- The easiest case is when Source_Base_Type and Target_Base_Type are
4814 -- the same since in this case we can simply do a direct check of the
4815 -- value of N against the bounds of Target_Type.
9dfe12ae 4816
4817 -- [constraint_error when N not in Target_Type]
4818
4819 -- Note: this is by far the most common case, for example all cases of
4820 -- checks on the RHS of assignments are in this category, but not all
4821 -- cases are like this. Notably conversions can involve two types.
4822
4823 if Source_Base_Type = Target_Base_Type then
4824 Insert_Action (N,
4825 Make_Raise_Constraint_Error (Loc,
4826 Condition =>
4827 Make_Not_In (Loc,
4828 Left_Opnd => Duplicate_Subexpr (N),
4829 Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
4830 Reason => Reason));
4831
4832 -- Next test for the case where the target type is within the bounds
4833 -- of the base type of the source type, since in this case we can
4834 -- simply convert these bounds to the base type of T to do the test.
4835
4836 -- [constraint_error when N not in
4837 -- Source_Base_Type (Target_Type'First)
4838 -- ..
4839 -- Source_Base_Type(Target_Type'Last))]
4840
f2a06be9 4841 -- The conversions will always work and need no check
9dfe12ae 4842
a9b57347 4843 -- Unchecked_Convert_To is used instead of Convert_To to handle the case
4844 -- of converting from an enumeration value to an integer type, such as
4845 -- occurs for the case of generating a range check on Enum'Val(Exp)
4846 -- (which used to be handled by gigi). This is OK, since the conversion
4847 -- itself does not require a check.
4848
7a1dabb3 4849 elsif In_Subrange_Of (Target_Type, Source_Base_Type) then
9dfe12ae 4850 Insert_Action (N,
4851 Make_Raise_Constraint_Error (Loc,
4852 Condition =>
4853 Make_Not_In (Loc,
4854 Left_Opnd => Duplicate_Subexpr (N),
4855
4856 Right_Opnd =>
4857 Make_Range (Loc,
4858 Low_Bound =>
a9b57347 4859 Unchecked_Convert_To (Source_Base_Type,
9dfe12ae 4860 Make_Attribute_Reference (Loc,
4861 Prefix =>
4862 New_Occurrence_Of (Target_Type, Loc),
4863 Attribute_Name => Name_First)),
4864
4865 High_Bound =>
a9b57347 4866 Unchecked_Convert_To (Source_Base_Type,
9dfe12ae 4867 Make_Attribute_Reference (Loc,
4868 Prefix =>
4869 New_Occurrence_Of (Target_Type, Loc),
4870 Attribute_Name => Name_Last)))),
4871 Reason => Reason));
4872
feff2f05 4873 -- Note that at this stage we now that the Target_Base_Type is not in
4874 -- the range of the Source_Base_Type (since even the Target_Type itself
4875 -- is not in this range). It could still be the case that Source_Type is
4876 -- in range of the target base type since we have not checked that case.
9dfe12ae 4877
feff2f05 4878 -- If that is the case, we can freely convert the source to the target,
4879 -- and then test the target result against the bounds.
9dfe12ae 4880
7a1dabb3 4881 elsif In_Subrange_Of (Source_Type, Target_Base_Type) then
9dfe12ae 4882
feff2f05 4883 -- We make a temporary to hold the value of the converted value
4884 -- (converted to the base type), and then we will do the test against
4885 -- this temporary.
9dfe12ae 4886
4887 -- Tnn : constant Target_Base_Type := Target_Base_Type (N);
4888 -- [constraint_error when Tnn not in Target_Type]
4889
4890 -- Then the conversion itself is replaced by an occurrence of Tnn
4891
4892 declare
46eb6933 4893 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9dfe12ae 4894
4895 begin
4896 Insert_Actions (N, New_List (
4897 Make_Object_Declaration (Loc,
4898 Defining_Identifier => Tnn,
4899 Object_Definition =>
4900 New_Occurrence_Of (Target_Base_Type, Loc),
4901 Constant_Present => True,
4902 Expression =>
4903 Make_Type_Conversion (Loc,
4904 Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc),
4905 Expression => Duplicate_Subexpr (N))),
4906
4907 Make_Raise_Constraint_Error (Loc,
4908 Condition =>
4909 Make_Not_In (Loc,
4910 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
4911 Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
4912
4913 Reason => Reason)));
4914
4915 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
2af58f67 4916
4917 -- Set the type of N, because the declaration for Tnn might not
4918 -- be analyzed yet, as is the case if N appears within a record
4919 -- declaration, as a discriminant constraint or expression.
4920
4921 Set_Etype (N, Target_Base_Type);
9dfe12ae 4922 end;
4923
4924 -- At this stage, we know that we have two scalar types, which are
4925 -- directly convertible, and where neither scalar type has a base
4926 -- range that is in the range of the other scalar type.
4927
4928 -- The only way this can happen is with a signed and unsigned type.
4929 -- So test for these two cases:
4930
4931 else
4932 -- Case of the source is unsigned and the target is signed
4933
4934 if Is_Unsigned_Type (Source_Base_Type)
4935 and then not Is_Unsigned_Type (Target_Base_Type)
4936 then
4937 -- If the source is unsigned and the target is signed, then we
4938 -- know that the source is not shorter than the target (otherwise
4939 -- the source base type would be in the target base type range).
4940
feff2f05 4941 -- In other words, the unsigned type is either the same size as
4942 -- the target, or it is larger. It cannot be smaller.
9dfe12ae 4943
4944 pragma Assert
4945 (Esize (Source_Base_Type) >= Esize (Target_Base_Type));
4946
4947 -- We only need to check the low bound if the low bound of the
4948 -- target type is non-negative. If the low bound of the target
4949 -- type is negative, then we know that we will fit fine.
4950
4951 -- If the high bound of the target type is negative, then we
4952 -- know we have a constraint error, since we can't possibly
4953 -- have a negative source.
4954
4955 -- With these two checks out of the way, we can do the check
4956 -- using the source type safely
4957
4958 -- This is definitely the most annoying case!
4959
4960 -- [constraint_error
4961 -- when (Target_Type'First >= 0
4962 -- and then
4963 -- N < Source_Base_Type (Target_Type'First))
4964 -- or else Target_Type'Last < 0
4965 -- or else N > Source_Base_Type (Target_Type'Last)];
4966
4967 -- We turn off all checks since we know that the conversions
4968 -- will work fine, given the guards for negative values.
4969
4970 Insert_Action (N,
4971 Make_Raise_Constraint_Error (Loc,
4972 Condition =>
4973 Make_Or_Else (Loc,
4974 Make_Or_Else (Loc,
4975 Left_Opnd =>
4976 Make_And_Then (Loc,
4977 Left_Opnd => Make_Op_Ge (Loc,
4978 Left_Opnd =>
4979 Make_Attribute_Reference (Loc,
4980 Prefix =>
4981 New_Occurrence_Of (Target_Type, Loc),
4982 Attribute_Name => Name_First),
4983 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
4984
4985 Right_Opnd =>
4986 Make_Op_Lt (Loc,
4987 Left_Opnd => Duplicate_Subexpr (N),
4988 Right_Opnd =>
4989 Convert_To (Source_Base_Type,
4990 Make_Attribute_Reference (Loc,
4991 Prefix =>
4992 New_Occurrence_Of (Target_Type, Loc),
4993 Attribute_Name => Name_First)))),
4994
4995 Right_Opnd =>
4996 Make_Op_Lt (Loc,
4997 Left_Opnd =>
4998 Make_Attribute_Reference (Loc,
4999 Prefix => New_Occurrence_Of (Target_Type, Loc),
5000 Attribute_Name => Name_Last),
5001 Right_Opnd => Make_Integer_Literal (Loc, Uint_0))),
5002
5003 Right_Opnd =>
5004 Make_Op_Gt (Loc,
5005 Left_Opnd => Duplicate_Subexpr (N),
5006 Right_Opnd =>
5007 Convert_To (Source_Base_Type,
5008 Make_Attribute_Reference (Loc,
5009 Prefix => New_Occurrence_Of (Target_Type, Loc),
5010 Attribute_Name => Name_Last)))),
5011
5012 Reason => Reason),
5013 Suppress => All_Checks);
5014
5015 -- Only remaining possibility is that the source is signed and
fc75802a 5016 -- the target is unsigned.
9dfe12ae 5017
5018 else
5019 pragma Assert (not Is_Unsigned_Type (Source_Base_Type)
5020 and then Is_Unsigned_Type (Target_Base_Type));
5021
feff2f05 5022 -- If the source is signed and the target is unsigned, then we
5023 -- know that the target is not shorter than the source (otherwise
5024 -- the target base type would be in the source base type range).
9dfe12ae 5025
feff2f05 5026 -- In other words, the unsigned type is either the same size as
5027 -- the target, or it is larger. It cannot be smaller.
9dfe12ae 5028
feff2f05 5029 -- Clearly we have an error if the source value is negative since
5030 -- no unsigned type can have negative values. If the source type
5031 -- is non-negative, then the check can be done using the target
5032 -- type.
9dfe12ae 5033
5034 -- Tnn : constant Target_Base_Type (N) := Target_Type;
5035
5036 -- [constraint_error
5037 -- when N < 0 or else Tnn not in Target_Type];
5038
feff2f05 5039 -- We turn off all checks for the conversion of N to the target
5040 -- base type, since we generate the explicit check to ensure that
5041 -- the value is non-negative
9dfe12ae 5042
5043 declare
46eb6933 5044 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9dfe12ae 5045
5046 begin
5047 Insert_Actions (N, New_List (
5048 Make_Object_Declaration (Loc,
5049 Defining_Identifier => Tnn,
5050 Object_Definition =>
5051 New_Occurrence_Of (Target_Base_Type, Loc),
5052 Constant_Present => True,
5053 Expression =>
a9b57347 5054 Make_Unchecked_Type_Conversion (Loc,
9dfe12ae 5055 Subtype_Mark =>
5056 New_Occurrence_Of (Target_Base_Type, Loc),
5057 Expression => Duplicate_Subexpr (N))),
5058
5059 Make_Raise_Constraint_Error (Loc,
5060 Condition =>
5061 Make_Or_Else (Loc,
5062 Left_Opnd =>
5063 Make_Op_Lt (Loc,
5064 Left_Opnd => Duplicate_Subexpr (N),
5065 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
5066
5067 Right_Opnd =>
5068 Make_Not_In (Loc,
5069 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
5070 Right_Opnd =>
5071 New_Occurrence_Of (Target_Type, Loc))),
5072
5073 Reason => Reason)),
5074 Suppress => All_Checks);
5075
feff2f05 5076 -- Set the Etype explicitly, because Insert_Actions may have
5077 -- placed the declaration in the freeze list for an enclosing
5078 -- construct, and thus it is not analyzed yet.
9dfe12ae 5079
5080 Set_Etype (Tnn, Target_Base_Type);
5081 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
5082 end;
5083 end if;
5084 end if;
5085 end Generate_Range_Check;
5086
2af58f67 5087 ------------------
5088 -- Get_Check_Id --
5089 ------------------
5090
5091 function Get_Check_Id (N : Name_Id) return Check_Id is
5092 begin
5093 -- For standard check name, we can do a direct computation
5094
5095 if N in First_Check_Name .. Last_Check_Name then
5096 return Check_Id (N - (First_Check_Name - 1));
5097
5098 -- For non-standard names added by pragma Check_Name, search table
5099
5100 else
5101 for J in All_Checks + 1 .. Check_Names.Last loop
5102 if Check_Names.Table (J) = N then
5103 return J;
5104 end if;
5105 end loop;
5106 end if;
5107
5108 -- No matching name found
5109
5110 return No_Check_Id;
5111 end Get_Check_Id;
5112
ee6ba406 5113 ---------------------
5114 -- Get_Discriminal --
5115 ---------------------
5116
5117 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
5118 Loc : constant Source_Ptr := Sloc (E);
5119 D : Entity_Id;
5120 Sc : Entity_Id;
5121
5122 begin
0577b0b1 5123 -- The bound can be a bona fide parameter of a protected operation,
5124 -- rather than a prival encoded as an in-parameter.
5125
5126 if No (Discriminal_Link (Entity (Bound))) then
5127 return Bound;
5128 end if;
5129
2af58f67 5130 -- Climb the scope stack looking for an enclosing protected type. If
5131 -- we run out of scopes, return the bound itself.
5132
5133 Sc := Scope (E);
5134 while Present (Sc) loop
5135 if Sc = Standard_Standard then
5136 return Bound;
5137
5138 elsif Ekind (Sc) = E_Protected_Type then
5139 exit;
5140 end if;
5141
5142 Sc := Scope (Sc);
5143 end loop;
5144
ee6ba406 5145 D := First_Discriminant (Sc);
2af58f67 5146 while Present (D) loop
5147 if Chars (D) = Chars (Bound) then
5148 return New_Occurrence_Of (Discriminal (D), Loc);
5149 end if;
ee6ba406 5150
ee6ba406 5151 Next_Discriminant (D);
5152 end loop;
5153
2af58f67 5154 return Bound;
ee6ba406 5155 end Get_Discriminal;
5156
2af58f67 5157 ----------------------
5158 -- Get_Range_Checks --
5159 ----------------------
5160
5161 function Get_Range_Checks
5162 (Ck_Node : Node_Id;
5163 Target_Typ : Entity_Id;
5164 Source_Typ : Entity_Id := Empty;
5165 Warn_Node : Node_Id := Empty) return Check_Result
5166 is
5167 begin
5168 return Selected_Range_Checks
5169 (Ck_Node, Target_Typ, Source_Typ, Warn_Node);
5170 end Get_Range_Checks;
5171
ee6ba406 5172 ------------------
5173 -- Guard_Access --
5174 ------------------
5175
5176 function Guard_Access
5177 (Cond : Node_Id;
5178 Loc : Source_Ptr;
314a23b6 5179 Ck_Node : Node_Id) return Node_Id
ee6ba406 5180 is
5181 begin
5182 if Nkind (Cond) = N_Or_Else then
5183 Set_Paren_Count (Cond, 1);
5184 end if;
5185
5186 if Nkind (Ck_Node) = N_Allocator then
5187 return Cond;
5188 else
5189 return
5190 Make_And_Then (Loc,
5191 Left_Opnd =>
5192 Make_Op_Ne (Loc,
9dfe12ae 5193 Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
ee6ba406 5194 Right_Opnd => Make_Null (Loc)),
5195 Right_Opnd => Cond);
5196 end if;
5197 end Guard_Access;
5198
5199 -----------------------------
5200 -- Index_Checks_Suppressed --
5201 -----------------------------
5202
5203 function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
5204 begin
9dfe12ae 5205 if Present (E) and then Checks_May_Be_Suppressed (E) then
5206 return Is_Check_Suppressed (E, Index_Check);
5207 else
5208 return Scope_Suppress (Index_Check);
5209 end if;
ee6ba406 5210 end Index_Checks_Suppressed;
5211
5212 ----------------
5213 -- Initialize --
5214 ----------------
5215
5216 procedure Initialize is
5217 begin
5218 for J in Determine_Range_Cache_N'Range loop
5219 Determine_Range_Cache_N (J) := Empty;
5220 end loop;
2af58f67 5221
5222 Check_Names.Init;
5223
5224 for J in Int range 1 .. All_Checks loop
5225 Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1));
5226 end loop;
ee6ba406 5227 end Initialize;
5228
5229 -------------------------
5230 -- Insert_Range_Checks --
5231 -------------------------
5232
5233 procedure Insert_Range_Checks
5234 (Checks : Check_Result;
5235 Node : Node_Id;
5236 Suppress_Typ : Entity_Id;
5237 Static_Sloc : Source_Ptr := No_Location;
5238 Flag_Node : Node_Id := Empty;
5239 Do_Before : Boolean := False)
5240 is
5241 Internal_Flag_Node : Node_Id := Flag_Node;
5242 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
5243
5244 Check_Node : Node_Id;
5245 Checks_On : constant Boolean :=
5246 (not Index_Checks_Suppressed (Suppress_Typ))
5247 or else
5248 (not Range_Checks_Suppressed (Suppress_Typ));
5249
5250 begin
feff2f05 5251 -- For now we just return if Checks_On is false, however this should be
5252 -- enhanced to check for an always True value in the condition and to
5253 -- generate a compilation warning???
ee6ba406 5254
5255 if not Expander_Active or else not Checks_On then
5256 return;
5257 end if;
5258
5259 if Static_Sloc = No_Location then
5260 Internal_Static_Sloc := Sloc (Node);
5261 end if;
5262
5263 if No (Flag_Node) then
5264 Internal_Flag_Node := Node;
5265 end if;
5266
5267 for J in 1 .. 2 loop
5268 exit when No (Checks (J));
5269
5270 if Nkind (Checks (J)) = N_Raise_Constraint_Error
5271 and then Present (Condition (Checks (J)))
5272 then
5273 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
5274 Check_Node := Checks (J);
5275 Mark_Rewrite_Insertion (Check_Node);
5276
5277 if Do_Before then
5278 Insert_Before_And_Analyze (Node, Check_Node);
5279 else
5280 Insert_After_And_Analyze (Node, Check_Node);
5281 end if;
5282
5283 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
5284 end if;
5285
5286 else
5287 Check_Node :=
f15731c4 5288 Make_Raise_Constraint_Error (Internal_Static_Sloc,
5289 Reason => CE_Range_Check_Failed);
ee6ba406 5290 Mark_Rewrite_Insertion (Check_Node);
5291
5292 if Do_Before then
5293 Insert_Before_And_Analyze (Node, Check_Node);
5294 else
5295 Insert_After_And_Analyze (Node, Check_Node);
5296 end if;
5297 end if;
5298 end loop;
5299 end Insert_Range_Checks;
5300
5301 ------------------------
5302 -- Insert_Valid_Check --
5303 ------------------------
5304
5305 procedure Insert_Valid_Check (Expr : Node_Id) is
5306 Loc : constant Source_Ptr := Sloc (Expr);
8b718dab 5307 Exp : Node_Id;
ee6ba406 5308
5309 begin
06ad5813 5310 -- Do not insert if checks off, or if not checking validity or
5311 -- if expression is known to be valid
ee6ba406 5312
0577b0b1 5313 if not Validity_Checks_On
5314 or else Range_Or_Validity_Checks_Suppressed (Expr)
06ad5813 5315 or else Expr_Known_Valid (Expr)
ee6ba406 5316 then
8b718dab 5317 return;
5318 end if;
ee6ba406 5319
8b718dab 5320 -- If we have a checked conversion, then validity check applies to
5321 -- the expression inside the conversion, not the result, since if
5322 -- the expression inside is valid, then so is the conversion result.
ee6ba406 5323
8b718dab 5324 Exp := Expr;
5325 while Nkind (Exp) = N_Type_Conversion loop
5326 Exp := Expression (Exp);
5327 end loop;
5328
0577b0b1 5329 -- We are about to insert the validity check for Exp. We save and
5330 -- reset the Do_Range_Check flag over this validity check, and then
5331 -- put it back for the final original reference (Exp may be rewritten).
5332
5333 declare
5334 DRC : constant Boolean := Do_Range_Check (Exp);
05fcfafb 5335
0577b0b1 5336 begin
5337 Set_Do_Range_Check (Exp, False);
5338
06ad5813 5339 -- Force evaluation to avoid multiple reads for atomic/volatile
5340
5341 if Is_Entity_Name (Exp)
5342 and then Is_Volatile (Entity (Exp))
5343 then
5344 Force_Evaluation (Exp, Name_Req => True);
5345 end if;
5346
0577b0b1 5347 -- Insert the validity check. Note that we do this with validity
5348 -- checks turned off, to avoid recursion, we do not want validity
5349 -- checks on the validity checking code itself!
5350
5351 Insert_Action
5352 (Expr,
5353 Make_Raise_Constraint_Error (Loc,
5354 Condition =>
5355 Make_Op_Not (Loc,
5356 Right_Opnd =>
5357 Make_Attribute_Reference (Loc,
5358 Prefix =>
5359 Duplicate_Subexpr_No_Checks (Exp, Name_Req => True),
5360 Attribute_Name => Name_Valid)),
5361 Reason => CE_Invalid_Data),
5362 Suppress => Validity_Check);
5363
6fb3c314 5364 -- If the expression is a reference to an element of a bit-packed
0577b0b1 5365 -- array, then it is rewritten as a renaming declaration. If the
5366 -- expression is an actual in a call, it has not been expanded,
5367 -- waiting for the proper point at which to do it. The same happens
5368 -- with renamings, so that we have to force the expansion now. This
5369 -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb
5370 -- and exp_ch6.adb.
5371
5372 if Is_Entity_Name (Exp)
5373 and then Nkind (Parent (Entity (Exp))) =
5374 N_Object_Renaming_Declaration
5375 then
5376 declare
5377 Old_Exp : constant Node_Id := Name (Parent (Entity (Exp)));
5378 begin
5379 if Nkind (Old_Exp) = N_Indexed_Component
5380 and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp)))
5381 then
5382 Expand_Packed_Element_Reference (Old_Exp);
5383 end if;
5384 end;
5385 end if;
5386
5387 -- Put back the Do_Range_Check flag on the resulting (possibly
5388 -- rewritten) expression.
5389
5390 -- Note: it might be thought that a validity check is not required
5391 -- when a range check is present, but that's not the case, because
5392 -- the back end is allowed to assume for the range check that the
5393 -- operand is within its declared range (an assumption that validity
5394 -- checking is all about NOT assuming!)
5395
00c403ee 5396 -- Note: no need to worry about Possible_Local_Raise here, it will
5397 -- already have been called if original node has Do_Range_Check set.
5398
0577b0b1 5399 Set_Do_Range_Check (Exp, DRC);
5400 end;
ee6ba406 5401 end Insert_Valid_Check;
5402
fa7497e8 5403 ----------------------------------
5404 -- Install_Null_Excluding_Check --
5405 ----------------------------------
5406
5407 procedure Install_Null_Excluding_Check (N : Node_Id) is
9f294c82 5408 Loc : constant Source_Ptr := Sloc (Parent (N));
84d0d4a5 5409 Typ : constant Entity_Id := Etype (N);
5410
7b31b357 5411 function Safe_To_Capture_In_Parameter_Value return Boolean;
5412 -- Determines if it is safe to capture Known_Non_Null status for an
5413 -- the entity referenced by node N. The caller ensures that N is indeed
5414 -- an entity name. It is safe to capture the non-null status for an IN
5415 -- parameter when the reference occurs within a declaration that is sure
5416 -- to be executed as part of the declarative region.
7870823d 5417
84d0d4a5 5418 procedure Mark_Non_Null;
7870823d 5419 -- After installation of check, if the node in question is an entity
5420 -- name, then mark this entity as non-null if possible.
5421
7b31b357 5422 function Safe_To_Capture_In_Parameter_Value return Boolean is
7870823d 5423 E : constant Entity_Id := Entity (N);
5424 S : constant Entity_Id := Current_Scope;
5425 S_Par : Node_Id;
5426
5427 begin
7b31b357 5428 if Ekind (E) /= E_In_Parameter then
5429 return False;
5430 end if;
7870823d 5431
5432 -- Two initial context checks. We must be inside a subprogram body
5433 -- with declarations and reference must not appear in nested scopes.
5434
7b31b357 5435 if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure)
7870823d 5436 or else Scope (E) /= S
5437 then
5438 return False;
5439 end if;
5440
5441 S_Par := Parent (Parent (S));
5442
5443 if Nkind (S_Par) /= N_Subprogram_Body
5444 or else No (Declarations (S_Par))
5445 then
5446 return False;
5447 end if;
5448
5449 declare
5450 N_Decl : Node_Id;
5451 P : Node_Id;
5452
5453 begin
5454 -- Retrieve the declaration node of N (if any). Note that N
5455 -- may be a part of a complex initialization expression.
5456
5457 P := Parent (N);
5458 N_Decl := Empty;
5459 while Present (P) loop
5460
7b31b357 5461 -- If we have a short circuit form, and we are within the right
5462 -- hand expression, we return false, since the right hand side
5463 -- is not guaranteed to be elaborated.
5464
5465 if Nkind (P) in N_Short_Circuit
5466 and then N = Right_Opnd (P)
5467 then
5468 return False;
5469 end if;
5470
5471 -- Similarly, if we are in a conditional expression and not
5472 -- part of the condition, then we return False, since neither
5473 -- the THEN or ELSE expressions will always be elaborated.
5474
5475 if Nkind (P) = N_Conditional_Expression
5476 and then N /= First (Expressions (P))
5477 then
5478 return False;
e977c0cf 5479 end if;
5480
6fb3c314 5481 -- If we are in a case expression, and not part of the
e977c0cf 5482 -- expression, then we return False, since a particular
5483 -- branch may not always be elaborated
5484
5485 if Nkind (P) = N_Case_Expression
5486 and then N /= Expression (P)
5487 then
5488 return False;
7b31b357 5489 end if;
5490
7870823d 5491 -- While traversing the parent chain, we find that N
5492 -- belongs to a statement, thus it may never appear in
5493 -- a declarative region.
5494
5495 if Nkind (P) in N_Statement_Other_Than_Procedure_Call
5496 or else Nkind (P) = N_Procedure_Call_Statement
5497 then
5498 return False;
5499 end if;
5500
7b31b357 5501 -- If we are at a declaration, record it and exit
5502
7870823d 5503 if Nkind (P) in N_Declaration
5504 and then Nkind (P) not in N_Subprogram_Specification
5505 then
5506 N_Decl := P;
5507 exit;
5508 end if;
5509
5510 P := Parent (P);
5511 end loop;
5512
5513 if No (N_Decl) then
5514 return False;
5515 end if;
5516
5517 return List_Containing (N_Decl) = Declarations (S_Par);
5518 end;
7b31b357 5519 end Safe_To_Capture_In_Parameter_Value;
84d0d4a5 5520
5521 -------------------
5522 -- Mark_Non_Null --
5523 -------------------
5524
5525 procedure Mark_Non_Null is
5526 begin
7870823d 5527 -- Only case of interest is if node N is an entity name
5528
84d0d4a5 5529 if Is_Entity_Name (N) then
7870823d 5530
5531 -- For sure, we want to clear an indication that this is known to
5532 -- be null, since if we get past this check, it definitely is not!
5533
84d0d4a5 5534 Set_Is_Known_Null (Entity (N), False);
5535
7870823d 5536 -- We can mark the entity as known to be non-null if either it is
5537 -- safe to capture the value, or in the case of an IN parameter,
5538 -- which is a constant, if the check we just installed is in the
5539 -- declarative region of the subprogram body. In this latter case,
7b31b357 5540 -- a check is decisive for the rest of the body if the expression
5541 -- is sure to be elaborated, since we know we have to elaborate
5542 -- all declarations before executing the body.
5543
5544 -- Couldn't this always be part of Safe_To_Capture_Value ???
7870823d 5545
5546 if Safe_To_Capture_Value (N, Entity (N))
7b31b357 5547 or else Safe_To_Capture_In_Parameter_Value
7870823d 5548 then
5549 Set_Is_Known_Non_Null (Entity (N));
84d0d4a5 5550 end if;
5551 end if;
5552 end Mark_Non_Null;
5553
5554 -- Start of processing for Install_Null_Excluding_Check
fa7497e8 5555
5556 begin
84d0d4a5 5557 pragma Assert (Is_Access_Type (Typ));
fa7497e8 5558
84d0d4a5 5559 -- No check inside a generic (why not???)
fa7497e8 5560
84d0d4a5 5561 if Inside_A_Generic then
fa7497e8 5562 return;
84d0d4a5 5563 end if;
5564
5565 -- No check needed if known to be non-null
5566
5567 if Known_Non_Null (N) then
05fcfafb 5568 return;
84d0d4a5 5569 end if;
fa7497e8 5570
84d0d4a5 5571 -- If known to be null, here is where we generate a compile time check
5572
5573 if Known_Null (N) then
d16989f1 5574
5575 -- Avoid generating warning message inside init procs
5576
5577 if not Inside_Init_Proc then
5578 Apply_Compile_Time_Constraint_Error
5579 (N,
5580 "null value not allowed here?",
5581 CE_Access_Check_Failed);
5582 else
5583 Insert_Action (N,
5584 Make_Raise_Constraint_Error (Loc,
5585 Reason => CE_Access_Check_Failed));
5586 end if;
5587
84d0d4a5 5588 Mark_Non_Null;
5589 return;
5590 end if;
5591
5592 -- If entity is never assigned, for sure a warning is appropriate
5593
5594 if Is_Entity_Name (N) then
5595 Check_Unset_Reference (N);
fa7497e8 5596 end if;
84d0d4a5 5597
5598 -- No check needed if checks are suppressed on the range. Note that we
5599 -- don't set Is_Known_Non_Null in this case (we could legitimately do
5600 -- so, since the program is erroneous, but we don't like to casually
5601 -- propagate such conclusions from erroneosity).
5602
5603 if Access_Checks_Suppressed (Typ) then
5604 return;
5605 end if;
5606
2af58f67 5607 -- No check needed for access to concurrent record types generated by
5608 -- the expander. This is not just an optimization (though it does indeed
5609 -- remove junk checks). It also avoids generation of junk warnings.
5610
5611 if Nkind (N) in N_Has_Chars
5612 and then Chars (N) = Name_uObject
5613 and then Is_Concurrent_Record_Type
5614 (Directly_Designated_Type (Etype (N)))
5615 then
5616 return;
5617 end if;
5618
84d0d4a5 5619 -- Otherwise install access check
5620
5621 Insert_Action (N,
5622 Make_Raise_Constraint_Error (Loc,
5623 Condition =>
5624 Make_Op_Eq (Loc,
5625 Left_Opnd => Duplicate_Subexpr_Move_Checks (N),
5626 Right_Opnd => Make_Null (Loc)),
5627 Reason => CE_Access_Check_Failed));
5628
5629 Mark_Non_Null;
fa7497e8 5630 end Install_Null_Excluding_Check;
5631
ee6ba406 5632 --------------------------
5633 -- Install_Static_Check --
5634 --------------------------
5635
5636 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
5637 Stat : constant Boolean := Is_Static_Expression (R_Cno);
5638 Typ : constant Entity_Id := Etype (R_Cno);
5639
5640 begin
f15731c4 5641 Rewrite (R_Cno,
5642 Make_Raise_Constraint_Error (Loc,
5643 Reason => CE_Range_Check_Failed));
ee6ba406 5644 Set_Analyzed (R_Cno);
5645 Set_Etype (R_Cno, Typ);
5646 Set_Raises_Constraint_Error (R_Cno);
5647 Set_Is_Static_Expression (R_Cno, Stat);
840ab274 5648
5649 -- Now deal with possible local raise handling
5650
5651 Possible_Local_Raise (R_Cno, Standard_Constraint_Error);
ee6ba406 5652 end Install_Static_Check;
5653
9dfe12ae 5654 ---------------------
5655 -- Kill_All_Checks --
5656 ---------------------
5657
5658 procedure Kill_All_Checks is
5659 begin
5660 if Debug_Flag_CC then
5661 w ("Kill_All_Checks");
5662 end if;
5663
feff2f05 5664 -- We reset the number of saved checks to zero, and also modify all
5665 -- stack entries for statement ranges to indicate that the number of
5666 -- checks at each level is now zero.
9dfe12ae 5667
5668 Num_Saved_Checks := 0;
5669
96da3284 5670 -- Note: the Int'Min here avoids any possibility of J being out of
5671 -- range when called from e.g. Conditional_Statements_Begin.
5672
5673 for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop
9dfe12ae 5674 Saved_Checks_Stack (J) := 0;
5675 end loop;
5676 end Kill_All_Checks;
5677
5678 -----------------
5679 -- Kill_Checks --
5680 -----------------
5681
5682 procedure Kill_Checks (V : Entity_Id) is
5683 begin
5684 if Debug_Flag_CC then
5685 w ("Kill_Checks for entity", Int (V));
5686 end if;
5687
5688 for J in 1 .. Num_Saved_Checks loop
5689 if Saved_Checks (J).Entity = V then
5690 if Debug_Flag_CC then
5691 w (" Checks killed for saved check ", J);
5692 end if;
5693
5694 Saved_Checks (J).Killed := True;
5695 end if;
5696 end loop;
5697 end Kill_Checks;
5698
ee6ba406 5699 ------------------------------
5700 -- Length_Checks_Suppressed --
5701 ------------------------------
5702
5703 function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
5704 begin
9dfe12ae 5705 if Present (E) and then Checks_May_Be_Suppressed (E) then
5706 return Is_Check_Suppressed (E, Length_Check);
5707 else
5708 return Scope_Suppress (Length_Check);
5709 end if;
ee6ba406 5710 end Length_Checks_Suppressed;
5711
5712 --------------------------------
5713 -- Overflow_Checks_Suppressed --
5714 --------------------------------
5715
5716 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
5717 begin
9dfe12ae 5718 if Present (E) and then Checks_May_Be_Suppressed (E) then
5719 return Is_Check_Suppressed (E, Overflow_Check);
5720 else
5721 return Scope_Suppress (Overflow_Check);
5722 end if;
ee6ba406 5723 end Overflow_Checks_Suppressed;
fc75802a 5724
ee6ba406 5725 -----------------------------
5726 -- Range_Checks_Suppressed --
5727 -----------------------------
5728
5729 function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
5730 begin
9dfe12ae 5731 if Present (E) then
5732
5733 -- Note: for now we always suppress range checks on Vax float types,
5734 -- since Gigi does not know how to generate these checks.
5735
5736 if Vax_Float (E) then
5737 return True;
5738 elsif Kill_Range_Checks (E) then
5739 return True;
5740 elsif Checks_May_Be_Suppressed (E) then
5741 return Is_Check_Suppressed (E, Range_Check);
5742 end if;
5743 end if;
ee6ba406 5744
9dfe12ae 5745 return Scope_Suppress (Range_Check);
ee6ba406 5746 end Range_Checks_Suppressed;
5747
0577b0b1 5748 -----------------------------------------
5749 -- Range_Or_Validity_Checks_Suppressed --
5750 -----------------------------------------
5751
5752 -- Note: the coding would be simpler here if we simply made appropriate
5753 -- calls to Range/Validity_Checks_Suppressed, but that would result in
5754 -- duplicated checks which we prefer to avoid.
5755
5756 function Range_Or_Validity_Checks_Suppressed
5757 (Expr : Node_Id) return Boolean
5758 is
5759 begin
5760 -- Immediate return if scope checks suppressed for either check
5761
5762 if Scope_Suppress (Range_Check) or Scope_Suppress (Validity_Check) then
5763 return True;
5764 end if;
5765
5766 -- If no expression, that's odd, decide that checks are suppressed,
5767 -- since we don't want anyone trying to do checks in this case, which
5768 -- is most likely the result of some other error.
5769
5770 if No (Expr) then
5771 return True;
5772 end if;
5773
5774 -- Expression is present, so perform suppress checks on type
5775
5776 declare
5777 Typ : constant Entity_Id := Etype (Expr);
5778 begin
5779 if Vax_Float (Typ) then
5780 return True;
5781 elsif Checks_May_Be_Suppressed (Typ)
5782 and then (Is_Check_Suppressed (Typ, Range_Check)
5783 or else
5784 Is_Check_Suppressed (Typ, Validity_Check))
5785 then
5786 return True;
5787 end if;
5788 end;
5789
5790 -- If expression is an entity name, perform checks on this entity
5791
5792 if Is_Entity_Name (Expr) then
5793 declare
5794 Ent : constant Entity_Id := Entity (Expr);
5795 begin
5796 if Checks_May_Be_Suppressed (Ent) then
5797 return Is_Check_Suppressed (Ent, Range_Check)
5798 or else Is_Check_Suppressed (Ent, Validity_Check);
5799 end if;
5800 end;
5801 end if;
5802
5803 -- If we fall through, no checks suppressed
5804
5805 return False;
5806 end Range_Or_Validity_Checks_Suppressed;
5807
226494a3 5808 -------------------
5809 -- Remove_Checks --
5810 -------------------
5811
5812 procedure Remove_Checks (Expr : Node_Id) is
226494a3 5813 function Process (N : Node_Id) return Traverse_Result;
5814 -- Process a single node during the traversal
5815
8f6e4fd5 5816 procedure Traverse is new Traverse_Proc (Process);
5817 -- The traversal procedure itself
226494a3 5818
5819 -------------
5820 -- Process --
5821 -------------
5822
5823 function Process (N : Node_Id) return Traverse_Result is
5824 begin
5825 if Nkind (N) not in N_Subexpr then
5826 return Skip;
5827 end if;
5828
5829 Set_Do_Range_Check (N, False);
5830
5831 case Nkind (N) is
5832 when N_And_Then =>
8f6e4fd5 5833 Traverse (Left_Opnd (N));
226494a3 5834 return Skip;
5835
5836 when N_Attribute_Reference =>
226494a3 5837 Set_Do_Overflow_Check (N, False);
5838
226494a3 5839 when N_Function_Call =>
5840 Set_Do_Tag_Check (N, False);
5841
226494a3 5842 when N_Op =>
5843 Set_Do_Overflow_Check (N, False);
5844
5845 case Nkind (N) is
5846 when N_Op_Divide =>
5847 Set_Do_Division_Check (N, False);
5848
5849 when N_Op_And =>
5850 Set_Do_Length_Check (N, False);
5851
5852 when N_Op_Mod =>
5853 Set_Do_Division_Check (N, False);
5854
5855 when N_Op_Or =>
5856 Set_Do_Length_Check (N, False);
5857
5858 when N_Op_Rem =>
5859 Set_Do_Division_Check (N, False);
5860
5861 when N_Op_Xor =>
5862 Set_Do_Length_Check (N, False);
5863
5864 when others =>
5865 null;
5866 end case;
5867
5868 when N_Or_Else =>
8f6e4fd5 5869 Traverse (Left_Opnd (N));
226494a3 5870 return Skip;
5871
5872 when N_Selected_Component =>
226494a3 5873 Set_Do_Discriminant_Check (N, False);
5874
226494a3 5875 when N_Type_Conversion =>
9dfe12ae 5876 Set_Do_Length_Check (N, False);
5877 Set_Do_Tag_Check (N, False);
226494a3 5878 Set_Do_Overflow_Check (N, False);
226494a3 5879
5880 when others =>
5881 null;
5882 end case;
5883
5884 return OK;
5885 end Process;
5886
5887 -- Start of processing for Remove_Checks
5888
5889 begin
8f6e4fd5 5890 Traverse (Expr);
226494a3 5891 end Remove_Checks;
5892
ee6ba406 5893 ----------------------------
5894 -- Selected_Length_Checks --
5895 ----------------------------
5896
5897 function Selected_Length_Checks
5898 (Ck_Node : Node_Id;
5899 Target_Typ : Entity_Id;
5900 Source_Typ : Entity_Id;
314a23b6 5901 Warn_Node : Node_Id) return Check_Result
ee6ba406 5902 is
5903 Loc : constant Source_Ptr := Sloc (Ck_Node);
5904 S_Typ : Entity_Id;
5905 T_Typ : Entity_Id;
5906 Expr_Actual : Node_Id;
5907 Exptyp : Entity_Id;
5908 Cond : Node_Id := Empty;
5909 Do_Access : Boolean := False;
5910 Wnode : Node_Id := Warn_Node;
5911 Ret_Result : Check_Result := (Empty, Empty);
5912 Num_Checks : Natural := 0;
5913
5914 procedure Add_Check (N : Node_Id);
5915 -- Adds the action given to Ret_Result if N is non-Empty
5916
5917 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
5918 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
314a23b6 5919 -- Comments required ???
ee6ba406 5920
5921 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
5922 -- True for equal literals and for nodes that denote the same constant
5f260d20 5923 -- entity, even if its value is not a static constant. This includes the
9dfe12ae 5924 -- case of a discriminal reference within an init proc. Removes some
5f260d20 5925 -- obviously superfluous checks.
ee6ba406 5926
5927 function Length_E_Cond
5928 (Exptyp : Entity_Id;
5929 Typ : Entity_Id;
314a23b6 5930 Indx : Nat) return Node_Id;
ee6ba406 5931 -- Returns expression to compute:
5932 -- Typ'Length /= Exptyp'Length
5933
5934 function Length_N_Cond
5935 (Expr : Node_Id;
5936 Typ : Entity_Id;
314a23b6 5937 Indx : Nat) return Node_Id;
ee6ba406 5938 -- Returns expression to compute:
5939 -- Typ'Length /= Expr'Length
5940
5941 ---------------
5942 -- Add_Check --
5943 ---------------
5944
5945 procedure Add_Check (N : Node_Id) is
5946 begin
5947 if Present (N) then
5948
5949 -- For now, ignore attempt to place more than 2 checks ???
5950
5951 if Num_Checks = 2 then
5952 return;
5953 end if;
5954
5955 pragma Assert (Num_Checks <= 1);
5956 Num_Checks := Num_Checks + 1;
5957 Ret_Result (Num_Checks) := N;
5958 end if;
5959 end Add_Check;
5960
5961 ------------------
5962 -- Get_E_Length --
5963 ------------------
5964
5965 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
00c403ee 5966 SE : constant Entity_Id := Scope (E);
ee6ba406 5967 N : Node_Id;
5968 E1 : Entity_Id := E;
ee6ba406 5969
5970 begin
5971 if Ekind (Scope (E)) = E_Record_Type
5972 and then Has_Discriminants (Scope (E))
5973 then
5974 N := Build_Discriminal_Subtype_Of_Component (E);
5975
5976 if Present (N) then
5977 Insert_Action (Ck_Node, N);
5978 E1 := Defining_Identifier (N);
5979 end if;
5980 end if;
5981
5982 if Ekind (E1) = E_String_Literal_Subtype then
5983 return
5984 Make_Integer_Literal (Loc,
5985 Intval => String_Literal_Length (E1));
5986
00c403ee 5987 elsif SE /= Standard_Standard
5988 and then Ekind (Scope (SE)) = E_Protected_Type
5989 and then Has_Discriminants (Scope (SE))
5990 and then Has_Completion (Scope (SE))
ee6ba406 5991 and then not Inside_Init_Proc
5992 then
ee6ba406 5993 -- If the type whose length is needed is a private component
5994 -- constrained by a discriminant, we must expand the 'Length
5995 -- attribute into an explicit computation, using the discriminal
5996 -- of the current protected operation. This is because the actual
5997 -- type of the prival is constructed after the protected opera-
5998 -- tion has been fully expanded.
5999
6000 declare
6001 Indx_Type : Node_Id;
6002 Lo : Node_Id;
6003 Hi : Node_Id;
6004 Do_Expand : Boolean := False;
6005
6006 begin
6007 Indx_Type := First_Index (E);
6008
6009 for J in 1 .. Indx - 1 loop
6010 Next_Index (Indx_Type);
6011 end loop;
6012
2af58f67 6013 Get_Index_Bounds (Indx_Type, Lo, Hi);
ee6ba406 6014
6015 if Nkind (Lo) = N_Identifier
6016 and then Ekind (Entity (Lo)) = E_In_Parameter
6017 then
6018 Lo := Get_Discriminal (E, Lo);
6019 Do_Expand := True;
6020 end if;
6021
6022 if Nkind (Hi) = N_Identifier
6023 and then Ekind (Entity (Hi)) = E_In_Parameter
6024 then
6025 Hi := Get_Discriminal (E, Hi);
6026 Do_Expand := True;
6027 end if;
6028
6029 if Do_Expand then
6030 if not Is_Entity_Name (Lo) then
9dfe12ae 6031 Lo := Duplicate_Subexpr_No_Checks (Lo);
ee6ba406 6032 end if;
6033
6034 if not Is_Entity_Name (Hi) then
9dfe12ae 6035 Lo := Duplicate_Subexpr_No_Checks (Hi);
ee6ba406 6036 end if;
6037
6038 N :=
6039 Make_Op_Add (Loc,
6040 Left_Opnd =>
6041 Make_Op_Subtract (Loc,
6042 Left_Opnd => Hi,
6043 Right_Opnd => Lo),
6044
6045 Right_Opnd => Make_Integer_Literal (Loc, 1));
6046 return N;
6047
6048 else
6049 N :=
6050 Make_Attribute_Reference (Loc,
6051 Attribute_Name => Name_Length,
6052 Prefix =>
6053 New_Occurrence_Of (E1, Loc));
6054
6055 if Indx > 1 then
6056 Set_Expressions (N, New_List (
6057 Make_Integer_Literal (Loc, Indx)));
6058 end if;
6059
6060 return N;
6061 end if;
6062 end;
6063
6064 else
6065 N :=
6066 Make_Attribute_Reference (Loc,
6067 Attribute_Name => Name_Length,
6068 Prefix =>
6069 New_Occurrence_Of (E1, Loc));
6070
6071 if Indx > 1 then
6072 Set_Expressions (N, New_List (
6073 Make_Integer_Literal (Loc, Indx)));
6074 end if;
6075
6076 return N;
ee6ba406 6077 end if;
6078 end Get_E_Length;
6079
6080 ------------------
6081 -- Get_N_Length --
6082 ------------------
6083
6084 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
6085 begin
6086 return
6087 Make_Attribute_Reference (Loc,
6088 Attribute_Name => Name_Length,
6089 Prefix =>
9dfe12ae 6090 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 6091 Expressions => New_List (
6092 Make_Integer_Literal (Loc, Indx)));
ee6ba406 6093 end Get_N_Length;
6094
6095 -------------------
6096 -- Length_E_Cond --
6097 -------------------
6098
6099 function Length_E_Cond
6100 (Exptyp : Entity_Id;
6101 Typ : Entity_Id;
314a23b6 6102 Indx : Nat) return Node_Id
ee6ba406 6103 is
6104 begin
6105 return
6106 Make_Op_Ne (Loc,
6107 Left_Opnd => Get_E_Length (Typ, Indx),
6108 Right_Opnd => Get_E_Length (Exptyp, Indx));
ee6ba406 6109 end Length_E_Cond;
6110
6111 -------------------
6112 -- Length_N_Cond --
6113 -------------------
6114
6115 function Length_N_Cond
6116 (Expr : Node_Id;
6117 Typ : Entity_Id;
314a23b6 6118 Indx : Nat) return Node_Id
ee6ba406 6119 is
6120 begin
6121 return
6122 Make_Op_Ne (Loc,
6123 Left_Opnd => Get_E_Length (Typ, Indx),
6124 Right_Opnd => Get_N_Length (Expr, Indx));
ee6ba406 6125 end Length_N_Cond;
6126
feff2f05 6127 -----------------
6128 -- Same_Bounds --
6129 -----------------
6130
ee6ba406 6131 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
6132 begin
6133 return
6134 (Nkind (L) = N_Integer_Literal
6135 and then Nkind (R) = N_Integer_Literal
6136 and then Intval (L) = Intval (R))
6137
6138 or else
6139 (Is_Entity_Name (L)
6140 and then Ekind (Entity (L)) = E_Constant
6141 and then ((Is_Entity_Name (R)
6142 and then Entity (L) = Entity (R))
6143 or else
6144 (Nkind (R) = N_Type_Conversion
6145 and then Is_Entity_Name (Expression (R))
6146 and then Entity (L) = Entity (Expression (R)))))
6147
6148 or else
6149 (Is_Entity_Name (R)
6150 and then Ekind (Entity (R)) = E_Constant
6151 and then Nkind (L) = N_Type_Conversion
6152 and then Is_Entity_Name (Expression (L))
5f260d20 6153 and then Entity (R) = Entity (Expression (L)))
6154
6155 or else
6156 (Is_Entity_Name (L)
6157 and then Is_Entity_Name (R)
6158 and then Entity (L) = Entity (R)
6159 and then Ekind (Entity (L)) = E_In_Parameter
6160 and then Inside_Init_Proc);
ee6ba406 6161 end Same_Bounds;
6162
6163 -- Start of processing for Selected_Length_Checks
6164
6165 begin
6166 if not Expander_Active then
6167 return Ret_Result;
6168 end if;
6169
6170 if Target_Typ = Any_Type
6171 or else Target_Typ = Any_Composite
6172 or else Raises_Constraint_Error (Ck_Node)
6173 then
6174 return Ret_Result;
6175 end if;
6176
6177 if No (Wnode) then
6178 Wnode := Ck_Node;
6179 end if;
6180
6181 T_Typ := Target_Typ;
6182
6183 if No (Source_Typ) then
6184 S_Typ := Etype (Ck_Node);
6185 else
6186 S_Typ := Source_Typ;
6187 end if;
6188
6189 if S_Typ = Any_Type or else S_Typ = Any_Composite then
6190 return Ret_Result;
6191 end if;
6192
6193 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
6194 S_Typ := Designated_Type (S_Typ);
6195 T_Typ := Designated_Type (T_Typ);
6196 Do_Access := True;
6197
2af58f67 6198 -- A simple optimization for the null case
ee6ba406 6199
2af58f67 6200 if Known_Null (Ck_Node) then
ee6ba406 6201 return Ret_Result;
6202 end if;
6203 end if;
6204
6205 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
6206 if Is_Constrained (T_Typ) then
6207
6208 -- The checking code to be generated will freeze the
6209 -- corresponding array type. However, we must freeze the
6210 -- type now, so that the freeze node does not appear within
6fb3c314 6211 -- the generated conditional expression, but ahead of it.
ee6ba406 6212
6213 Freeze_Before (Ck_Node, T_Typ);
6214
6215 Expr_Actual := Get_Referenced_Object (Ck_Node);
84d0d4a5 6216 Exptyp := Get_Actual_Subtype (Ck_Node);
ee6ba406 6217
6218 if Is_Access_Type (Exptyp) then
6219 Exptyp := Designated_Type (Exptyp);
6220 end if;
6221
6222 -- String_Literal case. This needs to be handled specially be-
6223 -- cause no index types are available for string literals. The
6224 -- condition is simply:
6225
6226 -- T_Typ'Length = string-literal-length
6227
9dfe12ae 6228 if Nkind (Expr_Actual) = N_String_Literal
6229 and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype
6230 then
ee6ba406 6231 Cond :=
6232 Make_Op_Ne (Loc,
6233 Left_Opnd => Get_E_Length (T_Typ, 1),
6234 Right_Opnd =>
6235 Make_Integer_Literal (Loc,
6236 Intval =>
6237 String_Literal_Length (Etype (Expr_Actual))));
6238
6239 -- General array case. Here we have a usable actual subtype for
6240 -- the expression, and the condition is built from the two types
6241 -- (Do_Length):
6242
6243 -- T_Typ'Length /= Exptyp'Length or else
6244 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
6245 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
6246 -- ...
6247
6248 elsif Is_Constrained (Exptyp) then
6249 declare
9dfe12ae 6250 Ndims : constant Nat := Number_Dimensions (T_Typ);
6251
6252 L_Index : Node_Id;
6253 R_Index : Node_Id;
6254 L_Low : Node_Id;
6255 L_High : Node_Id;
6256 R_Low : Node_Id;
6257 R_High : Node_Id;
ee6ba406 6258 L_Length : Uint;
6259 R_Length : Uint;
9dfe12ae 6260 Ref_Node : Node_Id;
ee6ba406 6261
6262 begin
feff2f05 6263 -- At the library level, we need to ensure that the type of
6264 -- the object is elaborated before the check itself is
6265 -- emitted. This is only done if the object is in the
6266 -- current compilation unit, otherwise the type is frozen
6267 -- and elaborated in its unit.
9dfe12ae 6268
6269 if Is_Itype (Exptyp)
6270 and then
6271 Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package
6272 and then
6273 not In_Package_Body (Cunit_Entity (Current_Sem_Unit))
d66aa9f6 6274 and then In_Open_Scopes (Scope (Exptyp))
9dfe12ae 6275 then
6276 Ref_Node := Make_Itype_Reference (Sloc (Ck_Node));
6277 Set_Itype (Ref_Node, Exptyp);
6278 Insert_Action (Ck_Node, Ref_Node);
6279 end if;
6280
ee6ba406 6281 L_Index := First_Index (T_Typ);
6282 R_Index := First_Index (Exptyp);
6283
6284 for Indx in 1 .. Ndims loop
6285 if not (Nkind (L_Index) = N_Raise_Constraint_Error
f15731c4 6286 or else
6287 Nkind (R_Index) = N_Raise_Constraint_Error)
ee6ba406 6288 then
6289 Get_Index_Bounds (L_Index, L_Low, L_High);
6290 Get_Index_Bounds (R_Index, R_Low, R_High);
6291
6292 -- Deal with compile time length check. Note that we
6293 -- skip this in the access case, because the access
6294 -- value may be null, so we cannot know statically.
6295
6296 if not Do_Access
6297 and then Compile_Time_Known_Value (L_Low)
6298 and then Compile_Time_Known_Value (L_High)
6299 and then Compile_Time_Known_Value (R_Low)
6300 and then Compile_Time_Known_Value (R_High)
6301 then
6302 if Expr_Value (L_High) >= Expr_Value (L_Low) then
6303 L_Length := Expr_Value (L_High) -
6304 Expr_Value (L_Low) + 1;
6305 else
6306 L_Length := UI_From_Int (0);
6307 end if;
6308
6309 if Expr_Value (R_High) >= Expr_Value (R_Low) then
6310 R_Length := Expr_Value (R_High) -
6311 Expr_Value (R_Low) + 1;
6312 else
6313 R_Length := UI_From_Int (0);
6314 end if;
6315
6316 if L_Length > R_Length then
6317 Add_Check
6318 (Compile_Time_Constraint_Error
6319 (Wnode, "too few elements for}?", T_Typ));
6320
6321 elsif L_Length < R_Length then
6322 Add_Check
6323 (Compile_Time_Constraint_Error
6324 (Wnode, "too many elements for}?", T_Typ));
6325 end if;
6326
6327 -- The comparison for an individual index subtype
6328 -- is omitted if the corresponding index subtypes
6329 -- statically match, since the result is known to
6330 -- be true. Note that this test is worth while even
6331 -- though we do static evaluation, because non-static
6332 -- subtypes can statically match.
6333
6334 elsif not
6335 Subtypes_Statically_Match
6336 (Etype (L_Index), Etype (R_Index))
6337
6338 and then not
6339 (Same_Bounds (L_Low, R_Low)
6340 and then Same_Bounds (L_High, R_High))
6341 then
6342 Evolve_Or_Else
6343 (Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
6344 end if;
6345
6346 Next (L_Index);
6347 Next (R_Index);
6348 end if;
6349 end loop;
6350 end;
6351
6352 -- Handle cases where we do not get a usable actual subtype that
6353 -- is constrained. This happens for example in the function call
6354 -- and explicit dereference cases. In these cases, we have to get
6355 -- the length or range from the expression itself, making sure we
6356 -- do not evaluate it more than once.
6357
6358 -- Here Ck_Node is the original expression, or more properly the
feff2f05 6359 -- result of applying Duplicate_Expr to the original tree, forcing
6360 -- the result to be a name.
ee6ba406 6361
6362 else
6363 declare
9dfe12ae 6364 Ndims : constant Nat := Number_Dimensions (T_Typ);
ee6ba406 6365
6366 begin
6367 -- Build the condition for the explicit dereference case
6368
6369 for Indx in 1 .. Ndims loop
6370 Evolve_Or_Else
6371 (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
6372 end loop;
6373 end;
6374 end if;
6375 end if;
6376 end if;
6377
6378 -- Construct the test and insert into the tree
6379
6380 if Present (Cond) then
6381 if Do_Access then
6382 Cond := Guard_Access (Cond, Loc, Ck_Node);
6383 end if;
6384
f15731c4 6385 Add_Check
6386 (Make_Raise_Constraint_Error (Loc,
6387 Condition => Cond,
6388 Reason => CE_Length_Check_Failed));
ee6ba406 6389 end if;
6390
6391 return Ret_Result;
ee6ba406 6392 end Selected_Length_Checks;
6393
6394 ---------------------------
6395 -- Selected_Range_Checks --
6396 ---------------------------
6397
6398 function Selected_Range_Checks
6399 (Ck_Node : Node_Id;
6400 Target_Typ : Entity_Id;
6401 Source_Typ : Entity_Id;
314a23b6 6402 Warn_Node : Node_Id) return Check_Result
ee6ba406 6403 is
6404 Loc : constant Source_Ptr := Sloc (Ck_Node);
6405 S_Typ : Entity_Id;
6406 T_Typ : Entity_Id;
6407 Expr_Actual : Node_Id;
6408 Exptyp : Entity_Id;
6409 Cond : Node_Id := Empty;
6410 Do_Access : Boolean := False;
6411 Wnode : Node_Id := Warn_Node;
6412 Ret_Result : Check_Result := (Empty, Empty);
6413 Num_Checks : Integer := 0;
6414
6415 procedure Add_Check (N : Node_Id);
6416 -- Adds the action given to Ret_Result if N is non-Empty
6417
6418 function Discrete_Range_Cond
6419 (Expr : Node_Id;
314a23b6 6420 Typ : Entity_Id) return Node_Id;
ee6ba406 6421 -- Returns expression to compute:
6422 -- Low_Bound (Expr) < Typ'First
6423 -- or else
6424 -- High_Bound (Expr) > Typ'Last
6425
6426 function Discrete_Expr_Cond
6427 (Expr : Node_Id;
314a23b6 6428 Typ : Entity_Id) return Node_Id;
ee6ba406 6429 -- Returns expression to compute:
6430 -- Expr < Typ'First
6431 -- or else
6432 -- Expr > Typ'Last
6433
6434 function Get_E_First_Or_Last
3cb12758 6435 (Loc : Source_Ptr;
6436 E : Entity_Id;
ee6ba406 6437 Indx : Nat;
314a23b6 6438 Nam : Name_Id) return Node_Id;
79212397 6439 -- Returns an attribute reference
ee6ba406 6440 -- E'First or E'Last
79212397 6441 -- with a source location of Loc.
f73ee678 6442 --
79212397 6443 -- Nam is Name_First or Name_Last, according to which attribute is
6444 -- desired. If Indx is non-zero, it is passed as a literal in the
6445 -- Expressions of the attribute reference (identifying the desired
6446 -- array dimension).
ee6ba406 6447
6448 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
6449 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
6450 -- Returns expression to compute:
9dfe12ae 6451 -- N'First or N'Last using Duplicate_Subexpr_No_Checks
ee6ba406 6452
6453 function Range_E_Cond
6454 (Exptyp : Entity_Id;
6455 Typ : Entity_Id;
6456 Indx : Nat)
6457 return Node_Id;
6458 -- Returns expression to compute:
6459 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
6460
6461 function Range_Equal_E_Cond
6462 (Exptyp : Entity_Id;
6463 Typ : Entity_Id;
314a23b6 6464 Indx : Nat) return Node_Id;
ee6ba406 6465 -- Returns expression to compute:
6466 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
6467
6468 function Range_N_Cond
6469 (Expr : Node_Id;
6470 Typ : Entity_Id;
314a23b6 6471 Indx : Nat) return Node_Id;
ee6ba406 6472 -- Return expression to compute:
6473 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
6474
6475 ---------------
6476 -- Add_Check --
6477 ---------------
6478
6479 procedure Add_Check (N : Node_Id) is
6480 begin
6481 if Present (N) then
6482
6483 -- For now, ignore attempt to place more than 2 checks ???
6484
6485 if Num_Checks = 2 then
6486 return;
6487 end if;
6488
6489 pragma Assert (Num_Checks <= 1);
6490 Num_Checks := Num_Checks + 1;
6491 Ret_Result (Num_Checks) := N;
6492 end if;
6493 end Add_Check;
6494
6495 -------------------------
6496 -- Discrete_Expr_Cond --
6497 -------------------------
6498
6499 function Discrete_Expr_Cond
6500 (Expr : Node_Id;
314a23b6 6501 Typ : Entity_Id) return Node_Id
ee6ba406 6502 is
6503 begin
6504 return
6505 Make_Or_Else (Loc,
6506 Left_Opnd =>
6507 Make_Op_Lt (Loc,
6508 Left_Opnd =>
9dfe12ae 6509 Convert_To (Base_Type (Typ),
6510 Duplicate_Subexpr_No_Checks (Expr)),
ee6ba406 6511 Right_Opnd =>
6512 Convert_To (Base_Type (Typ),
3cb12758 6513 Get_E_First_Or_Last (Loc, Typ, 0, Name_First))),
ee6ba406 6514
6515 Right_Opnd =>
6516 Make_Op_Gt (Loc,
6517 Left_Opnd =>
9dfe12ae 6518 Convert_To (Base_Type (Typ),
6519 Duplicate_Subexpr_No_Checks (Expr)),
ee6ba406 6520 Right_Opnd =>
6521 Convert_To
6522 (Base_Type (Typ),
3cb12758 6523 Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))));
ee6ba406 6524 end Discrete_Expr_Cond;
6525
6526 -------------------------
6527 -- Discrete_Range_Cond --
6528 -------------------------
6529
6530 function Discrete_Range_Cond
6531 (Expr : Node_Id;
314a23b6 6532 Typ : Entity_Id) return Node_Id
ee6ba406 6533 is
6534 LB : Node_Id := Low_Bound (Expr);
6535 HB : Node_Id := High_Bound (Expr);
6536
6537 Left_Opnd : Node_Id;
6538 Right_Opnd : Node_Id;
6539
6540 begin
6541 if Nkind (LB) = N_Identifier
feff2f05 6542 and then Ekind (Entity (LB)) = E_Discriminant
6543 then
ee6ba406 6544 LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
6545 end if;
6546
6547 if Nkind (HB) = N_Identifier
feff2f05 6548 and then Ekind (Entity (HB)) = E_Discriminant
6549 then
ee6ba406 6550 HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
6551 end if;
6552
6553 Left_Opnd :=
6554 Make_Op_Lt (Loc,
6555 Left_Opnd =>
6556 Convert_To
9dfe12ae 6557 (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)),
ee6ba406 6558
6559 Right_Opnd =>
6560 Convert_To
3cb12758 6561 (Base_Type (Typ),
6562 Get_E_First_Or_Last (Loc, Typ, 0, Name_First)));
ee6ba406 6563
6564 if Base_Type (Typ) = Typ then
6565 return Left_Opnd;
6566
6567 elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ)))
6568 and then
6569 Compile_Time_Known_Value (High_Bound (Scalar_Range
6570 (Base_Type (Typ))))
6571 then
6572 if Is_Floating_Point_Type (Typ) then
6573 if Expr_Value_R (High_Bound (Scalar_Range (Typ))) =
6574 Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ))))
6575 then
6576 return Left_Opnd;
6577 end if;
6578
6579 else
6580 if Expr_Value (High_Bound (Scalar_Range (Typ))) =
6581 Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ))))
6582 then
6583 return Left_Opnd;
6584 end if;
6585 end if;
6586 end if;
6587
6588 Right_Opnd :=
6589 Make_Op_Gt (Loc,
6590 Left_Opnd =>
6591 Convert_To
9dfe12ae 6592 (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)),
ee6ba406 6593
6594 Right_Opnd =>
6595 Convert_To
6596 (Base_Type (Typ),
3cb12758 6597 Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)));
ee6ba406 6598
6599 return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
6600 end Discrete_Range_Cond;
6601
6602 -------------------------
6603 -- Get_E_First_Or_Last --
6604 -------------------------
6605
6606 function Get_E_First_Or_Last
3cb12758 6607 (Loc : Source_Ptr;
6608 E : Entity_Id;
ee6ba406 6609 Indx : Nat;
314a23b6 6610 Nam : Name_Id) return Node_Id
ee6ba406 6611 is
3cb12758 6612 Exprs : List_Id;
ee6ba406 6613 begin
3cb12758 6614 if Indx > 0 then
6615 Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx)));
ee6ba406 6616 else
3cb12758 6617 Exprs := No_List;
ee6ba406 6618 end if;
6619
3cb12758 6620 return Make_Attribute_Reference (Loc,
6621 Prefix => New_Occurrence_Of (E, Loc),
6622 Attribute_Name => Nam,
6623 Expressions => Exprs);
ee6ba406 6624 end Get_E_First_Or_Last;
6625
6626 -----------------
6627 -- Get_N_First --
6628 -----------------
6629
6630 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
6631 begin
6632 return
6633 Make_Attribute_Reference (Loc,
6634 Attribute_Name => Name_First,
6635 Prefix =>
9dfe12ae 6636 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 6637 Expressions => New_List (
6638 Make_Integer_Literal (Loc, Indx)));
ee6ba406 6639 end Get_N_First;
6640
6641 ----------------
6642 -- Get_N_Last --
6643 ----------------
6644
6645 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
6646 begin
6647 return
6648 Make_Attribute_Reference (Loc,
6649 Attribute_Name => Name_Last,
6650 Prefix =>
9dfe12ae 6651 Duplicate_Subexpr_No_Checks (N, Name_Req => True),
ee6ba406 6652 Expressions => New_List (
6653 Make_Integer_Literal (Loc, Indx)));
ee6ba406 6654 end Get_N_Last;
6655
6656 ------------------
6657 -- Range_E_Cond --
6658 ------------------
6659
6660 function Range_E_Cond
6661 (Exptyp : Entity_Id;
6662 Typ : Entity_Id;
314a23b6 6663 Indx : Nat) return Node_Id
ee6ba406 6664 is
6665 begin
6666 return
6667 Make_Or_Else (Loc,
6668 Left_Opnd =>
6669 Make_Op_Lt (Loc,
3cb12758 6670 Left_Opnd =>
6671 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First),
6672 Right_Opnd =>
6673 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
ee6ba406 6674
6675 Right_Opnd =>
6676 Make_Op_Gt (Loc,
3cb12758 6677 Left_Opnd =>
6678 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last),
6679 Right_Opnd =>
6680 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 6681 end Range_E_Cond;
6682
6683 ------------------------
6684 -- Range_Equal_E_Cond --
6685 ------------------------
6686
6687 function Range_Equal_E_Cond
6688 (Exptyp : Entity_Id;
6689 Typ : Entity_Id;
314a23b6 6690 Indx : Nat) return Node_Id
ee6ba406 6691 is
6692 begin
6693 return
6694 Make_Or_Else (Loc,
6695 Left_Opnd =>
6696 Make_Op_Ne (Loc,
3cb12758 6697 Left_Opnd =>
6698 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First),
6699 Right_Opnd =>
6700 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
6701
ee6ba406 6702 Right_Opnd =>
6703 Make_Op_Ne (Loc,
3cb12758 6704 Left_Opnd =>
6705 Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last),
6706 Right_Opnd =>
6707 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 6708 end Range_Equal_E_Cond;
6709
6710 ------------------
6711 -- Range_N_Cond --
6712 ------------------
6713
6714 function Range_N_Cond
6715 (Expr : Node_Id;
6716 Typ : Entity_Id;
314a23b6 6717 Indx : Nat) return Node_Id
ee6ba406 6718 is
6719 begin
6720 return
6721 Make_Or_Else (Loc,
6722 Left_Opnd =>
6723 Make_Op_Lt (Loc,
3cb12758 6724 Left_Opnd =>
6725 Get_N_First (Expr, Indx),
6726 Right_Opnd =>
6727 Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)),
ee6ba406 6728
6729 Right_Opnd =>
6730 Make_Op_Gt (Loc,
3cb12758 6731 Left_Opnd =>
6732 Get_N_Last (Expr, Indx),
6733 Right_Opnd =>
6734 Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last)));
ee6ba406 6735 end Range_N_Cond;
6736
6737 -- Start of processing for Selected_Range_Checks
6738
6739 begin
6740 if not Expander_Active then
6741 return Ret_Result;
6742 end if;
6743
6744 if Target_Typ = Any_Type
6745 or else Target_Typ = Any_Composite
6746 or else Raises_Constraint_Error (Ck_Node)
6747 then
6748 return Ret_Result;
6749 end if;
6750
6751 if No (Wnode) then
6752 Wnode := Ck_Node;
6753 end if;
6754
6755 T_Typ := Target_Typ;
6756
6757 if No (Source_Typ) then
6758 S_Typ := Etype (Ck_Node);
6759 else
6760 S_Typ := Source_Typ;
6761 end if;
6762
6763 if S_Typ = Any_Type or else S_Typ = Any_Composite then
6764 return Ret_Result;
6765 end if;
6766
6767 -- The order of evaluating T_Typ before S_Typ seems to be critical
6768 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
6769 -- in, and since Node can be an N_Range node, it might be invalid.
6770 -- Should there be an assert check somewhere for taking the Etype of
6771 -- an N_Range node ???
6772
6773 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
6774 S_Typ := Designated_Type (S_Typ);
6775 T_Typ := Designated_Type (T_Typ);
6776 Do_Access := True;
6777
2af58f67 6778 -- A simple optimization for the null case
ee6ba406 6779
2af58f67 6780 if Known_Null (Ck_Node) then
ee6ba406 6781 return Ret_Result;
6782 end if;
6783 end if;
6784
6785 -- For an N_Range Node, check for a null range and then if not
6786 -- null generate a range check action.
6787
6788 if Nkind (Ck_Node) = N_Range then
6789
6790 -- There's no point in checking a range against itself
6791
6792 if Ck_Node = Scalar_Range (T_Typ) then
6793 return Ret_Result;
6794 end if;
6795
6796 declare
6797 T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
6798 T_HB : constant Node_Id := Type_High_Bound (T_Typ);
eefa141b 6799 Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB);
6800 Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB);
ee6ba406 6801
eefa141b 6802 LB : Node_Id := Low_Bound (Ck_Node);
6803 HB : Node_Id := High_Bound (Ck_Node);
6804 Known_LB : Boolean;
6805 Known_HB : Boolean;
6806
6807 Null_Range : Boolean;
ee6ba406 6808 Out_Of_Range_L : Boolean;
6809 Out_Of_Range_H : Boolean;
6810
6811 begin
eefa141b 6812 -- Compute what is known at compile time
6813
6814 if Known_T_LB and Known_T_HB then
6815 if Compile_Time_Known_Value (LB) then
6816 Known_LB := True;
6817
6818 -- There's no point in checking that a bound is within its
6819 -- own range so pretend that it is known in this case. First
6820 -- deal with low bound.
6821
6822 elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype
6823 and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ)
6824 then
6825 LB := T_LB;
6826 Known_LB := True;
6827
6828 else
6829 Known_LB := False;
6830 end if;
6831
6832 -- Likewise for the high bound
6833
6834 if Compile_Time_Known_Value (HB) then
6835 Known_HB := True;
6836
6837 elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype
6838 and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ)
6839 then
6840 HB := T_HB;
6841 Known_HB := True;
6842
6843 else
6844 Known_HB := False;
6845 end if;
6846 end if;
6847
6848 -- Check for case where everything is static and we can do the
6849 -- check at compile time. This is skipped if we have an access
6850 -- type, since the access value may be null.
6851
6852 -- ??? This code can be improved since you only need to know that
6853 -- the two respective bounds (LB & T_LB or HB & T_HB) are known at
6854 -- compile time to emit pertinent messages.
6855
6856 if Known_T_LB and Known_T_HB and Known_LB and Known_HB
6857 and not Do_Access
ee6ba406 6858 then
6859 -- Floating-point case
6860
6861 if Is_Floating_Point_Type (S_Typ) then
6862 Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
6863 Out_Of_Range_L :=
6864 (Expr_Value_R (LB) < Expr_Value_R (T_LB))
eefa141b 6865 or else
ee6ba406 6866 (Expr_Value_R (LB) > Expr_Value_R (T_HB));
6867
6868 Out_Of_Range_H :=
6869 (Expr_Value_R (HB) > Expr_Value_R (T_HB))
eefa141b 6870 or else
ee6ba406 6871 (Expr_Value_R (HB) < Expr_Value_R (T_LB));
6872
6873 -- Fixed or discrete type case
6874
6875 else
6876 Null_Range := Expr_Value (HB) < Expr_Value (LB);
6877 Out_Of_Range_L :=
6878 (Expr_Value (LB) < Expr_Value (T_LB))
eefa141b 6879 or else
ee6ba406 6880 (Expr_Value (LB) > Expr_Value (T_HB));
6881
6882 Out_Of_Range_H :=
6883 (Expr_Value (HB) > Expr_Value (T_HB))
eefa141b 6884 or else
ee6ba406 6885 (Expr_Value (HB) < Expr_Value (T_LB));
6886 end if;
6887
6888 if not Null_Range then
6889 if Out_Of_Range_L then
6890 if No (Warn_Node) then
6891 Add_Check
6892 (Compile_Time_Constraint_Error
6893 (Low_Bound (Ck_Node),
6894 "static value out of range of}?", T_Typ));
6895
6896 else
6897 Add_Check
6898 (Compile_Time_Constraint_Error
6899 (Wnode,
6900 "static range out of bounds of}?", T_Typ));
6901 end if;
6902 end if;
6903
6904 if Out_Of_Range_H then
6905 if No (Warn_Node) then
6906 Add_Check
6907 (Compile_Time_Constraint_Error
6908 (High_Bound (Ck_Node),
6909 "static value out of range of}?", T_Typ));
6910
6911 else
6912 Add_Check
6913 (Compile_Time_Constraint_Error
6914 (Wnode,
6915 "static range out of bounds of}?", T_Typ));
6916 end if;
6917 end if;
ee6ba406 6918 end if;
6919
6920 else
6921 declare
6922 LB : Node_Id := Low_Bound (Ck_Node);
6923 HB : Node_Id := High_Bound (Ck_Node);
6924
6925 begin
feff2f05 6926 -- If either bound is a discriminant and we are within the
6927 -- record declaration, it is a use of the discriminant in a
6928 -- constraint of a component, and nothing can be checked
6929 -- here. The check will be emitted within the init proc.
6930 -- Before then, the discriminal has no real meaning.
6931 -- Similarly, if the entity is a discriminal, there is no
6932 -- check to perform yet.
6933
6934 -- The same holds within a discriminated synchronized type,
6935 -- where the discriminant may constrain a component or an
6936 -- entry family.
ee6ba406 6937
6938 if Nkind (LB) = N_Identifier
0577b0b1 6939 and then Denotes_Discriminant (LB, True)
ee6ba406 6940 then
0577b0b1 6941 if Current_Scope = Scope (Entity (LB))
6942 or else Is_Concurrent_Type (Current_Scope)
6943 or else Ekind (Entity (LB)) /= E_Discriminant
6944 then
ee6ba406 6945 return Ret_Result;
6946 else
6947 LB :=
6948 New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
6949 end if;
6950 end if;
6951
6952 if Nkind (HB) = N_Identifier
0577b0b1 6953 and then Denotes_Discriminant (HB, True)
ee6ba406 6954 then
0577b0b1 6955 if Current_Scope = Scope (Entity (HB))
6956 or else Is_Concurrent_Type (Current_Scope)
6957 or else Ekind (Entity (HB)) /= E_Discriminant
6958 then
ee6ba406 6959 return Ret_Result;
6960 else
6961 HB :=
6962 New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
6963 end if;
6964 end if;
6965
6966 Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
6967 Set_Paren_Count (Cond, 1);
6968
6969 Cond :=
6970 Make_And_Then (Loc,
6971 Left_Opnd =>
6972 Make_Op_Ge (Loc,
9dfe12ae 6973 Left_Opnd => Duplicate_Subexpr_No_Checks (HB),
6974 Right_Opnd => Duplicate_Subexpr_No_Checks (LB)),
ee6ba406 6975 Right_Opnd => Cond);
6976 end;
ee6ba406 6977 end if;
6978 end;
6979
6980 elsif Is_Scalar_Type (S_Typ) then
6981
6982 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
6983 -- except the above simply sets a flag in the node and lets
6984 -- gigi generate the check base on the Etype of the expression.
6985 -- Sometimes, however we want to do a dynamic check against an
6986 -- arbitrary target type, so we do that here.
6987
6988 if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
6989 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
6990
6991 -- For literals, we can tell if the constraint error will be
6992 -- raised at compile time, so we never need a dynamic check, but
6993 -- if the exception will be raised, then post the usual warning,
6994 -- and replace the literal with a raise constraint error
6995 -- expression. As usual, skip this for access types
6996
6997 elsif Compile_Time_Known_Value (Ck_Node)
6998 and then not Do_Access
6999 then
7000 declare
7001 LB : constant Node_Id := Type_Low_Bound (T_Typ);
7002 UB : constant Node_Id := Type_High_Bound (T_Typ);
7003
7004 Out_Of_Range : Boolean;
7005 Static_Bounds : constant Boolean :=
7006 Compile_Time_Known_Value (LB)
7007 and Compile_Time_Known_Value (UB);
7008
7009 begin
7010 -- Following range tests should use Sem_Eval routine ???
7011
7012 if Static_Bounds then
7013 if Is_Floating_Point_Type (S_Typ) then
7014 Out_Of_Range :=
7015 (Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
7016 or else
7017 (Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
7018
eefa141b 7019 -- Fixed or discrete type
7020
7021 else
ee6ba406 7022 Out_Of_Range :=
7023 Expr_Value (Ck_Node) < Expr_Value (LB)
7024 or else
7025 Expr_Value (Ck_Node) > Expr_Value (UB);
7026 end if;
7027
eefa141b 7028 -- Bounds of the type are static and the literal is out of
7029 -- range so output a warning message.
ee6ba406 7030
7031 if Out_Of_Range then
7032 if No (Warn_Node) then
7033 Add_Check
7034 (Compile_Time_Constraint_Error
7035 (Ck_Node,
7036 "static value out of range of}?", T_Typ));
7037
7038 else
7039 Add_Check
7040 (Compile_Time_Constraint_Error
7041 (Wnode,
7042 "static value out of range of}?", T_Typ));
7043 end if;
7044 end if;
7045
7046 else
7047 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
7048 end if;
7049 end;
7050
7051 -- Here for the case of a non-static expression, we need a runtime
7052 -- check unless the source type range is guaranteed to be in the
7053 -- range of the target type.
7054
7055 else
7a1dabb3 7056 if not In_Subrange_Of (S_Typ, T_Typ) then
ee6ba406 7057 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
7058 end if;
7059 end if;
7060 end if;
7061
7062 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
7063 if Is_Constrained (T_Typ) then
7064
7065 Expr_Actual := Get_Referenced_Object (Ck_Node);
7066 Exptyp := Get_Actual_Subtype (Expr_Actual);
7067
7068 if Is_Access_Type (Exptyp) then
7069 Exptyp := Designated_Type (Exptyp);
7070 end if;
7071
7072 -- String_Literal case. This needs to be handled specially be-
7073 -- cause no index types are available for string literals. The
7074 -- condition is simply:
7075
7076 -- T_Typ'Length = string-literal-length
7077
7078 if Nkind (Expr_Actual) = N_String_Literal then
7079 null;
7080
7081 -- General array case. Here we have a usable actual subtype for
7082 -- the expression, and the condition is built from the two types
7083
7084 -- T_Typ'First < Exptyp'First or else
7085 -- T_Typ'Last > Exptyp'Last or else
7086 -- T_Typ'First(1) < Exptyp'First(1) or else
7087 -- T_Typ'Last(1) > Exptyp'Last(1) or else
7088 -- ...
7089
7090 elsif Is_Constrained (Exptyp) then
7091 declare
9dfe12ae 7092 Ndims : constant Nat := Number_Dimensions (T_Typ);
7093
ee6ba406 7094 L_Index : Node_Id;
7095 R_Index : Node_Id;
ee6ba406 7096
7097 begin
7098 L_Index := First_Index (T_Typ);
7099 R_Index := First_Index (Exptyp);
7100
7101 for Indx in 1 .. Ndims loop
7102 if not (Nkind (L_Index) = N_Raise_Constraint_Error
f15731c4 7103 or else
7104 Nkind (R_Index) = N_Raise_Constraint_Error)
ee6ba406 7105 then
ee6ba406 7106 -- Deal with compile time length check. Note that we
7107 -- skip this in the access case, because the access
7108 -- value may be null, so we cannot know statically.
7109
7110 if not
7111 Subtypes_Statically_Match
7112 (Etype (L_Index), Etype (R_Index))
7113 then
7114 -- If the target type is constrained then we
7115 -- have to check for exact equality of bounds
7116 -- (required for qualified expressions).
7117
7118 if Is_Constrained (T_Typ) then
7119 Evolve_Or_Else
7120 (Cond,
7121 Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
ee6ba406 7122 else
7123 Evolve_Or_Else
7124 (Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
7125 end if;
7126 end if;
7127
7128 Next (L_Index);
7129 Next (R_Index);
ee6ba406 7130 end if;
7131 end loop;
7132 end;
7133
7134 -- Handle cases where we do not get a usable actual subtype that
7135 -- is constrained. This happens for example in the function call
7136 -- and explicit dereference cases. In these cases, we have to get
7137 -- the length or range from the expression itself, making sure we
7138 -- do not evaluate it more than once.
7139
7140 -- Here Ck_Node is the original expression, or more properly the
7141 -- result of applying Duplicate_Expr to the original tree,
7142 -- forcing the result to be a name.
7143
7144 else
7145 declare
9dfe12ae 7146 Ndims : constant Nat := Number_Dimensions (T_Typ);
ee6ba406 7147
7148 begin
7149 -- Build the condition for the explicit dereference case
7150
7151 for Indx in 1 .. Ndims loop
7152 Evolve_Or_Else
7153 (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
7154 end loop;
7155 end;
ee6ba406 7156 end if;
7157
7158 else
feff2f05 7159 -- For a conversion to an unconstrained array type, generate an
7160 -- Action to check that the bounds of the source value are within
7161 -- the constraints imposed by the target type (RM 4.6(38)). No
7162 -- check is needed for a conversion to an access to unconstrained
7163 -- array type, as 4.6(24.15/2) requires the designated subtypes
7164 -- of the two access types to statically match.
7165
7166 if Nkind (Parent (Ck_Node)) = N_Type_Conversion
7167 and then not Do_Access
7168 then
ee6ba406 7169 declare
7170 Opnd_Index : Node_Id;
7171 Targ_Index : Node_Id;
00c403ee 7172 Opnd_Range : Node_Id;
ee6ba406 7173
7174 begin
feff2f05 7175 Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node));
ee6ba406 7176 Targ_Index := First_Index (T_Typ);
00c403ee 7177 while Present (Opnd_Index) loop
7178
7179 -- If the index is a range, use its bounds. If it is an
7180 -- entity (as will be the case if it is a named subtype
7181 -- or an itype created for a slice) retrieve its range.
7182
7183 if Is_Entity_Name (Opnd_Index)
7184 and then Is_Type (Entity (Opnd_Index))
7185 then
7186 Opnd_Range := Scalar_Range (Entity (Opnd_Index));
7187 else
7188 Opnd_Range := Opnd_Index;
7189 end if;
7190
7191 if Nkind (Opnd_Range) = N_Range then
9c486805 7192 if Is_In_Range
7193 (Low_Bound (Opnd_Range), Etype (Targ_Index),
7194 Assume_Valid => True)
ee6ba406 7195 and then
7196 Is_In_Range
9c486805 7197 (High_Bound (Opnd_Range), Etype (Targ_Index),
7198 Assume_Valid => True)
ee6ba406 7199 then
7200 null;
7201
feff2f05 7202 -- If null range, no check needed
f2a06be9 7203
9dfe12ae 7204 elsif
00c403ee 7205 Compile_Time_Known_Value (High_Bound (Opnd_Range))
9dfe12ae 7206 and then
00c403ee 7207 Compile_Time_Known_Value (Low_Bound (Opnd_Range))
9dfe12ae 7208 and then
00c403ee 7209 Expr_Value (High_Bound (Opnd_Range)) <
7210 Expr_Value (Low_Bound (Opnd_Range))
9dfe12ae 7211 then
7212 null;
7213
ee6ba406 7214 elsif Is_Out_Of_Range
9c486805 7215 (Low_Bound (Opnd_Range), Etype (Targ_Index),
7216 Assume_Valid => True)
ee6ba406 7217 or else
7218 Is_Out_Of_Range
9c486805 7219 (High_Bound (Opnd_Range), Etype (Targ_Index),
7220 Assume_Valid => True)
ee6ba406 7221 then
7222 Add_Check
7223 (Compile_Time_Constraint_Error
7224 (Wnode, "value out of range of}?", T_Typ));
7225
7226 else
7227 Evolve_Or_Else
7228 (Cond,
7229 Discrete_Range_Cond
00c403ee 7230 (Opnd_Range, Etype (Targ_Index)));
ee6ba406 7231 end if;
7232 end if;
7233
7234 Next_Index (Opnd_Index);
7235 Next_Index (Targ_Index);
7236 end loop;
7237 end;
7238 end if;
7239 end if;
7240 end if;
7241
7242 -- Construct the test and insert into the tree
7243
7244 if Present (Cond) then
7245 if Do_Access then
7246 Cond := Guard_Access (Cond, Loc, Ck_Node);
7247 end if;
7248
f15731c4 7249 Add_Check
7250 (Make_Raise_Constraint_Error (Loc,
eefa141b 7251 Condition => Cond,
7252 Reason => CE_Range_Check_Failed));
ee6ba406 7253 end if;
7254
7255 return Ret_Result;
ee6ba406 7256 end Selected_Range_Checks;
7257
7258 -------------------------------
7259 -- Storage_Checks_Suppressed --
7260 -------------------------------
7261
7262 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
7263 begin
9dfe12ae 7264 if Present (E) and then Checks_May_Be_Suppressed (E) then
7265 return Is_Check_Suppressed (E, Storage_Check);
7266 else
7267 return Scope_Suppress (Storage_Check);
7268 end if;
ee6ba406 7269 end Storage_Checks_Suppressed;
7270
7271 ---------------------------
7272 -- Tag_Checks_Suppressed --
7273 ---------------------------
7274
7275 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
7276 begin
9dfe12ae 7277 if Present (E) then
7278 if Kill_Tag_Checks (E) then
7279 return True;
7280 elsif Checks_May_Be_Suppressed (E) then
7281 return Is_Check_Suppressed (E, Tag_Check);
7282 end if;
7283 end if;
7284
7285 return Scope_Suppress (Tag_Check);
ee6ba406 7286 end Tag_Checks_Suppressed;
7287
0577b0b1 7288 --------------------------
7289 -- Validity_Check_Range --
7290 --------------------------
7291
7292 procedure Validity_Check_Range (N : Node_Id) is
7293 begin
7294 if Validity_Checks_On and Validity_Check_Operands then
7295 if Nkind (N) = N_Range then
7296 Ensure_Valid (Low_Bound (N));
7297 Ensure_Valid (High_Bound (N));
7298 end if;
7299 end if;
7300 end Validity_Check_Range;
7301
7302 --------------------------------
7303 -- Validity_Checks_Suppressed --
7304 --------------------------------
7305
7306 function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is
7307 begin
7308 if Present (E) and then Checks_May_Be_Suppressed (E) then
7309 return Is_Check_Suppressed (E, Validity_Check);
7310 else
7311 return Scope_Suppress (Validity_Check);
7312 end if;
7313 end Validity_Checks_Suppressed;
7314
ee6ba406 7315end Checks;