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)
71 const struct suffix vsuf_1
[] = {
72 { "u", 1, 0 }, { "U", 1, 0 },
73 { "l", 0, 1 }, { "L", 0, 1 }
76 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 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 an integer for #if. Accepts decimal, hex, or
89 octal with or without size suffixes. Returned op is CPP_ERROR on
90 error, otherwise it is a CPP_NUMBER. */
93 parse_number (pfile
, tok
)
98 const U_CHAR
*start
= tok
->val
.str
.text
;
99 const U_CHAR
*end
= start
+ tok
->val
.str
.len
;
100 const U_CHAR
*p
= start
;
102 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
105 int digit
, largest_digit
= 0;
106 const struct suffix
*sufftab
;
112 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
124 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
125 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
126 / ((unsigned HOST_WIDEST_INT
) base
));
132 if (c
>= '0' && c
<= '9')
134 /* We believe that in all live character sets, a-f are
135 consecutive, and so are A-F. */
136 else if (base
== 16 && c
>= 'a' && c
<= 'f')
137 digit
= c
- 'a' + 10;
138 else if (base
== 16 && c
>= 'A' && c
<= 'F')
139 digit
= c
- 'A' + 10;
143 if (largest_digit
< digit
)
144 largest_digit
= digit
;
145 nd
= n
* base
+ digit
;
146 overflow
|= MAX_over_base
< n
|| nd
< n
;
152 /* Check for a floating point constant. Note that float constants
153 with an exponent or suffix but no decimal point are technically
154 invalid (C99 6.4.4.2) but accepted elsewhere. */
155 if ((c
== '.' || c
== 'F' || c
== 'f')
156 || (base
== 10 && (c
== 'E' || c
== 'e')
157 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-'))
158 || (base
== 16 && (c
== 'P' || c
== 'p')
159 && p
+1 < end
&& (p
[1] == '+' || p
[1] == '-')))
160 SYNTAX_ERROR ("floating point numbers are not valid in #if");
162 /* Determine the suffix. l means long, and u means unsigned.
163 See the suffix tables, above. */
166 case 1: sufftab
= vsuf_1
; nsuff
= Nsuff(vsuf_1
); break;
167 case 2: sufftab
= vsuf_2
; nsuff
= Nsuff(vsuf_2
); break;
168 case 3: sufftab
= vsuf_3
; nsuff
= Nsuff(vsuf_3
); break;
169 default: goto invalid_suffix
;
172 for (i
= 0; i
< nsuff
; i
++)
173 if (memcmp (p
, sufftab
[i
].s
, end
- p
) == 0)
177 op
.unsignedp
= sufftab
[i
].u
;
179 if (CPP_WTRADITIONAL (pfile
)
181 && ! cpp_sys_macro_p (pfile
))
182 cpp_warning (pfile
, "traditional C rejects the `U' suffix");
183 if (sufftab
[i
].l
== 2 && CPP_OPTION (pfile
, pedantic
)
184 && ! CPP_OPTION (pfile
, c99
))
185 cpp_pedwarn (pfile
, "too many 'l' suffixes in integer constant");
188 if (base
<= largest_digit
)
189 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
192 cpp_pedwarn (pfile
, "integer constant out of range");
194 /* If too big to be signed, consider it unsigned. */
195 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
198 cpp_warning (pfile
, "integer constant is so large that it is unsigned");
207 cpp_error (pfile
, "invalid suffix '%.*s' on integer constant",
215 parse_defined (pfile
)
219 cpp_hashnode
*node
= 0;
223 /* Don't expand macros. */
224 pfile
->state
.prevent_expansion
++;
226 cpp_get_token (pfile
, &token
);
227 if (token
.type
== CPP_OPEN_PAREN
)
230 cpp_get_token (pfile
, &token
);
233 if (token
.type
== CPP_NAME
)
235 node
= token
.val
.node
;
238 cpp_get_token (pfile
, &token
);
239 if (token
.type
!= CPP_CLOSE_PAREN
)
241 cpp_error (pfile
, "missing ')' after \"defined\"");
248 cpp_error (pfile
, "operator \"defined\" requires an identifier");
249 if (token
.flags
& NAMED_OP
)
254 op
.type
= token
.type
;
256 "(\"%s\" is an alternative token for \"%s\" in C++)",
257 cpp_token_as_text (pfile
, &token
),
258 cpp_token_as_text (pfile
, &op
));
266 op
.value
= node
->type
== NT_MACRO
;
270 /* A possible controlling macro of the form #if !defined ().
271 _cpp_parse_expr checks there was no other junk on the line. */
272 pfile
->mi_ind_cmacro
= node
;
275 pfile
->state
.prevent_expansion
--;
279 /* Read a token. The returned type is CPP_NUMBER for a valid number
280 (an interpreted preprocessing number or character constant, or the
281 result of the "defined" or "#" operators), CPP_ERROR on error,
282 CPP_EOF, or the type of an operator token. */
285 lex (pfile
, skip_evaluation
, token
)
292 cpp_get_token (pfile
, token
);
297 return parse_number (pfile
, token
);
302 unsigned int chars_seen
;
304 /* This is always a signed type. */
307 op
.value
= cpp_interpret_charconst (pfile
, token
, 1, 0, &chars_seen
);
313 SYNTAX_ERROR ("string constants are not valid in #if");
316 if (ISGRAPH (token
->val
.c
))
317 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
319 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
322 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
324 if (pfile
->context
->prev
&& CPP_PEDANTIC (pfile
))
325 cpp_pedwarn (pfile
, "\"defined\" operator appears during macro expansion");
327 return parse_defined (pfile
);
329 else if (CPP_OPTION (pfile
, cplusplus
)
330 && (token
->val
.node
== pfile
->spec_nodes
.n_true
331 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
335 op
.value
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
337 /* Warn about use of true or false in #if when pedantic
338 and stdbool.h has not been included. */
339 if (CPP_PEDANTIC (pfile
)
340 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
341 cpp_pedwarn (pfile
, "ISO C++ does not permit \"%s\" in #if",
342 NODE_NAME (token
->val
.node
));
351 if (CPP_OPTION (pfile
, warn_undef
) && !skip_evaluation
)
352 cpp_warning (pfile
, "\"%s\" is not defined",
353 NODE_NAME (token
->val
.node
));
362 if (_cpp_test_assertion (pfile
, &temp
))
370 if (((int) token
->type
> (int) CPP_EQ
371 && (int) token
->type
< (int) CPP_PLUS_EQ
)
372 || token
->type
== CPP_EOF
)
378 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
379 cpp_token_as_text (pfile
, token
));
388 integer_overflow (pfile
)
391 if (CPP_PEDANTIC (pfile
))
392 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
395 static HOST_WIDEST_INT
396 left_shift (pfile
, a
, unsignedp
, b
)
399 unsigned int unsignedp
;
400 unsigned HOST_WIDEST_INT b
;
402 if (b
>= HOST_BITS_PER_WIDEST_INT
)
404 if (! unsignedp
&& a
!= 0)
405 integer_overflow (pfile
);
409 return (unsigned HOST_WIDEST_INT
) a
<< b
;
412 HOST_WIDEST_INT l
= a
<< b
;
414 integer_overflow (pfile
);
419 static HOST_WIDEST_INT
420 right_shift (pfile
, a
, unsignedp
, b
)
421 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
423 unsigned int unsignedp
;
424 unsigned HOST_WIDEST_INT b
;
426 if (b
>= HOST_BITS_PER_WIDEST_INT
)
427 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
429 return (unsigned HOST_WIDEST_INT
) a
>> b
;
434 /* Operator precedence and flags table.
436 After an operator is returned from the lexer, if it has priority less
437 than or equal to the operator on the top of the stack, we reduce the
438 stack by one operator and repeat the test. Since equal priorities
439 reduce, this is naturally left-associative.
441 We handle right-associative operators by clearing the lower bit of all
442 left-associative operators, and setting it for right-associative ones.
443 After the reduction phase of a new operator, just before it is pushed
444 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
445 during the reduction phase, the current right-associative operator has
446 a priority one greater than any other operator of otherwise equal
447 precedence that has been pushed on the top of the stack. This avoids
448 a reduction pass, and effectively makes the logic right-associative.
450 The remaining cases are '(' and ')'. We handle '(' by skipping the
451 reduction phase completely. ')' is given lower priority than
452 everything else, including '(', effectively forcing a reduction of the
453 parenthesised expression. If there is no matching '(', the stack will
454 be reduced all the way to the beginning, exiting the parser in the
455 same way as the ultra-low priority end-of-expression dummy operator.
456 The exit code checks to see if the operator that caused it is ')', and
457 if so outputs an appropriate error message.
459 The parser assumes all shifted operators require a right operand
460 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
461 These semantics are automatically checked, any extra semantics need to
462 be handled with operator-specific code. */
465 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
466 #define PRIO_SHIFT (FLAG_BITS + 1)
467 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
468 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
471 #define HAVE_VALUE (1 << 0)
472 #define NO_L_OPERAND (1 << 1)
473 #define NO_R_OPERAND (1 << 2)
474 #define SHORT_CIRCUIT (1 << 3)
476 /* Priority and flag combinations. */
477 #define RIGHT_ASSOC (1 << FLAG_BITS)
478 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
479 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
480 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
481 #define COMMA_PRIO (3 << PRIO_SHIFT)
482 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
483 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
484 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
485 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
486 #define OR_PRIO (8 << PRIO_SHIFT)
487 #define XOR_PRIO (9 << PRIO_SHIFT)
488 #define AND_PRIO (10 << PRIO_SHIFT)
489 #define MINMAX_PRIO (11 << PRIO_SHIFT)
490 #define EQUAL_PRIO (12 << PRIO_SHIFT)
491 #define LESS_PRIO (13 << PRIO_SHIFT)
492 #define SHIFT_PRIO (14 << PRIO_SHIFT)
493 #define PLUS_PRIO (15 << PRIO_SHIFT)
494 #define MUL_PRIO (16 << PRIO_SHIFT)
495 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
497 /* Operator to priority map. Must be in the same order as the first
498 N entries of enum cpp_ttype. */
502 /* EQ */ 0, /* dummy entry - can't happen */
503 /* NOT */ UNARY_PRIO
,
504 /* GREATER */ LESS_PRIO
,
505 /* LESS */ LESS_PRIO
,
506 /* PLUS */ UNARY_PRIO
, /* note these two can be unary */
507 /* MINUS */ UNARY_PRIO
, /* or binary */
514 /* RSHIFT */ SHIFT_PRIO
,
515 /* LSHIFT */ SHIFT_PRIO
,
516 /* MIN */ MINMAX_PRIO
, /* C++ specific */
517 /* MAX */ MINMAX_PRIO
, /* extensions */
519 /* COMPL */ UNARY_PRIO
,
520 /* AND_AND */ ANDAND_PRIO
,
521 /* OR_OR */ OROR_PRIO
,
522 /* QUERY */ COND_PRIO
,
523 /* COLON */ COLON_PRIO
,
524 /* COMMA */ COMMA_PRIO
,
525 /* OPEN_PAREN */ OPEN_PAREN_PRIO
,
526 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO
,
527 /* EQ_EQ */ EQUAL_PRIO
,
528 /* NOT_EQ */ EQUAL_PRIO
,
529 /* GREATER_EQ */ LESS_PRIO
,
530 /* LESS_EQ */ LESS_PRIO
533 #define COMPARE(OP) \
534 top->unsignedp = 0; \
535 top->value = (unsigned1 | unsigned2) \
536 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
538 #define EQUALITY(OP) \
539 top->value = v1 OP v2; \
541 #define BITWISE(OP) \
542 top->value = v1 OP v2; \
543 top->unsignedp = unsigned1 | unsigned2;
545 top->value = (v1 OP v2) ? v1 : v2; \
546 top->unsignedp = unsigned1 | unsigned2;
548 top->value = OP v2; \
549 top->unsignedp = unsigned2; \
550 top->flags |= HAVE_VALUE;
551 #define SHIFT(PSH, MSH) \
552 if (skip_evaluation) \
554 top->unsignedp = unsigned1; \
555 if (v2 < 0 && ! unsigned2) \
556 top->value = MSH (pfile, v1, unsigned1, -v2); \
558 top->value = PSH (pfile, v1, unsigned1, v2);
560 /* Parse and evaluate a C expression, reading from PFILE.
561 Returns the truth value of the expression. */
564 _cpp_parse_expr (pfile
)
567 /* The implementation is an operator precedence parser, i.e. a
568 bottom-up parser, using a stack for not-yet-reduced tokens.
570 The stack base is 'stack', and the current stack pointer is 'top'.
571 There is a stack element for each operator (only),
572 and the most recently pushed operator is 'top->op'.
573 An operand (value) is stored in the 'value' field of the stack
574 element of the operator that precedes it.
575 In that case the 'flags' field has the HAVE_VALUE flag set. */
577 #define INIT_STACK_SIZE 20
578 struct op init_stack
[INIT_STACK_SIZE
];
579 struct op
*stack
= init_stack
;
580 struct op
*limit
= stack
+ INIT_STACK_SIZE
;
582 register struct op
*top
= stack
+ 1;
583 int skip_evaluation
= 0;
585 unsigned int lex_count
, saw_leading_not
;
587 /* Set up detection of #if ! defined(). */
588 pfile
->mi_ind_cmacro
= 0;
592 /* We've finished when we try to reduce this. */
594 /* Nifty way to catch missing '('. */
595 top
->prio
= EXTRACT_PRIO(CLOSE_PAREN_PRIO
);
596 /* Avoid missing right operand checks. */
597 top
->flags
= NO_R_OPERAND
;
606 op
= lex (pfile
, skip_evaluation
, &token
);
609 /* If the token is an operand, push its value and get next
610 token. If it is an operator, get its priority and flags, and
611 try to reduce the expression on the stack. */
618 /* Push a value onto the stack. */
619 if (top
->flags
& HAVE_VALUE
)
620 SYNTAX_ERROR ("missing binary operator");
621 top
->value
= op
.value
;
622 top
->unsignedp
= op
.unsignedp
;
623 top
->flags
|= HAVE_VALUE
;
626 case CPP_EOF
: prio
= FORCE_REDUCE_PRIO
; break;
629 saw_leading_not
= lex_count
== 1;
630 prio
= op_to_prio
[op
.op
];
633 case CPP_MINUS
: prio
= PLUS_PRIO
; if (top
->flags
& HAVE_VALUE
) break;
634 /* else unary; fall through */
635 default: prio
= op_to_prio
[op
.op
]; break;
638 /* Separate the operator's code into priority and flags. */
639 flags
= EXTRACT_FLAGS(prio
);
640 prio
= EXTRACT_PRIO(prio
);
641 if (prio
== EXTRACT_PRIO(OPEN_PAREN_PRIO
))
644 /* Check for reductions. Then push the operator. */
645 while (prio
<= top
->prio
)
647 HOST_WIDEST_INT v1
, v2
;
648 unsigned int unsigned1
, unsigned2
;
650 /* Most operators that can appear on the stack require a
651 right operand. Check this before trying to reduce. */
652 if ((top
->flags
& (HAVE_VALUE
| NO_R_OPERAND
)) == 0)
654 if (top
->op
== CPP_OPEN_PAREN
)
655 SYNTAX_ERROR ("void expression between '(' and ')'");
657 SYNTAX_ERROR2 ("operator '%s' has no right operand",
658 op_as_text (pfile
, top
->op
));
661 unsigned2
= top
->unsignedp
, v2
= top
->value
;
663 unsigned1
= top
->unsignedp
, v1
= top
->value
;
665 /* Now set top->value = (top[1].op)(v1, v2); */
669 cpp_ice (pfile
, "impossible operator '%s'",
670 op_as_text (pfile
, top
[1].op
));
673 case CPP_NOT
: UNARY(!); break;
674 case CPP_COMPL
: UNARY(~); break;
675 case CPP_LESS
: COMPARE(<); break;
676 case CPP_GREATER
: COMPARE(>); break;
677 case CPP_LESS_EQ
: COMPARE(<=); break;
678 case CPP_GREATER_EQ
: COMPARE(>=); break;
679 case CPP_EQ_EQ
: EQUALITY(==); break;
680 case CPP_NOT_EQ
: EQUALITY(!=); break;
681 case CPP_AND
: BITWISE(&); break;
682 case CPP_XOR
: BITWISE(^); break;
683 case CPP_OR
: BITWISE(|); break;
684 case CPP_LSHIFT
: SHIFT(left_shift
, right_shift
); break;
685 case CPP_RSHIFT
: SHIFT(right_shift
, left_shift
); break;
686 case CPP_MIN
: MINMAX(<); break;
687 case CPP_MAX
: MINMAX(>); break;
690 if (!(top
->flags
& HAVE_VALUE
))
692 /* Can't use UNARY(+) because K+R C did not have unary
693 plus. Can't use UNARY() because some compilers object
694 to the empty argument. */
696 top
->unsignedp
= unsigned2
;
697 top
->flags
|= HAVE_VALUE
;
699 if (CPP_WTRADITIONAL (pfile
))
701 "traditional C rejects the unary plus operator");
705 top
->value
= v1
+ v2
;
706 top
->unsignedp
= unsigned1
| unsigned2
;
707 if (! top
->unsignedp
&& ! skip_evaluation
708 && ! possible_sum_sign (v1
, v2
, top
->value
))
709 integer_overflow (pfile
);
713 if (!(top
->flags
& HAVE_VALUE
))
716 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
717 integer_overflow (pfile
);
721 top
->value
= v1
- v2
;
722 top
->unsignedp
= unsigned1
| unsigned2
;
723 if (! top
->unsignedp
&& ! skip_evaluation
724 && ! possible_sum_sign (top
->value
, v2
, v1
))
725 integer_overflow (pfile
);
729 top
->unsignedp
= unsigned1
| unsigned2
;
731 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
732 else if (!skip_evaluation
)
734 top
->value
= v1
* v2
;
735 if (v1
&& (top
->value
/ v1
!= v2
736 || (top
->value
& v1
& v2
) < 0))
737 integer_overflow (pfile
);
745 SYNTAX_ERROR ("division by zero in #if");
746 top
->unsignedp
= unsigned1
| unsigned2
;
747 if (top
[1].op
== CPP_DIV
)
750 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
753 top
->value
= v1
/ v2
;
754 if ((top
->value
& v1
& v2
) < 0)
755 integer_overflow (pfile
);
761 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
763 top
->value
= v1
% v2
;
768 top
->value
= v1
|| v2
;
770 if (v1
) skip_evaluation
--;
773 top
->value
= v1
&& v2
;
775 if (!v1
) skip_evaluation
--;
778 if (CPP_PEDANTIC (pfile
))
779 cpp_pedwarn (pfile
, "comma operator in operand of #if");
781 top
->unsignedp
= unsigned2
;
784 SYNTAX_ERROR ("syntax error '?' without following ':'");
786 if (top
[0].op
!= CPP_QUERY
)
787 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
789 if (top
->value
) skip_evaluation
--;
790 top
->value
= top
->value
? v1
: v2
;
791 top
->unsignedp
= unsigned1
| unsigned2
;
794 if (op
.op
!= CPP_CLOSE_PAREN
)
795 SYNTAX_ERROR ("missing ')' in expression");
797 op
.unsignedp
= unsigned2
;
800 /* Reducing this dummy operator indicates we've finished. */
801 if (op
.op
== CPP_CLOSE_PAREN
)
802 SYNTAX_ERROR ("missing '(' in expression");
807 /* Handle short-circuit evaluations. */
808 if (flags
& SHORT_CIRCUIT
)
811 case CPP_OR_OR
: if (top
->value
) skip_evaluation
++; break;
813 case CPP_QUERY
: if (!top
->value
) skip_evaluation
++; break;
815 if (top
[-1].value
) /* Was '?' condition true? */
824 /* Check we have a left operand iff we need one. */
825 if (flags
& NO_L_OPERAND
)
827 if (top
->flags
& HAVE_VALUE
)
828 SYNTAX_ERROR2 ("missing binary operator before '%s'",
829 op_as_text (pfile
, top
->op
));
833 if (!(top
->flags
& HAVE_VALUE
))
834 SYNTAX_ERROR2 ("operator '%s' has no left operand",
835 op_as_text (pfile
, top
->op
));
838 /* Check for and handle stack overflow. */
842 struct op
*new_stack
;
843 int old_size
= (char *) limit
- (char *) stack
;
844 int new_size
= 2 * old_size
;
845 if (stack
!= init_stack
)
846 new_stack
= (struct op
*) xrealloc (stack
, new_size
);
849 new_stack
= (struct op
*) xmalloc (new_size
);
850 memcpy (new_stack
, stack
, old_size
);
853 top
= (struct op
*) ((char *) new_stack
+ old_size
);
854 limit
= (struct op
*) ((char *) new_stack
+ new_size
);
858 top
->prio
= prio
& ~EXTRACT_PRIO(RIGHT_ASSOC
);
863 /* The controlling macro expression is only valid if we called lex 3
864 times: <!> <defined expression> and <EOF>. push_conditional ()
865 checks that we are at top-of-file. */
866 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
867 pfile
->mi_ind_cmacro
= 0;
869 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
);