1 /* Parse C expressions for CCCP.
2 * Copyright (C) 1987, 1992, 1994, 1995 Free Software Foundation.
3 * Copyright (C) 2003-2011 Kim Woelders
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
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, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * In other words, you are welcome to use, share and improve this program.
20 * You are forbidden to forbid anyone else to use, share and improve
21 * what you give them. Help stamp out software-hoarding!
23 * Written by Per Bothner 1994. */
25 /* Parse a C expression from text in a string */
35 #ifdef MULTIBYTE_CHARS
46 /* This is used for communicating lists of keywords with cccp.c. */
54 /* Define a generic NULL if one hasn't already been defined. */
61 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
62 #define GENERIC_PTR void *
64 #define GENERIC_PTR char *
69 #define NULL_PTR ((GENERIC_PTR)0)
72 #ifndef CHAR_TYPE_SIZE
73 #define CHAR_TYPE_SIZE BITS_PER_UNIT
77 #define INT_TYPE_SIZE BITS_PER_WORD
80 #ifndef LONG_TYPE_SIZE
81 #define LONG_TYPE_SIZE BITS_PER_WORD
84 #ifndef WCHAR_TYPE_SIZE
85 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
88 #ifndef MAX_CHAR_TYPE_SIZE
89 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
92 #ifndef MAX_INT_TYPE_SIZE
93 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
96 #ifndef MAX_LONG_TYPE_SIZE
97 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
100 #ifndef MAX_WCHAR_TYPE_SIZE
101 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
104 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
105 * number with SUM's sign, where A, B, and SUM are all C integers. */
106 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
121 #define LEFT_OPERAND_REQUIRED 1
122 #define RIGHT_OPERAND_REQUIRED 2
124 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
125 * following operand should be short-circuited instead of evaluated. */
126 #define SKIP_OPERAND 8
127 /*#define UNSIGNEDP 16 */
131 char rprio; /* Priority of op (relative to it right operand). */
133 char unsignedp; /* true if value should be treated as unsigned */
134 HOST_WIDE_INT value; /* The value logically "right" of op. */
137 /* Take care of parsing a number (anything that starts with a digit).
138 * LEN is the number of characters in it. */
140 /* maybe needs to actually deal with floating point numbers */
143 parse_number(struct operation *op, cpp_reader * pfile, const char *start,
146 const char *p = start;
148 unsigned long n = 0, nd, ULONG_MAX_over_base;
152 int digit, largest_digit = 0;
157 for (c = 0; c < len; c++)
160 /* It's a float since it contains a point. */
162 "floating point numbers not allowed in #if expressions");
166 if (len >= 3 && (!strncmp(p, "0x", 2) || !strncmp(p, "0X", 2)))
175 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
176 ULONG_MAX_over_base = ((unsigned long)-1) / ((unsigned long)base);
178 for (; len > 0; len--)
182 if (c >= '0' && c <= '9')
184 else if (base == 16 && c >= 'a' && c <= 'f')
185 digit = c - 'a' + 10;
186 else if (base == 16 && c >= 'A' && c <= 'F')
187 digit = c - 'A' + 10;
190 /* `l' means long, and `u' means unsigned. */
193 if (c == 'l' || c == 'L')
196 cpp_error(pfile, "two `l's in integer constant");
199 else if (c == 'u' || c == 'U')
202 cpp_error(pfile, "two `u's in integer constant");
212 /* Don't look for any more digits after the suffixes. */
215 if (largest_digit < digit)
216 largest_digit = digit;
217 nd = n * base + digit;
218 overflow |= (ULONG_MAX_over_base < n) | (nd < n);
224 cpp_error(pfile, "Invalid number in #if expression");
228 if (base <= largest_digit)
229 cpp_warning(pfile, "integer constant contains digits beyond the radix");
232 cpp_warning(pfile, "integer constant out of range");
234 /* If too big to be signed, consider it unsigned. */
235 if ((long)n < 0 && !op->unsignedp)
239 "integer constant is so large that it is unsigned");
251 static struct token tokentab2[] = {
265 /* Read one token. */
268 cpp_lex(struct operation *op, cpp_reader * pfile)
271 struct token *toktab;
272 enum cpp_token token;
273 unsigned char *tok_start, *tok_end;
281 old_written = CPP_WRITTEN(pfile);
282 cpp_skip_hspace(pfile);
283 c = CPP_BUF_PEEK(CPP_BUFFER(pfile));
286 parse_number(op, pfile, cpp_read_check_assertion(pfile) ? "1" : "0", 1);
295 token = cpp_get_token(pfile);
296 tok_start = pfile->token_buffer + old_written;
297 tok_end = CPP_PWRITTEN(pfile);
298 pfile->limit = tok_start;
301 case CPP_EOF: /* Should not happen ... */
307 if (CPP_BUFFER(pfile)->fname)
319 parse_number(op, pfile, (char *)tok_start, tok_end - tok_start);
323 cpp_error(pfile, "string constants not allowed in #if expressions");
328 /* This code for reading a character constant
329 * handles multicharacter constants and wide characters.
330 * It is mostly copied from c-lex.c. */
334 unsigned width = MAX_CHAR_TYPE_SIZE;
337 char *ptr = (char *)tok_start;
339 #ifdef MULTIBYTE_CHARS
340 char token_buffer[MAX_LONG_TYPE_SIZE /
341 MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
348 width = MAX_WCHAR_TYPE_SIZE;
349 #ifdef MULTIBYTE_CHARS
350 max_chars = MB_CUR_MAX;
356 max_chars = MAX_LONG_TYPE_SIZE / width;
360 if (ptr >= (char *)CPP_PWRITTEN(pfile) || (c = *ptr++) == '\'')
365 c = cpp_parse_escape(pfile, &ptr);
366 if (width < HOST_BITS_PER_INT
367 && (unsigned)c >= (unsigned)(1 << width))
369 "escape sequence out of range for character");
373 /* Merge character into result; ignore excess chars. */
374 if (num_chars < max_chars + 1)
376 if (width < HOST_BITS_PER_INT)
377 result = (result << width) | (c & ((1 << width) - 1));
380 #ifdef MULTIBYTE_CHARS
381 token_buffer[num_chars - 1] = c;
386 #ifdef MULTIBYTE_CHARS
387 token_buffer[num_chars] = 0;
391 cpp_error(pfile, "malformatted character constant");
392 else if (num_chars == 0)
393 cpp_error(pfile, "empty character constant");
394 else if (num_chars > max_chars)
396 num_chars = max_chars;
397 cpp_error(pfile, "character constant too long");
399 else if (num_chars != 1 && !CPP_TRADITIONAL(pfile))
400 cpp_warning(pfile, "multi-character character constant");
402 /* If char type is signed, sign-extend the constant. */
405 int num_bits = num_chars * width;
407 if (cpp_lookup("__CHAR_UNSIGNED__",
408 sizeof("__CHAR_UNSIGNED__") - 1, -1)
409 || ((result >> (num_bits - 1)) & 1) == 0)
411 result & ((unsigned long)~0 >>
412 (HOST_BITS_PER_LONG - num_bits));
415 result | ~((unsigned long)~0 >>
416 (HOST_BITS_PER_LONG - num_bits));
420 #ifdef MULTIBYTE_CHARS
421 /* Set the initial shift state and convert the next sequence. */
423 /* In all locales L'\0' is zero and mbtowc will return zero,
424 * so don't use it. */
426 || (num_chars == 1 && token_buffer[0] != '\0'))
430 (void)mbtowc(NULL_PTR, NULL_PTR, 0);
431 if (mbtowc(&wc, token_buffer, num_chars) == num_chars)
435 "Ignoring invalid multibyte character");
442 /* This is always a signed type. */
448 parse_number(op, pfile, "0", 0);
452 /* See if it is a special token of length 2. */
453 if (tok_start + 2 == tok_end)
455 for (toktab = tokentab2; toktab->oper; toktab++)
456 if (tok_start[0] == toktab->oper[0]
457 && tok_start[1] == toktab->oper[1])
459 if (toktab->token == ERROR)
461 char *buf = (char *)malloc(40);
465 sprintf(buf, "`%s' not allowed in operand of `#if'",
467 cpp_error(pfile, buf);
470 op->op = toktab->token;
480 /* Parse a C escape sequence. STRING_PTR points to a variable
481 * containing a pointer to the string to parse. That pointer
482 * is updated past the characters we use. The value of the
483 * escape sequence is returned.
485 * A negative value means the sequence \ newline was seen,
486 * which is supposed to be equivalent to nothing at all.
488 * If \ is followed by a null character, we return a negative
489 * value and leave the string pointer pointing at the null character.
491 * If \ is followed by 000, we return 0 and leave the string pointer
492 * after the zeros. A value of 0 does not mean end of string. */
495 cpp_parse_escape(cpp_reader * pfile, char **string_ptr)
497 int c = *(*string_ptr)++;
507 if (CPP_PEDANTIC(pfile))
508 cpp_pedwarn(pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
513 return TARGET_NEWLINE;
540 c = *(*string_ptr)++;
541 if (c >= '0' && c <= '7')
542 i = (i << 3) + c - '0';
549 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
551 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
553 "octal character constant does not fit in a byte");
559 unsigned i = 0, overflow = 0, digits_found = 0, digit;
563 c = *(*string_ptr)++;
564 if (c >= '0' && c <= '9')
566 else if (c >= 'a' && c <= 'f')
567 digit = c - 'a' + 10;
568 else if (c >= 'A' && c <= 'F')
569 digit = c - 'A' + 10;
575 overflow |= i ^ (i << 4 >> 4);
576 i = (i << 4) + digit;
580 cpp_error(pfile, "\\x used with no following hex digits");
581 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
583 i &= (1 << BITS_PER_UNIT) - 1;
585 "hex character constant does not fit in a byte");
595 integer_overflow(cpp_reader * pfile)
597 if (CPP_PEDANTIC(pfile))
598 cpp_pedwarn(pfile, "integer overflow in preprocessor expression");
602 left_shift(cpp_reader * pfile, long a, int unsignedp, unsigned long b)
604 if (b >= HOST_BITS_PER_LONG)
606 if (!unsignedp && a != 0)
607 integer_overflow(pfile);
611 return (unsigned long)a << b;
617 integer_overflow(pfile);
623 right_shift(cpp_reader * pfile __UNUSED__, long a, int unsignedp,
626 if (b >= HOST_BITS_PER_LONG)
628 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
632 return (unsigned long)a >> b;
640 /* These priorities are all even, so we can handle associatively. */
641 #define PAREN_INNER_PRIO 0
643 #define COND_PRIO (COMMA_PRIO+2)
644 #define OROR_PRIO (COND_PRIO+2)
645 #define ANDAND_PRIO (OROR_PRIO+2)
646 #define OR_PRIO (ANDAND_PRIO+2)
647 #define XOR_PRIO (OR_PRIO+2)
648 #define AND_PRIO (XOR_PRIO+2)
649 #define EQUAL_PRIO (AND_PRIO+2)
650 #define LESS_PRIO (EQUAL_PRIO+2)
651 #define SHIFT_PRIO (LESS_PRIO+2)
652 #define PLUS_PRIO (SHIFT_PRIO+2)
653 #define MUL_PRIO (PLUS_PRIO+2)
654 #define UNARY_PRIO (MUL_PRIO+2)
655 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
657 #define COMPARE(OP) \
659 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
661 /* Parse and evaluate a C expression, reading from PFILE.
662 * Returns the value of the expression. */
665 cpp_parse_expr(cpp_reader * pfile)
667 /* The implementation is an operator precedence parser,
668 * i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
670 * The stack base is 'stack', and the current stack pointer is 'top'.
671 * There is a stack element for each operator (only),
672 * and the most recently pushed operator is 'top->op'.
673 * An operand (value) is stored in the 'value' field of the stack
674 * element of the operator that precedes it.
675 * In that case the 'flags' field has the HAVE_VALUE flag set. */
677 #define INIT_STACK_SIZE 20
678 struct operation init_stack[INIT_STACK_SIZE];
679 struct operation *stack = init_stack;
680 struct operation *limit = stack + INIT_STACK_SIZE;
681 struct operation *top = stack;
682 int lprio = 0, rprio = 0;
683 int skip_evaluation = 0;
695 /* See if the token is an operand, in which case go to set_value.
696 * If the token is an operator, figure out its left and right
697 * priorities, and then goto maybe_reduce. */
702 top->value = 0, top->unsignedp = 0;
706 top->value = op.value;
707 top->unsignedp = op.unsignedp;
714 /* Is this correct if unary ? FIXME */
715 flags = RIGHT_OPERAND_REQUIRED;
721 flags = RIGHT_OPERAND_REQUIRED;
763 lprio = PAREN_OUTER_PRIO;
764 rprio = PAREN_INNER_PRIO;
767 lprio = PAREN_INNER_PRIO;
768 rprio = PAREN_OUTER_PRIO;
775 lprio = COND_PRIO + 1;
779 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
783 cpp_error(pfile, "invalid character in #if");
788 /* Push a value onto the stack. */
789 if (top->flags & HAVE_VALUE)
791 cpp_error(pfile, "syntax error in #if");
794 top->flags |= HAVE_VALUE;
798 /* Push an operator, and check if we can reduce now. */
799 while (top->rprio > lprio)
801 long v1 = top[-1].value, v2 = top[0].value;
802 int unsigned1 = top[-1].unsignedp, unsigned2 =
806 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
807 && !(top[0].flags & HAVE_VALUE))
809 cpp_error(pfile, "syntax error - missing left operand");
812 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
813 && !(top[1].flags & HAVE_VALUE))
815 cpp_error(pfile, "syntax error - missing right operand");
818 /* top[0].value = (top[1].op)(v1, v2); */
822 if (!(top->flags & HAVE_VALUE))
825 top->unsignedp = unsigned2;
826 top->flags |= HAVE_VALUE;
830 top->value = v1 + v2;
831 top->unsignedp = unsigned1 || unsigned2;
832 if (!top->unsignedp && !skip_evaluation
833 && !possible_sum_sign(v1, v2, top->value))
834 integer_overflow(pfile);
838 if (skip_evaluation); /* do nothing */
839 else if (!(top->flags & HAVE_VALUE))
842 if ((top->value & v2) < 0 && !unsigned2)
843 integer_overflow(pfile);
844 top->unsignedp = unsigned2;
845 top->flags |= HAVE_VALUE;
849 top->value = v1 - v2;
850 top->unsignedp = unsigned1 || unsigned2;
852 && !possible_sum_sign(top->value, v2, v1))
853 integer_overflow(pfile);
857 top->unsignedp = unsigned1 || unsigned2;
859 top->value = (unsigned long)v1 *v2;
861 else if (!skip_evaluation)
863 top->value = v1 * v2;
865 && (top->value / v1 != v2
866 || (top->value & v1 & v2) < 0))
867 integer_overflow(pfile);
875 cpp_error(pfile, "division by zero in #if");
878 top->unsignedp = unsigned1 || unsigned2;
880 top->value = (unsigned long)v1 / v2;
883 top->value = v1 / v2;
884 if ((top->value & v1 & v2) < 0)
885 integer_overflow(pfile);
893 cpp_error(pfile, "division by zero in #if");
896 top->unsignedp = unsigned1 || unsigned2;
898 top->value = (unsigned long)v1 % v2;
900 top->value = v1 % v2;
903 if (top->flags & HAVE_VALUE)
905 cpp_error(pfile, "syntax error");
910 top->flags |= HAVE_VALUE;
913 if (top->flags & HAVE_VALUE)
915 cpp_error(pfile, "syntax error");
919 top->unsignedp = unsigned2;
920 top->flags |= HAVE_VALUE;
935 top->value = (v1 == v2);
939 top->value = (v1 != v2);
945 top->unsignedp = unsigned1;
946 if (v2 < 0 && !unsigned2)
947 top->value = right_shift(pfile, v1, unsigned1, -v2);
949 top->value = left_shift(pfile, v1, unsigned1, v2);
954 top->unsignedp = unsigned1;
955 if (v2 < 0 && !unsigned2)
956 top->value = left_shift(pfile, v1, unsigned1, -v2);
958 top->value = right_shift(pfile, v1, unsigned1, v2);
960 #define LOGICAL(OP) \
961 top->value = v1 OP v2;\
962 top->unsignedp = unsigned1 || unsigned2;
973 top->value = v1 && v2;
979 top->value = v1 || v2;
985 if (CPP_PEDANTIC(pfile))
986 cpp_pedwarn(pfile, "comma operator in operand of `#if'");
988 top->unsignedp = unsigned2;
992 cpp_error(pfile, "syntax error in #if");
995 if (top[0].op != '?')
998 "syntax error ':' without preceding '?'");
1001 else if (!(top[1].flags & HAVE_VALUE)
1002 || !(top[-1].flags & HAVE_VALUE)
1003 || !(top[0].flags & HAVE_VALUE))
1005 cpp_error(pfile, "bad syntax for ?: operator");
1013 top->value = top->value ? v1 : v2;
1014 top->unsignedp = unsigned1 || unsigned2;
1018 if ((top[1].flags & HAVE_VALUE)
1019 || !(top[0].flags & HAVE_VALUE)
1020 || top[0].op != '(' || (top[-1].flags & HAVE_VALUE))
1022 cpp_error(pfile, "mismatched parentheses in #if");
1029 top->unsignedp = unsigned1;
1030 top->flags |= HAVE_VALUE;
1035 top[1].op >= ' ' && top[1].op <= '~'
1036 ? "unimplemented operator '%c'\n"
1037 : "unimplemented operator '\\%03o'\n", top[1].op);
1043 cpp_error(pfile, "internal error in #if expression");
1044 if (stack != init_stack)
1050 /* Check for and handle stack overflow. */
1053 struct operation *new_stack;
1054 int old_size = (char *)limit - (char *)stack;
1055 int new_size = 2 * old_size;
1057 if (stack != init_stack)
1058 new_stack = (struct operation *)xrealloc(stack, new_size);
1061 new_stack = (struct operation *)xmalloc(new_size);
1062 memcpy((char *)new_stack, (char *)stack, old_size);
1065 top = (struct operation *)((char *)new_stack + old_size);
1066 limit = (struct operation *)((char *)new_stack + new_size);
1071 if ((op.op == OROR && top[-1].value)
1072 || (op.op == ANDAND && !top[-1].value)
1073 || (op.op == '?' && !top[-1].value))
1077 else if (op.op == ':')
1079 if (top[-2].value) /* Was condition true? */
1086 if (stack != init_stack)
1088 skip_rest_of_line(pfile);