1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009, 2010, 2011 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 3, 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; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
32 const cpp_token
*token
; /* The token forming op (for diagnostics). */
33 cpp_num value
; /* The value logically "right" of op. */
34 source_location loc
; /* The location of this value. */
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num
, size_t);
42 static bool num_greater_eq (cpp_num
, cpp_num
, size_t);
43 static cpp_num
num_trim (cpp_num
, size_t);
44 static cpp_num
num_part_mul (cpp_num_part
, cpp_num_part
);
46 static cpp_num
num_unary_op (cpp_reader
*, cpp_num
, enum cpp_ttype
);
47 static cpp_num
num_binary_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
48 static cpp_num
num_negate (cpp_num
, size_t);
49 static cpp_num
num_bitwise_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
50 static cpp_num
num_inequality_op (cpp_reader
*, cpp_num
, cpp_num
,
52 static cpp_num
num_equality_op (cpp_reader
*, cpp_num
, cpp_num
,
54 static cpp_num
num_mul (cpp_reader
*, cpp_num
, cpp_num
);
55 static cpp_num
num_div_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
,
57 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
58 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
60 static cpp_num
append_digit (cpp_num
, int, int, size_t);
61 static cpp_num
parse_defined (cpp_reader
*);
62 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*, source_location
);
63 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
64 static unsigned int interpret_float_suffix (const uchar
*, size_t);
65 static unsigned int interpret_int_suffix (const uchar
*, size_t);
66 static void check_promotion (cpp_reader
*, const struct op
*);
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
79 #define SYNTAX_ERROR_AT(loc, msgid) \
80 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
82 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
83 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
86 /* Subroutine of cpp_classify_number. S points to a float suffix of
87 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
88 flag vector describing the suffix. */
90 interpret_float_suffix (const uchar
*s
, size_t len
)
93 size_t f
, d
, l
, w
, q
, i
;
96 f
= d
= l
= w
= q
= i
= 0;
98 /* Process decimal float suffixes, which are two letters starting
99 with d or D. Order and case are significant. */
100 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
102 bool uppercase
= (*s
== 'D');
105 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
106 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
107 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
108 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
109 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
110 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
112 /* Additional two-character suffixes beginning with D are not
113 for decimal float constants. */
118 /* Recognize a fixed-point suffix. */
122 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
123 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
127 /* Continue processing a fixed-point suffix. The suffix is case
128 insensitive except for ll or LL. Order is significant. */
135 if (*s
== 'u' || *s
== 'U')
137 flags
|= CPP_N_UNSIGNED
;
148 return flags
|= CPP_N_SMALL
;
152 return flags
|= CPP_N_MEDIUM
;
153 if (len
== 2 && s
[1] == 'l')
154 return flags
|= CPP_N_LARGE
;
158 return flags
|= CPP_N_MEDIUM
;
159 if (len
== 2 && s
[1] == 'L')
160 return flags
|= CPP_N_LARGE
;
165 /* Anything left at this point is invalid. */
169 /* In any remaining valid suffix, the case and order don't matter. */
173 case 'f': case 'F': f
++; break;
174 case 'd': case 'D': d
++; break;
175 case 'l': case 'L': l
++; break;
176 case 'w': case 'W': w
++; break;
177 case 'q': case 'Q': q
++; break;
179 case 'j': case 'J': i
++; break;
184 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
187 return ((i
? CPP_N_IMAGINARY
: 0)
192 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
195 /* Return the classification flags for a float suffix. */
197 cpp_interpret_float_suffix (const char *s
, size_t len
)
199 return interpret_float_suffix ((const unsigned char *)s
, len
);
202 /* Subroutine of cpp_classify_number. S points to an integer suffix
203 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
204 flag vector describing the suffix. */
206 interpret_int_suffix (const uchar
*s
, size_t len
)
215 case 'u': case 'U': u
++; break;
217 case 'j': case 'J': i
++; break;
218 case 'l': case 'L': l
++;
219 /* If there are two Ls, they must be adjacent and the same case. */
220 if (l
== 2 && s
[len
] != s
[len
+ 1])
227 if (l
> 2 || u
> 1 || i
> 1)
230 return ((i
? CPP_N_IMAGINARY
: 0)
231 | (u
? CPP_N_UNSIGNED
: 0)
232 | ((l
== 0) ? CPP_N_SMALL
233 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
236 /* Return the classification flags for an int suffix. */
238 cpp_interpret_int_suffix (const char *s
, size_t len
)
240 return interpret_int_suffix ((const unsigned char *)s
, len
);
243 /* Return the string type corresponding to the the input user-defined string
244 literal type. If the input type is not a user-defined string literal
245 type return the input type. */
247 cpp_userdef_string_remove_type (enum cpp_ttype type
)
249 if (type
== CPP_STRING_USERDEF
)
251 else if (type
== CPP_WSTRING_USERDEF
)
253 else if (type
== CPP_STRING16_USERDEF
)
255 else if (type
== CPP_STRING32_USERDEF
)
257 else if (type
== CPP_UTF8STRING_USERDEF
)
258 return CPP_UTF8STRING
;
263 /* Return the user-defined string literal type corresponding to the input
264 string type. If the input type is not a string type return the input
267 cpp_userdef_string_add_type (enum cpp_ttype type
)
269 if (type
== CPP_STRING
)
270 return CPP_STRING_USERDEF
;
271 else if (type
== CPP_WSTRING
)
272 return CPP_WSTRING_USERDEF
;
273 else if (type
== CPP_STRING16
)
274 return CPP_STRING16_USERDEF
;
275 else if (type
== CPP_STRING32
)
276 return CPP_STRING32_USERDEF
;
277 else if (type
== CPP_UTF8STRING
)
278 return CPP_UTF8STRING_USERDEF
;
283 /* Return the char type corresponding to the the input user-defined char
284 literal type. If the input type is not a user-defined char literal
285 type return the input type. */
287 cpp_userdef_char_remove_type (enum cpp_ttype type
)
289 if (type
== CPP_CHAR_USERDEF
)
291 else if (type
== CPP_WCHAR_USERDEF
)
293 else if (type
== CPP_CHAR16_USERDEF
)
295 else if (type
== CPP_CHAR32_USERDEF
)
301 /* Return the user-defined char literal type corresponding to the input
302 char type. If the input type is not a char type return the input
305 cpp_userdef_char_add_type (enum cpp_ttype type
)
307 if (type
== CPP_CHAR
)
308 return CPP_CHAR_USERDEF
;
309 else if (type
== CPP_WCHAR
)
310 return CPP_WCHAR_USERDEF
;
311 else if (type
== CPP_CHAR16
)
312 return CPP_CHAR16_USERDEF
;
313 else if (type
== CPP_CHAR32
)
314 return CPP_CHAR32_USERDEF
;
319 /* Return true if the token type is a user-defined string literal. */
321 cpp_userdef_string_p (enum cpp_ttype type
)
323 if (type
== CPP_STRING_USERDEF
324 || type
== CPP_WSTRING_USERDEF
325 || type
== CPP_STRING16_USERDEF
326 || type
== CPP_STRING32_USERDEF
327 || type
== CPP_UTF8STRING_USERDEF
)
333 /* Return true if the token type is a user-defined char literal. */
335 cpp_userdef_char_p (enum cpp_ttype type
)
337 if (type
== CPP_CHAR_USERDEF
338 || type
== CPP_WCHAR_USERDEF
339 || type
== CPP_CHAR16_USERDEF
340 || type
== CPP_CHAR32_USERDEF
)
346 /* Extract the suffix from a user-defined literal string or char. */
348 cpp_get_userdef_suffix (const cpp_token
*tok
)
350 unsigned int len
= tok
->val
.str
.len
;
351 const char *text
= (const char *)tok
->val
.str
.text
;
354 for (i
= 0; i
< len
; ++i
)
355 if (text
[i
] == '\'' || text
[i
] == '"')
360 for (i
= len
; i
> 0; --i
)
361 if (text
[i
- 1] == delim
)
366 /* Categorize numeric constants according to their field (integer,
367 floating point, or invalid), radix (decimal, octal, hexadecimal),
370 TOKEN is the token that represents the numeric constant to
373 In C++0X if UD_SUFFIX is non null it will be assigned
374 any unrecognized suffix for a user-defined literal.
376 VIRTUAL_LOCATION is the virtual location for TOKEN. */
378 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
379 const char **ud_suffix
, source_location virtual_location
)
381 const uchar
*str
= token
->val
.str
.text
;
383 unsigned int max_digit
, result
, radix
;
384 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
390 /* If the lexer has done its job, length one can only be a single
391 digit. Fast-path this very common case. */
392 if (token
->val
.str
.len
== 1)
393 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
395 limit
= str
+ token
->val
.str
.len
;
396 float_flag
= NOT_FLOAT
;
401 /* First, interpret the radix. */
407 /* Require at least one hex digit to classify it as hex. */
408 if ((*str
== 'x' || *str
== 'X')
409 && (str
[1] == '.' || ISXDIGIT (str
[1])))
414 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
421 /* Now scan for a well-formed integer or float. */
424 unsigned int c
= *str
++;
426 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
435 if (float_flag
== NOT_FLOAT
)
436 float_flag
= AFTER_POINT
;
438 SYNTAX_ERROR_AT (virtual_location
,
439 "too many decimal points in number");
441 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
442 || (radix
== 16 && (c
== 'p' || c
== 'P')))
444 float_flag
= AFTER_EXPON
;
449 /* Start of suffix. */
455 /* The suffix may be for decimal fixed-point constants without exponent. */
456 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
458 result
= interpret_float_suffix (str
, limit
- str
);
459 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
461 result
|= CPP_N_FLOATING
;
462 /* We need to restore the radix to 10, if the radix is 8. */
466 if (CPP_PEDANTIC (pfile
))
467 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
468 "fixed-point constants are a GCC extension");
475 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
478 if (max_digit
>= radix
)
481 SYNTAX_ERROR2_AT (virtual_location
,
482 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
484 SYNTAX_ERROR2_AT (virtual_location
,
485 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
488 if (float_flag
!= NOT_FLOAT
)
492 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
493 "invalid prefix \"0b\" for floating constant");
494 return CPP_N_INVALID
;
497 if (radix
== 16 && !seen_digit
)
498 SYNTAX_ERROR_AT (virtual_location
,
499 "no digits in hexadecimal floating constant");
501 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
502 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
503 "use of C99 hexadecimal floating constant");
505 if (float_flag
== AFTER_EXPON
)
507 if (*str
== '+' || *str
== '-')
510 /* Exponent is decimal, even if string is a hex float. */
512 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
516 while (ISDIGIT (*str
));
518 else if (radix
== 16)
519 SYNTAX_ERROR_AT (virtual_location
,
520 "hexadecimal floating constants require an exponent");
522 result
= interpret_float_suffix (str
, limit
- str
);
525 if (CPP_OPTION (pfile
, user_literals
))
528 *ud_suffix
= (const char *) str
;
529 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
533 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
534 "invalid suffix \"%.*s\" on floating constant",
535 (int) (limit
- str
), str
);
536 return CPP_N_INVALID
;
540 /* Traditional C didn't accept any floating suffixes. */
542 && CPP_WTRADITIONAL (pfile
)
543 && ! cpp_sys_macro_p (pfile
))
544 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
545 "traditional C rejects the \"%.*s\" suffix",
546 (int) (limit
- str
), str
);
548 /* A suffix for double is a GCC extension via decimal float support.
549 If the suffix also specifies an imaginary value we'll catch that
551 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
552 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
553 "suffix for double constant is a GCC extension");
555 /* Radix must be 10 for decimal floats. */
556 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
558 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
559 "invalid suffix \"%.*s\" with hexadecimal floating constant",
560 (int) (limit
- str
), str
);
561 return CPP_N_INVALID
;
564 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
565 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
566 "fixed-point constants are a GCC extension");
568 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
569 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
570 "decimal float constants are a GCC extension");
572 result
|= CPP_N_FLOATING
;
576 result
= interpret_int_suffix (str
, limit
- str
);
579 if (CPP_OPTION (pfile
, user_literals
))
582 *ud_suffix
= (const char *) str
;
583 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
587 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
588 "invalid suffix \"%.*s\" on integer constant",
589 (int) (limit
- str
), str
);
590 return CPP_N_INVALID
;
594 /* Traditional C only accepted the 'L' suffix.
595 Suppress warning about 'LL' with -Wno-long-long. */
596 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
598 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
599 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
600 && CPP_OPTION (pfile
, cpp_warn_long_long
);
603 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
605 "traditional C rejects the \"%.*s\" suffix",
606 (int) (limit
- str
), str
);
609 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
610 && CPP_OPTION (pfile
, cpp_warn_long_long
))
612 const char *message
= CPP_OPTION (pfile
, cplusplus
)
613 ? N_("use of C++0x long long integer constant")
614 : N_("use of C99 long long integer constant");
616 if (CPP_OPTION (pfile
, c99
))
617 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
620 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
621 virtual_location
, 0, message
);
624 result
|= CPP_N_INTEGER
;
628 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
629 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
630 "imaginary constants are a GCC extension");
631 if (radix
== 2 && CPP_PEDANTIC (pfile
))
632 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
633 "binary constants are a GCC extension");
636 result
|= CPP_N_DECIMAL
;
637 else if (radix
== 16)
640 result
|= CPP_N_BINARY
;
642 result
|= CPP_N_OCTAL
;
647 return CPP_N_INVALID
;
650 /* cpp_interpret_integer converts an integer constant into a cpp_num,
651 of precision options->precision.
653 We do not provide any interface for decimal->float conversion,
654 because the preprocessor doesn't need it and we don't want to
655 drag in GCC's floating point emulator. */
657 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
660 const uchar
*p
, *end
;
665 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
666 result
.overflow
= false;
668 p
= token
->val
.str
.text
;
669 end
= p
+ token
->val
.str
.len
;
671 /* Common case of a single digit. */
672 if (token
->val
.str
.len
== 1)
673 result
.low
= p
[0] - '0';
677 size_t precision
= CPP_OPTION (pfile
, precision
);
678 unsigned int base
= 10, c
= 0;
679 bool overflow
= false;
681 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
686 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
691 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
697 /* We can add a digit to numbers strictly less than this without
698 needing the precision and slowness of double integers. */
699 max
= ~(cpp_num_part
) 0;
700 if (precision
< PART_PRECISION
)
701 max
>>= PART_PRECISION
- precision
;
702 max
= (max
- base
+ 1) / base
+ 1;
708 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
713 /* Strict inequality for when max is set to zero. */
714 if (result
.low
< max
)
715 result
.low
= result
.low
* base
+ c
;
718 result
= append_digit (result
, c
, base
, precision
);
719 overflow
|= result
.overflow
;
724 if (overflow
&& !(type
& CPP_N_USERDEF
))
725 cpp_error (pfile
, CPP_DL_PEDWARN
,
726 "integer constant is too large for its type");
727 /* If too big to be signed, consider it unsigned. Only warn for
728 decimal numbers. Traditional numbers were always signed (but
729 we still honor an explicit U suffix); but we only have
730 traditional semantics in directives. */
731 else if (!result
.unsignedp
732 && !(CPP_OPTION (pfile
, traditional
)
733 && pfile
->state
.in_directive
)
734 && !num_positive (result
, precision
))
736 /* This is for constants within the range of uintmax_t but
737 not that of intmax_t. For such decimal constants, a
738 diagnostic is required for C99 as the selected type must
739 be signed and not having a type is a constraint violation
740 (DR#298, TC3), so this must be a pedwarn. For C90,
741 unsigned long is specified to be used for a constant that
742 does not fit in signed long; if uintmax_t has the same
743 range as unsigned long this means only a warning is
744 appropriate here. C90 permits the preprocessor to use a
745 wider range than unsigned long in the compiler, so if
746 uintmax_t is wider than unsigned long no diagnostic is
747 required for such constants in preprocessor #if
748 expressions and the compiler will pedwarn for such
749 constants outside the range of unsigned long that reach
750 the compiler so a diagnostic is not required there
751 either; thus, pedwarn for C99 but use a plain warning for
754 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
757 "integer constant is so large that it is unsigned");
758 result
.unsignedp
= true;
765 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
767 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
772 cpp_num_part add_high
, add_low
;
774 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
775 need to worry about add_high overflowing. */
789 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
790 result
.high
= num
.high
<< shift
;
791 result
.low
= num
.low
<< shift
;
792 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
793 result
.unsignedp
= num
.unsignedp
;
797 add_low
= num
.low
<< 1;
798 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
801 add_high
= add_low
= 0;
803 if (add_low
+ digit
< add_low
)
807 if (result
.low
+ add_low
< result
.low
)
809 if (result
.high
+ add_high
< result
.high
)
812 result
.low
+= add_low
;
813 result
.high
+= add_high
;
814 result
.overflow
= overflow
;
816 /* The above code catches overflow of a cpp_num type. This catches
817 overflow of the (possibly shorter) target precision. */
818 num
.low
= result
.low
;
819 num
.high
= result
.high
;
820 result
= num_trim (result
, precision
);
821 if (!num_eq (result
, num
))
822 result
.overflow
= true;
827 /* Handle meeting "defined" in a preprocessor expression. */
829 parse_defined (cpp_reader
*pfile
)
833 cpp_hashnode
*node
= 0;
834 const cpp_token
*token
;
835 cpp_context
*initial_context
= pfile
->context
;
837 /* Don't expand macros. */
838 pfile
->state
.prevent_expansion
++;
840 token
= cpp_get_token (pfile
);
841 if (token
->type
== CPP_OPEN_PAREN
)
844 token
= cpp_get_token (pfile
);
847 if (token
->type
== CPP_NAME
)
849 node
= token
->val
.node
.node
;
850 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
852 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
858 cpp_error (pfile
, CPP_DL_ERROR
,
859 "operator \"defined\" requires an identifier");
860 if (token
->flags
& NAMED_OP
)
865 op
.type
= token
->type
;
866 cpp_error (pfile
, CPP_DL_ERROR
,
867 "(\"%s\" is an alternative token for \"%s\" in C++)",
868 cpp_token_as_text (pfile
, token
),
869 cpp_token_as_text (pfile
, &op
));
875 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
876 cpp_error (pfile
, CPP_DL_WARNING
,
877 "this use of \"defined\" may not be portable");
879 _cpp_mark_macro_used (node
);
880 if (!(node
->flags
& NODE_USED
))
882 node
->flags
|= NODE_USED
;
883 if (node
->type
== NT_MACRO
)
885 if ((node
->flags
& NODE_BUILTIN
)
886 && pfile
->cb
.user_builtin_macro
)
887 pfile
->cb
.user_builtin_macro (pfile
, node
);
888 if (pfile
->cb
.used_define
)
889 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
893 if (pfile
->cb
.used_undef
)
894 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
898 /* A possible controlling macro of the form #if !defined ().
899 _cpp_parse_expr checks there was no other junk on the line. */
900 pfile
->mi_ind_cmacro
= node
;
903 pfile
->state
.prevent_expansion
--;
905 /* Do not treat conditional macros as being defined. This is due to the
906 powerpc and spu ports using conditional macros for 'vector', 'bool', and
907 'pixel' to act as conditional keywords. This messes up tests like #ifndef
909 result
.unsignedp
= false;
911 result
.overflow
= false;
912 result
.low
= (node
&& node
->type
== NT_MACRO
913 && (node
->flags
& NODE_CONDITIONAL
) == 0);
917 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
918 number or character constant, or the result of the "defined" or "#"
921 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
922 source_location virtual_location
)
928 result
.unsignedp
= false;
929 result
.overflow
= false;
934 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
935 if (temp
& CPP_N_USERDEF
)
936 cpp_error (pfile
, CPP_DL_ERROR
,
937 "user-defined literal in preprocessor expression");
938 switch (temp
& CPP_N_CATEGORY
)
941 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
942 "floating constant in preprocessor expression");
945 if (!(temp
& CPP_N_IMAGINARY
))
946 return cpp_interpret_integer (pfile
, token
, temp
);
947 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
948 "imaginary number in preprocessor expression");
952 /* Error already issued. */
955 result
.high
= result
.low
= 0;
963 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
968 /* Sign-extend the result if necessary. */
969 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
971 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
972 result
.low
|= ~(~(cpp_num_part
) 0
973 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
974 result
.high
= ~(cpp_num_part
) 0;
975 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
981 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
982 return parse_defined (pfile
);
983 else if (CPP_OPTION (pfile
, cplusplus
)
984 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
985 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
988 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
994 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
995 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
996 "\"%s\" is not defined",
997 NODE_NAME (token
->val
.node
.node
));
1002 if (!pfile
->state
.skipping
)
1004 /* A pedantic warning takes precedence over a deprecated
1006 if (CPP_PEDANTIC (pfile
))
1007 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1008 virtual_location
, 0,
1009 "assertions are a GCC extension");
1010 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1011 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1012 "assertions are a deprecated extension");
1014 _cpp_test_assertion (pfile
, &temp
);
1023 result
.unsignedp
= !!unsignedp
;
1027 /* Operator precedence and flags table.
1029 After an operator is returned from the lexer, if it has priority less
1030 than the operator on the top of the stack, we reduce the stack by one
1031 operator and repeat the test. Since equal priorities do not reduce,
1032 this is naturally right-associative.
1034 We handle left-associative operators by decrementing the priority of
1035 just-lexed operators by one, but retaining the priority of operators
1036 already on the stack.
1038 The remaining cases are '(' and ')'. We handle '(' by skipping the
1039 reduction phase completely. ')' is given lower priority than
1040 everything else, including '(', effectively forcing a reduction of the
1041 parenthesized expression. If there is a matching '(', the routine
1042 reduce() exits immediately. If the normal exit route sees a ')', then
1043 there cannot have been a matching '(' and an error message is output.
1045 The parser assumes all shifted operators require a left operand unless
1046 the flag NO_L_OPERAND is set. These semantics are automatic; any
1047 extra semantics need to be handled with operator-specific code. */
1049 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1050 operand changes because of integer promotions. */
1051 #define NO_L_OPERAND (1 << 0)
1052 #define LEFT_ASSOC (1 << 1)
1053 #define CHECK_PROMOTION (1 << 2)
1055 /* Operator to priority map. Must be in the same order as the first
1056 N entries of enum cpp_ttype. */
1057 static const struct cpp_operator
1063 /* EQ */ {0, 0}, /* Shouldn't happen. */
1064 /* NOT */ {16, NO_L_OPERAND
},
1065 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1066 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1067 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1068 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1069 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1070 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1071 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1072 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1073 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1074 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1075 /* RSHIFT */ {13, LEFT_ASSOC
},
1076 /* LSHIFT */ {13, LEFT_ASSOC
},
1078 /* COMPL */ {16, NO_L_OPERAND
},
1079 /* AND_AND */ {6, LEFT_ASSOC
},
1080 /* OR_OR */ {5, LEFT_ASSOC
},
1081 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1082 However, there are some special cases for these in reduce(). */
1084 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1085 /* COMMA */ {4, LEFT_ASSOC
},
1086 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1087 /* CLOSE_PAREN */ {0, 0},
1089 /* EQ_EQ */ {11, LEFT_ASSOC
},
1090 /* NOT_EQ */ {11, LEFT_ASSOC
},
1091 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1092 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1093 /* UPLUS */ {16, NO_L_OPERAND
},
1094 /* UMINUS */ {16, NO_L_OPERAND
}
1097 /* Parse and evaluate a C expression, reading from PFILE.
1098 Returns the truth value of the expression.
1100 The implementation is an operator precedence parser, i.e. a
1101 bottom-up parser, using a stack for not-yet-reduced tokens.
1103 The stack base is op_stack, and the current stack pointer is 'top'.
1104 There is a stack element for each operator (only), and the most
1105 recently pushed operator is 'top->op'. An operand (value) is
1106 stored in the 'value' field of the stack element of the operator
1107 that precedes it. */
1109 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1111 struct op
*top
= pfile
->op_stack
;
1112 unsigned int lex_count
;
1113 bool saw_leading_not
, want_value
= true;
1114 source_location virtual_location
= 0;
1116 pfile
->state
.skip_eval
= 0;
1118 /* Set up detection of #if ! defined(). */
1119 pfile
->mi_ind_cmacro
= 0;
1120 saw_leading_not
= false;
1123 /* Lowest priority operator prevents further reductions. */
1131 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1132 op
.op
= op
.token
->type
;
1133 op
.loc
= virtual_location
;
1137 /* These tokens convert into values. */
1146 SYNTAX_ERROR2_AT (op
.loc
,
1147 "missing binary operator before token \"%s\"",
1148 cpp_token_as_text (pfile
, op
.token
));
1150 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1154 saw_leading_not
= lex_count
== 1;
1166 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1167 SYNTAX_ERROR2_AT (op
.loc
,
1168 "token \"%s\" is not valid in preprocessor expressions",
1169 cpp_token_as_text (pfile
, op
.token
));
1173 /* Check we have a value or operator as appropriate. */
1174 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1177 SYNTAX_ERROR2_AT (op
.loc
,
1178 "missing binary operator before token \"%s\"",
1179 cpp_token_as_text (pfile
, op
.token
));
1181 else if (want_value
)
1183 /* We want a number (or expression) and haven't got one.
1184 Try to emit a specific diagnostic. */
1185 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1186 SYNTAX_ERROR_AT (op
.loc
,
1187 "missing expression between '(' and ')'");
1189 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1190 SYNTAX_ERROR2_AT (op
.loc
,
1191 "%s with no expression", is_if
? "#if" : "#elif");
1193 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1194 SYNTAX_ERROR2_AT (op
.loc
,
1195 "operator '%s' has no right operand",
1196 cpp_token_as_text (pfile
, top
->token
));
1197 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1198 /* Complain about missing paren during reduction. */;
1200 SYNTAX_ERROR2_AT (op
.loc
,
1201 "operator '%s' has no left operand",
1202 cpp_token_as_text (pfile
, op
.token
));
1205 top
= reduce (pfile
, top
, op
.op
);
1209 if (op
.op
== CPP_EOF
)
1214 case CPP_CLOSE_PAREN
:
1217 if (!num_zerop (top
->value
))
1218 pfile
->state
.skip_eval
++;
1222 if (num_zerop (top
->value
))
1223 pfile
->state
.skip_eval
++;
1226 if (top
->op
!= CPP_QUERY
)
1227 SYNTAX_ERROR_AT (op
.loc
,
1228 " ':' without preceding '?'");
1229 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1230 pfile
->state
.skip_eval
++;
1232 pfile
->state
.skip_eval
--;
1239 /* Check for and handle stack overflow. */
1240 if (++top
== pfile
->op_limit
)
1241 top
= _cpp_expand_op_stack (pfile
);
1244 top
->token
= op
.token
;
1248 /* The controlling macro expression is only valid if we called lex 3
1249 times: <!> <defined expression> and <EOF>. push_conditional ()
1250 checks that we are at top-of-file. */
1251 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1252 pfile
->mi_ind_cmacro
= 0;
1254 if (top
!= pfile
->op_stack
)
1256 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1257 "unbalanced stack in %s",
1258 is_if
? "#if" : "#elif");
1260 return false; /* Return false on syntax error. */
1263 return !num_zerop (top
->value
);
1266 /* Reduce the operator / value stack if possible, in preparation for
1267 pushing operator OP. Returns NULL on error, otherwise the top of
1270 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1274 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1277 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1281 if (op
== CPP_OPEN_PAREN
)
1284 /* Decrement the priority of left-associative operators to force a
1285 reduction with operators of otherwise equal priority. */
1286 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1287 while (prio
< optab
[top
->op
].prio
)
1289 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1290 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1291 check_promotion (pfile
, top
);
1299 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1300 top
[-1].loc
= top
->loc
;
1308 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1309 top
->value
, top
->op
);
1310 top
[-1].loc
= top
->loc
;
1315 case CPP_GREATER_EQ
:
1318 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1319 top
[-1].loc
= top
->loc
;
1325 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1326 top
[-1].loc
= top
->loc
;
1333 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1334 top
[-1].loc
= top
->loc
;
1338 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1339 top
[-1].loc
= top
->loc
;
1344 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1345 top
->value
, top
->op
, top
->loc
);
1346 top
[-1].loc
= top
->loc
;
1351 if (!num_zerop (top
->value
))
1352 pfile
->state
.skip_eval
--;
1353 top
->value
.low
= (!num_zerop (top
->value
)
1354 || !num_zerop (top
[1].value
));
1355 top
->value
.high
= 0;
1356 top
->value
.unsignedp
= false;
1357 top
->value
.overflow
= false;
1358 top
->loc
= top
[1].loc
;
1363 if (num_zerop (top
->value
))
1364 pfile
->state
.skip_eval
--;
1365 top
->value
.low
= (!num_zerop (top
->value
)
1366 && !num_zerop (top
[1].value
));
1367 top
->value
.high
= 0;
1368 top
->value
.unsignedp
= false;
1369 top
->value
.overflow
= false;
1370 top
->loc
= top
[1].loc
;
1373 case CPP_OPEN_PAREN
:
1374 if (op
!= CPP_CLOSE_PAREN
)
1376 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1377 top
->token
->src_loc
,
1378 0, "missing ')' in expression");
1382 top
->value
= top
[1].value
;
1383 top
->loc
= top
[1].loc
;
1388 if (!num_zerop (top
->value
))
1390 pfile
->state
.skip_eval
--;
1391 top
->value
= top
[1].value
;
1392 top
->loc
= top
[1].loc
;
1396 top
->value
= top
[2].value
;
1397 top
->loc
= top
[2].loc
;
1399 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1400 || top
[2].value
.unsignedp
);
1404 /* COMMA and COLON should not reduce a QUERY operator. */
1405 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1407 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1415 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1416 cpp_error (pfile
, CPP_DL_PEDWARN
,
1417 "integer overflow in preprocessor expression");
1420 if (op
== CPP_CLOSE_PAREN
)
1422 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1429 /* Returns the position of the old top of stack after expansion. */
1431 _cpp_expand_op_stack (cpp_reader
*pfile
)
1433 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1434 size_t new_size
= old_size
* 2 + 20;
1436 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1437 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1439 return pfile
->op_stack
+ old_size
;
1442 /* Emits a warning if the effective sign of either operand of OP
1443 changes because of integer promotions. */
1445 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1447 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1450 if (op
->value
.unsignedp
)
1452 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1453 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1454 "the left operand of \"%s\" changes sign when promoted",
1455 cpp_token_as_text (pfile
, op
->token
));
1457 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1458 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1459 "the right operand of \"%s\" changes sign when promoted",
1460 cpp_token_as_text (pfile
, op
->token
));
1463 /* Clears the unused high order bits of the number pointed to by PNUM. */
1465 num_trim (cpp_num num
, size_t precision
)
1467 if (precision
> PART_PRECISION
)
1469 precision
-= PART_PRECISION
;
1470 if (precision
< PART_PRECISION
)
1471 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1475 if (precision
< PART_PRECISION
)
1476 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1483 /* True iff A (presumed signed) >= 0. */
1485 num_positive (cpp_num num
, size_t precision
)
1487 if (precision
> PART_PRECISION
)
1489 precision
-= PART_PRECISION
;
1490 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1493 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1496 /* Sign extend a number, with PRECISION significant bits and all
1497 others assumed clear, to fill out a cpp_num structure. */
1499 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1503 if (precision
> PART_PRECISION
)
1505 precision
-= PART_PRECISION
;
1506 if (precision
< PART_PRECISION
1507 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1508 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1510 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1512 if (precision
< PART_PRECISION
)
1513 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1514 num
.high
= ~(cpp_num_part
) 0;
1521 /* Returns the negative of NUM. */
1523 num_negate (cpp_num num
, size_t precision
)
1528 num
.high
= ~num
.high
;
1532 num
= num_trim (num
, precision
);
1533 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1538 /* Returns true if A >= B. */
1540 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1544 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1548 /* Both numbers have signed type. If they are of different
1549 sign, the answer is the sign of A. */
1550 unsignedp
= num_positive (pa
, precision
);
1552 if (unsignedp
!= num_positive (pb
, precision
))
1555 /* Otherwise we can do an unsigned comparison. */
1558 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1561 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1563 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1564 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1566 lhs
.overflow
= false;
1567 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1569 /* As excess precision is zeroed, there is no need to num_trim () as
1570 these operations cannot introduce a set bit there. */
1574 lhs
.high
&= rhs
.high
;
1576 else if (op
== CPP_OR
)
1579 lhs
.high
|= rhs
.high
;
1584 lhs
.high
^= rhs
.high
;
1590 /* Returns LHS OP RHS, where OP is an inequality. */
1592 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1595 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1597 if (op
== CPP_GREATER_EQ
)
1599 else if (op
== CPP_LESS
)
1601 else if (op
== CPP_GREATER
)
1602 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1603 else /* CPP_LESS_EQ. */
1604 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1607 lhs
.overflow
= false;
1608 lhs
.unsignedp
= false;
1612 /* Returns LHS OP RHS, where OP is == or !=. */
1614 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1615 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1617 /* Work around a 3.0.4 bug; see PR 6950. */
1618 bool eq
= num_eq (lhs
, rhs
);
1619 if (op
== CPP_NOT_EQ
)
1623 lhs
.overflow
= false;
1624 lhs
.unsignedp
= false;
1628 /* Shift NUM, of width PRECISION, right by N bits. */
1630 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1632 cpp_num_part sign_mask
;
1633 bool x
= num_positive (num
, precision
);
1635 if (num
.unsignedp
|| x
)
1638 sign_mask
= ~(cpp_num_part
) 0;
1641 num
.high
= num
.low
= sign_mask
;
1645 if (precision
< PART_PRECISION
)
1646 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1647 else if (precision
< 2 * PART_PRECISION
)
1648 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1650 if (n
>= PART_PRECISION
)
1652 n
-= PART_PRECISION
;
1654 num
.high
= sign_mask
;
1659 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1660 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1664 num
= num_trim (num
, precision
);
1665 num
.overflow
= false;
1669 /* Shift NUM, of width PRECISION, left by N bits. */
1671 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1675 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1676 num
.high
= num
.low
= 0;
1680 cpp_num orig
, maybe_orig
;
1684 if (m
>= PART_PRECISION
)
1686 m
-= PART_PRECISION
;
1692 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1695 num
= num_trim (num
, precision
);
1698 num
.overflow
= false;
1701 maybe_orig
= num_rshift (num
, precision
, n
);
1702 num
.overflow
= !num_eq (orig
, maybe_orig
);
1709 /* The four unary operators: +, -, ! and ~. */
1711 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1716 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1717 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1718 "traditional C rejects the unary plus operator");
1719 num
.overflow
= false;
1723 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1727 num
.high
= ~num
.high
;
1729 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1730 num
.overflow
= false;
1733 default: /* case CPP_NOT: */
1734 num
.low
= num_zerop (num
);
1736 num
.overflow
= false;
1737 num
.unsignedp
= false;
1744 /* The various binary operators. */
1746 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1749 size_t precision
= CPP_OPTION (pfile
, precision
);
1757 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1759 /* A negative shift is a positive shift the other way. */
1760 if (op
== CPP_LSHIFT
)
1764 rhs
= num_negate (rhs
, precision
);
1767 n
= ~0; /* Maximal. */
1770 if (op
== CPP_LSHIFT
)
1771 lhs
= num_lshift (lhs
, precision
, n
);
1773 lhs
= num_rshift (lhs
, precision
, n
);
1778 rhs
= num_negate (rhs
, precision
);
1780 result
.low
= lhs
.low
+ rhs
.low
;
1781 result
.high
= lhs
.high
+ rhs
.high
;
1782 if (result
.low
< lhs
.low
)
1784 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1785 result
.overflow
= false;
1787 result
= num_trim (result
, precision
);
1788 if (!result
.unsignedp
)
1790 bool lhsp
= num_positive (lhs
, precision
);
1791 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1792 && lhsp
!= num_positive (result
, precision
));
1797 default: /* case CPP_COMMA: */
1798 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1799 || !pfile
->state
.skip_eval
))
1800 cpp_error (pfile
, CPP_DL_PEDWARN
,
1801 "comma operator in operand of #if");
1809 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1812 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1815 cpp_num_part middle
[2], temp
;
1817 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1818 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1820 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1821 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1824 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1825 if (result
.low
< temp
)
1829 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1830 if (result
.low
< temp
)
1833 result
.high
+= HIGH_PART (middle
[0]);
1834 result
.high
+= HIGH_PART (middle
[1]);
1835 result
.unsignedp
= true;
1836 result
.overflow
= false;
1841 /* Multiply two preprocessing numbers. */
1843 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1845 cpp_num result
, temp
;
1846 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1847 bool overflow
, negate
= false;
1848 size_t precision
= CPP_OPTION (pfile
, precision
);
1850 /* Prepare for unsigned multiplication. */
1853 if (!num_positive (lhs
, precision
))
1854 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1855 if (!num_positive (rhs
, precision
))
1856 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1859 overflow
= lhs
.high
&& rhs
.high
;
1860 result
= num_part_mul (lhs
.low
, rhs
.low
);
1862 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1863 result
.high
+= temp
.low
;
1867 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1868 result
.high
+= temp
.low
;
1872 temp
.low
= result
.low
, temp
.high
= result
.high
;
1873 result
= num_trim (result
, precision
);
1874 if (!num_eq (result
, temp
))
1878 result
= num_negate (result
, precision
);
1881 result
.overflow
= false;
1883 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1884 && !num_zerop (result
));
1885 result
.unsignedp
= unsignedp
;
1890 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1891 or the remainder depending upon OP. LOCATION is the source location
1892 of this operator (for diagnostics). */
1895 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1896 source_location location
)
1898 cpp_num result
, sub
;
1900 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1901 bool negate
= false, lhs_neg
= false;
1902 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1904 /* Prepare for unsigned division. */
1907 if (!num_positive (lhs
, precision
))
1908 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1909 if (!num_positive (rhs
, precision
))
1910 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1913 /* Find the high bit. */
1917 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1918 for (; ; i
--, mask
>>= 1)
1919 if (rhs
.high
& mask
)
1924 if (precision
> PART_PRECISION
)
1925 i
= precision
- PART_PRECISION
- 1;
1928 mask
= (cpp_num_part
) 1 << i
;
1929 for (; ; i
--, mask
>>= 1)
1935 if (!pfile
->state
.skip_eval
)
1936 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
1937 "division by zero in #if");
1941 /* First nonzero bit of RHS is bit I. Do naive division by
1942 shifting the RHS fully left, and subtracting from LHS if LHS is
1943 at least as big, and then repeating but with one less shift.
1944 This is not very efficient, but is easy to understand. */
1946 rhs
.unsignedp
= true;
1947 lhs
.unsignedp
= true;
1948 i
= precision
- i
- 1;
1949 sub
= num_lshift (rhs
, precision
, i
);
1951 result
.high
= result
.low
= 0;
1954 if (num_greater_eq (lhs
, sub
, precision
))
1956 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1957 if (i
>= PART_PRECISION
)
1958 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1960 result
.low
|= (cpp_num_part
) 1 << i
;
1964 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1968 /* We divide so that the remainder has the sign of the LHS. */
1971 result
.unsignedp
= unsignedp
;
1972 result
.overflow
= false;
1976 result
= num_negate (result
, precision
);
1977 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1978 && !num_zerop (result
));
1985 lhs
.unsignedp
= unsignedp
;
1986 lhs
.overflow
= false;
1988 lhs
= num_negate (lhs
, precision
);