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)
73 static void integer_overflow PARAMS ((cpp_reader *));
74 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
76 unsigned HOST_WIDEST_INT));
77 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
79 unsigned HOST_WIDEST_INT));
80 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
82 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
84 static struct operation parse_defined PARAMS ((cpp_reader *));
85 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
87 static struct operation lex PARAMS ((cpp_reader *, int));
88 static const char * op_to_str PARAMS ((op_t, char *));
107 U_CHAR prio; /* Priority of op. */
109 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
110 HOST_WIDEST_INT value; /* The value logically "right" of op. */
113 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
114 with or without size suffixes. */
116 static struct operation
117 parse_number (pfile, start, end)
125 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
128 int digit, largest_digit = 0;
135 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
147 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
148 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
149 / ((unsigned HOST_WIDEST_INT) base));
155 if (c >= '0' && c <= '9')
157 /* FIXME: assumes ASCII */
158 else if (base == 16 && c >= 'a' && c <= 'f')
159 digit = c - 'a' + 10;
160 else if (base == 16 && c >= 'A' && c <= 'F')
161 digit = c - 'A' + 10;
164 /* It's a float since it contains a point. */
166 "floating point numbers are not allowed in #if expressions");
171 /* `l' means long, and `u' means unsigned. */
174 if (c == 'l' || c == 'L')
176 else if (c == 'u' || c == 'U')
180 /* Decrement p here so that the error for an invalid
181 number will be generated below in the case where
182 this is the last character in the buffer. */
190 /* Don't look for any more digits after the suffixes. */
194 if (largest_digit < digit)
195 largest_digit = digit;
196 nd = n * base + digit;
197 overflow |= MAX_over_base < n || nd < n;
203 cpp_error (pfile, "invalid number in #if expression");
206 else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2))
208 cpp_error (pfile, "too many 'l' suffixes in integer constant");
211 else if (op.unsignedp > 1)
213 cpp_error (pfile, "too many 'u' suffixes in integer constant");
217 if (base <= largest_digit)
219 "integer constant contains digits beyond the radix");
222 cpp_pedwarn (pfile, "integer constant out of range");
224 /* If too big to be signed, consider it unsigned. */
225 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
229 "integer constant is so large that it is unsigned");
242 /* Parse and convert a character constant for #if. Understands backslash
243 escapes (\n, \031) and multibyte characters (if so configured). */
244 static struct operation
245 parse_charconst (pfile, start, end)
251 HOST_WIDEST_INT result = 0;
254 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
263 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
265 max_chars = MAX_LONG_TYPE_SIZE / width;
267 ++ptr; /* skip initial quote */
276 c = parse_escape (pfile, &ptr, mask);
277 if (width < HOST_BITS_PER_INT
278 && (unsigned int) c >= (unsigned int)(1 << width))
280 "escape sequence out of range for character");
283 /* Merge character into result; ignore excess chars. */
284 if (++num_chars <= max_chars)
286 if (width < HOST_BITS_PER_INT)
287 result = (result << width) | (c & ((1 << width) - 1));
295 cpp_error (pfile, "empty character constant");
300 /* cpp_get_token has already emitted an error if !traditional. */
301 if (! CPP_TRADITIONAL (pfile))
302 cpp_error (pfile, "malformatted character constant");
305 else if (num_chars > max_chars)
307 cpp_error (pfile, "character constant too long");
310 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
311 cpp_warning (pfile, "multi-character character constant");
313 /* If char type is signed, sign-extend the constant. */
314 num_bits = num_chars * width;
316 if (cpp_defined (pfile, U"__CHAR_UNSIGNED__",
317 sizeof ("__CHAR_UNSIGNED__")-1)
318 || ((result >> (num_bits - 1)) & 1) == 0)
319 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
320 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
322 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
323 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
325 /* This is always a signed type. */
335 static struct operation
336 parse_defined (pfile)
341 enum cpp_ttype token;
343 long old_written = CPP_WRITTEN (pfile);
348 pfile->no_macro_expand++;
349 token = _cpp_get_directive_token (pfile);
350 if (token == CPP_OPEN_PAREN)
353 CPP_SET_WRITTEN (pfile, old_written);
354 token = _cpp_get_directive_token (pfile);
357 if (token != CPP_NAME)
360 tok = pfile->token_buffer + old_written;
361 len = CPP_PWRITTEN (pfile) - tok;
362 op.value = cpp_defined (pfile, tok, len);
366 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
369 CPP_SET_WRITTEN (pfile, old_written);
370 pfile->no_macro_expand--;
374 CPP_SET_WRITTEN (pfile, old_written);
375 pfile->no_macro_expand--;
376 cpp_error (pfile, "'defined' without an identifier");
384 const char *operator;
388 static const struct token tokentab2[] =
403 /* Read one token. */
405 static struct operation
406 lex (pfile, skip_evaluation)
410 const struct token *toktab;
411 enum cpp_ttype token;
413 U_CHAR *tok_start, *tok_end;
416 old_written = CPP_WRITTEN (pfile);
417 token = _cpp_get_directive_token (pfile);
419 tok_start = pfile->token_buffer + old_written;
420 tok_end = CPP_PWRITTEN (pfile);
421 CPP_SET_WRITTEN (pfile, old_written);
424 case CPP_EOF: /* Should not happen ... */
429 return parse_number (pfile, tok_start, tok_end);
433 "string constants are not allowed in #if expressions");
439 return parse_charconst (pfile, tok_start, tok_end);
442 if (!ustrncmp (tok_start, U"defined", 7))
443 return parse_defined (pfile);
449 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
450 cpp_warning (pfile, "'%.*s' is not defined",
451 (int) (tok_end - tok_start), tok_start);
457 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
461 /* See if it is a special token of length 2. */
462 if (tok_start + 2 == tok_end)
464 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
465 if (tok_start[0] == toktab->operator[0]
466 && tok_start[1] == toktab->operator[1])
468 if (toktab->token == ERROR)
469 cpp_error (pfile, "'%.*s' is not allowed in #if expressions",
470 (int) (tok_end - tok_start), tok_start);
471 op.op = toktab->token;
481 /* Convert an operator ID to a string. BUFF is a buffer at least 5
482 characters long which might be used to store the string. */
483 /* XXX FIXME: Remove BUFF when new lexer is implemented. */
489 const struct token *toktab;
491 /* See if it is a special token of length 2. */
492 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
493 if (op == toktab->token)
494 return toktab->operator;
497 sprintf (buff, "%c", (int) op);
499 sprintf (buff, "\\%03o", (int) op);
503 /* Parse a C escape sequence. STRING_PTR points to a variable
504 containing a pointer to the string to parse. That pointer
505 is updated past the characters we use. The value of the
506 escape sequence is returned.
508 A negative value means the sequence \ newline was seen,
509 which is supposed to be equivalent to nothing at all.
511 If \ is followed by a null character, we return a negative
512 value and leave the string pointer pointing at the null character.
514 If \ is followed by 000, we return 0 and leave the string pointer
515 after the zeros. A value of 0 does not mean end of string. */
517 static HOST_WIDEST_INT
518 parse_escape (pfile, string_ptr, result_mask)
521 HOST_WIDEST_INT result_mask;
523 register int c = *(*string_ptr)++;
532 if (CPP_PEDANTIC (pfile))
533 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, '\\%c'", c);
538 return TARGET_NEWLINE;
560 register HOST_WIDEST_INT i = c - '0';
561 register int count = 0;
564 c = *(*string_ptr)++;
565 if (c >= '0' && c <= '7')
566 i = (i << 3) + c - '0';
573 if (i != (i & result_mask))
576 cpp_pedwarn (pfile, "octal escape sequence out of range");
582 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
583 register int digits_found = 0, digit;
586 c = *(*string_ptr)++;
587 if (c >= '0' && c <= '9')
589 else if (c >= 'a' && c <= 'f')
590 digit = c - 'a' + 10;
591 else if (c >= 'A' && c <= 'F')
592 digit = c - 'A' + 10;
598 overflow |= i ^ (i << 4 >> 4);
599 i = (i << 4) + digit;
603 cpp_error (pfile, "\\x used with no following hex digits");
604 if (overflow | (i != (i & result_mask)))
607 cpp_pedwarn (pfile, "hex escape sequence out of range");
617 integer_overflow (pfile)
620 if (CPP_PEDANTIC (pfile))
621 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
624 static HOST_WIDEST_INT
625 left_shift (pfile, a, unsignedp, b)
628 unsigned int unsignedp;
629 unsigned HOST_WIDEST_INT b;
631 if (b >= HOST_BITS_PER_WIDEST_INT)
633 if (! unsignedp && a != 0)
634 integer_overflow (pfile);
638 return (unsigned HOST_WIDEST_INT) a << b;
641 HOST_WIDEST_INT l = a << b;
643 integer_overflow (pfile);
648 static HOST_WIDEST_INT
649 right_shift (pfile, a, unsignedp, b)
650 cpp_reader *pfile ATTRIBUTE_UNUSED;
652 unsigned int unsignedp;
653 unsigned HOST_WIDEST_INT b;
655 if (b >= HOST_BITS_PER_WIDEST_INT)
656 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
658 return (unsigned HOST_WIDEST_INT) a >> b;
663 /* Operator precedence and flags table.
665 After an operator is returned from the lexer, if it has priority less
666 than or equal to the operator on the top of the stack, we reduce the
667 stack by one operator and repeat the test. Since equal priorities
668 reduce, this is naturally left-associative.
670 We handle right-associative operators by clearing the lower bit of all
671 left-associative operators, and setting it for right-associative ones.
672 After the reduction phase of a new operator, just before it is pushed
673 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
674 during the reduction phase, the current right-associative operator has
675 a priority one greater than any other operator of otherwise equal
676 precedence that has been pushed on the top of the stack. This avoids
677 a reduction pass, and effectively makes the logic right-associative.
679 The remaining cases are '(' and ')'. We handle '(' by skipping the
680 reduction phase completely. ')' is given lower priority than
681 everything else, including '(', effectively forcing a reduction of the
682 parenthesised expression. If there is no matching '(', the stack will
683 be reduced all the way to the beginning, exiting the parser in the
684 same way as the ultra-low priority end-of-expression dummy operator.
685 The exit code checks to see if the operator that caused it is ')', and
686 if so outputs an appropriate error message.
688 The parser assumes all shifted operators require a right operand
689 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
690 These semantics are automatically checked, any extra semantics need to
691 be handled with operator-specific code. */
694 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
695 #define PRIO_SHIFT (FLAG_BITS + 1)
696 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
697 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
700 #define HAVE_VALUE (1 << 0)
701 #define NO_L_OPERAND (1 << 1)
702 #define NO_R_OPERAND (1 << 2)
703 #define SHORT_CIRCUIT (1 << 3)
705 /* Priority and flag combinations. */
706 #define RIGHT_ASSOC (1 << FLAG_BITS)
707 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
708 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
709 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
710 #define COMMA_PRIO (3 << PRIO_SHIFT)
711 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
712 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
713 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
714 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
715 #define OR_PRIO (8 << PRIO_SHIFT)
716 #define XOR_PRIO (9 << PRIO_SHIFT)
717 #define AND_PRIO (10 << PRIO_SHIFT)
718 #define EQUAL_PRIO (11 << PRIO_SHIFT)
719 #define LESS_PRIO (12 << PRIO_SHIFT)
720 #define SHIFT_PRIO (13 << PRIO_SHIFT)
721 #define PLUS_PRIO (14 << PRIO_SHIFT)
722 #define MUL_PRIO (15 << PRIO_SHIFT)
723 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
725 #define COMPARE(OP) \
726 top->unsignedp = 0; \
727 top->value = (unsigned1 | unsigned2) \
728 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
730 #define EQUALITY(OP) \
731 top->value = v1 OP v2; \
733 #define LOGICAL(OP) \
734 top->value = v1 OP v2; \
735 top->unsignedp = unsigned1 | unsigned2;
737 /* With -O2, gcc appears to produce nice code, moving the error
738 message load and subsequent jump completely out of the main path. */
739 #define CPP_ICE(msgid) \
740 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
741 #define SYNTAX_ERROR(msgid) \
742 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
743 #define SYNTAX_ERROR2(msgid, arg) \
744 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
746 /* Parse and evaluate a C expression, reading from PFILE.
747 Returns the truth value of the expression. */
750 _cpp_parse_expr (pfile)
753 /* The implementation is an operator precedence parser, i.e. a
754 bottom-up parser, using a stack for not-yet-reduced tokens.
756 The stack base is 'stack', and the current stack pointer is 'top'.
757 There is a stack element for each operator (only),
758 and the most recently pushed operator is 'top->op'.
759 An operand (value) is stored in the 'value' field of the stack
760 element of the operator that precedes it.
761 In that case the 'flags' field has the HAVE_VALUE flag set. */
763 #define INIT_STACK_SIZE 20
764 struct operation init_stack[INIT_STACK_SIZE];
765 struct operation *stack = init_stack;
766 struct operation *limit = stack + INIT_STACK_SIZE;
767 register struct operation *top = stack + 1;
768 long old_written = CPP_WRITTEN (pfile);
769 int skip_evaluation = 0;
773 pfile->parsing_if_directive++;
774 /* We've finished when we try to reduce this. */
776 /* Nifty way to catch missing '('. */
777 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
778 /* Avoid missing right operand checks. */
779 top->flags = NO_R_OPERAND;
788 op = lex (pfile, skip_evaluation);
790 /* If the token is an operand, push its value and get next
791 token. If it is an operator, get its priority and flags, and
792 try to reduce the expression on the stack. */
796 CPP_ICE ("lex returns a NAME");
800 /* We get '#' when get_directive_token hits a syntactically
801 invalid assertion predicate. _cpp_parse_assertion has
802 already issued an error. */
805 SYNTAX_ERROR ("invalid character in #if");
810 /* Push a value onto the stack. */
811 if (top->flags & HAVE_VALUE)
812 SYNTAX_ERROR ("missing binary operator");
813 top->value = op.value;
814 top->unsignedp = op.unsignedp;
815 top->flags |= HAVE_VALUE;
819 case '-': prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
820 /* else unary; fall through */
822 case '~': prio = UNARY_PRIO; break;
826 case '%': prio = MUL_PRIO; break;
830 case GEQ: prio = LESS_PRIO; break;
832 case EQUAL: prio = EQUAL_PRIO; break;
834 case RSH: prio = SHIFT_PRIO; break;
835 case '&': prio = AND_PRIO; break;
836 case '^': prio = XOR_PRIO; break;
837 case '|': prio = OR_PRIO; break;
838 case ANDAND: prio = ANDAND_PRIO; break;
839 case OROR: prio = OROR_PRIO; break;
840 case ',': prio = COMMA_PRIO; break;
841 case '(': prio = OPEN_PAREN_PRIO; break;
842 case ')': prio = CLOSE_PAREN_PRIO; break;
843 case ':': prio = COLON_PRIO; break;
844 case '?': prio = COND_PRIO; break;
845 case 0: prio = FORCE_REDUCE_PRIO; break;
848 /* Separate the operator's code into priority and flags. */
849 flags = EXTRACT_FLAGS(prio);
850 prio = EXTRACT_PRIO(prio);
854 /* Check for reductions. Then push the operator. */
855 while (prio <= top->prio)
857 HOST_WIDEST_INT v1, v2;
858 unsigned int unsigned1, unsigned2;
860 /* Most operators that can appear on the stack require a
861 right operand. Check this before trying to reduce. */
862 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
865 SYNTAX_ERROR ("void expression between '(' and ')'");
867 SYNTAX_ERROR2 ("operator '%s' has no right operand",
868 op_to_str (top->op, buff));
871 unsigned2 = top->unsignedp, v2 = top->value;
873 unsigned1 = top->unsignedp, v1 = top->value;
875 /* Now set top->value = (top[1].op)(v1, v2); */
879 if (!(top->flags & HAVE_VALUE))
882 top->unsignedp = unsigned2;
883 top->flags |= HAVE_VALUE;
887 top->value = v1 + v2;
888 top->unsignedp = unsigned1 | unsigned2;
889 if (! top->unsignedp && ! skip_evaluation
890 && ! possible_sum_sign (v1, v2, top->value))
891 integer_overflow (pfile);
895 if (!(top->flags & HAVE_VALUE))
898 if (!skip_evaluation && (top->value & v2) < 0
900 integer_overflow (pfile);
901 top->unsignedp = unsigned2;
902 top->flags |= HAVE_VALUE;
906 top->value = v1 - v2;
907 top->unsignedp = unsigned1 | unsigned2;
908 if (! top->unsignedp && ! skip_evaluation
909 && ! possible_sum_sign (top->value, v2, v1))
910 integer_overflow (pfile);
914 top->unsignedp = unsigned1 | unsigned2;
916 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
917 else if (!skip_evaluation)
919 top->value = v1 * v2;
920 if (v1 && (top->value / v1 != v2
921 || (top->value & v1 & v2) < 0))
922 integer_overflow (pfile);
930 SYNTAX_ERROR ("division by zero in #if");
931 top->unsignedp = unsigned1 | unsigned2;
932 if (top[1].op == '/')
935 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
938 top->value = v1 / v2;
939 if ((top->value & v1 & v2) < 0)
940 integer_overflow (pfile);
946 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
948 top->value = v1 % v2;
954 top->flags |= HAVE_VALUE;
958 top->unsignedp = unsigned2;
959 top->flags |= HAVE_VALUE;
961 case '<': COMPARE(<); break;
962 case '>': COMPARE(>); break;
963 case LEQ: COMPARE(<=); break;
964 case GEQ: COMPARE(>=); break;
965 case EQUAL: EQUALITY(==); break;
966 case NOTEQUAL: EQUALITY(!=); break;
970 top->unsignedp = unsigned1;
971 if (v2 < 0 && ! unsigned2)
972 top->value = right_shift (pfile, v1, unsigned1, -v2);
974 top->value = left_shift (pfile, v1, unsigned1, v2);
979 top->unsignedp = unsigned1;
980 if (v2 < 0 && ! unsigned2)
981 top->value = left_shift (pfile, v1, unsigned1, -v2);
983 top->value = right_shift (pfile, v1, unsigned1, v2);
985 case '&': LOGICAL(&); break;
986 case '^': LOGICAL(^); break;
987 case '|': LOGICAL(|); break;
989 top->value = v1 && v2; top->unsignedp = 0;
990 if (!v1) skip_evaluation--;
993 top->value = v1 || v2; top->unsignedp = 0;
994 if (v1) skip_evaluation--;
997 if (CPP_PEDANTIC (pfile))
998 cpp_pedwarn (pfile, "comma operator in operand of #if");
1000 top->unsignedp = unsigned2;
1003 SYNTAX_ERROR ("syntax error '?' without following ':'");
1005 if (top[0].op != '?')
1006 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
1008 if (top->value) skip_evaluation--;
1009 top->value = top->value ? v1 : v2;
1010 top->unsignedp = unsigned1 | unsigned2;
1014 SYNTAX_ERROR ("missing ')' in expression");
1016 op.unsignedp = unsigned2;
1017 goto push_immediate;
1019 SYNTAX_ERROR2 ("unimplemented operator '%s'",
1020 op_to_str (top[1].op, buff));
1022 /* Reducing this dummy operator indicates we've finished. */
1024 SYNTAX_ERROR ("missing '(' in expression");
1029 /* Handle short-circuit evaluations. */
1030 if (flags & SHORT_CIRCUIT)
1033 case OROR: if (top->value) skip_evaluation++; break;
1035 case '?': if (!top->value) skip_evaluation++; break;
1037 if (top[-1].value) /* Was '?' condition true? */
1044 /* Check we have a left operand iff we need one. */
1045 if (flags & NO_L_OPERAND)
1047 if (top->flags & HAVE_VALUE)
1048 SYNTAX_ERROR2 ("missing binary operator before '%s'",
1049 op_to_str (op.op, buff));
1053 if (!(top->flags & HAVE_VALUE))
1054 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1055 op_to_str (op.op, buff));
1058 /* Check for and handle stack overflow. */
1062 struct operation *new_stack;
1063 int old_size = (char *) limit - (char *) stack;
1064 int new_size = 2 * old_size;
1065 if (stack != init_stack)
1066 new_stack = (struct operation *) xrealloc (stack, new_size);
1069 new_stack = (struct operation *) xmalloc (new_size);
1070 memcpy (new_stack, stack, old_size);
1073 top = (struct operation *) ((char *) new_stack + old_size);
1074 limit = (struct operation *) ((char *) new_stack + new_size);
1078 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1083 result = (top[1].value != 0);
1085 CPP_ICE ("unbalanced stack in #if expression");
1086 else if (!(top[1].flags & HAVE_VALUE))
1088 SYNTAX_ERROR ("#if with no expression");
1090 _cpp_skip_rest_of_line (pfile);
1091 result = 0; /* Return 0 on syntax error. */
1094 /* Free dynamic stack if we allocated one. */
1095 if (stack != init_stack)
1097 pfile->parsing_if_directive--;
1098 CPP_SET_WRITTEN (pfile, old_written);