Remove some compile time warnings about duplicate definitions.
[official-gcc.git] / gcc / tradcif.y
blobbaafad1999ed1fccd0266d20989e4ad88781c388
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 (lookup ((const unsigned char *)"__CHAR_UNSIGNED__",
348 sizeof ("__CHAR_UNSIGNED__")-1, -1)
349 || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
350 yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
351 else
352 yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
355 yylval.integer.unsignedp = 0;
356 c = *lexptr++;
357 if (c != '\'') {
358 yyerror ("Invalid character constant in #if");
359 return ERROR;
362 return CHAR;
364 /* some of these chars are invalid in constant expressions;
365 maybe do something about them later */
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 case '#':
391 lexptr++;
392 return c;
394 case '"':
395 yyerror ("double quoted strings not allowed in #if expressions");
396 return ERROR;
398 if (ISDIGIT (c)) {
399 /* It's a number */
400 for (namelen = 0;
401 c = tokstart[namelen], is_idchar (c) || c == '.';
402 namelen++)
404 return parse_number (namelen);
407 if (!is_idstart (c)) {
408 yyerror ("Invalid token in expression");
409 return ERROR;
412 /* It is a name. See how long it is. */
414 for (namelen = 0;
415 is_idchar (tokstart[namelen]);
416 namelen++)
419 lexptr += namelen;
420 return NAME;
424 /* Parse a C escape sequence. STRING_PTR points to a variable
425 containing a pointer to the string to parse. That pointer
426 is updated past the characters we use. The value of the
427 escape sequence is returned.
429 A negative value means the sequence \ newline was seen,
430 which is supposed to be equivalent to nothing at all.
432 If \ is followed by a null character, we return a negative
433 value and leave the string pointer pointing at the null character.
435 If \ is followed by 000, we return 0 and leave the string pointer
436 after the zeros. A value of 0 does not mean end of string. */
438 static int
439 parse_escape (string_ptr)
440 const char **string_ptr;
442 int c = *(*string_ptr)++;
443 switch (c)
445 case 'a':
446 return TARGET_BELL;
447 case 'b':
448 return TARGET_BS;
449 case 'e':
450 return 033;
451 case 'f':
452 return TARGET_FF;
453 case 'n':
454 return TARGET_NEWLINE;
455 case 'r':
456 return TARGET_CR;
457 case 't':
458 return TARGET_TAB;
459 case 'v':
460 return TARGET_VT;
461 case '\n':
462 return -2;
463 case 0:
464 (*string_ptr)--;
465 return 0;
466 case '^':
467 c = *(*string_ptr)++;
468 if (c == '\\')
469 c = parse_escape (string_ptr);
470 if (c == '?')
471 return 0177;
472 return (c & 0200) | (c & 037);
474 case '0':
475 case '1':
476 case '2':
477 case '3':
478 case '4':
479 case '5':
480 case '6':
481 case '7':
483 int i = c - '0';
484 int count = 0;
485 while (++count < 3)
487 c = *(*string_ptr)++;
488 if (c >= '0' && c <= '7')
489 i = (i << 3) + c - '0';
490 else
492 (*string_ptr)--;
493 break;
496 if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
498 i &= (1 << CHAR_TYPE_SIZE) - 1;
499 warning ("octal character constant does not fit in a byte");
501 return i;
503 case 'x':
505 int i = 0;
506 for (;;)
508 c = *(*string_ptr)++;
509 if (hex_p (c))
510 i = (i << 4) + hex_value (c);
511 else
513 (*string_ptr)--;
514 break;
517 if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
519 i &= (1 << BITS_PER_UNIT) - 1;
520 warning ("hex character constant does not fit in a byte");
522 return i;
524 default:
525 return c;
529 static void
530 yyerror (msgid)
531 const char *msgid;
533 error ("%s", _(msgid));
534 longjmp (parse_return_error, 1);
537 /* This page contains the entry point to this file. */
539 /* Parse STRING as an expression, and complain if this fails
540 to use up all of the contents of STRING. */
541 /* We do not support C comments. They should be removed before
542 this function is called. */
545 parse_c_expression (string)
546 const char *string;
548 lexptr = string;
550 if (lexptr == 0 || *lexptr == 0) {
551 error ("empty #if expression");
552 return 0; /* don't include the #if group */
555 /* if there is some sort of scanning error, just return 0 and assume
556 the parsing routine has printed an error message somewhere.
557 there is surely a better thing to do than this. */
558 if (setjmp (parse_return_error))
559 return 0;
561 if (yyparse ())
562 return 0; /* actually this is never reached
563 the way things stand. */
564 if (*lexptr)
565 error ("Junk after end of expression.");
567 return expression_value; /* set by yyparse () */