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;
362 if (ptr >= CPP_PWRITTEN (pfile) || (c = *ptr++) == '\'')
367 c = cpp_parse_escape (pfile, &ptr);
368 if (width < HOST_BITS_PER_INT
369 && (unsigned) c >= (1 << width))
371 "escape sequence out of range for character");
376 /* Merge character into result; ignore excess chars. */
377 if (num_chars < max_chars + 1)
379 if (width < HOST_BITS_PER_INT)
380 result = (result << width) | (c & ((1 << width) - 1));
383 token_buffer[num_chars - 1] = c;
387 token_buffer[num_chars] = 0;
390 cpp_error (pfile, "malformatted character constant");
391 else if (num_chars == 0)
392 cpp_error (pfile, "empty character constant");
393 else if (num_chars > max_chars)
395 num_chars = max_chars;
396 cpp_error (pfile, "character constant too long");
398 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
399 cpp_warning (pfile, "multi-character character constant");
401 /* If char type is signed, sign-extend the constant. */
404 int num_bits = num_chars * width;
406 if (cpp_lookup (pfile, "__CHAR_UNSIGNED__",
407 sizeof ("__CHAR_UNSIGNED__")-1, -1)
408 || ((result >> (num_bits - 1)) & 1) == 0)
410 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
413 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
417 #ifdef MULTIBYTE_CHARS
418 /* Set the initial shift state and convert the next sequence. */
420 /* In all locales L'\0' is zero and mbtowc will return zero,
423 || (num_chars == 1 && token_buffer[0] != '\0'))
426 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
427 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
430 cpp_warning (pfile,"Ignoring invalid multibyte character");
437 /* This is always a signed type. */
444 return parse_number (pfile, "0", 0);
447 /* See if it is a special token of length 2. */
448 if (tok_start + 2 == tok_end)
450 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
451 if (tok_start[0] == toktab->operator[0]
452 && tok_start[1] == toktab->operator[1])
454 if (toktab->token == ERROR)
456 char *buf = (char *) alloca (40);
457 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
458 cpp_error (pfile, buf);
460 op.op = toktab->token;
471 /* Parse a C escape sequence. STRING_PTR points to a variable
472 containing a pointer to the string to parse. That pointer
473 is updated past the characters we use. The value of the
474 escape sequence is returned.
476 A negative value means the sequence \ newline was seen,
477 which is supposed to be equivalent to nothing at all.
479 If \ is followed by a null character, we return a negative
480 value and leave the string pointer pointing at the null character.
482 If \ is followed by 000, we return 0 and leave the string pointer
483 after the zeros. A value of 0 does not mean end of string. */
486 cpp_parse_escape (pfile, string_ptr)
490 register int c = *(*string_ptr)++;
499 if (CPP_PEDANTIC (pfile))
500 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
505 return TARGET_NEWLINE;
527 register int i = c - '0';
528 register int count = 0;
531 c = *(*string_ptr)++;
532 if (c >= '0' && c <= '7')
533 i = (i << 3) + c - '0';
540 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
542 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
544 "octal character constant does not fit in a byte");
550 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
553 c = *(*string_ptr)++;
554 if (c >= '0' && c <= '9')
556 else if (c >= 'a' && c <= 'f')
557 digit = c - 'a' + 10;
558 else if (c >= 'A' && c <= 'F')
559 digit = c - 'A' + 10;
565 overflow |= i ^ (i << 4 >> 4);
566 i = (i << 4) + digit;
570 cpp_error (pfile, "\\x used with no following hex digits");
571 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
573 i &= (1 << BITS_PER_UNIT) - 1;
575 "hex character constant does not fit in a byte");
585 integer_overflow (pfile)
588 if (CPP_PEDANTIC (pfile))
589 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
593 left_shift (pfile, a, unsignedp, b)
599 if (b >= HOST_BITS_PER_LONG)
601 if (! unsignedp && a != 0)
602 integer_overflow (pfile);
606 return (unsigned long) a << b;
611 integer_overflow (pfile);
617 right_shift (pfile, a, unsignedp, b)
623 if (b >= HOST_BITS_PER_LONG)
624 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
626 return (unsigned long) a >> b;
631 /* These priorities are all even, so we can handle associatively. */
632 #define PAREN_INNER_PRIO 0
634 #define COND_PRIO (COMMA_PRIO+2)
635 #define OROR_PRIO (COND_PRIO+2)
636 #define ANDAND_PRIO (OROR_PRIO+2)
637 #define OR_PRIO (ANDAND_PRIO+2)
638 #define XOR_PRIO (OR_PRIO+2)
639 #define AND_PRIO (XOR_PRIO+2)
640 #define EQUAL_PRIO (AND_PRIO+2)
641 #define LESS_PRIO (EQUAL_PRIO+2)
642 #define SHIFT_PRIO (LESS_PRIO+2)
643 #define PLUS_PRIO (SHIFT_PRIO+2)
644 #define MUL_PRIO (PLUS_PRIO+2)
645 #define UNARY_PRIO (MUL_PRIO+2)
646 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
648 #define COMPARE(OP) \
650 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP v2 : (v1 OP v2)
652 /* Parse and evaluate a C expression, reading from PFILE.
653 Returns the value of the expression. */
656 cpp_parse_expr (pfile)
659 /* The implementation is an operator precedence parser,
660 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
662 The stack base is 'stack', and the current stack pointer is 'top'.
663 There is a stack element for each operator (only),
664 and the most recently pushed operator is 'top->op'.
665 An operand (value) is stored in the 'value' field of the stack
666 element of the operator that precedes it.
667 In that case the 'flags' field has the HAVE_VALUE flag set. */
669 #define INIT_STACK_SIZE 20
670 struct operation init_stack[INIT_STACK_SIZE];
671 struct operation *stack = init_stack;
672 struct operation *limit = stack + INIT_STACK_SIZE;
673 register struct operation *top = stack;
684 op = cpp_lex (pfile);
686 /* See if the token is an operand, in which case go to set_value.
687 If the token is an operator, figure out its left and right
688 priorities, and then goto maybe_reduce. */
693 top->value = 0, top->unsignedp = 0;
696 top->value = op.value;
697 top->unsignedp = op.unsignedp;
700 lprio = 0; goto maybe_reduce;
702 /* Is this correct if unary ? FIXME */
703 flags = RIGHT_OPERAND_REQUIRED;
704 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
706 flags = RIGHT_OPERAND_REQUIRED;
707 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
708 case '*': case '/': case '%':
709 lprio = MUL_PRIO; goto binop;
710 case '<': case '>': case LEQ: case GEQ:
711 lprio = LESS_PRIO; goto binop;
712 case EQUAL: case NOTEQUAL:
713 lprio = EQUAL_PRIO; goto binop;
715 lprio = SHIFT_PRIO; goto binop;
716 case '&': lprio = AND_PRIO; goto binop;
717 case '^': lprio = XOR_PRIO; goto binop;
718 case '|': lprio = OR_PRIO; goto binop;
719 case ANDAND: lprio = ANDAND_PRIO; goto binop;
720 case OROR: lprio = OROR_PRIO; goto binop;
722 lprio = COMMA_PRIO; goto binop;
724 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
727 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
730 lprio = COND_PRIO; rprio = COND_PRIO;
733 lprio = COND_PRIO + 1; rprio = COND_PRIO;
736 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
740 cpp_error (pfile, "invalid character in #if");
745 /* Push a value onto the stack. */
746 if (top->flags & HAVE_VALUE)
748 cpp_error (pfile, "syntax error in #if");
751 top->flags |= HAVE_VALUE;
755 /* Push an operator, and check if we can reduce now. */
756 while (top->rprio > lprio)
758 long v1 = top[-1].value, v2 = top[0].value;
759 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
761 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
762 && ! (top[0].flags & HAVE_VALUE))
764 cpp_error (pfile, "syntax error - missing left operand");
767 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
768 && ! (top[1].flags & HAVE_VALUE))
770 cpp_error (pfile, "syntax error - missing right operand");
773 /* top[0].value = (top[1].op)(v1, v2);*/
777 if (!(top->flags & HAVE_VALUE))
780 top->unsignedp = unsigned2;
781 top->flags |= HAVE_VALUE;
785 top->value = v1 + v2;
786 top->unsignedp = unsigned1 || unsigned2;
788 && ! possible_sum_sign (v1, v2, top->value))
789 integer_overflow (pfile);
793 if (!(top->flags & HAVE_VALUE))
796 if ((top->value & v2) < 0 && ! unsigned2)
797 integer_overflow (pfile);
798 top->unsignedp = unsigned2;
799 top->flags |= HAVE_VALUE;
803 top->value = v1 - v2;
804 top->unsignedp = unsigned1 || unsigned2;
806 && ! possible_sum_sign (top->value, v2, v1))
807 integer_overflow (pfile);
811 top->unsignedp = unsigned1 || unsigned2;
813 top->value = (unsigned long) v1 * v2;
816 top->value = v1 * v2;
818 && (top->value / v1 != v2
819 || (top->value & v1 & v2) < 0))
820 integer_overflow (pfile);
826 cpp_error (pfile, "division by zero in #if");
829 top->unsignedp = unsigned1 || unsigned2;
831 top->value = (unsigned long) v1 / v2;
834 top->value = v1 / v2;
835 if ((top->value & v1 & v2) < 0)
836 integer_overflow (pfile);
842 cpp_error (pfile, "division by zero in #if");
845 top->unsignedp = unsigned1 || unsigned2;
847 top->value = (unsigned long) v1 % v2;
849 top->value = v1 % v2;
852 if (top->flags & HAVE_VALUE)
854 cpp_error (pfile, "syntax error");
859 top->flags |= HAVE_VALUE;
862 if (top->flags & HAVE_VALUE)
864 cpp_error (pfile, "syntax error");
868 top->unsignedp = unsigned2;
869 top->flags |= HAVE_VALUE;
871 case '<': COMPARE(<); break;
872 case '>': COMPARE(>); break;
873 case LEQ: COMPARE(<=); break;
874 case GEQ: COMPARE(>=); break;
876 top->value = (v1 == v2);
880 top->value = (v1 != v2);
884 top->unsignedp = unsigned1;
885 if (v2 < 0 && ! unsigned2)
886 top->value = right_shift (pfile, v1, unsigned1, -v2);
888 top->value = left_shift (pfile, v1, unsigned1, v2);
891 top->unsignedp = unsigned1;
892 if (v2 < 0 && ! unsigned2)
893 top->value = left_shift (pfile, v1, unsigned1, -v2);
895 top->value = right_shift (pfile, v1, unsigned1, v2);
897 #define LOGICAL(OP) \
898 top->value = v1 OP v2;\
899 top->unsignedp = unsigned1 || unsigned2;
900 case '&': LOGICAL(&); break;
901 case '^': LOGICAL(^); break;
902 case '|': LOGICAL(|); break;
904 top->value = v1 && v2; top->unsignedp = 0; break;
906 top->value = v1 || v2; top->unsignedp = 0; break;
908 if (CPP_PEDANTIC (pfile))
909 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
911 top->unsignedp = unsigned2;
914 cpp_error (pfile, "syntax error in #if");
917 if (top[0].op != '?')
920 "syntax error ':' without preceding '?'");
923 else if (! (top[1].flags & HAVE_VALUE)
924 || !(top[-1].flags & HAVE_VALUE)
925 || !(top[0].flags & HAVE_VALUE))
927 cpp_error (pfile, "bad syntax for ?: operator");
933 top->value = top->value ? v1 : v2;
934 top->unsignedp = unsigned1 || unsigned2;
938 if ((top[1].flags & HAVE_VALUE)
939 || ! (top[0].flags & HAVE_VALUE)
941 || (top[-1].flags & HAVE_VALUE))
943 cpp_error (pfile, "mismatched parentheses in #if");
950 top->unsignedp = unsigned1;
951 top->flags |= HAVE_VALUE;
956 top[1].op >= ' ' && top[1].op <= '~'
957 ? "unimplemented operator '%c'\n"
958 : "unimplemented operator '\\%03o'\n",
965 cpp_error (pfile, "internal error in #if expression");
966 if (stack != init_stack)
972 /* Check for and handle stack overflow. */
975 struct operation *new_stack;
976 int old_size = (char*)limit - (char*)stack;
977 int new_size = 2 * old_size;
978 if (stack != init_stack)
979 new_stack = (struct operation*) xrealloc (stack, new_size);
982 new_stack = (struct operation*) xmalloc (new_size);
983 bcopy ((char *) stack, (char *) new_stack, old_size);
986 top = (struct operation*)((char*) new_stack + old_size);
987 limit = (struct operation*)((char*) new_stack + new_size);
995 if (stack != init_stack)
997 skip_rest_of_line (pfile);