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