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 MAX_CHAR_TYPE_SIZE
29 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
32 #ifndef MAX_INT_TYPE_SIZE
33 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
36 #ifndef MAX_LONG_TYPE_SIZE
37 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
40 #ifndef MAX_WCHAR_TYPE_SIZE
41 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
44 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
45 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
46 : ~ (HOST_WIDEST_INT) 0)
48 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
49 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
50 : ~ (HOST_WIDEST_INT) 0)
52 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
53 number with SUM's sign, where A, B, and SUM are all C integers. */
54 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
56 static void integer_overflow
PARAMS ((cpp_reader
*));
57 static HOST_WIDEST_INT left_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
59 unsigned HOST_WIDEST_INT
));
60 static HOST_WIDEST_INT right_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
62 unsigned HOST_WIDEST_INT
));
63 static struct op parse_number
PARAMS ((cpp_reader
*, const cpp_token
*));
64 static struct op parse_charconst
PARAMS ((cpp_reader
*, const cpp_token
*));
65 static struct op parse_defined
PARAMS ((cpp_reader
*));
66 static HOST_WIDEST_INT parse_escape
PARAMS ((cpp_reader
*, const U_CHAR
**,
67 const U_CHAR
*, HOST_WIDEST_INT
));
68 static struct op lex
PARAMS ((cpp_reader
*, int, cpp_token
*));
69 static const unsigned char *op_as_text
PARAMS ((cpp_reader
*, enum cpp_ttype
));
74 U_CHAR prio
; /* Priority of op. */
76 U_CHAR unsignedp
; /* True if value should be treated as unsigned. */
77 HOST_WIDEST_INT value
; /* The value logically "right" of op. */
80 /* There is no "error" token, but we can't get comments in #if, so we can
81 abuse that token type. */
82 #define CPP_ERROR CPP_COMMENT
84 /* With -O2, gcc appears to produce nice code, moving the error
85 message load and subsequent jump completely out of the main path. */
86 #define CPP_ICE(msgid) \
87 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
88 #define SYNTAX_ERROR(msgid) \
89 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
90 #define SYNTAX_ERROR2(msgid, arg) \
91 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
93 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
94 with or without size suffixes. */
102 const struct suffix vsuf_1
[] = {
103 { "u", 1, 0 }, { "U", 1, 0 },
104 { "l", 0, 1 }, { "L", 0, 1 }
107 const struct suffix vsuf_2
[] = {
108 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
109 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
110 { "ll", 0, 2 }, { "LL", 0, 2 }
113 const struct suffix vsuf_3
[] = {
114 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
115 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
117 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
120 parse_number (pfile
, tok
)
122 const cpp_token
*tok
;
125 const U_CHAR
*start
= tok
->val
.str
.text
;
126 const U_CHAR
*end
= start
+ tok
->val
.str
.len
;
127 const U_CHAR
*p
= start
;
129 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
132 int digit
, largest_digit
= 0;
133 const struct suffix
*sufftab
;
139 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
151 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
152 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
153 / ((unsigned HOST_WIDEST_INT
) base
));
159 if (c
>= '0' && c
<= '9')
161 /* We believe that in all live character sets, a-f are
162 consecutive, and so are A-F. */
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;
170 if (largest_digit
< digit
)
171 largest_digit
= digit
;
172 nd
= n
* base
+ digit
;
173 overflow
|= MAX_over_base
< n
|| nd
< n
;
179 /* Check for a floating point constant. Note that float constants
180 with an exponent or suffix but no decimal point are technically
181 invalid (C99 6.4.4.2) but accepted elsewhere. */
182 if ((c
== '.' || c
== 'F' || c
== 'f')
183 || (base
== 10 && (c
== 'E' || c
== 'e')
184 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
185 || (base
== 16 && (c
== 'P' || c
== 'p')
186 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
187 SYNTAX_ERROR ("floating point numbers are not valid in #if");
189 /* Determine the suffix. l means long, and u means unsigned.
190 See the suffix tables, above. */
193 case 1: sufftab
= vsuf_1
; nsuff
= Nsuff(vsuf_1
); break;
194 case 2: sufftab
= vsuf_2
; nsuff
= Nsuff(vsuf_2
); break;
195 case 3: sufftab
= vsuf_3
; nsuff
= Nsuff(vsuf_3
); break;
196 default: goto invalid_suffix
;
199 for (i
= 0; i
< nsuff
; i
++)
200 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
204 op
.unsignedp
= sufftab
[i
].u
;
206 if (CPP_WTRADITIONAL (pfile
) && sufftab
[i
].u
)
207 cpp_warning (pfile
, "traditional C rejects the `U' suffix");
208 if (CPP_OPTION (pfile
, c89
) && sufftab
[i
].l
== 2)
209 SYNTAX_ERROR ("too many 'l' suffixes in integer constant");
212 if (base
<= largest_digit
)
213 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
216 cpp_pedwarn (pfile
, "integer constant out of range");
218 /* If too big to be signed, consider it unsigned. */
219 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
222 cpp_warning (pfile
, "integer constant is so large that it is unsigned");
231 cpp_error (pfile
, "invalid suffix '%.*s' on integer constant",
238 /* Parse and convert a character constant for #if. Understands backslash
239 escapes (\n, \031) and multibyte characters (if so configured). */
241 parse_charconst (pfile
, tok
)
243 const cpp_token
*tok
;
246 HOST_WIDEST_INT result
= 0;
249 unsigned int width
= MAX_CHAR_TYPE_SIZE
, mask
= MAX_CHAR_TYPE_MASK
;
251 const U_CHAR
*ptr
= tok
->val
.str
.text
;
252 const U_CHAR
*end
= ptr
+ tok
->val
.str
.len
;
256 if (tok
->type
== CPP_WCHAR
)
257 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
258 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
264 CPP_ICE ("unescaped ' in character constant");
267 c
= parse_escape (pfile
, &ptr
, end
, mask
);
268 if (width
< HOST_BITS_PER_INT
269 && (unsigned int) c
>= (unsigned int)(1 << width
))
271 "escape sequence out of range for character");
274 /* Merge character into result; ignore excess chars. */
275 if (++num_chars
<= max_chars
)
277 if (width
< HOST_BITS_PER_INT
)
278 result
= (result
<< width
) | (c
& ((1 << width
) - 1));
285 SYNTAX_ERROR ("empty character constant");
286 else if (num_chars
> max_chars
)
287 SYNTAX_ERROR ("character constant too long");
288 else if (num_chars
!= 1)
289 cpp_warning (pfile
, "multi-character character constant");
291 /* If char type is signed, sign-extend the constant. */
292 num_bits
= num_chars
* width
;
294 if (pfile
->spec_nodes
.n__CHAR_UNSIGNED__
->type
== NT_MACRO
295 || ((result
>> (num_bits
- 1)) & 1) == 0)
296 op
.value
= result
& ((unsigned HOST_WIDEST_INT
) ~0
297 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
299 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
300 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
302 /* This is always a signed type. */
313 parse_defined (pfile
)
317 cpp_hashnode
*node
= 0;
321 /* Don't expand macros. */
322 pfile
->state
.prevent_expansion
++;
324 _cpp_get_token (pfile
, &token
);
325 if (token
.type
== CPP_OPEN_PAREN
)
328 _cpp_get_token (pfile
, &token
);
331 if (token
.type
== CPP_NAME
)
333 node
= token
.val
.node
;
336 _cpp_get_token (pfile
, &token
);
337 if (token
.type
!= CPP_CLOSE_PAREN
)
339 cpp_error (pfile
, "missing ')' after \"defined\"");
345 cpp_error (pfile
, "\"defined\" without an identifier");
351 op
.value
= node
->type
== NT_MACRO
;
355 /* No macros? At top of file? */
356 if (pfile
->mi_state
== MI_OUTSIDE
&& pfile
->mi_cmacro
== 0
357 && pfile
->mi_if_not_defined
== MI_IND_NOT
&& pfile
->mi_lexed
== 1)
359 cpp_start_lookahead (pfile
);
360 cpp_get_token (pfile
, &token
);
361 if (token
.type
== CPP_EOF
)
362 pfile
->mi_ind_cmacro
= node
;
363 cpp_stop_lookahead (pfile
, 0);
367 pfile
->state
.prevent_expansion
--;
371 /* Read one token. */
374 lex (pfile
, skip_evaluation
, token
)
381 _cpp_get_token (pfile
, token
);
387 return parse_number (pfile
, token
);
390 return parse_charconst (pfile
, token
);
394 SYNTAX_ERROR ("string constants are not valid in #if");
397 SYNTAX_ERROR ("floating point numbers are not valid in #if");
400 if (ISGRAPH (token
->val
.c
))
401 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
403 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
406 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
408 if (pfile
->context
->prev
&& CPP_PEDANTIC (pfile
))
409 cpp_pedwarn (pfile
, "\"defined\" operator appears during macro expansion");
411 return parse_defined (pfile
);
413 /* Controlling #if expressions cannot contain identifiers (they
414 could become macros in the future). */
415 pfile
->mi_state
= MI_FAILED
;
421 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
422 cpp_warning (pfile
, "\"%s\" is not defined", token
->val
.node
->name
);
431 if (_cpp_test_assertion (pfile
, &temp
))
439 /* We don't worry about its position here. */
440 pfile
->mi_if_not_defined
= MI_IND_NOT
;
444 if ((token
->type
> CPP_EQ
&& token
->type
< CPP_PLUS_EQ
)
445 || token
->type
== CPP_EOF
)
451 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
452 cpp_token_as_text (pfile
, token
));
460 /* Parse a C escape sequence. STRING_PTR points to a variable
461 containing a pointer to the string to parse. That pointer
462 is updated past the characters we use. The value of the
463 escape sequence is returned.
465 If \ is followed by 000, we return 0 and leave the string pointer
466 after the zeros. A value of 0 does not mean end of string. */
468 static HOST_WIDEST_INT
469 parse_escape (pfile
, string_ptr
, limit
, result_mask
)
471 const U_CHAR
**string_ptr
;
473 HOST_WIDEST_INT result_mask
;
475 const U_CHAR
*ptr
= *string_ptr
;
476 /* We know we have at least one following character. */
480 case 'a': c
= TARGET_BELL
; break;
481 case 'b': c
= TARGET_BS
; break;
482 case 'f': c
= TARGET_FF
; break;
483 case 'n': c
= TARGET_NEWLINE
; break;
484 case 'r': c
= TARGET_CR
; break;
485 case 't': c
= TARGET_TAB
; break;
486 case 'v': c
= TARGET_VT
; break;
489 if (CPP_PEDANTIC (pfile
))
490 cpp_pedwarn (pfile
, "non-ISO-standard escape sequence, '\\%c'", c
);
494 case '0': case '1': case '2': case '3':
495 case '4': case '5': case '6': case '7':
497 unsigned int i
= c
- '0';
505 if (c
< '0' || c
> '7')
508 i
= (i
<< 3) + c
- '0';
510 if (i
!= (i
& result_mask
))
513 cpp_pedwarn (pfile
, "octal escape sequence out of range");
521 unsigned int i
= 0, overflow
= 0;
522 int digits_found
= 0, digit
;
528 if (c
>= '0' && c
<= '9')
530 else if (c
>= 'a' && c
<= 'f')
531 digit
= c
- 'a' + 10;
532 else if (c
>= 'A' && c
<= 'F')
533 digit
= c
- 'A' + 10;
537 overflow
|= i
^ (i
<< 4 >> 4);
538 i
= (i
<< 4) + digit
;
542 cpp_error (pfile
, "\\x used with no following hex digits");
543 if (overflow
| (i
!= (i
& result_mask
)))
546 cpp_pedwarn (pfile
, "hex escape sequence out of range");
557 integer_overflow (pfile
)
560 if (CPP_PEDANTIC (pfile
))
561 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
564 static HOST_WIDEST_INT
565 left_shift (pfile
, a
, unsignedp
, b
)
568 unsigned int unsignedp
;
569 unsigned HOST_WIDEST_INT b
;
571 if (b
>= HOST_BITS_PER_WIDEST_INT
)
573 if (! unsignedp
&& a
!= 0)
574 integer_overflow (pfile
);
578 return (unsigned HOST_WIDEST_INT
) a
<< b
;
581 HOST_WIDEST_INT l
= a
<< b
;
583 integer_overflow (pfile
);
588 static HOST_WIDEST_INT
589 right_shift (pfile
, a
, unsignedp
, b
)
590 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
592 unsigned int unsignedp
;
593 unsigned HOST_WIDEST_INT b
;
595 if (b
>= HOST_BITS_PER_WIDEST_INT
)
596 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
598 return (unsigned HOST_WIDEST_INT
) a
>> b
;
603 /* Operator precedence and flags table.
605 After an operator is returned from the lexer, if it has priority less
606 than or equal to the operator on the top of the stack, we reduce the
607 stack by one operator and repeat the test. Since equal priorities
608 reduce, this is naturally left-associative.
610 We handle right-associative operators by clearing the lower bit of all
611 left-associative operators, and setting it for right-associative ones.
612 After the reduction phase of a new operator, just before it is pushed
613 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
614 during the reduction phase, the current right-associative operator has
615 a priority one greater than any other operator of otherwise equal
616 precedence that has been pushed on the top of the stack. This avoids
617 a reduction pass, and effectively makes the logic right-associative.
619 The remaining cases are '(' and ')'. We handle '(' by skipping the
620 reduction phase completely. ')' is given lower priority than
621 everything else, including '(', effectively forcing a reduction of the
622 parenthesised expression. If there is no matching '(', the stack will
623 be reduced all the way to the beginning, exiting the parser in the
624 same way as the ultra-low priority end-of-expression dummy operator.
625 The exit code checks to see if the operator that caused it is ')', and
626 if so outputs an appropriate error message.
628 The parser assumes all shifted operators require a right operand
629 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
630 These semantics are automatically checked, any extra semantics need to
631 be handled with operator-specific code. */
634 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
635 #define PRIO_SHIFT (FLAG_BITS + 1)
636 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
637 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
640 #define HAVE_VALUE (1 << 0)
641 #define NO_L_OPERAND (1 << 1)
642 #define NO_R_OPERAND (1 << 2)
643 #define SHORT_CIRCUIT (1 << 3)
645 /* Priority and flag combinations. */
646 #define RIGHT_ASSOC (1 << FLAG_BITS)
647 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
648 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
649 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
650 #define COMMA_PRIO (3 << PRIO_SHIFT)
651 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
652 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
653 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
654 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
655 #define OR_PRIO (8 << PRIO_SHIFT)
656 #define XOR_PRIO (9 << PRIO_SHIFT)
657 #define AND_PRIO (10 << PRIO_SHIFT)
658 #define MINMAX_PRIO (11 << PRIO_SHIFT)
659 #define EQUAL_PRIO (12 << PRIO_SHIFT)
660 #define LESS_PRIO (13 << PRIO_SHIFT)
661 #define SHIFT_PRIO (14 << PRIO_SHIFT)
662 #define PLUS_PRIO (15 << PRIO_SHIFT)
663 #define MUL_PRIO (16 << PRIO_SHIFT)
664 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
666 /* Operator to priority map. Must be in the same order as the first
667 N entries of enum cpp_ttype. */
671 /* EQ */ 0, /* dummy entry - can't happen */
672 /* NOT */ UNARY_PRIO
,
673 /* GREATER */ LESS_PRIO
,
674 /* LESS */ LESS_PRIO
,
675 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
676 /* MINUS */ UNARY_PRIO
, /* or binary */
683 /* RSHIFT */ SHIFT_PRIO
,
684 /* LSHIFT */ SHIFT_PRIO
,
685 /* MIN */ MINMAX_PRIO
, /* C++ specific */
686 /* MAX */ MINMAX_PRIO
, /* extensions */
688 /* COMPL */ UNARY_PRIO
,
689 /* AND_AND */ ANDAND_PRIO
,
690 /* OR_OR */ OROR_PRIO
,
691 /* QUERY */ COND_PRIO
,
692 /* COLON */ COLON_PRIO
,
693 /* COMMA */ COMMA_PRIO
,
694 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
695 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
696 /* EQ_EQ */ EQUAL_PRIO
,
697 /* NOT_EQ */ EQUAL_PRIO
,
698 /* GREATER_EQ */ LESS_PRIO
,
699 /* LESS_EQ */ LESS_PRIO
702 #define COMPARE(OP) \
703 top->unsignedp = 0; \
704 top->value = (unsigned1 | unsigned2) \
705 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
707 #define EQUALITY(OP) \
708 top->value = v1 OP v2; \
710 #define BITWISE(OP) \
711 top->value = v1 OP v2; \
712 top->unsignedp = unsigned1 | unsigned2;
714 top->value = (v1 OP v2) ? v1 : v2; \
715 top->unsignedp = unsigned1 | unsigned2;
717 top->value = OP v2; \
718 top->unsignedp = unsigned2; \
719 top->flags |= HAVE_VALUE;
720 #define SHIFT(PSH, MSH) \
721 if (skip_evaluation) \
723 top->unsignedp = unsigned1; \
724 if (v2 < 0 && ! unsigned2) \
725 top->value = MSH (pfile, v1, unsigned1, -v2); \
727 top->value = PSH (pfile, v1, unsigned1, v2);
729 /* Parse and evaluate a C expression, reading from PFILE.
730 Returns the truth value of the expression. */
733 _cpp_parse_expr (pfile
)
736 /* The implementation is an operator precedence parser, i.e. a
737 bottom-up parser, using a stack for not-yet-reduced tokens.
739 The stack base is 'stack', and the current stack pointer is 'top'.
740 There is a stack element for each operator (only),
741 and the most recently pushed operator is 'top->op'.
742 An operand (value) is stored in the 'value' field of the stack
743 element of the operator that precedes it.
744 In that case the 'flags' field has the HAVE_VALUE flag set. */
746 #define INIT_STACK_SIZE 20
747 struct op init_stack
[INIT_STACK_SIZE
];
748 struct op
*stack
= init_stack
;
749 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
751 register struct op
*top
= stack
+ 1;
752 int skip_evaluation
= 0;
755 /* Set up detection of #if ! defined(). */
757 pfile
->mi_if_not_defined
= MI_IND_NONE
;
759 /* We've finished when we try to reduce this. */
761 /* Nifty way to catch missing '('. */
762 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
763 /* Avoid missing right operand checks. */
764 top
->flags
= NO_R_OPERAND
;
773 op
= lex (pfile
, skip_evaluation
, &token
);
776 /* If the token is an operand, push its value and get next
777 token. If it is an operator, get its priority and flags, and
778 try to reduce the expression on the stack. */
785 /* Push a value onto the stack. */
786 if (top
->flags
& HAVE_VALUE
)
787 SYNTAX_ERROR ("missing binary operator");
788 top
->value
= op
.value
;
789 top
->unsignedp
= op
.unsignedp
;
790 top
->flags
|= HAVE_VALUE
;
793 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
795 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
796 /* else unary; fall through */
797 default: prio
= op_to_prio
[op
.op
]; break;
800 /* Separate the operator's code into priority and flags. */
801 flags
= EXTRACT_FLAGS(prio
);
802 prio
= EXTRACT_PRIO(prio
);
803 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
806 /* Check for reductions. Then push the operator. */
807 while (prio
<= top
->prio
)
809 HOST_WIDEST_INT v1
, v2
;
810 unsigned int unsigned1
, unsigned2
;
812 /* Most operators that can appear on the stack require a
813 right operand. Check this before trying to reduce. */
814 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
816 if (top
->op
== CPP_OPEN_PAREN
)
817 SYNTAX_ERROR ("void expression between '(' and ')'");
819 SYNTAX_ERROR2 ("operator '%s' has no right operand",
820 op_as_text (pfile
, top
->op
));
823 unsigned2
= top
->unsignedp
, v2
= top
->value
;
825 unsigned1
= top
->unsignedp
, v1
= top
->value
;
827 /* Now set top->value = (top[1].op)(v1, v2); */
831 cpp_ice (pfile
, "impossible operator '%s'",
832 op_as_text (pfile
, top
[1].op
));
835 case CPP_NOT
: UNARY(!); break;
836 case CPP_COMPL
: UNARY(~); break;
837 case CPP_LESS
: COMPARE(<); break;
838 case CPP_GREATER
: COMPARE(>); break;
839 case CPP_LESS_EQ
: COMPARE(<=); break;
840 case CPP_GREATER_EQ
: COMPARE(>=); break;
841 case CPP_EQ_EQ
: EQUALITY(==); break;
842 case CPP_NOT_EQ
: EQUALITY(!=); break;
843 case CPP_AND
: BITWISE(&); break;
844 case CPP_XOR
: BITWISE(^); break;
845 case CPP_OR
: BITWISE(|); break;
846 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
847 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
848 case CPP_MIN
: MINMAX(<); break;
849 case CPP_MAX
: MINMAX(>); break;
852 if (!(top
->flags
& HAVE_VALUE
))
854 /* Can't use UNARY(+) because K+R C did not have unary
855 plus. Can't use UNARY() because some compilers object
856 to the empty argument. */
858 top
->unsignedp
= unsigned2
;
859 top
->flags
|= HAVE_VALUE
;
861 if (CPP_WTRADITIONAL (pfile
))
863 "traditional C rejects the unary plus operator");
867 top
->value
= v1
+ v2
;
868 top
->unsignedp
= unsigned1
| unsigned2
;
869 if (! top
->unsignedp
&& ! skip_evaluation
870 && ! possible_sum_sign (v1
, v2
, top
->value
))
871 integer_overflow (pfile
);
875 if (!(top
->flags
& HAVE_VALUE
))
878 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
879 integer_overflow (pfile
);
883 top
->value
= v1
- v2
;
884 top
->unsignedp
= unsigned1
| unsigned2
;
885 if (! top
->unsignedp
&& ! skip_evaluation
886 && ! possible_sum_sign (top
->value
, v2
, v1
))
887 integer_overflow (pfile
);
891 top
->unsignedp
= unsigned1
| unsigned2
;
893 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
894 else if (!skip_evaluation
)
896 top
->value
= v1
* v2
;
897 if (v1
&& (top
->value
/ v1
!= v2
898 || (top
->value
& v1
& v2
) < 0))
899 integer_overflow (pfile
);
907 SYNTAX_ERROR ("division by zero in #if");
908 top
->unsignedp
= unsigned1
| unsigned2
;
909 if (top
[1].op
== CPP_DIV
)
912 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
915 top
->value
= v1
/ v2
;
916 if ((top
->value
& v1
& v2
) < 0)
917 integer_overflow (pfile
);
923 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
925 top
->value
= v1
% v2
;
930 top
->value
= v1
|| v2
;
932 if (v1
) skip_evaluation
--;
935 top
->value
= v1
&& v2
;
937 if (!v1
) skip_evaluation
--;
940 if (CPP_PEDANTIC (pfile
))
941 cpp_pedwarn (pfile
, "comma operator in operand of #if");
943 top
->unsignedp
= unsigned2
;
946 SYNTAX_ERROR ("syntax error '?' without following ':'");
948 if (top
[0].op
!= CPP_QUERY
)
949 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
951 if (top
->value
) skip_evaluation
--;
952 top
->value
= top
->value
? v1
: v2
;
953 top
->unsignedp
= unsigned1
| unsigned2
;
956 if (op
.op
!= CPP_CLOSE_PAREN
)
957 SYNTAX_ERROR ("missing ')' in expression");
959 op
.unsignedp
= unsigned2
;
962 /* Reducing this dummy operator indicates we've finished. */
963 if (op
.op
== CPP_CLOSE_PAREN
)
964 SYNTAX_ERROR ("missing '(' in expression");
969 /* Handle short-circuit evaluations. */
970 if (flags
& SHORT_CIRCUIT
)
973 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
975 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
977 if (top
[-1].value
) /* Was '?' condition true? */
986 /* Check we have a left operand iff we need one. */
987 if (flags
& NO_L_OPERAND
)
989 if (top
->flags
& HAVE_VALUE
)
990 SYNTAX_ERROR2 ("missing binary operator before '%s'",
991 op_as_text (pfile
, top
->op
));
995 if (!(top
->flags
& HAVE_VALUE
))
996 SYNTAX_ERROR2 ("operator '%s' has no left operand",
997 op_as_text (pfile
, top
->op
));
1000 /* Check for and handle stack overflow. */
1004 struct op
*new_stack
;
1005 int old_size
= (char *) limit
- (char *) stack
;
1006 int new_size
= 2 * old_size
;
1007 if (stack
!= init_stack
)
1008 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
1011 new_stack
= (struct op
*) xmalloc (new_size
);
1012 memcpy (new_stack
, stack
, old_size
);
1015 top
= (struct op
*) ((char *) new_stack
+ old_size
);
1016 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
1020 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
1025 result
= (top
[1].value
!= 0);
1027 CPP_ICE ("unbalanced stack in #if");
1028 else if (!(top
[1].flags
& HAVE_VALUE
))
1030 SYNTAX_ERROR ("#if with no expression");
1032 result
= 0; /* Return 0 on syntax error. */
1035 /* Free dynamic stack if we allocated one. */
1036 if (stack
!= init_stack
)
1041 static const unsigned char *
1042 op_as_text (pfile
, op
)
1050 return cpp_token_as_text (pfile
, &token
);