1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
24 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
25 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
26 #define LOW_PART(num_part) (num_part & HALF_MASK)
27 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
31 const cpp_token
*token
; /* The token forming op (for diagnostics). */
32 cpp_num value
; /* The value logically "right" of op. */
33 source_location loc
; /* The location of this value. */
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive (cpp_num
, size_t);
41 static bool num_greater_eq (cpp_num
, cpp_num
, size_t);
42 static cpp_num
num_trim (cpp_num
, size_t);
43 static cpp_num
num_part_mul (cpp_num_part
, cpp_num_part
);
45 static cpp_num
num_unary_op (cpp_reader
*, cpp_num
, enum cpp_ttype
);
46 static cpp_num
num_binary_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
47 static cpp_num
num_negate (cpp_num
, size_t);
48 static cpp_num
num_bitwise_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
49 static cpp_num
num_inequality_op (cpp_reader
*, cpp_num
, cpp_num
,
51 static cpp_num
num_equality_op (cpp_reader
*, cpp_num
, cpp_num
,
53 static cpp_num
num_mul (cpp_reader
*, cpp_num
, cpp_num
);
54 static cpp_num
num_div_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
,
56 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
57 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
59 static cpp_num
append_digit (cpp_num
, int, int, size_t);
60 static cpp_num
parse_defined (cpp_reader
*);
61 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*, source_location
);
62 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
63 static unsigned int interpret_float_suffix (cpp_reader
*, const uchar
*, size_t);
64 static unsigned int interpret_int_suffix (cpp_reader
*, const uchar
*, size_t);
65 static void check_promotion (cpp_reader
*, const struct op
*);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 #define SYNTAX_ERROR_AT(loc, msgid) \
79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
81 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
85 /* Subroutine of cpp_classify_number. S points to a float suffix of
86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
87 flag vector describing the suffix. */
89 interpret_float_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
92 size_t f
, d
, l
, w
, q
, i
;
95 f
= d
= l
= w
= q
= i
= 0;
97 /* Process decimal float suffixes, which are two letters starting
98 with d or D. Order and case are significant. */
99 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
101 bool uppercase
= (*s
== 'D');
104 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
105 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
106 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
107 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
108 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
109 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
111 /* Additional two-character suffixes beginning with D are not
112 for decimal float constants. */
117 if (CPP_OPTION (pfile
, ext_numeric_literals
))
119 /* Recognize a fixed-point suffix. */
123 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
124 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
128 /* Continue processing a fixed-point suffix. The suffix is case
129 insensitive except for ll or LL. Order is significant. */
136 if (*s
== 'u' || *s
== 'U')
138 flags
|= CPP_N_UNSIGNED
;
149 return flags
|= CPP_N_SMALL
;
153 return flags
|= CPP_N_MEDIUM
;
154 if (len
== 2 && s
[1] == 'l')
155 return flags
|= CPP_N_LARGE
;
159 return flags
|= CPP_N_MEDIUM
;
160 if (len
== 2 && s
[1] == 'L')
161 return flags
|= CPP_N_LARGE
;
166 /* Anything left at this point is invalid. */
171 /* In any remaining valid suffix, the case and order don't matter. */
175 case 'f': case 'F': f
++; break;
176 case 'd': case 'D': d
++; break;
177 case 'l': case 'L': l
++; break;
178 case 'w': case 'W': w
++; break;
179 case 'q': case 'Q': q
++; break;
181 case 'j': case 'J': i
++; break;
186 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
189 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
192 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
195 return ((i
? CPP_N_IMAGINARY
: 0)
200 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
203 /* Return the classification flags for a float suffix. */
205 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
207 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
210 /* Subroutine of cpp_classify_number. S points to an integer suffix
211 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
212 flag vector describing the suffix. */
214 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
223 case 'u': case 'U': u
++; break;
225 case 'j': case 'J': i
++; break;
226 case 'l': case 'L': l
++;
227 /* If there are two Ls, they must be adjacent and the same case. */
228 if (l
== 2 && s
[len
] != s
[len
+ 1])
235 if (l
> 2 || u
> 1 || i
> 1)
238 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
241 return ((i
? CPP_N_IMAGINARY
: 0)
242 | (u
? CPP_N_UNSIGNED
: 0)
243 | ((l
== 0) ? CPP_N_SMALL
244 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
247 /* Return the classification flags for an int suffix. */
249 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
251 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
254 /* Return the string type corresponding to the the input user-defined string
255 literal type. If the input type is not a user-defined string literal
256 type return the input type. */
258 cpp_userdef_string_remove_type (enum cpp_ttype type
)
260 if (type
== CPP_STRING_USERDEF
)
262 else if (type
== CPP_WSTRING_USERDEF
)
264 else if (type
== CPP_STRING16_USERDEF
)
266 else if (type
== CPP_STRING32_USERDEF
)
268 else if (type
== CPP_UTF8STRING_USERDEF
)
269 return CPP_UTF8STRING
;
274 /* Return the user-defined string literal type corresponding to the input
275 string type. If the input type is not a string type return the input
278 cpp_userdef_string_add_type (enum cpp_ttype type
)
280 if (type
== CPP_STRING
)
281 return CPP_STRING_USERDEF
;
282 else if (type
== CPP_WSTRING
)
283 return CPP_WSTRING_USERDEF
;
284 else if (type
== CPP_STRING16
)
285 return CPP_STRING16_USERDEF
;
286 else if (type
== CPP_STRING32
)
287 return CPP_STRING32_USERDEF
;
288 else if (type
== CPP_UTF8STRING
)
289 return CPP_UTF8STRING_USERDEF
;
294 /* Return the char type corresponding to the the input user-defined char
295 literal type. If the input type is not a user-defined char literal
296 type return the input type. */
298 cpp_userdef_char_remove_type (enum cpp_ttype type
)
300 if (type
== CPP_CHAR_USERDEF
)
302 else if (type
== CPP_WCHAR_USERDEF
)
304 else if (type
== CPP_CHAR16_USERDEF
)
306 else if (type
== CPP_CHAR32_USERDEF
)
312 /* Return the user-defined char literal type corresponding to the input
313 char type. If the input type is not a char type return the input
316 cpp_userdef_char_add_type (enum cpp_ttype type
)
318 if (type
== CPP_CHAR
)
319 return CPP_CHAR_USERDEF
;
320 else if (type
== CPP_WCHAR
)
321 return CPP_WCHAR_USERDEF
;
322 else if (type
== CPP_CHAR16
)
323 return CPP_CHAR16_USERDEF
;
324 else if (type
== CPP_CHAR32
)
325 return CPP_CHAR32_USERDEF
;
330 /* Return true if the token type is a user-defined string literal. */
332 cpp_userdef_string_p (enum cpp_ttype type
)
334 if (type
== CPP_STRING_USERDEF
335 || type
== CPP_WSTRING_USERDEF
336 || type
== CPP_STRING16_USERDEF
337 || type
== CPP_STRING32_USERDEF
338 || type
== CPP_UTF8STRING_USERDEF
)
344 /* Return true if the token type is a user-defined char literal. */
346 cpp_userdef_char_p (enum cpp_ttype type
)
348 if (type
== CPP_CHAR_USERDEF
349 || type
== CPP_WCHAR_USERDEF
350 || type
== CPP_CHAR16_USERDEF
351 || type
== CPP_CHAR32_USERDEF
)
357 /* Extract the suffix from a user-defined literal string or char. */
359 cpp_get_userdef_suffix (const cpp_token
*tok
)
361 unsigned int len
= tok
->val
.str
.len
;
362 const char *text
= (const char *)tok
->val
.str
.text
;
365 for (i
= 0; i
< len
; ++i
)
366 if (text
[i
] == '\'' || text
[i
] == '"')
371 for (i
= len
; i
> 0; --i
)
372 if (text
[i
- 1] == delim
)
377 /* Categorize numeric constants according to their field (integer,
378 floating point, or invalid), radix (decimal, octal, hexadecimal),
381 TOKEN is the token that represents the numeric constant to
384 In C++0X if UD_SUFFIX is non null it will be assigned
385 any unrecognized suffix for a user-defined literal.
387 VIRTUAL_LOCATION is the virtual location for TOKEN. */
389 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
390 const char **ud_suffix
, source_location virtual_location
)
392 const uchar
*str
= token
->val
.str
.text
;
394 unsigned int max_digit
, result
, radix
;
395 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
401 /* If the lexer has done its job, length one can only be a single
402 digit. Fast-path this very common case. */
403 if (token
->val
.str
.len
== 1)
404 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
406 limit
= str
+ token
->val
.str
.len
;
407 float_flag
= NOT_FLOAT
;
412 /* First, interpret the radix. */
418 /* Require at least one hex digit to classify it as hex. */
419 if ((*str
== 'x' || *str
== 'X')
420 && (str
[1] == '.' || ISXDIGIT (str
[1])))
425 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
432 /* Now scan for a well-formed integer or float. */
435 unsigned int c
= *str
++;
437 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
446 if (float_flag
== NOT_FLOAT
)
447 float_flag
= AFTER_POINT
;
449 SYNTAX_ERROR_AT (virtual_location
,
450 "too many decimal points in number");
452 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
453 || (radix
== 16 && (c
== 'p' || c
== 'P')))
455 float_flag
= AFTER_EXPON
;
460 /* Start of suffix. */
466 /* The suffix may be for decimal fixed-point constants without exponent. */
467 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
469 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
470 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
472 result
|= CPP_N_FLOATING
;
473 /* We need to restore the radix to 10, if the radix is 8. */
477 if (CPP_PEDANTIC (pfile
))
478 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
479 "fixed-point constants are a GCC extension");
486 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
489 if (max_digit
>= radix
)
492 SYNTAX_ERROR2_AT (virtual_location
,
493 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
495 SYNTAX_ERROR2_AT (virtual_location
,
496 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
499 if (float_flag
!= NOT_FLOAT
)
503 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
504 "invalid prefix \"0b\" for floating constant");
505 return CPP_N_INVALID
;
508 if (radix
== 16 && !seen_digit
)
509 SYNTAX_ERROR_AT (virtual_location
,
510 "no digits in hexadecimal floating constant");
512 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
513 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
514 "use of C99 hexadecimal floating constant");
516 if (float_flag
== AFTER_EXPON
)
518 if (*str
== '+' || *str
== '-')
521 /* Exponent is decimal, even if string is a hex float. */
523 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
527 while (ISDIGIT (*str
));
529 else if (radix
== 16)
530 SYNTAX_ERROR_AT (virtual_location
,
531 "hexadecimal floating constants require an exponent");
533 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
536 if (CPP_OPTION (pfile
, user_literals
))
539 *ud_suffix
= (const char *) str
;
540 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
544 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
545 "invalid suffix \"%.*s\" on floating constant",
546 (int) (limit
- str
), str
);
547 return CPP_N_INVALID
;
551 /* Traditional C didn't accept any floating suffixes. */
553 && CPP_WTRADITIONAL (pfile
)
554 && ! cpp_sys_macro_p (pfile
))
555 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
556 "traditional C rejects the \"%.*s\" suffix",
557 (int) (limit
- str
), str
);
559 /* A suffix for double is a GCC extension via decimal float support.
560 If the suffix also specifies an imaginary value we'll catch that
562 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
563 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
564 "suffix for double constant is a GCC extension");
566 /* Radix must be 10 for decimal floats. */
567 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
569 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
570 "invalid suffix \"%.*s\" with hexadecimal floating constant",
571 (int) (limit
- str
), str
);
572 return CPP_N_INVALID
;
575 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
576 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
577 "fixed-point constants are a GCC extension");
579 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
580 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
581 "decimal float constants are a GCC extension");
583 result
|= CPP_N_FLOATING
;
587 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
590 if (CPP_OPTION (pfile
, user_literals
))
593 *ud_suffix
= (const char *) str
;
594 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
598 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
599 "invalid suffix \"%.*s\" on integer constant",
600 (int) (limit
- str
), str
);
601 return CPP_N_INVALID
;
605 /* Traditional C only accepted the 'L' suffix.
606 Suppress warning about 'LL' with -Wno-long-long. */
607 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
609 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
610 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
611 && CPP_OPTION (pfile
, cpp_warn_long_long
);
614 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
616 "traditional C rejects the \"%.*s\" suffix",
617 (int) (limit
- str
), str
);
620 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
621 && CPP_OPTION (pfile
, cpp_warn_long_long
))
623 const char *message
= CPP_OPTION (pfile
, cplusplus
)
624 ? N_("use of C++11 long long integer constant")
625 : N_("use of C99 long long integer constant");
627 if (CPP_OPTION (pfile
, c99
))
628 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
631 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
632 virtual_location
, 0, message
);
635 result
|= CPP_N_INTEGER
;
639 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
640 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
641 "imaginary constants are a GCC extension");
643 && !CPP_OPTION (pfile
, binary_constants
)
644 && CPP_PEDANTIC (pfile
))
645 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
646 CPP_OPTION (pfile
, cplusplus
)
647 ? "binary constants are a C++1y feature "
649 : "binary constants are a GCC extension");
652 result
|= CPP_N_DECIMAL
;
653 else if (radix
== 16)
656 result
|= CPP_N_BINARY
;
658 result
|= CPP_N_OCTAL
;
663 return CPP_N_INVALID
;
666 /* cpp_interpret_integer converts an integer constant into a cpp_num,
667 of precision options->precision.
669 We do not provide any interface for decimal->float conversion,
670 because the preprocessor doesn't need it and we don't want to
671 drag in GCC's floating point emulator. */
673 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
676 const uchar
*p
, *end
;
681 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
682 result
.overflow
= false;
684 p
= token
->val
.str
.text
;
685 end
= p
+ token
->val
.str
.len
;
687 /* Common case of a single digit. */
688 if (token
->val
.str
.len
== 1)
689 result
.low
= p
[0] - '0';
693 size_t precision
= CPP_OPTION (pfile
, precision
);
694 unsigned int base
= 10, c
= 0;
695 bool overflow
= false;
697 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
702 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
707 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
713 /* We can add a digit to numbers strictly less than this without
714 needing the precision and slowness of double integers. */
715 max
= ~(cpp_num_part
) 0;
716 if (precision
< PART_PRECISION
)
717 max
>>= PART_PRECISION
- precision
;
718 max
= (max
- base
+ 1) / base
+ 1;
724 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
729 /* Strict inequality for when max is set to zero. */
730 if (result
.low
< max
)
731 result
.low
= result
.low
* base
+ c
;
734 result
= append_digit (result
, c
, base
, precision
);
735 overflow
|= result
.overflow
;
740 if (overflow
&& !(type
& CPP_N_USERDEF
))
741 cpp_error (pfile
, CPP_DL_PEDWARN
,
742 "integer constant is too large for its type");
743 /* If too big to be signed, consider it unsigned. Only warn for
744 decimal numbers. Traditional numbers were always signed (but
745 we still honor an explicit U suffix); but we only have
746 traditional semantics in directives. */
747 else if (!result
.unsignedp
748 && !(CPP_OPTION (pfile
, traditional
)
749 && pfile
->state
.in_directive
)
750 && !num_positive (result
, precision
))
752 /* This is for constants within the range of uintmax_t but
753 not that of intmax_t. For such decimal constants, a
754 diagnostic is required for C99 as the selected type must
755 be signed and not having a type is a constraint violation
756 (DR#298, TC3), so this must be a pedwarn. For C90,
757 unsigned long is specified to be used for a constant that
758 does not fit in signed long; if uintmax_t has the same
759 range as unsigned long this means only a warning is
760 appropriate here. C90 permits the preprocessor to use a
761 wider range than unsigned long in the compiler, so if
762 uintmax_t is wider than unsigned long no diagnostic is
763 required for such constants in preprocessor #if
764 expressions and the compiler will pedwarn for such
765 constants outside the range of unsigned long that reach
766 the compiler so a diagnostic is not required there
767 either; thus, pedwarn for C99 but use a plain warning for
770 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
773 "integer constant is so large that it is unsigned");
774 result
.unsignedp
= true;
781 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
783 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
788 cpp_num_part add_high
, add_low
;
790 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
791 need to worry about add_high overflowing. */
805 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
806 result
.high
= num
.high
<< shift
;
807 result
.low
= num
.low
<< shift
;
808 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
809 result
.unsignedp
= num
.unsignedp
;
813 add_low
= num
.low
<< 1;
814 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
817 add_high
= add_low
= 0;
819 if (add_low
+ digit
< add_low
)
823 if (result
.low
+ add_low
< result
.low
)
825 if (result
.high
+ add_high
< result
.high
)
828 result
.low
+= add_low
;
829 result
.high
+= add_high
;
830 result
.overflow
= overflow
;
832 /* The above code catches overflow of a cpp_num type. This catches
833 overflow of the (possibly shorter) target precision. */
834 num
.low
= result
.low
;
835 num
.high
= result
.high
;
836 result
= num_trim (result
, precision
);
837 if (!num_eq (result
, num
))
838 result
.overflow
= true;
843 /* Handle meeting "defined" in a preprocessor expression. */
845 parse_defined (cpp_reader
*pfile
)
849 cpp_hashnode
*node
= 0;
850 const cpp_token
*token
;
851 cpp_context
*initial_context
= pfile
->context
;
853 /* Don't expand macros. */
854 pfile
->state
.prevent_expansion
++;
856 token
= cpp_get_token (pfile
);
857 if (token
->type
== CPP_OPEN_PAREN
)
860 token
= cpp_get_token (pfile
);
863 if (token
->type
== CPP_NAME
)
865 node
= token
->val
.node
.node
;
866 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
868 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
874 cpp_error (pfile
, CPP_DL_ERROR
,
875 "operator \"defined\" requires an identifier");
876 if (token
->flags
& NAMED_OP
)
881 op
.type
= token
->type
;
882 cpp_error (pfile
, CPP_DL_ERROR
,
883 "(\"%s\" is an alternative token for \"%s\" in C++)",
884 cpp_token_as_text (pfile
, token
),
885 cpp_token_as_text (pfile
, &op
));
891 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
892 cpp_error (pfile
, CPP_DL_WARNING
,
893 "this use of \"defined\" may not be portable");
895 _cpp_mark_macro_used (node
);
896 if (!(node
->flags
& NODE_USED
))
898 node
->flags
|= NODE_USED
;
899 if (node
->type
== NT_MACRO
)
901 if ((node
->flags
& NODE_BUILTIN
)
902 && pfile
->cb
.user_builtin_macro
)
903 pfile
->cb
.user_builtin_macro (pfile
, node
);
904 if (pfile
->cb
.used_define
)
905 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
909 if (pfile
->cb
.used_undef
)
910 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
914 /* A possible controlling macro of the form #if !defined ().
915 _cpp_parse_expr checks there was no other junk on the line. */
916 pfile
->mi_ind_cmacro
= node
;
919 pfile
->state
.prevent_expansion
--;
921 /* Do not treat conditional macros as being defined. This is due to the
922 powerpc and spu ports using conditional macros for 'vector', 'bool', and
923 'pixel' to act as conditional keywords. This messes up tests like #ifndef
925 result
.unsignedp
= false;
927 result
.overflow
= false;
928 result
.low
= (node
&& node
->type
== NT_MACRO
929 && (node
->flags
& NODE_CONDITIONAL
) == 0);
933 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
934 number or character constant, or the result of the "defined" or "#"
937 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
938 source_location virtual_location
)
944 result
.unsignedp
= false;
945 result
.overflow
= false;
950 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
951 if (temp
& CPP_N_USERDEF
)
952 cpp_error (pfile
, CPP_DL_ERROR
,
953 "user-defined literal in preprocessor expression");
954 switch (temp
& CPP_N_CATEGORY
)
957 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
958 "floating constant in preprocessor expression");
961 if (!(temp
& CPP_N_IMAGINARY
))
962 return cpp_interpret_integer (pfile
, token
, temp
);
963 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
964 "imaginary number in preprocessor expression");
968 /* Error already issued. */
971 result
.high
= result
.low
= 0;
979 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
984 /* Sign-extend the result if necessary. */
985 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
987 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
988 result
.low
|= ~(~(cpp_num_part
) 0
989 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
990 result
.high
= ~(cpp_num_part
) 0;
991 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
997 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
998 return parse_defined (pfile
);
999 else if (CPP_OPTION (pfile
, cplusplus
)
1000 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1001 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1004 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1010 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1011 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1012 "\"%s\" is not defined",
1013 NODE_NAME (token
->val
.node
.node
));
1018 if (!pfile
->state
.skipping
)
1020 /* A pedantic warning takes precedence over a deprecated
1022 if (CPP_PEDANTIC (pfile
))
1023 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1024 virtual_location
, 0,
1025 "assertions are a GCC extension");
1026 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1027 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1028 "assertions are a deprecated extension");
1030 _cpp_test_assertion (pfile
, &temp
);
1039 result
.unsignedp
= !!unsignedp
;
1043 /* Operator precedence and flags table.
1045 After an operator is returned from the lexer, if it has priority less
1046 than the operator on the top of the stack, we reduce the stack by one
1047 operator and repeat the test. Since equal priorities do not reduce,
1048 this is naturally right-associative.
1050 We handle left-associative operators by decrementing the priority of
1051 just-lexed operators by one, but retaining the priority of operators
1052 already on the stack.
1054 The remaining cases are '(' and ')'. We handle '(' by skipping the
1055 reduction phase completely. ')' is given lower priority than
1056 everything else, including '(', effectively forcing a reduction of the
1057 parenthesized expression. If there is a matching '(', the routine
1058 reduce() exits immediately. If the normal exit route sees a ')', then
1059 there cannot have been a matching '(' and an error message is output.
1061 The parser assumes all shifted operators require a left operand unless
1062 the flag NO_L_OPERAND is set. These semantics are automatic; any
1063 extra semantics need to be handled with operator-specific code. */
1065 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1066 operand changes because of integer promotions. */
1067 #define NO_L_OPERAND (1 << 0)
1068 #define LEFT_ASSOC (1 << 1)
1069 #define CHECK_PROMOTION (1 << 2)
1071 /* Operator to priority map. Must be in the same order as the first
1072 N entries of enum cpp_ttype. */
1073 static const struct cpp_operator
1079 /* EQ */ {0, 0}, /* Shouldn't happen. */
1080 /* NOT */ {16, NO_L_OPERAND
},
1081 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1082 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1083 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1084 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1085 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1086 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1087 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1088 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1089 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1090 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1091 /* RSHIFT */ {13, LEFT_ASSOC
},
1092 /* LSHIFT */ {13, LEFT_ASSOC
},
1094 /* COMPL */ {16, NO_L_OPERAND
},
1095 /* AND_AND */ {6, LEFT_ASSOC
},
1096 /* OR_OR */ {5, LEFT_ASSOC
},
1097 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1098 However, there are some special cases for these in reduce(). */
1100 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1101 /* COMMA */ {4, LEFT_ASSOC
},
1102 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1103 /* CLOSE_PAREN */ {0, 0},
1105 /* EQ_EQ */ {11, LEFT_ASSOC
},
1106 /* NOT_EQ */ {11, LEFT_ASSOC
},
1107 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1108 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1109 /* UPLUS */ {16, NO_L_OPERAND
},
1110 /* UMINUS */ {16, NO_L_OPERAND
}
1113 /* Parse and evaluate a C expression, reading from PFILE.
1114 Returns the truth value of the expression.
1116 The implementation is an operator precedence parser, i.e. a
1117 bottom-up parser, using a stack for not-yet-reduced tokens.
1119 The stack base is op_stack, and the current stack pointer is 'top'.
1120 There is a stack element for each operator (only), and the most
1121 recently pushed operator is 'top->op'. An operand (value) is
1122 stored in the 'value' field of the stack element of the operator
1123 that precedes it. */
1125 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1127 struct op
*top
= pfile
->op_stack
;
1128 unsigned int lex_count
;
1129 bool saw_leading_not
, want_value
= true;
1130 source_location virtual_location
= 0;
1132 pfile
->state
.skip_eval
= 0;
1134 /* Set up detection of #if ! defined(). */
1135 pfile
->mi_ind_cmacro
= 0;
1136 saw_leading_not
= false;
1139 /* Lowest priority operator prevents further reductions. */
1147 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1148 op
.op
= op
.token
->type
;
1149 op
.loc
= virtual_location
;
1153 /* These tokens convert into values. */
1162 SYNTAX_ERROR2_AT (op
.loc
,
1163 "missing binary operator before token \"%s\"",
1164 cpp_token_as_text (pfile
, op
.token
));
1166 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1170 saw_leading_not
= lex_count
== 1;
1182 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1183 SYNTAX_ERROR2_AT (op
.loc
,
1184 "token \"%s\" is not valid in preprocessor expressions",
1185 cpp_token_as_text (pfile
, op
.token
));
1189 /* Check we have a value or operator as appropriate. */
1190 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1193 SYNTAX_ERROR2_AT (op
.loc
,
1194 "missing binary operator before token \"%s\"",
1195 cpp_token_as_text (pfile
, op
.token
));
1197 else if (want_value
)
1199 /* We want a number (or expression) and haven't got one.
1200 Try to emit a specific diagnostic. */
1201 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1202 SYNTAX_ERROR_AT (op
.loc
,
1203 "missing expression between '(' and ')'");
1205 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1206 SYNTAX_ERROR2_AT (op
.loc
,
1207 "%s with no expression", is_if
? "#if" : "#elif");
1209 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1210 SYNTAX_ERROR2_AT (op
.loc
,
1211 "operator '%s' has no right operand",
1212 cpp_token_as_text (pfile
, top
->token
));
1213 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1214 /* Complain about missing paren during reduction. */;
1216 SYNTAX_ERROR2_AT (op
.loc
,
1217 "operator '%s' has no left operand",
1218 cpp_token_as_text (pfile
, op
.token
));
1221 top
= reduce (pfile
, top
, op
.op
);
1225 if (op
.op
== CPP_EOF
)
1230 case CPP_CLOSE_PAREN
:
1233 if (!num_zerop (top
->value
))
1234 pfile
->state
.skip_eval
++;
1238 if (num_zerop (top
->value
))
1239 pfile
->state
.skip_eval
++;
1242 if (top
->op
!= CPP_QUERY
)
1243 SYNTAX_ERROR_AT (op
.loc
,
1244 " ':' without preceding '?'");
1245 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1246 pfile
->state
.skip_eval
++;
1248 pfile
->state
.skip_eval
--;
1255 /* Check for and handle stack overflow. */
1256 if (++top
== pfile
->op_limit
)
1257 top
= _cpp_expand_op_stack (pfile
);
1260 top
->token
= op
.token
;
1264 /* The controlling macro expression is only valid if we called lex 3
1265 times: <!> <defined expression> and <EOF>. push_conditional ()
1266 checks that we are at top-of-file. */
1267 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1268 pfile
->mi_ind_cmacro
= 0;
1270 if (top
!= pfile
->op_stack
)
1272 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1273 "unbalanced stack in %s",
1274 is_if
? "#if" : "#elif");
1276 return false; /* Return false on syntax error. */
1279 return !num_zerop (top
->value
);
1282 /* Reduce the operator / value stack if possible, in preparation for
1283 pushing operator OP. Returns NULL on error, otherwise the top of
1286 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1290 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1293 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1297 if (op
== CPP_OPEN_PAREN
)
1300 /* Decrement the priority of left-associative operators to force a
1301 reduction with operators of otherwise equal priority. */
1302 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1303 while (prio
< optab
[top
->op
].prio
)
1305 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1306 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1307 check_promotion (pfile
, top
);
1315 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1316 top
[-1].loc
= top
->loc
;
1324 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1325 top
->value
, top
->op
);
1326 top
[-1].loc
= top
->loc
;
1331 case CPP_GREATER_EQ
:
1334 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1335 top
[-1].loc
= top
->loc
;
1341 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1342 top
[-1].loc
= top
->loc
;
1349 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1350 top
[-1].loc
= top
->loc
;
1354 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1355 top
[-1].loc
= top
->loc
;
1360 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1361 top
->value
, top
->op
, top
->loc
);
1362 top
[-1].loc
= top
->loc
;
1367 if (!num_zerop (top
->value
))
1368 pfile
->state
.skip_eval
--;
1369 top
->value
.low
= (!num_zerop (top
->value
)
1370 || !num_zerop (top
[1].value
));
1371 top
->value
.high
= 0;
1372 top
->value
.unsignedp
= false;
1373 top
->value
.overflow
= false;
1374 top
->loc
= top
[1].loc
;
1379 if (num_zerop (top
->value
))
1380 pfile
->state
.skip_eval
--;
1381 top
->value
.low
= (!num_zerop (top
->value
)
1382 && !num_zerop (top
[1].value
));
1383 top
->value
.high
= 0;
1384 top
->value
.unsignedp
= false;
1385 top
->value
.overflow
= false;
1386 top
->loc
= top
[1].loc
;
1389 case CPP_OPEN_PAREN
:
1390 if (op
!= CPP_CLOSE_PAREN
)
1392 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1393 top
->token
->src_loc
,
1394 0, "missing ')' in expression");
1398 top
->value
= top
[1].value
;
1399 top
->loc
= top
[1].loc
;
1404 if (!num_zerop (top
->value
))
1406 pfile
->state
.skip_eval
--;
1407 top
->value
= top
[1].value
;
1408 top
->loc
= top
[1].loc
;
1412 top
->value
= top
[2].value
;
1413 top
->loc
= top
[2].loc
;
1415 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1416 || top
[2].value
.unsignedp
);
1420 /* COMMA and COLON should not reduce a QUERY operator. */
1421 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1423 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1431 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1432 cpp_error (pfile
, CPP_DL_PEDWARN
,
1433 "integer overflow in preprocessor expression");
1436 if (op
== CPP_CLOSE_PAREN
)
1438 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1445 /* Returns the position of the old top of stack after expansion. */
1447 _cpp_expand_op_stack (cpp_reader
*pfile
)
1449 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1450 size_t new_size
= old_size
* 2 + 20;
1452 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1453 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1455 return pfile
->op_stack
+ old_size
;
1458 /* Emits a warning if the effective sign of either operand of OP
1459 changes because of integer promotions. */
1461 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1463 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1466 if (op
->value
.unsignedp
)
1468 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1469 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1470 "the left operand of \"%s\" changes sign when promoted",
1471 cpp_token_as_text (pfile
, op
->token
));
1473 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1474 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1475 "the right operand of \"%s\" changes sign when promoted",
1476 cpp_token_as_text (pfile
, op
->token
));
1479 /* Clears the unused high order bits of the number pointed to by PNUM. */
1481 num_trim (cpp_num num
, size_t precision
)
1483 if (precision
> PART_PRECISION
)
1485 precision
-= PART_PRECISION
;
1486 if (precision
< PART_PRECISION
)
1487 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1491 if (precision
< PART_PRECISION
)
1492 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1499 /* True iff A (presumed signed) >= 0. */
1501 num_positive (cpp_num num
, size_t precision
)
1503 if (precision
> PART_PRECISION
)
1505 precision
-= PART_PRECISION
;
1506 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1509 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1512 /* Sign extend a number, with PRECISION significant bits and all
1513 others assumed clear, to fill out a cpp_num structure. */
1515 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1519 if (precision
> PART_PRECISION
)
1521 precision
-= PART_PRECISION
;
1522 if (precision
< PART_PRECISION
1523 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1524 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1526 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1528 if (precision
< PART_PRECISION
)
1529 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1530 num
.high
= ~(cpp_num_part
) 0;
1537 /* Returns the negative of NUM. */
1539 num_negate (cpp_num num
, size_t precision
)
1544 num
.high
= ~num
.high
;
1548 num
= num_trim (num
, precision
);
1549 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1554 /* Returns true if A >= B. */
1556 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1560 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1564 /* Both numbers have signed type. If they are of different
1565 sign, the answer is the sign of A. */
1566 unsignedp
= num_positive (pa
, precision
);
1568 if (unsignedp
!= num_positive (pb
, precision
))
1571 /* Otherwise we can do an unsigned comparison. */
1574 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1577 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1579 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1580 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1582 lhs
.overflow
= false;
1583 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1585 /* As excess precision is zeroed, there is no need to num_trim () as
1586 these operations cannot introduce a set bit there. */
1590 lhs
.high
&= rhs
.high
;
1592 else if (op
== CPP_OR
)
1595 lhs
.high
|= rhs
.high
;
1600 lhs
.high
^= rhs
.high
;
1606 /* Returns LHS OP RHS, where OP is an inequality. */
1608 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1611 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1613 if (op
== CPP_GREATER_EQ
)
1615 else if (op
== CPP_LESS
)
1617 else if (op
== CPP_GREATER
)
1618 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1619 else /* CPP_LESS_EQ. */
1620 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1623 lhs
.overflow
= false;
1624 lhs
.unsignedp
= false;
1628 /* Returns LHS OP RHS, where OP is == or !=. */
1630 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1631 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1633 /* Work around a 3.0.4 bug; see PR 6950. */
1634 bool eq
= num_eq (lhs
, rhs
);
1635 if (op
== CPP_NOT_EQ
)
1639 lhs
.overflow
= false;
1640 lhs
.unsignedp
= false;
1644 /* Shift NUM, of width PRECISION, right by N bits. */
1646 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1648 cpp_num_part sign_mask
;
1649 bool x
= num_positive (num
, precision
);
1651 if (num
.unsignedp
|| x
)
1654 sign_mask
= ~(cpp_num_part
) 0;
1657 num
.high
= num
.low
= sign_mask
;
1661 if (precision
< PART_PRECISION
)
1662 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1663 else if (precision
< 2 * PART_PRECISION
)
1664 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1666 if (n
>= PART_PRECISION
)
1668 n
-= PART_PRECISION
;
1670 num
.high
= sign_mask
;
1675 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1676 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1680 num
= num_trim (num
, precision
);
1681 num
.overflow
= false;
1685 /* Shift NUM, of width PRECISION, left by N bits. */
1687 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1691 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1692 num
.high
= num
.low
= 0;
1696 cpp_num orig
, maybe_orig
;
1700 if (m
>= PART_PRECISION
)
1702 m
-= PART_PRECISION
;
1708 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1711 num
= num_trim (num
, precision
);
1714 num
.overflow
= false;
1717 maybe_orig
= num_rshift (num
, precision
, n
);
1718 num
.overflow
= !num_eq (orig
, maybe_orig
);
1725 /* The four unary operators: +, -, ! and ~. */
1727 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1732 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1733 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1734 "traditional C rejects the unary plus operator");
1735 num
.overflow
= false;
1739 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1743 num
.high
= ~num
.high
;
1745 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1746 num
.overflow
= false;
1749 default: /* case CPP_NOT: */
1750 num
.low
= num_zerop (num
);
1752 num
.overflow
= false;
1753 num
.unsignedp
= false;
1760 /* The various binary operators. */
1762 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1765 size_t precision
= CPP_OPTION (pfile
, precision
);
1773 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1775 /* A negative shift is a positive shift the other way. */
1776 if (op
== CPP_LSHIFT
)
1780 rhs
= num_negate (rhs
, precision
);
1783 n
= ~0; /* Maximal. */
1786 if (op
== CPP_LSHIFT
)
1787 lhs
= num_lshift (lhs
, precision
, n
);
1789 lhs
= num_rshift (lhs
, precision
, n
);
1794 rhs
= num_negate (rhs
, precision
);
1796 result
.low
= lhs
.low
+ rhs
.low
;
1797 result
.high
= lhs
.high
+ rhs
.high
;
1798 if (result
.low
< lhs
.low
)
1800 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1801 result
.overflow
= false;
1803 result
= num_trim (result
, precision
);
1804 if (!result
.unsignedp
)
1806 bool lhsp
= num_positive (lhs
, precision
);
1807 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1808 && lhsp
!= num_positive (result
, precision
));
1813 default: /* case CPP_COMMA: */
1814 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1815 || !pfile
->state
.skip_eval
))
1816 cpp_error (pfile
, CPP_DL_PEDWARN
,
1817 "comma operator in operand of #if");
1825 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1828 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1831 cpp_num_part middle
[2], temp
;
1833 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1834 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1836 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1837 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1840 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1841 if (result
.low
< temp
)
1845 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1846 if (result
.low
< temp
)
1849 result
.high
+= HIGH_PART (middle
[0]);
1850 result
.high
+= HIGH_PART (middle
[1]);
1851 result
.unsignedp
= true;
1852 result
.overflow
= false;
1857 /* Multiply two preprocessing numbers. */
1859 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1861 cpp_num result
, temp
;
1862 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1863 bool overflow
, negate
= false;
1864 size_t precision
= CPP_OPTION (pfile
, precision
);
1866 /* Prepare for unsigned multiplication. */
1869 if (!num_positive (lhs
, precision
))
1870 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1871 if (!num_positive (rhs
, precision
))
1872 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1875 overflow
= lhs
.high
&& rhs
.high
;
1876 result
= num_part_mul (lhs
.low
, rhs
.low
);
1878 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1879 result
.high
+= temp
.low
;
1883 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1884 result
.high
+= temp
.low
;
1888 temp
.low
= result
.low
, temp
.high
= result
.high
;
1889 result
= num_trim (result
, precision
);
1890 if (!num_eq (result
, temp
))
1894 result
= num_negate (result
, precision
);
1897 result
.overflow
= false;
1899 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1900 && !num_zerop (result
));
1901 result
.unsignedp
= unsignedp
;
1906 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1907 or the remainder depending upon OP. LOCATION is the source location
1908 of this operator (for diagnostics). */
1911 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1912 source_location location
)
1914 cpp_num result
, sub
;
1916 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1917 bool negate
= false, lhs_neg
= false;
1918 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1920 /* Prepare for unsigned division. */
1923 if (!num_positive (lhs
, precision
))
1924 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1925 if (!num_positive (rhs
, precision
))
1926 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1929 /* Find the high bit. */
1933 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1934 for (; ; i
--, mask
>>= 1)
1935 if (rhs
.high
& mask
)
1940 if (precision
> PART_PRECISION
)
1941 i
= precision
- PART_PRECISION
- 1;
1944 mask
= (cpp_num_part
) 1 << i
;
1945 for (; ; i
--, mask
>>= 1)
1951 if (!pfile
->state
.skip_eval
)
1952 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
1953 "division by zero in #if");
1957 /* First nonzero bit of RHS is bit I. Do naive division by
1958 shifting the RHS fully left, and subtracting from LHS if LHS is
1959 at least as big, and then repeating but with one less shift.
1960 This is not very efficient, but is easy to understand. */
1962 rhs
.unsignedp
= true;
1963 lhs
.unsignedp
= true;
1964 i
= precision
- i
- 1;
1965 sub
= num_lshift (rhs
, precision
, i
);
1967 result
.high
= result
.low
= 0;
1970 if (num_greater_eq (lhs
, sub
, precision
))
1972 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1973 if (i
>= PART_PRECISION
)
1974 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1976 result
.low
|= (cpp_num_part
) 1 << i
;
1980 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1984 /* We divide so that the remainder has the sign of the LHS. */
1987 result
.unsignedp
= unsignedp
;
1988 result
.overflow
= false;
1992 result
= num_negate (result
, precision
);
1993 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1994 && !num_zerop (result
));
2001 lhs
.unsignedp
= unsignedp
;
2002 lhs
.overflow
= false;
2004 lhs
= num_negate (lhs
, precision
);