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 /* 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));
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 SYNTAX_ERROR(msgid) \
58 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
59 #define SYNTAX_ERROR2(msgid, arg) \
60 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
64 const unsigned char s[4];
65 const unsigned char u;
66 const unsigned char l;
69 static const struct suffix vsuf_1[] = {
70 { "u", 1, 0 }, { "U", 1, 0 },
71 { "l", 0, 1 }, { "L", 0, 1 }
74 static const struct suffix vsuf_2[] = {
75 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
76 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
77 { "ll", 0, 2 }, { "LL", 0, 2 }
80 static const struct suffix vsuf_3[] = {
81 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
82 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
85 /* Parse and convert what is presumably an integer in TOK. Accepts
86 decimal, hex, or octal with or without size suffixes. Returned op
87 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
89 parse_number (pfile, tok)
94 const U_CHAR *start = tok->val.str.text;
95 const U_CHAR *end = start + tok->val.str.len;
96 const U_CHAR *p = start;
98 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
101 int digit, largest_digit = 0;
102 const struct suffix *sufftab;
108 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
120 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
121 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
122 / ((unsigned HOST_WIDEST_INT) base));
129 || (base == 16 && ISXDIGIT (c)))
130 digit = hex_value (c);
134 if (largest_digit < digit)
135 largest_digit = digit;
136 nd = n * base + digit;
137 overflow |= MAX_over_base < n || nd < n;
143 /* Check for a floating point constant. Note that float constants
144 with an exponent or suffix but no decimal point are technically
145 invalid (C99 6.4.4.2) but accepted elsewhere. */
146 if ((c == '.' || c == 'F' || c == 'f')
147 || (base == 10 && (c == 'E' || c == 'e')
148 && p+1 < end && (p[1] == '+' || p[1] == '-'))
149 || (base == 16 && (c == 'P' || c == 'p')
150 && p+1 < end && (p[1] == '+' || p[1] == '-')))
151 SYNTAX_ERROR ("floating point numbers are not valid in #if");
153 /* Determine the suffix. l means long, and u means unsigned.
154 See the suffix tables, above. */
157 case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
158 case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
159 case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
160 default: goto invalid_suffix;
163 for (i = 0; i < nsuff; i++)
164 if (memcmp (p, sufftab[i].s, end - p) == 0)
168 op.unsignedp = sufftab[i].u;
170 if (CPP_WTRADITIONAL (pfile)
172 && ! cpp_sys_macro_p (pfile))
173 cpp_error (pfile, DL_WARNING, "traditional C rejects the `U' suffix");
174 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
175 && ! CPP_OPTION (pfile, c99))
176 cpp_error (pfile, DL_PEDWARN,
177 "too many 'l' suffixes in integer constant");
180 if (base <= largest_digit)
181 cpp_error (pfile, DL_PEDWARN,
182 "integer constant contains digits beyond the radix");
185 cpp_error (pfile, DL_PEDWARN, "integer constant out of range");
187 /* If too big to be signed, consider it unsigned. */
188 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
191 cpp_error (pfile, DL_WARNING,
192 "integer constant is so large that it is unsigned");
201 cpp_error (pfile, DL_ERROR, "invalid suffix '%.*s' on integer constant",
208 /* Handle meeting "defined" in a preprocessor expression. */
210 parse_defined (pfile)
214 cpp_hashnode *node = 0;
215 const cpp_token *token;
217 cpp_context *initial_context = pfile->context;
219 /* Don't expand macros. */
220 pfile->state.prevent_expansion++;
222 token = cpp_get_token (pfile);
223 if (token->type == CPP_OPEN_PAREN)
226 token = cpp_get_token (pfile);
229 if (token->type == CPP_NAME)
231 node = token->val.node;
232 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
234 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
240 cpp_error (pfile, DL_ERROR,
241 "operator \"defined\" requires an identifier");
242 if (token->flags & NAMED_OP)
247 op.type = token->type;
248 cpp_error (pfile, DL_ERROR,
249 "(\"%s\" is an alternative token for \"%s\" in C++)",
250 cpp_token_as_text (pfile, token),
251 cpp_token_as_text (pfile, &op));
259 if (pfile->context != initial_context)
260 cpp_error (pfile, DL_WARNING,
261 "this use of \"defined\" may not be portable");
263 op.value = node->type == NT_MACRO;
267 /* A possible controlling macro of the form #if !defined ().
268 _cpp_parse_expr checks there was no other junk on the line. */
269 pfile->mi_ind_cmacro = node;
272 pfile->state.prevent_expansion--;
276 /* Read a token. The returned type is CPP_NUMBER for a valid number
277 (an interpreted preprocessing number or character constant, or the
278 result of the "defined" or "#" operators), CPP_ERROR on error,
279 CPP_EOF, or the type of an operator token. */
281 lex (pfile, skip_evaluation)
286 const cpp_token *token = cpp_get_token (pfile);
291 return parse_number (pfile, token);
296 unsigned int chars_seen;
298 if (token->type == CPP_CHAR)
301 op.unsignedp = WCHAR_UNSIGNED;
303 op.value = cpp_interpret_charconst (pfile, token, 1, &chars_seen);
309 SYNTAX_ERROR ("string constants are not valid in #if");
312 if (ISGRAPH (token->val.c))
313 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
315 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
318 if (token->val.node == pfile->spec_nodes.n_defined)
319 return parse_defined (pfile);
320 else if (CPP_OPTION (pfile, cplusplus)
321 && (token->val.node == pfile->spec_nodes.n_true
322 || token->val.node == pfile->spec_nodes.n_false))
326 op.value = (token->val.node == pfile->spec_nodes.n_true);
328 /* Warn about use of true or false in #if when pedantic
329 and stdbool.h has not been included. */
330 if (CPP_PEDANTIC (pfile)
331 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
332 cpp_error (pfile, DL_PEDWARN,
333 "ISO C++ does not permit \"%s\" in #if",
334 NODE_NAME (token->val.node));
343 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
344 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
345 NODE_NAME (token->val.node));
354 if (_cpp_test_assertion (pfile, &temp))
362 if (((int) token->type > (int) CPP_EQ
363 && (int) token->type < (int) CPP_PLUS_EQ)
364 || token->type == CPP_EOF)
370 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
371 cpp_token_as_text (pfile, token));
379 /* Warn if appropriate on overflow. */
381 integer_overflow (pfile)
384 if (CPP_PEDANTIC (pfile))
385 cpp_error (pfile, DL_PEDWARN,
386 "integer overflow in preprocessor expression");
389 /* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
391 static HOST_WIDEST_INT
392 left_shift (pfile, a, unsignedp, b)
395 unsigned int unsignedp;
396 unsigned HOST_WIDEST_INT b;
398 if (b >= HOST_BITS_PER_WIDEST_INT)
400 if (! unsignedp && a != 0)
401 integer_overflow (pfile);
405 return (unsigned HOST_WIDEST_INT) a << b;
408 HOST_WIDEST_INT l = a << b;
410 integer_overflow (pfile);
415 /* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
417 static HOST_WIDEST_INT
418 right_shift (pfile, a, unsignedp, b)
419 cpp_reader *pfile ATTRIBUTE_UNUSED;
421 unsigned int unsignedp;
422 unsigned HOST_WIDEST_INT b;
424 if (b >= HOST_BITS_PER_WIDEST_INT)
425 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
427 return (unsigned HOST_WIDEST_INT) a >> b;
432 /* Operator precedence and flags table.
434 After an operator is returned from the lexer, if it has priority less
435 than or equal to the operator on the top of the stack, we reduce the
436 stack by one operator and repeat the test. Since equal priorities
437 reduce, this is naturally left-associative.
439 We handle right-associative operators by clearing the lower bit of all
440 left-associative operators, and setting it for right-associative ones.
441 After the reduction phase of a new operator, just before it is pushed
442 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
443 during the reduction phase, the current right-associative operator has
444 a priority one greater than any other operator of otherwise equal
445 precedence that has been pushed on the top of the stack. This avoids
446 a reduction pass, and effectively makes the logic right-associative.
448 The remaining cases are '(' and ')'. We handle '(' by skipping the
449 reduction phase completely. ')' is given lower priority than
450 everything else, including '(', effectively forcing a reduction of the
451 parenthesised expression. If there is no matching '(', the stack will
452 be reduced all the way to the beginning, exiting the parser in the
453 same way as the ultra-low priority end-of-expression dummy operator.
454 The exit code checks to see if the operator that caused it is ')', and
455 if so outputs an appropriate error message.
457 The parser assumes all shifted operators require a right operand
458 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
459 These semantics are automatically checked, any extra semantics need to
460 be handled with operator-specific code. */
463 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
464 #define PRIO_SHIFT (FLAG_BITS + 1)
465 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
466 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
469 #define HAVE_VALUE (1 << 0)
470 #define NO_L_OPERAND (1 << 1)
471 #define NO_R_OPERAND (1 << 2)
472 #define SHORT_CIRCUIT (1 << 3)
474 /* Priority and flag combinations. */
475 #define RIGHT_ASSOC (1 << FLAG_BITS)
476 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
477 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
478 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
479 #define COMMA_PRIO (3 << PRIO_SHIFT)
480 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
481 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
482 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
483 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
484 #define OR_PRIO (8 << PRIO_SHIFT)
485 #define XOR_PRIO (9 << PRIO_SHIFT)
486 #define AND_PRIO (10 << PRIO_SHIFT)
487 #define MINMAX_PRIO (11 << PRIO_SHIFT)
488 #define EQUAL_PRIO (12 << PRIO_SHIFT)
489 #define LESS_PRIO (13 << PRIO_SHIFT)
490 #define SHIFT_PRIO (14 << PRIO_SHIFT)
491 #define PLUS_PRIO (15 << PRIO_SHIFT)
492 #define MUL_PRIO (16 << PRIO_SHIFT)
493 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
495 /* Operator to priority map. Must be in the same order as the first
496 N entries of enum cpp_ttype. */
500 /* EQ */ 0, /* dummy entry - can't happen */
501 /* NOT */ UNARY_PRIO,
502 /* GREATER */ LESS_PRIO,
503 /* LESS */ LESS_PRIO,
504 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
505 /* MINUS */ UNARY_PRIO, /* or binary */
512 /* RSHIFT */ SHIFT_PRIO,
513 /* LSHIFT */ SHIFT_PRIO,
514 /* MIN */ MINMAX_PRIO, /* C++ specific */
515 /* MAX */ MINMAX_PRIO, /* extensions */
517 /* COMPL */ UNARY_PRIO,
518 /* AND_AND */ ANDAND_PRIO,
519 /* OR_OR */ OROR_PRIO,
520 /* QUERY */ COND_PRIO,
521 /* COLON */ COLON_PRIO,
522 /* COMMA */ COMMA_PRIO,
523 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
524 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
525 /* EQ_EQ */ EQUAL_PRIO,
526 /* NOT_EQ */ EQUAL_PRIO,
527 /* GREATER_EQ */ LESS_PRIO,
528 /* LESS_EQ */ LESS_PRIO
531 #define COMPARE(OP) \
532 top->unsignedp = 0; \
533 top->value = (unsigned1 | unsigned2) \
534 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
536 #define EQUALITY(OP) \
537 top->value = v1 OP v2; \
539 #define BITWISE(OP) \
540 top->value = v1 OP v2; \
541 top->unsignedp = unsigned1 | unsigned2;
543 top->value = (v1 OP v2) ? v1 : v2; \
544 top->unsignedp = unsigned1 | unsigned2;
546 top->value = OP v2; \
547 top->unsignedp = unsigned2; \
548 top->flags |= HAVE_VALUE;
549 #define SHIFT(PSH, MSH) \
550 if (skip_evaluation) \
552 top->unsignedp = unsigned1; \
553 if (v2 < 0 && ! unsigned2) \
554 top->value = MSH (pfile, v1, unsigned1, -v2); \
556 top->value = PSH (pfile, v1, unsigned1, v2);
558 /* Parse and evaluate a C expression, reading from PFILE.
559 Returns the truth value of the expression. */
561 _cpp_parse_expr (pfile)
564 /* The implementation is an operator precedence parser, i.e. a
565 bottom-up parser, using a stack for not-yet-reduced tokens.
567 The stack base is 'stack', and the current stack pointer is 'top'.
568 There is a stack element for each operator (only),
569 and the most recently pushed operator is 'top->op'.
570 An operand (value) is stored in the 'value' field of the stack
571 element of the operator that precedes it.
572 In that case the 'flags' field has the HAVE_VALUE flag set. */
574 #define INIT_STACK_SIZE 20
575 struct op init_stack[INIT_STACK_SIZE];
576 struct op *stack = init_stack;
577 struct op *limit = stack + INIT_STACK_SIZE;
578 struct op *top = stack + 1;
579 int skip_evaluation = 0;
581 unsigned int lex_count, saw_leading_not;
583 /* Set up detection of #if ! defined(). */
584 pfile->mi_ind_cmacro = 0;
588 /* We've finished when we try to reduce this. */
590 /* Nifty way to catch missing '('. */
591 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
592 /* Avoid missing right operand checks. */
593 top->flags = NO_R_OPERAND;
602 op = lex (pfile, skip_evaluation);
605 /* If the token is an operand, push its value and get next
606 token. If it is an operator, get its priority and flags, and
607 try to reduce the expression on the stack. */
614 /* Push a value onto the stack. */
615 if (top->flags & HAVE_VALUE)
616 SYNTAX_ERROR ("missing binary operator");
617 top->value = op.value;
618 top->unsignedp = op.unsignedp;
619 top->flags |= HAVE_VALUE;
622 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
625 saw_leading_not = lex_count == 1;
626 prio = op_to_prio[op.op];
629 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
630 /* else unary; fall through */
631 default: prio = op_to_prio[op.op]; break;
634 /* Separate the operator's code into priority and flags. */
635 flags = EXTRACT_FLAGS(prio);
636 prio = EXTRACT_PRIO(prio);
637 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
640 /* Check for reductions. Then push the operator. */
641 while (prio <= top->prio)
643 HOST_WIDEST_INT v1, v2;
644 unsigned int unsigned1, unsigned2;
646 /* Most operators that can appear on the stack require a
647 right operand. Check this before trying to reduce. */
648 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
650 if (top->op == CPP_OPEN_PAREN)
651 SYNTAX_ERROR ("void expression between '(' and ')'");
653 SYNTAX_ERROR2 ("operator '%s' has no right operand",
654 op_as_text (pfile, top->op));
657 unsigned2 = top->unsignedp, v2 = top->value;
659 unsigned1 = top->unsignedp, v1 = top->value;
661 /* Now set top->value = (top[1].op)(v1, v2); */
665 cpp_error (pfile, DL_ICE, "impossible operator '%s'",
666 op_as_text (pfile, top[1].op));
669 case CPP_NOT: UNARY(!); break;
670 case CPP_COMPL: UNARY(~); break;
671 case CPP_LESS: COMPARE(<); break;
672 case CPP_GREATER: COMPARE(>); break;
673 case CPP_LESS_EQ: COMPARE(<=); break;
674 case CPP_GREATER_EQ: COMPARE(>=); break;
675 case CPP_EQ_EQ: EQUALITY(==); break;
676 case CPP_NOT_EQ: EQUALITY(!=); break;
677 case CPP_AND: BITWISE(&); break;
678 case CPP_XOR: BITWISE(^); break;
679 case CPP_OR: BITWISE(|); break;
680 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
681 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
682 case CPP_MIN: MINMAX(<); break;
683 case CPP_MAX: MINMAX(>); break;
686 if (!(top->flags & HAVE_VALUE))
688 /* Can't use UNARY(+) because K+R C did not have unary
689 plus. Can't use UNARY() because some compilers object
690 to the empty argument. */
692 top->unsignedp = unsigned2;
693 top->flags |= HAVE_VALUE;
695 if (CPP_WTRADITIONAL (pfile))
696 cpp_error (pfile, DL_WARNING,
697 "traditional C rejects the unary plus operator");
701 top->value = v1 + v2;
702 top->unsignedp = unsigned1 | unsigned2;
703 if (! top->unsignedp && ! skip_evaluation
704 && ! possible_sum_sign (v1, v2, top->value))
705 integer_overflow (pfile);
709 if (!(top->flags & HAVE_VALUE))
712 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
713 integer_overflow (pfile);
717 top->value = v1 - v2;
718 top->unsignedp = unsigned1 | unsigned2;
719 if (! top->unsignedp && ! skip_evaluation
720 && ! possible_sum_sign (top->value, v2, v1))
721 integer_overflow (pfile);
725 top->unsignedp = unsigned1 | unsigned2;
727 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
728 else if (!skip_evaluation)
730 top->value = v1 * v2;
731 if (v1 && (top->value / v1 != v2
732 || (top->value & v1 & v2) < 0))
733 integer_overflow (pfile);
741 SYNTAX_ERROR ("division by zero in #if");
742 top->unsignedp = unsigned1 | unsigned2;
743 if (top[1].op == CPP_DIV)
746 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
749 top->value = v1 / v2;
750 if ((top->value & v1 & v2) < 0)
751 integer_overflow (pfile);
757 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
759 top->value = v1 % v2;
764 top->value = v1 || v2;
766 if (v1) skip_evaluation--;
769 top->value = v1 && v2;
771 if (!v1) skip_evaluation--;
774 if (CPP_PEDANTIC (pfile))
775 cpp_error (pfile, DL_PEDWARN,
776 "comma operator in operand of #if");
778 top->unsignedp = unsigned2;
781 SYNTAX_ERROR ("syntax error '?' without following ':'");
783 if (top[0].op != CPP_QUERY)
784 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
786 if (top->value) skip_evaluation--;
787 top->value = top->value ? v1 : v2;
788 top->unsignedp = unsigned1 | unsigned2;
791 if (op.op != CPP_CLOSE_PAREN)
792 SYNTAX_ERROR ("missing ')' in expression");
794 op.unsignedp = unsigned2;
797 /* Reducing this dummy operator indicates we've finished. */
798 if (op.op == CPP_CLOSE_PAREN)
799 SYNTAX_ERROR ("missing '(' in expression");
804 /* Handle short-circuit evaluations. */
805 if (flags & SHORT_CIRCUIT)
808 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
810 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
812 if (top[-1].value) /* Was '?' condition true? */
821 /* Check we have a left operand iff we need one. */
822 if (flags & NO_L_OPERAND)
824 if (top->flags & HAVE_VALUE)
825 SYNTAX_ERROR2 ("missing binary operator before '%s'",
826 op_as_text (pfile, op.op));
830 if (!(top->flags & HAVE_VALUE))
831 SYNTAX_ERROR2 ("operator '%s' has no left operand",
832 op_as_text (pfile, op.op));
835 /* Check for and handle stack overflow. */
839 struct op *new_stack;
840 int old_size = (char *) limit - (char *) stack;
841 int new_size = 2 * old_size;
842 if (stack != init_stack)
843 new_stack = (struct op *) xrealloc (stack, new_size);
846 new_stack = (struct op *) xmalloc (new_size);
847 memcpy (new_stack, stack, old_size);
850 top = (struct op *) ((char *) new_stack + old_size);
851 limit = (struct op *) ((char *) new_stack + new_size);
855 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
860 /* The controlling macro expression is only valid if we called lex 3
861 times: <!> <defined expression> and <EOF>. push_conditional ()
862 checks that we are at top-of-file. */
863 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
864 pfile->mi_ind_cmacro = 0;
866 result = (top[1].value != 0);
870 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
873 else if (!(top[1].flags & HAVE_VALUE))
875 SYNTAX_ERROR ("#if with no expression");
877 result = 0; /* Return 0 on syntax error. */
880 /* Free dynamic stack if we allocated one. */
881 if (stack != init_stack)
886 /* Output OP as text for diagnostics. */
887 static const unsigned char *
888 op_as_text (pfile, op)
896 return cpp_token_as_text (pfile, &token);