1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Parse a C expression from text in a string */
29 #ifndef MAX_CHAR_TYPE_SIZE
30 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
33 #ifndef MAX_INT_TYPE_SIZE
34 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
37 #ifndef MAX_LONG_TYPE_SIZE
38 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
41 #ifndef MAX_WCHAR_TYPE_SIZE
42 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
45 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
46 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
47 : ~ (HOST_WIDEST_INT) 0)
49 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
50 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
51 : ~ (HOST_WIDEST_INT) 0)
53 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
54 number with SUM's sign, where A, B, and SUM are all C integers. */
55 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
57 static void integer_overflow
PARAMS ((cpp_reader
*));
58 static HOST_WIDEST_INT left_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
60 unsigned HOST_WIDEST_INT
));
61 static HOST_WIDEST_INT right_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
63 unsigned HOST_WIDEST_INT
));
64 static struct op parse_number
PARAMS ((cpp_reader
*, const cpp_token
*));
65 static struct op parse_charconst
PARAMS ((cpp_reader
*, const cpp_token
*));
66 static struct op parse_defined
PARAMS ((cpp_reader
*));
67 static HOST_WIDEST_INT parse_escape
PARAMS ((cpp_reader
*, const U_CHAR
**,
68 const U_CHAR
*, HOST_WIDEST_INT
));
69 static struct op lex
PARAMS ((cpp_reader
*, int, cpp_token
*));
70 static const unsigned char *op_as_text
PARAMS ((cpp_reader
*, enum cpp_ttype
));
75 U_CHAR prio
; /* Priority of op. */
77 U_CHAR unsignedp
; /* True if value should be treated as unsigned. */
78 HOST_WIDEST_INT value
; /* The value logically "right" of op. */
81 /* There is no "error" token, but we can't get comments in #if, so we can
82 abuse that token type. */
83 #define CPP_ERROR CPP_COMMENT
85 /* With -O2, gcc appears to produce nice code, moving the error
86 message load and subsequent jump completely out of the main path. */
87 #define CPP_ICE(msgid) \
88 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
89 #define SYNTAX_ERROR(msgid) \
90 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
91 #define SYNTAX_ERROR2(msgid, arg) \
92 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
94 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
95 with or without size suffixes. */
103 const struct suffix vsuf_1
[] = {
104 { "u", 1, 0 }, { "U", 1, 0 },
105 { "l", 0, 1 }, { "L", 0, 1 }
108 const struct suffix vsuf_2
[] = {
109 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
110 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
111 { "ll", 0, 2 }, { "LL", 0, 2 }
114 const struct suffix vsuf_3
[] = {
115 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
116 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
118 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
121 parse_number (pfile
, tok
)
123 const cpp_token
*tok
;
126 const U_CHAR
*start
= tok
->val
.str
.text
;
127 const U_CHAR
*end
= start
+ tok
->val
.str
.len
;
128 const U_CHAR
*p
= start
;
130 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
133 int digit
, largest_digit
= 0;
134 const struct suffix
*sufftab
;
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 /* We believe that in all live character sets, a-f are
163 consecutive, and so are A-F. */
164 else if (base
== 16 && c
>= 'a' && c
<= 'f')
165 digit
= c
- 'a' + 10;
166 else if (base
== 16 && c
>= 'A' && c
<= 'F')
167 digit
= c
- 'A' + 10;
171 if (largest_digit
< digit
)
172 largest_digit
= digit
;
173 nd
= n
* base
+ digit
;
174 overflow
|= MAX_over_base
< n
|| nd
< n
;
180 /* Check for a floating point constant. Note that float constants
181 with an exponent or suffix but no decimal point are technically
182 invalid (C99 6.4.4.2) but accepted elsewhere. */
183 if ((c
== '.' || c
== 'F' || c
== 'f')
184 || (base
== 10 && (c
== 'E' || c
== 'e')
185 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
186 || (base
== 16 && (c
== 'P' || c
== 'p')
187 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
188 SYNTAX_ERROR ("floating point numbers are not valid in #if");
190 /* Determine the suffix. l means long, and u means unsigned.
191 See the suffix tables, above. */
194 case 1: sufftab
= vsuf_1
; nsuff
= Nsuff(vsuf_1
); break;
195 case 2: sufftab
= vsuf_2
; nsuff
= Nsuff(vsuf_2
); break;
196 case 3: sufftab
= vsuf_3
; nsuff
= Nsuff(vsuf_3
); break;
197 default: goto invalid_suffix
;
200 for (i
= 0; i
< nsuff
; i
++)
201 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
205 op
.unsignedp
= sufftab
[i
].u
;
207 if (CPP_WTRADITIONAL (pfile
) && sufftab
[i
].u
)
208 cpp_warning (pfile
, "traditional C rejects the `U' suffix");
209 if (sufftab
[i
].l
== 2 && CPP_OPTION (pfile
, pedantic
)
210 && ! CPP_OPTION (pfile
, c99
))
211 cpp_pedwarn (pfile
, "too many 'l' suffixes in integer constant");
214 if (base
<= largest_digit
)
215 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
218 cpp_pedwarn (pfile
, "integer constant out of range");
220 /* If too big to be signed, consider it unsigned. */
221 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
224 cpp_warning (pfile
, "integer constant is so large that it is unsigned");
233 cpp_error (pfile
, "invalid suffix '%.*s' on integer constant",
240 /* Parse and convert a character constant for #if. Understands backslash
241 escapes (\n, \031) and multibyte characters (if so configured). */
243 parse_charconst (pfile
, tok
)
245 const cpp_token
*tok
;
248 HOST_WIDEST_INT result
= 0;
251 unsigned int width
= MAX_CHAR_TYPE_SIZE
;
252 HOST_WIDEST_INT mask
= MAX_CHAR_TYPE_MASK
;
254 const U_CHAR
*ptr
= tok
->val
.str
.text
;
255 const U_CHAR
*end
= ptr
+ tok
->val
.str
.len
;
259 if (tok
->type
== CPP_WCHAR
)
260 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
261 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
267 CPP_ICE ("unescaped ' in character constant");
270 c
= parse_escape (pfile
, &ptr
, end
, mask
);
271 if (width
< HOST_BITS_PER_INT
272 && (unsigned int) c
>= (unsigned int)(1 << width
))
274 "escape sequence out of range for character");
277 /* Merge character into result; ignore excess chars. */
278 if (++num_chars
<= max_chars
)
280 if (width
< HOST_BITS_PER_INT
)
281 result
= (result
<< width
) | (c
& ((1 << width
) - 1));
288 SYNTAX_ERROR ("empty character constant");
289 else if (num_chars
> max_chars
)
290 SYNTAX_ERROR ("character constant too long");
291 else if (num_chars
!= 1)
292 cpp_warning (pfile
, "multi-character character constant");
294 /* If char type is signed, sign-extend the constant. */
295 num_bits
= num_chars
* width
;
297 if (pfile
->spec_nodes
.n__CHAR_UNSIGNED__
->type
== NT_MACRO
298 || ((result
>> (num_bits
- 1)) & 1) == 0)
299 op
.value
= result
& ((unsigned HOST_WIDEST_INT
) ~0
300 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
302 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
303 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
305 /* This is always a signed type. */
316 parse_defined (pfile
)
320 cpp_hashnode
*node
= 0;
324 /* Don't expand macros. */
325 pfile
->state
.prevent_expansion
++;
327 cpp_get_token (pfile
, &token
);
328 if (token
.type
== CPP_OPEN_PAREN
)
331 cpp_get_token (pfile
, &token
);
334 if (token
.type
== CPP_NAME
)
336 node
= token
.val
.node
;
339 cpp_get_token (pfile
, &token
);
340 if (token
.type
!= CPP_CLOSE_PAREN
)
342 cpp_error (pfile
, "missing ')' after \"defined\"");
348 cpp_error (pfile
, "\"defined\" without an identifier");
354 op
.value
= node
->type
== NT_MACRO
;
358 /* No macros? At top of file? */
359 if (pfile
->mi_state
== MI_OUTSIDE
&& pfile
->mi_cmacro
== 0
360 && pfile
->mi_if_not_defined
== MI_IND_NOT
&& pfile
->mi_lexed
== 1)
362 cpp_start_lookahead (pfile
);
363 cpp_get_token (pfile
, &token
);
364 if (token
.type
== CPP_EOF
)
365 pfile
->mi_ind_cmacro
= node
;
366 cpp_stop_lookahead (pfile
, 0);
370 pfile
->state
.prevent_expansion
--;
374 /* Read one token. */
377 lex (pfile
, skip_evaluation
, token
)
384 cpp_get_token (pfile
, token
);
390 return parse_number (pfile
, token
);
394 return parse_charconst (pfile
, token
);
398 SYNTAX_ERROR ("string constants are not valid in #if");
401 SYNTAX_ERROR ("floating point numbers are not valid in #if");
404 if (ISGRAPH (token
->val
.c
))
405 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
407 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
410 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
412 if (pfile
->context
->prev
&& CPP_PEDANTIC (pfile
))
413 cpp_pedwarn (pfile
, "\"defined\" operator appears during macro expansion");
415 return parse_defined (pfile
);
417 /* Controlling #if expressions cannot contain identifiers (they
418 could become macros in the future). */
419 pfile
->mi_state
= MI_FAILED
;
425 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
426 cpp_warning (pfile
, "\"%s\" is not defined", token
->val
.node
->name
);
435 if (_cpp_test_assertion (pfile
, &temp
))
443 /* We don't worry about its position here. */
444 pfile
->mi_if_not_defined
= MI_IND_NOT
;
448 if ((token
->type
> CPP_EQ
&& token
->type
< CPP_PLUS_EQ
)
449 || token
->type
== CPP_EOF
)
455 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
456 cpp_token_as_text (pfile
, token
));
464 /* Parse a C escape sequence. STRING_PTR points to a variable
465 containing a pointer to the string to parse. That pointer
466 is updated past the characters we use. The value of the
467 escape sequence is returned.
469 If \ is followed by 000, we return 0 and leave the string pointer
470 after the zeros. A value of 0 does not mean end of string. */
472 static HOST_WIDEST_INT
473 parse_escape (pfile
, string_ptr
, limit
, result_mask
)
475 const U_CHAR
**string_ptr
;
477 HOST_WIDEST_INT result_mask
;
479 const U_CHAR
*ptr
= *string_ptr
;
480 /* We know we have at least one following character. */
484 case 'a': c
= TARGET_BELL
; break;
485 case 'b': c
= TARGET_BS
; break;
486 case 'f': c
= TARGET_FF
; break;
487 case 'n': c
= TARGET_NEWLINE
; break;
488 case 'r': c
= TARGET_CR
; break;
489 case 't': c
= TARGET_TAB
; break;
490 case 'v': c
= TARGET_VT
; break;
493 if (CPP_PEDANTIC (pfile
))
494 cpp_pedwarn (pfile
, "non-ISO-standard escape sequence, '\\%c'", c
);
498 case '0': case '1': case '2': case '3':
499 case '4': case '5': case '6': case '7':
501 unsigned int i
= c
- '0';
509 if (c
< '0' || c
> '7')
512 i
= (i
<< 3) + c
- '0';
514 if (i
!= (i
& result_mask
))
517 cpp_pedwarn (pfile
, "octal escape sequence out of range");
525 unsigned int i
= 0, overflow
= 0;
526 int digits_found
= 0, digit
;
532 if (c
>= '0' && c
<= '9')
534 else if (c
>= 'a' && c
<= 'f')
535 digit
= c
- 'a' + 10;
536 else if (c
>= 'A' && c
<= 'F')
537 digit
= c
- 'A' + 10;
541 overflow
|= i
^ (i
<< 4 >> 4);
542 i
= (i
<< 4) + digit
;
546 cpp_error (pfile
, "\\x used with no following hex digits");
547 if (overflow
| (i
!= (i
& result_mask
)))
550 cpp_pedwarn (pfile
, "hex escape sequence out of range");
561 integer_overflow (pfile
)
564 if (CPP_PEDANTIC (pfile
))
565 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
568 static HOST_WIDEST_INT
569 left_shift (pfile
, a
, unsignedp
, b
)
572 unsigned int unsignedp
;
573 unsigned HOST_WIDEST_INT b
;
575 if (b
>= HOST_BITS_PER_WIDEST_INT
)
577 if (! unsignedp
&& a
!= 0)
578 integer_overflow (pfile
);
582 return (unsigned HOST_WIDEST_INT
) a
<< b
;
585 HOST_WIDEST_INT l
= a
<< b
;
587 integer_overflow (pfile
);
592 static HOST_WIDEST_INT
593 right_shift (pfile
, a
, unsignedp
, b
)
594 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
596 unsigned int unsignedp
;
597 unsigned HOST_WIDEST_INT b
;
599 if (b
>= HOST_BITS_PER_WIDEST_INT
)
600 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
602 return (unsigned HOST_WIDEST_INT
) a
>> b
;
607 /* Operator precedence and flags table.
609 After an operator is returned from the lexer, if it has priority less
610 than or equal to the operator on the top of the stack, we reduce the
611 stack by one operator and repeat the test. Since equal priorities
612 reduce, this is naturally left-associative.
614 We handle right-associative operators by clearing the lower bit of all
615 left-associative operators, and setting it for right-associative ones.
616 After the reduction phase of a new operator, just before it is pushed
617 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
618 during the reduction phase, the current right-associative operator has
619 a priority one greater than any other operator of otherwise equal
620 precedence that has been pushed on the top of the stack. This avoids
621 a reduction pass, and effectively makes the logic right-associative.
623 The remaining cases are '(' and ')'. We handle '(' by skipping the
624 reduction phase completely. ')' is given lower priority than
625 everything else, including '(', effectively forcing a reduction of the
626 parenthesised expression. If there is no matching '(', the stack will
627 be reduced all the way to the beginning, exiting the parser in the
628 same way as the ultra-low priority end-of-expression dummy operator.
629 The exit code checks to see if the operator that caused it is ')', and
630 if so outputs an appropriate error message.
632 The parser assumes all shifted operators require a right operand
633 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
634 These semantics are automatically checked, any extra semantics need to
635 be handled with operator-specific code. */
638 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
639 #define PRIO_SHIFT (FLAG_BITS + 1)
640 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
641 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
644 #define HAVE_VALUE (1 << 0)
645 #define NO_L_OPERAND (1 << 1)
646 #define NO_R_OPERAND (1 << 2)
647 #define SHORT_CIRCUIT (1 << 3)
649 /* Priority and flag combinations. */
650 #define RIGHT_ASSOC (1 << FLAG_BITS)
651 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
652 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
653 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
654 #define COMMA_PRIO (3 << PRIO_SHIFT)
655 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
656 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
657 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
658 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
659 #define OR_PRIO (8 << PRIO_SHIFT)
660 #define XOR_PRIO (9 << PRIO_SHIFT)
661 #define AND_PRIO (10 << PRIO_SHIFT)
662 #define MINMAX_PRIO (11 << PRIO_SHIFT)
663 #define EQUAL_PRIO (12 << PRIO_SHIFT)
664 #define LESS_PRIO (13 << PRIO_SHIFT)
665 #define SHIFT_PRIO (14 << PRIO_SHIFT)
666 #define PLUS_PRIO (15 << PRIO_SHIFT)
667 #define MUL_PRIO (16 << PRIO_SHIFT)
668 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
670 /* Operator to priority map. Must be in the same order as the first
671 N entries of enum cpp_ttype. */
675 /* EQ */ 0, /* dummy entry - can't happen */
676 /* NOT */ UNARY_PRIO
,
677 /* GREATER */ LESS_PRIO
,
678 /* LESS */ LESS_PRIO
,
679 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
680 /* MINUS */ UNARY_PRIO
, /* or binary */
687 /* RSHIFT */ SHIFT_PRIO
,
688 /* LSHIFT */ SHIFT_PRIO
,
689 /* MIN */ MINMAX_PRIO
, /* C++ specific */
690 /* MAX */ MINMAX_PRIO
, /* extensions */
692 /* COMPL */ UNARY_PRIO
,
693 /* AND_AND */ ANDAND_PRIO
,
694 /* OR_OR */ OROR_PRIO
,
695 /* QUERY */ COND_PRIO
,
696 /* COLON */ COLON_PRIO
,
697 /* COMMA */ COMMA_PRIO
,
698 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
699 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
700 /* EQ_EQ */ EQUAL_PRIO
,
701 /* NOT_EQ */ EQUAL_PRIO
,
702 /* GREATER_EQ */ LESS_PRIO
,
703 /* LESS_EQ */ LESS_PRIO
706 #define COMPARE(OP) \
707 top->unsignedp = 0; \
708 top->value = (unsigned1 | unsigned2) \
709 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
711 #define EQUALITY(OP) \
712 top->value = v1 OP v2; \
714 #define BITWISE(OP) \
715 top->value = v1 OP v2; \
716 top->unsignedp = unsigned1 | unsigned2;
718 top->value = (v1 OP v2) ? v1 : v2; \
719 top->unsignedp = unsigned1 | unsigned2;
721 top->value = OP v2; \
722 top->unsignedp = unsigned2; \
723 top->flags |= HAVE_VALUE;
724 #define SHIFT(PSH, MSH) \
725 if (skip_evaluation) \
727 top->unsignedp = unsigned1; \
728 if (v2 < 0 && ! unsigned2) \
729 top->value = MSH (pfile, v1, unsigned1, -v2); \
731 top->value = PSH (pfile, v1, unsigned1, v2);
733 /* Parse and evaluate a C expression, reading from PFILE.
734 Returns the truth value of the expression. */
737 _cpp_parse_expr (pfile
)
740 /* The implementation is an operator precedence parser, i.e. a
741 bottom-up parser, using a stack for not-yet-reduced tokens.
743 The stack base is 'stack', and the current stack pointer is 'top'.
744 There is a stack element for each operator (only),
745 and the most recently pushed operator is 'top->op'.
746 An operand (value) is stored in the 'value' field of the stack
747 element of the operator that precedes it.
748 In that case the 'flags' field has the HAVE_VALUE flag set. */
750 #define INIT_STACK_SIZE 20
751 struct op init_stack
[INIT_STACK_SIZE
];
752 struct op
*stack
= init_stack
;
753 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
755 register struct op
*top
= stack
+ 1;
756 int skip_evaluation
= 0;
759 /* Set up detection of #if ! defined(). */
761 pfile
->mi_if_not_defined
= MI_IND_NONE
;
763 /* We've finished when we try to reduce this. */
765 /* Nifty way to catch missing '('. */
766 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
767 /* Avoid missing right operand checks. */
768 top
->flags
= NO_R_OPERAND
;
777 op
= lex (pfile
, skip_evaluation
, &token
);
780 /* If the token is an operand, push its value and get next
781 token. If it is an operator, get its priority and flags, and
782 try to reduce the expression on the stack. */
789 /* Push a value onto the stack. */
790 if (top
->flags
& HAVE_VALUE
)
791 SYNTAX_ERROR ("missing binary operator");
792 top
->value
= op
.value
;
793 top
->unsignedp
= op
.unsignedp
;
794 top
->flags
|= HAVE_VALUE
;
797 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
799 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
800 /* else unary; fall through */
801 default: prio
= op_to_prio
[op
.op
]; break;
804 /* Separate the operator's code into priority and flags. */
805 flags
= EXTRACT_FLAGS(prio
);
806 prio
= EXTRACT_PRIO(prio
);
807 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
810 /* Check for reductions. Then push the operator. */
811 while (prio
<= top
->prio
)
813 HOST_WIDEST_INT v1
, v2
;
814 unsigned int unsigned1
, unsigned2
;
816 /* Most operators that can appear on the stack require a
817 right operand. Check this before trying to reduce. */
818 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
820 if (top
->op
== CPP_OPEN_PAREN
)
821 SYNTAX_ERROR ("void expression between '(' and ')'");
823 SYNTAX_ERROR2 ("operator '%s' has no right operand",
824 op_as_text (pfile
, top
->op
));
827 unsigned2
= top
->unsignedp
, v2
= top
->value
;
829 unsigned1
= top
->unsignedp
, v1
= top
->value
;
831 /* Now set top->value = (top[1].op)(v1, v2); */
835 cpp_ice (pfile
, "impossible operator '%s'",
836 op_as_text (pfile
, top
[1].op
));
839 case CPP_NOT
: UNARY(!); break;
840 case CPP_COMPL
: UNARY(~); break;
841 case CPP_LESS
: COMPARE(<); break;
842 case CPP_GREATER
: COMPARE(>); break;
843 case CPP_LESS_EQ
: COMPARE(<=); break;
844 case CPP_GREATER_EQ
: COMPARE(>=); break;
845 case CPP_EQ_EQ
: EQUALITY(==); break;
846 case CPP_NOT_EQ
: EQUALITY(!=); break;
847 case CPP_AND
: BITWISE(&); break;
848 case CPP_XOR
: BITWISE(^); break;
849 case CPP_OR
: BITWISE(|); break;
850 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
851 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
852 case CPP_MIN
: MINMAX(<); break;
853 case CPP_MAX
: MINMAX(>); break;
856 if (!(top
->flags
& HAVE_VALUE
))
858 /* Can't use UNARY(+) because K+R C did not have unary
859 plus. Can't use UNARY() because some compilers object
860 to the empty argument. */
862 top
->unsignedp
= unsigned2
;
863 top
->flags
|= HAVE_VALUE
;
865 if (CPP_WTRADITIONAL (pfile
))
867 "traditional C rejects the unary plus operator");
871 top
->value
= v1
+ v2
;
872 top
->unsignedp
= unsigned1
| unsigned2
;
873 if (! top
->unsignedp
&& ! skip_evaluation
874 && ! possible_sum_sign (v1
, v2
, top
->value
))
875 integer_overflow (pfile
);
879 if (!(top
->flags
& HAVE_VALUE
))
882 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
883 integer_overflow (pfile
);
887 top
->value
= v1
- v2
;
888 top
->unsignedp
= unsigned1
| unsigned2
;
889 if (! top
->unsignedp
&& ! skip_evaluation
890 && ! possible_sum_sign (top
->value
, v2
, v1
))
891 integer_overflow (pfile
);
895 top
->unsignedp
= unsigned1
| unsigned2
;
897 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
898 else if (!skip_evaluation
)
900 top
->value
= v1
* v2
;
901 if (v1
&& (top
->value
/ v1
!= v2
902 || (top
->value
& v1
& v2
) < 0))
903 integer_overflow (pfile
);
911 SYNTAX_ERROR ("division by zero in #if");
912 top
->unsignedp
= unsigned1
| unsigned2
;
913 if (top
[1].op
== CPP_DIV
)
916 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
919 top
->value
= v1
/ v2
;
920 if ((top
->value
& v1
& v2
) < 0)
921 integer_overflow (pfile
);
927 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
929 top
->value
= v1
% v2
;
934 top
->value
= v1
|| v2
;
936 if (v1
) skip_evaluation
--;
939 top
->value
= v1
&& v2
;
941 if (!v1
) skip_evaluation
--;
944 if (CPP_PEDANTIC (pfile
))
945 cpp_pedwarn (pfile
, "comma operator in operand of #if");
947 top
->unsignedp
= unsigned2
;
950 SYNTAX_ERROR ("syntax error '?' without following ':'");
952 if (top
[0].op
!= CPP_QUERY
)
953 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
955 if (top
->value
) skip_evaluation
--;
956 top
->value
= top
->value
? v1
: v2
;
957 top
->unsignedp
= unsigned1
| unsigned2
;
960 if (op
.op
!= CPP_CLOSE_PAREN
)
961 SYNTAX_ERROR ("missing ')' in expression");
963 op
.unsignedp
= unsigned2
;
966 /* Reducing this dummy operator indicates we've finished. */
967 if (op
.op
== CPP_CLOSE_PAREN
)
968 SYNTAX_ERROR ("missing '(' in expression");
973 /* Handle short-circuit evaluations. */
974 if (flags
& SHORT_CIRCUIT
)
977 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
979 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
981 if (top
[-1].value
) /* Was '?' condition true? */
990 /* Check we have a left operand iff we need one. */
991 if (flags
& NO_L_OPERAND
)
993 if (top
->flags
& HAVE_VALUE
)
994 SYNTAX_ERROR2 ("missing binary operator before '%s'",
995 op_as_text (pfile
, top
->op
));
999 if (!(top
->flags
& HAVE_VALUE
))
1000 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1001 op_as_text (pfile
, top
->op
));
1004 /* Check for and handle stack overflow. */
1008 struct op
*new_stack
;
1009 int old_size
= (char *) limit
- (char *) stack
;
1010 int new_size
= 2 * old_size
;
1011 if (stack
!= init_stack
)
1012 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
1015 new_stack
= (struct op
*) xmalloc (new_size
);
1016 memcpy (new_stack
, stack
, old_size
);
1019 top
= (struct op
*) ((char *) new_stack
+ old_size
);
1020 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
1024 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
1029 result
= (top
[1].value
!= 0);
1031 CPP_ICE ("unbalanced stack in #if");
1032 else if (!(top
[1].flags
& HAVE_VALUE
))
1034 SYNTAX_ERROR ("#if with no expression");
1036 result
= 0; /* Return 0 on syntax error. */
1039 /* Free dynamic stack if we allocated one. */
1040 if (stack
!= init_stack
)
1045 static const unsigned char *
1046 op_as_text (pfile
, op
)
1054 return cpp_token_as_text (pfile
, &token
);