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 token
->val
.node
->name
);
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", token
->val
.node
->name
);
371 if (_cpp_test_assertion (pfile
, &temp
))
379 /* We don't worry about its position here. */
380 pfile
->mi_if_not_defined
= MI_IND_NOT
;
384 if (((int) token
->type
> (int) CPP_EQ
385 && (int) token
->type
< (int) CPP_PLUS_EQ
)
386 || token
->type
== CPP_EOF
)
392 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
393 cpp_token_as_text (pfile
, token
));
402 integer_overflow (pfile
)
405 if (CPP_PEDANTIC (pfile
))
406 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
409 static HOST_WIDEST_INT
410 left_shift (pfile
, a
, unsignedp
, b
)
413 unsigned int unsignedp
;
414 unsigned HOST_WIDEST_INT b
;
416 if (b
>= HOST_BITS_PER_WIDEST_INT
)
418 if (! unsignedp
&& a
!= 0)
419 integer_overflow (pfile
);
423 return (unsigned HOST_WIDEST_INT
) a
<< b
;
426 HOST_WIDEST_INT l
= a
<< b
;
428 integer_overflow (pfile
);
433 static HOST_WIDEST_INT
434 right_shift (pfile
, a
, unsignedp
, b
)
435 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
437 unsigned int unsignedp
;
438 unsigned HOST_WIDEST_INT b
;
440 if (b
>= HOST_BITS_PER_WIDEST_INT
)
441 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
443 return (unsigned HOST_WIDEST_INT
) a
>> b
;
448 /* Operator precedence and flags table.
450 After an operator is returned from the lexer, if it has priority less
451 than or equal to the operator on the top of the stack, we reduce the
452 stack by one operator and repeat the test. Since equal priorities
453 reduce, this is naturally left-associative.
455 We handle right-associative operators by clearing the lower bit of all
456 left-associative operators, and setting it for right-associative ones.
457 After the reduction phase of a new operator, just before it is pushed
458 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
459 during the reduction phase, the current right-associative operator has
460 a priority one greater than any other operator of otherwise equal
461 precedence that has been pushed on the top of the stack. This avoids
462 a reduction pass, and effectively makes the logic right-associative.
464 The remaining cases are '(' and ')'. We handle '(' by skipping the
465 reduction phase completely. ')' is given lower priority than
466 everything else, including '(', effectively forcing a reduction of the
467 parenthesised expression. If there is no matching '(', the stack will
468 be reduced all the way to the beginning, exiting the parser in the
469 same way as the ultra-low priority end-of-expression dummy operator.
470 The exit code checks to see if the operator that caused it is ')', and
471 if so outputs an appropriate error message.
473 The parser assumes all shifted operators require a right operand
474 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
475 These semantics are automatically checked, any extra semantics need to
476 be handled with operator-specific code. */
479 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
480 #define PRIO_SHIFT (FLAG_BITS + 1)
481 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
482 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
485 #define HAVE_VALUE (1 << 0)
486 #define NO_L_OPERAND (1 << 1)
487 #define NO_R_OPERAND (1 << 2)
488 #define SHORT_CIRCUIT (1 << 3)
490 /* Priority and flag combinations. */
491 #define RIGHT_ASSOC (1 << FLAG_BITS)
492 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
493 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
494 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
495 #define COMMA_PRIO (3 << PRIO_SHIFT)
496 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
497 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
498 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
499 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
500 #define OR_PRIO (8 << PRIO_SHIFT)
501 #define XOR_PRIO (9 << PRIO_SHIFT)
502 #define AND_PRIO (10 << PRIO_SHIFT)
503 #define MINMAX_PRIO (11 << PRIO_SHIFT)
504 #define EQUAL_PRIO (12 << PRIO_SHIFT)
505 #define LESS_PRIO (13 << PRIO_SHIFT)
506 #define SHIFT_PRIO (14 << PRIO_SHIFT)
507 #define PLUS_PRIO (15 << PRIO_SHIFT)
508 #define MUL_PRIO (16 << PRIO_SHIFT)
509 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
511 /* Operator to priority map. Must be in the same order as the first
512 N entries of enum cpp_ttype. */
516 /* EQ */ 0, /* dummy entry - can't happen */
517 /* NOT */ UNARY_PRIO
,
518 /* GREATER */ LESS_PRIO
,
519 /* LESS */ LESS_PRIO
,
520 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
521 /* MINUS */ UNARY_PRIO
, /* or binary */
528 /* RSHIFT */ SHIFT_PRIO
,
529 /* LSHIFT */ SHIFT_PRIO
,
530 /* MIN */ MINMAX_PRIO
, /* C++ specific */
531 /* MAX */ MINMAX_PRIO
, /* extensions */
533 /* COMPL */ UNARY_PRIO
,
534 /* AND_AND */ ANDAND_PRIO
,
535 /* OR_OR */ OROR_PRIO
,
536 /* QUERY */ COND_PRIO
,
537 /* COLON */ COLON_PRIO
,
538 /* COMMA */ COMMA_PRIO
,
539 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
540 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
541 /* EQ_EQ */ EQUAL_PRIO
,
542 /* NOT_EQ */ EQUAL_PRIO
,
543 /* GREATER_EQ */ LESS_PRIO
,
544 /* LESS_EQ */ LESS_PRIO
547 #define COMPARE(OP) \
548 top->unsignedp = 0; \
549 top->value = (unsigned1 | unsigned2) \
550 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
552 #define EQUALITY(OP) \
553 top->value = v1 OP v2; \
555 #define BITWISE(OP) \
556 top->value = v1 OP v2; \
557 top->unsignedp = unsigned1 | unsigned2;
559 top->value = (v1 OP v2) ? v1 : v2; \
560 top->unsignedp = unsigned1 | unsigned2;
562 top->value = OP v2; \
563 top->unsignedp = unsigned2; \
564 top->flags |= HAVE_VALUE;
565 #define SHIFT(PSH, MSH) \
566 if (skip_evaluation) \
568 top->unsignedp = unsigned1; \
569 if (v2 < 0 && ! unsigned2) \
570 top->value = MSH (pfile, v1, unsigned1, -v2); \
572 top->value = PSH (pfile, v1, unsigned1, v2);
574 /* Parse and evaluate a C expression, reading from PFILE.
575 Returns the truth value of the expression. */
578 _cpp_parse_expr (pfile
)
581 /* The implementation is an operator precedence parser, i.e. a
582 bottom-up parser, using a stack for not-yet-reduced tokens.
584 The stack base is 'stack', and the current stack pointer is 'top'.
585 There is a stack element for each operator (only),
586 and the most recently pushed operator is 'top->op'.
587 An operand (value) is stored in the 'value' field of the stack
588 element of the operator that precedes it.
589 In that case the 'flags' field has the HAVE_VALUE flag set. */
591 #define INIT_STACK_SIZE 20
592 struct op init_stack
[INIT_STACK_SIZE
];
593 struct op
*stack
= init_stack
;
594 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
596 register struct op
*top
= stack
+ 1;
597 int skip_evaluation
= 0;
600 /* Set up detection of #if ! defined(). */
602 pfile
->mi_if_not_defined
= MI_IND_NONE
;
604 /* We've finished when we try to reduce this. */
606 /* Nifty way to catch missing '('. */
607 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
608 /* Avoid missing right operand checks. */
609 top
->flags
= NO_R_OPERAND
;
618 op
= lex (pfile
, skip_evaluation
, &token
);
621 /* If the token is an operand, push its value and get next
622 token. If it is an operator, get its priority and flags, and
623 try to reduce the expression on the stack. */
630 /* Push a value onto the stack. */
631 if (top
->flags
& HAVE_VALUE
)
632 SYNTAX_ERROR ("missing binary operator");
633 top
->value
= op
.value
;
634 top
->unsignedp
= op
.unsignedp
;
635 top
->flags
|= HAVE_VALUE
;
638 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
640 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
641 /* else unary; fall through */
642 default: prio
= op_to_prio
[op
.op
]; break;
645 /* Separate the operator's code into priority and flags. */
646 flags
= EXTRACT_FLAGS(prio
);
647 prio
= EXTRACT_PRIO(prio
);
648 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
651 /* Check for reductions. Then push the operator. */
652 while (prio
<= top
->prio
)
654 HOST_WIDEST_INT v1
, v2
;
655 unsigned int unsigned1
, unsigned2
;
657 /* Most operators that can appear on the stack require a
658 right operand. Check this before trying to reduce. */
659 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
661 if (top
->op
== CPP_OPEN_PAREN
)
662 SYNTAX_ERROR ("void expression between '(' and ')'");
664 SYNTAX_ERROR2 ("operator '%s' has no right operand",
665 op_as_text (pfile
, top
->op
));
668 unsigned2
= top
->unsignedp
, v2
= top
->value
;
670 unsigned1
= top
->unsignedp
, v1
= top
->value
;
672 /* Now set top->value = (top[1].op)(v1, v2); */
676 cpp_ice (pfile
, "impossible operator '%s'",
677 op_as_text (pfile
, top
[1].op
));
680 case CPP_NOT
: UNARY(!); break;
681 case CPP_COMPL
: UNARY(~); break;
682 case CPP_LESS
: COMPARE(<); break;
683 case CPP_GREATER
: COMPARE(>); break;
684 case CPP_LESS_EQ
: COMPARE(<=); break;
685 case CPP_GREATER_EQ
: COMPARE(>=); break;
686 case CPP_EQ_EQ
: EQUALITY(==); break;
687 case CPP_NOT_EQ
: EQUALITY(!=); break;
688 case CPP_AND
: BITWISE(&); break;
689 case CPP_XOR
: BITWISE(^); break;
690 case CPP_OR
: BITWISE(|); break;
691 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
692 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
693 case CPP_MIN
: MINMAX(<); break;
694 case CPP_MAX
: MINMAX(>); break;
697 if (!(top
->flags
& HAVE_VALUE
))
699 /* Can't use UNARY(+) because K+R C did not have unary
700 plus. Can't use UNARY() because some compilers object
701 to the empty argument. */
703 top
->unsignedp
= unsigned2
;
704 top
->flags
|= HAVE_VALUE
;
706 if (CPP_WTRADITIONAL (pfile
))
708 "traditional C rejects the unary plus operator");
712 top
->value
= v1
+ v2
;
713 top
->unsignedp
= unsigned1
| unsigned2
;
714 if (! top
->unsignedp
&& ! skip_evaluation
715 && ! possible_sum_sign (v1
, v2
, top
->value
))
716 integer_overflow (pfile
);
720 if (!(top
->flags
& HAVE_VALUE
))
723 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
724 integer_overflow (pfile
);
728 top
->value
= v1
- v2
;
729 top
->unsignedp
= unsigned1
| unsigned2
;
730 if (! top
->unsignedp
&& ! skip_evaluation
731 && ! possible_sum_sign (top
->value
, v2
, v1
))
732 integer_overflow (pfile
);
736 top
->unsignedp
= unsigned1
| unsigned2
;
738 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
739 else if (!skip_evaluation
)
741 top
->value
= v1
* v2
;
742 if (v1
&& (top
->value
/ v1
!= v2
743 || (top
->value
& v1
& v2
) < 0))
744 integer_overflow (pfile
);
752 SYNTAX_ERROR ("division by zero in #if");
753 top
->unsignedp
= unsigned1
| unsigned2
;
754 if (top
[1].op
== CPP_DIV
)
757 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
760 top
->value
= v1
/ v2
;
761 if ((top
->value
& v1
& v2
) < 0)
762 integer_overflow (pfile
);
768 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
770 top
->value
= v1
% v2
;
775 top
->value
= v1
|| v2
;
777 if (v1
) skip_evaluation
--;
780 top
->value
= v1
&& v2
;
782 if (!v1
) skip_evaluation
--;
785 if (CPP_PEDANTIC (pfile
))
786 cpp_pedwarn (pfile
, "comma operator in operand of #if");
788 top
->unsignedp
= unsigned2
;
791 SYNTAX_ERROR ("syntax error '?' without following ':'");
793 if (top
[0].op
!= CPP_QUERY
)
794 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
796 if (top
->value
) skip_evaluation
--;
797 top
->value
= top
->value
? v1
: v2
;
798 top
->unsignedp
= unsigned1
| unsigned2
;
801 if (op
.op
!= CPP_CLOSE_PAREN
)
802 SYNTAX_ERROR ("missing ')' in expression");
804 op
.unsignedp
= unsigned2
;
807 /* Reducing this dummy operator indicates we've finished. */
808 if (op
.op
== CPP_CLOSE_PAREN
)
809 SYNTAX_ERROR ("missing '(' in expression");
814 /* Handle short-circuit evaluations. */
815 if (flags
& SHORT_CIRCUIT
)
818 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
820 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
822 if (top
[-1].value
) /* Was '?' condition true? */
831 /* Check we have a left operand iff we need one. */
832 if (flags
& NO_L_OPERAND
)
834 if (top
->flags
& HAVE_VALUE
)
835 SYNTAX_ERROR2 ("missing binary operator before '%s'",
836 op_as_text (pfile
, top
->op
));
840 if (!(top
->flags
& HAVE_VALUE
))
841 SYNTAX_ERROR2 ("operator '%s' has no left operand",
842 op_as_text (pfile
, top
->op
));
845 /* Check for and handle stack overflow. */
849 struct op
*new_stack
;
850 int old_size
= (char *) limit
- (char *) stack
;
851 int new_size
= 2 * old_size
;
852 if (stack
!= init_stack
)
853 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
856 new_stack
= (struct op
*) xmalloc (new_size
);
857 memcpy (new_stack
, stack
, old_size
);
860 top
= (struct op
*) ((char *) new_stack
+ old_size
);
861 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
865 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
870 result
= (top
[1].value
!= 0);
872 CPP_ICE ("unbalanced stack in #if");
873 else if (!(top
[1].flags
& HAVE_VALUE
))
875 SYNTAX_ERROR ("#if with no expression");
877 result
= 0; /* Return 0 on syntax error. */
880 /* Free dynamic stack if we allocated one. */
881 if (stack
!= init_stack
)
886 static const unsigned char *
887 op_as_text (pfile
, op
)
895 return cpp_token_as_text (pfile
, &token
);