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