1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997 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 ((void *, unsigned));
33 #ifdef MULTIBYTE_CHARS
44 /* This is used for communicating lists of keywords with cccp.c. */
52 /* Define a generic NULL if one hasn't already been defined. */
59 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
60 #define GENERIC_PTR void *
62 #define GENERIC_PTR char *
67 #define NULL_PTR ((GENERIC_PTR) 0)
70 extern char *xmalloc ();
72 #ifndef CHAR_TYPE_SIZE
73 #define CHAR_TYPE_SIZE BITS_PER_UNIT
77 #define INT_TYPE_SIZE BITS_PER_WORD
80 #ifndef LONG_TYPE_SIZE
81 #define LONG_TYPE_SIZE BITS_PER_WORD
84 #ifndef WCHAR_TYPE_SIZE
85 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
88 #ifndef MAX_CHAR_TYPE_SIZE
89 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
92 #ifndef MAX_INT_TYPE_SIZE
93 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
96 #ifndef MAX_LONG_TYPE_SIZE
97 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
100 #ifndef MAX_WCHAR_TYPE_SIZE
101 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
104 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
105 number with SUM's sign, where A, B, and SUM are all C integers. */
106 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
108 static void integer_overflow ();
109 static long left_shift ();
110 static long right_shift ();
125 #define LEFT_OPERAND_REQUIRED 1
126 #define RIGHT_OPERAND_REQUIRED 2
128 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
129 following operand should be short-circuited instead of evaluated. */
130 #define SKIP_OPERAND 8
131 /*#define UNSIGNEDP 16*/
133 /* Find the largest host integer type and set its size and type.
134 Watch out: on some crazy hosts `long' is shorter than `int'. */
136 #ifndef HOST_WIDE_INT
138 # include <inttypes.h>
139 # define HOST_WIDE_INT intmax_t
141 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
142 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
143 # define HOST_WIDE_INT int
145 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
146 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
147 # define HOST_WIDE_INT long
149 # define HOST_WIDE_INT long long
159 #ifndef HOST_BITS_PER_WIDE_INT
160 #define HOST_BITS_PER_WIDE_INT (CHAR_BIT * sizeof (HOST_WIDE_INT))
165 char rprio; /* Priority of op (relative to it right operand). */
167 char unsignedp; /* true if value should be treated as unsigned */
168 HOST_WIDE_INT value; /* The value logically "right" of op. */
171 /* Take care of parsing a number (anything that starts with a digit).
172 LEN is the number of characters in it. */
174 /* maybe needs to actually deal with floating point numbers */
177 parse_number (pfile, start, olen)
183 register char *p = start;
185 register unsigned long n = 0, nd, ULONG_MAX_over_base;
186 register int base = 10;
187 register int len = olen;
188 register int overflow = 0;
189 register int digit, largest_digit = 0;
194 for (c = 0; c < len; c++)
196 /* It's a float since it contains a point. */
198 "floating point numbers not allowed in #if expressions");
203 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
211 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
212 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
214 for (; len > 0; len--) {
217 if (c >= '0' && c <= '9')
219 else if (base == 16 && c >= 'a' && c <= 'f')
220 digit = c - 'a' + 10;
221 else if (base == 16 && c >= 'A' && c <= 'F')
222 digit = c - 'A' + 10;
224 /* `l' means long, and `u' means unsigned. */
226 if (c == 'l' || c == 'L')
229 cpp_error (pfile, "two `l's in integer constant");
232 else if (c == 'u' || c == 'U')
235 cpp_error (pfile, "two `u's in integer constant");
245 /* Don't look for any more digits after the suffixes. */
248 if (largest_digit < digit)
249 largest_digit = digit;
250 nd = n * base + digit;
251 overflow |= ULONG_MAX_over_base < n || nd < n;
257 cpp_error (pfile, "Invalid number in #if expression");
262 if (base <= largest_digit)
263 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
266 cpp_pedwarn (pfile, "integer constant out of range");
268 /* If too big to be signed, consider it unsigned. */
269 if ((long) n < 0 && ! op.unsignedp)
272 cpp_warning (pfile, "integer constant is so large that it is unsigned");
286 static struct token tokentab2[] = {
300 /* Read one token. */
303 cpp_lex (pfile, skip_evaluation)
308 register int namelen;
309 register struct token *toktab;
310 enum cpp_token token;
312 U_CHAR *tok_start, *tok_end;
317 old_written = CPP_WRITTEN (pfile);
318 cpp_skip_hspace (pfile);
319 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
321 return parse_number (pfile,
322 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
330 token = cpp_get_token (pfile);
331 tok_start = pfile->token_buffer + old_written;
332 tok_end = CPP_PWRITTEN (pfile);
333 pfile->limit = tok_start;
336 case CPP_EOF: /* Should not happen ... */
341 if (CPP_BUFFER (pfile)->fname != NULL)
346 cpp_pop_buffer (pfile);
348 case CPP_HSPACE: case CPP_COMMENT:
351 return parse_number (pfile, tok_start, tok_end - tok_start);
353 cpp_error (pfile, "string constants not allowed in #if expressions");
357 /* This code for reading a character constant
358 handles multicharacter constants and wide characters.
359 It is mostly copied from c-lex.c. */
361 register int result = 0;
362 register num_chars = 0;
363 unsigned width = MAX_CHAR_TYPE_SIZE;
366 U_CHAR *ptr = tok_start;
367 #ifdef MULTIBYTE_CHARS
368 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
370 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
377 width = MAX_WCHAR_TYPE_SIZE;
378 #ifdef MULTIBYTE_CHARS
379 max_chars = MB_CUR_MAX;
385 max_chars = MAX_LONG_TYPE_SIZE / width;
388 while (ptr < tok_end && ((c = *ptr++) != '\''))
392 c = cpp_parse_escape (pfile, (char **) &ptr);
393 if (width < HOST_BITS_PER_INT
394 && (unsigned) c >= (1 << width))
396 "escape sequence out of range for character");
401 /* Merge character into result; ignore excess chars. */
402 if (num_chars < max_chars + 1)
404 if (width < HOST_BITS_PER_INT)
405 result = (result << width) | (c & ((1 << width) - 1));
408 token_buffer[num_chars - 1] = c;
412 token_buffer[num_chars] = 0;
415 cpp_error (pfile, "malformatted character constant");
416 else if (num_chars == 0)
417 cpp_error (pfile, "empty character constant");
418 else if (num_chars > max_chars)
420 num_chars = max_chars;
421 cpp_error (pfile, "character constant too long");
423 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
424 cpp_warning (pfile, "multi-character character constant");
426 /* If char type is signed, sign-extend the constant. */
429 int num_bits = num_chars * width;
431 if (cpp_lookup (pfile, (U_CHAR *)"__CHAR_UNSIGNED__",
432 sizeof ("__CHAR_UNSIGNED__")-1, -1)
433 || ((result >> (num_bits - 1)) & 1) == 0)
435 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
438 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
442 #ifdef MULTIBYTE_CHARS
443 /* Set the initial shift state and convert the next sequence. */
445 /* In all locales L'\0' is zero and mbtowc will return zero,
448 || (num_chars == 1 && token_buffer[0] != '\0'))
451 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
452 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
455 cpp_pedwarn (pfile,"Ignoring invalid multibyte character");
462 /* This is always a signed type. */
469 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
470 cpp_warning (pfile, "`%.*s' is not defined",
471 (int) (tok_end - tok_start), tok_start);
472 return parse_number (pfile, "0", 0);
475 /* See if it is a special token of length 2. */
476 if (tok_start + 2 == tok_end)
478 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
479 if (tok_start[0] == toktab->operator[0]
480 && tok_start[1] == toktab->operator[1])
482 if (toktab->token == ERROR)
484 char *buf = (char *) alloca (40);
485 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
486 cpp_error (pfile, buf);
488 op.op = toktab->token;
499 /* Parse a C escape sequence. STRING_PTR points to a variable
500 containing a pointer to the string to parse. That pointer
501 is updated past the characters we use. The value of the
502 escape sequence is returned.
504 A negative value means the sequence \ newline was seen,
505 which is supposed to be equivalent to nothing at all.
507 If \ is followed by a null character, we return a negative
508 value and leave the string pointer pointing at the null character.
510 If \ is followed by 000, we return 0 and leave the string pointer
511 after the zeros. A value of 0 does not mean end of string. */
514 cpp_parse_escape (pfile, string_ptr)
518 register int c = *(*string_ptr)++;
527 if (CPP_PEDANTIC (pfile))
528 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
533 return TARGET_NEWLINE;
555 register int i = c - '0';
556 register int count = 0;
559 c = *(*string_ptr)++;
560 if (c >= '0' && c <= '7')
561 i = (i << 3) + c - '0';
568 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
570 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
572 "octal character constant does not fit in a byte");
578 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
581 c = *(*string_ptr)++;
582 if (c >= '0' && c <= '9')
584 else if (c >= 'a' && c <= 'f')
585 digit = c - 'a' + 10;
586 else if (c >= 'A' && c <= 'F')
587 digit = c - 'A' + 10;
593 overflow |= i ^ (i << 4 >> 4);
594 i = (i << 4) + digit;
598 cpp_error (pfile, "\\x used with no following hex digits");
599 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
601 i &= (1 << BITS_PER_UNIT) - 1;
603 "hex character constant does not fit in a byte");
613 integer_overflow (pfile)
616 if (CPP_PEDANTIC (pfile))
617 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
621 left_shift (pfile, a, unsignedp, b)
627 if (b >= HOST_BITS_PER_LONG)
629 if (! unsignedp && a != 0)
630 integer_overflow (pfile);
634 return (unsigned long) a << b;
639 integer_overflow (pfile);
645 right_shift (pfile, a, unsignedp, b)
651 if (b >= HOST_BITS_PER_LONG)
652 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
654 return (unsigned long) a >> b;
659 /* These priorities are all even, so we can handle associatively. */
660 #define PAREN_INNER_PRIO 0
662 #define COND_PRIO (COMMA_PRIO+2)
663 #define OROR_PRIO (COND_PRIO+2)
664 #define ANDAND_PRIO (OROR_PRIO+2)
665 #define OR_PRIO (ANDAND_PRIO+2)
666 #define XOR_PRIO (OR_PRIO+2)
667 #define AND_PRIO (XOR_PRIO+2)
668 #define EQUAL_PRIO (AND_PRIO+2)
669 #define LESS_PRIO (EQUAL_PRIO+2)
670 #define SHIFT_PRIO (LESS_PRIO+2)
671 #define PLUS_PRIO (SHIFT_PRIO+2)
672 #define MUL_PRIO (PLUS_PRIO+2)
673 #define UNARY_PRIO (MUL_PRIO+2)
674 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
676 #define COMPARE(OP) \
678 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP v2 : (v1 OP v2)
680 /* Parse and evaluate a C expression, reading from PFILE.
681 Returns the value of the expression. */
684 cpp_parse_expr (pfile)
687 /* The implementation is an operator precedence parser,
688 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
690 The stack base is 'stack', and the current stack pointer is 'top'.
691 There is a stack element for each operator (only),
692 and the most recently pushed operator is 'top->op'.
693 An operand (value) is stored in the 'value' field of the stack
694 element of the operator that precedes it.
695 In that case the 'flags' field has the HAVE_VALUE flag set. */
697 #define INIT_STACK_SIZE 20
698 struct operation init_stack[INIT_STACK_SIZE];
699 struct operation *stack = init_stack;
700 struct operation *limit = stack + INIT_STACK_SIZE;
701 register struct operation *top = stack;
703 int skip_evaluation = 0;
713 op = cpp_lex (pfile, skip_evaluation);
715 /* See if the token is an operand, in which case go to set_value.
716 If the token is an operator, figure out its left and right
717 priorities, and then goto maybe_reduce. */
724 top->value = op.value;
725 top->unsignedp = op.unsignedp;
728 lprio = 0; goto maybe_reduce;
730 /* Is this correct if unary ? FIXME */
731 flags = RIGHT_OPERAND_REQUIRED;
732 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
734 flags = RIGHT_OPERAND_REQUIRED;
735 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
736 case '*': case '/': case '%':
737 lprio = MUL_PRIO; goto binop;
738 case '<': case '>': case LEQ: case GEQ:
739 lprio = LESS_PRIO; goto binop;
740 case EQUAL: case NOTEQUAL:
741 lprio = EQUAL_PRIO; goto binop;
743 lprio = SHIFT_PRIO; goto binop;
744 case '&': lprio = AND_PRIO; goto binop;
745 case '^': lprio = XOR_PRIO; goto binop;
746 case '|': lprio = OR_PRIO; goto binop;
747 case ANDAND: lprio = ANDAND_PRIO; goto binop;
748 case OROR: lprio = OROR_PRIO; goto binop;
750 lprio = COMMA_PRIO; goto binop;
752 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
755 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
758 lprio = COND_PRIO; rprio = COND_PRIO;
761 lprio = COND_PRIO + 1; rprio = COND_PRIO;
764 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
768 cpp_error (pfile, "invalid character in #if");
773 /* Push a value onto the stack. */
774 if (top->flags & HAVE_VALUE)
776 cpp_error (pfile, "syntax error in #if");
779 top->flags |= HAVE_VALUE;
783 /* Push an operator, and check if we can reduce now. */
784 while (top->rprio > lprio)
786 long v1 = top[-1].value, v2 = top[0].value;
787 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
789 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
790 && ! (top[0].flags & HAVE_VALUE))
792 cpp_error (pfile, "syntax error - missing left operand");
795 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
796 && ! (top[1].flags & HAVE_VALUE))
798 cpp_error (pfile, "syntax error - missing right operand");
801 /* top[0].value = (top[1].op)(v1, v2);*/
805 if (!(top->flags & HAVE_VALUE))
808 top->unsignedp = unsigned2;
809 top->flags |= HAVE_VALUE;
813 top->value = v1 + v2;
814 top->unsignedp = unsigned1 || unsigned2;
815 if (! top->unsignedp && ! skip_evaluation
816 && ! possible_sum_sign (v1, v2, top->value))
817 integer_overflow (pfile);
821 if (!(top->flags & HAVE_VALUE))
824 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
825 integer_overflow (pfile);
826 top->unsignedp = unsigned2;
827 top->flags |= HAVE_VALUE;
831 top->value = v1 - v2;
832 top->unsignedp = unsigned1 || unsigned2;
833 if (! top->unsignedp && ! skip_evaluation
834 && ! possible_sum_sign (top->value, v2, v1))
835 integer_overflow (pfile);
839 top->unsignedp = unsigned1 || unsigned2;
841 top->value = (unsigned long) v1 * v2;
842 else if (!skip_evaluation)
844 top->value = v1 * v2;
846 && (top->value / v1 != v2
847 || (top->value & v1 & v2) < 0))
848 integer_overflow (pfile);
856 cpp_error (pfile, "division by zero in #if");
859 top->unsignedp = unsigned1 || unsigned2;
861 top->value = (unsigned long) v1 / v2;
864 top->value = v1 / v2;
865 if ((top->value & v1 & v2) < 0)
866 integer_overflow (pfile);
874 cpp_error (pfile, "division by zero in #if");
877 top->unsignedp = unsigned1 || unsigned2;
879 top->value = (unsigned long) v1 % v2;
881 top->value = v1 % v2;
884 if (top->flags & HAVE_VALUE)
886 cpp_error (pfile, "syntax error");
891 top->flags |= HAVE_VALUE;
894 if (top->flags & HAVE_VALUE)
896 cpp_error (pfile, "syntax error");
900 top->unsignedp = unsigned2;
901 top->flags |= HAVE_VALUE;
903 case '<': COMPARE(<); break;
904 case '>': COMPARE(>); break;
905 case LEQ: COMPARE(<=); break;
906 case GEQ: COMPARE(>=); break;
908 top->value = (v1 == v2);
912 top->value = (v1 != v2);
918 top->unsignedp = unsigned1;
919 if (v2 < 0 && ! unsigned2)
920 top->value = right_shift (pfile, v1, unsigned1, -v2);
922 top->value = left_shift (pfile, v1, unsigned1, v2);
927 top->unsignedp = unsigned1;
928 if (v2 < 0 && ! unsigned2)
929 top->value = left_shift (pfile, v1, unsigned1, -v2);
931 top->value = right_shift (pfile, v1, unsigned1, v2);
933 #define LOGICAL(OP) \
934 top->value = v1 OP v2;\
935 top->unsignedp = unsigned1 || unsigned2;
936 case '&': LOGICAL(&); break;
937 case '^': LOGICAL(^); break;
938 case '|': LOGICAL(|); break;
940 top->value = v1 && v2; top->unsignedp = 0;
941 if (!v1) skip_evaluation--;
944 top->value = v1 || v2; top->unsignedp = 0;
945 if (v1) skip_evaluation--;
948 if (CPP_PEDANTIC (pfile))
949 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
951 top->unsignedp = unsigned2;
954 cpp_error (pfile, "syntax error in #if");
957 if (top[0].op != '?')
960 "syntax error ':' without preceding '?'");
963 else if (! (top[1].flags & HAVE_VALUE)
964 || !(top[-1].flags & HAVE_VALUE)
965 || !(top[0].flags & HAVE_VALUE))
967 cpp_error (pfile, "bad syntax for ?: operator");
973 if (top->value) skip_evaluation--;
974 top->value = top->value ? v1 : v2;
975 top->unsignedp = unsigned1 || unsigned2;
979 if ((top[1].flags & HAVE_VALUE)
980 || ! (top[0].flags & HAVE_VALUE)
982 || (top[-1].flags & HAVE_VALUE))
984 cpp_error (pfile, "mismatched parentheses in #if");
991 top->unsignedp = unsigned1;
992 top->flags |= HAVE_VALUE;
997 top[1].op >= ' ' && top[1].op <= '~'
998 ? "unimplemented operator '%c'\n"
999 : "unimplemented operator '\\%03o'\n",
1006 cpp_error (pfile, "internal error in #if expression");
1007 if (stack != init_stack)
1013 /* Check for and handle stack overflow. */
1016 struct operation *new_stack;
1017 int old_size = (char *) limit - (char *) stack;
1018 int new_size = 2 * old_size;
1019 if (stack != init_stack)
1020 new_stack = (struct operation *) xrealloc (stack, new_size);
1023 new_stack = (struct operation *) xmalloc (new_size);
1024 bcopy ((char *) stack, (char *) new_stack, old_size);
1027 top = (struct operation *) ((char *) new_stack + old_size);
1028 limit = (struct operation *) ((char *) new_stack + new_size);
1034 if ((op.op == OROR && top[-1].value)
1035 || (op.op == ANDAND && !top[-1].value)
1036 || (op.op == '?' && !top[-1].value))
1040 else if (op.op == ':')
1042 if (top[-2].value) /* Was condition true? */
1049 if (stack != init_stack)
1051 skip_rest_of_line (pfile);