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