framework-2.c: Fix up for non existent includes being fatal errors now.
[official-gcc.git] / libcpp / expr.c
blob56945d94f4bba7eb664ba7e4a689c7dd99a66684
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "cpplib.h"
23 #include "internal.h"
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30 struct op
32 const cpp_token *token; /* The token forming op (for diagnostics). */
33 cpp_num value; /* The value logically "right" of op. */
34 source_location loc; /* The location of this value. */
35 enum cpp_ttype op;
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)
41 static bool num_positive (cpp_num, size_t);
42 static bool num_greater_eq (cpp_num, cpp_num, size_t);
43 static cpp_num num_trim (cpp_num, size_t);
44 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
46 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48 static cpp_num num_negate (cpp_num, size_t);
49 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56 static cpp_num num_lshift (cpp_num, size_t, size_t);
57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 static cpp_num append_digit (cpp_num, int, int, size_t);
60 static cpp_num parse_defined (cpp_reader *);
61 static cpp_num eval_token (cpp_reader *, const cpp_token *);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (const uchar *, size_t);
64 static unsigned int interpret_int_suffix (const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
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. */
82 static unsigned int
83 interpret_float_suffix (const uchar *s, size_t len)
85 size_t flags;
86 size_t f, d, l, w, q, i;
88 flags = 0;
89 f = d = l = w = q = i = 0;
91 /* Process decimal float suffixes, which are two letters starting
92 with d or D. Order and case are significant. */
93 if (len == 2 && (*s == 'd' || *s == 'D'))
95 bool uppercase = (*s == 'D');
96 switch (s[1])
98 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
99 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
100 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
101 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
102 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
103 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 default:
105 /* Additional two-character suffixes beginning with D are not
106 for decimal float constants. */
107 break;
111 /* Recognize a fixed-point suffix. */
112 switch (s[len-1])
114 case 'k': case 'K': flags = CPP_N_ACCUM; break;
115 case 'r': case 'R': flags = CPP_N_FRACT; break;
116 default: break;
119 /* Continue processing a fixed-point suffix. The suffix is case
120 insensitive except for ll or LL. Order is significant. */
121 if (flags)
123 if (len == 1)
124 return flags;
125 len--;
127 if (*s == 'u' || *s == 'U')
129 flags |= CPP_N_UNSIGNED;
130 if (len == 1)
131 return flags;
132 len--;
133 s++;
136 switch (*s)
138 case 'h': case 'H':
139 if (len == 1)
140 return flags |= CPP_N_SMALL;
141 break;
142 case 'l':
143 if (len == 1)
144 return flags |= CPP_N_MEDIUM;
145 if (len == 2 && s[1] == 'l')
146 return flags |= CPP_N_LARGE;
147 break;
148 case 'L':
149 if (len == 1)
150 return flags |= CPP_N_MEDIUM;
151 if (len == 2 && s[1] == 'L')
152 return flags |= CPP_N_LARGE;
153 break;
154 default:
155 break;
157 /* Anything left at this point is invalid. */
158 return 0;
161 /* In any remaining valid suffix, the case and order don't matter. */
162 while (len--)
163 switch (s[len])
165 case 'f': case 'F': f++; break;
166 case 'd': case 'D': d++; break;
167 case 'l': case 'L': l++; break;
168 case 'w': case 'W': w++; break;
169 case 'q': case 'Q': q++; break;
170 case 'i': case 'I':
171 case 'j': case 'J': i++; break;
172 default:
173 return 0;
176 if (f + d + l + w + q > 1 || i > 1)
177 return 0;
179 return ((i ? CPP_N_IMAGINARY : 0)
180 | (f ? CPP_N_SMALL :
181 d ? CPP_N_MEDIUM :
182 l ? CPP_N_LARGE :
183 w ? CPP_N_MD_W :
184 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
187 /* Subroutine of cpp_classify_number. S points to an integer suffix
188 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
189 flag vector describing the suffix. */
190 static unsigned int
191 interpret_int_suffix (const uchar *s, size_t len)
193 size_t u, l, i;
195 u = l = i = 0;
197 while (len--)
198 switch (s[len])
200 case 'u': case 'U': u++; break;
201 case 'i': case 'I':
202 case 'j': case 'J': i++; break;
203 case 'l': case 'L': l++;
204 /* If there are two Ls, they must be adjacent and the same case. */
205 if (l == 2 && s[len] != s[len + 1])
206 return 0;
207 break;
208 default:
209 return 0;
212 if (l > 2 || u > 1 || i > 1)
213 return 0;
215 return ((i ? CPP_N_IMAGINARY : 0)
216 | (u ? CPP_N_UNSIGNED : 0)
217 | ((l == 0) ? CPP_N_SMALL
218 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
221 /* Categorize numeric constants according to their field (integer,
222 floating point, or invalid), radix (decimal, octal, hexadecimal),
223 and type suffixes. */
224 unsigned int
225 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
227 const uchar *str = token->val.str.text;
228 const uchar *limit;
229 unsigned int max_digit, result, radix;
230 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
232 /* If the lexer has done its job, length one can only be a single
233 digit. Fast-path this very common case. */
234 if (token->val.str.len == 1)
235 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
237 limit = str + token->val.str.len;
238 float_flag = NOT_FLOAT;
239 max_digit = 0;
240 radix = 10;
242 /* First, interpret the radix. */
243 if (*str == '0')
245 radix = 8;
246 str++;
248 /* Require at least one hex digit to classify it as hex. */
249 if ((*str == 'x' || *str == 'X')
250 && (str[1] == '.' || ISXDIGIT (str[1])))
252 radix = 16;
253 str++;
255 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
257 radix = 2;
258 str++;
262 /* Now scan for a well-formed integer or float. */
263 for (;;)
265 unsigned int c = *str++;
267 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
269 c = hex_value (c);
270 if (c > max_digit)
271 max_digit = c;
273 else if (c == '.')
275 if (float_flag == NOT_FLOAT)
276 float_flag = AFTER_POINT;
277 else
278 SYNTAX_ERROR ("too many decimal points in number");
280 else if ((radix <= 10 && (c == 'e' || c == 'E'))
281 || (radix == 16 && (c == 'p' || c == 'P')))
283 float_flag = AFTER_EXPON;
284 break;
286 else
288 /* Start of suffix. */
289 str--;
290 break;
294 /* The suffix may be for decimal fixed-point constants without exponent. */
295 if (radix != 16 && float_flag == NOT_FLOAT)
297 result = interpret_float_suffix (str, limit - str);
298 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
300 result |= CPP_N_FLOATING;
301 /* We need to restore the radix to 10, if the radix is 8. */
302 if (radix == 8)
303 radix = 10;
305 if (CPP_PEDANTIC (pfile))
306 cpp_error (pfile, CPP_DL_PEDWARN,
307 "fixed-point constants are a GCC extension");
308 goto syntax_ok;
310 else
311 result = 0;
314 if (float_flag != NOT_FLOAT && radix == 8)
315 radix = 10;
317 if (max_digit >= radix)
319 if (radix == 2)
320 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
321 else
322 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
325 if (float_flag != NOT_FLOAT)
327 if (radix == 2)
329 cpp_error (pfile, CPP_DL_ERROR,
330 "invalid prefix \"0b\" for floating constant");
331 return CPP_N_INVALID;
334 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
335 cpp_error (pfile, CPP_DL_PEDWARN,
336 "use of C99 hexadecimal floating constant");
338 if (float_flag == AFTER_EXPON)
340 if (*str == '+' || *str == '-')
341 str++;
343 /* Exponent is decimal, even if string is a hex float. */
344 if (!ISDIGIT (*str))
345 SYNTAX_ERROR ("exponent has no digits");
348 str++;
349 while (ISDIGIT (*str));
351 else if (radix == 16)
352 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
354 result = interpret_float_suffix (str, limit - str);
355 if (result == 0)
357 cpp_error (pfile, CPP_DL_ERROR,
358 "invalid suffix \"%.*s\" on floating constant",
359 (int) (limit - str), str);
360 return CPP_N_INVALID;
363 /* Traditional C didn't accept any floating suffixes. */
364 if (limit != str
365 && CPP_WTRADITIONAL (pfile)
366 && ! cpp_sys_macro_p (pfile))
367 cpp_error (pfile, CPP_DL_WARNING,
368 "traditional C rejects the \"%.*s\" suffix",
369 (int) (limit - str), str);
371 /* A suffix for double is a GCC extension via decimal float support.
372 If the suffix also specifies an imaginary value we'll catch that
373 later. */
374 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
375 cpp_error (pfile, CPP_DL_PEDWARN,
376 "suffix for double constant is a GCC extension");
378 /* Radix must be 10 for decimal floats. */
379 if ((result & CPP_N_DFLOAT) && radix != 10)
381 cpp_error (pfile, CPP_DL_ERROR,
382 "invalid suffix \"%.*s\" with hexadecimal floating constant",
383 (int) (limit - str), str);
384 return CPP_N_INVALID;
387 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
388 cpp_error (pfile, CPP_DL_PEDWARN,
389 "fixed-point constants are a GCC extension");
391 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
392 cpp_error (pfile, CPP_DL_PEDWARN,
393 "decimal float constants are a GCC extension");
395 result |= CPP_N_FLOATING;
397 else
399 result = interpret_int_suffix (str, limit - str);
400 if (result == 0)
402 cpp_error (pfile, CPP_DL_ERROR,
403 "invalid suffix \"%.*s\" on integer constant",
404 (int) (limit - str), str);
405 return CPP_N_INVALID;
408 /* Traditional C only accepted the 'L' suffix.
409 Suppress warning about 'LL' with -Wno-long-long. */
410 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
412 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
413 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
415 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
416 cpp_error (pfile, CPP_DL_WARNING,
417 "traditional C rejects the \"%.*s\" suffix",
418 (int) (limit - str), str);
421 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
422 && ! CPP_OPTION (pfile, c99)
423 && CPP_OPTION (pfile, warn_long_long))
424 cpp_error (pfile, CPP_DL_PEDWARN,
425 "use of C99 long long integer constant");
427 result |= CPP_N_INTEGER;
430 syntax_ok:
431 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
432 cpp_error (pfile, CPP_DL_PEDWARN,
433 "imaginary constants are a GCC extension");
434 if (radix == 2 && CPP_PEDANTIC (pfile))
435 cpp_error (pfile, CPP_DL_PEDWARN,
436 "binary constants are a GCC extension");
438 if (radix == 10)
439 result |= CPP_N_DECIMAL;
440 else if (radix == 16)
441 result |= CPP_N_HEX;
442 else if (radix == 2)
443 result |= CPP_N_BINARY;
444 else
445 result |= CPP_N_OCTAL;
447 return result;
449 syntax_error:
450 return CPP_N_INVALID;
453 /* cpp_interpret_integer converts an integer constant into a cpp_num,
454 of precision options->precision.
456 We do not provide any interface for decimal->float conversion,
457 because the preprocessor doesn't need it and we don't want to
458 drag in GCC's floating point emulator. */
459 cpp_num
460 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
461 unsigned int type)
463 const uchar *p, *end;
464 cpp_num result;
466 result.low = 0;
467 result.high = 0;
468 result.unsignedp = !!(type & CPP_N_UNSIGNED);
469 result.overflow = false;
471 p = token->val.str.text;
472 end = p + token->val.str.len;
474 /* Common case of a single digit. */
475 if (token->val.str.len == 1)
476 result.low = p[0] - '0';
477 else
479 cpp_num_part max;
480 size_t precision = CPP_OPTION (pfile, precision);
481 unsigned int base = 10, c = 0;
482 bool overflow = false;
484 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
486 base = 8;
487 p++;
489 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
491 base = 16;
492 p += 2;
494 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
496 base = 2;
497 p += 2;
500 /* We can add a digit to numbers strictly less than this without
501 needing the precision and slowness of double integers. */
502 max = ~(cpp_num_part) 0;
503 if (precision < PART_PRECISION)
504 max >>= PART_PRECISION - precision;
505 max = (max - base + 1) / base + 1;
507 for (; p < end; p++)
509 c = *p;
511 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
512 c = hex_value (c);
513 else
514 break;
516 /* Strict inequality for when max is set to zero. */
517 if (result.low < max)
518 result.low = result.low * base + c;
519 else
521 result = append_digit (result, c, base, precision);
522 overflow |= result.overflow;
523 max = 0;
527 if (overflow)
528 cpp_error (pfile, CPP_DL_PEDWARN,
529 "integer constant is too large for its type");
530 /* If too big to be signed, consider it unsigned. Only warn for
531 decimal numbers. Traditional numbers were always signed (but
532 we still honor an explicit U suffix); but we only have
533 traditional semantics in directives. */
534 else if (!result.unsignedp
535 && !(CPP_OPTION (pfile, traditional)
536 && pfile->state.in_directive)
537 && !num_positive (result, precision))
539 if (base == 10)
540 cpp_error (pfile, CPP_DL_WARNING,
541 "integer constant is so large that it is unsigned");
542 result.unsignedp = true;
546 return result;
549 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
550 static cpp_num
551 append_digit (cpp_num num, int digit, int base, size_t precision)
553 cpp_num result;
554 unsigned int shift;
555 bool overflow;
556 cpp_num_part add_high, add_low;
558 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
559 need to worry about add_high overflowing. */
560 switch (base)
562 case 2:
563 shift = 1;
564 break;
566 case 16:
567 shift = 4;
568 break;
570 default:
571 shift = 3;
573 overflow = !!(num.high >> (PART_PRECISION - shift));
574 result.high = num.high << shift;
575 result.low = num.low << shift;
576 result.high |= num.low >> (PART_PRECISION - shift);
577 result.unsignedp = num.unsignedp;
579 if (base == 10)
581 add_low = num.low << 1;
582 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
584 else
585 add_high = add_low = 0;
587 if (add_low + digit < add_low)
588 add_high++;
589 add_low += digit;
591 if (result.low + add_low < result.low)
592 add_high++;
593 if (result.high + add_high < result.high)
594 overflow = true;
596 result.low += add_low;
597 result.high += add_high;
598 result.overflow = overflow;
600 /* The above code catches overflow of a cpp_num type. This catches
601 overflow of the (possibly shorter) target precision. */
602 num.low = result.low;
603 num.high = result.high;
604 result = num_trim (result, precision);
605 if (!num_eq (result, num))
606 result.overflow = true;
608 return result;
611 /* Handle meeting "defined" in a preprocessor expression. */
612 static cpp_num
613 parse_defined (cpp_reader *pfile)
615 cpp_num result;
616 int paren = 0;
617 cpp_hashnode *node = 0;
618 const cpp_token *token;
619 cpp_context *initial_context = pfile->context;
621 /* Don't expand macros. */
622 pfile->state.prevent_expansion++;
624 token = cpp_get_token (pfile);
625 if (token->type == CPP_OPEN_PAREN)
627 paren = 1;
628 token = cpp_get_token (pfile);
631 if (token->type == CPP_NAME)
633 node = token->val.node;
634 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
636 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
637 node = 0;
640 else
642 cpp_error (pfile, CPP_DL_ERROR,
643 "operator \"defined\" requires an identifier");
644 if (token->flags & NAMED_OP)
646 cpp_token op;
648 op.flags = 0;
649 op.type = token->type;
650 cpp_error (pfile, CPP_DL_ERROR,
651 "(\"%s\" is an alternative token for \"%s\" in C++)",
652 cpp_token_as_text (pfile, token),
653 cpp_token_as_text (pfile, &op));
657 if (node)
659 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
660 cpp_error (pfile, CPP_DL_WARNING,
661 "this use of \"defined\" may not be portable");
663 _cpp_mark_macro_used (node);
664 if (!(node->flags & NODE_USED))
666 node->flags |= NODE_USED;
667 if (node->type == NT_MACRO)
669 if (pfile->cb.used_define)
670 pfile->cb.used_define (pfile, pfile->directive_line, node);
672 else
674 if (pfile->cb.used_undef)
675 pfile->cb.used_undef (pfile, pfile->directive_line, node);
679 /* A possible controlling macro of the form #if !defined ().
680 _cpp_parse_expr checks there was no other junk on the line. */
681 pfile->mi_ind_cmacro = node;
684 pfile->state.prevent_expansion--;
686 result.unsignedp = false;
687 result.high = 0;
688 result.overflow = false;
689 result.low = node && node->type == NT_MACRO;
690 return result;
693 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
694 number or character constant, or the result of the "defined" or "#"
695 operators). */
696 static cpp_num
697 eval_token (cpp_reader *pfile, const cpp_token *token)
699 cpp_num result;
700 unsigned int temp;
701 int unsignedp = 0;
703 result.unsignedp = false;
704 result.overflow = false;
706 switch (token->type)
708 case CPP_NUMBER:
709 temp = cpp_classify_number (pfile, token);
710 switch (temp & CPP_N_CATEGORY)
712 case CPP_N_FLOATING:
713 cpp_error (pfile, CPP_DL_ERROR,
714 "floating constant in preprocessor expression");
715 break;
716 case CPP_N_INTEGER:
717 if (!(temp & CPP_N_IMAGINARY))
718 return cpp_interpret_integer (pfile, token, temp);
719 cpp_error (pfile, CPP_DL_ERROR,
720 "imaginary number in preprocessor expression");
721 break;
723 case CPP_N_INVALID:
724 /* Error already issued. */
725 break;
727 result.high = result.low = 0;
728 break;
730 case CPP_WCHAR:
731 case CPP_CHAR:
732 case CPP_CHAR16:
733 case CPP_CHAR32:
735 cppchar_t cc = cpp_interpret_charconst (pfile, token,
736 &temp, &unsignedp);
738 result.high = 0;
739 result.low = cc;
740 /* Sign-extend the result if necessary. */
741 if (!unsignedp && (cppchar_signed_t) cc < 0)
743 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
744 result.low |= ~(~(cpp_num_part) 0
745 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
746 result.high = ~(cpp_num_part) 0;
747 result = num_trim (result, CPP_OPTION (pfile, precision));
750 break;
752 case CPP_NAME:
753 if (token->val.node == pfile->spec_nodes.n_defined)
754 return parse_defined (pfile);
755 else if (CPP_OPTION (pfile, cplusplus)
756 && (token->val.node == pfile->spec_nodes.n_true
757 || token->val.node == pfile->spec_nodes.n_false))
759 result.high = 0;
760 result.low = (token->val.node == pfile->spec_nodes.n_true);
762 else
764 result.high = 0;
765 result.low = 0;
766 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
767 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
768 NODE_NAME (token->val.node));
770 break;
772 case CPP_HASH:
773 if (!pfile->state.skipping)
775 /* A pedantic warning takes precedence over a deprecated
776 warning here. */
777 if (CPP_PEDANTIC (pfile))
778 cpp_error (pfile, CPP_DL_PEDWARN,
779 "assertions are a GCC extension");
780 else if (CPP_OPTION (pfile, warn_deprecated))
781 cpp_error (pfile, CPP_DL_WARNING,
782 "assertions are a deprecated extension");
784 _cpp_test_assertion (pfile, &temp);
785 result.high = 0;
786 result.low = temp;
787 break;
789 default:
790 abort ();
793 result.unsignedp = !!unsignedp;
794 return result;
797 /* Operator precedence and flags table.
799 After an operator is returned from the lexer, if it has priority less
800 than the operator on the top of the stack, we reduce the stack by one
801 operator and repeat the test. Since equal priorities do not reduce,
802 this is naturally right-associative.
804 We handle left-associative operators by decrementing the priority of
805 just-lexed operators by one, but retaining the priority of operators
806 already on the stack.
808 The remaining cases are '(' and ')'. We handle '(' by skipping the
809 reduction phase completely. ')' is given lower priority than
810 everything else, including '(', effectively forcing a reduction of the
811 parenthesized expression. If there is a matching '(', the routine
812 reduce() exits immediately. If the normal exit route sees a ')', then
813 there cannot have been a matching '(' and an error message is output.
815 The parser assumes all shifted operators require a left operand unless
816 the flag NO_L_OPERAND is set. These semantics are automatic; any
817 extra semantics need to be handled with operator-specific code. */
819 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
820 operand changes because of integer promotions. */
821 #define NO_L_OPERAND (1 << 0)
822 #define LEFT_ASSOC (1 << 1)
823 #define CHECK_PROMOTION (1 << 2)
825 /* Operator to priority map. Must be in the same order as the first
826 N entries of enum cpp_ttype. */
827 static const struct cpp_operator
829 uchar prio;
830 uchar flags;
831 } optab[] =
833 /* EQ */ {0, 0}, /* Shouldn't happen. */
834 /* NOT */ {16, NO_L_OPERAND},
835 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
836 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
837 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
838 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
839 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
840 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
841 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
842 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
843 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
844 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
845 /* RSHIFT */ {13, LEFT_ASSOC},
846 /* LSHIFT */ {13, LEFT_ASSOC},
848 /* COMPL */ {16, NO_L_OPERAND},
849 /* AND_AND */ {6, LEFT_ASSOC},
850 /* OR_OR */ {5, LEFT_ASSOC},
851 /* Note that QUERY, COLON, and COMMA must have the same precedence.
852 However, there are some special cases for these in reduce(). */
853 /* QUERY */ {4, 0},
854 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
855 /* COMMA */ {4, LEFT_ASSOC},
856 /* OPEN_PAREN */ {1, NO_L_OPERAND},
857 /* CLOSE_PAREN */ {0, 0},
858 /* EOF */ {0, 0},
859 /* EQ_EQ */ {11, LEFT_ASSOC},
860 /* NOT_EQ */ {11, LEFT_ASSOC},
861 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
862 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
863 /* UPLUS */ {16, NO_L_OPERAND},
864 /* UMINUS */ {16, NO_L_OPERAND}
867 /* Parse and evaluate a C expression, reading from PFILE.
868 Returns the truth value of the expression.
870 The implementation is an operator precedence parser, i.e. a
871 bottom-up parser, using a stack for not-yet-reduced tokens.
873 The stack base is op_stack, and the current stack pointer is 'top'.
874 There is a stack element for each operator (only), and the most
875 recently pushed operator is 'top->op'. An operand (value) is
876 stored in the 'value' field of the stack element of the operator
877 that precedes it. */
878 bool
879 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
881 struct op *top = pfile->op_stack;
882 unsigned int lex_count;
883 bool saw_leading_not, want_value = true;
885 pfile->state.skip_eval = 0;
887 /* Set up detection of #if ! defined(). */
888 pfile->mi_ind_cmacro = 0;
889 saw_leading_not = false;
890 lex_count = 0;
892 /* Lowest priority operator prevents further reductions. */
893 top->op = CPP_EOF;
895 for (;;)
897 struct op op;
899 lex_count++;
900 op.token = cpp_get_token (pfile);
901 op.op = op.token->type;
902 op.loc = op.token->src_loc;
904 switch (op.op)
906 /* These tokens convert into values. */
907 case CPP_NUMBER:
908 case CPP_CHAR:
909 case CPP_WCHAR:
910 case CPP_CHAR16:
911 case CPP_CHAR32:
912 case CPP_NAME:
913 case CPP_HASH:
914 if (!want_value)
915 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
916 cpp_token_as_text (pfile, op.token));
917 want_value = false;
918 top->value = eval_token (pfile, op.token);
919 continue;
921 case CPP_NOT:
922 saw_leading_not = lex_count == 1;
923 break;
924 case CPP_PLUS:
925 if (want_value)
926 op.op = CPP_UPLUS;
927 break;
928 case CPP_MINUS:
929 if (want_value)
930 op.op = CPP_UMINUS;
931 break;
933 default:
934 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
935 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
936 cpp_token_as_text (pfile, op.token));
937 break;
940 /* Check we have a value or operator as appropriate. */
941 if (optab[op.op].flags & NO_L_OPERAND)
943 if (!want_value)
944 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
945 cpp_token_as_text (pfile, op.token));
947 else if (want_value)
949 /* We want a number (or expression) and haven't got one.
950 Try to emit a specific diagnostic. */
951 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
952 SYNTAX_ERROR ("missing expression between '(' and ')'");
954 if (op.op == CPP_EOF && top->op == CPP_EOF)
955 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
957 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
958 SYNTAX_ERROR2 ("operator '%s' has no right operand",
959 cpp_token_as_text (pfile, top->token));
960 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
961 /* Complain about missing paren during reduction. */;
962 else
963 SYNTAX_ERROR2 ("operator '%s' has no left operand",
964 cpp_token_as_text (pfile, op.token));
967 top = reduce (pfile, top, op.op);
968 if (!top)
969 goto syntax_error;
971 if (op.op == CPP_EOF)
972 break;
974 switch (op.op)
976 case CPP_CLOSE_PAREN:
977 continue;
978 case CPP_OR_OR:
979 if (!num_zerop (top->value))
980 pfile->state.skip_eval++;
981 break;
982 case CPP_AND_AND:
983 case CPP_QUERY:
984 if (num_zerop (top->value))
985 pfile->state.skip_eval++;
986 break;
987 case CPP_COLON:
988 if (top->op != CPP_QUERY)
989 SYNTAX_ERROR (" ':' without preceding '?'");
990 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
991 pfile->state.skip_eval++;
992 else
993 pfile->state.skip_eval--;
994 default:
995 break;
998 want_value = true;
1000 /* Check for and handle stack overflow. */
1001 if (++top == pfile->op_limit)
1002 top = _cpp_expand_op_stack (pfile);
1004 top->op = op.op;
1005 top->token = op.token;
1006 top->loc = op.token->src_loc;
1009 /* The controlling macro expression is only valid if we called lex 3
1010 times: <!> <defined expression> and <EOF>. push_conditional ()
1011 checks that we are at top-of-file. */
1012 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1013 pfile->mi_ind_cmacro = 0;
1015 if (top != pfile->op_stack)
1017 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1018 is_if ? "#if" : "#elif");
1019 syntax_error:
1020 return false; /* Return false on syntax error. */
1023 return !num_zerop (top->value);
1026 /* Reduce the operator / value stack if possible, in preparation for
1027 pushing operator OP. Returns NULL on error, otherwise the top of
1028 the stack. */
1029 static struct op *
1030 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1032 unsigned int prio;
1034 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1036 bad_op:
1037 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1038 return 0;
1041 if (op == CPP_OPEN_PAREN)
1042 return top;
1044 /* Decrement the priority of left-associative operators to force a
1045 reduction with operators of otherwise equal priority. */
1046 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1047 while (prio < optab[top->op].prio)
1049 if (CPP_OPTION (pfile, warn_num_sign_change)
1050 && optab[top->op].flags & CHECK_PROMOTION)
1051 check_promotion (pfile, top);
1053 switch (top->op)
1055 case CPP_UPLUS:
1056 case CPP_UMINUS:
1057 case CPP_NOT:
1058 case CPP_COMPL:
1059 top[-1].value = num_unary_op (pfile, top->value, top->op);
1060 top[-1].loc = top->loc;
1061 break;
1063 case CPP_PLUS:
1064 case CPP_MINUS:
1065 case CPP_RSHIFT:
1066 case CPP_LSHIFT:
1067 case CPP_COMMA:
1068 top[-1].value = num_binary_op (pfile, top[-1].value,
1069 top->value, top->op);
1070 top[-1].loc = top->loc;
1071 break;
1073 case CPP_GREATER:
1074 case CPP_LESS:
1075 case CPP_GREATER_EQ:
1076 case CPP_LESS_EQ:
1077 top[-1].value
1078 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1079 top[-1].loc = top->loc;
1080 break;
1082 case CPP_EQ_EQ:
1083 case CPP_NOT_EQ:
1084 top[-1].value
1085 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1086 top[-1].loc = top->loc;
1087 break;
1089 case CPP_AND:
1090 case CPP_OR:
1091 case CPP_XOR:
1092 top[-1].value
1093 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1094 top[-1].loc = top->loc;
1095 break;
1097 case CPP_MULT:
1098 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1099 top[-1].loc = top->loc;
1100 break;
1102 case CPP_DIV:
1103 case CPP_MOD:
1104 top[-1].value = num_div_op (pfile, top[-1].value,
1105 top->value, top->op);
1106 top[-1].loc = top->loc;
1107 break;
1109 case CPP_OR_OR:
1110 top--;
1111 if (!num_zerop (top->value))
1112 pfile->state.skip_eval--;
1113 top->value.low = (!num_zerop (top->value)
1114 || !num_zerop (top[1].value));
1115 top->value.high = 0;
1116 top->value.unsignedp = false;
1117 top->value.overflow = false;
1118 top->loc = top[1].loc;
1119 continue;
1121 case CPP_AND_AND:
1122 top--;
1123 if (num_zerop (top->value))
1124 pfile->state.skip_eval--;
1125 top->value.low = (!num_zerop (top->value)
1126 && !num_zerop (top[1].value));
1127 top->value.high = 0;
1128 top->value.unsignedp = false;
1129 top->value.overflow = false;
1130 top->loc = top[1].loc;
1131 continue;
1133 case CPP_OPEN_PAREN:
1134 if (op != CPP_CLOSE_PAREN)
1136 cpp_error_with_line (pfile, CPP_DL_ERROR,
1137 top->token->src_loc,
1138 0, "missing ')' in expression");
1139 return 0;
1141 top--;
1142 top->value = top[1].value;
1143 top->loc = top[1].loc;
1144 return top;
1146 case CPP_COLON:
1147 top -= 2;
1148 if (!num_zerop (top->value))
1150 pfile->state.skip_eval--;
1151 top->value = top[1].value;
1152 top->loc = top[1].loc;
1154 else
1156 top->value = top[2].value;
1157 top->loc = top[2].loc;
1159 top->value.unsignedp = (top[1].value.unsignedp
1160 || top[2].value.unsignedp);
1161 continue;
1163 case CPP_QUERY:
1164 /* COMMA and COLON should not reduce a QUERY operator. */
1165 if (op == CPP_COMMA || op == CPP_COLON)
1166 return top;
1167 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1168 return 0;
1170 default:
1171 goto bad_op;
1174 top--;
1175 if (top->value.overflow && !pfile->state.skip_eval)
1176 cpp_error (pfile, CPP_DL_PEDWARN,
1177 "integer overflow in preprocessor expression");
1180 if (op == CPP_CLOSE_PAREN)
1182 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1183 return 0;
1186 return top;
1189 /* Returns the position of the old top of stack after expansion. */
1190 struct op *
1191 _cpp_expand_op_stack (cpp_reader *pfile)
1193 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1194 size_t new_size = old_size * 2 + 20;
1196 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1197 pfile->op_limit = pfile->op_stack + new_size;
1199 return pfile->op_stack + old_size;
1202 /* Emits a warning if the effective sign of either operand of OP
1203 changes because of integer promotions. */
1204 static void
1205 check_promotion (cpp_reader *pfile, const struct op *op)
1207 if (op->value.unsignedp == op[-1].value.unsignedp)
1208 return;
1210 if (op->value.unsignedp)
1212 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1213 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1214 "the left operand of \"%s\" changes sign when promoted",
1215 cpp_token_as_text (pfile, op->token));
1217 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1218 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1219 "the right operand of \"%s\" changes sign when promoted",
1220 cpp_token_as_text (pfile, op->token));
1223 /* Clears the unused high order bits of the number pointed to by PNUM. */
1224 static cpp_num
1225 num_trim (cpp_num num, size_t precision)
1227 if (precision > PART_PRECISION)
1229 precision -= PART_PRECISION;
1230 if (precision < PART_PRECISION)
1231 num.high &= ((cpp_num_part) 1 << precision) - 1;
1233 else
1235 if (precision < PART_PRECISION)
1236 num.low &= ((cpp_num_part) 1 << precision) - 1;
1237 num.high = 0;
1240 return num;
1243 /* True iff A (presumed signed) >= 0. */
1244 static bool
1245 num_positive (cpp_num num, size_t precision)
1247 if (precision > PART_PRECISION)
1249 precision -= PART_PRECISION;
1250 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1253 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1256 /* Sign extend a number, with PRECISION significant bits and all
1257 others assumed clear, to fill out a cpp_num structure. */
1258 cpp_num
1259 cpp_num_sign_extend (cpp_num num, size_t precision)
1261 if (!num.unsignedp)
1263 if (precision > PART_PRECISION)
1265 precision -= PART_PRECISION;
1266 if (precision < PART_PRECISION
1267 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1268 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1270 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1272 if (precision < PART_PRECISION)
1273 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1274 num.high = ~(cpp_num_part) 0;
1278 return num;
1281 /* Returns the negative of NUM. */
1282 static cpp_num
1283 num_negate (cpp_num num, size_t precision)
1285 cpp_num copy;
1287 copy = num;
1288 num.high = ~num.high;
1289 num.low = ~num.low;
1290 if (++num.low == 0)
1291 num.high++;
1292 num = num_trim (num, precision);
1293 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1295 return num;
1298 /* Returns true if A >= B. */
1299 static bool
1300 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1302 bool unsignedp;
1304 unsignedp = pa.unsignedp || pb.unsignedp;
1306 if (!unsignedp)
1308 /* Both numbers have signed type. If they are of different
1309 sign, the answer is the sign of A. */
1310 unsignedp = num_positive (pa, precision);
1312 if (unsignedp != num_positive (pb, precision))
1313 return unsignedp;
1315 /* Otherwise we can do an unsigned comparison. */
1318 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1321 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1322 static cpp_num
1323 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1324 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1326 lhs.overflow = false;
1327 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1329 /* As excess precision is zeroed, there is no need to num_trim () as
1330 these operations cannot introduce a set bit there. */
1331 if (op == CPP_AND)
1333 lhs.low &= rhs.low;
1334 lhs.high &= rhs.high;
1336 else if (op == CPP_OR)
1338 lhs.low |= rhs.low;
1339 lhs.high |= rhs.high;
1341 else
1343 lhs.low ^= rhs.low;
1344 lhs.high ^= rhs.high;
1347 return lhs;
1350 /* Returns LHS OP RHS, where OP is an inequality. */
1351 static cpp_num
1352 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1353 enum cpp_ttype op)
1355 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1357 if (op == CPP_GREATER_EQ)
1358 lhs.low = gte;
1359 else if (op == CPP_LESS)
1360 lhs.low = !gte;
1361 else if (op == CPP_GREATER)
1362 lhs.low = gte && !num_eq (lhs, rhs);
1363 else /* CPP_LESS_EQ. */
1364 lhs.low = !gte || num_eq (lhs, rhs);
1366 lhs.high = 0;
1367 lhs.overflow = false;
1368 lhs.unsignedp = false;
1369 return lhs;
1372 /* Returns LHS OP RHS, where OP is == or !=. */
1373 static cpp_num
1374 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1375 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1377 /* Work around a 3.0.4 bug; see PR 6950. */
1378 bool eq = num_eq (lhs, rhs);
1379 if (op == CPP_NOT_EQ)
1380 eq = !eq;
1381 lhs.low = eq;
1382 lhs.high = 0;
1383 lhs.overflow = false;
1384 lhs.unsignedp = false;
1385 return lhs;
1388 /* Shift NUM, of width PRECISION, right by N bits. */
1389 static cpp_num
1390 num_rshift (cpp_num num, size_t precision, size_t n)
1392 cpp_num_part sign_mask;
1393 bool x = num_positive (num, precision);
1395 if (num.unsignedp || x)
1396 sign_mask = 0;
1397 else
1398 sign_mask = ~(cpp_num_part) 0;
1400 if (n >= precision)
1401 num.high = num.low = sign_mask;
1402 else
1404 /* Sign-extend. */
1405 if (precision < PART_PRECISION)
1406 num.high = sign_mask, num.low |= sign_mask << precision;
1407 else if (precision < 2 * PART_PRECISION)
1408 num.high |= sign_mask << (precision - PART_PRECISION);
1410 if (n >= PART_PRECISION)
1412 n -= PART_PRECISION;
1413 num.low = num.high;
1414 num.high = sign_mask;
1417 if (n)
1419 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1420 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1424 num = num_trim (num, precision);
1425 num.overflow = false;
1426 return num;
1429 /* Shift NUM, of width PRECISION, left by N bits. */
1430 static cpp_num
1431 num_lshift (cpp_num num, size_t precision, size_t n)
1433 if (n >= precision)
1435 num.overflow = !num.unsignedp && !num_zerop (num);
1436 num.high = num.low = 0;
1438 else
1440 cpp_num orig, maybe_orig;
1441 size_t m = n;
1443 orig = num;
1444 if (m >= PART_PRECISION)
1446 m -= PART_PRECISION;
1447 num.high = num.low;
1448 num.low = 0;
1450 if (m)
1452 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1453 num.low <<= m;
1455 num = num_trim (num, precision);
1457 if (num.unsignedp)
1458 num.overflow = false;
1459 else
1461 maybe_orig = num_rshift (num, precision, n);
1462 num.overflow = !num_eq (orig, maybe_orig);
1466 return num;
1469 /* The four unary operators: +, -, ! and ~. */
1470 static cpp_num
1471 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1473 switch (op)
1475 case CPP_UPLUS:
1476 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1477 cpp_error (pfile, CPP_DL_WARNING,
1478 "traditional C rejects the unary plus operator");
1479 num.overflow = false;
1480 break;
1482 case CPP_UMINUS:
1483 num = num_negate (num, CPP_OPTION (pfile, precision));
1484 break;
1486 case CPP_COMPL:
1487 num.high = ~num.high;
1488 num.low = ~num.low;
1489 num = num_trim (num, CPP_OPTION (pfile, precision));
1490 num.overflow = false;
1491 break;
1493 default: /* case CPP_NOT: */
1494 num.low = num_zerop (num);
1495 num.high = 0;
1496 num.overflow = false;
1497 num.unsignedp = false;
1498 break;
1501 return num;
1504 /* The various binary operators. */
1505 static cpp_num
1506 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1508 cpp_num result;
1509 size_t precision = CPP_OPTION (pfile, precision);
1510 size_t n;
1512 switch (op)
1514 /* Shifts. */
1515 case CPP_LSHIFT:
1516 case CPP_RSHIFT:
1517 if (!rhs.unsignedp && !num_positive (rhs, precision))
1519 /* A negative shift is a positive shift the other way. */
1520 if (op == CPP_LSHIFT)
1521 op = CPP_RSHIFT;
1522 else
1523 op = CPP_LSHIFT;
1524 rhs = num_negate (rhs, precision);
1526 if (rhs.high)
1527 n = ~0; /* Maximal. */
1528 else
1529 n = rhs.low;
1530 if (op == CPP_LSHIFT)
1531 lhs = num_lshift (lhs, precision, n);
1532 else
1533 lhs = num_rshift (lhs, precision, n);
1534 break;
1536 /* Arithmetic. */
1537 case CPP_MINUS:
1538 rhs = num_negate (rhs, precision);
1539 case CPP_PLUS:
1540 result.low = lhs.low + rhs.low;
1541 result.high = lhs.high + rhs.high;
1542 if (result.low < lhs.low)
1543 result.high++;
1544 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1545 result.overflow = false;
1547 result = num_trim (result, precision);
1548 if (!result.unsignedp)
1550 bool lhsp = num_positive (lhs, precision);
1551 result.overflow = (lhsp == num_positive (rhs, precision)
1552 && lhsp != num_positive (result, precision));
1554 return result;
1556 /* Comma. */
1557 default: /* case CPP_COMMA: */
1558 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1559 || !pfile->state.skip_eval))
1560 cpp_error (pfile, CPP_DL_PEDWARN,
1561 "comma operator in operand of #if");
1562 lhs = rhs;
1563 break;
1566 return lhs;
1569 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1570 cannot overflow. */
1571 static cpp_num
1572 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1574 cpp_num result;
1575 cpp_num_part middle[2], temp;
1577 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1578 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1580 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1581 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1583 temp = result.low;
1584 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1585 if (result.low < temp)
1586 result.high++;
1588 temp = result.low;
1589 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1590 if (result.low < temp)
1591 result.high++;
1593 result.high += HIGH_PART (middle[0]);
1594 result.high += HIGH_PART (middle[1]);
1595 result.unsignedp = true;
1596 result.overflow = false;
1598 return result;
1601 /* Multiply two preprocessing numbers. */
1602 static cpp_num
1603 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1605 cpp_num result, temp;
1606 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1607 bool overflow, negate = false;
1608 size_t precision = CPP_OPTION (pfile, precision);
1610 /* Prepare for unsigned multiplication. */
1611 if (!unsignedp)
1613 if (!num_positive (lhs, precision))
1614 negate = !negate, lhs = num_negate (lhs, precision);
1615 if (!num_positive (rhs, precision))
1616 negate = !negate, rhs = num_negate (rhs, precision);
1619 overflow = lhs.high && rhs.high;
1620 result = num_part_mul (lhs.low, rhs.low);
1622 temp = num_part_mul (lhs.high, rhs.low);
1623 result.high += temp.low;
1624 if (temp.high)
1625 overflow = true;
1627 temp = num_part_mul (lhs.low, rhs.high);
1628 result.high += temp.low;
1629 if (temp.high)
1630 overflow = true;
1632 temp.low = result.low, temp.high = result.high;
1633 result = num_trim (result, precision);
1634 if (!num_eq (result, temp))
1635 overflow = true;
1637 if (negate)
1638 result = num_negate (result, precision);
1640 if (unsignedp)
1641 result.overflow = false;
1642 else
1643 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1644 && !num_zerop (result));
1645 result.unsignedp = unsignedp;
1647 return result;
1650 /* Divide two preprocessing numbers, returning the answer or the
1651 remainder depending upon OP. */
1652 static cpp_num
1653 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1655 cpp_num result, sub;
1656 cpp_num_part mask;
1657 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1658 bool negate = false, lhs_neg = false;
1659 size_t i, precision = CPP_OPTION (pfile, precision);
1661 /* Prepare for unsigned division. */
1662 if (!unsignedp)
1664 if (!num_positive (lhs, precision))
1665 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1666 if (!num_positive (rhs, precision))
1667 negate = !negate, rhs = num_negate (rhs, precision);
1670 /* Find the high bit. */
1671 if (rhs.high)
1673 i = precision - 1;
1674 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1675 for (; ; i--, mask >>= 1)
1676 if (rhs.high & mask)
1677 break;
1679 else if (rhs.low)
1681 if (precision > PART_PRECISION)
1682 i = precision - PART_PRECISION - 1;
1683 else
1684 i = precision - 1;
1685 mask = (cpp_num_part) 1 << i;
1686 for (; ; i--, mask >>= 1)
1687 if (rhs.low & mask)
1688 break;
1690 else
1692 if (!pfile->state.skip_eval)
1693 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1694 return lhs;
1697 /* First nonzero bit of RHS is bit I. Do naive division by
1698 shifting the RHS fully left, and subtracting from LHS if LHS is
1699 at least as big, and then repeating but with one less shift.
1700 This is not very efficient, but is easy to understand. */
1702 rhs.unsignedp = true;
1703 lhs.unsignedp = true;
1704 i = precision - i - 1;
1705 sub = num_lshift (rhs, precision, i);
1707 result.high = result.low = 0;
1708 for (;;)
1710 if (num_greater_eq (lhs, sub, precision))
1712 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1713 if (i >= PART_PRECISION)
1714 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1715 else
1716 result.low |= (cpp_num_part) 1 << i;
1718 if (i-- == 0)
1719 break;
1720 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1721 sub.high >>= 1;
1724 /* We divide so that the remainder has the sign of the LHS. */
1725 if (op == CPP_DIV)
1727 result.unsignedp = unsignedp;
1728 result.overflow = false;
1729 if (!unsignedp)
1731 if (negate)
1732 result = num_negate (result, precision);
1733 result.overflow = (num_positive (result, precision) ^ !negate
1734 && !num_zerop (result));
1737 return result;
1740 /* CPP_MOD. */
1741 lhs.unsignedp = unsignedp;
1742 lhs.overflow = false;
1743 if (lhs_neg)
1744 lhs = num_negate (lhs, precision);
1746 return lhs;