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
*));
87 static HOST_WIDEST_INT cpp_parse_escape
PARAMS ((cpp_reader
*, U_CHAR
**, HOST_WIDEST_INT
));
102 #define LEFT_OPERAND_REQUIRED 1
103 #define RIGHT_OPERAND_REQUIRED 2
105 /* SKIP_OPERAND is set for '&&' '||' '?' and ':' when the
106 following operand should be short-circuited instead of evaluated. */
107 #define SKIP_OPERAND 8
108 /*#define UNSIGNEDP 16*/
112 char rprio
; /* Priority of op (relative to it right operand). */
114 char unsignedp
; /* true if value should be treated as unsigned */
115 HOST_WIDEST_INT value
; /* The value logically "right" of op. */
118 /* Parse and convert an integer for #if. Accepts decimal, hex, or octal
119 with or without size suffixes. */
121 static struct operation
122 parse_number (pfile
, start
, end
)
130 unsigned HOST_WIDEST_INT n
= 0, nd
, MAX_over_base
;
133 int digit
, largest_digit
= 0;
140 if (end
- start
>= 3 && (p
[1] == 'x' || p
[1] == 'X'))
152 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
153 MAX_over_base
= (((unsigned HOST_WIDEST_INT
) -1)
154 / ((unsigned HOST_WIDEST_INT
) base
));
160 if (c
>= '0' && c
<= '9')
162 else if (base
== 16 && c
>= 'a' && c
<= 'f') /* FIXME: assumes ASCII */
163 digit
= c
- 'a' + 10;
164 else if (base
== 16 && c
>= 'A' && c
<= 'F')
165 digit
= c
- 'A' + 10;
168 /* It's a float since it contains a point. */
170 "floating point numbers are not allowed in #if expressions");
175 /* `l' means long, and `u' means unsigned. */
178 if (c
== 'l' || c
== 'L')
180 else if (c
== 'u' || c
== 'U')
184 /* Decrement p here so that the error for an invalid number
185 will be generated below in the case where this is the
186 last character in the buffer. */
194 /* Don't look for any more digits after the suffixes. */
198 if (largest_digit
< digit
)
199 largest_digit
= digit
;
200 nd
= n
* base
+ digit
;
201 overflow
|= MAX_over_base
< n
|| nd
< n
;
207 cpp_error (pfile
, "invalid number in #if expression");
210 else if (spec_long
> (CPP_OPTIONS (pfile
)->c89
? 1 : 2))
212 cpp_error (pfile
, "too many `l' suffixes in integer constant");
215 else if (op
.unsignedp
> 1)
217 cpp_error (pfile
, "too many `u' suffixes in integer constant");
221 if (base
<= largest_digit
)
222 cpp_pedwarn (pfile
, "integer constant contains digits beyond the radix");
225 cpp_pedwarn (pfile
, "integer constant out of range");
227 /* If too big to be signed, consider it unsigned. */
228 else if ((HOST_WIDEST_INT
) n
< 0 && ! op
.unsignedp
)
232 "integer constant is so large that it is unsigned");
245 /* Parse and convert a character constant for #if. Understands backslash
246 escapes (\n, \031) and multibyte characters (if so configured). */
247 static struct operation
248 parse_charconst (pfile
, start
, end
)
254 HOST_WIDEST_INT result
= 0;
257 unsigned int width
= MAX_CHAR_TYPE_SIZE
, mask
= MAX_CHAR_TYPE_MASK
;
261 /* FIXME: Should use reentrant multibyte functions. */
262 #ifdef MULTIBYTE_CHARS
263 wchar_t c
= (wchar_t)-1;
264 (void) mbtowc (NULL_PTR
, NULL_PTR
, 0);
272 width
= MAX_WCHAR_TYPE_SIZE
, mask
= MAX_WCHAR_TYPE_MASK
;
274 max_chars
= MAX_LONG_TYPE_SIZE
/ width
;
276 ++ptr
; /* skip initial quote */
280 #ifndef MULTIBYTE_CHARS
283 ptr
+= mbtowc (&c
, ptr
, end
- ptr
);
285 if (c
== '\'' || c
== '\0')
289 /* Hopefully valid assumption: if mbtowc returns a backslash,
290 we are in initial shift state. No valid escape-sequence
291 character can take us out of initial shift state or begin
292 an unshifted multibyte char, so cpp_parse_escape doesn't
293 need to know about multibyte chars. */
295 c
= cpp_parse_escape (pfile
, &ptr
, mask
);
296 if (width
< HOST_BITS_PER_INT
297 && (unsigned int) c
>= (unsigned int)(1 << width
))
298 cpp_pedwarn (pfile
, "escape sequence out of range for character");
301 /* Merge character into result; ignore excess chars. */
302 if (++num_chars
<= max_chars
)
304 if (width
< HOST_BITS_PER_INT
)
305 result
= (result
<< width
) | (c
& ((1 << width
) - 1));
313 cpp_error (pfile
, "empty character constant");
318 /* cpp_get_token has already emitted an error if !traditional. */
319 if (! CPP_TRADITIONAL (pfile
))
320 cpp_error (pfile
, "malformatted character constant");
323 else if (num_chars
> max_chars
)
325 cpp_error (pfile
, "character constant too long");
328 else if (num_chars
!= 1 && ! CPP_TRADITIONAL (pfile
))
329 cpp_warning (pfile
, "multi-character character constant");
331 /* If char type is signed, sign-extend the constant. */
332 num_bits
= num_chars
* width
;
334 if (cpp_lookup (pfile
, (const U_CHAR
*)"__CHAR_UNSIGNED__",
335 sizeof ("__CHAR_UNSIGNED__")-1, -1)
336 || ((result
>> (num_bits
- 1)) & 1) == 0)
337 op
.value
= result
& ((unsigned HOST_WIDEST_INT
) ~0
338 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
340 op
.value
= result
| ~((unsigned HOST_WIDEST_INT
) ~0
341 >> (HOST_BITS_PER_WIDEST_INT
- num_bits
));
343 /* This is always a signed type. */
355 const char *operator;
359 static struct token tokentab2
[] = {
373 /* Read one token. */
375 static struct operation
376 cpp_lex (pfile
, skip_evaluation
)
381 struct token
*toktab
;
382 enum cpp_token token
;
384 U_CHAR
*tok_start
, *tok_end
;
389 old_written
= CPP_WRITTEN (pfile
);
390 cpp_skip_hspace (pfile
);
391 c
= CPP_BUF_PEEK (CPP_BUFFER (pfile
));
395 op
.value
= cpp_read_check_assertion (pfile
);
405 token
= cpp_get_token (pfile
);
406 tok_start
= pfile
->token_buffer
+ old_written
;
407 tok_end
= CPP_PWRITTEN (pfile
);
408 pfile
->limit
= tok_start
;
411 case CPP_EOF
: /* Should not happen ... */
416 if (CPP_BUFFER (pfile
)->fname
!= NULL
)
421 cpp_pop_buffer (pfile
);
427 return parse_number (pfile
, tok_start
, tok_end
);
429 cpp_error (pfile
, "string constants not allowed in #if expressions");
433 return parse_charconst (pfile
, tok_start
, tok_end
);
439 if (strcmp (tok_start
, "defined"))
441 if (CPP_WARN_UNDEF (pfile
) && !skip_evaluation
)
442 cpp_warning (pfile
, "`%.*s' is not defined",
443 (int) (tok_end
- tok_start
), tok_start
);
448 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
452 cpp_skip_hspace (pfile
);
456 ip
->cur
++; /* Skip over the paren */
457 cpp_skip_hspace (pfile
);
460 if (!is_idstart(*ip
->cur
))
462 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
465 while (is_idchar(*ip
->cur
))
468 cpp_skip_hspace (pfile
);
475 hp
= cpp_lookup (pfile
, tok
, len
, -1);
478 if (hp
->type
== T_POISON
)
479 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
487 cpp_error (pfile
, "`defined' without an identifier");
491 /* See if it is a special token of length 2. */
492 if (tok_start
+ 2 == tok_end
)
494 for (toktab
= tokentab2
; toktab
->operator != NULL
; toktab
++)
495 if (tok_start
[0] == toktab
->operator[0]
496 && tok_start
[1] == toktab
->operator[1])
498 if (toktab
->token
== ERROR
)
499 cpp_error (pfile
, "`%s' not allowed in operand of `#if'",
501 op
.op
= toktab
->token
;
512 /* Parse a C escape sequence. STRING_PTR points to a variable
513 containing a pointer to the string to parse. That pointer
514 is updated past the characters we use. The value of the
515 escape sequence is returned.
517 A negative value means the sequence \ newline was seen,
518 which is supposed to be equivalent to nothing at all.
520 If \ is followed by a null character, we return a negative
521 value and leave the string pointer pointing at the null character.
523 If \ is followed by 000, we return 0 and leave the string pointer
524 after the zeros. A value of 0 does not mean end of string. */
526 static HOST_WIDEST_INT
527 cpp_parse_escape (pfile
, string_ptr
, result_mask
)
530 HOST_WIDEST_INT result_mask
;
532 register int c
= *(*string_ptr
)++;
541 if (CPP_OPTIONS (pfile
)->pedantic
)
542 cpp_pedwarn (pfile
, "non-ANSI-standard escape sequence, `\\%c'", c
);
547 return TARGET_NEWLINE
;
569 register HOST_WIDEST_INT i
= c
- '0';
570 register int count
= 0;
573 c
= *(*string_ptr
)++;
574 if (c
>= '0' && c
<= '7')
575 i
= (i
<< 3) + c
- '0';
582 if (i
!= (i
& result_mask
))
585 cpp_pedwarn (pfile
, "octal escape sequence out of range");
591 register unsigned HOST_WIDEST_INT i
= 0, overflow
= 0;
592 register int digits_found
= 0, digit
;
595 c
= *(*string_ptr
)++;
596 if (c
>= '0' && c
<= '9')
598 else if (c
>= 'a' && c
<= 'f')
599 digit
= c
- 'a' + 10;
600 else if (c
>= 'A' && c
<= 'F')
601 digit
= c
- 'A' + 10;
607 overflow
|= i
^ (i
<< 4 >> 4);
608 i
= (i
<< 4) + digit
;
612 cpp_error (pfile
, "\\x used with no following hex digits");
613 if (overflow
| (i
!= (i
& result_mask
)))
616 cpp_pedwarn (pfile
, "hex escape sequence out of range");
626 integer_overflow (pfile
)
629 if (CPP_PEDANTIC (pfile
))
630 cpp_pedwarn (pfile
, "integer overflow in preprocessor expression");
633 static HOST_WIDEST_INT
634 left_shift (pfile
, a
, unsignedp
, b
)
638 unsigned HOST_WIDEST_INT b
;
640 if (b
>= HOST_BITS_PER_WIDEST_INT
)
642 if (! unsignedp
&& a
!= 0)
643 integer_overflow (pfile
);
647 return (unsigned HOST_WIDEST_INT
) a
<< b
;
650 HOST_WIDEST_INT l
= a
<< b
;
652 integer_overflow (pfile
);
657 static HOST_WIDEST_INT
658 right_shift (pfile
, a
, unsignedp
, b
)
659 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
662 unsigned HOST_WIDEST_INT b
;
664 if (b
>= HOST_BITS_PER_WIDEST_INT
)
665 return unsignedp
? 0 : a
>> (HOST_BITS_PER_WIDEST_INT
- 1);
667 return (unsigned HOST_WIDEST_INT
) a
>> b
;
672 /* These priorities are all even, so we can handle associatively. */
673 #define PAREN_INNER_PRIO 0
675 #define COND_PRIO (COMMA_PRIO+2)
676 #define OROR_PRIO (COND_PRIO+2)
677 #define ANDAND_PRIO (OROR_PRIO+2)
678 #define OR_PRIO (ANDAND_PRIO+2)
679 #define XOR_PRIO (OR_PRIO+2)
680 #define AND_PRIO (XOR_PRIO+2)
681 #define EQUAL_PRIO (AND_PRIO+2)
682 #define LESS_PRIO (EQUAL_PRIO+2)
683 #define SHIFT_PRIO (LESS_PRIO+2)
684 #define PLUS_PRIO (SHIFT_PRIO+2)
685 #define MUL_PRIO (PLUS_PRIO+2)
686 #define UNARY_PRIO (MUL_PRIO+2)
687 #define PAREN_OUTER_PRIO (UNARY_PRIO+2)
689 #define COMPARE(OP) \
691 top->value = (unsigned1 || unsigned2) \
692 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 : (v1 OP v2)
694 /* Parse and evaluate a C expression, reading from PFILE.
695 Returns the value of the expression. */
698 cpp_parse_expr (pfile
)
701 /* The implementation is an operator precedence parser,
702 i.e. a bottom-up parser, using a stack for not-yet-reduced tokens.
704 The stack base is 'stack', and the current stack pointer is 'top'.
705 There is a stack element for each operator (only),
706 and the most recently pushed operator is 'top->op'.
707 An operand (value) is stored in the 'value' field of the stack
708 element of the operator that precedes it.
709 In that case the 'flags' field has the HAVE_VALUE flag set. */
711 #define INIT_STACK_SIZE 20
712 struct operation init_stack
[INIT_STACK_SIZE
];
713 struct operation
*stack
= init_stack
;
714 struct operation
*limit
= stack
+ INIT_STACK_SIZE
;
715 register struct operation
*top
= stack
;
716 int lprio
, rprio
= 0;
717 int skip_evaluation
= 0;
727 op
= cpp_lex (pfile
, skip_evaluation
);
729 /* See if the token is an operand, in which case go to set_value.
730 If the token is an operator, figure out its left and right
731 priorities, and then goto maybe_reduce. */
736 cpp_fatal (pfile
, "internal error: cpp_lex returns a NAME");
739 top
->value
= op
.value
;
740 top
->unsignedp
= op
.unsignedp
;
743 lprio
= 0; goto maybe_reduce
;
745 /* Is this correct if unary ? FIXME */
746 flags
= RIGHT_OPERAND_REQUIRED
;
747 lprio
= PLUS_PRIO
; rprio
= lprio
+ 1; goto maybe_reduce
;
749 flags
= RIGHT_OPERAND_REQUIRED
;
750 rprio
= UNARY_PRIO
; lprio
= rprio
+ 1; goto maybe_reduce
;
751 case '*': case '/': case '%':
752 lprio
= MUL_PRIO
; goto binop
;
753 case '<': case '>': case LEQ
: case GEQ
:
754 lprio
= LESS_PRIO
; goto binop
;
755 case EQUAL
: case NOTEQUAL
:
756 lprio
= EQUAL_PRIO
; goto binop
;
758 lprio
= SHIFT_PRIO
; goto binop
;
759 case '&': lprio
= AND_PRIO
; goto binop
;
760 case '^': lprio
= XOR_PRIO
; goto binop
;
761 case '|': lprio
= OR_PRIO
; goto binop
;
762 case ANDAND
: lprio
= ANDAND_PRIO
; goto binop
;
763 case OROR
: lprio
= OROR_PRIO
; goto binop
;
765 lprio
= COMMA_PRIO
; goto binop
;
767 lprio
= PAREN_OUTER_PRIO
; rprio
= PAREN_INNER_PRIO
;
770 lprio
= PAREN_INNER_PRIO
; rprio
= PAREN_OUTER_PRIO
;
773 lprio
= COND_PRIO
; rprio
= COND_PRIO
;
776 lprio
= COND_PRIO
+ 1; rprio
= COND_PRIO
;
781 flags
= LEFT_OPERAND_REQUIRED
|RIGHT_OPERAND_REQUIRED
;
785 cpp_error (pfile
, "invalid character in #if");
790 /* Push a value onto the stack. */
791 if (top
->flags
& HAVE_VALUE
)
793 cpp_error (pfile
, "syntax error in #if");
796 top
->flags
|= HAVE_VALUE
;
800 /* Push an operator, and check if we can reduce now. */
801 while (top
->rprio
> lprio
)
803 HOST_WIDEST_INT v1
= top
[-1].value
, v2
= top
[0].value
;
804 int unsigned1
= top
[-1].unsignedp
, unsigned2
= top
[0].unsignedp
;
806 if ((top
[1].flags
& LEFT_OPERAND_REQUIRED
)
807 && ! (top
[0].flags
& HAVE_VALUE
))
809 cpp_error (pfile
, "syntax error - missing left operand");
812 if ((top
[1].flags
& RIGHT_OPERAND_REQUIRED
)
813 && ! (top
[1].flags
& HAVE_VALUE
))
815 cpp_error (pfile
, "syntax error - missing right operand");
818 /* top[0].value = (top[1].op)(v1, v2);*/
822 if (!(top
->flags
& HAVE_VALUE
))
825 top
->unsignedp
= unsigned2
;
826 top
->flags
|= HAVE_VALUE
;
830 top
->value
= v1
+ v2
;
831 top
->unsignedp
= unsigned1
|| unsigned2
;
832 if (! top
->unsignedp
&& ! skip_evaluation
833 && ! possible_sum_sign (v1
, v2
, top
->value
))
834 integer_overflow (pfile
);
838 if (!(top
->flags
& HAVE_VALUE
))
841 if (!skip_evaluation
&& (top
->value
& v2
) < 0 && !unsigned2
)
842 integer_overflow (pfile
);
843 top
->unsignedp
= unsigned2
;
844 top
->flags
|= HAVE_VALUE
;
848 top
->value
= v1
- v2
;
849 top
->unsignedp
= unsigned1
|| unsigned2
;
850 if (! top
->unsignedp
&& ! skip_evaluation
851 && ! possible_sum_sign (top
->value
, v2
, v1
))
852 integer_overflow (pfile
);
856 top
->unsignedp
= unsigned1
|| unsigned2
;
858 top
->value
= (unsigned HOST_WIDEST_INT
) v1
* v2
;
859 else if (!skip_evaluation
)
861 top
->value
= v1
* v2
;
863 && (top
->value
/ v1
!= v2
864 || (top
->value
& v1
& v2
) < 0))
865 integer_overflow (pfile
);
873 cpp_error (pfile
, "division by zero in #if");
876 top
->unsignedp
= unsigned1
|| unsigned2
;
878 top
->value
= (unsigned HOST_WIDEST_INT
) v1
/ v2
;
881 top
->value
= v1
/ v2
;
882 if ((top
->value
& v1
& v2
) < 0)
883 integer_overflow (pfile
);
891 cpp_error (pfile
, "division by zero in #if");
894 top
->unsignedp
= unsigned1
|| unsigned2
;
896 top
->value
= (unsigned HOST_WIDEST_INT
) v1
% v2
;
898 top
->value
= v1
% v2
;
901 if (top
->flags
& HAVE_VALUE
)
903 cpp_error (pfile
, "syntax error");
908 top
->flags
|= HAVE_VALUE
;
911 if (top
->flags
& HAVE_VALUE
)
913 cpp_error (pfile
, "syntax error");
917 top
->unsignedp
= unsigned2
;
918 top
->flags
|= HAVE_VALUE
;
920 case '<': COMPARE(<); break;
921 case '>': COMPARE(>); break;
922 case LEQ
: COMPARE(<=); break;
923 case GEQ
: COMPARE(>=); break;
925 top
->value
= (v1
== v2
);
929 top
->value
= (v1
!= v2
);
935 top
->unsignedp
= unsigned1
;
936 if (v2
< 0 && ! unsigned2
)
937 top
->value
= right_shift (pfile
, v1
, unsigned1
, -v2
);
939 top
->value
= left_shift (pfile
, v1
, unsigned1
, v2
);
944 top
->unsignedp
= unsigned1
;
945 if (v2
< 0 && ! unsigned2
)
946 top
->value
= left_shift (pfile
, v1
, unsigned1
, -v2
);
948 top
->value
= right_shift (pfile
, v1
, unsigned1
, v2
);
950 #define LOGICAL(OP) \
951 top->value = v1 OP v2;\
952 top->unsignedp = unsigned1 || unsigned2;
953 case '&': LOGICAL(&); break;
954 case '^': LOGICAL(^); break;
955 case '|': LOGICAL(|); break;
957 top
->value
= v1
&& v2
; top
->unsignedp
= 0;
958 if (!v1
) skip_evaluation
--;
961 top
->value
= v1
|| v2
; top
->unsignedp
= 0;
962 if (v1
) skip_evaluation
--;
965 if (CPP_PEDANTIC (pfile
))
966 cpp_pedwarn (pfile
, "comma operator in operand of `#if'");
968 top
->unsignedp
= unsigned2
;
971 cpp_error (pfile
, "syntax error in #if");
974 if (top
[0].op
!= '?')
977 "syntax error ':' without preceding '?'");
980 else if (! (top
[1].flags
& HAVE_VALUE
)
981 || !(top
[-1].flags
& HAVE_VALUE
)
982 || !(top
[0].flags
& HAVE_VALUE
))
984 cpp_error (pfile
, "bad syntax for ?: operator");
990 if (top
->value
) skip_evaluation
--;
991 top
->value
= top
->value
? v1
: v2
;
992 top
->unsignedp
= unsigned1
|| unsigned2
;
996 if ((top
[1].flags
& HAVE_VALUE
)
997 || ! (top
[0].flags
& HAVE_VALUE
)
999 || (top
[-1].flags
& HAVE_VALUE
))
1001 cpp_error (pfile
, "mismatched parentheses in #if");
1008 top
->unsignedp
= unsigned1
;
1009 top
->flags
|= HAVE_VALUE
;
1013 if (ISGRAPH (top
[1].op
))
1014 cpp_error (pfile
, "unimplemented operator '%c'\n", top
[1].op
);
1016 cpp_error (pfile
, "unimplemented operator '\\%03o'\n",
1023 cpp_error (pfile
, "internal error in #if expression");
1024 if (stack
!= init_stack
)
1030 /* Check for and handle stack overflow. */
1033 struct operation
*new_stack
;
1034 int old_size
= (char *) limit
- (char *) stack
;
1035 int new_size
= 2 * old_size
;
1036 if (stack
!= init_stack
)
1037 new_stack
= (struct operation
*) xrealloc (stack
, new_size
);
1040 new_stack
= (struct operation
*) xmalloc (new_size
);
1041 bcopy ((char *) stack
, (char *) new_stack
, old_size
);
1044 top
= (struct operation
*) ((char *) new_stack
+ old_size
);
1045 limit
= (struct operation
*) ((char *) new_stack
+ new_size
);
1051 if ((op
.op
== OROR
&& top
[-1].value
)
1052 || (op
.op
== ANDAND
&& !top
[-1].value
)
1053 || (op
.op
== '?' && !top
[-1].value
))
1057 else if (op
.op
== ':')
1059 if (top
[-2].value
) /* Was condition true? */
1066 if (stack
!= init_stack
)
1068 skip_rest_of_line (pfile
);