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