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. */
26 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
27 number with SUM's sign, where A, B, and SUM are all C integers. */
28 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
30 static void integer_overflow
PARAMS ((cpp_reader
*));
31 static HOST_WIDEST_INT left_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
33 unsigned HOST_WIDEST_INT
));
34 static HOST_WIDEST_INT right_shift
PARAMS ((cpp_reader
*, HOST_WIDEST_INT
,
36 unsigned HOST_WIDEST_INT
));
37 static struct op parse_number
PARAMS ((cpp_reader
*, const cpp_token
*));
38 static struct op parse_defined
PARAMS ((cpp_reader
*));
39 static struct op lex
PARAMS ((cpp_reader
*, int, cpp_token
*));
40 static const unsigned char *op_as_text
PARAMS ((cpp_reader
*, enum cpp_ttype
));
45 U_CHAR prio
; /* Priority of op. */
47 U_CHAR unsignedp
; /* True if value should be treated as unsigned. */
48 HOST_WIDEST_INT value
; /* The value logically "right" of op. */
51 /* There is no "error" token, but we can't get comments in #if, so we can
52 abuse that token type. */
53 #define CPP_ERROR CPP_COMMENT
55 /* With -O2, gcc appears to produce nice code, moving the error
56 message load and subsequent jump completely out of the main path. */
57 #define CPP_ICE(msgid) \
58 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
59 #define SYNTAX_ERROR(msgid) \
60 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
61 #define SYNTAX_ERROR2(msgid, arg) \
62 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
64 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
65 with or without size suffixes. */
73 const struct suffix vsuf_1
[] = {
74 { "u", 1, 0 }, { "U", 1, 0 },
75 { "l", 0, 1 }, { "L", 0, 1 }
78 const struct suffix vsuf_2
[] = {
79 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
80 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
81 { "ll", 0, 2 }, { "LL", 0, 2 }
84 const struct suffix vsuf_3
[] = {
85 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
86 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
88 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
91 parse_number (pfile
, tok
)
96 const U_CHAR
*start
= tok
->val
.str
.text
;
97 const U_CHAR
*end
= start
+ tok
->val
.str
.len
;
98 const U_CHAR
*p
= start
;
100 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
103 int digit
, largest_digit
= 0;
104 const struct suffix
*sufftab
;
110 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
122 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
123 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
124 / ((unsigned HOST_WIDEST_INT
) base
));
130 if (c
>= '0' && c
<= '9')
132 /* We believe that in all live character sets, a-f are
133 consecutive, and so are A-F. */
134 else if (base
== 16 && c
>= 'a' && c
<= 'f')
135 digit
= c
- 'a' + 10;
136 else if (base
== 16 && c
>= 'A' && c
<= 'F')
137 digit
= c
- 'A' + 10;
141 if (largest_digit
< digit
)
142 largest_digit
= digit
;
143 nd
= n
* base
+ digit
;
144 overflow
|= MAX_over_base
< n
|| nd
< n
;
150 /* Check for a floating point constant. Note that float constants
151 with an exponent or suffix but no decimal point are technically
152 invalid (C99 6.4.4.2) but accepted elsewhere. */
153 if ((c
== '.' || c
== 'F' || c
== 'f')
154 || (base
== 10 && (c
== 'E' || c
== 'e')
155 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
156 || (base
== 16 && (c
== 'P' || c
== 'p')
157 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
158 SYNTAX_ERROR ("floating point numbers are not valid in #if");
160 /* Determine the suffix. l means long, and u means unsigned.
161 See the suffix tables, above. */
164 case 1: sufftab
= vsuf_1
; nsuff
= Nsuff(vsuf_1
); break;
165 case 2: sufftab
= vsuf_2
; nsuff
= Nsuff(vsuf_2
); break;
166 case 3: sufftab
= vsuf_3
; nsuff
= Nsuff(vsuf_3
); break;
167 default: goto invalid_suffix
;
170 for (i
= 0; i
< nsuff
; i
++)
171 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
175 op
.unsignedp
= sufftab
[i
].u
;
177 if (CPP_WTRADITIONAL (pfile
)
179 && ! cpp_sys_macro_p (pfile
))
180 cpp_warning (pfile
, "traditional C rejects the `U' suffix");
181 if (sufftab
[i
].l
== 2 && CPP_OPTION (pfile
, pedantic
)
182 && ! CPP_OPTION (pfile
, c99
))
183 cpp_pedwarn (pfile
, "too many 'l' suffixes in integer constant");
186 if (base
<= largest_digit
)
187 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
190 cpp_pedwarn (pfile
, "integer constant out of range");
192 /* If too big to be signed, consider it unsigned. */
193 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
196 cpp_warning (pfile
, "integer constant is so large that it is unsigned");
205 cpp_error (pfile
, "invalid suffix '%.*s' on integer constant",
213 parse_defined (pfile
)
217 cpp_hashnode
*node
= 0;
221 /* Don't expand macros. */
222 pfile
->state
.prevent_expansion
++;
224 cpp_get_token (pfile
, &token
);
225 if (token
.type
== CPP_OPEN_PAREN
)
228 cpp_get_token (pfile
, &token
);
231 if (token
.type
== CPP_NAME
)
233 node
= token
.val
.node
;
236 cpp_get_token (pfile
, &token
);
237 if (token
.type
!= CPP_CLOSE_PAREN
)
239 cpp_error (pfile
, "missing ')' after \"defined\"");
246 cpp_error (pfile
, "operator \"defined\" requires an identifier");
247 if (token
.flags
& NAMED_OP
)
252 op
.type
= token
.type
;
254 "(\"%s\" is an alternative token for \"%s\" in C++)",
255 cpp_token_as_text (pfile
, &token
),
256 cpp_token_as_text (pfile
, &op
));
264 op
.value
= node
->type
== NT_MACRO
;
268 /* No macros? At top of file? */
269 if (pfile
->mi_state
== MI_OUTSIDE
&& pfile
->mi_cmacro
== 0
270 && pfile
->mi_if_not_defined
== MI_IND_NOT
&& pfile
->mi_lexed
== 1)
272 cpp_start_lookahead (pfile
);
273 cpp_get_token (pfile
, &token
);
274 if (token
.type
== CPP_EOF
)
275 pfile
->mi_ind_cmacro
= node
;
276 cpp_stop_lookahead (pfile
, 0);
280 pfile
->state
.prevent_expansion
--;
284 /* Read one token. */
287 lex (pfile
, skip_evaluation
, token
)
294 cpp_get_token (pfile
, token
);
300 return parse_number (pfile
, token
);
305 unsigned int chars_seen
;
307 /* This is always a signed type. */
310 op
.value
= cpp_interpret_charconst (pfile
, token
, 1, 0, &chars_seen
);
316 SYNTAX_ERROR ("string constants are not valid in #if");
319 SYNTAX_ERROR ("floating point numbers are not valid in #if");
322 if (ISGRAPH (token
->val
.c
))
323 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
325 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
328 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
330 if (pfile
->context
->prev
&& CPP_PEDANTIC (pfile
))
331 cpp_pedwarn (pfile
, "\"defined\" operator appears during macro expansion");
333 return parse_defined (pfile
);
335 else if (CPP_OPTION (pfile
, cplusplus
)
336 && (token
->val
.node
== pfile
->spec_nodes
.n_true
337 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
341 op
.value
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
343 /* Warn about use of true or false in #if when pedantic
344 and stdbool.h has not been included. */
345 if (CPP_PEDANTIC (pfile
)
346 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
347 cpp_pedwarn (pfile
, "ISO C++ does not permit \"%s\" in #if",
348 NODE_NAME (token
->val
.node
));
353 /* Controlling #if expressions cannot contain identifiers (they
354 could become macros in the future). */
355 pfile
->mi_state
= MI_FAILED
;
361 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
362 cpp_warning (pfile
, "\"%s\" is not defined",
363 NODE_NAME (token
->val
.node
));
372 if (_cpp_test_assertion (pfile
, &temp
))
380 /* We don't worry about its position here. */
381 pfile
->mi_if_not_defined
= MI_IND_NOT
;
385 if (((int) token
->type
> (int) CPP_EQ
386 && (int) token
->type
< (int) CPP_PLUS_EQ
)
387 || token
->type
== CPP_EOF
)
393 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
394 cpp_token_as_text (pfile
, token
));
403 integer_overflow (pfile
)
406 if (CPP_PEDANTIC (pfile
))
407 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
410 static HOST_WIDEST_INT
411 left_shift (pfile
, a
, unsignedp
, b
)
414 unsigned int unsignedp
;
415 unsigned HOST_WIDEST_INT b
;
417 if (b
>= HOST_BITS_PER_WIDEST_INT
)
419 if (! unsignedp
&& a
!= 0)
420 integer_overflow (pfile
);
424 return (unsigned HOST_WIDEST_INT
) a
<< b
;
427 HOST_WIDEST_INT l
= a
<< b
;
429 integer_overflow (pfile
);
434 static HOST_WIDEST_INT
435 right_shift (pfile
, a
, unsignedp
, b
)
436 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
438 unsigned int unsignedp
;
439 unsigned HOST_WIDEST_INT b
;
441 if (b
>= HOST_BITS_PER_WIDEST_INT
)
442 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
444 return (unsigned HOST_WIDEST_INT
) a
>> b
;
449 /* Operator precedence and flags table.
451 After an operator is returned from the lexer, if it has priority less
452 than or equal to the operator on the top of the stack, we reduce the
453 stack by one operator and repeat the test. Since equal priorities
454 reduce, this is naturally left-associative.
456 We handle right-associative operators by clearing the lower bit of all
457 left-associative operators, and setting it for right-associative ones.
458 After the reduction phase of a new operator, just before it is pushed
459 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
460 during the reduction phase, the current right-associative operator has
461 a priority one greater than any other operator of otherwise equal
462 precedence that has been pushed on the top of the stack. This avoids
463 a reduction pass, and effectively makes the logic right-associative.
465 The remaining cases are '(' and ')'. We handle '(' by skipping the
466 reduction phase completely. ')' is given lower priority than
467 everything else, including '(', effectively forcing a reduction of the
468 parenthesised expression. If there is no matching '(', the stack will
469 be reduced all the way to the beginning, exiting the parser in the
470 same way as the ultra-low priority end-of-expression dummy operator.
471 The exit code checks to see if the operator that caused it is ')', and
472 if so outputs an appropriate error message.
474 The parser assumes all shifted operators require a right operand
475 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
476 These semantics are automatically checked, any extra semantics need to
477 be handled with operator-specific code. */
480 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
481 #define PRIO_SHIFT (FLAG_BITS + 1)
482 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
483 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
486 #define HAVE_VALUE (1 << 0)
487 #define NO_L_OPERAND (1 << 1)
488 #define NO_R_OPERAND (1 << 2)
489 #define SHORT_CIRCUIT (1 << 3)
491 /* Priority and flag combinations. */
492 #define RIGHT_ASSOC (1 << FLAG_BITS)
493 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
494 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
495 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
496 #define COMMA_PRIO (3 << PRIO_SHIFT)
497 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
498 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
499 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
500 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
501 #define OR_PRIO (8 << PRIO_SHIFT)
502 #define XOR_PRIO (9 << PRIO_SHIFT)
503 #define AND_PRIO (10 << PRIO_SHIFT)
504 #define MINMAX_PRIO (11 << PRIO_SHIFT)
505 #define EQUAL_PRIO (12 << PRIO_SHIFT)
506 #define LESS_PRIO (13 << PRIO_SHIFT)
507 #define SHIFT_PRIO (14 << PRIO_SHIFT)
508 #define PLUS_PRIO (15 << PRIO_SHIFT)
509 #define MUL_PRIO (16 << PRIO_SHIFT)
510 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
512 /* Operator to priority map. Must be in the same order as the first
513 N entries of enum cpp_ttype. */
517 /* EQ */ 0, /* dummy entry - can't happen */
518 /* NOT */ UNARY_PRIO
,
519 /* GREATER */ LESS_PRIO
,
520 /* LESS */ LESS_PRIO
,
521 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
522 /* MINUS */ UNARY_PRIO
, /* or binary */
529 /* RSHIFT */ SHIFT_PRIO
,
530 /* LSHIFT */ SHIFT_PRIO
,
531 /* MIN */ MINMAX_PRIO
, /* C++ specific */
532 /* MAX */ MINMAX_PRIO
, /* extensions */
534 /* COMPL */ UNARY_PRIO
,
535 /* AND_AND */ ANDAND_PRIO
,
536 /* OR_OR */ OROR_PRIO
,
537 /* QUERY */ COND_PRIO
,
538 /* COLON */ COLON_PRIO
,
539 /* COMMA */ COMMA_PRIO
,
540 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
541 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
542 /* EQ_EQ */ EQUAL_PRIO
,
543 /* NOT_EQ */ EQUAL_PRIO
,
544 /* GREATER_EQ */ LESS_PRIO
,
545 /* LESS_EQ */ LESS_PRIO
548 #define COMPARE(OP) \
549 top->unsignedp = 0; \
550 top->value = (unsigned1 | unsigned2) \
551 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
553 #define EQUALITY(OP) \
554 top->value = v1 OP v2; \
556 #define BITWISE(OP) \
557 top->value = v1 OP v2; \
558 top->unsignedp = unsigned1 | unsigned2;
560 top->value = (v1 OP v2) ? v1 : v2; \
561 top->unsignedp = unsigned1 | unsigned2;
563 top->value = OP v2; \
564 top->unsignedp = unsigned2; \
565 top->flags |= HAVE_VALUE;
566 #define SHIFT(PSH, MSH) \
567 if (skip_evaluation) \
569 top->unsignedp = unsigned1; \
570 if (v2 < 0 && ! unsigned2) \
571 top->value = MSH (pfile, v1, unsigned1, -v2); \
573 top->value = PSH (pfile, v1, unsigned1, v2);
575 /* Parse and evaluate a C expression, reading from PFILE.
576 Returns the truth value of the expression. */
579 _cpp_parse_expr (pfile
)
582 /* The implementation is an operator precedence parser, i.e. a
583 bottom-up parser, using a stack for not-yet-reduced tokens.
585 The stack base is 'stack', and the current stack pointer is 'top'.
586 There is a stack element for each operator (only),
587 and the most recently pushed operator is 'top->op'.
588 An operand (value) is stored in the 'value' field of the stack
589 element of the operator that precedes it.
590 In that case the 'flags' field has the HAVE_VALUE flag set. */
592 #define INIT_STACK_SIZE 20
593 struct op init_stack
[INIT_STACK_SIZE
];
594 struct op
*stack
= init_stack
;
595 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
597 register struct op
*top
= stack
+ 1;
598 int skip_evaluation
= 0;
601 /* Set up detection of #if ! defined(). */
603 pfile
->mi_if_not_defined
= MI_IND_NONE
;
605 /* We've finished when we try to reduce this. */
607 /* Nifty way to catch missing '('. */
608 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
609 /* Avoid missing right operand checks. */
610 top
->flags
= NO_R_OPERAND
;
619 op
= lex (pfile
, skip_evaluation
, &token
);
622 /* If the token is an operand, push its value and get next
623 token. If it is an operator, get its priority and flags, and
624 try to reduce the expression on the stack. */
631 /* Push a value onto the stack. */
632 if (top
->flags
& HAVE_VALUE
)
633 SYNTAX_ERROR ("missing binary operator");
634 top
->value
= op
.value
;
635 top
->unsignedp
= op
.unsignedp
;
636 top
->flags
|= HAVE_VALUE
;
639 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
641 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
642 /* else unary; fall through */
643 default: prio
= op_to_prio
[op
.op
]; break;
646 /* Separate the operator's code into priority and flags. */
647 flags
= EXTRACT_FLAGS(prio
);
648 prio
= EXTRACT_PRIO(prio
);
649 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
652 /* Check for reductions. Then push the operator. */
653 while (prio
<= top
->prio
)
655 HOST_WIDEST_INT v1
, v2
;
656 unsigned int unsigned1
, unsigned2
;
658 /* Most operators that can appear on the stack require a
659 right operand. Check this before trying to reduce. */
660 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
662 if (top
->op
== CPP_OPEN_PAREN
)
663 SYNTAX_ERROR ("void expression between '(' and ')'");
665 SYNTAX_ERROR2 ("operator '%s' has no right operand",
666 op_as_text (pfile
, top
->op
));
669 unsigned2
= top
->unsignedp
, v2
= top
->value
;
671 unsigned1
= top
->unsignedp
, v1
= top
->value
;
673 /* Now set top->value = (top[1].op)(v1, v2); */
677 cpp_ice (pfile
, "impossible operator '%s'",
678 op_as_text (pfile
, top
[1].op
));
681 case CPP_NOT
: UNARY(!); break;
682 case CPP_COMPL
: UNARY(~); break;
683 case CPP_LESS
: COMPARE(<); break;
684 case CPP_GREATER
: COMPARE(>); break;
685 case CPP_LESS_EQ
: COMPARE(<=); break;
686 case CPP_GREATER_EQ
: COMPARE(>=); break;
687 case CPP_EQ_EQ
: EQUALITY(==); break;
688 case CPP_NOT_EQ
: EQUALITY(!=); break;
689 case CPP_AND
: BITWISE(&); break;
690 case CPP_XOR
: BITWISE(^); break;
691 case CPP_OR
: BITWISE(|); break;
692 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
693 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
694 case CPP_MIN
: MINMAX(<); break;
695 case CPP_MAX
: MINMAX(>); break;
698 if (!(top
->flags
& HAVE_VALUE
))
700 /* Can't use UNARY(+) because K+R C did not have unary
701 plus. Can't use UNARY() because some compilers object
702 to the empty argument. */
704 top
->unsignedp
= unsigned2
;
705 top
->flags
|= HAVE_VALUE
;
707 if (CPP_WTRADITIONAL (pfile
))
709 "traditional C rejects the unary plus operator");
713 top
->value
= v1
+ v2
;
714 top
->unsignedp
= unsigned1
| unsigned2
;
715 if (! top
->unsignedp
&& ! skip_evaluation
716 && ! possible_sum_sign (v1
, v2
, top
->value
))
717 integer_overflow (pfile
);
721 if (!(top
->flags
& HAVE_VALUE
))
724 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
725 integer_overflow (pfile
);
729 top
->value
= v1
- v2
;
730 top
->unsignedp
= unsigned1
| unsigned2
;
731 if (! top
->unsignedp
&& ! skip_evaluation
732 && ! possible_sum_sign (top
->value
, v2
, v1
))
733 integer_overflow (pfile
);
737 top
->unsignedp
= unsigned1
| unsigned2
;
739 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
740 else if (!skip_evaluation
)
742 top
->value
= v1
* v2
;
743 if (v1
&& (top
->value
/ v1
!= v2
744 || (top
->value
& v1
& v2
) < 0))
745 integer_overflow (pfile
);
753 SYNTAX_ERROR ("division by zero in #if");
754 top
->unsignedp
= unsigned1
| unsigned2
;
755 if (top
[1].op
== CPP_DIV
)
758 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
761 top
->value
= v1
/ v2
;
762 if ((top
->value
& v1
& v2
) < 0)
763 integer_overflow (pfile
);
769 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
771 top
->value
= v1
% v2
;
776 top
->value
= v1
|| v2
;
778 if (v1
) skip_evaluation
--;
781 top
->value
= v1
&& v2
;
783 if (!v1
) skip_evaluation
--;
786 if (CPP_PEDANTIC (pfile
))
787 cpp_pedwarn (pfile
, "comma operator in operand of #if");
789 top
->unsignedp
= unsigned2
;
792 SYNTAX_ERROR ("syntax error '?' without following ':'");
794 if (top
[0].op
!= CPP_QUERY
)
795 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
797 if (top
->value
) skip_evaluation
--;
798 top
->value
= top
->value
? v1
: v2
;
799 top
->unsignedp
= unsigned1
| unsigned2
;
802 if (op
.op
!= CPP_CLOSE_PAREN
)
803 SYNTAX_ERROR ("missing ')' in expression");
805 op
.unsignedp
= unsigned2
;
808 /* Reducing this dummy operator indicates we've finished. */
809 if (op
.op
== CPP_CLOSE_PAREN
)
810 SYNTAX_ERROR ("missing '(' in expression");
815 /* Handle short-circuit evaluations. */
816 if (flags
& SHORT_CIRCUIT
)
819 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
821 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
823 if (top
[-1].value
) /* Was '?' condition true? */
832 /* Check we have a left operand iff we need one. */
833 if (flags
& NO_L_OPERAND
)
835 if (top
->flags
& HAVE_VALUE
)
836 SYNTAX_ERROR2 ("missing binary operator before '%s'",
837 op_as_text (pfile
, top
->op
));
841 if (!(top
->flags
& HAVE_VALUE
))
842 SYNTAX_ERROR2 ("operator '%s' has no left operand",
843 op_as_text (pfile
, top
->op
));
846 /* Check for and handle stack overflow. */
850 struct op
*new_stack
;
851 int old_size
= (char *) limit
- (char *) stack
;
852 int new_size
= 2 * old_size
;
853 if (stack
!= init_stack
)
854 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
857 new_stack
= (struct op
*) xmalloc (new_size
);
858 memcpy (new_stack
, stack
, old_size
);
861 top
= (struct op
*) ((char *) new_stack
+ old_size
);
862 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
866 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
871 result
= (top
[1].value
!= 0);
873 CPP_ICE ("unbalanced stack in #if");
874 else if (!(top
[1].flags
& HAVE_VALUE
))
876 SYNTAX_ERROR ("#if with no expression");
878 result
= 0; /* Return 0 on syntax error. */
881 /* Free dynamic stack if we allocated one. */
882 if (stack
!= init_stack
)
887 static const unsigned char *
888 op_as_text (pfile
, op
)
896 return cpp_token_as_text (pfile
, &token
);