]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppexp.c
Test for PR gcj/163:
[thirdparty/gcc.git] / gcc / cppexp.c
CommitLineData
b0699dad 1/* Parse C expressions for cpplib.
525bc95d 2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
7f2935c7
PB
3
4This program is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6Free Software Foundation; either version 2, or (at your option) any
7later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
940d9d63
RK
16Foundation, 59 Temple Place - Suite 330,
17Boston, MA 02111-1307, USA.
7f2935c7
PB
18
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22
0f41302f 23Written by Per Bothner 1994. */
7f2935c7
PB
24
25/* Parse a C expression from text in a string */
26
27#include "config.h"
b04cd507 28#include "system.h"
487a6e06 29#include "cpplib.h"
7f2935c7 30
9870475c 31#ifdef MULTIBYTE_CHARS
7f2935c7
PB
32#include <locale.h>
33#endif
34
7f2935c7
PB
35#ifndef CHAR_TYPE_SIZE
36#define CHAR_TYPE_SIZE BITS_PER_UNIT
37#endif
38
39#ifndef INT_TYPE_SIZE
40#define INT_TYPE_SIZE BITS_PER_WORD
41#endif
42
43#ifndef LONG_TYPE_SIZE
44#define LONG_TYPE_SIZE BITS_PER_WORD
45#endif
46
47#ifndef WCHAR_TYPE_SIZE
48#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
49#endif
50
51#ifndef MAX_CHAR_TYPE_SIZE
52#define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
53#endif
54
55#ifndef MAX_INT_TYPE_SIZE
56#define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
57#endif
58
59#ifndef MAX_LONG_TYPE_SIZE
60#define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
61#endif
62
63#ifndef MAX_WCHAR_TYPE_SIZE
64#define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
65#endif
66
e915b770 67#define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
b0699dad 68 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
e915b770 69 : ~ (HOST_WIDEST_INT) 0)
f1a86df6 70
e915b770 71#define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
b0699dad 72 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
e915b770 73 : ~ (HOST_WIDEST_INT) 0)
f1a86df6 74
7f2935c7
PB
75/* Yield nonzero if adding two numbers with A's and B's signs can yield a
76 number with SUM's sign, where A, B, and SUM are all C integers. */
77#define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
78
b04cd507 79static void integer_overflow PARAMS ((cpp_reader *));
b0699dad
ZW
80static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
81 int, unsigned HOST_WIDEST_INT));
82static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
83 int, unsigned HOST_WIDEST_INT));
84static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *,
85 U_CHAR *));
86static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *,
87 U_CHAR *));
ba412f14 88static struct operation parse_defined PARAMS ((cpp_reader *));
b0699dad
ZW
89static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, U_CHAR **,
90 HOST_WIDEST_INT));
91static struct operation lex PARAMS ((cpp_reader *, int));
92
7f2935c7
PB
93
94#define ERROR 299
95#define OROR 300
96#define ANDAND 301
97#define EQUAL 302
98#define NOTEQUAL 303
99#define LEQ 304
100#define GEQ 305
101#define LSH 306
102#define RSH 307
103#define NAME 308
104#define INT 309
105#define CHAR 310
106
107#define LEFT_OPERAND_REQUIRED 1
108#define RIGHT_OPERAND_REQUIRED 2
109#define HAVE_VALUE 4
a4a315ef 110/* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
0f41302f 111 following operand should be short-circuited instead of evaluated. */
a4a315ef
PB
112#define SKIP_OPERAND 8
113/*#define UNSIGNEDP 16*/
7f2935c7 114
b0699dad
ZW
115struct operation
116{
117 short op;
118 char rprio; /* Priority of op (relative to it right operand). */
119 char flags;
120 char unsignedp; /* true if value should be treated as unsigned */
121 HOST_WIDEST_INT value; /* The value logically "right" of op. */
7f2935c7 122};
7f2935c7 123
f13eb63a
ZW
124/* Parse and convert an integer for #if. Accepts decimal, hex, or octal
125 with or without size suffixes. */
7f2935c7 126
f13eb63a
ZW
127static struct operation
128parse_number (pfile, start, end)
7f2935c7 129 cpp_reader *pfile;
f13eb63a
ZW
130 U_CHAR *start;
131 U_CHAR *end;
7f2935c7
PB
132{
133 struct operation op;
f13eb63a
ZW
134 U_CHAR *p = start;
135 int c;
e915b770 136 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
f13eb63a
ZW
137 int base = 10;
138 int overflow = 0;
139 int digit, largest_digit = 0;
7f2935c7
PB
140 int spec_long = 0;
141
142 op.unsignedp = 0;
143
f13eb63a
ZW
144 if (p[0] == '0')
145 {
146 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
147 {
148 p += 2;
149 base = 16;
150 }
151 else
152 {
153 p += 1;
154 base = 8;
155 }
7f2935c7
PB
156 }
157
0f41302f 158 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
e915b770
KG
159 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
160 / ((unsigned HOST_WIDEST_INT) base));
7f2935c7 161
f13eb63a
ZW
162 while (p < end)
163 {
164 c = *p++;
165
166 if (c >= '0' && c <= '9')
167 digit = c - '0';
168 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
169 digit = c - 'a' + 10;
170 else if (base == 16 && c >= 'A' && c <= 'F')
171 digit = c - 'A' + 10;
172 else if (c == '.')
173 {
174 /* It's a float since it contains a point. */
175 cpp_error (pfile,
176 "floating point numbers are not allowed in #if expressions");
177 goto error;
178 }
179 else
180 {
181 /* `l' means long, and `u' means unsigned. */
182 for (;;)
183 {
184 if (c == 'l' || c == 'L')
185 spec_long++;
186 else if (c == 'u' || c == 'U')
187 op.unsignedp++;
188 else
189 {
190 /* Decrement p here so that the error for an invalid number
191 will be generated below in the case where this is the
192 last character in the buffer. */
193 p--;
194 break;
195 }
196 if (p == end)
197 break;
198 c = *p++;
199 }
200 /* Don't look for any more digits after the suffixes. */
7f2935c7 201 break;
f13eb63a
ZW
202 }
203
204 if (largest_digit < digit)
205 largest_digit = digit;
206 nd = n * base + digit;
207 overflow |= MAX_over_base < n || nd < n;
208 n = nd;
7f2935c7 209 }
7f2935c7 210
f13eb63a 211 if (p != end)
7f2935c7 212 {
f13eb63a
ZW
213 cpp_error (pfile, "invalid number in #if expression");
214 goto error;
7f2935c7 215 }
f13eb63a
ZW
216 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
217 {
218 cpp_error (pfile, "too many `l' suffixes in integer constant");
219 goto error;
220 }
221 else if (op.unsignedp > 1)
222 {
223 cpp_error (pfile, "too many `u' suffixes in integer constant");
224 goto error;
225 }
226
7f2935c7 227 if (base <= largest_digit)
7c4033ff 228 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
7f2935c7
PB
229
230 if (overflow)
7c4033ff 231 cpp_pedwarn (pfile, "integer constant out of range");
7f2935c7
PB
232
233 /* If too big to be signed, consider it unsigned. */
e915b770 234 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
7f2935c7
PB
235 {
236 if (base == 10)
f13eb63a
ZW
237 cpp_warning (pfile,
238 "integer constant is so large that it is unsigned");
7f2935c7
PB
239 op.unsignedp = 1;
240 }
241
242 op.value = n;
243 op.op = INT;
244 return op;
f13eb63a
ZW
245
246 error:
247 op.op = ERROR;
248 return op;
7f2935c7
PB
249}
250
f13eb63a
ZW
251/* Parse and convert a character constant for #if. Understands backslash
252 escapes (\n, \031) and multibyte characters (if so configured). */
253static struct operation
254parse_charconst (pfile, start, end)
255 cpp_reader *pfile;
256 U_CHAR *start;
257 U_CHAR *end;
258{
259 struct operation op;
e915b770 260 HOST_WIDEST_INT result = 0;
f13eb63a
ZW
261 int num_chars = 0;
262 int num_bits;
263 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
264 int max_chars;
265 U_CHAR *ptr = start;
266
099a9dd0 267 int c = -1;
f13eb63a
ZW
268
269 if (*ptr == 'L')
270 {
271 ++ptr;
272 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
273 }
274 max_chars = MAX_LONG_TYPE_SIZE / width;
275
276 ++ptr; /* skip initial quote */
277
278 while (ptr < end)
279 {
f13eb63a 280 c = *ptr++;
f13eb63a
ZW
281 if (c == '\'' || c == '\0')
282 break;
283 else if (c == '\\')
284 {
b0699dad 285 c = parse_escape (pfile, &ptr, mask);
f13eb63a
ZW
286 if (width < HOST_BITS_PER_INT
287 && (unsigned int) c >= (unsigned int)(1 << width))
288 cpp_pedwarn (pfile, "escape sequence out of range for character");
289 }
290
291 /* Merge character into result; ignore excess chars. */
292 if (++num_chars <= max_chars)
293 {
294 if (width < HOST_BITS_PER_INT)
295 result = (result << width) | (c & ((1 << width) - 1));
296 else
297 result = c;
298 }
299 }
300
301 if (num_chars == 0)
302 {
303 cpp_error (pfile, "empty character constant");
304 goto error;
305 }
306 else if (c != '\'')
307 {
308 /* cpp_get_token has already emitted an error if !traditional. */
309 if (! CPP_TRADITIONAL (pfile))
310 cpp_error (pfile, "malformatted character constant");
311 goto error;
312 }
313 else if (num_chars > max_chars)
314 {
315 cpp_error (pfile, "character constant too long");
316 goto error;
317 }
318 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
319 cpp_warning (pfile, "multi-character character constant");
320
321 /* If char type is signed, sign-extend the constant. */
322 num_bits = num_chars * width;
323
cf4ed945
ZW
324 if (cpp_defined (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
325 sizeof ("__CHAR_UNSIGNED__")-1)
f13eb63a 326 || ((result >> (num_bits - 1)) & 1) == 0)
ca261cb4
KG
327 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
328 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
f13eb63a 329 else
ca261cb4
KG
330 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
331 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
f13eb63a
ZW
332
333 /* This is always a signed type. */
334 op.unsignedp = 0;
335 op.op = CHAR;
336 return op;
337
338 error:
339 op.op = ERROR;
340 return op;
341}
342
ba412f14
ZW
343static struct operation
344parse_defined (pfile)
345 cpp_reader *pfile;
346{
347 int paren = 0, len;
348 U_CHAR *tok;
349 enum cpp_token token;
350 struct operation op;
351 long old_written = CPP_WRITTEN (pfile);
352
353 op.unsignedp = 0;
354 op.op = INT;
355
356 pfile->no_macro_expand++;
357 token = get_directive_token (pfile);
358 if (token == CPP_LPAREN)
359 {
360 paren++;
361 CPP_SET_WRITTEN (pfile, old_written);
362 token = get_directive_token (pfile);
363 }
364
365 if (token != CPP_NAME)
366 goto oops;
367
368 tok = pfile->token_buffer + old_written;
369 len = CPP_PWRITTEN (pfile) - tok;
370 op.value = cpp_defined (pfile, tok, len);
371
372 if (paren)
373 {
374 if (get_directive_token (pfile) != CPP_RPAREN)
375 goto oops;
376 }
377 CPP_SET_WRITTEN (pfile, old_written);
378 pfile->no_macro_expand--;
379 return op;
380
381 oops:
382 CPP_SET_WRITTEN (pfile, old_written);
383 pfile->no_macro_expand--;
384 cpp_error (pfile, "`defined' without an identifier");
385
386 op.op = ERROR;
387 return op;
388}
389
f13eb63a 390
7f2935c7 391struct token {
bcc5cac9 392 const char *operator;
7f2935c7
PB
393 int token;
394};
395
455d2586 396static const struct token tokentab2[] = {
7f2935c7
PB
397 {"&&", ANDAND},
398 {"||", OROR},
399 {"<<", LSH},
400 {">>", RSH},
401 {"==", EQUAL},
402 {"!=", NOTEQUAL},
403 {"<=", LEQ},
404 {">=", GEQ},
405 {"++", ERROR},
406 {"--", ERROR},
407 {NULL, ERROR}
408};
409
0f41302f 410/* Read one token. */
7f2935c7 411
6de1e2a9 412static struct operation
b0699dad 413lex (pfile, skip_evaluation)
52529158 414 cpp_reader *pfile;
c9666c01 415 int skip_evaluation;
7f2935c7 416{
455d2586 417 const struct token *toktab;
7f2935c7
PB
418 enum cpp_token token;
419 struct operation op;
420 U_CHAR *tok_start, *tok_end;
cf4ed945 421 long old_written;
7f2935c7 422
1519594d 423 old_written = CPP_WRITTEN (pfile);
cf4ed945 424 token = get_directive_token (pfile);
7f2935c7 425
7f2935c7
PB
426 tok_start = pfile->token_buffer + old_written;
427 tok_end = CPP_PWRITTEN (pfile);
cf4ed945 428 CPP_SET_WRITTEN (pfile, old_written);
7f2935c7 429 switch (token)
ba412f14 430 {
0f41302f 431 case CPP_EOF: /* Should not happen ... */
a4a315ef 432 case CPP_VSPACE:
7f2935c7
PB
433 op.op = 0;
434 return op;
7f2935c7 435 case CPP_NUMBER:
f13eb63a 436 return parse_number (pfile, tok_start, tok_end);
7f2935c7 437 case CPP_STRING:
525bc95d 438 case CPP_WSTRING:
cf4ed945 439 cpp_error (pfile, "string constants are not allowed in #if expressions");
7f2935c7
PB
440 op.op = ERROR;
441 return op;
525bc95d 442
7f2935c7 443 case CPP_CHAR:
525bc95d 444 case CPP_WCHAR:
f13eb63a 445 return parse_charconst (pfile, tok_start, tok_end);
7f2935c7
PB
446
447 case CPP_NAME:
ba412f14
ZW
448 if (!strcmp (tok_start, "defined"))
449 return parse_defined (pfile);
450
f13eb63a
ZW
451 op.op = INT;
452 op.unsignedp = 0;
453 op.value = 0;
5dfa4da1 454
ba412f14
ZW
455 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
456 cpp_warning (pfile, "`%.*s' is not defined",
457 (int) (tok_end - tok_start), tok_start);
5dfa4da1
ZW
458 return op;
459
ba412f14
ZW
460 case CPP_ASSERTION:
461 op.op = INT;
462 op.unsignedp = 0;
463 op.value = cpp_defined (pfile, tok_start, tok_end - tok_start);
f13eb63a 464 return op;
7f2935c7
PB
465
466 case CPP_OTHER:
467 /* See if it is a special token of length 2. */
468 if (tok_start + 2 == tok_end)
469 {
470 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
471 if (tok_start[0] == toktab->operator[0]
472 && tok_start[1] == toktab->operator[1])
473 break;
474 if (toktab->token == ERROR)
ab87f8c8
JL
475 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
476 tok_start);
7f2935c7
PB
477 op.op = toktab->token;
478 return op;
479 }
480 /* fall through */
481 default:
482 op.op = *tok_start;
483 return op;
484 }
485}
486
487
488/* Parse a C escape sequence. STRING_PTR points to a variable
489 containing a pointer to the string to parse. That pointer
490 is updated past the characters we use. The value of the
491 escape sequence is returned.
492
493 A negative value means the sequence \ newline was seen,
494 which is supposed to be equivalent to nothing at all.
495
496 If \ is followed by a null character, we return a negative
497 value and leave the string pointer pointing at the null character.
498
499 If \ is followed by 000, we return 0 and leave the string pointer
500 after the zeros. A value of 0 does not mean end of string. */
501
a2c8e144 502static HOST_WIDEST_INT
b0699dad 503parse_escape (pfile, string_ptr, result_mask)
7f2935c7 504 cpp_reader *pfile;
a2c8e144 505 U_CHAR **string_ptr;
e915b770 506 HOST_WIDEST_INT result_mask;
7f2935c7
PB
507{
508 register int c = *(*string_ptr)++;
509 switch (c)
510 {
511 case 'a':
512 return TARGET_BELL;
513 case 'b':
514 return TARGET_BS;
515 case 'e':
516 case 'E':
f1a86df6 517 if (CPP_OPTIONS (pfile)->pedantic)
7f2935c7 518 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
f3ad1f9c 519 return TARGET_ESC;
7f2935c7
PB
520 case 'f':
521 return TARGET_FF;
522 case 'n':
523 return TARGET_NEWLINE;
524 case 'r':
525 return TARGET_CR;
526 case 't':
527 return TARGET_TAB;
528 case 'v':
529 return TARGET_VT;
530 case '\n':
531 return -2;
532 case 0:
533 (*string_ptr)--;
534 return 0;
535
536 case '0':
537 case '1':
538 case '2':
539 case '3':
540 case '4':
541 case '5':
542 case '6':
543 case '7':
544 {
e915b770 545 register HOST_WIDEST_INT i = c - '0';
7f2935c7
PB
546 register int count = 0;
547 while (++count < 3)
548 {
549 c = *(*string_ptr)++;
550 if (c >= '0' && c <= '7')
551 i = (i << 3) + c - '0';
552 else
553 {
554 (*string_ptr)--;
555 break;
556 }
557 }
f1a86df6 558 if (i != (i & result_mask))
7f2935c7 559 {
f1a86df6
ZW
560 i &= result_mask;
561 cpp_pedwarn (pfile, "octal escape sequence out of range");
7f2935c7
PB
562 }
563 return i;
564 }
565 case 'x':
566 {
e915b770 567 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
f1a86df6 568 register int digits_found = 0, digit;
7f2935c7
PB
569 for (;;)
570 {
571 c = *(*string_ptr)++;
572 if (c >= '0' && c <= '9')
573 digit = c - '0';
574 else if (c >= 'a' && c <= 'f')
575 digit = c - 'a' + 10;
576 else if (c >= 'A' && c <= 'F')
577 digit = c - 'A' + 10;
578 else
579 {
580 (*string_ptr)--;
581 break;
582 }
583 overflow |= i ^ (i << 4 >> 4);
584 i = (i << 4) + digit;
585 digits_found = 1;
586 }
587 if (!digits_found)
588 cpp_error (pfile, "\\x used with no following hex digits");
f1a86df6 589 if (overflow | (i != (i & result_mask)))
7f2935c7 590 {
f1a86df6
ZW
591 i &= result_mask;
592 cpp_pedwarn (pfile, "hex escape sequence out of range");
7f2935c7
PB
593 }
594 return i;
595 }
596 default:
597 return c;
598 }
599}
600
601static void
602integer_overflow (pfile)
603 cpp_reader *pfile;
604{
605 if (CPP_PEDANTIC (pfile))
606 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
607}
608
ca261cb4 609static HOST_WIDEST_INT
7f2935c7
PB
610left_shift (pfile, a, unsignedp, b)
611 cpp_reader *pfile;
ca261cb4 612 HOST_WIDEST_INT a;
7f2935c7 613 int unsignedp;
ca261cb4 614 unsigned HOST_WIDEST_INT b;
7f2935c7 615{
ca261cb4 616 if (b >= HOST_BITS_PER_WIDEST_INT)
7f2935c7
PB
617 {
618 if (! unsignedp && a != 0)
619 integer_overflow (pfile);
620 return 0;
621 }
622 else if (unsignedp)
ca261cb4 623 return (unsigned HOST_WIDEST_INT) a << b;
7f2935c7
PB
624 else
625 {
ca261cb4 626 HOST_WIDEST_INT l = a << b;
7f2935c7
PB
627 if (l >> b != a)
628 integer_overflow (pfile);
629 return l;
630 }
631}
632
ca261cb4 633static HOST_WIDEST_INT
7f2935c7 634right_shift (pfile, a, unsignedp, b)
d6f4ec51 635 cpp_reader *pfile ATTRIBUTE_UNUSED;
ca261cb4 636 HOST_WIDEST_INT a;
7f2935c7 637 int unsignedp;
ca261cb4 638 unsigned HOST_WIDEST_INT b;
7f2935c7 639{
ca261cb4
KG
640 if (b >= HOST_BITS_PER_WIDEST_INT)
641 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
7f2935c7 642 else if (unsignedp)
ca261cb4 643 return (unsigned HOST_WIDEST_INT) a >> b;
7f2935c7
PB
644 else
645 return a >> b;
646}
647\f
0f41302f 648/* These priorities are all even, so we can handle associatively. */
7f2935c7
PB
649#define PAREN_INNER_PRIO 0
650#define COMMA_PRIO 4
651#define COND_PRIO (COMMA_PRIO+2)
652#define OROR_PRIO (COND_PRIO+2)
653#define ANDAND_PRIO (OROR_PRIO+2)
654#define OR_PRIO (ANDAND_PRIO+2)
655#define XOR_PRIO (OR_PRIO+2)
656#define AND_PRIO (XOR_PRIO+2)
657#define EQUAL_PRIO (AND_PRIO+2)
658#define LESS_PRIO (EQUAL_PRIO+2)
659#define SHIFT_PRIO (LESS_PRIO+2)
660#define PLUS_PRIO (SHIFT_PRIO+2)
661#define MUL_PRIO (PLUS_PRIO+2)
662#define UNARY_PRIO (MUL_PRIO+2)
663#define PAREN_OUTER_PRIO (UNARY_PRIO+2)
664
665#define COMPARE(OP) \
666 top->unsignedp = 0;\
faa76596 667 top->value = (unsigned1 || unsigned2) \
ca261cb4 668 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
7f2935c7
PB
669
670/* Parse and evaluate a C expression, reading from PFILE.
671 Returns the value of the expression. */
672
e915b770 673HOST_WIDEST_INT
b0699dad 674_cpp_parse_expr (pfile)
7f2935c7
PB
675 cpp_reader *pfile;
676{
677 /* The implementation is an operator precedence parser,
678 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
679
680 The stack base is 'stack', and the current stack pointer is 'top'.
681 There is a stack element for each operator (only),
682 and the most recently pushed operator is 'top->op'.
683 An operand (value) is stored in the 'value' field of the stack
684 element of the operator that precedes it.
685 In that case the 'flags' field has the HAVE_VALUE flag set. */
686
687#define INIT_STACK_SIZE 20
688 struct operation init_stack[INIT_STACK_SIZE];
689 struct operation *stack = init_stack;
690 struct operation *limit = stack + INIT_STACK_SIZE;
691 register struct operation *top = stack;
099a9dd0 692 int lprio, rprio = 0;
a4a315ef 693 int skip_evaluation = 0;
7f2935c7
PB
694
695 top->rprio = 0;
696 top->flags = 0;
697 for (;;)
698 {
699 struct operation op;
700 char flags = 0;
701
702 /* Read a token */
b0699dad 703 op = lex (pfile, skip_evaluation);
7f2935c7
PB
704
705 /* See if the token is an operand, in which case go to set_value.
706 If the token is an operator, figure out its left and right
0f41302f 707 priorities, and then goto maybe_reduce. */
7f2935c7
PB
708
709 switch (op.op)
710 {
711 case NAME:
b0699dad 712 cpp_ice (pfile, "lex returns a NAME");
34ca9541 713 goto syntax_error;
7f2935c7 714 case INT: case CHAR:
7f2935c7
PB
715 goto set_value;
716 case 0:
717 lprio = 0; goto maybe_reduce;
718 case '+': case '-':
cf4ed945
ZW
719 if (top->flags & HAVE_VALUE)
720 {
721 lprio = PLUS_PRIO;
722 goto binop;
723 }
724 /* else fall through */
7f2935c7
PB
725 case '!': case '~':
726 flags = RIGHT_OPERAND_REQUIRED;
727 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
728 case '*': case '/': case '%':
729 lprio = MUL_PRIO; goto binop;
730 case '<': case '>': case LEQ: case GEQ:
731 lprio = LESS_PRIO; goto binop;
732 case EQUAL: case NOTEQUAL:
733 lprio = EQUAL_PRIO; goto binop;
734 case LSH: case RSH:
735 lprio = SHIFT_PRIO; goto binop;
736 case '&': lprio = AND_PRIO; goto binop;
737 case '^': lprio = XOR_PRIO; goto binop;
738 case '|': lprio = OR_PRIO; goto binop;
739 case ANDAND: lprio = ANDAND_PRIO; goto binop;
740 case OROR: lprio = OROR_PRIO; goto binop;
741 case ',':
742 lprio = COMMA_PRIO; goto binop;
743 case '(':
744 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
745 goto maybe_reduce;
746 case ')':
747 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
748 goto maybe_reduce;
749 case ':':
750 lprio = COND_PRIO; rprio = COND_PRIO;
751 goto maybe_reduce;
752 case '?':
753 lprio = COND_PRIO + 1; rprio = COND_PRIO;
754 goto maybe_reduce;
f13eb63a
ZW
755 case ERROR:
756 goto syntax_error;
7f2935c7
PB
757 default:
758 cpp_error (pfile, "invalid character in #if");
759 goto syntax_error;
760 }
761
762 set_value:
0f41302f 763 /* Push a value onto the stack. */
7f2935c7
PB
764 if (top->flags & HAVE_VALUE)
765 {
766 cpp_error (pfile, "syntax error in #if");
767 goto syntax_error;
768 }
cf4ed945
ZW
769 top->value = op.value;
770 top->unsignedp = op.unsignedp;
7f2935c7
PB
771 top->flags |= HAVE_VALUE;
772 continue;
773
cf4ed945
ZW
774 binop:
775 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
776 rprio = lprio + 1;
777
7f2935c7 778 maybe_reduce:
0f41302f 779 /* Push an operator, and check if we can reduce now. */
7f2935c7
PB
780 while (top->rprio > lprio)
781 {
ca261cb4 782 HOST_WIDEST_INT v1 = top[-1].value, v2 = top[0].value;
7f2935c7
PB
783 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
784 top--;
785 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
786 && ! (top[0].flags & HAVE_VALUE))
787 {
788 cpp_error (pfile, "syntax error - missing left operand");
789 goto syntax_error;
790 }
791 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
792 && ! (top[1].flags & HAVE_VALUE))
793 {
794 cpp_error (pfile, "syntax error - missing right operand");
795 goto syntax_error;
796 }
797 /* top[0].value = (top[1].op)(v1, v2);*/
798 switch (top[1].op)
799 {
800 case '+':
801 if (!(top->flags & HAVE_VALUE))
802 { /* Unary '+' */
803 top->value = v2;
804 top->unsignedp = unsigned2;
805 top->flags |= HAVE_VALUE;
806 }
807 else
808 {
809 top->value = v1 + v2;
810 top->unsignedp = unsigned1 || unsigned2;
a4a315ef 811 if (! top->unsignedp && ! skip_evaluation
7f2935c7
PB
812 && ! possible_sum_sign (v1, v2, top->value))
813 integer_overflow (pfile);
814 }
815 break;
816 case '-':
52529158 817 if (!(top->flags & HAVE_VALUE))
7f2935c7
PB
818 { /* Unary '-' */
819 top->value = - v2;
52529158 820 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
7f2935c7
PB
821 integer_overflow (pfile);
822 top->unsignedp = unsigned2;
823 top->flags |= HAVE_VALUE;
824 }
825 else
826 { /* Binary '-' */
827 top->value = v1 - v2;
828 top->unsignedp = unsigned1 || unsigned2;
52529158 829 if (! top->unsignedp && ! skip_evaluation
7f2935c7
PB
830 && ! possible_sum_sign (top->value, v2, v1))
831 integer_overflow (pfile);
832 }
833 break;
834 case '*':
835 top->unsignedp = unsigned1 || unsigned2;
836 if (top->unsignedp)
ca261cb4 837 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
a4a315ef 838 else if (!skip_evaluation)
7f2935c7
PB
839 {
840 top->value = v1 * v2;
841 if (v1
842 && (top->value / v1 != v2
843 || (top->value & v1 & v2) < 0))
844 integer_overflow (pfile);
845 }
846 break;
847 case '/':
a4a315ef
PB
848 if (skip_evaluation)
849 break;
7f2935c7
PB
850 if (v2 == 0)
851 {
852 cpp_error (pfile, "division by zero in #if");
853 v2 = 1;
854 }
855 top->unsignedp = unsigned1 || unsigned2;
856 if (top->unsignedp)
ca261cb4 857 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
7f2935c7
PB
858 else
859 {
860 top->value = v1 / v2;
861 if ((top->value & v1 & v2) < 0)
862 integer_overflow (pfile);
863 }
864 break;
865 case '%':
a4a315ef
PB
866 if (skip_evaluation)
867 break;
7f2935c7
PB
868 if (v2 == 0)
869 {
870 cpp_error (pfile, "division by zero in #if");
871 v2 = 1;
872 }
873 top->unsignedp = unsigned1 || unsigned2;
874 if (top->unsignedp)
ca261cb4 875 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
7f2935c7
PB
876 else
877 top->value = v1 % v2;
878 break;
879 case '!':
880 if (top->flags & HAVE_VALUE)
881 {
882 cpp_error (pfile, "syntax error");
883 goto syntax_error;
884 }
885 top->value = ! v2;
886 top->unsignedp = 0;
887 top->flags |= HAVE_VALUE;
888 break;
889 case '~':
890 if (top->flags & HAVE_VALUE)
891 {
892 cpp_error (pfile, "syntax error");
893 goto syntax_error;
894 }
895 top->value = ~ v2;
896 top->unsignedp = unsigned2;
897 top->flags |= HAVE_VALUE;
898 break;
899 case '<': COMPARE(<); break;
900 case '>': COMPARE(>); break;
901 case LEQ: COMPARE(<=); break;
902 case GEQ: COMPARE(>=); break;
903 case EQUAL:
904 top->value = (v1 == v2);
905 top->unsignedp = 0;
906 break;
907 case NOTEQUAL:
908 top->value = (v1 != v2);
909 top->unsignedp = 0;
910 break;
911 case LSH:
a4a315ef
PB
912 if (skip_evaluation)
913 break;
7f2935c7
PB
914 top->unsignedp = unsigned1;
915 if (v2 < 0 && ! unsigned2)
916 top->value = right_shift (pfile, v1, unsigned1, -v2);
917 else
918 top->value = left_shift (pfile, v1, unsigned1, v2);
919 break;
920 case RSH:
a4a315ef
PB
921 if (skip_evaluation)
922 break;
7f2935c7
PB
923 top->unsignedp = unsigned1;
924 if (v2 < 0 && ! unsigned2)
925 top->value = left_shift (pfile, v1, unsigned1, -v2);
926 else
927 top->value = right_shift (pfile, v1, unsigned1, v2);
928 break;
929#define LOGICAL(OP) \
930 top->value = v1 OP v2;\
931 top->unsignedp = unsigned1 || unsigned2;
932 case '&': LOGICAL(&); break;
933 case '^': LOGICAL(^); break;
934 case '|': LOGICAL(|); break;
935 case ANDAND:
a4a315ef
PB
936 top->value = v1 && v2; top->unsignedp = 0;
937 if (!v1) skip_evaluation--;
938 break;
7f2935c7 939 case OROR:
a4a315ef
PB
940 top->value = v1 || v2; top->unsignedp = 0;
941 if (v1) skip_evaluation--;
942 break;
7f2935c7
PB
943 case ',':
944 if (CPP_PEDANTIC (pfile))
945 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
946 top->value = v2;
947 top->unsignedp = unsigned2;
948 break;
949 case '(': case '?':
950 cpp_error (pfile, "syntax error in #if");
951 goto syntax_error;
952 case ':':
953 if (top[0].op != '?')
954 {
955 cpp_error (pfile,
956 "syntax error ':' without preceding '?'");
957 goto syntax_error;
958 }
959 else if (! (top[1].flags & HAVE_VALUE)
960 || !(top[-1].flags & HAVE_VALUE)
961 || !(top[0].flags & HAVE_VALUE))
962 {
963 cpp_error (pfile, "bad syntax for ?: operator");
964 goto syntax_error;
965 }
966 else
967 {
968 top--;
a4a315ef 969 if (top->value) skip_evaluation--;
7f2935c7
PB
970 top->value = top->value ? v1 : v2;
971 top->unsignedp = unsigned1 || unsigned2;
972 }
973 break;
974 case ')':
975 if ((top[1].flags & HAVE_VALUE)
976 || ! (top[0].flags & HAVE_VALUE)
977 || top[0].op != '('
978 || (top[-1].flags & HAVE_VALUE))
979 {
980 cpp_error (pfile, "mismatched parentheses in #if");
981 goto syntax_error;
982 }
983 else
984 {
985 top--;
986 top->value = v1;
987 top->unsignedp = unsigned1;
988 top->flags |= HAVE_VALUE;
989 }
990 break;
991 default:
a9ae4483
ZW
992 if (ISGRAPH (top[1].op))
993 cpp_error (pfile, "unimplemented operator '%c'\n", top[1].op);
994 else
995 cpp_error (pfile, "unimplemented operator '\\%03o'\n",
996 top[1].op);
7f2935c7
PB
997 }
998 }
999 if (op.op == 0)
1000 {
1001 if (top != stack)
c1212d2f 1002 cpp_ice (pfile, "unbalanced stack in #if expression");
7f2935c7
PB
1003 if (stack != init_stack)
1004 free (stack);
1005 return top->value;
1006 }
1007 top++;
1008
0f41302f 1009 /* Check for and handle stack overflow. */
7f2935c7
PB
1010 if (top == limit)
1011 {
1012 struct operation *new_stack;
0f41302f 1013 int old_size = (char *) limit - (char *) stack;
7f2935c7
PB
1014 int new_size = 2 * old_size;
1015 if (stack != init_stack)
0f41302f 1016 new_stack = (struct operation *) xrealloc (stack, new_size);
7f2935c7
PB
1017 else
1018 {
0f41302f 1019 new_stack = (struct operation *) xmalloc (new_size);
b0699dad 1020 memcpy (new_stack, stack, old_size);
7f2935c7
PB
1021 }
1022 stack = new_stack;
0f41302f
MS
1023 top = (struct operation *) ((char *) new_stack + old_size);
1024 limit = (struct operation *) ((char *) new_stack + new_size);
7f2935c7
PB
1025 }
1026
1027 top->flags = flags;
1028 top->rprio = rprio;
1029 top->op = op.op;
a4a315ef
PB
1030 if ((op.op == OROR && top[-1].value)
1031 || (op.op == ANDAND && !top[-1].value)
1032 || (op.op == '?' && !top[-1].value))
1033 {
1034 skip_evaluation++;
1035 }
1036 else if (op.op == ':')
1037 {
1038 if (top[-2].value) /* Was condition true? */
1039 skip_evaluation++;
1040 else
1041 skip_evaluation--;
1042 }
7f2935c7
PB
1043 }
1044 syntax_error:
1045 if (stack != init_stack)
1046 free (stack);
7f2935c7
PB
1047 return 0;
1048}