* config/i386/i386.md (mmx_pinsrw): Output operands in correct
[official-gcc.git] / gcc / cppexp.c
blob55b633e6569f98bbbfc53509e860c6554b46295c
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
3 Contributed by Per Bothner, 1994.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Parse a C expression from text in a string */
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "defaults.h"
28 #ifndef MAX_CHAR_TYPE_SIZE
29 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
30 #endif
32 #ifndef MAX_INT_TYPE_SIZE
33 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
34 #endif
36 #ifndef MAX_LONG_TYPE_SIZE
37 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
38 #endif
40 #ifndef MAX_WCHAR_TYPE_SIZE
41 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
42 #endif
44 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
45 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
46 : ~ (HOST_WIDEST_INT) 0)
48 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
49 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
50 : ~ (HOST_WIDEST_INT) 0)
52 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
53 number with SUM's sign, where A, B, and SUM are all C integers. */
54 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
56 static void integer_overflow PARAMS ((cpp_reader *));
57 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
58 unsigned int,
59 unsigned HOST_WIDEST_INT));
60 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
61 unsigned int,
62 unsigned HOST_WIDEST_INT));
63 static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
64 static struct op parse_charconst PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op parse_defined PARAMS ((cpp_reader *));
66 static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, const U_CHAR **,
67 const U_CHAR *, HOST_WIDEST_INT));
68 static struct op lex PARAMS ((cpp_reader *, int, cpp_token *));
69 static const unsigned char *op_as_text PARAMS ((cpp_reader *, enum cpp_ttype));
71 struct op
73 enum cpp_ttype op;
74 U_CHAR prio; /* Priority of op. */
75 U_CHAR flags;
76 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
77 HOST_WIDEST_INT value; /* The value logically "right" of op. */
80 /* There is no "error" token, but we can't get comments in #if, so we can
81 abuse that token type. */
82 #define CPP_ERROR CPP_COMMENT
84 /* With -O2, gcc appears to produce nice code, moving the error
85 message load and subsequent jump completely out of the main path. */
86 #define CPP_ICE(msgid) \
87 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
88 #define SYNTAX_ERROR(msgid) \
89 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
90 #define SYNTAX_ERROR2(msgid, arg) \
91 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
93 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
94 with or without size suffixes. */
95 struct suffix
97 unsigned char s[4];
98 unsigned char u;
99 unsigned char l;
102 const struct suffix vsuf_1[] = {
103 { "u", 1, 0 }, { "U", 1, 0 },
104 { "l", 0, 1 }, { "L", 0, 1 }
107 const struct suffix vsuf_2[] = {
108 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
109 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
110 { "ll", 0, 2 }, { "LL", 0, 2 }
113 const struct suffix vsuf_3[] = {
114 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
115 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
117 #define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
119 static struct op
120 parse_number (pfile, tok)
121 cpp_reader *pfile;
122 const cpp_token *tok;
124 struct op op;
125 const U_CHAR *start = tok->val.str.text;
126 const U_CHAR *end = start + tok->val.str.len;
127 const U_CHAR *p = start;
128 int c = 0, i, nsuff;
129 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
130 int base = 10;
131 int overflow = 0;
132 int digit, largest_digit = 0;
133 const struct suffix *sufftab;
135 op.unsignedp = 0;
137 if (p[0] == '0')
139 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
141 p += 2;
142 base = 16;
144 else
146 p += 1;
147 base = 8;
151 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
152 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
153 / ((unsigned HOST_WIDEST_INT) base));
155 for(; p < end; p++)
157 c = *p;
159 if (c >= '0' && c <= '9')
160 digit = c - '0';
161 /* We believe that in all live character sets, a-f are
162 consecutive, and so are A-F. */
163 else if (base == 16 && c >= 'a' && c <= 'f')
164 digit = c - 'a' + 10;
165 else if (base == 16 && c >= 'A' && c <= 'F')
166 digit = c - 'A' + 10;
167 else
168 break;
170 if (largest_digit < digit)
171 largest_digit = digit;
172 nd = n * base + digit;
173 overflow |= MAX_over_base < n || nd < n;
174 n = nd;
177 if (p < end)
179 /* Check for a floating point constant. Note that float constants
180 with an exponent or suffix but no decimal point are technically
181 invalid (C99 6.4.4.2) but accepted elsewhere. */
182 if ((c == '.' || c == 'F' || c == 'f')
183 || (base == 10 && (c == 'E' || c == 'e')
184 && p+1 < end && (p[1] == '+' || p[1] == '-'))
185 || (base == 16 && (c == 'P' || c == 'p')
186 && p+1 < end && (p[1] == '+' || p[1] == '-')))
187 SYNTAX_ERROR ("floating point numbers are not valid in #if");
189 /* Determine the suffix. l means long, and u means unsigned.
190 See the suffix tables, above. */
191 switch (end - p)
193 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
194 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
195 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
196 default: goto invalid_suffix;
199 for (i = 0; i < nsuff; i++)
200 if (memcmp (p, sufftab[i].s, end - p) == 0)
201 break;
202 if (i == nsuff)
203 goto invalid_suffix;
204 op.unsignedp = sufftab[i].u;
206 if (CPP_WTRADITIONAL (pfile) && sufftab[i].u)
207 cpp_warning (pfile, "traditional C rejects the `U' suffix");
208 if (CPP_OPTION (pfile, c89) && sufftab[i].l == 2)
209 SYNTAX_ERROR ("too many 'l' suffixes in integer constant");
212 if (base <= largest_digit)
213 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
215 if (overflow)
216 cpp_pedwarn (pfile, "integer constant out of range");
218 /* If too big to be signed, consider it unsigned. */
219 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
221 if (base == 10)
222 cpp_warning (pfile, "integer constant is so large that it is unsigned");
223 op.unsignedp = 1;
226 op.value = n;
227 op.op = CPP_INT;
228 return op;
230 invalid_suffix:
231 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
232 (int) (end - p), p);
233 syntax_error:
234 op.op = CPP_ERROR;
235 return op;
238 /* Parse and convert a character constant for #if. Understands backslash
239 escapes (\n, \031) and multibyte characters (if so configured). */
240 static struct op
241 parse_charconst (pfile, tok)
242 cpp_reader *pfile;
243 const cpp_token *tok;
245 struct op op;
246 HOST_WIDEST_INT result = 0;
247 int num_chars = 0;
248 int num_bits;
249 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
250 int max_chars;
251 const U_CHAR *ptr = tok->val.str.text;
252 const U_CHAR *end = ptr + tok->val.str.len;
254 int c = -1;
256 if (tok->type == CPP_WCHAR)
257 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
258 max_chars = MAX_LONG_TYPE_SIZE / width;
260 while (ptr < end)
262 c = *ptr++;
263 if (c == '\'')
264 CPP_ICE ("unescaped ' in character constant");
265 else if (c == '\\')
267 c = parse_escape (pfile, &ptr, end, mask);
268 if (width < HOST_BITS_PER_INT
269 && (unsigned int) c >= (unsigned int)(1 << width))
270 cpp_pedwarn (pfile,
271 "escape sequence out of range for character");
274 /* Merge character into result; ignore excess chars. */
275 if (++num_chars <= max_chars)
277 if (width < HOST_BITS_PER_INT)
278 result = (result << width) | (c & ((1 << width) - 1));
279 else
280 result = c;
284 if (num_chars == 0)
285 SYNTAX_ERROR ("empty character constant");
286 else if (num_chars > max_chars)
287 SYNTAX_ERROR ("character constant too long");
288 else if (num_chars != 1)
289 cpp_warning (pfile, "multi-character character constant");
291 /* If char type is signed, sign-extend the constant. */
292 num_bits = num_chars * width;
294 if (pfile->spec_nodes.n__CHAR_UNSIGNED__->type == NT_MACRO
295 || ((result >> (num_bits - 1)) & 1) == 0)
296 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
297 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
298 else
299 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
300 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
302 /* This is always a signed type. */
303 op.unsignedp = 0;
304 op.op = CPP_INT;
305 return op;
307 syntax_error:
308 op.op = CPP_ERROR;
309 return op;
312 static struct op
313 parse_defined (pfile)
314 cpp_reader *pfile;
316 int paren = 0;
317 cpp_hashnode *node = 0;
318 cpp_token token;
319 struct op op;
321 /* Don't expand macros. */
322 pfile->state.prevent_expansion++;
324 _cpp_get_token (pfile, &token);
325 if (token.type == CPP_OPEN_PAREN)
327 paren = 1;
328 _cpp_get_token (pfile, &token);
331 if (token.type == CPP_NAME)
333 node = token.val.node;
334 if (paren)
336 _cpp_get_token (pfile, &token);
337 if (token.type != CPP_CLOSE_PAREN)
339 cpp_error (pfile, "missing ')' after \"defined\"");
340 node = 0;
344 else
345 cpp_error (pfile, "\"defined\" without an identifier");
347 if (!node)
348 op.op = CPP_ERROR;
349 else
351 op.value = node->type == NT_MACRO;
352 op.unsignedp = 0;
353 op.op = CPP_INT;
355 /* No macros? At top of file? */
356 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0
357 && pfile->mi_if_not_defined == MI_IND_NOT && pfile->mi_lexed == 1)
359 cpp_start_lookahead (pfile);
360 cpp_get_token (pfile, &token);
361 if (token.type == CPP_EOF)
362 pfile->mi_ind_cmacro = node;
363 cpp_stop_lookahead (pfile, 0);
367 pfile->state.prevent_expansion--;
368 return op;
371 /* Read one token. */
373 static struct op
374 lex (pfile, skip_evaluation, token)
375 cpp_reader *pfile;
376 int skip_evaluation;
377 cpp_token *token;
379 struct op op;
381 retry:
382 _cpp_get_token (pfile, token);
384 switch (token->type)
386 case CPP_PLACEMARKER:
387 goto retry;
389 case CPP_INT:
390 case CPP_NUMBER:
391 return parse_number (pfile, token);
392 case CPP_CHAR:
393 case CPP_WCHAR:
394 return parse_charconst (pfile, token);
396 case CPP_STRING:
397 case CPP_WSTRING:
398 SYNTAX_ERROR ("string constants are not valid in #if");
400 case CPP_FLOAT:
401 SYNTAX_ERROR ("floating point numbers are not valid in #if");
403 case CPP_OTHER:
404 if (ISGRAPH (token->val.c))
405 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
406 else
407 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
409 case CPP_NAME:
410 if (token->val.node == pfile->spec_nodes.n_defined)
412 if (pfile->context->prev && CPP_PEDANTIC (pfile))
413 cpp_pedwarn (pfile, "\"defined\" operator appears during macro expansion");
415 return parse_defined (pfile);
417 /* Controlling #if expressions cannot contain identifiers (they
418 could become macros in the future). */
419 pfile->mi_state = MI_FAILED;
421 op.op = CPP_INT;
422 op.unsignedp = 0;
423 op.value = 0;
425 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
426 cpp_warning (pfile, "\"%s\" is not defined", token->val.node->name);
428 return op;
430 case CPP_HASH:
432 int temp;
434 op.op = CPP_INT;
435 if (_cpp_test_assertion (pfile, &temp))
436 op.op = CPP_ERROR;
437 op.unsignedp = 0;
438 op.value = temp;
439 return op;
442 case CPP_NOT:
443 /* We don't worry about its position here. */
444 pfile->mi_if_not_defined = MI_IND_NOT;
445 /* Fall through. */
447 default:
448 if ((token->type > CPP_EQ && token->type < CPP_PLUS_EQ)
449 || token->type == CPP_EOF)
451 op.op = token->type;
452 return op;
455 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
456 cpp_token_as_text (pfile, token));
459 syntax_error:
460 op.op = CPP_ERROR;
461 return op;
464 /* Parse a C escape sequence. STRING_PTR points to a variable
465 containing a pointer to the string to parse. That pointer
466 is updated past the characters we use. The value of the
467 escape sequence is returned.
469 If \ is followed by 000, we return 0 and leave the string pointer
470 after the zeros. A value of 0 does not mean end of string. */
472 static HOST_WIDEST_INT
473 parse_escape (pfile, string_ptr, limit, result_mask)
474 cpp_reader *pfile;
475 const U_CHAR **string_ptr;
476 const U_CHAR *limit;
477 HOST_WIDEST_INT result_mask;
479 const U_CHAR *ptr = *string_ptr;
480 /* We know we have at least one following character. */
481 int c = *ptr++;
482 switch (c)
484 case 'a': c = TARGET_BELL; break;
485 case 'b': c = TARGET_BS; break;
486 case 'f': c = TARGET_FF; break;
487 case 'n': c = TARGET_NEWLINE; break;
488 case 'r': c = TARGET_CR; break;
489 case 't': c = TARGET_TAB; break;
490 case 'v': c = TARGET_VT; break;
492 case 'e': case 'E':
493 if (CPP_PEDANTIC (pfile))
494 cpp_pedwarn (pfile, "non-ISO-standard escape sequence, '\\%c'", c);
495 c = TARGET_ESC;
496 break;
498 case '0': case '1': case '2': case '3':
499 case '4': case '5': case '6': case '7':
501 unsigned int i = c - '0';
502 int count = 0;
503 while (++count < 3)
505 if (ptr >= limit)
506 break;
508 c = *ptr;
509 if (c < '0' || c > '7')
510 break;
511 ptr++;
512 i = (i << 3) + c - '0';
514 if (i != (i & result_mask))
516 i &= result_mask;
517 cpp_pedwarn (pfile, "octal escape sequence out of range");
519 c = i;
520 break;
523 case 'x':
525 unsigned int i = 0, overflow = 0;
526 int digits_found = 0, digit;
527 for (;;)
529 if (ptr >= limit)
530 break;
531 c = *ptr;
532 if (c >= '0' && c <= '9')
533 digit = c - '0';
534 else if (c >= 'a' && c <= 'f')
535 digit = c - 'a' + 10;
536 else if (c >= 'A' && c <= 'F')
537 digit = c - 'A' + 10;
538 else
539 break;
540 ptr++;
541 overflow |= i ^ (i << 4 >> 4);
542 i = (i << 4) + digit;
543 digits_found = 1;
545 if (!digits_found)
546 cpp_error (pfile, "\\x used with no following hex digits");
547 if (overflow | (i != (i & result_mask)))
549 i &= result_mask;
550 cpp_pedwarn (pfile, "hex escape sequence out of range");
552 c = i;
553 break;
556 *string_ptr = ptr;
557 return c;
560 static void
561 integer_overflow (pfile)
562 cpp_reader *pfile;
564 if (CPP_PEDANTIC (pfile))
565 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
568 static HOST_WIDEST_INT
569 left_shift (pfile, a, unsignedp, b)
570 cpp_reader *pfile;
571 HOST_WIDEST_INT a;
572 unsigned int unsignedp;
573 unsigned HOST_WIDEST_INT b;
575 if (b >= HOST_BITS_PER_WIDEST_INT)
577 if (! unsignedp && a != 0)
578 integer_overflow (pfile);
579 return 0;
581 else if (unsignedp)
582 return (unsigned HOST_WIDEST_INT) a << b;
583 else
585 HOST_WIDEST_INT l = a << b;
586 if (l >> b != a)
587 integer_overflow (pfile);
588 return l;
592 static HOST_WIDEST_INT
593 right_shift (pfile, a, unsignedp, b)
594 cpp_reader *pfile ATTRIBUTE_UNUSED;
595 HOST_WIDEST_INT a;
596 unsigned int unsignedp;
597 unsigned HOST_WIDEST_INT b;
599 if (b >= HOST_BITS_PER_WIDEST_INT)
600 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
601 else if (unsignedp)
602 return (unsigned HOST_WIDEST_INT) a >> b;
603 else
604 return a >> b;
607 /* Operator precedence and flags table.
609 After an operator is returned from the lexer, if it has priority less
610 than or equal to the operator on the top of the stack, we reduce the
611 stack by one operator and repeat the test. Since equal priorities
612 reduce, this is naturally left-associative.
614 We handle right-associative operators by clearing the lower bit of all
615 left-associative operators, and setting it for right-associative ones.
616 After the reduction phase of a new operator, just before it is pushed
617 onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
618 during the reduction phase, the current right-associative operator has
619 a priority one greater than any other operator of otherwise equal
620 precedence that has been pushed on the top of the stack. This avoids
621 a reduction pass, and effectively makes the logic right-associative.
623 The remaining cases are '(' and ')'. We handle '(' by skipping the
624 reduction phase completely. ')' is given lower priority than
625 everything else, including '(', effectively forcing a reduction of the
626 parenthesised expression. If there is no matching '(', the stack will
627 be reduced all the way to the beginning, exiting the parser in the
628 same way as the ultra-low priority end-of-expression dummy operator.
629 The exit code checks to see if the operator that caused it is ')', and
630 if so outputs an appropriate error message.
632 The parser assumes all shifted operators require a right operand
633 unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
634 These semantics are automatically checked, any extra semantics need to
635 be handled with operator-specific code. */
637 #define FLAG_BITS 8
638 #define FLAG_MASK ((1 << FLAG_BITS) - 1)
639 #define PRIO_SHIFT (FLAG_BITS + 1)
640 #define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
641 #define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
643 /* Flags. */
644 #define HAVE_VALUE (1 << 0)
645 #define NO_L_OPERAND (1 << 1)
646 #define NO_R_OPERAND (1 << 2)
647 #define SHORT_CIRCUIT (1 << 3)
649 /* Priority and flag combinations. */
650 #define RIGHT_ASSOC (1 << FLAG_BITS)
651 #define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
652 #define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
653 #define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
654 #define COMMA_PRIO (3 << PRIO_SHIFT)
655 #define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
656 #define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
657 #define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
658 #define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
659 #define OR_PRIO (8 << PRIO_SHIFT)
660 #define XOR_PRIO (9 << PRIO_SHIFT)
661 #define AND_PRIO (10 << PRIO_SHIFT)
662 #define MINMAX_PRIO (11 << PRIO_SHIFT)
663 #define EQUAL_PRIO (12 << PRIO_SHIFT)
664 #define LESS_PRIO (13 << PRIO_SHIFT)
665 #define SHIFT_PRIO (14 << PRIO_SHIFT)
666 #define PLUS_PRIO (15 << PRIO_SHIFT)
667 #define MUL_PRIO (16 << PRIO_SHIFT)
668 #define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
670 /* Operator to priority map. Must be in the same order as the first
671 N entries of enum cpp_ttype. */
672 static const short
673 op_to_prio[] =
675 /* EQ */ 0, /* dummy entry - can't happen */
676 /* NOT */ UNARY_PRIO,
677 /* GREATER */ LESS_PRIO,
678 /* LESS */ LESS_PRIO,
679 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
680 /* MINUS */ UNARY_PRIO, /* or binary */
681 /* MULT */ MUL_PRIO,
682 /* DIV */ MUL_PRIO,
683 /* MOD */ MUL_PRIO,
684 /* AND */ AND_PRIO,
685 /* OR */ OR_PRIO,
686 /* XOR */ XOR_PRIO,
687 /* RSHIFT */ SHIFT_PRIO,
688 /* LSHIFT */ SHIFT_PRIO,
689 /* MIN */ MINMAX_PRIO, /* C++ specific */
690 /* MAX */ MINMAX_PRIO, /* extensions */
692 /* COMPL */ UNARY_PRIO,
693 /* AND_AND */ ANDAND_PRIO,
694 /* OR_OR */ OROR_PRIO,
695 /* QUERY */ COND_PRIO,
696 /* COLON */ COLON_PRIO,
697 /* COMMA */ COMMA_PRIO,
698 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
699 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
700 /* EQ_EQ */ EQUAL_PRIO,
701 /* NOT_EQ */ EQUAL_PRIO,
702 /* GREATER_EQ */ LESS_PRIO,
703 /* LESS_EQ */ LESS_PRIO
706 #define COMPARE(OP) \
707 top->unsignedp = 0; \
708 top->value = (unsigned1 | unsigned2) \
709 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
710 : (v1 OP v2)
711 #define EQUALITY(OP) \
712 top->value = v1 OP v2; \
713 top->unsignedp = 0;
714 #define BITWISE(OP) \
715 top->value = v1 OP v2; \
716 top->unsignedp = unsigned1 | unsigned2;
717 #define MINMAX(OP) \
718 top->value = (v1 OP v2) ? v1 : v2; \
719 top->unsignedp = unsigned1 | unsigned2;
720 #define UNARY(OP) \
721 top->value = OP v2; \
722 top->unsignedp = unsigned2; \
723 top->flags |= HAVE_VALUE;
724 #define SHIFT(PSH, MSH) \
725 if (skip_evaluation) \
726 break; \
727 top->unsignedp = unsigned1; \
728 if (v2 < 0 && ! unsigned2) \
729 top->value = MSH (pfile, v1, unsigned1, -v2); \
730 else \
731 top->value = PSH (pfile, v1, unsigned1, v2);
733 /* Parse and evaluate a C expression, reading from PFILE.
734 Returns the truth value of the expression. */
737 _cpp_parse_expr (pfile)
738 cpp_reader *pfile;
740 /* The implementation is an operator precedence parser, i.e. a
741 bottom-up parser, using a stack for not-yet-reduced tokens.
743 The stack base is 'stack', and the current stack pointer is 'top'.
744 There is a stack element for each operator (only),
745 and the most recently pushed operator is 'top->op'.
746 An operand (value) is stored in the 'value' field of the stack
747 element of the operator that precedes it.
748 In that case the 'flags' field has the HAVE_VALUE flag set. */
750 #define INIT_STACK_SIZE 20
751 struct op init_stack[INIT_STACK_SIZE];
752 struct op *stack = init_stack;
753 struct op *limit = stack + INIT_STACK_SIZE;
754 cpp_token token;
755 register struct op *top = stack + 1;
756 int skip_evaluation = 0;
757 int result;
759 /* Save parser state and set it to something sane. */
760 int save_skipping = pfile->skipping;
761 pfile->skipping = 0;
763 /* Set up detection of #if ! defined(). */
764 pfile->mi_lexed = 0;
765 pfile->mi_if_not_defined = MI_IND_NONE;
767 /* We've finished when we try to reduce this. */
768 top->op = CPP_EOF;
769 /* Nifty way to catch missing '('. */
770 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
771 /* Avoid missing right operand checks. */
772 top->flags = NO_R_OPERAND;
774 for (;;)
776 unsigned int prio;
777 unsigned int flags;
778 struct op op;
780 /* Read a token */
781 op = lex (pfile, skip_evaluation, &token);
782 pfile->mi_lexed++;
784 /* If the token is an operand, push its value and get next
785 token. If it is an operator, get its priority and flags, and
786 try to reduce the expression on the stack. */
787 switch (op.op)
789 case CPP_ERROR:
790 goto syntax_error;
791 push_immediate:
792 case CPP_INT:
793 /* Push a value onto the stack. */
794 if (top->flags & HAVE_VALUE)
795 SYNTAX_ERROR ("missing binary operator");
796 top->value = op.value;
797 top->unsignedp = op.unsignedp;
798 top->flags |= HAVE_VALUE;
799 continue;
801 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
802 case CPP_PLUS:
803 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
804 /* else unary; fall through */
805 default: prio = op_to_prio[op.op]; break;
808 /* Separate the operator's code into priority and flags. */
809 flags = EXTRACT_FLAGS(prio);
810 prio = EXTRACT_PRIO(prio);
811 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
812 goto skip_reduction;
814 /* Check for reductions. Then push the operator. */
815 while (prio <= top->prio)
817 HOST_WIDEST_INT v1, v2;
818 unsigned int unsigned1, unsigned2;
820 /* Most operators that can appear on the stack require a
821 right operand. Check this before trying to reduce. */
822 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
824 if (top->op == CPP_OPEN_PAREN)
825 SYNTAX_ERROR ("void expression between '(' and ')'");
826 else
827 SYNTAX_ERROR2 ("operator '%s' has no right operand",
828 op_as_text (pfile, top->op));
831 unsigned2 = top->unsignedp, v2 = top->value;
832 top--;
833 unsigned1 = top->unsignedp, v1 = top->value;
835 /* Now set top->value = (top[1].op)(v1, v2); */
836 switch (top[1].op)
838 default:
839 cpp_ice (pfile, "impossible operator '%s'",
840 op_as_text (pfile, top[1].op));
841 goto syntax_error;
843 case CPP_NOT: UNARY(!); break;
844 case CPP_COMPL: UNARY(~); break;
845 case CPP_LESS: COMPARE(<); break;
846 case CPP_GREATER: COMPARE(>); break;
847 case CPP_LESS_EQ: COMPARE(<=); break;
848 case CPP_GREATER_EQ: COMPARE(>=); break;
849 case CPP_EQ_EQ: EQUALITY(==); break;
850 case CPP_NOT_EQ: EQUALITY(!=); break;
851 case CPP_AND: BITWISE(&); break;
852 case CPP_XOR: BITWISE(^); break;
853 case CPP_OR: BITWISE(|); break;
854 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
855 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
856 case CPP_MIN: MINMAX(<); break;
857 case CPP_MAX: MINMAX(>); break;
859 case CPP_PLUS:
860 if (!(top->flags & HAVE_VALUE))
862 /* Can't use UNARY(+) because K+R C did not have unary
863 plus. Can't use UNARY() because some compilers object
864 to the empty argument. */
865 top->value = v2;
866 top->unsignedp = unsigned2;
867 top->flags |= HAVE_VALUE;
869 if (CPP_WTRADITIONAL (pfile))
870 cpp_warning (pfile,
871 "traditional C rejects the unary plus operator");
873 else
875 top->value = v1 + v2;
876 top->unsignedp = unsigned1 | unsigned2;
877 if (! top->unsignedp && ! skip_evaluation
878 && ! possible_sum_sign (v1, v2, top->value))
879 integer_overflow (pfile);
881 break;
882 case CPP_MINUS:
883 if (!(top->flags & HAVE_VALUE))
885 UNARY(-);
886 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
887 integer_overflow (pfile);
889 else
890 { /* Binary '-' */
891 top->value = v1 - v2;
892 top->unsignedp = unsigned1 | unsigned2;
893 if (! top->unsignedp && ! skip_evaluation
894 && ! possible_sum_sign (top->value, v2, v1))
895 integer_overflow (pfile);
897 break;
898 case CPP_MULT:
899 top->unsignedp = unsigned1 | unsigned2;
900 if (top->unsignedp)
901 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
902 else if (!skip_evaluation)
904 top->value = v1 * v2;
905 if (v1 && (top->value / v1 != v2
906 || (top->value & v1 & v2) < 0))
907 integer_overflow (pfile);
909 break;
910 case CPP_DIV:
911 case CPP_MOD:
912 if (skip_evaluation)
913 break;
914 if (v2 == 0)
915 SYNTAX_ERROR ("division by zero in #if");
916 top->unsignedp = unsigned1 | unsigned2;
917 if (top[1].op == CPP_DIV)
919 if (top->unsignedp)
920 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
921 else
923 top->value = v1 / v2;
924 if ((top->value & v1 & v2) < 0)
925 integer_overflow (pfile);
928 else
930 if (top->unsignedp)
931 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
932 else
933 top->value = v1 % v2;
935 break;
937 case CPP_OR_OR:
938 top->value = v1 || v2;
939 top->unsignedp = 0;
940 if (v1) skip_evaluation--;
941 break;
942 case CPP_AND_AND:
943 top->value = v1 && v2;
944 top->unsignedp = 0;
945 if (!v1) skip_evaluation--;
946 break;
947 case CPP_COMMA:
948 if (CPP_PEDANTIC (pfile))
949 cpp_pedwarn (pfile, "comma operator in operand of #if");
950 top->value = v2;
951 top->unsignedp = unsigned2;
952 break;
953 case CPP_QUERY:
954 SYNTAX_ERROR ("syntax error '?' without following ':'");
955 case CPP_COLON:
956 if (top[0].op != CPP_QUERY)
957 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
958 top--;
959 if (top->value) skip_evaluation--;
960 top->value = top->value ? v1 : v2;
961 top->unsignedp = unsigned1 | unsigned2;
962 break;
963 case CPP_OPEN_PAREN:
964 if (op.op != CPP_CLOSE_PAREN)
965 SYNTAX_ERROR ("missing ')' in expression");
966 op.value = v2;
967 op.unsignedp = unsigned2;
968 goto push_immediate;
969 case CPP_EOF:
970 /* Reducing this dummy operator indicates we've finished. */
971 if (op.op == CPP_CLOSE_PAREN)
972 SYNTAX_ERROR ("missing '(' in expression");
973 goto done;
977 /* Handle short-circuit evaluations. */
978 if (flags & SHORT_CIRCUIT)
979 switch (op.op)
981 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
982 case CPP_AND_AND:
983 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
984 case CPP_COLON:
985 if (top[-1].value) /* Was '?' condition true? */
986 skip_evaluation++;
987 else
988 skip_evaluation--;
989 default:
990 break;
993 skip_reduction:
994 /* Check we have a left operand iff we need one. */
995 if (flags & NO_L_OPERAND)
997 if (top->flags & HAVE_VALUE)
998 SYNTAX_ERROR2 ("missing binary operator before '%s'",
999 op_as_text (pfile, top->op));
1001 else
1003 if (!(top->flags & HAVE_VALUE))
1004 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1005 op_as_text (pfile, top->op));
1008 /* Check for and handle stack overflow. */
1009 top++;
1010 if (top == limit)
1012 struct op *new_stack;
1013 int old_size = (char *) limit - (char *) stack;
1014 int new_size = 2 * old_size;
1015 if (stack != init_stack)
1016 new_stack = (struct op *) xrealloc (stack, new_size);
1017 else
1019 new_stack = (struct op *) xmalloc (new_size);
1020 memcpy (new_stack, stack, old_size);
1022 stack = new_stack;
1023 top = (struct op *) ((char *) new_stack + old_size);
1024 limit = (struct op *) ((char *) new_stack + new_size);
1027 top->flags = flags;
1028 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
1029 top->op = op.op;
1032 done:
1033 result = (top[1].value != 0);
1034 if (top != stack)
1035 CPP_ICE ("unbalanced stack in #if");
1036 else if (!(top[1].flags & HAVE_VALUE))
1038 SYNTAX_ERROR ("#if with no expression");
1039 syntax_error:
1040 result = 0; /* Return 0 on syntax error. */
1043 /* Free dynamic stack if we allocated one. */
1044 if (stack != init_stack)
1045 free (stack);
1046 pfile->skipping = save_skipping;
1047 return result;
1050 static const unsigned char *
1051 op_as_text (pfile, op)
1052 cpp_reader *pfile;
1053 enum cpp_ttype op;
1055 cpp_token token;
1057 token.type = op;
1058 token.flags = 0;
1059 return cpp_token_as_text (pfile, &token);