* Makefile.in (cppexp.o): Depend on cpphash.h.
[official-gcc.git] / gcc / cppexp.c
blobf037924d92737892ed0542ad9f4514a28dd00d22
1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999 Free Software Foundation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
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!
23 Written by Per Bothner 1994. */
25 /* Parse a C expression from text in a string */
27 #include "config.h"
28 #include "system.h"
29 #include "cpplib.h"
30 #include "cpphash.h"
32 #ifdef MULTIBYTE_CHARS
33 #include <locale.h>
34 #endif
36 #ifndef CHAR_TYPE_SIZE
37 #define CHAR_TYPE_SIZE BITS_PER_UNIT
38 #endif
40 #ifndef INT_TYPE_SIZE
41 #define INT_TYPE_SIZE BITS_PER_WORD
42 #endif
44 #ifndef LONG_TYPE_SIZE
45 #define LONG_TYPE_SIZE BITS_PER_WORD
46 #endif
48 #ifndef WCHAR_TYPE_SIZE
49 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
50 #endif
52 #ifndef MAX_CHAR_TYPE_SIZE
53 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
54 #endif
56 #ifndef MAX_INT_TYPE_SIZE
57 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
58 #endif
60 #ifndef MAX_LONG_TYPE_SIZE
61 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
62 #endif
64 #ifndef MAX_WCHAR_TYPE_SIZE
65 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
66 #endif
68 #define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
69 ? (~ (~ (HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
70 : ~ (HOST_WIDEST_INT) 0)
72 #define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
73 ? ~ (~ (HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
74 : ~ (HOST_WIDEST_INT) 0)
76 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
77 number with SUM's sign, where A, B, and SUM are all C integers. */
78 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
80 static void integer_overflow PARAMS ((cpp_reader *));
81 static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
82 static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT, int, unsigned HOST_WIDEST_INT));
83 static struct operation parse_number PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
84 static struct operation parse_charconst PARAMS ((cpp_reader *, U_CHAR *, U_CHAR *));
85 static struct operation cpp_lex PARAMS ((cpp_reader *, int));
86 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
88 #define ERROR 299
89 #define OROR 300
90 #define ANDAND 301
91 #define EQUAL 302
92 #define NOTEQUAL 303
93 #define LEQ 304
94 #define GEQ 305
95 #define LSH 306
96 #define RSH 307
97 #define NAME 308
98 #define INT 309
99 #define CHAR 310
101 #define LEFT_OPERAND_REQUIRED 1
102 #define RIGHT_OPERAND_REQUIRED 2
103 #define HAVE_VALUE 4
104 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
105 following operand should be short-circuited instead of evaluated. */
106 #define SKIP_OPERAND 8
107 /*#define UNSIGNEDP 16*/
109 struct operation {
110 short op;
111 char rprio; /* Priority of op (relative to it right operand). */
112 char flags;
113 char unsignedp; /* true if value should be treated as unsigned */
114 HOST_WIDEST_INT value; /* The value logically "right" of op. */
117 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
118 with or without size suffixes. */
120 static struct operation
121 parse_number (pfile, start, end)
122 cpp_reader *pfile;
123 U_CHAR *start;
124 U_CHAR *end;
126 struct operation op;
127 U_CHAR *p = start;
128 int c;
129 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
130 int base = 10;
131 int overflow = 0;
132 int digit, largest_digit = 0;
133 int spec_long = 0;
135 op.unsignedp = 0;
137 if (p[0] == '0')
139 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
141 p += 2;
142 base = 16;
144 else
146 p += 1;
147 base = 8;
151 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
152 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
153 / ((unsigned HOST_WIDEST_INT) base));
155 while (p < end)
157 c = *p++;
159 if (c >= '0' && c <= '9')
160 digit = c - '0';
161 else if (base == 16 && c >= 'a' && c <= 'f') /* FIXME: assumes ASCII */
162 digit = c - 'a' + 10;
163 else if (base == 16 && c >= 'A' && c <= 'F')
164 digit = c - 'A' + 10;
165 else if (c == '.')
167 /* It's a float since it contains a point. */
168 cpp_error (pfile,
169 "floating point numbers are not allowed in #if expressions");
170 goto error;
172 else
174 /* `l' means long, and `u' means unsigned. */
175 for (;;)
177 if (c == 'l' || c == 'L')
178 spec_long++;
179 else if (c == 'u' || c == 'U')
180 op.unsignedp++;
181 else
183 /* Decrement p here so that the error for an invalid number
184 will be generated below in the case where this is the
185 last character in the buffer. */
186 p--;
187 break;
189 if (p == end)
190 break;
191 c = *p++;
193 /* Don't look for any more digits after the suffixes. */
194 break;
197 if (largest_digit < digit)
198 largest_digit = digit;
199 nd = n * base + digit;
200 overflow |= MAX_over_base < n || nd < n;
201 n = nd;
204 if (p != end)
206 cpp_error (pfile, "invalid number in #if expression");
207 goto error;
209 else if (spec_long > (CPP_OPTIONS (pfile)->c89 ? 1 : 2))
211 cpp_error (pfile, "too many `l' suffixes in integer constant");
212 goto error;
214 else if (op.unsignedp > 1)
216 cpp_error (pfile, "too many `u' suffixes in integer constant");
217 goto error;
220 if (base <= largest_digit)
221 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
223 if (overflow)
224 cpp_pedwarn (pfile, "integer constant out of range");
226 /* If too big to be signed, consider it unsigned. */
227 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
229 if (base == 10)
230 cpp_warning (pfile,
231 "integer constant is so large that it is unsigned");
232 op.unsignedp = 1;
235 op.value = n;
236 op.op = INT;
237 return op;
239 error:
240 op.op = ERROR;
241 return op;
244 /* Parse and convert a character constant for #if. Understands backslash
245 escapes (\n, \031) and multibyte characters (if so configured). */
246 static struct operation
247 parse_charconst (pfile, start, end)
248 cpp_reader *pfile;
249 U_CHAR *start;
250 U_CHAR *end;
252 struct operation op;
253 HOST_WIDEST_INT result = 0;
254 int num_chars = 0;
255 int num_bits;
256 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
257 int max_chars;
258 U_CHAR *ptr = start;
260 /* FIXME: Should use reentrant multibyte functions. */
261 #ifdef MULTIBYTE_CHARS
262 wchar_t c = (wchar_t)-1;
263 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
264 #else
265 int c = -1;
266 #endif
268 if (*ptr == 'L')
270 ++ptr;
271 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
273 max_chars = MAX_LONG_TYPE_SIZE / width;
275 ++ptr; /* skip initial quote */
277 while (ptr < end)
279 #ifndef MULTIBYTE_CHARS
280 c = *ptr++;
281 #else
282 ptr += mbtowc (&c, ptr, end - ptr);
283 #endif
284 if (c == '\'' || c == '\0')
285 break;
286 else if (c == '\\')
288 /* Hopefully valid assumption: if mbtowc returns a backslash,
289 we are in initial shift state. No valid escape-sequence
290 character can take us out of initial shift state or begin
291 an unshifted multibyte char, so cpp_parse_escape doesn't
292 need to know about multibyte chars. */
294 c = cpp_parse_escape (pfile, (char **) &ptr, mask);
295 if (width < HOST_BITS_PER_INT
296 && (unsigned int) c >= (unsigned int)(1 << width))
297 cpp_pedwarn (pfile, "escape sequence out of range for character");
300 /* Merge character into result; ignore excess chars. */
301 if (++num_chars <= max_chars)
303 if (width < HOST_BITS_PER_INT)
304 result = (result << width) | (c & ((1 << width) - 1));
305 else
306 result = c;
310 if (num_chars == 0)
312 cpp_error (pfile, "empty character constant");
313 goto error;
315 else if (c != '\'')
317 /* cpp_get_token has already emitted an error if !traditional. */
318 if (! CPP_TRADITIONAL (pfile))
319 cpp_error (pfile, "malformatted character constant");
320 goto error;
322 else if (num_chars > max_chars)
324 cpp_error (pfile, "character constant too long");
325 goto error;
327 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
328 cpp_warning (pfile, "multi-character character constant");
330 /* If char type is signed, sign-extend the constant. */
331 num_bits = num_chars * width;
333 if (cpp_lookup (pfile, (const U_CHAR *)"__CHAR_UNSIGNED__",
334 sizeof ("__CHAR_UNSIGNED__")-1, -1)
335 || ((result >> (num_bits - 1)) & 1) == 0)
336 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
337 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
338 else
339 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
340 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
342 /* This is always a signed type. */
343 op.unsignedp = 0;
344 op.op = CHAR;
345 return op;
347 error:
348 op.op = ERROR;
349 return op;
353 struct token {
354 const char *operator;
355 int token;
358 static struct token tokentab2[] = {
359 {"&&", ANDAND},
360 {"||", OROR},
361 {"<<", LSH},
362 {">>", RSH},
363 {"==", EQUAL},
364 {"!=", NOTEQUAL},
365 {"<=", LEQ},
366 {">=", GEQ},
367 {"++", ERROR},
368 {"--", ERROR},
369 {NULL, ERROR}
372 /* Read one token. */
374 static struct operation
375 cpp_lex (pfile, skip_evaluation)
376 cpp_reader *pfile;
377 int skip_evaluation;
379 U_CHAR c;
380 struct token *toktab;
381 enum cpp_token token;
382 struct operation op;
383 U_CHAR *tok_start, *tok_end;
384 int old_written;
386 retry:
388 old_written = CPP_WRITTEN (pfile);
389 cpp_skip_hspace (pfile);
390 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
391 if (c == '#')
393 op.op = INT;
394 op.value = cpp_read_check_assertion (pfile);
395 return op;
398 if (c == '\n')
400 op.op = 0;
401 return op;
404 token = cpp_get_token (pfile);
405 tok_start = pfile->token_buffer + old_written;
406 tok_end = CPP_PWRITTEN (pfile);
407 pfile->limit = tok_start;
408 switch (token)
410 case CPP_EOF: /* Should not happen ... */
411 case CPP_VSPACE:
412 op.op = 0;
413 return op;
414 case CPP_POP:
415 if (CPP_BUFFER (pfile)->fname != NULL)
417 op.op = 0;
418 return op;
420 cpp_pop_buffer (pfile);
421 goto retry;
422 case CPP_HSPACE:
423 case CPP_COMMENT:
424 goto retry;
425 case CPP_NUMBER:
426 return parse_number (pfile, tok_start, tok_end);
427 case CPP_STRING:
428 cpp_error (pfile, "string constants not allowed in #if expressions");
429 op.op = ERROR;
430 return op;
431 case CPP_CHAR:
432 return parse_charconst (pfile, tok_start, tok_end);
434 case CPP_NAME:
435 op.op = INT;
436 op.unsignedp = 0;
437 op.value = 0;
438 if (strcmp (tok_start, "defined"))
440 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
441 cpp_warning (pfile, "`%.*s' is not defined",
442 (int) (tok_end - tok_start), tok_start);
444 else
446 int paren = 0, len;
447 cpp_buffer *ip = CPP_BUFFER (pfile);
448 U_CHAR *tok;
449 HASHNODE *hp;
451 cpp_skip_hspace (pfile);
452 if (*ip->cur == '(')
454 paren++;
455 ip->cur++; /* Skip over the paren */
456 cpp_skip_hspace (pfile);
459 if (!is_idstart[*ip->cur])
460 goto oops;
461 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
462 goto oops;
463 tok = ip->cur;
464 while (is_idchar[*ip->cur])
465 ++ip->cur;
466 len = ip->cur - tok;
467 cpp_skip_hspace (pfile);
468 if (paren)
470 if (*ip->cur != ')')
471 goto oops;
472 ++ip->cur;
474 hp = cpp_lookup (pfile, tok, len, -1);
475 if (hp != NULL)
477 if (hp->type == T_POISON)
478 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
479 else
480 op.value = 1;
483 return op;
485 oops:
486 cpp_error (pfile, "`defined' without an identifier");
487 return op;
489 case CPP_OTHER:
490 /* See if it is a special token of length 2. */
491 if (tok_start + 2 == tok_end)
493 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
494 if (tok_start[0] == toktab->operator[0]
495 && tok_start[1] == toktab->operator[1])
496 break;
497 if (toktab->token == ERROR)
498 cpp_error (pfile, "`%s' not allowed in operand of `#if'",
499 tok_start);
500 op.op = toktab->token;
501 return op;
503 /* fall through */
504 default:
505 op.op = *tok_start;
506 return op;
511 /* Parse a C escape sequence. STRING_PTR points to a variable
512 containing a pointer to the string to parse. That pointer
513 is updated past the characters we use. The value of the
514 escape sequence is returned.
516 A negative value means the sequence \ newline was seen,
517 which is supposed to be equivalent to nothing at all.
519 If \ is followed by a null character, we return a negative
520 value and leave the string pointer pointing at the null character.
522 If \ is followed by 000, we return 0 and leave the string pointer
523 after the zeros. A value of 0 does not mean end of string. */
525 HOST_WIDEST_INT
526 cpp_parse_escape (pfile, string_ptr, result_mask)
527 cpp_reader *pfile;
528 char **string_ptr;
529 HOST_WIDEST_INT result_mask;
531 register int c = *(*string_ptr)++;
532 switch (c)
534 case 'a':
535 return TARGET_BELL;
536 case 'b':
537 return TARGET_BS;
538 case 'e':
539 case 'E':
540 if (CPP_OPTIONS (pfile)->pedantic)
541 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
542 return TARGET_ESC;
543 case 'f':
544 return TARGET_FF;
545 case 'n':
546 return TARGET_NEWLINE;
547 case 'r':
548 return TARGET_CR;
549 case 't':
550 return TARGET_TAB;
551 case 'v':
552 return TARGET_VT;
553 case '\n':
554 return -2;
555 case 0:
556 (*string_ptr)--;
557 return 0;
559 case '0':
560 case '1':
561 case '2':
562 case '3':
563 case '4':
564 case '5':
565 case '6':
566 case '7':
568 register HOST_WIDEST_INT i = c - '0';
569 register int count = 0;
570 while (++count < 3)
572 c = *(*string_ptr)++;
573 if (c >= '0' && c <= '7')
574 i = (i << 3) + c - '0';
575 else
577 (*string_ptr)--;
578 break;
581 if (i != (i & result_mask))
583 i &= result_mask;
584 cpp_pedwarn (pfile, "octal escape sequence out of range");
586 return i;
588 case 'x':
590 register unsigned HOST_WIDEST_INT i = 0, overflow = 0;
591 register int digits_found = 0, digit;
592 for (;;)
594 c = *(*string_ptr)++;
595 if (c >= '0' && c <= '9')
596 digit = c - '0';
597 else if (c >= 'a' && c <= 'f')
598 digit = c - 'a' + 10;
599 else if (c >= 'A' && c <= 'F')
600 digit = c - 'A' + 10;
601 else
603 (*string_ptr)--;
604 break;
606 overflow |= i ^ (i << 4 >> 4);
607 i = (i << 4) + digit;
608 digits_found = 1;
610 if (!digits_found)
611 cpp_error (pfile, "\\x used with no following hex digits");
612 if (overflow | (i != (i & result_mask)))
614 i &= result_mask;
615 cpp_pedwarn (pfile, "hex escape sequence out of range");
617 return i;
619 default:
620 return c;
624 static void
625 integer_overflow (pfile)
626 cpp_reader *pfile;
628 if (CPP_PEDANTIC (pfile))
629 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
632 static HOST_WIDEST_INT
633 left_shift (pfile, a, unsignedp, b)
634 cpp_reader *pfile;
635 HOST_WIDEST_INT a;
636 int unsignedp;
637 unsigned HOST_WIDEST_INT b;
639 if (b >= HOST_BITS_PER_WIDEST_INT)
641 if (! unsignedp && a != 0)
642 integer_overflow (pfile);
643 return 0;
645 else if (unsignedp)
646 return (unsigned HOST_WIDEST_INT) a << b;
647 else
649 HOST_WIDEST_INT l = a << b;
650 if (l >> b != a)
651 integer_overflow (pfile);
652 return l;
656 static HOST_WIDEST_INT
657 right_shift (pfile, a, unsignedp, b)
658 cpp_reader *pfile ATTRIBUTE_UNUSED;
659 HOST_WIDEST_INT a;
660 int unsignedp;
661 unsigned HOST_WIDEST_INT b;
663 if (b >= HOST_BITS_PER_WIDEST_INT)
664 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
665 else if (unsignedp)
666 return (unsigned HOST_WIDEST_INT) a >> b;
667 else
668 return a >> b;
671 /* These priorities are all even, so we can handle associatively. */
672 #define PAREN_INNER_PRIO 0
673 #define COMMA_PRIO 4
674 #define COND_PRIO (COMMA_PRIO+2)
675 #define OROR_PRIO (COND_PRIO+2)
676 #define ANDAND_PRIO (OROR_PRIO+2)
677 #define OR_PRIO (ANDAND_PRIO+2)
678 #define XOR_PRIO (OR_PRIO+2)
679 #define AND_PRIO (XOR_PRIO+2)
680 #define EQUAL_PRIO (AND_PRIO+2)
681 #define LESS_PRIO (EQUAL_PRIO+2)
682 #define SHIFT_PRIO (LESS_PRIO+2)
683 #define PLUS_PRIO (SHIFT_PRIO+2)
684 #define MUL_PRIO (PLUS_PRIO+2)
685 #define UNARY_PRIO (MUL_PRIO+2)
686 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
688 #define COMPARE(OP) \
689 top->unsignedp = 0;\
690 top->value = (unsigned1 || unsigned2) \
691 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
693 /* Parse and evaluate a C expression, reading from PFILE.
694 Returns the value of the expression. */
696 HOST_WIDEST_INT
697 cpp_parse_expr (pfile)
698 cpp_reader *pfile;
700 /* The implementation is an operator precedence parser,
701 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
703 The stack base is 'stack', and the current stack pointer is 'top'.
704 There is a stack element for each operator (only),
705 and the most recently pushed operator is 'top->op'.
706 An operand (value) is stored in the 'value' field of the stack
707 element of the operator that precedes it.
708 In that case the 'flags' field has the HAVE_VALUE flag set. */
710 #define INIT_STACK_SIZE 20
711 struct operation init_stack[INIT_STACK_SIZE];
712 struct operation *stack = init_stack;
713 struct operation *limit = stack + INIT_STACK_SIZE;
714 register struct operation *top = stack;
715 int lprio, rprio = 0;
716 int skip_evaluation = 0;
718 top->rprio = 0;
719 top->flags = 0;
720 for (;;)
722 struct operation op;
723 char flags = 0;
725 /* Read a token */
726 op = cpp_lex (pfile, skip_evaluation);
728 /* See if the token is an operand, in which case go to set_value.
729 If the token is an operator, figure out its left and right
730 priorities, and then goto maybe_reduce. */
732 switch (op.op)
734 case NAME:
735 cpp_fatal (pfile, "internal error: cpp_lex returns a NAME");
736 goto syntax_error;
737 case INT: case CHAR:
738 top->value = op.value;
739 top->unsignedp = op.unsignedp;
740 goto set_value;
741 case 0:
742 lprio = 0; goto maybe_reduce;
743 case '+': case '-':
744 /* Is this correct if unary ? FIXME */
745 flags = RIGHT_OPERAND_REQUIRED;
746 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
747 case '!': case '~':
748 flags = RIGHT_OPERAND_REQUIRED;
749 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
750 case '*': case '/': case '%':
751 lprio = MUL_PRIO; goto binop;
752 case '<': case '>': case LEQ: case GEQ:
753 lprio = LESS_PRIO; goto binop;
754 case EQUAL: case NOTEQUAL:
755 lprio = EQUAL_PRIO; goto binop;
756 case LSH: case RSH:
757 lprio = SHIFT_PRIO; goto binop;
758 case '&': lprio = AND_PRIO; goto binop;
759 case '^': lprio = XOR_PRIO; goto binop;
760 case '|': lprio = OR_PRIO; goto binop;
761 case ANDAND: lprio = ANDAND_PRIO; goto binop;
762 case OROR: lprio = OROR_PRIO; goto binop;
763 case ',':
764 lprio = COMMA_PRIO; goto binop;
765 case '(':
766 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
767 goto maybe_reduce;
768 case ')':
769 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
770 goto maybe_reduce;
771 case ':':
772 lprio = COND_PRIO; rprio = COND_PRIO;
773 goto maybe_reduce;
774 case '?':
775 lprio = COND_PRIO + 1; rprio = COND_PRIO;
776 goto maybe_reduce;
777 case ERROR:
778 goto syntax_error;
779 binop:
780 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
781 rprio = lprio + 1;
782 goto maybe_reduce;
783 default:
784 cpp_error (pfile, "invalid character in #if");
785 goto syntax_error;
788 set_value:
789 /* Push a value onto the stack. */
790 if (top->flags & HAVE_VALUE)
792 cpp_error (pfile, "syntax error in #if");
793 goto syntax_error;
795 top->flags |= HAVE_VALUE;
796 continue;
798 maybe_reduce:
799 /* Push an operator, and check if we can reduce now. */
800 while (top->rprio > lprio)
802 HOST_WIDEST_INT v1 = top[-1].value, v2 = top[0].value;
803 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
804 top--;
805 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
806 && ! (top[0].flags & HAVE_VALUE))
808 cpp_error (pfile, "syntax error - missing left operand");
809 goto syntax_error;
811 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
812 && ! (top[1].flags & HAVE_VALUE))
814 cpp_error (pfile, "syntax error - missing right operand");
815 goto syntax_error;
817 /* top[0].value = (top[1].op)(v1, v2);*/
818 switch (top[1].op)
820 case '+':
821 if (!(top->flags & HAVE_VALUE))
822 { /* Unary '+' */
823 top->value = v2;
824 top->unsignedp = unsigned2;
825 top->flags |= HAVE_VALUE;
827 else
829 top->value = v1 + v2;
830 top->unsignedp = unsigned1 || unsigned2;
831 if (! top->unsignedp && ! skip_evaluation
832 && ! possible_sum_sign (v1, v2, top->value))
833 integer_overflow (pfile);
835 break;
836 case '-':
837 if (!(top->flags & HAVE_VALUE))
838 { /* Unary '-' */
839 top->value = - v2;
840 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
841 integer_overflow (pfile);
842 top->unsignedp = unsigned2;
843 top->flags |= HAVE_VALUE;
845 else
846 { /* Binary '-' */
847 top->value = v1 - v2;
848 top->unsignedp = unsigned1 || unsigned2;
849 if (! top->unsignedp && ! skip_evaluation
850 && ! possible_sum_sign (top->value, v2, v1))
851 integer_overflow (pfile);
853 break;
854 case '*':
855 top->unsignedp = unsigned1 || unsigned2;
856 if (top->unsignedp)
857 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
858 else if (!skip_evaluation)
860 top->value = v1 * v2;
861 if (v1
862 && (top->value / v1 != v2
863 || (top->value & v1 & v2) < 0))
864 integer_overflow (pfile);
866 break;
867 case '/':
868 if (skip_evaluation)
869 break;
870 if (v2 == 0)
872 cpp_error (pfile, "division by zero in #if");
873 v2 = 1;
875 top->unsignedp = unsigned1 || unsigned2;
876 if (top->unsignedp)
877 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
878 else
880 top->value = v1 / v2;
881 if ((top->value & v1 & v2) < 0)
882 integer_overflow (pfile);
884 break;
885 case '%':
886 if (skip_evaluation)
887 break;
888 if (v2 == 0)
890 cpp_error (pfile, "division by zero in #if");
891 v2 = 1;
893 top->unsignedp = unsigned1 || unsigned2;
894 if (top->unsignedp)
895 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
896 else
897 top->value = v1 % v2;
898 break;
899 case '!':
900 if (top->flags & HAVE_VALUE)
902 cpp_error (pfile, "syntax error");
903 goto syntax_error;
905 top->value = ! v2;
906 top->unsignedp = 0;
907 top->flags |= HAVE_VALUE;
908 break;
909 case '~':
910 if (top->flags & HAVE_VALUE)
912 cpp_error (pfile, "syntax error");
913 goto syntax_error;
915 top->value = ~ v2;
916 top->unsignedp = unsigned2;
917 top->flags |= HAVE_VALUE;
918 break;
919 case '<': COMPARE(<); break;
920 case '>': COMPARE(>); break;
921 case LEQ: COMPARE(<=); break;
922 case GEQ: COMPARE(>=); break;
923 case EQUAL:
924 top->value = (v1 == v2);
925 top->unsignedp = 0;
926 break;
927 case NOTEQUAL:
928 top->value = (v1 != v2);
929 top->unsignedp = 0;
930 break;
931 case LSH:
932 if (skip_evaluation)
933 break;
934 top->unsignedp = unsigned1;
935 if (v2 < 0 && ! unsigned2)
936 top->value = right_shift (pfile, v1, unsigned1, -v2);
937 else
938 top->value = left_shift (pfile, v1, unsigned1, v2);
939 break;
940 case RSH:
941 if (skip_evaluation)
942 break;
943 top->unsignedp = unsigned1;
944 if (v2 < 0 && ! unsigned2)
945 top->value = left_shift (pfile, v1, unsigned1, -v2);
946 else
947 top->value = right_shift (pfile, v1, unsigned1, v2);
948 break;
949 #define LOGICAL(OP) \
950 top->value = v1 OP v2;\
951 top->unsignedp = unsigned1 || unsigned2;
952 case '&': LOGICAL(&); break;
953 case '^': LOGICAL(^); break;
954 case '|': LOGICAL(|); break;
955 case ANDAND:
956 top->value = v1 && v2; top->unsignedp = 0;
957 if (!v1) skip_evaluation--;
958 break;
959 case OROR:
960 top->value = v1 || v2; top->unsignedp = 0;
961 if (v1) skip_evaluation--;
962 break;
963 case ',':
964 if (CPP_PEDANTIC (pfile))
965 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
966 top->value = v2;
967 top->unsignedp = unsigned2;
968 break;
969 case '(': case '?':
970 cpp_error (pfile, "syntax error in #if");
971 goto syntax_error;
972 case ':':
973 if (top[0].op != '?')
975 cpp_error (pfile,
976 "syntax error ':' without preceding '?'");
977 goto syntax_error;
979 else if (! (top[1].flags & HAVE_VALUE)
980 || !(top[-1].flags & HAVE_VALUE)
981 || !(top[0].flags & HAVE_VALUE))
983 cpp_error (pfile, "bad syntax for ?: operator");
984 goto syntax_error;
986 else
988 top--;
989 if (top->value) skip_evaluation--;
990 top->value = top->value ? v1 : v2;
991 top->unsignedp = unsigned1 || unsigned2;
993 break;
994 case ')':
995 if ((top[1].flags & HAVE_VALUE)
996 || ! (top[0].flags & HAVE_VALUE)
997 || top[0].op != '('
998 || (top[-1].flags & HAVE_VALUE))
1000 cpp_error (pfile, "mismatched parentheses in #if");
1001 goto syntax_error;
1003 else
1005 top--;
1006 top->value = v1;
1007 top->unsignedp = unsigned1;
1008 top->flags |= HAVE_VALUE;
1010 break;
1011 default:
1012 cpp_error (pfile,
1013 (top[1].op >= ' ' && top[1].op <= '~'
1014 ? "unimplemented operator '%c'\n"
1015 : "unimplemented operator '\\%03o'\n"),
1016 top[1].op);
1019 if (op.op == 0)
1021 if (top != stack)
1022 cpp_error (pfile, "internal error in #if expression");
1023 if (stack != init_stack)
1024 free (stack);
1025 return top->value;
1027 top++;
1029 /* Check for and handle stack overflow. */
1030 if (top == limit)
1032 struct operation *new_stack;
1033 int old_size = (char *) limit - (char *) stack;
1034 int new_size = 2 * old_size;
1035 if (stack != init_stack)
1036 new_stack = (struct operation *) xrealloc (stack, new_size);
1037 else
1039 new_stack = (struct operation *) xmalloc (new_size);
1040 bcopy ((char *) stack, (char *) new_stack, old_size);
1042 stack = new_stack;
1043 top = (struct operation *) ((char *) new_stack + old_size);
1044 limit = (struct operation *) ((char *) new_stack + new_size);
1047 top->flags = flags;
1048 top->rprio = rprio;
1049 top->op = op.op;
1050 if ((op.op == OROR && top[-1].value)
1051 || (op.op == ANDAND && !top[-1].value)
1052 || (op.op == '?' && !top[-1].value))
1054 skip_evaluation++;
1056 else if (op.op == ':')
1058 if (top[-2].value) /* Was condition true? */
1059 skip_evaluation++;
1060 else
1061 skip_evaluation--;
1064 syntax_error:
1065 if (stack != init_stack)
1066 free (stack);
1067 skip_rest_of_line (pfile);
1068 return 0;