1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001
3 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 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
27 number with SUM's sign, where A, B, and SUM are all C integers. */
28 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
30 static void integer_overflow PARAMS ((cpp_reader *));
31 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
33 unsigned HOST_WIDEST_INT));
34 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
36 unsigned HOST_WIDEST_INT));
37 static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
38 static struct op parse_defined PARAMS ((cpp_reader *));
39 static struct op lex PARAMS ((cpp_reader *, int, cpp_token *));
40 static const unsigned char *op_as_text PARAMS ((cpp_reader *, enum cpp_ttype));
45 U_CHAR prio; /* Priority of op. */
47 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
48 HOST_WIDEST_INT value; /* The value logically "right" of op. */
51 /* There is no "error" token, but we can't get comments in #if, so we can
52 abuse that token type. */
53 #define CPP_ERROR CPP_COMMENT
55 /* With -O2, gcc appears to produce nice code, moving the error
56 message load and subsequent jump completely out of the main path. */
57 #define CPP_ICE(msgid) \
58 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
59 #define SYNTAX_ERROR(msgid) \
60 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
61 #define SYNTAX_ERROR2(msgid, arg) \
62 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
71 const struct suffix vsuf_1[] = {
72 { "u", 1, 0 }, { "U", 1, 0 },
73 { "l", 0, 1 }, { "L", 0, 1 }
76 const struct suffix vsuf_2[] = {
77 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
78 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
79 { "ll", 0, 2 }, { "LL", 0, 2 }
82 const struct suffix vsuf_3[] = {
83 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
84 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
86 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
88 /* Parse and convert an integer for #if. Accepts decimal, hex, or
89 octal with or without size suffixes. Returned op is CPP_ERROR on
90 error, otherwise it is a CPP_NUMBER. */
93 parse_number (pfile, tok)
98 const U_CHAR *start = tok->val.str.text;
99 const U_CHAR *end = start + tok->val.str.len;
100 const U_CHAR *p = start;
102 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
105 int digit, largest_digit = 0;
106 const struct suffix *sufftab;
112 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
124 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
125 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
126 / ((unsigned HOST_WIDEST_INT) base));
132 if (c >= '0' && c <= '9')
134 /* We believe that in all live character sets, a-f are
135 consecutive, and so are A-F. */
136 else if (base == 16 && c >= 'a' && c <= 'f')
137 digit = c - 'a' + 10;
138 else if (base == 16 && c >= 'A' && c <= 'F')
139 digit = c - 'A' + 10;
143 if (largest_digit < digit)
144 largest_digit = digit;
145 nd = n * base + digit;
146 overflow |= MAX_over_base < n || nd < n;
152 /* Check for a floating point constant. Note that float constants
153 with an exponent or suffix but no decimal point are technically
154 invalid (C99 6.4.4.2) but accepted elsewhere. */
155 if ((c == '.' || c == 'F' || c == 'f')
156 || (base == 10 && (c == 'E' || c == 'e')
157 && p+1 < end && (p[1] == '+' || p[1] == '-'))
158 || (base == 16 && (c == 'P' || c == 'p')
159 && p+1 < end && (p[1] == '+' || p[1] == '-')))
160 SYNTAX_ERROR ("floating point numbers are not valid in #if");
162 /* Determine the suffix. l means long, and u means unsigned.
163 See the suffix tables, above. */
166 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
167 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
168 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
169 default: goto invalid_suffix;
172 for (i = 0; i < nsuff; i++)
173 if (memcmp (p, sufftab[i].s, end - p) == 0)
177 op.unsignedp = sufftab[i].u;
179 if (CPP_WTRADITIONAL (pfile)
181 && ! cpp_sys_macro_p (pfile))
182 cpp_warning (pfile, "traditional C rejects the `U' suffix");
183 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
184 && ! CPP_OPTION (pfile, c99))
185 cpp_pedwarn (pfile, "too many 'l' suffixes in integer constant");
188 if (base <= largest_digit)
189 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
192 cpp_pedwarn (pfile, "integer constant out of range");
194 /* If too big to be signed, consider it unsigned. */
195 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
198 cpp_warning (pfile, "integer constant is so large that it is unsigned");
207 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
215 parse_defined (pfile)
219 cpp_hashnode *node = 0;
223 /* Don't expand macros. */
224 pfile->state.prevent_expansion++;
226 cpp_get_token (pfile, &token);
227 if (token.type == CPP_OPEN_PAREN)
230 cpp_get_token (pfile, &token);
233 if (token.type == CPP_NAME)
235 node = token.val.node;
238 cpp_get_token (pfile, &token);
239 if (token.type != CPP_CLOSE_PAREN)
241 cpp_error (pfile, "missing ')' after \"defined\"");
248 cpp_error (pfile, "operator \"defined\" requires an identifier");
249 if (token.flags & NAMED_OP)
254 op.type = token.type;
256 "(\"%s\" is an alternative token for \"%s\" in C++)",
257 cpp_token_as_text (pfile, &token),
258 cpp_token_as_text (pfile, &op));
266 op.value = node->type == NT_MACRO;
270 /* No macros? At top of file? */
271 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0
272 && pfile->mi_if_not_defined == MI_IND_NOT && pfile->mi_lexed == 1)
274 cpp_start_lookahead (pfile);
275 cpp_get_token (pfile, &token);
276 if (token.type == CPP_EOF)
277 pfile->mi_ind_cmacro = node;
278 cpp_stop_lookahead (pfile, 0);
282 pfile->state.prevent_expansion--;
286 /* Read a token. The returned type is CPP_NUMBER for a valid number
287 (an interpreted preprocessing number or character constant, or the
288 result of the "defined" or "#" operators), CPP_ERROR on error,
289 CPP_EOF, or the type of an operator token. */
292 lex (pfile, skip_evaluation, token)
299 cpp_get_token (pfile, token);
304 return parse_number (pfile, token);
309 unsigned int chars_seen;
311 /* This is always a signed type. */
314 op.value = cpp_interpret_charconst (pfile, token, 1, 0, &chars_seen);
320 SYNTAX_ERROR ("string constants are not valid in #if");
323 if (ISGRAPH (token->val.c))
324 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
326 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
329 if (token->val.node == pfile->spec_nodes.n_defined)
331 if (pfile->context->prev && CPP_PEDANTIC (pfile))
332 cpp_pedwarn (pfile, "\"defined\" operator appears during macro expansion");
334 return parse_defined (pfile);
336 else if (CPP_OPTION (pfile, cplusplus)
337 && (token->val.node == pfile->spec_nodes.n_true
338 || token->val.node == pfile->spec_nodes.n_false))
342 op.value = (token->val.node == pfile->spec_nodes.n_true);
344 /* Warn about use of true or false in #if when pedantic
345 and stdbool.h has not been included. */
346 if (CPP_PEDANTIC (pfile)
347 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
348 cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
349 NODE_NAME (token->val.node));
354 /* Controlling #if expressions cannot contain identifiers (they
355 could become macros in the future). */
356 pfile->mi_state = MI_FAILED;
362 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
363 cpp_warning (pfile, "\"%s\" is not defined",
364 NODE_NAME (token->val.node));
373 if (_cpp_test_assertion (pfile, &temp))
381 /* We don't worry about its position here. */
382 pfile->mi_if_not_defined = MI_IND_NOT;
386 if (((int) token->type > (int) CPP_EQ
387 && (int) token->type < (int) CPP_PLUS_EQ)
388 || token->type == CPP_EOF)
394 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
395 cpp_token_as_text (pfile, token));
404 integer_overflow (pfile)
407 if (CPP_PEDANTIC (pfile))
408 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
411 static HOST_WIDEST_INT
412 left_shift (pfile, a, unsignedp, b)
415 unsigned int unsignedp;
416 unsigned HOST_WIDEST_INT b;
418 if (b >= HOST_BITS_PER_WIDEST_INT)
420 if (! unsignedp && a != 0)
421 integer_overflow (pfile);
425 return (unsigned HOST_WIDEST_INT) a << b;
428 HOST_WIDEST_INT l = a << b;
430 integer_overflow (pfile);
435 static HOST_WIDEST_INT
436 right_shift (pfile, a, unsignedp, b)
437 cpp_reader *pfile ATTRIBUTE_UNUSED;
439 unsigned int unsignedp;
440 unsigned HOST_WIDEST_INT b;
442 if (b >= HOST_BITS_PER_WIDEST_INT)
443 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
445 return (unsigned HOST_WIDEST_INT) a >> b;
450 /* Operator precedence and flags table.
452 After an operator is returned from the lexer, if it has priority less
453 than or equal to the operator on the top of the stack, we reduce the
454 stack by one operator and repeat the test. Since equal priorities
455 reduce, this is naturally left-associative.
457 We handle right-associative operators by clearing the lower bit of all
458 left-associative operators, and setting it for right-associative ones.
459 After the reduction phase of a new operator, just before it is pushed
460 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
461 during the reduction phase, the current right-associative operator has
462 a priority one greater than any other operator of otherwise equal
463 precedence that has been pushed on the top of the stack. This avoids
464 a reduction pass, and effectively makes the logic right-associative.
466 The remaining cases are '(' and ')'. We handle '(' by skipping the
467 reduction phase completely. ')' is given lower priority than
468 everything else, including '(', effectively forcing a reduction of the
469 parenthesised expression. If there is no matching '(', the stack will
470 be reduced all the way to the beginning, exiting the parser in the
471 same way as the ultra-low priority end-of-expression dummy operator.
472 The exit code checks to see if the operator that caused it is ')', and
473 if so outputs an appropriate error message.
475 The parser assumes all shifted operators require a right operand
476 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
477 These semantics are automatically checked, any extra semantics need to
478 be handled with operator-specific code. */
481 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
482 #define PRIO_SHIFT (FLAG_BITS + 1)
483 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
484 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
487 #define HAVE_VALUE (1 << 0)
488 #define NO_L_OPERAND (1 << 1)
489 #define NO_R_OPERAND (1 << 2)
490 #define SHORT_CIRCUIT (1 << 3)
492 /* Priority and flag combinations. */
493 #define RIGHT_ASSOC (1 << FLAG_BITS)
494 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
495 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
496 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
497 #define COMMA_PRIO (3 << PRIO_SHIFT)
498 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
499 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
500 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
501 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
502 #define OR_PRIO (8 << PRIO_SHIFT)
503 #define XOR_PRIO (9 << PRIO_SHIFT)
504 #define AND_PRIO (10 << PRIO_SHIFT)
505 #define MINMAX_PRIO (11 << PRIO_SHIFT)
506 #define EQUAL_PRIO (12 << PRIO_SHIFT)
507 #define LESS_PRIO (13 << PRIO_SHIFT)
508 #define SHIFT_PRIO (14 << PRIO_SHIFT)
509 #define PLUS_PRIO (15 << PRIO_SHIFT)
510 #define MUL_PRIO (16 << PRIO_SHIFT)
511 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
513 /* Operator to priority map. Must be in the same order as the first
514 N entries of enum cpp_ttype. */
518 /* EQ */ 0, /* dummy entry - can't happen */
519 /* NOT */ UNARY_PRIO,
520 /* GREATER */ LESS_PRIO,
521 /* LESS */ LESS_PRIO,
522 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
523 /* MINUS */ UNARY_PRIO, /* or binary */
530 /* RSHIFT */ SHIFT_PRIO,
531 /* LSHIFT */ SHIFT_PRIO,
532 /* MIN */ MINMAX_PRIO, /* C++ specific */
533 /* MAX */ MINMAX_PRIO, /* extensions */
535 /* COMPL */ UNARY_PRIO,
536 /* AND_AND */ ANDAND_PRIO,
537 /* OR_OR */ OROR_PRIO,
538 /* QUERY */ COND_PRIO,
539 /* COLON */ COLON_PRIO,
540 /* COMMA */ COMMA_PRIO,
541 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
542 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
543 /* EQ_EQ */ EQUAL_PRIO,
544 /* NOT_EQ */ EQUAL_PRIO,
545 /* GREATER_EQ */ LESS_PRIO,
546 /* LESS_EQ */ LESS_PRIO
549 #define COMPARE(OP) \
550 top->unsignedp = 0; \
551 top->value = (unsigned1 | unsigned2) \
552 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
554 #define EQUALITY(OP) \
555 top->value = v1 OP v2; \
557 #define BITWISE(OP) \
558 top->value = v1 OP v2; \
559 top->unsignedp = unsigned1 | unsigned2;
561 top->value = (v1 OP v2) ? v1 : v2; \
562 top->unsignedp = unsigned1 | unsigned2;
564 top->value = OP v2; \
565 top->unsignedp = unsigned2; \
566 top->flags |= HAVE_VALUE;
567 #define SHIFT(PSH, MSH) \
568 if (skip_evaluation) \
570 top->unsignedp = unsigned1; \
571 if (v2 < 0 && ! unsigned2) \
572 top->value = MSH (pfile, v1, unsigned1, -v2); \
574 top->value = PSH (pfile, v1, unsigned1, v2);
576 /* Parse and evaluate a C expression, reading from PFILE.
577 Returns the truth value of the expression. */
580 _cpp_parse_expr (pfile)
583 /* The implementation is an operator precedence parser, i.e. a
584 bottom-up parser, using a stack for not-yet-reduced tokens.
586 The stack base is 'stack', and the current stack pointer is 'top'.
587 There is a stack element for each operator (only),
588 and the most recently pushed operator is 'top->op'.
589 An operand (value) is stored in the 'value' field of the stack
590 element of the operator that precedes it.
591 In that case the 'flags' field has the HAVE_VALUE flag set. */
593 #define INIT_STACK_SIZE 20
594 struct op init_stack[INIT_STACK_SIZE];
595 struct op *stack = init_stack;
596 struct op *limit = stack + INIT_STACK_SIZE;
598 register struct op *top = stack + 1;
599 int skip_evaluation = 0;
602 /* Set up detection of #if ! defined(). */
604 pfile->mi_if_not_defined = MI_IND_NONE;
606 /* We've finished when we try to reduce this. */
608 /* Nifty way to catch missing '('. */
609 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
610 /* Avoid missing right operand checks. */
611 top->flags = NO_R_OPERAND;
620 op = lex (pfile, skip_evaluation, &token);
623 /* If the token is an operand, push its value and get next
624 token. If it is an operator, get its priority and flags, and
625 try to reduce the expression on the stack. */
632 /* Push a value onto the stack. */
633 if (top->flags & HAVE_VALUE)
634 SYNTAX_ERROR ("missing binary operator");
635 top->value = op.value;
636 top->unsignedp = op.unsignedp;
637 top->flags |= HAVE_VALUE;
640 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
642 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
643 /* else unary; fall through */
644 default: prio = op_to_prio[op.op]; break;
647 /* Separate the operator's code into priority and flags. */
648 flags = EXTRACT_FLAGS(prio);
649 prio = EXTRACT_PRIO(prio);
650 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
653 /* Check for reductions. Then push the operator. */
654 while (prio <= top->prio)
656 HOST_WIDEST_INT v1, v2;
657 unsigned int unsigned1, unsigned2;
659 /* Most operators that can appear on the stack require a
660 right operand. Check this before trying to reduce. */
661 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
663 if (top->op == CPP_OPEN_PAREN)
664 SYNTAX_ERROR ("void expression between '(' and ')'");
666 SYNTAX_ERROR2 ("operator '%s' has no right operand",
667 op_as_text (pfile, top->op));
670 unsigned2 = top->unsignedp, v2 = top->value;
672 unsigned1 = top->unsignedp, v1 = top->value;
674 /* Now set top->value = (top[1].op)(v1, v2); */
678 cpp_ice (pfile, "impossible operator '%s'",
679 op_as_text (pfile, top[1].op));
682 case CPP_NOT: UNARY(!); break;
683 case CPP_COMPL: UNARY(~); break;
684 case CPP_LESS: COMPARE(<); break;
685 case CPP_GREATER: COMPARE(>); break;
686 case CPP_LESS_EQ: COMPARE(<=); break;
687 case CPP_GREATER_EQ: COMPARE(>=); break;
688 case CPP_EQ_EQ: EQUALITY(==); break;
689 case CPP_NOT_EQ: EQUALITY(!=); break;
690 case CPP_AND: BITWISE(&); break;
691 case CPP_XOR: BITWISE(^); break;
692 case CPP_OR: BITWISE(|); break;
693 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
694 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
695 case CPP_MIN: MINMAX(<); break;
696 case CPP_MAX: MINMAX(>); break;
699 if (!(top->flags & HAVE_VALUE))
701 /* Can't use UNARY(+) because K+R C did not have unary
702 plus. Can't use UNARY() because some compilers object
703 to the empty argument. */
705 top->unsignedp = unsigned2;
706 top->flags |= HAVE_VALUE;
708 if (CPP_WTRADITIONAL (pfile))
710 "traditional C rejects the unary plus operator");
714 top->value = v1 + v2;
715 top->unsignedp = unsigned1 | unsigned2;
716 if (! top->unsignedp && ! skip_evaluation
717 && ! possible_sum_sign (v1, v2, top->value))
718 integer_overflow (pfile);
722 if (!(top->flags & HAVE_VALUE))
725 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
726 integer_overflow (pfile);
730 top->value = v1 - v2;
731 top->unsignedp = unsigned1 | unsigned2;
732 if (! top->unsignedp && ! skip_evaluation
733 && ! possible_sum_sign (top->value, v2, v1))
734 integer_overflow (pfile);
738 top->unsignedp = unsigned1 | unsigned2;
740 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
741 else if (!skip_evaluation)
743 top->value = v1 * v2;
744 if (v1 && (top->value / v1 != v2
745 || (top->value & v1 & v2) < 0))
746 integer_overflow (pfile);
754 SYNTAX_ERROR ("division by zero in #if");
755 top->unsignedp = unsigned1 | unsigned2;
756 if (top[1].op == CPP_DIV)
759 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
762 top->value = v1 / v2;
763 if ((top->value & v1 & v2) < 0)
764 integer_overflow (pfile);
770 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
772 top->value = v1 % v2;
777 top->value = v1 || v2;
779 if (v1) skip_evaluation--;
782 top->value = v1 && v2;
784 if (!v1) skip_evaluation--;
787 if (CPP_PEDANTIC (pfile))
788 cpp_pedwarn (pfile, "comma operator in operand of #if");
790 top->unsignedp = unsigned2;
793 SYNTAX_ERROR ("syntax error '?' without following ':'");
795 if (top[0].op != CPP_QUERY)
796 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
798 if (top->value) skip_evaluation--;
799 top->value = top->value ? v1 : v2;
800 top->unsignedp = unsigned1 | unsigned2;
803 if (op.op != CPP_CLOSE_PAREN)
804 SYNTAX_ERROR ("missing ')' in expression");
806 op.unsignedp = unsigned2;
809 /* Reducing this dummy operator indicates we've finished. */
810 if (op.op == CPP_CLOSE_PAREN)
811 SYNTAX_ERROR ("missing '(' in expression");
816 /* Handle short-circuit evaluations. */
817 if (flags & SHORT_CIRCUIT)
820 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
822 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
824 if (top[-1].value) /* Was '?' condition true? */
833 /* Check we have a left operand iff we need one. */
834 if (flags & NO_L_OPERAND)
836 if (top->flags & HAVE_VALUE)
837 SYNTAX_ERROR2 ("missing binary operator before '%s'",
838 op_as_text (pfile, top->op));
842 if (!(top->flags & HAVE_VALUE))
843 SYNTAX_ERROR2 ("operator '%s' has no left operand",
844 op_as_text (pfile, top->op));
847 /* Check for and handle stack overflow. */
851 struct op *new_stack;
852 int old_size = (char *) limit - (char *) stack;
853 int new_size = 2 * old_size;
854 if (stack != init_stack)
855 new_stack = (struct op *) xrealloc (stack, new_size);
858 new_stack = (struct op *) xmalloc (new_size);
859 memcpy (new_stack, stack, old_size);
862 top = (struct op *) ((char *) new_stack + old_size);
863 limit = (struct op *) ((char *) new_stack + new_size);
867 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
872 result = (top[1].value != 0);
874 CPP_ICE ("unbalanced stack in #if");
875 else if (!(top[1].flags & HAVE_VALUE))
877 SYNTAX_ERROR ("#if with no expression");
879 result = 0; /* Return 0 on syntax error. */
882 /* Free dynamic stack if we allocated one. */
883 if (stack != init_stack)
888 static const unsigned char *
889 op_as_text (pfile, op)
897 return cpp_token_as_text (pfile, &token);