Introduce ORIGINAL_REGNO macro
[official-gcc.git] / gcc / cppexp.c
blobe101592dd155d9a7f01685f4b932bcb61afcb2dd
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000
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 /* Parse a C expression from text in a string */
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "defaults.h"
29 #ifndef MAX_CHAR_TYPE_SIZE
30 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
31 #endif
33 #ifndef MAX_INT_TYPE_SIZE
34 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
35 #endif
37 #ifndef MAX_LONG_TYPE_SIZE
38 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
39 #endif
41 #ifndef MAX_WCHAR_TYPE_SIZE
42 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
43 #endif
45 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
46 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
47 : ~ (HOST_WIDEST_INT) 0)
49 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
50 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
51 : ~ (HOST_WIDEST_INT) 0)
53 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
54 number with SUM's sign, where A, B, and SUM are all C integers. */
55 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
57 static void integer_overflow PARAMS ((cpp_reader *));
58 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
59 unsigned int,
60 unsigned HOST_WIDEST_INT));
61 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
62 unsigned int,
63 unsigned HOST_WIDEST_INT));
64 static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op parse_charconst PARAMS ((cpp_reader *, const cpp_token *));
66 static struct op parse_defined PARAMS ((cpp_reader *));
67 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, const U_CHAR **,
68 const U_CHAR *, HOST_WIDEST_INT));
69 static struct op lex PARAMS ((cpp_reader *, int, cpp_token *));
70 static const unsigned char *op_as_text PARAMS ((cpp_reader *, enum cpp_ttype));
72 struct op
74 enum cpp_ttype op;
75 U_CHAR prio; /* Priority of op. */
76 U_CHAR flags;
77 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
78 HOST_WIDEST_INT value; /* The value logically "right" of op. */
81 /* There is no "error" token, but we can't get comments in #if, so we can
82 abuse that token type. */
83 #define CPP_ERROR CPP_COMMENT
85 /* With -O2, gcc appears to produce nice code, moving the error
86 message load and subsequent jump completely out of the main path. */
87 #define CPP_ICE(msgid) \
88 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
89 #define SYNTAX_ERROR(msgid) \
90 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
91 #define SYNTAX_ERROR2(msgid, arg) \
92 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
94 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
95 with or without size suffixes. */
96 struct suffix
98 unsigned char s[4];
99 unsigned char u;
100 unsigned char l;
103 const struct suffix vsuf_1[] = {
104 { "u", 1, 0 }, { "U", 1, 0 },
105 { "l", 0, 1 }, { "L", 0, 1 }
108 const struct suffix vsuf_2[] = {
109 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
110 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
111 { "ll", 0, 2 }, { "LL", 0, 2 }
114 const struct suffix vsuf_3[] = {
115 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
116 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
118 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
120 static struct op
121 parse_number (pfile, tok)
122 cpp_reader *pfile;
123 const cpp_token *tok;
125 struct op op;
126 const U_CHAR *start = tok->val.str.text;
127 const U_CHAR *end = start + tok->val.str.len;
128 const U_CHAR *p = start;
129 int c = 0, i, nsuff;
130 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
131 int base = 10;
132 int overflow = 0;
133 int digit, largest_digit = 0;
134 const struct suffix *sufftab;
136 op.unsignedp = 0;
138 if (p[0] == '0')
140 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
142 p += 2;
143 base = 16;
145 else
147 p += 1;
148 base = 8;
152 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
153 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
154 / ((unsigned HOST_WIDEST_INT) base));
156 for(; p < end; p++)
158 c = *p;
160 if (c >= '0' && c <= '9')
161 digit = c - '0';
162 /* We believe that in all live character sets, a-f are
163 consecutive, and so are A-F. */
164 else if (base == 16 && c >= 'a' && c <= 'f')
165 digit = c - 'a' + 10;
166 else if (base == 16 && c >= 'A' && c <= 'F')
167 digit = c - 'A' + 10;
168 else
169 break;
171 if (largest_digit < digit)
172 largest_digit = digit;
173 nd = n * base + digit;
174 overflow |= MAX_over_base < n || nd < n;
175 n = nd;
178 if (p < end)
180 /* Check for a floating point constant. Note that float constants
181 with an exponent or suffix but no decimal point are technically
182 invalid (C99 6.4.4.2) but accepted elsewhere. */
183 if ((c == '.' || c == 'F' || c == 'f')
184 || (base == 10 && (c == 'E' || c == 'e')
185 && p+1 < end && (p[1] == '+' || p[1] == '-'))
186 || (base == 16 && (c == 'P' || c == 'p')
187 && p+1 < end && (p[1] == '+' || p[1] == '-')))
188 SYNTAX_ERROR ("floating point numbers are not valid in #if");
190 /* Determine the suffix. l means long, and u means unsigned.
191 See the suffix tables, above. */
192 switch (end - p)
194 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
195 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
196 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
197 default: goto invalid_suffix;
200 for (i = 0; i < nsuff; i++)
201 if (memcmp (p, sufftab[i].s, end - p) == 0)
202 break;
203 if (i == nsuff)
204 goto invalid_suffix;
205 op.unsignedp = sufftab[i].u;
207 if (CPP_WTRADITIONAL (pfile) && sufftab[i].u)
208 cpp_warning (pfile, "traditional C rejects the `U' suffix");
209 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
210 && ! CPP_OPTION (pfile, c99))
211 cpp_pedwarn (pfile, "too many 'l' suffixes in integer constant");
214 if (base <= largest_digit)
215 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
217 if (overflow)
218 cpp_pedwarn (pfile, "integer constant out of range");
220 /* If too big to be signed, consider it unsigned. */
221 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
223 if (base == 10)
224 cpp_warning (pfile, "integer constant is so large that it is unsigned");
225 op.unsignedp = 1;
228 op.value = n;
229 op.op = CPP_INT;
230 return op;
232 invalid_suffix:
233 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
234 (int) (end - p), p);
235 syntax_error:
236 op.op = CPP_ERROR;
237 return op;
240 /* Parse and convert a character constant for #if. Understands backslash
241 escapes (\n, \031) and multibyte characters (if so configured). */
242 static struct op
243 parse_charconst (pfile, tok)
244 cpp_reader *pfile;
245 const cpp_token *tok;
247 struct op op;
248 HOST_WIDEST_INT result = 0;
249 int num_chars = 0;
250 int num_bits;
251 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
252 int max_chars;
253 const U_CHAR *ptr = tok->val.str.text;
254 const U_CHAR *end = ptr + tok->val.str.len;
256 int c = -1;
258 if (tok->type == CPP_WCHAR)
259 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
260 max_chars = MAX_LONG_TYPE_SIZE / width;
262 while (ptr < end)
264 c = *ptr++;
265 if (c == '\'')
266 CPP_ICE ("unescaped ' in character constant");
267 else if (c == '\\')
269 c = parse_escape (pfile, &ptr, end, mask);
270 if (width < HOST_BITS_PER_INT
271 && (unsigned int) c >= (unsigned int)(1 << width))
272 cpp_pedwarn (pfile,
273 "escape sequence out of range for character");
276 /* Merge character into result; ignore excess chars. */
277 if (++num_chars <= max_chars)
279 if (width < HOST_BITS_PER_INT)
280 result = (result << width) | (c & ((1 << width) - 1));
281 else
282 result = c;
286 if (num_chars == 0)
287 SYNTAX_ERROR ("empty character constant");
288 else if (num_chars > max_chars)
289 SYNTAX_ERROR ("character constant too long");
290 else if (num_chars != 1)
291 cpp_warning (pfile, "multi-character character constant");
293 /* If char type is signed, sign-extend the constant. */
294 num_bits = num_chars * width;
296 if (pfile->spec_nodes.n__CHAR_UNSIGNED__->type == NT_MACRO
297 || ((result >> (num_bits - 1)) & 1) == 0)
298 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
299 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
300 else
301 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
302 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
304 /* This is always a signed type. */
305 op.unsignedp = 0;
306 op.op = CPP_INT;
307 return op;
309 syntax_error:
310 op.op = CPP_ERROR;
311 return op;
314 static struct op
315 parse_defined (pfile)
316 cpp_reader *pfile;
318 int paren = 0;
319 cpp_hashnode *node = 0;
320 cpp_token token;
321 struct op op;
323 /* Don't expand macros. */
324 pfile->state.prevent_expansion++;
326 cpp_get_token (pfile, &token);
327 if (token.type == CPP_OPEN_PAREN)
329 paren = 1;
330 cpp_get_token (pfile, &token);
333 if (token.type == CPP_NAME)
335 node = token.val.node;
336 if (paren)
338 cpp_get_token (pfile, &token);
339 if (token.type != CPP_CLOSE_PAREN)
341 cpp_error (pfile, "missing ')' after \"defined\"");
342 node = 0;
346 else
347 cpp_error (pfile, "\"defined\" without an identifier");
349 if (!node)
350 op.op = CPP_ERROR;
351 else
353 op.value = node->type == NT_MACRO;
354 op.unsignedp = 0;
355 op.op = CPP_INT;
357 /* No macros? At top of file? */
358 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0
359 && pfile->mi_if_not_defined == MI_IND_NOT && pfile->mi_lexed == 1)
361 cpp_start_lookahead (pfile);
362 cpp_get_token (pfile, &token);
363 if (token.type == CPP_EOF)
364 pfile->mi_ind_cmacro = node;
365 cpp_stop_lookahead (pfile, 0);
369 pfile->state.prevent_expansion--;
370 return op;
373 /* Read one token. */
375 static struct op
376 lex (pfile, skip_evaluation, token)
377 cpp_reader *pfile;
378 int skip_evaluation;
379 cpp_token *token;
381 struct op op;
383 cpp_get_token (pfile, token);
385 switch (token->type)
387 case CPP_INT:
388 case CPP_NUMBER:
389 return parse_number (pfile, token);
391 case CPP_CHAR:
392 case CPP_WCHAR:
393 return parse_charconst (pfile, token);
395 case CPP_STRING:
396 case CPP_WSTRING:
397 SYNTAX_ERROR ("string constants are not valid in #if");
399 case CPP_FLOAT:
400 SYNTAX_ERROR ("floating point numbers are not valid in #if");
402 case CPP_OTHER:
403 if (ISGRAPH (token->val.c))
404 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
405 else
406 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
408 case CPP_NAME:
409 if (token->val.node == pfile->spec_nodes.n_defined)
411 if (pfile->context->prev && CPP_PEDANTIC (pfile))
412 cpp_pedwarn (pfile, "\"defined\" operator appears during macro expansion");
414 return parse_defined (pfile);
416 /* Controlling #if expressions cannot contain identifiers (they
417 could become macros in the future). */
418 pfile->mi_state = MI_FAILED;
420 op.op = CPP_INT;
421 op.unsignedp = 0;
422 op.value = 0;
424 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
425 cpp_warning (pfile, "\"%s\" is not defined", token->val.node->name);
427 return op;
429 case CPP_HASH:
431 int temp;
433 op.op = CPP_INT;
434 if (_cpp_test_assertion (pfile, &temp))
435 op.op = CPP_ERROR;
436 op.unsignedp = 0;
437 op.value = temp;
438 return op;
441 case CPP_NOT:
442 /* We don't worry about its position here. */
443 pfile->mi_if_not_defined = MI_IND_NOT;
444 /* Fall through. */
446 default:
447 if ((token->type > CPP_EQ && token->type < CPP_PLUS_EQ)
448 || token->type == CPP_EOF)
450 op.op = token->type;
451 return op;
454 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
455 cpp_token_as_text (pfile, token));
458 syntax_error:
459 op.op = CPP_ERROR;
460 return op;
463 /* Parse a C escape sequence. STRING_PTR points to a variable
464 containing a pointer to the string to parse. That pointer
465 is updated past the characters we use. The value of the
466 escape sequence is returned.
468 If \ is followed by 000, we return 0 and leave the string pointer
469 after the zeros. A value of 0 does not mean end of string. */
471 static HOST_WIDEST_INT
472 parse_escape (pfile, string_ptr, limit, result_mask)
473 cpp_reader *pfile;
474 const U_CHAR **string_ptr;
475 const U_CHAR *limit;
476 HOST_WIDEST_INT result_mask;
478 const U_CHAR *ptr = *string_ptr;
479 /* We know we have at least one following character. */
480 int c = *ptr++;
481 switch (c)
483 case 'a': c = TARGET_BELL; break;
484 case 'b': c = TARGET_BS; break;
485 case 'f': c = TARGET_FF; break;
486 case 'n': c = TARGET_NEWLINE; break;
487 case 'r': c = TARGET_CR; break;
488 case 't': c = TARGET_TAB; break;
489 case 'v': c = TARGET_VT; break;
491 case 'e': case 'E':
492 if (CPP_PEDANTIC (pfile))
493 cpp_pedwarn (pfile, "non-ISO-standard escape sequence, '\\%c'", c);
494 c = TARGET_ESC;
495 break;
497 case '0': case '1': case '2': case '3':
498 case '4': case '5': case '6': case '7':
500 unsigned int i = c - '0';
501 int count = 0;
502 while (++count < 3)
504 if (ptr >= limit)
505 break;
507 c = *ptr;
508 if (c < '0' || c > '7')
509 break;
510 ptr++;
511 i = (i << 3) + c - '0';
513 if (i != (i & result_mask))
515 i &= result_mask;
516 cpp_pedwarn (pfile, "octal escape sequence out of range");
518 c = i;
519 break;
522 case 'x':
524 unsigned int i = 0, overflow = 0;
525 int digits_found = 0, digit;
526 for (;;)
528 if (ptr >= limit)
529 break;
530 c = *ptr;
531 if (c >= '0' && c <= '9')
532 digit = c - '0';
533 else if (c >= 'a' && c <= 'f')
534 digit = c - 'a' + 10;
535 else if (c >= 'A' && c <= 'F')
536 digit = c - 'A' + 10;
537 else
538 break;
539 ptr++;
540 overflow |= i ^ (i << 4 >> 4);
541 i = (i << 4) + digit;
542 digits_found = 1;
544 if (!digits_found)
545 cpp_error (pfile, "\\x used with no following hex digits");
546 if (overflow | (i != (i & result_mask)))
548 i &= result_mask;
549 cpp_pedwarn (pfile, "hex escape sequence out of range");
551 c = i;
552 break;
555 *string_ptr = ptr;
556 return c;
559 static void
560 integer_overflow (pfile)
561 cpp_reader *pfile;
563 if (CPP_PEDANTIC (pfile))
564 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
567 static HOST_WIDEST_INT
568 left_shift (pfile, a, unsignedp, b)
569 cpp_reader *pfile;
570 HOST_WIDEST_INT a;
571 unsigned int unsignedp;
572 unsigned HOST_WIDEST_INT b;
574 if (b >= HOST_BITS_PER_WIDEST_INT)
576 if (! unsignedp && a != 0)
577 integer_overflow (pfile);
578 return 0;
580 else if (unsignedp)
581 return (unsigned HOST_WIDEST_INT) a << b;
582 else
584 HOST_WIDEST_INT l = a << b;
585 if (l >> b != a)
586 integer_overflow (pfile);
587 return l;
591 static HOST_WIDEST_INT
592 right_shift (pfile, a, unsignedp, b)
593 cpp_reader *pfile ATTRIBUTE_UNUSED;
594 HOST_WIDEST_INT a;
595 unsigned int unsignedp;
596 unsigned HOST_WIDEST_INT b;
598 if (b >= HOST_BITS_PER_WIDEST_INT)
599 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
600 else if (unsignedp)
601 return (unsigned HOST_WIDEST_INT) a >> b;
602 else
603 return a >> b;
606 /* Operator precedence and flags table.
608 After an operator is returned from the lexer, if it has priority less
609 than or equal to the operator on the top of the stack, we reduce the
610 stack by one operator and repeat the test. Since equal priorities
611 reduce, this is naturally left-associative.
613 We handle right-associative operators by clearing the lower bit of all
614 left-associative operators, and setting it for right-associative ones.
615 After the reduction phase of a new operator, just before it is pushed
616 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
617 during the reduction phase, the current right-associative operator has
618 a priority one greater than any other operator of otherwise equal
619 precedence that has been pushed on the top of the stack. This avoids
620 a reduction pass, and effectively makes the logic right-associative.
622 The remaining cases are '(' and ')'. We handle '(' by skipping the
623 reduction phase completely. ')' is given lower priority than
624 everything else, including '(', effectively forcing a reduction of the
625 parenthesised expression. If there is no matching '(', the stack will
626 be reduced all the way to the beginning, exiting the parser in the
627 same way as the ultra-low priority end-of-expression dummy operator.
628 The exit code checks to see if the operator that caused it is ')', and
629 if so outputs an appropriate error message.
631 The parser assumes all shifted operators require a right operand
632 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
633 These semantics are automatically checked, any extra semantics need to
634 be handled with operator-specific code. */
636 #define FLAG_BITS 8
637 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
638 #define PRIO_SHIFT (FLAG_BITS + 1)
639 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
640 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
642 /* Flags. */
643 #define HAVE_VALUE (1 << 0)
644 #define NO_L_OPERAND (1 << 1)
645 #define NO_R_OPERAND (1 << 2)
646 #define SHORT_CIRCUIT (1 << 3)
648 /* Priority and flag combinations. */
649 #define RIGHT_ASSOC (1 << FLAG_BITS)
650 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
651 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
652 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
653 #define COMMA_PRIO (3 << PRIO_SHIFT)
654 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
655 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
656 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
657 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
658 #define OR_PRIO (8 << PRIO_SHIFT)
659 #define XOR_PRIO (9 << PRIO_SHIFT)
660 #define AND_PRIO (10 << PRIO_SHIFT)
661 #define MINMAX_PRIO (11 << PRIO_SHIFT)
662 #define EQUAL_PRIO (12 << PRIO_SHIFT)
663 #define LESS_PRIO (13 << PRIO_SHIFT)
664 #define SHIFT_PRIO (14 << PRIO_SHIFT)
665 #define PLUS_PRIO (15 << PRIO_SHIFT)
666 #define MUL_PRIO (16 << PRIO_SHIFT)
667 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
669 /* Operator to priority map. Must be in the same order as the first
670 N entries of enum cpp_ttype. */
671 static const short
672 op_to_prio[] =
674 /* EQ */ 0, /* dummy entry - can't happen */
675 /* NOT */ UNARY_PRIO,
676 /* GREATER */ LESS_PRIO,
677 /* LESS */ LESS_PRIO,
678 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
679 /* MINUS */ UNARY_PRIO, /* or binary */
680 /* MULT */ MUL_PRIO,
681 /* DIV */ MUL_PRIO,
682 /* MOD */ MUL_PRIO,
683 /* AND */ AND_PRIO,
684 /* OR */ OR_PRIO,
685 /* XOR */ XOR_PRIO,
686 /* RSHIFT */ SHIFT_PRIO,
687 /* LSHIFT */ SHIFT_PRIO,
688 /* MIN */ MINMAX_PRIO, /* C++ specific */
689 /* MAX */ MINMAX_PRIO, /* extensions */
691 /* COMPL */ UNARY_PRIO,
692 /* AND_AND */ ANDAND_PRIO,
693 /* OR_OR */ OROR_PRIO,
694 /* QUERY */ COND_PRIO,
695 /* COLON */ COLON_PRIO,
696 /* COMMA */ COMMA_PRIO,
697 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
698 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
699 /* EQ_EQ */ EQUAL_PRIO,
700 /* NOT_EQ */ EQUAL_PRIO,
701 /* GREATER_EQ */ LESS_PRIO,
702 /* LESS_EQ */ LESS_PRIO
705 #define COMPARE(OP) \
706 top->unsignedp = 0; \
707 top->value = (unsigned1 | unsigned2) \
708 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
709 : (v1 OP v2)
710 #define EQUALITY(OP) \
711 top->value = v1 OP v2; \
712 top->unsignedp = 0;
713 #define BITWISE(OP) \
714 top->value = v1 OP v2; \
715 top->unsignedp = unsigned1 | unsigned2;
716 #define MINMAX(OP) \
717 top->value = (v1 OP v2) ? v1 : v2; \
718 top->unsignedp = unsigned1 | unsigned2;
719 #define UNARY(OP) \
720 top->value = OP v2; \
721 top->unsignedp = unsigned2; \
722 top->flags |= HAVE_VALUE;
723 #define SHIFT(PSH, MSH) \
724 if (skip_evaluation) \
725 break; \
726 top->unsignedp = unsigned1; \
727 if (v2 < 0 && ! unsigned2) \
728 top->value = MSH (pfile, v1, unsigned1, -v2); \
729 else \
730 top->value = PSH (pfile, v1, unsigned1, v2);
732 /* Parse and evaluate a C expression, reading from PFILE.
733 Returns the truth value of the expression. */
736 _cpp_parse_expr (pfile)
737 cpp_reader *pfile;
739 /* The implementation is an operator precedence parser, i.e. a
740 bottom-up parser, using a stack for not-yet-reduced tokens.
742 The stack base is 'stack', and the current stack pointer is 'top'.
743 There is a stack element for each operator (only),
744 and the most recently pushed operator is 'top->op'.
745 An operand (value) is stored in the 'value' field of the stack
746 element of the operator that precedes it.
747 In that case the 'flags' field has the HAVE_VALUE flag set. */
749 #define INIT_STACK_SIZE 20
750 struct op init_stack[INIT_STACK_SIZE];
751 struct op *stack = init_stack;
752 struct op *limit = stack + INIT_STACK_SIZE;
753 cpp_token token;
754 register struct op *top = stack + 1;
755 int skip_evaluation = 0;
756 int result;
758 /* Set up detection of #if ! defined(). */
759 pfile->mi_lexed = 0;
760 pfile->mi_if_not_defined = MI_IND_NONE;
762 /* We've finished when we try to reduce this. */
763 top->op = CPP_EOF;
764 /* Nifty way to catch missing '('. */
765 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
766 /* Avoid missing right operand checks. */
767 top->flags = NO_R_OPERAND;
769 for (;;)
771 unsigned int prio;
772 unsigned int flags;
773 struct op op;
775 /* Read a token */
776 op = lex (pfile, skip_evaluation, &token);
777 pfile->mi_lexed++;
779 /* If the token is an operand, push its value and get next
780 token. If it is an operator, get its priority and flags, and
781 try to reduce the expression on the stack. */
782 switch (op.op)
784 case CPP_ERROR:
785 goto syntax_error;
786 push_immediate:
787 case CPP_INT:
788 /* Push a value onto the stack. */
789 if (top->flags & HAVE_VALUE)
790 SYNTAX_ERROR ("missing binary operator");
791 top->value = op.value;
792 top->unsignedp = op.unsignedp;
793 top->flags |= HAVE_VALUE;
794 continue;
796 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
797 case CPP_PLUS:
798 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
799 /* else unary; fall through */
800 default: prio = op_to_prio[op.op]; break;
803 /* Separate the operator's code into priority and flags. */
804 flags = EXTRACT_FLAGS(prio);
805 prio = EXTRACT_PRIO(prio);
806 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
807 goto skip_reduction;
809 /* Check for reductions. Then push the operator. */
810 while (prio <= top->prio)
812 HOST_WIDEST_INT v1, v2;
813 unsigned int unsigned1, unsigned2;
815 /* Most operators that can appear on the stack require a
816 right operand. Check this before trying to reduce. */
817 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
819 if (top->op == CPP_OPEN_PAREN)
820 SYNTAX_ERROR ("void expression between '(' and ')'");
821 else
822 SYNTAX_ERROR2 ("operator '%s' has no right operand",
823 op_as_text (pfile, top->op));
826 unsigned2 = top->unsignedp, v2 = top->value;
827 top--;
828 unsigned1 = top->unsignedp, v1 = top->value;
830 /* Now set top->value = (top[1].op)(v1, v2); */
831 switch (top[1].op)
833 default:
834 cpp_ice (pfile, "impossible operator '%s'",
835 op_as_text (pfile, top[1].op));
836 goto syntax_error;
838 case CPP_NOT: UNARY(!); break;
839 case CPP_COMPL: UNARY(~); break;
840 case CPP_LESS: COMPARE(<); break;
841 case CPP_GREATER: COMPARE(>); break;
842 case CPP_LESS_EQ: COMPARE(<=); break;
843 case CPP_GREATER_EQ: COMPARE(>=); break;
844 case CPP_EQ_EQ: EQUALITY(==); break;
845 case CPP_NOT_EQ: EQUALITY(!=); break;
846 case CPP_AND: BITWISE(&); break;
847 case CPP_XOR: BITWISE(^); break;
848 case CPP_OR: BITWISE(|); break;
849 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
850 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
851 case CPP_MIN: MINMAX(<); break;
852 case CPP_MAX: MINMAX(>); break;
854 case CPP_PLUS:
855 if (!(top->flags & HAVE_VALUE))
857 /* Can't use UNARY(+) because K+R C did not have unary
858 plus. Can't use UNARY() because some compilers object
859 to the empty argument. */
860 top->value = v2;
861 top->unsignedp = unsigned2;
862 top->flags |= HAVE_VALUE;
864 if (CPP_WTRADITIONAL (pfile))
865 cpp_warning (pfile,
866 "traditional C rejects the unary plus operator");
868 else
870 top->value = v1 + v2;
871 top->unsignedp = unsigned1 | unsigned2;
872 if (! top->unsignedp && ! skip_evaluation
873 && ! possible_sum_sign (v1, v2, top->value))
874 integer_overflow (pfile);
876 break;
877 case CPP_MINUS:
878 if (!(top->flags & HAVE_VALUE))
880 UNARY(-);
881 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
882 integer_overflow (pfile);
884 else
885 { /* Binary '-' */
886 top->value = v1 - v2;
887 top->unsignedp = unsigned1 | unsigned2;
888 if (! top->unsignedp && ! skip_evaluation
889 && ! possible_sum_sign (top->value, v2, v1))
890 integer_overflow (pfile);
892 break;
893 case CPP_MULT:
894 top->unsignedp = unsigned1 | unsigned2;
895 if (top->unsignedp)
896 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
897 else if (!skip_evaluation)
899 top->value = v1 * v2;
900 if (v1 && (top->value / v1 != v2
901 || (top->value & v1 & v2) < 0))
902 integer_overflow (pfile);
904 break;
905 case CPP_DIV:
906 case CPP_MOD:
907 if (skip_evaluation)
908 break;
909 if (v2 == 0)
910 SYNTAX_ERROR ("division by zero in #if");
911 top->unsignedp = unsigned1 | unsigned2;
912 if (top[1].op == CPP_DIV)
914 if (top->unsignedp)
915 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
916 else
918 top->value = v1 / v2;
919 if ((top->value & v1 & v2) < 0)
920 integer_overflow (pfile);
923 else
925 if (top->unsignedp)
926 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
927 else
928 top->value = v1 % v2;
930 break;
932 case CPP_OR_OR:
933 top->value = v1 || v2;
934 top->unsignedp = 0;
935 if (v1) skip_evaluation--;
936 break;
937 case CPP_AND_AND:
938 top->value = v1 && v2;
939 top->unsignedp = 0;
940 if (!v1) skip_evaluation--;
941 break;
942 case CPP_COMMA:
943 if (CPP_PEDANTIC (pfile))
944 cpp_pedwarn (pfile, "comma operator in operand of #if");
945 top->value = v2;
946 top->unsignedp = unsigned2;
947 break;
948 case CPP_QUERY:
949 SYNTAX_ERROR ("syntax error '?' without following ':'");
950 case CPP_COLON:
951 if (top[0].op != CPP_QUERY)
952 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
953 top--;
954 if (top->value) skip_evaluation--;
955 top->value = top->value ? v1 : v2;
956 top->unsignedp = unsigned1 | unsigned2;
957 break;
958 case CPP_OPEN_PAREN:
959 if (op.op != CPP_CLOSE_PAREN)
960 SYNTAX_ERROR ("missing ')' in expression");
961 op.value = v2;
962 op.unsignedp = unsigned2;
963 goto push_immediate;
964 case CPP_EOF:
965 /* Reducing this dummy operator indicates we've finished. */
966 if (op.op == CPP_CLOSE_PAREN)
967 SYNTAX_ERROR ("missing '(' in expression");
968 goto done;
972 /* Handle short-circuit evaluations. */
973 if (flags & SHORT_CIRCUIT)
974 switch (op.op)
976 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
977 case CPP_AND_AND:
978 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
979 case CPP_COLON:
980 if (top[-1].value) /* Was '?' condition true? */
981 skip_evaluation++;
982 else
983 skip_evaluation--;
984 default:
985 break;
988 skip_reduction:
989 /* Check we have a left operand iff we need one. */
990 if (flags & NO_L_OPERAND)
992 if (top->flags & HAVE_VALUE)
993 SYNTAX_ERROR2 ("missing binary operator before '%s'",
994 op_as_text (pfile, top->op));
996 else
998 if (!(top->flags & HAVE_VALUE))
999 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1000 op_as_text (pfile, top->op));
1003 /* Check for and handle stack overflow. */
1004 top++;
1005 if (top == limit)
1007 struct op *new_stack;
1008 int old_size = (char *) limit - (char *) stack;
1009 int new_size = 2 * old_size;
1010 if (stack != init_stack)
1011 new_stack = (struct op *) xrealloc (stack, new_size);
1012 else
1014 new_stack = (struct op *) xmalloc (new_size);
1015 memcpy (new_stack, stack, old_size);
1017 stack = new_stack;
1018 top = (struct op *) ((char *) new_stack + old_size);
1019 limit = (struct op *) ((char *) new_stack + new_size);
1022 top->flags = flags;
1023 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1024 top->op = op.op;
1027 done:
1028 result = (top[1].value != 0);
1029 if (top != stack)
1030 CPP_ICE ("unbalanced stack in #if");
1031 else if (!(top[1].flags & HAVE_VALUE))
1033 SYNTAX_ERROR ("#if with no expression");
1034 syntax_error:
1035 result = 0; /* Return 0 on syntax error. */
1038 /* Free dynamic stack if we allocated one. */
1039 if (stack != init_stack)
1040 free (stack);
1041 return result;
1044 static const unsigned char *
1045 op_as_text (pfile, op)
1046 cpp_reader *pfile;
1047 enum cpp_ttype op;
1049 cpp_token token;
1051 token.type = op;
1052 token.flags = 0;
1053 return cpp_token_as_text (pfile, &token);