1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . V A L U E _ I --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with System
.Val_Util
; use System
.Val_Util
;
34 package body System
.Value_I
is
36 -- Ghost code, loop invariants and assertions in this unit are meant for
37 -- analysis only, not for run-time checking, as it would be too costly
38 -- otherwise. This is enforced by setting the assertion policy to Ignore.
40 pragma Assertion_Policy
(Ghost
=> Ignore
,
41 Loop_Invariant
=> Ignore
,
43 Assert_And_Cut
=> Ignore
,
44 Subprogram_Variant
=> Ignore
);
50 procedure Scan_Integer
52 Ptr
: not null access Integer;
56 procedure Prove_Is_Int_Of_Uns
61 Pre
=> Spec
.Uns_Is_Valid_Int
(Minus
, Uval
)
63 (if Minus
and then Uval
= Uns
(Int
'Last) + 1 then Val
= Int
'First
64 elsif Minus
then Val
= -(Int
(Uval
))
65 else Val
= Int
(Uval
)),
66 Post
=> Spec
.Is_Int_Of_Uns
(Minus
, Uval
, Val
);
67 -- Unfold the definition of Is_Int_Of_Uns
69 procedure Prove_Is_Int_Of_Uns
79 -- Set to True if minus sign is present, otherwise to False
81 Unused_Start
: Positive;
82 -- Saves location of first non-blank (not used in this case)
84 Non_Blank
: constant Positive :=
85 First_Non_Space_Ghost
(Str
, Ptr
.all, Max
)
88 Fst_Num
: constant Positive :=
89 (if Str
(Non_Blank
) in '+' |
'-' then Non_Blank
+ 1
94 Scan_Sign
(Str
, Ptr
, Max
, Minus
, Unused_Start
);
96 if Str
(Ptr
.all) not in '0' .. '9' then
97 Ptr
.all := Unused_Start
;
101 Scan_Raw_Unsigned
(Str
, Ptr
, Max
, Uval
);
103 (Uval
= U_Spec
.Scan_Raw_Unsigned_Ghost
(Str
, Fst_Num
, Max
));
105 -- Deal with overflow cases, and also with largest negative number
107 if Uval
> Uns
(Int
'Last) then
108 if Minus
and then Uval
= Uns
(Int
'Last) + 1 then
117 Res
:= -(Int
(Uval
));
126 (Minus
=> Str
(Non_Blank
) = '-',
135 function Value_Integer
(Str
: String) return Int
is
137 -- We have to special case Str'Last = Positive'Last because the normal
138 -- circuit ends up setting P to Str'Last + 1 which is out of bounds. We
139 -- deal with this by converting to a subtype which fixes the bounds.
141 if Str
'Last = Positive'Last then
143 subtype NT
is String (1 .. Str
'Length);
144 procedure Prove_Is_Integer_Ghost
with
146 Pre
=> Str
'Length < Natural'Last
147 and then not Only_Space_Ghost
(Str
, Str
'First, Str
'Last)
148 and then Spec
.Is_Integer_Ghost
(Spec
.Slide_To_1
(Str
)),
149 Post
=> Spec
.Is_Integer_Ghost
(NT
(Str
));
150 procedure Prove_Is_Integer_Ghost
is null;
152 Prove_Is_Integer_Ghost
;
153 return Value_Integer
(NT
(Str
));
156 -- Normal case where Str'Last < Positive'Last
161 P
: aliased Integer := Str
'First;
163 Non_Blank
: constant Positive := First_Non_Space_Ghost
164 (Str
, Str
'First, Str
'Last)
167 Fst_Num
: constant Positive :=
168 (if Str
(Non_Blank
) in '+' |
'-' then Non_Blank
+ 1
174 P_Acc
: constant not null access Integer := P
'Access;
176 Scan_Integer
(Str
, P_Acc
, Str
'Last, V
);
180 (P
= U_Spec
.Raw_Unsigned_Last_Ghost
181 (Str
, Fst_Num
, Str
'Last));
183 Scan_Trailing_Blanks
(Str
, P
);
186 (Spec
.Is_Value_Integer_Ghost
(Spec
.Slide_If_Necessary
(Str
), V
));