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
41 /* This is used for communicating lists of keywords with cccp.c. */
49 /* Define a generic NULL if one hasn't already been defined. */
56 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
57 #define GENERIC_PTR void *
59 #define GENERIC_PTR char *
64 #define NULL_PTR ((GENERIC_PTR)0)
67 extern char *xmalloc ();
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)
105 static void integer_overflow ();
106 static long left_shift ();
107 static long right_shift ();
122 #define LEFT_OPERAND_REQUIRED 1
123 #define RIGHT_OPERAND_REQUIRED 2
125 /*#define UNSIGNEDP 8*/
127 #ifndef HOST_BITS_PER_WIDE_INT
129 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
130 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
131 #define HOST_WIDE_INT long
133 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
134 #define HOST_WIDE_INT int
141 char rprio; /* Priority of op (relative to it right operand). */
143 char unsignedp; /* true if value should be treated as unsigned */
144 HOST_WIDE_INT value; /* The value logically "right" of op. */
147 /* Take care of parsing a number (anything that starts with a digit).
148 LEN is the number of characters in it. */
150 /* maybe needs to actually deal with floating point numbers */
159 register char *p = start;
161 register unsigned long n = 0, nd, ULONG_MAX_over_base;
162 register int base = 10;
163 register int len = olen;
164 register int overflow = 0;
165 register int digit, largest_digit = 0;
170 for (c = 0; c < len; c++)
172 /* It's a float since it contains a point. */
174 "floating point numbers not allowed in #if expressions");
179 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
187 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
188 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
190 for (; len > 0; len--) {
193 if (c >= '0' && c <= '9')
195 else if (base == 16 && c >= 'a' && c <= 'f')
196 digit = c - 'a' + 10;
197 else if (base == 16 && c >= 'A' && c <= 'F')
198 digit = c - 'A' + 10;
200 /* `l' means long, and `u' means unsigned. */
202 if (c == 'l' || c == 'L')
205 cpp_error (pfile, "two `l's in integer constant");
208 else if (c == 'u' || c == 'U')
211 cpp_error (pfile, "two `u's in integer constant");
221 /* Don't look for any more digits after the suffixes. */
224 if (largest_digit < digit)
225 largest_digit = digit;
226 nd = n * base + digit;
227 overflow |= ULONG_MAX_over_base < n | nd < n;
233 cpp_error (pfile, "Invalid number in #if expression");
238 if (base <= largest_digit)
239 cpp_warning (pfile, "integer constant contains digits beyond the radix");
242 cpp_warning (pfile, "integer constant out of range");
244 /* If too big to be signed, consider it unsigned. */
245 if ((long) n < 0 && ! op.unsignedp)
248 cpp_warning (pfile, "integer constant is so large that it is unsigned");
262 static struct token tokentab2[] = {
276 /* Read one token. */
283 register int namelen;
284 register struct token *toktab;
285 enum cpp_token token;
287 U_CHAR *tok_start, *tok_end;
292 old_written = CPP_WRITTEN (pfile);
293 cpp_skip_hspace (pfile);
294 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
296 return parse_number (pfile,
297 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
305 token = cpp_get_token (pfile);
306 tok_start = pfile->token_buffer + old_written;
307 tok_end = CPP_PWRITTEN (pfile);
308 pfile->limit = tok_start;
311 case CPP_EOF: /* Should not happen ... */
316 if (CPP_BUFFER (pfile)->fname != NULL)
322 case CPP_HSPACE: case CPP_COMMENT:
325 return parse_number (pfile, tok_start, tok_end - tok_start);
327 cpp_error (pfile, "string constants not allowed in #if expressions");
331 /* This code for reading a character constant
332 handles multicharacter constants and wide characters.
333 It is mostly copied from c-lex.c. */
335 register int result = 0;
336 register num_chars = 0;
337 unsigned width = MAX_CHAR_TYPE_SIZE;
340 U_CHAR *ptr = tok_start;
341 #ifdef MULTIBYTE_CHARS
342 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
344 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
351 width = MAX_WCHAR_TYPE_SIZE;
352 #ifdef MULTIBYTE_CHARS
353 max_chars = MB_CUR_MAX;
359 max_chars = MAX_LONG_TYPE_SIZE / width;
362 while (ptr < tok_end && ((c = *ptr++) != '\''))
366 c = cpp_parse_escape (pfile, &ptr);
367 if (width < HOST_BITS_PER_INT
368 && (unsigned) c >= (1 << width))
370 "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 (pfile, "__CHAR_UNSIGNED__",
406 sizeof ("__CHAR_UNSIGNED__")-1, -1)
407 || ((result >> (num_bits - 1)) & 1) == 0)
409 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
412 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
416 #ifdef MULTIBYTE_CHARS
417 /* Set the initial shift state and convert the next sequence. */
419 /* In all locales L'\0' is zero and mbtowc will return zero,
422 || (num_chars == 1 && token_buffer[0] != '\0'))
425 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
426 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
429 cpp_warning (pfile,"Ignoring invalid multibyte character");
436 /* This is always a signed type. */
443 return parse_number (pfile, "0", 0);
446 /* See if it is a special token of length 2. */
447 if (tok_start + 2 == tok_end)
449 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
450 if (tok_start[0] == toktab->operator[0]
451 && tok_start[1] == toktab->operator[1])
453 if (toktab->token == ERROR)
455 char *buf = (char *) alloca (40);
456 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
457 cpp_error (pfile, buf);
459 op.op = toktab->token;
470 /* Parse a C escape sequence. STRING_PTR points to a variable
471 containing a pointer to the string to parse. That pointer
472 is updated past the characters we use. The value of the
473 escape sequence is returned.
475 A negative value means the sequence \ newline was seen,
476 which is supposed to be equivalent to nothing at all.
478 If \ is followed by a null character, we return a negative
479 value and leave the string pointer pointing at the null character.
481 If \ is followed by 000, we return 0 and leave the string pointer
482 after the zeros. A value of 0 does not mean end of string. */
489 register int c = *(*string_ptr)++;
498 if (CPP_PEDANTIC (pfile))
499 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
504 return TARGET_NEWLINE;
526 register int i = c - '0';
527 register int count = 0;
530 c = *(*string_ptr)++;
531 if (c >= '0' && c <= '7')
532 i = (i << 3) + c - '0';
539 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
541 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
543 "octal character constant does not fit in a byte");
549 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
552 c = *(*string_ptr)++;
553 if (c >= '0' && c <= '9')
555 else if (c >= 'a' && c <= 'f')
556 digit = c - 'a' + 10;
557 else if (c >= 'A' && c <= 'F')
558 digit = c - 'A' + 10;
564 overflow |= i ^ (i << 4 >> 4);
565 i = (i << 4) + digit;
569 cpp_error (pfile, "\\x used with no following hex digits");
570 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
572 i &= (1 << BITS_PER_UNIT) - 1;
574 "hex character constant does not fit in a byte");
587 if (CPP_PEDANTIC (pfile))
588 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
598 if (b >= HOST_BITS_PER_LONG)
600 if (! unsignedp && a != 0)
601 integer_overflow (pfile);
605 return (unsigned long) a << b;
610 integer_overflow (pfile);
622 if (b >= HOST_BITS_PER_LONG)
623 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
625 return (unsigned long) a >> b;
630 /* These priorities are all even, so we can handle associatively. */
631 #define PAREN_INNER_PRIO 0
633 #define COND_PRIO (COMMA_PRIO+2)
634 #define OROR_PRIO (COND_PRIO+2)
635 #define ANDAND_PRIO (OROR_PRIO+2)
636 #define OR_PRIO (ANDAND_PRIO+2)
637 #define XOR_PRIO (OR_PRIO+2)
638 #define AND_PRIO (XOR_PRIO+2)
639 #define EQUAL_PRIO (AND_PRIO+2)
640 #define LESS_PRIO (EQUAL_PRIO+2)
641 #define SHIFT_PRIO (LESS_PRIO+2)
642 #define PLUS_PRIO (SHIFT_PRIO+2)
643 #define MUL_PRIO (PLUS_PRIO+2)
644 #define UNARY_PRIO (MUL_PRIO+2)
645 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
647 #define COMPARE(OP) \
649 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP v2 : (v1 OP v2)
651 /* Parse and evaluate a C expression, reading from PFILE.
652 Returns the value of the expression. */
658 /* The implementation is an operator precedence parser,
659 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
661 The stack base is 'stack', and the current stack pointer is 'top'.
662 There is a stack element for each operator (only),
663 and the most recently pushed operator is 'top->op'.
664 An operand (value) is stored in the 'value' field of the stack
665 element of the operator that precedes it.
666 In that case the 'flags' field has the HAVE_VALUE flag set. */
668 #define INIT_STACK_SIZE 20
669 struct operation init_stack[INIT_STACK_SIZE];
670 struct operation *stack = init_stack;
671 struct operation *limit = stack + INIT_STACK_SIZE;
672 register struct operation *top = stack;
683 op = cpp_lex (pfile);
685 /* See if the token is an operand, in which case go to set_value.
686 If the token is an operator, figure out its left and right
687 priorities, and then goto maybe_reduce. */
692 top->value = 0, top->unsignedp = 0;
695 top->value = op.value;
696 top->unsignedp = op.unsignedp;
699 lprio = 0; goto maybe_reduce;
701 /* Is this correct if unary ? FIXME */
702 flags = RIGHT_OPERAND_REQUIRED;
703 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
705 flags = RIGHT_OPERAND_REQUIRED;
706 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
707 case '*': case '/': case '%':
708 lprio = MUL_PRIO; goto binop;
709 case '<': case '>': case LEQ: case GEQ:
710 lprio = LESS_PRIO; goto binop;
711 case EQUAL: case NOTEQUAL:
712 lprio = EQUAL_PRIO; goto binop;
714 lprio = SHIFT_PRIO; goto binop;
715 case '&': lprio = AND_PRIO; goto binop;
716 case '^': lprio = XOR_PRIO; goto binop;
717 case '|': lprio = OR_PRIO; goto binop;
718 case ANDAND: lprio = ANDAND_PRIO; goto binop;
719 case OROR: lprio = OROR_PRIO; goto binop;
721 lprio = COMMA_PRIO; goto binop;
723 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
726 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
729 lprio = COND_PRIO; rprio = COND_PRIO;
732 lprio = COND_PRIO + 1; rprio = COND_PRIO;
735 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
739 cpp_error (pfile, "invalid character in #if");
744 /* Push a value onto the stack. */
745 if (top->flags & HAVE_VALUE)
747 cpp_error (pfile, "syntax error in #if");
750 top->flags |= HAVE_VALUE;
754 /* Push an operator, and check if we can reduce now. */
755 while (top->rprio > lprio)
757 long v1 = top[-1].value, v2 = top[0].value;
758 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
760 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
761 && ! (top[0].flags & HAVE_VALUE))
763 cpp_error (pfile, "syntax error - missing left operand");
766 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
767 && ! (top[1].flags & HAVE_VALUE))
769 cpp_error (pfile, "syntax error - missing right operand");
772 /* top[0].value = (top[1].op)(v1, v2);*/
776 if (!(top->flags & HAVE_VALUE))
779 top->unsignedp = unsigned2;
780 top->flags |= HAVE_VALUE;
784 top->value = v1 + v2;
785 top->unsignedp = unsigned1 || unsigned2;
787 && ! possible_sum_sign (v1, v2, top->value))
788 integer_overflow (pfile);
792 if (!(top->flags & HAVE_VALUE))
795 if ((top->value & v2) < 0 && ! unsigned2)
796 integer_overflow (pfile);
797 top->unsignedp = unsigned2;
798 top->flags |= HAVE_VALUE;
802 top->value = v1 - v2;
803 top->unsignedp = unsigned1 || unsigned2;
805 && ! possible_sum_sign (top->value, v2, v1))
806 integer_overflow (pfile);
810 top->unsignedp = unsigned1 || unsigned2;
812 top->value = (unsigned long) v1 * v2;
815 top->value = v1 * v2;
817 && (top->value / v1 != v2
818 || (top->value & v1 & v2) < 0))
819 integer_overflow (pfile);
825 cpp_error (pfile, "division by zero in #if");
828 top->unsignedp = unsigned1 || unsigned2;
830 top->value = (unsigned long) v1 / v2;
833 top->value = v1 / v2;
834 if ((top->value & v1 & v2) < 0)
835 integer_overflow (pfile);
841 cpp_error (pfile, "division by zero in #if");
844 top->unsignedp = unsigned1 || unsigned2;
846 top->value = (unsigned long) v1 % v2;
848 top->value = v1 % v2;
851 if (top->flags & HAVE_VALUE)
853 cpp_error (pfile, "syntax error");
858 top->flags |= HAVE_VALUE;
861 if (top->flags & HAVE_VALUE)
863 cpp_error (pfile, "syntax error");
867 top->unsignedp = unsigned2;
868 top->flags |= HAVE_VALUE;
870 case '<': COMPARE(<); break;
871 case '>': COMPARE(>); break;
872 case LEQ: COMPARE(<=); break;
873 case GEQ: COMPARE(>=); break;
875 top->value = (v1 == v2);
879 top->value = (v1 != v2);
883 top->unsignedp = unsigned1;
884 if (v2 < 0 && ! unsigned2)
885 top->value = right_shift (pfile, v1, unsigned1, -v2);
887 top->value = left_shift (pfile, v1, unsigned1, v2);
890 top->unsignedp = unsigned1;
891 if (v2 < 0 && ! unsigned2)
892 top->value = left_shift (pfile, v1, unsigned1, -v2);
894 top->value = right_shift (pfile, v1, unsigned1, v2);
896 #define LOGICAL(OP) \
897 top->value = v1 OP v2;\
898 top->unsignedp = unsigned1 || unsigned2;
899 case '&': LOGICAL(&); break;
900 case '^': LOGICAL(^); break;
901 case '|': LOGICAL(|); break;
903 top->value = v1 && v2; top->unsignedp = 0; break;
905 top->value = v1 || v2; top->unsignedp = 0; break;
907 if (CPP_PEDANTIC (pfile))
908 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
910 top->unsignedp = unsigned2;
913 cpp_error (pfile, "syntax error in #if");
916 if (top[0].op != '?')
919 "syntax error ':' without preceding '?'");
922 else if (! (top[1].flags & HAVE_VALUE)
923 || !(top[-1].flags & HAVE_VALUE)
924 || !(top[0].flags & HAVE_VALUE))
926 cpp_error (pfile, "bad syntax for ?: operator");
932 top->value = top->value ? v1 : v2;
933 top->unsignedp = unsigned1 || unsigned2;
937 if ((top[1].flags & HAVE_VALUE)
938 || ! (top[0].flags & HAVE_VALUE)
940 || (top[-1].flags & HAVE_VALUE))
942 cpp_error (pfile, "mismatched parentheses in #if");
949 top->unsignedp = unsigned1;
950 top->flags |= HAVE_VALUE;
955 top[1].op >= ' ' && top[1].op <= '~'
956 ? "unimplemented operator '%c'\n"
957 : "unimplemented operator '\\%03o'\n",
964 cpp_error (pfile, "internal error in #if expression");
965 if (stack != init_stack)
971 /* Check for and handle stack overflow. */
974 struct operation *new_stack;
975 int old_size = (char*)limit - (char*)stack;
976 int new_size = 2 * old_size;
977 if (stack != init_stack)
978 new_stack = (struct operation*) xrealloc (stack, new_size);
981 new_stack = (struct operation*) xmalloc (new_size);
982 bcopy ((char *) stack, (char *) new_stack, old_size);
985 top = (struct operation*)((char*) new_stack + old_size);
986 limit = (struct operation*)((char*) new_stack + new_size);
994 if (stack != init_stack)
996 skip_rest_of_line (pfile);