2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / s-valllu.adb
blob2403e9d2337e55c556bf0b0b567c976498c7c89d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L _ L L U --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-1997 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with System.Unsigned_Types; use System.Unsigned_Types;
35 with System.Val_Util; use System.Val_Util;
37 package body System.Val_LLU is
39 -----------------------------
40 -- Scan_Long_Long_Unsigned --
41 -----------------------------
43 function Scan_Long_Long_Unsigned
44 (Str : String;
45 Ptr : access Integer;
46 Max : Integer)
47 return Long_Long_Unsigned
49 P : Integer;
50 -- Local copy of the pointer
52 Uval : Long_Long_Unsigned;
53 -- Accumulated unsigned integer result
55 Expon : Integer;
56 -- Exponent value
58 Minus : Boolean := False;
59 -- Set to True if minus sign is present, otherwise to False. Note that
60 -- a minus sign is permissible for the singular case of -0, and in any
61 -- case the pointer is left pointing past a negative integer literal.
63 Overflow : Boolean := False;
64 -- Set True if overflow is detected at any point
66 Start : Positive;
67 -- Save location of first non-blank character
69 Base_Char : Character;
70 -- Base character (# or :) in based case
72 Base : Long_Long_Unsigned := 10;
73 -- Base value (reset in based case)
75 Digit : Long_Long_Unsigned;
76 -- Digit value
78 begin
79 Scan_Sign (Str, Ptr, Max, Minus, Start);
81 if Str (Ptr.all) not in '0' .. '9' then
82 Ptr.all := Start;
83 raise Constraint_Error;
84 end if;
86 P := Ptr.all;
87 Uval := Character'Pos (Str (P)) - Character'Pos ('0');
88 P := P + 1;
90 -- Scan out digits of what is either the number or the base.
91 -- In either case, we are definitely scanning out in base 10.
93 declare
94 Umax : constant := (Long_Long_Unsigned'Last - 9) / 10;
95 -- Max value which cannot overflow on accumulating next digit
97 Umax10 : constant := Long_Long_Unsigned'Last / 10;
98 -- Numbers bigger than Umax10 overflow if multiplied by 10
100 begin
101 -- Loop through decimal digits
102 loop
103 exit when P > Max;
105 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
107 -- Non-digit encountered
109 if Digit > 9 then
110 if Str (P) = '_' then
111 Scan_Underscore (Str, P, Ptr, Max, False);
112 else
113 exit;
114 end if;
116 -- Accumulate result, checking for overflow
118 else
119 if Uval <= Umax then
120 Uval := 10 * Uval + Digit;
122 elsif Uval > Umax10 then
123 Overflow := True;
125 else
126 Uval := 10 * Uval + Digit;
128 if Uval < Umax10 then
129 Overflow := True;
130 end if;
131 end if;
133 P := P + 1;
134 end if;
135 end loop;
136 end;
138 Ptr.all := P;
140 -- Deal with based case
142 if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
143 Base_Char := Str (P);
144 P := P + 1;
145 Base := Uval;
146 Uval := 0;
148 -- Check base value. Overflow is set True if we find a bad base, or
149 -- a digit that is out of range of the base. That way, we scan out
150 -- the numeral that is still syntactically correct, though illegal.
151 -- We use a safe base of 16 for this scan, to avoid zero divide.
153 if Base not in 2 .. 16 then
154 Overflow := True;
155 Base := 16;
156 end if;
158 -- Scan out based integer
160 declare
161 Umax : constant Long_Long_Unsigned :=
162 (Long_Long_Unsigned'Last - Base + 1) / Base;
163 -- Max value which cannot overflow on accumulating next digit
165 UmaxB : constant Long_Long_Unsigned :=
166 Long_Long_Unsigned'Last / Base;
167 -- Numbers bigger than UmaxB overflow if multiplied by base
169 begin
170 -- Loop to scan out based integer value
172 loop
173 -- We require a digit at this stage
175 if Str (P) in '0' .. '9' then
176 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
178 elsif Str (P) in 'A' .. 'F' then
179 Digit :=
180 Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
182 elsif Str (P) in 'a' .. 'f' then
183 Digit :=
184 Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
186 -- If we don't have a digit, then this is not a based number
187 -- after all, so we use the value we scanned out as the base
188 -- (now in Base), and the pointer to the base character was
189 -- already stored in Ptr.all.
191 else
192 Uval := Base;
193 exit;
194 end if;
196 -- If digit is too large, just signal overflow and continue.
197 -- The idea here is to keep scanning as long as the input is
198 -- syntactically valid, even if we have detected overflow
200 if Digit >= Base then
201 Overflow := True;
203 -- Here we accumulate the value, checking overflow
205 elsif Uval <= Umax then
206 Uval := Base * Uval + Digit;
208 elsif Uval > UmaxB then
209 Overflow := True;
211 else
212 Uval := Base * Uval + Digit;
214 if Uval < UmaxB then
215 Overflow := True;
216 end if;
217 end if;
219 -- If at end of string with no base char, not a based number
220 -- but we signal Constraint_Error and set the pointer past
221 -- the end of the field, since this is what the ACVC tests
222 -- seem to require, see CE3704N, line 204.
224 P := P + 1;
226 if P > Max then
227 Ptr.all := P;
228 raise Constraint_Error;
229 end if;
231 -- If terminating base character, we are done with loop
233 if Str (P) = Base_Char then
234 Ptr.all := P + 1;
235 exit;
237 -- Deal with underscore
239 elsif Str (P) = '_' then
240 Scan_Underscore (Str, P, Ptr, Max, True);
241 end if;
243 end loop;
244 end;
245 end if;
247 -- Come here with scanned unsigned value in Uval. The only remaining
248 -- required step is to deal with exponent if one is present.
250 Expon := Scan_Exponent (Str, Ptr, Max);
252 if Expon /= 0 and then Uval /= 0 then
254 -- For non-zero value, scale by exponent value. No need to do this
255 -- efficiently, since use of exponent in integer literals is rare,
256 -- and in any case the exponent cannot be very large.
258 declare
259 UmaxB : constant Long_Long_Unsigned :=
260 Long_Long_Unsigned'Last / Base;
261 -- Numbers bigger than UmaxB overflow if multiplied by base
263 begin
264 for J in 1 .. Expon loop
265 if Uval > UmaxB then
266 Overflow := True;
267 exit;
268 end if;
270 Uval := Uval * Base;
271 end loop;
272 end;
273 end if;
275 -- Return result, dealing with sign and overflow
277 if Overflow or else (Minus and then Uval /= 0) then
278 raise Constraint_Error;
279 else
280 return Uval;
281 end if;
282 end Scan_Long_Long_Unsigned;
284 ------------------------------
285 -- Value_Long_Long_Unsigned --
286 ------------------------------
288 function Value_Long_Long_Unsigned
289 (Str : String)
290 return Long_Long_Unsigned
292 V : Long_Long_Unsigned;
293 P : aliased Integer := Str'First;
295 begin
296 V := Scan_Long_Long_Unsigned (Str, P'Access, Str'Last);
297 Scan_Trailing_Blanks (Str, P);
298 return V;
300 end Value_Long_Long_Unsigned;
302 end System.Val_LLU;