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 struct operation parse_assertion
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 /* With -O2, gcc appears to produce nice code, moving the error
115 message load and subsequent jump completely out of the main path. */
116 #define CPP_ICE(msgid) \
117 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
118 #define SYNTAX_ERROR(msgid) \
119 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
120 #define SYNTAX_ERROR2(msgid, arg) \
121 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
123 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
124 with or without size suffixes. */
126 static struct operation
127 parse_number (pfile
, start
, end
)
135 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
138 int digit
, largest_digit
= 0;
145 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
157 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
158 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
159 / ((unsigned HOST_WIDEST_INT
) base
));
165 if (c
>= '0' && c
<= '9')
167 /* FIXME: assumes ASCII */
168 else if (base
== 16 && c
>= 'a' && c
<= 'f')
169 digit
= c
- 'a' + 10;
170 else if (base
== 16 && c
>= 'A' && c
<= 'F')
171 digit
= c
- 'A' + 10;
174 /* It's a float since it contains a point. */
176 "floating point numbers are not allowed in #if expressions");
181 /* `l' means long, and `u' means unsigned. */
184 if (c
== 'l' || c
== 'L')
186 else if (c
== 'u' || c
== 'U')
190 /* Decrement p here so that the error for an invalid
191 number will be generated below in the case where
192 this is the last character in the buffer. */
200 /* Don't look for any more digits after the suffixes. */
204 if (largest_digit
< digit
)
205 largest_digit
= digit
;
206 nd
= n
* base
+ digit
;
207 overflow
|= MAX_over_base
< n
|| nd
< n
;
213 cpp_error (pfile
, "invalid number in #if expression");
216 else if (spec_long
> (CPP_OPTION (pfile
, c89
) ? 1 : 2))
218 cpp_error (pfile
, "too many 'l' suffixes in integer constant");
221 else if (op
.unsignedp
> 1)
223 cpp_error (pfile
, "too many 'u' suffixes in integer constant");
227 if (base
<= largest_digit
)
229 "integer constant contains digits beyond the radix");
232 cpp_pedwarn (pfile
, "integer constant out of range");
234 /* If too big to be signed, consider it unsigned. */
235 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
239 "integer constant is so large that it is unsigned");
252 /* Parse and convert a character constant for #if. Understands backslash
253 escapes (\n, \031) and multibyte characters (if so configured). */
254 static struct operation
255 parse_charconst (pfile
, start
, end
)
261 HOST_WIDEST_INT result
= 0;
264 unsigned int width
= MAX_CHAR_TYPE_SIZE
, mask
= MAX_CHAR_TYPE_MASK
;
273 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
275 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
277 ++ptr
; /* skip initial quote */
286 c
= parse_escape (pfile
, &ptr
, mask
);
287 if (width
< HOST_BITS_PER_INT
288 && (unsigned int) c
>= (unsigned int)(1 << width
))
290 "escape sequence out of range for character");
293 /* Merge character into result; ignore excess chars. */
294 if (++num_chars
<= max_chars
)
296 if (width
< HOST_BITS_PER_INT
)
297 result
= (result
<< width
) | (c
& ((1 << width
) - 1));
305 cpp_error (pfile
, "empty character constant");
310 /* cpp_get_token has already emitted an error if !traditional. */
311 if (! CPP_TRADITIONAL (pfile
))
312 cpp_error (pfile
, "malformatted character constant");
315 else if (num_chars
> max_chars
)
317 cpp_error (pfile
, "character constant too long");
320 else if (num_chars
!= 1 && ! CPP_TRADITIONAL (pfile
))
321 cpp_warning (pfile
, "multi-character character constant");
323 /* If char type is signed, sign-extend the constant. */
324 num_bits
= num_chars
* width
;
326 if (cpp_defined (pfile
, U
"__CHAR_UNSIGNED__",
327 sizeof ("__CHAR_UNSIGNED__")-1)
328 || ((result
>> (num_bits
- 1)) & 1) == 0)
329 op
.value
= result
& ((unsigned HOST_WIDEST_INT
) ~0
330 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
332 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
333 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
335 /* This is always a signed type. */
345 static struct operation
346 parse_defined (pfile
)
351 enum cpp_ttype token
;
353 long old_written
= CPP_WRITTEN (pfile
);
358 pfile
->no_macro_expand
++;
359 token
= _cpp_get_directive_token (pfile
);
360 if (token
== CPP_OPEN_PAREN
)
363 CPP_SET_WRITTEN (pfile
, old_written
);
364 token
= _cpp_get_directive_token (pfile
);
367 if (token
!= CPP_NAME
)
370 tok
= pfile
->token_buffer
+ old_written
;
371 len
= CPP_PWRITTEN (pfile
) - tok
;
372 op
.value
= cpp_defined (pfile
, tok
, len
);
376 if (_cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
379 CPP_SET_WRITTEN (pfile
, old_written
);
380 pfile
->no_macro_expand
--;
384 CPP_SET_WRITTEN (pfile
, old_written
);
385 pfile
->no_macro_expand
--;
386 cpp_error (pfile
, "'defined' without an identifier");
392 static struct operation
393 parse_assertion (pfile
)
398 struct predicate
*pred
;
403 unsigned int old_written
;
406 old_written
= CPP_WRITTEN (pfile
);
407 CPP_PUTC (pfile
, '#');
408 pfile
->no_macro_expand
++;
409 type
= _cpp_get_directive_token (pfile
);
410 if (type
== CPP_VSPACE
)
411 SYNTAX_ERROR ("assertion without predicate");
412 else if (type
!= CPP_NAME
)
413 SYNTAX_ERROR ("assertion predicate is not an identifier");
415 tok
= pfile
->token_buffer
+ old_written
;
416 len
= CPP_WRITTEN (pfile
) - old_written
;
417 hp
= cpp_lookup (pfile
, tok
, len
);
419 /* Look ahead for an open paren. */
420 _cpp_skip_hspace (pfile
);
421 if (CPP_BUF_PEEK (CPP_BUFFER (pfile
)) == '(')
423 if (_cpp_get_directive_token (pfile
) != CPP_OPEN_PAREN
)
424 CPP_ICE ("impossible token, expecting ( in parse_assertion");
426 _cpp_init_toklist (&query
, NO_DUMMY_TOKEN
);
428 if (_cpp_scan_until (pfile
, &query
, CPP_CLOSE_PAREN
) != CPP_CLOSE_PAREN
)
429 SYNTAX_ERROR ("missing close paren on assertion answer");
431 if (_cpp_get_directive_token (pfile
) != CPP_CLOSE_PAREN
)
432 CPP_ICE ("impossible token, expecting ) in parse_assertion");
435 /* If we get here, the syntax is valid. */
438 /* Has this predicate been asserted at all? */
439 if (hp
->type
== T_ASSERTION
)
443 for (pred
= hp
->value
.pred
; pred
; pred
= pred
->next
)
444 if (_cpp_equiv_toklists (&query
, &pred
->answer
))
449 _cpp_free_toklist (&query
);
456 pfile
->no_macro_expand
--;
457 CPP_SET_WRITTEN (pfile
, old_written
);
462 _cpp_free_toklist (&query
);
469 const char *operator;
473 static const struct token tokentab2
[] =
488 /* Read one token. */
490 static struct operation
491 lex (pfile
, skip_evaluation
)
495 const struct token
*toktab
;
496 enum cpp_ttype token
;
498 U_CHAR
*tok_start
, *tok_end
;
501 old_written
= CPP_WRITTEN (pfile
);
502 token
= _cpp_get_directive_token (pfile
);
504 tok_start
= pfile
->token_buffer
+ old_written
;
505 tok_end
= CPP_PWRITTEN (pfile
);
506 CPP_SET_WRITTEN (pfile
, old_written
);
509 case CPP_EOF
: /* Should not happen ... */
514 return parse_number (pfile
, tok_start
, tok_end
);
518 "string constants are not allowed in #if expressions");
524 return parse_charconst (pfile
, tok_start
, tok_end
);
527 if (!ustrncmp (tok_start
, U
"defined", 7))
528 return parse_defined (pfile
);
534 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
535 cpp_warning (pfile
, "'%.*s' is not defined",
536 (int) (tok_end
- tok_start
), tok_start
);
540 return parse_assertion (pfile
);
543 /* See if it is a special token of length 2. */
544 if (tok_start
+ 2 == tok_end
)
546 for (toktab
= tokentab2
; toktab
->operator != NULL
; toktab
++)
547 if (tok_start
[0] == toktab
->operator[0]
548 && tok_start
[1] == toktab
->operator[1])
550 if (toktab
->token
== ERROR
)
551 cpp_error (pfile
, "'%.*s' is not allowed in #if expressions",
552 (int) (tok_end
- tok_start
), tok_start
);
553 op
.op
= toktab
->token
;
563 /* Convert an operator ID to a string. BUFF is a buffer at least 5
564 characters long which might be used to store the string. */
565 /* XXX FIXME: Remove BUFF when new lexer is implemented. */
571 const struct token
*toktab
;
573 /* See if it is a special token of length 2. */
574 for (toktab
= tokentab2
; toktab
->operator != NULL
; toktab
++)
575 if (op
== toktab
->token
)
576 return toktab
->operator;
579 sprintf (buff
, "%c", (int) op
);
581 sprintf (buff
, "\\%03o", (int) op
);
585 /* Parse a C escape sequence. STRING_PTR points to a variable
586 containing a pointer to the string to parse. That pointer
587 is updated past the characters we use. The value of the
588 escape sequence is returned.
590 A negative value means the sequence \ newline was seen,
591 which is supposed to be equivalent to nothing at all.
593 If \ is followed by a null character, we return a negative
594 value and leave the string pointer pointing at the null character.
596 If \ is followed by 000, we return 0 and leave the string pointer
597 after the zeros. A value of 0 does not mean end of string. */
599 static HOST_WIDEST_INT
600 parse_escape (pfile
, string_ptr
, result_mask
)
603 HOST_WIDEST_INT result_mask
;
605 register int c
= *(*string_ptr
)++;
614 if (CPP_PEDANTIC (pfile
))
615 cpp_pedwarn (pfile
, "non-ANSI-standard escape sequence, '\\%c'", c
);
620 return TARGET_NEWLINE
;
642 register HOST_WIDEST_INT i
= c
- '0';
643 register int count
= 0;
646 c
= *(*string_ptr
)++;
647 if (c
>= '0' && c
<= '7')
648 i
= (i
<< 3) + c
- '0';
655 if (i
!= (i
& result_mask
))
658 cpp_pedwarn (pfile
, "octal escape sequence out of range");
664 register unsigned HOST_WIDEST_INT i
= 0, overflow
= 0;
665 register int digits_found
= 0, digit
;
668 c
= *(*string_ptr
)++;
669 if (c
>= '0' && c
<= '9')
671 else if (c
>= 'a' && c
<= 'f')
672 digit
= c
- 'a' + 10;
673 else if (c
>= 'A' && c
<= 'F')
674 digit
= c
- 'A' + 10;
680 overflow
|= i
^ (i
<< 4 >> 4);
681 i
= (i
<< 4) + digit
;
685 cpp_error (pfile
, "\\x used with no following hex digits");
686 if (overflow
| (i
!= (i
& result_mask
)))
689 cpp_pedwarn (pfile
, "hex escape sequence out of range");
699 integer_overflow (pfile
)
702 if (CPP_PEDANTIC (pfile
))
703 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
706 static HOST_WIDEST_INT
707 left_shift (pfile
, a
, unsignedp
, b
)
710 unsigned int unsignedp
;
711 unsigned HOST_WIDEST_INT b
;
713 if (b
>= HOST_BITS_PER_WIDEST_INT
)
715 if (! unsignedp
&& a
!= 0)
716 integer_overflow (pfile
);
720 return (unsigned HOST_WIDEST_INT
) a
<< b
;
723 HOST_WIDEST_INT l
= a
<< b
;
725 integer_overflow (pfile
);
730 static HOST_WIDEST_INT
731 right_shift (pfile
, a
, unsignedp
, b
)
732 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
734 unsigned int unsignedp
;
735 unsigned HOST_WIDEST_INT b
;
737 if (b
>= HOST_BITS_PER_WIDEST_INT
)
738 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
740 return (unsigned HOST_WIDEST_INT
) a
>> b
;
745 /* Operator precedence and flags table.
747 After an operator is returned from the lexer, if it has priority less
748 than or equal to the operator on the top of the stack, we reduce the
749 stack by one operator and repeat the test. Since equal priorities
750 reduce, this is naturally left-associative.
752 We handle right-associative operators by clearing the lower bit of all
753 left-associative operators, and setting it for right-associative ones.
754 After the reduction phase of a new operator, just before it is pushed
755 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
756 during the reduction phase, the current right-associative operator has
757 a priority one greater than any other operator of otherwise equal
758 precedence that has been pushed on the top of the stack. This avoids
759 a reduction pass, and effectively makes the logic right-associative.
761 The remaining cases are '(' and ')'. We handle '(' by skipping the
762 reduction phase completely. ')' is given lower priority than
763 everything else, including '(', effectively forcing a reduction of the
764 parenthesised expression. If there is no matching '(', the stack will
765 be reduced all the way to the beginning, exiting the parser in the
766 same way as the ultra-low priority end-of-expression dummy operator.
767 The exit code checks to see if the operator that caused it is ')', and
768 if so outputs an appropriate error message.
770 The parser assumes all shifted operators require a right operand
771 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
772 These semantics are automatically checked, any extra semantics need to
773 be handled with operator-specific code. */
776 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
777 #define PRIO_SHIFT (FLAG_BITS + 1)
778 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
779 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
782 #define HAVE_VALUE (1 << 0)
783 #define NO_L_OPERAND (1 << 1)
784 #define NO_R_OPERAND (1 << 2)
785 #define SHORT_CIRCUIT (1 << 3)
787 /* Priority and flag combinations. */
788 #define RIGHT_ASSOC (1 << FLAG_BITS)
789 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
790 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
791 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
792 #define COMMA_PRIO (3 << PRIO_SHIFT)
793 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
794 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
795 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
796 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
797 #define OR_PRIO (8 << PRIO_SHIFT)
798 #define XOR_PRIO (9 << PRIO_SHIFT)
799 #define AND_PRIO (10 << PRIO_SHIFT)
800 #define EQUAL_PRIO (11 << PRIO_SHIFT)
801 #define LESS_PRIO (12 << PRIO_SHIFT)
802 #define SHIFT_PRIO (13 << PRIO_SHIFT)
803 #define PLUS_PRIO (14 << PRIO_SHIFT)
804 #define MUL_PRIO (15 << PRIO_SHIFT)
805 #define UNARY_PRIO ((16 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
807 #define COMPARE(OP) \
808 top->unsignedp = 0; \
809 top->value = (unsigned1 | unsigned2) \
810 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
812 #define EQUALITY(OP) \
813 top->value = v1 OP v2; \
815 #define LOGICAL(OP) \
816 top->value = v1 OP v2; \
817 top->unsignedp = unsigned1 | unsigned2;
819 /* Parse and evaluate a C expression, reading from PFILE.
820 Returns the truth value of the expression. */
823 _cpp_parse_expr (pfile
)
826 /* The implementation is an operator precedence parser, i.e. a
827 bottom-up parser, using a stack for not-yet-reduced tokens.
829 The stack base is 'stack', and the current stack pointer is 'top'.
830 There is a stack element for each operator (only),
831 and the most recently pushed operator is 'top->op'.
832 An operand (value) is stored in the 'value' field of the stack
833 element of the operator that precedes it.
834 In that case the 'flags' field has the HAVE_VALUE flag set. */
836 #define INIT_STACK_SIZE 20
837 struct operation init_stack
[INIT_STACK_SIZE
];
838 struct operation
*stack
= init_stack
;
839 struct operation
*limit
= stack
+ INIT_STACK_SIZE
;
840 register struct operation
*top
= stack
+ 1;
841 long old_written
= CPP_WRITTEN (pfile
);
842 int skip_evaluation
= 0;
846 /* Save parser state and set it to something sane. */
847 int save_only_seen_white
= pfile
->only_seen_white
;
848 int save_skipping
= pfile
->skipping
;
849 pfile
->only_seen_white
= 0;
852 /* We've finished when we try to reduce this. */
854 /* Nifty way to catch missing '('. */
855 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
856 /* Avoid missing right operand checks. */
857 top
->flags
= NO_R_OPERAND
;
866 op
= lex (pfile
, skip_evaluation
);
868 /* If the token is an operand, push its value and get next
869 token. If it is an operator, get its priority and flags, and
870 try to reduce the expression on the stack. */
874 CPP_ICE ("lex returns a NAME");
878 SYNTAX_ERROR ("invalid character in #if");
883 /* Push a value onto the stack. */
884 if (top
->flags
& HAVE_VALUE
)
885 SYNTAX_ERROR ("missing binary operator");
886 top
->value
= op
.value
;
887 top
->unsignedp
= op
.unsignedp
;
888 top
->flags
|= HAVE_VALUE
;
892 case '-': prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
893 /* else unary; fall through */
895 case '~': prio
= UNARY_PRIO
; break;
899 case '%': prio
= MUL_PRIO
; break;
903 case GEQ
: prio
= LESS_PRIO
; break;
905 case EQUAL
: prio
= EQUAL_PRIO
; break;
907 case RSH
: prio
= SHIFT_PRIO
; break;
908 case '&': prio
= AND_PRIO
; break;
909 case '^': prio
= XOR_PRIO
; break;
910 case '|': prio
= OR_PRIO
; break;
911 case ANDAND
: prio
= ANDAND_PRIO
; break;
912 case OROR
: prio
= OROR_PRIO
; break;
913 case ',': prio
= COMMA_PRIO
; break;
914 case '(': prio
= OPEN_PAREN_PRIO
; break;
915 case ')': prio
= CLOSE_PAREN_PRIO
; break;
916 case ':': prio
= COLON_PRIO
; break;
917 case '?': prio
= COND_PRIO
; break;
918 case 0: prio
= FORCE_REDUCE_PRIO
; break;
921 /* Separate the operator's code into priority and flags. */
922 flags
= EXTRACT_FLAGS(prio
);
923 prio
= EXTRACT_PRIO(prio
);
927 /* Check for reductions. Then push the operator. */
928 while (prio
<= top
->prio
)
930 HOST_WIDEST_INT v1
, v2
;
931 unsigned int unsigned1
, unsigned2
;
933 /* Most operators that can appear on the stack require a
934 right operand. Check this before trying to reduce. */
935 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
938 SYNTAX_ERROR ("void expression between '(' and ')'");
940 SYNTAX_ERROR2 ("operator '%s' has no right operand",
941 op_to_str (top
->op
, buff
));
944 unsigned2
= top
->unsignedp
, v2
= top
->value
;
946 unsigned1
= top
->unsignedp
, v1
= top
->value
;
948 /* Now set top->value = (top[1].op)(v1, v2); */
952 if (!(top
->flags
& HAVE_VALUE
))
955 top
->unsignedp
= unsigned2
;
956 top
->flags
|= HAVE_VALUE
;
960 top
->value
= v1
+ v2
;
961 top
->unsignedp
= unsigned1
| unsigned2
;
962 if (! top
->unsignedp
&& ! skip_evaluation
963 && ! possible_sum_sign (v1
, v2
, top
->value
))
964 integer_overflow (pfile
);
968 if (!(top
->flags
& HAVE_VALUE
))
971 if (!skip_evaluation
&& (top
->value
& v2
) < 0
973 integer_overflow (pfile
);
974 top
->unsignedp
= unsigned2
;
975 top
->flags
|= HAVE_VALUE
;
979 top
->value
= v1
- v2
;
980 top
->unsignedp
= unsigned1
| unsigned2
;
981 if (! top
->unsignedp
&& ! skip_evaluation
982 && ! possible_sum_sign (top
->value
, v2
, v1
))
983 integer_overflow (pfile
);
987 top
->unsignedp
= unsigned1
| unsigned2
;
989 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
990 else if (!skip_evaluation
)
992 top
->value
= v1
* v2
;
993 if (v1
&& (top
->value
/ v1
!= v2
994 || (top
->value
& v1
& v2
) < 0))
995 integer_overflow (pfile
);
1000 if (skip_evaluation
)
1003 SYNTAX_ERROR ("division by zero in #if");
1004 top
->unsignedp
= unsigned1
| unsigned2
;
1005 if (top
[1].op
== '/')
1008 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
1011 top
->value
= v1
/ v2
;
1012 if ((top
->value
& v1
& v2
) < 0)
1013 integer_overflow (pfile
);
1019 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
1021 top
->value
= v1
% v2
;
1027 top
->flags
|= HAVE_VALUE
;
1031 top
->unsignedp
= unsigned2
;
1032 top
->flags
|= HAVE_VALUE
;
1034 case '<': COMPARE(<); break;
1035 case '>': COMPARE(>); break;
1036 case LEQ
: COMPARE(<=); break;
1037 case GEQ
: COMPARE(>=); break;
1038 case EQUAL
: EQUALITY(==); break;
1039 case NOTEQUAL
: EQUALITY(!=); break;
1041 if (skip_evaluation
)
1043 top
->unsignedp
= unsigned1
;
1044 if (v2
< 0 && ! unsigned2
)
1045 top
->value
= right_shift (pfile
, v1
, unsigned1
, -v2
);
1047 top
->value
= left_shift (pfile
, v1
, unsigned1
, v2
);
1050 if (skip_evaluation
)
1052 top
->unsignedp
= unsigned1
;
1053 if (v2
< 0 && ! unsigned2
)
1054 top
->value
= left_shift (pfile
, v1
, unsigned1
, -v2
);
1056 top
->value
= right_shift (pfile
, v1
, unsigned1
, v2
);
1058 case '&': LOGICAL(&); break;
1059 case '^': LOGICAL(^); break;
1060 case '|': LOGICAL(|); break;
1062 top
->value
= v1
&& v2
; top
->unsignedp
= 0;
1063 if (!v1
) skip_evaluation
--;
1066 top
->value
= v1
|| v2
; top
->unsignedp
= 0;
1067 if (v1
) skip_evaluation
--;
1070 if (CPP_PEDANTIC (pfile
))
1071 cpp_pedwarn (pfile
, "comma operator in operand of #if");
1073 top
->unsignedp
= unsigned2
;
1076 SYNTAX_ERROR ("syntax error '?' without following ':'");
1078 if (top
[0].op
!= '?')
1079 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
1081 if (top
->value
) skip_evaluation
--;
1082 top
->value
= top
->value
? v1
: v2
;
1083 top
->unsignedp
= unsigned1
| unsigned2
;
1087 SYNTAX_ERROR ("missing ')' in expression");
1089 op
.unsignedp
= unsigned2
;
1090 goto push_immediate
;
1092 SYNTAX_ERROR2 ("unimplemented operator '%s'",
1093 op_to_str (top
[1].op
, buff
));
1095 /* Reducing this dummy operator indicates we've finished. */
1097 SYNTAX_ERROR ("missing '(' in expression");
1102 /* Handle short-circuit evaluations. */
1103 if (flags
& SHORT_CIRCUIT
)
1106 case OROR
: if (top
->value
) skip_evaluation
++; break;
1108 case '?': if (!top
->value
) skip_evaluation
++; break;
1110 if (top
[-1].value
) /* Was '?' condition true? */
1117 /* Check we have a left operand iff we need one. */
1118 if (flags
& NO_L_OPERAND
)
1120 if (top
->flags
& HAVE_VALUE
)
1121 SYNTAX_ERROR2 ("missing binary operator before '%s'",
1122 op_to_str (op
.op
, buff
));
1126 if (!(top
->flags
& HAVE_VALUE
))
1127 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1128 op_to_str (op
.op
, buff
));
1131 /* Check for and handle stack overflow. */
1135 struct operation
*new_stack
;
1136 int old_size
= (char *) limit
- (char *) stack
;
1137 int new_size
= 2 * old_size
;
1138 if (stack
!= init_stack
)
1139 new_stack
= (struct operation
*) xrealloc (stack
, new_size
);
1142 new_stack
= (struct operation
*) xmalloc (new_size
);
1143 memcpy (new_stack
, stack
, old_size
);
1146 top
= (struct operation
*) ((char *) new_stack
+ old_size
);
1147 limit
= (struct operation
*) ((char *) new_stack
+ new_size
);
1151 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
1156 result
= (top
[1].value
!= 0);
1158 CPP_ICE ("unbalanced stack in #if expression");
1159 else if (!(top
[1].flags
& HAVE_VALUE
))
1161 SYNTAX_ERROR ("#if with no expression");
1163 _cpp_skip_rest_of_line (pfile
);
1164 result
= 0; /* Return 0 on syntax error. */
1167 /* Free dynamic stack if we allocated one. */
1168 if (stack
!= init_stack
)
1170 CPP_SET_WRITTEN (pfile
, old_written
);
1171 pfile
->only_seen_white
= save_only_seen_white
;
1172 pfile
->skipping
= save_skipping
;