]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/libgnat/s-valuei.ads
ada: Refactor the proof of the Value and Image runtime units
[thirdparty/gcc.git] / gcc / ada / libgnat / s-valuei.ads
CommitLineData
cb7584a4
EB
1------------------------------------------------------------------------------
2-- --
3-- GNAT RUN-TIME COMPONENTS --
4-- --
5-- S Y S T E M . V A L U E _ I --
6-- --
7-- S p e c --
8-- --
cccef051 9-- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
cb7584a4
EB
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 contains routines for scanning signed integer values for use
33-- in Text_IO.Integer_IO, and the Value attribute.
34
336ea8f2
CD
35pragma Assertion_Policy (Pre => Ignore,
36 Post => Ignore,
37 Contract_Cases => Ignore,
38 Ghost => Ignore,
39 Subprogram_Variant => Ignore);
336ea8f2 40
70bcf5c4 41with System.Val_Spec; use System.Val_Spec;
b3ae28dc 42with System.Value_I_Spec;
70bcf5c4 43with System.Value_U_Spec;
336ea8f2 44
cb7584a4
EB
45generic
46
47 type Int is range <>;
48
49 type Uns is mod <>;
50
649b3efa 51 with procedure Scan_Raw_Unsigned
cb7584a4
EB
52 (Str : String;
53 Ptr : not null access Integer;
649b3efa
CD
54 Max : Integer;
55 Res : out Uns);
cb7584a4 56
336ea8f2
CD
57 -- Additional parameters for ghost subprograms used inside contracts
58
70bcf5c4
CD
59 with package U_Spec is new System.Value_U_Spec (Uns => Uns) with Ghost;
60 with package Spec is new System.Value_I_Spec
61 (Int => Int, Uns => Uns, U_Spec => U_Spec)
b3ae28dc 62 with Ghost;
336ea8f2 63
cb7584a4
EB
64package System.Value_I is
65 pragma Preelaborate;
91d68769 66
649b3efa 67 procedure Scan_Integer
cb7584a4
EB
68 (Str : String;
69 Ptr : not null access Integer;
649b3efa 70 Max : Integer;
336ea8f2
CD
71 Res : out Int)
72 with
cfcc5380 73 Pre => Str'Last /= Positive'Last
336ea8f2
CD
74 -- Ptr.all .. Max is either an empty range, or a valid range in Str
75 and then (Ptr.all > Max
76 or else (Ptr.all >= Str'First and then Max <= Str'Last))
77 and then not Only_Space_Ghost (Str, Ptr.all, Max)
78 and then
79 (declare
80 Non_Blank : constant Positive := First_Non_Space_Ghost
81 (Str, Ptr.all, Max);
82 Fst_Num : constant Positive :=
83 (if Str (Non_Blank) in '+' | '-' then Non_Blank + 1
84 else Non_Blank);
85 begin
70bcf5c4
CD
86 U_Spec.Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Max))
87 and then U_Spec.Raw_Unsigned_No_Overflow_Ghost
b3ae28dc
CD
88 (Str, Fst_Num, Max)
89 and then Spec.Uns_Is_Valid_Int
cfcc5380 90 (Minus => Str (Non_Blank) = '-',
70bcf5c4 91 Uval => U_Spec.Scan_Raw_Unsigned_Ghost
b3ae28dc 92 (Str, Fst_Num, Max))),
336ea8f2
CD
93 Post =>
94 (declare
95 Non_Blank : constant Positive := First_Non_Space_Ghost
96 (Str, Ptr.all'Old, Max);
97 Fst_Num : constant Positive :=
98 (if Str (Non_Blank) in '+' | '-' then Non_Blank + 1
99 else Non_Blank);
100 Uval : constant Uns :=
70bcf5c4 101 U_Spec.Scan_Raw_Unsigned_Ghost (Str, Fst_Num, Max);
336ea8f2 102 begin
b3ae28dc
CD
103 Spec.Is_Int_Of_Uns (Minus => Str (Non_Blank) = '-',
104 Uval => Uval,
105 Val => Res)
70bcf5c4 106 and then Ptr.all = U_Spec.Raw_Unsigned_Last_Ghost
b3ae28dc 107 (Str, Fst_Num, Max));
649b3efa 108 -- This procedure scans the string starting at Str (Ptr.all) for a valid
cb7584a4
EB
109 -- integer according to the syntax described in (RM 3.5(43)). The substring
110 -- scanned extends no further than Str (Max). There are three cases for the
111 -- return:
112 --
113 -- If a valid integer is found after scanning past any initial spaces, then
114 -- Ptr.all is updated past the last character of the integer (but trailing
115 -- spaces are not scanned out).
116 --
117 -- If no valid integer is found, then Ptr.all points either to an initial
118 -- non-digit character, or to Max + 1 if the field is all spaces and the
119 -- exception Constraint_Error is raised.
120 --
121 -- If a syntactically valid integer is scanned, but the value is out of
122 -- range, or, in the based case, the base value is out of range or there
123 -- is an out of range digit, then Ptr.all points past the integer, and
124 -- Constraint_Error is raised.
125 --
126 -- Note: these rules correspond to the requirements for leaving the pointer
127 -- positioned in Text_Io.Get
128 --
129 -- Note: if Str is null, i.e. if Max is less than Ptr, then this is a
130 -- special case of an all-blank string, and Ptr is unchanged, and hence
131 -- is greater than Max as required in this case.
132
336ea8f2 133 function Value_Integer (Str : String) return Int
cfcc5380
YM
134 with
135 Pre => not Only_Space_Ghost (Str, Str'First, Str'Last)
136 and then Str'Length /= Positive'Last
b3ae28dc
CD
137 and then Spec.Is_Integer_Ghost (Spec.Slide_If_Necessary (Str)),
138 Post => Spec.Is_Value_Integer_Ghost
139 (Spec.Slide_If_Necessary (Str), Value_Integer'Result),
336ea8f2 140 Subprogram_Variant => (Decreases => Str'First);
cb7584a4
EB
141 -- Used in computing X'Value (Str) where X is a signed integer type whose
142 -- base range does not exceed the base range of Integer. Str is the string
143 -- argument of the attribute. Constraint_Error is raised if the string is
144 -- malformed, or if the value is out of range.
145
146end System.Value_I;