OSDN Git Service

* cppexp.c (CPP_UMINUS, CPP_UPLUS): New.
[pf3gnuchains/gcc-fork.git] / gcc / cppexp.c
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.
5
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.
10
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.
15
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.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
25
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)
29
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));
41
42 struct op
43 {
44   enum cpp_ttype op;
45   uchar prio;         /* Priority of op.  */
46   uchar flags;
47   uchar unsignedp;    /* True if value should be treated as unsigned.  */
48   HOST_WIDEST_INT value; /* The value logically "right" of op.  */
49 };
50
51 /* Token type abuse.  There is no "error" token, but we can't get
52    comments in #if, so we can abuse that token type.  Similarly,
53    create unary plus and minus operators.  */
54 #define CPP_ERROR CPP_COMMENT
55 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
56 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
57
58 /* With -O2, gcc appears to produce nice code, moving the error
59    message load and subsequent jump completely out of the main path.  */
60 #define SYNTAX_ERROR(msgid) \
61   do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
62 #define SYNTAX_ERROR2(msgid, arg) \
63   do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
64
65 struct suffix
66 {
67   const unsigned char s[4];
68   const unsigned char u;
69   const unsigned char l;
70 };
71
72 static const struct suffix vsuf_1[] = {
73   { "u", 1, 0 }, { "U", 1, 0 },
74   { "l", 0, 1 }, { "L", 0, 1 }
75 };
76
77 static const struct suffix vsuf_2[] = {
78   { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
79   { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
80   { "ll", 0, 2 }, { "LL", 0, 2 }
81 };
82
83 static const struct suffix vsuf_3[] = {
84   { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
85   { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
86 };
87
88 /* Parse and convert what is presumably an integer in TOK.  Accepts
89    decimal, hex, or octal with or without size suffixes.  Returned op
90    is CPP_ERROR on error, otherwise it is a CPP_NUMBER.  */
91 static struct op
92 parse_number (pfile, tok)
93      cpp_reader *pfile;
94      const cpp_token *tok;
95 {
96   struct op op;
97   const uchar *start = tok->val.str.text;
98   const uchar *end = start + tok->val.str.len;
99   const uchar *p = start;
100   int c = 0, i, nsuff;
101   unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
102   int base = 10;
103   int overflow = 0;
104   int digit, largest_digit = 0;
105   const struct suffix *sufftab;
106
107   op.unsignedp = 0;
108
109   if (p[0] == '0')
110     {
111       if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
112         {
113           p += 2;
114           base = 16;
115         }
116       else
117         {
118           p += 1;
119           base = 8;
120         }
121     }
122
123   /* Some buggy compilers (e.g. MPW C) seem to need both casts.  */
124   MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
125                    / ((unsigned HOST_WIDEST_INT) base));
126
127   for(; p < end; p++)
128     {
129       c = *p;
130
131       if (ISDIGIT (c)
132           || (base == 16 && ISXDIGIT (c)))
133         digit = hex_value (c);
134       else
135         break;
136
137       if (largest_digit < digit)
138         largest_digit = digit;
139       nd = n * base + digit;
140       overflow |= MAX_over_base < n || nd < n;
141       n = nd;
142     }
143
144   if (p < end)
145     {
146       /* Check for a floating point constant.  Note that float constants
147          with an exponent or suffix but no decimal point are technically
148          invalid (C99 6.4.4.2) but accepted elsewhere.  */
149       if ((c == '.' || c == 'F' || c == 'f')
150           || (base == 10 && (c == 'E' || c == 'e')
151               && p+1 < end && (p[1] == '+' || p[1] == '-'))
152           || (base == 16 && (c == 'P' || c == 'p')
153               && p+1 < end && (p[1] == '+' || p[1] == '-')))
154         SYNTAX_ERROR ("floating point numbers are not valid in #if");
155   
156       /* Determine the suffix. l means long, and u means unsigned.
157          See the suffix tables, above.  */
158       switch (end - p)
159         {
160         case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
161         case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
162         case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
163         default: goto invalid_suffix;
164         }
165
166       for (i = 0; i < nsuff; i++)
167         if (memcmp (p, sufftab[i].s, end - p) == 0)
168           break;
169       if (i == nsuff)
170         goto invalid_suffix;
171       op.unsignedp = sufftab[i].u;
172
173       if (CPP_WTRADITIONAL (pfile)
174           && sufftab[i].u
175           && ! cpp_sys_macro_p (pfile))
176         cpp_error (pfile, DL_WARNING, "traditional C rejects the `U' suffix");
177       if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
178           && ! CPP_OPTION (pfile, c99))
179         cpp_error (pfile, DL_PEDWARN,
180                    "too many 'l' suffixes in integer constant");
181     }
182   
183   if (base <= largest_digit)
184     cpp_error (pfile, DL_PEDWARN,
185                "integer constant contains digits beyond the radix");
186
187   if (overflow)
188     cpp_error (pfile, DL_PEDWARN, "integer constant out of range");
189
190   /* If too big to be signed, consider it unsigned.  */
191   else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
192     {
193       if (base == 10)
194         cpp_error (pfile, DL_WARNING,
195                    "integer constant is so large that it is unsigned");
196       op.unsignedp = 1;
197     }
198
199   op.value = n;
200   op.op = CPP_NUMBER;
201   return op;
202
203  invalid_suffix:
204   cpp_error (pfile, DL_ERROR, "invalid suffix '%.*s' on integer constant",
205              (int) (end - p), p);
206  syntax_error:
207   op.op = CPP_ERROR;
208   return op;
209 }
210
211 /* Handle meeting "defined" in a preprocessor expression.  */
212 static struct op
213 parse_defined (pfile)
214      cpp_reader *pfile;
215 {
216   int paren = 0;
217   cpp_hashnode *node = 0;
218   const cpp_token *token;
219   struct op op;
220   cpp_context *initial_context = pfile->context;
221
222   /* Don't expand macros.  */
223   pfile->state.prevent_expansion++;
224
225   token = cpp_get_token (pfile);
226   if (token->type == CPP_OPEN_PAREN)
227     {
228       paren = 1;
229       token = cpp_get_token (pfile);
230     }
231
232   if (token->type == CPP_NAME)
233     {
234       node = token->val.node;
235       if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
236         {
237           cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
238           node = 0;
239         }
240     }
241   else
242     {
243       cpp_error (pfile, DL_ERROR,
244                  "operator \"defined\" requires an identifier");
245       if (token->flags & NAMED_OP)
246         {
247           cpp_token op;
248
249           op.flags = 0;
250           op.type = token->type;
251           cpp_error (pfile, DL_ERROR,
252                      "(\"%s\" is an alternative token for \"%s\" in C++)",
253                      cpp_token_as_text (pfile, token),
254                      cpp_token_as_text (pfile, &op));
255         }
256     }
257
258   if (!node)
259     op.op = CPP_ERROR;
260   else
261     {
262       if (pfile->context != initial_context)
263         cpp_error (pfile, DL_WARNING,
264                    "this use of \"defined\" may not be portable");
265
266       op.value = node->type == NT_MACRO;
267       op.unsignedp = 0;
268       op.op = CPP_NUMBER;
269
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;
273     }
274
275   pfile->state.prevent_expansion--;
276   return op;
277 }
278
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.  */
283 static struct op
284 lex (pfile, skip_evaluation)
285      cpp_reader *pfile;
286      int skip_evaluation;
287 {
288   struct op op;
289   const cpp_token *token = cpp_get_token (pfile);
290
291   switch (token->type)
292     {
293     case CPP_NUMBER:
294       return parse_number (pfile, token);
295
296     case CPP_CHAR:
297     case CPP_WCHAR:
298       {
299         unsigned int chars_seen;
300
301         if (token->type == CPP_CHAR)
302           op.unsignedp = 0;
303         else
304           op.unsignedp = WCHAR_UNSIGNED;
305         op.op = CPP_NUMBER;
306         op.value = cpp_interpret_charconst (pfile, token, 1, &chars_seen);
307         return op;
308       }
309
310     case CPP_STRING:
311     case CPP_WSTRING:
312       SYNTAX_ERROR ("string constants are not valid in #if");
313
314     case CPP_OTHER:
315       if (ISGRAPH (token->val.c))
316         SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
317       else
318         SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
319
320     case CPP_NAME:
321       if (token->val.node == pfile->spec_nodes.n_defined)
322         return parse_defined (pfile);
323       else if (CPP_OPTION (pfile, cplusplus)
324                && (token->val.node == pfile->spec_nodes.n_true
325                    || token->val.node == pfile->spec_nodes.n_false))
326         {
327           op.op = CPP_NUMBER;
328           op.unsignedp = 0;
329           op.value = (token->val.node == pfile->spec_nodes.n_true);
330
331           /* Warn about use of true or false in #if when pedantic
332              and stdbool.h has not been included.  */
333           if (CPP_PEDANTIC (pfile)
334               && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
335             cpp_error (pfile, DL_PEDWARN,
336                        "ISO C++ does not permit \"%s\" in #if",
337                        NODE_NAME (token->val.node));
338           return op;
339         }
340       else
341         {
342           op.op = CPP_NUMBER;
343           op.unsignedp = 0;
344           op.value = 0;
345
346           if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
347             cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
348                        NODE_NAME (token->val.node));
349           return op;
350         }
351
352     case CPP_HASH:
353       {
354         int temp;
355
356         op.op = CPP_NUMBER;
357         if (_cpp_test_assertion (pfile, &temp))
358           op.op = CPP_ERROR;
359         op.unsignedp = 0;
360         op.value = temp;
361         return op;
362       }
363
364     default:
365       if (((int) token->type > (int) CPP_EQ
366            && (int) token->type < (int) CPP_PLUS_EQ)
367           || token->type == CPP_EOF)
368         {
369           op.op = token->type;
370           return op;
371         }
372
373       SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
374                      cpp_token_as_text (pfile, token));
375     }
376
377  syntax_error:
378   op.op = CPP_ERROR;
379   return op;
380 }
381
382 /* Warn if appropriate on overflow.  */
383 static void
384 integer_overflow (pfile)
385      cpp_reader *pfile;
386 {
387   if (CPP_PEDANTIC (pfile))
388     cpp_error (pfile, DL_PEDWARN,
389                "integer overflow in preprocessor expression");
390 }
391
392 /* Handle shifting A left by B bits.  UNSIGNEDP is non-zero if A is
393    unsigned.  */
394 static HOST_WIDEST_INT
395 left_shift (pfile, a, unsignedp, b)
396      cpp_reader *pfile;
397      HOST_WIDEST_INT a;
398      unsigned int unsignedp;
399      unsigned HOST_WIDEST_INT b;
400 {
401   if (b >= HOST_BITS_PER_WIDEST_INT)
402     {
403       if (! unsignedp && a != 0)
404         integer_overflow (pfile);
405       return 0;
406     }
407   else if (unsignedp)
408     return (unsigned HOST_WIDEST_INT) a << b;
409   else
410     {
411       HOST_WIDEST_INT l = a << b;
412       if (l >> b != a)
413         integer_overflow (pfile);
414       return l;
415     }
416 }
417
418 /* Handle shifting A right by B bits.  UNSIGNEDP is non-zero if A is
419    unsigned.  */
420 static HOST_WIDEST_INT
421 right_shift (pfile, a, unsignedp, b)
422      cpp_reader *pfile ATTRIBUTE_UNUSED;
423      HOST_WIDEST_INT a;
424      unsigned int unsignedp;
425      unsigned HOST_WIDEST_INT b;
426 {
427   if (b >= HOST_BITS_PER_WIDEST_INT)
428     return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
429   else if (unsignedp)
430     return (unsigned HOST_WIDEST_INT) a >> b;
431   else
432     return a >> b;
433 }
434 \f
435 /* Operator precedence and flags table.
436
437 After an operator is returned from the lexer, if it has priority less
438 than or equal to the operator on the top of the stack, we reduce the
439 stack by one operator and repeat the test.  Since equal priorities
440 reduce, this is naturally left-associative.
441
442 We handle right-associative operators by clearing the lower bit of all
443 left-associative operators, and setting it for right-associative ones.
444 After the reduction phase of a new operator, just before it is pushed
445 onto the stack, its RIGHT_ASSOC bit is cleared.  The effect is that
446 during the reduction phase, the current right-associative operator has
447 a priority one greater than any other operator of otherwise equal
448 precedence that has been pushed on the top of the stack.  This avoids
449 a reduction pass, and effectively makes the logic right-associative.
450
451 The remaining cases are '(' and ')'.  We handle '(' by skipping the
452 reduction phase completely.  ')' is given lower priority than
453 everything else, including '(', effectively forcing a reduction of the
454 parenthesised expression.  If there is no matching '(', the stack will
455 be reduced all the way to the beginning, exiting the parser in the
456 same way as the ultra-low priority end-of-expression dummy operator.
457 The exit code checks to see if the operator that caused it is ')', and
458 if so outputs an appropriate error message.
459
460 The parser assumes all shifted operators require a left operand unless
461 the flag NO_L_OPERAND is set.  These semantics are automatic; any
462 extra semantics need to be handled with operator-specific code.  */
463
464 #define FLAG_BITS  8
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)
469
470 /* Flags.  */
471 #define NO_L_OPERAND   (1 << 0)
472 #define SHORT_CIRCUIT  (1 << 1)
473
474 /* Priority and flag combinations.  */
475 #define RIGHT_ASSOC         (1 << FLAG_BITS)
476 #define FORCE_REDUCE_PRIO   (0 << PRIO_SHIFT)
477 #define CLOSE_PAREN_PRIO    (1 << PRIO_SHIFT)
478 #define OPEN_PAREN_PRIO    ((2 << PRIO_SHIFT) | NO_L_OPERAND)
479 #define COMMA_PRIO          (3 << PRIO_SHIFT)
480 #define COND_PRIO          ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
481 #define COLON_PRIO         ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
482 #define OROR_PRIO          ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
483 #define ANDAND_PRIO        ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
484 #define OR_PRIO             (8 << PRIO_SHIFT)
485 #define XOR_PRIO            (9 << PRIO_SHIFT)
486 #define AND_PRIO           (10 << PRIO_SHIFT)
487 #define MINMAX_PRIO        (11 << PRIO_SHIFT)
488 #define EQUAL_PRIO         (12 << PRIO_SHIFT)
489 #define LESS_PRIO          (13 << PRIO_SHIFT)
490 #define SHIFT_PRIO         (14 << PRIO_SHIFT)
491 #define PLUS_PRIO          (15 << PRIO_SHIFT)
492 #define MUL_PRIO           (16 << PRIO_SHIFT)
493 #define UNARY_PRIO        ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
494
495 /* Operator to priority map.  Must be in the same order as the first
496    N entries of enum cpp_ttype.  */
497 static const short
498 op_to_prio[] =
499 {
500   /* EQ */              0,              /* dummy entry - can't happen */
501   /* NOT */             UNARY_PRIO,
502   /* GREATER */         LESS_PRIO,
503   /* LESS */            LESS_PRIO,
504   /* PLUS */            PLUS_PRIO,
505   /* MINUS */           PLUS_PRIO,
506   /* MULT */            MUL_PRIO,
507   /* DIV */             MUL_PRIO,
508   /* MOD */             MUL_PRIO,
509   /* AND */             AND_PRIO,
510   /* OR */              OR_PRIO,
511   /* XOR */             XOR_PRIO,
512   /* RSHIFT */          SHIFT_PRIO,
513   /* LSHIFT */          SHIFT_PRIO,
514   /* MIN */             MINMAX_PRIO,    /* C++ specific */
515   /* MAX */             MINMAX_PRIO,    /* extensions */
516
517   /* COMPL */           UNARY_PRIO,
518   /* AND_AND */         ANDAND_PRIO,
519   /* OR_OR */           OROR_PRIO,
520   /* QUERY */           COND_PRIO,
521   /* COLON */           COLON_PRIO,
522   /* COMMA */           COMMA_PRIO,
523   /* OPEN_PAREN */      OPEN_PAREN_PRIO,
524   /* CLOSE_PAREN */     CLOSE_PAREN_PRIO,
525   /* EQ_EQ */           EQUAL_PRIO,
526   /* NOT_EQ */          EQUAL_PRIO,
527   /* GREATER_EQ */      LESS_PRIO,
528   /* LESS_EQ */         LESS_PRIO,
529   /* EOF */             FORCE_REDUCE_PRIO,
530   /* UPLUS */           UNARY_PRIO,
531   /* UMINUS */          UNARY_PRIO
532 };
533
534 #define COMPARE(OP) \
535   top->unsignedp = 0; \
536   top->value = (unsigned1 | unsigned2) \
537   ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
538   : (v1 OP v2)
539 #define EQUALITY(OP) \
540   top->value = v1 OP v2; \
541   top->unsignedp = 0;
542 #define BITWISE(OP) \
543   top->value = v1 OP v2; \
544   top->unsignedp = unsigned1 | unsigned2;
545 #define MINMAX(OP) \
546   top->value = (v1 OP v2) ? v1 : v2; \
547   top->unsignedp = unsigned1 | unsigned2;
548 #define UNARY(OP) \
549   top->value = OP v2; \
550   top->unsignedp = unsigned2;
551 #define SHIFT(PSH, MSH) \
552   if (skip_evaluation)  \
553     break;              \
554   top->unsignedp = unsigned1; \
555   if (v2 < 0 && ! unsigned2)  \
556     top->value = MSH (pfile, v1, unsigned1, -v2); \
557   else \
558     top->value = PSH (pfile, v1, unsigned1, v2);
559
560 /* Parse and evaluate a C expression, reading from PFILE.
561    Returns the truth value of the expression.  */
562 int
563 _cpp_parse_expr (pfile)
564      cpp_reader *pfile;
565 {
566   /* The implementation is an operator precedence parser, i.e. a
567      bottom-up parser, using a stack for not-yet-reduced tokens.
568
569      The stack base is 'stack', and the current stack pointer is 'top'.
570      There is a stack element for each operator (only),
571      and the most recently pushed operator is 'top->op'.
572      An operand (value) is stored in the 'value' field of the stack
573      element of the operator that precedes it.  */
574
575 #define INIT_STACK_SIZE 20
576   struct op init_stack[INIT_STACK_SIZE];
577   struct op *stack = init_stack;
578   struct op *limit = stack + INIT_STACK_SIZE;
579   struct op *top = stack + 1;
580   int skip_evaluation = 0;
581   int result;
582   unsigned int lex_count, saw_leading_not;
583   bool want_value = true;
584
585   /* Set up detection of #if ! defined().  */
586   pfile->mi_ind_cmacro = 0;
587   saw_leading_not = 0;
588   lex_count = 0;
589
590   /* We've finished when we try to reduce this.  */
591   top->op = CPP_EOF;
592   /* Nifty way to catch missing '('.  */
593   top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
594
595   for (;;)
596     {
597       unsigned int prio;
598       unsigned int flags;
599       struct op op;
600
601       /* Read a token */
602       op = lex (pfile, skip_evaluation);
603       lex_count++;
604
605       /* If the token is an operand, push its value and get next
606          token.  If it is an operator, handle some special cases, get
607          its priority and flags, and try to reduce the expression on
608          the stack.  */
609       switch (op.op)
610         {
611         case CPP_ERROR:
612           goto syntax_error;
613         case CPP_NUMBER:
614           /* Push a value onto the stack.  */
615           if (!want_value)
616             SYNTAX_ERROR ("missing binary operator");
617         push_immediate:
618           want_value = false;
619           top->value = op.value;
620           top->unsignedp = op.unsignedp;
621           continue;
622
623         case CPP_NOT:
624           saw_leading_not = lex_count == 1;
625           break;
626         case CPP_PLUS:
627           if (want_value)
628             op.op = CPP_UPLUS;
629           break;
630         case CPP_MINUS:
631           if (want_value)
632             op.op = CPP_UMINUS;
633           break;
634         default:
635           break;
636         }
637
638       flags = EXTRACT_FLAGS (op_to_prio[op.op]);
639       prio = EXTRACT_PRIO (op_to_prio[op.op]);
640       if (prio == EXTRACT_PRIO (OPEN_PAREN_PRIO))
641         goto skip_reduction;
642
643       /* Check for reductions.  Then push the operator.  */
644       while (prio <= top->prio)
645         {
646           HOST_WIDEST_INT v1, v2;
647           unsigned int unsigned1, unsigned2;
648           
649           /* Most operators that can appear on the stack require a
650              right operand.  Check this before trying to reduce.  */
651           if (want_value)
652             {
653               if (top->op == CPP_OPEN_PAREN)
654                 SYNTAX_ERROR ("void expression between '(' and ')'");
655               else if (top->op != CPP_EOF)
656                 SYNTAX_ERROR2 ("operator '%s' has no right operand",
657                                op_as_text (pfile, top->op));
658               else if (op.op != CPP_CLOSE_PAREN)
659                 SYNTAX_ERROR ("#if with no expression");
660             }
661
662           unsigned2 = top->unsignedp, v2 = top->value;
663           top--;
664           unsigned1 = top->unsignedp, v1 = top->value;
665
666           /* Now set top->value = (top[1].op)(v1, v2); */
667           switch (top[1].op)
668             {
669             default:
670               cpp_error (pfile, DL_ICE, "impossible operator '%s'",
671                          op_as_text (pfile, top[1].op));
672               goto syntax_error;
673
674             case CPP_NOT:        UNARY(!);      break;
675             case CPP_COMPL:      UNARY(~);      break;
676             case CPP_LESS:       COMPARE(<);    break;
677             case CPP_GREATER:    COMPARE(>);    break;
678             case CPP_LESS_EQ:    COMPARE(<=);   break;
679             case CPP_GREATER_EQ: COMPARE(>=);   break;
680             case CPP_EQ_EQ:      EQUALITY(==);  break;
681             case CPP_NOT_EQ:     EQUALITY(!=);  break;
682             case CPP_AND:        BITWISE(&);    break;
683             case CPP_XOR:        BITWISE(^);    break;
684             case CPP_OR:         BITWISE(|);    break;
685             case CPP_LSHIFT:     SHIFT(left_shift, right_shift); break;
686             case CPP_RSHIFT:     SHIFT(right_shift, left_shift); break;
687             case CPP_MIN:        MINMAX(<);     break;
688             case CPP_MAX:        MINMAX(>);     break;
689
690             case CPP_UPLUS:
691               /* Can't use UNARY(+) because K+R C did not have unary
692                  plus.  Can't use UNARY() because some compilers object
693                  to the empty argument.  */
694               top->value = v2;
695               top->unsignedp = unsigned2;
696               if (CPP_WTRADITIONAL (pfile))
697                 cpp_error (pfile, DL_WARNING,
698                            "traditional C rejects the unary plus operator");
699               break;
700             case CPP_UMINUS:
701               UNARY(-);
702               if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
703                 integer_overflow (pfile);
704               break;
705
706             case CPP_PLUS:
707               top->value = v1 + v2;
708               top->unsignedp = unsigned1 | unsigned2;
709               if (! top->unsignedp && ! skip_evaluation
710                   && ! possible_sum_sign (v1, v2, top->value))
711                 integer_overflow (pfile);
712               break;
713             case CPP_MINUS:
714               top->value = v1 - v2;
715               top->unsignedp = unsigned1 | unsigned2;
716               if (! top->unsignedp && ! skip_evaluation
717                   && ! possible_sum_sign (top->value, v2, v1))
718                 integer_overflow (pfile);
719               break;
720             case CPP_MULT:
721               top->unsignedp = unsigned1 | unsigned2;
722               if (top->unsignedp)
723                 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
724               else if (!skip_evaluation)
725                 {
726                   top->value = v1 * v2;
727                   if (v1 && (top->value / v1 != v2
728                              || (top->value & v1 & v2) < 0))
729                     integer_overflow (pfile);
730                 }
731               break;
732             case CPP_DIV:
733             case CPP_MOD:
734               if (skip_evaluation)
735                 break;
736               if (v2 == 0)
737                 SYNTAX_ERROR ("division by zero in #if");
738               top->unsignedp = unsigned1 | unsigned2;
739               if (top[1].op == CPP_DIV)
740                 {
741                   if (top->unsignedp)
742                     top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
743                   else
744                     {
745                       top->value = v1 / v2;
746                       if ((top->value & v1 & v2) < 0)
747                         integer_overflow (pfile);
748                     }
749                 }
750               else
751                 {
752                   if (top->unsignedp)
753                     top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
754                   else
755                     top->value = v1 % v2;
756                 }
757               break;
758
759             case CPP_OR_OR:
760               top->value = v1 || v2;
761               top->unsignedp = 0;
762               if (v1) skip_evaluation--;
763               break;
764             case CPP_AND_AND:
765               top->value = v1 && v2;
766               top->unsignedp = 0;
767               if (!v1) skip_evaluation--;
768               break;
769             case CPP_COMMA:
770               if (CPP_PEDANTIC (pfile))
771                 cpp_error (pfile, DL_PEDWARN,
772                            "comma operator in operand of #if");
773               top->value = v2;
774               top->unsignedp = unsigned2;
775               break;
776             case CPP_QUERY:
777               SYNTAX_ERROR ("syntax error '?' without following ':'");
778             case CPP_COLON:
779               if (top[0].op != CPP_QUERY)
780                 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
781               top--;
782               if (top->value) skip_evaluation--;
783               top->value = top->value ? v1 : v2;
784               top->unsignedp = unsigned1 | unsigned2;
785               break;
786             case CPP_OPEN_PAREN:
787               if (op.op != CPP_CLOSE_PAREN)
788                 SYNTAX_ERROR ("missing ')' in expression");
789               op.value = v2;
790               op.unsignedp = unsigned2;
791               goto push_immediate;
792             case CPP_EOF:
793               /* Reducing this dummy operator indicates we've finished.  */
794               if (op.op == CPP_CLOSE_PAREN)
795                 SYNTAX_ERROR ("missing '(' in expression");
796               goto done;
797             }
798         }
799
800       /* Handle short-circuit evaluations.  */
801       if (flags & SHORT_CIRCUIT)
802         switch (op.op)
803           {
804           case CPP_OR_OR:    if (top->value) skip_evaluation++; break;
805           case CPP_AND_AND:
806           case CPP_QUERY:    if (!top->value) skip_evaluation++; break;
807           case CPP_COLON:
808             if (top[-1].value) /* Was '?' condition true?  */
809               skip_evaluation++;
810             else
811               skip_evaluation--;
812           default:
813             break;
814           }
815
816     skip_reduction:
817       /* Check we have a left operand iff we need one.  */
818       if (flags & NO_L_OPERAND)
819         {
820           if (!want_value)
821             SYNTAX_ERROR2 ("missing binary operator before '%s'",
822                            op_as_text (pfile, op.op));
823         }
824       else
825         {
826           if (want_value)
827             SYNTAX_ERROR2 ("operator '%s' has no left operand",
828                            op_as_text (pfile, op.op));
829         }
830       want_value = true;
831
832       /* Check for and handle stack overflow.  */
833       top++;
834       if (top == limit)
835         {
836           struct op *new_stack;
837           int old_size = (char *) limit - (char *) stack;
838           int new_size = 2 * old_size;
839           if (stack != init_stack)
840             new_stack = (struct op *) xrealloc (stack, new_size);
841           else
842             {
843               new_stack = (struct op *) xmalloc (new_size);
844               memcpy (new_stack, stack, old_size);
845             }
846           stack = new_stack;
847           top = (struct op *) ((char *) new_stack + old_size);
848           limit = (struct op *) ((char *) new_stack + new_size);
849         }
850       
851       top->flags = flags;
852       top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
853       top->op = op.op;
854     }
855
856  done:
857   /* The controlling macro expression is only valid if we called lex 3
858      times: <!> <defined expression> and <EOF>.  push_conditional ()
859      checks that we are at top-of-file.  */
860   if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
861     pfile->mi_ind_cmacro = 0;
862
863   result = (top[1].value != 0);
864
865   if (top != stack)
866     {
867       cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
868     syntax_error:
869       result = 0;  /* Return 0 on syntax error.  */
870     }
871
872   /* Free dynamic stack if we allocated one.  */
873   if (stack != init_stack)
874     free (stack);
875   return result;
876 }
877
878 /* Output OP as text for diagnostics.  */
879 static const unsigned char *
880 op_as_text (pfile, op)
881      cpp_reader *pfile;
882      enum cpp_ttype op;
883 {
884   cpp_token token;
885
886   if (op == CPP_UPLUS)
887     op = CPP_PLUS;
888   else if (op == CPP_UMINUS)
889     op = CPP_MINUS;
890
891   token.type = op;
892   token.flags = 0;
893   return cpp_token_as_text (pfile, &token);
894 }