2002-04-30 Mark Mitchell <mark@codesourcery.com>
[official-gcc.git] / gcc / cppexp.c
blob914a2070ac4ad69eb1f20cdfcd71af9df5a47c41
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 struct op op;
288 op.op = CPP_NUMBER;
289 op.unsignedp = 0;
291 switch (token->type)
293 case CPP_NUMBER:
294 return parse_number (pfile, token);
296 case CPP_WCHAR:
297 op.unsignedp = WCHAR_UNSIGNED;
298 case CPP_CHAR: /* Always unsigned. */
299 op.value = cpp_interpret_charconst (pfile, token, 1, &temp);
300 break;
302 case CPP_NAME:
303 if (token->val.node == pfile->spec_nodes.n_defined)
304 return parse_defined (pfile);
305 else if (CPP_OPTION (pfile, cplusplus)
306 && (token->val.node == pfile->spec_nodes.n_true
307 || token->val.node == pfile->spec_nodes.n_false))
309 op.value = (token->val.node == pfile->spec_nodes.n_true);
311 /* Warn about use of true or false in #if when pedantic
312 and stdbool.h has not been included. */
313 if (CPP_PEDANTIC (pfile)
314 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
315 cpp_error (pfile, DL_PEDWARN,
316 "ISO C++ does not permit \"%s\" in #if",
317 NODE_NAME (token->val.node));
319 else
321 op.value = 0;
322 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
323 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
324 NODE_NAME (token->val.node));
326 break;
328 default: /* CPP_HASH */
329 if (_cpp_test_assertion (pfile, &temp))
330 op.op = CPP_ERROR;
331 op.value = temp;
334 return op;
337 /* Warn if appropriate on overflow. */
338 static void
339 integer_overflow (pfile)
340 cpp_reader *pfile;
342 if (CPP_PEDANTIC (pfile))
343 cpp_error (pfile, DL_PEDWARN,
344 "integer overflow in preprocessor expression");
347 /* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
348 unsigned. */
349 static HOST_WIDEST_INT
350 left_shift (pfile, a, unsignedp, b)
351 cpp_reader *pfile;
352 HOST_WIDEST_INT a;
353 unsigned int unsignedp;
354 unsigned HOST_WIDEST_INT b;
356 if (b >= HOST_BITS_PER_WIDEST_INT)
358 if (! unsignedp && a != 0)
359 integer_overflow (pfile);
360 return 0;
362 else if (unsignedp)
363 return (unsigned HOST_WIDEST_INT) a << b;
364 else
366 HOST_WIDEST_INT l = a << b;
367 if (l >> b != a)
368 integer_overflow (pfile);
369 return l;
373 /* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
374 unsigned. */
375 static HOST_WIDEST_INT
376 right_shift (pfile, a, unsignedp, b)
377 cpp_reader *pfile ATTRIBUTE_UNUSED;
378 HOST_WIDEST_INT a;
379 unsigned int unsignedp;
380 unsigned HOST_WIDEST_INT b;
382 if (b >= HOST_BITS_PER_WIDEST_INT)
383 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
384 else if (unsignedp)
385 return (unsigned HOST_WIDEST_INT) a >> b;
386 else
387 return a >> b;
390 /* Operator precedence and flags table.
392 After an operator is returned from the lexer, if it has priority less
393 than the operator on the top of the stack, we reduce the stack by one
394 operator and repeat the test. Since equal priorities do not reduce,
395 this is naturally right-associative.
397 We handle left-associative operators by decrementing the priority of
398 just-lexed operators by one, but retaining the priority of operators
399 already on the stack.
401 The remaining cases are '(' and ')'. We handle '(' by skipping the
402 reduction phase completely. ')' is given lower priority than
403 everything else, including '(', effectively forcing a reduction of the
404 parenthesised expression. If there is a matching '(', the routine
405 reduce() exits immediately. If the normal exit route sees a ')', then
406 there cannot have been a matching '(' and an error message is output.
408 The parser assumes all shifted operators require a left operand unless
409 the flag NO_L_OPERAND is set. These semantics are automatic; any
410 extra semantics need to be handled with operator-specific code. */
412 /* Flags. */
413 #define NO_L_OPERAND (1 << 0)
414 #define LEFT_ASSOC (1 << 1)
416 /* Operator to priority map. Must be in the same order as the first
417 N entries of enum cpp_ttype. */
418 static const struct operator
420 uchar prio;
421 uchar flags;
422 } optab[] =
424 /* EQ */ {0, 0}, /* Shouldn't happen. */
425 /* NOT */ {16, NO_L_OPERAND},
426 /* GREATER */ {12, LEFT_ASSOC},
427 /* LESS */ {12, LEFT_ASSOC},
428 /* PLUS */ {14, LEFT_ASSOC},
429 /* MINUS */ {14, LEFT_ASSOC},
430 /* MULT */ {15, LEFT_ASSOC},
431 /* DIV */ {15, LEFT_ASSOC},
432 /* MOD */ {15, LEFT_ASSOC},
433 /* AND */ {9, LEFT_ASSOC},
434 /* OR */ {7, LEFT_ASSOC},
435 /* XOR */ {8, LEFT_ASSOC},
436 /* RSHIFT */ {13, LEFT_ASSOC},
437 /* LSHIFT */ {13, LEFT_ASSOC},
438 /* MIN */ {10, LEFT_ASSOC}, /* C++ specific */
439 /* MAX */ {10, LEFT_ASSOC}, /* extensions */
441 /* COMPL */ {16, NO_L_OPERAND},
442 /* AND_AND */ {6, LEFT_ASSOC},
443 /* OR_OR */ {5, LEFT_ASSOC},
444 /* QUERY */ {3, 0},
445 /* COLON */ {4, LEFT_ASSOC},
446 /* COMMA */ {2, LEFT_ASSOC},
447 /* OPEN_PAREN */ {1, NO_L_OPERAND},
448 /* CLOSE_PAREN */ {0, 0},
449 /* EOF */ {0, 0},
450 /* EQ_EQ */ {11, LEFT_ASSOC},
451 /* NOT_EQ */ {11, LEFT_ASSOC},
452 /* GREATER_EQ */ {12, LEFT_ASSOC},
453 /* LESS_EQ */ {12, LEFT_ASSOC},
454 /* UPLUS */ {16, NO_L_OPERAND},
455 /* UMINUS */ {16, NO_L_OPERAND}
458 #define COMPARE(OP) \
459 top->unsignedp = 0; \
460 top->value = (unsigned1 | unsigned2) \
461 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
462 : (v1 OP v2)
463 #define EQUALITY(OP) \
464 top->value = v1 OP v2; \
465 top->unsignedp = 0;
466 #define BITWISE(OP) \
467 top->value = v1 OP v2; \
468 top->unsignedp = unsigned1 | unsigned2;
469 #define MINMAX(OP) \
470 top->value = (v1 OP v2) ? v1 : v2; \
471 top->unsignedp = unsigned1 | unsigned2;
472 #define UNARY(OP) \
473 top->value = OP v2; \
474 top->unsignedp = unsigned2;
475 #define SHIFT(PSH, MSH) \
476 if (pfile->state.skip_eval) \
477 break; \
478 top->unsignedp = unsigned1; \
479 if (v2 < 0 && ! unsigned2) \
480 top->value = MSH (pfile, v1, unsigned1, -v2); \
481 else \
482 top->value = PSH (pfile, v1, unsigned1, v2);
484 /* Parse and evaluate a C expression, reading from PFILE.
485 Returns the truth value of the expression.
487 The implementation is an operator precedence parser, i.e. a
488 bottom-up parser, using a stack for not-yet-reduced tokens.
490 The stack base is op_stack, and the current stack pointer is 'top'.
491 There is a stack element for each operator (only), and the most
492 recently pushed operator is 'top->op'. An operand (value) is
493 stored in the 'value' field of the stack element of the operator
494 that precedes it. */
495 bool
496 _cpp_parse_expr (pfile)
497 cpp_reader *pfile;
499 struct op *top = pfile->op_stack;
500 const cpp_token *token = NULL, *prev_token;
501 unsigned int lex_count;
502 bool saw_leading_not, want_value = true;
504 pfile->state.skip_eval = 0;
506 /* Set up detection of #if ! defined(). */
507 pfile->mi_ind_cmacro = 0;
508 saw_leading_not = false;
509 lex_count = 0;
511 /* Lowest priority operator prevents further reductions. */
512 top->op = CPP_EOF;
514 for (;;)
516 struct op op;
518 prev_token = token;
519 token = cpp_get_token (pfile);
520 lex_count++;
521 op.op = token->type;
523 switch (op.op)
525 /* These tokens convert into values. */
526 case CPP_NUMBER:
527 case CPP_CHAR:
528 case CPP_WCHAR:
529 case CPP_NAME:
530 case CPP_HASH:
531 if (!want_value)
532 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
533 cpp_token_as_text (pfile, token));
534 want_value = false;
535 op = eval_token (pfile, token);
536 if (op.op == CPP_ERROR)
537 goto syntax_error;
538 top->value = op.value;
539 top->unsignedp = op.unsignedp;
540 continue;
542 case CPP_NOT:
543 saw_leading_not = lex_count == 1;
544 break;
545 case CPP_PLUS:
546 if (want_value)
547 op.op = CPP_UPLUS;
548 break;
549 case CPP_MINUS:
550 if (want_value)
551 op.op = CPP_UMINUS;
552 break;
553 case CPP_OTHER:
554 if (ISGRAPH (token->val.c))
555 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
556 else
557 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
559 default:
560 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
561 SYNTAX_ERROR2 ("token \"%s\" is not valid in #if expressions",
562 cpp_token_as_text (pfile, token));
563 break;
566 /* Check we have a value or operator as appropriate. */
567 if (optab[op.op].flags & NO_L_OPERAND)
569 if (!want_value)
570 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
571 cpp_token_as_text (pfile, token));
573 else if (want_value)
575 /* Ordering here is subtle and intended to favour the
576 missing parenthesis diagnostics over alternatives. */
577 if (op.op == CPP_CLOSE_PAREN)
579 if (top->op == CPP_OPEN_PAREN)
580 SYNTAX_ERROR ("void expression between '(' and ')'");
582 else if (top->op == CPP_EOF)
583 SYNTAX_ERROR ("#if with no expression");
584 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
585 SYNTAX_ERROR2 ("operator '%s' has no right operand",
586 cpp_token_as_text (pfile, prev_token));
589 top = reduce (pfile, top, op.op);
590 if (!top)
591 goto syntax_error;
593 if (op.op == CPP_EOF)
594 break;
596 switch (op.op)
598 case CPP_CLOSE_PAREN:
599 continue;
600 case CPP_OR_OR:
601 if (top->value)
602 pfile->state.skip_eval++;
603 break;
604 case CPP_AND_AND:
605 case CPP_QUERY:
606 if (!top->value)
607 pfile->state.skip_eval++;
608 break;
609 case CPP_COLON:
610 if (top->op != CPP_QUERY)
611 SYNTAX_ERROR (" ':' without preceding '?'");
612 if (top[-1].value) /* Was '?' condition true? */
613 pfile->state.skip_eval++;
614 else
615 pfile->state.skip_eval--;
616 default:
617 break;
620 want_value = true;
622 /* Check for and handle stack overflow. */
623 if (++top == pfile->op_limit)
624 top = _cpp_expand_op_stack (pfile);
626 top->op = op.op;
629 /* The controlling macro expression is only valid if we called lex 3
630 times: <!> <defined expression> and <EOF>. push_conditional ()
631 checks that we are at top-of-file. */
632 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
633 pfile->mi_ind_cmacro = 0;
635 if (top != pfile->op_stack)
637 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
638 syntax_error:
639 return false; /* Return false on syntax error. */
642 return top->value != 0;
645 /* Reduce the operator / value stack if possible, in preparation for
646 pushing operator OP. Returns NULL on error, otherwise the top of
647 the stack. */
648 static struct op *
649 reduce (pfile, top, op)
650 cpp_reader *pfile;
651 struct op *top;
652 enum cpp_ttype op;
654 unsigned int prio;
656 if (op == CPP_OPEN_PAREN)
657 return top;
659 /* Decrement the priority of left-associative operators to force a
660 reduction with operators of otherwise equal priority. */
661 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
662 while (prio < optab[top->op].prio)
664 HOST_WIDEST_INT v1, v2;
665 unsigned int unsigned1, unsigned2;
667 unsigned2 = top->unsignedp, v2 = top->value;
668 top--;
669 unsigned1 = top->unsignedp, v1 = top->value;
671 /* Now set top->value = (top[1].op)(v1, v2); */
672 switch (top[1].op)
674 default:
675 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top[1].op);
676 return 0;
678 case CPP_NOT: UNARY(!); break;
679 case CPP_COMPL: UNARY(~); break;
680 case CPP_LESS: COMPARE(<); break;
681 case CPP_GREATER: COMPARE(>); break;
682 case CPP_LESS_EQ: COMPARE(<=); break;
683 case CPP_GREATER_EQ: COMPARE(>=); break;
684 case CPP_EQ_EQ: EQUALITY(==); break;
685 case CPP_NOT_EQ: EQUALITY(!=); break;
686 case CPP_AND: BITWISE(&); break;
687 case CPP_XOR: BITWISE(^); break;
688 case CPP_OR: BITWISE(|); break;
689 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
690 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
691 case CPP_MIN: MINMAX(<); break;
692 case CPP_MAX: MINMAX(>); break;
694 case CPP_UPLUS:
695 /* Can't use UNARY(+) because K+R C did not have unary
696 plus. Can't use UNARY() because some compilers object
697 to the empty argument. */
698 top->value = v2;
699 top->unsignedp = unsigned2;
700 if (CPP_WTRADITIONAL (pfile))
701 cpp_error (pfile, DL_WARNING,
702 "traditional C rejects the unary plus operator");
703 break;
704 case CPP_UMINUS:
705 UNARY(-);
706 if (!pfile->state.skip_eval && (top->value & v2) < 0 && !unsigned2)
707 integer_overflow (pfile);
708 break;
710 case CPP_PLUS:
711 top->value = v1 + v2;
712 top->unsignedp = unsigned1 | unsigned2;
713 if (! top->unsignedp && ! pfile->state.skip_eval
714 && ! possible_sum_sign (v1, v2, top->value))
715 integer_overflow (pfile);
716 break;
717 case CPP_MINUS:
718 top->value = v1 - v2;
719 top->unsignedp = unsigned1 | unsigned2;
720 if (! top->unsignedp && ! pfile->state.skip_eval
721 && ! possible_sum_sign (top->value, v2, v1))
722 integer_overflow (pfile);
723 break;
724 case CPP_MULT:
725 top->unsignedp = unsigned1 | unsigned2;
726 if (top->unsignedp)
727 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
728 else if (!pfile->state.skip_eval)
730 top->value = v1 * v2;
731 if (v1 && (top->value / v1 != v2
732 || (top->value & v1 & v2) < 0))
733 integer_overflow (pfile);
735 break;
736 case CPP_DIV:
737 case CPP_MOD:
738 if (pfile->state.skip_eval)
739 break;
740 if (v2 == 0)
742 cpp_error (pfile, DL_ERROR, "division by zero in #if");
743 return 0;
745 top->unsignedp = unsigned1 | unsigned2;
746 if (top[1].op == CPP_DIV)
748 if (top->unsignedp)
749 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
750 else
752 top->value = v1 / v2;
753 if ((top->value & v1 & v2) < 0)
754 integer_overflow (pfile);
757 else
759 if (top->unsignedp)
760 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
761 else
762 top->value = v1 % v2;
764 break;
766 case CPP_OR_OR:
767 top->value = v1 || v2;
768 top->unsignedp = 0;
769 if (v1) pfile->state.skip_eval--;
770 break;
771 case CPP_AND_AND:
772 top->value = v1 && v2;
773 top->unsignedp = 0;
774 if (!v1) pfile->state.skip_eval--;
775 break;
776 case CPP_COMMA:
777 if (CPP_PEDANTIC (pfile))
778 cpp_error (pfile, DL_PEDWARN,
779 "comma operator in operand of #if");
780 top->value = v2;
781 top->unsignedp = unsigned2;
782 break;
783 case CPP_QUERY:
784 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
785 return 0;
786 case CPP_COLON:
787 top--;
788 if (top->value) pfile->state.skip_eval--;
789 top->value = top->value ? v1 : v2;
790 top->unsignedp = unsigned1 | unsigned2;
791 break;
792 case CPP_OPEN_PAREN:
793 if (op != CPP_CLOSE_PAREN)
795 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
796 return 0;
798 top->value = v2;
799 top->unsignedp = unsigned2;
800 return top;
804 if (op == CPP_CLOSE_PAREN)
806 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
807 return 0;
810 return top;
813 /* Returns the position of the old top of stack after expansion. */
814 struct op *
815 _cpp_expand_op_stack (pfile)
816 cpp_reader *pfile;
818 size_t n = (size_t) (pfile->op_limit - pfile->op_stack);
820 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
821 (n * 2 + 20) * sizeof (struct op));
823 return pfile->op_stack + n;