OSDN Git Service

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