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, char *));
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 /* FIXME: assumes ASCII */
163 else if (base == 16 && c >= 'a' && c <= 'f')
164 digit = c - 'a' + 10;
165 else if (base == 16 && c >= 'A' && c <= 'F')
166 digit = c - 'A' + 10;
169 /* It's a float since it contains a point. */
171 "floating point numbers are not allowed in #if expressions");
176 /* `l' means long, and `u' means unsigned. */
179 if (c == 'l' || c == 'L')
181 else if (c == 'u' || c == 'U')
185 /* Decrement p here so that the error for an invalid
186 number will be generated below in the case where
187 this is the last character in the buffer. */
195 /* Don't look for any more digits after the suffixes. */
199 if (largest_digit < digit)
200 largest_digit = digit;
201 nd = n * base + digit;
202 overflow |= MAX_over_base < n || nd < n;
208 cpp_error (pfile, "invalid number in #if expression");
211 else if (spec_long > (CPP_OPTION (pfile, c89) ? 1 : 2))
213 cpp_error (pfile, "too many 'l' suffixes in integer constant");
216 else if (op.unsignedp > 1)
218 cpp_error (pfile, "too many 'u' suffixes in integer constant");
222 if (base <= largest_digit)
224 "integer constant contains digits beyond the radix");
227 cpp_pedwarn (pfile, "integer constant out of range");
229 /* If too big to be signed, consider it unsigned. */
230 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
234 "integer constant is so large that it is unsigned");
247 /* Parse and convert a character constant for #if. Understands backslash
248 escapes (\n, \031) and multibyte characters (if so configured). */
249 static struct operation
250 parse_charconst (pfile, start, end)
256 HOST_WIDEST_INT result = 0;
259 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
268 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
270 max_chars = MAX_LONG_TYPE_SIZE / width;
272 ++ptr; /* skip initial quote */
281 c = parse_escape (pfile, &ptr, mask);
282 if (width < HOST_BITS_PER_INT
283 && (unsigned int) c >= (unsigned int)(1 << width))
285 "escape sequence out of range for character");
288 /* Merge character into result; ignore excess chars. */
289 if (++num_chars <= max_chars)
291 if (width < HOST_BITS_PER_INT)
292 result = (result << width) | (c & ((1 << width) - 1));
300 cpp_error (pfile, "empty character constant");
305 /* cpp_get_token has already emitted an error if !traditional. */
306 if (! CPP_TRADITIONAL (pfile))
307 cpp_error (pfile, "malformatted character constant");
310 else if (num_chars > max_chars)
312 cpp_error (pfile, "character constant too long");
315 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
316 cpp_warning (pfile, "multi-character character constant");
318 /* If char type is signed, sign-extend the constant. */
319 num_bits = num_chars * width;
321 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
322 sizeof ("__CHAR_UNSIGNED__")-1)
323 || ((result >> (num_bits - 1)) & 1) == 0)
324 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
325 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
327 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
328 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
330 /* This is always a signed type. */
340 static struct operation
341 parse_defined (pfile)
346 enum cpp_ttype token;
348 long old_written = CPP_WRITTEN (pfile);
353 pfile->no_macro_expand++;
354 token = _cpp_get_directive_token (pfile);
355 if (token == CPP_LPAREN)
358 CPP_SET_WRITTEN (pfile, old_written);
359 token = _cpp_get_directive_token (pfile);
362 if (token != CPP_NAME)
365 tok = pfile->token_buffer + old_written;
366 len = CPP_PWRITTEN (pfile) - tok;
367 op.value = cpp_defined (pfile, tok, len);
371 if (_cpp_get_directive_token (pfile) != CPP_RPAREN)
374 CPP_SET_WRITTEN (pfile, old_written);
375 pfile->no_macro_expand--;
379 CPP_SET_WRITTEN (pfile, old_written);
380 pfile->no_macro_expand--;
381 cpp_error (pfile, "'defined' without an identifier");
389 const char *operator;
393 static const struct token tokentab2[] =
408 /* Read one token. */
410 static struct operation
411 lex (pfile, skip_evaluation)
415 const struct token *toktab;
416 enum cpp_ttype token;
418 U_CHAR *tok_start, *tok_end;
421 old_written = CPP_WRITTEN (pfile);
422 token = _cpp_get_directive_token (pfile);
424 tok_start = pfile->token_buffer + old_written;
425 tok_end = CPP_PWRITTEN (pfile);
426 CPP_SET_WRITTEN (pfile, old_written);
429 case CPP_EOF: /* Should not happen ... */
434 return parse_number (pfile, tok_start, tok_end);
438 "string constants are not allowed in #if expressions");
444 return parse_charconst (pfile, tok_start, tok_end);
447 if (!strcmp (tok_start, "defined"))
448 return parse_defined (pfile);
454 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
455 cpp_warning (pfile, "'%.*s' is not defined",
456 (int) (tok_end - tok_start), tok_start);
462 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
466 /* See if it is a special token of length 2. */
467 if (tok_start + 2 == tok_end)
469 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
470 if (tok_start[0] == toktab->operator[0]
471 && tok_start[1] == toktab->operator[1])
473 if (toktab->token == ERROR)
474 cpp_error (pfile, "'%s' not allowed in operand of #if",
476 op.op = toktab->token;
486 /* Convert an operator ID to a string. BUFF is a buffer at least 5
487 characters long which might be used to store the string. */
488 /* XXX FIXME: Remove BUFF when new lexer is implemented. */
494 const struct token *toktab;
496 /* See if it is a special token of length 2. */
497 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
498 if (op == toktab->token)
499 return toktab->operator;
502 sprintf (buff, "%c", (int) op);
504 sprintf (buff, "\\%03o", (int) op);
508 /* Parse a C escape sequence. STRING_PTR points to a variable
509 containing a pointer to the string to parse. That pointer
510 is updated past the characters we use. The value of the
511 escape sequence is returned.
513 A negative value means the sequence \ newline was seen,
514 which is supposed to be equivalent to nothing at all.
516 If \ is followed by a null character, we return a negative
517 value and leave the string pointer pointing at the null character.
519 If \ is followed by 000, we return 0 and leave the string pointer
520 after the zeros. A value of 0 does not mean end of string. */
522 static HOST_WIDEST_INT
523 parse_escape (pfile, string_ptr, result_mask)
526 HOST_WIDEST_INT result_mask;
528 register int c = *(*string_ptr)++;
537 if (CPP_PEDANTIC (pfile))
538 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, '\\%c'", c);
543 return TARGET_NEWLINE;
565 register HOST_WIDEST_INT i = c - '0';
566 register int count = 0;
569 c = *(*string_ptr)++;
570 if (c >= '0' && c <= '7')
571 i = (i << 3) + c - '0';
578 if (i != (i & result_mask))
581 cpp_pedwarn (pfile, "octal escape sequence out of range");
587 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
588 register int digits_found = 0, digit;
591 c = *(*string_ptr)++;
592 if (c >= '0' && c <= '9')
594 else if (c >= 'a' && c <= 'f')
595 digit = c - 'a' + 10;
596 else if (c >= 'A' && c <= 'F')
597 digit = c - 'A' + 10;
603 overflow |= i ^ (i << 4 >> 4);
604 i = (i << 4) + digit;
608 cpp_error (pfile, "\\x used with no following hex digits");
609 if (overflow | (i != (i & result_mask)))
612 cpp_pedwarn (pfile, "hex escape sequence out of range");
622 integer_overflow (pfile)
625 if (CPP_PEDANTIC (pfile))
626 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
629 static HOST_WIDEST_INT
630 left_shift (pfile, a, unsignedp, b)
633 unsigned int unsignedp;
634 unsigned HOST_WIDEST_INT b;
636 if (b >= HOST_BITS_PER_WIDEST_INT)
638 if (! unsignedp && a != 0)
639 integer_overflow (pfile);
643 return (unsigned HOST_WIDEST_INT) a << b;
646 HOST_WIDEST_INT l = a << b;
648 integer_overflow (pfile);
653 static HOST_WIDEST_INT
654 right_shift (pfile, a, unsignedp, b)
655 cpp_reader *pfile ATTRIBUTE_UNUSED;
657 unsigned int unsignedp;
658 unsigned HOST_WIDEST_INT b;
660 if (b >= HOST_BITS_PER_WIDEST_INT)
661 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
663 return (unsigned HOST_WIDEST_INT) a >> b;
668 /* Operator precedence and flags table.
670 After an operator is returned from the lexer, if it has priority less
671 than or equal to the operator on the top of the stack, we reduce the
672 stack by one operator and repeat the test. Since equal priorities
673 reduce, this is naturally left-associative.
675 We handle right-associative operators by clearing the lower bit of all
676 left-associative operators, and setting it for right-associative ones.
677 After the reduction phase of a new operator, just before it is pushed
678 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
679 during the reduction phase, the current right-associative operator has
680 a priority one greater than any other operator of otherwise equal
681 precedence that has been pushed on the top of the stack. This avoids
682 a reduction pass, and effectively makes the logic right-associative.
684 The remaining cases are '(' and ')'. We handle '(' by skipping the
685 reduction phase completely. ')' is given lower priority than
686 everything else, including '(', effectively forcing a reduction of the
687 parenthesised expression. If there is no matching '(', the stack will
688 be reduced all the way to the beginning, exiting the parser in the
689 same way as the ultra-low priority end-of-expression dummy operator.
690 The exit code checks to see if the operator that caused it is ')', and
691 if so outputs an appropriate error message.
693 The parser assumes all shifted operators require a right operand
694 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
695 These semantics are automatically checked, any extra semantics need to
696 be handled with operator-specific code. */
699 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
700 #define PRIO_SHIFT (FLAG_BITS + 1)
701 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
702 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
705 #define HAVE_VALUE (1 << 0)
706 #define NO_L_OPERAND (1 << 1)
707 #define NO_R_OPERAND (1 << 2)
708 #define SHORT_CIRCUIT (1 << 3)
710 /* Priority and flag combinations. */
711 #define RIGHT_ASSOC (1 << FLAG_BITS)
712 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
713 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
714 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
715 #define COMMA_PRIO (3 << PRIO_SHIFT)
716 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
717 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
718 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
719 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
720 #define OR_PRIO (8 << PRIO_SHIFT)
721 #define XOR_PRIO (9 << PRIO_SHIFT)
722 #define AND_PRIO (10 << PRIO_SHIFT)
723 #define EQUAL_PRIO (11 << PRIO_SHIFT)
724 #define LESS_PRIO (12 << PRIO_SHIFT)
725 #define SHIFT_PRIO (13 << PRIO_SHIFT)
726 #define PLUS_PRIO (14 << PRIO_SHIFT)
727 #define MUL_PRIO (15 << PRIO_SHIFT)
728 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
730 #define COMPARE(OP) \
731 top->unsignedp = 0; \
732 top->value = (unsigned1 | unsigned2) \
733 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
735 #define EQUALITY(OP) \
736 top->value = v1 OP v2; \
738 #define LOGICAL(OP) \
739 top->value = v1 OP v2; \
740 top->unsignedp = unsigned1 | unsigned2;
742 /* With -O2, gcc appears to produce nice code, moving the error
743 message load and subsequent jump completely out of the main path. */
744 #define CPP_ICE(msgid) \
745 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
746 #define SYNTAX_ERROR(msgid) \
747 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
748 #define SYNTAX_ERROR2(msgid, arg) \
749 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
751 /* Parse and evaluate a C expression, reading from PFILE.
752 Returns the truth value of the expression. */
755 _cpp_parse_expr (pfile)
758 /* The implementation is an operator precedence parser, i.e. a
759 bottom-up parser, using a stack for not-yet-reduced tokens.
761 The stack base is 'stack', and the current stack pointer is 'top'.
762 There is a stack element for each operator (only),
763 and the most recently pushed operator is 'top->op'.
764 An operand (value) is stored in the 'value' field of the stack
765 element of the operator that precedes it.
766 In that case the 'flags' field has the HAVE_VALUE flag set. */
768 #define INIT_STACK_SIZE 20
769 struct operation init_stack[INIT_STACK_SIZE];
770 struct operation *stack = init_stack;
771 struct operation *limit = stack + INIT_STACK_SIZE;
772 register struct operation *top = stack + 1;
773 long old_written = CPP_WRITTEN (pfile);
774 int skip_evaluation = 0;
778 pfile->parsing_if_directive++;
779 /* We've finished when we try to reduce this. */
781 /* Nifty way to catch missing '('. */
782 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
783 /* Avoid missing right operand checks. */
784 top->flags = NO_R_OPERAND;
793 op = lex (pfile, skip_evaluation);
795 /* If the token is an operand, push its value and get next
796 token. If it is an operator, get its priority and flags, and
797 try to reduce the expression on the stack. */
801 CPP_ICE ("lex returns a NAME");
805 /* We get '#' when get_directive_token hits a syntactically
806 invalid assertion predicate. _cpp_parse_assertion has
807 already issued an error. */
810 SYNTAX_ERROR ("invalid character in #if");
815 /* Push a value onto the stack. */
816 if (top->flags & HAVE_VALUE)
817 SYNTAX_ERROR ("missing binary operator");
818 top->value = op.value;
819 top->unsignedp = op.unsignedp;
820 top->flags |= HAVE_VALUE;
824 case '-': prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
825 /* else unary; fall through */
827 case '~': prio = UNARY_PRIO; break;
831 case '%': prio = MUL_PRIO; break;
835 case GEQ: prio = LESS_PRIO; break;
837 case EQUAL: prio = EQUAL_PRIO; break;
839 case RSH: prio = SHIFT_PRIO; break;
840 case '&': prio = AND_PRIO; break;
841 case '^': prio = XOR_PRIO; break;
842 case '|': prio = OR_PRIO; break;
843 case ANDAND: prio = ANDAND_PRIO; break;
844 case OROR: prio = OROR_PRIO; break;
845 case ',': prio = COMMA_PRIO; break;
846 case '(': prio = OPEN_PAREN_PRIO; break;
847 case ')': prio = CLOSE_PAREN_PRIO; break;
848 case ':': prio = COLON_PRIO; break;
849 case '?': prio = COND_PRIO; break;
850 case 0: prio = FORCE_REDUCE_PRIO; break;
853 /* Separate the operator's code into priority and flags. */
854 flags = EXTRACT_FLAGS(prio);
855 prio = EXTRACT_PRIO(prio);
859 /* Check for reductions. Then push the operator. */
860 while (prio <= top->prio)
862 HOST_WIDEST_INT v1, v2;
863 unsigned int unsigned1, unsigned2;
865 /* Most operators that can appear on the stack require a
866 right operand. Check this before trying to reduce. */
867 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
870 SYNTAX_ERROR ("void expression between '(' and ')'");
872 SYNTAX_ERROR2 ("operator '%s' has no right operand",
873 op_to_str (top->op, buff));
876 unsigned2 = top->unsignedp, v2 = top->value;
878 unsigned1 = top->unsignedp, v1 = top->value;
880 /* Now set top->value = (top[1].op)(v1, v2); */
884 if (!(top->flags & HAVE_VALUE))
887 top->unsignedp = unsigned2;
888 top->flags |= HAVE_VALUE;
892 top->value = v1 + v2;
893 top->unsignedp = unsigned1 | unsigned2;
894 if (! top->unsignedp && ! skip_evaluation
895 && ! possible_sum_sign (v1, v2, top->value))
896 integer_overflow (pfile);
900 if (!(top->flags & HAVE_VALUE))
903 if (!skip_evaluation && (top->value & v2) < 0
905 integer_overflow (pfile);
906 top->unsignedp = unsigned2;
907 top->flags |= HAVE_VALUE;
911 top->value = v1 - v2;
912 top->unsignedp = unsigned1 | unsigned2;
913 if (! top->unsignedp && ! skip_evaluation
914 && ! possible_sum_sign (top->value, v2, v1))
915 integer_overflow (pfile);
919 top->unsignedp = unsigned1 | unsigned2;
921 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
922 else if (!skip_evaluation)
924 top->value = v1 * v2;
925 if (v1 && (top->value / v1 != v2
926 || (top->value & v1 & v2) < 0))
927 integer_overflow (pfile);
935 SYNTAX_ERROR ("division by zero in #if");
936 top->unsignedp = unsigned1 | unsigned2;
937 if (top[1].op == '/')
940 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
943 top->value = v1 / v2;
944 if ((top->value & v1 & v2) < 0)
945 integer_overflow (pfile);
951 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
953 top->value = v1 % v2;
959 top->flags |= HAVE_VALUE;
963 top->unsignedp = unsigned2;
964 top->flags |= HAVE_VALUE;
966 case '<': COMPARE(<); break;
967 case '>': COMPARE(>); break;
968 case LEQ: COMPARE(<=); break;
969 case GEQ: COMPARE(>=); break;
970 case EQUAL: EQUALITY(==); break;
971 case NOTEQUAL: EQUALITY(!=); break;
975 top->unsignedp = unsigned1;
976 if (v2 < 0 && ! unsigned2)
977 top->value = right_shift (pfile, v1, unsigned1, -v2);
979 top->value = left_shift (pfile, v1, unsigned1, v2);
984 top->unsignedp = unsigned1;
985 if (v2 < 0 && ! unsigned2)
986 top->value = left_shift (pfile, v1, unsigned1, -v2);
988 top->value = right_shift (pfile, v1, unsigned1, v2);
990 case '&': LOGICAL(&); break;
991 case '^': LOGICAL(^); break;
992 case '|': LOGICAL(|); break;
994 top->value = v1 && v2; top->unsignedp = 0;
995 if (!v1) skip_evaluation--;
998 top->value = v1 || v2; top->unsignedp = 0;
999 if (v1) skip_evaluation--;
1002 if (CPP_PEDANTIC (pfile))
1003 cpp_pedwarn (pfile, "comma operator in operand of #if");
1005 top->unsignedp = unsigned2;
1008 SYNTAX_ERROR ("syntax error '?' without following ':'");
1010 if (top[0].op != '?')
1011 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
1013 if (top->value) skip_evaluation--;
1014 top->value = top->value ? v1 : v2;
1015 top->unsignedp = unsigned1 | unsigned2;
1019 SYNTAX_ERROR ("missing ')' in expression");
1021 op.unsignedp = unsigned2;
1022 goto push_immediate;
1024 SYNTAX_ERROR2 ("unimplemented operator '%s'",
1025 op_to_str (top[1].op, buff));
1027 /* Reducing this dummy operator indicates we've finished. */
1029 SYNTAX_ERROR ("missing '(' in expression");
1034 /* Handle short-circuit evaluations. */
1035 if (flags & SHORT_CIRCUIT)
1038 case OROR: if (top->value) skip_evaluation++; break;
1040 case '?': if (!top->value) skip_evaluation++; break;
1042 if (top[-1].value) /* Was '?' condition true? */
1049 /* Check we have a left operand iff we need one. */
1050 if (flags & NO_L_OPERAND)
1052 if (top->flags & HAVE_VALUE)
1053 SYNTAX_ERROR2 ("missing binary operator before '%s'",
1054 op_to_str (op.op, buff));
1058 if (!(top->flags & HAVE_VALUE))
1059 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1060 op_to_str (op.op, buff));
1063 /* Check for and handle stack overflow. */
1067 struct operation *new_stack;
1068 int old_size = (char *) limit - (char *) stack;
1069 int new_size = 2 * old_size;
1070 if (stack != init_stack)
1071 new_stack = (struct operation *) xrealloc (stack, new_size);
1074 new_stack = (struct operation *) xmalloc (new_size);
1075 memcpy (new_stack, stack, old_size);
1078 top = (struct operation *) ((char *) new_stack + old_size);
1079 limit = (struct operation *) ((char *) new_stack + new_size);
1083 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1088 result = (top[1].value != 0);
1090 CPP_ICE ("unbalanced stack in #if expression");
1091 else if (!(top[1].flags & HAVE_VALUE))
1093 SYNTAX_ERROR ("#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);