1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998 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 */
31 extern char *xmalloc PARAMS ((unsigned));
32 extern char *xrealloc PARAMS ((void *, unsigned));
34 #ifdef MULTIBYTE_CHARS
38 /* This is used for communicating lists of keywords with cccp.c. */
46 #ifndef CHAR_TYPE_SIZE
47 #define CHAR_TYPE_SIZE BITS_PER_UNIT
51 #define INT_TYPE_SIZE BITS_PER_WORD
54 #ifndef LONG_TYPE_SIZE
55 #define LONG_TYPE_SIZE BITS_PER_WORD
58 #ifndef WCHAR_TYPE_SIZE
59 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
62 #ifndef MAX_CHAR_TYPE_SIZE
63 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
66 #ifndef MAX_INT_TYPE_SIZE
67 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
70 #ifndef MAX_LONG_TYPE_SIZE
71 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
74 #ifndef MAX_WCHAR_TYPE_SIZE
75 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
78 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
79 number with SUM's sign, where A, B, and SUM are all C integers. */
80 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
82 static void integer_overflow PARAMS ((cpp_reader *));
83 static long left_shift PARAMS ((cpp_reader *, long, int, unsigned long));
84 static long right_shift PARAMS ((cpp_reader *, long, int, unsigned long));
99 #define LEFT_OPERAND_REQUIRED 1
100 #define RIGHT_OPERAND_REQUIRED 2
102 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
103 following operand should be short-circuited instead of evaluated. */
104 #define SKIP_OPERAND 8
105 /*#define UNSIGNEDP 16*/
107 /* Find the largest host integer type and set its size and type.
108 Watch out: on some crazy hosts `long' is shorter than `int'. */
110 #ifndef HOST_WIDE_INT
112 # include <inttypes.h>
113 # define HOST_WIDE_INT intmax_t
115 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
116 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
117 # define HOST_WIDE_INT int
119 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
120 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
121 # define HOST_WIDE_INT long
123 # define HOST_WIDE_INT long long
133 #ifndef HOST_BITS_PER_WIDE_INT
134 #define HOST_BITS_PER_WIDE_INT (CHAR_BIT * sizeof (HOST_WIDE_INT))
139 char rprio; /* Priority of op (relative to it right operand). */
141 char unsignedp; /* true if value should be treated as unsigned */
142 HOST_WIDE_INT value; /* The value logically "right" of op. */
145 /* Take care of parsing a number (anything that starts with a digit).
146 LEN is the number of characters in it. */
148 /* maybe needs to actually deal with floating point numbers */
151 parse_number (pfile, start, olen)
157 register char *p = start;
159 register unsigned long n = 0, nd, ULONG_MAX_over_base;
160 register int base = 10;
161 register int len = olen;
162 register int overflow = 0;
163 register int digit, largest_digit = 0;
168 for (c = 0; c < len; c++)
170 /* It's a float since it contains a point. */
172 "floating point numbers not allowed in #if expressions");
177 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
185 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
186 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
188 for (; len > 0; len--) {
191 if (c >= '0' && c <= '9')
193 else if (base == 16 && c >= 'a' && c <= 'f')
194 digit = c - 'a' + 10;
195 else if (base == 16 && c >= 'A' && c <= 'F')
196 digit = c - 'A' + 10;
198 /* `l' means long, and `u' means unsigned. */
200 if (c == 'l' || c == 'L')
203 cpp_error (pfile, "two `l's in integer constant");
206 else if (c == 'u' || c == 'U')
209 cpp_error (pfile, "two `u's in integer constant");
219 /* Don't look for any more digits after the suffixes. */
222 if (largest_digit < digit)
223 largest_digit = digit;
224 nd = n * base + digit;
225 overflow |= ULONG_MAX_over_base < n || nd < n;
231 cpp_error (pfile, "Invalid number in #if expression");
236 if (base <= largest_digit)
237 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
240 cpp_pedwarn (pfile, "integer constant out of range");
242 /* If too big to be signed, consider it unsigned. */
243 if ((long) n < 0 && ! op.unsignedp)
246 cpp_warning (pfile, "integer constant is so large that it is unsigned");
260 static struct token tokentab2[] = {
274 /* Read one token. */
277 cpp_lex (pfile, skip_evaluation)
282 register struct token *toktab;
283 enum cpp_token token;
285 U_CHAR *tok_start, *tok_end;
290 old_written = CPP_WRITTEN (pfile);
291 cpp_skip_hspace (pfile);
292 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
294 return parse_number (pfile,
295 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
303 token = cpp_get_token (pfile);
304 tok_start = pfile->token_buffer + old_written;
305 tok_end = CPP_PWRITTEN (pfile);
306 pfile->limit = tok_start;
309 case CPP_EOF: /* Should not happen ... */
314 if (CPP_BUFFER (pfile)->fname != NULL)
319 cpp_pop_buffer (pfile);
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 int 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, (char **) &ptr);
366 if (width < HOST_BITS_PER_INT
367 && (unsigned) c >= (unsigned)(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, (U_CHAR *)"__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_pedwarn (pfile,"Ignoring invalid multibyte character");
435 /* This is always a signed type. */
442 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
443 cpp_warning (pfile, "`%.*s' is not defined",
444 (int) (tok_end - tok_start), tok_start);
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. */
487 cpp_parse_escape (pfile, string_ptr)
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");
586 integer_overflow (pfile)
589 if (CPP_PEDANTIC (pfile))
590 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
594 left_shift (pfile, a, unsignedp, b)
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);
618 right_shift (pfile, a, unsignedp, b)
619 cpp_reader *pfile ATTRIBUTE_UNUSED;
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) \
652 ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
654 /* Parse and evaluate a C expression, reading from PFILE.
655 Returns the value of the expression. */
658 cpp_parse_expr (pfile)
661 /* The implementation is an operator precedence parser,
662 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
664 The stack base is 'stack', and the current stack pointer is 'top'.
665 There is a stack element for each operator (only),
666 and the most recently pushed operator is 'top->op'.
667 An operand (value) is stored in the 'value' field of the stack
668 element of the operator that precedes it.
669 In that case the 'flags' field has the HAVE_VALUE flag set. */
671 #define INIT_STACK_SIZE 20
672 struct operation init_stack[INIT_STACK_SIZE];
673 struct operation *stack = init_stack;
674 struct operation *limit = stack + INIT_STACK_SIZE;
675 register struct operation *top = stack;
677 int skip_evaluation = 0;
687 op = cpp_lex (pfile, skip_evaluation);
689 /* See if the token is an operand, in which case go to set_value.
690 If the token is an operator, figure out its left and right
691 priorities, and then goto maybe_reduce. */
698 top->value = op.value;
699 top->unsignedp = op.unsignedp;
702 lprio = 0; goto maybe_reduce;
704 /* Is this correct if unary ? FIXME */
705 flags = RIGHT_OPERAND_REQUIRED;
706 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
708 flags = RIGHT_OPERAND_REQUIRED;
709 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
710 case '*': case '/': case '%':
711 lprio = MUL_PRIO; goto binop;
712 case '<': case '>': case LEQ: case GEQ:
713 lprio = LESS_PRIO; goto binop;
714 case EQUAL: case NOTEQUAL:
715 lprio = EQUAL_PRIO; goto binop;
717 lprio = SHIFT_PRIO; goto binop;
718 case '&': lprio = AND_PRIO; goto binop;
719 case '^': lprio = XOR_PRIO; goto binop;
720 case '|': lprio = OR_PRIO; goto binop;
721 case ANDAND: lprio = ANDAND_PRIO; goto binop;
722 case OROR: lprio = OROR_PRIO; goto binop;
724 lprio = COMMA_PRIO; goto binop;
726 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
729 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
732 lprio = COND_PRIO; rprio = COND_PRIO;
735 lprio = COND_PRIO + 1; rprio = COND_PRIO;
738 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
742 cpp_error (pfile, "invalid character in #if");
747 /* Push a value onto the stack. */
748 if (top->flags & HAVE_VALUE)
750 cpp_error (pfile, "syntax error in #if");
753 top->flags |= HAVE_VALUE;
757 /* Push an operator, and check if we can reduce now. */
758 while (top->rprio > lprio)
760 long v1 = top[-1].value, v2 = top[0].value;
761 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
763 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
764 && ! (top[0].flags & HAVE_VALUE))
766 cpp_error (pfile, "syntax error - missing left operand");
769 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
770 && ! (top[1].flags & HAVE_VALUE))
772 cpp_error (pfile, "syntax error - missing right operand");
775 /* top[0].value = (top[1].op)(v1, v2);*/
779 if (!(top->flags & HAVE_VALUE))
782 top->unsignedp = unsigned2;
783 top->flags |= HAVE_VALUE;
787 top->value = v1 + v2;
788 top->unsignedp = unsigned1 || unsigned2;
789 if (! top->unsignedp && ! skip_evaluation
790 && ! possible_sum_sign (v1, v2, top->value))
791 integer_overflow (pfile);
795 if (!(top->flags & HAVE_VALUE))
798 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
799 integer_overflow (pfile);
800 top->unsignedp = unsigned2;
801 top->flags |= HAVE_VALUE;
805 top->value = v1 - v2;
806 top->unsignedp = unsigned1 || unsigned2;
807 if (! top->unsignedp && ! skip_evaluation
808 && ! possible_sum_sign (top->value, v2, v1))
809 integer_overflow (pfile);
813 top->unsignedp = unsigned1 || unsigned2;
815 top->value = (unsigned long) v1 * v2;
816 else if (!skip_evaluation)
818 top->value = v1 * v2;
820 && (top->value / v1 != v2
821 || (top->value & v1 & v2) < 0))
822 integer_overflow (pfile);
830 cpp_error (pfile, "division by zero in #if");
833 top->unsignedp = unsigned1 || unsigned2;
835 top->value = (unsigned long) v1 / v2;
838 top->value = v1 / v2;
839 if ((top->value & v1 & v2) < 0)
840 integer_overflow (pfile);
848 cpp_error (pfile, "division by zero in #if");
851 top->unsignedp = unsigned1 || unsigned2;
853 top->value = (unsigned long) v1 % v2;
855 top->value = v1 % v2;
858 if (top->flags & HAVE_VALUE)
860 cpp_error (pfile, "syntax error");
865 top->flags |= HAVE_VALUE;
868 if (top->flags & HAVE_VALUE)
870 cpp_error (pfile, "syntax error");
874 top->unsignedp = unsigned2;
875 top->flags |= HAVE_VALUE;
877 case '<': COMPARE(<); break;
878 case '>': COMPARE(>); break;
879 case LEQ: COMPARE(<=); break;
880 case GEQ: COMPARE(>=); break;
882 top->value = (v1 == v2);
886 top->value = (v1 != v2);
892 top->unsignedp = unsigned1;
893 if (v2 < 0 && ! unsigned2)
894 top->value = right_shift (pfile, v1, unsigned1, -v2);
896 top->value = left_shift (pfile, v1, unsigned1, v2);
901 top->unsignedp = unsigned1;
902 if (v2 < 0 && ! unsigned2)
903 top->value = left_shift (pfile, v1, unsigned1, -v2);
905 top->value = right_shift (pfile, v1, unsigned1, v2);
907 #define LOGICAL(OP) \
908 top->value = v1 OP v2;\
909 top->unsignedp = unsigned1 || unsigned2;
910 case '&': LOGICAL(&); break;
911 case '^': LOGICAL(^); break;
912 case '|': LOGICAL(|); break;
914 top->value = v1 && v2; top->unsignedp = 0;
915 if (!v1) skip_evaluation--;
918 top->value = v1 || v2; top->unsignedp = 0;
919 if (v1) skip_evaluation--;
922 if (CPP_PEDANTIC (pfile))
923 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
925 top->unsignedp = unsigned2;
928 cpp_error (pfile, "syntax error in #if");
931 if (top[0].op != '?')
934 "syntax error ':' without preceding '?'");
937 else if (! (top[1].flags & HAVE_VALUE)
938 || !(top[-1].flags & HAVE_VALUE)
939 || !(top[0].flags & HAVE_VALUE))
941 cpp_error (pfile, "bad syntax for ?: operator");
947 if (top->value) skip_evaluation--;
948 top->value = top->value ? v1 : v2;
949 top->unsignedp = unsigned1 || unsigned2;
953 if ((top[1].flags & HAVE_VALUE)
954 || ! (top[0].flags & HAVE_VALUE)
956 || (top[-1].flags & HAVE_VALUE))
958 cpp_error (pfile, "mismatched parentheses in #if");
965 top->unsignedp = unsigned1;
966 top->flags |= HAVE_VALUE;
971 top[1].op >= ' ' && top[1].op <= '~'
972 ? "unimplemented operator '%c'\n"
973 : "unimplemented operator '\\%03o'\n",
980 cpp_error (pfile, "internal error in #if expression");
981 if (stack != init_stack)
987 /* Check for and handle stack overflow. */
990 struct operation *new_stack;
991 int old_size = (char *) limit - (char *) stack;
992 int new_size = 2 * old_size;
993 if (stack != init_stack)
994 new_stack = (struct operation *) xrealloc (stack, new_size);
997 new_stack = (struct operation *) xmalloc (new_size);
998 bcopy ((char *) stack, (char *) new_stack, old_size);
1001 top = (struct operation *) ((char *) new_stack + old_size);
1002 limit = (struct operation *) ((char *) new_stack + new_size);
1008 if ((op.op == OROR && top[-1].value)
1009 || (op.op == ANDAND && !top[-1].value)
1010 || (op.op == '?' && !top[-1].value))
1014 else if (op.op == ':')
1016 if (top[-2].value) /* Was condition true? */
1023 if (stack != init_stack)
1025 skip_rest_of_line (pfile);