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 */
28 #ifndef CHAR_TYPE_SIZE
29 #define CHAR_TYPE_SIZE BITS_PER_UNIT
33 #define INT_TYPE_SIZE BITS_PER_WORD
36 #ifndef LONG_TYPE_SIZE
37 #define LONG_TYPE_SIZE BITS_PER_WORD
40 #ifndef WCHAR_TYPE_SIZE
41 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
44 #ifndef MAX_CHAR_TYPE_SIZE
45 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
48 #ifndef MAX_INT_TYPE_SIZE
49 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
52 #ifndef MAX_LONG_TYPE_SIZE
53 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
56 #ifndef MAX_WCHAR_TYPE_SIZE
57 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
60 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
61 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
62 : ~ (HOST_WIDEST_INT) 0)
64 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
65 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
66 : ~ (HOST_WIDEST_INT) 0)
68 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
69 number with SUM's sign, where A, B, and SUM are all C integers. */
70 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
74 static void integer_overflow
PARAMS ((cpp_reader
*));
75 static HOST_WIDEST_INT left_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
77 unsigned HOST_WIDEST_INT
));
78 static HOST_WIDEST_INT right_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
80 unsigned HOST_WIDEST_INT
));
81 static struct operation parse_number
PARAMS ((cpp_reader
*, U_CHAR
*,
83 static struct operation parse_charconst
PARAMS ((cpp_reader
*, U_CHAR
*,
85 static struct operation parse_defined
PARAMS ((cpp_reader
*));
86 static HOST_WIDEST_INT parse_escape
PARAMS ((cpp_reader
*, U_CHAR
**,
88 static struct operation lex
PARAMS ((cpp_reader
*, int));
89 static const char * op_to_str
PARAMS ((op_t
, char *));
108 U_CHAR prio
; /* Priority of op. */
110 U_CHAR unsignedp
; /* True if value should be treated as unsigned. */
111 HOST_WIDEST_INT value
; /* The value logically "right" of op. */
114 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
115 with or without size suffixes. */
117 static struct operation
118 parse_number (pfile
, start
, end
)
126 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
129 int digit
, largest_digit
= 0;
136 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
148 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
149 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
150 / ((unsigned HOST_WIDEST_INT
) base
));
156 if (c
>= '0' && c
<= '9')
158 /* FIXME: assumes ASCII */
159 else if (base
== 16 && c
>= 'a' && c
<= 'f')
160 digit
= c
- 'a' + 10;
161 else if (base
== 16 && c
>= 'A' && c
<= 'F')
162 digit
= c
- 'A' + 10;
165 /* It's a float since it contains a point. */
167 "floating point numbers are not allowed in #if expressions");
172 /* `l' means long, and `u' means unsigned. */
175 if (c
== 'l' || c
== 'L')
177 else if (c
== 'u' || c
== 'U')
181 /* Decrement p here so that the error for an invalid
182 number will be generated below in the case where
183 this is the last character in the buffer. */
191 /* Don't look for any more digits after the suffixes. */
195 if (largest_digit
< digit
)
196 largest_digit
= digit
;
197 nd
= n
* base
+ digit
;
198 overflow
|= MAX_over_base
< n
|| nd
< n
;
204 cpp_error (pfile
, "invalid number in #if expression");
207 else if (spec_long
> (CPP_OPTION (pfile
, c89
) ? 1 : 2))
209 cpp_error (pfile
, "too many 'l' suffixes in integer constant");
212 else if (op
.unsignedp
> 1)
214 cpp_error (pfile
, "too many 'u' suffixes in integer constant");
218 if (base
<= largest_digit
)
220 "integer constant contains digits beyond the radix");
223 cpp_pedwarn (pfile
, "integer constant out of range");
225 /* If too big to be signed, consider it unsigned. */
226 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
230 "integer constant is so large that it is unsigned");
243 /* Parse and convert a character constant for #if. Understands backslash
244 escapes (\n, \031) and multibyte characters (if so configured). */
245 static struct operation
246 parse_charconst (pfile
, start
, end
)
252 HOST_WIDEST_INT result
= 0;
255 unsigned int width
= MAX_CHAR_TYPE_SIZE
, mask
= MAX_CHAR_TYPE_MASK
;
264 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
266 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
268 ++ptr
; /* skip initial quote */
277 c
= parse_escape (pfile
, &ptr
, mask
);
278 if (width
< HOST_BITS_PER_INT
279 && (unsigned int) c
>= (unsigned int)(1 << width
))
281 "escape sequence out of range for character");
284 /* Merge character into result; ignore excess chars. */
285 if (++num_chars
<= max_chars
)
287 if (width
< HOST_BITS_PER_INT
)
288 result
= (result
<< width
) | (c
& ((1 << width
) - 1));
296 cpp_error (pfile
, "empty character constant");
301 /* cpp_get_token has already emitted an error if !traditional. */
302 if (! CPP_TRADITIONAL (pfile
))
303 cpp_error (pfile
, "malformatted character constant");
306 else if (num_chars
> max_chars
)
308 cpp_error (pfile
, "character constant too long");
311 else if (num_chars
!= 1 && ! CPP_TRADITIONAL (pfile
))
312 cpp_warning (pfile
, "multi-character character constant");
314 /* If char type is signed, sign-extend the constant. */
315 num_bits
= num_chars
* width
;
317 if (cpp_defined (pfile
, (const U_CHAR
*)"__CHAR_UNSIGNED__",
318 sizeof ("__CHAR_UNSIGNED__")-1)
319 || ((result
>> (num_bits
- 1)) & 1) == 0)
320 op
.value
= result
& ((unsigned HOST_WIDEST_INT
) ~0
321 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
323 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
324 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
326 /* This is always a signed type. */
336 static struct operation
337 parse_defined (pfile
)
342 enum cpp_ttype token
;
344 long old_written
= CPP_WRITTEN (pfile
);
349 pfile
->no_macro_expand
++;
350 token
= _cpp_get_directive_token (pfile
);
351 if (token
== CPP_OPEN_PAREN
)
354 CPP_SET_WRITTEN (pfile
, old_written
);
355 token
= _cpp_get_directive_token (pfile
);
358 if (token
!= CPP_NAME
)
361 tok
= pfile
->token_buffer
+ old_written
;
362 len
= CPP_PWRITTEN (pfile
) - tok
;
363 op
.value
= cpp_defined (pfile
, tok
, len
);
367 if (_cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
370 CPP_SET_WRITTEN (pfile
, old_written
);
371 pfile
->no_macro_expand
--;
375 CPP_SET_WRITTEN (pfile
, old_written
);
376 pfile
->no_macro_expand
--;
377 cpp_error (pfile
, "'defined' without an identifier");
385 const char *operator;
389 static const struct token tokentab2
[] =
404 /* Read one token. */
406 static struct operation
407 lex (pfile
, skip_evaluation
)
411 const struct token
*toktab
;
412 enum cpp_ttype token
;
414 U_CHAR
*tok_start
, *tok_end
;
417 old_written
= CPP_WRITTEN (pfile
);
418 token
= _cpp_get_directive_token (pfile
);
420 tok_start
= pfile
->token_buffer
+ old_written
;
421 tok_end
= CPP_PWRITTEN (pfile
);
422 CPP_SET_WRITTEN (pfile
, old_written
);
425 case CPP_EOF
: /* Should not happen ... */
430 return parse_number (pfile
, tok_start
, tok_end
);
434 "string constants are not allowed in #if expressions");
440 return parse_charconst (pfile
, tok_start
, tok_end
);
443 if (!strncmp (tok_start
, "defined", 7))
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. BUFF is a buffer at least 5
483 characters long which might be used to store the string. */
484 /* XXX FIXME: Remove BUFF when new lexer is implemented. */
490 const struct token
*toktab
;
492 /* See if it is a special token of length 2. */
493 for (toktab
= tokentab2
; toktab
->operator != NULL
; toktab
++)
494 if (op
== toktab
->token
)
495 return toktab
->operator;
498 sprintf (buff
, "%c", (int) op
);
500 sprintf (buff
, "\\%03o", (int) op
);
504 /* Parse a C escape sequence. STRING_PTR points to a variable
505 containing a pointer to the string to parse. That pointer
506 is updated past the characters we use. The value of the
507 escape sequence is returned.
509 A negative value means the sequence \ newline was seen,
510 which is supposed to be equivalent to nothing at all.
512 If \ is followed by a null character, we return a negative
513 value and leave the string pointer pointing at the null character.
515 If \ is followed by 000, we return 0 and leave the string pointer
516 after the zeros. A value of 0 does not mean end of string. */
518 static HOST_WIDEST_INT
519 parse_escape (pfile
, string_ptr
, result_mask
)
522 HOST_WIDEST_INT result_mask
;
524 register int c
= *(*string_ptr
)++;
533 if (CPP_PEDANTIC (pfile
))
534 cpp_pedwarn (pfile
, "non-ANSI-standard escape sequence, '\\%c'", c
);
539 return TARGET_NEWLINE
;
561 register HOST_WIDEST_INT i
= c
- '0';
562 register int count
= 0;
565 c
= *(*string_ptr
)++;
566 if (c
>= '0' && c
<= '7')
567 i
= (i
<< 3) + c
- '0';
574 if (i
!= (i
& result_mask
))
577 cpp_pedwarn (pfile
, "octal escape sequence out of range");
583 register unsigned HOST_WIDEST_INT i
= 0, overflow
= 0;
584 register int digits_found
= 0, digit
;
587 c
= *(*string_ptr
)++;
588 if (c
>= '0' && c
<= '9')
590 else if (c
>= 'a' && c
<= 'f')
591 digit
= c
- 'a' + 10;
592 else if (c
>= 'A' && c
<= 'F')
593 digit
= c
- 'A' + 10;
599 overflow
|= i
^ (i
<< 4 >> 4);
600 i
= (i
<< 4) + digit
;
604 cpp_error (pfile
, "\\x used with no following hex digits");
605 if (overflow
| (i
!= (i
& result_mask
)))
608 cpp_pedwarn (pfile
, "hex escape sequence out of range");
618 integer_overflow (pfile
)
621 if (CPP_PEDANTIC (pfile
))
622 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
625 static HOST_WIDEST_INT
626 left_shift (pfile
, a
, unsignedp
, b
)
629 unsigned int unsignedp
;
630 unsigned HOST_WIDEST_INT b
;
632 if (b
>= HOST_BITS_PER_WIDEST_INT
)
634 if (! unsignedp
&& a
!= 0)
635 integer_overflow (pfile
);
639 return (unsigned HOST_WIDEST_INT
) a
<< b
;
642 HOST_WIDEST_INT l
= a
<< b
;
644 integer_overflow (pfile
);
649 static HOST_WIDEST_INT
650 right_shift (pfile
, a
, unsignedp
, b
)
651 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
653 unsigned int unsignedp
;
654 unsigned HOST_WIDEST_INT b
;
656 if (b
>= HOST_BITS_PER_WIDEST_INT
)
657 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
659 return (unsigned HOST_WIDEST_INT
) a
>> b
;
664 /* Operator precedence and flags table.
666 After an operator is returned from the lexer, if it has priority less
667 than or equal to the operator on the top of the stack, we reduce the
668 stack by one operator and repeat the test. Since equal priorities
669 reduce, this is naturally left-associative.
671 We handle right-associative operators by clearing the lower bit of all
672 left-associative operators, and setting it for right-associative ones.
673 After the reduction phase of a new operator, just before it is pushed
674 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
675 during the reduction phase, the current right-associative operator has
676 a priority one greater than any other operator of otherwise equal
677 precedence that has been pushed on the top of the stack. This avoids
678 a reduction pass, and effectively makes the logic right-associative.
680 The remaining cases are '(' and ')'. We handle '(' by skipping the
681 reduction phase completely. ')' is given lower priority than
682 everything else, including '(', effectively forcing a reduction of the
683 parenthesised expression. If there is no matching '(', the stack will
684 be reduced all the way to the beginning, exiting the parser in the
685 same way as the ultra-low priority end-of-expression dummy operator.
686 The exit code checks to see if the operator that caused it is ')', and
687 if so outputs an appropriate error message.
689 The parser assumes all shifted operators require a right operand
690 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
691 These semantics are automatically checked, any extra semantics need to
692 be handled with operator-specific code. */
695 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
696 #define PRIO_SHIFT (FLAG_BITS + 1)
697 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
698 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
701 #define HAVE_VALUE (1 << 0)
702 #define NO_L_OPERAND (1 << 1)
703 #define NO_R_OPERAND (1 << 2)
704 #define SHORT_CIRCUIT (1 << 3)
706 /* Priority and flag combinations. */
707 #define RIGHT_ASSOC (1 << FLAG_BITS)
708 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
709 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
710 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
711 #define COMMA_PRIO (3 << PRIO_SHIFT)
712 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
713 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
714 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
715 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
716 #define OR_PRIO (8 << PRIO_SHIFT)
717 #define XOR_PRIO (9 << PRIO_SHIFT)
718 #define AND_PRIO (10 << PRIO_SHIFT)
719 #define EQUAL_PRIO (11 << PRIO_SHIFT)
720 #define LESS_PRIO (12 << PRIO_SHIFT)
721 #define SHIFT_PRIO (13 << PRIO_SHIFT)
722 #define PLUS_PRIO (14 << PRIO_SHIFT)
723 #define MUL_PRIO (15 << PRIO_SHIFT)
724 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
726 #define COMPARE(OP) \
727 top->unsignedp = 0; \
728 top->value = (unsigned1 | unsigned2) \
729 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
731 #define EQUALITY(OP) \
732 top->value = v1 OP v2; \
734 #define LOGICAL(OP) \
735 top->value = v1 OP v2; \
736 top->unsignedp = unsigned1 | unsigned2;
738 /* With -O2, gcc appears to produce nice code, moving the error
739 message load and subsequent jump completely out of the main path. */
740 #define CPP_ICE(msgid) \
741 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
742 #define SYNTAX_ERROR(msgid) \
743 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
744 #define SYNTAX_ERROR2(msgid, arg) \
745 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
747 /* Parse and evaluate a C expression, reading from PFILE.
748 Returns the truth value of the expression. */
751 _cpp_parse_expr (pfile
)
754 /* The implementation is an operator precedence parser, i.e. a
755 bottom-up parser, using a stack for not-yet-reduced tokens.
757 The stack base is 'stack', and the current stack pointer is 'top'.
758 There is a stack element for each operator (only),
759 and the most recently pushed operator is 'top->op'.
760 An operand (value) is stored in the 'value' field of the stack
761 element of the operator that precedes it.
762 In that case the 'flags' field has the HAVE_VALUE flag set. */
764 #define INIT_STACK_SIZE 20
765 struct operation init_stack
[INIT_STACK_SIZE
];
766 struct operation
*stack
= init_stack
;
767 struct operation
*limit
= stack
+ INIT_STACK_SIZE
;
768 register struct operation
*top
= stack
+ 1;
769 long old_written
= CPP_WRITTEN (pfile
);
770 int skip_evaluation
= 0;
774 pfile
->parsing_if_directive
++;
775 /* We've finished when we try to reduce this. */
777 /* Nifty way to catch missing '('. */
778 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
779 /* Avoid missing right operand checks. */
780 top
->flags
= NO_R_OPERAND
;
789 op
= lex (pfile
, skip_evaluation
);
791 /* If the token is an operand, push its value and get next
792 token. If it is an operator, get its priority and flags, and
793 try to reduce the expression on the stack. */
797 CPP_ICE ("lex returns a NAME");
801 /* We get '#' when get_directive_token hits a syntactically
802 invalid assertion predicate. _cpp_parse_assertion has
803 already issued an error. */
806 SYNTAX_ERROR ("invalid character in #if");
811 /* Push a value onto the stack. */
812 if (top
->flags
& HAVE_VALUE
)
813 SYNTAX_ERROR ("missing binary operator");
814 top
->value
= op
.value
;
815 top
->unsignedp
= op
.unsignedp
;
816 top
->flags
|= HAVE_VALUE
;
820 case '-': prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
821 /* else unary; fall through */
823 case '~': prio
= UNARY_PRIO
; break;
827 case '%': prio
= MUL_PRIO
; break;
831 case GEQ
: prio
= LESS_PRIO
; break;
833 case EQUAL
: prio
= EQUAL_PRIO
; break;
835 case RSH
: prio
= SHIFT_PRIO
; break;
836 case '&': prio
= AND_PRIO
; break;
837 case '^': prio
= XOR_PRIO
; break;
838 case '|': prio
= OR_PRIO
; break;
839 case ANDAND
: prio
= ANDAND_PRIO
; break;
840 case OROR
: prio
= OROR_PRIO
; break;
841 case ',': prio
= COMMA_PRIO
; break;
842 case '(': prio
= OPEN_PAREN_PRIO
; break;
843 case ')': prio
= CLOSE_PAREN_PRIO
; break;
844 case ':': prio
= COLON_PRIO
; break;
845 case '?': prio
= COND_PRIO
; break;
846 case 0: prio
= FORCE_REDUCE_PRIO
; break;
849 /* Separate the operator's code into priority and flags. */
850 flags
= EXTRACT_FLAGS(prio
);
851 prio
= EXTRACT_PRIO(prio
);
855 /* Check for reductions. Then push the operator. */
856 while (prio
<= top
->prio
)
858 HOST_WIDEST_INT v1
, v2
;
859 unsigned int unsigned1
, unsigned2
;
861 /* Most operators that can appear on the stack require a
862 right operand. Check this before trying to reduce. */
863 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
866 SYNTAX_ERROR ("void expression between '(' and ')'");
868 SYNTAX_ERROR2 ("operator '%s' has no right operand",
869 op_to_str (top
->op
, buff
));
872 unsigned2
= top
->unsignedp
, v2
= top
->value
;
874 unsigned1
= top
->unsignedp
, v1
= top
->value
;
876 /* Now set top->value = (top[1].op)(v1, v2); */
880 if (!(top
->flags
& HAVE_VALUE
))
883 top
->unsignedp
= unsigned2
;
884 top
->flags
|= HAVE_VALUE
;
888 top
->value
= v1
+ v2
;
889 top
->unsignedp
= unsigned1
| unsigned2
;
890 if (! top
->unsignedp
&& ! skip_evaluation
891 && ! possible_sum_sign (v1
, v2
, top
->value
))
892 integer_overflow (pfile
);
896 if (!(top
->flags
& HAVE_VALUE
))
899 if (!skip_evaluation
&& (top
->value
& v2
) < 0
901 integer_overflow (pfile
);
902 top
->unsignedp
= unsigned2
;
903 top
->flags
|= HAVE_VALUE
;
907 top
->value
= v1
- v2
;
908 top
->unsignedp
= unsigned1
| unsigned2
;
909 if (! top
->unsignedp
&& ! skip_evaluation
910 && ! possible_sum_sign (top
->value
, v2
, v1
))
911 integer_overflow (pfile
);
915 top
->unsignedp
= unsigned1
| unsigned2
;
917 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
918 else if (!skip_evaluation
)
920 top
->value
= v1
* v2
;
921 if (v1
&& (top
->value
/ v1
!= v2
922 || (top
->value
& v1
& v2
) < 0))
923 integer_overflow (pfile
);
931 SYNTAX_ERROR ("division by zero in #if");
932 top
->unsignedp
= unsigned1
| unsigned2
;
933 if (top
[1].op
== '/')
936 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
939 top
->value
= v1
/ v2
;
940 if ((top
->value
& v1
& v2
) < 0)
941 integer_overflow (pfile
);
947 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
949 top
->value
= v1
% v2
;
955 top
->flags
|= HAVE_VALUE
;
959 top
->unsignedp
= unsigned2
;
960 top
->flags
|= HAVE_VALUE
;
962 case '<': COMPARE(<); break;
963 case '>': COMPARE(>); break;
964 case LEQ
: COMPARE(<=); break;
965 case GEQ
: COMPARE(>=); break;
966 case EQUAL
: EQUALITY(==); break;
967 case NOTEQUAL
: EQUALITY(!=); break;
971 top
->unsignedp
= unsigned1
;
972 if (v2
< 0 && ! unsigned2
)
973 top
->value
= right_shift (pfile
, v1
, unsigned1
, -v2
);
975 top
->value
= left_shift (pfile
, v1
, unsigned1
, v2
);
980 top
->unsignedp
= unsigned1
;
981 if (v2
< 0 && ! unsigned2
)
982 top
->value
= left_shift (pfile
, v1
, unsigned1
, -v2
);
984 top
->value
= right_shift (pfile
, v1
, unsigned1
, v2
);
986 case '&': LOGICAL(&); break;
987 case '^': LOGICAL(^); break;
988 case '|': LOGICAL(|); break;
990 top
->value
= v1
&& v2
; top
->unsignedp
= 0;
991 if (!v1
) skip_evaluation
--;
994 top
->value
= v1
|| v2
; top
->unsignedp
= 0;
995 if (v1
) skip_evaluation
--;
998 if (CPP_PEDANTIC (pfile
))
999 cpp_pedwarn (pfile
, "comma operator in operand of #if");
1001 top
->unsignedp
= unsigned2
;
1004 SYNTAX_ERROR ("syntax error '?' without following ':'");
1006 if (top
[0].op
!= '?')
1007 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
1009 if (top
->value
) skip_evaluation
--;
1010 top
->value
= top
->value
? v1
: v2
;
1011 top
->unsignedp
= unsigned1
| unsigned2
;
1015 SYNTAX_ERROR ("missing ')' in expression");
1017 op
.unsignedp
= unsigned2
;
1018 goto push_immediate
;
1020 SYNTAX_ERROR2 ("unimplemented operator '%s'",
1021 op_to_str (top
[1].op
, buff
));
1023 /* Reducing this dummy operator indicates we've finished. */
1025 SYNTAX_ERROR ("missing '(' in expression");
1030 /* Handle short-circuit evaluations. */
1031 if (flags
& SHORT_CIRCUIT
)
1034 case OROR
: if (top
->value
) skip_evaluation
++; break;
1036 case '?': if (!top
->value
) skip_evaluation
++; break;
1038 if (top
[-1].value
) /* Was '?' condition true? */
1045 /* Check we have a left operand iff we need one. */
1046 if (flags
& NO_L_OPERAND
)
1048 if (top
->flags
& HAVE_VALUE
)
1049 SYNTAX_ERROR2 ("missing binary operator before '%s'",
1050 op_to_str (op
.op
, buff
));
1054 if (!(top
->flags
& HAVE_VALUE
))
1055 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1056 op_to_str (op
.op
, buff
));
1059 /* Check for and handle stack overflow. */
1063 struct operation
*new_stack
;
1064 int old_size
= (char *) limit
- (char *) stack
;
1065 int new_size
= 2 * old_size
;
1066 if (stack
!= init_stack
)
1067 new_stack
= (struct operation
*) xrealloc (stack
, new_size
);
1070 new_stack
= (struct operation
*) xmalloc (new_size
);
1071 memcpy (new_stack
, stack
, old_size
);
1074 top
= (struct operation
*) ((char *) new_stack
+ old_size
);
1075 limit
= (struct operation
*) ((char *) new_stack
+ new_size
);
1079 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
1084 result
= (top
[1].value
!= 0);
1086 CPP_ICE ("unbalanced stack in #if expression");
1087 else if (!(top
[1].flags
& HAVE_VALUE
))
1089 SYNTAX_ERROR ("#if with no expression");
1091 _cpp_skip_rest_of_line (pfile
);
1092 result
= 0; /* Return 0 on syntax error. */
1095 /* Free dynamic stack if we allocated one. */
1096 if (stack
!= init_stack
)
1098 pfile
->parsing_if_directive
--;
1099 CPP_SET_WRITTEN (pfile
, old_written
);