1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
32 const cpp_token *token; /* The token forming op (for diagnostics). */
33 cpp_num value; /* The value logically "right" of op. */
34 source_location loc; /* The location of this value. */
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)
86 size_t f, d, l, w, q, i;
89 f = d = l = w = q = i = 0;
91 /* Process decimal float suffixes, which are two letters starting
92 with d or D. Order and case are significant. */
93 if (len == 2 && (*s == 'd' || *s == 'D'))
95 bool uppercase = (*s == 'D');
98 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
99 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
100 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
101 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
102 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
103 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
105 /* Additional two-character suffixes beginning with D are not
106 for decimal float constants. */
111 /* Recognize a fixed-point suffix. */
114 case 'k': case 'K': flags = CPP_N_ACCUM; break;
115 case 'r': case 'R': flags = CPP_N_FRACT; break;
119 /* Continue processing a fixed-point suffix. The suffix is case
120 insensitive except for ll or LL. Order is significant. */
127 if (*s == 'u' || *s == 'U')
129 flags |= CPP_N_UNSIGNED;
140 return flags |= CPP_N_SMALL;
144 return flags |= CPP_N_MEDIUM;
145 if (len == 2 && s[1] == 'l')
146 return flags |= CPP_N_LARGE;
150 return flags |= CPP_N_MEDIUM;
151 if (len == 2 && s[1] == 'L')
152 return flags |= CPP_N_LARGE;
157 /* Anything left at this point is invalid. */
161 /* In any remaining valid suffix, the case and order don't matter. */
165 case 'f': case 'F': f++; break;
166 case 'd': case 'D': d++; break;
167 case 'l': case 'L': l++; break;
168 case 'w': case 'W': w++; break;
169 case 'q': case 'Q': q++; break;
171 case 'j': case 'J': i++; break;
176 if (f + d + l + w + q > 1 || i > 1)
179 return ((i ? CPP_N_IMAGINARY : 0)
184 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
187 /* Subroutine of cpp_classify_number. S points to an integer suffix
188 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
189 flag vector describing the suffix. */
191 interpret_int_suffix (const uchar *s, size_t len)
200 case 'u': case 'U': u++; break;
202 case 'j': case 'J': i++; break;
203 case 'l': case 'L': l++;
204 /* If there are two Ls, they must be adjacent and the same case. */
205 if (l == 2 && s[len] != s[len + 1])
212 if (l > 2 || u > 1 || i > 1)
215 return ((i ? CPP_N_IMAGINARY : 0)
216 | (u ? CPP_N_UNSIGNED : 0)
217 | ((l == 0) ? CPP_N_SMALL
218 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
221 /* Categorize numeric constants according to their field (integer,
222 floating point, or invalid), radix (decimal, octal, hexadecimal),
223 and type suffixes. */
225 cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
227 const uchar *str = token->val.str.text;
229 unsigned int max_digit, result, radix;
230 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
232 /* If the lexer has done its job, length one can only be a single
233 digit. Fast-path this very common case. */
234 if (token->val.str.len == 1)
235 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
237 limit = str + token->val.str.len;
238 float_flag = NOT_FLOAT;
242 /* First, interpret the radix. */
248 /* Require at least one hex digit to classify it as hex. */
249 if ((*str == 'x' || *str == 'X')
250 && (str[1] == '.' || ISXDIGIT (str[1])))
255 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
262 /* Now scan for a well-formed integer or float. */
265 unsigned int c = *str++;
267 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
275 if (float_flag == NOT_FLOAT)
276 float_flag = AFTER_POINT;
278 SYNTAX_ERROR ("too many decimal points in number");
280 else if ((radix <= 10 && (c == 'e' || c == 'E'))
281 || (radix == 16 && (c == 'p' || c == 'P')))
283 float_flag = AFTER_EXPON;
288 /* Start of suffix. */
294 /* The suffix may be for decimal fixed-point constants without exponent. */
295 if (radix != 16 && float_flag == NOT_FLOAT)
297 result = interpret_float_suffix (str, limit - str);
298 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
300 result |= CPP_N_FLOATING;
301 /* We need to restore the radix to 10, if the radix is 8. */
305 if (CPP_PEDANTIC (pfile))
306 cpp_error (pfile, CPP_DL_PEDWARN,
307 "fixed-point constants are a GCC extension");
314 if (float_flag != NOT_FLOAT && radix == 8)
317 if (max_digit >= radix)
320 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
322 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
325 if (float_flag != NOT_FLOAT)
329 cpp_error (pfile, CPP_DL_ERROR,
330 "invalid prefix \"0b\" for floating constant");
331 return CPP_N_INVALID;
334 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
335 cpp_error (pfile, CPP_DL_PEDWARN,
336 "use of C99 hexadecimal floating constant");
338 if (float_flag == AFTER_EXPON)
340 if (*str == '+' || *str == '-')
343 /* Exponent is decimal, even if string is a hex float. */
345 SYNTAX_ERROR ("exponent has no digits");
349 while (ISDIGIT (*str));
351 else if (radix == 16)
352 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
354 result = interpret_float_suffix (str, limit - str);
357 cpp_error (pfile, CPP_DL_ERROR,
358 "invalid suffix \"%.*s\" on floating constant",
359 (int) (limit - str), str);
360 return CPP_N_INVALID;
363 /* Traditional C didn't accept any floating suffixes. */
365 && CPP_WTRADITIONAL (pfile)
366 && ! cpp_sys_macro_p (pfile))
367 cpp_error (pfile, CPP_DL_WARNING,
368 "traditional C rejects the \"%.*s\" suffix",
369 (int) (limit - str), str);
371 /* A suffix for double is a GCC extension via decimal float support.
372 If the suffix also specifies an imaginary value we'll catch that
374 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
375 cpp_error (pfile, CPP_DL_PEDWARN,
376 "suffix for double constant is a GCC extension");
378 /* Radix must be 10 for decimal floats. */
379 if ((result & CPP_N_DFLOAT) && radix != 10)
381 cpp_error (pfile, CPP_DL_ERROR,
382 "invalid suffix \"%.*s\" with hexadecimal floating constant",
383 (int) (limit - str), str);
384 return CPP_N_INVALID;
387 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
388 cpp_error (pfile, CPP_DL_PEDWARN,
389 "fixed-point constants are a GCC extension");
391 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
392 cpp_error (pfile, CPP_DL_PEDWARN,
393 "decimal float constants are a GCC extension");
395 result |= CPP_N_FLOATING;
399 result = interpret_int_suffix (str, limit - str);
402 cpp_error (pfile, CPP_DL_ERROR,
403 "invalid suffix \"%.*s\" on integer constant",
404 (int) (limit - str), str);
405 return CPP_N_INVALID;
408 /* Traditional C only accepted the 'L' suffix.
409 Suppress warning about 'LL' with -Wno-long-long. */
410 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
412 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
413 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
415 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
416 cpp_error (pfile, CPP_DL_WARNING,
417 "traditional C rejects the \"%.*s\" suffix",
418 (int) (limit - str), str);
421 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
422 && CPP_OPTION (pfile, warn_long_long))
424 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN,
425 CPP_OPTION (pfile, cplusplus)
426 ? "use of C++0x long long integer constant"
427 : "use of C99 long long integer constant");
429 result |= CPP_N_INTEGER;
433 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
434 cpp_error (pfile, CPP_DL_PEDWARN,
435 "imaginary constants are a GCC extension");
436 if (radix == 2 && CPP_PEDANTIC (pfile))
437 cpp_error (pfile, CPP_DL_PEDWARN,
438 "binary constants are a GCC extension");
441 result |= CPP_N_DECIMAL;
442 else if (radix == 16)
445 result |= CPP_N_BINARY;
447 result |= CPP_N_OCTAL;
452 return CPP_N_INVALID;
455 /* cpp_interpret_integer converts an integer constant into a cpp_num,
456 of precision options->precision.
458 We do not provide any interface for decimal->float conversion,
459 because the preprocessor doesn't need it and we don't want to
460 drag in GCC's floating point emulator. */
462 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
465 const uchar *p, *end;
470 result.unsignedp = !!(type & CPP_N_UNSIGNED);
471 result.overflow = false;
473 p = token->val.str.text;
474 end = p + token->val.str.len;
476 /* Common case of a single digit. */
477 if (token->val.str.len == 1)
478 result.low = p[0] - '0';
482 size_t precision = CPP_OPTION (pfile, precision);
483 unsigned int base = 10, c = 0;
484 bool overflow = false;
486 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
491 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
496 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
502 /* We can add a digit to numbers strictly less than this without
503 needing the precision and slowness of double integers. */
504 max = ~(cpp_num_part) 0;
505 if (precision < PART_PRECISION)
506 max >>= PART_PRECISION - precision;
507 max = (max - base + 1) / base + 1;
513 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
518 /* Strict inequality for when max is set to zero. */
519 if (result.low < max)
520 result.low = result.low * base + c;
523 result = append_digit (result, c, base, precision);
524 overflow |= result.overflow;
530 cpp_error (pfile, CPP_DL_PEDWARN,
531 "integer constant is too large for its type");
532 /* If too big to be signed, consider it unsigned. Only warn for
533 decimal numbers. Traditional numbers were always signed (but
534 we still honor an explicit U suffix); but we only have
535 traditional semantics in directives. */
536 else if (!result.unsignedp
537 && !(CPP_OPTION (pfile, traditional)
538 && pfile->state.in_directive)
539 && !num_positive (result, precision))
542 cpp_error (pfile, CPP_DL_WARNING,
543 "integer constant is so large that it is unsigned");
544 result.unsignedp = true;
551 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
553 append_digit (cpp_num num, int digit, int base, size_t precision)
558 cpp_num_part add_high, add_low;
560 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
561 need to worry about add_high overflowing. */
575 overflow = !!(num.high >> (PART_PRECISION - shift));
576 result.high = num.high << shift;
577 result.low = num.low << shift;
578 result.high |= num.low >> (PART_PRECISION - shift);
579 result.unsignedp = num.unsignedp;
583 add_low = num.low << 1;
584 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
587 add_high = add_low = 0;
589 if (add_low + digit < add_low)
593 if (result.low + add_low < result.low)
595 if (result.high + add_high < result.high)
598 result.low += add_low;
599 result.high += add_high;
600 result.overflow = overflow;
602 /* The above code catches overflow of a cpp_num type. This catches
603 overflow of the (possibly shorter) target precision. */
604 num.low = result.low;
605 num.high = result.high;
606 result = num_trim (result, precision);
607 if (!num_eq (result, num))
608 result.overflow = true;
613 /* Handle meeting "defined" in a preprocessor expression. */
615 parse_defined (cpp_reader *pfile)
619 cpp_hashnode *node = 0;
620 const cpp_token *token;
621 cpp_context *initial_context = pfile->context;
623 /* Don't expand macros. */
624 pfile->state.prevent_expansion++;
626 token = cpp_get_token (pfile);
627 if (token->type == CPP_OPEN_PAREN)
630 token = cpp_get_token (pfile);
633 if (token->type == CPP_NAME)
635 node = token->val.node;
636 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
638 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
644 cpp_error (pfile, CPP_DL_ERROR,
645 "operator \"defined\" requires an identifier");
646 if (token->flags & NAMED_OP)
651 op.type = token->type;
652 cpp_error (pfile, CPP_DL_ERROR,
653 "(\"%s\" is an alternative token for \"%s\" in C++)",
654 cpp_token_as_text (pfile, token),
655 cpp_token_as_text (pfile, &op));
661 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
662 cpp_error (pfile, CPP_DL_WARNING,
663 "this use of \"defined\" may not be portable");
665 _cpp_mark_macro_used (node);
666 if (!(node->flags & NODE_USED))
668 node->flags |= NODE_USED;
669 if (node->type == NT_MACRO)
671 if (pfile->cb.used_define)
672 pfile->cb.used_define (pfile, pfile->directive_line, node);
676 if (pfile->cb.used_undef)
677 pfile->cb.used_undef (pfile, pfile->directive_line, node);
681 /* A possible controlling macro of the form #if !defined ().
682 _cpp_parse_expr checks there was no other junk on the line. */
683 pfile->mi_ind_cmacro = node;
686 pfile->state.prevent_expansion--;
688 result.unsignedp = false;
690 result.overflow = false;
691 result.low = node && node->type == NT_MACRO;
695 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
696 number or character constant, or the result of the "defined" or "#"
699 eval_token (cpp_reader *pfile, const cpp_token *token)
705 result.unsignedp = false;
706 result.overflow = false;
711 temp = cpp_classify_number (pfile, token);
712 switch (temp & CPP_N_CATEGORY)
715 cpp_error (pfile, CPP_DL_ERROR,
716 "floating constant in preprocessor expression");
719 if (!(temp & CPP_N_IMAGINARY))
720 return cpp_interpret_integer (pfile, token, temp);
721 cpp_error (pfile, CPP_DL_ERROR,
722 "imaginary number in preprocessor expression");
726 /* Error already issued. */
729 result.high = result.low = 0;
737 cppchar_t cc = cpp_interpret_charconst (pfile, token,
742 /* Sign-extend the result if necessary. */
743 if (!unsignedp && (cppchar_signed_t) cc < 0)
745 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
746 result.low |= ~(~(cpp_num_part) 0
747 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
748 result.high = ~(cpp_num_part) 0;
749 result = num_trim (result, CPP_OPTION (pfile, precision));
755 if (token->val.node == pfile->spec_nodes.n_defined)
756 return parse_defined (pfile);
757 else if (CPP_OPTION (pfile, cplusplus)
758 && (token->val.node == pfile->spec_nodes.n_true
759 || token->val.node == pfile->spec_nodes.n_false))
762 result.low = (token->val.node == pfile->spec_nodes.n_true);
768 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
769 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
770 NODE_NAME (token->val.node));
775 if (!pfile->state.skipping)
777 /* A pedantic warning takes precedence over a deprecated
779 if (CPP_PEDANTIC (pfile))
780 cpp_error (pfile, CPP_DL_PEDWARN,
781 "assertions are a GCC extension");
782 else if (CPP_OPTION (pfile, warn_deprecated))
783 cpp_error (pfile, CPP_DL_WARNING,
784 "assertions are a deprecated extension");
786 _cpp_test_assertion (pfile, &temp);
795 result.unsignedp = !!unsignedp;
799 /* Operator precedence and flags table.
801 After an operator is returned from the lexer, if it has priority less
802 than the operator on the top of the stack, we reduce the stack by one
803 operator and repeat the test. Since equal priorities do not reduce,
804 this is naturally right-associative.
806 We handle left-associative operators by decrementing the priority of
807 just-lexed operators by one, but retaining the priority of operators
808 already on the stack.
810 The remaining cases are '(' and ')'. We handle '(' by skipping the
811 reduction phase completely. ')' is given lower priority than
812 everything else, including '(', effectively forcing a reduction of the
813 parenthesized expression. If there is a matching '(', the routine
814 reduce() exits immediately. If the normal exit route sees a ')', then
815 there cannot have been a matching '(' and an error message is output.
817 The parser assumes all shifted operators require a left operand unless
818 the flag NO_L_OPERAND is set. These semantics are automatic; any
819 extra semantics need to be handled with operator-specific code. */
821 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
822 operand changes because of integer promotions. */
823 #define NO_L_OPERAND (1 << 0)
824 #define LEFT_ASSOC (1 << 1)
825 #define CHECK_PROMOTION (1 << 2)
827 /* Operator to priority map. Must be in the same order as the first
828 N entries of enum cpp_ttype. */
829 static const struct cpp_operator
835 /* EQ */ {0, 0}, /* Shouldn't happen. */
836 /* NOT */ {16, NO_L_OPERAND},
837 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
838 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
839 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
840 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
841 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
842 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
843 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
844 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
845 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
846 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
847 /* RSHIFT */ {13, LEFT_ASSOC},
848 /* LSHIFT */ {13, LEFT_ASSOC},
850 /* COMPL */ {16, NO_L_OPERAND},
851 /* AND_AND */ {6, LEFT_ASSOC},
852 /* OR_OR */ {5, LEFT_ASSOC},
853 /* Note that QUERY, COLON, and COMMA must have the same precedence.
854 However, there are some special cases for these in reduce(). */
856 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
857 /* COMMA */ {4, LEFT_ASSOC},
858 /* OPEN_PAREN */ {1, NO_L_OPERAND},
859 /* CLOSE_PAREN */ {0, 0},
861 /* EQ_EQ */ {11, LEFT_ASSOC},
862 /* NOT_EQ */ {11, LEFT_ASSOC},
863 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
864 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
865 /* UPLUS */ {16, NO_L_OPERAND},
866 /* UMINUS */ {16, NO_L_OPERAND}
869 /* Parse and evaluate a C expression, reading from PFILE.
870 Returns the truth value of the expression.
872 The implementation is an operator precedence parser, i.e. a
873 bottom-up parser, using a stack for not-yet-reduced tokens.
875 The stack base is op_stack, and the current stack pointer is 'top'.
876 There is a stack element for each operator (only), and the most
877 recently pushed operator is 'top->op'. An operand (value) is
878 stored in the 'value' field of the stack element of the operator
881 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
883 struct op *top = pfile->op_stack;
884 unsigned int lex_count;
885 bool saw_leading_not, want_value = true;
887 pfile->state.skip_eval = 0;
889 /* Set up detection of #if ! defined(). */
890 pfile->mi_ind_cmacro = 0;
891 saw_leading_not = false;
894 /* Lowest priority operator prevents further reductions. */
902 op.token = cpp_get_token (pfile);
903 op.op = op.token->type;
904 op.loc = op.token->src_loc;
908 /* These tokens convert into values. */
917 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
918 cpp_token_as_text (pfile, op.token));
920 top->value = eval_token (pfile, op.token);
924 saw_leading_not = lex_count == 1;
936 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
937 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
938 cpp_token_as_text (pfile, op.token));
942 /* Check we have a value or operator as appropriate. */
943 if (optab[op.op].flags & NO_L_OPERAND)
946 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
947 cpp_token_as_text (pfile, op.token));
951 /* We want a number (or expression) and haven't got one.
952 Try to emit a specific diagnostic. */
953 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
954 SYNTAX_ERROR ("missing expression between '(' and ')'");
956 if (op.op == CPP_EOF && top->op == CPP_EOF)
957 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
959 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
960 SYNTAX_ERROR2 ("operator '%s' has no right operand",
961 cpp_token_as_text (pfile, top->token));
962 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
963 /* Complain about missing paren during reduction. */;
965 SYNTAX_ERROR2 ("operator '%s' has no left operand",
966 cpp_token_as_text (pfile, op.token));
969 top = reduce (pfile, top, op.op);
973 if (op.op == CPP_EOF)
978 case CPP_CLOSE_PAREN:
981 if (!num_zerop (top->value))
982 pfile->state.skip_eval++;
986 if (num_zerop (top->value))
987 pfile->state.skip_eval++;
990 if (top->op != CPP_QUERY)
991 SYNTAX_ERROR (" ':' without preceding '?'");
992 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
993 pfile->state.skip_eval++;
995 pfile->state.skip_eval--;
1002 /* Check for and handle stack overflow. */
1003 if (++top == pfile->op_limit)
1004 top = _cpp_expand_op_stack (pfile);
1007 top->token = op.token;
1008 top->loc = op.token->src_loc;
1011 /* The controlling macro expression is only valid if we called lex 3
1012 times: <!> <defined expression> and <EOF>. push_conditional ()
1013 checks that we are at top-of-file. */
1014 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1015 pfile->mi_ind_cmacro = 0;
1017 if (top != pfile->op_stack)
1019 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1020 is_if ? "#if" : "#elif");
1022 return false; /* Return false on syntax error. */
1025 return !num_zerop (top->value);
1028 /* Reduce the operator / value stack if possible, in preparation for
1029 pushing operator OP. Returns NULL on error, otherwise the top of
1032 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1036 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1039 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1043 if (op == CPP_OPEN_PAREN)
1046 /* Decrement the priority of left-associative operators to force a
1047 reduction with operators of otherwise equal priority. */
1048 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1049 while (prio < optab[top->op].prio)
1051 if (CPP_OPTION (pfile, warn_num_sign_change)
1052 && optab[top->op].flags & CHECK_PROMOTION)
1053 check_promotion (pfile, top);
1061 top[-1].value = num_unary_op (pfile, top->value, top->op);
1062 top[-1].loc = top->loc;
1070 top[-1].value = num_binary_op (pfile, top[-1].value,
1071 top->value, top->op);
1072 top[-1].loc = top->loc;
1077 case CPP_GREATER_EQ:
1080 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1081 top[-1].loc = top->loc;
1087 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1088 top[-1].loc = top->loc;
1095 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1096 top[-1].loc = top->loc;
1100 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1101 top[-1].loc = top->loc;
1106 top[-1].value = num_div_op (pfile, top[-1].value,
1107 top->value, top->op);
1108 top[-1].loc = top->loc;
1113 if (!num_zerop (top->value))
1114 pfile->state.skip_eval--;
1115 top->value.low = (!num_zerop (top->value)
1116 || !num_zerop (top[1].value));
1117 top->value.high = 0;
1118 top->value.unsignedp = false;
1119 top->value.overflow = false;
1120 top->loc = top[1].loc;
1125 if (num_zerop (top->value))
1126 pfile->state.skip_eval--;
1127 top->value.low = (!num_zerop (top->value)
1128 && !num_zerop (top[1].value));
1129 top->value.high = 0;
1130 top->value.unsignedp = false;
1131 top->value.overflow = false;
1132 top->loc = top[1].loc;
1135 case CPP_OPEN_PAREN:
1136 if (op != CPP_CLOSE_PAREN)
1138 cpp_error_with_line (pfile, CPP_DL_ERROR,
1139 top->token->src_loc,
1140 0, "missing ')' in expression");
1144 top->value = top[1].value;
1145 top->loc = top[1].loc;
1150 if (!num_zerop (top->value))
1152 pfile->state.skip_eval--;
1153 top->value = top[1].value;
1154 top->loc = top[1].loc;
1158 top->value = top[2].value;
1159 top->loc = top[2].loc;
1161 top->value.unsignedp = (top[1].value.unsignedp
1162 || top[2].value.unsignedp);
1166 /* COMMA and COLON should not reduce a QUERY operator. */
1167 if (op == CPP_COMMA || op == CPP_COLON)
1169 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1177 if (top->value.overflow && !pfile->state.skip_eval)
1178 cpp_error (pfile, CPP_DL_PEDWARN,
1179 "integer overflow in preprocessor expression");
1182 if (op == CPP_CLOSE_PAREN)
1184 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1191 /* Returns the position of the old top of stack after expansion. */
1193 _cpp_expand_op_stack (cpp_reader *pfile)
1195 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1196 size_t new_size = old_size * 2 + 20;
1198 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1199 pfile->op_limit = pfile->op_stack + new_size;
1201 return pfile->op_stack + old_size;
1204 /* Emits a warning if the effective sign of either operand of OP
1205 changes because of integer promotions. */
1207 check_promotion (cpp_reader *pfile, const struct op *op)
1209 if (op->value.unsignedp == op[-1].value.unsignedp)
1212 if (op->value.unsignedp)
1214 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1215 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1216 "the left operand of \"%s\" changes sign when promoted",
1217 cpp_token_as_text (pfile, op->token));
1219 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1220 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1221 "the right operand of \"%s\" changes sign when promoted",
1222 cpp_token_as_text (pfile, op->token));
1225 /* Clears the unused high order bits of the number pointed to by PNUM. */
1227 num_trim (cpp_num num, size_t precision)
1229 if (precision > PART_PRECISION)
1231 precision -= PART_PRECISION;
1232 if (precision < PART_PRECISION)
1233 num.high &= ((cpp_num_part) 1 << precision) - 1;
1237 if (precision < PART_PRECISION)
1238 num.low &= ((cpp_num_part) 1 << precision) - 1;
1245 /* True iff A (presumed signed) >= 0. */
1247 num_positive (cpp_num num, size_t precision)
1249 if (precision > PART_PRECISION)
1251 precision -= PART_PRECISION;
1252 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1255 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1258 /* Sign extend a number, with PRECISION significant bits and all
1259 others assumed clear, to fill out a cpp_num structure. */
1261 cpp_num_sign_extend (cpp_num num, size_t precision)
1265 if (precision > PART_PRECISION)
1267 precision -= PART_PRECISION;
1268 if (precision < PART_PRECISION
1269 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1270 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1272 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1274 if (precision < PART_PRECISION)
1275 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1276 num.high = ~(cpp_num_part) 0;
1283 /* Returns the negative of NUM. */
1285 num_negate (cpp_num num, size_t precision)
1290 num.high = ~num.high;
1294 num = num_trim (num, precision);
1295 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1300 /* Returns true if A >= B. */
1302 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1306 unsignedp = pa.unsignedp || pb.unsignedp;
1310 /* Both numbers have signed type. If they are of different
1311 sign, the answer is the sign of A. */
1312 unsignedp = num_positive (pa, precision);
1314 if (unsignedp != num_positive (pb, precision))
1317 /* Otherwise we can do an unsigned comparison. */
1320 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1323 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1325 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1326 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1328 lhs.overflow = false;
1329 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1331 /* As excess precision is zeroed, there is no need to num_trim () as
1332 these operations cannot introduce a set bit there. */
1336 lhs.high &= rhs.high;
1338 else if (op == CPP_OR)
1341 lhs.high |= rhs.high;
1346 lhs.high ^= rhs.high;
1352 /* Returns LHS OP RHS, where OP is an inequality. */
1354 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1357 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1359 if (op == CPP_GREATER_EQ)
1361 else if (op == CPP_LESS)
1363 else if (op == CPP_GREATER)
1364 lhs.low = gte && !num_eq (lhs, rhs);
1365 else /* CPP_LESS_EQ. */
1366 lhs.low = !gte || num_eq (lhs, rhs);
1369 lhs.overflow = false;
1370 lhs.unsignedp = false;
1374 /* Returns LHS OP RHS, where OP is == or !=. */
1376 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1377 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1379 /* Work around a 3.0.4 bug; see PR 6950. */
1380 bool eq = num_eq (lhs, rhs);
1381 if (op == CPP_NOT_EQ)
1385 lhs.overflow = false;
1386 lhs.unsignedp = false;
1390 /* Shift NUM, of width PRECISION, right by N bits. */
1392 num_rshift (cpp_num num, size_t precision, size_t n)
1394 cpp_num_part sign_mask;
1395 bool x = num_positive (num, precision);
1397 if (num.unsignedp || x)
1400 sign_mask = ~(cpp_num_part) 0;
1403 num.high = num.low = sign_mask;
1407 if (precision < PART_PRECISION)
1408 num.high = sign_mask, num.low |= sign_mask << precision;
1409 else if (precision < 2 * PART_PRECISION)
1410 num.high |= sign_mask << (precision - PART_PRECISION);
1412 if (n >= PART_PRECISION)
1414 n -= PART_PRECISION;
1416 num.high = sign_mask;
1421 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1422 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1426 num = num_trim (num, precision);
1427 num.overflow = false;
1431 /* Shift NUM, of width PRECISION, left by N bits. */
1433 num_lshift (cpp_num num, size_t precision, size_t n)
1437 num.overflow = !num.unsignedp && !num_zerop (num);
1438 num.high = num.low = 0;
1442 cpp_num orig, maybe_orig;
1446 if (m >= PART_PRECISION)
1448 m -= PART_PRECISION;
1454 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1457 num = num_trim (num, precision);
1460 num.overflow = false;
1463 maybe_orig = num_rshift (num, precision, n);
1464 num.overflow = !num_eq (orig, maybe_orig);
1471 /* The four unary operators: +, -, ! and ~. */
1473 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1478 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1479 cpp_error (pfile, CPP_DL_WARNING,
1480 "traditional C rejects the unary plus operator");
1481 num.overflow = false;
1485 num = num_negate (num, CPP_OPTION (pfile, precision));
1489 num.high = ~num.high;
1491 num = num_trim (num, CPP_OPTION (pfile, precision));
1492 num.overflow = false;
1495 default: /* case CPP_NOT: */
1496 num.low = num_zerop (num);
1498 num.overflow = false;
1499 num.unsignedp = false;
1506 /* The various binary operators. */
1508 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1511 size_t precision = CPP_OPTION (pfile, precision);
1519 if (!rhs.unsignedp && !num_positive (rhs, precision))
1521 /* A negative shift is a positive shift the other way. */
1522 if (op == CPP_LSHIFT)
1526 rhs = num_negate (rhs, precision);
1529 n = ~0; /* Maximal. */
1532 if (op == CPP_LSHIFT)
1533 lhs = num_lshift (lhs, precision, n);
1535 lhs = num_rshift (lhs, precision, n);
1540 rhs = num_negate (rhs, precision);
1542 result.low = lhs.low + rhs.low;
1543 result.high = lhs.high + rhs.high;
1544 if (result.low < lhs.low)
1546 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1547 result.overflow = false;
1549 result = num_trim (result, precision);
1550 if (!result.unsignedp)
1552 bool lhsp = num_positive (lhs, precision);
1553 result.overflow = (lhsp == num_positive (rhs, precision)
1554 && lhsp != num_positive (result, precision));
1559 default: /* case CPP_COMMA: */
1560 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1561 || !pfile->state.skip_eval))
1562 cpp_error (pfile, CPP_DL_PEDWARN,
1563 "comma operator in operand of #if");
1571 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1574 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1577 cpp_num_part middle[2], temp;
1579 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1580 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1582 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1583 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1586 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1587 if (result.low < temp)
1591 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1592 if (result.low < temp)
1595 result.high += HIGH_PART (middle[0]);
1596 result.high += HIGH_PART (middle[1]);
1597 result.unsignedp = true;
1598 result.overflow = false;
1603 /* Multiply two preprocessing numbers. */
1605 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1607 cpp_num result, temp;
1608 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1609 bool overflow, negate = false;
1610 size_t precision = CPP_OPTION (pfile, precision);
1612 /* Prepare for unsigned multiplication. */
1615 if (!num_positive (lhs, precision))
1616 negate = !negate, lhs = num_negate (lhs, precision);
1617 if (!num_positive (rhs, precision))
1618 negate = !negate, rhs = num_negate (rhs, precision);
1621 overflow = lhs.high && rhs.high;
1622 result = num_part_mul (lhs.low, rhs.low);
1624 temp = num_part_mul (lhs.high, rhs.low);
1625 result.high += temp.low;
1629 temp = num_part_mul (lhs.low, rhs.high);
1630 result.high += temp.low;
1634 temp.low = result.low, temp.high = result.high;
1635 result = num_trim (result, precision);
1636 if (!num_eq (result, temp))
1640 result = num_negate (result, precision);
1643 result.overflow = false;
1645 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1646 && !num_zerop (result));
1647 result.unsignedp = unsignedp;
1652 /* Divide two preprocessing numbers, returning the answer or the
1653 remainder depending upon OP. */
1655 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1657 cpp_num result, sub;
1659 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1660 bool negate = false, lhs_neg = false;
1661 size_t i, precision = CPP_OPTION (pfile, precision);
1663 /* Prepare for unsigned division. */
1666 if (!num_positive (lhs, precision))
1667 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1668 if (!num_positive (rhs, precision))
1669 negate = !negate, rhs = num_negate (rhs, precision);
1672 /* Find the high bit. */
1676 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1677 for (; ; i--, mask >>= 1)
1678 if (rhs.high & mask)
1683 if (precision > PART_PRECISION)
1684 i = precision - PART_PRECISION - 1;
1687 mask = (cpp_num_part) 1 << i;
1688 for (; ; i--, mask >>= 1)
1694 if (!pfile->state.skip_eval)
1695 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
1699 /* First nonzero bit of RHS is bit I. Do naive division by
1700 shifting the RHS fully left, and subtracting from LHS if LHS is
1701 at least as big, and then repeating but with one less shift.
1702 This is not very efficient, but is easy to understand. */
1704 rhs.unsignedp = true;
1705 lhs.unsignedp = true;
1706 i = precision - i - 1;
1707 sub = num_lshift (rhs, precision, i);
1709 result.high = result.low = 0;
1712 if (num_greater_eq (lhs, sub, precision))
1714 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1715 if (i >= PART_PRECISION)
1716 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1718 result.low |= (cpp_num_part) 1 << i;
1722 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1726 /* We divide so that the remainder has the sign of the LHS. */
1729 result.unsignedp = unsignedp;
1730 result.overflow = false;
1734 result = num_negate (result, precision);
1735 result.overflow = (num_positive (result, precision) ^ !negate
1736 && !num_zerop (result));
1743 lhs.unsignedp = unsignedp;
1744 lhs.overflow = false;
1746 lhs = num_negate (lhs, precision);