1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
3 Contributed by Per Bothner, 1994.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Parse a C expression from text in a string */
27 #ifndef CHAR_TYPE_SIZE
28 #define CHAR_TYPE_SIZE BITS_PER_UNIT
32 #define INT_TYPE_SIZE BITS_PER_WORD
35 #ifndef LONG_TYPE_SIZE
36 #define LONG_TYPE_SIZE BITS_PER_WORD
39 #ifndef WCHAR_TYPE_SIZE
40 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
43 #ifndef MAX_CHAR_TYPE_SIZE
44 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
47 #ifndef MAX_INT_TYPE_SIZE
48 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
51 #ifndef MAX_LONG_TYPE_SIZE
52 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
55 #ifndef MAX_WCHAR_TYPE_SIZE
56 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
59 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
60 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
61 : ~ (HOST_WIDEST_INT) 0)
63 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
64 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
65 : ~ (HOST_WIDEST_INT) 0)
67 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
68 number with SUM's sign, where A, B, and SUM are all C integers. */
69 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
71 static void integer_overflow PARAMS ((cpp_reader *));
72 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
74 unsigned HOST_WIDEST_INT));
75 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
77 unsigned HOST_WIDEST_INT));
78 static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
79 static struct op parse_charconst PARAMS ((cpp_reader *, const cpp_token *));
80 static struct op parse_defined PARAMS ((cpp_reader *));
81 static struct op parse_assertion PARAMS ((cpp_reader *));
82 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, const U_CHAR **,
83 const U_CHAR *, HOST_WIDEST_INT));
84 static struct op lex PARAMS ((cpp_reader *, int));
89 U_CHAR prio; /* Priority of op. */
91 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
92 HOST_WIDEST_INT value; /* The value logically "right" of op. */
95 /* There is no "error" token, but we can't get comments in #if, so we can
96 abuse that token type. */
97 #define CPP_ERROR CPP_COMMENT
99 /* With -O2, gcc appears to produce nice code, moving the error
100 message load and subsequent jump completely out of the main path. */
101 #define CPP_ICE(msgid) \
102 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
103 #define SYNTAX_ERROR(msgid) \
104 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
105 #define SYNTAX_ERROR2(msgid, arg) \
106 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
108 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
109 with or without size suffixes. */
117 const struct suffix vsuf_1[] = {
118 { "u", 1, 0 }, { "U", 1, 0 },
119 { "l", 0, 1 }, { "L", 0, 1 }
122 const struct suffix vsuf_2[] = {
123 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
124 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
125 { "ll", 0, 2 }, { "LL", 0, 2 }
128 const struct suffix vsuf_3[] = {
129 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
130 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
132 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
135 parse_number (pfile, tok)
137 const cpp_token *tok;
140 const U_CHAR *start = tok->val.str.text;
141 const U_CHAR *end = start + tok->val.str.len;
142 const U_CHAR *p = start;
144 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
147 int digit, largest_digit = 0;
148 const struct suffix *sufftab;
154 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
166 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
167 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
168 / ((unsigned HOST_WIDEST_INT) base));
174 if (c >= '0' && c <= '9')
176 /* We believe that in all live character sets, a-f are
177 consecutive, and so are A-F. */
178 else if (base == 16 && c >= 'a' && c <= 'f')
179 digit = c - 'a' + 10;
180 else if (base == 16 && c >= 'A' && c <= 'F')
181 digit = c - 'A' + 10;
185 if (largest_digit < digit)
186 largest_digit = digit;
187 nd = n * base + digit;
188 overflow |= MAX_over_base < n || nd < n;
194 /* Check for a floating point constant. Note that float constants
195 with an exponent or suffix but no decimal point are technically
196 invalid (C99 6.4.4.2) but accepted elsewhere. */
197 if ((c == '.' || c == 'F' || c == 'f')
198 || (base == 10 && (c == 'E' || c == 'e')
199 && p+1 < end && (p[1] == '+' || p[1] == '-'))
200 || (base == 16 && (c == 'P' || c == 'p')
201 && p+1 < end && (p[1] == '+' || p[1] == '-')))
202 SYNTAX_ERROR ("floating point numbers are not valid in #if");
204 /* Determine the suffix. l means long, and u means unsigned.
205 See the suffix tables, above. */
208 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
209 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
210 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
211 default: goto invalid_suffix;
214 for (i = 0; i < nsuff; i++)
215 if (memcmp (p, sufftab[i].s, end - p) == 0)
219 op.unsignedp = sufftab[i].u;
221 if (CPP_WTRADITIONAL (pfile) && sufftab[i].u)
222 cpp_warning (pfile, "traditional C rejects the `U' suffix");
223 if (CPP_OPTION (pfile, c89) && sufftab[i].l == 2)
224 SYNTAX_ERROR ("too many 'l' suffixes in integer constant");
227 if (base <= largest_digit)
228 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
231 cpp_pedwarn (pfile, "integer constant out of range");
233 /* If too big to be signed, consider it unsigned. */
234 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
237 cpp_warning (pfile, "integer constant is so large that it is unsigned");
246 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
253 /* Parse and convert a character constant for #if. Understands backslash
254 escapes (\n, \031) and multibyte characters (if so configured). */
256 parse_charconst (pfile, tok)
258 const cpp_token *tok;
261 HOST_WIDEST_INT result = 0;
264 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
266 const U_CHAR *ptr = tok->val.str.text;
267 const U_CHAR *end = ptr + tok->val.str.len;
271 if (tok->type == CPP_WCHAR)
272 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
273 max_chars = MAX_LONG_TYPE_SIZE / width;
279 CPP_ICE ("unescaped ' in character constant");
282 c = parse_escape (pfile, &ptr, end, mask);
283 if (width < HOST_BITS_PER_INT
284 && (unsigned int) c >= (unsigned int)(1 << width))
286 "escape sequence out of range for character");
289 /* Merge character into result; ignore excess chars. */
290 if (++num_chars <= max_chars)
292 if (width < HOST_BITS_PER_INT)
293 result = (result << width) | (c & ((1 << width) - 1));
300 SYNTAX_ERROR ("empty character constant");
301 else if (num_chars > max_chars)
302 SYNTAX_ERROR ("character constant too long");
303 else if (num_chars != 1)
304 cpp_warning (pfile, "multi-character character constant");
306 /* If char type is signed, sign-extend the constant. */
307 num_bits = num_chars * width;
309 if (pfile->spec_nodes->n__CHAR_UNSIGNED__->type != T_VOID
310 || ((result >> (num_bits - 1)) & 1) == 0)
311 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
312 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
314 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
315 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
317 /* This is always a signed type. */
328 parse_defined (pfile)
332 const cpp_token *tok;
336 tok = _cpp_get_raw_token (pfile);
337 if (tok->type == CPP_OPEN_PAREN)
340 tok = _cpp_get_raw_token (pfile);
343 if (tok->type != CPP_NAME)
344 SYNTAX_ERROR ("\"defined\" without an identifier");
346 if (paren && _cpp_get_raw_token (pfile)->type != CPP_CLOSE_PAREN)
347 SYNTAX_ERROR ("missing close paren after \"defined\"");
349 if (tok->val.node->type == T_POISON)
350 SYNTAX_ERROR2 ("attempt to use poisoned \"%s\"", tok->val.node->name);
352 op.value = tok->val.node->type != T_VOID;
363 parse_assertion (pfile)
367 struct answer *answer;
371 hp = _cpp_parse_assertion (pfile, &answer);
374 /* If we get here, the syntax is valid. */
377 op.value = (hp->type == T_ASSERTION &&
378 (answer == 0 || *_cpp_find_answer (hp, &answer->list) != 0));
381 FREE_ANSWER (answer);
386 /* Read one token. */
389 lex (pfile, skip_evaluation)
394 const cpp_token *tok;
397 tok = _cpp_get_token (pfile);
401 case CPP_PLACEMARKER:
406 return parse_number (pfile, tok);
409 return parse_charconst (pfile, tok);
413 SYNTAX_ERROR ("string constants are not valid in #if");
416 SYNTAX_ERROR ("floating point numbers are not valid in #if");
419 if (ISGRAPH (tok->val.aux))
420 SYNTAX_ERROR2 ("invalid character '%c' in #if", tok->val.aux);
422 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", tok->val.aux);
425 return parse_defined (pfile);
432 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
433 cpp_warning (pfile, "\"%s\" is not defined", tok->val.node->name);
437 return parse_assertion (pfile);
440 if ((tok->type > CPP_EQ && tok->type < CPP_PLUS_EQ)
441 || tok->type == CPP_EOF)
447 SYNTAX_ERROR2("'%s' is not valid in #if expressions", TOKEN_NAME (tok));
455 /* Parse a C escape sequence. STRING_PTR points to a variable
456 containing a pointer to the string to parse. That pointer
457 is updated past the characters we use. The value of the
458 escape sequence is returned.
460 If \ is followed by 000, we return 0 and leave the string pointer
461 after the zeros. A value of 0 does not mean end of string. */
463 static HOST_WIDEST_INT
464 parse_escape (pfile, string_ptr, limit, result_mask)
466 const U_CHAR **string_ptr;
468 HOST_WIDEST_INT result_mask;
470 const U_CHAR *ptr = *string_ptr;
471 /* We know we have at least one following character. */
475 case 'a': c = TARGET_BELL; break;
476 case 'b': c = TARGET_BS; break;
477 case 'f': c = TARGET_FF; break;
478 case 'n': c = TARGET_NEWLINE; break;
479 case 'r': c = TARGET_CR; break;
480 case 't': c = TARGET_TAB; break;
481 case 'v': c = TARGET_VT; break;
484 if (CPP_PEDANTIC (pfile))
485 cpp_pedwarn (pfile, "non-ISO-standard escape sequence, '\\%c'", c);
489 case '0': case '1': case '2': case '3':
490 case '4': case '5': case '6': case '7':
492 unsigned int i = c - '0';
500 if (c < '0' || c > '7')
503 i = (i << 3) + c - '0';
505 if (i != (i & result_mask))
508 cpp_pedwarn (pfile, "octal escape sequence out of range");
516 unsigned int i = 0, overflow = 0;
517 int digits_found = 0, digit;
523 if (c >= '0' && c <= '9')
525 else if (c >= 'a' && c <= 'f')
526 digit = c - 'a' + 10;
527 else if (c >= 'A' && c <= 'F')
528 digit = c - 'A' + 10;
532 overflow |= i ^ (i << 4 >> 4);
533 i = (i << 4) + digit;
537 cpp_error (pfile, "\\x used with no following hex digits");
538 if (overflow | (i != (i & result_mask)))
541 cpp_pedwarn (pfile, "hex escape sequence out of range");
552 integer_overflow (pfile)
555 if (CPP_PEDANTIC (pfile))
556 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
559 static HOST_WIDEST_INT
560 left_shift (pfile, a, unsignedp, b)
563 unsigned int unsignedp;
564 unsigned HOST_WIDEST_INT b;
566 if (b >= HOST_BITS_PER_WIDEST_INT)
568 if (! unsignedp && a != 0)
569 integer_overflow (pfile);
573 return (unsigned HOST_WIDEST_INT) a << b;
576 HOST_WIDEST_INT l = a << b;
578 integer_overflow (pfile);
583 static HOST_WIDEST_INT
584 right_shift (pfile, a, unsignedp, b)
585 cpp_reader *pfile ATTRIBUTE_UNUSED;
587 unsigned int unsignedp;
588 unsigned HOST_WIDEST_INT b;
590 if (b >= HOST_BITS_PER_WIDEST_INT)
591 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
593 return (unsigned HOST_WIDEST_INT) a >> b;
598 /* Operator precedence and flags table.
600 After an operator is returned from the lexer, if it has priority less
601 than or equal to the operator on the top of the stack, we reduce the
602 stack by one operator and repeat the test. Since equal priorities
603 reduce, this is naturally left-associative.
605 We handle right-associative operators by clearing the lower bit of all
606 left-associative operators, and setting it for right-associative ones.
607 After the reduction phase of a new operator, just before it is pushed
608 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
609 during the reduction phase, the current right-associative operator has
610 a priority one greater than any other operator of otherwise equal
611 precedence that has been pushed on the top of the stack. This avoids
612 a reduction pass, and effectively makes the logic right-associative.
614 The remaining cases are '(' and ')'. We handle '(' by skipping the
615 reduction phase completely. ')' is given lower priority than
616 everything else, including '(', effectively forcing a reduction of the
617 parenthesised expression. If there is no matching '(', the stack will
618 be reduced all the way to the beginning, exiting the parser in the
619 same way as the ultra-low priority end-of-expression dummy operator.
620 The exit code checks to see if the operator that caused it is ')', and
621 if so outputs an appropriate error message.
623 The parser assumes all shifted operators require a right operand
624 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
625 These semantics are automatically checked, any extra semantics need to
626 be handled with operator-specific code. */
629 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
630 #define PRIO_SHIFT (FLAG_BITS + 1)
631 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
632 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
635 #define HAVE_VALUE (1 << 0)
636 #define NO_L_OPERAND (1 << 1)
637 #define NO_R_OPERAND (1 << 2)
638 #define SHORT_CIRCUIT (1 << 3)
640 /* Priority and flag combinations. */
641 #define RIGHT_ASSOC (1 << FLAG_BITS)
642 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
643 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
644 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
645 #define COMMA_PRIO (3 << PRIO_SHIFT)
646 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
647 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
648 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
649 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
650 #define OR_PRIO (8 << PRIO_SHIFT)
651 #define XOR_PRIO (9 << PRIO_SHIFT)
652 #define AND_PRIO (10 << PRIO_SHIFT)
653 #define MINMAX_PRIO (11 << PRIO_SHIFT)
654 #define EQUAL_PRIO (12 << PRIO_SHIFT)
655 #define LESS_PRIO (13 << PRIO_SHIFT)
656 #define SHIFT_PRIO (14 << PRIO_SHIFT)
657 #define PLUS_PRIO (15 << PRIO_SHIFT)
658 #define MUL_PRIO (16 << PRIO_SHIFT)
659 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
661 /* Operator to priority map. Must be in the same order as the first
662 N entries of enum cpp_ttype. */
666 /* EQ */ 0, /* dummy entry - can't happen */
667 /* NOT */ UNARY_PRIO,
668 /* GREATER */ LESS_PRIO,
669 /* LESS */ LESS_PRIO,
670 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
671 /* MINUS */ UNARY_PRIO, /* or binary */
678 /* RSHIFT */ SHIFT_PRIO,
679 /* LSHIFT */ SHIFT_PRIO,
680 /* MIN */ MINMAX_PRIO, /* C++ specific */
681 /* MAX */ MINMAX_PRIO, /* extensions */
683 /* COMPL */ UNARY_PRIO,
684 /* AND_AND */ ANDAND_PRIO,
685 /* OR_OR */ OROR_PRIO,
686 /* QUERY */ COND_PRIO,
687 /* COLON */ COLON_PRIO,
688 /* COMMA */ COMMA_PRIO,
689 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
690 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
691 /* EQ_EQ */ EQUAL_PRIO,
692 /* NOT_EQ */ EQUAL_PRIO,
693 /* GREATER_EQ */ LESS_PRIO,
694 /* LESS_EQ */ LESS_PRIO
697 #define COMPARE(OP) \
698 top->unsignedp = 0; \
699 top->value = (unsigned1 | unsigned2) \
700 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
702 #define EQUALITY(OP) \
703 top->value = v1 OP v2; \
705 #define BITWISE(OP) \
706 top->value = v1 OP v2; \
707 top->unsignedp = unsigned1 | unsigned2;
709 top->value = (v1 OP v2) ? v1 : v2; \
710 top->unsignedp = unsigned1 | unsigned2;
712 top->value = OP v2; \
713 top->unsignedp = unsigned2; \
714 top->flags |= HAVE_VALUE;
715 #define SHIFT(PSH, MSH) \
716 if (skip_evaluation) \
718 top->unsignedp = unsigned1; \
719 if (v2 < 0 && ! unsigned2) \
720 top->value = MSH (pfile, v1, unsigned1, -v2); \
722 top->value = PSH (pfile, v1, unsigned1, v2);
724 /* Parse and evaluate a C expression, reading from PFILE.
725 Returns the truth value of the expression. */
727 #define TYPE_NAME(t) _cpp_token_spellings[t].name
730 _cpp_parse_expr (pfile)
733 /* The implementation is an operator precedence parser, i.e. a
734 bottom-up parser, using a stack for not-yet-reduced tokens.
736 The stack base is 'stack', and the current stack pointer is 'top'.
737 There is a stack element for each operator (only),
738 and the most recently pushed operator is 'top->op'.
739 An operand (value) is stored in the 'value' field of the stack
740 element of the operator that precedes it.
741 In that case the 'flags' field has the HAVE_VALUE flag set. */
743 #define INIT_STACK_SIZE 20
744 struct op init_stack[INIT_STACK_SIZE];
745 struct op *stack = init_stack;
746 struct op *limit = stack + INIT_STACK_SIZE;
747 register struct op *top = stack + 1;
748 int skip_evaluation = 0;
751 /* Save parser state and set it to something sane. */
752 int save_skipping = pfile->skipping;
755 /* We've finished when we try to reduce this. */
757 /* Nifty way to catch missing '('. */
758 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
759 /* Avoid missing right operand checks. */
760 top->flags = NO_R_OPERAND;
769 op = lex (pfile, skip_evaluation);
771 /* If the token is an operand, push its value and get next
772 token. If it is an operator, get its priority and flags, and
773 try to reduce the expression on the stack. */
780 /* Push a value onto the stack. */
781 if (top->flags & HAVE_VALUE)
782 SYNTAX_ERROR ("missing binary operator");
783 top->value = op.value;
784 top->unsignedp = op.unsignedp;
785 top->flags |= HAVE_VALUE;
788 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
790 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
791 /* else unary; fall through */
792 default: prio = op_to_prio[op.op]; break;
795 /* Separate the operator's code into priority and flags. */
796 flags = EXTRACT_FLAGS(prio);
797 prio = EXTRACT_PRIO(prio);
798 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
801 /* Check for reductions. Then push the operator. */
802 while (prio <= top->prio)
804 HOST_WIDEST_INT v1, v2;
805 unsigned int unsigned1, unsigned2;
807 /* Most operators that can appear on the stack require a
808 right operand. Check this before trying to reduce. */
809 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
811 if (top->op == CPP_OPEN_PAREN)
812 SYNTAX_ERROR ("void expression between '(' and ')'");
814 SYNTAX_ERROR2 ("operator '%s' has no right operand",
815 TYPE_NAME (top->op));
818 unsigned2 = top->unsignedp, v2 = top->value;
820 unsigned1 = top->unsignedp, v1 = top->value;
822 /* Now set top->value = (top[1].op)(v1, v2); */
826 cpp_ice (pfile, "impossible operator type %s", TYPE_NAME (op.op));
829 case CPP_NOT: UNARY(!); break;
830 case CPP_COMPL: UNARY(~); break;
831 case CPP_LESS: COMPARE(<); break;
832 case CPP_GREATER: COMPARE(>); break;
833 case CPP_LESS_EQ: COMPARE(<=); break;
834 case CPP_GREATER_EQ: COMPARE(>=); break;
835 case CPP_EQ_EQ: EQUALITY(==); break;
836 case CPP_NOT_EQ: EQUALITY(!=); break;
837 case CPP_AND: BITWISE(&); break;
838 case CPP_XOR: BITWISE(^); break;
839 case CPP_OR: BITWISE(|); break;
840 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
841 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
842 case CPP_MIN: MINMAX(<); break;
843 case CPP_MAX: MINMAX(>); break;
846 if (!(top->flags & HAVE_VALUE))
848 /* Can't use UNARY(+) because K+R C did not have unary
849 plus. Can't use UNARY() because some compilers object
850 to the empty argument. */
852 top->unsignedp = unsigned2;
853 top->flags |= HAVE_VALUE;
855 if (CPP_WTRADITIONAL (pfile))
857 "traditional C rejects the unary plus operator");
861 top->value = v1 + v2;
862 top->unsignedp = unsigned1 | unsigned2;
863 if (! top->unsignedp && ! skip_evaluation
864 && ! possible_sum_sign (v1, v2, top->value))
865 integer_overflow (pfile);
869 if (!(top->flags & HAVE_VALUE))
872 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
873 integer_overflow (pfile);
877 top->value = v1 - v2;
878 top->unsignedp = unsigned1 | unsigned2;
879 if (! top->unsignedp && ! skip_evaluation
880 && ! possible_sum_sign (top->value, v2, v1))
881 integer_overflow (pfile);
885 top->unsignedp = unsigned1 | unsigned2;
887 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
888 else if (!skip_evaluation)
890 top->value = v1 * v2;
891 if (v1 && (top->value / v1 != v2
892 || (top->value & v1 & v2) < 0))
893 integer_overflow (pfile);
901 SYNTAX_ERROR ("division by zero in #if");
902 top->unsignedp = unsigned1 | unsigned2;
903 if (top[1].op == CPP_DIV)
906 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
909 top->value = v1 / v2;
910 if ((top->value & v1 & v2) < 0)
911 integer_overflow (pfile);
917 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
919 top->value = v1 % v2;
924 top->value = v1 || v2;
926 if (v1) skip_evaluation--;
929 top->value = v1 && v2;
931 if (!v1) skip_evaluation--;
934 if (CPP_PEDANTIC (pfile))
935 cpp_pedwarn (pfile, "comma operator in operand of #if");
937 top->unsignedp = unsigned2;
940 SYNTAX_ERROR ("syntax error '?' without following ':'");
942 if (top[0].op != CPP_QUERY)
943 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
945 if (top->value) skip_evaluation--;
946 top->value = top->value ? v1 : v2;
947 top->unsignedp = unsigned1 | unsigned2;
950 if (op.op != CPP_CLOSE_PAREN)
951 SYNTAX_ERROR ("missing ')' in expression");
953 op.unsignedp = unsigned2;
956 /* Reducing this dummy operator indicates we've finished. */
957 if (op.op == CPP_CLOSE_PAREN)
958 SYNTAX_ERROR ("missing '(' in expression");
963 /* Handle short-circuit evaluations. */
964 if (flags & SHORT_CIRCUIT)
967 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
969 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
971 if (top[-1].value) /* Was '?' condition true? */
980 /* Check we have a left operand iff we need one. */
981 if (flags & NO_L_OPERAND)
983 if (top->flags & HAVE_VALUE)
984 SYNTAX_ERROR2 ("missing binary operator before '%s'",
989 if (!(top->flags & HAVE_VALUE))
990 SYNTAX_ERROR2 ("operator '%s' has no left operand",
994 /* Check for and handle stack overflow. */
998 struct op *new_stack;
999 int old_size = (char *) limit - (char *) stack;
1000 int new_size = 2 * old_size;
1001 if (stack != init_stack)
1002 new_stack = (struct op *) xrealloc (stack, new_size);
1005 new_stack = (struct op *) xmalloc (new_size);
1006 memcpy (new_stack, stack, old_size);
1009 top = (struct op *) ((char *) new_stack + old_size);
1010 limit = (struct op *) ((char *) new_stack + new_size);
1014 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1019 result = (top[1].value != 0);
1021 CPP_ICE ("unbalanced stack in #if");
1022 else if (!(top[1].flags & HAVE_VALUE))
1024 SYNTAX_ERROR ("#if with no expression");
1026 result = 0; /* Return 0 on syntax error. */
1029 /* Free dynamic stack if we allocated one. */
1030 if (stack != init_stack)
1032 pfile->skipping = save_skipping;