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 #ifdef MULTIBYTE_CHARS
35 #ifndef CHAR_TYPE_SIZE
36 #define CHAR_TYPE_SIZE BITS_PER_UNIT
40 #define INT_TYPE_SIZE BITS_PER_WORD
43 #ifndef LONG_TYPE_SIZE
44 #define LONG_TYPE_SIZE BITS_PER_WORD
47 #ifndef WCHAR_TYPE_SIZE
48 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
51 #ifndef MAX_CHAR_TYPE_SIZE
52 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
55 #ifndef MAX_INT_TYPE_SIZE
56 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
59 #ifndef MAX_LONG_TYPE_SIZE
60 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
63 #ifndef MAX_WCHAR_TYPE_SIZE
64 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
67 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT \
68 ? (~ (~ (HOST_WIDE_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
69 : ~ (HOST_WIDE_INT) 0)
71 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT \
72 ? ~ (~ (HOST_WIDE_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
73 : ~ (HOST_WIDE_INT) 0)
75 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
76 number with SUM's sign, where A, B, and SUM are all C integers. */
77 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
79 static void integer_overflow PARAMS ((cpp_reader *));
80 static long left_shift PARAMS ((cpp_reader *, long, int, unsigned long));
81 static long right_shift PARAMS ((cpp_reader *, long, int, unsigned long));
96 #define LEFT_OPERAND_REQUIRED 1
97 #define RIGHT_OPERAND_REQUIRED 2
99 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
100 following operand should be short-circuited instead of evaluated. */
101 #define SKIP_OPERAND 8
102 /*#define UNSIGNEDP 16*/
108 #ifndef HOST_BITS_PER_WIDE_INT
109 #define HOST_BITS_PER_WIDE_INT (CHAR_BIT * sizeof (HOST_WIDE_INT))
114 char rprio; /* Priority of op (relative to it right operand). */
116 char unsignedp; /* true if value should be treated as unsigned */
117 HOST_WIDE_INT value; /* The value logically "right" of op. */
120 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
121 with or without size suffixes. */
123 static struct operation
124 parse_number (pfile, start, end)
132 unsigned HOST_WIDE_INT n = 0, nd, MAX_over_base;
135 int digit, largest_digit = 0;
142 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
154 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
155 MAX_over_base = (((unsigned HOST_WIDE_INT) -1)
156 / ((unsigned HOST_WIDE_INT) base));
162 if (c >= '0' && c <= '9')
164 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
165 digit = c - 'a' + 10;
166 else if (base == 16 && c >= 'A' && c <= 'F')
167 digit = c - 'A' + 10;
170 /* It's a float since it contains a point. */
172 "floating point numbers are not allowed in #if expressions");
177 /* `l' means long, and `u' means unsigned. */
180 if (c == 'l' || c == 'L')
182 else if (c == 'u' || c == 'U')
186 /* Decrement p here so that the error for an invalid number
187 will be generated below in the case where this is the
188 last character in the buffer. */
196 /* Don't look for any more digits after the suffixes. */
200 if (largest_digit < digit)
201 largest_digit = digit;
202 nd = n * base + digit;
203 overflow |= MAX_over_base < n || nd < n;
209 cpp_error (pfile, "invalid number in #if expression");
212 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
214 cpp_error (pfile, "too many `l' suffixes in integer constant");
217 else if (op.unsignedp > 1)
219 cpp_error (pfile, "too many `u' suffixes in integer constant");
223 if (base <= largest_digit)
224 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
227 cpp_pedwarn (pfile, "integer constant out of range");
229 /* If too big to be signed, consider it unsigned. */
230 else if ((HOST_WIDE_INT) n < 0 && ! op.unsignedp)
234 "integer constant is so large that it is unsigned");
247 /* Parse and convert a character constant for #if. Understands backslash
248 escapes (\n, \031) and multibyte characters (if so configured). */
249 static struct operation
250 parse_charconst (pfile, start, end)
256 HOST_WIDE_INT result = 0;
259 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
263 /* FIXME: Should use reentrant multibyte functions. */
264 #ifdef MULTIBYTE_CHARS
266 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
274 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
276 max_chars = MAX_LONG_TYPE_SIZE / width;
278 ++ptr; /* skip initial quote */
282 #ifndef MULTIBYTE_CHARS
285 ptr += mbtowc (&c, ptr, end - ptr);
287 if (c == '\'' || c == '\0')
291 /* Hopefully valid assumption: if mbtowc returns a backslash,
292 we are in initial shift state. No valid escape-sequence
293 character can take us out of initial shift state or begin
294 an unshifted multibyte char, so cpp_parse_escape doesn't
295 need to know about multibyte chars. */
297 c = cpp_parse_escape (pfile, (char **) &ptr, mask);
298 if (width < HOST_BITS_PER_INT
299 && (unsigned int) c >= (unsigned int)(1 << width))
300 cpp_pedwarn (pfile, "escape sequence out of range for character");
303 /* Merge character into result; ignore excess chars. */
304 if (++num_chars <= max_chars)
306 if (width < HOST_BITS_PER_INT)
307 result = (result << width) | (c & ((1 << width) - 1));
315 cpp_error (pfile, "empty character constant");
320 /* cpp_get_token has already emitted an error if !traditional. */
321 if (! CPP_TRADITIONAL (pfile))
322 cpp_error (pfile, "malformatted character constant");
325 else if (num_chars > max_chars)
327 cpp_error (pfile, "character constant too long");
330 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
331 cpp_warning (pfile, "multi-character character constant");
333 /* If char type is signed, sign-extend the constant. */
334 num_bits = num_chars * width;
336 if (cpp_lookup (pfile, (U_CHAR *)"__CHAR_UNSIGNED__",
337 sizeof ("__CHAR_UNSIGNED__")-1, -1)
338 || ((result >> (num_bits - 1)) & 1) == 0)
339 op.value = result & ((unsigned long) ~0
340 >> (HOST_BITS_PER_LONG - num_bits));
342 op.value = result | ~((unsigned long) ~0
343 >> (HOST_BITS_PER_LONG - num_bits));
345 /* This is always a signed type. */
361 static struct token tokentab2[] = {
375 /* Read one token. */
378 cpp_lex (pfile, skip_evaluation)
383 struct token *toktab;
384 enum cpp_token token;
386 U_CHAR *tok_start, *tok_end;
391 old_written = CPP_WRITTEN (pfile);
392 cpp_skip_hspace (pfile);
393 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
397 op.value = cpp_read_check_assertion (pfile);
407 token = cpp_get_token (pfile);
408 tok_start = pfile->token_buffer + old_written;
409 tok_end = CPP_PWRITTEN (pfile);
410 pfile->limit = tok_start;
413 case CPP_EOF: /* Should not happen ... */
418 if (CPP_BUFFER (pfile)->fname != NULL)
423 cpp_pop_buffer (pfile);
429 return parse_number (pfile, tok_start, tok_end);
431 cpp_error (pfile, "string constants not allowed in #if expressions");
435 return parse_charconst (pfile, tok_start, tok_end);
438 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
439 cpp_warning (pfile, "`%.*s' is not defined",
440 (int) (tok_end - tok_start), tok_start);
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, result_mask)
489 HOST_WIDE_INT result_mask;
491 register int c = *(*string_ptr)++;
500 if (CPP_OPTIONS (pfile)->pedantic)
501 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
506 return TARGET_NEWLINE;
528 register HOST_WIDE_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 != (i & result_mask))
544 cpp_pedwarn (pfile, "octal escape sequence out of range");
550 register unsigned HOST_WIDE_INT i = 0, overflow = 0;
551 register int 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 != (i & result_mask)))
575 cpp_pedwarn (pfile, "hex escape sequence out of range");
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)
618 cpp_reader *pfile ATTRIBUTE_UNUSED;
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) \
651 ? (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. */
657 cpp_parse_expr (pfile)
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;
676 int skip_evaluation = 0;
686 op = cpp_lex (pfile, skip_evaluation);
688 /* See if the token is an operand, in which case go to set_value.
689 If the token is an operator, figure out its left and right
690 priorities, and then goto maybe_reduce. */
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;
739 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
743 cpp_error (pfile, "invalid character in #if");
748 /* Push a value onto the stack. */
749 if (top->flags & HAVE_VALUE)
751 cpp_error (pfile, "syntax error in #if");
754 top->flags |= HAVE_VALUE;
758 /* Push an operator, and check if we can reduce now. */
759 while (top->rprio > lprio)
761 long v1 = top[-1].value, v2 = top[0].value;
762 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
764 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
765 && ! (top[0].flags & HAVE_VALUE))
767 cpp_error (pfile, "syntax error - missing left operand");
770 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
771 && ! (top[1].flags & HAVE_VALUE))
773 cpp_error (pfile, "syntax error - missing right operand");
776 /* top[0].value = (top[1].op)(v1, v2);*/
780 if (!(top->flags & HAVE_VALUE))
783 top->unsignedp = unsigned2;
784 top->flags |= HAVE_VALUE;
788 top->value = v1 + v2;
789 top->unsignedp = unsigned1 || unsigned2;
790 if (! top->unsignedp && ! skip_evaluation
791 && ! possible_sum_sign (v1, v2, top->value))
792 integer_overflow (pfile);
796 if (!(top->flags & HAVE_VALUE))
799 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
800 integer_overflow (pfile);
801 top->unsignedp = unsigned2;
802 top->flags |= HAVE_VALUE;
806 top->value = v1 - v2;
807 top->unsignedp = unsigned1 || unsigned2;
808 if (! top->unsignedp && ! skip_evaluation
809 && ! possible_sum_sign (top->value, v2, v1))
810 integer_overflow (pfile);
814 top->unsignedp = unsigned1 || unsigned2;
816 top->value = (unsigned long) v1 * v2;
817 else if (!skip_evaluation)
819 top->value = v1 * v2;
821 && (top->value / v1 != v2
822 || (top->value & v1 & v2) < 0))
823 integer_overflow (pfile);
831 cpp_error (pfile, "division by zero in #if");
834 top->unsignedp = unsigned1 || unsigned2;
836 top->value = (unsigned long) v1 / v2;
839 top->value = v1 / v2;
840 if ((top->value & v1 & v2) < 0)
841 integer_overflow (pfile);
849 cpp_error (pfile, "division by zero in #if");
852 top->unsignedp = unsigned1 || unsigned2;
854 top->value = (unsigned long) v1 % v2;
856 top->value = v1 % v2;
859 if (top->flags & HAVE_VALUE)
861 cpp_error (pfile, "syntax error");
866 top->flags |= HAVE_VALUE;
869 if (top->flags & HAVE_VALUE)
871 cpp_error (pfile, "syntax error");
875 top->unsignedp = unsigned2;
876 top->flags |= HAVE_VALUE;
878 case '<': COMPARE(<); break;
879 case '>': COMPARE(>); break;
880 case LEQ: COMPARE(<=); break;
881 case GEQ: COMPARE(>=); break;
883 top->value = (v1 == v2);
887 top->value = (v1 != v2);
893 top->unsignedp = unsigned1;
894 if (v2 < 0 && ! unsigned2)
895 top->value = right_shift (pfile, v1, unsigned1, -v2);
897 top->value = left_shift (pfile, v1, unsigned1, v2);
902 top->unsignedp = unsigned1;
903 if (v2 < 0 && ! unsigned2)
904 top->value = left_shift (pfile, v1, unsigned1, -v2);
906 top->value = right_shift (pfile, v1, unsigned1, v2);
908 #define LOGICAL(OP) \
909 top->value = v1 OP v2;\
910 top->unsignedp = unsigned1 || unsigned2;
911 case '&': LOGICAL(&); break;
912 case '^': LOGICAL(^); break;
913 case '|': LOGICAL(|); break;
915 top->value = v1 && v2; top->unsignedp = 0;
916 if (!v1) skip_evaluation--;
919 top->value = v1 || v2; top->unsignedp = 0;
920 if (v1) skip_evaluation--;
923 if (CPP_PEDANTIC (pfile))
924 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
926 top->unsignedp = unsigned2;
929 cpp_error (pfile, "syntax error in #if");
932 if (top[0].op != '?')
935 "syntax error ':' without preceding '?'");
938 else if (! (top[1].flags & HAVE_VALUE)
939 || !(top[-1].flags & HAVE_VALUE)
940 || !(top[0].flags & HAVE_VALUE))
942 cpp_error (pfile, "bad syntax for ?: operator");
948 if (top->value) skip_evaluation--;
949 top->value = top->value ? v1 : v2;
950 top->unsignedp = unsigned1 || unsigned2;
954 if ((top[1].flags & HAVE_VALUE)
955 || ! (top[0].flags & HAVE_VALUE)
957 || (top[-1].flags & HAVE_VALUE))
959 cpp_error (pfile, "mismatched parentheses in #if");
966 top->unsignedp = unsigned1;
967 top->flags |= HAVE_VALUE;
972 top[1].op >= ' ' && top[1].op <= '~'
973 ? "unimplemented operator '%c'\n"
974 : "unimplemented operator '\\%03o'\n",
981 cpp_error (pfile, "internal error in #if expression");
982 if (stack != init_stack)
988 /* Check for and handle stack overflow. */
991 struct operation *new_stack;
992 int old_size = (char *) limit - (char *) stack;
993 int new_size = 2 * old_size;
994 if (stack != init_stack)
995 new_stack = (struct operation *) xrealloc (stack, new_size);
998 new_stack = (struct operation *) xmalloc (new_size);
999 bcopy ((char *) stack, (char *) new_stack, old_size);
1002 top = (struct operation *) ((char *) new_stack + old_size);
1003 limit = (struct operation *) ((char *) new_stack + new_size);
1009 if ((op.op == OROR && top[-1].value)
1010 || (op.op == ANDAND && !top[-1].value)
1011 || (op.op == '?' && !top[-1].value))
1015 else if (op.op == ':')
1017 if (top[-2].value) /* Was condition true? */
1024 if (stack != init_stack)
1026 skip_rest_of_line (pfile);