* config/xtensa/xtensa.h (ASM_OUTPUT_POOL_PROLOGUE): Emit a
[official-gcc.git] / gcc / cppexp.c
blob5766a7903c07fb6a9bccf0a57274e737e5a5e9ab
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.
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 }
87 /* Parse and convert what is presumably an integer in TOK. Accepts
88 decimal, hex, or octal with or without size suffixes. Returned op
89 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
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 (ISDIGIT (c)
131 || (base == 16 && ISXDIGIT (c)))
132 digit = hex_value (c);
133 else
134 break;
136 if (largest_digit < digit)
137 largest_digit = digit;
138 nd = n * base + digit;
139 overflow |= MAX_over_base < n || nd < n;
140 n = nd;
143 if (p < end)
145 /* Check for a floating point constant. Note that float constants
146 with an exponent or suffix but no decimal point are technically
147 invalid (C99 6.4.4.2) but accepted elsewhere. */
148 if ((c == '.' || c == 'F' || c == 'f')
149 || (base == 10 && (c == 'E' || c == 'e')
150 && p+1 < end && (p[1] == '+' || p[1] == '-'))
151 || (base == 16 && (c == 'P' || c == 'p')
152 && p+1 < end && (p[1] == '+' || p[1] == '-')))
153 SYNTAX_ERROR ("floating point numbers are not valid in #if");
155 /* Determine the suffix. l means long, and u means unsigned.
156 See the suffix tables, above. */
157 switch (end - p)
159 case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
160 case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
161 case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
162 default: goto invalid_suffix;
165 for (i = 0; i < nsuff; i++)
166 if (memcmp (p, sufftab[i].s, end - p) == 0)
167 break;
168 if (i == nsuff)
169 goto invalid_suffix;
170 op.unsignedp = sufftab[i].u;
172 if (CPP_WTRADITIONAL (pfile)
173 && sufftab[i].u
174 && ! cpp_sys_macro_p (pfile))
175 cpp_warning (pfile, "traditional C rejects the `U' suffix");
176 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
177 && ! CPP_OPTION (pfile, c99))
178 cpp_pedwarn (pfile, "too many 'l' suffixes in integer constant");
181 if (base <= largest_digit)
182 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
184 if (overflow)
185 cpp_pedwarn (pfile, "integer constant out of range");
187 /* If too big to be signed, consider it unsigned. */
188 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
190 if (base == 10)
191 cpp_warning (pfile, "integer constant is so large that it is unsigned");
192 op.unsignedp = 1;
195 op.value = n;
196 op.op = CPP_NUMBER;
197 return op;
199 invalid_suffix:
200 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
201 (int) (end - p), p);
202 syntax_error:
203 op.op = CPP_ERROR;
204 return op;
207 /* Handle meeting "defined" in a preprocessor expression. */
208 static struct op
209 parse_defined (pfile)
210 cpp_reader *pfile;
212 int paren = 0;
213 cpp_hashnode *node = 0;
214 const cpp_token *token;
215 struct op op;
216 cpp_context *initial_context = pfile->context;
218 /* Don't expand macros. */
219 pfile->state.prevent_expansion++;
221 token = cpp_get_token (pfile);
222 if (token->type == CPP_OPEN_PAREN)
224 paren = 1;
225 token = cpp_get_token (pfile);
228 if (token->type == CPP_NAME)
230 node = token->val.node;
231 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
233 cpp_error (pfile, "missing ')' after \"defined\"");
234 node = 0;
237 else
239 cpp_error (pfile, "operator \"defined\" requires an identifier");
240 if (token->flags & NAMED_OP)
242 cpp_token op;
244 op.flags = 0;
245 op.type = token->type;
246 cpp_error (pfile,
247 "(\"%s\" is an alternative token for \"%s\" in C++)",
248 cpp_token_as_text (pfile, token),
249 cpp_token_as_text (pfile, &op));
253 if (!node)
254 op.op = CPP_ERROR;
255 else
257 if (pfile->context != initial_context)
258 cpp_warning (pfile, "this use of \"defined\" may not be portable");
260 op.value = node->type == NT_MACRO;
261 op.unsignedp = 0;
262 op.op = CPP_NUMBER;
264 /* A possible controlling macro of the form #if !defined ().
265 _cpp_parse_expr checks there was no other junk on the line. */
266 pfile->mi_ind_cmacro = node;
269 pfile->state.prevent_expansion--;
270 return op;
273 /* Read a token. The returned type is CPP_NUMBER for a valid number
274 (an interpreted preprocessing number or character constant, or the
275 result of the "defined" or "#" operators), CPP_ERROR on error,
276 CPP_EOF, or the type of an operator token. */
277 static struct op
278 lex (pfile, skip_evaluation)
279 cpp_reader *pfile;
280 int skip_evaluation;
282 struct op op;
283 const cpp_token *token = cpp_get_token (pfile);
285 switch (token->type)
287 case CPP_NUMBER:
288 return parse_number (pfile, token);
290 case CPP_CHAR:
291 case CPP_WCHAR:
293 unsigned int chars_seen;
295 if (token->type == CPP_CHAR)
296 op.unsignedp = 0;
297 else
298 op.unsignedp = WCHAR_UNSIGNED;
299 op.op = CPP_NUMBER;
300 op.value = cpp_interpret_charconst (pfile, token, 1, &chars_seen);
301 return op;
304 case CPP_STRING:
305 case CPP_WSTRING:
306 SYNTAX_ERROR ("string constants are not valid in #if");
308 case CPP_OTHER:
309 if (ISGRAPH (token->val.c))
310 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
311 else
312 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
314 case CPP_NAME:
315 if (token->val.node == pfile->spec_nodes.n_defined)
316 return parse_defined (pfile);
317 else if (CPP_OPTION (pfile, cplusplus)
318 && (token->val.node == pfile->spec_nodes.n_true
319 || token->val.node == pfile->spec_nodes.n_false))
321 op.op = CPP_NUMBER;
322 op.unsignedp = 0;
323 op.value = (token->val.node == pfile->spec_nodes.n_true);
325 /* Warn about use of true or false in #if when pedantic
326 and stdbool.h has not been included. */
327 if (CPP_PEDANTIC (pfile)
328 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
329 cpp_pedwarn (pfile, "ISO C++ does not permit \"%s\" in #if",
330 NODE_NAME (token->val.node));
331 return op;
333 else
335 op.op = CPP_NUMBER;
336 op.unsignedp = 0;
337 op.value = 0;
339 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
340 cpp_warning (pfile, "\"%s\" is not defined",
341 NODE_NAME (token->val.node));
342 return op;
345 case CPP_HASH:
347 int temp;
349 op.op = CPP_NUMBER;
350 if (_cpp_test_assertion (pfile, &temp))
351 op.op = CPP_ERROR;
352 op.unsignedp = 0;
353 op.value = temp;
354 return op;
357 default:
358 if (((int) token->type > (int) CPP_EQ
359 && (int) token->type < (int) CPP_PLUS_EQ)
360 || token->type == CPP_EOF)
362 op.op = token->type;
363 return op;
366 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
367 cpp_token_as_text (pfile, token));
370 syntax_error:
371 op.op = CPP_ERROR;
372 return op;
375 /* Warn if appropriate on overflow. */
376 static void
377 integer_overflow (pfile)
378 cpp_reader *pfile;
380 if (CPP_PEDANTIC (pfile))
381 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
384 /* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
385 unsigned. */
386 static HOST_WIDEST_INT
387 left_shift (pfile, a, unsignedp, b)
388 cpp_reader *pfile;
389 HOST_WIDEST_INT a;
390 unsigned int unsignedp;
391 unsigned HOST_WIDEST_INT b;
393 if (b >= HOST_BITS_PER_WIDEST_INT)
395 if (! unsignedp && a != 0)
396 integer_overflow (pfile);
397 return 0;
399 else if (unsignedp)
400 return (unsigned HOST_WIDEST_INT) a << b;
401 else
403 HOST_WIDEST_INT l = a << b;
404 if (l >> b != a)
405 integer_overflow (pfile);
406 return l;
410 /* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
411 unsigned. */
412 static HOST_WIDEST_INT
413 right_shift (pfile, a, unsignedp, b)
414 cpp_reader *pfile ATTRIBUTE_UNUSED;
415 HOST_WIDEST_INT a;
416 unsigned int unsignedp;
417 unsigned HOST_WIDEST_INT b;
419 if (b >= HOST_BITS_PER_WIDEST_INT)
420 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
421 else if (unsignedp)
422 return (unsigned HOST_WIDEST_INT) a >> b;
423 else
424 return a >> b;
427 /* Operator precedence and flags table.
429 After an operator is returned from the lexer, if it has priority less
430 than or equal to the operator on the top of the stack, we reduce the
431 stack by one operator and repeat the test. Since equal priorities
432 reduce, this is naturally left-associative.
434 We handle right-associative operators by clearing the lower bit of all
435 left-associative operators, and setting it for right-associative ones.
436 After the reduction phase of a new operator, just before it is pushed
437 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
438 during the reduction phase, the current right-associative operator has
439 a priority one greater than any other operator of otherwise equal
440 precedence that has been pushed on the top of the stack. This avoids
441 a reduction pass, and effectively makes the logic right-associative.
443 The remaining cases are '(' and ')'. We handle '(' by skipping the
444 reduction phase completely. ')' is given lower priority than
445 everything else, including '(', effectively forcing a reduction of the
446 parenthesised expression. If there is no matching '(', the stack will
447 be reduced all the way to the beginning, exiting the parser in the
448 same way as the ultra-low priority end-of-expression dummy operator.
449 The exit code checks to see if the operator that caused it is ')', and
450 if so outputs an appropriate error message.
452 The parser assumes all shifted operators require a right operand
453 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
454 These semantics are automatically checked, any extra semantics need to
455 be handled with operator-specific code. */
457 #define FLAG_BITS 8
458 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
459 #define PRIO_SHIFT (FLAG_BITS + 1)
460 #define EXTRACT_PRIO(CNST) ((CNST) >> FLAG_BITS)
461 #define EXTRACT_FLAGS(CNST) ((CNST) & FLAG_MASK)
463 /* Flags. */
464 #define HAVE_VALUE (1 << 0)
465 #define NO_L_OPERAND (1 << 1)
466 #define NO_R_OPERAND (1 << 2)
467 #define SHORT_CIRCUIT (1 << 3)
469 /* Priority and flag combinations. */
470 #define RIGHT_ASSOC (1 << FLAG_BITS)
471 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
472 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
473 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
474 #define COMMA_PRIO (3 << PRIO_SHIFT)
475 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
476 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
477 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
478 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
479 #define OR_PRIO (8 << PRIO_SHIFT)
480 #define XOR_PRIO (9 << PRIO_SHIFT)
481 #define AND_PRIO (10 << PRIO_SHIFT)
482 #define MINMAX_PRIO (11 << PRIO_SHIFT)
483 #define EQUAL_PRIO (12 << PRIO_SHIFT)
484 #define LESS_PRIO (13 << PRIO_SHIFT)
485 #define SHIFT_PRIO (14 << PRIO_SHIFT)
486 #define PLUS_PRIO (15 << PRIO_SHIFT)
487 #define MUL_PRIO (16 << PRIO_SHIFT)
488 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
490 /* Operator to priority map. Must be in the same order as the first
491 N entries of enum cpp_ttype. */
492 static const short
493 op_to_prio[] =
495 /* EQ */ 0, /* dummy entry - can't happen */
496 /* NOT */ UNARY_PRIO,
497 /* GREATER */ LESS_PRIO,
498 /* LESS */ LESS_PRIO,
499 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
500 /* MINUS */ UNARY_PRIO, /* or binary */
501 /* MULT */ MUL_PRIO,
502 /* DIV */ MUL_PRIO,
503 /* MOD */ MUL_PRIO,
504 /* AND */ AND_PRIO,
505 /* OR */ OR_PRIO,
506 /* XOR */ XOR_PRIO,
507 /* RSHIFT */ SHIFT_PRIO,
508 /* LSHIFT */ SHIFT_PRIO,
509 /* MIN */ MINMAX_PRIO, /* C++ specific */
510 /* MAX */ MINMAX_PRIO, /* extensions */
512 /* COMPL */ UNARY_PRIO,
513 /* AND_AND */ ANDAND_PRIO,
514 /* OR_OR */ OROR_PRIO,
515 /* QUERY */ COND_PRIO,
516 /* COLON */ COLON_PRIO,
517 /* COMMA */ COMMA_PRIO,
518 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
519 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
520 /* EQ_EQ */ EQUAL_PRIO,
521 /* NOT_EQ */ EQUAL_PRIO,
522 /* GREATER_EQ */ LESS_PRIO,
523 /* LESS_EQ */ LESS_PRIO
526 #define COMPARE(OP) \
527 top->unsignedp = 0; \
528 top->value = (unsigned1 | unsigned2) \
529 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
530 : (v1 OP v2)
531 #define EQUALITY(OP) \
532 top->value = v1 OP v2; \
533 top->unsignedp = 0;
534 #define BITWISE(OP) \
535 top->value = v1 OP v2; \
536 top->unsignedp = unsigned1 | unsigned2;
537 #define MINMAX(OP) \
538 top->value = (v1 OP v2) ? v1 : v2; \
539 top->unsignedp = unsigned1 | unsigned2;
540 #define UNARY(OP) \
541 top->value = OP v2; \
542 top->unsignedp = unsigned2; \
543 top->flags |= HAVE_VALUE;
544 #define SHIFT(PSH, MSH) \
545 if (skip_evaluation) \
546 break; \
547 top->unsignedp = unsigned1; \
548 if (v2 < 0 && ! unsigned2) \
549 top->value = MSH (pfile, v1, unsigned1, -v2); \
550 else \
551 top->value = PSH (pfile, v1, unsigned1, v2);
553 /* Parse and evaluate a C expression, reading from PFILE.
554 Returns the truth value of the expression. */
556 _cpp_parse_expr (pfile)
557 cpp_reader *pfile;
559 /* The implementation is an operator precedence parser, i.e. a
560 bottom-up parser, using a stack for not-yet-reduced tokens.
562 The stack base is 'stack', and the current stack pointer is 'top'.
563 There is a stack element for each operator (only),
564 and the most recently pushed operator is 'top->op'.
565 An operand (value) is stored in the 'value' field of the stack
566 element of the operator that precedes it.
567 In that case the 'flags' field has the HAVE_VALUE flag set. */
569 #define INIT_STACK_SIZE 20
570 struct op init_stack[INIT_STACK_SIZE];
571 struct op *stack = init_stack;
572 struct op *limit = stack + INIT_STACK_SIZE;
573 struct op *top = stack + 1;
574 int skip_evaluation = 0;
575 int result;
576 unsigned int lex_count, saw_leading_not;
578 /* Set up detection of #if ! defined(). */
579 pfile->mi_ind_cmacro = 0;
580 saw_leading_not = 0;
581 lex_count = 0;
583 /* We've finished when we try to reduce this. */
584 top->op = CPP_EOF;
585 /* Nifty way to catch missing '('. */
586 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
587 /* Avoid missing right operand checks. */
588 top->flags = NO_R_OPERAND;
590 for (;;)
592 unsigned int prio;
593 unsigned int flags;
594 struct op op;
596 /* Read a token */
597 op = lex (pfile, skip_evaluation);
598 lex_count++;
600 /* If the token is an operand, push its value and get next
601 token. If it is an operator, get its priority and flags, and
602 try to reduce the expression on the stack. */
603 switch (op.op)
605 case CPP_ERROR:
606 goto syntax_error;
607 push_immediate:
608 case CPP_NUMBER:
609 /* Push a value onto the stack. */
610 if (top->flags & HAVE_VALUE)
611 SYNTAX_ERROR ("missing binary operator");
612 top->value = op.value;
613 top->unsignedp = op.unsignedp;
614 top->flags |= HAVE_VALUE;
615 continue;
617 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
619 case CPP_NOT:
620 saw_leading_not = lex_count == 1;
621 prio = op_to_prio[op.op];
622 break;
623 case CPP_PLUS:
624 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
625 /* else unary; fall through */
626 default: prio = op_to_prio[op.op]; break;
629 /* Separate the operator's code into priority and flags. */
630 flags = EXTRACT_FLAGS(prio);
631 prio = EXTRACT_PRIO(prio);
632 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
633 goto skip_reduction;
635 /* Check for reductions. Then push the operator. */
636 while (prio <= top->prio)
638 HOST_WIDEST_INT v1, v2;
639 unsigned int unsigned1, unsigned2;
641 /* Most operators that can appear on the stack require a
642 right operand. Check this before trying to reduce. */
643 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
645 if (top->op == CPP_OPEN_PAREN)
646 SYNTAX_ERROR ("void expression between '(' and ')'");
647 else
648 SYNTAX_ERROR2 ("operator '%s' has no right operand",
649 op_as_text (pfile, top->op));
652 unsigned2 = top->unsignedp, v2 = top->value;
653 top--;
654 unsigned1 = top->unsignedp, v1 = top->value;
656 /* Now set top->value = (top[1].op)(v1, v2); */
657 switch (top[1].op)
659 default:
660 cpp_ice (pfile, "impossible operator '%s'",
661 op_as_text (pfile, top[1].op));
662 goto syntax_error;
664 case CPP_NOT: UNARY(!); break;
665 case CPP_COMPL: UNARY(~); break;
666 case CPP_LESS: COMPARE(<); break;
667 case CPP_GREATER: COMPARE(>); break;
668 case CPP_LESS_EQ: COMPARE(<=); break;
669 case CPP_GREATER_EQ: COMPARE(>=); break;
670 case CPP_EQ_EQ: EQUALITY(==); break;
671 case CPP_NOT_EQ: EQUALITY(!=); break;
672 case CPP_AND: BITWISE(&); break;
673 case CPP_XOR: BITWISE(^); break;
674 case CPP_OR: BITWISE(|); break;
675 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
676 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
677 case CPP_MIN: MINMAX(<); break;
678 case CPP_MAX: MINMAX(>); break;
680 case CPP_PLUS:
681 if (!(top->flags & HAVE_VALUE))
683 /* Can't use UNARY(+) because K+R C did not have unary
684 plus. Can't use UNARY() because some compilers object
685 to the empty argument. */
686 top->value = v2;
687 top->unsignedp = unsigned2;
688 top->flags |= HAVE_VALUE;
690 if (CPP_WTRADITIONAL (pfile))
691 cpp_warning (pfile,
692 "traditional C rejects the unary plus operator");
694 else
696 top->value = v1 + v2;
697 top->unsignedp = unsigned1 | unsigned2;
698 if (! top->unsignedp && ! skip_evaluation
699 && ! possible_sum_sign (v1, v2, top->value))
700 integer_overflow (pfile);
702 break;
703 case CPP_MINUS:
704 if (!(top->flags & HAVE_VALUE))
706 UNARY(-);
707 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
708 integer_overflow (pfile);
710 else
711 { /* Binary '-' */
712 top->value = v1 - v2;
713 top->unsignedp = unsigned1 | unsigned2;
714 if (! top->unsignedp && ! skip_evaluation
715 && ! possible_sum_sign (top->value, v2, v1))
716 integer_overflow (pfile);
718 break;
719 case CPP_MULT:
720 top->unsignedp = unsigned1 | unsigned2;
721 if (top->unsignedp)
722 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
723 else if (!skip_evaluation)
725 top->value = v1 * v2;
726 if (v1 && (top->value / v1 != v2
727 || (top->value & v1 & v2) < 0))
728 integer_overflow (pfile);
730 break;
731 case CPP_DIV:
732 case CPP_MOD:
733 if (skip_evaluation)
734 break;
735 if (v2 == 0)
736 SYNTAX_ERROR ("division by zero in #if");
737 top->unsignedp = unsigned1 | unsigned2;
738 if (top[1].op == CPP_DIV)
740 if (top->unsignedp)
741 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
742 else
744 top->value = v1 / v2;
745 if ((top->value & v1 & v2) < 0)
746 integer_overflow (pfile);
749 else
751 if (top->unsignedp)
752 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
753 else
754 top->value = v1 % v2;
756 break;
758 case CPP_OR_OR:
759 top->value = v1 || v2;
760 top->unsignedp = 0;
761 if (v1) skip_evaluation--;
762 break;
763 case CPP_AND_AND:
764 top->value = v1 && v2;
765 top->unsignedp = 0;
766 if (!v1) skip_evaluation--;
767 break;
768 case CPP_COMMA:
769 if (CPP_PEDANTIC (pfile))
770 cpp_pedwarn (pfile, "comma operator in operand of #if");
771 top->value = v2;
772 top->unsignedp = unsigned2;
773 break;
774 case CPP_QUERY:
775 SYNTAX_ERROR ("syntax error '?' without following ':'");
776 case CPP_COLON:
777 if (top[0].op != CPP_QUERY)
778 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
779 top--;
780 if (top->value) skip_evaluation--;
781 top->value = top->value ? v1 : v2;
782 top->unsignedp = unsigned1 | unsigned2;
783 break;
784 case CPP_OPEN_PAREN:
785 if (op.op != CPP_CLOSE_PAREN)
786 SYNTAX_ERROR ("missing ')' in expression");
787 op.value = v2;
788 op.unsignedp = unsigned2;
789 goto push_immediate;
790 case CPP_EOF:
791 /* Reducing this dummy operator indicates we've finished. */
792 if (op.op == CPP_CLOSE_PAREN)
793 SYNTAX_ERROR ("missing '(' in expression");
794 goto done;
798 /* Handle short-circuit evaluations. */
799 if (flags & SHORT_CIRCUIT)
800 switch (op.op)
802 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
803 case CPP_AND_AND:
804 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
805 case CPP_COLON:
806 if (top[-1].value) /* Was '?' condition true? */
807 skip_evaluation++;
808 else
809 skip_evaluation--;
810 default:
811 break;
814 skip_reduction:
815 /* Check we have a left operand iff we need one. */
816 if (flags & NO_L_OPERAND)
818 if (top->flags & HAVE_VALUE)
819 SYNTAX_ERROR2 ("missing binary operator before '%s'",
820 op_as_text (pfile, op.op));
822 else
824 if (!(top->flags & HAVE_VALUE))
825 SYNTAX_ERROR2 ("operator '%s' has no left operand",
826 op_as_text (pfile, op.op));
829 /* Check for and handle stack overflow. */
830 top++;
831 if (top == limit)
833 struct op *new_stack;
834 int old_size = (char *) limit - (char *) stack;
835 int new_size = 2 * old_size;
836 if (stack != init_stack)
837 new_stack = (struct op *) xrealloc (stack, new_size);
838 else
840 new_stack = (struct op *) xmalloc (new_size);
841 memcpy (new_stack, stack, old_size);
843 stack = new_stack;
844 top = (struct op *) ((char *) new_stack + old_size);
845 limit = (struct op *) ((char *) new_stack + new_size);
848 top->flags = flags;
849 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
850 top->op = op.op;
853 done:
854 /* The controlling macro expression is only valid if we called lex 3
855 times: <!> <defined expression> and <EOF>. push_conditional ()
856 checks that we are at top-of-file. */
857 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
858 pfile->mi_ind_cmacro = 0;
860 result = (top[1].value != 0);
862 if (top != stack)
863 CPP_ICE ("unbalanced stack in #if");
864 else if (!(top[1].flags & HAVE_VALUE))
866 SYNTAX_ERROR ("#if with no expression");
867 syntax_error:
868 result = 0; /* Return 0 on syntax error. */
871 /* Free dynamic stack if we allocated one. */
872 if (stack != init_stack)
873 free (stack);
874 return result;
877 /* Output OP as text for diagnostics. */
878 static const unsigned char *
879 op_as_text (pfile, op)
880 cpp_reader *pfile;
881 enum cpp_ttype op;
883 cpp_token token;
885 token.type = op;
886 token.flags = 0;
887 return cpp_token_as_text (pfile, &token);