PR c++/3637
[official-gcc.git] / gcc / cppexp.c
blobf7fe054b7e0739e5b0fbce5ed9367e958ec45fc9
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
9 later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
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,
32 unsigned int,
33 unsigned HOST_WIDEST_INT));
34 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
35 unsigned 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));
42 struct op
44 enum cpp_ttype op;
45 U_CHAR prio; /* Priority of op. */
46 U_CHAR flags;
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 struct suffix
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 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. */
92 static struct op
93 parse_number (pfile, tok)
94 cpp_reader *pfile;
95 const cpp_token *tok;
97 struct op op;
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;
101 int c = 0, i, nsuff;
102 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
103 int base = 10;
104 int overflow = 0;
105 int digit, largest_digit = 0;
106 const struct suffix *sufftab;
108 op.unsignedp = 0;
110 if (p[0] == '0')
112 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
114 p += 2;
115 base = 16;
117 else
119 p += 1;
120 base = 8;
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));
128 for(; p < end; p++)
130 c = *p;
132 if (ISDIGIT (c)
133 || (base == 16 && ISXDIGIT (c)))
134 digit = hex_value (c);
135 else
136 break;
138 if (largest_digit < digit)
139 largest_digit = digit;
140 nd = n * base + digit;
141 overflow |= MAX_over_base < n || nd < n;
142 n = nd;
145 if (p < end)
147 /* Check for a floating point constant. Note that float constants
148 with an exponent or suffix but no decimal point are technically
149 invalid (C99 6.4.4.2) but accepted elsewhere. */
150 if ((c == '.' || c == 'F' || c == 'f')
151 || (base == 10 && (c == 'E' || c == 'e')
152 && p+1 < end && (p[1] == '+' || p[1] == '-'))
153 || (base == 16 && (c == 'P' || c == 'p')
154 && p+1 < end && (p[1] == '+' || p[1] == '-')))
155 SYNTAX_ERROR ("floating point numbers are not valid in #if");
157 /* Determine the suffix. l means long, and u means unsigned.
158 See the suffix tables, above. */
159 switch (end - p)
161 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
162 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
163 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
164 default: goto invalid_suffix;
167 for (i = 0; i < nsuff; i++)
168 if (memcmp (p, sufftab[i].s, end - p) == 0)
169 break;
170 if (i == nsuff)
171 goto invalid_suffix;
172 op.unsignedp = sufftab[i].u;
174 if (CPP_WTRADITIONAL (pfile)
175 && sufftab[i].u
176 && ! cpp_sys_macro_p (pfile))
177 cpp_warning (pfile, "traditional C rejects the `U' suffix");
178 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
179 && ! CPP_OPTION (pfile, c99))
180 cpp_pedwarn (pfile, "too many 'l' suffixes in integer constant");
183 if (base <= largest_digit)
184 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
186 if (overflow)
187 cpp_pedwarn (pfile, "integer constant out of range");
189 /* If too big to be signed, consider it unsigned. */
190 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
192 if (base == 10)
193 cpp_warning (pfile, "integer constant is so large that it is unsigned");
194 op.unsignedp = 1;
197 op.value = n;
198 op.op = CPP_NUMBER;
199 return op;
201 invalid_suffix:
202 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
203 (int) (end - p), p);
204 syntax_error:
205 op.op = CPP_ERROR;
206 return op;
209 static struct op
210 parse_defined (pfile)
211 cpp_reader *pfile;
213 int paren = 0;
214 cpp_hashnode *node = 0;
215 const cpp_token *token;
216 struct op op;
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)
225 paren = 1;
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\"");
235 node = 0;
238 else
240 cpp_error (pfile, "operator \"defined\" requires an identifier");
241 if (token->flags & NAMED_OP)
243 cpp_token op;
245 op.flags = 0;
246 op.type = token->type;
247 cpp_error (pfile,
248 "(\"%s\" is an alternative token for \"%s\" in C++)",
249 cpp_token_as_text (pfile, token),
250 cpp_token_as_text (pfile, &op));
254 if (!node)
255 op.op = CPP_ERROR;
256 else
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;
262 op.unsignedp = 0;
263 op.op = CPP_NUMBER;
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--;
271 return op;
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 static struct op
280 lex (pfile, skip_evaluation)
281 cpp_reader *pfile;
282 int skip_evaluation;
284 struct op op;
285 const cpp_token *token = cpp_get_token (pfile);
287 switch (token->type)
289 case CPP_NUMBER:
290 return parse_number (pfile, token);
292 case CPP_CHAR:
293 case CPP_WCHAR:
295 unsigned int chars_seen;
297 /* This is always a signed type. */
298 op.unsignedp = 0;
299 op.op = CPP_NUMBER;
300 op.value = cpp_interpret_charconst (pfile, token, 1, 0, &chars_seen);
301 return op;
304 case CPP_STRING:
305 case CPP_WSTRING:
306 SYNTAX_ERROR ("string constants are not valid in #if");
308 case CPP_OTHER:
309 if (ISGRAPH (token->val.c))
310 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
311 else
312 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
314 case CPP_NAME:
315 if (token->val.node == pfile->spec_nodes.n_defined)
316 return parse_defined (pfile);
317 else if (CPP_OPTION (pfile, cplusplus)
318 && (token->val.node == pfile->spec_nodes.n_true
319 || token->val.node == pfile->spec_nodes.n_false))
321 op.op = CPP_NUMBER;
322 op.unsignedp = 0;
323 op.value = (token->val.node == pfile->spec_nodes.n_true);
325 /* Warn about use of true or false in #if when pedantic
326 and stdbool.h has not been included. */
327 if (CPP_PEDANTIC (pfile)
328 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
329 cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
330 NODE_NAME (token->val.node));
331 return op;
333 else
335 op.op = CPP_NUMBER;
336 op.unsignedp = 0;
337 op.value = 0;
339 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
340 cpp_warning (pfile, "\"%s\" is not defined",
341 NODE_NAME (token->val.node));
342 return op;
345 case CPP_HASH:
347 int temp;
349 op.op = CPP_NUMBER;
350 if (_cpp_test_assertion (pfile, &temp))
351 op.op = CPP_ERROR;
352 op.unsignedp = 0;
353 op.value = temp;
354 return op;
357 default:
358 if (((int) token->type > (int) CPP_EQ
359 && (int) token->type < (int) CPP_PLUS_EQ)
360 || token->type == CPP_EOF)
362 op.op = token->type;
363 return op;
366 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
367 cpp_token_as_text (pfile, token));
370 syntax_error:
371 op.op = CPP_ERROR;
372 return op;
375 static void
376 integer_overflow (pfile)
377 cpp_reader *pfile;
379 if (CPP_PEDANTIC (pfile))
380 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
383 static HOST_WIDEST_INT
384 left_shift (pfile, a, unsignedp, b)
385 cpp_reader *pfile;
386 HOST_WIDEST_INT a;
387 unsigned int unsignedp;
388 unsigned HOST_WIDEST_INT b;
390 if (b >= HOST_BITS_PER_WIDEST_INT)
392 if (! unsignedp && a != 0)
393 integer_overflow (pfile);
394 return 0;
396 else if (unsignedp)
397 return (unsigned HOST_WIDEST_INT) a << b;
398 else
400 HOST_WIDEST_INT l = a << b;
401 if (l >> b != a)
402 integer_overflow (pfile);
403 return l;
407 static HOST_WIDEST_INT
408 right_shift (pfile, a, unsignedp, b)
409 cpp_reader *pfile ATTRIBUTE_UNUSED;
410 HOST_WIDEST_INT a;
411 unsigned int unsignedp;
412 unsigned HOST_WIDEST_INT b;
414 if (b >= HOST_BITS_PER_WIDEST_INT)
415 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
416 else if (unsignedp)
417 return (unsigned HOST_WIDEST_INT) a >> b;
418 else
419 return a >> b;
422 /* Operator precedence and flags table.
424 After an operator is returned from the lexer, if it has priority less
425 than or equal to the operator on the top of the stack, we reduce the
426 stack by one operator and repeat the test. Since equal priorities
427 reduce, this is naturally left-associative.
429 We handle right-associative operators by clearing the lower bit of all
430 left-associative operators, and setting it for right-associative ones.
431 After the reduction phase of a new operator, just before it is pushed
432 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
433 during the reduction phase, the current right-associative operator has
434 a priority one greater than any other operator of otherwise equal
435 precedence that has been pushed on the top of the stack. This avoids
436 a reduction pass, and effectively makes the logic right-associative.
438 The remaining cases are '(' and ')'. We handle '(' by skipping the
439 reduction phase completely. ')' is given lower priority than
440 everything else, including '(', effectively forcing a reduction of the
441 parenthesised expression. If there is no matching '(', the stack will
442 be reduced all the way to the beginning, exiting the parser in the
443 same way as the ultra-low priority end-of-expression dummy operator.
444 The exit code checks to see if the operator that caused it is ')', and
445 if so outputs an appropriate error message.
447 The parser assumes all shifted operators require a right operand
448 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
449 These semantics are automatically checked, any extra semantics need to
450 be handled with operator-specific code. */
452 #define FLAG_BITS 8
453 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
454 #define PRIO_SHIFT (FLAG_BITS + 1)
455 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
456 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
458 /* Flags. */
459 #define HAVE_VALUE (1 << 0)
460 #define NO_L_OPERAND (1 << 1)
461 #define NO_R_OPERAND (1 << 2)
462 #define SHORT_CIRCUIT (1 << 3)
464 /* Priority and flag combinations. */
465 #define RIGHT_ASSOC (1 << FLAG_BITS)
466 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
467 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
468 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
469 #define COMMA_PRIO (3 << PRIO_SHIFT)
470 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
471 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
472 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
473 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
474 #define OR_PRIO (8 << PRIO_SHIFT)
475 #define XOR_PRIO (9 << PRIO_SHIFT)
476 #define AND_PRIO (10 << PRIO_SHIFT)
477 #define MINMAX_PRIO (11 << PRIO_SHIFT)
478 #define EQUAL_PRIO (12 << PRIO_SHIFT)
479 #define LESS_PRIO (13 << PRIO_SHIFT)
480 #define SHIFT_PRIO (14 << PRIO_SHIFT)
481 #define PLUS_PRIO (15 << PRIO_SHIFT)
482 #define MUL_PRIO (16 << PRIO_SHIFT)
483 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
485 /* Operator to priority map. Must be in the same order as the first
486 N entries of enum cpp_ttype. */
487 static const short
488 op_to_prio[] =
490 /* EQ */ 0, /* dummy entry - can't happen */
491 /* NOT */ UNARY_PRIO,
492 /* GREATER */ LESS_PRIO,
493 /* LESS */ LESS_PRIO,
494 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
495 /* MINUS */ UNARY_PRIO, /* or binary */
496 /* MULT */ MUL_PRIO,
497 /* DIV */ MUL_PRIO,
498 /* MOD */ MUL_PRIO,
499 /* AND */ AND_PRIO,
500 /* OR */ OR_PRIO,
501 /* XOR */ XOR_PRIO,
502 /* RSHIFT */ SHIFT_PRIO,
503 /* LSHIFT */ SHIFT_PRIO,
504 /* MIN */ MINMAX_PRIO, /* C++ specific */
505 /* MAX */ MINMAX_PRIO, /* extensions */
507 /* COMPL */ UNARY_PRIO,
508 /* AND_AND */ ANDAND_PRIO,
509 /* OR_OR */ OROR_PRIO,
510 /* QUERY */ COND_PRIO,
511 /* COLON */ COLON_PRIO,
512 /* COMMA */ COMMA_PRIO,
513 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
514 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
515 /* EQ_EQ */ EQUAL_PRIO,
516 /* NOT_EQ */ EQUAL_PRIO,
517 /* GREATER_EQ */ LESS_PRIO,
518 /* LESS_EQ */ LESS_PRIO
521 #define COMPARE(OP) \
522 top->unsignedp = 0; \
523 top->value = (unsigned1 | unsigned2) \
524 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
525 : (v1 OP v2)
526 #define EQUALITY(OP) \
527 top->value = v1 OP v2; \
528 top->unsignedp = 0;
529 #define BITWISE(OP) \
530 top->value = v1 OP v2; \
531 top->unsignedp = unsigned1 | unsigned2;
532 #define MINMAX(OP) \
533 top->value = (v1 OP v2) ? v1 : v2; \
534 top->unsignedp = unsigned1 | unsigned2;
535 #define UNARY(OP) \
536 top->value = OP v2; \
537 top->unsignedp = unsigned2; \
538 top->flags |= HAVE_VALUE;
539 #define SHIFT(PSH, MSH) \
540 if (skip_evaluation) \
541 break; \
542 top->unsignedp = unsigned1; \
543 if (v2 < 0 && ! unsigned2) \
544 top->value = MSH (pfile, v1, unsigned1, -v2); \
545 else \
546 top->value = PSH (pfile, v1, unsigned1, v2);
548 /* Parse and evaluate a C expression, reading from PFILE.
549 Returns the truth value of the expression. */
552 _cpp_parse_expr (pfile)
553 cpp_reader *pfile;
555 /* The implementation is an operator precedence parser, i.e. a
556 bottom-up parser, using a stack for not-yet-reduced tokens.
558 The stack base is 'stack', and the current stack pointer is 'top'.
559 There is a stack element for each operator (only),
560 and the most recently pushed operator is 'top->op'.
561 An operand (value) is stored in the 'value' field of the stack
562 element of the operator that precedes it.
563 In that case the 'flags' field has the HAVE_VALUE flag set. */
565 #define INIT_STACK_SIZE 20
566 struct op init_stack[INIT_STACK_SIZE];
567 struct op *stack = init_stack;
568 struct op *limit = stack + INIT_STACK_SIZE;
569 struct op *top = stack + 1;
570 int skip_evaluation = 0;
571 int result;
572 unsigned int lex_count, saw_leading_not;
574 /* Set up detection of #if ! defined(). */
575 pfile->mi_ind_cmacro = 0;
576 saw_leading_not = 0;
577 lex_count = 0;
579 /* We've finished when we try to reduce this. */
580 top->op = CPP_EOF;
581 /* Nifty way to catch missing '('. */
582 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
583 /* Avoid missing right operand checks. */
584 top->flags = NO_R_OPERAND;
586 for (;;)
588 unsigned int prio;
589 unsigned int flags;
590 struct op op;
592 /* Read a token */
593 op = lex (pfile, skip_evaluation);
594 lex_count++;
596 /* If the token is an operand, push its value and get next
597 token. If it is an operator, get its priority and flags, and
598 try to reduce the expression on the stack. */
599 switch (op.op)
601 case CPP_ERROR:
602 goto syntax_error;
603 push_immediate:
604 case CPP_NUMBER:
605 /* Push a value onto the stack. */
606 if (top->flags & HAVE_VALUE)
607 SYNTAX_ERROR ("missing binary operator");
608 top->value = op.value;
609 top->unsignedp = op.unsignedp;
610 top->flags |= HAVE_VALUE;
611 continue;
613 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
615 case CPP_NOT:
616 saw_leading_not = lex_count == 1;
617 prio = op_to_prio[op.op];
618 break;
619 case CPP_PLUS:
620 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
621 /* else unary; fall through */
622 default: prio = op_to_prio[op.op]; break;
625 /* Separate the operator's code into priority and flags. */
626 flags = EXTRACT_FLAGS(prio);
627 prio = EXTRACT_PRIO(prio);
628 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
629 goto skip_reduction;
631 /* Check for reductions. Then push the operator. */
632 while (prio <= top->prio)
634 HOST_WIDEST_INT v1, v2;
635 unsigned int unsigned1, unsigned2;
637 /* Most operators that can appear on the stack require a
638 right operand. Check this before trying to reduce. */
639 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
641 if (top->op == CPP_OPEN_PAREN)
642 SYNTAX_ERROR ("void expression between '(' and ')'");
643 else
644 SYNTAX_ERROR2 ("operator '%s' has no right operand",
645 op_as_text (pfile, top->op));
648 unsigned2 = top->unsignedp, v2 = top->value;
649 top--;
650 unsigned1 = top->unsignedp, v1 = top->value;
652 /* Now set top->value = (top[1].op)(v1, v2); */
653 switch (top[1].op)
655 default:
656 cpp_ice (pfile, "impossible operator '%s'",
657 op_as_text (pfile, top[1].op));
658 goto syntax_error;
660 case CPP_NOT: UNARY(!); break;
661 case CPP_COMPL: UNARY(~); break;
662 case CPP_LESS: COMPARE(<); break;
663 case CPP_GREATER: COMPARE(>); break;
664 case CPP_LESS_EQ: COMPARE(<=); break;
665 case CPP_GREATER_EQ: COMPARE(>=); break;
666 case CPP_EQ_EQ: EQUALITY(==); break;
667 case CPP_NOT_EQ: EQUALITY(!=); break;
668 case CPP_AND: BITWISE(&); break;
669 case CPP_XOR: BITWISE(^); break;
670 case CPP_OR: BITWISE(|); break;
671 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
672 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
673 case CPP_MIN: MINMAX(<); break;
674 case CPP_MAX: MINMAX(>); break;
676 case CPP_PLUS:
677 if (!(top->flags & HAVE_VALUE))
679 /* Can't use UNARY(+) because K+R C did not have unary
680 plus. Can't use UNARY() because some compilers object
681 to the empty argument. */
682 top->value = v2;
683 top->unsignedp = unsigned2;
684 top->flags |= HAVE_VALUE;
686 if (CPP_WTRADITIONAL (pfile))
687 cpp_warning (pfile,
688 "traditional C rejects the unary plus operator");
690 else
692 top->value = v1 + v2;
693 top->unsignedp = unsigned1 | unsigned2;
694 if (! top->unsignedp && ! skip_evaluation
695 && ! possible_sum_sign (v1, v2, top->value))
696 integer_overflow (pfile);
698 break;
699 case CPP_MINUS:
700 if (!(top->flags & HAVE_VALUE))
702 UNARY(-);
703 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
704 integer_overflow (pfile);
706 else
707 { /* Binary '-' */
708 top->value = v1 - v2;
709 top->unsignedp = unsigned1 | unsigned2;
710 if (! top->unsignedp && ! skip_evaluation
711 && ! possible_sum_sign (top->value, v2, v1))
712 integer_overflow (pfile);
714 break;
715 case CPP_MULT:
716 top->unsignedp = unsigned1 | unsigned2;
717 if (top->unsignedp)
718 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
719 else if (!skip_evaluation)
721 top->value = v1 * v2;
722 if (v1 && (top->value / v1 != v2
723 || (top->value & v1 & v2) < 0))
724 integer_overflow (pfile);
726 break;
727 case CPP_DIV:
728 case CPP_MOD:
729 if (skip_evaluation)
730 break;
731 if (v2 == 0)
732 SYNTAX_ERROR ("division by zero in #if");
733 top->unsignedp = unsigned1 | unsigned2;
734 if (top[1].op == CPP_DIV)
736 if (top->unsignedp)
737 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
738 else
740 top->value = v1 / v2;
741 if ((top->value & v1 & v2) < 0)
742 integer_overflow (pfile);
745 else
747 if (top->unsignedp)
748 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
749 else
750 top->value = v1 % v2;
752 break;
754 case CPP_OR_OR:
755 top->value = v1 || v2;
756 top->unsignedp = 0;
757 if (v1) skip_evaluation--;
758 break;
759 case CPP_AND_AND:
760 top->value = v1 && v2;
761 top->unsignedp = 0;
762 if (!v1) skip_evaluation--;
763 break;
764 case CPP_COMMA:
765 if (CPP_PEDANTIC (pfile))
766 cpp_pedwarn (pfile, "comma operator in operand of #if");
767 top->value = v2;
768 top->unsignedp = unsigned2;
769 break;
770 case CPP_QUERY:
771 SYNTAX_ERROR ("syntax error '?' without following ':'");
772 case CPP_COLON:
773 if (top[0].op != CPP_QUERY)
774 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
775 top--;
776 if (top->value) skip_evaluation--;
777 top->value = top->value ? v1 : v2;
778 top->unsignedp = unsigned1 | unsigned2;
779 break;
780 case CPP_OPEN_PAREN:
781 if (op.op != CPP_CLOSE_PAREN)
782 SYNTAX_ERROR ("missing ')' in expression");
783 op.value = v2;
784 op.unsignedp = unsigned2;
785 goto push_immediate;
786 case CPP_EOF:
787 /* Reducing this dummy operator indicates we've finished. */
788 if (op.op == CPP_CLOSE_PAREN)
789 SYNTAX_ERROR ("missing '(' in expression");
790 goto done;
794 /* Handle short-circuit evaluations. */
795 if (flags & SHORT_CIRCUIT)
796 switch (op.op)
798 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
799 case CPP_AND_AND:
800 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
801 case CPP_COLON:
802 if (top[-1].value) /* Was '?' condition true? */
803 skip_evaluation++;
804 else
805 skip_evaluation--;
806 default:
807 break;
810 skip_reduction:
811 /* Check we have a left operand iff we need one. */
812 if (flags & NO_L_OPERAND)
814 if (top->flags & HAVE_VALUE)
815 SYNTAX_ERROR2 ("missing binary operator before '%s'",
816 op_as_text (pfile, op.op));
818 else
820 if (!(top->flags & HAVE_VALUE))
821 SYNTAX_ERROR2 ("operator '%s' has no left operand",
822 op_as_text (pfile, op.op));
825 /* Check for and handle stack overflow. */
826 top++;
827 if (top == limit)
829 struct op *new_stack;
830 int old_size = (char *) limit - (char *) stack;
831 int new_size = 2 * old_size;
832 if (stack != init_stack)
833 new_stack = (struct op *) xrealloc (stack, new_size);
834 else
836 new_stack = (struct op *) xmalloc (new_size);
837 memcpy (new_stack, stack, old_size);
839 stack = new_stack;
840 top = (struct op *) ((char *) new_stack + old_size);
841 limit = (struct op *) ((char *) new_stack + new_size);
844 top->flags = flags;
845 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
846 top->op = op.op;
849 done:
850 /* The controlling macro expression is only valid if we called lex 3
851 times: <!> <defined expression> and <EOF>. push_conditional ()
852 checks that we are at top-of-file. */
853 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
854 pfile->mi_ind_cmacro = 0;
856 result = (top[1].value != 0);
858 if (top != stack)
859 CPP_ICE ("unbalanced stack in #if");
860 else if (!(top[1].flags & HAVE_VALUE))
862 SYNTAX_ERROR ("#if with no expression");
863 syntax_error:
864 result = 0; /* Return 0 on syntax error. */
867 /* Free dynamic stack if we allocated one. */
868 if (stack != init_stack)
869 free (stack);
870 return result;
873 static const unsigned char *
874 op_as_text (pfile, op)
875 cpp_reader *pfile;
876 enum cpp_ttype op;
878 cpp_token token;
880 token.type = op;
881 token.flags = 0;
882 return cpp_token_as_text (pfile, &token);