OSDN Git Service

* java-tree.h (end_params_node): Declare global.
[pf3gnuchains/gcc-fork.git] / gcc / cppexp.c
1 /* Parse C expressions for CCCP.
2    Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998 Free Software Foundation.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18
19  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!
22
23 Written by Per Bothner 1994.  */
24
25 /* Parse a C expression from text in a string  */
26    
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30
31 extern char *xmalloc PARAMS ((unsigned));
32 extern char *xrealloc PARAMS ((void *, unsigned));
33
34 #ifdef MULTIBYTE_CHARS
35 #include <locale.h>
36 #endif
37
38 /* This is used for communicating lists of keywords with cccp.c.  */
39 struct arglist {
40   struct arglist *next;
41   U_CHAR *name;
42   int length;
43   int argno;
44 };
45
46 #ifndef CHAR_TYPE_SIZE
47 #define CHAR_TYPE_SIZE BITS_PER_UNIT
48 #endif
49
50 #ifndef INT_TYPE_SIZE
51 #define INT_TYPE_SIZE BITS_PER_WORD
52 #endif
53
54 #ifndef LONG_TYPE_SIZE
55 #define LONG_TYPE_SIZE BITS_PER_WORD
56 #endif
57
58 #ifndef WCHAR_TYPE_SIZE
59 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
60 #endif
61
62 #ifndef MAX_CHAR_TYPE_SIZE
63 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
64 #endif
65
66 #ifndef MAX_INT_TYPE_SIZE
67 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
68 #endif
69
70 #ifndef MAX_LONG_TYPE_SIZE
71 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
72 #endif
73
74 #ifndef MAX_WCHAR_TYPE_SIZE
75 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
76 #endif
77
78 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
79    number with SUM's sign, where A, B, and SUM are all C integers.  */
80 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
81
82 static void integer_overflow PARAMS ((cpp_reader *));
83 static long left_shift PARAMS ((cpp_reader *, long, int, unsigned long));
84 static long right_shift PARAMS ((cpp_reader *, long, int, unsigned long));
85
86 #define ERROR 299
87 #define OROR 300
88 #define ANDAND 301
89 #define EQUAL 302
90 #define NOTEQUAL 303
91 #define LEQ 304
92 #define GEQ 305
93 #define LSH 306
94 #define RSH 307
95 #define NAME 308
96 #define INT 309
97 #define CHAR 310
98
99 #define LEFT_OPERAND_REQUIRED 1
100 #define RIGHT_OPERAND_REQUIRED 2
101 #define HAVE_VALUE 4
102 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
103    following operand should be short-circuited instead of evaluated.  */
104 #define SKIP_OPERAND 8
105 /*#define UNSIGNEDP 16*/
106
107 /* Find the largest host integer type and set its size and type.
108    Watch out: on some crazy hosts `long' is shorter than `int'.  */
109
110 #ifndef HOST_WIDE_INT
111 # if HAVE_INTTYPES_H
112 #  include <inttypes.h>
113 #  define HOST_WIDE_INT intmax_t
114 # else
115 #  if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
116        && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
117 #   define HOST_WIDE_INT int
118 #  else
119 #  if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
120        || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
121 #   define HOST_WIDE_INT long
122 #  else
123 #   define HOST_WIDE_INT long long
124 #  endif
125 #  endif
126 # endif
127 #endif
128
129 #ifndef CHAR_BIT
130 #define CHAR_BIT 8
131 #endif
132
133 #ifndef HOST_BITS_PER_WIDE_INT
134 #define HOST_BITS_PER_WIDE_INT (CHAR_BIT * sizeof (HOST_WIDE_INT))
135 #endif
136
137 struct operation {
138     short op;
139     char rprio; /* Priority of op (relative to it right operand).  */
140     char flags;
141     char unsignedp;    /* true if value should be treated as unsigned */
142     HOST_WIDE_INT value;        /* The value logically "right" of op.  */
143 };
144 \f
145 /* Take care of parsing a number (anything that starts with a digit).
146    LEN is the number of characters in it.  */
147
148 /* maybe needs to actually deal with floating point numbers */
149
150 struct operation
151 parse_number (pfile, start, olen)
152      cpp_reader *pfile;
153      char *start;
154      int olen;
155 {
156   struct operation op;
157   register char *p = start;
158   register int c;
159   register unsigned long n = 0, nd, ULONG_MAX_over_base;
160   register int base = 10;
161   register int len = olen;
162   register int overflow = 0;
163   register int digit, largest_digit = 0;
164   int spec_long = 0;
165
166   op.unsignedp = 0;
167
168   for (c = 0; c < len; c++)
169     if (p[c] == '.') {
170       /* It's a float since it contains a point.  */
171       cpp_error (pfile,
172                  "floating point numbers not allowed in #if expressions");
173       op.op = ERROR;
174       return op;
175     }
176
177   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
178     p += 2;
179     base = 16;
180     len -= 2;
181   }
182   else if (*p == '0')
183     base = 8;
184
185   /* Some buggy compilers (e.g. MPW C) seem to need both casts.  */
186   ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
187
188   for (; len > 0; len--) {
189     c = *p++;
190
191     if (c >= '0' && c <= '9')
192       digit = c - '0';
193     else if (base == 16 && c >= 'a' && c <= 'f')
194       digit = c - 'a' + 10;
195     else if (base == 16 && c >= 'A' && c <= 'F')
196       digit = c - 'A' + 10;
197     else {
198       /* `l' means long, and `u' means unsigned.  */
199       while (1) {
200         if (c == 'l' || c == 'L')
201           {
202             if (spec_long)
203               cpp_error (pfile, "two `l's in integer constant");
204             spec_long = 1;
205           }
206         else if (c == 'u' || c == 'U')
207           {
208             if (op.unsignedp)
209               cpp_error (pfile, "two `u's in integer constant");
210             op.unsignedp = 1;
211           }
212         else
213           break;
214
215         if (--len == 0)
216           break;
217         c = *p++;
218       }
219       /* Don't look for any more digits after the suffixes.  */
220       break;
221     }
222     if (largest_digit < digit)
223       largest_digit = digit;
224     nd = n * base + digit;
225     overflow |= ULONG_MAX_over_base < n || nd < n;
226     n = nd;
227   }
228
229   if (len != 0)
230     {
231       cpp_error (pfile, "Invalid number in #if expression");
232       op.op = ERROR;
233       return op;
234     }
235
236   if (base <= largest_digit)
237     cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
238
239   if (overflow)
240     cpp_pedwarn (pfile, "integer constant out of range");
241
242   /* If too big to be signed, consider it unsigned.  */
243   if ((long) n < 0 && ! op.unsignedp)
244     {
245       if (base == 10)
246         cpp_warning (pfile, "integer constant is so large that it is unsigned");
247       op.unsignedp = 1;
248     }
249
250   op.value = n;
251   op.op = INT;
252   return op;
253 }
254
255 struct token {
256   char *operator;
257   int token;
258 };
259
260 static struct token tokentab2[] = {
261   {"&&", ANDAND},
262   {"||", OROR},
263   {"<<", LSH},
264   {">>", RSH},
265   {"==", EQUAL},
266   {"!=", NOTEQUAL},
267   {"<=", LEQ},
268   {">=", GEQ},
269   {"++", ERROR},
270   {"--", ERROR},
271   {NULL, ERROR}
272 };
273
274 /* Read one token.  */
275
276 struct operation
277 cpp_lex (pfile, skip_evaluation)
278      cpp_reader *pfile;
279      int skip_evaluation;
280 {
281   register int c;
282   register struct token *toktab;
283   enum cpp_token token;
284   struct operation op;
285   U_CHAR *tok_start, *tok_end;
286   int old_written;
287
288  retry:
289
290   old_written = CPP_WRITTEN (pfile);
291   cpp_skip_hspace (pfile);
292   c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
293   if (c == '#')
294     return parse_number (pfile,
295                          cpp_read_check_assertion (pfile) ? "1" : "0", 1);
296
297   if (c == '\n')
298     {
299       op.op = 0;
300       return op;
301     }
302
303   token = cpp_get_token (pfile);
304   tok_start = pfile->token_buffer + old_written;
305   tok_end = CPP_PWRITTEN (pfile);
306   pfile->limit = tok_start;
307   switch (token)
308   {
309     case CPP_EOF: /* Should not happen ...  */
310     case CPP_VSPACE:
311       op.op = 0;
312       return op;
313     case CPP_POP:
314       if (CPP_BUFFER (pfile)->fname != NULL)
315         {
316           op.op = 0;
317           return op;
318         }
319       cpp_pop_buffer (pfile);
320       goto retry;
321     case CPP_HSPACE:   case CPP_COMMENT: 
322       goto retry;
323     case CPP_NUMBER:
324       return parse_number (pfile, tok_start, tok_end - tok_start);
325     case CPP_STRING:
326       cpp_error (pfile, "string constants not allowed in #if expressions");
327       op.op = ERROR;
328       return op;
329     case CPP_CHAR:
330       /* This code for reading a character constant
331          handles multicharacter constants and wide characters.
332          It is mostly copied from c-lex.c.  */
333       {
334         register int result = 0;
335         register int num_chars = 0;
336         unsigned width = MAX_CHAR_TYPE_SIZE;
337         int wide_flag = 0;
338         int max_chars;
339         U_CHAR *ptr = tok_start;
340 #ifdef MULTIBYTE_CHARS
341         char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
342 #else
343         char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
344 #endif
345
346         if (*ptr == 'L')
347           {
348             ptr++;
349             wide_flag = 1;
350             width = MAX_WCHAR_TYPE_SIZE;
351 #ifdef MULTIBYTE_CHARS
352             max_chars = MB_CUR_MAX;
353 #else
354             max_chars = 1;
355 #endif
356           }
357         else
358             max_chars = MAX_LONG_TYPE_SIZE / width;
359
360         ++ptr;
361         while (ptr < tok_end && ((c = *ptr++) != '\''))
362           {
363             if (c == '\\')
364               {
365                 c = cpp_parse_escape (pfile, (char **) &ptr);
366                 if (width < HOST_BITS_PER_INT
367                   && (unsigned) c >= (unsigned)(1 << width))
368                     cpp_pedwarn (pfile,
369                                  "escape sequence out of range for character");
370               }
371
372             num_chars++;
373
374             /* Merge character into result; ignore excess chars.  */
375             if (num_chars < max_chars + 1)
376               {
377                 if (width < HOST_BITS_PER_INT)
378                   result = (result << width) | (c & ((1 << width) - 1));
379                 else
380                   result = c;
381                 token_buffer[num_chars - 1] = c;
382               }
383           }
384
385         token_buffer[num_chars] = 0;
386
387         if (c != '\'')
388           cpp_error (pfile, "malformatted character constant");
389         else if (num_chars == 0)
390           cpp_error (pfile, "empty character constant");
391         else if (num_chars > max_chars)
392           {
393             num_chars = max_chars;
394             cpp_error (pfile, "character constant too long");
395           }
396         else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
397           cpp_warning (pfile, "multi-character character constant");
398
399         /* If char type is signed, sign-extend the constant.  */
400         if (! wide_flag)
401           {
402             int num_bits = num_chars * width;
403
404             if (cpp_lookup (pfile, (U_CHAR *)"__CHAR_UNSIGNED__",
405                             sizeof ("__CHAR_UNSIGNED__")-1, -1)
406                 || ((result >> (num_bits - 1)) & 1) == 0)
407                 op.value
408                     = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
409             else
410                 op.value
411                     = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
412           }
413         else
414           {
415 #ifdef MULTIBYTE_CHARS
416             /* Set the initial shift state and convert the next sequence.  */
417               result = 0;
418               /* In all locales L'\0' is zero and mbtowc will return zero,
419                  so don't use it.  */
420               if (num_chars > 1
421                   || (num_chars == 1 && token_buffer[0] != '\0'))
422                 {
423                   wchar_t wc;
424                   (void) mbtowc (NULL_PTR, NULL_PTR, 0);
425                   if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
426                     result = wc;
427                   else
428                     cpp_pedwarn (pfile,"Ignoring invalid multibyte character");
429                 }
430 #endif
431               op.value = result;
432             }
433         }
434
435       /* This is always a signed type.  */
436       op.unsignedp = 0;
437       op.op = CHAR;
438     
439       return op;
440
441     case CPP_NAME:
442       if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
443         cpp_warning (pfile, "`%.*s' is not defined",
444                      (int) (tok_end - tok_start), tok_start);
445       return parse_number (pfile, "0", 0);
446
447     case CPP_OTHER:
448       /* See if it is a special token of length 2.  */
449       if (tok_start + 2 == tok_end)
450         {
451           for (toktab = tokentab2; toktab->operator != NULL; toktab++)
452             if (tok_start[0] == toktab->operator[0]
453                 && tok_start[1] == toktab->operator[1])
454                 break;
455           if (toktab->token == ERROR)
456             {
457               char *buf = (char *) alloca (40);
458               sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
459               cpp_error (pfile, buf);
460             }
461           op.op = toktab->token; 
462           return op;
463         }
464       /* fall through */
465     default:
466       op.op = *tok_start;
467       return op;
468   }
469 }
470
471
472 /* Parse a C escape sequence.  STRING_PTR points to a variable
473    containing a pointer to the string to parse.  That pointer
474    is updated past the characters we use.  The value of the
475    escape sequence is returned.
476
477    A negative value means the sequence \ newline was seen,
478    which is supposed to be equivalent to nothing at all.
479
480    If \ is followed by a null character, we return a negative
481    value and leave the string pointer pointing at the null character.
482
483    If \ is followed by 000, we return 0 and leave the string pointer
484    after the zeros.  A value of 0 does not mean end of string.  */
485
486 int
487 cpp_parse_escape (pfile, string_ptr)
488      cpp_reader *pfile;
489      char **string_ptr;
490 {
491   register int c = *(*string_ptr)++;
492   switch (c)
493     {
494     case 'a':
495       return TARGET_BELL;
496     case 'b':
497       return TARGET_BS;
498     case 'e':
499     case 'E':
500       if (CPP_PEDANTIC (pfile))
501         cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
502       return 033;
503     case 'f':
504       return TARGET_FF;
505     case 'n':
506       return TARGET_NEWLINE;
507     case 'r':
508       return TARGET_CR;
509     case 't':
510       return TARGET_TAB;
511     case 'v':
512       return TARGET_VT;
513     case '\n':
514       return -2;
515     case 0:
516       (*string_ptr)--;
517       return 0;
518       
519     case '0':
520     case '1':
521     case '2':
522     case '3':
523     case '4':
524     case '5':
525     case '6':
526     case '7':
527       {
528         register int i = c - '0';
529         register int count = 0;
530         while (++count < 3)
531           {
532             c = *(*string_ptr)++;
533             if (c >= '0' && c <= '7')
534               i = (i << 3) + c - '0';
535             else
536               {
537                 (*string_ptr)--;
538                 break;
539               }
540           }
541         if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
542           {
543             i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
544             cpp_pedwarn (pfile,
545                           "octal character constant does not fit in a byte");
546           }
547         return i;
548       }
549     case 'x':
550       {
551         register unsigned i = 0, overflow = 0, digits_found = 0, digit;
552         for (;;)
553           {
554             c = *(*string_ptr)++;
555             if (c >= '0' && c <= '9')
556               digit = c - '0';
557             else if (c >= 'a' && c <= 'f')
558               digit = c - 'a' + 10;
559             else if (c >= 'A' && c <= 'F')
560               digit = c - 'A' + 10;
561             else
562               {
563                 (*string_ptr)--;
564                 break;
565               }
566             overflow |= i ^ (i << 4 >> 4);
567             i = (i << 4) + digit;
568             digits_found = 1;
569           }
570         if (!digits_found)
571           cpp_error (pfile, "\\x used with no following hex digits");
572         if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
573           {
574             i &= (1 << BITS_PER_UNIT) - 1;
575             cpp_pedwarn (pfile,
576                          "hex character constant does not fit in a byte");
577           }
578         return i;
579       }
580     default:
581       return c;
582     }
583 }
584
585 static void
586 integer_overflow (pfile)
587      cpp_reader *pfile;
588 {
589   if (CPP_PEDANTIC (pfile))
590     cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
591 }
592
593 static long
594 left_shift (pfile, a, unsignedp, b)
595      cpp_reader *pfile;
596      long a;
597      int unsignedp;
598      unsigned long b;
599 {
600   if (b >= HOST_BITS_PER_LONG)
601     {
602       if (! unsignedp && a != 0)
603         integer_overflow (pfile);
604       return 0;
605     }
606   else if (unsignedp)
607     return (unsigned long) a << b;
608   else
609     {
610       long l = a << b;
611       if (l >> b != a)
612         integer_overflow (pfile);
613       return l;
614     }
615 }
616
617 static long
618 right_shift (pfile, a, unsignedp, b)
619      cpp_reader *pfile ATTRIBUTE_UNUSED;
620      long a;
621      int unsignedp;
622      unsigned long b;
623 {
624   if (b >= HOST_BITS_PER_LONG)
625     return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
626   else if (unsignedp)
627     return (unsigned long) a >> b;
628   else
629     return a >> b;
630 }
631 \f
632 /* These priorities are all even, so we can handle associatively.  */
633 #define PAREN_INNER_PRIO 0
634 #define COMMA_PRIO 4
635 #define COND_PRIO (COMMA_PRIO+2)
636 #define OROR_PRIO (COND_PRIO+2)
637 #define ANDAND_PRIO (OROR_PRIO+2)
638 #define OR_PRIO (ANDAND_PRIO+2)
639 #define XOR_PRIO (OR_PRIO+2)
640 #define AND_PRIO (XOR_PRIO+2)
641 #define EQUAL_PRIO (AND_PRIO+2)
642 #define LESS_PRIO (EQUAL_PRIO+2)
643 #define SHIFT_PRIO (LESS_PRIO+2)
644 #define PLUS_PRIO (SHIFT_PRIO+2)
645 #define MUL_PRIO (PLUS_PRIO+2)
646 #define UNARY_PRIO (MUL_PRIO+2)
647 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
648
649 #define COMPARE(OP) \
650   top->unsignedp = 0;\
651   top->value = (unsigned1 || unsigned2) \
652   ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
653
654 /* Parse and evaluate a C expression, reading from PFILE.
655    Returns the value of the expression.  */
656
657 HOST_WIDE_INT
658 cpp_parse_expr (pfile)
659      cpp_reader *pfile;
660 {
661   /* The implementation is an operator precedence parser,
662      i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
663
664      The stack base is 'stack', and the current stack pointer is 'top'.
665      There is a stack element for each operator (only),
666      and the most recently pushed operator is 'top->op'.
667      An operand (value) is stored in the 'value' field of the stack
668      element of the operator that precedes it.
669      In that case the 'flags' field has the HAVE_VALUE flag set.  */
670
671 #define INIT_STACK_SIZE 20
672   struct operation init_stack[INIT_STACK_SIZE];
673   struct operation *stack = init_stack;
674   struct operation *limit = stack + INIT_STACK_SIZE;
675   register struct operation *top = stack;
676   int lprio, rprio;
677   int skip_evaluation = 0;
678
679   top->rprio = 0;
680   top->flags = 0;
681   for (;;)
682     {
683       struct operation op;
684       char flags = 0;
685
686       /* Read a token */
687       op =  cpp_lex (pfile, skip_evaluation);
688
689       /* See if the token is an operand, in which case go to set_value.
690          If the token is an operator, figure out its left and right
691          priorities, and then goto maybe_reduce.  */
692
693       switch (op.op)
694         {
695         case NAME:
696           abort ();
697         case INT:  case CHAR:
698           top->value = op.value;
699           top->unsignedp = op.unsignedp;
700           goto set_value;
701         case 0:
702           lprio = 0;  goto maybe_reduce;
703         case '+':  case '-':
704           /* Is this correct if unary ? FIXME */
705           flags = RIGHT_OPERAND_REQUIRED;
706           lprio = PLUS_PRIO;  rprio = lprio + 1;  goto maybe_reduce;
707         case '!':  case '~':
708           flags = RIGHT_OPERAND_REQUIRED;
709           rprio = UNARY_PRIO;  lprio = rprio + 1;  goto maybe_reduce;
710         case '*':  case '/':  case '%':
711           lprio = MUL_PRIO;  goto binop;
712         case '<':  case '>':  case LEQ:  case GEQ:
713           lprio = LESS_PRIO;  goto binop;
714         case EQUAL:  case NOTEQUAL:
715           lprio = EQUAL_PRIO;  goto binop;
716         case LSH:  case RSH:
717           lprio = SHIFT_PRIO;  goto binop;
718         case '&':  lprio = AND_PRIO;  goto binop;
719         case '^':  lprio = XOR_PRIO;  goto binop;
720         case '|':  lprio = OR_PRIO;  goto binop;
721         case ANDAND:  lprio = ANDAND_PRIO;  goto binop;
722         case OROR:  lprio = OROR_PRIO;  goto binop;
723         case ',':
724           lprio = COMMA_PRIO;  goto binop;
725         case '(':
726           lprio = PAREN_OUTER_PRIO;  rprio = PAREN_INNER_PRIO;
727           goto maybe_reduce;
728         case ')':
729           lprio = PAREN_INNER_PRIO;  rprio = PAREN_OUTER_PRIO;
730           goto maybe_reduce;
731         case ':':
732           lprio = COND_PRIO;  rprio = COND_PRIO;
733           goto maybe_reduce;
734         case '?':
735           lprio = COND_PRIO + 1;  rprio = COND_PRIO;
736           goto maybe_reduce;
737         binop:
738           flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
739           rprio = lprio + 1;
740           goto maybe_reduce;
741         default:
742           cpp_error (pfile, "invalid character in #if");
743           goto syntax_error;
744         }
745
746     set_value:
747       /* Push a value onto the stack.  */
748       if (top->flags & HAVE_VALUE)
749         {
750           cpp_error (pfile, "syntax error in #if");
751           goto syntax_error;
752         }
753       top->flags |= HAVE_VALUE;
754       continue;
755
756     maybe_reduce:
757       /* Push an operator, and check if we can reduce now.  */
758       while (top->rprio > lprio)
759         {
760           long v1 = top[-1].value, v2 = top[0].value;
761           int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
762           top--;
763           if ((top[1].flags & LEFT_OPERAND_REQUIRED)
764               && ! (top[0].flags & HAVE_VALUE))
765             {
766               cpp_error (pfile, "syntax error - missing left operand");
767               goto syntax_error;
768             }
769           if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
770               && ! (top[1].flags & HAVE_VALUE))
771             {
772               cpp_error (pfile, "syntax error - missing right operand");
773               goto syntax_error;
774             }
775           /* top[0].value = (top[1].op)(v1, v2);*/
776           switch (top[1].op)
777             {
778             case '+':
779               if (!(top->flags & HAVE_VALUE))
780                 { /* Unary '+' */
781                   top->value = v2;
782                   top->unsignedp = unsigned2;
783                   top->flags |= HAVE_VALUE;
784                 }
785               else
786                 {
787                   top->value = v1 + v2;
788                   top->unsignedp = unsigned1 || unsigned2;
789                   if (! top->unsignedp && ! skip_evaluation
790                       && ! possible_sum_sign (v1, v2, top->value))
791                     integer_overflow (pfile);
792                 }
793               break;
794             case '-':
795               if (!(top->flags & HAVE_VALUE))
796                 { /* Unary '-' */
797                   top->value = - v2;
798                   if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
799                     integer_overflow (pfile);
800                   top->unsignedp = unsigned2;
801                   top->flags |= HAVE_VALUE;
802                 }
803               else
804                 { /* Binary '-' */
805                   top->value = v1 - v2;
806                   top->unsignedp = unsigned1 || unsigned2;
807                   if (! top->unsignedp && ! skip_evaluation
808                       && ! possible_sum_sign (top->value, v2, v1))
809                     integer_overflow (pfile);
810                 }
811               break;
812             case '*':
813               top->unsignedp = unsigned1 || unsigned2;
814               if (top->unsignedp)
815                 top->value = (unsigned long) v1 * v2;
816               else if (!skip_evaluation)
817                 {
818                   top->value = v1 * v2;
819                   if (v1
820                       && (top->value / v1 != v2
821                           || (top->value & v1 & v2) < 0))
822                     integer_overflow (pfile);
823                 }
824               break;
825             case '/':
826               if (skip_evaluation)
827                 break;
828               if (v2 == 0)
829                 {
830                   cpp_error (pfile, "division by zero in #if");
831                   v2 = 1;
832                 }
833               top->unsignedp = unsigned1 || unsigned2;
834               if (top->unsignedp)
835                 top->value = (unsigned long) v1 / v2;
836               else
837                 {
838                   top->value = v1 / v2;
839                   if ((top->value & v1 & v2) < 0)
840                     integer_overflow (pfile);
841                 }
842               break;
843             case '%':
844               if (skip_evaluation)
845                 break;
846               if (v2 == 0)
847                 {
848                   cpp_error (pfile, "division by zero in #if");
849                   v2 = 1;
850                 }
851               top->unsignedp = unsigned1 || unsigned2;
852               if (top->unsignedp)
853                 top->value = (unsigned long) v1 % v2;
854               else
855                 top->value = v1 % v2;
856               break;
857             case '!':
858               if (top->flags & HAVE_VALUE)
859                 {
860                   cpp_error (pfile, "syntax error");
861                   goto syntax_error;
862                 }
863               top->value = ! v2;
864               top->unsignedp = 0;
865               top->flags |= HAVE_VALUE;
866               break;
867             case '~':
868               if (top->flags & HAVE_VALUE)
869                 {
870                   cpp_error (pfile, "syntax error");
871                   goto syntax_error;
872                 }
873               top->value = ~ v2;
874               top->unsignedp = unsigned2;
875               top->flags |= HAVE_VALUE;
876               break;
877             case '<':  COMPARE(<);  break;
878             case '>':  COMPARE(>);  break;
879             case LEQ:  COMPARE(<=); break;
880             case GEQ:  COMPARE(>=); break;
881             case EQUAL:
882               top->value = (v1 == v2);
883               top->unsignedp = 0;
884               break;
885             case NOTEQUAL:
886               top->value = (v1 != v2);
887               top->unsignedp = 0;
888               break;
889             case LSH:
890               if (skip_evaluation)
891                 break;
892               top->unsignedp = unsigned1;
893               if (v2 < 0 && ! unsigned2)
894                 top->value = right_shift (pfile, v1, unsigned1, -v2);
895               else
896                 top->value = left_shift (pfile, v1, unsigned1, v2);
897               break;
898             case RSH:
899               if (skip_evaluation)
900                 break;
901               top->unsignedp = unsigned1;
902               if (v2 < 0 && ! unsigned2)
903                 top->value = left_shift (pfile, v1, unsigned1, -v2);
904               else
905                 top->value = right_shift (pfile, v1, unsigned1, v2);
906               break;
907 #define LOGICAL(OP) \
908               top->value = v1 OP v2;\
909               top->unsignedp = unsigned1 || unsigned2;
910             case '&':  LOGICAL(&); break;
911             case '^':  LOGICAL(^);  break;
912             case '|':  LOGICAL(|);  break;
913             case ANDAND:
914               top->value = v1 && v2;  top->unsignedp = 0;
915               if (!v1) skip_evaluation--;
916               break;
917             case OROR:
918               top->value = v1 || v2;  top->unsignedp = 0;
919               if (v1) skip_evaluation--;
920               break;
921             case ',':
922               if (CPP_PEDANTIC (pfile))
923                 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
924               top->value = v2;
925               top->unsignedp = unsigned2;
926               break;
927             case '(':  case '?':
928               cpp_error (pfile, "syntax error in #if");
929               goto syntax_error;
930             case ':':
931               if (top[0].op != '?')
932                 {
933                   cpp_error (pfile,
934                              "syntax error ':' without preceding '?'");
935                   goto syntax_error;
936                 }
937               else if (! (top[1].flags & HAVE_VALUE)
938                        || !(top[-1].flags & HAVE_VALUE)
939                        || !(top[0].flags & HAVE_VALUE))
940                 {
941                   cpp_error (pfile, "bad syntax for ?: operator");
942                   goto syntax_error;
943                 }
944               else
945                 {
946                   top--;
947                   if (top->value) skip_evaluation--;
948                   top->value = top->value ? v1 : v2;
949                   top->unsignedp = unsigned1 || unsigned2;
950                 }
951               break;
952             case ')':
953               if ((top[1].flags & HAVE_VALUE)
954                   || ! (top[0].flags & HAVE_VALUE)
955                   || top[0].op != '('
956                   || (top[-1].flags & HAVE_VALUE))
957                 {
958                   cpp_error (pfile, "mismatched parentheses in #if");
959                   goto syntax_error;
960                 }
961               else
962                 {
963                   top--;
964                   top->value = v1;
965                   top->unsignedp = unsigned1;
966                   top->flags |= HAVE_VALUE;
967                 }
968               break;
969             default:
970               fprintf (stderr,
971                        top[1].op >= ' ' && top[1].op <= '~'
972                        ? "unimplemented operator '%c'\n"
973                        : "unimplemented operator '\\%03o'\n",
974                        top[1].op);
975             }
976         }
977       if (op.op == 0)
978         {
979           if (top != stack)
980             cpp_error (pfile, "internal error in #if expression");
981           if (stack != init_stack)
982             free (stack);
983           return top->value;
984         }
985       top++;
986       
987       /* Check for and handle stack overflow.  */
988       if (top == limit)
989         {
990           struct operation *new_stack;
991           int old_size = (char *) limit - (char *) stack;
992           int new_size = 2 * old_size;
993           if (stack != init_stack)
994             new_stack = (struct operation *) xrealloc (stack, new_size);
995           else
996             {
997               new_stack = (struct operation *) xmalloc (new_size);
998               bcopy ((char *) stack, (char *) new_stack, old_size);
999             }
1000           stack = new_stack;
1001           top = (struct operation *) ((char *) new_stack + old_size);
1002           limit = (struct operation *) ((char *) new_stack + new_size);
1003         }
1004       
1005       top->flags = flags;
1006       top->rprio = rprio;
1007       top->op = op.op;
1008       if ((op.op == OROR && top[-1].value)
1009           || (op.op == ANDAND && !top[-1].value)
1010           || (op.op == '?' && !top[-1].value))
1011         {
1012           skip_evaluation++;
1013         }
1014       else if (op.op == ':')
1015         {
1016           if (top[-2].value) /* Was condition true? */
1017             skip_evaluation++;
1018           else
1019             skip_evaluation--;
1020         }
1021     }
1022  syntax_error:
1023   if (stack != init_stack)
1024     free (stack);
1025   skip_rest_of_line (pfile);
1026   return 0;
1027 }