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, 675 Mass Ave, Cambridge, MA 02139, USA.
18 In other words, you are welcome to use, share and improve this program.
19 You are forbidden to forbid anyone else to use, share and improve
20 what you give them. Help stamp out software-hoarding!
22 Adapted from expread.y of GDB by Paul Rubin, July 1986. */
24 /* Parse a C expression from text in a string */
29 /* #define YYDEBUG 1 */
31 #ifdef MULTIBYTE_CHARS
38 typedef unsigned char U_CHAR;
40 /* This is used for communicating lists of keywords with cccp.c. */
48 /* Define a generic NULL if one hasn't already been defined. */
55 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
56 #define GENERIC_PTR void *
58 #define GENERIC_PTR char *
62 /* Find the largest host integer type and set its size and type. */
64 #ifndef HOST_BITS_PER_WIDE_INT
66 #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
67 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
68 #define HOST_WIDE_INT long
70 #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
71 #define HOST_WIDE_INT int
77 #define NULL_PTR ((GENERIC_PTR)0)
82 HOST_WIDE_INT expression_value;
84 static jmp_buf parse_return_error;
86 /* Nonzero means count most punctuation as part of a name. */
87 static int keyword_parsing = 0;
89 /* some external tables of character types */
90 extern unsigned char is_idstart[], is_idchar[], is_hor_space[];
92 extern char *xmalloc ();
94 /* Flag for -pedantic. */
97 /* Flag for -traditional. */
98 extern int traditional;
100 #ifndef CHAR_TYPE_SIZE
101 #define CHAR_TYPE_SIZE BITS_PER_UNIT
104 #ifndef INT_TYPE_SIZE
105 #define INT_TYPE_SIZE BITS_PER_WORD
108 #ifndef LONG_TYPE_SIZE
109 #define LONG_TYPE_SIZE BITS_PER_WORD
112 #ifndef WCHAR_TYPE_SIZE
113 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
116 #ifndef MAX_CHAR_TYPE_SIZE
117 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
120 #ifndef MAX_INT_TYPE_SIZE
121 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
124 #ifndef MAX_LONG_TYPE_SIZE
125 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
128 #ifndef MAX_WCHAR_TYPE_SIZE
129 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
132 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
133 number with SUM's sign, where A, B, and SUM are all C integers. */
134 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
136 static void integer_overflow ();
137 static long left_shift ();
138 static long right_shift ();
142 struct constant {long value; int unsignedp;} integer;
143 struct name {U_CHAR *address; int length;} name;
144 struct arglist *keywords;
147 %type <integer> exp exp1 start
148 %type <keywords> keywords
149 %token <integer> INT CHAR
151 %token <integer> ERROR
161 %left '<' '>' LEQ GEQ
172 { expression_value = $1.value; }
175 /* Expressions, including the comma operator. */
179 pedwarn ("comma operator in operand of `#if'");
183 /* Expressions, not including the comma operator. */
184 exp : '-' exp %prec UNARY
185 { $$.value = - $2.value;
186 if (($$.value & $2.value) < 0 && ! $2.unsignedp)
188 $$.unsignedp = $2.unsignedp; }
189 | '!' exp %prec UNARY
190 { $$.value = ! $2.value;
192 | '+' exp %prec UNARY
194 | '~' exp %prec UNARY
195 { $$.value = ~ $2.value;
196 $$.unsignedp = $2.unsignedp; }
198 { $$.value = check_assertion ($2.address, $2.length,
202 { keyword_parsing = 1; }
204 { $$.value = check_assertion ($2.address, $2.length,
212 /* Binary operators in order of decreasing precedence. */
214 { $$.unsignedp = $1.unsignedp || $3.unsignedp;
216 $$.value = (unsigned long) $1.value * $3.value;
219 $$.value = $1.value * $3.value;
221 && ($$.value / $1.value != $3.value
222 || ($$.value & $1.value & $3.value) < 0))
228 error ("division by zero in #if");
231 $$.unsignedp = $1.unsignedp || $3.unsignedp;
233 $$.value = (unsigned long) $1.value / $3.value;
236 $$.value = $1.value / $3.value;
237 if (($$.value & $1.value & $3.value) < 0)
243 error ("division by zero in #if");
246 $$.unsignedp = $1.unsignedp || $3.unsignedp;
248 $$.value = (unsigned long) $1.value % $3.value;
250 $$.value = $1.value % $3.value; }
252 { $$.value = $1.value + $3.value;
253 $$.unsignedp = $1.unsignedp || $3.unsignedp;
255 && ! possible_sum_sign ($1.value, $3.value,
257 integer_overflow (); }
259 { $$.value = $1.value - $3.value;
260 $$.unsignedp = $1.unsignedp || $3.unsignedp;
262 && ! possible_sum_sign ($$.value, $3.value,
264 integer_overflow (); }
266 { $$.unsignedp = $1.unsignedp;
267 if ($3.value < 0 && ! $3.unsignedp)
268 $$.value = right_shift (&$1, -$3.value);
270 $$.value = left_shift (&$1, $3.value); }
272 { $$.unsignedp = $1.unsignedp;
273 if ($3.value < 0 && ! $3.unsignedp)
274 $$.value = left_shift (&$1, -$3.value);
276 $$.value = right_shift (&$1, $3.value); }
278 { $$.value = ($1.value == $3.value);
281 { $$.value = ($1.value != $3.value);
285 if ($1.unsignedp || $3.unsignedp)
286 $$.value = (unsigned long) $1.value <= $3.value;
288 $$.value = $1.value <= $3.value; }
291 if ($1.unsignedp || $3.unsignedp)
292 $$.value = (unsigned long) $1.value >= $3.value;
294 $$.value = $1.value >= $3.value; }
297 if ($1.unsignedp || $3.unsignedp)
298 $$.value = (unsigned long) $1.value < $3.value;
300 $$.value = $1.value < $3.value; }
303 if ($1.unsignedp || $3.unsignedp)
304 $$.value = (unsigned long) $1.value > $3.value;
306 $$.value = $1.value > $3.value; }
308 { $$.value = $1.value & $3.value;
309 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
311 { $$.value = $1.value ^ $3.value;
312 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
314 { $$.value = $1.value | $3.value;
315 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
317 { $$.value = ($1.value && $3.value);
320 { $$.value = ($1.value || $3.value);
322 | exp '?' exp ':' exp
323 { $$.value = $1.value ? $3.value : $5.value;
324 $$.unsignedp = $3.unsignedp || $5.unsignedp; }
326 { $$ = yylval.integer; }
328 { $$ = yylval.integer; }
336 | '(' keywords ')' keywords
337 { struct arglist *temp;
338 $$ = (struct arglist *) xmalloc (sizeof (struct arglist));
340 $$->name = (U_CHAR *) "(";
343 while (temp != 0 && temp->next != 0)
345 temp->next = (struct arglist *) xmalloc (sizeof (struct arglist));
346 temp->next->next = $4;
347 temp->next->name = (U_CHAR *) ")";
348 temp->next->length = 1; }
350 { $$ = (struct arglist *) xmalloc (sizeof (struct arglist));
351 $$->name = $1.address;
352 $$->length = $1.length;
357 /* During parsing of a C expression, the pointer to the next character
358 is in this variable. */
362 /* Take care of parsing a number (anything that starts with a digit).
363 Set yylval and return the token type; update lexptr.
364 LEN is the number of characters in it. */
366 /* maybe needs to actually deal with floating point numbers */
372 register char *p = lexptr;
374 register unsigned long n = 0, nd, ULONG_MAX_over_base;
375 register int base = 10;
376 register int len = olen;
377 register int overflow = 0;
378 register int digit, largest_digit = 0;
381 for (c = 0; c < len; c++)
383 /* It's a float since it contains a point. */
384 yyerror ("floating point numbers not allowed in #if expressions");
388 yylval.integer.unsignedp = 0;
390 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
398 ULONG_MAX_over_base = (unsigned long) -1 / base;
400 for (; len > 0; len--) {
403 if (c >= '0' && c <= '9')
405 else if (base == 16 && c >= 'a' && c <= 'f')
406 digit = c - 'a' + 10;
407 else if (base == 16 && c >= 'A' && c <= 'F')
408 digit = c - 'A' + 10;
410 /* `l' means long, and `u' means unsigned. */
412 if (c == 'l' || c == 'L')
415 yyerror ("two `l's in integer constant");
418 else if (c == 'u' || c == 'U')
420 if (yylval.integer.unsignedp)
421 yyerror ("two `u's in integer constant");
422 yylval.integer.unsignedp = 1;
431 /* Don't look for any more digits after the suffixes. */
434 if (largest_digit < digit)
435 largest_digit = digit;
436 nd = n * base + digit;
437 overflow |= ULONG_MAX_over_base < n | nd < n;
442 yyerror ("Invalid number in #if expression");
446 if (base <= largest_digit)
447 warning ("integer constant contains digits beyond the radix");
450 warning ("integer constant out of range");
452 /* If too big to be signed, consider it unsigned. */
453 if ((long) n < 0 && ! yylval.integer.unsignedp)
456 warning ("integer constant is so large that it is unsigned");
457 yylval.integer.unsignedp = 1;
461 yylval.integer.value = n;
470 static struct token tokentab2[] = {
484 /* Read one token, getting characters through lexptr. */
490 register int namelen;
491 register unsigned char *tokstart;
492 register struct token *toktab;
497 tokstart = (unsigned char *) lexptr;
499 /* See if it is a special token of length 2. */
500 if (! keyword_parsing)
501 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
502 if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
504 if (toktab->token == ERROR)
506 char *buf = (char *) alloca (40);
507 sprintf (buf, "`%s' not allowed in operand of `#if'", toktab->operator);
510 return toktab->token;
525 /* Capital L may start a wide-string or wide-character constant. */
526 if (lexptr[1] == '\'')
532 if (lexptr[1] == '"')
536 goto string_constant;
544 if (keyword_parsing) {
545 char *start_ptr = lexptr - 1;
549 c = parse_escape (&lexptr);
553 yylval.name.address = tokstart;
554 yylval.name.length = lexptr - start_ptr;
558 /* This code for reading a character constant
559 handles multicharacter constants and wide characters.
560 It is mostly copied from c-lex.c. */
562 register int result = 0;
563 register num_chars = 0;
564 unsigned width = MAX_CHAR_TYPE_SIZE;
570 width = MAX_WCHAR_TYPE_SIZE;
571 #ifdef MULTIBYTE_CHARS
572 max_chars = MB_CUR_MAX;
578 max_chars = MAX_LONG_TYPE_SIZE / width;
580 token_buffer = (char *) alloca (max_chars + 1);
586 if (c == '\'' || c == EOF)
591 c = parse_escape (&lexptr);
592 if (width < HOST_BITS_PER_INT
593 && (unsigned) c >= (1 << width))
594 pedwarn ("escape sequence out of range for character");
599 /* Merge character into result; ignore excess chars. */
600 if (num_chars < max_chars + 1)
602 if (width < HOST_BITS_PER_INT)
603 result = (result << width) | (c & ((1 << width) - 1));
606 token_buffer[num_chars - 1] = c;
610 token_buffer[num_chars] = 0;
613 error ("malformatted character constant");
614 else if (num_chars == 0)
615 error ("empty character constant");
616 else if (num_chars > max_chars)
618 num_chars = max_chars;
619 error ("character constant too long");
621 else if (num_chars != 1 && ! traditional)
622 warning ("multi-character character constant");
624 /* If char type is signed, sign-extend the constant. */
627 int num_bits = num_chars * width;
629 if (lookup ("__CHAR_UNSIGNED__", sizeof ("__CHAR_UNSIGNED__")-1, -1)
630 || ((result >> (num_bits - 1)) & 1) == 0)
632 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
635 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
639 #ifdef MULTIBYTE_CHARS
640 /* Set the initial shift state and convert the next sequence. */
642 /* In all locales L'\0' is zero and mbtowc will return zero,
645 || (num_chars == 1 && token_buffer[0] != '\0'))
648 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
649 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
652 warning ("Ignoring invalid multibyte character");
655 yylval.integer.value = result;
659 /* This is always a signed type. */
660 yylval.integer.unsignedp = 0;
664 /* some of these chars are invalid in constant expressions;
665 maybe do something about them later */
698 if (keyword_parsing) {
699 char *start_ptr = lexptr;
704 c = parse_escape (&lexptr);
708 yylval.name.address = tokstart;
709 yylval.name.length = lexptr - start_ptr;
712 yyerror ("string constants not allowed in #if expressions");
716 if (c >= '0' && c <= '9' && !keyword_parsing) {
719 c = tokstart[namelen], is_idchar[c] || c == '.';
722 return parse_number (namelen);
725 /* It is a name. See how long it is. */
727 if (keyword_parsing) {
728 for (namelen = 0;; namelen++) {
729 if (is_hor_space[tokstart[namelen]])
731 if (tokstart[namelen] == '(' || tokstart[namelen] == ')')
733 if (tokstart[namelen] == '"' || tokstart[namelen] == '\'')
737 if (!is_idstart[c]) {
738 yyerror ("Invalid token in expression");
742 for (namelen = 0; is_idchar[tokstart[namelen]]; namelen++)
747 yylval.name.address = tokstart;
748 yylval.name.length = namelen;
753 /* Parse a C escape sequence. STRING_PTR points to a variable
754 containing a pointer to the string to parse. That pointer
755 is updated past the characters we use. The value of the
756 escape sequence is returned.
758 A negative value means the sequence \ newline was seen,
759 which is supposed to be equivalent to nothing at all.
761 If \ is followed by a null character, we return a negative
762 value and leave the string pointer pointing at the null character.
764 If \ is followed by 000, we return 0 and leave the string pointer
765 after the zeros. A value of 0 does not mean end of string. */
768 parse_escape (string_ptr)
771 register int c = *(*string_ptr)++;
781 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
786 return TARGET_NEWLINE;
808 register int i = c - '0';
809 register int count = 0;
812 c = *(*string_ptr)++;
813 if (c >= '0' && c <= '7')
814 i = (i << 3) + c - '0';
821 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
823 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
824 warning ("octal character constant does not fit in a byte");
830 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
833 c = *(*string_ptr)++;
834 if (c >= '0' && c <= '9')
836 else if (c >= 'a' && c <= 'f')
837 digit = c - 'a' + 10;
838 else if (c >= 'A' && c <= 'F')
839 digit = c - 'A' + 10;
845 overflow |= i ^ (i << 4 >> 4);
846 i = (i << 4) + digit;
850 yyerror ("\\x used with no following hex digits");
851 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
853 i &= (1 << BITS_PER_UNIT) - 1;
854 warning ("hex character constant does not fit in a byte");
868 longjmp (parse_return_error, 1);
875 pedwarn ("integer overflow in preprocessor expression");
883 if (b >= HOST_BITS_PER_LONG)
885 if (! a->unsignedp && a->value != 0)
889 else if (a->unsignedp)
890 return (unsigned long) a->value << b;
893 long l = a->value << b;
894 if (l >> b != a->value)
905 if (b >= HOST_BITS_PER_LONG)
906 return a->unsignedp ? 0 : a->value >> (HOST_BITS_PER_LONG - 1);
907 else if (a->unsignedp)
908 return (unsigned long) a->value >> b;
910 return a->value >> b;
913 /* This page contains the entry point to this file. */
915 /* Parse STRING as an expression, and complain if this fails
916 to use up all of the contents of STRING. */
917 /* We do not support C comments. They should be removed before
918 this function is called. */
921 parse_c_expression (string)
926 if (lexptr == 0 || *lexptr == 0) {
927 error ("empty #if expression");
928 return 0; /* don't include the #if group */
931 /* if there is some sort of scanning error, just return 0 and assume
932 the parsing routine has printed an error message somewhere.
933 there is surely a better thing to do than this. */
934 if (setjmp (parse_return_error))
938 return 0; /* actually this is never reached
939 the way things stand. */
941 error ("Junk after end of expression.");
943 return expression_value; /* set by yyparse () */
946 #ifdef TEST_EXP_READER
949 /* Main program for testing purposes. */
959 initialize_random_junk ();
962 printf ("enter expression: ");
964 while ((buf[n] = getchar ()) != '\n' && buf[n] != EOF)
969 printf ("parser returned %ld\n", parse_c_expression (buf));
975 /* table to tell if char can be part of a C identifier. */
976 unsigned char is_idchar[256];
977 /* table to tell if char can be first char of a c identifier. */
978 unsigned char is_idstart[256];
979 /* table to tell if c is horizontal space. isspace () thinks that
980 newline is space; this is not a good idea for this program. */
981 char is_hor_space[256];
984 * initialize random junk in the hash table and maybe other places
986 initialize_random_junk ()
991 * Set up is_idchar and is_idstart tables. These should be
992 * faster than saying (is_alpha (c) || c == '_'), etc.
993 * Must do set up these things before calling any routines tthat
996 for (i = 'a'; i <= 'z'; i++) {
997 ++is_idchar[i - 'a' + 'A'];
999 ++is_idstart[i - 'a' + 'A'];
1002 for (i = '0'; i <= '9'; i++)
1006 #if DOLLARS_IN_IDENTIFIERS
1011 /* horizontal space table */
1012 ++is_hor_space[' '];
1013 ++is_hor_space['\t'];
1018 printf ("error: %s\n", msg);
1023 printf ("warning: %s\n", msg);
1027 lookup (name, len, hash)
1032 return (DEFAULT_SIGNED_CHAR) ? 0 : ((struct hashnode *) -1);