Daily bump.
[official-gcc.git] / gcc / tradcif.y
blob953e2d6706ff5fcbe1a0e861eab657b6b2c8b093
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 "intl.h"
28 #include "tradcpp.h"
29 #include <setjmp.h>
31 static int yylex PARAMS ((void));
32 static void yyerror PARAMS ((const char *msgid)) ATTRIBUTE_NORETURN;
34 static int parse_number PARAMS ((int));
35 static int parse_escape PARAMS ((const char **));
37 static int expression_value;
38 static jmp_buf parse_return_error;
40 /* During parsing of a C expression, the pointer to the next
41 character is in this variable. */
43 static const char *lexptr;
46 %union {
47 struct constant {long value; int unsignedp;} integer;
48 int voidval;
49 char *sval;
52 %type <integer> exp exp1 start
53 %token <integer> INT CHAR
54 %token <sval> NAME
55 %token <integer> ERROR
57 %right '?' ':'
58 %left ','
59 %left OR
60 %left AND
61 %left '|'
62 %left '^'
63 %left '&'
64 %left EQUAL NOTEQUAL
65 %left '<' '>' LEQ GEQ
66 %left LSH RSH
67 %left '+' '-'
68 %left '*' '/' '%'
69 %right UNARY
71 /* %expect 40 */
75 start : exp1
76 { expression_value = $1.value; }
79 /* Expressions, including the comma operator. */
80 exp1 : exp
81 | exp1 ',' exp
82 { $$ = $3; }
85 /* Expressions, not including the comma operator. */
86 exp : '-' exp %prec UNARY
87 { $$.value = - $2.value;
88 $$.unsignedp = $2.unsignedp; }
89 | '!' exp %prec UNARY
90 { $$.value = ! $2.value;
91 $$.unsignedp = 0; }
92 | '+' exp %prec UNARY
93 { $$ = $2; }
94 | '~' exp %prec UNARY
95 { $$.value = ~ $2.value;
96 $$.unsignedp = $2.unsignedp; }
97 | '(' exp1 ')'
98 { $$ = $2; }
101 /* Binary operators in order of decreasing precedence. */
102 exp : exp '*' exp
103 { $$.unsignedp = $1.unsignedp || $3.unsignedp;
104 if ($$.unsignedp)
105 $$.value = (unsigned) $1.value * $3.value;
106 else
107 $$.value = $1.value * $3.value; }
108 | exp '/' exp
109 { if ($3.value == 0)
111 error ("division by zero in #if");
112 $3.value = 1;
114 $$.unsignedp = $1.unsignedp || $3.unsignedp;
115 if ($$.unsignedp)
116 $$.value = (unsigned) $1.value / $3.value;
117 else
118 $$.value = $1.value / $3.value; }
119 | exp '%' exp
120 { if ($3.value == 0)
122 error ("division by zero in #if");
123 $3.value = 1;
125 $$.unsignedp = $1.unsignedp || $3.unsignedp;
126 if ($$.unsignedp)
127 $$.value = (unsigned) $1.value % $3.value;
128 else
129 $$.value = $1.value % $3.value; }
130 | exp '+' exp
131 { $$.value = $1.value + $3.value;
132 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
133 | exp '-' exp
134 { $$.value = $1.value - $3.value;
135 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
136 | exp LSH exp
137 { $$.unsignedp = $1.unsignedp;
138 if ($$.unsignedp)
139 $$.value = (unsigned) $1.value << $3.value;
140 else
141 $$.value = $1.value << $3.value; }
142 | exp RSH exp
143 { $$.unsignedp = $1.unsignedp;
144 if ($$.unsignedp)
145 $$.value = (unsigned) $1.value >> $3.value;
146 else
147 $$.value = $1.value >> $3.value; }
148 | exp EQUAL exp
149 { $$.value = ($1.value == $3.value);
150 $$.unsignedp = 0; }
151 | exp NOTEQUAL exp
152 { $$.value = ($1.value != $3.value);
153 $$.unsignedp = 0; }
154 | exp LEQ exp
155 { $$.unsignedp = 0;
156 if ($1.unsignedp || $3.unsignedp)
157 $$.value =
158 (unsigned) $1.value <= (unsigned) $3.value;
159 else
160 $$.value = $1.value <= $3.value; }
161 | exp GEQ exp
162 { $$.unsignedp = 0;
163 if ($1.unsignedp || $3.unsignedp)
164 $$.value =
165 (unsigned) $1.value >= (unsigned) $3.value;
166 else
167 $$.value = $1.value >= $3.value; }
168 | exp '<' exp
169 { $$.unsignedp = 0;
170 if ($1.unsignedp || $3.unsignedp)
171 $$.value =
172 (unsigned) $1.value < (unsigned) $3.value;
173 else
174 $$.value = $1.value < $3.value; }
175 | exp '>' exp
176 { $$.unsignedp = 0;
177 if ($1.unsignedp || $3.unsignedp)
178 $$.value =
179 (unsigned) $1.value > (unsigned) $3.value;
180 else
181 $$.value = $1.value > $3.value; }
182 | exp '&' exp
183 { $$.value = $1.value & $3.value;
184 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
185 | exp '^' exp
186 { $$.value = $1.value ^ $3.value;
187 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
188 | exp '|' exp
189 { $$.value = $1.value | $3.value;
190 $$.unsignedp = $1.unsignedp || $3.unsignedp; }
191 | exp AND exp
192 { $$.value = ($1.value && $3.value);
193 $$.unsignedp = 0; }
194 | exp OR exp
195 { $$.value = ($1.value || $3.value);
196 $$.unsignedp = 0; }
197 | exp '?' exp ':' exp
198 { $$.value = $1.value ? $3.value : $5.value;
199 $$.unsignedp = $3.unsignedp || $5.unsignedp; }
200 | INT
201 { $$ = yylval.integer; }
202 | CHAR
203 { $$ = yylval.integer; }
204 | NAME
205 { $$.value = 0;
206 $$.unsignedp = 0; }
207 | '#' { $$.value =
208 test_assertion ((unsigned char **) &lexptr); }
212 /* Take care of parsing a number (anything that starts with a digit).
213 Set yylval and return the token type; update lexptr.
214 LEN is the number of characters in it. */
216 /* maybe needs to actually deal with floating point numbers */
218 static int
219 parse_number (olen)
220 int olen;
222 const char *p = lexptr;
223 long n = 0;
224 int c;
225 int base = 10;
226 int len = olen;
228 for (c = 0; c < len; c++)
229 if (p[c] == '.') {
230 /* It's a float since it contains a point. */
231 yyerror ("floating point numbers not allowed in #if expressions");
232 return ERROR;
235 /* Traditionally, all numbers are signed. However, we make it
236 unsigned if requested with a suffix. */
237 yylval.integer.unsignedp = 0;
239 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
240 p += 2;
241 base = 16;
242 len -= 2;
244 else if (*p == '0')
245 base = 8;
247 while (len > 0) {
248 c = *p++;
249 len--;
250 if (ISUPPER (c))
251 c = TOLOWER (c);
253 if (ISDIGIT (c)
254 || (base == 16 && ISXDIGIT (c))) {
255 n = (n * base) + hex_value (c);
256 } else {
257 /* `l' means long, and `u' means unsigned. */
258 while (1) {
259 if (c == 'l' || c == 'L')
261 else if (c == 'u' || c == 'U')
262 yylval.integer.unsignedp = 1;
263 else
264 break;
266 if (len == 0)
267 break;
268 c = *p++;
269 len--;
271 /* Don't look for any more digits after the suffixes. */
272 break;
276 if (len != 0) {
277 yyerror ("invalid number in #if expression");
278 return ERROR;
281 lexptr = p;
282 yylval.integer.value = n;
283 return INT;
286 struct token {
287 const char *const operator;
288 const int token;
291 #ifndef NULL
292 #define NULL 0
293 #endif
295 static const struct token tokentab2[] = {
296 {"&&", AND},
297 {"||", OR},
298 {"<<", LSH},
299 {">>", RSH},
300 {"==", EQUAL},
301 {"!=", NOTEQUAL},
302 {"<=", LEQ},
303 {">=", GEQ},
304 {NULL, ERROR}
307 /* Read one token, getting characters through lexptr. */
309 static int
310 yylex ()
312 int c;
313 int namelen;
314 const char *tokstart;
315 const struct token *toktab;
317 retry:
319 tokstart = lexptr;
320 c = *tokstart;
321 /* See if it is a special token of length 2. */
322 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
323 if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
324 lexptr += 2;
325 return toktab->token;
328 switch (c) {
329 case 0:
330 return 0;
332 case ' ':
333 case '\t':
334 case '\r':
335 case '\n':
336 lexptr++;
337 goto retry;
339 case '\'':
340 lexptr++;
341 c = *lexptr++;
342 if (c == '\\')
343 c = parse_escape (&lexptr);
345 /* Sign-extend the constant if chars are signed on target machine. */
347 if (flag_signed_char == 0
348 || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
349 yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
350 else
351 yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
354 yylval.integer.unsignedp = 0;
355 c = *lexptr++;
356 if (c != '\'') {
357 yyerror ("invalid character constant in #if");
358 return ERROR;
361 return CHAR;
363 /* some of these chars are invalid in constant expressions;
364 maybe do something about them later */
365 case '/':
366 case '+':
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 lexptr++;
391 return c;
393 case '"':
394 yyerror ("double quoted strings not allowed in #if expressions");
395 return ERROR;
397 if (ISDIGIT (c)) {
398 /* It's a number */
399 for (namelen = 0;
400 c = tokstart[namelen], is_idchar (c) || c == '.';
401 namelen++)
403 return parse_number (namelen);
406 if (!is_idstart (c)) {
407 yyerror ("invalid token in expression");
408 return ERROR;
411 /* It is a name. See how long it is. */
413 for (namelen = 0;
414 is_idchar (tokstart[namelen]);
415 namelen++)
418 lexptr += namelen;
419 return NAME;
423 /* Parse a C escape sequence. STRING_PTR points to a variable
424 containing a pointer to the string to parse. That pointer
425 is updated past the characters we use. The value of the
426 escape sequence is returned.
428 A negative value means the sequence \ newline was seen,
429 which is supposed to be equivalent to nothing at all.
431 If \ is followed by a null character, we return a negative
432 value and leave the string pointer pointing at the null character.
434 If \ is followed by 000, we return 0 and leave the string pointer
435 after the zeros. A value of 0 does not mean end of string. */
437 static int
438 parse_escape (string_ptr)
439 const char **string_ptr;
441 int c = *(*string_ptr)++;
442 switch (c)
444 case 'a':
445 return TARGET_BELL;
446 case 'b':
447 return TARGET_BS;
448 case 'e':
449 return 033;
450 case 'f':
451 return TARGET_FF;
452 case 'n':
453 return TARGET_NEWLINE;
454 case 'r':
455 return TARGET_CR;
456 case 't':
457 return TARGET_TAB;
458 case 'v':
459 return TARGET_VT;
460 case '\n':
461 return -2;
462 case 0:
463 (*string_ptr)--;
464 return 0;
465 case '^':
466 c = *(*string_ptr)++;
467 if (c == '\\')
468 c = parse_escape (string_ptr);
469 if (c == '?')
470 return 0177;
471 return (c & 0200) | (c & 037);
473 case '0':
474 case '1':
475 case '2':
476 case '3':
477 case '4':
478 case '5':
479 case '6':
480 case '7':
482 int i = c - '0';
483 int count = 0;
484 while (++count < 3)
486 c = *(*string_ptr)++;
487 if (c >= '0' && c <= '7')
488 i = (i << 3) + c - '0';
489 else
491 (*string_ptr)--;
492 break;
495 if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
497 i &= (1 << CHAR_TYPE_SIZE) - 1;
498 warning ("octal character constant does not fit in a byte");
500 return i;
502 case 'x':
504 int i = 0;
505 for (;;)
507 c = *(*string_ptr)++;
508 if (hex_p (c))
509 i = (i << 4) + hex_value (c);
510 else
512 (*string_ptr)--;
513 break;
516 if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
518 i &= (1 << BITS_PER_UNIT) - 1;
519 warning ("hex character constant does not fit in a byte");
521 return i;
523 default:
524 return c;
528 static void
529 yyerror (msgid)
530 const char *msgid;
532 error ("%s", _(msgid));
533 longjmp (parse_return_error, 1);
536 /* This page contains the entry point to this file. */
538 /* Parse STRING as an expression, and complain if this fails
539 to use up all of the contents of STRING. */
540 /* We do not support C comments. They should be removed before
541 this function is called. */
544 parse_c_expression (string)
545 const char *string;
547 lexptr = string;
549 if (lexptr == 0 || *lexptr == 0) {
550 error ("empty #if expression");
551 return 0; /* don't include the #if group */
554 /* if there is some sort of scanning error, just return 0 and assume
555 the parsing routine has printed an error message somewhere.
556 there is surely a better thing to do than this. */
557 if (setjmp (parse_return_error))
558 return 0;
560 if (yyparse ())
561 return 0; /* actually this is never reached
562 the way things stand. */
563 if (*lexptr)
564 error ("Junk after end of expression.");
566 return expression_value; /* set by yyparse () */