1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004 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 2, or (at your option) any
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; if not, write to the Free Software
18 Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
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))
33 const cpp_token *token; /* The token forming op (for diagnostics). */
34 cpp_num value; /* The value logically "right" of 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,
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
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; } \
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. */
83 interpret_float_suffix (const uchar *s, size_t len)
85 size_t f = 0, l = 0, i = 0, d = 0;
101 case 'j': case 'J': i++; break;
102 case 'd': case 'D': d++; break;
107 if (f + l > 1 || i > 1)
110 /* Allow dd, df, dl suffixes for decimal float constants. */
111 if (d && ((d + f + l != 2) || i))
114 return ((i ? CPP_N_IMAGINARY : 0)
116 l ? CPP_N_LARGE : CPP_N_MEDIUM)
117 | (d ? CPP_N_DFLOAT : 0));
120 /* Subroutine of cpp_classify_number. S points to an integer suffix
121 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
122 flag vector describing the suffix. */
124 interpret_int_suffix (const uchar *s, size_t len)
133 case 'u': case 'U': u++; break;
135 case 'j': case 'J': i++; break;
136 case 'l': case 'L': l++;
137 /* If there are two Ls, they must be adjacent and the same case. */
138 if (l == 2 && s[len] != s[len + 1])
145 if (l > 2 || u > 1 || i > 1)
148 return ((i ? CPP_N_IMAGINARY : 0)
149 | (u ? CPP_N_UNSIGNED : 0)
150 | ((l == 0) ? CPP_N_SMALL
151 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
154 /* Categorize numeric constants according to their field (integer,
155 floating point, or invalid), radix (decimal, octal, hexadecimal),
156 and type suffixes. */
158 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
160 const uchar *str = token->val.str.text;
162 unsigned int max_digit, result, radix;
163 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
165 /* If the lexer has done its job, length one can only be a single
166 digit. Fast-path this very common case. */
167 if (token->val.str.len == 1)
168 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
170 limit = str + token->val.str.len;
171 float_flag = NOT_FLOAT;
175 /* First, interpret the radix. */
181 /* Require at least one hex digit to classify it as hex. */
182 if ((*str == 'x' || *str == 'X')
183 && (str[1] == '.' || ISXDIGIT (str[1])))
190 /* Now scan for a well-formed integer or float. */
193 unsigned int c = *str++;
195 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
203 if (float_flag == NOT_FLOAT)
204 float_flag = AFTER_POINT;
206 SYNTAX_ERROR ("too many decimal points in number");
208 else if ((radix <= 10 && (c == 'e' || c == 'E'))
209 || (radix == 16 && (c == 'p' || c == 'P')))
211 float_flag = AFTER_EXPON;
216 /* Start of suffix. */
222 if (float_flag != NOT_FLOAT && radix == 8)
225 if (max_digit >= radix)
226 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
228 if (float_flag != NOT_FLOAT)
230 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
231 cpp_error (pfile, CPP_DL_PEDWARN,
232 "use of C99 hexadecimal floating constant");
234 if (float_flag == AFTER_EXPON)
236 if (*str == '+' || *str == '-')
239 /* Exponent is decimal, even if string is a hex float. */
241 SYNTAX_ERROR ("exponent has no digits");
245 while (ISDIGIT (*str));
247 else if (radix == 16)
248 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
250 result = interpret_float_suffix (str, limit - str);
253 cpp_error (pfile, CPP_DL_ERROR,
254 "invalid suffix \"%.*s\" on floating constant",
255 (int) (limit - str), str);
256 return CPP_N_INVALID;
259 /* Traditional C didn't accept any floating suffixes. */
261 && CPP_WTRADITIONAL (pfile)
262 && ! cpp_sys_macro_p (pfile))
263 cpp_error (pfile, CPP_DL_WARNING,
264 "traditional C rejects the \"%.*s\" suffix",
265 (int) (limit - str), str);
267 /* Radix must be 10 for decimal floats. */
268 if ((result & CPP_N_DFLOAT) && radix != 10)
270 cpp_error (pfile, CPP_DL_ERROR,
271 "invalid suffix \"%.*s\" with hexadecimal floating constant",
272 (int) (limit - str), str);
273 return CPP_N_INVALID;
276 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
277 cpp_error (pfile, CPP_DL_PEDWARN,
278 "decimal float constants are a GCC extension");
280 result |= CPP_N_FLOATING;
284 result = interpret_int_suffix (str, limit - str);
287 cpp_error (pfile, CPP_DL_ERROR,
288 "invalid suffix \"%.*s\" on integer constant",
289 (int) (limit - str), str);
290 return CPP_N_INVALID;
293 /* Traditional C only accepted the 'L' suffix.
294 Suppress warning about 'LL' with -Wno-long-long. */
295 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
297 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
298 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
300 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
301 cpp_error (pfile, CPP_DL_WARNING,
302 "traditional C rejects the \"%.*s\" suffix",
303 (int) (limit - str), str);
306 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
307 && ! CPP_OPTION (pfile, c99)
308 && CPP_OPTION (pfile, warn_long_long))
309 cpp_error (pfile, CPP_DL_PEDWARN,
310 "use of C99 long long integer constant");
312 result |= CPP_N_INTEGER;
315 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
316 cpp_error (pfile, CPP_DL_PEDWARN,
317 "imaginary constants are a GCC extension");
320 result |= CPP_N_DECIMAL;
321 else if (radix == 16)
324 result |= CPP_N_OCTAL;
329 return CPP_N_INVALID;
332 /* cpp_interpret_integer converts an integer constant into a cpp_num,
333 of precision options->precision.
335 We do not provide any interface for decimal->float conversion,
336 because the preprocessor doesn't need it and we don't want to
337 drag in GCC's floating point emulator. */
339 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
342 const uchar *p, *end;
347 result.unsignedp = !!(type & CPP_N_UNSIGNED);
348 result.overflow = false;
350 p = token->val.str.text;
351 end = p + token->val.str.len;
353 /* Common case of a single digit. */
354 if (token->val.str.len == 1)
355 result.low = p[0] - '0';
359 size_t precision = CPP_OPTION (pfile, precision);
360 unsigned int base = 10, c = 0;
361 bool overflow = false;
363 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
368 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
374 /* We can add a digit to numbers strictly less than this without
375 needing the precision and slowness of double integers. */
376 max = ~(cpp_num_part) 0;
377 if (precision < PART_PRECISION)
378 max >>= PART_PRECISION - precision;
379 max = (max - base + 1) / base + 1;
385 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
390 /* Strict inequality for when max is set to zero. */
391 if (result.low < max)
392 result.low = result.low * base + c;
395 result = append_digit (result, c, base, precision);
396 overflow |= result.overflow;
402 cpp_error (pfile, CPP_DL_PEDWARN,
403 "integer constant is too large for its type");
404 /* If too big to be signed, consider it unsigned. Only warn for
405 decimal numbers. Traditional numbers were always signed (but
406 we still honor an explicit U suffix); but we only have
407 traditional semantics in directives. */
408 else if (!result.unsignedp
409 && !(CPP_OPTION (pfile, traditional)
410 && pfile->state.in_directive)
411 && !num_positive (result, precision))
414 cpp_error (pfile, CPP_DL_WARNING,
415 "integer constant is so large that it is unsigned");
416 result.unsignedp = true;
423 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
425 append_digit (cpp_num num, int digit, int base, size_t precision)
428 unsigned int shift = 3 + (base == 16);
430 cpp_num_part add_high, add_low;
432 /* Multiply by 8 or 16. Catching this overflow here means we don't
433 need to worry about add_high overflowing. */
434 overflow = !!(num.high >> (PART_PRECISION - shift));
435 result.high = num.high << shift;
436 result.low = num.low << shift;
437 result.high |= num.low >> (PART_PRECISION - shift);
438 result.unsignedp = num.unsignedp;
442 add_low = num.low << 1;
443 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
446 add_high = add_low = 0;
448 if (add_low + digit < add_low)
452 if (result.low + add_low < result.low)
454 if (result.high + add_high < result.high)
457 result.low += add_low;
458 result.high += add_high;
459 result.overflow = overflow;
461 /* The above code catches overflow of a cpp_num type. This catches
462 overflow of the (possibly shorter) target precision. */
463 num.low = result.low;
464 num.high = result.high;
465 result = num_trim (result, precision);
466 if (!num_eq (result, num))
467 result.overflow = true;
472 /* Handle meeting "defined" in a preprocessor expression. */
474 parse_defined (cpp_reader *pfile)
478 cpp_hashnode *node = 0;
479 const cpp_token *token;
480 cpp_context *initial_context = pfile->context;
482 /* Don't expand macros. */
483 pfile->state.prevent_expansion++;
485 token = cpp_get_token (pfile);
486 if (token->type == CPP_OPEN_PAREN)
489 token = cpp_get_token (pfile);
492 if (token->type == CPP_NAME)
494 node = token->val.node;
495 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
497 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
503 cpp_error (pfile, CPP_DL_ERROR,
504 "operator \"defined\" requires an identifier");
505 if (token->flags & NAMED_OP)
510 op.type = token->type;
511 cpp_error (pfile, CPP_DL_ERROR,
512 "(\"%s\" is an alternative token for \"%s\" in C++)",
513 cpp_token_as_text (pfile, token),
514 cpp_token_as_text (pfile, &op));
520 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
521 cpp_error (pfile, CPP_DL_WARNING,
522 "this use of \"defined\" may not be portable");
524 _cpp_mark_macro_used (node);
526 /* A possible controlling macro of the form #if !defined ().
527 _cpp_parse_expr checks there was no other junk on the line. */
528 pfile->mi_ind_cmacro = node;
531 pfile->state.prevent_expansion--;
533 result.unsignedp = false;
535 result.overflow = false;
536 result.low = node && node->type == NT_MACRO;
540 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
541 number or character constant, or the result of the "defined" or "#"
544 eval_token (cpp_reader *pfile, const cpp_token *token)
550 result.unsignedp = false;
551 result.overflow = false;
556 temp = cpp_classify_number (pfile, token);
557 switch (temp & CPP_N_CATEGORY)
560 cpp_error (pfile, CPP_DL_ERROR,
561 "floating constant in preprocessor expression");
564 if (!(temp & CPP_N_IMAGINARY))
565 return cpp_interpret_integer (pfile, token, temp);
566 cpp_error (pfile, CPP_DL_ERROR,
567 "imaginary number in preprocessor expression");
571 /* Error already issued. */
574 result.high = result.low = 0;
580 cppchar_t cc = cpp_interpret_charconst (pfile, token,
585 /* Sign-extend the result if necessary. */
586 if (!unsignedp && (cppchar_signed_t) cc < 0)
588 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
589 result.low |= ~(~(cpp_num_part) 0
590 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
591 result.high = ~(cpp_num_part) 0;
592 result = num_trim (result, CPP_OPTION (pfile, precision));
598 if (token->val.node == pfile->spec_nodes.n_defined)
599 return parse_defined (pfile);
600 else if (CPP_OPTION (pfile, cplusplus)
601 && (token->val.node == pfile->spec_nodes.n_true
602 || token->val.node == pfile->spec_nodes.n_false))
605 result.low = (token->val.node == pfile->spec_nodes.n_true);
611 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
612 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
613 NODE_NAME (token->val.node));
617 default: /* CPP_HASH */
618 _cpp_test_assertion (pfile, &temp);
623 result.unsignedp = !!unsignedp;
627 /* Operator precedence and flags table.
629 After an operator is returned from the lexer, if it has priority less
630 than the operator on the top of the stack, we reduce the stack by one
631 operator and repeat the test. Since equal priorities do not reduce,
632 this is naturally right-associative.
634 We handle left-associative operators by decrementing the priority of
635 just-lexed operators by one, but retaining the priority of operators
636 already on the stack.
638 The remaining cases are '(' and ')'. We handle '(' by skipping the
639 reduction phase completely. ')' is given lower priority than
640 everything else, including '(', effectively forcing a reduction of the
641 parenthesized expression. If there is a matching '(', the routine
642 reduce() exits immediately. If the normal exit route sees a ')', then
643 there cannot have been a matching '(' and an error message is output.
645 The parser assumes all shifted operators require a left operand unless
646 the flag NO_L_OPERAND is set. These semantics are automatic; any
647 extra semantics need to be handled with operator-specific code. */
649 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
650 operand changes because of integer promotions. */
651 #define NO_L_OPERAND (1 << 0)
652 #define LEFT_ASSOC (1 << 1)
653 #define CHECK_PROMOTION (1 << 2)
655 /* Operator to priority map. Must be in the same order as the first
656 N entries of enum cpp_ttype. */
657 static const struct cpp_operator
663 /* EQ */ {0, 0}, /* Shouldn't happen. */
664 /* NOT */ {16, NO_L_OPERAND},
665 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
666 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
667 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
668 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
669 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
670 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
671 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
672 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
673 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
674 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
675 /* RSHIFT */ {13, LEFT_ASSOC},
676 /* LSHIFT */ {13, LEFT_ASSOC},
678 /* COMPL */ {16, NO_L_OPERAND},
679 /* AND_AND */ {6, LEFT_ASSOC},
680 /* OR_OR */ {5, LEFT_ASSOC},
682 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
683 /* COMMA */ {2, LEFT_ASSOC},
684 /* OPEN_PAREN */ {1, NO_L_OPERAND},
685 /* CLOSE_PAREN */ {0, 0},
687 /* EQ_EQ */ {11, LEFT_ASSOC},
688 /* NOT_EQ */ {11, LEFT_ASSOC},
689 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
690 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
691 /* UPLUS */ {16, NO_L_OPERAND},
692 /* UMINUS */ {16, NO_L_OPERAND}
695 /* Parse and evaluate a C expression, reading from PFILE.
696 Returns the truth value of the expression.
698 The implementation is an operator precedence parser, i.e. a
699 bottom-up parser, using a stack for not-yet-reduced tokens.
701 The stack base is op_stack, and the current stack pointer is 'top'.
702 There is a stack element for each operator (only), and the most
703 recently pushed operator is 'top->op'. An operand (value) is
704 stored in the 'value' field of the stack element of the operator
707 _cpp_parse_expr (cpp_reader *pfile)
709 struct op *top = pfile->op_stack;
710 unsigned int lex_count;
711 bool saw_leading_not, want_value = true;
713 pfile->state.skip_eval = 0;
715 /* Set up detection of #if ! defined(). */
716 pfile->mi_ind_cmacro = 0;
717 saw_leading_not = false;
720 /* Lowest priority operator prevents further reductions. */
728 op.token = cpp_get_token (pfile);
729 op.op = op.token->type;
733 /* These tokens convert into values. */
740 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
741 cpp_token_as_text (pfile, op.token));
743 top->value = eval_token (pfile, op.token);
747 saw_leading_not = lex_count == 1;
759 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
760 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
761 cpp_token_as_text (pfile, op.token));
765 /* Check we have a value or operator as appropriate. */
766 if (optab[op.op].flags & NO_L_OPERAND)
769 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
770 cpp_token_as_text (pfile, op.token));
774 /* We want a number (or expression) and haven't got one.
775 Try to emit a specific diagnostic. */
776 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
777 SYNTAX_ERROR ("missing expression between '(' and ')'");
779 if (op.op == CPP_EOF && top->op == CPP_EOF)
780 SYNTAX_ERROR ("#if with no expression");
782 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
783 SYNTAX_ERROR2 ("operator '%s' has no right operand",
784 cpp_token_as_text (pfile, top->token));
785 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
786 /* Complain about missing paren during reduction. */;
788 SYNTAX_ERROR2 ("operator '%s' has no left operand",
789 cpp_token_as_text (pfile, op.token));
792 top = reduce (pfile, top, op.op);
796 if (op.op == CPP_EOF)
801 case CPP_CLOSE_PAREN:
804 if (!num_zerop (top->value))
805 pfile->state.skip_eval++;
809 if (num_zerop (top->value))
810 pfile->state.skip_eval++;
813 if (top->op != CPP_QUERY)
814 SYNTAX_ERROR (" ':' without preceding '?'");
815 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
816 pfile->state.skip_eval++;
818 pfile->state.skip_eval--;
825 /* Check for and handle stack overflow. */
826 if (++top == pfile->op_limit)
827 top = _cpp_expand_op_stack (pfile);
830 top->token = op.token;
833 /* The controlling macro expression is only valid if we called lex 3
834 times: <!> <defined expression> and <EOF>. push_conditional ()
835 checks that we are at top-of-file. */
836 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
837 pfile->mi_ind_cmacro = 0;
839 if (top != pfile->op_stack)
841 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
843 return false; /* Return false on syntax error. */
846 return !num_zerop (top->value);
849 /* Reduce the operator / value stack if possible, in preparation for
850 pushing operator OP. Returns NULL on error, otherwise the top of
853 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
857 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
860 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
864 if (op == CPP_OPEN_PAREN)
867 /* Decrement the priority of left-associative operators to force a
868 reduction with operators of otherwise equal priority. */
869 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
870 while (prio < optab[top->op].prio)
872 if (CPP_OPTION (pfile, warn_num_sign_change)
873 && optab[top->op].flags & CHECK_PROMOTION)
874 check_promotion (pfile, top);
882 top[-1].value = num_unary_op (pfile, top->value, top->op);
890 top[-1].value = num_binary_op (pfile, top[-1].value,
891 top->value, top->op);
899 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
905 = num_equality_op (pfile, top[-1].value, top->value, top->op);
912 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
916 top[-1].value = num_mul (pfile, top[-1].value, top->value);
921 top[-1].value = num_div_op (pfile, top[-1].value,
922 top->value, top->op);
927 if (!num_zerop (top->value))
928 pfile->state.skip_eval--;
929 top->value.low = (!num_zerop (top->value)
930 || !num_zerop (top[1].value));
932 top->value.unsignedp = false;
933 top->value.overflow = false;
938 if (num_zerop (top->value))
939 pfile->state.skip_eval--;
940 top->value.low = (!num_zerop (top->value)
941 && !num_zerop (top[1].value));
943 top->value.unsignedp = false;
944 top->value.overflow = false;
948 if (op != CPP_CLOSE_PAREN)
950 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
954 top->value = top[1].value;
959 if (!num_zerop (top->value))
961 pfile->state.skip_eval--;
962 top->value = top[1].value;
965 top->value = top[2].value;
966 top->value.unsignedp = (top[1].value.unsignedp
967 || top[2].value.unsignedp);
971 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
979 if (top->value.overflow && !pfile->state.skip_eval)
980 cpp_error (pfile, CPP_DL_PEDWARN,
981 "integer overflow in preprocessor expression");
984 if (op == CPP_CLOSE_PAREN)
986 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
993 /* Returns the position of the old top of stack after expansion. */
995 _cpp_expand_op_stack (cpp_reader *pfile)
997 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
998 size_t new_size = old_size * 2 + 20;
1000 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1001 pfile->op_limit = pfile->op_stack + new_size;
1003 return pfile->op_stack + old_size;
1006 /* Emits a warning if the effective sign of either operand of OP
1007 changes because of integer promotions. */
1009 check_promotion (cpp_reader *pfile, const struct op *op)
1011 if (op->value.unsignedp == op[-1].value.unsignedp)
1014 if (op->value.unsignedp)
1016 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1017 cpp_error (pfile, CPP_DL_WARNING,
1018 "the left operand of \"%s\" changes sign when promoted",
1019 cpp_token_as_text (pfile, op->token));
1021 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1022 cpp_error (pfile, CPP_DL_WARNING,
1023 "the right operand of \"%s\" changes sign when promoted",
1024 cpp_token_as_text (pfile, op->token));
1027 /* Clears the unused high order bits of the number pointed to by PNUM. */
1029 num_trim (cpp_num num, size_t precision)
1031 if (precision > PART_PRECISION)
1033 precision -= PART_PRECISION;
1034 if (precision < PART_PRECISION)
1035 num.high &= ((cpp_num_part) 1 << precision) - 1;
1039 if (precision < PART_PRECISION)
1040 num.low &= ((cpp_num_part) 1 << precision) - 1;
1047 /* True iff A (presumed signed) >= 0. */
1049 num_positive (cpp_num num, size_t precision)
1051 if (precision > PART_PRECISION)
1053 precision -= PART_PRECISION;
1054 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1057 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1060 /* Sign extend a number, with PRECISION significant bits and all
1061 others assumed clear, to fill out a cpp_num structure. */
1063 cpp_num_sign_extend (cpp_num num, size_t precision)
1067 if (precision > PART_PRECISION)
1069 precision -= PART_PRECISION;
1070 if (precision < PART_PRECISION
1071 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1072 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1074 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1076 if (precision < PART_PRECISION)
1077 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1078 num.high = ~(cpp_num_part) 0;
1085 /* Returns the negative of NUM. */
1087 num_negate (cpp_num num, size_t precision)
1092 num.high = ~num.high;
1096 num = num_trim (num, precision);
1097 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1102 /* Returns true if A >= B. */
1104 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1108 unsignedp = pa.unsignedp || pb.unsignedp;
1112 /* Both numbers have signed type. If they are of different
1113 sign, the answer is the sign of A. */
1114 unsignedp = num_positive (pa, precision);
1116 if (unsignedp != num_positive (pb, precision))
1119 /* Otherwise we can do an unsigned comparison. */
1122 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1125 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1127 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1128 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1130 lhs.overflow = false;
1131 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1133 /* As excess precision is zeroed, there is no need to num_trim () as
1134 these operations cannot introduce a set bit there. */
1138 lhs.high &= rhs.high;
1140 else if (op == CPP_OR)
1143 lhs.high |= rhs.high;
1148 lhs.high ^= rhs.high;
1154 /* Returns LHS OP RHS, where OP is an inequality. */
1156 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1159 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1161 if (op == CPP_GREATER_EQ)
1163 else if (op == CPP_LESS)
1165 else if (op == CPP_GREATER)
1166 lhs.low = gte && !num_eq (lhs, rhs);
1167 else /* CPP_LESS_EQ. */
1168 lhs.low = !gte || num_eq (lhs, rhs);
1171 lhs.overflow = false;
1172 lhs.unsignedp = false;
1176 /* Returns LHS OP RHS, where OP is == or !=. */
1178 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1179 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1181 /* Work around a 3.0.4 bug; see PR 6950. */
1182 bool eq = num_eq (lhs, rhs);
1183 if (op == CPP_NOT_EQ)
1187 lhs.overflow = false;
1188 lhs.unsignedp = false;
1192 /* Shift NUM, of width PRECISION, right by N bits. */
1194 num_rshift (cpp_num num, size_t precision, size_t n)
1196 cpp_num_part sign_mask;
1197 bool x = num_positive (num, precision);
1199 if (num.unsignedp || x)
1202 sign_mask = ~(cpp_num_part) 0;
1205 num.high = num.low = sign_mask;
1209 if (precision < PART_PRECISION)
1210 num.high = sign_mask, num.low |= sign_mask << precision;
1211 else if (precision < 2 * PART_PRECISION)
1212 num.high |= sign_mask << (precision - PART_PRECISION);
1214 if (n >= PART_PRECISION)
1216 n -= PART_PRECISION;
1218 num.high = sign_mask;
1223 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1224 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1228 num = num_trim (num, precision);
1229 num.overflow = false;
1233 /* Shift NUM, of width PRECISION, left by N bits. */
1235 num_lshift (cpp_num num, size_t precision, size_t n)
1239 num.overflow = !num.unsignedp && !num_zerop (num);
1240 num.high = num.low = 0;
1244 cpp_num orig, maybe_orig;
1248 if (m >= PART_PRECISION)
1250 m -= PART_PRECISION;
1256 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1259 num = num_trim (num, precision);
1262 num.overflow = false;
1265 maybe_orig = num_rshift (num, precision, n);
1266 num.overflow = !num_eq (orig, maybe_orig);
1273 /* The four unary operators: +, -, ! and ~. */
1275 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1280 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1281 cpp_error (pfile, CPP_DL_WARNING,
1282 "traditional C rejects the unary plus operator");
1283 num.overflow = false;
1287 num = num_negate (num, CPP_OPTION (pfile, precision));
1291 num.high = ~num.high;
1293 num = num_trim (num, CPP_OPTION (pfile, precision));
1294 num.overflow = false;
1297 default: /* case CPP_NOT: */
1298 num.low = num_zerop (num);
1300 num.overflow = false;
1301 num.unsignedp = false;
1308 /* The various binary operators. */
1310 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1313 size_t precision = CPP_OPTION (pfile, precision);
1321 if (!rhs.unsignedp && !num_positive (rhs, precision))
1323 /* A negative shift is a positive shift the other way. */
1324 if (op == CPP_LSHIFT)
1328 rhs = num_negate (rhs, precision);
1331 n = ~0; /* Maximal. */
1334 if (op == CPP_LSHIFT)
1335 lhs = num_lshift (lhs, precision, n);
1337 lhs = num_rshift (lhs, precision, n);
1342 rhs = num_negate (rhs, precision);
1344 result.low = lhs.low + rhs.low;
1345 result.high = lhs.high + rhs.high;
1346 if (result.low < lhs.low)
1348 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1349 result.overflow = false;
1351 result = num_trim (result, precision);
1352 if (!result.unsignedp)
1354 bool lhsp = num_positive (lhs, precision);
1355 result.overflow = (lhsp == num_positive (rhs, precision)
1356 && lhsp != num_positive (result, precision));
1361 default: /* case CPP_COMMA: */
1362 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1363 || !pfile->state.skip_eval))
1364 cpp_error (pfile, CPP_DL_PEDWARN,
1365 "comma operator in operand of #if");
1373 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1376 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1379 cpp_num_part middle[2], temp;
1381 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1382 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1384 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1385 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1388 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1389 if (result.low < temp)
1393 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1394 if (result.low < temp)
1397 result.high += HIGH_PART (middle[0]);
1398 result.high += HIGH_PART (middle[1]);
1399 result.unsignedp = true;
1400 result.overflow = false;
1405 /* Multiply two preprocessing numbers. */
1407 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1409 cpp_num result, temp;
1410 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1411 bool overflow, negate = false;
1412 size_t precision = CPP_OPTION (pfile, precision);
1414 /* Prepare for unsigned multiplication. */
1417 if (!num_positive (lhs, precision))
1418 negate = !negate, lhs = num_negate (lhs, precision);
1419 if (!num_positive (rhs, precision))
1420 negate = !negate, rhs = num_negate (rhs, precision);
1423 overflow = lhs.high && rhs.high;
1424 result = num_part_mul (lhs.low, rhs.low);
1426 temp = num_part_mul (lhs.high, rhs.low);
1427 result.high += temp.low;
1431 temp = num_part_mul (lhs.low, rhs.high);
1432 result.high += temp.low;
1436 temp.low = result.low, temp.high = result.high;
1437 result = num_trim (result, precision);
1438 if (!num_eq (result, temp))
1442 result = num_negate (result, precision);
1445 result.overflow = false;
1447 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1448 && !num_zerop (result));
1449 result.unsignedp = unsignedp;
1454 /* Divide two preprocessing numbers, returning the answer or the
1455 remainder depending upon OP. */
1457 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1459 cpp_num result, sub;
1461 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1462 bool negate = false, lhs_neg = false;
1463 size_t i, precision = CPP_OPTION (pfile, precision);
1465 /* Prepare for unsigned division. */
1468 if (!num_positive (lhs, precision))
1469 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1470 if (!num_positive (rhs, precision))
1471 negate = !negate, rhs = num_negate (rhs, precision);
1474 /* Find the high bit. */
1478 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1479 for (; ; i--, mask >>= 1)
1480 if (rhs.high & mask)
1485 if (precision > PART_PRECISION)
1486 i = precision - PART_PRECISION - 1;
1489 mask = (cpp_num_part) 1 << i;
1490 for (; ; i--, mask >>= 1)
1496 if (!pfile->state.skip_eval)
1497 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1501 /* First nonzero bit of RHS is bit I. Do naive division by
1502 shifting the RHS fully left, and subtracting from LHS if LHS is
1503 at least as big, and then repeating but with one less shift.
1504 This is not very efficient, but is easy to understand. */
1506 rhs.unsignedp = true;
1507 lhs.unsignedp = true;
1508 i = precision - i - 1;
1509 sub = num_lshift (rhs, precision, i);
1511 result.high = result.low = 0;
1514 if (num_greater_eq (lhs, sub, precision))
1516 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1517 if (i >= PART_PRECISION)
1518 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1520 result.low |= (cpp_num_part) 1 << i;
1524 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1528 /* We divide so that the remainder has the sign of the LHS. */
1531 result.unsignedp = unsignedp;
1532 result.overflow = false;
1536 result = num_negate (result, precision);
1537 result.overflow = (num_positive (result, precision) ^ !negate
1538 && !num_zerop (result));
1545 lhs.unsignedp = unsignedp;
1546 lhs.overflow = false;
1548 lhs = num_negate (lhs, precision);