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