* gcc.c (getenv_spec_function): New function.
[official-gcc.git] / gcc / ada / s-valuns.adb
blobd9d7940a3560787fed50961544228a03ed3309c8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L _ U N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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_Uns is
39 -----------------------
40 -- Scan_Raw_Unsigned --
41 -----------------------
43 function Scan_Raw_Unsigned
44 (Str : String;
45 Ptr : access Integer;
46 Max : Integer) return Unsigned
48 P : Integer;
49 -- Local copy of the pointer
51 Uval : Unsigned;
52 -- Accumulated unsigned integer result
54 Expon : Integer;
55 -- Exponent value
57 Overflow : Boolean := False;
58 -- Set True if overflow is detected at any point
60 Base_Char : Character;
61 -- Base character (# or :) in based case
63 Base : Unsigned := 10;
64 -- Base value (reset in based case)
66 Digit : Unsigned;
67 -- Digit value
69 begin
70 P := Ptr.all;
71 Uval := Character'Pos (Str (P)) - Character'Pos ('0');
72 P := P + 1;
74 -- Scan out digits of what is either the number or the base.
75 -- In either case, we are definitely scanning out in base 10.
77 declare
78 Umax : constant := (Unsigned'Last - 9) / 10;
79 -- Max value which cannot overflow on accumulating next digit
81 Umax10 : constant := Unsigned'Last / 10;
82 -- Numbers bigger than Umax10 overflow if multiplied by 10
84 begin
85 -- Loop through decimal digits
86 loop
87 exit when P > Max;
89 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
91 -- Non-digit encountered
93 if Digit > 9 then
94 if Str (P) = '_' then
95 Scan_Underscore (Str, P, Ptr, Max, False);
96 else
97 exit;
98 end if;
100 -- Accumulate result, checking for overflow
102 else
103 if Uval <= Umax then
104 Uval := 10 * Uval + Digit;
106 elsif Uval > Umax10 then
107 Overflow := True;
109 else
110 Uval := 10 * Uval + Digit;
112 if Uval < Umax10 then
113 Overflow := True;
114 end if;
115 end if;
117 P := P + 1;
118 end if;
119 end loop;
120 end;
122 Ptr.all := P;
124 -- Deal with based case
126 if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
127 Base_Char := Str (P);
128 P := P + 1;
129 Base := Uval;
130 Uval := 0;
132 -- Check base value. Overflow is set True if we find a bad base, or
133 -- a digit that is out of range of the base. That way, we scan out
134 -- the numeral that is still syntactically correct, though illegal.
135 -- We use a safe base of 16 for this scan, to avoid zero divide.
137 if Base not in 2 .. 16 then
138 Overflow := True;
139 Base := 16;
140 end if;
142 -- Scan out based integer
144 declare
145 Umax : constant Unsigned := (Unsigned'Last - Base + 1) / Base;
146 -- Max value which cannot overflow on accumulating next digit
148 UmaxB : constant Unsigned := Unsigned'Last / Base;
149 -- Numbers bigger than UmaxB overflow if multiplied by base
151 begin
152 -- Loop to scan out based integer value
154 loop
155 -- We require a digit at this stage
157 if Str (P) in '0' .. '9' then
158 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
160 elsif Str (P) in 'A' .. 'F' then
161 Digit :=
162 Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
164 elsif Str (P) in 'a' .. 'f' then
165 Digit :=
166 Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
168 -- If we don't have a digit, then this is not a based number
169 -- after all, so we use the value we scanned out as the base
170 -- (now in Base), and the pointer to the base character was
171 -- already stored in Ptr.all.
173 else
174 Uval := Base;
175 exit;
176 end if;
178 -- If digit is too large, just signal overflow and continue.
179 -- The idea here is to keep scanning as long as the input is
180 -- syntactically valid, even if we have detected overflow
182 if Digit >= Base then
183 Overflow := True;
185 -- Here we accumulate the value, checking overflow
187 elsif Uval <= Umax then
188 Uval := Base * Uval + Digit;
190 elsif Uval > UmaxB then
191 Overflow := True;
193 else
194 Uval := Base * Uval + Digit;
196 if Uval < UmaxB then
197 Overflow := True;
198 end if;
199 end if;
201 -- If at end of string with no base char, not a based number
202 -- but we signal Constraint_Error and set the pointer past
203 -- the end of the field, since this is what the ACVC tests
204 -- seem to require, see CE3704N, line 204.
206 P := P + 1;
208 if P > Max then
209 Ptr.all := P;
210 raise Constraint_Error;
211 end if;
213 -- If terminating base character, we are done with loop
215 if Str (P) = Base_Char then
216 Ptr.all := P + 1;
217 exit;
219 -- Deal with underscore
221 elsif Str (P) = '_' then
222 Scan_Underscore (Str, P, Ptr, Max, True);
223 end if;
225 end loop;
226 end;
227 end if;
229 -- Come here with scanned unsigned value in Uval. The only remaining
230 -- required step is to deal with exponent if one is present.
232 Expon := Scan_Exponent (Str, Ptr, Max);
234 if Expon /= 0 and then Uval /= 0 then
236 -- For non-zero value, scale by exponent value. No need to do this
237 -- efficiently, since use of exponent in integer literals is rare,
238 -- and in any case the exponent cannot be very large.
240 declare
241 UmaxB : constant Unsigned := Unsigned'Last / Base;
242 -- Numbers bigger than UmaxB overflow if multiplied by base
244 begin
245 for J in 1 .. Expon loop
246 if Uval > UmaxB then
247 Overflow := True;
248 exit;
249 end if;
251 Uval := Uval * Base;
252 end loop;
253 end;
254 end if;
256 -- Return result, dealing with sign and overflow
258 if Overflow then
259 raise Constraint_Error;
260 else
261 return Uval;
262 end if;
263 end Scan_Raw_Unsigned;
265 -------------------
266 -- Scan_Unsigned --
267 -------------------
269 function Scan_Unsigned
270 (Str : String;
271 Ptr : access Integer;
272 Max : Integer) return Unsigned
274 Start : Positive;
275 -- Save location of first non-blank character
277 begin
278 Scan_Plus_Sign (Str, Ptr, Max, Start);
280 if Str (Ptr.all) not in '0' .. '9' then
281 Ptr.all := Start;
282 raise Constraint_Error;
283 end if;
285 return Scan_Raw_Unsigned (Str, Ptr, Max);
286 end Scan_Unsigned;
288 --------------------
289 -- Value_Unsigned --
290 --------------------
292 function Value_Unsigned (Str : String) return Unsigned is
293 V : Unsigned;
294 P : aliased Integer := Str'First;
295 begin
296 V := Scan_Unsigned (Str, P'Access, Str'Last);
297 Scan_Trailing_Blanks (Str, P);
298 return V;
299 end Value_Unsigned;
301 end System.Val_Uns;