* class.c (build_class_ref): Changed name of primitive classes to
[official-gcc.git] / gcc / cppexp.c
blob8fc5f04a948068ac15e4c1cb1fe9a140a3db0224
1 /* Parse C expressions for CCCP.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998 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 "gansidecl.h"
30 #include "cpplib.h"
32 extern char *xmalloc PARAMS ((unsigned));
33 extern char *xrealloc PARAMS ((void *, unsigned));
35 #ifdef MULTIBYTE_CHARS
36 #include <locale.h>
37 #endif
39 /* This is used for communicating lists of keywords with cccp.c. */
40 struct arglist {
41 struct arglist *next;
42 U_CHAR *name;
43 int length;
44 int argno;
47 #ifndef CHAR_TYPE_SIZE
48 #define CHAR_TYPE_SIZE BITS_PER_UNIT
49 #endif
51 #ifndef INT_TYPE_SIZE
52 #define INT_TYPE_SIZE BITS_PER_WORD
53 #endif
55 #ifndef LONG_TYPE_SIZE
56 #define LONG_TYPE_SIZE BITS_PER_WORD
57 #endif
59 #ifndef WCHAR_TYPE_SIZE
60 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
61 #endif
63 #ifndef MAX_CHAR_TYPE_SIZE
64 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
65 #endif
67 #ifndef MAX_INT_TYPE_SIZE
68 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
69 #endif
71 #ifndef MAX_LONG_TYPE_SIZE
72 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
73 #endif
75 #ifndef MAX_WCHAR_TYPE_SIZE
76 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
77 #endif
79 /* Yield nonzero if adding two numbers with A's and B's signs can yield a
80 number with SUM's sign, where A, B, and SUM are all C integers. */
81 #define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
83 static void integer_overflow PARAMS ((cpp_reader *));
84 static long left_shift PARAMS ((cpp_reader *, long, int, unsigned long));
85 static long right_shift PARAMS ((cpp_reader *, long, int, unsigned long));
87 #define ERROR 299
88 #define OROR 300
89 #define ANDAND 301
90 #define EQUAL 302
91 #define NOTEQUAL 303
92 #define LEQ 304
93 #define GEQ 305
94 #define LSH 306
95 #define RSH 307
96 #define NAME 308
97 #define INT 309
98 #define CHAR 310
100 #define LEFT_OPERAND_REQUIRED 1
101 #define RIGHT_OPERAND_REQUIRED 2
102 #define HAVE_VALUE 4
103 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
104 following operand should be short-circuited instead of evaluated. */
105 #define SKIP_OPERAND 8
106 /*#define UNSIGNEDP 16*/
108 /* Find the largest host integer type and set its size and type.
109 Watch out: on some crazy hosts `long' is shorter than `int'. */
111 #ifndef HOST_WIDE_INT
112 # if HAVE_INTTYPES_H
113 # include <inttypes.h>
114 # define HOST_WIDE_INT intmax_t
115 # else
116 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
117 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
118 # define HOST_WIDE_INT int
119 # else
120 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
121 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
122 # define HOST_WIDE_INT long
123 # else
124 # define HOST_WIDE_INT long long
125 # endif
126 # endif
127 # endif
128 #endif
130 #ifndef CHAR_BIT
131 #define CHAR_BIT 8
132 #endif
134 #ifndef HOST_BITS_PER_WIDE_INT
135 #define HOST_BITS_PER_WIDE_INT (CHAR_BIT * sizeof (HOST_WIDE_INT))
136 #endif
138 struct operation {
139 short op;
140 char rprio; /* Priority of op (relative to it right operand). */
141 char flags;
142 char unsignedp; /* true if value should be treated as unsigned */
143 HOST_WIDE_INT value; /* The value logically "right" of op. */
146 /* Take care of parsing a number (anything that starts with a digit).
147 LEN is the number of characters in it. */
149 /* maybe needs to actually deal with floating point numbers */
151 struct operation
152 parse_number (pfile, start, olen)
153 cpp_reader *pfile;
154 char *start;
155 int olen;
157 struct operation op;
158 register char *p = start;
159 register int c;
160 register unsigned long n = 0, nd, ULONG_MAX_over_base;
161 register int base = 10;
162 register int len = olen;
163 register int overflow = 0;
164 register int digit, largest_digit = 0;
165 int spec_long = 0;
167 op.unsignedp = 0;
169 for (c = 0; c < len; c++)
170 if (p[c] == '.') {
171 /* It's a float since it contains a point. */
172 cpp_error (pfile,
173 "floating point numbers not allowed in #if expressions");
174 op.op = ERROR;
175 return op;
178 if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2))) {
179 p += 2;
180 base = 16;
181 len -= 2;
183 else if (*p == '0')
184 base = 8;
186 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
187 ULONG_MAX_over_base = ((unsigned long) -1) / ((unsigned long) base);
189 for (; len > 0; len--) {
190 c = *p++;
192 if (c >= '0' && c <= '9')
193 digit = c - '0';
194 else if (base == 16 && c >= 'a' && c <= 'f')
195 digit = c - 'a' + 10;
196 else if (base == 16 && c >= 'A' && c <= 'F')
197 digit = c - 'A' + 10;
198 else {
199 /* `l' means long, and `u' means unsigned. */
200 while (1) {
201 if (c == 'l' || c == 'L')
203 if (spec_long)
204 cpp_error (pfile, "two `l's in integer constant");
205 spec_long = 1;
207 else if (c == 'u' || c == 'U')
209 if (op.unsignedp)
210 cpp_error (pfile, "two `u's in integer constant");
211 op.unsignedp = 1;
213 else
214 break;
216 if (--len == 0)
217 break;
218 c = *p++;
220 /* Don't look for any more digits after the suffixes. */
221 break;
223 if (largest_digit < digit)
224 largest_digit = digit;
225 nd = n * base + digit;
226 overflow |= ULONG_MAX_over_base < n || nd < n;
227 n = nd;
230 if (len != 0)
232 cpp_error (pfile, "Invalid number in #if expression");
233 op.op = ERROR;
234 return op;
237 if (base <= largest_digit)
238 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
240 if (overflow)
241 cpp_pedwarn (pfile, "integer constant out of range");
243 /* If too big to be signed, consider it unsigned. */
244 if ((long) n < 0 && ! op.unsignedp)
246 if (base == 10)
247 cpp_warning (pfile, "integer constant is so large that it is unsigned");
248 op.unsignedp = 1;
251 op.value = n;
252 op.op = INT;
253 return op;
256 struct token {
257 char *operator;
258 int token;
261 static struct token tokentab2[] = {
262 {"&&", ANDAND},
263 {"||", OROR},
264 {"<<", LSH},
265 {">>", RSH},
266 {"==", EQUAL},
267 {"!=", NOTEQUAL},
268 {"<=", LEQ},
269 {">=", GEQ},
270 {"++", ERROR},
271 {"--", ERROR},
272 {NULL, ERROR}
275 /* Read one token. */
277 struct operation
278 cpp_lex (pfile, skip_evaluation)
279 cpp_reader *pfile;
280 int skip_evaluation;
282 register int c;
283 register struct token *toktab;
284 enum cpp_token token;
285 struct operation op;
286 U_CHAR *tok_start, *tok_end;
287 int old_written;
289 retry:
291 old_written = CPP_WRITTEN (pfile);
292 cpp_skip_hspace (pfile);
293 c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
294 if (c == '#')
295 return parse_number (pfile,
296 cpp_read_check_assertion (pfile) ? "1" : "0", 1);
298 if (c == '\n')
300 op.op = 0;
301 return op;
304 token = cpp_get_token (pfile);
305 tok_start = pfile->token_buffer + old_written;
306 tok_end = CPP_PWRITTEN (pfile);
307 pfile->limit = tok_start;
308 switch (token)
310 case CPP_EOF: /* Should not happen ... */
311 case CPP_VSPACE:
312 op.op = 0;
313 return op;
314 case CPP_POP:
315 if (CPP_BUFFER (pfile)->fname != NULL)
317 op.op = 0;
318 return op;
320 cpp_pop_buffer (pfile);
321 goto retry;
322 case CPP_HSPACE: case CPP_COMMENT:
323 goto retry;
324 case CPP_NUMBER:
325 return parse_number (pfile, tok_start, tok_end - tok_start);
326 case CPP_STRING:
327 cpp_error (pfile, "string constants not allowed in #if expressions");
328 op.op = ERROR;
329 return op;
330 case CPP_CHAR:
331 /* This code for reading a character constant
332 handles multicharacter constants and wide characters.
333 It is mostly copied from c-lex.c. */
335 register int result = 0;
336 register int num_chars = 0;
337 unsigned width = MAX_CHAR_TYPE_SIZE;
338 int wide_flag = 0;
339 int max_chars;
340 U_CHAR *ptr = tok_start;
341 #ifdef MULTIBYTE_CHARS
342 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + MB_CUR_MAX];
343 #else
344 char token_buffer[MAX_LONG_TYPE_SIZE/MAX_CHAR_TYPE_SIZE + 1];
345 #endif
347 if (*ptr == 'L')
349 ptr++;
350 wide_flag = 1;
351 width = MAX_WCHAR_TYPE_SIZE;
352 #ifdef MULTIBYTE_CHARS
353 max_chars = MB_CUR_MAX;
354 #else
355 max_chars = 1;
356 #endif
358 else
359 max_chars = MAX_LONG_TYPE_SIZE / width;
361 ++ptr;
362 while (ptr < tok_end && ((c = *ptr++) != '\''))
364 if (c == '\\')
366 c = cpp_parse_escape (pfile, (char **) &ptr);
367 if (width < HOST_BITS_PER_INT
368 && (unsigned) c >= (unsigned)(1 << width))
369 cpp_pedwarn (pfile,
370 "escape sequence out of range for character");
373 num_chars++;
375 /* Merge character into result; ignore excess chars. */
376 if (num_chars < max_chars + 1)
378 if (width < HOST_BITS_PER_INT)
379 result = (result << width) | (c & ((1 << width) - 1));
380 else
381 result = c;
382 token_buffer[num_chars - 1] = c;
386 token_buffer[num_chars] = 0;
388 if (c != '\'')
389 cpp_error (pfile, "malformatted character constant");
390 else if (num_chars == 0)
391 cpp_error (pfile, "empty character constant");
392 else if (num_chars > max_chars)
394 num_chars = max_chars;
395 cpp_error (pfile, "character constant too long");
397 else if (num_chars != 1 && ! CPP_TRADITIONAL (pfile))
398 cpp_warning (pfile, "multi-character character constant");
400 /* If char type is signed, sign-extend the constant. */
401 if (! wide_flag)
403 int num_bits = num_chars * width;
405 if (cpp_lookup (pfile, (U_CHAR *)"__CHAR_UNSIGNED__",
406 sizeof ("__CHAR_UNSIGNED__")-1, -1)
407 || ((result >> (num_bits - 1)) & 1) == 0)
408 op.value
409 = result & ((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
410 else
411 op.value
412 = result | ~((unsigned long) ~0 >> (HOST_BITS_PER_LONG - num_bits));
414 else
416 #ifdef MULTIBYTE_CHARS
417 /* Set the initial shift state and convert the next sequence. */
418 result = 0;
419 /* In all locales L'\0' is zero and mbtowc will return zero,
420 so don't use it. */
421 if (num_chars > 1
422 || (num_chars == 1 && token_buffer[0] != '\0'))
424 wchar_t wc;
425 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
426 if (mbtowc (& wc, token_buffer, num_chars) == num_chars)
427 result = wc;
428 else
429 cpp_pedwarn (pfile,"Ignoring invalid multibyte character");
431 #endif
432 op.value = result;
436 /* This is always a signed type. */
437 op.unsignedp = 0;
438 op.op = CHAR;
440 return op;
442 case CPP_NAME:
443 if (CPP_WARN_UNDEF (pfile) && !skip_evaluation)
444 cpp_warning (pfile, "`%.*s' is not defined",
445 (int) (tok_end - tok_start), tok_start);
446 return parse_number (pfile, "0", 0);
448 case CPP_OTHER:
449 /* See if it is a special token of length 2. */
450 if (tok_start + 2 == tok_end)
452 for (toktab = tokentab2; toktab->operator != NULL; toktab++)
453 if (tok_start[0] == toktab->operator[0]
454 && tok_start[1] == toktab->operator[1])
455 break;
456 if (toktab->token == ERROR)
458 char *buf = (char *) alloca (40);
459 sprintf (buf, "`%s' not allowed in operand of `#if'", tok_start);
460 cpp_error (pfile, buf);
462 op.op = toktab->token;
463 return op;
465 /* fall through */
466 default:
467 op.op = *tok_start;
468 return op;
473 /* Parse a C escape sequence. STRING_PTR points to a variable
474 containing a pointer to the string to parse. That pointer
475 is updated past the characters we use. The value of the
476 escape sequence is returned.
478 A negative value means the sequence \ newline was seen,
479 which is supposed to be equivalent to nothing at all.
481 If \ is followed by a null character, we return a negative
482 value and leave the string pointer pointing at the null character.
484 If \ is followed by 000, we return 0 and leave the string pointer
485 after the zeros. A value of 0 does not mean end of string. */
488 cpp_parse_escape (pfile, string_ptr)
489 cpp_reader *pfile;
490 char **string_ptr;
492 register int c = *(*string_ptr)++;
493 switch (c)
495 case 'a':
496 return TARGET_BELL;
497 case 'b':
498 return TARGET_BS;
499 case 'e':
500 case 'E':
501 if (CPP_PEDANTIC (pfile))
502 cpp_pedwarn (pfile, "non-ANSI-standard escape sequence, `\\%c'", c);
503 return 033;
504 case 'f':
505 return TARGET_FF;
506 case 'n':
507 return TARGET_NEWLINE;
508 case 'r':
509 return TARGET_CR;
510 case 't':
511 return TARGET_TAB;
512 case 'v':
513 return TARGET_VT;
514 case '\n':
515 return -2;
516 case 0:
517 (*string_ptr)--;
518 return 0;
520 case '0':
521 case '1':
522 case '2':
523 case '3':
524 case '4':
525 case '5':
526 case '6':
527 case '7':
529 register int i = c - '0';
530 register int count = 0;
531 while (++count < 3)
533 c = *(*string_ptr)++;
534 if (c >= '0' && c <= '7')
535 i = (i << 3) + c - '0';
536 else
538 (*string_ptr)--;
539 break;
542 if ((i & ~((1 << MAX_CHAR_TYPE_SIZE) - 1)) != 0)
544 i &= (1 << MAX_CHAR_TYPE_SIZE) - 1;
545 cpp_pedwarn (pfile,
546 "octal character constant does not fit in a byte");
548 return i;
550 case 'x':
552 register unsigned i = 0, overflow = 0, digits_found = 0, digit;
553 for (;;)
555 c = *(*string_ptr)++;
556 if (c >= '0' && c <= '9')
557 digit = c - '0';
558 else if (c >= 'a' && c <= 'f')
559 digit = c - 'a' + 10;
560 else if (c >= 'A' && c <= 'F')
561 digit = c - 'A' + 10;
562 else
564 (*string_ptr)--;
565 break;
567 overflow |= i ^ (i << 4 >> 4);
568 i = (i << 4) + digit;
569 digits_found = 1;
571 if (!digits_found)
572 cpp_error (pfile, "\\x used with no following hex digits");
573 if (overflow | (i & ~((1 << BITS_PER_UNIT) - 1)))
575 i &= (1 << BITS_PER_UNIT) - 1;
576 cpp_pedwarn (pfile,
577 "hex character constant does not fit in a byte");
579 return i;
581 default:
582 return c;
586 static void
587 integer_overflow (pfile)
588 cpp_reader *pfile;
590 if (CPP_PEDANTIC (pfile))
591 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
594 static long
595 left_shift (pfile, a, unsignedp, b)
596 cpp_reader *pfile;
597 long a;
598 int unsignedp;
599 unsigned long b;
601 if (b >= HOST_BITS_PER_LONG)
603 if (! unsignedp && a != 0)
604 integer_overflow (pfile);
605 return 0;
607 else if (unsignedp)
608 return (unsigned long) a << b;
609 else
611 long l = a << b;
612 if (l >> b != a)
613 integer_overflow (pfile);
614 return l;
618 static long
619 right_shift (pfile, a, unsignedp, b)
620 cpp_reader *pfile ATTRIBUTE_UNUSED;
621 long a;
622 int unsignedp;
623 unsigned long b;
625 if (b >= HOST_BITS_PER_LONG)
626 return unsignedp ? 0 : a >> (HOST_BITS_PER_LONG - 1);
627 else if (unsignedp)
628 return (unsigned long) a >> b;
629 else
630 return a >> b;
633 /* These priorities are all even, so we can handle associatively. */
634 #define PAREN_INNER_PRIO 0
635 #define COMMA_PRIO 4
636 #define COND_PRIO (COMMA_PRIO+2)
637 #define OROR_PRIO (COND_PRIO+2)
638 #define ANDAND_PRIO (OROR_PRIO+2)
639 #define OR_PRIO (ANDAND_PRIO+2)
640 #define XOR_PRIO (OR_PRIO+2)
641 #define AND_PRIO (XOR_PRIO+2)
642 #define EQUAL_PRIO (AND_PRIO+2)
643 #define LESS_PRIO (EQUAL_PRIO+2)
644 #define SHIFT_PRIO (LESS_PRIO+2)
645 #define PLUS_PRIO (SHIFT_PRIO+2)
646 #define MUL_PRIO (PLUS_PRIO+2)
647 #define UNARY_PRIO (MUL_PRIO+2)
648 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
650 #define COMPARE(OP) \
651 top->unsignedp = 0;\
652 top->value = (unsigned1 || unsigned2) \
653 ? (unsigned long) v1 OP (unsigned long) v2 : (v1 OP v2)
655 /* Parse and evaluate a C expression, reading from PFILE.
656 Returns the value of the expression. */
658 HOST_WIDE_INT
659 cpp_parse_expr (pfile)
660 cpp_reader *pfile;
662 /* The implementation is an operator precedence parser,
663 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
665 The stack base is 'stack', and the current stack pointer is 'top'.
666 There is a stack element for each operator (only),
667 and the most recently pushed operator is 'top->op'.
668 An operand (value) is stored in the 'value' field of the stack
669 element of the operator that precedes it.
670 In that case the 'flags' field has the HAVE_VALUE flag set. */
672 #define INIT_STACK_SIZE 20
673 struct operation init_stack[INIT_STACK_SIZE];
674 struct operation *stack = init_stack;
675 struct operation *limit = stack + INIT_STACK_SIZE;
676 register struct operation *top = stack;
677 int lprio, rprio;
678 int skip_evaluation = 0;
680 top->rprio = 0;
681 top->flags = 0;
682 for (;;)
684 struct operation op;
685 char flags = 0;
687 /* Read a token */
688 op = cpp_lex (pfile, skip_evaluation);
690 /* See if the token is an operand, in which case go to set_value.
691 If the token is an operator, figure out its left and right
692 priorities, and then goto maybe_reduce. */
694 switch (op.op)
696 case NAME:
697 abort ();
698 case INT: case CHAR:
699 top->value = op.value;
700 top->unsignedp = op.unsignedp;
701 goto set_value;
702 case 0:
703 lprio = 0; goto maybe_reduce;
704 case '+': case '-':
705 /* Is this correct if unary ? FIXME */
706 flags = RIGHT_OPERAND_REQUIRED;
707 lprio = PLUS_PRIO; rprio = lprio + 1; goto maybe_reduce;
708 case '!': case '~':
709 flags = RIGHT_OPERAND_REQUIRED;
710 rprio = UNARY_PRIO; lprio = rprio + 1; goto maybe_reduce;
711 case '*': case '/': case '%':
712 lprio = MUL_PRIO; goto binop;
713 case '<': case '>': case LEQ: case GEQ:
714 lprio = LESS_PRIO; goto binop;
715 case EQUAL: case NOTEQUAL:
716 lprio = EQUAL_PRIO; goto binop;
717 case LSH: case RSH:
718 lprio = SHIFT_PRIO; goto binop;
719 case '&': lprio = AND_PRIO; goto binop;
720 case '^': lprio = XOR_PRIO; goto binop;
721 case '|': lprio = OR_PRIO; goto binop;
722 case ANDAND: lprio = ANDAND_PRIO; goto binop;
723 case OROR: lprio = OROR_PRIO; goto binop;
724 case ',':
725 lprio = COMMA_PRIO; goto binop;
726 case '(':
727 lprio = PAREN_OUTER_PRIO; rprio = PAREN_INNER_PRIO;
728 goto maybe_reduce;
729 case ')':
730 lprio = PAREN_INNER_PRIO; rprio = PAREN_OUTER_PRIO;
731 goto maybe_reduce;
732 case ':':
733 lprio = COND_PRIO; rprio = COND_PRIO;
734 goto maybe_reduce;
735 case '?':
736 lprio = COND_PRIO + 1; rprio = COND_PRIO;
737 goto maybe_reduce;
738 binop:
739 flags = LEFT_OPERAND_REQUIRED|RIGHT_OPERAND_REQUIRED;
740 rprio = lprio + 1;
741 goto maybe_reduce;
742 default:
743 cpp_error (pfile, "invalid character in #if");
744 goto syntax_error;
747 set_value:
748 /* Push a value onto the stack. */
749 if (top->flags & HAVE_VALUE)
751 cpp_error (pfile, "syntax error in #if");
752 goto syntax_error;
754 top->flags |= HAVE_VALUE;
755 continue;
757 maybe_reduce:
758 /* Push an operator, and check if we can reduce now. */
759 while (top->rprio > lprio)
761 long v1 = top[-1].value, v2 = top[0].value;
762 int unsigned1 = top[-1].unsignedp, unsigned2 = top[0].unsignedp;
763 top--;
764 if ((top[1].flags & LEFT_OPERAND_REQUIRED)
765 && ! (top[0].flags & HAVE_VALUE))
767 cpp_error (pfile, "syntax error - missing left operand");
768 goto syntax_error;
770 if ((top[1].flags & RIGHT_OPERAND_REQUIRED)
771 && ! (top[1].flags & HAVE_VALUE))
773 cpp_error (pfile, "syntax error - missing right operand");
774 goto syntax_error;
776 /* top[0].value = (top[1].op)(v1, v2);*/
777 switch (top[1].op)
779 case '+':
780 if (!(top->flags & HAVE_VALUE))
781 { /* Unary '+' */
782 top->value = v2;
783 top->unsignedp = unsigned2;
784 top->flags |= HAVE_VALUE;
786 else
788 top->value = v1 + v2;
789 top->unsignedp = unsigned1 || unsigned2;
790 if (! top->unsignedp && ! skip_evaluation
791 && ! possible_sum_sign (v1, v2, top->value))
792 integer_overflow (pfile);
794 break;
795 case '-':
796 if (!(top->flags & HAVE_VALUE))
797 { /* Unary '-' */
798 top->value = - v2;
799 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
800 integer_overflow (pfile);
801 top->unsignedp = unsigned2;
802 top->flags |= HAVE_VALUE;
804 else
805 { /* Binary '-' */
806 top->value = v1 - v2;
807 top->unsignedp = unsigned1 || unsigned2;
808 if (! top->unsignedp && ! skip_evaluation
809 && ! possible_sum_sign (top->value, v2, v1))
810 integer_overflow (pfile);
812 break;
813 case '*':
814 top->unsignedp = unsigned1 || unsigned2;
815 if (top->unsignedp)
816 top->value = (unsigned long) v1 * v2;
817 else if (!skip_evaluation)
819 top->value = v1 * v2;
820 if (v1
821 && (top->value / v1 != v2
822 || (top->value & v1 & v2) < 0))
823 integer_overflow (pfile);
825 break;
826 case '/':
827 if (skip_evaluation)
828 break;
829 if (v2 == 0)
831 cpp_error (pfile, "division by zero in #if");
832 v2 = 1;
834 top->unsignedp = unsigned1 || unsigned2;
835 if (top->unsignedp)
836 top->value = (unsigned long) v1 / v2;
837 else
839 top->value = v1 / v2;
840 if ((top->value & v1 & v2) < 0)
841 integer_overflow (pfile);
843 break;
844 case '%':
845 if (skip_evaluation)
846 break;
847 if (v2 == 0)
849 cpp_error (pfile, "division by zero in #if");
850 v2 = 1;
852 top->unsignedp = unsigned1 || unsigned2;
853 if (top->unsignedp)
854 top->value = (unsigned long) v1 % v2;
855 else
856 top->value = v1 % v2;
857 break;
858 case '!':
859 if (top->flags & HAVE_VALUE)
861 cpp_error (pfile, "syntax error");
862 goto syntax_error;
864 top->value = ! v2;
865 top->unsignedp = 0;
866 top->flags |= HAVE_VALUE;
867 break;
868 case '~':
869 if (top->flags & HAVE_VALUE)
871 cpp_error (pfile, "syntax error");
872 goto syntax_error;
874 top->value = ~ v2;
875 top->unsignedp = unsigned2;
876 top->flags |= HAVE_VALUE;
877 break;
878 case '<': COMPARE(<); break;
879 case '>': COMPARE(>); break;
880 case LEQ: COMPARE(<=); break;
881 case GEQ: COMPARE(>=); break;
882 case EQUAL:
883 top->value = (v1 == v2);
884 top->unsignedp = 0;
885 break;
886 case NOTEQUAL:
887 top->value = (v1 != v2);
888 top->unsignedp = 0;
889 break;
890 case LSH:
891 if (skip_evaluation)
892 break;
893 top->unsignedp = unsigned1;
894 if (v2 < 0 && ! unsigned2)
895 top->value = right_shift (pfile, v1, unsigned1, -v2);
896 else
897 top->value = left_shift (pfile, v1, unsigned1, v2);
898 break;
899 case RSH:
900 if (skip_evaluation)
901 break;
902 top->unsignedp = unsigned1;
903 if (v2 < 0 && ! unsigned2)
904 top->value = left_shift (pfile, v1, unsigned1, -v2);
905 else
906 top->value = right_shift (pfile, v1, unsigned1, v2);
907 break;
908 #define LOGICAL(OP) \
909 top->value = v1 OP v2;\
910 top->unsignedp = unsigned1 || unsigned2;
911 case '&': LOGICAL(&); break;
912 case '^': LOGICAL(^); break;
913 case '|': LOGICAL(|); break;
914 case ANDAND:
915 top->value = v1 && v2; top->unsignedp = 0;
916 if (!v1) skip_evaluation--;
917 break;
918 case OROR:
919 top->value = v1 || v2; top->unsignedp = 0;
920 if (v1) skip_evaluation--;
921 break;
922 case ',':
923 if (CPP_PEDANTIC (pfile))
924 cpp_pedwarn (pfile, "comma operator in operand of `#if'");
925 top->value = v2;
926 top->unsignedp = unsigned2;
927 break;
928 case '(': case '?':
929 cpp_error (pfile, "syntax error in #if");
930 goto syntax_error;
931 case ':':
932 if (top[0].op != '?')
934 cpp_error (pfile,
935 "syntax error ':' without preceding '?'");
936 goto syntax_error;
938 else if (! (top[1].flags & HAVE_VALUE)
939 || !(top[-1].flags & HAVE_VALUE)
940 || !(top[0].flags & HAVE_VALUE))
942 cpp_error (pfile, "bad syntax for ?: operator");
943 goto syntax_error;
945 else
947 top--;
948 if (top->value) skip_evaluation--;
949 top->value = top->value ? v1 : v2;
950 top->unsignedp = unsigned1 || unsigned2;
952 break;
953 case ')':
954 if ((top[1].flags & HAVE_VALUE)
955 || ! (top[0].flags & HAVE_VALUE)
956 || top[0].op != '('
957 || (top[-1].flags & HAVE_VALUE))
959 cpp_error (pfile, "mismatched parentheses in #if");
960 goto syntax_error;
962 else
964 top--;
965 top->value = v1;
966 top->unsignedp = unsigned1;
967 top->flags |= HAVE_VALUE;
969 break;
970 default:
971 fprintf (stderr,
972 top[1].op >= ' ' && top[1].op <= '~'
973 ? "unimplemented operator '%c'\n"
974 : "unimplemented operator '\\%03o'\n",
975 top[1].op);
978 if (op.op == 0)
980 if (top != stack)
981 cpp_error (pfile, "internal error in #if expression");
982 if (stack != init_stack)
983 free (stack);
984 return top->value;
986 top++;
988 /* Check for and handle stack overflow. */
989 if (top == limit)
991 struct operation *new_stack;
992 int old_size = (char *) limit - (char *) stack;
993 int new_size = 2 * old_size;
994 if (stack != init_stack)
995 new_stack = (struct operation *) xrealloc (stack, new_size);
996 else
998 new_stack = (struct operation *) xmalloc (new_size);
999 bcopy ((char *) stack, (char *) new_stack, old_size);
1001 stack = new_stack;
1002 top = (struct operation *) ((char *) new_stack + old_size);
1003 limit = (struct operation *) ((char *) new_stack + new_size);
1006 top->flags = flags;
1007 top->rprio = rprio;
1008 top->op = op.op;
1009 if ((op.op == OROR && top[-1].value)
1010 || (op.op == ANDAND && !top[-1].value)
1011 || (op.op == '?' && !top[-1].value))
1013 skip_evaluation++;
1015 else if (op.op == ':')
1017 if (top[-2].value) /* Was condition true? */
1018 skip_evaluation++;
1019 else
1020 skip_evaluation--;
1023 syntax_error:
1024 if (stack != init_stack)
1025 free (stack);
1026 skip_rest_of_line (pfile);
1027 return 0;