OSDN Git Service

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