1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
23 Written by Per Bothner 1994. */
25 /* Parse a C expression from text in a string */
32 #ifndef CHAR_TYPE_SIZE
33 #define CHAR_TYPE_SIZE BITS_PER_UNIT
37 #define INT_TYPE_SIZE BITS_PER_WORD
40 #ifndef LONG_TYPE_SIZE
41 #define LONG_TYPE_SIZE BITS_PER_WORD
44 #ifndef WCHAR_TYPE_SIZE
45 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
48 #ifndef MAX_CHAR_TYPE_SIZE
49 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
52 #ifndef MAX_INT_TYPE_SIZE
53 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
56 #ifndef MAX_LONG_TYPE_SIZE
57 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
60 #ifndef MAX_WCHAR_TYPE_SIZE
61 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
64 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
65 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
66 : ~ (HOST_WIDEST_INT) 0)
68 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
69 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
70 : ~ (HOST_WIDEST_INT) 0)
72 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
73 number with SUM's sign, where A, B, and SUM are all C integers. */
74 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
78 static void integer_overflow PARAMS ((cpp_reader *));
79 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
81 unsigned HOST_WIDEST_INT));
82 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
84 unsigned HOST_WIDEST_INT));
85 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
87 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
89 static struct operation parse_defined PARAMS ((cpp_reader *));
90 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
92 static struct operation lex PARAMS ((cpp_reader *, int));
93 static const char * op_to_str PARAMS ((op_t));
112 U_CHAR prio; /* Priority of op. */
114 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
115 HOST_WIDEST_INT value; /* The value logically "right" of op. */
118 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
119 with or without size suffixes. */
121 static struct operation
122 parse_number (pfile, start, end)
130 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
133 int digit, largest_digit = 0;
140 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
152 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
153 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
154 / ((unsigned HOST_WIDEST_INT) base));
160 if (c >= '0' && c <= '9')
162 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
163 digit = c - 'a' + 10;
164 else if (base == 16 && c >= 'A' && c <= 'F')
165 digit = c - 'A' + 10;
168 /* It's a float since it contains a point. */
170 "floating point numbers are not allowed in #if expressions");
175 /* `l' means long, and `u' means unsigned. */
178 if (c == 'l' || c == 'L')
180 else if (c == 'u' || c == 'U')
184 /* Decrement p here so that the error for an invalid number
185 will be generated below in the case where this is the
186 last character in the buffer. */
194 /* Don't look for any more digits after the suffixes. */
198 if (largest_digit < digit)
199 largest_digit = digit;
200 nd = n * base + digit;
201 overflow |= MAX_over_base < n || nd < n;
207 cpp_error (pfile, "invalid number in #if expression");
210 else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2))
212 cpp_error (pfile, "too many `l' suffixes in integer constant");
215 else if (op.unsignedp > 1)
217 cpp_error (pfile, "too many `u' suffixes in integer constant");
221 if (base <= largest_digit)
222 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
225 cpp_pedwarn (pfile, "integer constant out of range");
227 /* If too big to be signed, consider it unsigned. */
228 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
232 "integer constant is so large that it is unsigned");
245 /* Parse and convert a character constant for #if. Understands backslash
246 escapes (\n, \031) and multibyte characters (if so configured). */
247 static struct operation
248 parse_charconst (pfile, start, end)
254 HOST_WIDEST_INT result = 0;
257 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
266 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
268 max_chars = MAX_LONG_TYPE_SIZE / width;
270 ++ptr; /* skip initial quote */
275 if (c == '\'' || c == '\0')
279 c = parse_escape (pfile, &ptr, mask);
280 if (width < HOST_BITS_PER_INT
281 && (unsigned int) c >= (unsigned int)(1 << width))
282 cpp_pedwarn (pfile, "escape sequence out of range for character");
285 /* Merge character into result; ignore excess chars. */
286 if (++num_chars <= max_chars)
288 if (width < HOST_BITS_PER_INT)
289 result = (result << width) | (c & ((1 << width) - 1));
297 cpp_error (pfile, "empty character constant");
302 /* cpp_get_token has already emitted an error if !traditional. */
303 if (! CPP_TRADITIONAL (pfile))
304 cpp_error (pfile, "malformatted character constant");
307 else if (num_chars > max_chars)
309 cpp_error (pfile, "character constant too long");
312 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
313 cpp_warning (pfile, "multi-character character constant");
315 /* If char type is signed, sign-extend the constant. */
316 num_bits = num_chars * width;
318 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
319 sizeof ("__CHAR_UNSIGNED__")-1)
320 || ((result >> (num_bits - 1)) & 1) == 0)
321 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
322 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
324 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
325 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
327 /* This is always a signed type. */
337 static struct operation
338 parse_defined (pfile)
343 enum cpp_token token;
345 long old_written = CPP_WRITTEN (pfile);
350 pfile->no_macro_expand++;
351 token = _cpp_get_directive_token (pfile);
352 if (token == CPP_LPAREN)
355 CPP_SET_WRITTEN (pfile, old_written);
356 token = _cpp_get_directive_token (pfile);
359 if (token != CPP_NAME)
362 tok = pfile->token_buffer + old_written;
363 len = CPP_PWRITTEN (pfile) - tok;
364 op.value = cpp_defined (pfile, tok, len);
368 if (_cpp_get_directive_token (pfile) != CPP_RPAREN)
371 CPP_SET_WRITTEN (pfile, old_written);
372 pfile->no_macro_expand--;
376 CPP_SET_WRITTEN (pfile, old_written);
377 pfile->no_macro_expand--;
378 cpp_error (pfile, "`defined' without an identifier");
386 const char *operator;
390 static const struct token tokentab2[] =
405 /* Read one token. */
407 static struct operation
408 lex (pfile, skip_evaluation)
412 const struct token *toktab;
413 enum cpp_token token;
415 U_CHAR *tok_start, *tok_end;
418 old_written = CPP_WRITTEN (pfile);
419 token = _cpp_get_directive_token (pfile);
421 tok_start = pfile->token_buffer + old_written;
422 tok_end = CPP_PWRITTEN (pfile);
423 CPP_SET_WRITTEN (pfile, old_written);
426 case CPP_EOF: /* Should not happen ... */
431 return parse_number (pfile, tok_start, tok_end);
434 cpp_error (pfile, "string constants are not allowed in #if expressions");
440 return parse_charconst (pfile, tok_start, tok_end);
443 if (!strcmp (tok_start, "defined"))
444 return parse_defined (pfile);
450 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
451 cpp_warning (pfile, "`%.*s' is not defined",
452 (int) (tok_end - tok_start), tok_start);
458 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
462 /* See if it is a special token of length 2. */
463 if (tok_start + 2 == tok_end)
465 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
466 if (tok_start[0] == toktab->operator[0]
467 && tok_start[1] == toktab->operator[1])
469 if (toktab->token == ERROR)
470 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
472 op.op = toktab->token;
482 /* Convert an operator ID to a string. */
487 static char str[5]; /* XXX static variable. */
488 const struct token *toktab;
490 /* See if it is a special token of length 2. */
491 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
492 if (op == toktab->token)
493 return toktab->operator;
496 sprintf (str, "%c", (int) op);
498 sprintf (str, "\\%03o", (int) op);
502 /* Parse a C escape sequence. STRING_PTR points to a variable
503 containing a pointer to the string to parse. That pointer
504 is updated past the characters we use. The value of the
505 escape sequence is returned.
507 A negative value means the sequence \ newline was seen,
508 which is supposed to be equivalent to nothing at all.
510 If \ is followed by a null character, we return a negative
511 value and leave the string pointer pointing at the null character.
513 If \ is followed by 000, we return 0 and leave the string pointer
514 after the zeros. A value of 0 does not mean end of string. */
516 static HOST_WIDEST_INT
517 parse_escape (pfile, string_ptr, result_mask)
520 HOST_WIDEST_INT result_mask;
522 register int c = *(*string_ptr)++;
531 if (CPP_PEDANTIC (pfile))
532 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
537 return TARGET_NEWLINE;
559 register HOST_WIDEST_INT i = c - '0';
560 register int count = 0;
563 c = *(*string_ptr)++;
564 if (c >= '0' && c <= '7')
565 i = (i << 3) + c - '0';
572 if (i != (i & result_mask))
575 cpp_pedwarn (pfile, "octal escape sequence out of range");
581 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
582 register int digits_found = 0, digit;
585 c = *(*string_ptr)++;
586 if (c >= '0' && c <= '9')
588 else if (c >= 'a' && c <= 'f')
589 digit = c - 'a' + 10;
590 else if (c >= 'A' && c <= 'F')
591 digit = c - 'A' + 10;
597 overflow |= i ^ (i << 4 >> 4);
598 i = (i << 4) + digit;
602 cpp_error (pfile, "\\x used with no following hex digits");
603 if (overflow | (i != (i & result_mask)))
606 cpp_pedwarn (pfile, "hex escape sequence out of range");
616 integer_overflow (pfile)
619 if (CPP_PEDANTIC (pfile))
620 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
623 static HOST_WIDEST_INT
624 left_shift (pfile, a, unsignedp, b)
627 unsigned int unsignedp;
628 unsigned HOST_WIDEST_INT b;
630 if (b >= HOST_BITS_PER_WIDEST_INT)
632 if (! unsignedp && a != 0)
633 integer_overflow (pfile);
637 return (unsigned HOST_WIDEST_INT) a << b;
640 HOST_WIDEST_INT l = a << b;
642 integer_overflow (pfile);
647 static HOST_WIDEST_INT
648 right_shift (pfile, a, unsignedp, b)
649 cpp_reader *pfile ATTRIBUTE_UNUSED;
651 unsigned int unsignedp;
652 unsigned HOST_WIDEST_INT b;
654 if (b >= HOST_BITS_PER_WIDEST_INT)
655 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
657 return (unsigned HOST_WIDEST_INT) a >> b;
662 /* Operator precedence and flags table.
664 After an operator is returned from the lexer, if it has priority less
665 than or equal to the operator on the top of the stack, we reduce the
666 stack by one operator and repeat the test. Since equal priorities
667 reduce, this is naturally left-associative.
669 We handle right-associative operators by clearing the lower bit of all
670 left-associative operators, and setting it for right-associative ones.
671 After the reduction phase of a new operator, just before it is pushed
672 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
673 during the reduction phase, the current right-associative operator has
674 a priority one greater than any other operator of otherwise equal
675 precedence that has been pushed on the top of the stack. This avoids
676 a reduction pass, and effectively makes the logic right-associative.
678 The remaining cases are '(' and ')'. We handle '(' by skipping the
679 reduction phase completely. ')' is given lower priority than
680 everything else, including '(', effectively forcing a reduction of the
681 parenthesised expression. If there is no matching '(', the stack will
682 be reduced all the way to the beginning, exiting the parser in the
683 same way as the ultra-low priority end-of-expression dummy operator.
684 The exit code checks to see if the operator that caused it is ')', and
685 if so outputs an appropriate error message.
687 The parser assumes all shifted operators require a right operand
688 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
689 These semantics are automatically checked, any extra semantics need to
690 be handled with operator-specific code. */
693 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
694 #define PRIO_SHIFT (FLAG_BITS + 1)
695 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
696 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
699 #define HAVE_VALUE (1 << 0)
700 #define NO_L_OPERAND (1 << 1)
701 #define NO_R_OPERAND (1 << 2)
702 #define SHORT_CIRCUIT (1 << 3)
704 /* Priority and flag combinations. */
705 #define RIGHT_ASSOC (1 << FLAG_BITS)
706 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
707 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
708 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
709 #define COMMA_PRIO (3 << PRIO_SHIFT)
710 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
711 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
712 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
713 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
714 #define OR_PRIO (8 << PRIO_SHIFT)
715 #define XOR_PRIO (9 << PRIO_SHIFT)
716 #define AND_PRIO (10 << PRIO_SHIFT)
717 #define EQUAL_PRIO (11 << PRIO_SHIFT)
718 #define LESS_PRIO (12 << PRIO_SHIFT)
719 #define SHIFT_PRIO (13 << PRIO_SHIFT)
720 #define PLUS_PRIO (14 << PRIO_SHIFT)
721 #define MUL_PRIO (15 << PRIO_SHIFT)
722 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
724 #define COMPARE(OP) \
725 top->unsignedp = 0; \
726 top->value = (unsigned1 || unsigned2) \
727 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
729 #define EQUALITY(OP) \
730 top->value = v1 OP v2; \
732 #define LOGICAL(OP) \
733 top->value = v1 OP v2; \
734 top->unsignedp = unsigned1 || unsigned2;
736 /* Parse and evaluate a C expression, reading from PFILE.
737 Returns the truth value of the expression. */
740 _cpp_parse_expr (pfile)
743 /* The implementation is an operator precedence parser, i.e. a
744 bottom-up parser, using a stack for not-yet-reduced tokens.
746 The stack base is 'stack', and the current stack pointer is 'top'.
747 There is a stack element for each operator (only),
748 and the most recently pushed operator is 'top->op'.
749 An operand (value) is stored in the 'value' field of the stack
750 element of the operator that precedes it.
751 In that case the 'flags' field has the HAVE_VALUE flag set. */
753 #define INIT_STACK_SIZE 20
754 struct operation init_stack[INIT_STACK_SIZE];
755 struct operation *stack = init_stack;
756 struct operation *limit = stack + INIT_STACK_SIZE;
757 register struct operation *top = stack + 1;
758 long old_written = CPP_WRITTEN (pfile);
759 int skip_evaluation = 0;
762 pfile->parsing_if_directive++;
763 /* We've finished when we try to reduce this. */
765 /* Nifty way to catch missing '('. */
766 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
767 /* Avoid missing right operand checks. */
768 top->flags = NO_R_OPERAND;
777 op = lex (pfile, skip_evaluation);
779 /* If the token is an operand, push its value and get next
780 token. If it is an operator, get its priority and flags, and
781 try to reduce the expression on the stack. */
785 cpp_ice (pfile, "lex returns a NAME");
789 cpp_error (pfile, "invalid character in #if");
795 /* Push a value onto the stack. */
796 if (top->flags & HAVE_VALUE)
798 cpp_error (pfile, "missing binary operator");
801 top->value = op.value;
802 top->unsignedp = op.unsignedp;
803 top->flags |= HAVE_VALUE;
807 case '-': prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
808 /* else unary; fall through */
810 case '~': prio = UNARY_PRIO; break;
814 case '%': prio = MUL_PRIO; break;
818 case GEQ: prio = LESS_PRIO; break;
820 case EQUAL: prio = EQUAL_PRIO; break;
822 case RSH: prio = SHIFT_PRIO; break;
823 case '&': prio = AND_PRIO; break;
824 case '^': prio = XOR_PRIO; break;
825 case '|': prio = OR_PRIO; break;
826 case ANDAND: prio = ANDAND_PRIO; break;
827 case OROR: prio = OROR_PRIO; break;
828 case ',': prio = COMMA_PRIO; break;
829 case '(': prio = OPEN_PAREN_PRIO; break;
830 case ')': prio = CLOSE_PAREN_PRIO; break;
831 case ':': prio = COLON_PRIO; break;
832 case '?': prio = COND_PRIO; break;
833 case 0: prio = FORCE_REDUCE_PRIO; break;
836 /* Separate the operator's code into priority and flags. */
837 flags = EXTRACT_FLAGS(prio);
838 prio = EXTRACT_PRIO(prio);
842 /* Check for reductions. Then push the operator. */
843 while (prio <= top->prio)
845 HOST_WIDEST_INT v1, v2;
846 unsigned int unsigned1, unsigned2;
848 /* Most operators that can appear on the stack require a
849 right operand. Check this before trying to reduce. */
850 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
853 cpp_error (pfile, "void expression between '(' and ')'");
855 cpp_error (pfile, "operator `%s' has no right operand",
856 op_to_str (top->op));
860 unsigned2 = top->unsignedp, v2 = top->value;
862 unsigned1 = top->unsignedp, v1 = top->value;
864 /* Now set top->value = (top[1].op)(v1, v2); */
868 if (!(top->flags & HAVE_VALUE))
871 top->unsignedp = unsigned2;
872 top->flags |= HAVE_VALUE;
876 top->value = v1 + v2;
877 top->unsignedp = unsigned1 || unsigned2;
878 if (! top->unsignedp && ! skip_evaluation
879 && ! possible_sum_sign (v1, v2, top->value))
880 integer_overflow (pfile);
884 if (!(top->flags & HAVE_VALUE))
887 if (!skip_evaluation && (top->value & v2) < 0
889 integer_overflow (pfile);
890 top->unsignedp = unsigned2;
891 top->flags |= HAVE_VALUE;
895 top->value = v1 - v2;
896 top->unsignedp = unsigned1 || unsigned2;
897 if (! top->unsignedp && ! skip_evaluation
898 && ! possible_sum_sign (top->value, v2, v1))
899 integer_overflow (pfile);
903 top->unsignedp = unsigned1 || unsigned2;
905 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
906 else if (!skip_evaluation)
908 top->value = v1 * v2;
909 if (v1 && (top->value / v1 != v2
910 || (top->value & v1 & v2) < 0))
911 integer_overflow (pfile);
920 cpp_error (pfile, "division by zero in #if");
923 top->unsignedp = unsigned1 || unsigned2;
924 if (top[1].op == '/')
927 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
930 top->value = v1 / v2;
931 if ((top->value & v1 & v2) < 0)
932 integer_overflow (pfile);
938 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
940 top->value = v1 % v2;
946 top->flags |= HAVE_VALUE;
950 top->unsignedp = unsigned2;
951 top->flags |= HAVE_VALUE;
953 case '<': COMPARE(<); break;
954 case '>': COMPARE(>); break;
955 case LEQ: COMPARE(<=); break;
956 case GEQ: COMPARE(>=); break;
957 case EQUAL: EQUALITY(==); break;
958 case NOTEQUAL: EQUALITY(!=); break;
962 top->unsignedp = unsigned1;
963 if (v2 < 0 && ! unsigned2)
964 top->value = right_shift (pfile, v1, unsigned1, -v2);
966 top->value = left_shift (pfile, v1, unsigned1, v2);
971 top->unsignedp = unsigned1;
972 if (v2 < 0 && ! unsigned2)
973 top->value = left_shift (pfile, v1, unsigned1, -v2);
975 top->value = right_shift (pfile, v1, unsigned1, v2);
977 case '&': LOGICAL(&); break;
978 case '^': LOGICAL(^); break;
979 case '|': LOGICAL(|); break;
981 top->value = v1 && v2; top->unsignedp = 0;
982 if (!v1) skip_evaluation--;
985 top->value = v1 || v2; top->unsignedp = 0;
986 if (v1) skip_evaluation--;
989 if (CPP_PEDANTIC (pfile))
990 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
992 top->unsignedp = unsigned2;
995 cpp_error (pfile, "syntax error '?' without following ':'");
998 if (top[0].op != '?')
1001 "syntax error ':' without preceding '?'");
1005 if (top->value) skip_evaluation--;
1006 top->value = top->value ? v1 : v2;
1007 top->unsignedp = unsigned1 || unsigned2;
1012 cpp_error (pfile, "missing ')' in expression");
1016 op.unsignedp = unsigned2;
1017 goto push_immediate;
1019 cpp_error (pfile, "unimplemented operator `%s'",
1020 op_to_str (top[1].op));
1023 /* Reducing this dummy operator indicates we've finished. */
1026 cpp_error (pfile, "missing '(' in expression");
1033 /* Handle short-circuit evaluations. */
1034 if (flags & SHORT_CIRCUIT)
1037 case OROR: if (top->value) skip_evaluation++; break;
1039 case '?': if (!top->value) skip_evaluation++; break;
1041 if (top[-1].value) /* Was '?' condition true? */
1048 /* Check we have a left operand iff we need one. */
1049 if (((flags & NO_L_OPERAND) != 0) ^ ((top->flags & HAVE_VALUE) == 0))
1051 if (flags & NO_L_OPERAND)
1052 cpp_error (pfile, "missing binary operator before `%s'",
1055 cpp_error (pfile, "operator `%s' has no left operand",
1060 /* Check for and handle stack overflow. */
1064 struct operation *new_stack;
1065 int old_size = (char *) limit - (char *) stack;
1066 int new_size = 2 * old_size;
1067 if (stack != init_stack)
1068 new_stack = (struct operation *) xrealloc (stack, new_size);
1071 new_stack = (struct operation *) xmalloc (new_size);
1072 memcpy (new_stack, stack, old_size);
1075 top = (struct operation *) ((char *) new_stack + old_size);
1076 limit = (struct operation *) ((char *) new_stack + new_size);
1080 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1085 result = (top[1].value != 0);
1088 cpp_ice (pfile, "unbalanced stack in #if expression");
1091 else if (!(top[1].flags & HAVE_VALUE))
1093 cpp_error (pfile, "#if with no expression");
1095 _cpp_skip_rest_of_line (pfile);
1096 result = 0; /* Return 0 on syntax error. */
1099 /* Free dynamic stack if we allocated one. */
1100 if (stack != init_stack)
1102 pfile->parsing_if_directive--;
1103 CPP_SET_WRITTEN (pfile, old_written);