OSDN Git Service

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