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, d0 = 0;
101 case 'j': case 'J': i++; break;
102 case 'd': case 'D': d++; break;
107 if (d == 1 && !f && !l) {
112 if (f + d0 + l > 1 || i > 1)
115 /* Allow dd, df, dl suffixes for decimal float constants. */
116 if (d && ((d + f + l != 2) || i))
119 return ((i ? CPP_N_IMAGINARY : 0)
122 l ? CPP_N_LARGE : CPP_N_DEFAULT)
123 | (d ? CPP_N_DFLOAT : 0));
126 /* Subroutine of cpp_classify_number. S points to an integer suffix
127 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
128 flag vector describing the suffix. */
130 interpret_int_suffix (const uchar *s, size_t len)
139 case 'u': case 'U': u++; break;
141 case 'j': case 'J': i++; break;
142 case 'l': case 'L': l++;
143 /* If there are two Ls, they must be adjacent and the same case. */
144 if (l == 2 && s[len] != s[len + 1])
151 if (l > 2 || u > 1 || i > 1)
154 return ((i ? CPP_N_IMAGINARY : 0)
155 | (u ? CPP_N_UNSIGNED : 0)
156 | ((l == 0) ? CPP_N_SMALL
157 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
160 /* Categorize numeric constants according to their field (integer,
161 floating point, or invalid), radix (decimal, octal, hexadecimal),
162 and type suffixes. */
164 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
166 const uchar *str = token->val.str.text;
168 unsigned int max_digit, result, radix;
169 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
171 /* If the lexer has done its job, length one can only be a single
172 digit. Fast-path this very common case. */
173 if (token->val.str.len == 1)
174 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
176 limit = str + token->val.str.len;
177 float_flag = NOT_FLOAT;
181 /* First, interpret the radix. */
187 /* Require at least one hex digit to classify it as hex. */
188 if ((*str == 'x' || *str == 'X')
189 && (str[1] == '.' || ISXDIGIT (str[1])))
194 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
201 /* Now scan for a well-formed integer or float. */
204 unsigned int c = *str++;
206 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
214 if (float_flag == NOT_FLOAT)
215 float_flag = AFTER_POINT;
217 SYNTAX_ERROR ("too many decimal points in number");
219 else if ((radix <= 10 && (c == 'e' || c == 'E'))
220 || (radix == 16 && (c == 'p' || c == 'P')))
222 float_flag = AFTER_EXPON;
227 /* Start of suffix. */
233 if (float_flag != NOT_FLOAT && radix == 8)
236 if (max_digit >= radix)
239 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
241 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
244 if (float_flag != NOT_FLOAT)
248 cpp_error (pfile, CPP_DL_ERROR,
249 "invalid prefix \"0b\" for floating constant");
250 return CPP_N_INVALID;
253 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
254 cpp_error (pfile, CPP_DL_PEDWARN,
255 "use of C99 hexadecimal floating constant");
257 if (float_flag == AFTER_EXPON)
259 if (*str == '+' || *str == '-')
262 /* Exponent is decimal, even if string is a hex float. */
264 SYNTAX_ERROR ("exponent has no digits");
268 while (ISDIGIT (*str));
270 else if (radix == 16)
271 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
273 result = interpret_float_suffix (str, limit - str);
276 cpp_error (pfile, CPP_DL_ERROR,
277 "invalid suffix \"%.*s\" on floating constant",
278 (int) (limit - str), str);
279 return CPP_N_INVALID;
282 /* Traditional C didn't accept any floating suffixes. */
284 && CPP_WTRADITIONAL (pfile)
285 && ! cpp_sys_macro_p (pfile))
286 cpp_error (pfile, CPP_DL_WARNING,
287 "traditional C rejects the \"%.*s\" suffix",
288 (int) (limit - str), str);
290 /* A suffix for double is a GCC extension via decimal float support.
291 If the suffix also specifies an imaginary value we'll catch that
293 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
294 cpp_error (pfile, CPP_DL_PEDWARN,
295 "suffix for double constant is a GCC extension");
297 /* Radix must be 10 for decimal floats. */
298 if ((result & CPP_N_DFLOAT) && radix != 10)
300 cpp_error (pfile, CPP_DL_ERROR,
301 "invalid suffix \"%.*s\" with hexadecimal floating constant",
302 (int) (limit - str), str);
303 return CPP_N_INVALID;
306 result |= CPP_N_FLOATING;
310 result = interpret_int_suffix (str, limit - str);
313 cpp_error (pfile, CPP_DL_ERROR,
314 "invalid suffix \"%.*s\" on integer constant",
315 (int) (limit - str), str);
316 return CPP_N_INVALID;
319 /* Traditional C only accepted the 'L' suffix.
320 Suppress warning about 'LL' with -Wno-long-long. */
321 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
323 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
324 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
326 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
327 cpp_error (pfile, CPP_DL_WARNING,
328 "traditional C rejects the \"%.*s\" suffix",
329 (int) (limit - str), str);
332 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
333 && ! CPP_OPTION (pfile, c99)
334 && CPP_OPTION (pfile, warn_long_long))
335 cpp_error (pfile, CPP_DL_PEDWARN,
336 "use of C99 long long integer constant");
338 result |= CPP_N_INTEGER;
341 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
342 cpp_error (pfile, CPP_DL_PEDWARN,
343 "imaginary constants are a GCC extension");
344 if (radix == 2 && CPP_PEDANTIC (pfile))
345 cpp_error (pfile, CPP_DL_PEDWARN,
346 "binary constants are a GCC extension");
349 result |= CPP_N_DECIMAL;
350 else if (radix == 16)
353 result |= CPP_N_BINARY;
355 result |= CPP_N_OCTAL;
360 return CPP_N_INVALID;
363 /* cpp_interpret_integer converts an integer constant into a cpp_num,
364 of precision options->precision.
366 We do not provide any interface for decimal->float conversion,
367 because the preprocessor doesn't need it and we don't want to
368 drag in GCC's floating point emulator. */
370 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
373 const uchar *p, *end;
378 result.unsignedp = !!(type & CPP_N_UNSIGNED);
379 result.overflow = false;
381 p = token->val.str.text;
382 end = p + token->val.str.len;
384 /* Common case of a single digit. */
385 if (token->val.str.len == 1)
386 result.low = p[0] - '0';
390 size_t precision = CPP_OPTION (pfile, precision);
391 unsigned int base = 10, c = 0;
392 bool overflow = false;
394 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
399 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
404 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
410 /* We can add a digit to numbers strictly less than this without
411 needing the precision and slowness of double integers. */
412 max = ~(cpp_num_part) 0;
413 if (precision < PART_PRECISION)
414 max >>= PART_PRECISION - precision;
415 max = (max - base + 1) / base + 1;
421 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
426 /* Strict inequality for when max is set to zero. */
427 if (result.low < max)
428 result.low = result.low * base + c;
431 result = append_digit (result, c, base, precision);
432 overflow |= result.overflow;
438 cpp_error (pfile, CPP_DL_PEDWARN,
439 "integer constant is too large for its type");
440 /* If too big to be signed, consider it unsigned. Only warn for
441 decimal numbers. Traditional numbers were always signed (but
442 we still honor an explicit U suffix); but we only have
443 traditional semantics in directives. */
444 else if (!result.unsignedp
445 && !(CPP_OPTION (pfile, traditional)
446 && pfile->state.in_directive)
447 && !num_positive (result, precision))
450 cpp_error (pfile, CPP_DL_WARNING,
451 "integer constant is so large that it is unsigned");
452 result.unsignedp = true;
459 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
461 append_digit (cpp_num num, int digit, int base, size_t precision)
466 cpp_num_part add_high, add_low;
468 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
469 need to worry about add_high overflowing. */
483 overflow = !!(num.high >> (PART_PRECISION - shift));
484 result.high = num.high << shift;
485 result.low = num.low << shift;
486 result.high |= num.low >> (PART_PRECISION - shift);
487 result.unsignedp = num.unsignedp;
491 add_low = num.low << 1;
492 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
495 add_high = add_low = 0;
497 if (add_low + digit < add_low)
501 if (result.low + add_low < result.low)
503 if (result.high + add_high < result.high)
506 result.low += add_low;
507 result.high += add_high;
508 result.overflow = overflow;
510 /* The above code catches overflow of a cpp_num type. This catches
511 overflow of the (possibly shorter) target precision. */
512 num.low = result.low;
513 num.high = result.high;
514 result = num_trim (result, precision);
515 if (!num_eq (result, num))
516 result.overflow = true;
521 /* Handle meeting "defined" in a preprocessor expression. */
523 parse_defined (cpp_reader *pfile)
527 cpp_hashnode *node = 0;
528 const cpp_token *token;
529 cpp_context *initial_context = pfile->context;
531 /* Don't expand macros. */
532 pfile->state.prevent_expansion++;
534 token = cpp_get_token (pfile);
535 if (token->type == CPP_OPEN_PAREN)
538 token = cpp_get_token (pfile);
541 if (token->type == CPP_NAME)
543 node = token->val.node;
544 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
546 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
552 cpp_error (pfile, CPP_DL_ERROR,
553 "operator \"defined\" requires an identifier");
554 if (token->flags & NAMED_OP)
559 op.type = token->type;
560 cpp_error (pfile, CPP_DL_ERROR,
561 "(\"%s\" is an alternative token for \"%s\" in C++)",
562 cpp_token_as_text (pfile, token),
563 cpp_token_as_text (pfile, &op));
569 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
570 cpp_error (pfile, CPP_DL_WARNING,
571 "this use of \"defined\" may not be portable");
573 _cpp_mark_macro_used (node);
575 /* A possible controlling macro of the form #if !defined ().
576 _cpp_parse_expr checks there was no other junk on the line. */
577 pfile->mi_ind_cmacro = node;
580 pfile->state.prevent_expansion--;
582 result.unsignedp = false;
584 result.overflow = false;
585 result.low = node && node->type == NT_MACRO;
589 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
590 number or character constant, or the result of the "defined" or "#"
593 eval_token (cpp_reader *pfile, const cpp_token *token)
599 result.unsignedp = false;
600 result.overflow = false;
605 temp = cpp_classify_number (pfile, token);
606 switch (temp & CPP_N_CATEGORY)
609 cpp_error (pfile, CPP_DL_ERROR,
610 "floating constant in preprocessor expression");
613 if (!(temp & CPP_N_IMAGINARY))
614 return cpp_interpret_integer (pfile, token, temp);
615 cpp_error (pfile, CPP_DL_ERROR,
616 "imaginary number in preprocessor expression");
620 /* Error already issued. */
623 result.high = result.low = 0;
629 cppchar_t cc = cpp_interpret_charconst (pfile, token,
634 /* Sign-extend the result if necessary. */
635 if (!unsignedp && (cppchar_signed_t) cc < 0)
637 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
638 result.low |= ~(~(cpp_num_part) 0
639 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
640 result.high = ~(cpp_num_part) 0;
641 result = num_trim (result, CPP_OPTION (pfile, precision));
647 if (token->val.node == pfile->spec_nodes.n_defined)
648 return parse_defined (pfile);
649 else if (CPP_OPTION (pfile, cplusplus)
650 && (token->val.node == pfile->spec_nodes.n_true
651 || token->val.node == pfile->spec_nodes.n_false))
654 result.low = (token->val.node == pfile->spec_nodes.n_true);
660 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
661 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
662 NODE_NAME (token->val.node));
666 default: /* CPP_HASH */
667 _cpp_test_assertion (pfile, &temp);
672 result.unsignedp = !!unsignedp;
676 /* Operator precedence and flags table.
678 After an operator is returned from the lexer, if it has priority less
679 than the operator on the top of the stack, we reduce the stack by one
680 operator and repeat the test. Since equal priorities do not reduce,
681 this is naturally right-associative.
683 We handle left-associative operators by decrementing the priority of
684 just-lexed operators by one, but retaining the priority of operators
685 already on the stack.
687 The remaining cases are '(' and ')'. We handle '(' by skipping the
688 reduction phase completely. ')' is given lower priority than
689 everything else, including '(', effectively forcing a reduction of the
690 parenthesized expression. If there is a matching '(', the routine
691 reduce() exits immediately. If the normal exit route sees a ')', then
692 there cannot have been a matching '(' and an error message is output.
694 The parser assumes all shifted operators require a left operand unless
695 the flag NO_L_OPERAND is set. These semantics are automatic; any
696 extra semantics need to be handled with operator-specific code. */
698 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
699 operand changes because of integer promotions. */
700 #define NO_L_OPERAND (1 << 0)
701 #define LEFT_ASSOC (1 << 1)
702 #define CHECK_PROMOTION (1 << 2)
704 /* Operator to priority map. Must be in the same order as the first
705 N entries of enum cpp_ttype. */
706 static const struct cpp_operator
712 /* EQ */ {0, 0}, /* Shouldn't happen. */
713 /* NOT */ {16, NO_L_OPERAND},
714 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
715 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
716 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
717 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
718 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
719 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
720 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
721 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
722 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
723 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
724 /* RSHIFT */ {13, LEFT_ASSOC},
725 /* LSHIFT */ {13, LEFT_ASSOC},
727 /* COMPL */ {16, NO_L_OPERAND},
728 /* AND_AND */ {6, LEFT_ASSOC},
729 /* OR_OR */ {5, LEFT_ASSOC},
731 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
732 /* COMMA */ {2, LEFT_ASSOC},
733 /* OPEN_PAREN */ {1, NO_L_OPERAND},
734 /* CLOSE_PAREN */ {0, 0},
736 /* EQ_EQ */ {11, LEFT_ASSOC},
737 /* NOT_EQ */ {11, LEFT_ASSOC},
738 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
739 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
740 /* UPLUS */ {16, NO_L_OPERAND},
741 /* UMINUS */ {16, NO_L_OPERAND}
744 /* Parse and evaluate a C expression, reading from PFILE.
745 Returns the truth value of the expression.
747 The implementation is an operator precedence parser, i.e. a
748 bottom-up parser, using a stack for not-yet-reduced tokens.
750 The stack base is op_stack, and the current stack pointer is 'top'.
751 There is a stack element for each operator (only), and the most
752 recently pushed operator is 'top->op'. An operand (value) is
753 stored in the 'value' field of the stack element of the operator
756 _cpp_parse_expr (cpp_reader *pfile)
758 struct op *top = pfile->op_stack;
759 unsigned int lex_count;
760 bool saw_leading_not, want_value = true;
762 pfile->state.skip_eval = 0;
764 /* Set up detection of #if ! defined(). */
765 pfile->mi_ind_cmacro = 0;
766 saw_leading_not = false;
769 /* Lowest priority operator prevents further reductions. */
777 op.token = cpp_get_token (pfile);
778 op.op = op.token->type;
782 /* These tokens convert into values. */
789 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
790 cpp_token_as_text (pfile, op.token));
792 top->value = eval_token (pfile, op.token);
796 saw_leading_not = lex_count == 1;
808 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
809 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
810 cpp_token_as_text (pfile, op.token));
814 /* Check we have a value or operator as appropriate. */
815 if (optab[op.op].flags & NO_L_OPERAND)
818 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
819 cpp_token_as_text (pfile, op.token));
823 /* We want a number (or expression) and haven't got one.
824 Try to emit a specific diagnostic. */
825 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
826 SYNTAX_ERROR ("missing expression between '(' and ')'");
828 if (op.op == CPP_EOF && top->op == CPP_EOF)
829 SYNTAX_ERROR ("#if with no expression");
831 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
832 SYNTAX_ERROR2 ("operator '%s' has no right operand",
833 cpp_token_as_text (pfile, top->token));
834 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
835 /* Complain about missing paren during reduction. */;
837 SYNTAX_ERROR2 ("operator '%s' has no left operand",
838 cpp_token_as_text (pfile, op.token));
841 top = reduce (pfile, top, op.op);
845 if (op.op == CPP_EOF)
850 case CPP_CLOSE_PAREN:
853 if (!num_zerop (top->value))
854 pfile->state.skip_eval++;
858 if (num_zerop (top->value))
859 pfile->state.skip_eval++;
862 if (top->op != CPP_QUERY)
863 SYNTAX_ERROR (" ':' without preceding '?'");
864 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
865 pfile->state.skip_eval++;
867 pfile->state.skip_eval--;
874 /* Check for and handle stack overflow. */
875 if (++top == pfile->op_limit)
876 top = _cpp_expand_op_stack (pfile);
879 top->token = op.token;
882 /* The controlling macro expression is only valid if we called lex 3
883 times: <!> <defined expression> and <EOF>. push_conditional ()
884 checks that we are at top-of-file. */
885 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
886 pfile->mi_ind_cmacro = 0;
888 if (top != pfile->op_stack)
890 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
892 return false; /* Return false on syntax error. */
895 return !num_zerop (top->value);
898 /* Reduce the operator / value stack if possible, in preparation for
899 pushing operator OP. Returns NULL on error, otherwise the top of
902 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
906 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
909 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
913 if (op == CPP_OPEN_PAREN)
916 /* Decrement the priority of left-associative operators to force a
917 reduction with operators of otherwise equal priority. */
918 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
919 while (prio < optab[top->op].prio)
921 if (CPP_OPTION (pfile, warn_num_sign_change)
922 && optab[top->op].flags & CHECK_PROMOTION)
923 check_promotion (pfile, top);
931 top[-1].value = num_unary_op (pfile, top->value, top->op);
939 top[-1].value = num_binary_op (pfile, top[-1].value,
940 top->value, top->op);
948 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
954 = num_equality_op (pfile, top[-1].value, top->value, top->op);
961 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
965 top[-1].value = num_mul (pfile, top[-1].value, top->value);
970 top[-1].value = num_div_op (pfile, top[-1].value,
971 top->value, top->op);
976 if (!num_zerop (top->value))
977 pfile->state.skip_eval--;
978 top->value.low = (!num_zerop (top->value)
979 || !num_zerop (top[1].value));
981 top->value.unsignedp = false;
982 top->value.overflow = false;
987 if (num_zerop (top->value))
988 pfile->state.skip_eval--;
989 top->value.low = (!num_zerop (top->value)
990 && !num_zerop (top[1].value));
992 top->value.unsignedp = false;
993 top->value.overflow = false;
997 if (op != CPP_CLOSE_PAREN)
999 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
1003 top->value = top[1].value;
1008 if (!num_zerop (top->value))
1010 pfile->state.skip_eval--;
1011 top->value = top[1].value;
1014 top->value = top[2].value;
1015 top->value.unsignedp = (top[1].value.unsignedp
1016 || top[2].value.unsignedp);
1020 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1028 if (top->value.overflow && !pfile->state.skip_eval)
1029 cpp_error (pfile, CPP_DL_PEDWARN,
1030 "integer overflow in preprocessor expression");
1033 if (op == CPP_CLOSE_PAREN)
1035 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1042 /* Returns the position of the old top of stack after expansion. */
1044 _cpp_expand_op_stack (cpp_reader *pfile)
1046 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1047 size_t new_size = old_size * 2 + 20;
1049 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1050 pfile->op_limit = pfile->op_stack + new_size;
1052 return pfile->op_stack + old_size;
1055 /* Emits a warning if the effective sign of either operand of OP
1056 changes because of integer promotions. */
1058 check_promotion (cpp_reader *pfile, const struct op *op)
1060 if (op->value.unsignedp == op[-1].value.unsignedp)
1063 if (op->value.unsignedp)
1065 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1066 cpp_error (pfile, CPP_DL_WARNING,
1067 "the left operand of \"%s\" changes sign when promoted",
1068 cpp_token_as_text (pfile, op->token));
1070 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1071 cpp_error (pfile, CPP_DL_WARNING,
1072 "the right operand of \"%s\" changes sign when promoted",
1073 cpp_token_as_text (pfile, op->token));
1076 /* Clears the unused high order bits of the number pointed to by PNUM. */
1078 num_trim (cpp_num num, size_t precision)
1080 if (precision > PART_PRECISION)
1082 precision -= PART_PRECISION;
1083 if (precision < PART_PRECISION)
1084 num.high &= ((cpp_num_part) 1 << precision) - 1;
1088 if (precision < PART_PRECISION)
1089 num.low &= ((cpp_num_part) 1 << precision) - 1;
1096 /* True iff A (presumed signed) >= 0. */
1098 num_positive (cpp_num num, size_t precision)
1100 if (precision > PART_PRECISION)
1102 precision -= PART_PRECISION;
1103 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1106 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1109 /* Sign extend a number, with PRECISION significant bits and all
1110 others assumed clear, to fill out a cpp_num structure. */
1112 cpp_num_sign_extend (cpp_num num, size_t precision)
1116 if (precision > PART_PRECISION)
1118 precision -= PART_PRECISION;
1119 if (precision < PART_PRECISION
1120 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1121 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1123 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1125 if (precision < PART_PRECISION)
1126 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1127 num.high = ~(cpp_num_part) 0;
1134 /* Returns the negative of NUM. */
1136 num_negate (cpp_num num, size_t precision)
1141 num.high = ~num.high;
1145 num = num_trim (num, precision);
1146 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1151 /* Returns true if A >= B. */
1153 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1157 unsignedp = pa.unsignedp || pb.unsignedp;
1161 /* Both numbers have signed type. If they are of different
1162 sign, the answer is the sign of A. */
1163 unsignedp = num_positive (pa, precision);
1165 if (unsignedp != num_positive (pb, precision))
1168 /* Otherwise we can do an unsigned comparison. */
1171 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1174 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1176 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1177 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1179 lhs.overflow = false;
1180 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1182 /* As excess precision is zeroed, there is no need to num_trim () as
1183 these operations cannot introduce a set bit there. */
1187 lhs.high &= rhs.high;
1189 else if (op == CPP_OR)
1192 lhs.high |= rhs.high;
1197 lhs.high ^= rhs.high;
1203 /* Returns LHS OP RHS, where OP is an inequality. */
1205 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1208 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1210 if (op == CPP_GREATER_EQ)
1212 else if (op == CPP_LESS)
1214 else if (op == CPP_GREATER)
1215 lhs.low = gte && !num_eq (lhs, rhs);
1216 else /* CPP_LESS_EQ. */
1217 lhs.low = !gte || num_eq (lhs, rhs);
1220 lhs.overflow = false;
1221 lhs.unsignedp = false;
1225 /* Returns LHS OP RHS, where OP is == or !=. */
1227 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1228 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1230 /* Work around a 3.0.4 bug; see PR 6950. */
1231 bool eq = num_eq (lhs, rhs);
1232 if (op == CPP_NOT_EQ)
1236 lhs.overflow = false;
1237 lhs.unsignedp = false;
1241 /* Shift NUM, of width PRECISION, right by N bits. */
1243 num_rshift (cpp_num num, size_t precision, size_t n)
1245 cpp_num_part sign_mask;
1246 bool x = num_positive (num, precision);
1248 if (num.unsignedp || x)
1251 sign_mask = ~(cpp_num_part) 0;
1254 num.high = num.low = sign_mask;
1258 if (precision < PART_PRECISION)
1259 num.high = sign_mask, num.low |= sign_mask << precision;
1260 else if (precision < 2 * PART_PRECISION)
1261 num.high |= sign_mask << (precision - PART_PRECISION);
1263 if (n >= PART_PRECISION)
1265 n -= PART_PRECISION;
1267 num.high = sign_mask;
1272 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1273 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1277 num = num_trim (num, precision);
1278 num.overflow = false;
1282 /* Shift NUM, of width PRECISION, left by N bits. */
1284 num_lshift (cpp_num num, size_t precision, size_t n)
1288 num.overflow = !num.unsignedp && !num_zerop (num);
1289 num.high = num.low = 0;
1293 cpp_num orig, maybe_orig;
1297 if (m >= PART_PRECISION)
1299 m -= PART_PRECISION;
1305 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1308 num = num_trim (num, precision);
1311 num.overflow = false;
1314 maybe_orig = num_rshift (num, precision, n);
1315 num.overflow = !num_eq (orig, maybe_orig);
1322 /* The four unary operators: +, -, ! and ~. */
1324 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1329 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1330 cpp_error (pfile, CPP_DL_WARNING,
1331 "traditional C rejects the unary plus operator");
1332 num.overflow = false;
1336 num = num_negate (num, CPP_OPTION (pfile, precision));
1340 num.high = ~num.high;
1342 num = num_trim (num, CPP_OPTION (pfile, precision));
1343 num.overflow = false;
1346 default: /* case CPP_NOT: */
1347 num.low = num_zerop (num);
1349 num.overflow = false;
1350 num.unsignedp = false;
1357 /* The various binary operators. */
1359 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1362 size_t precision = CPP_OPTION (pfile, precision);
1370 if (!rhs.unsignedp && !num_positive (rhs, precision))
1372 /* A negative shift is a positive shift the other way. */
1373 if (op == CPP_LSHIFT)
1377 rhs = num_negate (rhs, precision);
1380 n = ~0; /* Maximal. */
1383 if (op == CPP_LSHIFT)
1384 lhs = num_lshift (lhs, precision, n);
1386 lhs = num_rshift (lhs, precision, n);
1391 rhs = num_negate (rhs, precision);
1393 result.low = lhs.low + rhs.low;
1394 result.high = lhs.high + rhs.high;
1395 if (result.low < lhs.low)
1397 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1398 result.overflow = false;
1400 result = num_trim (result, precision);
1401 if (!result.unsignedp)
1403 bool lhsp = num_positive (lhs, precision);
1404 result.overflow = (lhsp == num_positive (rhs, precision)
1405 && lhsp != num_positive (result, precision));
1410 default: /* case CPP_COMMA: */
1411 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1412 || !pfile->state.skip_eval))
1413 cpp_error (pfile, CPP_DL_PEDWARN,
1414 "comma operator in operand of #if");
1422 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1425 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1428 cpp_num_part middle[2], temp;
1430 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1431 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1433 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1434 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1437 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1438 if (result.low < temp)
1442 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1443 if (result.low < temp)
1446 result.high += HIGH_PART (middle[0]);
1447 result.high += HIGH_PART (middle[1]);
1448 result.unsignedp = true;
1449 result.overflow = false;
1454 /* Multiply two preprocessing numbers. */
1456 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1458 cpp_num result, temp;
1459 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1460 bool overflow, negate = false;
1461 size_t precision = CPP_OPTION (pfile, precision);
1463 /* Prepare for unsigned multiplication. */
1466 if (!num_positive (lhs, precision))
1467 negate = !negate, lhs = num_negate (lhs, precision);
1468 if (!num_positive (rhs, precision))
1469 negate = !negate, rhs = num_negate (rhs, precision);
1472 overflow = lhs.high && rhs.high;
1473 result = num_part_mul (lhs.low, rhs.low);
1475 temp = num_part_mul (lhs.high, rhs.low);
1476 result.high += temp.low;
1480 temp = num_part_mul (lhs.low, rhs.high);
1481 result.high += temp.low;
1485 temp.low = result.low, temp.high = result.high;
1486 result = num_trim (result, precision);
1487 if (!num_eq (result, temp))
1491 result = num_negate (result, precision);
1494 result.overflow = false;
1496 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1497 && !num_zerop (result));
1498 result.unsignedp = unsignedp;
1503 /* Divide two preprocessing numbers, returning the answer or the
1504 remainder depending upon OP. */
1506 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1508 cpp_num result, sub;
1510 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1511 bool negate = false, lhs_neg = false;
1512 size_t i, precision = CPP_OPTION (pfile, precision);
1514 /* Prepare for unsigned division. */
1517 if (!num_positive (lhs, precision))
1518 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1519 if (!num_positive (rhs, precision))
1520 negate = !negate, rhs = num_negate (rhs, precision);
1523 /* Find the high bit. */
1527 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1528 for (; ; i--, mask >>= 1)
1529 if (rhs.high & mask)
1534 if (precision > PART_PRECISION)
1535 i = precision - PART_PRECISION - 1;
1538 mask = (cpp_num_part) 1 << i;
1539 for (; ; i--, mask >>= 1)
1545 if (!pfile->state.skip_eval)
1546 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1550 /* First nonzero bit of RHS is bit I. Do naive division by
1551 shifting the RHS fully left, and subtracting from LHS if LHS is
1552 at least as big, and then repeating but with one less shift.
1553 This is not very efficient, but is easy to understand. */
1555 rhs.unsignedp = true;
1556 lhs.unsignedp = true;
1557 i = precision - i - 1;
1558 sub = num_lshift (rhs, precision, i);
1560 result.high = result.low = 0;
1563 if (num_greater_eq (lhs, sub, precision))
1565 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1566 if (i >= PART_PRECISION)
1567 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1569 result.low |= (cpp_num_part) 1 << i;
1573 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1577 /* We divide so that the remainder has the sign of the LHS. */
1580 result.unsignedp = unsignedp;
1581 result.overflow = false;
1585 result = num_negate (result, precision);
1586 result.overflow = (num_positive (result, precision) ^ !negate
1587 && !num_zerop (result));
1594 lhs.unsignedp = unsignedp;
1595 lhs.overflow = false;
1597 lhs = num_negate (lhs, precision);