| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L U E _ I _ 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 signed 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.Value_U_Spec; |
| 50 |
with System.Val_Spec; use System.Val_Spec; |
| 51 |
|
| 52 |
generic |
| 53 |
|
| 54 |
type Int is range <>; |
| 55 |
|
| 56 |
type Uns is mod <>; |
| 57 |
|
| 58 |
-- Additional parameters for ghost subprograms used inside contracts |
| 59 |
|
| 60 |
with package U_Spec is new System.Value_U_Spec (Uns => Uns) with Ghost; |
| 61 |
|
| 62 |
package System.Value_I_Spec with |
| 63 |
Ghost, |
| 64 |
SPARK_Mode, |
| 65 |
Always_Terminates |
| 66 |
is |
| 67 |
pragma Preelaborate; |
| 68 |
use all type U_Spec.Uns_Option; |
| 69 |
|
| 70 |
function Uns_Is_Valid_Int (Minus : Boolean; Uval : Uns) return Boolean is |
| 71 |
(if Minus then Uval <= Uns (Int'Last) + 1 |
| 72 |
else Uval <= Uns (Int'Last)) |
| 73 |
with Post => True; |
| 74 |
-- Return True if Uval (or -Uval when Minus is True) is a valid number of |
| 75 |
-- type Int. |
| 76 |
|
| 77 |
function Is_Int_Of_Uns |
| 78 |
(Minus : Boolean; |
| 79 |
Uval : Uns; |
| 80 |
Val : Int) |
| 81 |
return Boolean |
| 82 |
is |
| 83 |
(if Minus and then Uval = Uns (Int'Last) + 1 then Val = Int'First |
| 84 |
elsif Minus then Val = -(Int (Uval)) |
| 85 |
else Val = Int (Uval)) |
| 86 |
with |
| 87 |
Pre => Uns_Is_Valid_Int (Minus, Uval), |
| 88 |
Post => True; |
| 89 |
-- Return True if Uval (or -Uval when Minus is True) is equal to Val |
| 90 |
|
| 91 |
function Abs_Uns_Of_Int (Val : Int) return Uns is |
| 92 |
(if Val = Int'First then Uns (Int'Last) + 1 |
| 93 |
elsif Val < 0 then Uns (-Val) |
| 94 |
else Uns (Val)); |
| 95 |
-- Return the unsigned absolute value of Val |
| 96 |
|
| 97 |
function Slide_To_1 (Str : String) return String |
| 98 |
with |
| 99 |
Post => |
| 100 |
Only_Space_Ghost (Str, Str'First, Str'Last) = |
| 101 |
(for all J in Str'First .. Str'Last => |
| 102 |
Slide_To_1'Result (J - Str'First + 1) = ' '); |
| 103 |
-- Slides Str so that it starts at 1 |
| 104 |
|
| 105 |
function Slide_If_Necessary (Str : String) return String is |
| 106 |
(if Str'Last = Positive'Last then Slide_To_1 (Str) else Str); |
| 107 |
-- If Str'Last = Positive'Last then slides Str so that it starts at 1 |
| 108 |
|
| 109 |
function Is_Integer_Ghost (Str : String) return Boolean is |
| 110 |
(declare |
| 111 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 112 |
(Str, Str'First, Str'Last); |
| 113 |
Fst_Num : constant Positive := |
| 114 |
(if Str (Non_Blank) in '+' | '-' then Non_Blank + 1 else Non_Blank); |
| 115 |
begin |
| 116 |
U_Spec.Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Str'Last)) |
| 117 |
and then U_Spec.Raw_Unsigned_No_Overflow_Ghost |
| 118 |
(Str, Fst_Num, Str'Last) |
| 119 |
and then |
| 120 |
Uns_Is_Valid_Int |
| 121 |
(Minus => Str (Non_Blank) = '-', |
| 122 |
Uval => U_Spec.Scan_Raw_Unsigned_Ghost |
| 123 |
(Str, Fst_Num, Str'Last)) |
| 124 |
and then Only_Space_Ghost |
| 125 |
(Str, U_Spec.Raw_Unsigned_Last_Ghost |
| 126 |
(Str, Fst_Num, Str'Last), Str'Last)) |
| 127 |
with |
| 128 |
Pre => not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 129 |
and then Str'Last /= Positive'Last, |
| 130 |
Post => True; |
| 131 |
-- Ghost function that determines if Str has the correct format for a |
| 132 |
-- signed number, consisting in some blank characters, an optional |
| 133 |
-- sign, a raw unsigned number which does not overflow and then some |
| 134 |
-- more blank characters. |
| 135 |
|
| 136 |
function Is_Value_Integer_Ghost (Str : String; Val : Int) return Boolean is |
| 137 |
(declare |
| 138 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 139 |
(Str, Str'First, Str'Last); |
| 140 |
Fst_Num : constant Positive := |
| 141 |
(if Str (Non_Blank) in '+' | '-' then Non_Blank + 1 else Non_Blank); |
| 142 |
Uval : constant Uns := |
| 143 |
U_Spec.Scan_Raw_Unsigned_Ghost (Str, Fst_Num, Str'Last); |
| 144 |
begin |
| 145 |
Is_Int_Of_Uns (Minus => Str (Non_Blank) = '-', |
| 146 |
Uval => Uval, |
| 147 |
Val => Val)) |
| 148 |
with |
| 149 |
Pre => not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 150 |
and then Str'Last /= Positive'Last |
| 151 |
and then Is_Integer_Ghost (Str), |
| 152 |
Post => True; |
| 153 |
-- Ghost function that returns True if Val is the value corresponding to |
| 154 |
-- the signed number represented by Str. |
| 155 |
|
| 156 |
procedure Prove_Scan_Only_Decimal_Ghost (Str : String; Val : Int) |
| 157 |
with |
| 158 |
Ghost, |
| 159 |
Pre => Str'Last /= Positive'Last |
| 160 |
and then Str'Length >= 2 |
| 161 |
and then Str (Str'First) in ' ' | '-' |
| 162 |
and then (Str (Str'First) = '-') = (Val < 0) |
| 163 |
and then U_Spec.Only_Decimal_Ghost (Str, Str'First + 1, Str'Last) |
| 164 |
and then U_Spec.Scan_Based_Number_Ghost |
| 165 |
(Str, Str'First + 1, Str'Last) |
| 166 |
= U_Spec.Wrap_Option (Abs_Uns_Of_Int (Val)), |
| 167 |
Post => Is_Integer_Ghost (Slide_If_Necessary (Str)) |
| 168 |
and then Is_Value_Integer_Ghost (Str, Val); |
| 169 |
-- Ghost lemma used in the proof of 'Image implementation, to prove that |
| 170 |
-- the result of Value_Integer on a decimal string is the same as the |
| 171 |
-- signing the result of Scan_Based_Number_Ghost. |
| 172 |
|
| 173 |
private |
| 174 |
|
| 175 |
---------------- |
| 176 |
-- Slide_To_1 -- |
| 177 |
---------------- |
| 178 |
|
| 179 |
function Slide_To_1 (Str : String) return String is |
| 180 |
(declare |
| 181 |
Res : constant String (1 .. Str'Length) := Str; |
| 182 |
begin |
| 183 |
Res); |
| 184 |
|
| 185 |
end System.Value_I_Spec; |