| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L U E _ U _ S P E C -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2022-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 |
-- This package is part of a set of Ghost code packages used to proof the |
| 33 |
-- implementations of the Image and Value attributes. It provides the |
| 34 |
-- specification entities using for the formal verification of the routines |
| 35 |
-- for scanning modular unsigned integer values. |
| 36 |
|
| 37 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 38 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 39 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 40 |
-- contract cases should not be executed at runtime as well, in order not to |
| 41 |
-- slow down the execution of these functions. |
| 42 |
|
| 43 |
pragma Assertion_Policy (Pre => Ignore, |
| 44 |
Post => Ignore, |
| 45 |
Contract_Cases => Ignore, |
| 46 |
Ghost => Ignore, |
| 47 |
Subprogram_Variant => Ignore); |
| 48 |
|
| 49 |
with System.Val_Spec; use System.Val_Spec; |
| 50 |
|
| 51 |
generic |
| 52 |
|
| 53 |
type Uns is mod <>; |
| 54 |
|
| 55 |
package System.Value_U_Spec with |
| 56 |
Ghost, |
| 57 |
SPARK_Mode, |
| 58 |
Always_Terminates |
| 59 |
is |
| 60 |
pragma Preelaborate; |
| 61 |
|
| 62 |
-- Maximum value of exponent for 10 that fits in Uns'Base |
| 63 |
function Max_Log10 return Natural is |
| 64 |
(case Uns'Base'Size is |
| 65 |
when 8 => 2, |
| 66 |
when 16 => 4, |
| 67 |
when 32 => 9, |
| 68 |
when 64 => 19, |
| 69 |
when 128 => 38, |
| 70 |
when others => raise Program_Error) |
| 71 |
with Ghost; |
| 72 |
|
| 73 |
pragma Annotate (Gnatcheck, Exempt_On, "Discriminated_Records", |
| 74 |
"variant record only used in proof code"); |
| 75 |
type Uns_Option (Overflow : Boolean := False) is record |
| 76 |
case Overflow is |
| 77 |
when True => |
| 78 |
null; |
| 79 |
when False => |
| 80 |
Value : Uns := 0; |
| 81 |
end case; |
| 82 |
end record; |
| 83 |
pragma Annotate (Gnatcheck, Exempt_Off, "Discriminated_Records"); |
| 84 |
|
| 85 |
function Wrap_Option (Value : Uns) return Uns_Option is |
| 86 |
(Overflow => False, Value => Value); |
| 87 |
|
| 88 |
function Only_Decimal_Ghost |
| 89 |
(Str : String; |
| 90 |
From, To : Integer) |
| 91 |
return Boolean |
| 92 |
is |
| 93 |
(for all J in From .. To => Str (J) in '0' .. '9') |
| 94 |
with |
| 95 |
Pre => From > To or else (From >= Str'First and then To <= Str'Last); |
| 96 |
-- Ghost function that returns True if S has only decimal characters |
| 97 |
-- from index From to index To. |
| 98 |
|
| 99 |
function Only_Hexa_Ghost (Str : String; From, To : Integer) return Boolean |
| 100 |
is |
| 101 |
(for all J in From .. To => |
| 102 |
Str (J) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_') |
| 103 |
with |
| 104 |
Pre => From > To or else (From >= Str'First and then To <= Str'Last); |
| 105 |
-- Ghost function that returns True if S has only hexadecimal characters |
| 106 |
-- from index From to index To. |
| 107 |
|
| 108 |
function Last_Hexa_Ghost (Str : String) return Positive |
| 109 |
with |
| 110 |
Pre => Str /= "" |
| 111 |
and then Str (Str'First) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F', |
| 112 |
Post => Last_Hexa_Ghost'Result in Str'Range |
| 113 |
and then (if Last_Hexa_Ghost'Result < Str'Last then |
| 114 |
Str (Last_Hexa_Ghost'Result + 1) not in |
| 115 |
'0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_') |
| 116 |
and then Only_Hexa_Ghost (Str, Str'First, Last_Hexa_Ghost'Result); |
| 117 |
-- Ghost function that returns the index of the last character in S that |
| 118 |
-- is either an hexadecimal digit or an underscore, which necessarily |
| 119 |
-- exists given the precondition on Str. |
| 120 |
|
| 121 |
function Is_Based_Format_Ghost (Str : String) return Boolean |
| 122 |
is |
| 123 |
(Str /= "" |
| 124 |
and then Str (Str'First) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' |
| 125 |
and then |
| 126 |
(declare |
| 127 |
L : constant Positive := Last_Hexa_Ghost (Str); |
| 128 |
begin |
| 129 |
Str (L) /= '_' |
| 130 |
and then (for all J in Str'First .. L => |
| 131 |
(if Str (J) = '_' then Str (J + 1) /= '_')))); |
| 132 |
-- Ghost function that determines if Str has the correct format for a |
| 133 |
-- based number, consisting in a sequence of hexadecimal digits possibly |
| 134 |
-- separated by single underscores. It may be followed by other characters. |
| 135 |
|
| 136 |
function Hexa_To_Unsigned_Ghost (X : Character) return Uns is |
| 137 |
(case X is |
| 138 |
when '0' .. '9' => Character'Pos (X) - Character'Pos ('0'), |
| 139 |
when 'a' .. 'f' => Character'Pos (X) - Character'Pos ('a') + 10, |
| 140 |
when 'A' .. 'F' => Character'Pos (X) - Character'Pos ('A') + 10, |
| 141 |
when others => raise Program_Error) |
| 142 |
with |
| 143 |
Pre => X in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F'; |
| 144 |
-- Ghost function that computes the value corresponding to an hexadecimal |
| 145 |
-- digit. |
| 146 |
|
| 147 |
function Scan_Overflows_Ghost |
| 148 |
(Digit : Uns; |
| 149 |
Base : Uns; |
| 150 |
Acc : Uns) return Boolean |
| 151 |
is |
| 152 |
(Digit >= Base |
| 153 |
or else Acc > Uns'Last / Base |
| 154 |
or else Uns'Last - Digit < Base * Acc); |
| 155 |
-- Ghost function which returns True if Digit + Base * Acc overflows or |
| 156 |
-- Digit is greater than Base, as this is used by the algorithm for the |
| 157 |
-- test of overflow. |
| 158 |
|
| 159 |
function Scan_Based_Number_Ghost |
| 160 |
(Str : String; |
| 161 |
From, To : Integer; |
| 162 |
Base : Uns := 10; |
| 163 |
Acc : Uns := 0) return Uns_Option |
| 164 |
with |
| 165 |
Subprogram_Variant => (Increases => From), |
| 166 |
Pre => Str'Last /= Positive'Last |
| 167 |
and then |
| 168 |
(From > To or else (From >= Str'First and then To <= Str'Last)) |
| 169 |
and then Only_Hexa_Ghost (Str, From, To); |
| 170 |
-- Ghost function that recursively computes the based number in Str, |
| 171 |
-- assuming Acc has been scanned already and scanning continues at index |
| 172 |
-- From. |
| 173 |
|
| 174 |
-- Lemmas unfolding the recursive definition of Scan_Based_Number_Ghost |
| 175 |
|
| 176 |
procedure Lemma_Scan_Based_Number_Ghost_Base |
| 177 |
(Str : String; |
| 178 |
From, To : Integer; |
| 179 |
Base : Uns := 10; |
| 180 |
Acc : Uns := 0) |
| 181 |
with |
| 182 |
Global => null, |
| 183 |
Pre => Str'Last /= Positive'Last |
| 184 |
and then |
| 185 |
(From > To or else (From >= Str'First and then To <= Str'Last)) |
| 186 |
and then Only_Hexa_Ghost (Str, From, To), |
| 187 |
Post => |
| 188 |
(if From > To |
| 189 |
then Scan_Based_Number_Ghost (Str, From, To, Base, Acc) = |
| 190 |
(Overflow => False, Value => Acc)); |
| 191 |
-- Base case: Scan_Based_Number_Ghost returns Acc if From is bigger than To |
| 192 |
|
| 193 |
procedure Lemma_Scan_Based_Number_Ghost_Underscore |
| 194 |
(Str : String; |
| 195 |
From, To : Integer; |
| 196 |
Base : Uns := 10; |
| 197 |
Acc : Uns := 0) |
| 198 |
with |
| 199 |
Global => null, |
| 200 |
Pre => Str'Last /= Positive'Last |
| 201 |
and then |
| 202 |
(From > To or else (From >= Str'First and then To <= Str'Last)) |
| 203 |
and then Only_Hexa_Ghost (Str, From, To), |
| 204 |
Post => |
| 205 |
(if From <= To and then Str (From) = '_' |
| 206 |
then Scan_Based_Number_Ghost (Str, From, To, Base, Acc) = |
| 207 |
Scan_Based_Number_Ghost (Str, From + 1, To, Base, Acc)); |
| 208 |
-- Underscore case: underscores are ignored while scanning |
| 209 |
|
| 210 |
procedure Lemma_Scan_Based_Number_Ghost_Overflow |
| 211 |
(Str : String; |
| 212 |
From, To : Integer; |
| 213 |
Base : Uns := 10; |
| 214 |
Acc : Uns := 0) |
| 215 |
with |
| 216 |
Global => null, |
| 217 |
Pre => Str'Last /= Positive'Last |
| 218 |
and then |
| 219 |
(From > To or else (From >= Str'First and then To <= Str'Last)) |
| 220 |
and then Only_Hexa_Ghost (Str, From, To), |
| 221 |
Post => |
| 222 |
(if From <= To |
| 223 |
and then Str (From) /= '_' |
| 224 |
and then Scan_Overflows_Ghost |
| 225 |
(Hexa_To_Unsigned_Ghost (Str (From)), Base, Acc) |
| 226 |
then Scan_Based_Number_Ghost (Str, From, To, Base, Acc) = |
| 227 |
(Overflow => True)); |
| 228 |
-- Overflow case: scanning a digit which causes an overflow |
| 229 |
|
| 230 |
procedure Lemma_Scan_Based_Number_Ghost_Step |
| 231 |
(Str : String; |
| 232 |
From, To : Integer; |
| 233 |
Base : Uns := 10; |
| 234 |
Acc : Uns := 0) |
| 235 |
with |
| 236 |
Global => null, |
| 237 |
Pre => Str'Last /= Positive'Last |
| 238 |
and then |
| 239 |
(From > To or else (From >= Str'First and then To <= Str'Last)) |
| 240 |
and then Only_Hexa_Ghost (Str, From, To), |
| 241 |
Post => |
| 242 |
(if From <= To |
| 243 |
and then Str (From) /= '_' |
| 244 |
and then not Scan_Overflows_Ghost |
| 245 |
(Hexa_To_Unsigned_Ghost (Str (From)), Base, Acc) |
| 246 |
then Scan_Based_Number_Ghost (Str, From, To, Base, Acc) = |
| 247 |
Scan_Based_Number_Ghost |
| 248 |
(Str, From + 1, To, Base, |
| 249 |
Base * Acc + Hexa_To_Unsigned_Ghost (Str (From)))); |
| 250 |
-- Normal case: scanning a digit without overflows |
| 251 |
|
| 252 |
function Exponent_Unsigned_Ghost |
| 253 |
(Value : Uns; |
| 254 |
Exp : Natural; |
| 255 |
Base : Uns := 10) return Uns_Option |
| 256 |
with |
| 257 |
Subprogram_Variant => (Decreases => Exp); |
| 258 |
-- Ghost function that recursively computes Value * Base ** Exp |
| 259 |
|
| 260 |
-- Lemmas unfolding the recursive definition of Exponent_Unsigned_Ghost |
| 261 |
|
| 262 |
procedure Lemma_Exponent_Unsigned_Ghost_Base |
| 263 |
(Value : Uns; |
| 264 |
Exp : Natural; |
| 265 |
Base : Uns := 10) |
| 266 |
with |
| 267 |
Post => |
| 268 |
(if Exp = 0 or Value = 0 |
| 269 |
then Exponent_Unsigned_Ghost (Value, Exp, Base) = |
| 270 |
(Overflow => False, Value => Value)); |
| 271 |
-- Base case: Exponent_Unsigned_Ghost returns 0 if Value or Exp is 0 |
| 272 |
|
| 273 |
procedure Lemma_Exponent_Unsigned_Ghost_Overflow |
| 274 |
(Value : Uns; |
| 275 |
Exp : Natural; |
| 276 |
Base : Uns := 10) |
| 277 |
with |
| 278 |
Post => |
| 279 |
(if Exp /= 0 |
| 280 |
and then Value /= 0 |
| 281 |
and then Scan_Overflows_Ghost (0, Base, Value) |
| 282 |
then Exponent_Unsigned_Ghost (Value, Exp, Base) = (Overflow => True)); |
| 283 |
-- Overflow case: the next multiplication overflows |
| 284 |
|
| 285 |
procedure Lemma_Exponent_Unsigned_Ghost_Step |
| 286 |
(Value : Uns; |
| 287 |
Exp : Natural; |
| 288 |
Base : Uns := 10) |
| 289 |
with |
| 290 |
Post => |
| 291 |
(if Exp /= 0 |
| 292 |
and then Value /= 0 |
| 293 |
and then not Scan_Overflows_Ghost (0, Base, Value) |
| 294 |
then Exponent_Unsigned_Ghost (Value, Exp, Base) = |
| 295 |
Exponent_Unsigned_Ghost (Value * Base, Exp - 1, Base)); |
| 296 |
-- Normal case: exponentiation without overflows |
| 297 |
|
| 298 |
function Raw_Unsigned_Starts_As_Based_Ghost |
| 299 |
(Str : String; |
| 300 |
Last_Num_Init, To : Integer) |
| 301 |
return Boolean |
| 302 |
is |
| 303 |
(Last_Num_Init < To - 1 |
| 304 |
and then Str (Last_Num_Init + 1) in '#' | ':' |
| 305 |
and then Str (Last_Num_Init + 2) in |
| 306 |
'0' .. '9' | 'a' .. 'f' | 'A' .. 'F') |
| 307 |
with Ghost, |
| 308 |
Pre => Last_Num_Init in Str'Range |
| 309 |
and then To in Str'Range; |
| 310 |
-- Return True if Str starts as a based number |
| 311 |
|
| 312 |
function Raw_Unsigned_Is_Based_Ghost |
| 313 |
(Str : String; |
| 314 |
Last_Num_Init : Integer; |
| 315 |
Last_Num_Based : Integer; |
| 316 |
To : Integer) |
| 317 |
return Boolean |
| 318 |
is |
| 319 |
(Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, To) |
| 320 |
and then Last_Num_Based < To |
| 321 |
and then Str (Last_Num_Based + 1) = Str (Last_Num_Init + 1)) |
| 322 |
with Ghost, |
| 323 |
Pre => Last_Num_Init in Str'Range |
| 324 |
and then Last_Num_Based in Last_Num_Init .. Str'Last |
| 325 |
and then To in Str'Range; |
| 326 |
-- Return True if Str is a based number |
| 327 |
|
| 328 |
function Is_Raw_Unsigned_Format_Ghost (Str : String) return Boolean is |
| 329 |
(Is_Natural_Format_Ghost (Str) |
| 330 |
and then |
| 331 |
(declare |
| 332 |
Last_Num_Init : constant Integer := Last_Number_Ghost (Str); |
| 333 |
Starts_As_Based : constant Boolean := |
| 334 |
Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, Str'Last); |
| 335 |
Last_Num_Based : constant Integer := |
| 336 |
(if Starts_As_Based |
| 337 |
then Last_Hexa_Ghost (Str (Last_Num_Init + 2 .. Str'Last)) |
| 338 |
else Last_Num_Init); |
| 339 |
Is_Based : constant Boolean := |
| 340 |
Raw_Unsigned_Is_Based_Ghost |
| 341 |
(Str, Last_Num_Init, Last_Num_Based, Str'Last); |
| 342 |
First_Exp : constant Integer := |
| 343 |
(if Is_Based then Last_Num_Based + 2 else Last_Num_Init + 1); |
| 344 |
begin |
| 345 |
(if Starts_As_Based then |
| 346 |
Is_Based_Format_Ghost (Str (Last_Num_Init + 2 .. Str'Last)) |
| 347 |
and then Last_Num_Based < Str'Last) |
| 348 |
and then Is_Opt_Exponent_Format_Ghost |
| 349 |
(Str (First_Exp .. Str'Last)))) |
| 350 |
with |
| 351 |
Pre => Str'Last /= Positive'Last; |
| 352 |
-- Ghost function that determines if Str has the correct format for an |
| 353 |
-- unsigned number without a sign character. |
| 354 |
-- It is a natural number in base 10, optionally followed by a based |
| 355 |
-- number surrounded by delimiters # or :, optionally followed by an |
| 356 |
-- exponent part. |
| 357 |
|
| 358 |
type Split_Value_Ghost is record |
| 359 |
Value : Uns; |
| 360 |
Base : Uns; |
| 361 |
Expon : Natural; |
| 362 |
end record; |
| 363 |
|
| 364 |
function Scan_Split_No_Overflow_Ghost |
| 365 |
(Str : String; |
| 366 |
From, To : Integer) |
| 367 |
return Boolean |
| 368 |
is |
| 369 |
(declare |
| 370 |
Last_Num_Init : constant Integer := |
| 371 |
Last_Number_Ghost (Str (From .. To)); |
| 372 |
Init_Val : constant Uns_Option := |
| 373 |
Scan_Based_Number_Ghost (Str, From, Last_Num_Init); |
| 374 |
Starts_As_Based : constant Boolean := |
| 375 |
Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, To); |
| 376 |
Last_Num_Based : constant Integer := |
| 377 |
(if Starts_As_Based |
| 378 |
then Last_Hexa_Ghost (Str (Last_Num_Init + 2 .. To)) |
| 379 |
else Last_Num_Init); |
| 380 |
Based_Val : constant Uns_Option := |
| 381 |
(if Starts_As_Based and then not Init_Val.Overflow |
| 382 |
then Scan_Based_Number_Ghost |
| 383 |
(Str, Last_Num_Init + 2, Last_Num_Based, Init_Val.Value) |
| 384 |
else Init_Val); |
| 385 |
begin |
| 386 |
not Init_Val.Overflow |
| 387 |
and then |
| 388 |
(Last_Num_Init >= To - 1 |
| 389 |
or else Str (Last_Num_Init + 1) not in '#' | ':' |
| 390 |
or else Init_Val.Value in 2 .. 16) |
| 391 |
and then |
| 392 |
(not Starts_As_Based |
| 393 |
or else not Based_Val.Overflow)) |
| 394 |
with |
| 395 |
Pre => Str'Last /= Positive'Last |
| 396 |
and then From in Str'Range |
| 397 |
and then To in From .. Str'Last |
| 398 |
and then Str (From) in '0' .. '9'; |
| 399 |
-- Ghost function that determines if an overflow might occur while scanning |
| 400 |
-- the representation of an unsigned number. The computation overflows if |
| 401 |
-- either: |
| 402 |
-- * The computation of the decimal part overflows, |
| 403 |
-- * The decimal part is followed by a valid delimiter for a based |
| 404 |
-- part, and the number corresponding to the base is not a valid base, |
| 405 |
-- or |
| 406 |
-- * The computation of the based part overflows. |
| 407 |
|
| 408 |
pragma Warnings (Off, "constant * is not referenced"); |
| 409 |
function Scan_Split_Value_Ghost |
| 410 |
(Str : String; |
| 411 |
From, To : Integer) |
| 412 |
return Split_Value_Ghost |
| 413 |
is |
| 414 |
(declare |
| 415 |
Last_Num_Init : constant Integer := |
| 416 |
Last_Number_Ghost (Str (From .. To)); |
| 417 |
Init_Val : constant Uns_Option := |
| 418 |
Scan_Based_Number_Ghost (Str, From, Last_Num_Init); |
| 419 |
Starts_As_Based : constant Boolean := |
| 420 |
Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, To); |
| 421 |
Last_Num_Based : constant Integer := |
| 422 |
(if Starts_As_Based |
| 423 |
then Last_Hexa_Ghost (Str (Last_Num_Init + 2 .. To)) |
| 424 |
else Last_Num_Init); |
| 425 |
Is_Based : constant Boolean := |
| 426 |
Raw_Unsigned_Is_Based_Ghost (Str, Last_Num_Init, Last_Num_Based, To); |
| 427 |
Based_Val : constant Uns_Option := |
| 428 |
(if Starts_As_Based and then not Init_Val.Overflow |
| 429 |
then Scan_Based_Number_Ghost |
| 430 |
(Str, Last_Num_Init + 2, Last_Num_Based, Init_Val.Value) |
| 431 |
else Init_Val); |
| 432 |
First_Exp : constant Integer := |
| 433 |
(if Is_Based then Last_Num_Based + 2 else Last_Num_Init + 1); |
| 434 |
Expon : constant Natural := |
| 435 |
(if Starts_As_Exponent_Format_Ghost (Str (First_Exp .. To)) |
| 436 |
then Scan_Exponent_Ghost (Str (First_Exp .. To)) |
| 437 |
else 0); |
| 438 |
Base : constant Uns := |
| 439 |
(if Is_Based then Init_Val.Value else 10); |
| 440 |
Value : constant Uns := |
| 441 |
(if Is_Based then Based_Val.Value else Init_Val.Value); |
| 442 |
begin |
| 443 |
(Value => Value, Base => Base, Expon => Expon)) |
| 444 |
with |
| 445 |
Pre => Str'Last /= Positive'Last |
| 446 |
and then From in Str'Range |
| 447 |
and then To in From .. Str'Last |
| 448 |
and then Str (From) in '0' .. '9' |
| 449 |
and then Scan_Split_No_Overflow_Ghost (Str, From, To); |
| 450 |
-- Ghost function that scans an unsigned number without a sign character |
| 451 |
-- and return a record containing the values scanned for its value, its |
| 452 |
-- base, and its exponent. |
| 453 |
pragma Warnings (On, "constant * is not referenced"); |
| 454 |
|
| 455 |
function Raw_Unsigned_No_Overflow_Ghost |
| 456 |
(Str : String; |
| 457 |
From, To : Integer) |
| 458 |
return Boolean |
| 459 |
is |
| 460 |
(Scan_Split_No_Overflow_Ghost (Str, From, To) |
| 461 |
and then |
| 462 |
(declare |
| 463 |
Val : constant Split_Value_Ghost := Scan_Split_Value_Ghost |
| 464 |
(Str, From, To); |
| 465 |
begin |
| 466 |
not Exponent_Unsigned_Ghost |
| 467 |
(Val.Value, Val.Expon, Val.Base).Overflow)) |
| 468 |
with |
| 469 |
Pre => Str'Last /= Positive'Last |
| 470 |
and then From in Str'Range |
| 471 |
and then To in From .. Str'Last |
| 472 |
and then Str (From) in '0' .. '9'; |
| 473 |
-- Ghost function that determines if the computation of the unsigned number |
| 474 |
-- represented by Str will overflow. The computation overflows if either: |
| 475 |
-- * The scan of the string overflows, or |
| 476 |
-- * The computation of the exponentiation overflows. |
| 477 |
|
| 478 |
function Scan_Raw_Unsigned_Ghost |
| 479 |
(Str : String; |
| 480 |
From, To : Integer) |
| 481 |
return Uns |
| 482 |
is |
| 483 |
(declare |
| 484 |
Val : constant Split_Value_Ghost := Scan_Split_Value_Ghost |
| 485 |
(Str, From, To); |
| 486 |
begin |
| 487 |
Exponent_Unsigned_Ghost (Val.Value, Val.Expon, Val.Base).Value) |
| 488 |
with |
| 489 |
Pre => Str'Last /= Positive'Last |
| 490 |
and then From in Str'Range |
| 491 |
and then To in From .. Str'Last |
| 492 |
and then Str (From) in '0' .. '9' |
| 493 |
and then Raw_Unsigned_No_Overflow_Ghost (Str, From, To); |
| 494 |
-- Ghost function that scans an unsigned number without a sign character |
| 495 |
|
| 496 |
function Raw_Unsigned_Last_Ghost |
| 497 |
(Str : String; |
| 498 |
From, To : Integer) |
| 499 |
return Positive |
| 500 |
is |
| 501 |
(declare |
| 502 |
Last_Num_Init : constant Integer := |
| 503 |
Last_Number_Ghost (Str (From .. To)); |
| 504 |
Starts_As_Based : constant Boolean := |
| 505 |
Raw_Unsigned_Starts_As_Based_Ghost (Str, Last_Num_Init, To); |
| 506 |
Last_Num_Based : constant Integer := |
| 507 |
(if Starts_As_Based |
| 508 |
then Last_Hexa_Ghost (Str (Last_Num_Init + 2 .. To)) |
| 509 |
else Last_Num_Init); |
| 510 |
Is_Based : constant Boolean := |
| 511 |
Raw_Unsigned_Is_Based_Ghost (Str, Last_Num_Init, Last_Num_Based, To); |
| 512 |
First_Exp : constant Integer := |
| 513 |
(if Is_Based then Last_Num_Based + 2 else Last_Num_Init + 1); |
| 514 |
begin |
| 515 |
(if not Starts_As_Exponent_Format_Ghost (Str (First_Exp .. To)) |
| 516 |
then First_Exp |
| 517 |
elsif Str (First_Exp + 1) in '-' | '+' then |
| 518 |
Last_Number_Ghost (Str (First_Exp + 2 .. To)) + 1 |
| 519 |
else Last_Number_Ghost (Str (First_Exp + 1 .. To)) + 1)) |
| 520 |
with |
| 521 |
Pre => Str'Last /= Positive'Last |
| 522 |
and then From in Str'Range |
| 523 |
and then To in From .. Str'Last |
| 524 |
and then Str (From) in '0' .. '9', |
| 525 |
Post => Raw_Unsigned_Last_Ghost'Result >= From; |
| 526 |
-- Ghost function that returns the position of the cursor once an unsigned |
| 527 |
-- number has been seen. |
| 528 |
|
| 529 |
function Slide_To_1 (Str : String) return String |
| 530 |
with |
| 531 |
Post => |
| 532 |
Only_Space_Ghost (Str, Str'First, Str'Last) = |
| 533 |
(for all J in Str'First .. Str'Last => |
| 534 |
Slide_To_1'Result (J - Str'First + 1) = ' '); |
| 535 |
-- Slides Str so that it starts at 1 |
| 536 |
|
| 537 |
function Slide_If_Necessary (Str : String) return String is |
| 538 |
(if Str'Last = Positive'Last then Slide_To_1 (Str) else Str); |
| 539 |
-- If Str'Last = Positive'Last then slides Str so that it starts at 1 |
| 540 |
|
| 541 |
function Is_Unsigned_Ghost (Str : String) return Boolean is |
| 542 |
(declare |
| 543 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 544 |
(Str, Str'First, Str'Last); |
| 545 |
Fst_Num : constant Positive := |
| 546 |
(if Str (Non_Blank) = '+' then Non_Blank + 1 else Non_Blank); |
| 547 |
begin |
| 548 |
Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Str'Last)) |
| 549 |
and then Raw_Unsigned_No_Overflow_Ghost (Str, Fst_Num, Str'Last) |
| 550 |
and then Only_Space_Ghost |
| 551 |
(Str, Raw_Unsigned_Last_Ghost (Str, Fst_Num, Str'Last), Str'Last)) |
| 552 |
with |
| 553 |
Pre => not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 554 |
and then Str'Last /= Positive'Last; |
| 555 |
-- Ghost function that determines if Str has the correct format for an |
| 556 |
-- unsigned number, consisting in some blank characters, an optional |
| 557 |
-- + sign, a raw unsigned number which does not overflow and then some |
| 558 |
-- more blank characters. |
| 559 |
|
| 560 |
function Is_Value_Unsigned_Ghost (Str : String; Val : Uns) return Boolean is |
| 561 |
(declare |
| 562 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 563 |
(Str, Str'First, Str'Last); |
| 564 |
Fst_Num : constant Positive := |
| 565 |
(if Str (Non_Blank) = '+' then Non_Blank + 1 else Non_Blank); |
| 566 |
begin |
| 567 |
Val = Scan_Raw_Unsigned_Ghost (Str, Fst_Num, Str'Last)) |
| 568 |
with |
| 569 |
Pre => not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 570 |
and then Str'Last /= Positive'Last |
| 571 |
and then Is_Unsigned_Ghost (Str); |
| 572 |
-- Ghost function that returns True if Val is the value corresponding to |
| 573 |
-- the unsigned number represented by Str. |
| 574 |
|
| 575 |
procedure Prove_Scan_Based_Number_Ghost_Eq |
| 576 |
(Str1, Str2 : String; |
| 577 |
From, To : Integer; |
| 578 |
Base : Uns := 10; |
| 579 |
Acc : Uns := 0) |
| 580 |
with |
| 581 |
Subprogram_Variant => (Increases => From), |
| 582 |
Pre => Str1'Last /= Positive'Last |
| 583 |
and then Str2'Last /= Positive'Last |
| 584 |
and then |
| 585 |
(From > To or else (From >= Str1'First and then To <= Str1'Last)) |
| 586 |
and then |
| 587 |
(From > To or else (From >= Str2'First and then To <= Str2'Last)) |
| 588 |
and then Only_Hexa_Ghost (Str1, From, To) |
| 589 |
and then (for all J in From .. To => Str1 (J) = Str2 (J)), |
| 590 |
Post => |
| 591 |
Scan_Based_Number_Ghost (Str1, From, To, Base, Acc) |
| 592 |
= Scan_Based_Number_Ghost (Str2, From, To, Base, Acc); |
| 593 |
-- Scan_Based_Number_Ghost returns the same value on two slices which are |
| 594 |
-- equal. |
| 595 |
|
| 596 |
procedure Prove_Scan_Only_Decimal_Ghost |
| 597 |
(Str : String; |
| 598 |
Val : Uns) |
| 599 |
with |
| 600 |
Pre => Str'Last /= Positive'Last |
| 601 |
and then Str'Length >= 2 |
| 602 |
and then Str (Str'First) = ' ' |
| 603 |
and then Only_Decimal_Ghost (Str, Str'First + 1, Str'Last) |
| 604 |
and then Scan_Based_Number_Ghost (Str, Str'First + 1, Str'Last) |
| 605 |
= Wrap_Option (Val), |
| 606 |
Post => Is_Unsigned_Ghost (Slide_If_Necessary (Str)) |
| 607 |
and then |
| 608 |
Is_Value_Unsigned_Ghost (Slide_If_Necessary (Str), Val); |
| 609 |
-- Ghost lemma used in the proof of 'Image implementation, to prove that |
| 610 |
-- the result of Value_Unsigned on a decimal string is the same as the |
| 611 |
-- result of Scan_Based_Number_Ghost. |
| 612 |
|
| 613 |
-- Bundle Uns type with other types, constants and subprograms used in |
| 614 |
-- ghost code, so that this package can be instantiated once and used |
| 615 |
-- multiple times as generic formal for a given Int type. |
| 616 |
|
| 617 |
private |
| 618 |
|
| 619 |
---------------- |
| 620 |
-- Slide_To_1 -- |
| 621 |
---------------- |
| 622 |
|
| 623 |
function Slide_To_1 (Str : String) return String is |
| 624 |
(declare |
| 625 |
Res : constant String (1 .. Str'Length) := Str; |
| 626 |
begin |
| 627 |
Res); |
| 628 |
|
| 629 |
end System.Value_U_Spec; |