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 */
27 //#if defined(_MSC_VER)
36 //extern char *xmalloc PARAMS ((unsigned));
37 //extern char *xrealloc PARAMS ((char *, unsigned));
39 #ifdef MULTIBYTE_CHARS
47 /* This is used for communicating lists of keywords with cccp.c. */
55 /* Define a generic NULL if one hasn't already been defined. */
62 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
63 #define GENERIC_PTR void *
65 #define GENERIC_PTR char *
70 #define NULL_PTR ((GENERIC_PTR)0)
73 //extern char *xmalloc ();
75 #ifndef CHAR_TYPE_SIZE
76 #define CHAR_TYPE_SIZE BITS_PER_UNIT
80 #define INT_TYPE_SIZE BITS_PER_WORD
83 #ifndef LONG_TYPE_SIZE
84 #define LONG_TYPE_SIZE BITS_PER_WORD
87 #ifndef WCHAR_TYPE_SIZE
88 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
91 #ifndef MAX_CHAR_TYPE_SIZE
92 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
95 #ifndef MAX_INT_TYPE_SIZE
96 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
99 #ifndef MAX_LONG_TYPE_SIZE
100 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
103 #ifndef MAX_WCHAR_TYPE_SIZE
104 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
107 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
108 number with SUM's sign, where A, B, and SUM are all C integers. */
109 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
111 static void integer_overflow ();
112 static long left_shift ();
113 static long right_shift ();
128 #define LEFT_OPERAND_REQUIRED 1
129 #define RIGHT_OPERAND_REQUIRED 2
131 /*#define UNSIGNEDP 8*/
133 #ifndef HOST_BITS_PER_WIDE_INT
135 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
136 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
137 #define HOST_WIDE_INT long
139 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
140 #define HOST_WIDE_INT int
147 char rprio; /* Priority of op (relative to it right operand). */
149 char unsignedp; /* true if value should be treated as unsigned */
150 HOST_WIDE_INT value; /* The value logically "right" of op. */
153 /* Take care of parsing a number (anything that starts with a digit).
154 LEN is the number of characters in it. */
156 /* maybe needs to actually deal with floating point numbers */
165 register char *p = start;
167 register unsigned long n = 0, nd, ULONG_MAX_over_base;
168 register int base = 10;
169 register int len = olen;
170 register int overflow = 0;
171 register int digit, largest_digit = 0;
176 for (c = 0; c < len; c++)
178 /* It's a float since it contains a point. */
180 "floating point numbers not allowed in #if expressions");
185 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
193 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
194 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
196 for (; len > 0; len--) {
199 if (c >= '0' && c <= '9')
201 else if (base == 16 && c >= 'a' && c <= 'f')
202 digit = c - 'a' + 10;
203 else if (base == 16 && c >= 'A' && c <= 'F')
204 digit = c - 'A' + 10;
206 /* `l' means long, and `u' means unsigned. */
208 if (c == 'l' || c == 'L')
211 cpp_error (pfile, "two `l's in integer constant");
214 else if (c == 'u' || c == 'U')
217 cpp_error (pfile, "two `u's in integer constant");
227 /* Don't look for any more digits after the suffixes. */
230 if (largest_digit < digit)
231 largest_digit = digit;
232 nd = n * base + digit;
233 overflow |= ULONG_MAX_over_base < n | nd < n;
239 cpp_error (pfile, "Invalid number in #if expression");
244 if (base <= largest_digit)
245 cpp_warning (pfile, "integer constant contains digits beyond the radix");
248 cpp_warning (pfile, "integer constant out of range");
250 /* If too big to be signed, consider it unsigned. */
251 if ((long) n < 0 && ! op.unsignedp)
254 cpp_warning (pfile, "integer constant is so large that it is unsigned");
268 static struct token tokentab2[] = {
282 /* Read one token. */
289 register int namelen;
290 register struct token *toktab;
291 enum cpp_token token;
293 U_CHAR *tok_start, *tok_end;
298 old_written = CPP_WRITTEN (pfile);
299 cpp_skip_hspace (pfile);
300 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
302 return parse_number (pfile,
303 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
311 token = cpp_get_token (pfile);
312 tok_start = pfile->token_buffer + old_written;
313 tok_end = CPP_PWRITTEN (pfile);
314 pfile->limit = tok_start;
317 case CPP_EOF: /* Should not happen ... */
322 if (CPP_BUFFER (pfile)->fname != NULL)
328 case CPP_HSPACE: case CPP_COMMENT:
331 return parse_number (pfile, tok_start, tok_end - tok_start);
333 cpp_error (pfile, "string constants not allowed in #if expressions");
337 /* This code for reading a character constant
338 handles multicharacter constants and wide characters.
339 It is mostly copied from c-lex.c. */
341 register int result = 0;
342 register num_chars = 0;
343 unsigned width = MAX_CHAR_TYPE_SIZE;
346 U_CHAR *ptr = tok_start;
347 #ifdef MULTIBYTE_CHARS
348 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
350 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
357 width = MAX_WCHAR_TYPE_SIZE;
358 #ifdef MULTIBYTE_CHARS
359 max_chars = MB_CUR_MAX;
365 max_chars = MAX_LONG_TYPE_SIZE / width;
368 while (ptr < tok_end && ((c = *ptr++) != '\''))
372 c = cpp_parse_escape (pfile, &ptr);
373 if (width < HOST_BITS_PER_INT
374 && (unsigned) c >= (1 << width))
376 "escape sequence out of range for character");
381 /* Merge character into result; ignore excess chars. */
382 if (num_chars < max_chars + 1)
384 if (width < HOST_BITS_PER_INT)
385 result = (result << width) | (c & ((1 << width) - 1));
388 token_buffer[num_chars - 1] = c;
392 token_buffer[num_chars] = 0;
395 cpp_error (pfile, "malformatted character constant");
396 else if (num_chars == 0)
397 cpp_error (pfile, "empty character constant");
398 else if (num_chars > max_chars)
400 num_chars = max_chars;
401 cpp_error (pfile, "character constant too long");
403 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
404 cpp_warning (pfile, "multi-character character constant");
406 /* If char type is signed, sign-extend the constant. */
409 int num_bits = num_chars * width;
411 if (cpp_lookup (pfile, "__CHAR_UNSIGNED__",
412 sizeof ("__CHAR_UNSIGNED__")-1, -1)
413 || ((result >> (num_bits - 1)) & 1) == 0)
415 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
418 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
422 #ifdef MULTIBYTE_CHARS
423 /* Set the initial shift state and convert the next sequence. */
425 /* In all locales L'\0' is zero and mbtowc will return zero,
428 || (num_chars == 1 && token_buffer[0] != '\0'))
431 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
432 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
435 cpp_warning (pfile,"Ignoring invalid multibyte character");
442 /* This is always a signed type. */
449 return parse_number (pfile, "0", 0);
452 /* See if it is a special token of length 2. */
453 if (tok_start + 2 == tok_end)
455 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
456 if (tok_start[0] == toktab->operator[0]
457 && tok_start[1] == toktab->operator[1])
459 if (toktab->token == ERROR)
461 char *buf = (char *) alloca (40);
462 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
463 cpp_error (pfile, buf);
465 op.op = toktab->token;
476 /* Parse a C escape sequence. STRING_PTR points to a variable
477 containing a pointer to the string to parse. That pointer
478 is updated past the characters we use. The value of the
479 escape sequence is returned.
481 A negative value means the sequence \ newline was seen,
482 which is supposed to be equivalent to nothing at all.
484 If \ is followed by a null character, we return a negative
485 value and leave the string pointer pointing at the null character.
487 If \ is followed by 000, we return 0 and leave the string pointer
488 after the zeros. A value of 0 does not mean end of string. */
495 register int c = *(*string_ptr)++;
504 if (CPP_PEDANTIC (pfile))
505 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
510 return TARGET_NEWLINE;
532 register int i = c - '0';
533 register int count = 0;
536 c = *(*string_ptr)++;
537 if (c >= '0' && c <= '7')
538 i = (i << 3) + c - '0';
545 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
547 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
549 "octal character constant does not fit in a byte");
555 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
558 c = *(*string_ptr)++;
559 if (c >= '0' && c <= '9')
561 else if (c >= 'a' && c <= 'f')
562 digit = c - 'a' + 10;
563 else if (c >= 'A' && c <= 'F')
564 digit = c - 'A' + 10;
570 overflow |= i ^ (i << 4 >> 4);
571 i = (i << 4) + digit;
575 cpp_error (pfile, "\\x used with no following hex digits");
576 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
578 i &= (1 << BITS_PER_UNIT) - 1;
580 "hex character constant does not fit in a byte");
593 if (CPP_PEDANTIC (pfile))
594 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
604 if (b >= HOST_BITS_PER_LONG)
606 if (! unsignedp && a != 0)
607 integer_overflow (pfile);
611 return (unsigned long) a << b;
616 integer_overflow (pfile);
628 if (b >= HOST_BITS_PER_LONG)
629 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
631 return (unsigned long) a >> b;
636 /* These priorities are all even, so we can handle associatively. */
637 #define PAREN_INNER_PRIO 0
639 #define COND_PRIO (COMMA_PRIO+2)
640 #define OROR_PRIO (COND_PRIO+2)
641 #define ANDAND_PRIO (OROR_PRIO+2)
642 #define OR_PRIO (ANDAND_PRIO+2)
643 #define XOR_PRIO (OR_PRIO+2)
644 #define AND_PRIO (XOR_PRIO+2)
645 #define EQUAL_PRIO (AND_PRIO+2)
646 #define LESS_PRIO (EQUAL_PRIO+2)
647 #define SHIFT_PRIO (LESS_PRIO+2)
648 #define PLUS_PRIO (SHIFT_PRIO+2)
649 #define MUL_PRIO (PLUS_PRIO+2)
650 #define UNARY_PRIO (MUL_PRIO+2)
651 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
653 #define COMPARE(OP) \
655 top->value = (unsigned1 || unsigned2) ? (unsigned long) v1 OP v2 : (v1 OP v2)
657 /* Parse and evaluate a C expression, reading from PFILE.
658 Returns the value of the expression. */
664 /* The implementation is an operator precedence parser,
665 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
667 The stack base is 'stack', and the current stack pointer is 'top'.
668 There is a stack element for each operator (only),
669 and the most recently pushed operator is 'top->op'.
670 An operand (value) is stored in the 'value' field of the stack
671 element of the operator that precedes it.
672 In that case the 'flags' field has the HAVE_VALUE flag set. */
674 #define INIT_STACK_SIZE 20
675 struct operation init_stack[INIT_STACK_SIZE];
676 struct operation *stack = init_stack;
677 struct operation *limit = stack + INIT_STACK_SIZE;
678 register struct operation *top = stack;
689 op = cpp_lex (pfile);
691 /* See if the token is an operand, in which case go to set_value.
692 If the token is an operator, figure out its left and right
693 priorities, and then goto maybe_reduce. */
698 top->value = 0, top->unsignedp = 0;
701 top->value = op.value;
702 top->unsignedp = op.unsignedp;
705 lprio = 0; goto maybe_reduce;
707 /* Is this correct if unary ? FIXME */
708 flags = RIGHT_OPERAND_REQUIRED;
709 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
711 flags = RIGHT_OPERAND_REQUIRED;
712 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
713 case '*': case '/': case '%':
714 lprio = MUL_PRIO; goto binop;
715 case '<': case '>': case LEQ: case GEQ:
716 lprio = LESS_PRIO; goto binop;
717 case EQUAL: case NOTEQUAL:
718 lprio = EQUAL_PRIO; goto binop;
720 lprio = SHIFT_PRIO; goto binop;
721 case '&': lprio = AND_PRIO; goto binop;
722 case '^': lprio = XOR_PRIO; goto binop;
723 case '|': lprio = OR_PRIO; goto binop;
724 case ANDAND: lprio = ANDAND_PRIO; goto binop;
725 case OROR: lprio = OROR_PRIO; goto binop;
727 lprio = COMMA_PRIO; goto binop;
729 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
732 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
735 lprio = COND_PRIO; rprio = COND_PRIO;
738 lprio = COND_PRIO + 1; rprio = COND_PRIO;
741 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
745 cpp_error (pfile, "invalid character in #if");
750 /* Push a value onto the stack. */
751 if (top->flags & HAVE_VALUE)
753 cpp_error (pfile, "syntax error in #if");
756 top->flags |= HAVE_VALUE;
760 /* Push an operator, and check if we can reduce now. */
761 while (top->rprio > lprio)
763 long v1 = top[-1].value, v2 = top[0].value;
764 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
766 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
767 && ! (top[0].flags & HAVE_VALUE))
769 cpp_error (pfile, "syntax error - missing left operand");
772 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
773 && ! (top[1].flags & HAVE_VALUE))
775 cpp_error (pfile, "syntax error - missing right operand");
778 /* top[0].value = (top[1].op)(v1, v2);*/
782 if (!(top->flags & HAVE_VALUE))
785 top->unsignedp = unsigned2;
786 top->flags |= HAVE_VALUE;
790 top->value = v1 + v2;
791 top->unsignedp = unsigned1 || unsigned2;
793 && ! possible_sum_sign (v1, v2, top->value))
794 integer_overflow (pfile);
798 if (!(top->flags & HAVE_VALUE))
801 if ((top->value & v2) < 0 && ! unsigned2)
802 integer_overflow (pfile);
803 top->unsignedp = unsigned2;
804 top->flags |= HAVE_VALUE;
808 top->value = v1 - v2;
809 top->unsignedp = unsigned1 || unsigned2;
811 && ! possible_sum_sign (top->value, v2, v1))
812 integer_overflow (pfile);
816 top->unsignedp = unsigned1 || unsigned2;
818 top->value = (unsigned long) v1 * v2;
821 top->value = v1 * v2;
823 && (top->value / v1 != v2
824 || (top->value & v1 & v2) < 0))
825 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);
847 cpp_error (pfile, "division by zero in #if");
850 top->unsignedp = unsigned1 || unsigned2;
852 top->value = (unsigned long) v1 % v2;
854 top->value = v1 % v2;
857 if (top->flags & HAVE_VALUE)
859 cpp_error (pfile, "syntax error");
864 top->flags |= HAVE_VALUE;
867 if (top->flags & HAVE_VALUE)
869 cpp_error (pfile, "syntax error");
873 top->unsignedp = unsigned2;
874 top->flags |= HAVE_VALUE;
876 case '<': COMPARE(<); break;
877 case '>': COMPARE(>); break;
878 case LEQ: COMPARE(<=); break;
879 case GEQ: COMPARE(>=); break;
881 top->value = (v1 == v2);
885 top->value = (v1 != v2);
889 top->unsignedp = unsigned1;
890 if (v2 < 0 && ! unsigned2)
891 top->value = right_shift (pfile, v1, unsigned1, -v2);
893 top->value = left_shift (pfile, v1, unsigned1, v2);
896 top->unsignedp = unsigned1;
897 if (v2 < 0 && ! unsigned2)
898 top->value = left_shift (pfile, v1, unsigned1, -v2);
900 top->value = right_shift (pfile, v1, unsigned1, v2);
902 #define LOGICAL(OP) \
903 top->value = v1 OP v2;\
904 top->unsignedp = unsigned1 || unsigned2;
905 case '&': LOGICAL(&); break;
906 case '^': LOGICAL(^); break;
907 case '|': LOGICAL(|); break;
909 top->value = v1 && v2; top->unsignedp = 0; break;
911 top->value = v1 || v2; top->unsignedp = 0; break;
913 if (CPP_PEDANTIC (pfile))
914 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
916 top->unsignedp = unsigned2;
919 cpp_error (pfile, "syntax error in #if");
922 if (top[0].op != '?')
925 "syntax error ':' without preceding '?'");
928 else if (! (top[1].flags & HAVE_VALUE)
929 || !(top[-1].flags & HAVE_VALUE)
930 || !(top[0].flags & HAVE_VALUE))
932 cpp_error (pfile, "bad syntax for ?: operator");
938 top->value = top->value ? v1 : v2;
939 top->unsignedp = unsigned1 || unsigned2;
943 if ((top[1].flags & HAVE_VALUE)
944 || ! (top[0].flags & HAVE_VALUE)
946 || (top[-1].flags & HAVE_VALUE))
948 cpp_error (pfile, "mismatched parentheses in #if");
955 top->unsignedp = unsigned1;
956 top->flags |= HAVE_VALUE;
961 top[1].op >= ' ' && top[1].op <= '~'
962 ? "unimplemented operator '%c'\n"
963 : "unimplemented operator '\\%03o'\n",
970 cpp_error (pfile, "internal error in #if expression");
971 if (stack != init_stack)
977 /* Check for and handle stack overflow. */
980 struct operation *new_stack;
981 int old_size = (char*)limit - (char*)stack;
982 int new_size = 2 * old_size;
983 if (stack != init_stack)
984 new_stack = (struct operation*) Safe_realloc (stack, new_size);
987 new_stack = (struct operation*) Safe_malloc (new_size);
988 bcopy ((char *) stack, (char *) new_stack, old_size);
991 top = (struct operation*)((char*) new_stack + old_size);
992 limit = (struct operation*)((char*) new_stack + new_size);
1000 if (stack != init_stack)
1002 skip_rest_of_line (pfile);