| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L _ S P E C -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2023-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 some common |
| 34 |
-- specification functions used by the s-valxxx files. |
| 35 |
|
| 36 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 37 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 38 |
-- setting the corresponding assertion policy to Ignore. Postconditions and |
| 39 |
-- contract cases should not be executed at runtime as well, in order not to |
| 40 |
-- slow down the execution of these functions. |
| 41 |
|
| 42 |
pragma Assertion_Policy (Pre => Ignore, |
| 43 |
Post => Ignore, |
| 44 |
Contract_Cases => Ignore, |
| 45 |
Ghost => Ignore); |
| 46 |
|
| 47 |
package System.Val_Spec with |
| 48 |
SPARK_Mode, |
| 49 |
Pure, |
| 50 |
Ghost |
| 51 |
is |
| 52 |
function Only_Space_Ghost (S : String; From, To : Integer) return Boolean is |
| 53 |
(for all J in From .. To => S (J) = ' ') |
| 54 |
with |
| 55 |
Pre => From > To or else (From >= S'First and then To <= S'Last), |
| 56 |
Post => True; |
| 57 |
-- Ghost function that returns True if S has only space characters from |
| 58 |
-- index From to index To. |
| 59 |
|
| 60 |
function First_Non_Space_Ghost |
| 61 |
(S : String; |
| 62 |
From, To : Integer) return Positive |
| 63 |
with |
| 64 |
Pre => From in S'Range |
| 65 |
and then To in S'Range |
| 66 |
and then not Only_Space_Ghost (S, From, To), |
| 67 |
Post => First_Non_Space_Ghost'Result in From .. To |
| 68 |
and then S (First_Non_Space_Ghost'Result) /= ' ' |
| 69 |
and then Only_Space_Ghost |
| 70 |
(S, From, First_Non_Space_Ghost'Result - 1); |
| 71 |
-- Ghost function that returns the index of the first non-space character |
| 72 |
-- in S, which necessarily exists given the precondition on S. |
| 73 |
|
| 74 |
function Is_Boolean_Image_Ghost |
| 75 |
(Str : String; |
| 76 |
Val : Boolean) return Boolean |
| 77 |
is |
| 78 |
(not Only_Space_Ghost (Str, Str'First, Str'Last) |
| 79 |
and then |
| 80 |
(declare |
| 81 |
F : constant Positive := First_Non_Space_Ghost |
| 82 |
(Str, Str'First, Str'Last); |
| 83 |
begin |
| 84 |
(Val |
| 85 |
and then F <= Str'Last - 3 |
| 86 |
and then Str (F) in 't' | 'T' |
| 87 |
and then Str (F + 1) in 'r' | 'R' |
| 88 |
and then Str (F + 2) in 'u' | 'U' |
| 89 |
and then Str (F + 3) in 'e' | 'E' |
| 90 |
and then |
| 91 |
(if F + 3 < Str'Last then |
| 92 |
Only_Space_Ghost (Str, F + 4, Str'Last))) |
| 93 |
or else |
| 94 |
(not Val |
| 95 |
and then F <= Str'Last - 4 |
| 96 |
and then Str (F) in 'f' | 'F' |
| 97 |
and then Str (F + 1) in 'a' | 'A' |
| 98 |
and then Str (F + 2) in 'l' | 'L' |
| 99 |
and then Str (F + 3) in 's' | 'S' |
| 100 |
and then Str (F + 4) in 'e' | 'E' |
| 101 |
and then |
| 102 |
(if F + 4 < Str'Last then |
| 103 |
Only_Space_Ghost (Str, F + 5, Str'Last))))) |
| 104 |
with |
| 105 |
Ghost; |
| 106 |
-- Ghost function that returns True iff Str is the image of boolean Val, |
| 107 |
-- that is "true" or "false" in any capitalization, possibly surounded by |
| 108 |
-- space characters. |
| 109 |
|
| 110 |
function Only_Number_Ghost (Str : String; From, To : Integer) return Boolean |
| 111 |
is |
| 112 |
(for all J in From .. To => Str (J) in '0' .. '9' | '_') |
| 113 |
with |
| 114 |
Pre => From > To or else (From >= Str'First and then To <= Str'Last); |
| 115 |
-- Ghost function that returns True if S has only number characters from |
| 116 |
-- index From to index To. |
| 117 |
|
| 118 |
function Last_Number_Ghost (Str : String) return Positive |
| 119 |
with |
| 120 |
Pre => Str /= "" and then Str (Str'First) in '0' .. '9', |
| 121 |
Post => Last_Number_Ghost'Result in Str'Range |
| 122 |
and then (if Last_Number_Ghost'Result < Str'Last then |
| 123 |
Str (Last_Number_Ghost'Result + 1) not in '0' .. '9' | '_') |
| 124 |
and then Only_Number_Ghost (Str, Str'First, Last_Number_Ghost'Result); |
| 125 |
-- Ghost function that returns the index of the last character in S that |
| 126 |
-- is either a figure or underscore, which necessarily exists given the |
| 127 |
-- precondition on Str. |
| 128 |
|
| 129 |
function Is_Natural_Format_Ghost (Str : String) return Boolean is |
| 130 |
(Str /= "" |
| 131 |
and then Str (Str'First) in '0' .. '9' |
| 132 |
and then |
| 133 |
(declare |
| 134 |
L : constant Positive := Last_Number_Ghost (Str); |
| 135 |
begin |
| 136 |
Str (L) in '0' .. '9' |
| 137 |
and then (for all J in Str'First .. L => |
| 138 |
(if Str (J) = '_' then Str (J + 1) /= '_')))); |
| 139 |
-- Ghost function that determines if Str has the correct format for a |
| 140 |
-- natural number, consisting in a sequence of figures possibly separated |
| 141 |
-- by single underscores. It may be followed by other characters. |
| 142 |
|
| 143 |
function Starts_As_Exponent_Format_Ghost |
| 144 |
(Str : String; |
| 145 |
Real : Boolean := False) return Boolean |
| 146 |
is |
| 147 |
(Str'Length > 1 |
| 148 |
and then Str (Str'First) in 'E' | 'e' |
| 149 |
and then |
| 150 |
(declare |
| 151 |
Plus_Sign : constant Boolean := Str (Str'First + 1) = '+'; |
| 152 |
Minus_Sign : constant Boolean := Str (Str'First + 1) = '-'; |
| 153 |
Sign : constant Boolean := Plus_Sign or Minus_Sign; |
| 154 |
begin |
| 155 |
(if Minus_Sign then Real) |
| 156 |
and then (if Sign then Str'Length > 2) |
| 157 |
and then |
| 158 |
(declare |
| 159 |
Start : constant Natural := |
| 160 |
(if Sign then Str'First + 2 else Str'First + 1); |
| 161 |
begin |
| 162 |
Str (Start) in '0' .. '9'))); |
| 163 |
-- Ghost function that determines if Str is recognized as something which |
| 164 |
-- might be an exponent, ie. it starts with an 'e', capitalized or not, |
| 165 |
-- followed by an optional sign which can only be '-' if we are working on |
| 166 |
-- real numbers (Real is True), and then a digit in decimal notation. |
| 167 |
|
| 168 |
function Is_Opt_Exponent_Format_Ghost |
| 169 |
(Str : String; |
| 170 |
Real : Boolean := False) return Boolean |
| 171 |
is |
| 172 |
(not Starts_As_Exponent_Format_Ghost (Str, Real) |
| 173 |
or else |
| 174 |
(declare |
| 175 |
Start : constant Natural := |
| 176 |
(if Str (Str'First + 1) in '+' | '-' then Str'First + 2 |
| 177 |
else Str'First + 1); |
| 178 |
begin Is_Natural_Format_Ghost (Str (Start .. Str'Last)))); |
| 179 |
-- Ghost function that determines if Str has the correct format for an |
| 180 |
-- optional exponent, that is, either it does not start as an exponent, or |
| 181 |
-- it is in a correct format for a natural number. |
| 182 |
|
| 183 |
function Scan_Natural_Ghost |
| 184 |
(Str : String; |
| 185 |
P : Natural; |
| 186 |
Acc : Natural) |
| 187 |
return Natural |
| 188 |
with |
| 189 |
Subprogram_Variant => (Increases => P), |
| 190 |
Pre => Str /= "" and then Str (Str'First) in '0' .. '9' |
| 191 |
and then Str'Last < Natural'Last |
| 192 |
and then P in Str'First .. Last_Number_Ghost (Str) + 1; |
| 193 |
-- Ghost function that recursively computes the natural number in Str, up |
| 194 |
-- to the first number greater or equal to Natural'Last / 10, assuming Acc |
| 195 |
-- has been scanned already and scanning continues at index P. |
| 196 |
|
| 197 |
function Scan_Exponent_Ghost |
| 198 |
(Str : String; |
| 199 |
Real : Boolean := False) |
| 200 |
return Integer |
| 201 |
is |
| 202 |
(declare |
| 203 |
Plus_Sign : constant Boolean := Str (Str'First + 1) = '+'; |
| 204 |
Minus_Sign : constant Boolean := Str (Str'First + 1) = '-'; |
| 205 |
Sign : constant Boolean := Plus_Sign or Minus_Sign; |
| 206 |
Start : constant Natural := |
| 207 |
(if Sign then Str'First + 2 else Str'First + 1); |
| 208 |
Value : constant Natural := |
| 209 |
Scan_Natural_Ghost (Str (Start .. Str'Last), Start, 0); |
| 210 |
begin |
| 211 |
(if Minus_Sign then -Value else Value)) |
| 212 |
with |
| 213 |
Pre => Str'Last < Natural'Last |
| 214 |
and then Starts_As_Exponent_Format_Ghost (Str, Real), |
| 215 |
Post => (if not Real then Scan_Exponent_Ghost'Result >= 0); |
| 216 |
-- Ghost function that scans an exponent |
| 217 |
|
| 218 |
private |
| 219 |
|
| 220 |
------------------------ |
| 221 |
-- Scan_Natural_Ghost -- |
| 222 |
------------------------ |
| 223 |
|
| 224 |
function Scan_Natural_Ghost |
| 225 |
(Str : String; |
| 226 |
P : Natural; |
| 227 |
Acc : Natural) |
| 228 |
return Natural |
| 229 |
is |
| 230 |
(if P > Str'Last |
| 231 |
or else Str (P) not in '0' .. '9' | '_' |
| 232 |
or else Acc >= Integer'Last / 10 |
| 233 |
then |
| 234 |
Acc |
| 235 |
elsif Str (P) = '_' then |
| 236 |
Scan_Natural_Ghost (Str, P + 1, Acc) |
| 237 |
else |
| 238 |
(declare |
| 239 |
Shift_Acc : constant Natural := |
| 240 |
Acc * 10 + |
| 241 |
(Integer'(Character'Pos (Str (P))) - |
| 242 |
Integer'(Character'Pos ('0'))); |
| 243 |
begin |
| 244 |
Scan_Natural_Ghost (Str, P + 1, Shift_Acc))); |
| 245 |
|
| 246 |
end System.Val_Spec; |