OSDN Git Service

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