1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 2000, 2001 Free Software Foundation.
3 Adapted from expread.y of GDB by Paul Rubin, July 1986.
4 Adapted to ANSI C, Richard Stallman, Jan 1987
5 Dusted off, polished, and adapted for use as traditional
6 preprocessor only, Zack Weinberg, Jul 2000
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Parse a C expression from text in a string */
30 static int yylex PARAMS ((void));
31 static void yyerror PARAMS ((const char *msgid)) ATTRIBUTE_NORETURN;
33 static int parse_number PARAMS ((int));
34 static int parse_escape PARAMS ((const char **));
36 static int expression_value;
37 static jmp_buf parse_return_error;
39 /* During parsing of a C expression, the pointer to the next
40 character is in this variable. */
42 static const char *lexptr;
46 struct constant {long value; int unsignedp;} integer;
51 %type <integer> exp exp1 start
52 %token <integer> INT CHAR
54 %token <integer> ERROR
75 { expression_value = $1.value; }
78 /* Expressions, including the comma operator. */
84 /* Expressions, not including the comma operator. */
85 exp : '-' exp %prec UNARY
86 { $$.value = - $2.value;
87 $$.unsignedp = $2.unsignedp; }
89 { $$.value = ! $2.value;
94 { $$.value = ~ $2.value;
95 $$.unsignedp = $2.unsignedp; }
100 /* Binary operators in order of decreasing precedence. */
102 { $$.unsignedp = $1.unsignedp || $3.unsignedp;
104 $$.value = (unsigned) $1.value * $3.value;
106 $$.value = $1.value * $3.value; }
110 error ("division by zero in #if");
113 $$.unsignedp = $1.unsignedp || $3.unsignedp;
115 $$.value = (unsigned) $1.value / $3.value;
117 $$.value = $1.value / $3.value; }
121 error ("division by zero in #if");
124 $$.unsignedp = $1.unsignedp || $3.unsignedp;
126 $$.value = (unsigned) $1.value % $3.value;
128 $$.value = $1.value % $3.value; }
130 { $$.value = $1.value + $3.value;
131 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
133 { $$.value = $1.value - $3.value;
134 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
136 { $$.unsignedp = $1.unsignedp;
138 $$.value = (unsigned) $1.value << $3.value;
140 $$.value = $1.value << $3.value; }
142 { $$.unsignedp = $1.unsignedp;
144 $$.value = (unsigned) $1.value >> $3.value;
146 $$.value = $1.value >> $3.value; }
148 { $$.value = ($1.value == $3.value);
151 { $$.value = ($1.value != $3.value);
155 if ($1.unsignedp || $3.unsignedp)
157 (unsigned) $1.value <= (unsigned) $3.value;
159 $$.value = $1.value <= $3.value; }
162 if ($1.unsignedp || $3.unsignedp)
164 (unsigned) $1.value >= (unsigned) $3.value;
166 $$.value = $1.value >= $3.value; }
169 if ($1.unsignedp || $3.unsignedp)
171 (unsigned) $1.value < (unsigned) $3.value;
173 $$.value = $1.value < $3.value; }
176 if ($1.unsignedp || $3.unsignedp)
178 (unsigned) $1.value > (unsigned) $3.value;
180 $$.value = $1.value > $3.value; }
182 { $$.value = $1.value & $3.value;
183 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
185 { $$.value = $1.value ^ $3.value;
186 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
188 { $$.value = $1.value | $3.value;
189 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
191 { $$.value = ($1.value && $3.value);
194 { $$.value = ($1.value || $3.value);
196 | exp '?' exp ':' exp
197 { $$.value = $1.value ? $3.value : $5.value;
198 $$.unsignedp = $3.unsignedp || $5.unsignedp; }
200 { $$ = yylval.integer; }
202 { $$ = yylval.integer; }
207 test_assertion ((unsigned char **) &lexptr); }
211 /* Take care of parsing a number (anything that starts with a digit).
212 Set yylval and return the token type; update lexptr.
213 LEN is the number of characters in it. */
215 /* maybe needs to actually deal with floating point numbers */
221 register const char *p = lexptr;
224 register int base = 10;
225 register int len = olen;
227 for (c = 0; c < len; c++)
229 /* It's a float since it contains a point. */
230 yyerror ("floating point numbers not allowed in #if expressions");
234 /* Traditionally, all numbers are signed. However, we make it
235 unsigned if requested with a suffix. */
236 yylval.integer.unsignedp = 0;
238 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
249 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
251 if (c >= '0' && c <= '9') {
254 } else if (base == 16 && c >= 'a' && c <= 'f') {
258 /* `l' means long, and `u' means unsigned. */
260 if (c == 'l' || c == 'L')
262 else if (c == 'u' || c == 'U')
263 yylval.integer.unsignedp = 1;
272 /* Don't look for any more digits after the suffixes. */
278 yyerror ("Invalid number in #if expression");
283 yylval.integer.value = n;
288 const char *operator;
296 static struct token tokentab2[] = {
308 /* Read one token, getting characters through lexptr. */
314 register int namelen;
315 register const char *tokstart;
316 register struct token *toktab;
322 /* See if it is a special token of length 2. */
323 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
324 if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
326 return toktab->token;
344 c = parse_escape (&lexptr);
346 /* Sign-extend the constant if chars are signed on target machine. */
348 if (lookup ((const unsigned char *)"__CHAR_UNSIGNED__",
349 sizeof ("__CHAR_UNSIGNED__")-1, -1)
350 || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
351 yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
353 yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
356 yylval.integer.unsignedp = 0;
359 yyerror ("Invalid character constant in #if");
365 /* some of these chars are invalid in constant expressions;
366 maybe do something about them later */
396 yyerror ("double quoted strings not allowed in #if expressions");
399 if (c >= '0' && c <= '9') {
402 c = tokstart[namelen], is_idchar (c) || c == '.';
405 return parse_number (namelen);
408 if (!is_idstart (c)) {
409 yyerror ("Invalid token in expression");
413 /* It is a name. See how long it is. */
416 is_idchar (tokstart[namelen]);
425 /* Parse a C escape sequence. STRING_PTR points to a variable
426 containing a pointer to the string to parse. That pointer
427 is updated past the characters we use. The value of the
428 escape sequence is returned.
430 A negative value means the sequence \ newline was seen,
431 which is supposed to be equivalent to nothing at all.
433 If \ is followed by a null character, we return a negative
434 value and leave the string pointer pointing at the null character.
436 If \ is followed by 000, we return 0 and leave the string pointer
437 after the zeros. A value of 0 does not mean end of string. */
440 parse_escape (string_ptr)
441 const char **string_ptr;
443 register int c = *(*string_ptr)++;
455 return TARGET_NEWLINE;
468 c = *(*string_ptr)++;
470 c = parse_escape (string_ptr);
473 return (c & 0200) | (c & 037);
484 register int i = c - '0';
485 register int count = 0;
488 c = *(*string_ptr)++;
489 if (c >= '0' && c <= '7')
490 i = (i << 3) + c - '0';
497 if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
499 i &= (1 << CHAR_TYPE_SIZE) - 1;
500 warning ("octal character constant does not fit in a byte");
509 c = *(*string_ptr)++;
510 if (c >= '0' && c <= '9')
511 i = (i << 4) + c - '0';
512 else if (c >= 'a' && c <= 'f')
513 i = (i << 4) + c - 'a' + 10;
514 else if (c >= 'A' && c <= 'F')
515 i = (i << 4) + c - 'A' + 10;
522 if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
524 i &= (1 << BITS_PER_UNIT) - 1;
525 warning ("hex character constant does not fit in a byte");
539 longjmp (parse_return_error, 1);
542 /* This page contains the entry point to this file. */
544 /* Parse STRING as an expression, and complain if this fails
545 to use up all of the contents of STRING. */
546 /* We do not support C comments. They should be removed before
547 this function is called. */
550 parse_c_expression (string)
555 if (lexptr == 0 || *lexptr == 0) {
556 error ("empty #if expression");
557 return 0; /* don't include the #if group */
560 /* if there is some sort of scanning error, just return 0 and assume
561 the parsing routine has printed an error message somewhere.
562 there is surely a better thing to do than this. */
563 if (setjmp (parse_return_error))
567 return 0; /* actually this is never reached
568 the way things stand. */
570 error ("Junk after end of expression.");
572 return expression_value; /* set by yyparse () */