blob: 591308b3619a5f236e38446ec304a40d07db4a4b [file] [log] [blame]
Zack Weinbergb0699da2000-03-07 20:58:47 +00001/* Parse C expressions for cpplib.
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
Tom Tromey71c10032008-05-06 17:15:07 +00003 2002, 2004, 2008 Free Software Foundation.
Richard Kennere38992e2000-04-18 20:42:00 +00004 Contributed by Per Bothner, 1994.
Per Bothner7f2935c1995-03-16 13:59:07 -08005
6This program is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
Kelley Cook200031d2005-06-29 02:34:39 +000018Foundation, 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080020
Per Bothner7f2935c1995-03-16 13:59:07 -080021#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000022#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000023#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000024#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080025
Neil Booth91318902002-05-26 18:42:21 +000026#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28#define LOW_PART(num_part) (num_part & HALF_MASK)
29#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30
Zack Weinbergcf00a882000-07-08 02:33:00 +000031struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000032{
Neil Booth68e652752002-07-20 13:31:56 +000033 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000034 cpp_num value; /* The value logically "right" of op. */
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +000035 source_location loc; /* The location of this value. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000036 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080037};
Per Bothner7f2935c1995-03-16 13:59:07 -080038
Neil Booth91318902002-05-26 18:42:21 +000039/* Some simple utility routines on double integers. */
40#define num_zerop(num) ((num.low | num.high) == 0)
41#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000042static bool num_positive (cpp_num, size_t);
43static bool num_greater_eq (cpp_num, cpp_num, size_t);
44static cpp_num num_trim (cpp_num, size_t);
45static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000046
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000047static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
48static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49static cpp_num num_negate (cpp_num, size_t);
50static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
51static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
54 enum cpp_ttype);
55static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
56static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
57static cpp_num num_lshift (cpp_num, size_t, size_t);
58static cpp_num num_rshift (cpp_num, size_t, size_t);
Neil Booth91318902002-05-26 18:42:21 +000059
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000060static cpp_num append_digit (cpp_num, int, int, size_t);
61static cpp_num parse_defined (cpp_reader *);
62static cpp_num eval_token (cpp_reader *, const cpp_token *);
63static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64static unsigned int interpret_float_suffix (const uchar *, size_t);
65static unsigned int interpret_int_suffix (const uchar *, size_t);
66static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000067
Neil Boothcd7ab832002-05-29 17:15:42 +000068/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000069#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000071
Zack Weinberg15dad1d2000-05-18 15:55:46 +000072/* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000074#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000075 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000076#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000077 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000079
Neil Boothcd7ab832002-05-29 17:15:42 +000080/* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
83static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000084interpret_float_suffix (const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080085{
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020086 size_t f, l, w, q, i, d;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000087 size_t r, k, u, h;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020088
89 f = l = w = q = i = d = 0;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000090 r = k = u = h = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000091
Neil Boothcd7ab832002-05-29 17:15:42 +000092 while (len--)
93 switch (s[len])
94 {
Chao-ying Fuac6b1c62007-08-30 23:05:17 +000095 case 'r': case 'R': r++; break;
96 case 'k': case 'K': k++; break;
97 case 'u': case 'U': u++; break;
98 case 'h': case 'H': h++; break;
Janis Johnson30e04922007-05-14 23:43:07 +000099 case 'f': case 'F':
100 if (d > 0)
101 return 0;
102 f++;
103 break;
104 case 'l': case 'L':
105 if (d > 0)
106 return 0;
107 l++;
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000108 /* If there are two Ls, they must be adjacent and the same case. */
109 if (l == 2 && s[len] != s[len + 1])
110 return 0;
Janis Johnson30e04922007-05-14 23:43:07 +0000111 break;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200112 case 'w': case 'W':
113 if (d > 0)
114 return 0;
115 w++;
116 break;
117 case 'q': case 'Q':
118 if (d > 0)
119 return 0;
120 q++;
121 break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000122 case 'i': case 'I':
123 case 'j': case 'J': i++; break;
Janis Johnson30e04922007-05-14 23:43:07 +0000124 case 'd': case 'D': d++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000125 default:
126 return 0;
127 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000128
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000129 if (r + k > 1 || h > 1 || l > 2 || u > 1)
130 return 0;
131
132 if (r == 1)
133 {
134 if (f || i || d || w || q)
135 return 0;
136
137 return (CPP_N_FRACT
138 | (u ? CPP_N_UNSIGNED : 0)
139 | (h ? CPP_N_SMALL :
140 l == 2 ? CPP_N_LARGE :
141 l == 1 ? CPP_N_MEDIUM : 0));
142 }
143
144 if (k == 1)
145 {
146 if (f || i || d || w || q)
147 return 0;
148
149 return (CPP_N_ACCUM
150 | (u ? CPP_N_UNSIGNED : 0)
151 | (h ? CPP_N_SMALL :
152 l == 2 ? CPP_N_LARGE :
153 l == 1 ? CPP_N_MEDIUM : 0));
154 }
155
156 if (f + l + w + q > 1 || i > 1 || h + u > 0)
Neil Boothcd7ab832002-05-29 17:15:42 +0000157 return 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000158
Jon Grimmad6ed772005-12-06 23:13:15 +0000159 /* Allow dd, df, dl suffixes for decimal float constants. */
160 if (d && ((d + f + l != 2) || i))
161 return 0;
162
Neil Boothcd7ab832002-05-29 17:15:42 +0000163 return ((i ? CPP_N_IMAGINARY : 0)
164 | (f ? CPP_N_SMALL :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200165 l ? CPP_N_LARGE :
166 w ? CPP_N_MD_W :
167 q ? CPP_N_MD_Q : CPP_N_MEDIUM)
Jon Grimmad6ed772005-12-06 23:13:15 +0000168 | (d ? CPP_N_DFLOAT : 0));
Neil Boothcd7ab832002-05-29 17:15:42 +0000169}
170
171/* Subroutine of cpp_classify_number. S points to an integer suffix
172 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
173 flag vector describing the suffix. */
174static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000175interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000176{
177 size_t u, l, i;
178
179 u = l = i = 0;
180
181 while (len--)
182 switch (s[len])
183 {
184 case 'u': case 'U': u++; break;
185 case 'i': case 'I':
186 case 'j': case 'J': i++; break;
187 case 'l': case 'L': l++;
188 /* If there are two Ls, they must be adjacent and the same case. */
189 if (l == 2 && s[len] != s[len + 1])
190 return 0;
191 break;
192 default:
193 return 0;
194 }
195
196 if (l > 2 || u > 1 || i > 1)
197 return 0;
198
199 return ((i ? CPP_N_IMAGINARY : 0)
200 | (u ? CPP_N_UNSIGNED : 0)
201 | ((l == 0) ? CPP_N_SMALL
202 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
203}
204
205/* Categorize numeric constants according to their field (integer,
206 floating point, or invalid), radix (decimal, octal, hexadecimal),
207 and type suffixes. */
208unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000209cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
Neil Boothcd7ab832002-05-29 17:15:42 +0000210{
211 const uchar *str = token->val.str.text;
212 const uchar *limit;
213 unsigned int max_digit, result, radix;
214 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
215
216 /* If the lexer has done its job, length one can only be a single
217 digit. Fast-path this very common case. */
218 if (token->val.str.len == 1)
219 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
220
221 limit = str + token->val.str.len;
222 float_flag = NOT_FLOAT;
223 max_digit = 0;
224 radix = 10;
225
226 /* First, interpret the radix. */
227 if (*str == '0')
228 {
229 radix = 8;
230 str++;
231
232 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000233 if ((*str == 'x' || *str == 'X')
234 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000235 {
236 radix = 16;
237 str++;
238 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000239 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
240 {
241 radix = 2;
242 str++;
243 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000244 }
245
246 /* Now scan for a well-formed integer or float. */
247 for (;;)
248 {
249 unsigned int c = *str++;
250
251 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
252 {
253 c = hex_value (c);
254 if (c > max_digit)
255 max_digit = c;
256 }
257 else if (c == '.')
258 {
259 if (float_flag == NOT_FLOAT)
260 float_flag = AFTER_POINT;
261 else
262 SYNTAX_ERROR ("too many decimal points in number");
263 }
264 else if ((radix <= 10 && (c == 'e' || c == 'E'))
265 || (radix == 16 && (c == 'p' || c == 'P')))
266 {
267 float_flag = AFTER_EXPON;
268 break;
269 }
270 else
271 {
272 /* Start of suffix. */
273 str--;
274 break;
275 }
276 }
277
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000278 /* The suffix may be for decimal fixed-point constants without exponent. */
279 if (radix != 16 && float_flag == NOT_FLOAT)
280 {
281 result = interpret_float_suffix (str, limit - str);
282 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
283 {
284 result |= CPP_N_FLOATING;
285 /* We need to restore the radix to 10, if the radix is 8. */
286 if (radix == 8)
287 radix = 10;
288
289 if (CPP_PEDANTIC (pfile))
290 cpp_error (pfile, CPP_DL_PEDWARN,
291 "fixed-point constants are a GCC extension");
292 goto syntax_ok;
293 }
294 else
295 result = 0;
296 }
297
Neil Boothcd7ab832002-05-29 17:15:42 +0000298 if (float_flag != NOT_FLOAT && radix == 8)
299 radix = 10;
300
301 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000302 {
303 if (radix == 2)
304 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
305 else
306 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
307 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000308
309 if (float_flag != NOT_FLOAT)
310 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000311 if (radix == 2)
312 {
313 cpp_error (pfile, CPP_DL_ERROR,
314 "invalid prefix \"0b\" for floating constant");
315 return CPP_N_INVALID;
316 }
317
Neil Boothcd7ab832002-05-29 17:15:42 +0000318 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000319 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000320 "use of C99 hexadecimal floating constant");
321
322 if (float_flag == AFTER_EXPON)
323 {
324 if (*str == '+' || *str == '-')
325 str++;
326
327 /* Exponent is decimal, even if string is a hex float. */
328 if (!ISDIGIT (*str))
329 SYNTAX_ERROR ("exponent has no digits");
330
331 do
332 str++;
333 while (ISDIGIT (*str));
334 }
335 else if (radix == 16)
336 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
337
338 result = interpret_float_suffix (str, limit - str);
339 if (result == 0)
340 {
John David Anglin0527bc42003-11-01 22:56:54 +0000341 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000342 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200343 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000344 return CPP_N_INVALID;
345 }
346
347 /* Traditional C didn't accept any floating suffixes. */
348 if (limit != str
349 && CPP_WTRADITIONAL (pfile)
350 && ! cpp_sys_macro_p (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000351 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000352 "traditional C rejects the \"%.*s\" suffix",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200353 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000354
Jon Grimmad6ed772005-12-06 23:13:15 +0000355 /* Radix must be 10 for decimal floats. */
356 if ((result & CPP_N_DFLOAT) && radix != 10)
357 {
358 cpp_error (pfile, CPP_DL_ERROR,
359 "invalid suffix \"%.*s\" with hexadecimal floating constant",
360 (int) (limit - str), str);
361 return CPP_N_INVALID;
362 }
363
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000364 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
365 cpp_error (pfile, CPP_DL_PEDWARN,
366 "fixed-point constants are a GCC extension");
367
Janis Johnson5a6bb572007-05-14 23:45:40 +0000368 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
369 cpp_error (pfile, CPP_DL_PEDWARN,
370 "decimal float constants are a GCC extension");
371
Neil Boothcd7ab832002-05-29 17:15:42 +0000372 result |= CPP_N_FLOATING;
373 }
374 else
375 {
376 result = interpret_int_suffix (str, limit - str);
377 if (result == 0)
378 {
John David Anglin0527bc42003-11-01 22:56:54 +0000379 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000380 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200381 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000382 return CPP_N_INVALID;
383 }
384
Zack Weinberg56da7202002-08-02 04:18:16 +0000385 /* Traditional C only accepted the 'L' suffix.
386 Suppress warning about 'LL' with -Wno-long-long. */
387 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
388 {
389 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
390 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
391
392 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
John David Anglin0527bc42003-11-01 22:56:54 +0000393 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000394 "traditional C rejects the \"%.*s\" suffix",
395 (int) (limit - str), str);
396 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000397
398 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
399 && ! CPP_OPTION (pfile, c99)
400 && CPP_OPTION (pfile, warn_long_long))
John David Anglin0527bc42003-11-01 22:56:54 +0000401 cpp_error (pfile, CPP_DL_PEDWARN,
402 "use of C99 long long integer constant");
Neil Boothcd7ab832002-05-29 17:15:42 +0000403
404 result |= CPP_N_INTEGER;
405 }
406
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000407 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000408 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000409 cpp_error (pfile, CPP_DL_PEDWARN,
410 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000411 if (radix == 2 && CPP_PEDANTIC (pfile))
412 cpp_error (pfile, CPP_DL_PEDWARN,
413 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000414
415 if (radix == 10)
416 result |= CPP_N_DECIMAL;
417 else if (radix == 16)
418 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000419 else if (radix == 2)
420 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000421 else
422 result |= CPP_N_OCTAL;
423
424 return result;
425
426 syntax_error:
427 return CPP_N_INVALID;
428}
429
430/* cpp_interpret_integer converts an integer constant into a cpp_num,
431 of precision options->precision.
432
433 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000434 because the preprocessor doesn't need it and we don't want to
435 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000436cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000437cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
438 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000439{
440 const uchar *p, *end;
441 cpp_num result;
442
443 result.low = 0;
444 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000445 result.unsignedp = !!(type & CPP_N_UNSIGNED);
446 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000447
448 p = token->val.str.text;
449 end = p + token->val.str.len;
450
451 /* Common case of a single digit. */
452 if (token->val.str.len == 1)
453 result.low = p[0] - '0';
454 else
455 {
456 cpp_num_part max;
457 size_t precision = CPP_OPTION (pfile, precision);
458 unsigned int base = 10, c = 0;
459 bool overflow = false;
460
461 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
462 {
463 base = 8;
464 p++;
465 }
466 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
467 {
468 base = 16;
469 p += 2;
470 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000471 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
472 {
473 base = 2;
474 p += 2;
475 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000476
477 /* We can add a digit to numbers strictly less than this without
478 needing the precision and slowness of double integers. */
479 max = ~(cpp_num_part) 0;
480 if (precision < PART_PRECISION)
481 max >>= PART_PRECISION - precision;
482 max = (max - base + 1) / base + 1;
483
484 for (; p < end; p++)
485 {
486 c = *p;
487
488 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
489 c = hex_value (c);
490 else
491 break;
492
493 /* Strict inequality for when max is set to zero. */
494 if (result.low < max)
495 result.low = result.low * base + c;
496 else
497 {
498 result = append_digit (result, c, base, precision);
499 overflow |= result.overflow;
500 max = 0;
501 }
502 }
503
504 if (overflow)
John David Anglin0527bc42003-11-01 22:56:54 +0000505 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000506 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000507 /* If too big to be signed, consider it unsigned. Only warn for
508 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000509 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000510 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000511 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000512 && !(CPP_OPTION (pfile, traditional)
513 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000514 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000515 {
Neil Boothcd7ab832002-05-29 17:15:42 +0000516 if (base == 10)
John David Anglin0527bc42003-11-01 22:56:54 +0000517 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000518 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000519 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000520 }
521 }
522
523 return result;
524}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000525
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000526/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000527static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000528append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000529{
530 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000531 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000532 bool overflow;
533 cpp_num_part add_high, add_low;
534
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000535 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000536 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000537 switch (base)
538 {
539 case 2:
540 shift = 1;
541 break;
542
543 case 16:
544 shift = 4;
545 break;
546
547 default:
548 shift = 3;
549 }
Neil Booth23ff0222002-07-17 17:27:14 +0000550 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000551 result.high = num.high << shift;
552 result.low = num.low << shift;
553 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400554 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000555
556 if (base == 10)
557 {
558 add_low = num.low << 1;
559 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
560 }
561 else
562 add_high = add_low = 0;
563
564 if (add_low + digit < add_low)
565 add_high++;
566 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000567
Neil Booth91318902002-05-26 18:42:21 +0000568 if (result.low + add_low < result.low)
569 add_high++;
570 if (result.high + add_high < result.high)
571 overflow = true;
572
573 result.low += add_low;
574 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400575 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000576
577 /* The above code catches overflow of a cpp_num type. This catches
578 overflow of the (possibly shorter) target precision. */
579 num.low = result.low;
580 num.high = result.high;
581 result = num_trim (result, precision);
582 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400583 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000584
Neil Booth91318902002-05-26 18:42:21 +0000585 return result;
586}
587
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000588/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000589static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000590parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000591{
Neil Booth91318902002-05-26 18:42:21 +0000592 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000593 int paren = 0;
594 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000595 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000596 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000597
Neil Booth93c803682000-10-28 17:59:06 +0000598 /* Don't expand macros. */
599 pfile->state.prevent_expansion++;
600
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000601 token = cpp_get_token (pfile);
602 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000603 {
604 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000605 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000606 }
607
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000608 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000609 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000610 node = token->val.node;
611 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000612 {
John David Anglin0527bc42003-11-01 22:56:54 +0000613 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000614 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000615 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000616 }
Neil Booth93c803682000-10-28 17:59:06 +0000617 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000618 {
John David Anglin0527bc42003-11-01 22:56:54 +0000619 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000620 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000621 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000622 {
623 cpp_token op;
624
625 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000626 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000627 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000628 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000629 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000630 cpp_token_as_text (pfile, &op));
631 }
632 }
Neil Booth93c803682000-10-28 17:59:06 +0000633
Neil Booth91318902002-05-26 18:42:21 +0000634 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000635 {
Neil Booth335d03e2003-08-03 12:23:46 +0000636 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000637 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000638 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000639
Neil Bootha69cbaa2002-07-23 22:57:49 +0000640 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100641 if (!(node->flags & NODE_USED))
642 {
643 node->flags |= NODE_USED;
644 if (node->type == NT_MACRO)
645 {
646 if (pfile->cb.used_define)
647 pfile->cb.used_define (pfile, pfile->directive_line, node);
648 }
649 else
650 {
651 if (pfile->cb.used_undef)
652 pfile->cb.used_undef (pfile, pfile->directive_line, node);
653 }
654 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000655
Neil Booth6d18adb2001-07-29 17:27:57 +0000656 /* A possible controlling macro of the form #if !defined ().
657 _cpp_parse_expr checks there was no other junk on the line. */
658 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000659 }
660
661 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000662
Neil Booth23ff0222002-07-17 17:27:14 +0000663 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000664 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000665 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000666 result.low = node && node->type == NT_MACRO;
667 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000668}
669
Neil Booth60284a52002-04-28 23:14:56 +0000670/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
671 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000672 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000673static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000674eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800675{
Neil Booth91318902002-05-26 18:42:21 +0000676 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000677 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000678 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000679
Diego Novillo6de9cd92004-05-13 02:41:07 -0400680 result.unsignedp = false;
681 result.overflow = false;
682
Neil Booth93c803682000-10-28 17:59:06 +0000683 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000684 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800685 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000686 temp = cpp_classify_number (pfile, token);
687 switch (temp & CPP_N_CATEGORY)
688 {
689 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000690 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000691 "floating constant in preprocessor expression");
692 break;
693 case CPP_N_INTEGER:
694 if (!(temp & CPP_N_IMAGINARY))
695 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000696 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000697 "imaginary number in preprocessor expression");
698 break;
699
700 case CPP_N_INVALID:
701 /* Error already issued. */
702 break;
703 }
704 result.high = result.low = 0;
705 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000706
Alexandre Oliva525bc952000-02-23 19:21:07 +0000707 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000708 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000709 case CPP_CHAR16:
710 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +0000711 {
Neil Booth91318902002-05-26 18:42:21 +0000712 cppchar_t cc = cpp_interpret_charconst (pfile, token,
713 &temp, &unsignedp);
714
715 result.high = 0;
716 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000717 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000718 if (!unsignedp && (cppchar_signed_t) cc < 0)
719 {
720 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
721 result.low |= ~(~(cpp_num_part) 0
722 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
723 result.high = ~(cpp_num_part) 0;
724 result = num_trim (result, CPP_OPTION (pfile, precision));
725 }
Neil Bootha5a49442002-05-06 22:53:10 +0000726 }
Neil Booth60284a52002-04-28 23:14:56 +0000727 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000728
Zack Weinberg92936ec2000-07-19 20:18:08 +0000729 case CPP_NAME:
Neil Booth93c803682000-10-28 17:59:06 +0000730 if (token->val.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000731 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000732 else if (CPP_OPTION (pfile, cplusplus)
733 && (token->val.node == pfile->spec_nodes.n_true
734 || token->val.node == pfile->spec_nodes.n_false))
735 {
Neil Booth91318902002-05-26 18:42:21 +0000736 result.high = 0;
737 result.low = (token->val.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000738 }
739 else
740 {
Neil Booth91318902002-05-26 18:42:21 +0000741 result.high = 0;
742 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000743 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000744 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
Neil Boothebef4e82002-04-14 18:42:47 +0000745 NODE_NAME (token->val.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000746 }
Neil Booth60284a52002-04-28 23:14:56 +0000747 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000748
Tom Tromey899015a2008-05-13 14:50:27 +0000749 case CPP_HASH:
750 if (!pfile->state.skipping)
751 {
752 /* A pedantic warning takes precedence over a deprecated
753 warning here. */
754 if (CPP_PEDANTIC (pfile))
755 cpp_error (pfile, CPP_DL_PEDWARN,
756 "assertions are a GCC extension");
757 else if (CPP_OPTION (pfile, warn_deprecated))
758 cpp_error (pfile, CPP_DL_WARNING,
759 "assertions are a deprecated extension");
760 }
Neil Booth91318902002-05-26 18:42:21 +0000761 _cpp_test_assertion (pfile, &temp);
762 result.high = 0;
763 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +0000764 break;
765
766 default:
767 abort ();
Neil Booth93c803682000-10-28 17:59:06 +0000768 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800769
Neil Booth23ff0222002-07-17 17:27:14 +0000770 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000771 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800772}
773
Neil Booth4063b942000-04-02 08:27:23 +0000774/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000775
776After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000777than the operator on the top of the stack, we reduce the stack by one
778operator and repeat the test. Since equal priorities do not reduce,
779this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000780
Neil Booth87ed1092002-04-28 19:42:54 +0000781We handle left-associative operators by decrementing the priority of
782just-lexed operators by one, but retaining the priority of operators
783already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000784
785The remaining cases are '(' and ')'. We handle '(' by skipping the
786reduction phase completely. ')' is given lower priority than
787everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000788parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000789reduce() exits immediately. If the normal exit route sees a ')', then
790there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000791
Neil Boothf8b954f2002-04-26 06:32:50 +0000792The parser assumes all shifted operators require a left operand unless
793the flag NO_L_OPERAND is set. These semantics are automatic; any
794extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800795
Neil Booth68e652752002-07-20 13:31:56 +0000796/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
797 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000798#define NO_L_OPERAND (1 << 0)
799#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000800#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000801
Zack Weinbergcf00a882000-07-08 02:33:00 +0000802/* Operator to priority map. Must be in the same order as the first
803 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000804static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000805{
Neil Booth60284a52002-04-28 23:14:56 +0000806 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000807 uchar flags;
808} optab[] =
809{
Neil Boothad28cff2002-07-18 22:08:35 +0000810 /* EQ */ {0, 0}, /* Shouldn't happen. */
811 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000812 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
813 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
814 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
815 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
816 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
817 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
818 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
819 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
820 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
821 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000822 /* RSHIFT */ {13, LEFT_ASSOC},
823 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000824
Neil Boothad28cff2002-07-18 22:08:35 +0000825 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000826 /* AND_AND */ {6, LEFT_ASSOC},
827 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +0000828 /* Note that QUERY, COLON, and COMMA must have the same precedence.
829 However, there are some special cases for these in reduce(). */
830 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000831 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +0000832 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000833 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000834 /* CLOSE_PAREN */ {0, 0},
835 /* EOF */ {0, 0},
836 /* EQ_EQ */ {11, LEFT_ASSOC},
837 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000838 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
839 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000840 /* UPLUS */ {16, NO_L_OPERAND},
841 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000842};
843
Per Bothner7f2935c1995-03-16 13:59:07 -0800844/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000845 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000846
847 The implementation is an operator precedence parser, i.e. a
848 bottom-up parser, using a stack for not-yet-reduced tokens.
849
850 The stack base is op_stack, and the current stack pointer is 'top'.
851 There is a stack element for each operator (only), and the most
852 recently pushed operator is 'top->op'. An operand (value) is
853 stored in the 'value' field of the stack element of the operator
854 that precedes it. */
855bool
Tom Tromeyd7508872008-05-30 14:25:09 +0000856_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -0800857{
Neil Booth87ed1092002-04-28 19:42:54 +0000858 struct op *top = pfile->op_stack;
859 unsigned int lex_count;
860 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800861
Neil Booth87ed1092002-04-28 19:42:54 +0000862 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800863
Neil Booth93c803682000-10-28 17:59:06 +0000864 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000865 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000866 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000867 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000868
Neil Booth87ed1092002-04-28 19:42:54 +0000869 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000870 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000871
Per Bothner7f2935c1995-03-16 13:59:07 -0800872 for (;;)
873 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000874 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800875
Neil Booth6d18adb2001-07-29 17:27:57 +0000876 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000877 op.token = cpp_get_token (pfile);
878 op.op = op.token->type;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +0000879 op.loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -0800880
Per Bothner7f2935c1995-03-16 13:59:07 -0800881 switch (op.op)
882 {
Neil Booth60284a52002-04-28 23:14:56 +0000883 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000884 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000885 case CPP_CHAR:
886 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000887 case CPP_CHAR16:
888 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +0000889 case CPP_NAME:
890 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000891 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000892 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000893 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000894 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000895 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000896 continue;
897
Neil Booth6d18adb2001-07-29 17:27:57 +0000898 case CPP_NOT:
899 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000900 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000901 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000902 if (want_value)
903 op.op = CPP_UPLUS;
904 break;
905 case CPP_MINUS:
906 if (want_value)
907 op.op = CPP_UMINUS;
908 break;
Neil Booth60284a52002-04-28 23:14:56 +0000909
Neil Boothf8b954f2002-04-26 06:32:50 +0000910 default:
Neil Booth60284a52002-04-28 23:14:56 +0000911 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000912 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000913 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000914 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800915 }
916
Neil Booth87ed1092002-04-28 19:42:54 +0000917 /* Check we have a value or operator as appropriate. */
918 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000919 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000920 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000921 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000922 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000923 }
Neil Booth87ed1092002-04-28 19:42:54 +0000924 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000925 {
Neil Bootha09d4742004-07-04 12:57:50 +0000926 /* We want a number (or expression) and haven't got one.
927 Try to emit a specific diagnostic. */
928 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
929 SYNTAX_ERROR ("missing expression between '(' and ')'");
930
931 if (op.op == CPP_EOF && top->op == CPP_EOF)
Tom Tromeyd7508872008-05-30 14:25:09 +0000932 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +0000933
934 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
935 SYNTAX_ERROR2 ("operator '%s' has no right operand",
936 cpp_token_as_text (pfile, top->token));
937 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
938 /* Complain about missing paren during reduction. */;
939 else
940 SYNTAX_ERROR2 ("operator '%s' has no left operand",
941 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000942 }
Neil Booth87ed1092002-04-28 19:42:54 +0000943
944 top = reduce (pfile, top, op.op);
945 if (!top)
946 goto syntax_error;
947
Neil Booth60284a52002-04-28 23:14:56 +0000948 if (op.op == CPP_EOF)
949 break;
950
Neil Booth87ed1092002-04-28 19:42:54 +0000951 switch (op.op)
952 {
953 case CPP_CLOSE_PAREN:
954 continue;
Neil Booth87ed1092002-04-28 19:42:54 +0000955 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +0000956 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000957 pfile->state.skip_eval++;
958 break;
959 case CPP_AND_AND:
960 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +0000961 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000962 pfile->state.skip_eval++;
963 break;
964 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +0000965 if (top->op != CPP_QUERY)
966 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +0000967 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +0000968 pfile->state.skip_eval++;
969 else
970 pfile->state.skip_eval--;
971 default:
972 break;
973 }
974
Neil Boothf8b954f2002-04-26 06:32:50 +0000975 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +0000976
Mike Stump0f413021996-07-03 22:07:53 +0000977 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +0000978 if (++top == pfile->op_limit)
979 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +0000980
Per Bothner7f2935c1995-03-16 13:59:07 -0800981 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +0000982 top->token = op.token;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +0000983 top->loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -0800984 }
Neil Booth9ee703132000-04-01 07:42:37 +0000985
Neil Booth6d18adb2001-07-29 17:27:57 +0000986 /* The controlling macro expression is only valid if we called lex 3
987 times: <!> <defined expression> and <EOF>. push_conditional ()
988 checks that we are at top-of-file. */
989 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
990 pfile->mi_ind_cmacro = 0;
991
Neil Booth87ed1092002-04-28 19:42:54 +0000992 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +0000993 {
Tom Tromeyd7508872008-05-30 14:25:09 +0000994 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
995 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +0000996 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +0000997 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +0000998 }
Neil Booth9ee703132000-04-01 07:42:37 +0000999
Neil Booth91318902002-05-26 18:42:21 +00001000 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001001}
1002
1003/* Reduce the operator / value stack if possible, in preparation for
1004 pushing operator OP. Returns NULL on error, otherwise the top of
1005 the stack. */
1006static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001007reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001008{
1009 unsigned int prio;
1010
Neil Booth91318902002-05-26 18:42:21 +00001011 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1012 {
1013 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001014 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001015 return 0;
1016 }
1017
Neil Booth87ed1092002-04-28 19:42:54 +00001018 if (op == CPP_OPEN_PAREN)
1019 return top;
1020
1021 /* Decrement the priority of left-associative operators to force a
1022 reduction with operators of otherwise equal priority. */
1023 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1024 while (prio < optab[top->op].prio)
1025 {
Neil Booth68e652752002-07-20 13:31:56 +00001026 if (CPP_OPTION (pfile, warn_num_sign_change)
1027 && optab[top->op].flags & CHECK_PROMOTION)
1028 check_promotion (pfile, top);
1029
Neil Booth75aef482002-07-19 19:24:43 +00001030 switch (top->op)
1031 {
1032 case CPP_UPLUS:
1033 case CPP_UMINUS:
1034 case CPP_NOT:
1035 case CPP_COMPL:
1036 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001037 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001038 break;
Neil Booth91318902002-05-26 18:42:21 +00001039
Neil Booth75aef482002-07-19 19:24:43 +00001040 case CPP_PLUS:
1041 case CPP_MINUS:
1042 case CPP_RSHIFT:
1043 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001044 case CPP_COMMA:
1045 top[-1].value = num_binary_op (pfile, top[-1].value,
1046 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001047 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001048 break;
Neil Booth91318902002-05-26 18:42:21 +00001049
Neil Booth75aef482002-07-19 19:24:43 +00001050 case CPP_GREATER:
1051 case CPP_LESS:
1052 case CPP_GREATER_EQ:
1053 case CPP_LESS_EQ:
1054 top[-1].value
1055 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001056 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001057 break;
Neil Booth91318902002-05-26 18:42:21 +00001058
Neil Booth75aef482002-07-19 19:24:43 +00001059 case CPP_EQ_EQ:
1060 case CPP_NOT_EQ:
1061 top[-1].value
1062 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001063 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001064 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001065
Neil Booth75aef482002-07-19 19:24:43 +00001066 case CPP_AND:
1067 case CPP_OR:
1068 case CPP_XOR:
1069 top[-1].value
1070 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001071 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001072 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001073
Neil Booth75aef482002-07-19 19:24:43 +00001074 case CPP_MULT:
1075 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001076 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001077 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001078
Neil Booth75aef482002-07-19 19:24:43 +00001079 case CPP_DIV:
1080 case CPP_MOD:
1081 top[-1].value = num_div_op (pfile, top[-1].value,
1082 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001083 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001084 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001085
Neil Booth75aef482002-07-19 19:24:43 +00001086 case CPP_OR_OR:
1087 top--;
1088 if (!num_zerop (top->value))
1089 pfile->state.skip_eval--;
1090 top->value.low = (!num_zerop (top->value)
1091 || !num_zerop (top[1].value));
1092 top->value.high = 0;
1093 top->value.unsignedp = false;
1094 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001095 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001096 continue;
1097
1098 case CPP_AND_AND:
1099 top--;
1100 if (num_zerop (top->value))
1101 pfile->state.skip_eval--;
1102 top->value.low = (!num_zerop (top->value)
1103 && !num_zerop (top[1].value));
1104 top->value.high = 0;
1105 top->value.unsignedp = false;
1106 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001107 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001108 continue;
1109
1110 case CPP_OPEN_PAREN:
1111 if (op != CPP_CLOSE_PAREN)
1112 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001113 cpp_error_with_line (pfile, CPP_DL_ERROR,
1114 top->token->src_loc,
1115 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001116 return 0;
1117 }
1118 top--;
1119 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001120 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001121 return top;
1122
1123 case CPP_COLON:
1124 top -= 2;
1125 if (!num_zerop (top->value))
1126 {
Neil Booth91318902002-05-26 18:42:21 +00001127 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001128 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001129 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001130 }
1131 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001132 {
1133 top->value = top[2].value;
1134 top->loc = top[2].loc;
1135 }
Neil Booth75aef482002-07-19 19:24:43 +00001136 top->value.unsignedp = (top[1].value.unsignedp
1137 || top[2].value.unsignedp);
1138 continue;
Neil Booth91318902002-05-26 18:42:21 +00001139
Neil Booth75aef482002-07-19 19:24:43 +00001140 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001141 /* COMMA and COLON should not reduce a QUERY operator. */
1142 if (op == CPP_COMMA || op == CPP_COLON)
1143 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001144 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001145 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001146
Neil Booth75aef482002-07-19 19:24:43 +00001147 default:
1148 goto bad_op;
1149 }
Neil Boothad28cff2002-07-18 22:08:35 +00001150
1151 top--;
Neil Booth91318902002-05-26 18:42:21 +00001152 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001153 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001154 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001155 }
1156
1157 if (op == CPP_CLOSE_PAREN)
1158 {
John David Anglin0527bc42003-11-01 22:56:54 +00001159 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001160 return 0;
1161 }
1162
1163 return top;
1164}
1165
1166/* Returns the position of the old top of stack after expansion. */
1167struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001168_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001169{
Neil Booth32fa4562002-05-09 22:27:31 +00001170 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1171 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001172
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001173 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001174 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001175
Neil Booth32fa4562002-05-09 22:27:31 +00001176 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001177}
Neil Booth91318902002-05-26 18:42:21 +00001178
Neil Booth68e652752002-07-20 13:31:56 +00001179/* Emits a warning if the effective sign of either operand of OP
1180 changes because of integer promotions. */
1181static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001182check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001183{
1184 if (op->value.unsignedp == op[-1].value.unsignedp)
1185 return;
1186
1187 if (op->value.unsignedp)
1188 {
1189 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001190 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1191 "the left operand of \"%s\" changes sign when promoted",
1192 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001193 }
1194 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001195 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001196 "the right operand of \"%s\" changes sign when promoted",
1197 cpp_token_as_text (pfile, op->token));
1198}
1199
Neil Booth91318902002-05-26 18:42:21 +00001200/* Clears the unused high order bits of the number pointed to by PNUM. */
1201static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001202num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001203{
1204 if (precision > PART_PRECISION)
1205 {
1206 precision -= PART_PRECISION;
1207 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001208 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001209 }
1210 else
1211 {
1212 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001213 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001214 num.high = 0;
1215 }
1216
1217 return num;
1218}
1219
1220/* True iff A (presumed signed) >= 0. */
1221static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001222num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001223{
1224 if (precision > PART_PRECISION)
1225 {
1226 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001227 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001228 }
1229
Neil Booth359b0be2002-05-28 05:44:06 +00001230 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001231}
1232
Neil Boothceeedfc2002-06-02 19:37:34 +00001233/* Sign extend a number, with PRECISION significant bits and all
1234 others assumed clear, to fill out a cpp_num structure. */
1235cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001236cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001237{
1238 if (!num.unsignedp)
1239 {
1240 if (precision > PART_PRECISION)
1241 {
1242 precision -= PART_PRECISION;
1243 if (precision < PART_PRECISION
1244 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1245 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1246 }
1247 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1248 {
1249 if (precision < PART_PRECISION)
1250 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1251 num.high = ~(cpp_num_part) 0;
1252 }
1253 }
1254
1255 return num;
1256}
1257
Neil Booth91318902002-05-26 18:42:21 +00001258/* Returns the negative of NUM. */
1259static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001260num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001261{
1262 cpp_num copy;
1263
1264 copy = num;
1265 num.high = ~num.high;
1266 num.low = ~num.low;
1267 if (++num.low == 0)
1268 num.high++;
1269 num = num_trim (num, precision);
1270 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1271
1272 return num;
1273}
1274
1275/* Returns true if A >= B. */
1276static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001277num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001278{
1279 bool unsignedp;
1280
1281 unsignedp = pa.unsignedp || pb.unsignedp;
1282
1283 if (!unsignedp)
1284 {
1285 /* Both numbers have signed type. If they are of different
1286 sign, the answer is the sign of A. */
1287 unsignedp = num_positive (pa, precision);
1288
1289 if (unsignedp != num_positive (pb, precision))
1290 return unsignedp;
1291
1292 /* Otherwise we can do an unsigned comparison. */
1293 }
1294
1295 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1296}
1297
1298/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1299static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001300num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1301 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001302{
1303 lhs.overflow = false;
1304 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1305
1306 /* As excess precision is zeroed, there is no need to num_trim () as
1307 these operations cannot introduce a set bit there. */
1308 if (op == CPP_AND)
1309 {
1310 lhs.low &= rhs.low;
1311 lhs.high &= rhs.high;
1312 }
1313 else if (op == CPP_OR)
1314 {
1315 lhs.low |= rhs.low;
1316 lhs.high |= rhs.high;
1317 }
1318 else
1319 {
1320 lhs.low ^= rhs.low;
1321 lhs.high ^= rhs.high;
1322 }
1323
1324 return lhs;
1325}
1326
1327/* Returns LHS OP RHS, where OP is an inequality. */
1328static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001329num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1330 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001331{
1332 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1333
1334 if (op == CPP_GREATER_EQ)
1335 lhs.low = gte;
1336 else if (op == CPP_LESS)
1337 lhs.low = !gte;
1338 else if (op == CPP_GREATER)
1339 lhs.low = gte && !num_eq (lhs, rhs);
1340 else /* CPP_LESS_EQ. */
1341 lhs.low = !gte || num_eq (lhs, rhs);
1342
1343 lhs.high = 0;
1344 lhs.overflow = false;
1345 lhs.unsignedp = false;
1346 return lhs;
1347}
1348
1349/* Returns LHS OP RHS, where OP is == or !=. */
1350static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001351num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1352 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001353{
Jason Merrill97459792002-06-07 09:29:17 -04001354 /* Work around a 3.0.4 bug; see PR 6950. */
1355 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001356 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001357 eq = !eq;
1358 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001359 lhs.high = 0;
1360 lhs.overflow = false;
1361 lhs.unsignedp = false;
1362 return lhs;
1363}
1364
1365/* Shift NUM, of width PRECISION, right by N bits. */
1366static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001367num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001368{
1369 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001370 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001371
Diego Novillo6de9cd92004-05-13 02:41:07 -04001372 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001373 sign_mask = 0;
1374 else
1375 sign_mask = ~(cpp_num_part) 0;
1376
1377 if (n >= precision)
1378 num.high = num.low = sign_mask;
1379 else
1380 {
1381 /* Sign-extend. */
1382 if (precision < PART_PRECISION)
1383 num.high = sign_mask, num.low |= sign_mask << precision;
1384 else if (precision < 2 * PART_PRECISION)
1385 num.high |= sign_mask << (precision - PART_PRECISION);
1386
1387 if (n >= PART_PRECISION)
1388 {
1389 n -= PART_PRECISION;
1390 num.low = num.high;
1391 num.high = sign_mask;
1392 }
1393
1394 if (n)
1395 {
1396 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1397 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1398 }
1399 }
1400
1401 num = num_trim (num, precision);
1402 num.overflow = false;
1403 return num;
1404}
1405
1406/* Shift NUM, of width PRECISION, left by N bits. */
1407static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001408num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001409{
1410 if (n >= precision)
1411 {
1412 num.overflow = !num.unsignedp && !num_zerop (num);
1413 num.high = num.low = 0;
1414 }
1415 else
1416 {
1417 cpp_num orig, maybe_orig;
1418 size_t m = n;
1419
1420 orig = num;
1421 if (m >= PART_PRECISION)
1422 {
1423 m -= PART_PRECISION;
1424 num.high = num.low;
1425 num.low = 0;
1426 }
1427 if (m)
1428 {
1429 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1430 num.low <<= m;
1431 }
1432 num = num_trim (num, precision);
1433
1434 if (num.unsignedp)
1435 num.overflow = false;
1436 else
1437 {
1438 maybe_orig = num_rshift (num, precision, n);
1439 num.overflow = !num_eq (orig, maybe_orig);
1440 }
1441 }
1442
1443 return num;
1444}
1445
1446/* The four unary operators: +, -, ! and ~. */
1447static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001448num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001449{
1450 switch (op)
1451 {
1452 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001453 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001454 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth91318902002-05-26 18:42:21 +00001455 "traditional C rejects the unary plus operator");
1456 num.overflow = false;
1457 break;
1458
1459 case CPP_UMINUS:
1460 num = num_negate (num, CPP_OPTION (pfile, precision));
1461 break;
1462
1463 case CPP_COMPL:
1464 num.high = ~num.high;
1465 num.low = ~num.low;
1466 num = num_trim (num, CPP_OPTION (pfile, precision));
1467 num.overflow = false;
1468 break;
1469
1470 default: /* case CPP_NOT: */
1471 num.low = num_zerop (num);
1472 num.high = 0;
1473 num.overflow = false;
1474 num.unsignedp = false;
1475 break;
1476 }
1477
1478 return num;
1479}
1480
1481/* The various binary operators. */
1482static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001483num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001484{
1485 cpp_num result;
1486 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001487 size_t n;
1488
1489 switch (op)
1490 {
1491 /* Shifts. */
1492 case CPP_LSHIFT:
1493 case CPP_RSHIFT:
1494 if (!rhs.unsignedp && !num_positive (rhs, precision))
1495 {
1496 /* A negative shift is a positive shift the other way. */
1497 if (op == CPP_LSHIFT)
1498 op = CPP_RSHIFT;
1499 else
1500 op = CPP_LSHIFT;
1501 rhs = num_negate (rhs, precision);
1502 }
1503 if (rhs.high)
1504 n = ~0; /* Maximal. */
1505 else
1506 n = rhs.low;
1507 if (op == CPP_LSHIFT)
1508 lhs = num_lshift (lhs, precision, n);
1509 else
1510 lhs = num_rshift (lhs, precision, n);
1511 break;
1512
Neil Booth91318902002-05-26 18:42:21 +00001513 /* Arithmetic. */
1514 case CPP_MINUS:
1515 rhs = num_negate (rhs, precision);
1516 case CPP_PLUS:
1517 result.low = lhs.low + rhs.low;
1518 result.high = lhs.high + rhs.high;
1519 if (result.low < lhs.low)
1520 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001521 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1522 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001523
1524 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001525 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001526 {
1527 bool lhsp = num_positive (lhs, precision);
1528 result.overflow = (lhsp == num_positive (rhs, precision)
1529 && lhsp != num_positive (result, precision));
1530 }
1531 return result;
1532
1533 /* Comma. */
1534 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001535 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1536 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001537 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001538 "comma operator in operand of #if");
1539 lhs = rhs;
1540 break;
1541 }
1542
1543 return lhs;
1544}
1545
1546/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1547 cannot overflow. */
1548static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001549num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001550{
1551 cpp_num result;
1552 cpp_num_part middle[2], temp;
1553
1554 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1555 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1556
1557 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1558 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1559
1560 temp = result.low;
1561 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1562 if (result.low < temp)
1563 result.high++;
1564
1565 temp = result.low;
1566 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1567 if (result.low < temp)
1568 result.high++;
1569
1570 result.high += HIGH_PART (middle[0]);
1571 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001572 result.unsignedp = true;
1573 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001574
1575 return result;
1576}
1577
1578/* Multiply two preprocessing numbers. */
1579static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001580num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001581{
1582 cpp_num result, temp;
1583 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1584 bool overflow, negate = false;
1585 size_t precision = CPP_OPTION (pfile, precision);
1586
1587 /* Prepare for unsigned multiplication. */
1588 if (!unsignedp)
1589 {
1590 if (!num_positive (lhs, precision))
1591 negate = !negate, lhs = num_negate (lhs, precision);
1592 if (!num_positive (rhs, precision))
1593 negate = !negate, rhs = num_negate (rhs, precision);
1594 }
1595
1596 overflow = lhs.high && rhs.high;
1597 result = num_part_mul (lhs.low, rhs.low);
1598
1599 temp = num_part_mul (lhs.high, rhs.low);
1600 result.high += temp.low;
1601 if (temp.high)
1602 overflow = true;
1603
1604 temp = num_part_mul (lhs.low, rhs.high);
1605 result.high += temp.low;
1606 if (temp.high)
1607 overflow = true;
1608
1609 temp.low = result.low, temp.high = result.high;
1610 result = num_trim (result, precision);
1611 if (!num_eq (result, temp))
1612 overflow = true;
1613
1614 if (negate)
1615 result = num_negate (result, precision);
1616
1617 if (unsignedp)
1618 result.overflow = false;
1619 else
1620 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1621 && !num_zerop (result));
1622 result.unsignedp = unsignedp;
1623
1624 return result;
1625}
1626
1627/* Divide two preprocessing numbers, returning the answer or the
1628 remainder depending upon OP. */
1629static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001630num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001631{
1632 cpp_num result, sub;
1633 cpp_num_part mask;
1634 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1635 bool negate = false, lhs_neg = false;
1636 size_t i, precision = CPP_OPTION (pfile, precision);
1637
1638 /* Prepare for unsigned division. */
1639 if (!unsignedp)
1640 {
1641 if (!num_positive (lhs, precision))
1642 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1643 if (!num_positive (rhs, precision))
1644 negate = !negate, rhs = num_negate (rhs, precision);
1645 }
1646
1647 /* Find the high bit. */
1648 if (rhs.high)
1649 {
1650 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001651 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001652 for (; ; i--, mask >>= 1)
1653 if (rhs.high & mask)
1654 break;
1655 }
1656 else if (rhs.low)
1657 {
1658 if (precision > PART_PRECISION)
1659 i = precision - PART_PRECISION - 1;
1660 else
1661 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001662 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001663 for (; ; i--, mask >>= 1)
1664 if (rhs.low & mask)
1665 break;
1666 }
1667 else
1668 {
Neil Booth75aef482002-07-19 19:24:43 +00001669 if (!pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001670 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001671 return lhs;
1672 }
1673
Kazu Hiratada7d8302002-09-22 02:03:17 +00001674 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001675 shifting the RHS fully left, and subtracting from LHS if LHS is
1676 at least as big, and then repeating but with one less shift.
1677 This is not very efficient, but is easy to understand. */
1678
1679 rhs.unsignedp = true;
1680 lhs.unsignedp = true;
1681 i = precision - i - 1;
1682 sub = num_lshift (rhs, precision, i);
1683
1684 result.high = result.low = 0;
1685 for (;;)
1686 {
1687 if (num_greater_eq (lhs, sub, precision))
1688 {
1689 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1690 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001691 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001692 else
Neil Booth359b0be2002-05-28 05:44:06 +00001693 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001694 }
1695 if (i-- == 0)
1696 break;
1697 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1698 sub.high >>= 1;
1699 }
1700
1701 /* We divide so that the remainder has the sign of the LHS. */
1702 if (op == CPP_DIV)
1703 {
1704 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001705 result.overflow = false;
1706 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001707 {
1708 if (negate)
1709 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001710 result.overflow = (num_positive (result, precision) ^ !negate
1711 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001712 }
1713
1714 return result;
1715 }
1716
1717 /* CPP_MOD. */
1718 lhs.unsignedp = unsignedp;
1719 lhs.overflow = false;
1720 if (lhs_neg)
1721 lhs = num_negate (lhs, precision);
1722
1723 return lhs;
1724}