OSDN Git Service

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