OSDN Git Service

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