| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L U E _ U -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 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- -- |
| 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with System.SPARK.Cut_Operations; use System.SPARK.Cut_Operations; |
| 33 |
with System.Val_Util; use System.Val_Util; |
| 34 |
|
| 35 |
package body System.Value_U is |
| 36 |
|
| 37 |
-- Ghost code, loop invariants and assertions in this unit are meant for |
| 38 |
-- analysis only, not for run-time checking, as it would be too costly |
| 39 |
-- otherwise. This is enforced by setting the assertion policy to Ignore. |
| 40 |
|
| 41 |
pragma Assertion_Policy (Ghost => Ignore, |
| 42 |
Loop_Invariant => Ignore, |
| 43 |
Assert => Ignore, |
| 44 |
Assert_And_Cut => Ignore, |
| 45 |
Subprogram_Variant => Ignore); |
| 46 |
|
| 47 |
use type Spec.Uns_Option; |
| 48 |
use type Spec.Split_Value_Ghost; |
| 49 |
|
| 50 |
-- Local lemmas |
| 51 |
|
| 52 |
procedure Lemma_Digit_Not_Last |
| 53 |
(Str : String; |
| 54 |
P : Integer; |
| 55 |
From : Integer; |
| 56 |
To : Integer) |
| 57 |
with Ghost, |
| 58 |
Pre => Str'Last /= Positive'Last |
| 59 |
and then From in Str'Range |
| 60 |
and then To in From .. Str'Last |
| 61 |
and then Str (From) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' |
| 62 |
and then P in From .. To |
| 63 |
and then P <= Spec.Last_Hexa_Ghost (Str (From .. To)) + 1 |
| 64 |
and then Spec.Is_Based_Format_Ghost (Str (From .. To)), |
| 65 |
Post => |
| 66 |
(if Str (P) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' |
| 67 |
then P <= Spec.Last_Hexa_Ghost (Str (From .. To))); |
| 68 |
|
| 69 |
procedure Lemma_Underscore_Not_Last |
| 70 |
(Str : String; |
| 71 |
P : Integer; |
| 72 |
From : Integer; |
| 73 |
To : Integer) |
| 74 |
with Ghost, |
| 75 |
Pre => Str'Last /= Positive'Last |
| 76 |
and then From in Str'Range |
| 77 |
and then To in From .. Str'Last |
| 78 |
and then Str (From) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' |
| 79 |
and then P in From .. To |
| 80 |
and then Str (P) = '_' |
| 81 |
and then P <= Spec.Last_Hexa_Ghost (Str (From .. To)) + 1 |
| 82 |
and then Spec.Is_Based_Format_Ghost (Str (From .. To)), |
| 83 |
Post => P + 1 <= Spec.Last_Hexa_Ghost (Str (From .. To)) |
| 84 |
and then Str (P + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F'; |
| 85 |
|
| 86 |
----------------------------- |
| 87 |
-- Local lemma null bodies -- |
| 88 |
----------------------------- |
| 89 |
|
| 90 |
procedure Lemma_Digit_Not_Last |
| 91 |
(Str : String; |
| 92 |
P : Integer; |
| 93 |
From : Integer; |
| 94 |
To : Integer) |
| 95 |
is null; |
| 96 |
|
| 97 |
procedure Lemma_Underscore_Not_Last |
| 98 |
(Str : String; |
| 99 |
P : Integer; |
| 100 |
From : Integer; |
| 101 |
To : Integer) |
| 102 |
is null; |
| 103 |
|
| 104 |
----------------------- |
| 105 |
-- Scan_Raw_Unsigned -- |
| 106 |
----------------------- |
| 107 |
|
| 108 |
procedure Scan_Raw_Unsigned |
| 109 |
(Str : String; |
| 110 |
Ptr : not null access Integer; |
| 111 |
Max : Integer; |
| 112 |
Res : out Uns) |
| 113 |
is |
| 114 |
P : Integer; |
| 115 |
-- Local copy of the pointer |
| 116 |
|
| 117 |
Uval : Uns; |
| 118 |
-- Accumulated unsigned integer result |
| 119 |
|
| 120 |
Expon : Integer; |
| 121 |
-- Exponent value |
| 122 |
|
| 123 |
Overflow : Boolean := False; |
| 124 |
-- Set True if overflow is detected at any point |
| 125 |
|
| 126 |
Base_Char : Character; |
| 127 |
-- Base character (# or :) in based case |
| 128 |
|
| 129 |
Base : Uns := 10; |
| 130 |
-- Base value (reset in based case) |
| 131 |
|
| 132 |
Digit : Uns; |
| 133 |
-- Digit value |
| 134 |
|
| 135 |
Ptr_Old : constant Integer := Ptr.all |
| 136 |
with Ghost; |
| 137 |
Last_Num_Init : constant Integer := |
| 138 |
Last_Number_Ghost (Str (Ptr.all .. Max)) |
| 139 |
with Ghost; |
| 140 |
Init_Val : constant Spec.Uns_Option := |
| 141 |
Spec.Scan_Based_Number_Ghost (Str, Ptr.all, Last_Num_Init) |
| 142 |
with Ghost; |
| 143 |
Starts_As_Based : constant Boolean := |
| 144 |
Spec.Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, Max) |
| 145 |
with Ghost; |
| 146 |
Last_Num_Based : constant Integer := |
| 147 |
(if Starts_As_Based |
| 148 |
then Spec.Last_Hexa_Ghost (Str (Last_Num_Init + 2 .. Max)) |
| 149 |
else Last_Num_Init) |
| 150 |
with Ghost; |
| 151 |
Is_Based : constant Boolean := |
| 152 |
Spec.Raw_Unsigned_Is_Based_Ghost |
| 153 |
(Str, Last_Num_Init, Last_Num_Based, Max) |
| 154 |
with Ghost; |
| 155 |
Based_Val : constant Spec.Uns_Option := |
| 156 |
(if Starts_As_Based and then not Init_Val.Overflow |
| 157 |
then Spec.Scan_Based_Number_Ghost |
| 158 |
(Str, Last_Num_Init + 2, Last_Num_Based, Init_Val.Value) |
| 159 |
else Init_Val) |
| 160 |
with Ghost; |
| 161 |
First_Exp : constant Integer := |
| 162 |
(if Is_Based then Last_Num_Based + 2 else Last_Num_Init + 1) |
| 163 |
with Ghost; |
| 164 |
|
| 165 |
begin |
| 166 |
-- We do not tolerate strings with Str'Last = Positive'Last |
| 167 |
|
| 168 |
if Str'Last = Positive'Last then |
| 169 |
raise Program_Error with |
| 170 |
"string upper bound is Positive'Last, not supported"; |
| 171 |
end if; |
| 172 |
|
| 173 |
P := Ptr.all; |
| 174 |
Spec.Lemma_Scan_Based_Number_Ghost_Step (Str, P, Last_Num_Init); |
| 175 |
Uval := Character'Pos (Str (P)) - Character'Pos ('0'); |
| 176 |
pragma Assert (Str (P) in '0' .. '9'); |
| 177 |
P := P + 1; |
| 178 |
|
| 179 |
-- Scan out digits of what is either the number or the base. |
| 180 |
-- In either case, we are definitely scanning out in base 10. |
| 181 |
|
| 182 |
declare |
| 183 |
Umax : constant Uns := (Uns'Last - 9) / 10; |
| 184 |
-- Max value which cannot overflow on accumulating next digit |
| 185 |
|
| 186 |
Umax10 : constant Uns := Uns'Last / 10; |
| 187 |
-- Numbers bigger than Umax10 overflow if multiplied by 10 |
| 188 |
|
| 189 |
begin |
| 190 |
-- Loop through decimal digits |
| 191 |
loop |
| 192 |
pragma Loop_Invariant (P in P'Loop_Entry .. Last_Num_Init + 1); |
| 193 |
pragma Loop_Invariant |
| 194 |
(if Overflow then Init_Val.Overflow); |
| 195 |
pragma Loop_Invariant |
| 196 |
(if not Overflow |
| 197 |
then Init_Val = Spec.Scan_Based_Number_Ghost |
| 198 |
(Str, P, Last_Num_Init, Acc => Uval)); |
| 199 |
|
| 200 |
exit when P > Max; |
| 201 |
|
| 202 |
Digit := Character'Pos (Str (P)) - Character'Pos ('0'); |
| 203 |
|
| 204 |
-- Non-digit encountered |
| 205 |
|
| 206 |
if Digit > 9 then |
| 207 |
if Str (P) = '_' then |
| 208 |
Spec.Lemma_Scan_Based_Number_Ghost_Underscore |
| 209 |
(Str, P, Last_Num_Init, Acc => Uval); |
| 210 |
Scan_Underscore (Str, P, Ptr, Max, False); |
| 211 |
else |
| 212 |
exit; |
| 213 |
end if; |
| 214 |
|
| 215 |
-- Accumulate result, checking for overflow |
| 216 |
|
| 217 |
else |
| 218 |
pragma Assert |
| 219 |
(By |
| 220 |
(Str (P) in '0' .. '9', |
| 221 |
By |
| 222 |
(Character'Pos (Str (P)) >= Character'Pos ('0'), |
| 223 |
Uns '(Character'Pos (Str (P))) >= |
| 224 |
Character'Pos ('0')))); |
| 225 |
Spec.Lemma_Scan_Based_Number_Ghost_Step |
| 226 |
(Str, P, Last_Num_Init, Acc => Uval); |
| 227 |
Spec.Lemma_Scan_Based_Number_Ghost_Overflow |
| 228 |
(Str, P, Last_Num_Init, Acc => Uval); |
| 229 |
|
| 230 |
if Uval <= Umax then |
| 231 |
Uval := 10 * Uval + Digit; |
| 232 |
pragma Assert |
| 233 |
(if not Overflow |
| 234 |
then Init_Val = Spec.Scan_Based_Number_Ghost |
| 235 |
(Str, P + 1, Last_Num_Init, Acc => Uval)); |
| 236 |
|
| 237 |
elsif Uval > Umax10 then |
| 238 |
Overflow := True; |
| 239 |
|
| 240 |
else |
| 241 |
Uval := 10 * Uval + Digit; |
| 242 |
|
| 243 |
if Uval < Umax10 then |
| 244 |
Overflow := True; |
| 245 |
end if; |
| 246 |
pragma Assert |
| 247 |
(if not Overflow |
| 248 |
then Init_Val = Spec.Scan_Based_Number_Ghost |
| 249 |
(Str, P + 1, Last_Num_Init, Acc => Uval)); |
| 250 |
|
| 251 |
end if; |
| 252 |
|
| 253 |
P := P + 1; |
| 254 |
end if; |
| 255 |
end loop; |
| 256 |
Spec.Lemma_Scan_Based_Number_Ghost_Base |
| 257 |
(Str, P, Last_Num_Init, Acc => Uval); |
| 258 |
end; |
| 259 |
|
| 260 |
pragma Assert_And_Cut |
| 261 |
(By |
| 262 |
(P = Last_Num_Init + 1, |
| 263 |
P > Max or else Str (P) not in '_' | '0' .. '9') |
| 264 |
and then Overflow = Init_Val.Overflow |
| 265 |
and then (if not Overflow then Init_Val.Value = Uval)); |
| 266 |
|
| 267 |
Ptr.all := P; |
| 268 |
|
| 269 |
-- Deal with based case. We recognize either the standard '#' or the |
| 270 |
-- allowed alternative replacement ':' (see RM J.2(3)). |
| 271 |
|
| 272 |
if P < Max and then (Str (P) = '#' or else Str (P) = ':') then |
| 273 |
Base_Char := Str (P); |
| 274 |
P := P + 1; |
| 275 |
Base := Uval; |
| 276 |
Uval := 0; |
| 277 |
|
| 278 |
-- Check base value. Overflow is set True if we find a bad base, or |
| 279 |
-- a digit that is out of range of the base. That way, we scan out |
| 280 |
-- the numeral that is still syntactically correct, though illegal. |
| 281 |
-- We use a safe base of 16 for this scan, to avoid zero divide. |
| 282 |
|
| 283 |
if Base not in 2 .. 16 then |
| 284 |
Overflow := True; |
| 285 |
Base := 16; |
| 286 |
end if; |
| 287 |
|
| 288 |
-- Scan out based integer |
| 289 |
|
| 290 |
declare |
| 291 |
Umax : constant Uns := (Uns'Last - Base + 1) / Base; |
| 292 |
-- Max value which cannot overflow on accumulating next digit |
| 293 |
|
| 294 |
UmaxB : constant Uns := Uns'Last / Base; |
| 295 |
-- Numbers bigger than UmaxB overflow if multiplied by base |
| 296 |
|
| 297 |
begin |
| 298 |
pragma Assert |
| 299 |
(if Str (P) in '0' .. '9' | 'A' .. 'F' | 'a' .. 'f' |
| 300 |
then Spec.Is_Based_Format_Ghost (Str (P .. Max))); |
| 301 |
|
| 302 |
-- Loop to scan out based integer value |
| 303 |
|
| 304 |
loop |
| 305 |
-- We require a digit at this stage |
| 306 |
|
| 307 |
if Str (P) in '0' .. '9' then |
| 308 |
Digit := Character'Pos (Str (P)) - Character'Pos ('0'); |
| 309 |
|
| 310 |
elsif Str (P) in 'A' .. 'F' then |
| 311 |
Digit := |
| 312 |
Character'Pos (Str (P)) - (Character'Pos ('A') - 10); |
| 313 |
|
| 314 |
elsif Str (P) in 'a' .. 'f' then |
| 315 |
Digit := |
| 316 |
Character'Pos (Str (P)) - (Character'Pos ('a') - 10); |
| 317 |
|
| 318 |
-- If we don't have a digit, then this is not a based number |
| 319 |
-- after all, so we use the value we scanned out as the base |
| 320 |
-- (now in Base), and the pointer to the base character was |
| 321 |
-- already stored in Ptr.all. |
| 322 |
|
| 323 |
else |
| 324 |
pragma Assert |
| 325 |
(By |
| 326 |
(Spec.Only_Hexa_Ghost (Str, P, Last_Num_Based), |
| 327 |
P > Last_Num_Init + 1 |
| 328 |
and Spec.Only_Hexa_Ghost |
| 329 |
(Str, Last_Num_Init + 2, Last_Num_Based))); |
| 330 |
Spec.Lemma_Scan_Based_Number_Ghost_Base |
| 331 |
(Str, P, Last_Num_Based, Base, Uval); |
| 332 |
Uval := Base; |
| 333 |
Base := 10; |
| 334 |
pragma Assert (Ptr.all = Last_Num_Init + 1); |
| 335 |
pragma Assert |
| 336 |
(if Starts_As_Based |
| 337 |
then By |
| 338 |
(P = Last_Num_Based + 1, |
| 339 |
P <= Last_Num_Based + 1 |
| 340 |
and Str (P) not in |
| 341 |
'0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_')); |
| 342 |
pragma Assert (not Is_Based); |
| 343 |
pragma Assert (if not Overflow then Uval = Init_Val.Value); |
| 344 |
exit; |
| 345 |
end if; |
| 346 |
|
| 347 |
pragma Loop_Invariant (P in P'Loop_Entry .. Last_Num_Based); |
| 348 |
pragma Loop_Invariant |
| 349 |
(Str (P) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' |
| 350 |
and then Digit = Spec.Hexa_To_Unsigned_Ghost (Str (P))); |
| 351 |
pragma Loop_Invariant |
| 352 |
(if Overflow'Loop_Entry then Overflow); |
| 353 |
pragma Loop_Invariant |
| 354 |
(if Overflow then |
| 355 |
(Overflow'Loop_Entry or else Based_Val.Overflow)); |
| 356 |
pragma Loop_Invariant |
| 357 |
(if not Overflow |
| 358 |
then Based_Val = Spec.Scan_Based_Number_Ghost |
| 359 |
(Str, P, Last_Num_Based, Base, Uval)); |
| 360 |
pragma Loop_Invariant (Ptr.all = Last_Num_Init + 1); |
| 361 |
|
| 362 |
Spec.Lemma_Scan_Based_Number_Ghost_Step |
| 363 |
(Str, P, Last_Num_Based, Base, Uval); |
| 364 |
Spec.Lemma_Scan_Based_Number_Ghost_Overflow |
| 365 |
(Str, P, Last_Num_Based, Base, Uval); |
| 366 |
|
| 367 |
-- If digit is too large, just signal overflow and continue. |
| 368 |
-- The idea here is to keep scanning as long as the input is |
| 369 |
-- syntactically valid, even if we have detected overflow |
| 370 |
|
| 371 |
if Digit >= Base then |
| 372 |
Overflow := True; |
| 373 |
|
| 374 |
-- Here we accumulate the value, checking overflow |
| 375 |
|
| 376 |
elsif Uval <= Umax then |
| 377 |
Uval := Base * Uval + Digit; |
| 378 |
pragma Assert |
| 379 |
(if not Overflow |
| 380 |
then Based_Val = Spec.Scan_Based_Number_Ghost |
| 381 |
(Str, P + 1, Last_Num_Based, Base, Uval)); |
| 382 |
|
| 383 |
elsif Uval > UmaxB then |
| 384 |
Overflow := True; |
| 385 |
|
| 386 |
else |
| 387 |
Uval := Base * Uval + Digit; |
| 388 |
|
| 389 |
if Uval < UmaxB then |
| 390 |
Overflow := True; |
| 391 |
end if; |
| 392 |
pragma Assert |
| 393 |
(if not Overflow |
| 394 |
then Based_Val = Spec.Scan_Based_Number_Ghost |
| 395 |
(Str, P + 1, Last_Num_Based, Base, Uval)); |
| 396 |
end if; |
| 397 |
|
| 398 |
-- If at end of string with no base char, not a based number |
| 399 |
-- but we signal Constraint_Error and set the pointer past |
| 400 |
-- the end of the field, since this is what the ACVC tests |
| 401 |
-- seem to require, see CE3704N, line 204. |
| 402 |
|
| 403 |
P := P + 1; |
| 404 |
|
| 405 |
if P > Max then |
| 406 |
Ptr.all := P; |
| 407 |
Bad_Value (Str); |
| 408 |
end if; |
| 409 |
|
| 410 |
-- If terminating base character, we are done with loop |
| 411 |
|
| 412 |
if Str (P) = Base_Char then |
| 413 |
Ptr.all := P + 1; |
| 414 |
pragma Assert (P = Last_Num_Based + 1); |
| 415 |
pragma Assert (Ptr.all = Last_Num_Based + 2); |
| 416 |
pragma Assert |
| 417 |
(By |
| 418 |
(Is_Based, |
| 419 |
So |
| 420 |
(Starts_As_Based, |
| 421 |
So |
| 422 |
(Last_Num_Based < Max, |
| 423 |
Str (Last_Num_Based + 1) = Base_Char |
| 424 |
and Base_Char = Str (Last_Num_Init + 1))))); |
| 425 |
Spec.Lemma_Scan_Based_Number_Ghost_Base |
| 426 |
(Str, P, Last_Num_Based, Base, Uval); |
| 427 |
exit; |
| 428 |
|
| 429 |
-- Deal with underscore |
| 430 |
|
| 431 |
elsif Str (P) = '_' then |
| 432 |
Lemma_Underscore_Not_Last (Str, P, Last_Num_Init + 2, Max); |
| 433 |
Spec.Lemma_Scan_Based_Number_Ghost_Underscore |
| 434 |
(Str, P, Last_Num_Based, Base, Uval); |
| 435 |
Scan_Underscore (Str, P, Ptr, Max, True); |
| 436 |
pragma Assert |
| 437 |
(if not Overflow |
| 438 |
then Based_Val = Spec.Scan_Based_Number_Ghost |
| 439 |
(Str, P, Last_Num_Based, Base, Uval)); |
| 440 |
pragma Assert (Str (P) not in '_' | Base_Char); |
| 441 |
end if; |
| 442 |
|
| 443 |
Lemma_Digit_Not_Last (Str, P, Last_Num_Init + 2, Max); |
| 444 |
pragma Assert (Str (P) not in '_' | Base_Char); |
| 445 |
end loop; |
| 446 |
end; |
| 447 |
pragma Assert |
| 448 |
(if Starts_As_Based then P = Last_Num_Based + 1 |
| 449 |
else P = Last_Num_Init + 2); |
| 450 |
pragma Assert |
| 451 |
(By |
| 452 |
(Overflow /= Spec.Scan_Split_No_Overflow_Ghost |
| 453 |
(Str, Ptr_Old, Max), |
| 454 |
So |
| 455 |
(Last_Num_Init < Max - 1 |
| 456 |
and then Str (Last_Num_Init + 1) in '#' | ':', |
| 457 |
Overflow = |
| 458 |
(Init_Val.Overflow |
| 459 |
or else Init_Val.Value not in 2 .. 16 |
| 460 |
or else (Starts_As_Based and Based_Val.Overflow))))); |
| 461 |
end if; |
| 462 |
|
| 463 |
pragma Assert_And_Cut |
| 464 |
(Overflow /= Spec.Scan_Split_No_Overflow_Ghost (Str, Ptr_Old, Max) |
| 465 |
and then Ptr.all = First_Exp |
| 466 |
and then Base in 2 .. 16 |
| 467 |
and then |
| 468 |
(if not Overflow then |
| 469 |
(if Is_Based then Base = Init_Val.Value else Base = 10)) |
| 470 |
and then |
| 471 |
(if not Overflow then |
| 472 |
(if Is_Based then Uval = Based_Val.Value |
| 473 |
else Uval = Init_Val.Value))); |
| 474 |
|
| 475 |
-- Come here with scanned unsigned value in Uval. The only remaining |
| 476 |
-- required step is to deal with exponent if one is present. |
| 477 |
|
| 478 |
Scan_Exponent (Str, Ptr, Max, Expon); |
| 479 |
|
| 480 |
pragma Assert |
| 481 |
(By |
| 482 |
(Ptr.all = Spec.Raw_Unsigned_Last_Ghost (Str, Ptr_Old, Max), |
| 483 |
Ptr.all = |
| 484 |
(if not Starts_As_Exponent_Format_Ghost (Str (First_Exp .. Max)) |
| 485 |
then First_Exp |
| 486 |
elsif Str (First_Exp + 1) in '-' | '+' then |
| 487 |
Last_Number_Ghost (Str (First_Exp + 2 .. Max)) + 1 |
| 488 |
else Last_Number_Ghost (Str (First_Exp + 1 .. Max)) + 1))); |
| 489 |
pragma Assert |
| 490 |
(if not Overflow |
| 491 |
then Spec.Scan_Split_Value_Ghost (Str, Ptr_Old, Max) = |
| 492 |
(Uval, Base, Expon)); |
| 493 |
|
| 494 |
if Expon /= 0 and then Uval /= 0 then |
| 495 |
|
| 496 |
-- For non-zero value, scale by exponent value. No need to do this |
| 497 |
-- efficiently, since use of exponent in integer literals is rare, |
| 498 |
-- and in any case the exponent cannot be very large. |
| 499 |
|
| 500 |
declare |
| 501 |
UmaxB : constant Uns := Uns'Last / Base; |
| 502 |
-- Numbers bigger than UmaxB overflow if multiplied by base |
| 503 |
|
| 504 |
Res_Val : constant Spec.Uns_Option := |
| 505 |
Spec.Exponent_Unsigned_Ghost (Uval, Expon, Base) |
| 506 |
with Ghost; |
| 507 |
begin |
| 508 |
for J in 1 .. Expon loop |
| 509 |
pragma Loop_Invariant |
| 510 |
(if Overflow'Loop_Entry then Overflow); |
| 511 |
pragma Loop_Invariant |
| 512 |
(if Overflow |
| 513 |
then Overflow'Loop_Entry or else Res_Val.Overflow); |
| 514 |
pragma Loop_Invariant (Uval /= 0); |
| 515 |
pragma Loop_Invariant |
| 516 |
(if not Overflow |
| 517 |
then Res_Val = Spec.Exponent_Unsigned_Ghost |
| 518 |
(Uval, Expon - J + 1, Base)); |
| 519 |
|
| 520 |
pragma Assert |
| 521 |
((Uval > UmaxB) = Spec.Scan_Overflows_Ghost (0, Base, Uval)); |
| 522 |
|
| 523 |
if Uval > UmaxB then |
| 524 |
Spec.Lemma_Exponent_Unsigned_Ghost_Overflow |
| 525 |
(Uval, Expon - J + 1, Base); |
| 526 |
Overflow := True; |
| 527 |
exit; |
| 528 |
end if; |
| 529 |
|
| 530 |
Spec.Lemma_Exponent_Unsigned_Ghost_Step |
| 531 |
(Uval, Expon - J + 1, Base); |
| 532 |
|
| 533 |
Uval := Uval * Base; |
| 534 |
end loop; |
| 535 |
Spec.Lemma_Exponent_Unsigned_Ghost_Base (Uval, 0, Base); |
| 536 |
|
| 537 |
pragma Assert |
| 538 |
(Overflow /= |
| 539 |
Spec.Raw_Unsigned_No_Overflow_Ghost (Str, Ptr_Old, Max)); |
| 540 |
pragma Assert (if not Overflow then Res_Val = (False, Uval)); |
| 541 |
end; |
| 542 |
end if; |
| 543 |
Spec.Lemma_Exponent_Unsigned_Ghost_Base (Uval, Expon, Base); |
| 544 |
pragma Assert |
| 545 |
(if Expon = 0 or else Uval = 0 then |
| 546 |
Spec.Exponent_Unsigned_Ghost (Uval, Expon, Base) = (False, Uval)); |
| 547 |
pragma Assert |
| 548 |
(Overflow /= |
| 549 |
Spec.Raw_Unsigned_No_Overflow_Ghost (Str, Ptr_Old, Max)); |
| 550 |
pragma Assert |
| 551 |
(if not Overflow then |
| 552 |
Uval = Spec.Scan_Raw_Unsigned_Ghost (Str, Ptr_Old, Max)); |
| 553 |
|
| 554 |
-- Return result, dealing with overflow |
| 555 |
|
| 556 |
if Overflow then |
| 557 |
Bad_Value (Str); |
| 558 |
pragma Annotate |
| 559 |
(GNATprove, Intentional, |
| 560 |
"call to nonreturning subprogram might be executed", |
| 561 |
"it is expected that Constraint_Error is raised in case of" |
| 562 |
& " overflow"); |
| 563 |
else |
| 564 |
Res := Uval; |
| 565 |
end if; |
| 566 |
end Scan_Raw_Unsigned; |
| 567 |
|
| 568 |
------------------- |
| 569 |
-- Scan_Unsigned -- |
| 570 |
------------------- |
| 571 |
|
| 572 |
procedure Scan_Unsigned |
| 573 |
(Str : String; |
| 574 |
Ptr : not null access Integer; |
| 575 |
Max : Integer; |
| 576 |
Res : out Uns) |
| 577 |
is |
| 578 |
Start : Positive; |
| 579 |
-- Save location of first non-blank character |
| 580 |
|
| 581 |
begin |
| 582 |
pragma Warnings |
| 583 |
(Off, |
| 584 |
"""Start"" is set by ""Scan_Plus_Sign"" but not used after the call"); |
| 585 |
Scan_Plus_Sign (Str, Ptr, Max, Start); |
| 586 |
pragma Warnings |
| 587 |
(On, |
| 588 |
"""Start"" is set by ""Scan_Plus_Sign"" but not used after the call"); |
| 589 |
|
| 590 |
if Str (Ptr.all) not in '0' .. '9' then |
| 591 |
Ptr.all := Start; |
| 592 |
Bad_Value (Str); |
| 593 |
end if; |
| 594 |
|
| 595 |
Scan_Raw_Unsigned (Str, Ptr, Max, Res); |
| 596 |
end Scan_Unsigned; |
| 597 |
|
| 598 |
-------------------- |
| 599 |
-- Value_Unsigned -- |
| 600 |
-------------------- |
| 601 |
|
| 602 |
function Value_Unsigned (Str : String) return Uns is |
| 603 |
begin |
| 604 |
-- We have to special case Str'Last = Positive'Last because the normal |
| 605 |
-- circuit ends up setting P to Str'Last + 1 which is out of bounds. We |
| 606 |
-- deal with this by converting to a subtype which fixes the bounds. |
| 607 |
|
| 608 |
if Str'Last = Positive'Last then |
| 609 |
declare |
| 610 |
subtype NT is String (1 .. Str'Length); |
| 611 |
procedure Prove_Is_Unsigned_Ghost with |
| 612 |
Ghost, |
| 613 |
Pre => Str'Length < Natural'Last |
| 614 |
and then not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 615 |
and then Spec.Is_Unsigned_Ghost (Spec.Slide_To_1 (Str)), |
| 616 |
Post => Spec.Is_Unsigned_Ghost (NT (Str)); |
| 617 |
procedure Prove_Is_Unsigned_Ghost is null; |
| 618 |
begin |
| 619 |
Prove_Is_Unsigned_Ghost; |
| 620 |
return Value_Unsigned (NT (Str)); |
| 621 |
end; |
| 622 |
|
| 623 |
-- Normal case where Str'Last < Positive'Last |
| 624 |
|
| 625 |
else |
| 626 |
declare |
| 627 |
V : Uns; |
| 628 |
P : aliased Integer := Str'First; |
| 629 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 630 |
(Str, Str'First, Str'Last) |
| 631 |
with Ghost; |
| 632 |
Fst_Num : constant Positive := |
| 633 |
(if Str (Non_Blank) = '+' then Non_Blank + 1 else Non_Blank) |
| 634 |
with Ghost; |
| 635 |
begin |
| 636 |
declare |
| 637 |
P_Acc : constant not null access Integer := P'Access; |
| 638 |
begin |
| 639 |
Scan_Unsigned (Str, P_Acc, Str'Last, V); |
| 640 |
end; |
| 641 |
|
| 642 |
pragma Assert |
| 643 |
(P = Spec.Raw_Unsigned_Last_Ghost (Str, Fst_Num, Str'Last)); |
| 644 |
pragma Assert |
| 645 |
(V = Spec.Scan_Raw_Unsigned_Ghost (Str, Fst_Num, Str'Last)); |
| 646 |
|
| 647 |
Scan_Trailing_Blanks (Str, P); |
| 648 |
|
| 649 |
pragma Assert |
| 650 |
(Spec.Is_Value_Unsigned_Ghost |
| 651 |
(Spec.Slide_If_Necessary (Str), V)); |
| 652 |
return V; |
| 653 |
end; |
| 654 |
end if; |
| 655 |
end Value_Unsigned; |
| 656 |
|
| 657 |
end System.Value_U; |