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 */
32 //extern char *xmalloc PARAMS ((unsigned));
33 //extern char *xrealloc PARAMS ((char *, unsigned));
36 #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 //extern char *xmalloc ();
71 #ifndef CHAR_TYPE_SIZE
72 #define CHAR_TYPE_SIZE BITS_PER_UNIT
76 #define INT_TYPE_SIZE BITS_PER_WORD
79 #ifndef LONG_TYPE_SIZE
80 #define LONG_TYPE_SIZE BITS_PER_WORD
83 #ifndef WCHAR_TYPE_SIZE
84 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
87 #ifndef MAX_CHAR_TYPE_SIZE
88 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
91 #ifndef MAX_INT_TYPE_SIZE
92 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
95 #ifndef MAX_LONG_TYPE_SIZE
96 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
99 #ifndef MAX_WCHAR_TYPE_SIZE
100 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
103 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
104 number with SUM's sign, where A, B, and SUM are all C integers. */
105 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
107 static void integer_overflow ();
108 static long left_shift ();
109 static long right_shift ();
124 #define LEFT_OPERAND_REQUIRED 1
125 #define RIGHT_OPERAND_REQUIRED 2
127 /*#define UNSIGNEDP 8*/
129 #ifndef HOST_BITS_PER_WIDE_INT
131 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
132 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
133 #define HOST_WIDE_INT long
135 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
136 #define HOST_WIDE_INT int
143 char rprio; /* Priority of op (relative to it right operand). */
145 char unsignedp; /* true if value should be treated as unsigned */
146 HOST_WIDE_INT value; /* The value logically "right" of op. */
149 /* Take care of parsing a number (anything that starts with a digit).
150 LEN is the number of characters in it. */
152 /* maybe needs to actually deal with floating point numbers */
161 register char *p = start;
163 register unsigned long n = 0, nd, ULONG_MAX_over_base;
164 register int base = 10;
165 register int len = olen;
166 register int overflow = 0;
167 register int digit, largest_digit = 0;
172 for (c = 0; c < len; c++)
174 /* It's a float since it contains a point. */
176 "floating point numbers not allowed in #if expressions");
181 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
189 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
190 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
192 for (; len > 0; len--) {
195 if (c >= '0' && c <= '9')
197 else if (base == 16 && c >= 'a' && c <= 'f')
198 digit = c - 'a' + 10;
199 else if (base == 16 && c >= 'A' && c <= 'F')
200 digit = c - 'A' + 10;
202 /* `l' means long, and `u' means unsigned. */
204 if (c == 'l' || c == 'L')
207 cpp_error (pfile, "two `l's in integer constant");
210 else if (c == 'u' || c == 'U')
213 cpp_error (pfile, "two `u's in integer constant");
223 /* Don't look for any more digits after the suffixes. */
226 if (largest_digit < digit)
227 largest_digit = digit;
228 nd = n * base + digit;
229 overflow |= ((ULONG_MAX_over_base < n) | (nd < n)) ;
235 cpp_error (pfile, "Invalid number in #if expression");
240 if (base <= largest_digit)
241 cpp_warning (pfile, "integer constant contains digits beyond the radix");
244 cpp_warning (pfile, "integer constant out of range");
246 /* If too big to be signed, consider it unsigned. */
247 if ((long) n < 0 && ! op.unsignedp)
250 cpp_warning (pfile, "integer constant is so large that it is unsigned");
264 static struct token tokentab2[] = {
278 /* Read one token. */
285 /* register int namelen; */
286 register struct token *toktab;
287 enum cpp_token token;
289 U_CHAR *tok_start, *tok_end;
294 old_written = CPP_WRITTEN (pfile);
295 cpp_skip_hspace (pfile);
296 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
298 return parse_number (pfile,
299 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
307 token = cpp_get_token (pfile);
308 tok_start = pfile->token_buffer + old_written;
309 tok_end = CPP_PWRITTEN (pfile);
310 pfile->limit = tok_start;
313 case CPP_EOF: /* Should not happen ... */
318 if (CPP_BUFFER (pfile)->fname != NULL)
324 case CPP_HSPACE: case CPP_COMMENT:
327 return parse_number (pfile, tok_start, tok_end - tok_start);
329 cpp_error (pfile, "string constants not allowed in #if expressions");
333 /* This code for reading a character constant
334 handles multicharacter constants and wide characters.
335 It is mostly copied from c-lex.c. */
337 register int result = 0;
338 register int num_chars = 0;
339 unsigned width = MAX_CHAR_TYPE_SIZE;
342 U_CHAR *ptr = tok_start;
343 #ifdef MULTIBYTE_CHARS
344 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
346 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
353 width = MAX_WCHAR_TYPE_SIZE;
354 #ifdef MULTIBYTE_CHARS
355 max_chars = MB_CUR_MAX;
361 max_chars = MAX_LONG_TYPE_SIZE / width;
364 while (ptr < tok_end && ((c = *ptr++) != '\''))
368 c = cpp_parse_escape (pfile, &ptr);
369 if (width < HOST_BITS_PER_INT
370 && (unsigned) c >= (1U << width))
372 "escape sequence out of range for character");
377 /* Merge character into result; ignore excess chars. */
378 if (num_chars < max_chars + 1)
380 if (width < HOST_BITS_PER_INT)
381 result = (result << width) | (c & ((1 << width) - 1));
384 token_buffer[num_chars - 1] = c;
388 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 (pfile, "__CHAR_UNSIGNED__",
408 sizeof ("__CHAR_UNSIGNED__")-1, -1)
409 || ((result >> (num_bits - 1)) & 1) == 0)
411 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
414 = result | ~((unsigned long) ~0 >> (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,
424 || (num_chars == 1 && token_buffer[0] != '\0'))
427 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
428 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
431 cpp_warning (pfile,"Ignoring invalid multibyte character");
438 /* This is always a signed type. */
445 return parse_number (pfile, "0", 0);
448 /* See if it is a special token of length 2. */
449 if (tok_start + 2 == tok_end)
451 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
452 if (tok_start[0] == toktab->operator[0]
453 && tok_start[1] == toktab->operator[1])
455 if (toktab->token == ERROR)
457 char *buf = (char *) alloca (40);
458 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
459 cpp_error (pfile, buf);
461 op.op = toktab->token;
472 /* Parse a C escape sequence. STRING_PTR points to a variable
473 containing a pointer to the string to parse. That pointer
474 is updated past the characters we use. The value of the
475 escape sequence is returned.
477 A negative value means the sequence \ newline was seen,
478 which is supposed to be equivalent to nothing at all.
480 If \ is followed by a null character, we return a negative
481 value and leave the string pointer pointing at the null character.
483 If \ is followed by 000, we return 0 and leave the string pointer
484 after the zeros. A value of 0 does not mean end of string. */
491 register int c = *(*string_ptr)++;
500 if (CPP_PEDANTIC (pfile))
501 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
506 return TARGET_NEWLINE;
528 register int i = c - '0';
529 register int count = 0;
532 c = *(*string_ptr)++;
533 if (c >= '0' && c <= '7')
534 i = (i << 3) + c - '0';
541 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
543 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
545 "octal character constant does not fit in a byte");
551 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
554 c = *(*string_ptr)++;
555 if (c >= '0' && c <= '9')
557 else if (c >= 'a' && c <= 'f')
558 digit = c - 'a' + 10;
559 else if (c >= 'A' && c <= 'F')
560 digit = c - 'A' + 10;
566 overflow |= i ^ (i << 4 >> 4);
567 i = (i << 4) + digit;
571 cpp_error (pfile, "\\x used with no following hex digits");
572 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
574 i &= (1 << BITS_PER_UNIT) - 1;
576 "hex character constant does not fit in a byte");
589 if (CPP_PEDANTIC (pfile))
590 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
600 if (b >= HOST_BITS_PER_LONG)
602 if (! unsignedp && a != 0)
603 integer_overflow (pfile);
607 return (unsigned long) a << b;
612 integer_overflow (pfile);
624 if (b >= HOST_BITS_PER_LONG)
625 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
627 return (unsigned long) a >> b;
632 /* These priorities are all even, so we can handle associatively. */
633 #define PAREN_INNER_PRIO 0
635 #define COND_PRIO (COMMA_PRIO+2)
636 #define OROR_PRIO (COND_PRIO+2)
637 #define ANDAND_PRIO (OROR_PRIO+2)
638 #define OR_PRIO (ANDAND_PRIO+2)
639 #define XOR_PRIO (OR_PRIO+2)
640 #define AND_PRIO (XOR_PRIO+2)
641 #define EQUAL_PRIO (AND_PRIO+2)
642 #define LESS_PRIO (EQUAL_PRIO+2)
643 #define SHIFT_PRIO (LESS_PRIO+2)
644 #define PLUS_PRIO (SHIFT_PRIO+2)
645 #define MUL_PRIO (PLUS_PRIO+2)
646 #define UNARY_PRIO (MUL_PRIO+2)
647 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
649 #define COMPARE(OP) \
651 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
653 /* Parse and evaluate a C expression, reading from PFILE.
654 Returns the value of the expression. */
660 /* The implementation is an operator precedence parser,
661 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
663 The stack base is 'stack', and the current stack pointer is 'top'.
664 There is a stack element for each operator (only),
665 and the most recently pushed operator is 'top->op'.
666 An operand (value) is stored in the 'value' field of the stack
667 element of the operator that precedes it.
668 In that case the 'flags' field has the HAVE_VALUE flag set. */
670 #define INIT_STACK_SIZE 20
671 struct operation init_stack[INIT_STACK_SIZE];
672 struct operation *stack = init_stack;
673 struct operation *limit = stack + INIT_STACK_SIZE;
674 register struct operation *top = stack;
675 int lprio, rprio = 0;
685 op = cpp_lex (pfile);
687 /* See if the token is an operand, in which case go to set_value.
688 If the token is an operator, figure out its left and right
689 priorities, and then goto maybe_reduce. */
694 top->value = 0, top->unsignedp = 0;
697 top->value = op.value;
698 top->unsignedp = op.unsignedp;
701 lprio = 0; goto maybe_reduce;
703 /* Is this correct if unary ? FIXME */
704 flags = RIGHT_OPERAND_REQUIRED;
705 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
707 flags = RIGHT_OPERAND_REQUIRED;
708 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
709 case '*': case '/': case '%':
710 lprio = MUL_PRIO; goto binop;
711 case '<': case '>': case LEQ: case GEQ:
712 lprio = LESS_PRIO; goto binop;
713 case EQUAL: case NOTEQUAL:
714 lprio = EQUAL_PRIO; goto binop;
716 lprio = SHIFT_PRIO; goto binop;
717 case '&': lprio = AND_PRIO; goto binop;
718 case '^': lprio = XOR_PRIO; goto binop;
719 case '|': lprio = OR_PRIO; goto binop;
720 case ANDAND: lprio = ANDAND_PRIO; goto binop;
721 case OROR: lprio = OROR_PRIO; goto binop;
723 lprio = COMMA_PRIO; goto binop;
725 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
728 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
731 lprio = COND_PRIO; rprio = COND_PRIO;
734 lprio = COND_PRIO + 1; rprio = COND_PRIO;
737 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
741 cpp_error (pfile, "invalid character in #if");
746 /* Push a value onto the stack. */
747 if (top->flags & HAVE_VALUE)
749 cpp_error (pfile, "syntax error in #if");
752 top->flags |= HAVE_VALUE;
756 /* Push an operator, and check if we can reduce now. */
757 while (top->rprio > lprio)
759 long v1 = top[-1].value, v2 = top[0].value;
760 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
762 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
763 && ! (top[0].flags & HAVE_VALUE))
765 cpp_error (pfile, "syntax error - missing left operand");
768 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
769 && ! (top[1].flags & HAVE_VALUE))
771 cpp_error (pfile, "syntax error - missing right operand");
774 /* top[0].value = (top[1].op)(v1, v2);*/
778 if (!(top->flags & HAVE_VALUE))
781 top->unsignedp = unsigned2;
782 top->flags |= HAVE_VALUE;
786 top->value = v1 + v2;
787 top->unsignedp = unsigned1 || unsigned2;
789 && ! possible_sum_sign (v1, v2, top->value))
790 integer_overflow (pfile);
794 if (!(top->flags & HAVE_VALUE))
797 if ((top->value & v2) < 0 && ! unsigned2)
798 integer_overflow (pfile);
799 top->unsignedp = unsigned2;
800 top->flags |= HAVE_VALUE;
804 top->value = v1 - v2;
805 top->unsignedp = unsigned1 || unsigned2;
807 && ! possible_sum_sign (top->value, v2, v1))
808 integer_overflow (pfile);
812 top->unsignedp = unsigned1 || unsigned2;
814 top->value = (unsigned long) v1 * v2;
817 top->value = v1 * v2;
819 && (top->value / v1 != v2
820 || (top->value & v1 & v2) < 0))
821 integer_overflow (pfile);
827 cpp_error (pfile, "division by zero in #if");
830 top->unsignedp = unsigned1 || unsigned2;
832 top->value = (unsigned long) v1 / v2;
835 top->value = v1 / v2;
836 if ((top->value & v1 & v2) < 0)
837 integer_overflow (pfile);
843 cpp_error (pfile, "division by zero in #if");
846 top->unsignedp = unsigned1 || unsigned2;
848 top->value = (unsigned long) v1 % v2;
850 top->value = v1 % v2;
853 if (top->flags & HAVE_VALUE)
855 cpp_error (pfile, "syntax error");
860 top->flags |= HAVE_VALUE;
863 if (top->flags & HAVE_VALUE)
865 cpp_error (pfile, "syntax error");
869 top->unsignedp = unsigned2;
870 top->flags |= HAVE_VALUE;
872 case '<': COMPARE(<); break;
873 case '>': COMPARE(>); break;
874 case LEQ: COMPARE(<=); break;
875 case GEQ: COMPARE(>=); break;
877 top->value = (v1 == v2);
881 top->value = (v1 != v2);
885 top->unsignedp = unsigned1;
886 if (v2 < 0 && ! unsigned2)
887 top->value = right_shift (pfile, v1, unsigned1, -v2);
889 top->value = left_shift (pfile, v1, unsigned1, v2);
892 top->unsignedp = unsigned1;
893 if (v2 < 0 && ! unsigned2)
894 top->value = left_shift (pfile, v1, unsigned1, -v2);
896 top->value = right_shift (pfile, v1, unsigned1, v2);
898 #define LOGICAL(OP) \
899 top->value = v1 OP v2;\
900 top->unsignedp = unsigned1 || unsigned2;
901 case '&': LOGICAL(&); break;
902 case '^': LOGICAL(^); break;
903 case '|': LOGICAL(|); break;
905 top->value = v1 && v2; top->unsignedp = 0; break;
907 top->value = v1 || v2; top->unsignedp = 0; break;
909 if (CPP_PEDANTIC (pfile))
910 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
912 top->unsignedp = unsigned2;
915 cpp_error (pfile, "syntax error in #if");
918 if (top[0].op != '?')
921 "syntax error ':' without preceding '?'");
924 else if (! (top[1].flags & HAVE_VALUE)
925 || !(top[-1].flags & HAVE_VALUE)
926 || !(top[0].flags & HAVE_VALUE))
928 cpp_error (pfile, "bad syntax for ?: operator");
934 top->value = top->value ? v1 : v2;
935 top->unsignedp = unsigned1 || unsigned2;
939 if ((top[1].flags & HAVE_VALUE)
940 || ! (top[0].flags & HAVE_VALUE)
942 || (top[-1].flags & HAVE_VALUE))
944 cpp_error (pfile, "mismatched parentheses in #if");
951 top->unsignedp = unsigned1;
952 top->flags |= HAVE_VALUE;
957 top[1].op >= ' ' && top[1].op <= '~'
958 ? "unimplemented operator '%c'\n"
959 : "unimplemented operator '\\%03o'\n",
966 cpp_error (pfile, "internal error in #if expression");
967 if (stack != init_stack)
973 /* Check for and handle stack overflow. */
976 struct operation *new_stack;
977 int old_size = (char*)limit - (char*)stack;
978 int new_size = 2 * old_size;
979 if (stack != init_stack)
980 new_stack = (struct operation*) Safe_realloc (stack, new_size);
983 new_stack = (struct operation*) Safe_malloc (new_size);
984 bcopy ((char *) stack, (char *) new_stack, old_size);
987 top = (struct operation*)((char*) new_stack + old_size);
988 limit = (struct operation*)((char*) new_stack + new_size);
996 if (stack != init_stack)
998 skip_rest_of_line (pfile);