1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 cpp_num value; /* The value logically "right" of op. */
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive PARAMS ((cpp_num, size_t));
41 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
42 static cpp_num num_trim PARAMS ((cpp_num, size_t));
43 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
45 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
46 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
48 static cpp_num num_negate PARAMS ((cpp_num, size_t));
49 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
51 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
53 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
55 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num));
56 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
58 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
59 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
61 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
62 static cpp_num parse_defined PARAMS ((cpp_reader *));
63 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
64 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
65 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
66 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
70 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
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 (s, len)
87 size_t f = 0, l = 0, i = 0;
92 case 'f': case 'F': f++; break;
93 case 'l': case 'L': l++; break;
95 case 'j': case 'J': i++; break;
100 if (f + l > 1 || i > 1)
103 return ((i ? CPP_N_IMAGINARY : 0)
105 l ? CPP_N_LARGE : CPP_N_MEDIUM));
108 /* Subroutine of cpp_classify_number. S points to an integer suffix
109 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
110 flag vector describing the suffix. */
112 interpret_int_suffix (s, len)
123 case 'u': case 'U': u++; break;
125 case 'j': case 'J': i++; break;
126 case 'l': case 'L': l++;
127 /* If there are two Ls, they must be adjacent and the same case. */
128 if (l == 2 && s[len] != s[len + 1])
135 if (l > 2 || u > 1 || i > 1)
138 return ((i ? CPP_N_IMAGINARY : 0)
139 | (u ? CPP_N_UNSIGNED : 0)
140 | ((l == 0) ? CPP_N_SMALL
141 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
144 /* Categorize numeric constants according to their field (integer,
145 floating point, or invalid), radix (decimal, octal, hexadecimal),
146 and type suffixes. */
148 cpp_classify_number (pfile, token)
150 const cpp_token *token;
152 const uchar *str = token->val.str.text;
154 unsigned int max_digit, result, radix;
155 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
157 /* If the lexer has done its job, length one can only be a single
158 digit. Fast-path this very common case. */
159 if (token->val.str.len == 1)
160 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
162 limit = str + token->val.str.len;
163 float_flag = NOT_FLOAT;
167 /* First, interpret the radix. */
173 /* Require at least one hex digit to classify it as hex. */
174 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
181 /* Now scan for a well-formed integer or float. */
184 unsigned int c = *str++;
186 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
194 if (float_flag == NOT_FLOAT)
195 float_flag = AFTER_POINT;
197 SYNTAX_ERROR ("too many decimal points in number");
199 else if ((radix <= 10 && (c == 'e' || c == 'E'))
200 || (radix == 16 && (c == 'p' || c == 'P')))
202 float_flag = AFTER_EXPON;
207 /* Start of suffix. */
213 if (float_flag != NOT_FLOAT && radix == 8)
216 if (max_digit >= radix)
217 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
219 if (float_flag != NOT_FLOAT)
221 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
222 cpp_error (pfile, DL_PEDWARN,
223 "use of C99 hexadecimal floating constant");
225 if (float_flag == AFTER_EXPON)
227 if (*str == '+' || *str == '-')
230 /* Exponent is decimal, even if string is a hex float. */
232 SYNTAX_ERROR ("exponent has no digits");
236 while (ISDIGIT (*str));
238 else if (radix == 16)
239 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
241 result = interpret_float_suffix (str, limit - str);
244 cpp_error (pfile, DL_ERROR,
245 "invalid suffix \"%.*s\" on floating constant",
246 (int) (limit - str), str);
247 return CPP_N_INVALID;
250 /* Traditional C didn't accept any floating suffixes. */
252 && CPP_WTRADITIONAL (pfile)
253 && ! cpp_sys_macro_p (pfile))
254 cpp_error (pfile, DL_WARNING,
255 "traditional C rejects the \"%.*s\" suffix",
256 (int) (limit - str), str);
258 result |= CPP_N_FLOATING;
262 result = interpret_int_suffix (str, limit - str);
265 cpp_error (pfile, DL_ERROR,
266 "invalid suffix \"%.*s\" on integer constant",
267 (int) (limit - str), str);
268 return CPP_N_INVALID;
271 /* Traditional C only accepted the 'L' suffix. */
272 if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
273 && CPP_WTRADITIONAL (pfile)
274 && ! cpp_sys_macro_p (pfile))
275 cpp_error (pfile, DL_WARNING,
276 "traditional C rejects the \"%.*s\" suffix",
277 (int) (limit - str), str);
279 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
280 && ! CPP_OPTION (pfile, c99)
281 && CPP_OPTION (pfile, warn_long_long))
282 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
284 result |= CPP_N_INTEGER;
287 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
288 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
291 result |= CPP_N_DECIMAL;
292 else if (radix == 16)
295 result |= CPP_N_OCTAL;
300 return CPP_N_INVALID;
303 /* cpp_interpret_integer converts an integer constant into a cpp_num,
304 of precision options->precision.
306 We do not provide any interface for decimal->float conversion,
307 because the preprocessor doesn't need it and the floating point
308 handling in GCC proper is too ugly to speak of. */
310 cpp_interpret_integer (pfile, token, type)
312 const cpp_token *token;
315 const uchar *p, *end;
320 result.unsignedp = !!(type & CPP_N_UNSIGNED);
321 result.overflow = false;
323 p = token->val.str.text;
324 end = p + token->val.str.len;
326 /* Common case of a single digit. */
327 if (token->val.str.len == 1)
328 result.low = p[0] - '0';
332 size_t precision = CPP_OPTION (pfile, precision);
333 unsigned int base = 10, c = 0;
334 bool overflow = false;
336 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
341 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
347 /* We can add a digit to numbers strictly less than this without
348 needing the precision and slowness of double integers. */
349 max = ~(cpp_num_part) 0;
350 if (precision < PART_PRECISION)
351 max >>= PART_PRECISION - precision;
352 max = (max - base + 1) / base + 1;
358 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
363 /* Strict inequality for when max is set to zero. */
364 if (result.low < max)
365 result.low = result.low * base + c;
368 result = append_digit (result, c, base, precision);
369 overflow |= result.overflow;
375 cpp_error (pfile, DL_PEDWARN,
376 "integer constant is too large for its type");
377 /* If too big to be signed, consider it unsigned. Only warn for
378 decimal numbers. Traditional numbers were always signed (but
379 we still honour an explicit U suffix); but we only have
380 traditional semantics in directives. */
381 else if (!result.unsignedp
382 && !(CPP_OPTION (pfile, traditional)
383 && pfile->state.in_directive)
384 && !num_positive (result, precision))
387 cpp_error (pfile, DL_WARNING,
388 "integer constant is so large that it is unsigned");
389 result.unsignedp = true;
396 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
399 append_digit (num, digit, base, precision)
405 unsigned int shift = 3 + (base == 16);
407 cpp_num_part add_high, add_low;
409 /* Multiply by 8 or 16. Catching this overflow here means we don't
410 need to worry about add_high overflowing. */
411 overflow = !!(num.high >> (PART_PRECISION - shift));
412 result.high = num.high << shift;
413 result.low = num.low << shift;
414 result.high |= num.low >> (PART_PRECISION - shift);
418 add_low = num.low << 1;
419 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
422 add_high = add_low = 0;
424 if (add_low + digit < add_low)
428 if (result.low + add_low < result.low)
430 if (result.high + add_high < result.high)
433 result.low += add_low;
434 result.high += add_high;
436 /* The above code catches overflow of a cpp_num type. This catches
437 overflow of the (possibly shorter) target precision. */
438 num.low = result.low;
439 num.high = result.high;
440 result = num_trim (result, precision);
441 if (!num_eq (result, num))
444 result.unsignedp = num.unsignedp;
445 result.overflow = overflow;
449 /* Handle meeting "defined" in a preprocessor expression. */
451 parse_defined (pfile)
456 cpp_hashnode *node = 0;
457 const cpp_token *token;
458 cpp_context *initial_context = pfile->context;
460 /* Don't expand macros. */
461 pfile->state.prevent_expansion++;
463 token = cpp_get_token (pfile);
464 if (token->type == CPP_OPEN_PAREN)
467 token = cpp_get_token (pfile);
470 if (token->type == CPP_NAME)
472 node = token->val.node;
473 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
475 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
481 cpp_error (pfile, DL_ERROR,
482 "operator \"defined\" requires an identifier");
483 if (token->flags & NAMED_OP)
488 op.type = token->type;
489 cpp_error (pfile, DL_ERROR,
490 "(\"%s\" is an alternative token for \"%s\" in C++)",
491 cpp_token_as_text (pfile, token),
492 cpp_token_as_text (pfile, &op));
498 if (pfile->context != initial_context)
499 cpp_error (pfile, DL_WARNING,
500 "this use of \"defined\" may not be portable");
502 /* A possible controlling macro of the form #if !defined ().
503 _cpp_parse_expr checks there was no other junk on the line. */
504 pfile->mi_ind_cmacro = node;
507 pfile->state.prevent_expansion--;
509 result.unsignedp = false;
511 result.overflow = false;
512 result.low = node && node->type == NT_MACRO;
516 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
517 number or character constant, or the result of the "defined" or "#"
520 eval_token (pfile, token)
522 const cpp_token *token;
531 temp = cpp_classify_number (pfile, token);
532 switch (temp & CPP_N_CATEGORY)
535 cpp_error (pfile, DL_ERROR,
536 "floating constant in preprocessor expression");
539 if (!(temp & CPP_N_IMAGINARY))
540 return cpp_interpret_integer (pfile, token, temp);
541 cpp_error (pfile, DL_ERROR,
542 "imaginary number in preprocessor expression");
546 /* Error already issued. */
549 result.high = result.low = 0;
555 cppchar_t cc = cpp_interpret_charconst (pfile, token,
560 /* Sign-extend the result if necessary. */
561 if (!unsignedp && (cppchar_signed_t) cc < 0)
563 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
564 result.low |= ~(~(cpp_num_part) 0
565 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
566 result.high = ~(cpp_num_part) 0;
567 result = num_trim (result, CPP_OPTION (pfile, precision));
573 if (token->val.node == pfile->spec_nodes.n_defined)
574 return parse_defined (pfile);
575 else if (CPP_OPTION (pfile, cplusplus)
576 && (token->val.node == pfile->spec_nodes.n_true
577 || token->val.node == pfile->spec_nodes.n_false))
580 result.low = (token->val.node == pfile->spec_nodes.n_true);
582 /* Warn about use of true or false in #if when pedantic
583 and stdbool.h has not been included. */
584 if (CPP_PEDANTIC (pfile)
585 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
586 cpp_error (pfile, DL_PEDWARN,
587 "ISO C++ does not permit \"%s\" in #if",
588 NODE_NAME (token->val.node));
594 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
595 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
596 NODE_NAME (token->val.node));
600 default: /* CPP_HASH */
601 _cpp_test_assertion (pfile, &temp);
606 result.unsignedp = !!unsignedp;
607 result.overflow = false;
611 /* Operator precedence and flags table.
613 After an operator is returned from the lexer, if it has priority less
614 than the operator on the top of the stack, we reduce the stack by one
615 operator and repeat the test. Since equal priorities do not reduce,
616 this is naturally right-associative.
618 We handle left-associative operators by decrementing the priority of
619 just-lexed operators by one, but retaining the priority of operators
620 already on the stack.
622 The remaining cases are '(' and ')'. We handle '(' by skipping the
623 reduction phase completely. ')' is given lower priority than
624 everything else, including '(', effectively forcing a reduction of the
625 parenthesised expression. If there is a matching '(', the routine
626 reduce() exits immediately. If the normal exit route sees a ')', then
627 there cannot have been a matching '(' and an error message is output.
629 The parser assumes all shifted operators require a left operand unless
630 the flag NO_L_OPERAND is set. These semantics are automatic; any
631 extra semantics need to be handled with operator-specific code. */
634 #define NO_L_OPERAND (1 << 0)
635 #define LEFT_ASSOC (1 << 1)
637 /* Operator to priority map. Must be in the same order as the first
638 N entries of enum cpp_ttype. */
639 static const struct operator
645 /* EQ */ {0, 0}, /* Shouldn't happen. */
646 /* NOT */ {16, NO_L_OPERAND},
647 /* GREATER */ {12, LEFT_ASSOC},
648 /* LESS */ {12, LEFT_ASSOC},
649 /* PLUS */ {14, LEFT_ASSOC},
650 /* MINUS */ {14, LEFT_ASSOC},
651 /* MULT */ {15, LEFT_ASSOC},
652 /* DIV */ {15, LEFT_ASSOC},
653 /* MOD */ {15, LEFT_ASSOC},
654 /* AND */ {9, LEFT_ASSOC},
655 /* OR */ {7, LEFT_ASSOC},
656 /* XOR */ {8, LEFT_ASSOC},
657 /* RSHIFT */ {13, LEFT_ASSOC},
658 /* LSHIFT */ {13, LEFT_ASSOC},
660 /* MIN */ {10, LEFT_ASSOC},
661 /* MAX */ {10, LEFT_ASSOC},
663 /* COMPL */ {16, NO_L_OPERAND},
664 /* AND_AND */ {6, LEFT_ASSOC},
665 /* OR_OR */ {5, LEFT_ASSOC},
667 /* COLON */ {4, LEFT_ASSOC},
668 /* COMMA */ {2, LEFT_ASSOC},
669 /* OPEN_PAREN */ {1, NO_L_OPERAND},
670 /* CLOSE_PAREN */ {0, 0},
672 /* EQ_EQ */ {11, LEFT_ASSOC},
673 /* NOT_EQ */ {11, LEFT_ASSOC},
674 /* GREATER_EQ */ {12, LEFT_ASSOC},
675 /* LESS_EQ */ {12, LEFT_ASSOC},
676 /* UPLUS */ {16, NO_L_OPERAND},
677 /* UMINUS */ {16, NO_L_OPERAND}
680 /* Parse and evaluate a C expression, reading from PFILE.
681 Returns the truth value of the expression.
683 The implementation is an operator precedence parser, i.e. a
684 bottom-up parser, using a stack for not-yet-reduced tokens.
686 The stack base is op_stack, and the current stack pointer is 'top'.
687 There is a stack element for each operator (only), and the most
688 recently pushed operator is 'top->op'. An operand (value) is
689 stored in the 'value' field of the stack element of the operator
692 _cpp_parse_expr (pfile)
695 struct op *top = pfile->op_stack;
696 const cpp_token *token = NULL, *prev_token;
697 unsigned int lex_count;
698 bool saw_leading_not, want_value = true;
700 pfile->state.skip_eval = 0;
702 /* Set up detection of #if ! defined(). */
703 pfile->mi_ind_cmacro = 0;
704 saw_leading_not = false;
707 /* Lowest priority operator prevents further reductions. */
715 token = cpp_get_token (pfile);
721 /* These tokens convert into values. */
728 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
729 cpp_token_as_text (pfile, token));
731 top->value = eval_token (pfile, token);
735 saw_leading_not = lex_count == 1;
746 if (ISGRAPH (token->val.c))
747 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
749 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
752 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
753 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
754 cpp_token_as_text (pfile, token));
758 /* Check we have a value or operator as appropriate. */
759 if (optab[op.op].flags & NO_L_OPERAND)
762 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
763 cpp_token_as_text (pfile, token));
767 /* Ordering here is subtle and intended to favour the
768 missing parenthesis diagnostics over alternatives. */
769 if (op.op == CPP_CLOSE_PAREN)
771 if (top->op == CPP_OPEN_PAREN)
772 SYNTAX_ERROR ("void expression between '(' and ')'");
774 else if (top->op == CPP_EOF)
775 SYNTAX_ERROR ("#if with no expression");
776 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
777 SYNTAX_ERROR2 ("operator '%s' has no right operand",
778 cpp_token_as_text (pfile, prev_token));
781 top = reduce (pfile, top, op.op);
785 if (op.op == CPP_EOF)
790 case CPP_CLOSE_PAREN:
793 if (!num_zerop (top->value))
794 pfile->state.skip_eval++;
798 if (num_zerop (top->value))
799 pfile->state.skip_eval++;
802 if (top->op != CPP_QUERY)
803 SYNTAX_ERROR (" ':' without preceding '?'");
804 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
805 pfile->state.skip_eval++;
807 pfile->state.skip_eval--;
814 /* Check for and handle stack overflow. */
815 if (++top == pfile->op_limit)
816 top = _cpp_expand_op_stack (pfile);
821 /* The controlling macro expression is only valid if we called lex 3
822 times: <!> <defined expression> and <EOF>. push_conditional ()
823 checks that we are at top-of-file. */
824 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
825 pfile->mi_ind_cmacro = 0;
827 if (top != pfile->op_stack)
829 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
831 return false; /* Return false on syntax error. */
834 return !num_zerop (top->value);
837 /* Reduce the operator / value stack if possible, in preparation for
838 pushing operator OP. Returns NULL on error, otherwise the top of
841 reduce (pfile, top, op)
848 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
851 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
855 if (op == CPP_OPEN_PAREN)
858 /* Decrement the priority of left-associative operators to force a
859 reduction with operators of otherwise equal priority. */
860 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
861 while (prio < optab[top->op].prio)
869 top[-1].value = num_unary_op (pfile, top->value, top->op);
879 top[-1].value = num_binary_op (pfile, top[-1].value,
880 top->value, top->op);
888 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
894 = num_equality_op (pfile, top[-1].value, top->value, top->op);
901 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
905 top[-1].value = num_mul (pfile, top[-1].value, top->value);
910 top[-1].value = num_div_op (pfile, top[-1].value,
911 top->value, top->op);
916 if (!num_zerop (top->value))
917 pfile->state.skip_eval--;
918 top->value.low = (!num_zerop (top->value)
919 || !num_zerop (top[1].value));
921 top->value.unsignedp = false;
922 top->value.overflow = false;
927 if (num_zerop (top->value))
928 pfile->state.skip_eval--;
929 top->value.low = (!num_zerop (top->value)
930 && !num_zerop (top[1].value));
932 top->value.unsignedp = false;
933 top->value.overflow = false;
937 if (op != CPP_CLOSE_PAREN)
939 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
943 top->value = top[1].value;
948 if (!num_zerop (top->value))
950 pfile->state.skip_eval--;
951 top->value = top[1].value;
954 top->value = top[2].value;
955 top->value.unsignedp = (top[1].value.unsignedp
956 || top[2].value.unsignedp);
960 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
968 if (top->value.overflow && !pfile->state.skip_eval)
969 cpp_error (pfile, DL_PEDWARN,
970 "integer overflow in preprocessor expression");
973 if (op == CPP_CLOSE_PAREN)
975 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
982 /* Returns the position of the old top of stack after expansion. */
984 _cpp_expand_op_stack (pfile)
987 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
988 size_t new_size = old_size * 2 + 20;
990 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
991 new_size * sizeof (struct op));
992 pfile->op_limit = pfile->op_stack + new_size;
994 return pfile->op_stack + old_size;
997 /* Clears the unused high order bits of the number pointed to by PNUM. */
999 num_trim (num, precision)
1003 if (precision > PART_PRECISION)
1005 precision -= PART_PRECISION;
1006 if (precision < PART_PRECISION)
1007 num.high &= ((cpp_num_part) 1 << precision) - 1;
1011 if (precision < PART_PRECISION)
1012 num.low &= ((cpp_num_part) 1 << precision) - 1;
1019 /* True iff A (presumed signed) >= 0. */
1021 num_positive (num, precision)
1025 if (precision > PART_PRECISION)
1027 precision -= PART_PRECISION;
1028 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1031 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1034 /* Sign extend a number, with PRECISION significant bits and all
1035 others assumed clear, to fill out a cpp_num structure. */
1037 cpp_num_sign_extend (num, precision)
1043 if (precision > PART_PRECISION)
1045 precision -= PART_PRECISION;
1046 if (precision < PART_PRECISION
1047 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1048 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1050 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1052 if (precision < PART_PRECISION)
1053 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1054 num.high = ~(cpp_num_part) 0;
1061 /* Returns the negative of NUM. */
1063 num_negate (num, precision)
1070 num.high = ~num.high;
1074 num = num_trim (num, precision);
1075 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1080 /* Returns true if A >= B. */
1082 num_greater_eq (pa, pb, precision)
1088 unsignedp = pa.unsignedp || pb.unsignedp;
1092 /* Both numbers have signed type. If they are of different
1093 sign, the answer is the sign of A. */
1094 unsignedp = num_positive (pa, precision);
1096 if (unsignedp != num_positive (pb, precision))
1099 /* Otherwise we can do an unsigned comparison. */
1102 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1105 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1107 num_bitwise_op (pfile, lhs, rhs, op)
1108 cpp_reader *pfile ATTRIBUTE_UNUSED;
1112 lhs.overflow = false;
1113 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1115 /* As excess precision is zeroed, there is no need to num_trim () as
1116 these operations cannot introduce a set bit there. */
1120 lhs.high &= rhs.high;
1122 else if (op == CPP_OR)
1125 lhs.high |= rhs.high;
1130 lhs.high ^= rhs.high;
1136 /* Returns LHS OP RHS, where OP is an inequality. */
1138 num_inequality_op (pfile, lhs, rhs, op)
1143 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1145 if (op == CPP_GREATER_EQ)
1147 else if (op == CPP_LESS)
1149 else if (op == CPP_GREATER)
1150 lhs.low = gte && !num_eq (lhs, rhs);
1151 else /* CPP_LESS_EQ. */
1152 lhs.low = !gte || num_eq (lhs, rhs);
1155 lhs.overflow = false;
1156 lhs.unsignedp = false;
1160 /* Returns LHS OP RHS, where OP is == or !=. */
1162 num_equality_op (pfile, lhs, rhs, op)
1163 cpp_reader *pfile ATTRIBUTE_UNUSED;
1167 /* Work around a 3.0.4 bug; see PR 6950. */
1168 bool eq = num_eq (lhs, rhs);
1169 if (op == CPP_NOT_EQ)
1173 lhs.overflow = false;
1174 lhs.unsignedp = false;
1178 /* Shift NUM, of width PRECISION, right by N bits. */
1180 num_rshift (num, precision, n)
1182 size_t precision, n;
1184 cpp_num_part sign_mask;
1186 if (num.unsignedp || num_positive (num, precision))
1189 sign_mask = ~(cpp_num_part) 0;
1192 num.high = num.low = sign_mask;
1196 if (precision < PART_PRECISION)
1197 num.high = sign_mask, num.low |= sign_mask << precision;
1198 else if (precision < 2 * PART_PRECISION)
1199 num.high |= sign_mask << (precision - PART_PRECISION);
1201 if (n >= PART_PRECISION)
1203 n -= PART_PRECISION;
1205 num.high = sign_mask;
1210 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1211 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1215 num = num_trim (num, precision);
1216 num.overflow = false;
1220 /* Shift NUM, of width PRECISION, left by N bits. */
1222 num_lshift (num, precision, n)
1224 size_t precision, n;
1228 num.overflow = !num.unsignedp && !num_zerop (num);
1229 num.high = num.low = 0;
1233 cpp_num orig, maybe_orig;
1237 if (m >= PART_PRECISION)
1239 m -= PART_PRECISION;
1245 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1248 num = num_trim (num, precision);
1251 num.overflow = false;
1254 maybe_orig = num_rshift (num, precision, n);
1255 num.overflow = !num_eq (orig, maybe_orig);
1262 /* The four unary operators: +, -, ! and ~. */
1264 num_unary_op (pfile, num, op)
1272 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1273 cpp_error (pfile, DL_WARNING,
1274 "traditional C rejects the unary plus operator");
1275 num.overflow = false;
1279 num = num_negate (num, CPP_OPTION (pfile, precision));
1283 num.high = ~num.high;
1285 num = num_trim (num, CPP_OPTION (pfile, precision));
1286 num.overflow = false;
1289 default: /* case CPP_NOT: */
1290 num.low = num_zerop (num);
1292 num.overflow = false;
1293 num.unsignedp = false;
1300 /* The various binary operators. */
1302 num_binary_op (pfile, lhs, rhs, op)
1308 size_t precision = CPP_OPTION (pfile, precision);
1317 if (!rhs.unsignedp && !num_positive (rhs, precision))
1319 /* A negative shift is a positive shift the other way. */
1320 if (op == CPP_LSHIFT)
1324 rhs = num_negate (rhs, precision);
1327 n = ~0; /* Maximal. */
1330 if (op == CPP_LSHIFT)
1331 lhs = num_lshift (lhs, precision, n);
1333 lhs = num_rshift (lhs, precision, n);
1340 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1342 gte = num_greater_eq (lhs, rhs, precision);
1347 lhs.unsignedp = unsignedp;
1353 rhs = num_negate (rhs, precision);
1355 result.low = lhs.low + rhs.low;
1356 result.high = lhs.high + rhs.high;
1357 if (result.low < lhs.low)
1360 result = num_trim (result, precision);
1361 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1362 if (result.unsignedp)
1363 result.overflow = false;
1366 bool lhsp = num_positive (lhs, precision);
1367 result.overflow = (lhsp == num_positive (rhs, precision)
1368 && lhsp != num_positive (result, precision));
1373 default: /* case CPP_COMMA: */
1374 if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
1375 cpp_error (pfile, DL_PEDWARN,
1376 "comma operator in operand of #if");
1384 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1387 num_part_mul (lhs, rhs)
1388 cpp_num_part lhs, rhs;
1391 cpp_num_part middle[2], temp;
1393 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1394 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1396 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1397 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1400 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1401 if (result.low < temp)
1405 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1406 if (result.low < temp)
1409 result.high += HIGH_PART (middle[0]);
1410 result.high += HIGH_PART (middle[1]);
1415 /* Multiply two preprocessing numbers. */
1417 num_mul (pfile, lhs, rhs)
1421 cpp_num result, temp;
1422 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1423 bool overflow, negate = false;
1424 size_t precision = CPP_OPTION (pfile, precision);
1426 /* Prepare for unsigned multiplication. */
1429 if (!num_positive (lhs, precision))
1430 negate = !negate, lhs = num_negate (lhs, precision);
1431 if (!num_positive (rhs, precision))
1432 negate = !negate, rhs = num_negate (rhs, precision);
1435 overflow = lhs.high && rhs.high;
1436 result = num_part_mul (lhs.low, rhs.low);
1438 temp = num_part_mul (lhs.high, rhs.low);
1439 result.high += temp.low;
1443 temp = num_part_mul (lhs.low, rhs.high);
1444 result.high += temp.low;
1448 temp.low = result.low, temp.high = result.high;
1449 result = num_trim (result, precision);
1450 if (!num_eq (result, temp))
1454 result = num_negate (result, precision);
1457 result.overflow = false;
1459 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1460 && !num_zerop (result));
1461 result.unsignedp = unsignedp;
1466 /* Divide two preprocessing numbers, returning the answer or the
1467 remainder depending upon OP. */
1469 num_div_op (pfile, lhs, rhs, op)
1474 cpp_num result, sub;
1476 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1477 bool negate = false, lhs_neg = false;
1478 size_t i, precision = CPP_OPTION (pfile, precision);
1480 /* Prepare for unsigned division. */
1483 if (!num_positive (lhs, precision))
1484 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1485 if (!num_positive (rhs, precision))
1486 negate = !negate, rhs = num_negate (rhs, precision);
1489 /* Find the high bit. */
1493 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1494 for (; ; i--, mask >>= 1)
1495 if (rhs.high & mask)
1500 if (precision > PART_PRECISION)
1501 i = precision - PART_PRECISION - 1;
1504 mask = (cpp_num_part) 1 << i;
1505 for (; ; i--, mask >>= 1)
1511 if (!pfile->state.skip_eval)
1512 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1516 /* First non-zero bit of RHS is bit I. Do naive division by
1517 shifting the RHS fully left, and subtracting from LHS if LHS is
1518 at least as big, and then repeating but with one less shift.
1519 This is not very efficient, but is easy to understand. */
1521 rhs.unsignedp = true;
1522 lhs.unsignedp = true;
1523 i = precision - i - 1;
1524 sub = num_lshift (rhs, precision, i);
1526 result.high = result.low = 0;
1529 if (num_greater_eq (lhs, sub, precision))
1531 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1532 if (i >= PART_PRECISION)
1533 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1535 result.low |= (cpp_num_part) 1 << i;
1539 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1543 /* We divide so that the remainder has the sign of the LHS. */
1546 result.unsignedp = unsignedp;
1548 result.overflow = false;
1552 result = num_negate (result, precision);
1553 result.overflow = num_positive (result, precision) ^ !negate;
1560 lhs.unsignedp = unsignedp;
1561 lhs.overflow = false;
1563 lhs = num_negate (lhs, precision);