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