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