1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 1992, 1994, 1995 Free Software Foundation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, 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 */
30 extern char *xmalloc PARAMS ((unsigned));
31 extern char *xrealloc PARAMS ((char *, unsigned));
33 #ifdef MULTIBYTE_CHARS
40 /* This is used for communicating lists of keywords with cccp.c. */
48 /* Define a generic NULL if one hasn't already been defined. */
55 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
56 #define GENERIC_PTR void *
58 #define GENERIC_PTR char *
63 #define NULL_PTR ((GENERIC_PTR)0)
66 extern char *xmalloc ();
68 #ifndef CHAR_TYPE_SIZE
69 #define CHAR_TYPE_SIZE BITS_PER_UNIT
73 #define INT_TYPE_SIZE BITS_PER_WORD
76 #ifndef LONG_TYPE_SIZE
77 #define LONG_TYPE_SIZE BITS_PER_WORD
80 #ifndef WCHAR_TYPE_SIZE
81 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
84 #ifndef MAX_CHAR_TYPE_SIZE
85 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
88 #ifndef MAX_INT_TYPE_SIZE
89 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
92 #ifndef MAX_LONG_TYPE_SIZE
93 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
96 #ifndef MAX_WCHAR_TYPE_SIZE
97 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
100 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
101 number with SUM's sign, where A, B, and SUM are all C integers. */
102 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
104 static void integer_overflow ();
105 static long left_shift ();
106 static long right_shift ();
121 #define LEFT_OPERAND_REQUIRED 1
122 #define RIGHT_OPERAND_REQUIRED 2
124 /*#define UNSIGNEDP 8*/
126 #ifndef HOST_BITS_PER_WIDE_INT
128 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
129 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
130 #define HOST_WIDE_INT long
132 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
133 #define HOST_WIDE_INT int
140 char rprio; /* Priority of op (relative to it right operand). */
142 char unsignedp; /* true if value should be treated as unsigned */
143 HOST_WIDE_INT value; /* The value logically "right" of op. */
146 /* Take care of parsing a number (anything that starts with a digit).
147 LEN is the number of characters in it. */
149 /* maybe needs to actually deal with floating point numbers */
152 parse_number (pfile, start, olen)
158 register char *p = start;
160 register unsigned long n = 0, nd, ULONG_MAX_over_base;
161 register int base = 10;
162 register int len = olen;
163 register int overflow = 0;
164 register int digit, largest_digit = 0;
169 for (c = 0; c < len; c++)
171 /* It's a float since it contains a point. */
173 "floating point numbers not allowed in #if expressions");
178 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
186 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
187 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
189 for (; len > 0; len--) {
192 if (c >= '0' && c <= '9')
194 else if (base == 16 && c >= 'a' && c <= 'f')
195 digit = c - 'a' + 10;
196 else if (base == 16 && c >= 'A' && c <= 'F')
197 digit = c - 'A' + 10;
199 /* `l' means long, and `u' means unsigned. */
201 if (c == 'l' || c == 'L')
204 cpp_error (pfile, "two `l's in integer constant");
207 else if (c == 'u' || c == 'U')
210 cpp_error (pfile, "two `u's in integer constant");
220 /* Don't look for any more digits after the suffixes. */
223 if (largest_digit < digit)
224 largest_digit = digit;
225 nd = n * base + digit;
226 overflow |= ULONG_MAX_over_base < n | nd < n;
232 cpp_error (pfile, "Invalid number in #if expression");
237 if (base <= largest_digit)
238 cpp_warning (pfile, "integer constant contains digits beyond the radix");
241 cpp_warning (pfile, "integer constant out of range");
243 /* If too big to be signed, consider it unsigned. */
244 if ((long) n < 0 && ! op.unsignedp)
247 cpp_warning (pfile, "integer constant is so large that it is unsigned");
261 static struct token tokentab2[] = {
275 /* Read one token. */
282 register int namelen;
283 register struct token *toktab;
284 enum cpp_token token;
286 U_CHAR *tok_start, *tok_end;
291 old_written = CPP_WRITTEN (pfile);
292 cpp_skip_hspace (pfile);
293 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
295 return parse_number (pfile,
296 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
304 token = cpp_get_token (pfile);
305 tok_start = pfile->token_buffer + old_written;
306 tok_end = CPP_PWRITTEN (pfile);
307 pfile->limit = tok_start;
310 case CPP_EOF: /* Should not happen ... */
315 if (CPP_BUFFER (pfile)->fname != NULL)
321 case CPP_HSPACE: case CPP_COMMENT:
324 return parse_number (pfile, tok_start, tok_end - tok_start);
326 cpp_error (pfile, "string constants not allowed in #if expressions");
330 /* This code for reading a character constant
331 handles multicharacter constants and wide characters.
332 It is mostly copied from c-lex.c. */
334 register int result = 0;
335 register num_chars = 0;
336 unsigned width = MAX_CHAR_TYPE_SIZE;
339 U_CHAR *ptr = tok_start;
340 #ifdef MULTIBYTE_CHARS
341 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
343 char token_buffer[MAX_LONG_TYPE_SIZE/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;
361 while (ptr < tok_end && ((c = *ptr++) != '\''))
365 c = cpp_parse_escape (pfile, &ptr);
366 if (width < HOST_BITS_PER_INT
367 && (unsigned) c >= (1 << width))
369 "escape sequence out of range for character");
374 /* Merge character into result; ignore excess chars. */
375 if (num_chars < max_chars + 1)
377 if (width < HOST_BITS_PER_INT)
378 result = (result << width) | (c & ((1 << width) - 1));
381 token_buffer[num_chars - 1] = c;
385 token_buffer[num_chars] = 0;
388 cpp_error (pfile, "malformatted character constant");
389 else if (num_chars == 0)
390 cpp_error (pfile, "empty character constant");
391 else if (num_chars > max_chars)
393 num_chars = max_chars;
394 cpp_error (pfile, "character constant too long");
396 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
397 cpp_warning (pfile, "multi-character character constant");
399 /* If char type is signed, sign-extend the constant. */
402 int num_bits = num_chars * width;
404 if (cpp_lookup (pfile, "__CHAR_UNSIGNED__",
405 sizeof ("__CHAR_UNSIGNED__")-1, -1)
406 || ((result >> (num_bits - 1)) & 1) == 0)
408 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
411 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
415 #ifdef MULTIBYTE_CHARS
416 /* Set the initial shift state and convert the next sequence. */
418 /* In all locales L'\0' is zero and mbtowc will return zero,
421 || (num_chars == 1 && token_buffer[0] != '\0'))
424 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
425 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
428 cpp_warning (pfile,"Ignoring invalid multibyte character");
435 /* This is always a signed type. */
442 return parse_number (pfile, "0", 0);
445 /* See if it is a special token of length 2. */
446 if (tok_start + 2 == tok_end)
448 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
449 if (tok_start[0] == toktab->operator[0]
450 && tok_start[1] == toktab->operator[1])
452 if (toktab->token == ERROR)
454 char *buf = (char *) alloca (40);
455 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
456 cpp_error (pfile, buf);
458 op.op = toktab->token;
469 /* Parse a C escape sequence. STRING_PTR points to a variable
470 containing a pointer to the string to parse. That pointer
471 is updated past the characters we use. The value of the
472 escape sequence is returned.
474 A negative value means the sequence \ newline was seen,
475 which is supposed to be equivalent to nothing at all.
477 If \ is followed by a null character, we return a negative
478 value and leave the string pointer pointing at the null character.
480 If \ is followed by 000, we return 0 and leave the string pointer
481 after the zeros. A value of 0 does not mean end of string. */
484 cpp_parse_escape (pfile, string_ptr)
488 register int c = *(*string_ptr)++;
497 if (CPP_PEDANTIC (pfile))
498 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
503 return TARGET_NEWLINE;
525 register int i = c - '0';
526 register int count = 0;
529 c = *(*string_ptr)++;
530 if (c >= '0' && c <= '7')
531 i = (i << 3) + c - '0';
538 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
540 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
542 "octal character constant does not fit in a byte");
548 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
551 c = *(*string_ptr)++;
552 if (c >= '0' && c <= '9')
554 else if (c >= 'a' && c <= 'f')
555 digit = c - 'a' + 10;
556 else if (c >= 'A' && c <= 'F')
557 digit = c - 'A' + 10;
563 overflow |= i ^ (i << 4 >> 4);
564 i = (i << 4) + digit;
568 cpp_error (pfile, "\\x used with no following hex digits");
569 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
571 i &= (1 << BITS_PER_UNIT) - 1;
573 "hex character constant does not fit in a byte");
583 integer_overflow (pfile)
586 if (CPP_PEDANTIC (pfile))
587 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
591 left_shift (pfile, a, unsignedp, b)
597 if (b >= HOST_BITS_PER_LONG)
599 if (! unsignedp && a != 0)
600 integer_overflow (pfile);
604 return (unsigned long) a << b;
609 integer_overflow (pfile);
615 right_shift (pfile, a, unsignedp, b)
621 if (b >= HOST_BITS_PER_LONG)
622 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
624 return (unsigned long) a >> b;
629 /* These priorities are all even, so we can handle associatively. */
630 #define PAREN_INNER_PRIO 0
632 #define COND_PRIO (COMMA_PRIO+2)
633 #define OROR_PRIO (COND_PRIO+2)
634 #define ANDAND_PRIO (OROR_PRIO+2)
635 #define OR_PRIO (ANDAND_PRIO+2)
636 #define XOR_PRIO (OR_PRIO+2)
637 #define AND_PRIO (XOR_PRIO+2)
638 #define EQUAL_PRIO (AND_PRIO+2)
639 #define LESS_PRIO (EQUAL_PRIO+2)
640 #define SHIFT_PRIO (LESS_PRIO+2)
641 #define PLUS_PRIO (SHIFT_PRIO+2)
642 #define MUL_PRIO (PLUS_PRIO+2)
643 #define UNARY_PRIO (MUL_PRIO+2)
644 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
646 #define COMPARE(OP) \
648 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP v2 : (v1 OP v2)
650 /* Parse and evaluate a C expression, reading from PFILE.
651 Returns the value of the expression. */
654 cpp_parse_expr (pfile)
657 /* The implementation is an operator precedence parser,
658 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
660 The stack base is 'stack', and the current stack pointer is 'top'.
661 There is a stack element for each operator (only),
662 and the most recently pushed operator is 'top->op'.
663 An operand (value) is stored in the 'value' field of the stack
664 element of the operator that precedes it.
665 In that case the 'flags' field has the HAVE_VALUE flag set. */
667 #define INIT_STACK_SIZE 20
668 struct operation init_stack[INIT_STACK_SIZE];
669 struct operation *stack = init_stack;
670 struct operation *limit = stack + INIT_STACK_SIZE;
671 register struct operation *top = stack;
682 op = cpp_lex (pfile);
684 /* See if the token is an operand, in which case go to set_value.
685 If the token is an operator, figure out its left and right
686 priorities, and then goto maybe_reduce. */
691 top->value = 0, top->unsignedp = 0;
694 top->value = op.value;
695 top->unsignedp = op.unsignedp;
698 lprio = 0; goto maybe_reduce;
700 /* Is this correct if unary ? FIXME */
701 flags = RIGHT_OPERAND_REQUIRED;
702 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
704 flags = RIGHT_OPERAND_REQUIRED;
705 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
706 case '*': case '/': case '%':
707 lprio = MUL_PRIO; goto binop;
708 case '<': case '>': case LEQ: case GEQ:
709 lprio = LESS_PRIO; goto binop;
710 case EQUAL: case NOTEQUAL:
711 lprio = EQUAL_PRIO; goto binop;
713 lprio = SHIFT_PRIO; goto binop;
714 case '&': lprio = AND_PRIO; goto binop;
715 case '^': lprio = XOR_PRIO; goto binop;
716 case '|': lprio = OR_PRIO; goto binop;
717 case ANDAND: lprio = ANDAND_PRIO; goto binop;
718 case OROR: lprio = OROR_PRIO; goto binop;
720 lprio = COMMA_PRIO; goto binop;
722 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
725 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
728 lprio = COND_PRIO; rprio = COND_PRIO;
731 lprio = COND_PRIO + 1; rprio = COND_PRIO;
734 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
738 cpp_error (pfile, "invalid character in #if");
743 /* Push a value onto the stack. */
744 if (top->flags & HAVE_VALUE)
746 cpp_error (pfile, "syntax error in #if");
749 top->flags |= HAVE_VALUE;
753 /* Push an operator, and check if we can reduce now. */
754 while (top->rprio > lprio)
756 long v1 = top[-1].value, v2 = top[0].value;
757 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
759 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
760 && ! (top[0].flags & HAVE_VALUE))
762 cpp_error (pfile, "syntax error - missing left operand");
765 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
766 && ! (top[1].flags & HAVE_VALUE))
768 cpp_error (pfile, "syntax error - missing right operand");
771 /* top[0].value = (top[1].op)(v1, v2);*/
775 if (!(top->flags & HAVE_VALUE))
778 top->unsignedp = unsigned2;
779 top->flags |= HAVE_VALUE;
783 top->value = v1 + v2;
784 top->unsignedp = unsigned1 || unsigned2;
786 && ! possible_sum_sign (v1, v2, top->value))
787 integer_overflow (pfile);
791 if (!(top->flags & HAVE_VALUE))
794 if ((top->value & v2) < 0 && ! unsigned2)
795 integer_overflow (pfile);
796 top->unsignedp = unsigned2;
797 top->flags |= HAVE_VALUE;
801 top->value = v1 - v2;
802 top->unsignedp = unsigned1 || unsigned2;
804 && ! possible_sum_sign (top->value, v2, v1))
805 integer_overflow (pfile);
809 top->unsignedp = unsigned1 || unsigned2;
811 top->value = (unsigned long) v1 * v2;
814 top->value = v1 * v2;
816 && (top->value / v1 != v2
817 || (top->value & v1 & v2) < 0))
818 integer_overflow (pfile);
824 cpp_error (pfile, "division by zero in #if");
827 top->unsignedp = unsigned1 || unsigned2;
829 top->value = (unsigned long) v1 / v2;
832 top->value = v1 / v2;
833 if ((top->value & v1 & v2) < 0)
834 integer_overflow (pfile);
840 cpp_error (pfile, "division by zero in #if");
843 top->unsignedp = unsigned1 || unsigned2;
845 top->value = (unsigned long) v1 % v2;
847 top->value = v1 % v2;
850 if (top->flags & HAVE_VALUE)
852 cpp_error (pfile, "syntax error");
857 top->flags |= HAVE_VALUE;
860 if (top->flags & HAVE_VALUE)
862 cpp_error (pfile, "syntax error");
866 top->unsignedp = unsigned2;
867 top->flags |= HAVE_VALUE;
869 case '<': COMPARE(<); break;
870 case '>': COMPARE(>); break;
871 case LEQ: COMPARE(<=); break;
872 case GEQ: COMPARE(>=); break;
874 top->value = (v1 == v2);
878 top->value = (v1 != v2);
882 top->unsignedp = unsigned1;
883 if (v2 < 0 && ! unsigned2)
884 top->value = right_shift (pfile, v1, unsigned1, -v2);
886 top->value = left_shift (pfile, v1, unsigned1, v2);
889 top->unsignedp = unsigned1;
890 if (v2 < 0 && ! unsigned2)
891 top->value = left_shift (pfile, v1, unsigned1, -v2);
893 top->value = right_shift (pfile, v1, unsigned1, v2);
895 #define LOGICAL(OP) \
896 top->value = v1 OP v2;\
897 top->unsignedp = unsigned1 || unsigned2;
898 case '&': LOGICAL(&); break;
899 case '^': LOGICAL(^); break;
900 case '|': LOGICAL(|); break;
902 top->value = v1 && v2; top->unsignedp = 0; break;
904 top->value = v1 || v2; top->unsignedp = 0; break;
906 if (CPP_PEDANTIC (pfile))
907 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
909 top->unsignedp = unsigned2;
912 cpp_error (pfile, "syntax error in #if");
915 if (top[0].op != '?')
918 "syntax error ':' without preceding '?'");
921 else if (! (top[1].flags & HAVE_VALUE)
922 || !(top[-1].flags & HAVE_VALUE)
923 || !(top[0].flags & HAVE_VALUE))
925 cpp_error (pfile, "bad syntax for ?: operator");
931 top->value = top->value ? v1 : v2;
932 top->unsignedp = unsigned1 || unsigned2;
936 if ((top[1].flags & HAVE_VALUE)
937 || ! (top[0].flags & HAVE_VALUE)
939 || (top[-1].flags & HAVE_VALUE))
941 cpp_error (pfile, "mismatched parentheses in #if");
948 top->unsignedp = unsigned1;
949 top->flags |= HAVE_VALUE;
954 top[1].op >= ' ' && top[1].op <= '~'
955 ? "unimplemented operator '%c'\n"
956 : "unimplemented operator '\\%03o'\n",
963 cpp_error (pfile, "internal error in #if expression");
964 if (stack != init_stack)
970 /* Check for and handle stack overflow. */
973 struct operation *new_stack;
974 int old_size = (char*)limit - (char*)stack;
975 int new_size = 2 * old_size;
976 if (stack != init_stack)
977 new_stack = (struct operation*) xrealloc (stack, new_size);
980 new_stack = (struct operation*) xmalloc (new_size);
981 bcopy ((char *) stack, (char *) new_stack, old_size);
984 top = (struct operation*)((char*) new_stack + old_size);
985 limit = (struct operation*)((char*) new_stack + new_size);
993 if (stack != init_stack)
995 skip_rest_of_line (pfile);