* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / cppexp.c
blob040bf86f48c365fcadb26d8bb6b9511a813f42fd
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));
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 struct suffix
66 const unsigned char s[4];
67 const unsigned char u;
68 const unsigned char l;
71 static const struct suffix vsuf_1[] = {
72 { "u", 1, 0 }, { "U", 1, 0 },
73 { "l", 0, 1 }, { "L", 0, 1 }
76 static 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 }
82 static 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 }
86 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
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. */
92 static struct op
93 parse_number (pfile, tok)
94 cpp_reader *pfile;
95 const cpp_token *tok;
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;
108 op.unsignedp = 0;
110 if (p[0] == '0')
112 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
114 p += 2;
115 base = 16;
117 else
119 p += 1;
120 base = 8;
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));
128 for(; p < end; p++)
130 c = *p;
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;
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;
150 if (p < end)
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");
162 /* Determine the suffix. l means long, and u means unsigned.
163 See the suffix tables, above. */
164 switch (end - p)
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;
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;
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");
188 if (base <= largest_digit)
189 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
191 if (overflow)
192 cpp_pedwarn (pfile, "integer constant out of range");
194 /* If too big to be signed, consider it unsigned. */
195 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
197 if (base == 10)
198 cpp_warning (pfile, "integer constant is so large that it is unsigned");
199 op.unsignedp = 1;
202 op.value = n;
203 op.op = CPP_NUMBER;
204 return op;
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;
214 static struct op
215 parse_defined (pfile)
216 cpp_reader *pfile;
218 int paren = 0;
219 cpp_hashnode *node = 0;
220 const cpp_token *token;
221 struct op op;
223 /* Don't expand macros. */
224 pfile->state.prevent_expansion++;
226 token = cpp_get_token (pfile);
227 if (token->type == CPP_OPEN_PAREN)
229 paren = 1;
230 token = cpp_get_token (pfile);
233 if (token->type == CPP_NAME)
235 node = token->val.node;
236 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
238 cpp_error (pfile, "missing ')' after \"defined\"");
239 node = 0;
242 else
244 cpp_error (pfile, "operator \"defined\" requires an identifier");
245 if (token->flags & NAMED_OP)
247 cpp_token op;
249 op.flags = 0;
250 op.type = token->type;
251 cpp_error (pfile,
252 "(\"%s\" is an alternative token for \"%s\" in C++)",
253 cpp_token_as_text (pfile, token),
254 cpp_token_as_text (pfile, &op));
258 if (!node)
259 op.op = CPP_ERROR;
260 else
262 op.value = node->type == NT_MACRO;
263 op.unsignedp = 0;
264 op.op = CPP_NUMBER;
266 /* A possible controlling macro of the form #if !defined ().
267 _cpp_parse_expr checks there was no other junk on the line. */
268 pfile->mi_ind_cmacro = node;
271 pfile->state.prevent_expansion--;
272 return op;
275 /* Read a token. The returned type is CPP_NUMBER for a valid number
276 (an interpreted preprocessing number or character constant, or the
277 result of the "defined" or "#" operators), CPP_ERROR on error,
278 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;
285 struct op op;
286 const cpp_token *token = cpp_get_token (pfile);
288 switch (token->type)
290 case CPP_NUMBER:
291 return parse_number (pfile, token);
293 case CPP_CHAR:
294 case CPP_WCHAR:
296 unsigned int chars_seen;
298 /* This is always a signed type. */
299 op.unsignedp = 0;
300 op.op = CPP_NUMBER;
301 op.value = cpp_interpret_charconst (pfile, token, 1, 0, &chars_seen);
302 return op;
305 case CPP_STRING:
306 case CPP_WSTRING:
307 SYNTAX_ERROR ("string constants are not valid in #if");
309 case CPP_OTHER:
310 if (ISGRAPH (token->val.c))
311 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
312 else
313 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
315 case CPP_NAME:
316 if (token->val.node == pfile->spec_nodes.n_defined)
318 if (pfile->context->prev && CPP_PEDANTIC (pfile))
319 cpp_pedwarn (pfile, "\"defined\" operator appears during macro expansion");
321 return parse_defined (pfile);
323 else if (CPP_OPTION (pfile, cplusplus)
324 && (token->val.node == pfile->spec_nodes.n_true
325 || token->val.node == pfile->spec_nodes.n_false))
327 op.op = CPP_NUMBER;
328 op.unsignedp = 0;
329 op.value = (token->val.node == pfile->spec_nodes.n_true);
331 /* Warn about use of true or false in #if when pedantic
332 and stdbool.h has not been included. */
333 if (CPP_PEDANTIC (pfile)
334 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
335 cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
336 NODE_NAME (token->val.node));
337 return op;
339 else
341 op.op = CPP_NUMBER;
342 op.unsignedp = 0;
343 op.value = 0;
345 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
346 cpp_warning (pfile, "\"%s\" is not defined",
347 NODE_NAME (token->val.node));
348 return op;
351 case CPP_HASH:
353 int temp;
355 op.op = CPP_NUMBER;
356 if (_cpp_test_assertion (pfile, &temp))
357 op.op = CPP_ERROR;
358 op.unsignedp = 0;
359 op.value = temp;
360 return op;
363 default:
364 if (((int) token->type > (int) CPP_EQ
365 && (int) token->type < (int) CPP_PLUS_EQ)
366 || token->type == CPP_EOF)
368 op.op = token->type;
369 return op;
372 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
373 cpp_token_as_text (pfile, token));
376 syntax_error:
377 op.op = CPP_ERROR;
378 return op;
381 static void
382 integer_overflow (pfile)
383 cpp_reader *pfile;
385 if (CPP_PEDANTIC (pfile))
386 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
389 static HOST_WIDEST_INT
390 left_shift (pfile, a, unsignedp, b)
391 cpp_reader *pfile;
392 HOST_WIDEST_INT a;
393 unsigned int unsignedp;
394 unsigned HOST_WIDEST_INT b;
396 if (b >= HOST_BITS_PER_WIDEST_INT)
398 if (! unsignedp && a != 0)
399 integer_overflow (pfile);
400 return 0;
402 else if (unsignedp)
403 return (unsigned HOST_WIDEST_INT) a << b;
404 else
406 HOST_WIDEST_INT l = a << b;
407 if (l >> b != a)
408 integer_overflow (pfile);
409 return l;
413 static HOST_WIDEST_INT
414 right_shift (pfile, a, unsignedp, b)
415 cpp_reader *pfile ATTRIBUTE_UNUSED;
416 HOST_WIDEST_INT a;
417 unsigned int unsignedp;
418 unsigned HOST_WIDEST_INT b;
420 if (b >= HOST_BITS_PER_WIDEST_INT)
421 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
422 else if (unsignedp)
423 return (unsigned HOST_WIDEST_INT) a >> b;
424 else
425 return a >> b;
428 /* Operator precedence and flags table.
430 After an operator is returned from the lexer, if it has priority less
431 than or equal to the operator on the top of the stack, we reduce the
432 stack by one operator and repeat the test. Since equal priorities
433 reduce, this is naturally left-associative.
435 We handle right-associative operators by clearing the lower bit of all
436 left-associative operators, and setting it for right-associative ones.
437 After the reduction phase of a new operator, just before it is pushed
438 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
439 during the reduction phase, the current right-associative operator has
440 a priority one greater than any other operator of otherwise equal
441 precedence that has been pushed on the top of the stack. This avoids
442 a reduction pass, and effectively makes the logic right-associative.
444 The remaining cases are '(' and ')'. We handle '(' by skipping the
445 reduction phase completely. ')' is given lower priority than
446 everything else, including '(', effectively forcing a reduction of the
447 parenthesised expression. If there is no matching '(', the stack will
448 be reduced all the way to the beginning, exiting the parser in the
449 same way as the ultra-low priority end-of-expression dummy operator.
450 The exit code checks to see if the operator that caused it is ')', and
451 if so outputs an appropriate error message.
453 The parser assumes all shifted operators require a right operand
454 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
455 These semantics are automatically checked, any extra semantics need to
456 be handled with operator-specific code. */
458 #define FLAG_BITS 8
459 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
460 #define PRIO_SHIFT (FLAG_BITS + 1)
461 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
462 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
464 /* Flags. */
465 #define HAVE_VALUE (1 << 0)
466 #define NO_L_OPERAND (1 << 1)
467 #define NO_R_OPERAND (1 << 2)
468 #define SHORT_CIRCUIT (1 << 3)
470 /* Priority and flag combinations. */
471 #define RIGHT_ASSOC (1 << FLAG_BITS)
472 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
473 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
474 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
475 #define COMMA_PRIO (3 << PRIO_SHIFT)
476 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
477 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
478 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
479 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
480 #define OR_PRIO (8 << PRIO_SHIFT)
481 #define XOR_PRIO (9 << PRIO_SHIFT)
482 #define AND_PRIO (10 << PRIO_SHIFT)
483 #define MINMAX_PRIO (11 << PRIO_SHIFT)
484 #define EQUAL_PRIO (12 << PRIO_SHIFT)
485 #define LESS_PRIO (13 << PRIO_SHIFT)
486 #define SHIFT_PRIO (14 << PRIO_SHIFT)
487 #define PLUS_PRIO (15 << PRIO_SHIFT)
488 #define MUL_PRIO (16 << PRIO_SHIFT)
489 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
491 /* Operator to priority map. Must be in the same order as the first
492 N entries of enum cpp_ttype. */
493 static const short
494 op_to_prio[] =
496 /* EQ */ 0, /* dummy entry - can't happen */
497 /* NOT */ UNARY_PRIO,
498 /* GREATER */ LESS_PRIO,
499 /* LESS */ LESS_PRIO,
500 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
501 /* MINUS */ UNARY_PRIO, /* or binary */
502 /* MULT */ MUL_PRIO,
503 /* DIV */ MUL_PRIO,
504 /* MOD */ MUL_PRIO,
505 /* AND */ AND_PRIO,
506 /* OR */ OR_PRIO,
507 /* XOR */ XOR_PRIO,
508 /* RSHIFT */ SHIFT_PRIO,
509 /* LSHIFT */ SHIFT_PRIO,
510 /* MIN */ MINMAX_PRIO, /* C++ specific */
511 /* MAX */ MINMAX_PRIO, /* extensions */
513 /* COMPL */ UNARY_PRIO,
514 /* AND_AND */ ANDAND_PRIO,
515 /* OR_OR */ OROR_PRIO,
516 /* QUERY */ COND_PRIO,
517 /* COLON */ COLON_PRIO,
518 /* COMMA */ COMMA_PRIO,
519 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
520 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
521 /* EQ_EQ */ EQUAL_PRIO,
522 /* NOT_EQ */ EQUAL_PRIO,
523 /* GREATER_EQ */ LESS_PRIO,
524 /* LESS_EQ */ LESS_PRIO
527 #define COMPARE(OP) \
528 top->unsignedp = 0; \
529 top->value = (unsigned1 | unsigned2) \
530 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
531 : (v1 OP v2)
532 #define EQUALITY(OP) \
533 top->value = v1 OP v2; \
534 top->unsignedp = 0;
535 #define BITWISE(OP) \
536 top->value = v1 OP v2; \
537 top->unsignedp = unsigned1 | unsigned2;
538 #define MINMAX(OP) \
539 top->value = (v1 OP v2) ? v1 : v2; \
540 top->unsignedp = unsigned1 | unsigned2;
541 #define UNARY(OP) \
542 top->value = OP v2; \
543 top->unsignedp = unsigned2; \
544 top->flags |= HAVE_VALUE;
545 #define SHIFT(PSH, MSH) \
546 if (skip_evaluation) \
547 break; \
548 top->unsignedp = unsigned1; \
549 if (v2 < 0 && ! unsigned2) \
550 top->value = MSH (pfile, v1, unsigned1, -v2); \
551 else \
552 top->value = PSH (pfile, v1, unsigned1, v2);
554 /* Parse and evaluate a C expression, reading from PFILE.
555 Returns the truth value of the expression. */
558 _cpp_parse_expr (pfile)
559 cpp_reader *pfile;
561 /* The implementation is an operator precedence parser, i.e. a
562 bottom-up parser, using a stack for not-yet-reduced tokens.
564 The stack base is 'stack', and the current stack pointer is 'top'.
565 There is a stack element for each operator (only),
566 and the most recently pushed operator is 'top->op'.
567 An operand (value) is stored in the 'value' field of the stack
568 element of the operator that precedes it.
569 In that case the 'flags' field has the HAVE_VALUE flag set. */
571 #define INIT_STACK_SIZE 20
572 struct op init_stack[INIT_STACK_SIZE];
573 struct op *stack = init_stack;
574 struct op *limit = stack + INIT_STACK_SIZE;
575 struct op *top = stack + 1;
576 int skip_evaluation = 0;
577 int result;
578 unsigned int lex_count, saw_leading_not;
580 /* Set up detection of #if ! defined(). */
581 pfile->mi_ind_cmacro = 0;
582 saw_leading_not = 0;
583 lex_count = 0;
585 /* We've finished when we try to reduce this. */
586 top->op = CPP_EOF;
587 /* Nifty way to catch missing '('. */
588 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
589 /* Avoid missing right operand checks. */
590 top->flags = NO_R_OPERAND;
592 for (;;)
594 unsigned int prio;
595 unsigned int flags;
596 struct op op;
598 /* Read a token */
599 op = lex (pfile, skip_evaluation);
600 lex_count++;
602 /* If the token is an operand, push its value and get next
603 token. If it is an operator, get its priority and flags, and
604 try to reduce the expression on the stack. */
605 switch (op.op)
607 case CPP_ERROR:
608 goto syntax_error;
609 push_immediate:
610 case CPP_NUMBER:
611 /* Push a value onto the stack. */
612 if (top->flags & HAVE_VALUE)
613 SYNTAX_ERROR ("missing binary operator");
614 top->value = op.value;
615 top->unsignedp = op.unsignedp;
616 top->flags |= HAVE_VALUE;
617 continue;
619 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
621 case CPP_NOT:
622 saw_leading_not = lex_count == 1;
623 prio = op_to_prio[op.op];
624 break;
625 case CPP_PLUS:
626 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
627 /* else unary; fall through */
628 default: prio = op_to_prio[op.op]; break;
631 /* Separate the operator's code into priority and flags. */
632 flags = EXTRACT_FLAGS(prio);
633 prio = EXTRACT_PRIO(prio);
634 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
635 goto skip_reduction;
637 /* Check for reductions. Then push the operator. */
638 while (prio <= top->prio)
640 HOST_WIDEST_INT v1, v2;
641 unsigned int unsigned1, unsigned2;
643 /* Most operators that can appear on the stack require a
644 right operand. Check this before trying to reduce. */
645 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
647 if (top->op == CPP_OPEN_PAREN)
648 SYNTAX_ERROR ("void expression between '(' and ')'");
649 else
650 SYNTAX_ERROR2 ("operator '%s' has no right operand",
651 op_as_text (pfile, top->op));
654 unsigned2 = top->unsignedp, v2 = top->value;
655 top--;
656 unsigned1 = top->unsignedp, v1 = top->value;
658 /* Now set top->value = (top[1].op)(v1, v2); */
659 switch (top[1].op)
661 default:
662 cpp_ice (pfile, "impossible operator '%s'",
663 op_as_text (pfile, top[1].op));
664 goto syntax_error;
666 case CPP_NOT: UNARY(!); break;
667 case CPP_COMPL: UNARY(~); break;
668 case CPP_LESS: COMPARE(<); break;
669 case CPP_GREATER: COMPARE(>); break;
670 case CPP_LESS_EQ: COMPARE(<=); break;
671 case CPP_GREATER_EQ: COMPARE(>=); break;
672 case CPP_EQ_EQ: EQUALITY(==); break;
673 case CPP_NOT_EQ: EQUALITY(!=); break;
674 case CPP_AND: BITWISE(&); break;
675 case CPP_XOR: BITWISE(^); break;
676 case CPP_OR: BITWISE(|); break;
677 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
678 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
679 case CPP_MIN: MINMAX(<); break;
680 case CPP_MAX: MINMAX(>); break;
682 case CPP_PLUS:
683 if (!(top->flags & HAVE_VALUE))
685 /* Can't use UNARY(+) because K+R C did not have unary
686 plus. Can't use UNARY() because some compilers object
687 to the empty argument. */
688 top->value = v2;
689 top->unsignedp = unsigned2;
690 top->flags |= HAVE_VALUE;
692 if (CPP_WTRADITIONAL (pfile))
693 cpp_warning (pfile,
694 "traditional C rejects the unary plus operator");
696 else
698 top->value = v1 + v2;
699 top->unsignedp = unsigned1 | unsigned2;
700 if (! top->unsignedp && ! skip_evaluation
701 && ! possible_sum_sign (v1, v2, top->value))
702 integer_overflow (pfile);
704 break;
705 case CPP_MINUS:
706 if (!(top->flags & HAVE_VALUE))
708 UNARY(-);
709 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
710 integer_overflow (pfile);
712 else
713 { /* Binary '-' */
714 top->value = v1 - v2;
715 top->unsignedp = unsigned1 | unsigned2;
716 if (! top->unsignedp && ! skip_evaluation
717 && ! possible_sum_sign (top->value, v2, v1))
718 integer_overflow (pfile);
720 break;
721 case CPP_MULT:
722 top->unsignedp = unsigned1 | unsigned2;
723 if (top->unsignedp)
724 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
725 else if (!skip_evaluation)
727 top->value = v1 * v2;
728 if (v1 && (top->value / v1 != v2
729 || (top->value & v1 & v2) < 0))
730 integer_overflow (pfile);
732 break;
733 case CPP_DIV:
734 case CPP_MOD:
735 if (skip_evaluation)
736 break;
737 if (v2 == 0)
738 SYNTAX_ERROR ("division by zero in #if");
739 top->unsignedp = unsigned1 | unsigned2;
740 if (top[1].op == CPP_DIV)
742 if (top->unsignedp)
743 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
744 else
746 top->value = v1 / v2;
747 if ((top->value & v1 & v2) < 0)
748 integer_overflow (pfile);
751 else
753 if (top->unsignedp)
754 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
755 else
756 top->value = v1 % v2;
758 break;
760 case CPP_OR_OR:
761 top->value = v1 || v2;
762 top->unsignedp = 0;
763 if (v1) skip_evaluation--;
764 break;
765 case CPP_AND_AND:
766 top->value = v1 && v2;
767 top->unsignedp = 0;
768 if (!v1) skip_evaluation--;
769 break;
770 case CPP_COMMA:
771 if (CPP_PEDANTIC (pfile))
772 cpp_pedwarn (pfile, "comma operator in operand of #if");
773 top->value = v2;
774 top->unsignedp = unsigned2;
775 break;
776 case CPP_QUERY:
777 SYNTAX_ERROR ("syntax error '?' without following ':'");
778 case CPP_COLON:
779 if (top[0].op != CPP_QUERY)
780 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
781 top--;
782 if (top->value) skip_evaluation--;
783 top->value = top->value ? v1 : v2;
784 top->unsignedp = unsigned1 | unsigned2;
785 break;
786 case CPP_OPEN_PAREN:
787 if (op.op != CPP_CLOSE_PAREN)
788 SYNTAX_ERROR ("missing ')' in expression");
789 op.value = v2;
790 op.unsignedp = unsigned2;
791 goto push_immediate;
792 case CPP_EOF:
793 /* Reducing this dummy operator indicates we've finished. */
794 if (op.op == CPP_CLOSE_PAREN)
795 SYNTAX_ERROR ("missing '(' in expression");
796 goto done;
800 /* Handle short-circuit evaluations. */
801 if (flags & SHORT_CIRCUIT)
802 switch (op.op)
804 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
805 case CPP_AND_AND:
806 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
807 case CPP_COLON:
808 if (top[-1].value) /* Was '?' condition true? */
809 skip_evaluation++;
810 else
811 skip_evaluation--;
812 default:
813 break;
816 skip_reduction:
817 /* Check we have a left operand iff we need one. */
818 if (flags & NO_L_OPERAND)
820 if (top->flags & HAVE_VALUE)
821 SYNTAX_ERROR2 ("missing binary operator before '%s'",
822 op_as_text (pfile, op.op));
824 else
826 if (!(top->flags & HAVE_VALUE))
827 SYNTAX_ERROR2 ("operator '%s' has no left operand",
828 op_as_text (pfile, op.op));
831 /* Check for and handle stack overflow. */
832 top++;
833 if (top == limit)
835 struct op *new_stack;
836 int old_size = (char *) limit - (char *) stack;
837 int new_size = 2 * old_size;
838 if (stack != init_stack)
839 new_stack = (struct op *) xrealloc (stack, new_size);
840 else
842 new_stack = (struct op *) xmalloc (new_size);
843 memcpy (new_stack, stack, old_size);
845 stack = new_stack;
846 top = (struct op *) ((char *) new_stack + old_size);
847 limit = (struct op *) ((char *) new_stack + new_size);
850 top->flags = flags;
851 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
852 top->op = op.op;
855 done:
856 /* The controlling macro expression is only valid if we called lex 3
857 times: <!> <defined expression> and <EOF>. push_conditional ()
858 checks that we are at top-of-file. */
859 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
860 pfile->mi_ind_cmacro = 0;
862 result = (top[1].value != 0);
864 if (top != stack)
865 CPP_ICE ("unbalanced stack in #if");
866 else if (!(top[1].flags & HAVE_VALUE))
868 SYNTAX_ERROR ("#if with no expression");
869 syntax_error:
870 result = 0; /* Return 0 on syntax error. */
873 /* Free dynamic stack if we allocated one. */
874 if (stack != init_stack)
875 free (stack);
876 return result;
879 static const unsigned char *
880 op_as_text (pfile, op)
881 cpp_reader *pfile;
882 enum cpp_ttype op;
884 cpp_token token;
886 token.type = op;
887 token.flags = 0;
888 return cpp_token_as_text (pfile, &token);