]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/expr.c
2011-10-26 Paolo Carlini <paolo.carlini@oracle.com>
[thirdparty/gcc.git] / libcpp / expr.c
CommitLineData
b0699dad 1/* Parse C expressions for cpplib.
5d8ebbd8 2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
dadab4fd 3 2002, 2004, 2008, 2009, 2010 Free Software Foundation.
e38992e8 4 Contributed by Per Bothner, 1994.
7f2935c7
PB
5
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
748086b7 8Free Software Foundation; either version 3, or (at your option) any
7f2935c7
PB
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
748086b7
JJ
17along with this program; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
7f2935c7 19
7f2935c7 20#include "config.h"
b04cd507 21#include "system.h"
487a6e06 22#include "cpplib.h"
4f4e53dd 23#include "internal.h"
7f2935c7 24
91318908
NB
25#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27#define LOW_PART(num_part) (num_part & HALF_MASK)
28#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
29
cf00a885 30struct op
b0699dad 31{
68e65275 32 const cpp_token *token; /* The token forming op (for diagnostics). */
ad28cff7 33 cpp_num value; /* The value logically "right" of op. */
47960aaf 34 source_location loc; /* The location of this value. */
cf00a885 35 enum cpp_ttype op;
7f2935c7 36};
7f2935c7 37
91318908
NB
38/* Some simple utility routines on double integers. */
39#define num_zerop(num) ((num.low | num.high) == 0)
40#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
6cf87ca4
ZW
41static bool num_positive (cpp_num, size_t);
42static bool num_greater_eq (cpp_num, cpp_num, size_t);
43static cpp_num num_trim (cpp_num, size_t);
44static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
45
46static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48static cpp_num num_negate (cpp_num, size_t);
49static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
b506a5a2
MLI
55static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
56 source_location);
6cf87ca4
ZW
57static cpp_num num_lshift (cpp_num, size_t, size_t);
58static cpp_num num_rshift (cpp_num, size_t, size_t);
59
60static 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 *);
91318908 67
cd7ab83f 68/* Token type abuse to create unary plus and minus operators. */
c3f829c1
GDR
69#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
cf00a885 71
15dad1d9
ZW
72/* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
15dad1d9 74#define SYNTAX_ERROR(msgid) \
0527bc4e 75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
15dad1d9 76#define SYNTAX_ERROR2(msgid, arg) \
0527bc4e
JDA
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
15dad1d9 79
cd7ab83f
NB
80/* 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
6cf87ca4 84interpret_float_suffix (const uchar *s, size_t len)
cf00a885 85{
eec49116 86 size_t flags;
839a3b8a 87 size_t f, d, l, w, q, i;
c77cd3d1 88
eec49116 89 flags = 0;
839a3b8a 90 f = d = l = w = q = i = 0;
cf00a885 91
eec49116
JJ
92 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */
94 if (len == 2 && (*s == 'd' || *s == 'D'))
95 {
96 bool uppercase = (*s == 'D');
97 switch (s[1])
cd7ab83f 98 {
eec49116
JJ
99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
cd7ab83f 105 default:
839a3b8a
JJ
106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
108 break;
cd7ab83f 109 }
eec49116 110 }
cf00a885 111
eec49116
JJ
112 /* Recognize a fixed-point suffix. */
113 switch (s[len-1])
ac6b1c67 114 {
eec49116
JJ
115 case 'k': case 'K': flags = CPP_N_ACCUM; break;
116 case 'r': case 'R': flags = CPP_N_FRACT; break;
117 default: break;
ac6b1c67
CF
118 }
119
eec49116
JJ
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
122 if (flags)
ac6b1c67 123 {
eec49116
JJ
124 if (len == 1)
125 return flags;
126 len--;
127
128 if (*s == 'u' || *s == 'U')
129 {
130 flags |= CPP_N_UNSIGNED;
131 if (len == 1)
132 return flags;
133 len--;
134 s++;
135 }
136
137 switch (*s)
138 {
139 case 'h': case 'H':
140 if (len == 1)
141 return flags |= CPP_N_SMALL;
142 break;
143 case 'l':
144 if (len == 1)
145 return flags |= CPP_N_MEDIUM;
146 if (len == 2 && s[1] == 'l')
147 return flags |= CPP_N_LARGE;
148 break;
149 case 'L':
150 if (len == 1)
151 return flags |= CPP_N_MEDIUM;
152 if (len == 2 && s[1] == 'L')
153 return flags |= CPP_N_LARGE;
154 break;
155 default:
156 break;
157 }
158 /* Anything left at this point is invalid. */
159 return 0;
ac6b1c67
CF
160 }
161
eec49116
JJ
162 /* In any remaining valid suffix, the case and order don't matter. */
163 while (len--)
164 switch (s[len])
165 {
166 case 'f': case 'F': f++; break;
839a3b8a 167 case 'd': case 'D': d++; break;
eec49116
JJ
168 case 'l': case 'L': l++; break;
169 case 'w': case 'W': w++; break;
170 case 'q': case 'Q': q++; break;
171 case 'i': case 'I':
172 case 'j': case 'J': i++; break;
173 default:
174 return 0;
175 }
7f2935c7 176
839a3b8a 177 if (f + d + l + w + q > 1 || i > 1)
ad6ed77e
JG
178 return 0;
179
cd7ab83f
NB
180 return ((i ? CPP_N_IMAGINARY : 0)
181 | (f ? CPP_N_SMALL :
839a3b8a 182 d ? CPP_N_MEDIUM :
c77cd3d1
UB
183 l ? CPP_N_LARGE :
184 w ? CPP_N_MD_W :
839a3b8a 185 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
cd7ab83f
NB
186}
187
188/* Subroutine of cpp_classify_number. S points to an integer suffix
189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
190 flag vector describing the suffix. */
191static unsigned int
6cf87ca4 192interpret_int_suffix (const uchar *s, size_t len)
cd7ab83f
NB
193{
194 size_t u, l, i;
195
196 u = l = i = 0;
197
198 while (len--)
199 switch (s[len])
200 {
201 case 'u': case 'U': u++; break;
202 case 'i': case 'I':
203 case 'j': case 'J': i++; break;
204 case 'l': case 'L': l++;
205 /* If there are two Ls, they must be adjacent and the same case. */
206 if (l == 2 && s[len] != s[len + 1])
207 return 0;
208 break;
209 default:
210 return 0;
211 }
212
213 if (l > 2 || u > 1 || i > 1)
214 return 0;
215
216 return ((i ? CPP_N_IMAGINARY : 0)
217 | (u ? CPP_N_UNSIGNED : 0)
218 | ((l == 0) ? CPP_N_SMALL
219 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
220}
221
222/* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */
225unsigned int
6cf87ca4 226cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
cd7ab83f
NB
227{
228 const uchar *str = token->val.str.text;
229 const uchar *limit;
230 unsigned int max_digit, result, radix;
231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
dadab4fd 232 bool seen_digit;
cd7ab83f
NB
233
234 /* If the lexer has done its job, length one can only be a single
235 digit. Fast-path this very common case. */
236 if (token->val.str.len == 1)
237 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
238
239 limit = str + token->val.str.len;
240 float_flag = NOT_FLOAT;
241 max_digit = 0;
242 radix = 10;
dadab4fd 243 seen_digit = false;
cd7ab83f
NB
244
245 /* First, interpret the radix. */
246 if (*str == '0')
247 {
248 radix = 8;
249 str++;
250
251 /* Require at least one hex digit to classify it as hex. */
7f1fc38e
MM
252 if ((*str == 'x' || *str == 'X')
253 && (str[1] == '.' || ISXDIGIT (str[1])))
cd7ab83f
NB
254 {
255 radix = 16;
256 str++;
257 }
f7fd775f
JW
258 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
259 {
260 radix = 2;
261 str++;
262 }
cd7ab83f
NB
263 }
264
265 /* Now scan for a well-formed integer or float. */
266 for (;;)
267 {
268 unsigned int c = *str++;
269
270 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
271 {
dadab4fd 272 seen_digit = true;
cd7ab83f
NB
273 c = hex_value (c);
274 if (c > max_digit)
275 max_digit = c;
276 }
277 else if (c == '.')
278 {
279 if (float_flag == NOT_FLOAT)
280 float_flag = AFTER_POINT;
281 else
282 SYNTAX_ERROR ("too many decimal points in number");
283 }
284 else if ((radix <= 10 && (c == 'e' || c == 'E'))
285 || (radix == 16 && (c == 'p' || c == 'P')))
286 {
287 float_flag = AFTER_EXPON;
288 break;
289 }
290 else
291 {
292 /* Start of suffix. */
293 str--;
294 break;
295 }
296 }
297
ac6b1c67
CF
298 /* The suffix may be for decimal fixed-point constants without exponent. */
299 if (radix != 16 && float_flag == NOT_FLOAT)
300 {
301 result = interpret_float_suffix (str, limit - str);
302 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
303 {
304 result |= CPP_N_FLOATING;
305 /* We need to restore the radix to 10, if the radix is 8. */
306 if (radix == 8)
307 radix = 10;
308
309 if (CPP_PEDANTIC (pfile))
310 cpp_error (pfile, CPP_DL_PEDWARN,
311 "fixed-point constants are a GCC extension");
312 goto syntax_ok;
313 }
314 else
315 result = 0;
316 }
317
cd7ab83f
NB
318 if (float_flag != NOT_FLOAT && radix == 8)
319 radix = 10;
320
321 if (max_digit >= radix)
f7fd775f
JW
322 {
323 if (radix == 2)
324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
325 else
326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
327 }
cd7ab83f
NB
328
329 if (float_flag != NOT_FLOAT)
330 {
f7fd775f
JW
331 if (radix == 2)
332 {
333 cpp_error (pfile, CPP_DL_ERROR,
334 "invalid prefix \"0b\" for floating constant");
335 return CPP_N_INVALID;
336 }
337
dadab4fd
JM
338 if (radix == 16 && !seen_digit)
339 SYNTAX_ERROR ("no digits in hexadecimal floating constant");
340
cd7ab83f 341 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
0527bc4e 342 cpp_error (pfile, CPP_DL_PEDWARN,
cd7ab83f
NB
343 "use of C99 hexadecimal floating constant");
344
345 if (float_flag == AFTER_EXPON)
346 {
347 if (*str == '+' || *str == '-')
348 str++;
349
350 /* Exponent is decimal, even if string is a hex float. */
351 if (!ISDIGIT (*str))
352 SYNTAX_ERROR ("exponent has no digits");
353
354 do
355 str++;
356 while (ISDIGIT (*str));
357 }
358 else if (radix == 16)
359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
360
361 result = interpret_float_suffix (str, limit - str);
362 if (result == 0)
363 {
0527bc4e 364 cpp_error (pfile, CPP_DL_ERROR,
cd7ab83f 365 "invalid suffix \"%.*s\" on floating constant",
91b12472 366 (int) (limit - str), str);
cd7ab83f
NB
367 return CPP_N_INVALID;
368 }
369
370 /* Traditional C didn't accept any floating suffixes. */
371 if (limit != str
372 && CPP_WTRADITIONAL (pfile)
373 && ! cpp_sys_macro_p (pfile))
87cf0651
SB
374 cpp_warning (pfile, CPP_W_TRADITIONAL,
375 "traditional C rejects the \"%.*s\" suffix",
376 (int) (limit - str), str);
cd7ab83f 377
839a3b8a
JJ
378 /* A suffix for double is a GCC extension via decimal float support.
379 If the suffix also specifies an imaginary value we'll catch that
380 later. */
381 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
382 cpp_error (pfile, CPP_DL_PEDWARN,
383 "suffix for double constant is a GCC extension");
384
ad6ed77e
JG
385 /* Radix must be 10 for decimal floats. */
386 if ((result & CPP_N_DFLOAT) && radix != 10)
387 {
388 cpp_error (pfile, CPP_DL_ERROR,
389 "invalid suffix \"%.*s\" with hexadecimal floating constant",
390 (int) (limit - str), str);
391 return CPP_N_INVALID;
392 }
393
ac6b1c67
CF
394 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
395 cpp_error (pfile, CPP_DL_PEDWARN,
396 "fixed-point constants are a GCC extension");
397
5a6bb57e
JJ
398 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
399 cpp_error (pfile, CPP_DL_PEDWARN,
400 "decimal float constants are a GCC extension");
401
cd7ab83f
NB
402 result |= CPP_N_FLOATING;
403 }
404 else
405 {
406 result = interpret_int_suffix (str, limit - str);
407 if (result == 0)
408 {
0527bc4e 409 cpp_error (pfile, CPP_DL_ERROR,
cd7ab83f 410 "invalid suffix \"%.*s\" on integer constant",
91b12472 411 (int) (limit - str), str);
cd7ab83f
NB
412 return CPP_N_INVALID;
413 }
414
56da7207
ZW
415 /* Traditional C only accepted the 'L' suffix.
416 Suppress warning about 'LL' with -Wno-long-long. */
417 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
418 {
419 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
87cf0651 420 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
e3339d0f 421 && CPP_OPTION (pfile, cpp_warn_long_long);
56da7207 422
87cf0651
SB
423 if (u_or_i || large)
424 cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
425 "traditional C rejects the \"%.*s\" suffix",
426 (int) (limit - str), str);
56da7207 427 }
cd7ab83f
NB
428
429 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
e3339d0f 430 && CPP_OPTION (pfile, cpp_warn_long_long))
87cf0651
SB
431 {
432 const char *message = CPP_OPTION (pfile, cplusplus)
433 ? N_("use of C++0x long long integer constant")
434 : N_("use of C99 long long integer constant");
435
436 if (CPP_OPTION (pfile, c99))
437 cpp_warning (pfile, CPP_W_LONG_LONG, message);
438 else
439 cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
440 }
cd7ab83f
NB
441
442 result |= CPP_N_INTEGER;
443 }
444
ac6b1c67 445 syntax_ok:
cd7ab83f 446 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
0527bc4e
JDA
447 cpp_error (pfile, CPP_DL_PEDWARN,
448 "imaginary constants are a GCC extension");
f7fd775f
JW
449 if (radix == 2 && CPP_PEDANTIC (pfile))
450 cpp_error (pfile, CPP_DL_PEDWARN,
451 "binary constants are a GCC extension");
cd7ab83f
NB
452
453 if (radix == 10)
454 result |= CPP_N_DECIMAL;
455 else if (radix == 16)
456 result |= CPP_N_HEX;
f7fd775f
JW
457 else if (radix == 2)
458 result |= CPP_N_BINARY;
cd7ab83f
NB
459 else
460 result |= CPP_N_OCTAL;
461
462 return result;
463
464 syntax_error:
465 return CPP_N_INVALID;
466}
467
468/* cpp_interpret_integer converts an integer constant into a cpp_num,
469 of precision options->precision.
470
471 We do not provide any interface for decimal->float conversion,
6cf87ca4
ZW
472 because the preprocessor doesn't need it and we don't want to
473 drag in GCC's floating point emulator. */
cd7ab83f 474cpp_num
6cf87ca4
ZW
475cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
476 unsigned int type)
cd7ab83f
NB
477{
478 const uchar *p, *end;
479 cpp_num result;
480
481 result.low = 0;
482 result.high = 0;
23ff0223
NB
483 result.unsignedp = !!(type & CPP_N_UNSIGNED);
484 result.overflow = false;
cd7ab83f
NB
485
486 p = token->val.str.text;
487 end = p + token->val.str.len;
488
489 /* Common case of a single digit. */
490 if (token->val.str.len == 1)
491 result.low = p[0] - '0';
492 else
493 {
494 cpp_num_part max;
495 size_t precision = CPP_OPTION (pfile, precision);
496 unsigned int base = 10, c = 0;
497 bool overflow = false;
498
499 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
500 {
501 base = 8;
502 p++;
503 }
504 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
505 {
506 base = 16;
507 p += 2;
508 }
f7fd775f
JW
509 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
510 {
511 base = 2;
512 p += 2;
513 }
cd7ab83f
NB
514
515 /* We can add a digit to numbers strictly less than this without
516 needing the precision and slowness of double integers. */
517 max = ~(cpp_num_part) 0;
518 if (precision < PART_PRECISION)
519 max >>= PART_PRECISION - precision;
520 max = (max - base + 1) / base + 1;
521
522 for (; p < end; p++)
523 {
524 c = *p;
525
526 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
527 c = hex_value (c);
528 else
529 break;
530
531 /* Strict inequality for when max is set to zero. */
532 if (result.low < max)
533 result.low = result.low * base + c;
534 else
535 {
536 result = append_digit (result, c, base, precision);
537 overflow |= result.overflow;
538 max = 0;
539 }
540 }
541
542 if (overflow)
0527bc4e 543 cpp_error (pfile, CPP_DL_PEDWARN,
cd7ab83f 544 "integer constant is too large for its type");
017acb41
NB
545 /* If too big to be signed, consider it unsigned. Only warn for
546 decimal numbers. Traditional numbers were always signed (but
8d9afc4e 547 we still honor an explicit U suffix); but we only have
cd98faa1 548 traditional semantics in directives. */
017acb41 549 else if (!result.unsignedp
cd98faa1
NB
550 && !(CPP_OPTION (pfile, traditional)
551 && pfile->state.in_directive)
017acb41 552 && !num_positive (result, precision))
cd7ab83f 553 {
f88d0772 554 /* This is for constants within the range of uintmax_t but
813b9e7e 555 not that of intmax_t. For such decimal constants, a
f88d0772
JM
556 diagnostic is required for C99 as the selected type must
557 be signed and not having a type is a constraint violation
558 (DR#298, TC3), so this must be a pedwarn. For C90,
559 unsigned long is specified to be used for a constant that
560 does not fit in signed long; if uintmax_t has the same
561 range as unsigned long this means only a warning is
562 appropriate here. C90 permits the preprocessor to use a
563 wider range than unsigned long in the compiler, so if
564 uintmax_t is wider than unsigned long no diagnostic is
565 required for such constants in preprocessor #if
566 expressions and the compiler will pedwarn for such
567 constants outside the range of unsigned long that reach
568 the compiler so a diagnostic is not required there
569 either; thus, pedwarn for C99 but use a plain warning for
570 C90. */
cd7ab83f 571 if (base == 10)
f88d0772
JM
572 cpp_error (pfile, (CPP_OPTION (pfile, c99)
573 ? CPP_DL_PEDWARN
574 : CPP_DL_WARNING),
cd7ab83f 575 "integer constant is so large that it is unsigned");
23ff0223 576 result.unsignedp = true;
cd7ab83f
NB
577 }
578 }
579
580 return result;
581}
cf00a885 582
6cf87ca4 583/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
91318908 584static cpp_num
6cf87ca4 585append_digit (cpp_num num, int digit, int base, size_t precision)
91318908
NB
586{
587 cpp_num result;
f7fd775f 588 unsigned int shift;
91318908
NB
589 bool overflow;
590 cpp_num_part add_high, add_low;
591
f7fd775f 592 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
91318908 593 need to worry about add_high overflowing. */
f7fd775f
JW
594 switch (base)
595 {
596 case 2:
597 shift = 1;
598 break;
599
600 case 16:
601 shift = 4;
602 break;
603
604 default:
605 shift = 3;
606 }
23ff0223 607 overflow = !!(num.high >> (PART_PRECISION - shift));
91318908
NB
608 result.high = num.high << shift;
609 result.low = num.low << shift;
610 result.high |= num.low >> (PART_PRECISION - shift);
6de9cd9a 611 result.unsignedp = num.unsignedp;
91318908
NB
612
613 if (base == 10)
614 {
615 add_low = num.low << 1;
616 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
617 }
618 else
619 add_high = add_low = 0;
620
621 if (add_low + digit < add_low)
622 add_high++;
623 add_low += digit;
22a8a52d 624
91318908
NB
625 if (result.low + add_low < result.low)
626 add_high++;
627 if (result.high + add_high < result.high)
628 overflow = true;
629
630 result.low += add_low;
631 result.high += add_high;
6de9cd9a 632 result.overflow = overflow;
91318908
NB
633
634 /* The above code catches overflow of a cpp_num type. This catches
635 overflow of the (possibly shorter) target precision. */
636 num.low = result.low;
637 num.high = result.high;
638 result = num_trim (result, precision);
639 if (!num_eq (result, num))
6de9cd9a 640 result.overflow = true;
91318908 641
91318908
NB
642 return result;
643}
644
5d8ebbd8 645/* Handle meeting "defined" in a preprocessor expression. */
91318908 646static cpp_num
6cf87ca4 647parse_defined (cpp_reader *pfile)
ba412f14 648{
91318908 649 cpp_num result;
93c80368
NB
650 int paren = 0;
651 cpp_hashnode *node = 0;
4ed5bcfb 652 const cpp_token *token;
63d75005 653 cpp_context *initial_context = pfile->context;
ba412f14 654
93c80368
NB
655 /* Don't expand macros. */
656 pfile->state.prevent_expansion++;
657
4ed5bcfb
NB
658 token = cpp_get_token (pfile);
659 if (token->type == CPP_OPEN_PAREN)
ba412f14 660 {
cf00a885 661 paren = 1;
4ed5bcfb 662 token = cpp_get_token (pfile);
ba412f14
ZW
663 }
664
4ed5bcfb 665 if (token->type == CPP_NAME)
93c80368 666 {
9a0c6187 667 node = token->val.node.node;
4ed5bcfb 668 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
93c80368 669 {
0527bc4e 670 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
4ed5bcfb 671 node = 0;
93c80368
NB
672 }
673 }
674 else
3c8465d0 675 {
0527bc4e 676 cpp_error (pfile, CPP_DL_ERROR,
ebef4e8c 677 "operator \"defined\" requires an identifier");
4ed5bcfb 678 if (token->flags & NAMED_OP)
3c8465d0
NB
679 {
680 cpp_token op;
681
682 op.flags = 0;
4ed5bcfb 683 op.type = token->type;
0527bc4e 684 cpp_error (pfile, CPP_DL_ERROR,
3c8465d0 685 "(\"%s\" is an alternative token for \"%s\" in C++)",
4ed5bcfb 686 cpp_token_as_text (pfile, token),
3c8465d0
NB
687 cpp_token_as_text (pfile, &op));
688 }
689 }
15dad1d9 690
91318908 691 if (node)
15dad1d9 692 {
335d03ec 693 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
0527bc4e 694 cpp_error (pfile, CPP_DL_WARNING,
ebef4e8c 695 "this use of \"defined\" may not be portable");
63d75005 696
a69cbaac 697 _cpp_mark_macro_used (node);
93d45d9e
JM
698 if (!(node->flags & NODE_USED))
699 {
700 node->flags |= NODE_USED;
701 if (node->type == NT_MACRO)
702 {
a69d2520
JJ
703 if ((node->flags & NODE_BUILTIN)
704 && pfile->cb.user_builtin_macro)
705 pfile->cb.user_builtin_macro (pfile, node);
93d45d9e
JM
706 if (pfile->cb.used_define)
707 pfile->cb.used_define (pfile, pfile->directive_line, node);
708 }
709 else
710 {
711 if (pfile->cb.used_undef)
712 pfile->cb.used_undef (pfile, pfile->directive_line, node);
713 }
714 }
a69cbaac 715
6d18adbc
NB
716 /* A possible controlling macro of the form #if !defined ().
717 _cpp_parse_expr checks there was no other junk on the line. */
718 pfile->mi_ind_cmacro = node;
15dad1d9 719 }
93c80368
NB
720
721 pfile->state.prevent_expansion--;
91318908 722
f3c33d9d
MM
723 /* Do not treat conditional macros as being defined. This is due to the
724 powerpc and spu ports using conditional macros for 'vector', 'bool', and
725 'pixel' to act as conditional keywords. This messes up tests like #ifndef
726 bool. */
23ff0223 727 result.unsignedp = false;
91318908 728 result.high = 0;
23ff0223 729 result.overflow = false;
f3c33d9d
MM
730 result.low = (node && node->type == NT_MACRO
731 && (node->flags & NODE_CONDITIONAL) == 0);
91318908 732 return result;
15dad1d9
ZW
733}
734
60284a59
NB
735/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
736 number or character constant, or the result of the "defined" or "#"
cd7ab83f 737 operators). */
91318908 738static cpp_num
6cf87ca4 739eval_token (cpp_reader *pfile, const cpp_token *token)
7f2935c7 740{
91318908 741 cpp_num result;
60284a59 742 unsigned int temp;
4268e8bb 743 int unsignedp = 0;
041c3194 744
6de9cd9a
DN
745 result.unsignedp = false;
746 result.overflow = false;
747
93c80368 748 switch (token->type)
ba412f14 749 {
7f2935c7 750 case CPP_NUMBER:
cd7ab83f
NB
751 temp = cpp_classify_number (pfile, token);
752 switch (temp & CPP_N_CATEGORY)
753 {
754 case CPP_N_FLOATING:
0527bc4e 755 cpp_error (pfile, CPP_DL_ERROR,
cd7ab83f
NB
756 "floating constant in preprocessor expression");
757 break;
758 case CPP_N_INTEGER:
759 if (!(temp & CPP_N_IMAGINARY))
760 return cpp_interpret_integer (pfile, token, temp);
0527bc4e 761 cpp_error (pfile, CPP_DL_ERROR,
cd7ab83f
NB
762 "imaginary number in preprocessor expression");
763 break;
764
765 case CPP_N_INVALID:
766 /* Error already issued. */
767 break;
768 }
769 result.high = result.low = 0;
770 break;
7f2f1a66 771
cf00a885 772 case CPP_WCHAR:
4268e8bb 773 case CPP_CHAR:
b6baa67d
KVH
774 case CPP_CHAR16:
775 case CPP_CHAR32:
a5a49440 776 {
91318908
NB
777 cppchar_t cc = cpp_interpret_charconst (pfile, token,
778 &temp, &unsignedp);
779
780 result.high = 0;
781 result.low = cc;
a5a49440 782 /* Sign-extend the result if necessary. */
91318908
NB
783 if (!unsignedp && (cppchar_signed_t) cc < 0)
784 {
785 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
786 result.low |= ~(~(cpp_num_part) 0
787 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
788 result.high = ~(cpp_num_part) 0;
789 result = num_trim (result, CPP_OPTION (pfile, precision));
790 }
a5a49440 791 }
60284a59 792 break;
ba412f14 793
92936ecf 794 case CPP_NAME:
9a0c6187 795 if (token->val.node.node == pfile->spec_nodes.n_defined)
63d75005 796 return parse_defined (pfile);
7d4918a2 797 else if (CPP_OPTION (pfile, cplusplus)
9a0c6187
JM
798 && (token->val.node.node == pfile->spec_nodes.n_true
799 || token->val.node.node == pfile->spec_nodes.n_false))
7d4918a2 800 {
91318908 801 result.high = 0;
9a0c6187 802 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
7d4918a2
ZW
803 }
804 else
805 {
91318908
NB
806 result.high = 0;
807 result.low = 0;
87ed109f 808 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
87cf0651
SB
809 cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
810 NODE_NAME (token->val.node.node));
7f2935c7 811 }
60284a59 812 break;
7f2935c7 813
899015a0
TT
814 case CPP_HASH:
815 if (!pfile->state.skipping)
816 {
817 /* A pedantic warning takes precedence over a deprecated
818 warning here. */
819 if (CPP_PEDANTIC (pfile))
820 cpp_error (pfile, CPP_DL_PEDWARN,
821 "assertions are a GCC extension");
e3339d0f 822 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
87cf0651
SB
823 cpp_warning (pfile, CPP_W_DEPRECATED,
824 "assertions are a deprecated extension");
899015a0 825 }
91318908
NB
826 _cpp_test_assertion (pfile, &temp);
827 result.high = 0;
828 result.low = temp;
899015a0
TT
829 break;
830
831 default:
832 abort ();
93c80368 833 }
7c3bb1de 834
23ff0223 835 result.unsignedp = !!unsignedp;
91318908 836 return result;
7f2935c7
PB
837}
838\f
4063b943 839/* Operator precedence and flags table.
dbac4aff
NB
840
841After an operator is returned from the lexer, if it has priority less
87ed109f
NB
842than the operator on the top of the stack, we reduce the stack by one
843operator and repeat the test. Since equal priorities do not reduce,
844this is naturally right-associative.
845
846We handle left-associative operators by decrementing the priority of
847just-lexed operators by one, but retaining the priority of operators
848already on the stack.
dbac4aff
NB
849
850The remaining cases are '(' and ')'. We handle '(' by skipping the
851reduction phase completely. ')' is given lower priority than
852everything else, including '(', effectively forcing a reduction of the
272d0bee 853parenthesized expression. If there is a matching '(', the routine
87ed109f
NB
854reduce() exits immediately. If the normal exit route sees a ')', then
855there cannot have been a matching '(' and an error message is output.
4063b943 856
f8b954fc
NB
857The parser assumes all shifted operators require a left operand unless
858the flag NO_L_OPERAND is set. These semantics are automatic; any
859extra semantics need to be handled with operator-specific code. */
4063b943 860
68e65275
NB
861/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
862 operand changes because of integer promotions. */
87ed109f
NB
863#define NO_L_OPERAND (1 << 0)
864#define LEFT_ASSOC (1 << 1)
68e65275 865#define CHECK_PROMOTION (1 << 2)
eba30526 866
cf00a885
ZW
867/* Operator to priority map. Must be in the same order as the first
868 N entries of enum cpp_ttype. */
c3f829c1 869static const struct cpp_operator
87ed109f 870{
60284a59 871 uchar prio;
87ed109f
NB
872 uchar flags;
873} optab[] =
cf00a885 874{
ad28cff7
NB
875 /* EQ */ {0, 0}, /* Shouldn't happen. */
876 /* NOT */ {16, NO_L_OPERAND},
68e65275
NB
877 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
878 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
879 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
880 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
881 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
882 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
883 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
884 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
885 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
886 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
ad28cff7
NB
887 /* RSHIFT */ {13, LEFT_ASSOC},
888 /* LSHIFT */ {13, LEFT_ASSOC},
889
ad28cff7 890 /* COMPL */ {16, NO_L_OPERAND},
75aef48a
NB
891 /* AND_AND */ {6, LEFT_ASSOC},
892 /* OR_OR */ {5, LEFT_ASSOC},
71c10038
TT
893 /* Note that QUERY, COLON, and COMMA must have the same precedence.
894 However, there are some special cases for these in reduce(). */
895 /* QUERY */ {4, 0},
68e65275 896 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
71c10038 897 /* COMMA */ {4, LEFT_ASSOC},
75aef48a 898 /* OPEN_PAREN */ {1, NO_L_OPERAND},
ad28cff7
NB
899 /* CLOSE_PAREN */ {0, 0},
900 /* EOF */ {0, 0},
901 /* EQ_EQ */ {11, LEFT_ASSOC},
902 /* NOT_EQ */ {11, LEFT_ASSOC},
68e65275
NB
903 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
904 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
ad28cff7
NB
905 /* UPLUS */ {16, NO_L_OPERAND},
906 /* UMINUS */ {16, NO_L_OPERAND}
cf00a885
ZW
907};
908
7f2935c7 909/* Parse and evaluate a C expression, reading from PFILE.
df383483 910 Returns the truth value of the expression.
87ed109f
NB
911
912 The implementation is an operator precedence parser, i.e. a
913 bottom-up parser, using a stack for not-yet-reduced tokens.
914
915 The stack base is op_stack, and the current stack pointer is 'top'.
916 There is a stack element for each operator (only), and the most
917 recently pushed operator is 'top->op'. An operand (value) is
918 stored in the 'value' field of the stack element of the operator
919 that precedes it. */
920bool
d750887f 921_cpp_parse_expr (cpp_reader *pfile, bool is_if)
7f2935c7 922{
87ed109f
NB
923 struct op *top = pfile->op_stack;
924 unsigned int lex_count;
925 bool saw_leading_not, want_value = true;
926
927 pfile->state.skip_eval = 0;
7f2935c7 928
93c80368 929 /* Set up detection of #if ! defined(). */
6d18adbc 930 pfile->mi_ind_cmacro = 0;
87ed109f 931 saw_leading_not = false;
6d18adbc 932 lex_count = 0;
93c80368 933
87ed109f 934 /* Lowest priority operator prevents further reductions. */
cf00a885 935 top->op = CPP_EOF;
4063b943 936
7f2935c7
PB
937 for (;;)
938 {
cf00a885 939 struct op op;
7f2935c7 940
6d18adbc 941 lex_count++;
68e65275
NB
942 op.token = cpp_get_token (pfile);
943 op.op = op.token->type;
47960aaf 944 op.loc = op.token->src_loc;
7f2935c7 945
7f2935c7
PB
946 switch (op.op)
947 {
60284a59 948 /* These tokens convert into values. */
c60e94a7 949 case CPP_NUMBER:
60284a59
NB
950 case CPP_CHAR:
951 case CPP_WCHAR:
b6baa67d
KVH
952 case CPP_CHAR16:
953 case CPP_CHAR32:
60284a59
NB
954 case CPP_NAME:
955 case CPP_HASH:
f8b954fc 956 if (!want_value)
60284a59 957 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
68e65275 958 cpp_token_as_text (pfile, op.token));
f8b954fc 959 want_value = false;
68e65275 960 top->value = eval_token (pfile, op.token);
9ee70313
NB
961 continue;
962
6d18adbc
NB
963 case CPP_NOT:
964 saw_leading_not = lex_count == 1;
6d18adbc 965 break;
cf00a885 966 case CPP_PLUS:
f8b954fc
NB
967 if (want_value)
968 op.op = CPP_UPLUS;
969 break;
970 case CPP_MINUS:
971 if (want_value)
972 op.op = CPP_UMINUS;
973 break;
60284a59 974
f8b954fc 975 default:
60284a59 976 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
cd7ab83f 977 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
68e65275 978 cpp_token_as_text (pfile, op.token));
f8b954fc 979 break;
7f2935c7 980 }
7f2935c7 981
87ed109f
NB
982 /* Check we have a value or operator as appropriate. */
983 if (optab[op.op].flags & NO_L_OPERAND)
7f2935c7 984 {
87ed109f 985 if (!want_value)
60284a59 986 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
68e65275 987 cpp_token_as_text (pfile, op.token));
87ed109f
NB
988 }
989 else if (want_value)
990 {
a09d4744
NB
991 /* We want a number (or expression) and haven't got one.
992 Try to emit a specific diagnostic. */
993 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
994 SYNTAX_ERROR ("missing expression between '(' and ')'");
995
996 if (op.op == CPP_EOF && top->op == CPP_EOF)
d750887f 997 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
a09d4744
NB
998
999 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1000 SYNTAX_ERROR2 ("operator '%s' has no right operand",
1001 cpp_token_as_text (pfile, top->token));
1002 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1003 /* Complain about missing paren during reduction. */;
1004 else
1005 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1006 cpp_token_as_text (pfile, op.token));
7f2935c7 1007 }
9ee70313 1008
87ed109f
NB
1009 top = reduce (pfile, top, op.op);
1010 if (!top)
1011 goto syntax_error;
9ee70313 1012
60284a59
NB
1013 if (op.op == CPP_EOF)
1014 break;
1015
87ed109f 1016 switch (op.op)
b22ef131 1017 {
87ed109f
NB
1018 case CPP_CLOSE_PAREN:
1019 continue;
87ed109f 1020 case CPP_OR_OR:
91318908 1021 if (!num_zerop (top->value))
87ed109f
NB
1022 pfile->state.skip_eval++;
1023 break;
1024 case CPP_AND_AND:
1025 case CPP_QUERY:
91318908 1026 if (num_zerop (top->value))
87ed109f
NB
1027 pfile->state.skip_eval++;
1028 break;
1029 case CPP_COLON:
60284a59
NB
1030 if (top->op != CPP_QUERY)
1031 SYNTAX_ERROR (" ':' without preceding '?'");
91318908 1032 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
87ed109f
NB
1033 pfile->state.skip_eval++;
1034 else
1035 pfile->state.skip_eval--;
1036 default:
1037 break;
4063b943 1038 }
87ed109f 1039
f8b954fc 1040 want_value = true;
4063b943 1041
0f41302f 1042 /* Check for and handle stack overflow. */
87ed109f
NB
1043 if (++top == pfile->op_limit)
1044 top = _cpp_expand_op_stack (pfile);
df383483 1045
7f2935c7 1046 top->op = op.op;
68e65275 1047 top->token = op.token;
47960aaf 1048 top->loc = op.token->src_loc;
7f2935c7 1049 }
9ee70313 1050
6d18adbc
NB
1051 /* The controlling macro expression is only valid if we called lex 3
1052 times: <!> <defined expression> and <EOF>. push_conditional ()
1053 checks that we are at top-of-file. */
1054 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1055 pfile->mi_ind_cmacro = 0;
1056
87ed109f 1057 if (top != pfile->op_stack)
ebef4e8c 1058 {
d750887f
TT
1059 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1060 is_if ? "#if" : "#elif");
4063b943 1061 syntax_error:
87ed109f 1062 return false; /* Return false on syntax error. */
4063b943 1063 }
9ee70313 1064
91318908 1065 return !num_zerop (top->value);
87ed109f
NB
1066}
1067
1068/* Reduce the operator / value stack if possible, in preparation for
1069 pushing operator OP. Returns NULL on error, otherwise the top of
1070 the stack. */
1071static struct op *
6cf87ca4 1072reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
87ed109f
NB
1073{
1074 unsigned int prio;
1075
91318908
NB
1076 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1077 {
1078 bad_op:
0527bc4e 1079 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
91318908
NB
1080 return 0;
1081 }
1082
87ed109f
NB
1083 if (op == CPP_OPEN_PAREN)
1084 return top;
1085
1086 /* Decrement the priority of left-associative operators to force a
1087 reduction with operators of otherwise equal priority. */
1088 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1089 while (prio < optab[top->op].prio)
1090 {
68e65275
NB
1091 if (CPP_OPTION (pfile, warn_num_sign_change)
1092 && optab[top->op].flags & CHECK_PROMOTION)
1093 check_promotion (pfile, top);
1094
75aef48a
NB
1095 switch (top->op)
1096 {
1097 case CPP_UPLUS:
1098 case CPP_UMINUS:
1099 case CPP_NOT:
1100 case CPP_COMPL:
1101 top[-1].value = num_unary_op (pfile, top->value, top->op);
47960aaf 1102 top[-1].loc = top->loc;
75aef48a 1103 break;
87ed109f 1104
75aef48a
NB
1105 case CPP_PLUS:
1106 case CPP_MINUS:
1107 case CPP_RSHIFT:
1108 case CPP_LSHIFT:
75aef48a
NB
1109 case CPP_COMMA:
1110 top[-1].value = num_binary_op (pfile, top[-1].value,
1111 top->value, top->op);
47960aaf 1112 top[-1].loc = top->loc;
75aef48a 1113 break;
91318908 1114
75aef48a
NB
1115 case CPP_GREATER:
1116 case CPP_LESS:
1117 case CPP_GREATER_EQ:
1118 case CPP_LESS_EQ:
1119 top[-1].value
1120 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
47960aaf 1121 top[-1].loc = top->loc;
75aef48a 1122 break;
91318908 1123
75aef48a
NB
1124 case CPP_EQ_EQ:
1125 case CPP_NOT_EQ:
1126 top[-1].value
1127 = num_equality_op (pfile, top[-1].value, top->value, top->op);
47960aaf 1128 top[-1].loc = top->loc;
75aef48a 1129 break;
87ed109f 1130
75aef48a
NB
1131 case CPP_AND:
1132 case CPP_OR:
1133 case CPP_XOR:
1134 top[-1].value
1135 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
47960aaf 1136 top[-1].loc = top->loc;
75aef48a 1137 break;
91318908 1138
75aef48a
NB
1139 case CPP_MULT:
1140 top[-1].value = num_mul (pfile, top[-1].value, top->value);
47960aaf 1141 top[-1].loc = top->loc;
75aef48a 1142 break;
ad28cff7 1143
75aef48a
NB
1144 case CPP_DIV:
1145 case CPP_MOD:
1146 top[-1].value = num_div_op (pfile, top[-1].value,
b506a5a2 1147 top->value, top->op, top->loc);
47960aaf 1148 top[-1].loc = top->loc;
75aef48a 1149 break;
ad28cff7 1150
75aef48a
NB
1151 case CPP_OR_OR:
1152 top--;
1153 if (!num_zerop (top->value))
1154 pfile->state.skip_eval--;
1155 top->value.low = (!num_zerop (top->value)
1156 || !num_zerop (top[1].value));
1157 top->value.high = 0;
1158 top->value.unsignedp = false;
1159 top->value.overflow = false;
47960aaf 1160 top->loc = top[1].loc;
75aef48a
NB
1161 continue;
1162
1163 case CPP_AND_AND:
1164 top--;
1165 if (num_zerop (top->value))
1166 pfile->state.skip_eval--;
1167 top->value.low = (!num_zerop (top->value)
1168 && !num_zerop (top[1].value));
1169 top->value.high = 0;
1170 top->value.unsignedp = false;
1171 top->value.overflow = false;
47960aaf 1172 top->loc = top[1].loc;
75aef48a
NB
1173 continue;
1174
1175 case CPP_OPEN_PAREN:
1176 if (op != CPP_CLOSE_PAREN)
1177 {
47960aaf
MLI
1178 cpp_error_with_line (pfile, CPP_DL_ERROR,
1179 top->token->src_loc,
1180 0, "missing ')' in expression");
75aef48a
NB
1181 return 0;
1182 }
1183 top--;
1184 top->value = top[1].value;
47960aaf 1185 top->loc = top[1].loc;
75aef48a
NB
1186 return top;
1187
1188 case CPP_COLON:
1189 top -= 2;
1190 if (!num_zerop (top->value))
1191 {
ad28cff7 1192 pfile->state.skip_eval--;
75aef48a 1193 top->value = top[1].value;
47960aaf 1194 top->loc = top[1].loc;
75aef48a
NB
1195 }
1196 else
47960aaf
MLI
1197 {
1198 top->value = top[2].value;
1199 top->loc = top[2].loc;
1200 }
75aef48a
NB
1201 top->value.unsignedp = (top[1].value.unsignedp
1202 || top[2].value.unsignedp);
1203 continue;
1204
1205 case CPP_QUERY:
71c10038
TT
1206 /* COMMA and COLON should not reduce a QUERY operator. */
1207 if (op == CPP_COMMA || op == CPP_COLON)
1208 return top;
0527bc4e 1209 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
75aef48a
NB
1210 return 0;
1211
1212 default:
1213 goto bad_op;
1214 }
91318908 1215
ad28cff7 1216 top--;
91318908 1217 if (top->value.overflow && !pfile->state.skip_eval)
0527bc4e 1218 cpp_error (pfile, CPP_DL_PEDWARN,
91318908 1219 "integer overflow in preprocessor expression");
87ed109f
NB
1220 }
1221
1222 if (op == CPP_CLOSE_PAREN)
1223 {
0527bc4e 1224 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
87ed109f
NB
1225 return 0;
1226 }
1227
1228 return top;
1229}
1230
1231/* Returns the position of the old top of stack after expansion. */
1232struct op *
6cf87ca4 1233_cpp_expand_op_stack (cpp_reader *pfile)
87ed109f 1234{
32fa4565
NB
1235 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1236 size_t new_size = old_size * 2 + 20;
87ed109f 1237
c3f829c1 1238 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
32fa4565 1239 pfile->op_limit = pfile->op_stack + new_size;
87ed109f 1240
32fa4565 1241 return pfile->op_stack + old_size;
7f2935c7 1242}
91318908 1243
68e65275
NB
1244/* Emits a warning if the effective sign of either operand of OP
1245 changes because of integer promotions. */
1246static void
6cf87ca4 1247check_promotion (cpp_reader *pfile, const struct op *op)
68e65275
NB
1248{
1249 if (op->value.unsignedp == op[-1].value.unsignedp)
1250 return;
1251
1252 if (op->value.unsignedp)
1253 {
1254 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
47960aaf
MLI
1255 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1256 "the left operand of \"%s\" changes sign when promoted",
1257 cpp_token_as_text (pfile, op->token));
68e65275
NB
1258 }
1259 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
47960aaf 1260 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
68e65275
NB
1261 "the right operand of \"%s\" changes sign when promoted",
1262 cpp_token_as_text (pfile, op->token));
1263}
1264
91318908
NB
1265/* Clears the unused high order bits of the number pointed to by PNUM. */
1266static cpp_num
6cf87ca4 1267num_trim (cpp_num num, size_t precision)
91318908
NB
1268{
1269 if (precision > PART_PRECISION)
1270 {
1271 precision -= PART_PRECISION;
1272 if (precision < PART_PRECISION)
359b0bec 1273 num.high &= ((cpp_num_part) 1 << precision) - 1;
91318908
NB
1274 }
1275 else
1276 {
1277 if (precision < PART_PRECISION)
359b0bec 1278 num.low &= ((cpp_num_part) 1 << precision) - 1;
91318908
NB
1279 num.high = 0;
1280 }
1281
1282 return num;
1283}
1284
1285/* True iff A (presumed signed) >= 0. */
1286static bool
6cf87ca4 1287num_positive (cpp_num num, size_t precision)
91318908
NB
1288{
1289 if (precision > PART_PRECISION)
1290 {
1291 precision -= PART_PRECISION;
359b0bec 1292 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
91318908
NB
1293 }
1294
359b0bec 1295 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
91318908
NB
1296}
1297
ceeedfc1
NB
1298/* Sign extend a number, with PRECISION significant bits and all
1299 others assumed clear, to fill out a cpp_num structure. */
1300cpp_num
6cf87ca4 1301cpp_num_sign_extend (cpp_num num, size_t precision)
ceeedfc1
NB
1302{
1303 if (!num.unsignedp)
1304 {
1305 if (precision > PART_PRECISION)
1306 {
1307 precision -= PART_PRECISION;
1308 if (precision < PART_PRECISION
1309 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1310 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1311 }
1312 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1313 {
1314 if (precision < PART_PRECISION)
1315 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1316 num.high = ~(cpp_num_part) 0;
1317 }
1318 }
1319
1320 return num;
1321}
1322
91318908
NB
1323/* Returns the negative of NUM. */
1324static cpp_num
6cf87ca4 1325num_negate (cpp_num num, size_t precision)
91318908
NB
1326{
1327 cpp_num copy;
1328
1329 copy = num;
1330 num.high = ~num.high;
1331 num.low = ~num.low;
1332 if (++num.low == 0)
1333 num.high++;
1334 num = num_trim (num, precision);
1335 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1336
1337 return num;
1338}
1339
1340/* Returns true if A >= B. */
1341static bool
6cf87ca4 1342num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
91318908
NB
1343{
1344 bool unsignedp;
1345
1346 unsignedp = pa.unsignedp || pb.unsignedp;
1347
1348 if (!unsignedp)
1349 {
1350 /* Both numbers have signed type. If they are of different
1351 sign, the answer is the sign of A. */
1352 unsignedp = num_positive (pa, precision);
1353
1354 if (unsignedp != num_positive (pb, precision))
1355 return unsignedp;
1356
1357 /* Otherwise we can do an unsigned comparison. */
1358 }
1359
1360 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1361}
1362
1363/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1364static cpp_num
6cf87ca4
ZW
1365num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1366 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
91318908
NB
1367{
1368 lhs.overflow = false;
1369 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1370
1371 /* As excess precision is zeroed, there is no need to num_trim () as
1372 these operations cannot introduce a set bit there. */
1373 if (op == CPP_AND)
1374 {
1375 lhs.low &= rhs.low;
1376 lhs.high &= rhs.high;
1377 }
1378 else if (op == CPP_OR)
1379 {
1380 lhs.low |= rhs.low;
1381 lhs.high |= rhs.high;
1382 }
1383 else
1384 {
1385 lhs.low ^= rhs.low;
1386 lhs.high ^= rhs.high;
1387 }
1388
1389 return lhs;
1390}
1391
1392/* Returns LHS OP RHS, where OP is an inequality. */
1393static cpp_num
6cf87ca4
ZW
1394num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1395 enum cpp_ttype op)
91318908
NB
1396{
1397 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1398
1399 if (op == CPP_GREATER_EQ)
1400 lhs.low = gte;
1401 else if (op == CPP_LESS)
1402 lhs.low = !gte;
1403 else if (op == CPP_GREATER)
1404 lhs.low = gte && !num_eq (lhs, rhs);
1405 else /* CPP_LESS_EQ. */
1406 lhs.low = !gte || num_eq (lhs, rhs);
1407
1408 lhs.high = 0;
1409 lhs.overflow = false;
1410 lhs.unsignedp = false;
1411 return lhs;
1412}
1413
1414/* Returns LHS OP RHS, where OP is == or !=. */
1415static cpp_num
6cf87ca4
ZW
1416num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1417 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
91318908 1418{
97459791
JM
1419 /* Work around a 3.0.4 bug; see PR 6950. */
1420 bool eq = num_eq (lhs, rhs);
91318908 1421 if (op == CPP_NOT_EQ)
97459791
JM
1422 eq = !eq;
1423 lhs.low = eq;
91318908
NB
1424 lhs.high = 0;
1425 lhs.overflow = false;
1426 lhs.unsignedp = false;
1427 return lhs;
1428}
1429
1430/* Shift NUM, of width PRECISION, right by N bits. */
1431static cpp_num
6cf87ca4 1432num_rshift (cpp_num num, size_t precision, size_t n)
91318908
NB
1433{
1434 cpp_num_part sign_mask;
6de9cd9a 1435 bool x = num_positive (num, precision);
91318908 1436
6de9cd9a 1437 if (num.unsignedp || x)
91318908
NB
1438 sign_mask = 0;
1439 else
1440 sign_mask = ~(cpp_num_part) 0;
1441
1442 if (n >= precision)
1443 num.high = num.low = sign_mask;
1444 else
1445 {
1446 /* Sign-extend. */
1447 if (precision < PART_PRECISION)
1448 num.high = sign_mask, num.low |= sign_mask << precision;
1449 else if (precision < 2 * PART_PRECISION)
1450 num.high |= sign_mask << (precision - PART_PRECISION);
1451
1452 if (n >= PART_PRECISION)
1453 {
1454 n -= PART_PRECISION;
1455 num.low = num.high;
1456 num.high = sign_mask;
1457 }
1458
1459 if (n)
1460 {
1461 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1462 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1463 }
1464 }
1465
1466 num = num_trim (num, precision);
1467 num.overflow = false;
1468 return num;
1469}
1470
1471/* Shift NUM, of width PRECISION, left by N bits. */
1472static cpp_num
6cf87ca4 1473num_lshift (cpp_num num, size_t precision, size_t n)
91318908
NB
1474{
1475 if (n >= precision)
1476 {
1477 num.overflow = !num.unsignedp && !num_zerop (num);
1478 num.high = num.low = 0;
1479 }
1480 else
1481 {
1482 cpp_num orig, maybe_orig;
1483 size_t m = n;
1484
1485 orig = num;
1486 if (m >= PART_PRECISION)
1487 {
1488 m -= PART_PRECISION;
1489 num.high = num.low;
1490 num.low = 0;
1491 }
1492 if (m)
1493 {
1494 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1495 num.low <<= m;
1496 }
1497 num = num_trim (num, precision);
1498
1499 if (num.unsignedp)
1500 num.overflow = false;
1501 else
1502 {
1503 maybe_orig = num_rshift (num, precision, n);
1504 num.overflow = !num_eq (orig, maybe_orig);
1505 }
1506 }
1507
1508 return num;
1509}
1510
1511/* The four unary operators: +, -, ! and ~. */
1512static cpp_num
6cf87ca4 1513num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
91318908
NB
1514{
1515 switch (op)
1516 {
1517 case CPP_UPLUS:
75aef48a 1518 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
87cf0651
SB
1519 cpp_warning (pfile, CPP_W_TRADITIONAL,
1520 "traditional C rejects the unary plus operator");
91318908
NB
1521 num.overflow = false;
1522 break;
1523
1524 case CPP_UMINUS:
1525 num = num_negate (num, CPP_OPTION (pfile, precision));
1526 break;
1527
1528 case CPP_COMPL:
1529 num.high = ~num.high;
1530 num.low = ~num.low;
1531 num = num_trim (num, CPP_OPTION (pfile, precision));
1532 num.overflow = false;
1533 break;
1534
1535 default: /* case CPP_NOT: */
1536 num.low = num_zerop (num);
1537 num.high = 0;
1538 num.overflow = false;
1539 num.unsignedp = false;
1540 break;
1541 }
1542
1543 return num;
1544}
1545
1546/* The various binary operators. */
1547static cpp_num
6cf87ca4 1548num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
91318908
NB
1549{
1550 cpp_num result;
1551 size_t precision = CPP_OPTION (pfile, precision);
91318908
NB
1552 size_t n;
1553
1554 switch (op)
1555 {
1556 /* Shifts. */
1557 case CPP_LSHIFT:
1558 case CPP_RSHIFT:
1559 if (!rhs.unsignedp && !num_positive (rhs, precision))
1560 {
1561 /* A negative shift is a positive shift the other way. */
1562 if (op == CPP_LSHIFT)
1563 op = CPP_RSHIFT;
1564 else
1565 op = CPP_LSHIFT;
1566 rhs = num_negate (rhs, precision);
1567 }
1568 if (rhs.high)
1569 n = ~0; /* Maximal. */
1570 else
1571 n = rhs.low;
1572 if (op == CPP_LSHIFT)
1573 lhs = num_lshift (lhs, precision, n);
1574 else
1575 lhs = num_rshift (lhs, precision, n);
1576 break;
1577
91318908
NB
1578 /* Arithmetic. */
1579 case CPP_MINUS:
1580 rhs = num_negate (rhs, precision);
1581 case CPP_PLUS:
1582 result.low = lhs.low + rhs.low;
1583 result.high = lhs.high + rhs.high;
1584 if (result.low < lhs.low)
1585 result.high++;
6de9cd9a
DN
1586 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1587 result.overflow = false;
91318908
NB
1588
1589 result = num_trim (result, precision);
6de9cd9a 1590 if (!result.unsignedp)
91318908
NB
1591 {
1592 bool lhsp = num_positive (lhs, precision);
1593 result.overflow = (lhsp == num_positive (rhs, precision)
1594 && lhsp != num_positive (result, precision));
1595 }
1596 return result;
1597
1598 /* Comma. */
1599 default: /* case CPP_COMMA: */
32e8aa9a
JM
1600 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1601 || !pfile->state.skip_eval))
0527bc4e 1602 cpp_error (pfile, CPP_DL_PEDWARN,
91318908
NB
1603 "comma operator in operand of #if");
1604 lhs = rhs;
1605 break;
1606 }
1607
1608 return lhs;
1609}
1610
1611/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1612 cannot overflow. */
1613static cpp_num
6cf87ca4 1614num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
91318908
NB
1615{
1616 cpp_num result;
1617 cpp_num_part middle[2], temp;
1618
1619 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1620 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1621
1622 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1623 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1624
1625 temp = result.low;
1626 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1627 if (result.low < temp)
1628 result.high++;
1629
1630 temp = result.low;
1631 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1632 if (result.low < temp)
1633 result.high++;
1634
1635 result.high += HIGH_PART (middle[0]);
1636 result.high += HIGH_PART (middle[1]);
6de9cd9a
DN
1637 result.unsignedp = true;
1638 result.overflow = false;
91318908
NB
1639
1640 return result;
1641}
1642
1643/* Multiply two preprocessing numbers. */
1644static cpp_num
6cf87ca4 1645num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
91318908
NB
1646{
1647 cpp_num result, temp;
1648 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1649 bool overflow, negate = false;
1650 size_t precision = CPP_OPTION (pfile, precision);
1651
1652 /* Prepare for unsigned multiplication. */
1653 if (!unsignedp)
1654 {
1655 if (!num_positive (lhs, precision))
1656 negate = !negate, lhs = num_negate (lhs, precision);
1657 if (!num_positive (rhs, precision))
1658 negate = !negate, rhs = num_negate (rhs, precision);
1659 }
1660
1661 overflow = lhs.high && rhs.high;
1662 result = num_part_mul (lhs.low, rhs.low);
1663
1664 temp = num_part_mul (lhs.high, rhs.low);
1665 result.high += temp.low;
1666 if (temp.high)
1667 overflow = true;
1668
1669 temp = num_part_mul (lhs.low, rhs.high);
1670 result.high += temp.low;
1671 if (temp.high)
1672 overflow = true;
1673
1674 temp.low = result.low, temp.high = result.high;
1675 result = num_trim (result, precision);
1676 if (!num_eq (result, temp))
1677 overflow = true;
1678
1679 if (negate)
1680 result = num_negate (result, precision);
1681
1682 if (unsignedp)
1683 result.overflow = false;
1684 else
1685 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1686 && !num_zerop (result));
1687 result.unsignedp = unsignedp;
1688
1689 return result;
1690}
1691
b506a5a2
MLI
1692/* Divide two preprocessing numbers, LHS and RHS, returning the answer
1693 or the remainder depending upon OP. LOCATION is the source location
1694 of this operator (for diagnostics). */
1695
91318908 1696static cpp_num
b506a5a2
MLI
1697num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1698 source_location location)
91318908
NB
1699{
1700 cpp_num result, sub;
1701 cpp_num_part mask;
1702 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1703 bool negate = false, lhs_neg = false;
1704 size_t i, precision = CPP_OPTION (pfile, precision);
1705
1706 /* Prepare for unsigned division. */
1707 if (!unsignedp)
1708 {
1709 if (!num_positive (lhs, precision))
1710 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1711 if (!num_positive (rhs, precision))
1712 negate = !negate, rhs = num_negate (rhs, precision);
1713 }
1714
1715 /* Find the high bit. */
1716 if (rhs.high)
1717 {
1718 i = precision - 1;
359b0bec 1719 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
91318908
NB
1720 for (; ; i--, mask >>= 1)
1721 if (rhs.high & mask)
1722 break;
1723 }
1724 else if (rhs.low)
1725 {
1726 if (precision > PART_PRECISION)
1727 i = precision - PART_PRECISION - 1;
1728 else
1729 i = precision - 1;
359b0bec 1730 mask = (cpp_num_part) 1 << i;
91318908
NB
1731 for (; ; i--, mask >>= 1)
1732 if (rhs.low & mask)
1733 break;
1734 }
1735 else
1736 {
75aef48a 1737 if (!pfile->state.skip_eval)
b506a5a2
MLI
1738 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1739 "division by zero in #if");
91318908
NB
1740 return lhs;
1741 }
1742
da7d8304 1743 /* First nonzero bit of RHS is bit I. Do naive division by
91318908
NB
1744 shifting the RHS fully left, and subtracting from LHS if LHS is
1745 at least as big, and then repeating but with one less shift.
1746 This is not very efficient, but is easy to understand. */
1747
1748 rhs.unsignedp = true;
1749 lhs.unsignedp = true;
1750 i = precision - i - 1;
1751 sub = num_lshift (rhs, precision, i);
1752
1753 result.high = result.low = 0;
1754 for (;;)
1755 {
1756 if (num_greater_eq (lhs, sub, precision))
1757 {
1758 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1759 if (i >= PART_PRECISION)
359b0bec 1760 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
91318908 1761 else
359b0bec 1762 result.low |= (cpp_num_part) 1 << i;
91318908
NB
1763 }
1764 if (i-- == 0)
1765 break;
1766 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1767 sub.high >>= 1;
1768 }
1769
1770 /* We divide so that the remainder has the sign of the LHS. */
1771 if (op == CPP_DIV)
1772 {
1773 result.unsignedp = unsignedp;
6de9cd9a
DN
1774 result.overflow = false;
1775 if (!unsignedp)
91318908
NB
1776 {
1777 if (negate)
1778 result = num_negate (result, precision);
22a8a52d
EC
1779 result.overflow = (num_positive (result, precision) ^ !negate
1780 && !num_zerop (result));
91318908
NB
1781 }
1782
1783 return result;
1784 }
1785
1786 /* CPP_MOD. */
1787 lhs.unsignedp = unsignedp;
1788 lhs.overflow = false;
1789 if (lhs_neg)
1790 lhs = num_negate (lhs, precision);
1791
1792 return lhs;
1793}