PR opt/3995
[official-gcc.git] / gcc / cppexp.c
blob288cdd406b00818690fc7837c9c1ebe67aa22054
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
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 eval_token PARAMS ((cpp_reader *, const cpp_token *));
40 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
42 struct op
44 enum cpp_ttype op;
45 uchar unsignedp; /* True if value should be treated as unsigned. */
46 HOST_WIDEST_INT value; /* The value logically "right" of op. */
49 /* Token type abuse. There is no "error" token, but we can't get
50 comments in #if, so we can abuse that token type. Similarly,
51 create unary plus and minus operators. */
52 #define CPP_ERROR CPP_COMMENT
53 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
54 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
56 /* With -O2, gcc appears to produce nice code, moving the error
57 message load and subsequent jump completely out of the main path. */
58 #define SYNTAX_ERROR(msgid) \
59 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
60 #define SYNTAX_ERROR2(msgid, arg) \
61 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
63 struct suffix
65 const unsigned char s[4];
66 const unsigned char u;
67 const unsigned char l;
70 static const struct suffix vsuf_1[] = {
71 { "u", 1, 0 }, { "U", 1, 0 },
72 { "l", 0, 1 }, { "L", 0, 1 }
75 static const struct suffix vsuf_2[] = {
76 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
77 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
78 { "ll", 0, 2 }, { "LL", 0, 2 }
81 static const struct suffix vsuf_3[] = {
82 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
83 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
86 /* Parse and convert what is presumably an integer in TOK. Accepts
87 decimal, hex, or octal with or without size suffixes. Returned op
88 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
89 static struct op
90 parse_number (pfile, tok)
91 cpp_reader *pfile;
92 const cpp_token *tok;
94 struct op op;
95 const uchar *start = tok->val.str.text;
96 const uchar *end = start + tok->val.str.len;
97 const uchar *p = start;
98 int c = 0, i, nsuff;
99 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
100 int base = 10;
101 int overflow = 0;
102 int digit, largest_digit = 0;
103 const struct suffix *sufftab;
105 op.unsignedp = 0;
107 if (p[0] == '0')
109 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
111 p += 2;
112 base = 16;
114 else
116 p += 1;
117 base = 8;
121 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
122 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
123 / ((unsigned HOST_WIDEST_INT) base));
125 for(; p < end; p++)
127 c = *p;
129 if (ISDIGIT (c)
130 || (base == 16 && ISXDIGIT (c)))
131 digit = hex_value (c);
132 else
133 break;
135 if (largest_digit < digit)
136 largest_digit = digit;
137 nd = n * base + digit;
138 overflow |= MAX_over_base < n || nd < n;
139 n = nd;
142 if (p < end)
144 /* Check for a floating point constant. Note that float constants
145 with an exponent or suffix but no decimal point are technically
146 invalid (C99 6.4.4.2) but accepted elsewhere. */
147 if ((c == '.' || c == 'F' || c == 'f')
148 || (base == 10 && (c == 'E' || c == 'e')
149 && p+1 < end && (p[1] == '+' || p[1] == '-'))
150 || (base == 16 && (c == 'P' || c == 'p')
151 && p+1 < end && (p[1] == '+' || p[1] == '-')))
152 SYNTAX_ERROR ("floating point numbers are not valid in #if");
154 /* Determine the suffix. l means long, and u means unsigned.
155 See the suffix tables, above. */
156 switch (end - p)
158 case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
159 case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
160 case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
161 default: goto invalid_suffix;
164 for (i = 0; i < nsuff; i++)
165 if (memcmp (p, sufftab[i].s, end - p) == 0)
166 break;
167 if (i == nsuff)
168 goto invalid_suffix;
169 op.unsignedp = sufftab[i].u;
171 if (CPP_WTRADITIONAL (pfile)
172 && sufftab[i].u
173 && ! cpp_sys_macro_p (pfile))
174 cpp_error (pfile, DL_WARNING, "traditional C rejects the `U' suffix");
175 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
176 && ! CPP_OPTION (pfile, c99))
177 cpp_error (pfile, DL_PEDWARN,
178 "too many 'l' suffixes in integer constant");
181 if (base <= largest_digit)
182 cpp_error (pfile, DL_PEDWARN,
183 "integer constant contains digits beyond the radix");
185 if (overflow)
186 cpp_error (pfile, DL_PEDWARN, "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)
191 if (base == 10)
192 cpp_error (pfile, DL_WARNING,
193 "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, DL_ERROR, "invalid suffix '%.*s' on integer constant",
203 (int) (end - p), p);
204 syntax_error:
205 op.op = CPP_ERROR;
206 return op;
209 /* Handle meeting "defined" in a preprocessor expression. */
210 static struct op
211 parse_defined (pfile)
212 cpp_reader *pfile;
214 int paren = 0;
215 cpp_hashnode *node = 0;
216 const cpp_token *token;
217 struct op op;
218 cpp_context *initial_context = pfile->context;
220 /* Don't expand macros. */
221 pfile->state.prevent_expansion++;
223 token = cpp_get_token (pfile);
224 if (token->type == CPP_OPEN_PAREN)
226 paren = 1;
227 token = cpp_get_token (pfile);
230 if (token->type == CPP_NAME)
232 node = token->val.node;
233 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
235 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
236 node = 0;
239 else
241 cpp_error (pfile, DL_ERROR,
242 "operator \"defined\" requires an identifier");
243 if (token->flags & NAMED_OP)
245 cpp_token op;
247 op.flags = 0;
248 op.type = token->type;
249 cpp_error (pfile, DL_ERROR,
250 "(\"%s\" is an alternative token for \"%s\" in C++)",
251 cpp_token_as_text (pfile, token),
252 cpp_token_as_text (pfile, &op));
256 if (!node)
257 op.op = CPP_ERROR;
258 else
260 if (pfile->context != initial_context)
261 cpp_error (pfile, DL_WARNING,
262 "this use of \"defined\" may not be portable");
264 op.value = node->type == NT_MACRO;
265 op.unsignedp = 0;
266 op.op = CPP_NUMBER;
268 /* A possible controlling macro of the form #if !defined ().
269 _cpp_parse_expr checks there was no other junk on the line. */
270 pfile->mi_ind_cmacro = node;
273 pfile->state.prevent_expansion--;
274 return op;
277 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
278 number or character constant, or the result of the "defined" or "#"
279 operators), or CPP_ERROR on error. */
280 static struct op
281 eval_token (pfile, token)
282 cpp_reader *pfile;
283 const cpp_token *token;
285 unsigned int temp;
286 int unsignedp = 0;
287 struct op op;
289 op.op = CPP_NUMBER;
291 switch (token->type)
293 case CPP_NUMBER:
294 return parse_number (pfile, token);
296 case CPP_WCHAR:
297 case CPP_CHAR:
299 cppchar_t result = cpp_interpret_charconst (pfile, token,
300 &temp, &unsignedp);
301 op.value = result;
302 /* Sign-extend the result if necessary. */
303 if (!unsignedp && (cppchar_signed_t) result < 0
304 && sizeof (HOST_WIDEST_INT) > sizeof (cppchar_t))
305 op.value |= ~(((unsigned HOST_WIDEST_INT) 1 << BITS_PER_CPPCHAR_T)
306 - 1);
308 break;
310 case CPP_NAME:
311 if (token->val.node == pfile->spec_nodes.n_defined)
312 return parse_defined (pfile);
313 else if (CPP_OPTION (pfile, cplusplus)
314 && (token->val.node == pfile->spec_nodes.n_true
315 || token->val.node == pfile->spec_nodes.n_false))
317 op.value = (token->val.node == pfile->spec_nodes.n_true);
319 /* Warn about use of true or false in #if when pedantic
320 and stdbool.h has not been included. */
321 if (CPP_PEDANTIC (pfile)
322 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
323 cpp_error (pfile, DL_PEDWARN,
324 "ISO C++ does not permit \"%s\" in #if",
325 NODE_NAME (token->val.node));
327 else
329 op.value = 0;
330 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
331 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
332 NODE_NAME (token->val.node));
334 break;
336 default: /* CPP_HASH */
337 if (_cpp_test_assertion (pfile, &temp))
338 op.op = CPP_ERROR;
339 op.value = temp;
342 op.unsignedp = unsignedp;
343 return op;
346 /* Warn if appropriate on overflow. */
347 static void
348 integer_overflow (pfile)
349 cpp_reader *pfile;
351 if (CPP_PEDANTIC (pfile))
352 cpp_error (pfile, DL_PEDWARN,
353 "integer overflow in preprocessor expression");
356 /* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
357 unsigned. */
358 static HOST_WIDEST_INT
359 left_shift (pfile, a, unsignedp, b)
360 cpp_reader *pfile;
361 HOST_WIDEST_INT a;
362 unsigned int unsignedp;
363 unsigned HOST_WIDEST_INT b;
365 if (b >= HOST_BITS_PER_WIDEST_INT)
367 if (! unsignedp && a != 0)
368 integer_overflow (pfile);
369 return 0;
371 else if (unsignedp)
372 return (unsigned HOST_WIDEST_INT) a << b;
373 else
375 HOST_WIDEST_INT l = a << b;
376 if (l >> b != a)
377 integer_overflow (pfile);
378 return l;
382 /* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
383 unsigned. */
384 static HOST_WIDEST_INT
385 right_shift (pfile, a, unsignedp, b)
386 cpp_reader *pfile ATTRIBUTE_UNUSED;
387 HOST_WIDEST_INT a;
388 unsigned int unsignedp;
389 unsigned HOST_WIDEST_INT b;
391 if (b >= HOST_BITS_PER_WIDEST_INT)
392 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
393 else if (unsignedp)
394 return (unsigned HOST_WIDEST_INT) a >> b;
395 else
396 return a >> b;
399 /* Operator precedence and flags table.
401 After an operator is returned from the lexer, if it has priority less
402 than the operator on the top of the stack, we reduce the stack by one
403 operator and repeat the test. Since equal priorities do not reduce,
404 this is naturally right-associative.
406 We handle left-associative operators by decrementing the priority of
407 just-lexed operators by one, but retaining the priority of operators
408 already on the stack.
410 The remaining cases are '(' and ')'. We handle '(' by skipping the
411 reduction phase completely. ')' is given lower priority than
412 everything else, including '(', effectively forcing a reduction of the
413 parenthesised expression. If there is a matching '(', the routine
414 reduce() exits immediately. If the normal exit route sees a ')', then
415 there cannot have been a matching '(' and an error message is output.
417 The parser assumes all shifted operators require a left operand unless
418 the flag NO_L_OPERAND is set. These semantics are automatic; any
419 extra semantics need to be handled with operator-specific code. */
421 /* Flags. */
422 #define NO_L_OPERAND (1 << 0)
423 #define LEFT_ASSOC (1 << 1)
425 /* Operator to priority map. Must be in the same order as the first
426 N entries of enum cpp_ttype. */
427 static const struct operator
429 uchar prio;
430 uchar flags;
431 } optab[] =
433 /* EQ */ {0, 0}, /* Shouldn't happen. */
434 /* NOT */ {16, NO_L_OPERAND},
435 /* GREATER */ {12, LEFT_ASSOC},
436 /* LESS */ {12, LEFT_ASSOC},
437 /* PLUS */ {14, LEFT_ASSOC},
438 /* MINUS */ {14, LEFT_ASSOC},
439 /* MULT */ {15, LEFT_ASSOC},
440 /* DIV */ {15, LEFT_ASSOC},
441 /* MOD */ {15, LEFT_ASSOC},
442 /* AND */ {9, LEFT_ASSOC},
443 /* OR */ {7, LEFT_ASSOC},
444 /* XOR */ {8, LEFT_ASSOC},
445 /* RSHIFT */ {13, LEFT_ASSOC},
446 /* LSHIFT */ {13, LEFT_ASSOC},
447 /* MIN */ {10, LEFT_ASSOC}, /* C++ specific */
448 /* MAX */ {10, LEFT_ASSOC}, /* extensions */
450 /* COMPL */ {16, NO_L_OPERAND},
451 /* AND_AND */ {6, LEFT_ASSOC},
452 /* OR_OR */ {5, LEFT_ASSOC},
453 /* QUERY */ {3, 0},
454 /* COLON */ {4, LEFT_ASSOC},
455 /* COMMA */ {2, LEFT_ASSOC},
456 /* OPEN_PAREN */ {1, NO_L_OPERAND},
457 /* CLOSE_PAREN */ {0, 0},
458 /* EOF */ {0, 0},
459 /* EQ_EQ */ {11, LEFT_ASSOC},
460 /* NOT_EQ */ {11, LEFT_ASSOC},
461 /* GREATER_EQ */ {12, LEFT_ASSOC},
462 /* LESS_EQ */ {12, LEFT_ASSOC},
463 /* UPLUS */ {16, NO_L_OPERAND},
464 /* UMINUS */ {16, NO_L_OPERAND}
467 #define COMPARE(OP) \
468 top->unsignedp = 0; \
469 top->value = (unsigned1 | unsigned2) \
470 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
471 : (v1 OP v2)
472 #define EQUALITY(OP) \
473 top->value = v1 OP v2; \
474 top->unsignedp = 0;
475 #define BITWISE(OP) \
476 top->value = v1 OP v2; \
477 top->unsignedp = unsigned1 | unsigned2;
478 #define MINMAX(OP) \
479 top->value = (v1 OP v2) ? v1 : v2; \
480 top->unsignedp = unsigned1 | unsigned2;
481 #define UNARY(OP) \
482 top->value = OP v2; \
483 top->unsignedp = unsigned2;
484 #define SHIFT(PSH, MSH) \
485 if (pfile->state.skip_eval) \
486 break; \
487 top->unsignedp = unsigned1; \
488 if (v2 < 0 && ! unsigned2) \
489 top->value = MSH (pfile, v1, unsigned1, -v2); \
490 else \
491 top->value = PSH (pfile, v1, unsigned1, v2);
493 /* Parse and evaluate a C expression, reading from PFILE.
494 Returns the truth value of the expression.
496 The implementation is an operator precedence parser, i.e. a
497 bottom-up parser, using a stack for not-yet-reduced tokens.
499 The stack base is op_stack, and the current stack pointer is 'top'.
500 There is a stack element for each operator (only), and the most
501 recently pushed operator is 'top->op'. An operand (value) is
502 stored in the 'value' field of the stack element of the operator
503 that precedes it. */
504 bool
505 _cpp_parse_expr (pfile)
506 cpp_reader *pfile;
508 struct op *top = pfile->op_stack;
509 const cpp_token *token = NULL, *prev_token;
510 unsigned int lex_count;
511 bool saw_leading_not, want_value = true;
513 pfile->state.skip_eval = 0;
515 /* Set up detection of #if ! defined(). */
516 pfile->mi_ind_cmacro = 0;
517 saw_leading_not = false;
518 lex_count = 0;
520 /* Lowest priority operator prevents further reductions. */
521 top->op = CPP_EOF;
523 for (;;)
525 struct op op;
527 prev_token = token;
528 token = cpp_get_token (pfile);
529 lex_count++;
530 op.op = token->type;
532 switch (op.op)
534 /* These tokens convert into values. */
535 case CPP_NUMBER:
536 case CPP_CHAR:
537 case CPP_WCHAR:
538 case CPP_NAME:
539 case CPP_HASH:
540 if (!want_value)
541 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
542 cpp_token_as_text (pfile, token));
543 want_value = false;
544 op = eval_token (pfile, token);
545 if (op.op == CPP_ERROR)
546 goto syntax_error;
547 top->value = op.value;
548 top->unsignedp = op.unsignedp;
549 continue;
551 case CPP_NOT:
552 saw_leading_not = lex_count == 1;
553 break;
554 case CPP_PLUS:
555 if (want_value)
556 op.op = CPP_UPLUS;
557 break;
558 case CPP_MINUS:
559 if (want_value)
560 op.op = CPP_UMINUS;
561 break;
562 case CPP_OTHER:
563 if (ISGRAPH (token->val.c))
564 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
565 else
566 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
568 default:
569 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
570 SYNTAX_ERROR2 ("token \"%s\" is not valid in #if expressions",
571 cpp_token_as_text (pfile, token));
572 break;
575 /* Check we have a value or operator as appropriate. */
576 if (optab[op.op].flags & NO_L_OPERAND)
578 if (!want_value)
579 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
580 cpp_token_as_text (pfile, token));
582 else if (want_value)
584 /* Ordering here is subtle and intended to favour the
585 missing parenthesis diagnostics over alternatives. */
586 if (op.op == CPP_CLOSE_PAREN)
588 if (top->op == CPP_OPEN_PAREN)
589 SYNTAX_ERROR ("void expression between '(' and ')'");
591 else if (top->op == CPP_EOF)
592 SYNTAX_ERROR ("#if with no expression");
593 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
594 SYNTAX_ERROR2 ("operator '%s' has no right operand",
595 cpp_token_as_text (pfile, prev_token));
598 top = reduce (pfile, top, op.op);
599 if (!top)
600 goto syntax_error;
602 if (op.op == CPP_EOF)
603 break;
605 switch (op.op)
607 case CPP_CLOSE_PAREN:
608 continue;
609 case CPP_OR_OR:
610 if (top->value)
611 pfile->state.skip_eval++;
612 break;
613 case CPP_AND_AND:
614 case CPP_QUERY:
615 if (!top->value)
616 pfile->state.skip_eval++;
617 break;
618 case CPP_COLON:
619 if (top->op != CPP_QUERY)
620 SYNTAX_ERROR (" ':' without preceding '?'");
621 if (top[-1].value) /* Was '?' condition true? */
622 pfile->state.skip_eval++;
623 else
624 pfile->state.skip_eval--;
625 default:
626 break;
629 want_value = true;
631 /* Check for and handle stack overflow. */
632 if (++top == pfile->op_limit)
633 top = _cpp_expand_op_stack (pfile);
635 top->op = op.op;
638 /* The controlling macro expression is only valid if we called lex 3
639 times: <!> <defined expression> and <EOF>. push_conditional ()
640 checks that we are at top-of-file. */
641 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
642 pfile->mi_ind_cmacro = 0;
644 if (top != pfile->op_stack)
646 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
647 syntax_error:
648 return false; /* Return false on syntax error. */
651 return top->value != 0;
654 /* Reduce the operator / value stack if possible, in preparation for
655 pushing operator OP. Returns NULL on error, otherwise the top of
656 the stack. */
657 static struct op *
658 reduce (pfile, top, op)
659 cpp_reader *pfile;
660 struct op *top;
661 enum cpp_ttype op;
663 unsigned int prio;
665 if (op == CPP_OPEN_PAREN)
666 return top;
668 /* Decrement the priority of left-associative operators to force a
669 reduction with operators of otherwise equal priority. */
670 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
671 while (prio < optab[top->op].prio)
673 HOST_WIDEST_INT v1, v2;
674 unsigned int unsigned1, unsigned2;
676 unsigned2 = top->unsignedp, v2 = top->value;
677 top--;
678 unsigned1 = top->unsignedp, v1 = top->value;
680 /* Now set top->value = (top[1].op)(v1, v2); */
681 switch (top[1].op)
683 default:
684 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top[1].op);
685 return 0;
687 case CPP_NOT: UNARY(!); break;
688 case CPP_COMPL: UNARY(~); break;
689 case CPP_LESS: COMPARE(<); break;
690 case CPP_GREATER: COMPARE(>); break;
691 case CPP_LESS_EQ: COMPARE(<=); break;
692 case CPP_GREATER_EQ: COMPARE(>=); break;
693 case CPP_EQ_EQ: EQUALITY(==); break;
694 case CPP_NOT_EQ: EQUALITY(!=); break;
695 case CPP_AND: BITWISE(&); break;
696 case CPP_XOR: BITWISE(^); break;
697 case CPP_OR: BITWISE(|); break;
698 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
699 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
700 case CPP_MIN: MINMAX(<); break;
701 case CPP_MAX: MINMAX(>); break;
703 case CPP_UPLUS:
704 /* Can't use UNARY(+) because K+R C did not have unary
705 plus. Can't use UNARY() because some compilers object
706 to the empty argument. */
707 top->value = v2;
708 top->unsignedp = unsigned2;
709 if (CPP_WTRADITIONAL (pfile))
710 cpp_error (pfile, DL_WARNING,
711 "traditional C rejects the unary plus operator");
712 break;
713 case CPP_UMINUS:
714 UNARY(-);
715 if (!pfile->state.skip_eval && (top->value & v2) < 0 && !unsigned2)
716 integer_overflow (pfile);
717 break;
719 case CPP_PLUS:
720 top->value = v1 + v2;
721 top->unsignedp = unsigned1 | unsigned2;
722 if (! top->unsignedp && ! pfile->state.skip_eval
723 && ! possible_sum_sign (v1, v2, top->value))
724 integer_overflow (pfile);
725 break;
726 case CPP_MINUS:
727 top->value = v1 - v2;
728 top->unsignedp = unsigned1 | unsigned2;
729 if (! top->unsignedp && ! pfile->state.skip_eval
730 && ! possible_sum_sign (top->value, v2, v1))
731 integer_overflow (pfile);
732 break;
733 case CPP_MULT:
734 top->unsignedp = unsigned1 | unsigned2;
735 if (top->unsignedp)
736 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
737 else if (!pfile->state.skip_eval)
739 top->value = v1 * v2;
740 if (v1 && (top->value / v1 != v2
741 || (top->value & v1 & v2) < 0))
742 integer_overflow (pfile);
744 break;
745 case CPP_DIV:
746 case CPP_MOD:
747 if (pfile->state.skip_eval)
748 break;
749 if (v2 == 0)
751 cpp_error (pfile, DL_ERROR, "division by zero in #if");
752 return 0;
754 top->unsignedp = unsigned1 | unsigned2;
755 if (top[1].op == CPP_DIV)
757 if (top->unsignedp)
758 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
759 else
761 top->value = v1 / v2;
762 if ((top->value & v1 & v2) < 0)
763 integer_overflow (pfile);
766 else
768 if (top->unsignedp)
769 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
770 else
771 top->value = v1 % v2;
773 break;
775 case CPP_OR_OR:
776 top->value = v1 || v2;
777 top->unsignedp = 0;
778 if (v1) pfile->state.skip_eval--;
779 break;
780 case CPP_AND_AND:
781 top->value = v1 && v2;
782 top->unsignedp = 0;
783 if (!v1) pfile->state.skip_eval--;
784 break;
785 case CPP_COMMA:
786 if (CPP_PEDANTIC (pfile))
787 cpp_error (pfile, DL_PEDWARN,
788 "comma operator in operand of #if");
789 top->value = v2;
790 top->unsignedp = unsigned2;
791 break;
792 case CPP_QUERY:
793 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
794 return 0;
795 case CPP_COLON:
796 top--;
797 if (top->value) pfile->state.skip_eval--;
798 top->value = top->value ? v1 : v2;
799 top->unsignedp = unsigned1 | unsigned2;
800 break;
801 case CPP_OPEN_PAREN:
802 if (op != CPP_CLOSE_PAREN)
804 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
805 return 0;
807 top->value = v2;
808 top->unsignedp = unsigned2;
809 return top;
813 if (op == CPP_CLOSE_PAREN)
815 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
816 return 0;
819 return top;
822 /* Returns the position of the old top of stack after expansion. */
823 struct op *
824 _cpp_expand_op_stack (pfile)
825 cpp_reader *pfile;
827 size_t n = (size_t) (pfile->op_limit - pfile->op_stack);
829 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
830 (n * 2 + 20) * sizeof (struct op));
832 return pfile->op_stack + n;