1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999 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))
112 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
116 char rprio; /* Priority of op (relative to it right operand). */
118 char unsignedp; /* true if value should be treated as unsigned */
119 HOST_WIDE_INT value; /* The value logically "right" of op. */
122 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
123 with or without size suffixes. */
125 static struct operation
126 parse_number (pfile, start, end)
134 unsigned HOST_WIDE_INT n = 0, nd, MAX_over_base;
137 int digit, largest_digit = 0;
144 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
156 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
157 MAX_over_base = (((unsigned HOST_WIDE_INT) -1)
158 / ((unsigned HOST_WIDE_INT) base));
164 if (c >= '0' && c <= '9')
166 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
167 digit = c - 'a' + 10;
168 else if (base == 16 && c >= 'A' && c <= 'F')
169 digit = c - 'A' + 10;
172 /* It's a float since it contains a point. */
174 "floating point numbers are not allowed in #if expressions");
179 /* `l' means long, and `u' means unsigned. */
182 if (c == 'l' || c == 'L')
184 else if (c == 'u' || c == 'U')
188 /* Decrement p here so that the error for an invalid number
189 will be generated below in the case where this is the
190 last character in the buffer. */
198 /* Don't look for any more digits after the suffixes. */
202 if (largest_digit < digit)
203 largest_digit = digit;
204 nd = n * base + digit;
205 overflow |= MAX_over_base < n || nd < n;
211 cpp_error (pfile, "invalid number in #if expression");
214 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
216 cpp_error (pfile, "too many `l' suffixes in integer constant");
219 else if (op.unsignedp > 1)
221 cpp_error (pfile, "too many `u' suffixes in integer constant");
225 if (base <= largest_digit)
226 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
229 cpp_pedwarn (pfile, "integer constant out of range");
231 /* If too big to be signed, consider it unsigned. */
232 else if ((HOST_WIDE_INT) n < 0 && ! op.unsignedp)
236 "integer constant is so large that it is unsigned");
249 /* Parse and convert a character constant for #if. Understands backslash
250 escapes (\n, \031) and multibyte characters (if so configured). */
251 static struct operation
252 parse_charconst (pfile, start, end)
258 HOST_WIDE_INT result = 0;
261 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
265 /* FIXME: Should use reentrant multibyte functions. */
266 #ifdef MULTIBYTE_CHARS
268 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
276 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
278 max_chars = MAX_LONG_TYPE_SIZE / width;
280 ++ptr; /* skip initial quote */
284 #ifndef MULTIBYTE_CHARS
287 ptr += mbtowc (&c, ptr, end - ptr);
289 if (c == '\'' || c == '\0')
293 /* Hopefully valid assumption: if mbtowc returns a backslash,
294 we are in initial shift state. No valid escape-sequence
295 character can take us out of initial shift state or begin
296 an unshifted multibyte char, so cpp_parse_escape doesn't
297 need to know about multibyte chars. */
299 c = cpp_parse_escape (pfile, (char **) &ptr, mask);
300 if (width < HOST_BITS_PER_INT
301 && (unsigned int) c >= (unsigned int)(1 << width))
302 cpp_pedwarn (pfile, "escape sequence out of range for character");
305 /* Merge character into result; ignore excess chars. */
306 if (++num_chars <= max_chars)
308 if (width < HOST_BITS_PER_INT)
309 result = (result << width) | (c & ((1 << width) - 1));
317 cpp_error (pfile, "empty character constant");
322 /* cpp_get_token has already emitted an error if !traditional. */
323 if (! CPP_TRADITIONAL (pfile))
324 cpp_error (pfile, "malformatted character constant");
327 else if (num_chars > max_chars)
329 cpp_error (pfile, "character constant too long");
332 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
333 cpp_warning (pfile, "multi-character character constant");
335 /* If char type is signed, sign-extend the constant. */
336 num_bits = num_chars * width;
338 if (cpp_lookup (pfile, (U_CHAR *)"__CHAR_UNSIGNED__",
339 sizeof ("__CHAR_UNSIGNED__")-1, -1)
340 || ((result >> (num_bits - 1)) & 1) == 0)
341 op.value = result & ((unsigned long) ~0
342 >> (HOST_BITS_PER_LONG - num_bits));
344 op.value = result | ~((unsigned long) ~0
345 >> (HOST_BITS_PER_LONG - num_bits));
347 /* This is always a signed type. */
363 static struct token tokentab2[] = {
377 /* Read one token. */
379 static struct operation
380 cpp_lex (pfile, skip_evaluation)
385 struct token *toktab;
386 enum cpp_token token;
388 U_CHAR *tok_start, *tok_end;
393 old_written = CPP_WRITTEN (pfile);
394 cpp_skip_hspace (pfile);
395 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
399 op.value = cpp_read_check_assertion (pfile);
409 token = cpp_get_token (pfile);
410 tok_start = pfile->token_buffer + old_written;
411 tok_end = CPP_PWRITTEN (pfile);
412 pfile->limit = tok_start;
415 case CPP_EOF: /* Should not happen ... */
420 if (CPP_BUFFER (pfile)->fname != NULL)
425 cpp_pop_buffer (pfile);
431 return parse_number (pfile, tok_start, tok_end);
433 cpp_error (pfile, "string constants not allowed in #if expressions");
437 return parse_charconst (pfile, tok_start, tok_end);
443 if (strcmp (tok_start, "defined"))
445 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
446 cpp_warning (pfile, "`%.*s' is not defined",
447 (int) (tok_end - tok_start), tok_start);
452 cpp_buffer *ip = CPP_BUFFER (pfile);
455 SKIP_WHITE_SPACE (ip->cur);
459 ip->cur++; /* Skip over the paren */
460 SKIP_WHITE_SPACE (ip->cur);
463 if (!is_idstart[*ip->cur])
465 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
468 while (is_idchar[*ip->cur])
471 SKIP_WHITE_SPACE (ip->cur);
478 if (cpp_lookup (pfile, tok, len, -1))
485 cpp_error (pfile, "`defined' without an identifier");
489 /* See if it is a special token of length 2. */
490 if (tok_start + 2 == tok_end)
492 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
493 if (tok_start[0] == toktab->operator[0]
494 && tok_start[1] == toktab->operator[1])
496 if (toktab->token == ERROR)
497 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
499 op.op = toktab->token;
510 /* Parse a C escape sequence. STRING_PTR points to a variable
511 containing a pointer to the string to parse. That pointer
512 is updated past the characters we use. The value of the
513 escape sequence is returned.
515 A negative value means the sequence \ newline was seen,
516 which is supposed to be equivalent to nothing at all.
518 If \ is followed by a null character, we return a negative
519 value and leave the string pointer pointing at the null character.
521 If \ is followed by 000, we return 0 and leave the string pointer
522 after the zeros. A value of 0 does not mean end of string. */
525 cpp_parse_escape (pfile, string_ptr, result_mask)
528 HOST_WIDE_INT result_mask;
530 register int c = *(*string_ptr)++;
539 if (CPP_OPTIONS (pfile)->pedantic)
540 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
545 return TARGET_NEWLINE;
567 register HOST_WIDE_INT i = c - '0';
568 register int count = 0;
571 c = *(*string_ptr)++;
572 if (c >= '0' && c <= '7')
573 i = (i << 3) + c - '0';
580 if (i != (i & result_mask))
583 cpp_pedwarn (pfile, "octal escape sequence out of range");
589 register unsigned HOST_WIDE_INT i = 0, overflow = 0;
590 register int digits_found = 0, digit;
593 c = *(*string_ptr)++;
594 if (c >= '0' && c <= '9')
596 else if (c >= 'a' && c <= 'f')
597 digit = c - 'a' + 10;
598 else if (c >= 'A' && c <= 'F')
599 digit = c - 'A' + 10;
605 overflow |= i ^ (i << 4 >> 4);
606 i = (i << 4) + digit;
610 cpp_error (pfile, "\\x used with no following hex digits");
611 if (overflow | (i != (i & result_mask)))
614 cpp_pedwarn (pfile, "hex escape sequence out of range");
624 integer_overflow (pfile)
627 if (CPP_PEDANTIC (pfile))
628 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
632 left_shift (pfile, a, unsignedp, b)
638 if (b >= HOST_BITS_PER_LONG)
640 if (! unsignedp && a != 0)
641 integer_overflow (pfile);
645 return (unsigned long) a << b;
650 integer_overflow (pfile);
656 right_shift (pfile, a, unsignedp, b)
657 cpp_reader *pfile ATTRIBUTE_UNUSED;
662 if (b >= HOST_BITS_PER_LONG)
663 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
665 return (unsigned long) a >> b;
670 /* These priorities are all even, so we can handle associatively. */
671 #define PAREN_INNER_PRIO 0
673 #define COND_PRIO (COMMA_PRIO+2)
674 #define OROR_PRIO (COND_PRIO+2)
675 #define ANDAND_PRIO (OROR_PRIO+2)
676 #define OR_PRIO (ANDAND_PRIO+2)
677 #define XOR_PRIO (OR_PRIO+2)
678 #define AND_PRIO (XOR_PRIO+2)
679 #define EQUAL_PRIO (AND_PRIO+2)
680 #define LESS_PRIO (EQUAL_PRIO+2)
681 #define SHIFT_PRIO (LESS_PRIO+2)
682 #define PLUS_PRIO (SHIFT_PRIO+2)
683 #define MUL_PRIO (PLUS_PRIO+2)
684 #define UNARY_PRIO (MUL_PRIO+2)
685 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
687 #define COMPARE(OP) \
689 top->value = (unsigned1 || unsigned2) \
690 ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
692 /* Parse and evaluate a C expression, reading from PFILE.
693 Returns the value of the expression. */
696 cpp_parse_expr (pfile)
699 /* The implementation is an operator precedence parser,
700 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
702 The stack base is 'stack', and the current stack pointer is 'top'.
703 There is a stack element for each operator (only),
704 and the most recently pushed operator is 'top->op'.
705 An operand (value) is stored in the 'value' field of the stack
706 element of the operator that precedes it.
707 In that case the 'flags' field has the HAVE_VALUE flag set. */
709 #define INIT_STACK_SIZE 20
710 struct operation init_stack[INIT_STACK_SIZE];
711 struct operation *stack = init_stack;
712 struct operation *limit = stack + INIT_STACK_SIZE;
713 register struct operation *top = stack;
715 int skip_evaluation = 0;
725 op = cpp_lex (pfile, skip_evaluation);
727 /* See if the token is an operand, in which case go to set_value.
728 If the token is an operator, figure out its left and right
729 priorities, and then goto maybe_reduce. */
736 top->value = op.value;
737 top->unsignedp = op.unsignedp;
740 lprio = 0; goto maybe_reduce;
742 /* Is this correct if unary ? FIXME */
743 flags = RIGHT_OPERAND_REQUIRED;
744 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
746 flags = RIGHT_OPERAND_REQUIRED;
747 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
748 case '*': case '/': case '%':
749 lprio = MUL_PRIO; goto binop;
750 case '<': case '>': case LEQ: case GEQ:
751 lprio = LESS_PRIO; goto binop;
752 case EQUAL: case NOTEQUAL:
753 lprio = EQUAL_PRIO; goto binop;
755 lprio = SHIFT_PRIO; goto binop;
756 case '&': lprio = AND_PRIO; goto binop;
757 case '^': lprio = XOR_PRIO; goto binop;
758 case '|': lprio = OR_PRIO; goto binop;
759 case ANDAND: lprio = ANDAND_PRIO; goto binop;
760 case OROR: lprio = OROR_PRIO; goto binop;
762 lprio = COMMA_PRIO; goto binop;
764 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
767 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
770 lprio = COND_PRIO; rprio = COND_PRIO;
773 lprio = COND_PRIO + 1; rprio = COND_PRIO;
778 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
782 cpp_error (pfile, "invalid character in #if");
787 /* Push a value onto the stack. */
788 if (top->flags & HAVE_VALUE)
790 cpp_error (pfile, "syntax error in #if");
793 top->flags |= HAVE_VALUE;
797 /* Push an operator, and check if we can reduce now. */
798 while (top->rprio > lprio)
800 long v1 = top[-1].value, v2 = top[0].value;
801 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
803 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
804 && ! (top[0].flags & HAVE_VALUE))
806 cpp_error (pfile, "syntax error - missing left operand");
809 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
810 && ! (top[1].flags & HAVE_VALUE))
812 cpp_error (pfile, "syntax error - missing right operand");
815 /* top[0].value = (top[1].op)(v1, v2);*/
819 if (!(top->flags & HAVE_VALUE))
822 top->unsignedp = unsigned2;
823 top->flags |= HAVE_VALUE;
827 top->value = v1 + v2;
828 top->unsignedp = unsigned1 || unsigned2;
829 if (! top->unsignedp && ! skip_evaluation
830 && ! possible_sum_sign (v1, v2, top->value))
831 integer_overflow (pfile);
835 if (!(top->flags & HAVE_VALUE))
838 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
839 integer_overflow (pfile);
840 top->unsignedp = unsigned2;
841 top->flags |= HAVE_VALUE;
845 top->value = v1 - v2;
846 top->unsignedp = unsigned1 || unsigned2;
847 if (! top->unsignedp && ! skip_evaluation
848 && ! possible_sum_sign (top->value, v2, v1))
849 integer_overflow (pfile);
853 top->unsignedp = unsigned1 || unsigned2;
855 top->value = (unsigned long) v1 * v2;
856 else if (!skip_evaluation)
858 top->value = v1 * v2;
860 && (top->value / v1 != v2
861 || (top->value & v1 & v2) < 0))
862 integer_overflow (pfile);
870 cpp_error (pfile, "division by zero in #if");
873 top->unsignedp = unsigned1 || unsigned2;
875 top->value = (unsigned long) v1 / v2;
878 top->value = v1 / v2;
879 if ((top->value & v1 & v2) < 0)
880 integer_overflow (pfile);
888 cpp_error (pfile, "division by zero in #if");
891 top->unsignedp = unsigned1 || unsigned2;
893 top->value = (unsigned long) v1 % v2;
895 top->value = v1 % v2;
898 if (top->flags & HAVE_VALUE)
900 cpp_error (pfile, "syntax error");
905 top->flags |= HAVE_VALUE;
908 if (top->flags & HAVE_VALUE)
910 cpp_error (pfile, "syntax error");
914 top->unsignedp = unsigned2;
915 top->flags |= HAVE_VALUE;
917 case '<': COMPARE(<); break;
918 case '>': COMPARE(>); break;
919 case LEQ: COMPARE(<=); break;
920 case GEQ: COMPARE(>=); break;
922 top->value = (v1 == v2);
926 top->value = (v1 != v2);
932 top->unsignedp = unsigned1;
933 if (v2 < 0 && ! unsigned2)
934 top->value = right_shift (pfile, v1, unsigned1, -v2);
936 top->value = left_shift (pfile, v1, unsigned1, v2);
941 top->unsignedp = unsigned1;
942 if (v2 < 0 && ! unsigned2)
943 top->value = left_shift (pfile, v1, unsigned1, -v2);
945 top->value = right_shift (pfile, v1, unsigned1, v2);
947 #define LOGICAL(OP) \
948 top->value = v1 OP v2;\
949 top->unsignedp = unsigned1 || unsigned2;
950 case '&': LOGICAL(&); break;
951 case '^': LOGICAL(^); break;
952 case '|': LOGICAL(|); break;
954 top->value = v1 && v2; top->unsignedp = 0;
955 if (!v1) skip_evaluation--;
958 top->value = v1 || v2; top->unsignedp = 0;
959 if (v1) skip_evaluation--;
962 if (CPP_PEDANTIC (pfile))
963 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
965 top->unsignedp = unsigned2;
968 cpp_error (pfile, "syntax error in #if");
971 if (top[0].op != '?')
974 "syntax error ':' without preceding '?'");
977 else if (! (top[1].flags & HAVE_VALUE)
978 || !(top[-1].flags & HAVE_VALUE)
979 || !(top[0].flags & HAVE_VALUE))
981 cpp_error (pfile, "bad syntax for ?: operator");
987 if (top->value) skip_evaluation--;
988 top->value = top->value ? v1 : v2;
989 top->unsignedp = unsigned1 || unsigned2;
993 if ((top[1].flags & HAVE_VALUE)
994 || ! (top[0].flags & HAVE_VALUE)
996 || (top[-1].flags & HAVE_VALUE))
998 cpp_error (pfile, "mismatched parentheses in #if");
1005 top->unsignedp = unsigned1;
1006 top->flags |= HAVE_VALUE;
1011 (top[1].op >= ' ' && top[1].op <= '~'
1012 ? "unimplemented operator '%c'\n"
1013 : "unimplemented operator '\\%03o'\n"),
1020 cpp_error (pfile, "internal error in #if expression");
1021 if (stack != init_stack)
1027 /* Check for and handle stack overflow. */
1030 struct operation *new_stack;
1031 int old_size = (char *) limit - (char *) stack;
1032 int new_size = 2 * old_size;
1033 if (stack != init_stack)
1034 new_stack = (struct operation *) xrealloc (stack, new_size);
1037 new_stack = (struct operation *) xmalloc (new_size);
1038 bcopy ((char *) stack, (char *) new_stack, old_size);
1041 top = (struct operation *) ((char *) new_stack + old_size);
1042 limit = (struct operation *) ((char *) new_stack + new_size);
1048 if ((op.op == OROR && top[-1].value)
1049 || (op.op == ANDAND && !top[-1].value)
1050 || (op.op == '?' && !top[-1].value))
1054 else if (op.op == ':')
1056 if (top[-2].value) /* Was condition true? */
1063 if (stack != init_stack)
1065 skip_rest_of_line (pfile);