OSDN Git Service

* df.c (df_insn_refs_record): Use XEXP (x, 0) for USE.
[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   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.  */
49 };
50
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
54
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)
63
64 struct suffix
65 {
66   const unsigned char s[4];
67   const unsigned char u;
68   const unsigned char l;
69 };
70
71 static const struct suffix vsuf_1[] = {
72   { "u", 1, 0 }, { "U", 1, 0 },
73   { "l", 0, 1 }, { "L", 0, 1 }
74 };
75
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 }
80 };
81
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 }
85 };
86 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
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 U_CHAR *start = tok->val.str.text;
98   const U_CHAR *end = start + tok->val.str.len;
99   const U_CHAR *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 = Nsuff(vsuf_1); break;
161         case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
162         case 3: sufftab = vsuf_3; nsuff = Nsuff(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_warning (pfile, "traditional C rejects the `U' suffix");
177       if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
178           && ! CPP_OPTION (pfile, c99))
179         cpp_pedwarn (pfile, "too many 'l' suffixes in integer constant");
180     }
181   
182   if (base <= largest_digit)
183     cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
184
185   if (overflow)
186     cpp_pedwarn (pfile, "integer constant out of range");
187
188   /* If too big to be signed, consider it unsigned.  */
189   else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
190     {
191       if (base == 10)
192         cpp_warning (pfile, "integer constant is so large that it is unsigned");
193       op.unsignedp = 1;
194     }
195
196   op.value = n;
197   op.op = CPP_NUMBER;
198   return op;
199
200  invalid_suffix:
201   cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
202              (int) (end - p), p);
203  syntax_error:
204   op.op = CPP_ERROR;
205   return op;
206 }
207
208 /* Handle meeting "defined" in a preprocessor expression.  */
209 static struct op
210 parse_defined (pfile)
211      cpp_reader *pfile;
212 {
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;
218
219   /* Don't expand macros.  */
220   pfile->state.prevent_expansion++;
221
222   token = cpp_get_token (pfile);
223   if (token->type == CPP_OPEN_PAREN)
224     {
225       paren = 1;
226       token = cpp_get_token (pfile);
227     }
228
229   if (token->type == CPP_NAME)
230     {
231       node = token->val.node;
232       if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
233         {
234           cpp_error (pfile, "missing ')' after \"defined\"");
235           node = 0;
236         }
237     }
238   else
239     {
240       cpp_error (pfile, "operator \"defined\" requires an identifier");
241       if (token->flags & NAMED_OP)
242         {
243           cpp_token op;
244
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));
251         }
252     }
253
254   if (!node)
255     op.op = CPP_ERROR;
256   else
257     {
258       if (pfile->context != initial_context)
259         cpp_warning (pfile, "this use of \"defined\" may not be portable");
260
261       op.value = node->type == NT_MACRO;
262       op.unsignedp = 0;
263       op.op = CPP_NUMBER;
264
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;
268     }
269
270   pfile->state.prevent_expansion--;
271   return op;
272 }
273
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.  */
278 static struct op
279 lex (pfile, skip_evaluation)
280      cpp_reader *pfile;
281      int skip_evaluation;
282 {
283   struct op op;
284   const cpp_token *token = cpp_get_token (pfile);
285
286   switch (token->type)
287     {
288     case CPP_NUMBER:
289       return parse_number (pfile, token);
290
291     case CPP_CHAR:
292     case CPP_WCHAR:
293       {
294         unsigned int chars_seen;
295
296         if (token->type == CPP_CHAR)
297           op.unsignedp = 0;
298         else
299           op.unsignedp = WCHAR_UNSIGNED;
300         op.op = CPP_NUMBER;
301         op.value = cpp_interpret_charconst (pfile, token, 1, 0, &chars_seen);
302         return op;
303       }
304
305     case CPP_STRING:
306     case CPP_WSTRING:
307       SYNTAX_ERROR ("string constants are not valid in #if");
308
309     case CPP_OTHER:
310       if (ISGRAPH (token->val.c))
311         SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
312       else
313         SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
314
315     case CPP_NAME:
316       if (token->val.node == pfile->spec_nodes.n_defined)
317         return parse_defined (pfile);
318       else if (CPP_OPTION (pfile, cplusplus)
319                && (token->val.node == pfile->spec_nodes.n_true
320                    || token->val.node == pfile->spec_nodes.n_false))
321         {
322           op.op = CPP_NUMBER;
323           op.unsignedp = 0;
324           op.value = (token->val.node == pfile->spec_nodes.n_true);
325
326           /* Warn about use of true or false in #if when pedantic
327              and stdbool.h has not been included.  */
328           if (CPP_PEDANTIC (pfile)
329               && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
330             cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
331                          NODE_NAME (token->val.node));
332           return op;
333         }
334       else
335         {
336           op.op = CPP_NUMBER;
337           op.unsignedp = 0;
338           op.value = 0;
339
340           if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
341             cpp_warning (pfile, "\"%s\" is not defined",
342                          NODE_NAME (token->val.node));
343           return op;
344         }
345
346     case CPP_HASH:
347       {
348         int temp;
349
350         op.op = CPP_NUMBER;
351         if (_cpp_test_assertion (pfile, &temp))
352           op.op = CPP_ERROR;
353         op.unsignedp = 0;
354         op.value = temp;
355         return op;
356       }
357
358     default:
359       if (((int) token->type > (int) CPP_EQ
360            && (int) token->type < (int) CPP_PLUS_EQ)
361           || token->type == CPP_EOF)
362         {
363           op.op = token->type;
364           return op;
365         }
366
367       SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
368                      cpp_token_as_text (pfile, token));
369     }
370
371  syntax_error:
372   op.op = CPP_ERROR;
373   return op;
374 }
375
376 /* Warn if appropriate on overflow.  */
377 static void
378 integer_overflow (pfile)
379      cpp_reader *pfile;
380 {
381   if (CPP_PEDANTIC (pfile))
382     cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
383 }
384
385 /* Handle shifting A left by B bits.  UNSIGNEDP is non-zero if A is
386    unsigned.  */
387 static HOST_WIDEST_INT
388 left_shift (pfile, a, unsignedp, b)
389      cpp_reader *pfile;
390      HOST_WIDEST_INT a;
391      unsigned int unsignedp;
392      unsigned HOST_WIDEST_INT b;
393 {
394   if (b >= HOST_BITS_PER_WIDEST_INT)
395     {
396       if (! unsignedp && a != 0)
397         integer_overflow (pfile);
398       return 0;
399     }
400   else if (unsignedp)
401     return (unsigned HOST_WIDEST_INT) a << b;
402   else
403     {
404       HOST_WIDEST_INT l = a << b;
405       if (l >> b != a)
406         integer_overflow (pfile);
407       return l;
408     }
409 }
410
411 /* Handle shifting A right by B bits.  UNSIGNEDP is non-zero if A is
412    unsigned.  */
413 static HOST_WIDEST_INT
414 right_shift (pfile, a, unsignedp, b)
415      cpp_reader *pfile ATTRIBUTE_UNUSED;
416      HOST_WIDEST_INT a;
417      unsigned int unsignedp;
418      unsigned HOST_WIDEST_INT b;
419 {
420   if (b >= HOST_BITS_PER_WIDEST_INT)
421     return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
422   else if (unsignedp)
423     return (unsigned HOST_WIDEST_INT) a >> b;
424   else
425     return a >> b;
426 }
427 \f
428 /* Operator precedence and flags table.
429
430 After an operator is returned from the lexer, if it has priority less
431 than or equal to the operator on the top of the stack, we reduce the
432 stack by one operator and repeat the test.  Since equal priorities
433 reduce, this is naturally left-associative.
434
435 We handle right-associative operators by clearing the lower bit of all
436 left-associative operators, and setting it for right-associative ones.
437 After the reduction phase of a new operator, just before it is pushed
438 onto the stack, its RIGHT_ASSOC bit is cleared.  The effect is that
439 during the reduction phase, the current right-associative operator has
440 a priority one greater than any other operator of otherwise equal
441 precedence that has been pushed on the top of the stack.  This avoids
442 a reduction pass, and effectively makes the logic right-associative.
443
444 The remaining cases are '(' and ')'.  We handle '(' by skipping the
445 reduction phase completely.  ')' is given lower priority than
446 everything else, including '(', effectively forcing a reduction of the
447 parenthesised expression.  If there is no matching '(', the stack will
448 be reduced all the way to the beginning, exiting the parser in the
449 same way as the ultra-low priority end-of-expression dummy operator.
450 The exit code checks to see if the operator that caused it is ')', and
451 if so outputs an appropriate error message.
452
453 The parser assumes all shifted operators require a right operand
454 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
455 These semantics are automatically checked, any extra semantics need to
456 be handled with operator-specific code.  */
457
458 #define FLAG_BITS  8
459 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
460 #define PRIO_SHIFT (FLAG_BITS + 1)
461 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
462 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
463
464 /* Flags.  */
465 #define HAVE_VALUE     (1 << 0)
466 #define NO_L_OPERAND   (1 << 1)
467 #define NO_R_OPERAND   (1 << 2)
468 #define SHORT_CIRCUIT  (1 << 3)
469
470 /* Priority and flag combinations.  */
471 #define RIGHT_ASSOC         (1 << FLAG_BITS)
472 #define FORCE_REDUCE_PRIO   (0 << PRIO_SHIFT)
473 #define CLOSE_PAREN_PRIO    (1 << PRIO_SHIFT)
474 #define OPEN_PAREN_PRIO    ((2 << PRIO_SHIFT) | NO_L_OPERAND)
475 #define COMMA_PRIO          (3 << PRIO_SHIFT)
476 #define COND_PRIO          ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
477 #define COLON_PRIO         ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
478 #define OROR_PRIO          ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
479 #define ANDAND_PRIO        ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
480 #define OR_PRIO             (8 << PRIO_SHIFT)
481 #define XOR_PRIO            (9 << PRIO_SHIFT)
482 #define AND_PRIO           (10 << PRIO_SHIFT)
483 #define MINMAX_PRIO        (11 << PRIO_SHIFT)
484 #define EQUAL_PRIO         (12 << PRIO_SHIFT)
485 #define LESS_PRIO          (13 << PRIO_SHIFT)
486 #define SHIFT_PRIO         (14 << PRIO_SHIFT)
487 #define PLUS_PRIO          (15 << PRIO_SHIFT)
488 #define MUL_PRIO           (16 << PRIO_SHIFT)
489 #define UNARY_PRIO        ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
490
491 /* Operator to priority map.  Must be in the same order as the first
492    N entries of enum cpp_ttype.  */
493 static const short
494 op_to_prio[] =
495 {
496   /* EQ */              0,              /* dummy entry - can't happen */
497   /* NOT */             UNARY_PRIO,
498   /* GREATER */         LESS_PRIO,
499   /* LESS */            LESS_PRIO,
500   /* PLUS */            UNARY_PRIO,     /* note these two can be unary */
501   /* MINUS */           UNARY_PRIO,     /* or binary */
502   /* MULT */            MUL_PRIO,
503   /* DIV */             MUL_PRIO,
504   /* MOD */             MUL_PRIO,
505   /* AND */             AND_PRIO,
506   /* OR */              OR_PRIO,
507   /* XOR */             XOR_PRIO,
508   /* RSHIFT */          SHIFT_PRIO,
509   /* LSHIFT */          SHIFT_PRIO,
510   /* MIN */             MINMAX_PRIO,    /* C++ specific */
511   /* MAX */             MINMAX_PRIO,    /* extensions */
512
513   /* COMPL */           UNARY_PRIO,
514   /* AND_AND */         ANDAND_PRIO,
515   /* OR_OR */           OROR_PRIO,
516   /* QUERY */           COND_PRIO,
517   /* COLON */           COLON_PRIO,
518   /* COMMA */           COMMA_PRIO,
519   /* OPEN_PAREN */      OPEN_PAREN_PRIO,
520   /* CLOSE_PAREN */     CLOSE_PAREN_PRIO,
521   /* EQ_EQ */           EQUAL_PRIO,
522   /* NOT_EQ */          EQUAL_PRIO,
523   /* GREATER_EQ */      LESS_PRIO,
524   /* LESS_EQ */         LESS_PRIO
525 };
526
527 #define COMPARE(OP) \
528   top->unsignedp = 0; \
529   top->value = (unsigned1 | unsigned2) \
530   ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
531   : (v1 OP v2)
532 #define EQUALITY(OP) \
533   top->value = v1 OP v2; \
534   top->unsignedp = 0;
535 #define BITWISE(OP) \
536   top->value = v1 OP v2; \
537   top->unsignedp = unsigned1 | unsigned2;
538 #define MINMAX(OP) \
539   top->value = (v1 OP v2) ? v1 : v2; \
540   top->unsignedp = unsigned1 | unsigned2;
541 #define UNARY(OP) \
542   top->value = OP v2; \
543   top->unsignedp = unsigned2; \
544   top->flags |= HAVE_VALUE;
545 #define SHIFT(PSH, MSH) \
546   if (skip_evaluation)  \
547     break;              \
548   top->unsignedp = unsigned1; \
549   if (v2 < 0 && ! unsigned2)  \
550     top->value = MSH (pfile, v1, unsigned1, -v2); \
551   else \
552     top->value = PSH (pfile, v1, unsigned1, v2);
553
554 /* Parse and evaluate a C expression, reading from PFILE.
555    Returns the truth value of the expression.  */
556 int
557 _cpp_parse_expr (pfile)
558      cpp_reader *pfile;
559 {
560   /* The implementation is an operator precedence parser, i.e. a
561      bottom-up parser, using a stack for not-yet-reduced tokens.
562
563      The stack base is 'stack', and the current stack pointer is 'top'.
564      There is a stack element for each operator (only),
565      and the most recently pushed operator is 'top->op'.
566      An operand (value) is stored in the 'value' field of the stack
567      element of the operator that precedes it.
568      In that case the 'flags' field has the HAVE_VALUE flag set.  */
569
570 #define INIT_STACK_SIZE 20
571   struct op init_stack[INIT_STACK_SIZE];
572   struct op *stack = init_stack;
573   struct op *limit = stack + INIT_STACK_SIZE;
574   struct op *top = stack + 1;
575   int skip_evaluation = 0;
576   int result;
577   unsigned int lex_count, saw_leading_not;
578
579   /* Set up detection of #if ! defined().  */
580   pfile->mi_ind_cmacro = 0;
581   saw_leading_not = 0;
582   lex_count = 0;
583
584   /* We've finished when we try to reduce this.  */
585   top->op = CPP_EOF;
586   /* Nifty way to catch missing '('.  */
587   top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
588   /* Avoid missing right operand checks.  */
589   top->flags = NO_R_OPERAND;
590
591   for (;;)
592     {
593       unsigned int prio;
594       unsigned int flags;
595       struct op op;
596
597       /* Read a token */
598       op = lex (pfile, skip_evaluation);
599       lex_count++;
600
601       /* If the token is an operand, push its value and get next
602          token.  If it is an operator, get its priority and flags, and
603          try to reduce the expression on the stack.  */
604       switch (op.op)
605         {
606         case CPP_ERROR:
607           goto syntax_error;
608         push_immediate:
609         case CPP_NUMBER:
610           /* Push a value onto the stack.  */
611           if (top->flags & HAVE_VALUE)
612             SYNTAX_ERROR ("missing binary operator");
613           top->value = op.value;
614           top->unsignedp = op.unsignedp;
615           top->flags |= HAVE_VALUE;
616           continue;
617
618         case CPP_EOF:   prio = FORCE_REDUCE_PRIO;       break;
619
620         case CPP_NOT:
621           saw_leading_not = lex_count == 1;
622           prio = op_to_prio[op.op];
623           break;
624         case CPP_PLUS:
625         case CPP_MINUS: prio = PLUS_PRIO;  if (top->flags & HAVE_VALUE) break;
626           /* else unary; fall through */
627         default:        prio = op_to_prio[op.op];       break;
628         }
629
630       /* Separate the operator's code into priority and flags.  */
631       flags = EXTRACT_FLAGS(prio);
632       prio = EXTRACT_PRIO(prio);
633       if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
634         goto skip_reduction;
635
636       /* Check for reductions.  Then push the operator.  */
637       while (prio <= top->prio)
638         {
639           HOST_WIDEST_INT v1, v2;
640           unsigned int unsigned1, unsigned2;
641           
642           /* Most operators that can appear on the stack require a
643              right operand.  Check this before trying to reduce.  */
644           if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
645             {
646               if (top->op == CPP_OPEN_PAREN)
647                 SYNTAX_ERROR ("void expression between '(' and ')'");
648               else
649                 SYNTAX_ERROR2 ("operator '%s' has no right operand",
650                                op_as_text (pfile, top->op));
651             }
652
653           unsigned2 = top->unsignedp, v2 = top->value;
654           top--;
655           unsigned1 = top->unsignedp, v1 = top->value;
656
657           /* Now set top->value = (top[1].op)(v1, v2); */
658           switch (top[1].op)
659             {
660             default:
661               cpp_ice (pfile, "impossible operator '%s'",
662                                op_as_text (pfile, top[1].op));
663               goto syntax_error;
664
665             case CPP_NOT:        UNARY(!);      break;
666             case CPP_COMPL:      UNARY(~);      break;
667             case CPP_LESS:       COMPARE(<);    break;
668             case CPP_GREATER:    COMPARE(>);    break;
669             case CPP_LESS_EQ:    COMPARE(<=);   break;
670             case CPP_GREATER_EQ: COMPARE(>=);   break;
671             case CPP_EQ_EQ:      EQUALITY(==);  break;
672             case CPP_NOT_EQ:     EQUALITY(!=);  break;
673             case CPP_AND:        BITWISE(&);    break;
674             case CPP_XOR:        BITWISE(^);    break;
675             case CPP_OR:         BITWISE(|);    break;
676             case CPP_LSHIFT:     SHIFT(left_shift, right_shift); break;
677             case CPP_RSHIFT:     SHIFT(right_shift, left_shift); break;
678             case CPP_MIN:        MINMAX(<);     break;
679             case CPP_MAX:        MINMAX(>);     break;
680
681             case CPP_PLUS:
682               if (!(top->flags & HAVE_VALUE))
683                 {
684                   /* Can't use UNARY(+) because K+R C did not have unary
685                      plus.  Can't use UNARY() because some compilers object
686                      to the empty argument.  */
687                   top->value = v2;
688                   top->unsignedp = unsigned2;
689                   top->flags |= HAVE_VALUE;
690
691                   if (CPP_WTRADITIONAL (pfile))
692                     cpp_warning (pfile,
693                         "traditional C rejects the unary plus operator");
694                 }
695               else
696                 {
697                   top->value = v1 + v2;
698                   top->unsignedp = unsigned1 | unsigned2;
699                   if (! top->unsignedp && ! skip_evaluation
700                       && ! possible_sum_sign (v1, v2, top->value))
701                     integer_overflow (pfile);
702                 }
703               break;
704             case CPP_MINUS:
705               if (!(top->flags & HAVE_VALUE))
706                 {
707                   UNARY(-);
708                   if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
709                     integer_overflow (pfile);
710                 }
711               else
712                 { /* Binary '-' */
713                   top->value = v1 - v2;
714                   top->unsignedp = unsigned1 | unsigned2;
715                   if (! top->unsignedp && ! skip_evaluation
716                       && ! possible_sum_sign (top->value, v2, v1))
717                     integer_overflow (pfile);
718                 }
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_pedwarn (pfile, "comma operator in operand of #if");
772               top->value = v2;
773               top->unsignedp = unsigned2;
774               break;
775             case CPP_QUERY:
776               SYNTAX_ERROR ("syntax error '?' without following ':'");
777             case CPP_COLON:
778               if (top[0].op != CPP_QUERY)
779                 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
780               top--;
781               if (top->value) skip_evaluation--;
782               top->value = top->value ? v1 : v2;
783               top->unsignedp = unsigned1 | unsigned2;
784               break;
785             case CPP_OPEN_PAREN:
786               if (op.op != CPP_CLOSE_PAREN)
787                 SYNTAX_ERROR ("missing ')' in expression");
788               op.value = v2;
789               op.unsignedp = unsigned2;
790               goto push_immediate;
791             case CPP_EOF:
792               /* Reducing this dummy operator indicates we've finished.  */
793               if (op.op == CPP_CLOSE_PAREN)
794                 SYNTAX_ERROR ("missing '(' in expression");
795               goto done;
796             }
797         }
798
799       /* Handle short-circuit evaluations.  */
800       if (flags & SHORT_CIRCUIT)
801         switch (op.op)
802           {
803           case CPP_OR_OR:    if (top->value) skip_evaluation++; break;
804           case CPP_AND_AND:
805           case CPP_QUERY:    if (!top->value) skip_evaluation++; break;
806           case CPP_COLON:
807             if (top[-1].value) /* Was '?' condition true?  */
808               skip_evaluation++;
809             else
810               skip_evaluation--;
811           default:
812             break;
813           }
814
815     skip_reduction:
816       /* Check we have a left operand iff we need one.  */
817       if (flags & NO_L_OPERAND)
818         {
819           if (top->flags & HAVE_VALUE)
820             SYNTAX_ERROR2 ("missing binary operator before '%s'",
821                            op_as_text (pfile, op.op));
822         }
823       else
824         {
825           if (!(top->flags & HAVE_VALUE))
826             SYNTAX_ERROR2 ("operator '%s' has no left operand",
827                            op_as_text (pfile, op.op));
828         }
829
830       /* Check for and handle stack overflow.  */
831       top++;
832       if (top == limit)
833         {
834           struct op *new_stack;
835           int old_size = (char *) limit - (char *) stack;
836           int new_size = 2 * old_size;
837           if (stack != init_stack)
838             new_stack = (struct op *) xrealloc (stack, new_size);
839           else
840             {
841               new_stack = (struct op *) xmalloc (new_size);
842               memcpy (new_stack, stack, old_size);
843             }
844           stack = new_stack;
845           top = (struct op *) ((char *) new_stack + old_size);
846           limit = (struct op *) ((char *) new_stack + new_size);
847         }
848       
849       top->flags = flags;
850       top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
851       top->op = op.op;
852     }
853
854  done:
855   /* The controlling macro expression is only valid if we called lex 3
856      times: <!> <defined expression> and <EOF>.  push_conditional ()
857      checks that we are at top-of-file.  */
858   if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
859     pfile->mi_ind_cmacro = 0;
860
861   result = (top[1].value != 0);
862
863   if (top != stack)
864     CPP_ICE ("unbalanced stack in #if");
865   else if (!(top[1].flags & HAVE_VALUE))
866     {
867       SYNTAX_ERROR ("#if with no expression");
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   token.type = op;
887   token.flags = 0;
888   return cpp_token_as_text (pfile, &token);
889 }