1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 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));
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)
66 const unsigned char s
[4];
67 const unsigned char u
;
68 const unsigned char l
;
71 static const struct suffix vsuf_1
[] = {
72 { "u", 1, 0 }, { "U", 1, 0 },
73 { "l", 0, 1 }, { "L", 0, 1 }
76 static const struct suffix vsuf_2
[] = {
77 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
78 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
79 { "ll", 0, 2 }, { "LL", 0, 2 }
82 static const struct suffix vsuf_3
[] = {
83 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
84 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
86 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
88 /* Parse and convert what is presumably an integer in TOK. Accepts
89 decimal, hex, or octal with or without size suffixes. Returned op
90 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
92 parse_number (pfile
, tok
)
97 const U_CHAR
*start
= tok
->val
.str
.text
;
98 const U_CHAR
*end
= start
+ tok
->val
.str
.len
;
99 const U_CHAR
*p
= start
;
101 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
104 int digit
, largest_digit
= 0;
105 const struct suffix
*sufftab
;
111 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
123 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
124 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
125 / ((unsigned HOST_WIDEST_INT
) base
));
132 || (base
== 16 && ISXDIGIT (c
)))
133 digit
= hex_value (c
);
137 if (largest_digit
< digit
)
138 largest_digit
= digit
;
139 nd
= n
* base
+ digit
;
140 overflow
|= MAX_over_base
< n
|| nd
< n
;
146 /* Check for a floating point constant. Note that float constants
147 with an exponent or suffix but no decimal point are technically
148 invalid (C99 6.4.4.2) but accepted elsewhere. */
149 if ((c
== '.' || c
== 'F' || c
== 'f')
150 || (base
== 10 && (c
== 'E' || c
== 'e')
151 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
152 || (base
== 16 && (c
== 'P' || c
== 'p')
153 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
154 SYNTAX_ERROR ("floating point numbers are not valid in #if");
156 /* Determine the suffix. l means long, and u means unsigned.
157 See the suffix tables, above. */
160 case 1: sufftab
= vsuf_1
; nsuff
= Nsuff(vsuf_1
); break;
161 case 2: sufftab
= vsuf_2
; nsuff
= Nsuff(vsuf_2
); break;
162 case 3: sufftab
= vsuf_3
; nsuff
= Nsuff(vsuf_3
); break;
163 default: goto invalid_suffix
;
166 for (i
= 0; i
< nsuff
; i
++)
167 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
171 op
.unsignedp
= sufftab
[i
].u
;
173 if (CPP_WTRADITIONAL (pfile
)
175 && ! cpp_sys_macro_p (pfile
))
176 cpp_warning (pfile
, "traditional C rejects the `U' suffix");
177 if (sufftab
[i
].l
== 2 && CPP_OPTION (pfile
, pedantic
)
178 && ! CPP_OPTION (pfile
, c99
))
179 cpp_pedwarn (pfile
, "too many 'l' suffixes in integer constant");
182 if (base
<= largest_digit
)
183 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
186 cpp_pedwarn (pfile
, "integer constant out of range");
188 /* If too big to be signed, consider it unsigned. */
189 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
192 cpp_warning (pfile
, "integer constant is so large that it is unsigned");
201 cpp_error (pfile
, "invalid suffix '%.*s' on integer constant",
208 /* Handle meeting "defined" in a preprocessor expression. */
210 parse_defined (pfile
)
214 cpp_hashnode
*node
= 0;
215 const cpp_token
*token
;
217 cpp_context
*initial_context
= pfile
->context
;
219 /* Don't expand macros. */
220 pfile
->state
.prevent_expansion
++;
222 token
= cpp_get_token (pfile
);
223 if (token
->type
== CPP_OPEN_PAREN
)
226 token
= cpp_get_token (pfile
);
229 if (token
->type
== CPP_NAME
)
231 node
= token
->val
.node
;
232 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
234 cpp_error (pfile
, "missing ')' after \"defined\"");
240 cpp_error (pfile
, "operator \"defined\" requires an identifier");
241 if (token
->flags
& NAMED_OP
)
246 op
.type
= token
->type
;
248 "(\"%s\" is an alternative token for \"%s\" in C++)",
249 cpp_token_as_text (pfile
, token
),
250 cpp_token_as_text (pfile
, &op
));
258 if (pfile
->context
!= initial_context
)
259 cpp_warning (pfile
, "this use of \"defined\" may not be portable");
261 op
.value
= node
->type
== NT_MACRO
;
265 /* A possible controlling macro of the form #if !defined ().
266 _cpp_parse_expr checks there was no other junk on the line. */
267 pfile
->mi_ind_cmacro
= node
;
270 pfile
->state
.prevent_expansion
--;
274 /* Read a token. The returned type is CPP_NUMBER for a valid number
275 (an interpreted preprocessing number or character constant, or the
276 result of the "defined" or "#" operators), CPP_ERROR on error,
277 CPP_EOF, or the type of an operator token. */
279 lex (pfile
, skip_evaluation
)
284 const cpp_token
*token
= cpp_get_token (pfile
);
289 return parse_number (pfile
, token
);
294 unsigned int chars_seen
;
296 /* This is always a signed type. */
299 op
.value
= cpp_interpret_charconst (pfile
, token
, 1, 0, &chars_seen
);
305 SYNTAX_ERROR ("string constants are not valid in #if");
308 if (ISGRAPH (token
->val
.c
))
309 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
311 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
314 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
315 return parse_defined (pfile
);
316 else if (CPP_OPTION (pfile
, cplusplus
)
317 && (token
->val
.node
== pfile
->spec_nodes
.n_true
318 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
322 op
.value
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
324 /* Warn about use of true or false in #if when pedantic
325 and stdbool.h has not been included. */
326 if (CPP_PEDANTIC (pfile
)
327 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
328 cpp_pedwarn (pfile
, "ISO C++ does not permit \"%s\" in #if",
329 NODE_NAME (token
->val
.node
));
338 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
339 cpp_warning (pfile
, "\"%s\" is not defined",
340 NODE_NAME (token
->val
.node
));
349 if (_cpp_test_assertion (pfile
, &temp
))
357 if (((int) token
->type
> (int) CPP_EQ
358 && (int) token
->type
< (int) CPP_PLUS_EQ
)
359 || token
->type
== CPP_EOF
)
365 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
366 cpp_token_as_text (pfile
, token
));
374 /* Warn if appropriate on overflow. */
376 integer_overflow (pfile
)
379 if (CPP_PEDANTIC (pfile
))
380 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
383 /* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
385 static HOST_WIDEST_INT
386 left_shift (pfile
, a
, unsignedp
, b
)
389 unsigned int unsignedp
;
390 unsigned HOST_WIDEST_INT b
;
392 if (b
>= HOST_BITS_PER_WIDEST_INT
)
394 if (! unsignedp
&& a
!= 0)
395 integer_overflow (pfile
);
399 return (unsigned HOST_WIDEST_INT
) a
<< b
;
402 HOST_WIDEST_INT l
= a
<< b
;
404 integer_overflow (pfile
);
409 /* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
411 static HOST_WIDEST_INT
412 right_shift (pfile
, a
, unsignedp
, b
)
413 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
415 unsigned int unsignedp
;
416 unsigned HOST_WIDEST_INT b
;
418 if (b
>= HOST_BITS_PER_WIDEST_INT
)
419 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
421 return (unsigned HOST_WIDEST_INT
) a
>> b
;
426 /* Operator precedence and flags table.
428 After an operator is returned from the lexer, if it has priority less
429 than or equal to the operator on the top of the stack, we reduce the
430 stack by one operator and repeat the test. Since equal priorities
431 reduce, this is naturally left-associative.
433 We handle right-associative operators by clearing the lower bit of all
434 left-associative operators, and setting it for right-associative ones.
435 After the reduction phase of a new operator, just before it is pushed
436 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
437 during the reduction phase, the current right-associative operator has
438 a priority one greater than any other operator of otherwise equal
439 precedence that has been pushed on the top of the stack. This avoids
440 a reduction pass, and effectively makes the logic right-associative.
442 The remaining cases are '(' and ')'. We handle '(' by skipping the
443 reduction phase completely. ')' is given lower priority than
444 everything else, including '(', effectively forcing a reduction of the
445 parenthesised expression. If there is no matching '(', the stack will
446 be reduced all the way to the beginning, exiting the parser in the
447 same way as the ultra-low priority end-of-expression dummy operator.
448 The exit code checks to see if the operator that caused it is ')', and
449 if so outputs an appropriate error message.
451 The parser assumes all shifted operators require a right operand
452 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
453 These semantics are automatically checked, any extra semantics need to
454 be handled with operator-specific code. */
457 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
458 #define PRIO_SHIFT (FLAG_BITS + 1)
459 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
460 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
463 #define HAVE_VALUE (1 << 0)
464 #define NO_L_OPERAND (1 << 1)
465 #define NO_R_OPERAND (1 << 2)
466 #define SHORT_CIRCUIT (1 << 3)
468 /* Priority and flag combinations. */
469 #define RIGHT_ASSOC (1 << FLAG_BITS)
470 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
471 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
472 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
473 #define COMMA_PRIO (3 << PRIO_SHIFT)
474 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
475 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
476 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
477 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
478 #define OR_PRIO (8 << PRIO_SHIFT)
479 #define XOR_PRIO (9 << PRIO_SHIFT)
480 #define AND_PRIO (10 << PRIO_SHIFT)
481 #define MINMAX_PRIO (11 << PRIO_SHIFT)
482 #define EQUAL_PRIO (12 << PRIO_SHIFT)
483 #define LESS_PRIO (13 << PRIO_SHIFT)
484 #define SHIFT_PRIO (14 << PRIO_SHIFT)
485 #define PLUS_PRIO (15 << PRIO_SHIFT)
486 #define MUL_PRIO (16 << PRIO_SHIFT)
487 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
489 /* Operator to priority map. Must be in the same order as the first
490 N entries of enum cpp_ttype. */
494 /* EQ */ 0, /* dummy entry - can't happen */
495 /* NOT */ UNARY_PRIO
,
496 /* GREATER */ LESS_PRIO
,
497 /* LESS */ LESS_PRIO
,
498 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
499 /* MINUS */ UNARY_PRIO
, /* or binary */
506 /* RSHIFT */ SHIFT_PRIO
,
507 /* LSHIFT */ SHIFT_PRIO
,
508 /* MIN */ MINMAX_PRIO
, /* C++ specific */
509 /* MAX */ MINMAX_PRIO
, /* extensions */
511 /* COMPL */ UNARY_PRIO
,
512 /* AND_AND */ ANDAND_PRIO
,
513 /* OR_OR */ OROR_PRIO
,
514 /* QUERY */ COND_PRIO
,
515 /* COLON */ COLON_PRIO
,
516 /* COMMA */ COMMA_PRIO
,
517 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
518 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
519 /* EQ_EQ */ EQUAL_PRIO
,
520 /* NOT_EQ */ EQUAL_PRIO
,
521 /* GREATER_EQ */ LESS_PRIO
,
522 /* LESS_EQ */ LESS_PRIO
525 #define COMPARE(OP) \
526 top->unsignedp = 0; \
527 top->value = (unsigned1 | unsigned2) \
528 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
530 #define EQUALITY(OP) \
531 top->value = v1 OP v2; \
533 #define BITWISE(OP) \
534 top->value = v1 OP v2; \
535 top->unsignedp = unsigned1 | unsigned2;
537 top->value = (v1 OP v2) ? v1 : v2; \
538 top->unsignedp = unsigned1 | unsigned2;
540 top->value = OP v2; \
541 top->unsignedp = unsigned2; \
542 top->flags |= HAVE_VALUE;
543 #define SHIFT(PSH, MSH) \
544 if (skip_evaluation) \
546 top->unsignedp = unsigned1; \
547 if (v2 < 0 && ! unsigned2) \
548 top->value = MSH (pfile, v1, unsigned1, -v2); \
550 top->value = PSH (pfile, v1, unsigned1, v2);
552 /* Parse and evaluate a C expression, reading from PFILE.
553 Returns the truth value of the expression. */
555 _cpp_parse_expr (pfile
)
558 /* The implementation is an operator precedence parser, i.e. a
559 bottom-up parser, using a stack for not-yet-reduced tokens.
561 The stack base is 'stack', and the current stack pointer is 'top'.
562 There is a stack element for each operator (only),
563 and the most recently pushed operator is 'top->op'.
564 An operand (value) is stored in the 'value' field of the stack
565 element of the operator that precedes it.
566 In that case the 'flags' field has the HAVE_VALUE flag set. */
568 #define INIT_STACK_SIZE 20
569 struct op init_stack
[INIT_STACK_SIZE
];
570 struct op
*stack
= init_stack
;
571 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
572 struct op
*top
= stack
+ 1;
573 int skip_evaluation
= 0;
575 unsigned int lex_count
, saw_leading_not
;
577 /* Set up detection of #if ! defined(). */
578 pfile
->mi_ind_cmacro
= 0;
582 /* We've finished when we try to reduce this. */
584 /* Nifty way to catch missing '('. */
585 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
586 /* Avoid missing right operand checks. */
587 top
->flags
= NO_R_OPERAND
;
596 op
= lex (pfile
, skip_evaluation
);
599 /* If the token is an operand, push its value and get next
600 token. If it is an operator, get its priority and flags, and
601 try to reduce the expression on the stack. */
608 /* Push a value onto the stack. */
609 if (top
->flags
& HAVE_VALUE
)
610 SYNTAX_ERROR ("missing binary operator");
611 top
->value
= op
.value
;
612 top
->unsignedp
= op
.unsignedp
;
613 top
->flags
|= HAVE_VALUE
;
616 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
619 saw_leading_not
= lex_count
== 1;
620 prio
= op_to_prio
[op
.op
];
623 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
624 /* else unary; fall through */
625 default: prio
= op_to_prio
[op
.op
]; break;
628 /* Separate the operator's code into priority and flags. */
629 flags
= EXTRACT_FLAGS(prio
);
630 prio
= EXTRACT_PRIO(prio
);
631 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
634 /* Check for reductions. Then push the operator. */
635 while (prio
<= top
->prio
)
637 HOST_WIDEST_INT v1
, v2
;
638 unsigned int unsigned1
, unsigned2
;
640 /* Most operators that can appear on the stack require a
641 right operand. Check this before trying to reduce. */
642 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
644 if (top
->op
== CPP_OPEN_PAREN
)
645 SYNTAX_ERROR ("void expression between '(' and ')'");
647 SYNTAX_ERROR2 ("operator '%s' has no right operand",
648 op_as_text (pfile
, top
->op
));
651 unsigned2
= top
->unsignedp
, v2
= top
->value
;
653 unsigned1
= top
->unsignedp
, v1
= top
->value
;
655 /* Now set top->value = (top[1].op)(v1, v2); */
659 cpp_ice (pfile
, "impossible operator '%s'",
660 op_as_text (pfile
, top
[1].op
));
663 case CPP_NOT
: UNARY(!); break;
664 case CPP_COMPL
: UNARY(~); break;
665 case CPP_LESS
: COMPARE(<); break;
666 case CPP_GREATER
: COMPARE(>); break;
667 case CPP_LESS_EQ
: COMPARE(<=); break;
668 case CPP_GREATER_EQ
: COMPARE(>=); break;
669 case CPP_EQ_EQ
: EQUALITY(==); break;
670 case CPP_NOT_EQ
: EQUALITY(!=); break;
671 case CPP_AND
: BITWISE(&); break;
672 case CPP_XOR
: BITWISE(^); break;
673 case CPP_OR
: BITWISE(|); break;
674 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
675 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
676 case CPP_MIN
: MINMAX(<); break;
677 case CPP_MAX
: MINMAX(>); break;
680 if (!(top
->flags
& HAVE_VALUE
))
682 /* Can't use UNARY(+) because K+R C did not have unary
683 plus. Can't use UNARY() because some compilers object
684 to the empty argument. */
686 top
->unsignedp
= unsigned2
;
687 top
->flags
|= HAVE_VALUE
;
689 if (CPP_WTRADITIONAL (pfile
))
691 "traditional C rejects the unary plus operator");
695 top
->value
= v1
+ v2
;
696 top
->unsignedp
= unsigned1
| unsigned2
;
697 if (! top
->unsignedp
&& ! skip_evaluation
698 && ! possible_sum_sign (v1
, v2
, top
->value
))
699 integer_overflow (pfile
);
703 if (!(top
->flags
& HAVE_VALUE
))
706 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
707 integer_overflow (pfile
);
711 top
->value
= v1
- v2
;
712 top
->unsignedp
= unsigned1
| unsigned2
;
713 if (! top
->unsignedp
&& ! skip_evaluation
714 && ! possible_sum_sign (top
->value
, v2
, v1
))
715 integer_overflow (pfile
);
719 top
->unsignedp
= unsigned1
| unsigned2
;
721 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
722 else if (!skip_evaluation
)
724 top
->value
= v1
* v2
;
725 if (v1
&& (top
->value
/ v1
!= v2
726 || (top
->value
& v1
& v2
) < 0))
727 integer_overflow (pfile
);
735 SYNTAX_ERROR ("division by zero in #if");
736 top
->unsignedp
= unsigned1
| unsigned2
;
737 if (top
[1].op
== CPP_DIV
)
740 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
743 top
->value
= v1
/ v2
;
744 if ((top
->value
& v1
& v2
) < 0)
745 integer_overflow (pfile
);
751 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
753 top
->value
= v1
% v2
;
758 top
->value
= v1
|| v2
;
760 if (v1
) skip_evaluation
--;
763 top
->value
= v1
&& v2
;
765 if (!v1
) skip_evaluation
--;
768 if (CPP_PEDANTIC (pfile
))
769 cpp_pedwarn (pfile
, "comma operator in operand of #if");
771 top
->unsignedp
= unsigned2
;
774 SYNTAX_ERROR ("syntax error '?' without following ':'");
776 if (top
[0].op
!= CPP_QUERY
)
777 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
779 if (top
->value
) skip_evaluation
--;
780 top
->value
= top
->value
? v1
: v2
;
781 top
->unsignedp
= unsigned1
| unsigned2
;
784 if (op
.op
!= CPP_CLOSE_PAREN
)
785 SYNTAX_ERROR ("missing ')' in expression");
787 op
.unsignedp
= unsigned2
;
790 /* Reducing this dummy operator indicates we've finished. */
791 if (op
.op
== CPP_CLOSE_PAREN
)
792 SYNTAX_ERROR ("missing '(' in expression");
797 /* Handle short-circuit evaluations. */
798 if (flags
& SHORT_CIRCUIT
)
801 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
803 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
805 if (top
[-1].value
) /* Was '?' condition true? */
814 /* Check we have a left operand iff we need one. */
815 if (flags
& NO_L_OPERAND
)
817 if (top
->flags
& HAVE_VALUE
)
818 SYNTAX_ERROR2 ("missing binary operator before '%s'",
819 op_as_text (pfile
, op
.op
));
823 if (!(top
->flags
& HAVE_VALUE
))
824 SYNTAX_ERROR2 ("operator '%s' has no left operand",
825 op_as_text (pfile
, op
.op
));
828 /* Check for and handle stack overflow. */
832 struct op
*new_stack
;
833 int old_size
= (char *) limit
- (char *) stack
;
834 int new_size
= 2 * old_size
;
835 if (stack
!= init_stack
)
836 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
839 new_stack
= (struct op
*) xmalloc (new_size
);
840 memcpy (new_stack
, stack
, old_size
);
843 top
= (struct op
*) ((char *) new_stack
+ old_size
);
844 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
848 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
853 /* The controlling macro expression is only valid if we called lex 3
854 times: <!> <defined expression> and <EOF>. push_conditional ()
855 checks that we are at top-of-file. */
856 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
857 pfile
->mi_ind_cmacro
= 0;
859 result
= (top
[1].value
!= 0);
862 CPP_ICE ("unbalanced stack in #if");
863 else if (!(top
[1].flags
& HAVE_VALUE
))
865 SYNTAX_ERROR ("#if with no expression");
867 result
= 0; /* Return 0 on syntax error. */
870 /* Free dynamic stack if we allocated one. */
871 if (stack
!= init_stack
)
876 /* Output OP as text for diagnostics. */
877 static const unsigned char *
878 op_as_text (pfile
, op
)
886 return cpp_token_as_text (pfile
, &token
);