OSDN Git Service

* tradcpp.c: Include intl.h. Rename WARNING, ERROR, FATAL to
[pf3gnuchains/gcc-fork.git] / gcc / tradcif.y
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
7
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.
12
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.
17
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.  */
21
22 /* Parse a C expression from text in a string  */
23    
24 %{
25 #include "config.h"
26 #include "system.h"
27 #include "intl.h"
28 #include "tradcpp.h"
29 #include <setjmp.h>
30
31   static int yylex PARAMS ((void));
32   static void yyerror PARAMS ((const char *msgid)) ATTRIBUTE_NORETURN;
33
34   static int parse_number PARAMS ((int));
35   static int parse_escape PARAMS ((const char **));
36
37   static int expression_value;
38   static jmp_buf parse_return_error;
39
40   /* During parsing of a C expression, the pointer to the next
41      character is in this variable.  */
42
43   static const char *lexptr;
44 %}
45
46 %union {
47   struct constant {long value; int unsignedp;} integer;
48   int voidval;
49   char *sval;
50 }
51
52 %type <integer> exp exp1 start
53 %token <integer> INT CHAR
54 %token <sval> NAME
55 %token <integer> ERROR
56
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
70
71 /* %expect 40 */
72 \f
73 %%
74
75 start   :       exp1
76                 { expression_value = $1.value; }
77         ;
78
79 /* Expressions, including the comma operator.  */
80 exp1    :       exp
81         |       exp1 ',' exp
82                         { $$ = $3; }
83         ;
84
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; }
99         ;
100
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)
110                             {
111                               error ("division by zero in #if");
112                               $3.value = 1;
113                             }
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)
121                             {
122                               error ("division by zero in #if");
123                               $3.value = 1;
124                             }
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); }
209         ;
210 %%
211 \f
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.  */
215
216 /* maybe needs to actually deal with floating point numbers */
217
218 static int
219 parse_number (olen)
220      int olen;
221 {
222   const char *p = lexptr;
223   long n = 0;
224   int c;
225   int base = 10;
226   int len = olen;
227
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;
233     }
234
235   /* Traditionally, all numbers are signed.  However, we make it
236      unsigned if requested with a suffix.  */
237   yylval.integer.unsignedp = 0;
238
239   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
240     p += 2;
241     base = 16;
242     len -= 2;
243   }
244   else if (*p == '0')
245     base = 8;
246
247   while (len > 0) {
248     c = *p++;
249     len--;
250     if (ISUPPER (c))
251       c += 'a' - 'A';
252
253     if (ISDIGIT (c)) {
254       n *= base;
255       n += c - '0';
256     } else if (base == 16 && c >= 'a' && c <= 'f') {
257       n *= base;
258       n += c - 'a' + 10;
259     } else {
260       /* `l' means long, and `u' means unsigned.  */
261       while (1) {
262         if (c == 'l' || c == 'L')
263           ;
264         else if (c == 'u' || c == 'U')
265           yylval.integer.unsignedp = 1;
266         else
267           break;
268
269         if (len == 0)
270           break;
271         c = *p++;
272         len--;
273       }
274       /* Don't look for any more digits after the suffixes.  */
275       break;
276     }
277   }
278
279   if (len != 0) {
280     yyerror ("Invalid number in #if expression");
281     return ERROR;
282   }
283
284   lexptr = p;
285   yylval.integer.value = n;
286   return INT;
287 }
288
289 struct token {
290   const char *const operator;
291   const int token;
292 };
293
294 #ifndef NULL
295 #define NULL 0
296 #endif
297
298 static const struct token tokentab2[] = {
299   {"&&", AND},
300   {"||", OR},
301   {"<<", LSH},
302   {">>", RSH},
303   {"==", EQUAL},
304   {"!=", NOTEQUAL},
305   {"<=", LEQ},
306   {">=", GEQ},
307   {NULL, ERROR}
308 };
309
310 /* Read one token, getting characters through lexptr.  */
311
312 static int
313 yylex ()
314 {
315   int c;
316   int namelen;
317   const char *tokstart;
318   const struct token *toktab;
319
320  retry:
321
322   tokstart = lexptr;
323   c = *tokstart;
324   /* See if it is a special token of length 2.  */
325   for (toktab = tokentab2; toktab->operator != NULL; toktab++)
326     if (c == *toktab->operator && tokstart[1] == toktab->operator[1]) {
327       lexptr += 2;
328       return toktab->token;
329     }
330
331   switch (c) {
332   case 0:
333     return 0;
334     
335   case ' ':
336   case '\t':
337   case '\r':
338   case '\n':
339     lexptr++;
340     goto retry;
341     
342   case '\'':
343     lexptr++;
344     c = *lexptr++;
345     if (c == '\\')
346       c = parse_escape (&lexptr);
347
348     /* Sign-extend the constant if chars are signed on target machine.  */
349     {
350       if (lookup ((const unsigned char *)"__CHAR_UNSIGNED__",
351                    sizeof ("__CHAR_UNSIGNED__")-1, -1)
352           || ((c >> (CHAR_TYPE_SIZE - 1)) & 1) == 0)
353         yylval.integer.value = c & ((1 << CHAR_TYPE_SIZE) - 1);
354       else
355         yylval.integer.value = c | ~((1 << CHAR_TYPE_SIZE) - 1);
356     }
357
358     yylval.integer.unsignedp = 0;
359     c = *lexptr++;
360     if (c != '\'') {
361       yyerror ("Invalid character constant in #if");
362       return ERROR;
363     }
364     
365     return CHAR;
366
367     /* some of these chars are invalid in constant expressions;
368        maybe do something about them later */
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   case ',':
393   case '#':
394     lexptr++;
395     return c;
396     
397   case '"':
398     yyerror ("double quoted strings not allowed in #if expressions");
399     return ERROR;
400   }
401   if (ISDIGIT (c)) {
402     /* It's a number */
403     for (namelen = 0;
404          c = tokstart[namelen], is_idchar (c) || c == '.'; 
405          namelen++)
406       ;
407     return parse_number (namelen);
408   }
409   
410   if (!is_idstart (c)) {
411     yyerror ("Invalid token in expression");
412     return ERROR;
413   }
414   
415   /* It is a name.  See how long it is.  */
416   
417   for (namelen = 0;
418        is_idchar (tokstart[namelen]);
419        namelen++)
420     ;
421   
422   lexptr += namelen;
423   return NAME;
424 }
425
426
427 /* Parse a C escape sequence.  STRING_PTR points to a variable
428    containing a pointer to the string to parse.  That pointer
429    is updated past the characters we use.  The value of the
430    escape sequence is returned.
431
432    A negative value means the sequence \ newline was seen,
433    which is supposed to be equivalent to nothing at all.
434
435    If \ is followed by a null character, we return a negative
436    value and leave the string pointer pointing at the null character.
437
438    If \ is followed by 000, we return 0 and leave the string pointer
439    after the zeros.  A value of 0 does not mean end of string.  */
440
441 static int
442 parse_escape (string_ptr)
443      const char **string_ptr;
444 {
445   int c = *(*string_ptr)++;
446   switch (c)
447     {
448     case 'a':
449       return TARGET_BELL;
450     case 'b':
451       return TARGET_BS;
452     case 'e':
453       return 033;
454     case 'f':
455       return TARGET_FF;
456     case 'n':
457       return TARGET_NEWLINE;
458     case 'r':
459       return TARGET_CR;
460     case 't':
461       return TARGET_TAB;
462     case 'v':
463       return TARGET_VT;
464     case '\n':
465       return -2;
466     case 0:
467       (*string_ptr)--;
468       return 0;
469     case '^':
470       c = *(*string_ptr)++;
471       if (c == '\\')
472         c = parse_escape (string_ptr);
473       if (c == '?')
474         return 0177;
475       return (c & 0200) | (c & 037);
476       
477     case '0':
478     case '1':
479     case '2':
480     case '3':
481     case '4':
482     case '5':
483     case '6':
484     case '7':
485       {
486         int i = c - '0';
487         int count = 0;
488         while (++count < 3)
489           {
490             c = *(*string_ptr)++;
491             if (c >= '0' && c <= '7')
492               i = (i << 3) + c - '0';
493             else
494               {
495                 (*string_ptr)--;
496                 break;
497               }
498           }
499         if ((i & ~((1 << CHAR_TYPE_SIZE) - 1)) != 0)
500           {
501             i &= (1 << CHAR_TYPE_SIZE) - 1;
502             warning ("octal character constant does not fit in a byte");
503           }
504         return i;
505       }
506     case 'x':
507       {
508         int i = 0;
509         for (;;)
510           {
511             c = *(*string_ptr)++;
512             if (ISDIGIT (c))
513               i = (i << 4) + c - '0';
514             else if (c >= 'a' && c <= 'f')
515               i = (i << 4) + c - 'a' + 10;
516             else if (c >= 'A' && c <= 'F')
517               i = (i << 4) + c - 'A' + 10;
518             else
519               {
520                 (*string_ptr)--;
521                 break;
522               }
523           }
524         if ((i & ~((1 << BITS_PER_UNIT) - 1)) != 0)
525           {
526             i &= (1 << BITS_PER_UNIT) - 1;
527             warning ("hex character constant does not fit in a byte");
528           }
529         return i;
530       }
531     default:
532       return c;
533     }
534 }
535
536 static void
537 yyerror (msgid)
538      const char *msgid;
539 {
540   error ("%s", _(msgid));
541   longjmp (parse_return_error, 1);
542 }
543 \f
544 /* This page contains the entry point to this file.  */
545
546 /* Parse STRING as an expression, and complain if this fails
547    to use up all of the contents of STRING.  */
548 /* We do not support C comments.  They should be removed before
549    this function is called.  */
550
551 int
552 parse_c_expression (string)
553      const char *string;
554 {
555   lexptr = string;
556   
557   if (lexptr == 0 || *lexptr == 0) {
558     error ("empty #if expression");
559     return 0;                   /* don't include the #if group */
560   }
561
562   /* if there is some sort of scanning error, just return 0 and assume
563      the parsing routine has printed an error message somewhere.
564      there is surely a better thing to do than this.     */
565   if (setjmp (parse_return_error))
566     return 0;
567
568   if (yyparse ())
569     return 0;                   /* actually this is never reached
570                                    the way things stand. */
571   if (*lexptr)
572     error ("Junk after end of expression.");
573
574   return expression_value;      /* set by yyparse () */
575 }