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
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 */
32 #ifdef MULTIBYTE_CHARS
36 #ifndef CHAR_TYPE_SIZE
37 #define CHAR_TYPE_SIZE BITS_PER_UNIT
41 #define INT_TYPE_SIZE BITS_PER_WORD
44 #ifndef LONG_TYPE_SIZE
45 #define LONG_TYPE_SIZE BITS_PER_WORD
48 #ifndef WCHAR_TYPE_SIZE
49 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
52 #ifndef MAX_CHAR_TYPE_SIZE
53 #define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
56 #ifndef MAX_INT_TYPE_SIZE
57 #define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
60 #ifndef MAX_LONG_TYPE_SIZE
61 #define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
64 #ifndef MAX_WCHAR_TYPE_SIZE
65 #define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
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
*));
101 #define LEFT_OPERAND_REQUIRED 1
102 #define RIGHT_OPERAND_REQUIRED 2
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*/
111 char rprio
; /* Priority of op (relative to it right operand). */
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
)
129 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
132 int digit
, largest_digit
= 0;
139 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
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
));
159 if (c
>= '0' && c
<= '9')
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;
167 /* It's a float since it contains a point. */
169 "floating point numbers are not allowed in #if expressions");
174 /* `l' means long, and `u' means unsigned. */
177 if (c
== 'l' || c
== 'L')
179 else if (c
== 'u' || c
== 'U')
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. */
193 /* Don't look for any more digits after the suffixes. */
197 if (largest_digit
< digit
)
198 largest_digit
= digit
;
199 nd
= n
* base
+ digit
;
200 overflow
|= MAX_over_base
< n
|| nd
< n
;
206 cpp_error (pfile
, "invalid number in #if expression");
209 else if (spec_long
> (CPP_OPTIONS (pfile
)->c89
? 1 : 2))
211 cpp_error (pfile
, "too many `l' suffixes in integer constant");
214 else if (op
.unsignedp
> 1)
216 cpp_error (pfile
, "too many `u' suffixes in integer constant");
220 if (base
<= largest_digit
)
221 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
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
)
231 "integer constant is so large that it is unsigned");
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
)
253 HOST_WIDEST_INT result
= 0;
256 unsigned int width
= MAX_CHAR_TYPE_SIZE
, mask
= MAX_CHAR_TYPE_MASK
;
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);
271 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
273 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
275 ++ptr
; /* skip initial quote */
279 #ifndef MULTIBYTE_CHARS
282 ptr
+= mbtowc (&c
, ptr
, end
- ptr
);
284 if (c
== '\'' || c
== '\0')
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));
312 cpp_error (pfile
, "empty character constant");
317 /* cpp_get_token has already emitted an error if !traditional. */
318 if (! CPP_TRADITIONAL (pfile
))
319 cpp_error (pfile
, "malformatted character constant");
322 else if (num_chars
> max_chars
)
324 cpp_error (pfile
, "character constant too long");
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
));
339 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
340 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
342 /* This is always a signed type. */
354 const char *operator;
358 static struct token tokentab2
[] = {
372 /* Read one token. */
374 static struct operation
375 cpp_lex (pfile
, skip_evaluation
)
380 struct token
*toktab
;
381 enum cpp_token token
;
383 U_CHAR
*tok_start
, *tok_end
;
388 old_written
= CPP_WRITTEN (pfile
);
389 cpp_skip_hspace (pfile
);
390 c
= CPP_BUF_PEEK (CPP_BUFFER (pfile
));
394 op
.value
= cpp_read_check_assertion (pfile
);
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
;
410 case CPP_EOF
: /* Should not happen ... */
415 if (CPP_BUFFER (pfile
)->fname
!= NULL
)
420 cpp_pop_buffer (pfile
);
426 return parse_number (pfile
, tok_start
, tok_end
);
428 cpp_error (pfile
, "string constants not allowed in #if expressions");
432 return parse_charconst (pfile
, tok_start
, tok_end
);
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
);
447 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
451 cpp_skip_hspace (pfile
);
455 ip
->cur
++; /* Skip over the paren */
456 cpp_skip_hspace (pfile
);
459 if (!is_idstart
[*ip
->cur
])
461 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
464 while (is_idchar
[*ip
->cur
])
467 cpp_skip_hspace (pfile
);
474 hp
= cpp_lookup (pfile
, tok
, len
, -1);
477 if (hp
->type
== T_POISON
)
478 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
486 cpp_error (pfile
, "`defined' without an identifier");
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])
497 if (toktab
->token
== ERROR
)
498 cpp_error (pfile
, "`%s' not allowed in operand of `#if'",
500 op
.op
= toktab
->token
;
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. */
526 cpp_parse_escape (pfile
, string_ptr
, result_mask
)
529 HOST_WIDEST_INT result_mask
;
531 register int c
= *(*string_ptr
)++;
540 if (CPP_OPTIONS (pfile
)->pedantic
)
541 cpp_pedwarn (pfile
, "non-ANSI-standard escape sequence, `\\%c'", c
);
546 return TARGET_NEWLINE
;
568 register HOST_WIDEST_INT i
= c
- '0';
569 register int count
= 0;
572 c
= *(*string_ptr
)++;
573 if (c
>= '0' && c
<= '7')
574 i
= (i
<< 3) + c
- '0';
581 if (i
!= (i
& result_mask
))
584 cpp_pedwarn (pfile
, "octal escape sequence out of range");
590 register unsigned HOST_WIDEST_INT i
= 0, overflow
= 0;
591 register int digits_found
= 0, digit
;
594 c
= *(*string_ptr
)++;
595 if (c
>= '0' && c
<= '9')
597 else if (c
>= 'a' && c
<= 'f')
598 digit
= c
- 'a' + 10;
599 else if (c
>= 'A' && c
<= 'F')
600 digit
= c
- 'A' + 10;
606 overflow
|= i
^ (i
<< 4 >> 4);
607 i
= (i
<< 4) + digit
;
611 cpp_error (pfile
, "\\x used with no following hex digits");
612 if (overflow
| (i
!= (i
& result_mask
)))
615 cpp_pedwarn (pfile
, "hex escape sequence out of range");
625 integer_overflow (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
)
637 unsigned HOST_WIDEST_INT b
;
639 if (b
>= HOST_BITS_PER_WIDEST_INT
)
641 if (! unsignedp
&& a
!= 0)
642 integer_overflow (pfile
);
646 return (unsigned HOST_WIDEST_INT
) a
<< b
;
649 HOST_WIDEST_INT l
= a
<< b
;
651 integer_overflow (pfile
);
656 static HOST_WIDEST_INT
657 right_shift (pfile
, a
, unsignedp
, b
)
658 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
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);
666 return (unsigned HOST_WIDEST_INT
) a
>> b
;
671 /* These priorities are all even, so we can handle associatively. */
672 #define PAREN_INNER_PRIO 0
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) \
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. */
697 cpp_parse_expr (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;
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. */
735 cpp_fatal (pfile
, "internal error: cpp_lex returns a NAME");
738 top
->value
= op
.value
;
739 top
->unsignedp
= op
.unsignedp
;
742 lprio
= 0; goto maybe_reduce
;
744 /* Is this correct if unary ? FIXME */
745 flags
= RIGHT_OPERAND_REQUIRED
;
746 lprio
= PLUS_PRIO
; rprio
= lprio
+ 1; goto maybe_reduce
;
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
;
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
;
764 lprio
= COMMA_PRIO
; goto binop
;
766 lprio
= PAREN_OUTER_PRIO
; rprio
= PAREN_INNER_PRIO
;
769 lprio
= PAREN_INNER_PRIO
; rprio
= PAREN_OUTER_PRIO
;
772 lprio
= COND_PRIO
; rprio
= COND_PRIO
;
775 lprio
= COND_PRIO
+ 1; rprio
= COND_PRIO
;
780 flags
= LEFT_OPERAND_REQUIRED
|RIGHT_OPERAND_REQUIRED
;
784 cpp_error (pfile
, "invalid character in #if");
789 /* Push a value onto the stack. */
790 if (top
->flags
& HAVE_VALUE
)
792 cpp_error (pfile
, "syntax error in #if");
795 top
->flags
|= HAVE_VALUE
;
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
;
805 if ((top
[1].flags
& LEFT_OPERAND_REQUIRED
)
806 && ! (top
[0].flags
& HAVE_VALUE
))
808 cpp_error (pfile
, "syntax error - missing left operand");
811 if ((top
[1].flags
& RIGHT_OPERAND_REQUIRED
)
812 && ! (top
[1].flags
& HAVE_VALUE
))
814 cpp_error (pfile
, "syntax error - missing right operand");
817 /* top[0].value = (top[1].op)(v1, v2);*/
821 if (!(top
->flags
& HAVE_VALUE
))
824 top
->unsignedp
= unsigned2
;
825 top
->flags
|= HAVE_VALUE
;
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
);
837 if (!(top
->flags
& HAVE_VALUE
))
840 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
841 integer_overflow (pfile
);
842 top
->unsignedp
= unsigned2
;
843 top
->flags
|= HAVE_VALUE
;
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
);
855 top
->unsignedp
= unsigned1
|| unsigned2
;
857 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
858 else if (!skip_evaluation
)
860 top
->value
= v1
* v2
;
862 && (top
->value
/ v1
!= v2
863 || (top
->value
& v1
& v2
) < 0))
864 integer_overflow (pfile
);
872 cpp_error (pfile
, "division by zero in #if");
875 top
->unsignedp
= unsigned1
|| unsigned2
;
877 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
880 top
->value
= v1
/ v2
;
881 if ((top
->value
& v1
& v2
) < 0)
882 integer_overflow (pfile
);
890 cpp_error (pfile
, "division by zero in #if");
893 top
->unsignedp
= unsigned1
|| unsigned2
;
895 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
897 top
->value
= v1
% v2
;
900 if (top
->flags
& HAVE_VALUE
)
902 cpp_error (pfile
, "syntax error");
907 top
->flags
|= HAVE_VALUE
;
910 if (top
->flags
& HAVE_VALUE
)
912 cpp_error (pfile
, "syntax error");
916 top
->unsignedp
= unsigned2
;
917 top
->flags
|= HAVE_VALUE
;
919 case '<': COMPARE(<); break;
920 case '>': COMPARE(>); break;
921 case LEQ
: COMPARE(<=); break;
922 case GEQ
: COMPARE(>=); break;
924 top
->value
= (v1
== v2
);
928 top
->value
= (v1
!= v2
);
934 top
->unsignedp
= unsigned1
;
935 if (v2
< 0 && ! unsigned2
)
936 top
->value
= right_shift (pfile
, v1
, unsigned1
, -v2
);
938 top
->value
= left_shift (pfile
, v1
, unsigned1
, v2
);
943 top
->unsignedp
= unsigned1
;
944 if (v2
< 0 && ! unsigned2
)
945 top
->value
= left_shift (pfile
, v1
, unsigned1
, -v2
);
947 top
->value
= right_shift (pfile
, v1
, unsigned1
, v2
);
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;
956 top
->value
= v1
&& v2
; top
->unsignedp
= 0;
957 if (!v1
) skip_evaluation
--;
960 top
->value
= v1
|| v2
; top
->unsignedp
= 0;
961 if (v1
) skip_evaluation
--;
964 if (CPP_PEDANTIC (pfile
))
965 cpp_pedwarn (pfile
, "comma operator in operand of `#if'");
967 top
->unsignedp
= unsigned2
;
970 cpp_error (pfile
, "syntax error in #if");
973 if (top
[0].op
!= '?')
976 "syntax error ':' without preceding '?'");
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");
989 if (top
->value
) skip_evaluation
--;
990 top
->value
= top
->value
? v1
: v2
;
991 top
->unsignedp
= unsigned1
|| unsigned2
;
995 if ((top
[1].flags
& HAVE_VALUE
)
996 || ! (top
[0].flags
& HAVE_VALUE
)
998 || (top
[-1].flags
& HAVE_VALUE
))
1000 cpp_error (pfile
, "mismatched parentheses in #if");
1007 top
->unsignedp
= unsigned1
;
1008 top
->flags
|= HAVE_VALUE
;
1013 (top
[1].op
>= ' ' && top
[1].op
<= '~'
1014 ? "unimplemented operator '%c'\n"
1015 : "unimplemented operator '\\%03o'\n"),
1022 cpp_error (pfile
, "internal error in #if expression");
1023 if (stack
!= init_stack
)
1029 /* Check for and handle stack overflow. */
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
);
1039 new_stack
= (struct operation
*) xmalloc (new_size
);
1040 bcopy ((char *) stack
, (char *) new_stack
, old_size
);
1043 top
= (struct operation
*) ((char *) new_stack
+ old_size
);
1044 limit
= (struct operation
*) ((char *) new_stack
+ new_size
);
1050 if ((op
.op
== OROR
&& top
[-1].value
)
1051 || (op
.op
== ANDAND
&& !top
[-1].value
)
1052 || (op
.op
== '?' && !top
[-1].value
))
1056 else if (op
.op
== ':')
1058 if (top
[-2].value
) /* Was condition true? */
1065 if (stack
!= init_stack
)
1067 skip_rest_of_line (pfile
);