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;
90 case 'f': case 'F': f++; break;
91 case 'l': case 'L': l++; break;
93 case 'j': case 'J': i++; break;
95 /* Disallow fd, ld suffixes. */
104 if (d == 1 && !f && !l) {
109 if (f + d0 + l > 1 || i > 1)
112 /* Allow dd, df, dl suffixes for decimal float constants. */
113 if (d && ((d + f + l != 2) || i))
116 return ((i ? CPP_N_IMAGINARY : 0)
119 l ? CPP_N_LARGE : CPP_N_DEFAULT)
120 | (d ? CPP_N_DFLOAT : 0));
123 /* Subroutine of cpp_classify_number. S points to an integer suffix
124 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
125 flag vector describing the suffix. */
127 interpret_int_suffix (const uchar *s, size_t len)
136 case 'u': case 'U': u++; break;
138 case 'j': case 'J': i++; break;
139 case 'l': case 'L': l++;
140 /* If there are two Ls, they must be adjacent and the same case. */
141 if (l == 2 && s[len] != s[len + 1])
148 if (l > 2 || u > 1 || i > 1)
151 return ((i ? CPP_N_IMAGINARY : 0)
152 | (u ? CPP_N_UNSIGNED : 0)
153 | ((l == 0) ? CPP_N_SMALL
154 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
157 /* Categorize numeric constants according to their field (integer,
158 floating point, or invalid), radix (decimal, octal, hexadecimal),
159 and type suffixes. */
161 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
163 const uchar *str = token->val.str.text;
165 unsigned int max_digit, result, radix;
166 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
168 /* If the lexer has done its job, length one can only be a single
169 digit. Fast-path this very common case. */
170 if (token->val.str.len == 1)
171 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
173 limit = str + token->val.str.len;
174 float_flag = NOT_FLOAT;
178 /* First, interpret the radix. */
184 /* Require at least one hex digit to classify it as hex. */
185 if ((*str == 'x' || *str == 'X')
186 && (str[1] == '.' || ISXDIGIT (str[1])))
191 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
198 /* Now scan for a well-formed integer or float. */
201 unsigned int c = *str++;
203 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
211 if (float_flag == NOT_FLOAT)
212 float_flag = AFTER_POINT;
214 SYNTAX_ERROR ("too many decimal points in number");
216 else if ((radix <= 10 && (c == 'e' || c == 'E'))
217 || (radix == 16 && (c == 'p' || c == 'P')))
219 float_flag = AFTER_EXPON;
224 /* Start of suffix. */
230 if (float_flag != NOT_FLOAT && radix == 8)
233 if (max_digit >= radix)
236 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
238 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
241 if (float_flag != NOT_FLOAT)
245 cpp_error (pfile, CPP_DL_ERROR,
246 "invalid prefix \"0b\" for floating constant");
247 return CPP_N_INVALID;
250 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
251 cpp_error (pfile, CPP_DL_PEDWARN,
252 "use of C99 hexadecimal floating constant");
254 if (float_flag == AFTER_EXPON)
256 if (*str == '+' || *str == '-')
259 /* Exponent is decimal, even if string is a hex float. */
261 SYNTAX_ERROR ("exponent has no digits");
265 while (ISDIGIT (*str));
267 else if (radix == 16)
268 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
270 result = interpret_float_suffix (str, limit - str);
273 cpp_error (pfile, CPP_DL_ERROR,
274 "invalid suffix \"%.*s\" on floating constant",
275 (int) (limit - str), str);
276 return CPP_N_INVALID;
279 /* Traditional C didn't accept any floating suffixes. */
281 && CPP_WTRADITIONAL (pfile)
282 && ! cpp_sys_macro_p (pfile))
283 cpp_error (pfile, CPP_DL_WARNING,
284 "traditional C rejects the \"%.*s\" suffix",
285 (int) (limit - str), str);
287 /* A suffix for double is a GCC extension via decimal float support.
288 If the suffix also specifies an imaginary value we'll catch that
290 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
291 cpp_error (pfile, CPP_DL_PEDWARN,
292 "suffix for double constant is a GCC extension");
294 /* Radix must be 10 for decimal floats. */
295 if ((result & CPP_N_DFLOAT) && radix != 10)
297 cpp_error (pfile, CPP_DL_ERROR,
298 "invalid suffix \"%.*s\" with hexadecimal floating constant",
299 (int) (limit - str), str);
300 return CPP_N_INVALID;
303 result |= CPP_N_FLOATING;
307 result = interpret_int_suffix (str, limit - str);
310 cpp_error (pfile, CPP_DL_ERROR,
311 "invalid suffix \"%.*s\" on integer constant",
312 (int) (limit - str), str);
313 return CPP_N_INVALID;
316 /* Traditional C only accepted the 'L' suffix.
317 Suppress warning about 'LL' with -Wno-long-long. */
318 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
320 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
321 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
323 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
324 cpp_error (pfile, CPP_DL_WARNING,
325 "traditional C rejects the \"%.*s\" suffix",
326 (int) (limit - str), str);
329 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
330 && ! CPP_OPTION (pfile, c99)
331 && CPP_OPTION (pfile, warn_long_long))
332 cpp_error (pfile, CPP_DL_PEDWARN,
333 "use of C99 long long integer constant");
335 result |= CPP_N_INTEGER;
338 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
339 cpp_error (pfile, CPP_DL_PEDWARN,
340 "imaginary constants are a GCC extension");
341 if (radix == 2 && CPP_PEDANTIC (pfile))
342 cpp_error (pfile, CPP_DL_PEDWARN,
343 "binary constants are a GCC extension");
346 result |= CPP_N_DECIMAL;
347 else if (radix == 16)
350 result |= CPP_N_BINARY;
352 result |= CPP_N_OCTAL;
357 return CPP_N_INVALID;
360 /* cpp_interpret_integer converts an integer constant into a cpp_num,
361 of precision options->precision.
363 We do not provide any interface for decimal->float conversion,
364 because the preprocessor doesn't need it and we don't want to
365 drag in GCC's floating point emulator. */
367 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
370 const uchar *p, *end;
375 result.unsignedp = !!(type & CPP_N_UNSIGNED);
376 result.overflow = false;
378 p = token->val.str.text;
379 end = p + token->val.str.len;
381 /* Common case of a single digit. */
382 if (token->val.str.len == 1)
383 result.low = p[0] - '0';
387 size_t precision = CPP_OPTION (pfile, precision);
388 unsigned int base = 10, c = 0;
389 bool overflow = false;
391 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
396 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
401 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
407 /* We can add a digit to numbers strictly less than this without
408 needing the precision and slowness of double integers. */
409 max = ~(cpp_num_part) 0;
410 if (precision < PART_PRECISION)
411 max >>= PART_PRECISION - precision;
412 max = (max - base + 1) / base + 1;
418 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
423 /* Strict inequality for when max is set to zero. */
424 if (result.low < max)
425 result.low = result.low * base + c;
428 result = append_digit (result, c, base, precision);
429 overflow |= result.overflow;
435 cpp_error (pfile, CPP_DL_PEDWARN,
436 "integer constant is too large for its type");
437 /* If too big to be signed, consider it unsigned. Only warn for
438 decimal numbers. Traditional numbers were always signed (but
439 we still honor an explicit U suffix); but we only have
440 traditional semantics in directives. */
441 else if (!result.unsignedp
442 && !(CPP_OPTION (pfile, traditional)
443 && pfile->state.in_directive)
444 && !num_positive (result, precision))
447 cpp_error (pfile, CPP_DL_WARNING,
448 "integer constant is so large that it is unsigned");
449 result.unsignedp = true;
456 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
458 append_digit (cpp_num num, int digit, int base, size_t precision)
463 cpp_num_part add_high, add_low;
465 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
466 need to worry about add_high overflowing. */
480 overflow = !!(num.high >> (PART_PRECISION - shift));
481 result.high = num.high << shift;
482 result.low = num.low << shift;
483 result.high |= num.low >> (PART_PRECISION - shift);
484 result.unsignedp = num.unsignedp;
488 add_low = num.low << 1;
489 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
492 add_high = add_low = 0;
494 if (add_low + digit < add_low)
498 if (result.low + add_low < result.low)
500 if (result.high + add_high < result.high)
503 result.low += add_low;
504 result.high += add_high;
505 result.overflow = overflow;
507 /* The above code catches overflow of a cpp_num type. This catches
508 overflow of the (possibly shorter) target precision. */
509 num.low = result.low;
510 num.high = result.high;
511 result = num_trim (result, precision);
512 if (!num_eq (result, num))
513 result.overflow = true;
518 /* Handle meeting "defined" in a preprocessor expression. */
520 parse_defined (cpp_reader *pfile)
524 cpp_hashnode *node = 0;
525 const cpp_token *token;
526 cpp_context *initial_context = pfile->context;
528 /* Don't expand macros. */
529 pfile->state.prevent_expansion++;
531 token = cpp_get_token (pfile);
532 if (token->type == CPP_OPEN_PAREN)
535 token = cpp_get_token (pfile);
538 if (token->type == CPP_NAME)
540 node = token->val.node;
541 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
543 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
549 cpp_error (pfile, CPP_DL_ERROR,
550 "operator \"defined\" requires an identifier");
551 if (token->flags & NAMED_OP)
556 op.type = token->type;
557 cpp_error (pfile, CPP_DL_ERROR,
558 "(\"%s\" is an alternative token for \"%s\" in C++)",
559 cpp_token_as_text (pfile, token),
560 cpp_token_as_text (pfile, &op));
566 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
567 cpp_error (pfile, CPP_DL_WARNING,
568 "this use of \"defined\" may not be portable");
570 _cpp_mark_macro_used (node);
572 /* A possible controlling macro of the form #if !defined ().
573 _cpp_parse_expr checks there was no other junk on the line. */
574 pfile->mi_ind_cmacro = node;
577 pfile->state.prevent_expansion--;
579 result.unsignedp = false;
581 result.overflow = false;
582 result.low = node && node->type == NT_MACRO;
586 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
587 number or character constant, or the result of the "defined" or "#"
590 eval_token (cpp_reader *pfile, const cpp_token *token)
596 result.unsignedp = false;
597 result.overflow = false;
602 temp = cpp_classify_number (pfile, token);
603 switch (temp & CPP_N_CATEGORY)
606 cpp_error (pfile, CPP_DL_ERROR,
607 "floating constant in preprocessor expression");
610 if (!(temp & CPP_N_IMAGINARY))
611 return cpp_interpret_integer (pfile, token, temp);
612 cpp_error (pfile, CPP_DL_ERROR,
613 "imaginary number in preprocessor expression");
617 /* Error already issued. */
620 result.high = result.low = 0;
626 cppchar_t cc = cpp_interpret_charconst (pfile, token,
631 /* Sign-extend the result if necessary. */
632 if (!unsignedp && (cppchar_signed_t) cc < 0)
634 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
635 result.low |= ~(~(cpp_num_part) 0
636 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
637 result.high = ~(cpp_num_part) 0;
638 result = num_trim (result, CPP_OPTION (pfile, precision));
644 if (token->val.node == pfile->spec_nodes.n_defined)
645 return parse_defined (pfile);
646 else if (CPP_OPTION (pfile, cplusplus)
647 && (token->val.node == pfile->spec_nodes.n_true
648 || token->val.node == pfile->spec_nodes.n_false))
651 result.low = (token->val.node == pfile->spec_nodes.n_true);
657 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
658 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
659 NODE_NAME (token->val.node));
663 default: /* CPP_HASH */
664 _cpp_test_assertion (pfile, &temp);
669 result.unsignedp = !!unsignedp;
673 /* Operator precedence and flags table.
675 After an operator is returned from the lexer, if it has priority less
676 than the operator on the top of the stack, we reduce the stack by one
677 operator and repeat the test. Since equal priorities do not reduce,
678 this is naturally right-associative.
680 We handle left-associative operators by decrementing the priority of
681 just-lexed operators by one, but retaining the priority of operators
682 already on the stack.
684 The remaining cases are '(' and ')'. We handle '(' by skipping the
685 reduction phase completely. ')' is given lower priority than
686 everything else, including '(', effectively forcing a reduction of the
687 parenthesized expression. If there is a matching '(', the routine
688 reduce() exits immediately. If the normal exit route sees a ')', then
689 there cannot have been a matching '(' and an error message is output.
691 The parser assumes all shifted operators require a left operand unless
692 the flag NO_L_OPERAND is set. These semantics are automatic; any
693 extra semantics need to be handled with operator-specific code. */
695 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
696 operand changes because of integer promotions. */
697 #define NO_L_OPERAND (1 << 0)
698 #define LEFT_ASSOC (1 << 1)
699 #define CHECK_PROMOTION (1 << 2)
701 /* Operator to priority map. Must be in the same order as the first
702 N entries of enum cpp_ttype. */
703 static const struct cpp_operator
709 /* EQ */ {0, 0}, /* Shouldn't happen. */
710 /* NOT */ {16, NO_L_OPERAND},
711 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
712 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
713 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
714 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
715 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
716 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
717 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
718 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
719 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
720 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
721 /* RSHIFT */ {13, LEFT_ASSOC},
722 /* LSHIFT */ {13, LEFT_ASSOC},
724 /* COMPL */ {16, NO_L_OPERAND},
725 /* AND_AND */ {6, LEFT_ASSOC},
726 /* OR_OR */ {5, LEFT_ASSOC},
728 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
729 /* COMMA */ {2, LEFT_ASSOC},
730 /* OPEN_PAREN */ {1, NO_L_OPERAND},
731 /* CLOSE_PAREN */ {0, 0},
733 /* EQ_EQ */ {11, LEFT_ASSOC},
734 /* NOT_EQ */ {11, LEFT_ASSOC},
735 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
736 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
737 /* UPLUS */ {16, NO_L_OPERAND},
738 /* UMINUS */ {16, NO_L_OPERAND}
741 /* Parse and evaluate a C expression, reading from PFILE.
742 Returns the truth value of the expression.
744 The implementation is an operator precedence parser, i.e. a
745 bottom-up parser, using a stack for not-yet-reduced tokens.
747 The stack base is op_stack, and the current stack pointer is 'top'.
748 There is a stack element for each operator (only), and the most
749 recently pushed operator is 'top->op'. An operand (value) is
750 stored in the 'value' field of the stack element of the operator
753 _cpp_parse_expr (cpp_reader *pfile)
755 struct op *top = pfile->op_stack;
756 unsigned int lex_count;
757 bool saw_leading_not, want_value = true;
759 pfile->state.skip_eval = 0;
761 /* Set up detection of #if ! defined(). */
762 pfile->mi_ind_cmacro = 0;
763 saw_leading_not = false;
766 /* Lowest priority operator prevents further reductions. */
774 op.token = cpp_get_token (pfile);
775 op.op = op.token->type;
779 /* These tokens convert into values. */
786 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
787 cpp_token_as_text (pfile, op.token));
789 top->value = eval_token (pfile, op.token);
793 saw_leading_not = lex_count == 1;
805 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
806 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
807 cpp_token_as_text (pfile, op.token));
811 /* Check we have a value or operator as appropriate. */
812 if (optab[op.op].flags & NO_L_OPERAND)
815 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
816 cpp_token_as_text (pfile, op.token));
820 /* We want a number (or expression) and haven't got one.
821 Try to emit a specific diagnostic. */
822 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
823 SYNTAX_ERROR ("missing expression between '(' and ')'");
825 if (op.op == CPP_EOF && top->op == CPP_EOF)
826 SYNTAX_ERROR ("#if with no expression");
828 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
829 SYNTAX_ERROR2 ("operator '%s' has no right operand",
830 cpp_token_as_text (pfile, top->token));
831 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
832 /* Complain about missing paren during reduction. */;
834 SYNTAX_ERROR2 ("operator '%s' has no left operand",
835 cpp_token_as_text (pfile, op.token));
838 top = reduce (pfile, top, op.op);
842 if (op.op == CPP_EOF)
847 case CPP_CLOSE_PAREN:
850 if (!num_zerop (top->value))
851 pfile->state.skip_eval++;
855 if (num_zerop (top->value))
856 pfile->state.skip_eval++;
859 if (top->op != CPP_QUERY)
860 SYNTAX_ERROR (" ':' without preceding '?'");
861 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
862 pfile->state.skip_eval++;
864 pfile->state.skip_eval--;
871 /* Check for and handle stack overflow. */
872 if (++top == pfile->op_limit)
873 top = _cpp_expand_op_stack (pfile);
876 top->token = op.token;
879 /* The controlling macro expression is only valid if we called lex 3
880 times: <!> <defined expression> and <EOF>. push_conditional ()
881 checks that we are at top-of-file. */
882 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
883 pfile->mi_ind_cmacro = 0;
885 if (top != pfile->op_stack)
887 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in #if");
889 return false; /* Return false on syntax error. */
892 return !num_zerop (top->value);
895 /* Reduce the operator / value stack if possible, in preparation for
896 pushing operator OP. Returns NULL on error, otherwise the top of
899 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
903 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
906 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
910 if (op == CPP_OPEN_PAREN)
913 /* Decrement the priority of left-associative operators to force a
914 reduction with operators of otherwise equal priority. */
915 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
916 while (prio < optab[top->op].prio)
918 if (CPP_OPTION (pfile, warn_num_sign_change)
919 && optab[top->op].flags & CHECK_PROMOTION)
920 check_promotion (pfile, top);
928 top[-1].value = num_unary_op (pfile, top->value, top->op);
936 top[-1].value = num_binary_op (pfile, top[-1].value,
937 top->value, top->op);
945 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
951 = num_equality_op (pfile, top[-1].value, top->value, top->op);
958 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
962 top[-1].value = num_mul (pfile, top[-1].value, top->value);
967 top[-1].value = num_div_op (pfile, top[-1].value,
968 top->value, top->op);
973 if (!num_zerop (top->value))
974 pfile->state.skip_eval--;
975 top->value.low = (!num_zerop (top->value)
976 || !num_zerop (top[1].value));
978 top->value.unsignedp = false;
979 top->value.overflow = false;
984 if (num_zerop (top->value))
985 pfile->state.skip_eval--;
986 top->value.low = (!num_zerop (top->value)
987 && !num_zerop (top[1].value));
989 top->value.unsignedp = false;
990 top->value.overflow = false;
994 if (op != CPP_CLOSE_PAREN)
996 cpp_error (pfile, CPP_DL_ERROR, "missing ')' in expression");
1000 top->value = top[1].value;
1005 if (!num_zerop (top->value))
1007 pfile->state.skip_eval--;
1008 top->value = top[1].value;
1011 top->value = top[2].value;
1012 top->value.unsignedp = (top[1].value.unsignedp
1013 || top[2].value.unsignedp);
1017 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1025 if (top->value.overflow && !pfile->state.skip_eval)
1026 cpp_error (pfile, CPP_DL_PEDWARN,
1027 "integer overflow in preprocessor expression");
1030 if (op == CPP_CLOSE_PAREN)
1032 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1039 /* Returns the position of the old top of stack after expansion. */
1041 _cpp_expand_op_stack (cpp_reader *pfile)
1043 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1044 size_t new_size = old_size * 2 + 20;
1046 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1047 pfile->op_limit = pfile->op_stack + new_size;
1049 return pfile->op_stack + old_size;
1052 /* Emits a warning if the effective sign of either operand of OP
1053 changes because of integer promotions. */
1055 check_promotion (cpp_reader *pfile, const struct op *op)
1057 if (op->value.unsignedp == op[-1].value.unsignedp)
1060 if (op->value.unsignedp)
1062 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1063 cpp_error (pfile, CPP_DL_WARNING,
1064 "the left operand of \"%s\" changes sign when promoted",
1065 cpp_token_as_text (pfile, op->token));
1067 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1068 cpp_error (pfile, CPP_DL_WARNING,
1069 "the right operand of \"%s\" changes sign when promoted",
1070 cpp_token_as_text (pfile, op->token));
1073 /* Clears the unused high order bits of the number pointed to by PNUM. */
1075 num_trim (cpp_num num, size_t precision)
1077 if (precision > PART_PRECISION)
1079 precision -= PART_PRECISION;
1080 if (precision < PART_PRECISION)
1081 num.high &= ((cpp_num_part) 1 << precision) - 1;
1085 if (precision < PART_PRECISION)
1086 num.low &= ((cpp_num_part) 1 << precision) - 1;
1093 /* True iff A (presumed signed) >= 0. */
1095 num_positive (cpp_num num, size_t precision)
1097 if (precision > PART_PRECISION)
1099 precision -= PART_PRECISION;
1100 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1103 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1106 /* Sign extend a number, with PRECISION significant bits and all
1107 others assumed clear, to fill out a cpp_num structure. */
1109 cpp_num_sign_extend (cpp_num num, size_t precision)
1113 if (precision > PART_PRECISION)
1115 precision -= PART_PRECISION;
1116 if (precision < PART_PRECISION
1117 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1118 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1120 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1122 if (precision < PART_PRECISION)
1123 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1124 num.high = ~(cpp_num_part) 0;
1131 /* Returns the negative of NUM. */
1133 num_negate (cpp_num num, size_t precision)
1138 num.high = ~num.high;
1142 num = num_trim (num, precision);
1143 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1148 /* Returns true if A >= B. */
1150 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1154 unsignedp = pa.unsignedp || pb.unsignedp;
1158 /* Both numbers have signed type. If they are of different
1159 sign, the answer is the sign of A. */
1160 unsignedp = num_positive (pa, precision);
1162 if (unsignedp != num_positive (pb, precision))
1165 /* Otherwise we can do an unsigned comparison. */
1168 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1171 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1173 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1174 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1176 lhs.overflow = false;
1177 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1179 /* As excess precision is zeroed, there is no need to num_trim () as
1180 these operations cannot introduce a set bit there. */
1184 lhs.high &= rhs.high;
1186 else if (op == CPP_OR)
1189 lhs.high |= rhs.high;
1194 lhs.high ^= rhs.high;
1200 /* Returns LHS OP RHS, where OP is an inequality. */
1202 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1205 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1207 if (op == CPP_GREATER_EQ)
1209 else if (op == CPP_LESS)
1211 else if (op == CPP_GREATER)
1212 lhs.low = gte && !num_eq (lhs, rhs);
1213 else /* CPP_LESS_EQ. */
1214 lhs.low = !gte || num_eq (lhs, rhs);
1217 lhs.overflow = false;
1218 lhs.unsignedp = false;
1222 /* Returns LHS OP RHS, where OP is == or !=. */
1224 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1225 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1227 /* Work around a 3.0.4 bug; see PR 6950. */
1228 bool eq = num_eq (lhs, rhs);
1229 if (op == CPP_NOT_EQ)
1233 lhs.overflow = false;
1234 lhs.unsignedp = false;
1238 /* Shift NUM, of width PRECISION, right by N bits. */
1240 num_rshift (cpp_num num, size_t precision, size_t n)
1242 cpp_num_part sign_mask;
1243 bool x = num_positive (num, precision);
1245 if (num.unsignedp || x)
1248 sign_mask = ~(cpp_num_part) 0;
1251 num.high = num.low = sign_mask;
1255 if (precision < PART_PRECISION)
1256 num.high = sign_mask, num.low |= sign_mask << precision;
1257 else if (precision < 2 * PART_PRECISION)
1258 num.high |= sign_mask << (precision - PART_PRECISION);
1260 if (n >= PART_PRECISION)
1262 n -= PART_PRECISION;
1264 num.high = sign_mask;
1269 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1270 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1274 num = num_trim (num, precision);
1275 num.overflow = false;
1279 /* Shift NUM, of width PRECISION, left by N bits. */
1281 num_lshift (cpp_num num, size_t precision, size_t n)
1285 num.overflow = !num.unsignedp && !num_zerop (num);
1286 num.high = num.low = 0;
1290 cpp_num orig, maybe_orig;
1294 if (m >= PART_PRECISION)
1296 m -= PART_PRECISION;
1302 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1305 num = num_trim (num, precision);
1308 num.overflow = false;
1311 maybe_orig = num_rshift (num, precision, n);
1312 num.overflow = !num_eq (orig, maybe_orig);
1319 /* The four unary operators: +, -, ! and ~. */
1321 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1326 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1327 cpp_error (pfile, CPP_DL_WARNING,
1328 "traditional C rejects the unary plus operator");
1329 num.overflow = false;
1333 num = num_negate (num, CPP_OPTION (pfile, precision));
1337 num.high = ~num.high;
1339 num = num_trim (num, CPP_OPTION (pfile, precision));
1340 num.overflow = false;
1343 default: /* case CPP_NOT: */
1344 num.low = num_zerop (num);
1346 num.overflow = false;
1347 num.unsignedp = false;
1354 /* The various binary operators. */
1356 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1359 size_t precision = CPP_OPTION (pfile, precision);
1367 if (!rhs.unsignedp && !num_positive (rhs, precision))
1369 /* A negative shift is a positive shift the other way. */
1370 if (op == CPP_LSHIFT)
1374 rhs = num_negate (rhs, precision);
1377 n = ~0; /* Maximal. */
1380 if (op == CPP_LSHIFT)
1381 lhs = num_lshift (lhs, precision, n);
1383 lhs = num_rshift (lhs, precision, n);
1388 rhs = num_negate (rhs, precision);
1390 result.low = lhs.low + rhs.low;
1391 result.high = lhs.high + rhs.high;
1392 if (result.low < lhs.low)
1394 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1395 result.overflow = false;
1397 result = num_trim (result, precision);
1398 if (!result.unsignedp)
1400 bool lhsp = num_positive (lhs, precision);
1401 result.overflow = (lhsp == num_positive (rhs, precision)
1402 && lhsp != num_positive (result, precision));
1407 default: /* case CPP_COMMA: */
1408 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1409 || !pfile->state.skip_eval))
1410 cpp_error (pfile, CPP_DL_PEDWARN,
1411 "comma operator in operand of #if");
1419 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1422 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1425 cpp_num_part middle[2], temp;
1427 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1428 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1430 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1431 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1434 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1435 if (result.low < temp)
1439 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1440 if (result.low < temp)
1443 result.high += HIGH_PART (middle[0]);
1444 result.high += HIGH_PART (middle[1]);
1445 result.unsignedp = true;
1446 result.overflow = false;
1451 /* Multiply two preprocessing numbers. */
1453 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1455 cpp_num result, temp;
1456 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1457 bool overflow, negate = false;
1458 size_t precision = CPP_OPTION (pfile, precision);
1460 /* Prepare for unsigned multiplication. */
1463 if (!num_positive (lhs, precision))
1464 negate = !negate, lhs = num_negate (lhs, precision);
1465 if (!num_positive (rhs, precision))
1466 negate = !negate, rhs = num_negate (rhs, precision);
1469 overflow = lhs.high && rhs.high;
1470 result = num_part_mul (lhs.low, rhs.low);
1472 temp = num_part_mul (lhs.high, rhs.low);
1473 result.high += temp.low;
1477 temp = num_part_mul (lhs.low, rhs.high);
1478 result.high += temp.low;
1482 temp.low = result.low, temp.high = result.high;
1483 result = num_trim (result, precision);
1484 if (!num_eq (result, temp))
1488 result = num_negate (result, precision);
1491 result.overflow = false;
1493 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1494 && !num_zerop (result));
1495 result.unsignedp = unsignedp;
1500 /* Divide two preprocessing numbers, returning the answer or the
1501 remainder depending upon OP. */
1503 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1505 cpp_num result, sub;
1507 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1508 bool negate = false, lhs_neg = false;
1509 size_t i, precision = CPP_OPTION (pfile, precision);
1511 /* Prepare for unsigned division. */
1514 if (!num_positive (lhs, precision))
1515 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1516 if (!num_positive (rhs, precision))
1517 negate = !negate, rhs = num_negate (rhs, precision);
1520 /* Find the high bit. */
1524 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1525 for (; ; i--, mask >>= 1)
1526 if (rhs.high & mask)
1531 if (precision > PART_PRECISION)
1532 i = precision - PART_PRECISION - 1;
1535 mask = (cpp_num_part) 1 << i;
1536 for (; ; i--, mask >>= 1)
1542 if (!pfile->state.skip_eval)
1543 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1547 /* First nonzero bit of RHS is bit I. Do naive division by
1548 shifting the RHS fully left, and subtracting from LHS if LHS is
1549 at least as big, and then repeating but with one less shift.
1550 This is not very efficient, but is easy to understand. */
1552 rhs.unsignedp = true;
1553 lhs.unsignedp = true;
1554 i = precision - i - 1;
1555 sub = num_lshift (rhs, precision, i);
1557 result.high = result.low = 0;
1560 if (num_greater_eq (lhs, sub, precision))
1562 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1563 if (i >= PART_PRECISION)
1564 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1566 result.low |= (cpp_num_part) 1 << i;
1570 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1574 /* We divide so that the remainder has the sign of the LHS. */
1577 result.unsignedp = unsignedp;
1578 result.overflow = false;
1582 result = num_negate (result, precision);
1583 result.overflow = num_positive (result, precision) ^ !negate;
1590 lhs.unsignedp = unsignedp;
1591 lhs.overflow = false;
1593 lhs = num_negate (lhs, precision);