1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "coretypes.h"
28 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
29 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
30 #define LOW_PART(num_part) (num_part & HALF_MASK)
31 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
35 const cpp_token
*token
; /* The token forming op (for diagnostics). */
36 cpp_num value
; /* The value logically "right" of op. */
40 /* Some simple utility routines on double integers. */
41 #define num_zerop(num) ((num.low | num.high) == 0)
42 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
43 static bool num_positive
PARAMS ((cpp_num
, size_t));
44 static bool num_greater_eq
PARAMS ((cpp_num
, cpp_num
, size_t));
45 static cpp_num num_trim
PARAMS ((cpp_num
, size_t));
46 static cpp_num num_part_mul
PARAMS ((cpp_num_part
, cpp_num_part
));
48 static cpp_num num_unary_op
PARAMS ((cpp_reader
*, cpp_num
, enum cpp_ttype
));
49 static cpp_num num_binary_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
51 static cpp_num num_negate
PARAMS ((cpp_num
, size_t));
52 static cpp_num num_bitwise_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
54 static cpp_num num_inequality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
56 static cpp_num num_equality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
58 static cpp_num num_mul
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
));
59 static cpp_num num_div_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
61 static cpp_num num_lshift
PARAMS ((cpp_num
, size_t, size_t));
62 static cpp_num num_rshift
PARAMS ((cpp_num
, size_t, size_t));
64 static cpp_num append_digit
PARAMS ((cpp_num
, int, int, size_t));
65 static cpp_num parse_defined
PARAMS ((cpp_reader
*));
66 static cpp_num eval_token
PARAMS ((cpp_reader
*, const cpp_token
*));
67 static struct op
*reduce
PARAMS ((cpp_reader
*, struct op
*, enum cpp_ttype
));
68 static unsigned int interpret_float_suffix
PARAMS ((const uchar
*, size_t));
69 static unsigned int interpret_int_suffix
PARAMS ((const uchar
*, size_t));
70 static void check_promotion
PARAMS ((cpp_reader
*, const struct op
*));
72 /* Token type abuse to create unary plus and minus operators. */
73 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
74 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
76 /* With -O2, gcc appears to produce nice code, moving the error
77 message load and subsequent jump completely out of the main path. */
78 #define SYNTAX_ERROR(msgid) \
79 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
80 #define SYNTAX_ERROR2(msgid, arg) \
81 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
83 /* Subroutine of cpp_classify_number. S points to a float suffix of
84 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
85 flag vector describing the suffix. */
87 interpret_float_suffix (s
, len
)
91 size_t f
= 0, l
= 0, i
= 0;
96 case 'f': case 'F': f
++; break;
97 case 'l': case 'L': l
++; break;
99 case 'j': case 'J': i
++; break;
104 if (f
+ l
> 1 || i
> 1)
107 return ((i
? CPP_N_IMAGINARY
: 0)
109 l
? CPP_N_LARGE
: CPP_N_MEDIUM
));
112 /* Subroutine of cpp_classify_number. S points to an integer suffix
113 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
114 flag vector describing the suffix. */
116 interpret_int_suffix (s
, len
)
127 case 'u': case 'U': u
++; break;
129 case 'j': case 'J': i
++; break;
130 case 'l': case 'L': l
++;
131 /* If there are two Ls, they must be adjacent and the same case. */
132 if (l
== 2 && s
[len
] != s
[len
+ 1])
139 if (l
> 2 || u
> 1 || i
> 1)
142 return ((i
? CPP_N_IMAGINARY
: 0)
143 | (u
? CPP_N_UNSIGNED
: 0)
144 | ((l
== 0) ? CPP_N_SMALL
145 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
148 /* Categorize numeric constants according to their field (integer,
149 floating point, or invalid), radix (decimal, octal, hexadecimal),
150 and type suffixes. */
152 cpp_classify_number (pfile
, token
)
154 const cpp_token
*token
;
156 const uchar
*str
= token
->val
.str
.text
;
158 unsigned int max_digit
, result
, radix
;
159 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
161 /* If the lexer has done its job, length one can only be a single
162 digit. Fast-path this very common case. */
163 if (token
->val
.str
.len
== 1)
164 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
166 limit
= str
+ token
->val
.str
.len
;
167 float_flag
= NOT_FLOAT
;
171 /* First, interpret the radix. */
177 /* Require at least one hex digit to classify it as hex. */
178 if ((*str
== 'x' || *str
== 'X') && ISXDIGIT (str
[1]))
185 /* Now scan for a well-formed integer or float. */
188 unsigned int c
= *str
++;
190 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
198 if (float_flag
== NOT_FLOAT
)
199 float_flag
= AFTER_POINT
;
201 SYNTAX_ERROR ("too many decimal points in number");
203 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
204 || (radix
== 16 && (c
== 'p' || c
== 'P')))
206 float_flag
= AFTER_EXPON
;
211 /* Start of suffix. */
217 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
220 if (max_digit
>= radix
)
221 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
223 if (float_flag
!= NOT_FLOAT
)
225 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
226 cpp_error (pfile
, DL_PEDWARN
,
227 "use of C99 hexadecimal floating constant");
229 if (float_flag
== AFTER_EXPON
)
231 if (*str
== '+' || *str
== '-')
234 /* Exponent is decimal, even if string is a hex float. */
236 SYNTAX_ERROR ("exponent has no digits");
240 while (ISDIGIT (*str
));
242 else if (radix
== 16)
243 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
245 result
= interpret_float_suffix (str
, limit
- str
);
248 cpp_error (pfile
, DL_ERROR
,
249 "invalid suffix \"%.*s\" on floating constant",
250 (int) (limit
- str
), str
);
251 return CPP_N_INVALID
;
254 /* Traditional C didn't accept any floating suffixes. */
256 && CPP_WTRADITIONAL (pfile
)
257 && ! cpp_sys_macro_p (pfile
))
258 cpp_error (pfile
, DL_WARNING
,
259 "traditional C rejects the \"%.*s\" suffix",
260 (int) (limit
- str
), str
);
262 result
|= CPP_N_FLOATING
;
266 result
= interpret_int_suffix (str
, limit
- str
);
269 cpp_error (pfile
, DL_ERROR
,
270 "invalid suffix \"%.*s\" on integer constant",
271 (int) (limit
- str
), str
);
272 return CPP_N_INVALID
;
275 /* Traditional C only accepted the 'L' suffix.
276 Suppress warning about 'LL' with -Wno-long-long. */
277 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
279 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
280 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
282 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
283 cpp_error (pfile
, DL_WARNING
,
284 "traditional C rejects the \"%.*s\" suffix",
285 (int) (limit
- str
), str
);
288 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
289 && ! CPP_OPTION (pfile
, c99
)
290 && CPP_OPTION (pfile
, warn_long_long
))
291 cpp_error (pfile
, DL_PEDWARN
, "use of C99 long long integer constant");
293 result
|= CPP_N_INTEGER
;
296 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
297 cpp_error (pfile
, DL_PEDWARN
, "imaginary constants are a GCC extension");
300 result
|= CPP_N_DECIMAL
;
301 else if (radix
== 16)
304 result
|= CPP_N_OCTAL
;
309 return CPP_N_INVALID
;
312 /* cpp_interpret_integer converts an integer constant into a cpp_num,
313 of precision options->precision.
315 We do not provide any interface for decimal->float conversion,
316 because the preprocessor doesn't need it and the floating point
317 handling in GCC proper is too ugly to speak of. */
319 cpp_interpret_integer (pfile
, token
, type
)
321 const cpp_token
*token
;
324 const uchar
*p
, *end
;
329 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
330 result
.overflow
= false;
332 p
= token
->val
.str
.text
;
333 end
= p
+ token
->val
.str
.len
;
335 /* Common case of a single digit. */
336 if (token
->val
.str
.len
== 1)
337 result
.low
= p
[0] - '0';
341 size_t precision
= CPP_OPTION (pfile
, precision
);
342 unsigned int base
= 10, c
= 0;
343 bool overflow
= false;
345 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
350 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
356 /* We can add a digit to numbers strictly less than this without
357 needing the precision and slowness of double integers. */
358 max
= ~(cpp_num_part
) 0;
359 if (precision
< PART_PRECISION
)
360 max
>>= PART_PRECISION
- precision
;
361 max
= (max
- base
+ 1) / base
+ 1;
367 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
372 /* Strict inequality for when max is set to zero. */
373 if (result
.low
< max
)
374 result
.low
= result
.low
* base
+ c
;
377 result
= append_digit (result
, c
, base
, precision
);
378 overflow
|= result
.overflow
;
384 cpp_error (pfile
, DL_PEDWARN
,
385 "integer constant is too large for its type");
386 /* If too big to be signed, consider it unsigned. Only warn for
387 decimal numbers. Traditional numbers were always signed (but
388 we still honor an explicit U suffix); but we only have
389 traditional semantics in directives. */
390 else if (!result
.unsignedp
391 && !(CPP_OPTION (pfile
, traditional
)
392 && pfile
->state
.in_directive
)
393 && !num_positive (result
, precision
))
396 cpp_error (pfile
, DL_WARNING
,
397 "integer constant is so large that it is unsigned");
398 result
.unsignedp
= true;
405 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
408 append_digit (num
, digit
, base
, precision
)
414 unsigned int shift
= 3 + (base
== 16);
416 cpp_num_part add_high
, add_low
;
418 /* Multiply by 8 or 16. Catching this overflow here means we don't
419 need to worry about add_high overflowing. */
420 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
421 result
.high
= num
.high
<< shift
;
422 result
.low
= num
.low
<< shift
;
423 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
427 add_low
= num
.low
<< 1;
428 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
431 add_high
= add_low
= 0;
433 if (add_low
+ digit
< add_low
)
437 if (result
.low
+ add_low
< result
.low
)
439 if (result
.high
+ add_high
< result
.high
)
442 result
.low
+= add_low
;
443 result
.high
+= add_high
;
445 /* The above code catches overflow of a cpp_num type. This catches
446 overflow of the (possibly shorter) target precision. */
447 num
.low
= result
.low
;
448 num
.high
= result
.high
;
449 result
= num_trim (result
, precision
);
450 if (!num_eq (result
, num
))
453 result
.unsignedp
= num
.unsignedp
;
454 result
.overflow
= overflow
;
458 /* Handle meeting "defined" in a preprocessor expression. */
460 parse_defined (pfile
)
465 cpp_hashnode
*node
= 0;
466 const cpp_token
*token
;
467 cpp_context
*initial_context
= pfile
->context
;
469 /* Don't expand macros. */
470 pfile
->state
.prevent_expansion
++;
472 token
= cpp_get_token (pfile
);
473 if (token
->type
== CPP_OPEN_PAREN
)
476 token
= cpp_get_token (pfile
);
479 if (token
->type
== CPP_NAME
)
481 node
= token
->val
.node
;
482 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
484 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
490 cpp_error (pfile
, DL_ERROR
,
491 "operator \"defined\" requires an identifier");
492 if (token
->flags
& NAMED_OP
)
497 op
.type
= token
->type
;
498 cpp_error (pfile
, DL_ERROR
,
499 "(\"%s\" is an alternative token for \"%s\" in C++)",
500 cpp_token_as_text (pfile
, token
),
501 cpp_token_as_text (pfile
, &op
));
507 if (pfile
->context
!= initial_context
)
508 cpp_error (pfile
, DL_WARNING
,
509 "this use of \"defined\" may not be portable");
511 _cpp_mark_macro_used (node
);
513 /* A possible controlling macro of the form #if !defined ().
514 _cpp_parse_expr checks there was no other junk on the line. */
515 pfile
->mi_ind_cmacro
= node
;
518 pfile
->state
.prevent_expansion
--;
520 result
.unsignedp
= false;
522 result
.overflow
= false;
523 result
.low
= node
&& node
->type
== NT_MACRO
;
527 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
528 number or character constant, or the result of the "defined" or "#"
531 eval_token (pfile
, token
)
533 const cpp_token
*token
;
542 temp
= cpp_classify_number (pfile
, token
);
543 switch (temp
& CPP_N_CATEGORY
)
546 cpp_error (pfile
, DL_ERROR
,
547 "floating constant in preprocessor expression");
550 if (!(temp
& CPP_N_IMAGINARY
))
551 return cpp_interpret_integer (pfile
, token
, temp
);
552 cpp_error (pfile
, DL_ERROR
,
553 "imaginary number in preprocessor expression");
557 /* Error already issued. */
560 result
.high
= result
.low
= 0;
566 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
571 /* Sign-extend the result if necessary. */
572 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
574 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
575 result
.low
|= ~(~(cpp_num_part
) 0
576 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
577 result
.high
= ~(cpp_num_part
) 0;
578 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
584 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
585 return parse_defined (pfile
);
586 else if (CPP_OPTION (pfile
, cplusplus
)
587 && (token
->val
.node
== pfile
->spec_nodes
.n_true
588 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
591 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
593 /* Warn about use of true or false in #if when pedantic
594 and stdbool.h has not been included. */
595 if (CPP_PEDANTIC (pfile
)
596 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
597 cpp_error (pfile
, DL_PEDWARN
,
598 "ISO C++ does not permit \"%s\" in #if",
599 NODE_NAME (token
->val
.node
));
605 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
606 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
607 NODE_NAME (token
->val
.node
));
611 default: /* CPP_HASH */
612 _cpp_test_assertion (pfile
, &temp
);
617 result
.unsignedp
= !!unsignedp
;
618 result
.overflow
= false;
622 /* Operator precedence and flags table.
624 After an operator is returned from the lexer, if it has priority less
625 than the operator on the top of the stack, we reduce the stack by one
626 operator and repeat the test. Since equal priorities do not reduce,
627 this is naturally right-associative.
629 We handle left-associative operators by decrementing the priority of
630 just-lexed operators by one, but retaining the priority of operators
631 already on the stack.
633 The remaining cases are '(' and ')'. We handle '(' by skipping the
634 reduction phase completely. ')' is given lower priority than
635 everything else, including '(', effectively forcing a reduction of the
636 parenthesized expression. If there is a matching '(', the routine
637 reduce() exits immediately. If the normal exit route sees a ')', then
638 there cannot have been a matching '(' and an error message is output.
640 The parser assumes all shifted operators require a left operand unless
641 the flag NO_L_OPERAND is set. These semantics are automatic; any
642 extra semantics need to be handled with operator-specific code. */
644 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
645 operand changes because of integer promotions. */
646 #define NO_L_OPERAND (1 << 0)
647 #define LEFT_ASSOC (1 << 1)
648 #define CHECK_PROMOTION (1 << 2)
650 /* Operator to priority map. Must be in the same order as the first
651 N entries of enum cpp_ttype. */
652 static const struct operator
658 /* EQ */ {0, 0}, /* Shouldn't happen. */
659 /* NOT */ {16, NO_L_OPERAND
},
660 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
661 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
662 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
663 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
664 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
665 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
666 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
667 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
668 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
669 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
670 /* RSHIFT */ {13, LEFT_ASSOC
},
671 /* LSHIFT */ {13, LEFT_ASSOC
},
673 /* MIN */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
674 /* MAX */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
676 /* COMPL */ {16, NO_L_OPERAND
},
677 /* AND_AND */ {6, LEFT_ASSOC
},
678 /* OR_OR */ {5, LEFT_ASSOC
},
680 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
681 /* COMMA */ {2, LEFT_ASSOC
},
682 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
683 /* CLOSE_PAREN */ {0, 0},
685 /* EQ_EQ */ {11, LEFT_ASSOC
},
686 /* NOT_EQ */ {11, LEFT_ASSOC
},
687 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
688 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
689 /* UPLUS */ {16, NO_L_OPERAND
},
690 /* UMINUS */ {16, NO_L_OPERAND
}
693 /* Parse and evaluate a C expression, reading from PFILE.
694 Returns the truth value of the expression.
696 The implementation is an operator precedence parser, i.e. a
697 bottom-up parser, using a stack for not-yet-reduced tokens.
699 The stack base is op_stack, and the current stack pointer is 'top'.
700 There is a stack element for each operator (only), and the most
701 recently pushed operator is 'top->op'. An operand (value) is
702 stored in the 'value' field of the stack element of the operator
705 _cpp_parse_expr (pfile
)
708 struct op
*top
= pfile
->op_stack
;
709 unsigned int lex_count
;
710 bool saw_leading_not
, want_value
= true;
712 pfile
->state
.skip_eval
= 0;
714 /* Set up detection of #if ! defined(). */
715 pfile
->mi_ind_cmacro
= 0;
716 saw_leading_not
= false;
719 /* Lowest priority operator prevents further reductions. */
727 op
.token
= cpp_get_token (pfile
);
728 op
.op
= op
.token
->type
;
732 /* These tokens convert into values. */
739 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
740 cpp_token_as_text (pfile
, op
.token
));
742 top
->value
= eval_token (pfile
, op
.token
);
746 saw_leading_not
= lex_count
== 1;
757 if (ISGRAPH (op
.token
->val
.c
))
758 SYNTAX_ERROR2 ("invalid character '%c' in #if", op
.token
->val
.c
);
760 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if",
764 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
765 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
766 cpp_token_as_text (pfile
, op
.token
));
770 /* Check we have a value or operator as appropriate. */
771 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
774 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
775 cpp_token_as_text (pfile
, op
.token
));
779 /* Ordering here is subtle and intended to favor the
780 missing parenthesis diagnostics over alternatives. */
781 if (op
.op
== CPP_CLOSE_PAREN
)
783 if (top
->op
== CPP_OPEN_PAREN
)
784 SYNTAX_ERROR ("void expression between '(' and ')'");
786 else if (top
->op
== CPP_EOF
)
787 SYNTAX_ERROR ("#if with no expression");
788 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
789 SYNTAX_ERROR2 ("operator '%s' has no right operand",
790 cpp_token_as_text (pfile
, top
->token
));
793 top
= reduce (pfile
, top
, op
.op
);
797 if (op
.op
== CPP_EOF
)
802 case CPP_CLOSE_PAREN
:
805 if (!num_zerop (top
->value
))
806 pfile
->state
.skip_eval
++;
810 if (num_zerop (top
->value
))
811 pfile
->state
.skip_eval
++;
814 if (top
->op
!= CPP_QUERY
)
815 SYNTAX_ERROR (" ':' without preceding '?'");
816 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
817 pfile
->state
.skip_eval
++;
819 pfile
->state
.skip_eval
--;
826 /* Check for and handle stack overflow. */
827 if (++top
== pfile
->op_limit
)
828 top
= _cpp_expand_op_stack (pfile
);
831 top
->token
= op
.token
;
834 /* The controlling macro expression is only valid if we called lex 3
835 times: <!> <defined expression> and <EOF>. push_conditional ()
836 checks that we are at top-of-file. */
837 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
838 pfile
->mi_ind_cmacro
= 0;
840 if (top
!= pfile
->op_stack
)
842 cpp_error (pfile
, DL_ICE
, "unbalanced stack in #if");
844 return false; /* Return false on syntax error. */
847 return !num_zerop (top
->value
);
850 /* Reduce the operator / value stack if possible, in preparation for
851 pushing operator OP. Returns NULL on error, otherwise the top of
854 reduce (pfile
, top
, op
)
861 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
864 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
868 if (op
== CPP_OPEN_PAREN
)
871 /* Decrement the priority of left-associative operators to force a
872 reduction with operators of otherwise equal priority. */
873 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
874 while (prio
< optab
[top
->op
].prio
)
876 if (CPP_OPTION (pfile
, warn_num_sign_change
)
877 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
878 check_promotion (pfile
, top
);
886 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
896 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
897 top
->value
, top
->op
);
905 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
911 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
918 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
922 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
927 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
928 top
->value
, top
->op
);
933 if (!num_zerop (top
->value
))
934 pfile
->state
.skip_eval
--;
935 top
->value
.low
= (!num_zerop (top
->value
)
936 || !num_zerop (top
[1].value
));
938 top
->value
.unsignedp
= false;
939 top
->value
.overflow
= false;
944 if (num_zerop (top
->value
))
945 pfile
->state
.skip_eval
--;
946 top
->value
.low
= (!num_zerop (top
->value
)
947 && !num_zerop (top
[1].value
));
949 top
->value
.unsignedp
= false;
950 top
->value
.overflow
= false;
954 if (op
!= CPP_CLOSE_PAREN
)
956 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
960 top
->value
= top
[1].value
;
965 if (!num_zerop (top
->value
))
967 pfile
->state
.skip_eval
--;
968 top
->value
= top
[1].value
;
971 top
->value
= top
[2].value
;
972 top
->value
.unsignedp
= (top
[1].value
.unsignedp
973 || top
[2].value
.unsignedp
);
977 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
985 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
986 cpp_error (pfile
, DL_PEDWARN
,
987 "integer overflow in preprocessor expression");
990 if (op
== CPP_CLOSE_PAREN
)
992 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
999 /* Returns the position of the old top of stack after expansion. */
1001 _cpp_expand_op_stack (pfile
)
1004 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1005 size_t new_size
= old_size
* 2 + 20;
1007 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
1008 new_size
* sizeof (struct op
));
1009 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1011 return pfile
->op_stack
+ old_size
;
1014 /* Emits a warning if the effective sign of either operand of OP
1015 changes because of integer promotions. */
1017 check_promotion (pfile
, op
)
1019 const struct op
*op
;
1021 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1024 if (op
->value
.unsignedp
)
1026 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1027 cpp_error (pfile
, DL_WARNING
,
1028 "the left operand of \"%s\" changes sign when promoted",
1029 cpp_token_as_text (pfile
, op
->token
));
1031 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1032 cpp_error (pfile
, DL_WARNING
,
1033 "the right operand of \"%s\" changes sign when promoted",
1034 cpp_token_as_text (pfile
, op
->token
));
1037 /* Clears the unused high order bits of the number pointed to by PNUM. */
1039 num_trim (num
, precision
)
1043 if (precision
> PART_PRECISION
)
1045 precision
-= PART_PRECISION
;
1046 if (precision
< PART_PRECISION
)
1047 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1051 if (precision
< PART_PRECISION
)
1052 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1059 /* True iff A (presumed signed) >= 0. */
1061 num_positive (num
, precision
)
1065 if (precision
> PART_PRECISION
)
1067 precision
-= PART_PRECISION
;
1068 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1071 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1074 /* Sign extend a number, with PRECISION significant bits and all
1075 others assumed clear, to fill out a cpp_num structure. */
1077 cpp_num_sign_extend (num
, precision
)
1083 if (precision
> PART_PRECISION
)
1085 precision
-= PART_PRECISION
;
1086 if (precision
< PART_PRECISION
1087 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1088 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1090 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1092 if (precision
< PART_PRECISION
)
1093 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1094 num
.high
= ~(cpp_num_part
) 0;
1101 /* Returns the negative of NUM. */
1103 num_negate (num
, precision
)
1110 num
.high
= ~num
.high
;
1114 num
= num_trim (num
, precision
);
1115 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1120 /* Returns true if A >= B. */
1122 num_greater_eq (pa
, pb
, precision
)
1128 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1132 /* Both numbers have signed type. If they are of different
1133 sign, the answer is the sign of A. */
1134 unsignedp
= num_positive (pa
, precision
);
1136 if (unsignedp
!= num_positive (pb
, precision
))
1139 /* Otherwise we can do an unsigned comparison. */
1142 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1145 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1147 num_bitwise_op (pfile
, lhs
, rhs
, op
)
1148 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1152 lhs
.overflow
= false;
1153 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1155 /* As excess precision is zeroed, there is no need to num_trim () as
1156 these operations cannot introduce a set bit there. */
1160 lhs
.high
&= rhs
.high
;
1162 else if (op
== CPP_OR
)
1165 lhs
.high
|= rhs
.high
;
1170 lhs
.high
^= rhs
.high
;
1176 /* Returns LHS OP RHS, where OP is an inequality. */
1178 num_inequality_op (pfile
, lhs
, rhs
, op
)
1183 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1185 if (op
== CPP_GREATER_EQ
)
1187 else if (op
== CPP_LESS
)
1189 else if (op
== CPP_GREATER
)
1190 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1191 else /* CPP_LESS_EQ. */
1192 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1195 lhs
.overflow
= false;
1196 lhs
.unsignedp
= false;
1200 /* Returns LHS OP RHS, where OP is == or !=. */
1202 num_equality_op (pfile
, lhs
, rhs
, op
)
1203 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1207 /* Work around a 3.0.4 bug; see PR 6950. */
1208 bool eq
= num_eq (lhs
, rhs
);
1209 if (op
== CPP_NOT_EQ
)
1213 lhs
.overflow
= false;
1214 lhs
.unsignedp
= false;
1218 /* Shift NUM, of width PRECISION, right by N bits. */
1220 num_rshift (num
, precision
, n
)
1222 size_t precision
, n
;
1224 cpp_num_part sign_mask
;
1226 if (num
.unsignedp
|| num_positive (num
, precision
))
1229 sign_mask
= ~(cpp_num_part
) 0;
1232 num
.high
= num
.low
= sign_mask
;
1236 if (precision
< PART_PRECISION
)
1237 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1238 else if (precision
< 2 * PART_PRECISION
)
1239 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1241 if (n
>= PART_PRECISION
)
1243 n
-= PART_PRECISION
;
1245 num
.high
= sign_mask
;
1250 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1251 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1255 num
= num_trim (num
, precision
);
1256 num
.overflow
= false;
1260 /* Shift NUM, of width PRECISION, left by N bits. */
1262 num_lshift (num
, precision
, n
)
1264 size_t precision
, n
;
1268 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1269 num
.high
= num
.low
= 0;
1273 cpp_num orig
, maybe_orig
;
1277 if (m
>= PART_PRECISION
)
1279 m
-= PART_PRECISION
;
1285 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1288 num
= num_trim (num
, precision
);
1291 num
.overflow
= false;
1294 maybe_orig
= num_rshift (num
, precision
, n
);
1295 num
.overflow
= !num_eq (orig
, maybe_orig
);
1302 /* The four unary operators: +, -, ! and ~. */
1304 num_unary_op (pfile
, num
, op
)
1312 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1313 cpp_error (pfile
, DL_WARNING
,
1314 "traditional C rejects the unary plus operator");
1315 num
.overflow
= false;
1319 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1323 num
.high
= ~num
.high
;
1325 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1326 num
.overflow
= false;
1329 default: /* case CPP_NOT: */
1330 num
.low
= num_zerop (num
);
1332 num
.overflow
= false;
1333 num
.unsignedp
= false;
1340 /* The various binary operators. */
1342 num_binary_op (pfile
, lhs
, rhs
, op
)
1348 size_t precision
= CPP_OPTION (pfile
, precision
);
1357 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1359 /* A negative shift is a positive shift the other way. */
1360 if (op
== CPP_LSHIFT
)
1364 rhs
= num_negate (rhs
, precision
);
1367 n
= ~0; /* Maximal. */
1370 if (op
== CPP_LSHIFT
)
1371 lhs
= num_lshift (lhs
, precision
, n
);
1373 lhs
= num_rshift (lhs
, precision
, n
);
1380 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1382 gte
= num_greater_eq (lhs
, rhs
, precision
);
1387 lhs
.unsignedp
= unsignedp
;
1393 rhs
= num_negate (rhs
, precision
);
1395 result
.low
= lhs
.low
+ rhs
.low
;
1396 result
.high
= lhs
.high
+ rhs
.high
;
1397 if (result
.low
< lhs
.low
)
1400 result
= num_trim (result
, precision
);
1401 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1402 if (result
.unsignedp
)
1403 result
.overflow
= false;
1406 bool lhsp
= num_positive (lhs
, precision
);
1407 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1408 && lhsp
!= num_positive (result
, precision
));
1413 default: /* case CPP_COMMA: */
1414 if (CPP_PEDANTIC (pfile
) && !pfile
->state
.skip_eval
)
1415 cpp_error (pfile
, DL_PEDWARN
,
1416 "comma operator in operand of #if");
1424 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1427 num_part_mul (lhs
, rhs
)
1428 cpp_num_part lhs
, rhs
;
1431 cpp_num_part middle
[2], temp
;
1433 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1434 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1436 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1437 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1440 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1441 if (result
.low
< temp
)
1445 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1446 if (result
.low
< temp
)
1449 result
.high
+= HIGH_PART (middle
[0]);
1450 result
.high
+= HIGH_PART (middle
[1]);
1451 result
.unsignedp
= 1;
1456 /* Multiply two preprocessing numbers. */
1458 num_mul (pfile
, lhs
, rhs
)
1462 cpp_num result
, temp
;
1463 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1464 bool overflow
, negate
= false;
1465 size_t precision
= CPP_OPTION (pfile
, precision
);
1467 /* Prepare for unsigned multiplication. */
1470 if (!num_positive (lhs
, precision
))
1471 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1472 if (!num_positive (rhs
, precision
))
1473 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1476 overflow
= lhs
.high
&& rhs
.high
;
1477 result
= num_part_mul (lhs
.low
, rhs
.low
);
1479 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1480 result
.high
+= temp
.low
;
1484 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1485 result
.high
+= temp
.low
;
1489 temp
.low
= result
.low
, temp
.high
= result
.high
;
1490 result
= num_trim (result
, precision
);
1491 if (!num_eq (result
, temp
))
1495 result
= num_negate (result
, precision
);
1498 result
.overflow
= false;
1500 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1501 && !num_zerop (result
));
1502 result
.unsignedp
= unsignedp
;
1507 /* Divide two preprocessing numbers, returning the answer or the
1508 remainder depending upon OP. */
1510 num_div_op (pfile
, lhs
, rhs
, op
)
1515 cpp_num result
, sub
;
1517 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1518 bool negate
= false, lhs_neg
= false;
1519 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1521 /* Prepare for unsigned division. */
1524 if (!num_positive (lhs
, precision
))
1525 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1526 if (!num_positive (rhs
, precision
))
1527 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1530 /* Find the high bit. */
1534 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1535 for (; ; i
--, mask
>>= 1)
1536 if (rhs
.high
& mask
)
1541 if (precision
> PART_PRECISION
)
1542 i
= precision
- PART_PRECISION
- 1;
1545 mask
= (cpp_num_part
) 1 << i
;
1546 for (; ; i
--, mask
>>= 1)
1552 if (!pfile
->state
.skip_eval
)
1553 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1557 /* First nonzero bit of RHS is bit I. Do naive division by
1558 shifting the RHS fully left, and subtracting from LHS if LHS is
1559 at least as big, and then repeating but with one less shift.
1560 This is not very efficient, but is easy to understand. */
1562 rhs
.unsignedp
= true;
1563 lhs
.unsignedp
= true;
1564 i
= precision
- i
- 1;
1565 sub
= num_lshift (rhs
, precision
, i
);
1567 result
.high
= result
.low
= 0;
1570 if (num_greater_eq (lhs
, sub
, precision
))
1572 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1573 if (i
>= PART_PRECISION
)
1574 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1576 result
.low
|= (cpp_num_part
) 1 << i
;
1580 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1584 /* We divide so that the remainder has the sign of the LHS. */
1587 result
.unsignedp
= unsignedp
;
1589 result
.overflow
= false;
1593 result
= num_negate (result
, precision
);
1594 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1601 lhs
.unsignedp
= unsignedp
;
1602 lhs
.overflow
= false;
1604 lhs
= num_negate (lhs
, precision
);