* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / tradcif.y
blob1f8eb2858fcec903641caaf5e7dcaeff0e249213
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
11 later version.
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 */
25 #include "config.h"
26 #include "system.h"
27 #include "tradcpp.h"
28 #include <setjmp.h>
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;
45 %union {
46 struct constant {long value; int unsignedp;} integer;
47 int voidval;
48 char *sval;
51 %type <integer> exp exp1 start
52 %token <integer> INT CHAR
53 %token <sval> NAME
54 %token <integer> ERROR
56 %right '?' ':'
57 %left ','
58 %left OR
59 %left AND
60 %left '|'
61 %left '^'
62 %left '&'
63 %left EQUAL NOTEQUAL
64 %left '<' '>' LEQ GEQ
65 %left LSH RSH
66 %left '+' '-'
67 %left '*' '/' '%'
68 %right UNARY
70 /* %expect 40 */
74 start : exp1
75 { expression_value = $1.value; }
78 /* Expressions, including the comma operator. */
79 exp1 : exp
80 | exp1 ',' exp
81 { $$ = $3; }
84 /* Expressions, not including the comma operator. */
85 exp : '-' exp %prec UNARY
86 { $$.value = - $2.value;
87 $$.unsignedp = $2.unsignedp; }
88 | '!' exp %prec UNARY
89 { $$.value = ! $2.value;
90 $$.unsignedp = 0; }
91 | '+' exp %prec UNARY
92 { $$ = $2; }
93 | '~' exp %prec UNARY
94 { $$.value = ~ $2.value;
95 $$.unsignedp = $2.unsignedp; }
96 | '(' exp1 ')'
97 { $$ = $2; }
100 /* Binary operators in order of decreasing precedence. */
101 exp : exp '*' exp
102 { $$.unsignedp = $1.unsignedp || $3.unsignedp;
103 if ($$.unsignedp)
104 $$.value = (unsigned) $1.value * $3.value;
105 else
106 $$.value = $1.value * $3.value; }
107 | exp '/' exp
108 { if ($3.value == 0)
110 error ("division by zero in #if");
111 $3.value = 1;
113 $$.unsignedp = $1.unsignedp || $3.unsignedp;
114 if ($$.unsignedp)
115 $$.value = (unsigned) $1.value / $3.value;
116 else
117 $$.value = $1.value / $3.value; }
118 | exp '%' exp
119 { if ($3.value == 0)
121 error ("division by zero in #if");
122 $3.value = 1;
124 $$.unsignedp = $1.unsignedp || $3.unsignedp;
125 if ($$.unsignedp)
126 $$.value = (unsigned) $1.value % $3.value;
127 else
128 $$.value = $1.value % $3.value; }
129 | exp '+' exp
130 { $$.value = $1.value + $3.value;
131 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
132 | exp '-' exp
133 { $$.value = $1.value - $3.value;
134 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
135 | exp LSH exp
136 { $$.unsignedp = $1.unsignedp;
137 if ($$.unsignedp)
138 $$.value = (unsigned) $1.value << $3.value;
139 else
140 $$.value = $1.value << $3.value; }
141 | exp RSH exp
142 { $$.unsignedp = $1.unsignedp;
143 if ($$.unsignedp)
144 $$.value = (unsigned) $1.value >> $3.value;
145 else
146 $$.value = $1.value >> $3.value; }
147 | exp EQUAL exp
148 { $$.value = ($1.value == $3.value);
149 $$.unsignedp = 0; }
150 | exp NOTEQUAL exp
151 { $$.value = ($1.value != $3.value);
152 $$.unsignedp = 0; }
153 | exp LEQ exp
154 { $$.unsignedp = 0;
155 if ($1.unsignedp || $3.unsignedp)
156 $$.value =
157 (unsigned) $1.value <= (unsigned) $3.value;
158 else
159 $$.value = $1.value <= $3.value; }
160 | exp GEQ exp
161 { $$.unsignedp = 0;
162 if ($1.unsignedp || $3.unsignedp)
163 $$.value =
164 (unsigned) $1.value >= (unsigned) $3.value;
165 else
166 $$.value = $1.value >= $3.value; }
167 | exp '<' exp
168 { $$.unsignedp = 0;
169 if ($1.unsignedp || $3.unsignedp)
170 $$.value =
171 (unsigned) $1.value < (unsigned) $3.value;
172 else
173 $$.value = $1.value < $3.value; }
174 | exp '>' exp
175 { $$.unsignedp = 0;
176 if ($1.unsignedp || $3.unsignedp)
177 $$.value =
178 (unsigned) $1.value > (unsigned) $3.value;
179 else
180 $$.value = $1.value > $3.value; }
181 | exp '&' exp
182 { $$.value = $1.value & $3.value;
183 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
184 | exp '^' exp
185 { $$.value = $1.value ^ $3.value;
186 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
187 | exp '|' exp
188 { $$.value = $1.value | $3.value;
189 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
190 | exp AND exp
191 { $$.value = ($1.value && $3.value);
192 $$.unsignedp = 0; }
193 | exp OR exp
194 { $$.value = ($1.value || $3.value);
195 $$.unsignedp = 0; }
196 | exp '?' exp ':' exp
197 { $$.value = $1.value ? $3.value : $5.value;
198 $$.unsignedp = $3.unsignedp || $5.unsignedp; }
199 | INT
200 { $$ = yylval.integer; }
201 | CHAR
202 { $$ = yylval.integer; }
203 | NAME
204 { $$.value = 0;
205 $$.unsignedp = 0; }
206 | '#' { $$.value =
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 */
217 static int
218 parse_number (olen)
219 int olen;
221 const char *p = lexptr;
222 long n = 0;
223 int c;
224 int base = 10;
225 int len = olen;
227 for (c = 0; c < len; c++)
228 if (p[c] == '.') {
229 /* It's a float since it contains a point. */
230 yyerror ("floating point numbers not allowed in #if expressions");
231 return ERROR;
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))) {
239 p += 2;
240 base = 16;
241 len -= 2;
243 else if (*p == '0')
244 base = 8;
246 while (len > 0) {
247 c = *p++;
248 len--;
249 if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
251 if (c >= '0' && c <= '9') {
252 n *= base;
253 n += c - '0';
254 } else if (base == 16 && c >= 'a' && c <= 'f') {
255 n *= base;
256 n += c - 'a' + 10;
257 } else {
258 /* `l' means long, and `u' means unsigned. */
259 while (1) {
260 if (c == 'l' || c == 'L')
262 else if (c == 'u' || c == 'U')
263 yylval.integer.unsignedp = 1;
264 else
265 break;
267 if (len == 0)
268 break;
269 c = *p++;
270 len--;
272 /* Don't look for any more digits after the suffixes. */
273 break;
277 if (len != 0) {
278 yyerror ("Invalid number in #if expression");
279 return ERROR;
282 lexptr = p;
283 yylval.integer.value = n;
284 return INT;
287 struct token {
288 const char *const operator;
289 const int token;
292 #ifndef NULL
293 #define NULL 0
294 #endif
296 static const struct token tokentab2[] = {
297 {"&&", AND},
298 {"||", OR},
299 {"<<", LSH},
300 {">>", RSH},
301 {"==", EQUAL},
302 {"!=", NOTEQUAL},
303 {"<=", LEQ},
304 {">=", GEQ},
305 {NULL, ERROR}
308 /* Read one token, getting characters through lexptr. */
310 static int
311 yylex ()
313 int c;
314 int namelen;
315 const char *tokstart;
316 const struct token *toktab;
318 retry:
320 tokstart = lexptr;
321 c = *tokstart;
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]) {
325 lexptr += 2;
326 return toktab->token;
329 switch (c) {
330 case 0:
331 return 0;
333 case ' ':
334 case '\t':
335 case '\r':
336 case '\n':
337 lexptr++;
338 goto retry;
340 case '\'':
341 lexptr++;
342 c = *lexptr++;
343 if (c == '\\')
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);
352 else
353 yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
356 yylval.integer.unsignedp = 0;
357 c = *lexptr++;
358 if (c != '\'') {
359 yyerror ("Invalid character constant in #if");
360 return ERROR;
363 return CHAR;
365 /* some of these chars are invalid in constant expressions;
366 maybe do something about them later */
367 case '/':
368 case '+':
369 case '-':
370 case '*':
371 case '%':
372 case '|':
373 case '&':
374 case '^':
375 case '~':
376 case '!':
377 case '@':
378 case '<':
379 case '>':
380 case '(':
381 case ')':
382 case '[':
383 case ']':
384 case '.':
385 case '?':
386 case ':':
387 case '=':
388 case '{':
389 case '}':
390 case ',':
391 case '#':
392 lexptr++;
393 return c;
395 case '"':
396 yyerror ("double quoted strings not allowed in #if expressions");
397 return ERROR;
399 if (c >= '0' && c <= '9') {
400 /* It's a number */
401 for (namelen = 0;
402 c = tokstart[namelen], is_idchar (c) || c == '.';
403 namelen++)
405 return parse_number (namelen);
408 if (!is_idstart (c)) {
409 yyerror ("Invalid token in expression");
410 return ERROR;
413 /* It is a name. See how long it is. */
415 for (namelen = 0;
416 is_idchar (tokstart[namelen]);
417 namelen++)
420 lexptr += namelen;
421 return NAME;
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. */
439 static int
440 parse_escape (string_ptr)
441 const char **string_ptr;
443 int c = *(*string_ptr)++;
444 switch (c)
446 case 'a':
447 return TARGET_BELL;
448 case 'b':
449 return TARGET_BS;
450 case 'e':
451 return 033;
452 case 'f':
453 return TARGET_FF;
454 case 'n':
455 return TARGET_NEWLINE;
456 case 'r':
457 return TARGET_CR;
458 case 't':
459 return TARGET_TAB;
460 case 'v':
461 return TARGET_VT;
462 case '\n':
463 return -2;
464 case 0:
465 (*string_ptr)--;
466 return 0;
467 case '^':
468 c = *(*string_ptr)++;
469 if (c == '\\')
470 c = parse_escape (string_ptr);
471 if (c == '?')
472 return 0177;
473 return (c & 0200) | (c & 037);
475 case '0':
476 case '1':
477 case '2':
478 case '3':
479 case '4':
480 case '5':
481 case '6':
482 case '7':
484 int i = c - '0';
485 int count = 0;
486 while (++count < 3)
488 c = *(*string_ptr)++;
489 if (c >= '0' && c <= '7')
490 i = (i << 3) + c - '0';
491 else
493 (*string_ptr)--;
494 break;
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");
502 return i;
504 case 'x':
506 int i = 0;
507 for (;;)
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;
516 else
518 (*string_ptr)--;
519 break;
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");
527 return i;
529 default:
530 return c;
534 static void
535 yyerror (s)
536 const char *s;
538 error ("%s", s);
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)
551 const char *string;
553 lexptr = 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))
564 return 0;
566 if (yyparse ())
567 return 0; /* actually this is never reached
568 the way things stand. */
569 if (*lexptr)
570 error ("Junk after end of expression.");
572 return expression_value; /* set by yyparse () */