OSDN Git Service

2004-05-17 Steve Kargl <kargls@comcast.net>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-valuns.adb
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-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 ------------------------------------------------------------------------------
33
34 with System.Unsigned_Types; use System.Unsigned_Types;
35 with System.Val_Util;       use System.Val_Util;
36
37 package body System.Val_Uns is
38
39    -------------------
40    -- Scan_Unsigned --
41    -------------------
42
43    function Scan_Unsigned
44      (Str  : String;
45       Ptr  : access Integer;
46       Max  : Integer)
47       return Unsigned
48    is
49       P : Integer;
50       --  Local copy of the pointer
51
52       Uval : Unsigned;
53       --  Accumulated unsigned integer result
54
55       Expon : Integer;
56       --  Exponent value
57
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.
62
63       Overflow : Boolean := False;
64       --  Set True if overflow is detected at any point
65
66       Start : Positive;
67       --  Save location of first non-blank character
68
69       Base_Char : Character;
70       --  Base character (# or :) in based case
71
72       Base : Unsigned := 10;
73       --  Base value (reset in based case)
74
75       Digit : Unsigned;
76       --  Digit value
77
78    begin
79       Scan_Sign (Str, Ptr, Max, Minus, Start);
80
81       if Str (Ptr.all) not in '0' .. '9' then
82          Ptr.all := Start;
83          raise Constraint_Error;
84       end if;
85
86       P := Ptr.all;
87       Uval := Character'Pos (Str (P)) - Character'Pos ('0');
88       P := P + 1;
89
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.
92
93       declare
94          Umax : constant := (Unsigned'Last - 9) / 10;
95          --  Max value which cannot overflow on accumulating next digit
96
97          Umax10 : constant := Unsigned'Last / 10;
98          --  Numbers bigger than Umax10 overflow if multiplied by 10
99
100       begin
101          --  Loop through decimal digits
102          loop
103             exit when P > Max;
104
105             Digit := Character'Pos (Str (P)) - Character'Pos ('0');
106
107             --  Non-digit encountered
108
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;
115
116             --  Accumulate result, checking for overflow
117
118             else
119                if Uval <= Umax then
120                   Uval := 10 * Uval + Digit;
121
122                elsif Uval > Umax10 then
123                   Overflow := True;
124
125                else
126                   Uval := 10 * Uval + Digit;
127
128                   if Uval < Umax10 then
129                      Overflow := True;
130                   end if;
131                end if;
132
133                P := P + 1;
134             end if;
135          end loop;
136       end;
137
138       Ptr.all := P;
139
140       --  Deal with based case
141
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;
147
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.
152
153          if Base not in 2 .. 16 then
154             Overflow := True;
155             Base :=  16;
156          end if;
157
158          --  Scan out based integer
159
160          declare
161             Umax : constant Unsigned := (Unsigned'Last - Base + 1) / Base;
162             --  Max value which cannot overflow on accumulating next digit
163
164             UmaxB : constant Unsigned := Unsigned'Last / Base;
165             --  Numbers bigger than UmaxB overflow if multiplied by base
166
167          begin
168             --  Loop to scan out based integer value
169
170             loop
171                --  We require a digit at this stage
172
173                if Str (P) in '0' .. '9' then
174                   Digit := Character'Pos (Str (P)) - Character'Pos ('0');
175
176                elsif Str (P) in 'A' .. 'F' then
177                   Digit :=
178                     Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
179
180                elsif Str (P) in 'a' .. 'f' then
181                   Digit :=
182                     Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
183
184                --  If we don't have a digit, then this is not a based number
185                --  after all, so we use the value we scanned out as the base
186                --  (now in Base), and the pointer to the base character was
187                --  already stored in Ptr.all.
188
189                else
190                   Uval := Base;
191                   exit;
192                end if;
193
194                --  If digit is too large, just signal overflow and continue.
195                --  The idea here is to keep scanning as long as the input is
196                --  syntactically valid, even if we have detected overflow
197
198                if Digit >= Base then
199                   Overflow := True;
200
201                --  Here we accumulate the value, checking overflow
202
203                elsif Uval <= Umax then
204                   Uval := Base * Uval + Digit;
205
206                elsif Uval > UmaxB then
207                   Overflow := True;
208
209                else
210                   Uval := Base * Uval + Digit;
211
212                   if Uval < UmaxB then
213                      Overflow := True;
214                   end if;
215                end if;
216
217                --  If at end of string with no base char, not a based number
218                --  but we signal Constraint_Error and set the pointer past
219                --  the end of the field, since this is what the ACVC tests
220                --  seem to require, see CE3704N, line 204.
221
222                P := P + 1;
223
224                if P > Max then
225                   Ptr.all := P;
226                   raise Constraint_Error;
227                end if;
228
229                --  If terminating base character, we are done with loop
230
231                if Str (P) = Base_Char then
232                   Ptr.all := P + 1;
233                   exit;
234
235                --  Deal with underscore
236
237                elsif Str (P) = '_' then
238                   Scan_Underscore (Str, P, Ptr, Max, True);
239                end if;
240
241             end loop;
242          end;
243       end if;
244
245       --  Come here with scanned unsigned value in Uval. The only remaining
246       --  required step is to deal with exponent if one is present.
247
248       Expon := Scan_Exponent (Str, Ptr, Max);
249
250       if Expon /= 0 and then Uval /= 0 then
251
252          --  For non-zero value, scale by exponent value. No need to do this
253          --  efficiently, since use of exponent in integer literals is rare,
254          --  and in any case the exponent cannot be very large.
255
256          declare
257             UmaxB : constant Unsigned := Unsigned'Last / Base;
258             --  Numbers bigger than UmaxB overflow if multiplied by base
259
260          begin
261             for J in 1 .. Expon loop
262                if Uval > UmaxB then
263                   Overflow := True;
264                   exit;
265                end if;
266
267                Uval := Uval * Base;
268             end loop;
269          end;
270       end if;
271
272       --  Return result, dealing with sign and overflow
273
274       if Overflow or else (Minus and then Uval /= 0) then
275          raise Constraint_Error;
276       else
277          return Uval;
278       end if;
279    end Scan_Unsigned;
280
281    --------------------
282    -- Value_Unsigned --
283    --------------------
284
285    function Value_Unsigned (Str : String) return Unsigned is
286       V : Unsigned;
287       P : aliased Integer := Str'First;
288
289    begin
290       V := Scan_Unsigned (Str, P'Access, Str'Last);
291       Scan_Trailing_Blanks (Str, P);
292       return V;
293
294    end Value_Unsigned;
295
296 end System.Val_Uns;