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 (cpp_reader
*, const uchar
*, size_t);
65 static unsigned int interpret_int_suffix (cpp_reader
*, 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 (cpp_reader
*pfile
, 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 if (CPP_OPTION (pfile
, ext_numeric_literals
))
120 /* Recognize a fixed-point suffix. */
124 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
125 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
129 /* Continue processing a fixed-point suffix. The suffix is case
130 insensitive except for ll or LL. Order is significant. */
137 if (*s
== 'u' || *s
== 'U')
139 flags
|= CPP_N_UNSIGNED
;
150 return flags
|= CPP_N_SMALL
;
154 return flags
|= CPP_N_MEDIUM
;
155 if (len
== 2 && s
[1] == 'l')
156 return flags
|= CPP_N_LARGE
;
160 return flags
|= CPP_N_MEDIUM
;
161 if (len
== 2 && s
[1] == 'L')
162 return flags
|= CPP_N_LARGE
;
167 /* Anything left at this point is invalid. */
172 /* In any remaining valid suffix, the case and order don't matter. */
176 case 'f': case 'F': f
++; break;
177 case 'd': case 'D': d
++; break;
178 case 'l': case 'L': l
++; break;
179 case 'w': case 'W': w
++; break;
180 case 'q': case 'Q': q
++; break;
182 case 'j': case 'J': i
++; break;
187 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
190 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
193 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
196 return ((i
? CPP_N_IMAGINARY
: 0)
201 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
204 /* Return the classification flags for a float suffix. */
206 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
208 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
211 /* Subroutine of cpp_classify_number. S points to an integer suffix
212 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
213 flag vector describing the suffix. */
215 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
224 case 'u': case 'U': u
++; break;
226 case 'j': case 'J': i
++; break;
227 case 'l': case 'L': l
++;
228 /* If there are two Ls, they must be adjacent and the same case. */
229 if (l
== 2 && s
[len
] != s
[len
+ 1])
236 if (l
> 2 || u
> 1 || i
> 1)
239 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
242 return ((i
? CPP_N_IMAGINARY
: 0)
243 | (u
? CPP_N_UNSIGNED
: 0)
244 | ((l
== 0) ? CPP_N_SMALL
245 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
248 /* Return the classification flags for an int suffix. */
250 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
252 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
255 /* Return the string type corresponding to the the input user-defined string
256 literal type. If the input type is not a user-defined string literal
257 type return the input type. */
259 cpp_userdef_string_remove_type (enum cpp_ttype type
)
261 if (type
== CPP_STRING_USERDEF
)
263 else if (type
== CPP_WSTRING_USERDEF
)
265 else if (type
== CPP_STRING16_USERDEF
)
267 else if (type
== CPP_STRING32_USERDEF
)
269 else if (type
== CPP_UTF8STRING_USERDEF
)
270 return CPP_UTF8STRING
;
275 /* Return the user-defined string literal type corresponding to the input
276 string type. If the input type is not a string type return the input
279 cpp_userdef_string_add_type (enum cpp_ttype type
)
281 if (type
== CPP_STRING
)
282 return CPP_STRING_USERDEF
;
283 else if (type
== CPP_WSTRING
)
284 return CPP_WSTRING_USERDEF
;
285 else if (type
== CPP_STRING16
)
286 return CPP_STRING16_USERDEF
;
287 else if (type
== CPP_STRING32
)
288 return CPP_STRING32_USERDEF
;
289 else if (type
== CPP_UTF8STRING
)
290 return CPP_UTF8STRING_USERDEF
;
295 /* Return the char type corresponding to the the input user-defined char
296 literal type. If the input type is not a user-defined char literal
297 type return the input type. */
299 cpp_userdef_char_remove_type (enum cpp_ttype type
)
301 if (type
== CPP_CHAR_USERDEF
)
303 else if (type
== CPP_WCHAR_USERDEF
)
305 else if (type
== CPP_CHAR16_USERDEF
)
307 else if (type
== CPP_CHAR32_USERDEF
)
313 /* Return the user-defined char literal type corresponding to the input
314 char type. If the input type is not a char type return the input
317 cpp_userdef_char_add_type (enum cpp_ttype type
)
319 if (type
== CPP_CHAR
)
320 return CPP_CHAR_USERDEF
;
321 else if (type
== CPP_WCHAR
)
322 return CPP_WCHAR_USERDEF
;
323 else if (type
== CPP_CHAR16
)
324 return CPP_CHAR16_USERDEF
;
325 else if (type
== CPP_CHAR32
)
326 return CPP_CHAR32_USERDEF
;
331 /* Return true if the token type is a user-defined string literal. */
333 cpp_userdef_string_p (enum cpp_ttype type
)
335 if (type
== CPP_STRING_USERDEF
336 || type
== CPP_WSTRING_USERDEF
337 || type
== CPP_STRING16_USERDEF
338 || type
== CPP_STRING32_USERDEF
339 || type
== CPP_UTF8STRING_USERDEF
)
345 /* Return true if the token type is a user-defined char literal. */
347 cpp_userdef_char_p (enum cpp_ttype type
)
349 if (type
== CPP_CHAR_USERDEF
350 || type
== CPP_WCHAR_USERDEF
351 || type
== CPP_CHAR16_USERDEF
352 || type
== CPP_CHAR32_USERDEF
)
358 /* Extract the suffix from a user-defined literal string or char. */
360 cpp_get_userdef_suffix (const cpp_token
*tok
)
362 unsigned int len
= tok
->val
.str
.len
;
363 const char *text
= (const char *)tok
->val
.str
.text
;
366 for (i
= 0; i
< len
; ++i
)
367 if (text
[i
] == '\'' || text
[i
] == '"')
372 for (i
= len
; i
> 0; --i
)
373 if (text
[i
- 1] == delim
)
378 /* Categorize numeric constants according to their field (integer,
379 floating point, or invalid), radix (decimal, octal, hexadecimal),
382 TOKEN is the token that represents the numeric constant to
385 In C++0X if UD_SUFFIX is non null it will be assigned
386 any unrecognized suffix for a user-defined literal.
388 VIRTUAL_LOCATION is the virtual location for TOKEN. */
390 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
391 const char **ud_suffix
, source_location virtual_location
)
393 const uchar
*str
= token
->val
.str
.text
;
395 unsigned int max_digit
, result
, radix
;
396 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
402 /* If the lexer has done its job, length one can only be a single
403 digit. Fast-path this very common case. */
404 if (token
->val
.str
.len
== 1)
405 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
407 limit
= str
+ token
->val
.str
.len
;
408 float_flag
= NOT_FLOAT
;
413 /* First, interpret the radix. */
419 /* Require at least one hex digit to classify it as hex. */
420 if ((*str
== 'x' || *str
== 'X')
421 && (str
[1] == '.' || ISXDIGIT (str
[1])))
426 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
433 /* Now scan for a well-formed integer or float. */
436 unsigned int c
= *str
++;
438 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
447 if (float_flag
== NOT_FLOAT
)
448 float_flag
= AFTER_POINT
;
450 SYNTAX_ERROR_AT (virtual_location
,
451 "too many decimal points in number");
453 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
454 || (radix
== 16 && (c
== 'p' || c
== 'P')))
456 float_flag
= AFTER_EXPON
;
461 /* Start of suffix. */
467 /* The suffix may be for decimal fixed-point constants without exponent. */
468 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
470 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
471 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
473 result
|= CPP_N_FLOATING
;
474 /* We need to restore the radix to 10, if the radix is 8. */
478 if (CPP_PEDANTIC (pfile
))
479 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
480 "fixed-point constants are a GCC extension");
487 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
490 if (max_digit
>= radix
)
493 SYNTAX_ERROR2_AT (virtual_location
,
494 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
496 SYNTAX_ERROR2_AT (virtual_location
,
497 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
500 if (float_flag
!= NOT_FLOAT
)
504 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
505 "invalid prefix \"0b\" for floating constant");
506 return CPP_N_INVALID
;
509 if (radix
== 16 && !seen_digit
)
510 SYNTAX_ERROR_AT (virtual_location
,
511 "no digits in hexadecimal floating constant");
513 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
514 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
515 "use of C99 hexadecimal floating constant");
517 if (float_flag
== AFTER_EXPON
)
519 if (*str
== '+' || *str
== '-')
522 /* Exponent is decimal, even if string is a hex float. */
524 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
528 while (ISDIGIT (*str
));
530 else if (radix
== 16)
531 SYNTAX_ERROR_AT (virtual_location
,
532 "hexadecimal floating constants require an exponent");
534 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
537 if (CPP_OPTION (pfile
, user_literals
))
540 *ud_suffix
= (const char *) str
;
541 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
545 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
546 "invalid suffix \"%.*s\" on floating constant",
547 (int) (limit
- str
), str
);
548 return CPP_N_INVALID
;
552 /* Traditional C didn't accept any floating suffixes. */
554 && CPP_WTRADITIONAL (pfile
)
555 && ! cpp_sys_macro_p (pfile
))
556 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
557 "traditional C rejects the \"%.*s\" suffix",
558 (int) (limit
- str
), str
);
560 /* A suffix for double is a GCC extension via decimal float support.
561 If the suffix also specifies an imaginary value we'll catch that
563 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
564 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
565 "suffix for double constant is a GCC extension");
567 /* Radix must be 10 for decimal floats. */
568 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
570 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
571 "invalid suffix \"%.*s\" with hexadecimal floating constant",
572 (int) (limit
- str
), str
);
573 return CPP_N_INVALID
;
576 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
577 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
578 "fixed-point constants are a GCC extension");
580 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
581 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
582 "decimal float constants are a GCC extension");
584 result
|= CPP_N_FLOATING
;
588 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
591 if (CPP_OPTION (pfile
, user_literals
))
594 *ud_suffix
= (const char *) str
;
595 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
599 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
600 "invalid suffix \"%.*s\" on integer constant",
601 (int) (limit
- str
), str
);
602 return CPP_N_INVALID
;
606 /* Traditional C only accepted the 'L' suffix.
607 Suppress warning about 'LL' with -Wno-long-long. */
608 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
610 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
611 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
612 && CPP_OPTION (pfile
, cpp_warn_long_long
);
615 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
617 "traditional C rejects the \"%.*s\" suffix",
618 (int) (limit
- str
), str
);
621 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
622 && CPP_OPTION (pfile
, cpp_warn_long_long
))
624 const char *message
= CPP_OPTION (pfile
, cplusplus
)
625 ? N_("use of C++0x long long integer constant")
626 : N_("use of C99 long long integer constant");
628 if (CPP_OPTION (pfile
, c99
))
629 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
632 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
633 virtual_location
, 0, message
);
636 result
|= CPP_N_INTEGER
;
640 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
641 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
642 "imaginary constants are a GCC extension");
643 if (radix
== 2 && CPP_PEDANTIC (pfile
))
644 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
645 "binary constants are a GCC extension");
648 result
|= CPP_N_DECIMAL
;
649 else if (radix
== 16)
652 result
|= CPP_N_BINARY
;
654 result
|= CPP_N_OCTAL
;
659 return CPP_N_INVALID
;
662 /* cpp_interpret_integer converts an integer constant into a cpp_num,
663 of precision options->precision.
665 We do not provide any interface for decimal->float conversion,
666 because the preprocessor doesn't need it and we don't want to
667 drag in GCC's floating point emulator. */
669 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
672 const uchar
*p
, *end
;
677 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
678 result
.overflow
= false;
680 p
= token
->val
.str
.text
;
681 end
= p
+ token
->val
.str
.len
;
683 /* Common case of a single digit. */
684 if (token
->val
.str
.len
== 1)
685 result
.low
= p
[0] - '0';
689 size_t precision
= CPP_OPTION (pfile
, precision
);
690 unsigned int base
= 10, c
= 0;
691 bool overflow
= false;
693 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
698 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
703 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
709 /* We can add a digit to numbers strictly less than this without
710 needing the precision and slowness of double integers. */
711 max
= ~(cpp_num_part
) 0;
712 if (precision
< PART_PRECISION
)
713 max
>>= PART_PRECISION
- precision
;
714 max
= (max
- base
+ 1) / base
+ 1;
720 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
725 /* Strict inequality for when max is set to zero. */
726 if (result
.low
< max
)
727 result
.low
= result
.low
* base
+ c
;
730 result
= append_digit (result
, c
, base
, precision
);
731 overflow
|= result
.overflow
;
736 if (overflow
&& !(type
& CPP_N_USERDEF
))
737 cpp_error (pfile
, CPP_DL_PEDWARN
,
738 "integer constant is too large for its type");
739 /* If too big to be signed, consider it unsigned. Only warn for
740 decimal numbers. Traditional numbers were always signed (but
741 we still honor an explicit U suffix); but we only have
742 traditional semantics in directives. */
743 else if (!result
.unsignedp
744 && !(CPP_OPTION (pfile
, traditional
)
745 && pfile
->state
.in_directive
)
746 && !num_positive (result
, precision
))
748 /* This is for constants within the range of uintmax_t but
749 not that of intmax_t. For such decimal constants, a
750 diagnostic is required for C99 as the selected type must
751 be signed and not having a type is a constraint violation
752 (DR#298, TC3), so this must be a pedwarn. For C90,
753 unsigned long is specified to be used for a constant that
754 does not fit in signed long; if uintmax_t has the same
755 range as unsigned long this means only a warning is
756 appropriate here. C90 permits the preprocessor to use a
757 wider range than unsigned long in the compiler, so if
758 uintmax_t is wider than unsigned long no diagnostic is
759 required for such constants in preprocessor #if
760 expressions and the compiler will pedwarn for such
761 constants outside the range of unsigned long that reach
762 the compiler so a diagnostic is not required there
763 either; thus, pedwarn for C99 but use a plain warning for
766 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
769 "integer constant is so large that it is unsigned");
770 result
.unsignedp
= true;
777 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
779 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
784 cpp_num_part add_high
, add_low
;
786 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
787 need to worry about add_high overflowing. */
801 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
802 result
.high
= num
.high
<< shift
;
803 result
.low
= num
.low
<< shift
;
804 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
805 result
.unsignedp
= num
.unsignedp
;
809 add_low
= num
.low
<< 1;
810 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
813 add_high
= add_low
= 0;
815 if (add_low
+ digit
< add_low
)
819 if (result
.low
+ add_low
< result
.low
)
821 if (result
.high
+ add_high
< result
.high
)
824 result
.low
+= add_low
;
825 result
.high
+= add_high
;
826 result
.overflow
= overflow
;
828 /* The above code catches overflow of a cpp_num type. This catches
829 overflow of the (possibly shorter) target precision. */
830 num
.low
= result
.low
;
831 num
.high
= result
.high
;
832 result
= num_trim (result
, precision
);
833 if (!num_eq (result
, num
))
834 result
.overflow
= true;
839 /* Handle meeting "defined" in a preprocessor expression. */
841 parse_defined (cpp_reader
*pfile
)
845 cpp_hashnode
*node
= 0;
846 const cpp_token
*token
;
847 cpp_context
*initial_context
= pfile
->context
;
849 /* Don't expand macros. */
850 pfile
->state
.prevent_expansion
++;
852 token
= cpp_get_token (pfile
);
853 if (token
->type
== CPP_OPEN_PAREN
)
856 token
= cpp_get_token (pfile
);
859 if (token
->type
== CPP_NAME
)
861 node
= token
->val
.node
.node
;
862 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
864 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
870 cpp_error (pfile
, CPP_DL_ERROR
,
871 "operator \"defined\" requires an identifier");
872 if (token
->flags
& NAMED_OP
)
877 op
.type
= token
->type
;
878 cpp_error (pfile
, CPP_DL_ERROR
,
879 "(\"%s\" is an alternative token for \"%s\" in C++)",
880 cpp_token_as_text (pfile
, token
),
881 cpp_token_as_text (pfile
, &op
));
887 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
888 cpp_error (pfile
, CPP_DL_WARNING
,
889 "this use of \"defined\" may not be portable");
891 _cpp_mark_macro_used (node
);
892 if (!(node
->flags
& NODE_USED
))
894 node
->flags
|= NODE_USED
;
895 if (node
->type
== NT_MACRO
)
897 if ((node
->flags
& NODE_BUILTIN
)
898 && pfile
->cb
.user_builtin_macro
)
899 pfile
->cb
.user_builtin_macro (pfile
, node
);
900 if (pfile
->cb
.used_define
)
901 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
905 if (pfile
->cb
.used_undef
)
906 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
910 /* A possible controlling macro of the form #if !defined ().
911 _cpp_parse_expr checks there was no other junk on the line. */
912 pfile
->mi_ind_cmacro
= node
;
915 pfile
->state
.prevent_expansion
--;
917 /* Do not treat conditional macros as being defined. This is due to the
918 powerpc and spu ports using conditional macros for 'vector', 'bool', and
919 'pixel' to act as conditional keywords. This messes up tests like #ifndef
921 result
.unsignedp
= false;
923 result
.overflow
= false;
924 result
.low
= (node
&& node
->type
== NT_MACRO
925 && (node
->flags
& NODE_CONDITIONAL
) == 0);
929 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
930 number or character constant, or the result of the "defined" or "#"
933 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
934 source_location virtual_location
)
940 result
.unsignedp
= false;
941 result
.overflow
= false;
946 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
947 if (temp
& CPP_N_USERDEF
)
948 cpp_error (pfile
, CPP_DL_ERROR
,
949 "user-defined literal in preprocessor expression");
950 switch (temp
& CPP_N_CATEGORY
)
953 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
954 "floating constant in preprocessor expression");
957 if (!(temp
& CPP_N_IMAGINARY
))
958 return cpp_interpret_integer (pfile
, token
, temp
);
959 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
960 "imaginary number in preprocessor expression");
964 /* Error already issued. */
967 result
.high
= result
.low
= 0;
975 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
980 /* Sign-extend the result if necessary. */
981 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
983 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
984 result
.low
|= ~(~(cpp_num_part
) 0
985 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
986 result
.high
= ~(cpp_num_part
) 0;
987 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
993 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
994 return parse_defined (pfile
);
995 else if (CPP_OPTION (pfile
, cplusplus
)
996 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
997 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1000 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1006 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1007 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1008 "\"%s\" is not defined",
1009 NODE_NAME (token
->val
.node
.node
));
1014 if (!pfile
->state
.skipping
)
1016 /* A pedantic warning takes precedence over a deprecated
1018 if (CPP_PEDANTIC (pfile
))
1019 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1020 virtual_location
, 0,
1021 "assertions are a GCC extension");
1022 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1023 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1024 "assertions are a deprecated extension");
1026 _cpp_test_assertion (pfile
, &temp
);
1035 result
.unsignedp
= !!unsignedp
;
1039 /* Operator precedence and flags table.
1041 After an operator is returned from the lexer, if it has priority less
1042 than the operator on the top of the stack, we reduce the stack by one
1043 operator and repeat the test. Since equal priorities do not reduce,
1044 this is naturally right-associative.
1046 We handle left-associative operators by decrementing the priority of
1047 just-lexed operators by one, but retaining the priority of operators
1048 already on the stack.
1050 The remaining cases are '(' and ')'. We handle '(' by skipping the
1051 reduction phase completely. ')' is given lower priority than
1052 everything else, including '(', effectively forcing a reduction of the
1053 parenthesized expression. If there is a matching '(', the routine
1054 reduce() exits immediately. If the normal exit route sees a ')', then
1055 there cannot have been a matching '(' and an error message is output.
1057 The parser assumes all shifted operators require a left operand unless
1058 the flag NO_L_OPERAND is set. These semantics are automatic; any
1059 extra semantics need to be handled with operator-specific code. */
1061 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1062 operand changes because of integer promotions. */
1063 #define NO_L_OPERAND (1 << 0)
1064 #define LEFT_ASSOC (1 << 1)
1065 #define CHECK_PROMOTION (1 << 2)
1067 /* Operator to priority map. Must be in the same order as the first
1068 N entries of enum cpp_ttype. */
1069 static const struct cpp_operator
1075 /* EQ */ {0, 0}, /* Shouldn't happen. */
1076 /* NOT */ {16, NO_L_OPERAND
},
1077 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1078 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1079 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1080 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1081 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1082 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1083 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1084 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1085 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1086 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1087 /* RSHIFT */ {13, LEFT_ASSOC
},
1088 /* LSHIFT */ {13, LEFT_ASSOC
},
1090 /* COMPL */ {16, NO_L_OPERAND
},
1091 /* AND_AND */ {6, LEFT_ASSOC
},
1092 /* OR_OR */ {5, LEFT_ASSOC
},
1093 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1094 However, there are some special cases for these in reduce(). */
1096 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1097 /* COMMA */ {4, LEFT_ASSOC
},
1098 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1099 /* CLOSE_PAREN */ {0, 0},
1101 /* EQ_EQ */ {11, LEFT_ASSOC
},
1102 /* NOT_EQ */ {11, LEFT_ASSOC
},
1103 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1104 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1105 /* UPLUS */ {16, NO_L_OPERAND
},
1106 /* UMINUS */ {16, NO_L_OPERAND
}
1109 /* Parse and evaluate a C expression, reading from PFILE.
1110 Returns the truth value of the expression.
1112 The implementation is an operator precedence parser, i.e. a
1113 bottom-up parser, using a stack for not-yet-reduced tokens.
1115 The stack base is op_stack, and the current stack pointer is 'top'.
1116 There is a stack element for each operator (only), and the most
1117 recently pushed operator is 'top->op'. An operand (value) is
1118 stored in the 'value' field of the stack element of the operator
1119 that precedes it. */
1121 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1123 struct op
*top
= pfile
->op_stack
;
1124 unsigned int lex_count
;
1125 bool saw_leading_not
, want_value
= true;
1126 source_location virtual_location
= 0;
1128 pfile
->state
.skip_eval
= 0;
1130 /* Set up detection of #if ! defined(). */
1131 pfile
->mi_ind_cmacro
= 0;
1132 saw_leading_not
= false;
1135 /* Lowest priority operator prevents further reductions. */
1143 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1144 op
.op
= op
.token
->type
;
1145 op
.loc
= virtual_location
;
1149 /* These tokens convert into values. */
1158 SYNTAX_ERROR2_AT (op
.loc
,
1159 "missing binary operator before token \"%s\"",
1160 cpp_token_as_text (pfile
, op
.token
));
1162 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1166 saw_leading_not
= lex_count
== 1;
1178 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1179 SYNTAX_ERROR2_AT (op
.loc
,
1180 "token \"%s\" is not valid in preprocessor expressions",
1181 cpp_token_as_text (pfile
, op
.token
));
1185 /* Check we have a value or operator as appropriate. */
1186 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1189 SYNTAX_ERROR2_AT (op
.loc
,
1190 "missing binary operator before token \"%s\"",
1191 cpp_token_as_text (pfile
, op
.token
));
1193 else if (want_value
)
1195 /* We want a number (or expression) and haven't got one.
1196 Try to emit a specific diagnostic. */
1197 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1198 SYNTAX_ERROR_AT (op
.loc
,
1199 "missing expression between '(' and ')'");
1201 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1202 SYNTAX_ERROR2_AT (op
.loc
,
1203 "%s with no expression", is_if
? "#if" : "#elif");
1205 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1206 SYNTAX_ERROR2_AT (op
.loc
,
1207 "operator '%s' has no right operand",
1208 cpp_token_as_text (pfile
, top
->token
));
1209 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1210 /* Complain about missing paren during reduction. */;
1212 SYNTAX_ERROR2_AT (op
.loc
,
1213 "operator '%s' has no left operand",
1214 cpp_token_as_text (pfile
, op
.token
));
1217 top
= reduce (pfile
, top
, op
.op
);
1221 if (op
.op
== CPP_EOF
)
1226 case CPP_CLOSE_PAREN
:
1229 if (!num_zerop (top
->value
))
1230 pfile
->state
.skip_eval
++;
1234 if (num_zerop (top
->value
))
1235 pfile
->state
.skip_eval
++;
1238 if (top
->op
!= CPP_QUERY
)
1239 SYNTAX_ERROR_AT (op
.loc
,
1240 " ':' without preceding '?'");
1241 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1242 pfile
->state
.skip_eval
++;
1244 pfile
->state
.skip_eval
--;
1251 /* Check for and handle stack overflow. */
1252 if (++top
== pfile
->op_limit
)
1253 top
= _cpp_expand_op_stack (pfile
);
1256 top
->token
= op
.token
;
1260 /* The controlling macro expression is only valid if we called lex 3
1261 times: <!> <defined expression> and <EOF>. push_conditional ()
1262 checks that we are at top-of-file. */
1263 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1264 pfile
->mi_ind_cmacro
= 0;
1266 if (top
!= pfile
->op_stack
)
1268 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1269 "unbalanced stack in %s",
1270 is_if
? "#if" : "#elif");
1272 return false; /* Return false on syntax error. */
1275 return !num_zerop (top
->value
);
1278 /* Reduce the operator / value stack if possible, in preparation for
1279 pushing operator OP. Returns NULL on error, otherwise the top of
1282 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1286 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1289 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1293 if (op
== CPP_OPEN_PAREN
)
1296 /* Decrement the priority of left-associative operators to force a
1297 reduction with operators of otherwise equal priority. */
1298 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1299 while (prio
< optab
[top
->op
].prio
)
1301 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1302 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1303 check_promotion (pfile
, top
);
1311 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1312 top
[-1].loc
= top
->loc
;
1320 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1321 top
->value
, top
->op
);
1322 top
[-1].loc
= top
->loc
;
1327 case CPP_GREATER_EQ
:
1330 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1331 top
[-1].loc
= top
->loc
;
1337 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1338 top
[-1].loc
= top
->loc
;
1345 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1346 top
[-1].loc
= top
->loc
;
1350 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1351 top
[-1].loc
= top
->loc
;
1356 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1357 top
->value
, top
->op
, top
->loc
);
1358 top
[-1].loc
= top
->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
;
1375 if (num_zerop (top
->value
))
1376 pfile
->state
.skip_eval
--;
1377 top
->value
.low
= (!num_zerop (top
->value
)
1378 && !num_zerop (top
[1].value
));
1379 top
->value
.high
= 0;
1380 top
->value
.unsignedp
= false;
1381 top
->value
.overflow
= false;
1382 top
->loc
= top
[1].loc
;
1385 case CPP_OPEN_PAREN
:
1386 if (op
!= CPP_CLOSE_PAREN
)
1388 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1389 top
->token
->src_loc
,
1390 0, "missing ')' in expression");
1394 top
->value
= top
[1].value
;
1395 top
->loc
= top
[1].loc
;
1400 if (!num_zerop (top
->value
))
1402 pfile
->state
.skip_eval
--;
1403 top
->value
= top
[1].value
;
1404 top
->loc
= top
[1].loc
;
1408 top
->value
= top
[2].value
;
1409 top
->loc
= top
[2].loc
;
1411 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1412 || top
[2].value
.unsignedp
);
1416 /* COMMA and COLON should not reduce a QUERY operator. */
1417 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1419 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1427 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1428 cpp_error (pfile
, CPP_DL_PEDWARN
,
1429 "integer overflow in preprocessor expression");
1432 if (op
== CPP_CLOSE_PAREN
)
1434 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1441 /* Returns the position of the old top of stack after expansion. */
1443 _cpp_expand_op_stack (cpp_reader
*pfile
)
1445 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1446 size_t new_size
= old_size
* 2 + 20;
1448 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1449 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1451 return pfile
->op_stack
+ old_size
;
1454 /* Emits a warning if the effective sign of either operand of OP
1455 changes because of integer promotions. */
1457 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1459 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1462 if (op
->value
.unsignedp
)
1464 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1465 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1466 "the left operand of \"%s\" changes sign when promoted",
1467 cpp_token_as_text (pfile
, op
->token
));
1469 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1470 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1471 "the right operand of \"%s\" changes sign when promoted",
1472 cpp_token_as_text (pfile
, op
->token
));
1475 /* Clears the unused high order bits of the number pointed to by PNUM. */
1477 num_trim (cpp_num num
, size_t precision
)
1479 if (precision
> PART_PRECISION
)
1481 precision
-= PART_PRECISION
;
1482 if (precision
< PART_PRECISION
)
1483 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1487 if (precision
< PART_PRECISION
)
1488 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1495 /* True iff A (presumed signed) >= 0. */
1497 num_positive (cpp_num num
, size_t precision
)
1499 if (precision
> PART_PRECISION
)
1501 precision
-= PART_PRECISION
;
1502 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1505 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1508 /* Sign extend a number, with PRECISION significant bits and all
1509 others assumed clear, to fill out a cpp_num structure. */
1511 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1515 if (precision
> PART_PRECISION
)
1517 precision
-= PART_PRECISION
;
1518 if (precision
< PART_PRECISION
1519 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1520 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1522 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1524 if (precision
< PART_PRECISION
)
1525 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1526 num
.high
= ~(cpp_num_part
) 0;
1533 /* Returns the negative of NUM. */
1535 num_negate (cpp_num num
, size_t precision
)
1540 num
.high
= ~num
.high
;
1544 num
= num_trim (num
, precision
);
1545 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1550 /* Returns true if A >= B. */
1552 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1556 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1560 /* Both numbers have signed type. If they are of different
1561 sign, the answer is the sign of A. */
1562 unsignedp
= num_positive (pa
, precision
);
1564 if (unsignedp
!= num_positive (pb
, precision
))
1567 /* Otherwise we can do an unsigned comparison. */
1570 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1573 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1575 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1576 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1578 lhs
.overflow
= false;
1579 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1581 /* As excess precision is zeroed, there is no need to num_trim () as
1582 these operations cannot introduce a set bit there. */
1586 lhs
.high
&= rhs
.high
;
1588 else if (op
== CPP_OR
)
1591 lhs
.high
|= rhs
.high
;
1596 lhs
.high
^= rhs
.high
;
1602 /* Returns LHS OP RHS, where OP is an inequality. */
1604 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1607 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1609 if (op
== CPP_GREATER_EQ
)
1611 else if (op
== CPP_LESS
)
1613 else if (op
== CPP_GREATER
)
1614 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1615 else /* CPP_LESS_EQ. */
1616 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1619 lhs
.overflow
= false;
1620 lhs
.unsignedp
= false;
1624 /* Returns LHS OP RHS, where OP is == or !=. */
1626 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1627 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1629 /* Work around a 3.0.4 bug; see PR 6950. */
1630 bool eq
= num_eq (lhs
, rhs
);
1631 if (op
== CPP_NOT_EQ
)
1635 lhs
.overflow
= false;
1636 lhs
.unsignedp
= false;
1640 /* Shift NUM, of width PRECISION, right by N bits. */
1642 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1644 cpp_num_part sign_mask
;
1645 bool x
= num_positive (num
, precision
);
1647 if (num
.unsignedp
|| x
)
1650 sign_mask
= ~(cpp_num_part
) 0;
1653 num
.high
= num
.low
= sign_mask
;
1657 if (precision
< PART_PRECISION
)
1658 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1659 else if (precision
< 2 * PART_PRECISION
)
1660 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1662 if (n
>= PART_PRECISION
)
1664 n
-= PART_PRECISION
;
1666 num
.high
= sign_mask
;
1671 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1672 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1676 num
= num_trim (num
, precision
);
1677 num
.overflow
= false;
1681 /* Shift NUM, of width PRECISION, left by N bits. */
1683 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1687 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1688 num
.high
= num
.low
= 0;
1692 cpp_num orig
, maybe_orig
;
1696 if (m
>= PART_PRECISION
)
1698 m
-= PART_PRECISION
;
1704 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1707 num
= num_trim (num
, precision
);
1710 num
.overflow
= false;
1713 maybe_orig
= num_rshift (num
, precision
, n
);
1714 num
.overflow
= !num_eq (orig
, maybe_orig
);
1721 /* The four unary operators: +, -, ! and ~. */
1723 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1728 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1729 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1730 "traditional C rejects the unary plus operator");
1731 num
.overflow
= false;
1735 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1739 num
.high
= ~num
.high
;
1741 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1742 num
.overflow
= false;
1745 default: /* case CPP_NOT: */
1746 num
.low
= num_zerop (num
);
1748 num
.overflow
= false;
1749 num
.unsignedp
= false;
1756 /* The various binary operators. */
1758 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1761 size_t precision
= CPP_OPTION (pfile
, precision
);
1769 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1771 /* A negative shift is a positive shift the other way. */
1772 if (op
== CPP_LSHIFT
)
1776 rhs
= num_negate (rhs
, precision
);
1779 n
= ~0; /* Maximal. */
1782 if (op
== CPP_LSHIFT
)
1783 lhs
= num_lshift (lhs
, precision
, n
);
1785 lhs
= num_rshift (lhs
, precision
, n
);
1790 rhs
= num_negate (rhs
, precision
);
1792 result
.low
= lhs
.low
+ rhs
.low
;
1793 result
.high
= lhs
.high
+ rhs
.high
;
1794 if (result
.low
< lhs
.low
)
1796 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1797 result
.overflow
= false;
1799 result
= num_trim (result
, precision
);
1800 if (!result
.unsignedp
)
1802 bool lhsp
= num_positive (lhs
, precision
);
1803 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1804 && lhsp
!= num_positive (result
, precision
));
1809 default: /* case CPP_COMMA: */
1810 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1811 || !pfile
->state
.skip_eval
))
1812 cpp_error (pfile
, CPP_DL_PEDWARN
,
1813 "comma operator in operand of #if");
1821 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1824 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1827 cpp_num_part middle
[2], temp
;
1829 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1830 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1832 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1833 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1836 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1837 if (result
.low
< temp
)
1841 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1842 if (result
.low
< temp
)
1845 result
.high
+= HIGH_PART (middle
[0]);
1846 result
.high
+= HIGH_PART (middle
[1]);
1847 result
.unsignedp
= true;
1848 result
.overflow
= false;
1853 /* Multiply two preprocessing numbers. */
1855 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1857 cpp_num result
, temp
;
1858 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1859 bool overflow
, negate
= false;
1860 size_t precision
= CPP_OPTION (pfile
, precision
);
1862 /* Prepare for unsigned multiplication. */
1865 if (!num_positive (lhs
, precision
))
1866 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1867 if (!num_positive (rhs
, precision
))
1868 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1871 overflow
= lhs
.high
&& rhs
.high
;
1872 result
= num_part_mul (lhs
.low
, rhs
.low
);
1874 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1875 result
.high
+= temp
.low
;
1879 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1880 result
.high
+= temp
.low
;
1884 temp
.low
= result
.low
, temp
.high
= result
.high
;
1885 result
= num_trim (result
, precision
);
1886 if (!num_eq (result
, temp
))
1890 result
= num_negate (result
, precision
);
1893 result
.overflow
= false;
1895 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1896 && !num_zerop (result
));
1897 result
.unsignedp
= unsignedp
;
1902 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1903 or the remainder depending upon OP. LOCATION is the source location
1904 of this operator (for diagnostics). */
1907 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1908 source_location location
)
1910 cpp_num result
, sub
;
1912 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1913 bool negate
= false, lhs_neg
= false;
1914 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1916 /* Prepare for unsigned division. */
1919 if (!num_positive (lhs
, precision
))
1920 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1921 if (!num_positive (rhs
, precision
))
1922 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1925 /* Find the high bit. */
1929 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1930 for (; ; i
--, mask
>>= 1)
1931 if (rhs
.high
& mask
)
1936 if (precision
> PART_PRECISION
)
1937 i
= precision
- PART_PRECISION
- 1;
1940 mask
= (cpp_num_part
) 1 << i
;
1941 for (; ; i
--, mask
>>= 1)
1947 if (!pfile
->state
.skip_eval
)
1948 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
1949 "division by zero in #if");
1953 /* First nonzero bit of RHS is bit I. Do naive division by
1954 shifting the RHS fully left, and subtracting from LHS if LHS is
1955 at least as big, and then repeating but with one less shift.
1956 This is not very efficient, but is easy to understand. */
1958 rhs
.unsignedp
= true;
1959 lhs
.unsignedp
= true;
1960 i
= precision
- i
- 1;
1961 sub
= num_lshift (rhs
, precision
, i
);
1963 result
.high
= result
.low
= 0;
1966 if (num_greater_eq (lhs
, sub
, precision
))
1968 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1969 if (i
>= PART_PRECISION
)
1970 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1972 result
.low
|= (cpp_num_part
) 1 << i
;
1976 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1980 /* We divide so that the remainder has the sign of the LHS. */
1983 result
.unsignedp
= unsignedp
;
1984 result
.overflow
= false;
1988 result
= num_negate (result
, precision
);
1989 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1990 && !num_zerop (result
));
1997 lhs
.unsignedp
= unsignedp
;
1998 lhs
.overflow
= false;
2000 lhs
= num_negate (lhs
, precision
);