2 /* Parse C expressions for CCCP.
3 * Copyright (C) 1987, 1992, 1994, 1995 Free Software Foundation.
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
43 /* This is used for communicating lists of keywords with cccp.c. */
51 /* Define a generic NULL if one hasn't already been defined. */
58 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
59 #define GENERIC_PTR void *
61 #define GENERIC_PTR char *
66 #define NULL_PTR ((GENERIC_PTR)0)
69 #ifndef CHAR_TYPE_SIZE
70 #define CHAR_TYPE_SIZE BITS_PER_UNIT
74 #define INT_TYPE_SIZE BITS_PER_WORD
77 #ifndef LONG_TYPE_SIZE
78 #define LONG_TYPE_SIZE BITS_PER_WORD
81 #ifndef WCHAR_TYPE_SIZE
82 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
85 #ifndef MAX_CHAR_TYPE_SIZE
86 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
89 #ifndef MAX_INT_TYPE_SIZE
90 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
93 #ifndef MAX_LONG_TYPE_SIZE
94 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
97 #ifndef MAX_WCHAR_TYPE_SIZE
98 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
101 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
102 * number with SUM's sign, where A, B, and SUM are all C integers. */
103 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
118 #define LEFT_OPERAND_REQUIRED 1
119 #define RIGHT_OPERAND_REQUIRED 2
121 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
122 * following operand should be short-circuited instead of evaluated. */
123 #define SKIP_OPERAND 8
124 /*#define UNSIGNEDP 16 */
128 char rprio; /* Priority of op (relative to it right operand). */
130 char unsignedp; /* true if value should be treated as unsigned */
131 HOST_WIDE_INT value; /* The value logically "right" of op. */
134 /* Take care of parsing a number (anything that starts with a digit).
135 * LEN is the number of characters in it. */
137 /* maybe needs to actually deal with floating point numbers */
140 parse_number(struct operation *op, cpp_reader * pfile, const char *start,
143 const char *p = start;
145 unsigned long n = 0, nd, ULONG_MAX_over_base;
149 int digit, largest_digit = 0;
154 for (c = 0; c < len; c++)
157 /* It's a float since it contains a point. */
159 "floating point numbers not allowed in #if expressions");
163 if (len >= 3 && (!strncmp(p, "0x", 2) || !strncmp(p, "0X", 2)))
172 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
173 ULONG_MAX_over_base = ((unsigned long)-1) / ((unsigned long)base);
175 for (; len > 0; len--)
179 if (c >= '0' && c <= '9')
181 else if (base == 16 && c >= 'a' && c <= 'f')
182 digit = c - 'a' + 10;
183 else if (base == 16 && c >= 'A' && c <= 'F')
184 digit = c - 'A' + 10;
187 /* `l' means long, and `u' means unsigned. */
190 if (c == 'l' || c == 'L')
193 cpp_error(pfile, "two `l's in integer constant");
196 else if (c == 'u' || c == 'U')
199 cpp_error(pfile, "two `u's in integer constant");
209 /* Don't look for any more digits after the suffixes. */
212 if (largest_digit < digit)
213 largest_digit = digit;
214 nd = n * base + digit;
215 overflow |= (ULONG_MAX_over_base < n) | (nd < n);
221 cpp_error(pfile, "Invalid number in #if expression");
225 if (base <= largest_digit)
226 cpp_warning(pfile, "integer constant contains digits beyond the radix");
229 cpp_warning(pfile, "integer constant out of range");
231 /* If too big to be signed, consider it unsigned. */
232 if ((long)n < 0 && !op->unsignedp)
236 "integer constant is so large that it is unsigned");
248 static struct token tokentab2[] = {
262 /* Read one token. */
265 cpp_lex(struct operation *op, cpp_reader * pfile)
268 struct token *toktab;
269 enum cpp_token token;
270 unsigned char *tok_start, *tok_end;
278 old_written = CPP_WRITTEN(pfile);
279 cpp_skip_hspace(pfile);
280 c = CPP_BUF_PEEK(CPP_BUFFER(pfile));
283 parse_number(op, pfile, cpp_read_check_assertion(pfile) ? "1" : "0", 1);
292 token = cpp_get_token(pfile);
293 tok_start = pfile->token_buffer + old_written;
294 tok_end = CPP_PWRITTEN(pfile);
295 pfile->limit = tok_start;
298 case CPP_EOF: /* Should not happen ... */
304 if (CPP_BUFFER(pfile)->fname != NULL)
316 parse_number(op, pfile, (char *)tok_start, tok_end - tok_start);
320 cpp_error(pfile, "string constants not allowed in #if expressions");
325 /* This code for reading a character constant
326 * handles multicharacter constants and wide characters.
327 * It is mostly copied from c-lex.c. */
331 unsigned width = MAX_CHAR_TYPE_SIZE;
334 char *ptr = (char *)tok_start;
336 #ifdef MULTIBYTE_CHARS
337 char token_buffer[MAX_LONG_TYPE_SIZE /
338 MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
341 char token_buffer[MAX_LONG_TYPE_SIZE /
342 MAX_CHAR_TYPE_SIZE + 1];
350 width = MAX_WCHAR_TYPE_SIZE;
351 #ifdef MULTIBYTE_CHARS
352 max_chars = MB_CUR_MAX;
358 max_chars = MAX_LONG_TYPE_SIZE / width;
362 if (ptr >= (char *)CPP_PWRITTEN(pfile) || (c = *ptr++) == '\'')
367 c = cpp_parse_escape(pfile, &ptr);
368 if (width < HOST_BITS_PER_INT
369 && (unsigned)c >= (unsigned)(1 << width))
371 "escape sequence out of range for character");
375 /* Merge character into result; ignore excess chars. */
376 if (num_chars < max_chars + 1)
378 if (width < HOST_BITS_PER_INT)
379 result = (result << width) | (c & ((1 << width) - 1));
382 token_buffer[num_chars - 1] = c;
386 token_buffer[num_chars] = 0;
389 cpp_error(pfile, "malformatted character constant");
390 else if (num_chars == 0)
391 cpp_error(pfile, "empty character constant");
392 else if (num_chars > max_chars)
394 num_chars = max_chars;
395 cpp_error(pfile, "character constant too long");
397 else if (num_chars != 1 && !CPP_TRADITIONAL(pfile))
398 cpp_warning(pfile, "multi-character character constant");
400 /* If char type is signed, sign-extend the constant. */
403 int num_bits = num_chars * width;
405 if (cpp_lookup("__CHAR_UNSIGNED__",
406 sizeof("__CHAR_UNSIGNED__") - 1, -1)
407 || ((result >> (num_bits - 1)) & 1) == 0)
409 result & ((unsigned long)~0 >>
410 (HOST_BITS_PER_LONG - num_bits));
413 result | ~((unsigned long)~0 >>
414 (HOST_BITS_PER_LONG - num_bits));
418 #ifdef MULTIBYTE_CHARS
419 /* Set the initial shift state and convert the next sequence. */
421 /* In all locales L'\0' is zero and mbtowc will return zero,
422 * so don't use it. */
424 || (num_chars == 1 && token_buffer[0] != '\0'))
428 (void)mbtowc(NULL_PTR, NULL_PTR, 0);
429 if (mbtowc(&wc, token_buffer, num_chars) == num_chars)
433 "Ignoring invalid multibyte character");
440 /* This is always a signed type. */
446 parse_number(op, pfile, "0", 0);
450 /* See if it is a special token of length 2. */
451 if (tok_start + 2 == tok_end)
453 for (toktab = tokentab2; toktab->oper != NULL; toktab++)
454 if (tok_start[0] == toktab->oper[0]
455 && tok_start[1] == toktab->oper[1])
457 if (toktab->token == ERROR)
459 char *buf = (char *)malloc(40);
463 sprintf(buf, "`%s' not allowed in operand of `#if'",
465 cpp_error(pfile, buf);
468 op->op = toktab->token;
478 /* Parse a C escape sequence. STRING_PTR points to a variable
479 * containing a pointer to the string to parse. That pointer
480 * is updated past the characters we use. The value of the
481 * escape sequence is returned.
483 * A negative value means the sequence \ newline was seen,
484 * which is supposed to be equivalent to nothing at all.
486 * If \ is followed by a null character, we return a negative
487 * value and leave the string pointer pointing at the null character.
489 * If \ is followed by 000, we return 0 and leave the string pointer
490 * after the zeros. A value of 0 does not mean end of string. */
493 cpp_parse_escape(cpp_reader * pfile, char **string_ptr)
495 int c = *(*string_ptr)++;
505 if (CPP_PEDANTIC(pfile))
506 cpp_pedwarn(pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
511 return TARGET_NEWLINE;
538 c = *(*string_ptr)++;
539 if (c >= '0' && c <= '7')
540 i = (i << 3) + c - '0';
547 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
549 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
551 "octal character constant does not fit in a byte");
557 unsigned i = 0, overflow = 0, digits_found = 0, digit;
561 c = *(*string_ptr)++;
562 if (c >= '0' && c <= '9')
564 else if (c >= 'a' && c <= 'f')
565 digit = c - 'a' + 10;
566 else if (c >= 'A' && c <= 'F')
567 digit = c - 'A' + 10;
573 overflow |= i ^ (i << 4 >> 4);
574 i = (i << 4) + digit;
578 cpp_error(pfile, "\\x used with no following hex digits");
579 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
581 i &= (1 << BITS_PER_UNIT) - 1;
583 "hex character constant does not fit in a byte");
593 integer_overflow(cpp_reader * pfile)
595 if (CPP_PEDANTIC(pfile))
596 cpp_pedwarn(pfile, "integer overflow in preprocessor expression");
600 left_shift(cpp_reader * pfile, long a, int unsignedp, unsigned long b)
602 if (b >= HOST_BITS_PER_LONG)
604 if (!unsignedp && a != 0)
605 integer_overflow(pfile);
609 return (unsigned long)a << b;
615 integer_overflow(pfile);
621 right_shift(cpp_reader * pfile, long a, int unsignedp, unsigned long b)
624 if (b >= HOST_BITS_PER_LONG)
626 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
630 return (unsigned long)a >> b;
638 /* These priorities are all even, so we can handle associatively. */
639 #define PAREN_INNER_PRIO 0
641 #define COND_PRIO (COMMA_PRIO+2)
642 #define OROR_PRIO (COND_PRIO+2)
643 #define ANDAND_PRIO (OROR_PRIO+2)
644 #define OR_PRIO (ANDAND_PRIO+2)
645 #define XOR_PRIO (OR_PRIO+2)
646 #define AND_PRIO (XOR_PRIO+2)
647 #define EQUAL_PRIO (AND_PRIO+2)
648 #define LESS_PRIO (EQUAL_PRIO+2)
649 #define SHIFT_PRIO (LESS_PRIO+2)
650 #define PLUS_PRIO (SHIFT_PRIO+2)
651 #define MUL_PRIO (PLUS_PRIO+2)
652 #define UNARY_PRIO (MUL_PRIO+2)
653 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
655 #define COMPARE(OP) \
657 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
659 /* Parse and evaluate a C expression, reading from PFILE.
660 * Returns the value of the expression. */
663 cpp_parse_expr(cpp_reader * pfile)
665 /* The implementation is an operator precedence parser,
666 * i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
668 * The stack base is 'stack', and the current stack pointer is 'top'.
669 * There is a stack element for each operator (only),
670 * and the most recently pushed operator is 'top->op'.
671 * An operand (value) is stored in the 'value' field of the stack
672 * element of the operator that precedes it.
673 * In that case the 'flags' field has the HAVE_VALUE flag set. */
675 #define INIT_STACK_SIZE 20
676 struct operation init_stack[INIT_STACK_SIZE];
677 struct operation *stack = init_stack;
678 struct operation *limit = stack + INIT_STACK_SIZE;
679 struct operation *top = stack;
680 int lprio = 0, rprio = 0;
681 int skip_evaluation = 0;
693 /* See if the token is an operand, in which case go to set_value.
694 * If the token is an operator, figure out its left and right
695 * priorities, and then goto maybe_reduce. */
700 top->value = 0, top->unsignedp = 0;
704 top->value = op.value;
705 top->unsignedp = op.unsignedp;
712 /* Is this correct if unary ? FIXME */
713 flags = RIGHT_OPERAND_REQUIRED;
719 flags = RIGHT_OPERAND_REQUIRED;
761 lprio = PAREN_OUTER_PRIO;
762 rprio = PAREN_INNER_PRIO;
765 lprio = PAREN_INNER_PRIO;
766 rprio = PAREN_OUTER_PRIO;
773 lprio = COND_PRIO + 1;
777 flags = LEFT_OPERAND_REQUIRED | RIGHT_OPERAND_REQUIRED;
781 cpp_error(pfile, "invalid character in #if");
786 /* Push a value onto the stack. */
787 if (top->flags & HAVE_VALUE)
789 cpp_error(pfile, "syntax error in #if");
792 top->flags |= HAVE_VALUE;
796 /* Push an operator, and check if we can reduce now. */
797 while (top->rprio > lprio)
799 long v1 = top[-1].value, v2 = top[0].value;
800 int unsigned1 = top[-1].unsignedp, unsigned2 =
804 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
805 && !(top[0].flags & HAVE_VALUE))
807 cpp_error(pfile, "syntax error - missing left operand");
810 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
811 && !(top[1].flags & HAVE_VALUE))
813 cpp_error(pfile, "syntax error - missing right operand");
816 /* top[0].value = (top[1].op)(v1, v2); */
820 if (!(top->flags & HAVE_VALUE))
823 top->unsignedp = unsigned2;
824 top->flags |= HAVE_VALUE;
828 top->value = v1 + v2;
829 top->unsignedp = unsigned1 || unsigned2;
830 if (!top->unsignedp && !skip_evaluation
831 && !possible_sum_sign(v1, v2, top->value))
832 integer_overflow(pfile);
836 if (skip_evaluation); /* do nothing */
837 else if (!(top->flags & HAVE_VALUE))
840 if ((top->value & v2) < 0 && !unsigned2)
841 integer_overflow(pfile);
842 top->unsignedp = unsigned2;
843 top->flags |= HAVE_VALUE;
847 top->value = v1 - v2;
848 top->unsignedp = unsigned1 || unsigned2;
850 && !possible_sum_sign(top->value, v2, v1))
851 integer_overflow(pfile);
855 top->unsignedp = unsigned1 || unsigned2;
857 top->value = (unsigned long)v1 *v2;
859 else if (!skip_evaluation)
861 top->value = v1 * v2;
863 && (top->value / v1 != v2
864 || (top->value & v1 & v2) < 0))
865 integer_overflow(pfile);
873 cpp_error(pfile, "division by zero in #if");
876 top->unsignedp = unsigned1 || unsigned2;
878 top->value = (unsigned long)v1 / v2;
881 top->value = v1 / v2;
882 if ((top->value & v1 & v2) < 0)
883 integer_overflow(pfile);
891 cpp_error(pfile, "division by zero in #if");
894 top->unsignedp = unsigned1 || unsigned2;
896 top->value = (unsigned long)v1 % v2;
898 top->value = v1 % v2;
901 if (top->flags & HAVE_VALUE)
903 cpp_error(pfile, "syntax error");
908 top->flags |= HAVE_VALUE;
911 if (top->flags & HAVE_VALUE)
913 cpp_error(pfile, "syntax error");
917 top->unsignedp = unsigned2;
918 top->flags |= HAVE_VALUE;
933 top->value = (v1 == v2);
937 top->value = (v1 != v2);
943 top->unsignedp = unsigned1;
944 if (v2 < 0 && !unsigned2)
945 top->value = right_shift(pfile, v1, unsigned1, -v2);
947 top->value = left_shift(pfile, v1, unsigned1, v2);
952 top->unsignedp = unsigned1;
953 if (v2 < 0 && !unsigned2)
954 top->value = left_shift(pfile, v1, unsigned1, -v2);
956 top->value = right_shift(pfile, v1, unsigned1, v2);
958 #define LOGICAL(OP) \
959 top->value = v1 OP v2;\
960 top->unsignedp = unsigned1 || unsigned2;
971 top->value = v1 && v2;
977 top->value = v1 || v2;
983 if (CPP_PEDANTIC(pfile))
984 cpp_pedwarn(pfile, "comma operator in operand of `#if'");
986 top->unsignedp = unsigned2;
990 cpp_error(pfile, "syntax error in #if");
993 if (top[0].op != '?')
996 "syntax error ':' without preceding '?'");
999 else if (!(top[1].flags & HAVE_VALUE)
1000 || !(top[-1].flags & HAVE_VALUE)
1001 || !(top[0].flags & HAVE_VALUE))
1003 cpp_error(pfile, "bad syntax for ?: operator");
1011 top->value = top->value ? v1 : v2;
1012 top->unsignedp = unsigned1 || unsigned2;
1016 if ((top[1].flags & HAVE_VALUE)
1017 || !(top[0].flags & HAVE_VALUE)
1018 || top[0].op != '(' || (top[-1].flags & HAVE_VALUE))
1020 cpp_error(pfile, "mismatched parentheses in #if");
1027 top->unsignedp = unsigned1;
1028 top->flags |= HAVE_VALUE;
1033 top[1].op >= ' ' && top[1].op <= '~'
1034 ? "unimplemented operator '%c'\n"
1035 : "unimplemented operator '\\%03o'\n", top[1].op);
1041 cpp_error(pfile, "internal error in #if expression");
1042 if (stack != init_stack)
1048 /* Check for and handle stack overflow. */
1051 struct operation *new_stack;
1052 int old_size = (char *)limit - (char *)stack;
1053 int new_size = 2 * old_size;
1055 if (stack != init_stack)
1056 new_stack = (struct operation *)xrealloc(stack, new_size);
1059 new_stack = (struct operation *)xmalloc(new_size);
1060 memcpy((char *)new_stack, (char *)stack, old_size);
1063 top = (struct operation *)((char *)new_stack + old_size);
1064 limit = (struct operation *)((char *)new_stack + new_size);
1069 if ((op.op == OROR && top[-1].value)
1070 || (op.op == ANDAND && !top[-1].value)
1071 || (op.op == '?' && !top[-1].value))
1075 else if (op.op == ':')
1077 if (top[-2].value) /* Was condition true? */
1084 if (stack != init_stack)
1086 skip_rest_of_line(pfile);