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])))
188 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
195 /* Now scan for a well-formed integer or float. */
198 unsigned int c = *str++;
200 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
208 if (float_flag == NOT_FLOAT)
209 float_flag = AFTER_POINT;
211 SYNTAX_ERROR ("too many decimal points in number");
213 else if ((radix <= 10 && (c == 'e' || c == 'E'))
214 || (radix == 16 && (c == 'p' || c == 'P')))
216 float_flag = AFTER_EXPON;
221 /* Start of suffix. */
227 if (float_flag != NOT_FLOAT && radix == 8)
230 if (max_digit >= radix)
233 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
235 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
238 if (float_flag != NOT_FLOAT)
242 cpp_error (pfile, CPP_DL_ERROR,
243 "invalid prefix \"0b\" for floating constant");
244 return CPP_N_INVALID;
247 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
248 cpp_error (pfile, CPP_DL_PEDWARN,
249 "use of C99 hexadecimal floating constant");
251 if (float_flag == AFTER_EXPON)
253 if (*str == '+' || *str == '-')
256 /* Exponent is decimal, even if string is a hex float. */
258 SYNTAX_ERROR ("exponent has no digits");
262 while (ISDIGIT (*str));
264 else if (radix == 16)
265 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
267 result = interpret_float_suffix (str, limit - str);
270 cpp_error (pfile, CPP_DL_ERROR,
271 "invalid suffix \"%.*s\" on floating constant",
272 (int) (limit - str), str);
273 return CPP_N_INVALID;
276 /* Traditional C didn't accept any floating suffixes. */
278 && CPP_WTRADITIONAL (pfile)
279 && ! cpp_sys_macro_p (pfile))
280 cpp_error (pfile, CPP_DL_WARNING,
281 "traditional C rejects the \"%.*s\" suffix",
282 (int) (limit - str), str);
284 /* Radix must be 10 for decimal floats. */
285 if ((result & CPP_N_DFLOAT) && radix != 10)
287 cpp_error (pfile, CPP_DL_ERROR,
288 "invalid suffix \"%.*s\" with hexadecimal floating constant",
289 (int) (limit - str), str);
290 return CPP_N_INVALID;
293 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
294 cpp_error (pfile, CPP_DL_PEDWARN,
295 "decimal float constants are a GCC extension");
297 result |= CPP_N_FLOATING;
301 result = interpret_int_suffix (str, limit - str);
304 cpp_error (pfile, CPP_DL_ERROR,
305 "invalid suffix \"%.*s\" on integer constant",
306 (int) (limit - str), str);
307 return CPP_N_INVALID;
310 /* Traditional C only accepted the 'L' suffix.
311 Suppress warning about 'LL' with -Wno-long-long. */
312 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
314 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
315 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
317 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
318 cpp_error (pfile, CPP_DL_WARNING,
319 "traditional C rejects the \"%.*s\" suffix",
320 (int) (limit - str), str);
323 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
324 && ! CPP_OPTION (pfile, c99)
325 && CPP_OPTION (pfile, warn_long_long))
326 cpp_error (pfile, CPP_DL_PEDWARN,
327 "use of C99 long long integer constant");
329 result |= CPP_N_INTEGER;
332 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
333 cpp_error (pfile, CPP_DL_PEDWARN,
334 "imaginary constants are a GCC extension");
335 if (radix == 2 && CPP_PEDANTIC (pfile))
336 cpp_error (pfile, CPP_DL_PEDWARN,
337 "binary constants are a GCC extension");
340 result |= CPP_N_DECIMAL;
341 else if (radix == 16)
344 result |= CPP_N_BINARY;
346 result |= CPP_N_OCTAL;
351 return CPP_N_INVALID;
354 /* cpp_interpret_integer converts an integer constant into a cpp_num,
355 of precision options->precision.
357 We do not provide any interface for decimal->float conversion,
358 because the preprocessor doesn't need it and we don't want to
359 drag in GCC's floating point emulator. */
361 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
364 const uchar *p, *end;
369 result.unsignedp = !!(type & CPP_N_UNSIGNED);
370 result.overflow = false;
372 p = token->val.str.text;
373 end = p + token->val.str.len;
375 /* Common case of a single digit. */
376 if (token->val.str.len == 1)
377 result.low = p[0] - '0';
381 size_t precision = CPP_OPTION (pfile, precision);
382 unsigned int base = 10, c = 0;
383 bool overflow = false;
385 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
390 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
395 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
401 /* We can add a digit to numbers strictly less than this without
402 needing the precision and slowness of double integers. */
403 max = ~(cpp_num_part) 0;
404 if (precision < PART_PRECISION)
405 max >>= PART_PRECISION - precision;
406 max = (max - base + 1) / base + 1;
412 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
417 /* Strict inequality for when max is set to zero. */
418 if (result.low < max)
419 result.low = result.low * base + c;
422 result = append_digit (result, c, base, precision);
423 overflow |= result.overflow;
429 cpp_error (pfile, CPP_DL_PEDWARN,
430 "integer constant is too large for its type");
431 /* If too big to be signed, consider it unsigned. Only warn for
432 decimal numbers. Traditional numbers were always signed (but
433 we still honor an explicit U suffix); but we only have
434 traditional semantics in directives. */
435 else if (!result.unsignedp
436 && !(CPP_OPTION (pfile, traditional)
437 && pfile->state.in_directive)
438 && !num_positive (result, precision))
441 cpp_error (pfile, CPP_DL_WARNING,
442 "integer constant is so large that it is unsigned");
443 result.unsignedp = true;
450 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
452 append_digit (cpp_num num, int digit, int base, size_t precision)
457 cpp_num_part add_high, add_low;
459 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
460 need to worry about add_high overflowing. */
474 overflow = !!(num.high >> (PART_PRECISION - shift));
475 result.high = num.high << shift;
476 result.low = num.low << shift;
477 result.high |= num.low >> (PART_PRECISION - shift);
478 result.unsignedp = num.unsignedp;
482 add_low = num.low << 1;
483 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
486 add_high = add_low = 0;
488 if (add_low + digit < add_low)
492 if (result.low + add_low < result.low)
494 if (result.high + add_high < result.high)
497 result.low += add_low;
498 result.high += add_high;
499 result.overflow = overflow;
501 /* The above code catches overflow of a cpp_num type. This catches
502 overflow of the (possibly shorter) target precision. */
503 num.low = result.low;
504 num.high = result.high;
505 result = num_trim (result, precision);
506 if (!num_eq (result, num))
507 result.overflow = true;
512 /* Handle meeting "defined" in a preprocessor expression. */
514 parse_defined (cpp_reader *pfile)
518 cpp_hashnode *node = 0;
519 const cpp_token *token;
520 cpp_context *initial_context = pfile->context;
522 /* Don't expand macros. */
523 pfile->state.prevent_expansion++;
525 token = cpp_get_token (pfile);
526 if (token->type == CPP_OPEN_PAREN)
529 token = cpp_get_token (pfile);
532 if (token->type == CPP_NAME)
534 node = token->val.node;
535 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
537 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
543 cpp_error (pfile, CPP_DL_ERROR,
544 "operator \"defined\" requires an identifier");
545 if (token->flags & NAMED_OP)
550 op.type = token->type;
551 cpp_error (pfile, CPP_DL_ERROR,
552 "(\"%s\" is an alternative token for \"%s\" in C++)",
553 cpp_token_as_text (pfile, token),
554 cpp_token_as_text (pfile, &op));
560 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
561 cpp_error (pfile, CPP_DL_WARNING,
562 "this use of \"defined\" may not be portable");
564 _cpp_mark_macro_used (node);
566 /* A possible controlling macro of the form #if !defined ().
567 _cpp_parse_expr checks there was no other junk on the line. */
568 pfile->mi_ind_cmacro = node;
571 pfile->state.prevent_expansion--;
573 result.unsignedp = false;
575 result.overflow = false;
576 result.low = node && node->type == NT_MACRO;
580 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
581 number or character constant, or the result of the "defined" or "#"
584 eval_token (cpp_reader *pfile, const cpp_token *token)
590 result.unsignedp = false;
591 result.overflow = false;
596 temp = cpp_classify_number (pfile, token);
597 switch (temp & CPP_N_CATEGORY)
600 cpp_error (pfile, CPP_DL_ERROR,
601 "floating constant in preprocessor expression");
604 if (!(temp & CPP_N_IMAGINARY))
605 return cpp_interpret_integer (pfile, token, temp);
606 cpp_error (pfile, CPP_DL_ERROR,
607 "imaginary number in preprocessor expression");
611 /* Error already issued. */
614 result.high = result.low = 0;
620 cppchar_t cc = cpp_interpret_charconst (pfile, token,
625 /* Sign-extend the result if necessary. */
626 if (!unsignedp && (cppchar_signed_t) cc < 0)
628 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
629 result.low |= ~(~(cpp_num_part) 0
630 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
631 result.high = ~(cpp_num_part) 0;
632 result = num_trim (result, CPP_OPTION (pfile, precision));
638 if (token->val.node == pfile->spec_nodes.n_defined)
639 return parse_defined (pfile);
640 else if (CPP_OPTION (pfile, cplusplus)
641 && (token->val.node == pfile->spec_nodes.n_true
642 || token->val.node == pfile->spec_nodes.n_false))
645 result.low = (token->val.node == pfile->spec_nodes.n_true);
651 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
652 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
653 NODE_NAME (token->val.node));
657 default: /* CPP_HASH */
658 _cpp_test_assertion (pfile, &temp);
663 result.unsignedp = !!unsignedp;
667 /* Operator precedence and flags table.
669 After an operator is returned from the lexer, if it has priority less
670 than the operator on the top of the stack, we reduce the stack by one
671 operator and repeat the test. Since equal priorities do not reduce,
672 this is naturally right-associative.
674 We handle left-associative operators by decrementing the priority of
675 just-lexed operators by one, but retaining the priority of operators
676 already on the stack.
678 The remaining cases are '(' and ')'. We handle '(' by skipping the
679 reduction phase completely. ')' is given lower priority than
680 everything else, including '(', effectively forcing a reduction of the
681 parenthesized expression. If there is a matching '(', the routine
682 reduce() exits immediately. If the normal exit route sees a ')', then
683 there cannot have been a matching '(' and an error message is output.
685 The parser assumes all shifted operators require a left operand unless
686 the flag NO_L_OPERAND is set. These semantics are automatic; any
687 extra semantics need to be handled with operator-specific code. */
689 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
690 operand changes because of integer promotions. */
691 #define NO_L_OPERAND (1 << 0)
692 #define LEFT_ASSOC (1 << 1)
693 #define CHECK_PROMOTION (1 << 2)
695 /* Operator to priority map. Must be in the same order as the first
696 N entries of enum cpp_ttype. */
697 static const struct cpp_operator
703 /* EQ */ {0, 0}, /* Shouldn't happen. */
704 /* NOT */ {16, NO_L_OPERAND},
705 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
706 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
707 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
708 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
709 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
710 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
711 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
712 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
713 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
714 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
715 /* RSHIFT */ {13, LEFT_ASSOC},
716 /* LSHIFT */ {13, LEFT_ASSOC},
718 /* COMPL */ {16, NO_L_OPERAND},
719 /* AND_AND */ {6, LEFT_ASSOC},
720 /* OR_OR */ {5, LEFT_ASSOC},
722 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
723 /* COMMA */ {2, LEFT_ASSOC},
724 /* OPEN_PAREN */ {1, NO_L_OPERAND},
725 /* CLOSE_PAREN */ {0, 0},
727 /* EQ_EQ */ {11, LEFT_ASSOC},
728 /* NOT_EQ */ {11, LEFT_ASSOC},
729 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
730 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
731 /* UPLUS */ {16, NO_L_OPERAND},
732 /* UMINUS */ {16, NO_L_OPERAND}
735 /* Parse and evaluate a C expression, reading from PFILE.
736 Returns the truth value of the expression.
738 The implementation is an operator precedence parser, i.e. a
739 bottom-up parser, using a stack for not-yet-reduced tokens.
741 The stack base is op_stack, and the current stack pointer is 'top'.
742 There is a stack element for each operator (only), and the most
743 recently pushed operator is 'top->op'. An operand (value) is
744 stored in the 'value' field of the stack element of the operator
747 _cpp_parse_expr (cpp_reader *pfile)
749 struct op *top = pfile->op_stack;
750 unsigned int lex_count;
751 bool saw_leading_not, want_value = true;
753 pfile->state.skip_eval = 0;
755 /* Set up detection of #if ! defined(). */
756 pfile->mi_ind_cmacro = 0;
757 saw_leading_not = false;
760 /* Lowest priority operator prevents further reductions. */
768 op.token = cpp_get_token (pfile);
769 op.op = op.token->type;
773 /* These tokens convert into values. */
780 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
781 cpp_token_as_text (pfile, op.token));
783 top->value = eval_token (pfile, op.token);
787 saw_leading_not = lex_count == 1;
799 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
800 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
801 cpp_token_as_text (pfile, op.token));
805 /* Check we have a value or operator as appropriate. */
806 if (optab[op.op].flags & NO_L_OPERAND)
809 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
810 cpp_token_as_text (pfile, op.token));
814 /* We want a number (or expression) and haven't got one.
815 Try to emit a specific diagnostic. */
816 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
817 SYNTAX_ERROR ("missing expression between '(' and ')'");
819 if (op.op == CPP_EOF && top->op == CPP_EOF)
820 SYNTAX_ERROR ("#if with no expression");
822 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
823 SYNTAX_ERROR2 ("operator '%s' has no right operand",
824 cpp_token_as_text (pfile, top->token));
825 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
826 /* Complain about missing paren during reduction. */;
828 SYNTAX_ERROR2 ("operator '%s' has no left operand",
829 cpp_token_as_text (pfile, op.token));
832 top = reduce (pfile, top, op.op);
836 if (op.op == CPP_EOF)
841 case CPP_CLOSE_PAREN:
844 if (!num_zerop (top->value))
845 pfile->state.skip_eval++;
849 if (num_zerop (top->value))
850 pfile->state.skip_eval++;
853 if (top->op != CPP_QUERY)
854 SYNTAX_ERROR (" ':' without preceding '?'");
855 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
856 pfile->state.skip_eval++;
858 pfile->state.skip_eval--;
865 /* Check for and handle stack overflow. */
866 if (++top == pfile->op_limit)
867 top = _cpp_expand_op_stack (pfile);
870 top->token = op.token;
873 /* The controlling macro expression is only valid if we called lex 3
874 times: <!> <defined expression> and <EOF>. push_conditional ()
875 checks that we are at top-of-file. */
876 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
877 pfile->mi_ind_cmacro = 0;
879 if (top != pfile->op_stack)
881 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
883 return false; /* Return false on syntax error. */
886 return !num_zerop (top->value);
889 /* Reduce the operator / value stack if possible, in preparation for
890 pushing operator OP. Returns NULL on error, otherwise the top of
893 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
897 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
900 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
904 if (op == CPP_OPEN_PAREN)
907 /* Decrement the priority of left-associative operators to force a
908 reduction with operators of otherwise equal priority. */
909 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
910 while (prio < optab[top->op].prio)
912 if (CPP_OPTION (pfile, warn_num_sign_change)
913 && optab[top->op].flags & CHECK_PROMOTION)
914 check_promotion (pfile, top);
922 top[-1].value = num_unary_op (pfile, top->value, top->op);
930 top[-1].value = num_binary_op (pfile, top[-1].value,
931 top->value, top->op);
939 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
945 = num_equality_op (pfile, top[-1].value, top->value, top->op);
952 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
956 top[-1].value = num_mul (pfile, top[-1].value, top->value);
961 top[-1].value = num_div_op (pfile, top[-1].value,
962 top->value, top->op);
967 if (!num_zerop (top->value))
968 pfile->state.skip_eval--;
969 top->value.low = (!num_zerop (top->value)
970 || !num_zerop (top[1].value));
972 top->value.unsignedp = false;
973 top->value.overflow = false;
978 if (num_zerop (top->value))
979 pfile->state.skip_eval--;
980 top->value.low = (!num_zerop (top->value)
981 && !num_zerop (top[1].value));
983 top->value.unsignedp = false;
984 top->value.overflow = false;
988 if (op != CPP_CLOSE_PAREN)
990 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
994 top->value = top[1].value;
999 if (!num_zerop (top->value))
1001 pfile->state.skip_eval--;
1002 top->value = top[1].value;
1005 top->value = top[2].value;
1006 top->value.unsignedp = (top[1].value.unsignedp
1007 || top[2].value.unsignedp);
1011 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1019 if (top->value.overflow && !pfile->state.skip_eval)
1020 cpp_error (pfile, CPP_DL_PEDWARN,
1021 "integer overflow in preprocessor expression");
1024 if (op == CPP_CLOSE_PAREN)
1026 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1033 /* Returns the position of the old top of stack after expansion. */
1035 _cpp_expand_op_stack (cpp_reader *pfile)
1037 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1038 size_t new_size = old_size * 2 + 20;
1040 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1041 pfile->op_limit = pfile->op_stack + new_size;
1043 return pfile->op_stack + old_size;
1046 /* Emits a warning if the effective sign of either operand of OP
1047 changes because of integer promotions. */
1049 check_promotion (cpp_reader *pfile, const struct op *op)
1051 if (op->value.unsignedp == op[-1].value.unsignedp)
1054 if (op->value.unsignedp)
1056 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1057 cpp_error (pfile, CPP_DL_WARNING,
1058 "the left operand of \"%s\" changes sign when promoted",
1059 cpp_token_as_text (pfile, op->token));
1061 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1062 cpp_error (pfile, CPP_DL_WARNING,
1063 "the right operand of \"%s\" changes sign when promoted",
1064 cpp_token_as_text (pfile, op->token));
1067 /* Clears the unused high order bits of the number pointed to by PNUM. */
1069 num_trim (cpp_num num, size_t precision)
1071 if (precision > PART_PRECISION)
1073 precision -= PART_PRECISION;
1074 if (precision < PART_PRECISION)
1075 num.high &= ((cpp_num_part) 1 << precision) - 1;
1079 if (precision < PART_PRECISION)
1080 num.low &= ((cpp_num_part) 1 << precision) - 1;
1087 /* True iff A (presumed signed) >= 0. */
1089 num_positive (cpp_num num, size_t precision)
1091 if (precision > PART_PRECISION)
1093 precision -= PART_PRECISION;
1094 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1097 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1100 /* Sign extend a number, with PRECISION significant bits and all
1101 others assumed clear, to fill out a cpp_num structure. */
1103 cpp_num_sign_extend (cpp_num num, size_t precision)
1107 if (precision > PART_PRECISION)
1109 precision -= PART_PRECISION;
1110 if (precision < PART_PRECISION
1111 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1112 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1114 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1116 if (precision < PART_PRECISION)
1117 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1118 num.high = ~(cpp_num_part) 0;
1125 /* Returns the negative of NUM. */
1127 num_negate (cpp_num num, size_t precision)
1132 num.high = ~num.high;
1136 num = num_trim (num, precision);
1137 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1142 /* Returns true if A >= B. */
1144 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1148 unsignedp = pa.unsignedp || pb.unsignedp;
1152 /* Both numbers have signed type. If they are of different
1153 sign, the answer is the sign of A. */
1154 unsignedp = num_positive (pa, precision);
1156 if (unsignedp != num_positive (pb, precision))
1159 /* Otherwise we can do an unsigned comparison. */
1162 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1165 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1167 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1168 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1170 lhs.overflow = false;
1171 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1173 /* As excess precision is zeroed, there is no need to num_trim () as
1174 these operations cannot introduce a set bit there. */
1178 lhs.high &= rhs.high;
1180 else if (op == CPP_OR)
1183 lhs.high |= rhs.high;
1188 lhs.high ^= rhs.high;
1194 /* Returns LHS OP RHS, where OP is an inequality. */
1196 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1199 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1201 if (op == CPP_GREATER_EQ)
1203 else if (op == CPP_LESS)
1205 else if (op == CPP_GREATER)
1206 lhs.low = gte && !num_eq (lhs, rhs);
1207 else /* CPP_LESS_EQ. */
1208 lhs.low = !gte || num_eq (lhs, rhs);
1211 lhs.overflow = false;
1212 lhs.unsignedp = false;
1216 /* Returns LHS OP RHS, where OP is == or !=. */
1218 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1219 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1221 /* Work around a 3.0.4 bug; see PR 6950. */
1222 bool eq = num_eq (lhs, rhs);
1223 if (op == CPP_NOT_EQ)
1227 lhs.overflow = false;
1228 lhs.unsignedp = false;
1232 /* Shift NUM, of width PRECISION, right by N bits. */
1234 num_rshift (cpp_num num, size_t precision, size_t n)
1236 cpp_num_part sign_mask;
1237 bool x = num_positive (num, precision);
1239 if (num.unsignedp || x)
1242 sign_mask = ~(cpp_num_part) 0;
1245 num.high = num.low = sign_mask;
1249 if (precision < PART_PRECISION)
1250 num.high = sign_mask, num.low |= sign_mask << precision;
1251 else if (precision < 2 * PART_PRECISION)
1252 num.high |= sign_mask << (precision - PART_PRECISION);
1254 if (n >= PART_PRECISION)
1256 n -= PART_PRECISION;
1258 num.high = sign_mask;
1263 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1264 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1268 num = num_trim (num, precision);
1269 num.overflow = false;
1273 /* Shift NUM, of width PRECISION, left by N bits. */
1275 num_lshift (cpp_num num, size_t precision, size_t n)
1279 num.overflow = !num.unsignedp && !num_zerop (num);
1280 num.high = num.low = 0;
1284 cpp_num orig, maybe_orig;
1288 if (m >= PART_PRECISION)
1290 m -= PART_PRECISION;
1296 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1299 num = num_trim (num, precision);
1302 num.overflow = false;
1305 maybe_orig = num_rshift (num, precision, n);
1306 num.overflow = !num_eq (orig, maybe_orig);
1313 /* The four unary operators: +, -, ! and ~. */
1315 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1320 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1321 cpp_error (pfile, CPP_DL_WARNING,
1322 "traditional C rejects the unary plus operator");
1323 num.overflow = false;
1327 num = num_negate (num, CPP_OPTION (pfile, precision));
1331 num.high = ~num.high;
1333 num = num_trim (num, CPP_OPTION (pfile, precision));
1334 num.overflow = false;
1337 default: /* case CPP_NOT: */
1338 num.low = num_zerop (num);
1340 num.overflow = false;
1341 num.unsignedp = false;
1348 /* The various binary operators. */
1350 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1353 size_t precision = CPP_OPTION (pfile, precision);
1361 if (!rhs.unsignedp && !num_positive (rhs, precision))
1363 /* A negative shift is a positive shift the other way. */
1364 if (op == CPP_LSHIFT)
1368 rhs = num_negate (rhs, precision);
1371 n = ~0; /* Maximal. */
1374 if (op == CPP_LSHIFT)
1375 lhs = num_lshift (lhs, precision, n);
1377 lhs = num_rshift (lhs, precision, n);
1382 rhs = num_negate (rhs, precision);
1384 result.low = lhs.low + rhs.low;
1385 result.high = lhs.high + rhs.high;
1386 if (result.low < lhs.low)
1388 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1389 result.overflow = false;
1391 result = num_trim (result, precision);
1392 if (!result.unsignedp)
1394 bool lhsp = num_positive (lhs, precision);
1395 result.overflow = (lhsp == num_positive (rhs, precision)
1396 && lhsp != num_positive (result, precision));
1401 default: /* case CPP_COMMA: */
1402 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1403 || !pfile->state.skip_eval))
1404 cpp_error (pfile, CPP_DL_PEDWARN,
1405 "comma operator in operand of #if");
1413 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1416 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1419 cpp_num_part middle[2], temp;
1421 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1422 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1424 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1425 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1428 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1429 if (result.low < temp)
1433 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1434 if (result.low < temp)
1437 result.high += HIGH_PART (middle[0]);
1438 result.high += HIGH_PART (middle[1]);
1439 result.unsignedp = true;
1440 result.overflow = false;
1445 /* Multiply two preprocessing numbers. */
1447 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1449 cpp_num result, temp;
1450 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1451 bool overflow, negate = false;
1452 size_t precision = CPP_OPTION (pfile, precision);
1454 /* Prepare for unsigned multiplication. */
1457 if (!num_positive (lhs, precision))
1458 negate = !negate, lhs = num_negate (lhs, precision);
1459 if (!num_positive (rhs, precision))
1460 negate = !negate, rhs = num_negate (rhs, precision);
1463 overflow = lhs.high && rhs.high;
1464 result = num_part_mul (lhs.low, rhs.low);
1466 temp = num_part_mul (lhs.high, rhs.low);
1467 result.high += temp.low;
1471 temp = num_part_mul (lhs.low, rhs.high);
1472 result.high += temp.low;
1476 temp.low = result.low, temp.high = result.high;
1477 result = num_trim (result, precision);
1478 if (!num_eq (result, temp))
1482 result = num_negate (result, precision);
1485 result.overflow = false;
1487 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1488 && !num_zerop (result));
1489 result.unsignedp = unsignedp;
1494 /* Divide two preprocessing numbers, returning the answer or the
1495 remainder depending upon OP. */
1497 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1499 cpp_num result, sub;
1501 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1502 bool negate = false, lhs_neg = false;
1503 size_t i, precision = CPP_OPTION (pfile, precision);
1505 /* Prepare for unsigned division. */
1508 if (!num_positive (lhs, precision))
1509 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1510 if (!num_positive (rhs, precision))
1511 negate = !negate, rhs = num_negate (rhs, precision);
1514 /* Find the high bit. */
1518 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1519 for (; ; i--, mask >>= 1)
1520 if (rhs.high & mask)
1525 if (precision > PART_PRECISION)
1526 i = precision - PART_PRECISION - 1;
1529 mask = (cpp_num_part) 1 << i;
1530 for (; ; i--, mask >>= 1)
1536 if (!pfile->state.skip_eval)
1537 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1541 /* First nonzero bit of RHS is bit I. Do naive division by
1542 shifting the RHS fully left, and subtracting from LHS if LHS is
1543 at least as big, and then repeating but with one less shift.
1544 This is not very efficient, but is easy to understand. */
1546 rhs.unsignedp = true;
1547 lhs.unsignedp = true;
1548 i = precision - i - 1;
1549 sub = num_lshift (rhs, precision, i);
1551 result.high = result.low = 0;
1554 if (num_greater_eq (lhs, sub, precision))
1556 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1557 if (i >= PART_PRECISION)
1558 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1560 result.low |= (cpp_num_part) 1 << i;
1564 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1568 /* We divide so that the remainder has the sign of the LHS. */
1571 result.unsignedp = unsignedp;
1572 result.overflow = false;
1576 result = num_negate (result, precision);
1577 result.overflow = (num_positive (result, precision) ^ !negate
1578 && !num_zerop (result));
1585 lhs.unsignedp = unsignedp;
1586 lhs.overflow = false;
1588 lhs = num_negate (lhs, precision);