OSDN Git Service

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