1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2022 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 location_t 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
*, location_t
);
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 (of CPP_N_* bits) describing the suffix. */
89 interpret_float_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
91 size_t orig_len
= len
;
92 const uchar
*orig_s
= s
;
94 size_t f
, d
, l
, w
, q
, i
, fn
, fnx
, fn_bits
;
97 f
= d
= l
= w
= q
= i
= fn
= fnx
= fn_bits
= 0;
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C2X, are supported:
104 dl, DL - _Decimal128.
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
112 (i) An optional u or U, for unsigned types.
114 (ii) An optional h or H, for short types, or l or L, for long
115 types, or ll or LL, for long long types. Use of ll or LL is a
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
120 Otherwise the suffix is for a binary or standard floating-point
121 type. Such a suffix, or the absence of a suffix, may be preceded
122 or followed by i, I, j or J, to indicate an imaginary number with
123 the corresponding complex type. The following suffixes for
124 binary or standard floating-point types are supported:
126 f, F - float (ISO C and C++).
127 l, L - long double (ISO C and C++).
128 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
129 operation (from TR 24732:2009; the pragma and the suffix
130 are not included in TS 18661-2:2015).
131 w, W - machine-specific type such as __float80 (GNU extension).
132 q, Q - machine-specific type such as __float128 (GNU extension).
133 fN, FN - _FloatN (TS 18661-3:2015).
134 fNx, FNx - _FloatNx (TS 18661-3:2015). */
136 /* Process decimal float suffixes, which are two letters starting
137 with d or D. Order and case are significant. */
138 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
140 bool uppercase
= (*s
== 'D');
143 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
144 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
145 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
146 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
147 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
148 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
150 /* Additional two-character suffixes beginning with D are not
151 for decimal float constants. */
156 if (CPP_OPTION (pfile
, ext_numeric_literals
))
158 /* Recognize a fixed-point suffix. */
162 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
163 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
175 if (*s
== 'u' || *s
== 'U')
177 flags
|= CPP_N_UNSIGNED
;
188 return flags
|= CPP_N_SMALL
;
192 return flags
|= CPP_N_MEDIUM
;
193 if (len
== 2 && s
[1] == 'l')
194 return flags
|= CPP_N_LARGE
;
198 return flags
|= CPP_N_MEDIUM
;
199 if (len
== 2 && s
[1] == 'L')
200 return flags
|= CPP_N_LARGE
;
205 /* Anything left at this point is invalid. */
210 /* In any remaining valid suffix, the case and order don't matter. */
226 && fn_bits
< CPP_FLOATN_MAX
)
228 fn_bits
= fn_bits
* 10 + (s
[1] - '0');
232 if (len
> 0 && s
[1] == 'x')
242 case 'd': case 'D': d
++; break;
243 case 'l': case 'L': l
++; break;
244 case 'w': case 'W': w
++; break;
245 case 'q': case 'Q': q
++; break;
247 case 'j': case 'J': i
++; break;
254 /* Reject any case of multiple suffixes specifying types, multiple
255 suffixes specifying an imaginary constant, _FloatN or _FloatNx
256 suffixes for invalid values of N, and _FloatN suffixes for values
257 of N larger than can be represented in the return value. The
258 caller is responsible for rejecting _FloatN suffixes where
259 _FloatN is not supported on the chosen target. */
260 if (f
+ d
+ l
+ w
+ q
+ fn
+ fnx
> 1 || i
> 1)
262 if (fn_bits
> CPP_FLOATN_MAX
)
264 if (fnx
&& fn_bits
!= 32 && fn_bits
!= 64 && fn_bits
!= 128)
266 if (fn
&& fn_bits
!= 16 && fn_bits
% 32 != 0)
268 if (fn
&& fn_bits
== 96)
273 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
276 /* In C++14 and up these suffixes are in the standard library, so treat
277 them as user-defined literals. */
278 if (CPP_OPTION (pfile
, cplusplus
)
279 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
283 && (orig_s
[1] == 'f' || orig_s
[1] == 'l'))))
287 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
290 return ((i
? CPP_N_IMAGINARY
: 0)
296 fn
? CPP_N_FLOATN
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
297 fnx
? CPP_N_FLOATNX
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
301 /* Return the classification flags for a float suffix. */
303 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
305 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
308 /* Subroutine of cpp_classify_number. S points to an integer suffix
309 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
310 flag vector describing the suffix. */
312 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
314 size_t orig_len
= len
;
322 case 'z': case 'Z': z
++; break;
323 case 'u': case 'U': u
++; break;
325 case 'j': case 'J': i
++; break;
326 case 'l': case 'L': l
++;
327 /* If there are two Ls, they must be adjacent and the same case. */
328 if (l
== 2 && s
[len
] != s
[len
+ 1])
335 if (l
> 2 || u
> 1 || i
> 1 || z
> 1)
342 if (!CPP_OPTION (pfile
, cplusplus
))
348 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
351 /* In C++14 and up these suffixes are in the standard library, so treat
352 them as user-defined literals. */
353 if (CPP_OPTION (pfile
, cplusplus
)
354 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
356 && (orig_len
== 1 || (orig_len
== 2 && s
[1] == 'l')))
360 return ((i
? CPP_N_IMAGINARY
: 0)
361 | (u
? CPP_N_UNSIGNED
: 0)
362 | ((l
== 0) ? CPP_N_SMALL
363 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
)
364 | (z
? CPP_N_SIZE_T
: 0));
367 /* Return the classification flags for an int suffix. */
369 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
371 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
374 /* Return the string type corresponding to the the input user-defined string
375 literal type. If the input type is not a user-defined string literal
376 type return the input type. */
378 cpp_userdef_string_remove_type (enum cpp_ttype type
)
380 if (type
== CPP_STRING_USERDEF
)
382 else if (type
== CPP_WSTRING_USERDEF
)
384 else if (type
== CPP_STRING16_USERDEF
)
386 else if (type
== CPP_STRING32_USERDEF
)
388 else if (type
== CPP_UTF8STRING_USERDEF
)
389 return CPP_UTF8STRING
;
394 /* Return the user-defined string literal type corresponding to the input
395 string type. If the input type is not a string type return the input
398 cpp_userdef_string_add_type (enum cpp_ttype type
)
400 if (type
== CPP_STRING
)
401 return CPP_STRING_USERDEF
;
402 else if (type
== CPP_WSTRING
)
403 return CPP_WSTRING_USERDEF
;
404 else if (type
== CPP_STRING16
)
405 return CPP_STRING16_USERDEF
;
406 else if (type
== CPP_STRING32
)
407 return CPP_STRING32_USERDEF
;
408 else if (type
== CPP_UTF8STRING
)
409 return CPP_UTF8STRING_USERDEF
;
414 /* Return the char type corresponding to the the input user-defined char
415 literal type. If the input type is not a user-defined char literal
416 type return the input type. */
418 cpp_userdef_char_remove_type (enum cpp_ttype type
)
420 if (type
== CPP_CHAR_USERDEF
)
422 else if (type
== CPP_WCHAR_USERDEF
)
424 else if (type
== CPP_CHAR16_USERDEF
)
426 else if (type
== CPP_CHAR32_USERDEF
)
428 else if (type
== CPP_UTF8CHAR_USERDEF
)
434 /* Return the user-defined char literal type corresponding to the input
435 char type. If the input type is not a char type return the input
438 cpp_userdef_char_add_type (enum cpp_ttype type
)
440 if (type
== CPP_CHAR
)
441 return CPP_CHAR_USERDEF
;
442 else if (type
== CPP_WCHAR
)
443 return CPP_WCHAR_USERDEF
;
444 else if (type
== CPP_CHAR16
)
445 return CPP_CHAR16_USERDEF
;
446 else if (type
== CPP_CHAR32
)
447 return CPP_CHAR32_USERDEF
;
448 else if (type
== CPP_UTF8CHAR
)
449 return CPP_UTF8CHAR_USERDEF
;
454 /* Return true if the token type is a user-defined string literal. */
456 cpp_userdef_string_p (enum cpp_ttype type
)
458 if (type
== CPP_STRING_USERDEF
459 || type
== CPP_WSTRING_USERDEF
460 || type
== CPP_STRING16_USERDEF
461 || type
== CPP_STRING32_USERDEF
462 || type
== CPP_UTF8STRING_USERDEF
)
468 /* Return true if the token type is a user-defined char literal. */
470 cpp_userdef_char_p (enum cpp_ttype type
)
472 if (type
== CPP_CHAR_USERDEF
473 || type
== CPP_WCHAR_USERDEF
474 || type
== CPP_CHAR16_USERDEF
475 || type
== CPP_CHAR32_USERDEF
476 || type
== CPP_UTF8CHAR_USERDEF
)
482 /* Extract the suffix from a user-defined literal string or char. */
484 cpp_get_userdef_suffix (const cpp_token
*tok
)
486 unsigned int len
= tok
->val
.str
.len
;
487 const char *text
= (const char *)tok
->val
.str
.text
;
490 for (i
= 0; i
< len
; ++i
)
491 if (text
[i
] == '\'' || text
[i
] == '"')
496 for (i
= len
; i
> 0; --i
)
497 if (text
[i
- 1] == delim
)
502 /* Categorize numeric constants according to their field (integer,
503 floating point, or invalid), radix (decimal, octal, hexadecimal),
506 TOKEN is the token that represents the numeric constant to
509 In C++0X if UD_SUFFIX is non null it will be assigned
510 any unrecognized suffix for a user-defined literal.
512 VIRTUAL_LOCATION is the virtual location for TOKEN. */
514 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
515 const char **ud_suffix
, location_t virtual_location
)
517 const uchar
*str
= token
->val
.str
.text
;
519 unsigned int max_digit
, result
, radix
;
520 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
527 /* If the lexer has done its job, length one can only be a single
528 digit. Fast-path this very common case. */
529 if (token
->val
.str
.len
== 1)
530 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
532 limit
= str
+ token
->val
.str
.len
;
533 float_flag
= NOT_FLOAT
;
537 seen_digit_sep
= false;
539 /* First, interpret the radix. */
545 /* Require at least one hex digit to classify it as hex. */
546 if (*str
== 'x' || *str
== 'X')
548 if (str
[1] == '.' || ISXDIGIT (str
[1]))
553 else if (DIGIT_SEP (str
[1]))
554 SYNTAX_ERROR_AT (virtual_location
,
555 "digit separator after base indicator");
557 else if (*str
== 'b' || *str
== 'B')
559 if (str
[1] == '0' || str
[1] == '1')
564 else if (DIGIT_SEP (str
[1]))
565 SYNTAX_ERROR_AT (virtual_location
,
566 "digit separator after base indicator");
570 /* Now scan for a well-formed integer or float. */
573 unsigned int c
= *str
++;
575 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
577 seen_digit_sep
= false;
583 else if (DIGIT_SEP (c
))
584 seen_digit_sep
= true;
587 if (seen_digit_sep
|| DIGIT_SEP (*str
))
588 SYNTAX_ERROR_AT (virtual_location
,
589 "digit separator adjacent to decimal point");
590 seen_digit_sep
= false;
591 if (float_flag
== NOT_FLOAT
)
592 float_flag
= AFTER_POINT
;
594 SYNTAX_ERROR_AT (virtual_location
,
595 "too many decimal points in number");
597 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
598 || (radix
== 16 && (c
== 'p' || c
== 'P')))
600 if (seen_digit_sep
|| DIGIT_SEP (*str
))
601 SYNTAX_ERROR_AT (virtual_location
,
602 "digit separator adjacent to exponent");
603 float_flag
= AFTER_EXPON
;
608 /* Start of suffix. */
614 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
615 SYNTAX_ERROR_AT (virtual_location
,
616 "digit separator outside digit sequence");
618 /* The suffix may be for decimal fixed-point constants without exponent. */
619 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
621 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
622 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
624 result
|= CPP_N_FLOATING
;
625 /* We need to restore the radix to 10, if the radix is 8. */
629 if (CPP_PEDANTIC (pfile
))
630 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
631 "fixed-point constants are a GCC extension");
638 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
641 if (max_digit
>= radix
)
644 SYNTAX_ERROR2_AT (virtual_location
,
645 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
647 SYNTAX_ERROR2_AT (virtual_location
,
648 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
651 if (float_flag
!= NOT_FLOAT
)
655 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
656 "invalid prefix \"0b\" for floating constant");
657 return CPP_N_INVALID
;
660 if (radix
== 16 && !seen_digit
)
661 SYNTAX_ERROR_AT (virtual_location
,
662 "no digits in hexadecimal floating constant");
664 if (radix
== 16 && CPP_PEDANTIC (pfile
)
665 && !CPP_OPTION (pfile
, extended_numbers
))
667 if (CPP_OPTION (pfile
, cplusplus
))
668 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
669 "use of C++17 hexadecimal floating constant");
671 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
672 "use of C99 hexadecimal floating constant");
675 if (float_flag
== AFTER_EXPON
)
677 if (*str
== '+' || *str
== '-')
680 /* Exponent is decimal, even if string is a hex float. */
683 if (DIGIT_SEP (*str
))
684 SYNTAX_ERROR_AT (virtual_location
,
685 "digit separator adjacent to exponent");
687 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
691 seen_digit_sep
= DIGIT_SEP (*str
);
694 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
696 else if (radix
== 16)
697 SYNTAX_ERROR_AT (virtual_location
,
698 "hexadecimal floating constants require an exponent");
701 SYNTAX_ERROR_AT (virtual_location
,
702 "digit separator outside digit sequence");
704 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
707 if (CPP_OPTION (pfile
, user_literals
))
710 *ud_suffix
= (const char *) str
;
711 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
715 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
716 "invalid suffix \"%.*s\" on floating constant",
717 (int) (limit
- str
), str
);
718 return CPP_N_INVALID
;
722 /* Traditional C didn't accept any floating suffixes. */
724 && CPP_WTRADITIONAL (pfile
)
725 && ! cpp_sys_macro_p (pfile
))
726 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
727 "traditional C rejects the \"%.*s\" suffix",
728 (int) (limit
- str
), str
);
730 /* A suffix for double is a GCC extension via decimal float support.
731 If the suffix also specifies an imaginary value we'll catch that
733 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
734 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
735 "suffix for double constant is a GCC extension");
737 /* Radix must be 10 for decimal floats. */
738 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
740 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
741 "invalid suffix \"%.*s\" with hexadecimal floating constant",
742 (int) (limit
- str
), str
);
743 return CPP_N_INVALID
;
746 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
747 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
748 "fixed-point constants are a GCC extension");
750 if (result
& CPP_N_DFLOAT
)
752 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, dfp_constants
))
753 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
754 "decimal float constants are a C2X feature");
755 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
756 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
758 "decimal float constants are a C2X feature");
761 result
|= CPP_N_FLOATING
;
765 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
768 if (CPP_OPTION (pfile
, user_literals
))
771 *ud_suffix
= (const char *) str
;
772 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
776 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
777 "invalid suffix \"%.*s\" on integer constant",
778 (int) (limit
- str
), str
);
779 return CPP_N_INVALID
;
783 /* Traditional C only accepted the 'L' suffix.
784 Suppress warning about 'LL' with -Wno-long-long. */
785 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
787 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
788 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
789 && CPP_OPTION (pfile
, cpp_warn_long_long
);
792 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
794 "traditional C rejects the \"%.*s\" suffix",
795 (int) (limit
- str
), str
);
798 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
799 && CPP_OPTION (pfile
, cpp_warn_long_long
))
801 const char *message
= CPP_OPTION (pfile
, cplusplus
)
802 ? N_("use of C++11 long long integer constant")
803 : N_("use of C99 long long integer constant");
805 if (CPP_OPTION (pfile
, c99
))
806 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
809 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
810 virtual_location
, 0, message
);
813 if ((result
& CPP_N_SIZE_T
) == CPP_N_SIZE_T
814 && !CPP_OPTION (pfile
, size_t_literals
))
816 const char *message
= (result
& CPP_N_UNSIGNED
) == CPP_N_UNSIGNED
817 ? N_("use of C++23 %<size_t%> integer constant")
818 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant");
819 cpp_warning_with_line (pfile
, CPP_W_SIZE_T_LITERALS
,
820 virtual_location
, 0, message
);
823 result
|= CPP_N_INTEGER
;
827 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
828 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
829 "imaginary constants are a GCC extension");
832 if (!CPP_OPTION (pfile
, binary_constants
)
833 && CPP_PEDANTIC (pfile
))
834 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
835 CPP_OPTION (pfile
, cplusplus
)
836 ? N_("binary constants are a C++14 feature "
838 : N_("binary constants are a C2X feature "
839 "or GCC extension"));
840 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
841 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
843 "binary constants are a C2X feature");
847 result
|= CPP_N_DECIMAL
;
848 else if (radix
== 16)
851 result
|= CPP_N_BINARY
;
853 result
|= CPP_N_OCTAL
;
858 return CPP_N_INVALID
;
861 /* cpp_interpret_integer converts an integer constant into a cpp_num,
862 of precision options->precision.
864 We do not provide any interface for decimal->float conversion,
865 because the preprocessor doesn't need it and we don't want to
866 drag in GCC's floating point emulator. */
868 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
871 const uchar
*p
, *end
;
876 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
877 result
.overflow
= false;
879 p
= token
->val
.str
.text
;
880 end
= p
+ token
->val
.str
.len
;
882 /* Common case of a single digit. */
883 if (token
->val
.str
.len
== 1)
884 result
.low
= p
[0] - '0';
888 size_t precision
= CPP_OPTION (pfile
, precision
);
889 unsigned int base
= 10, c
= 0;
890 bool overflow
= false;
892 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
897 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
902 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
908 /* We can add a digit to numbers strictly less than this without
909 needing the precision and slowness of double integers. */
910 max
= ~(cpp_num_part
) 0;
911 if (precision
< PART_PRECISION
)
912 max
>>= PART_PRECISION
- precision
;
913 max
= (max
- base
+ 1) / base
+ 1;
919 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
921 else if (DIGIT_SEP (c
))
926 /* Strict inequality for when max is set to zero. */
927 if (result
.low
< max
)
928 result
.low
= result
.low
* base
+ c
;
931 result
= append_digit (result
, c
, base
, precision
);
932 overflow
|= result
.overflow
;
937 if (overflow
&& !(type
& CPP_N_USERDEF
))
938 cpp_error (pfile
, CPP_DL_PEDWARN
,
939 "integer constant is too large for its type");
940 /* If too big to be signed, consider it unsigned. Only warn for
941 decimal numbers. Traditional numbers were always signed (but
942 we still honor an explicit U suffix); but we only have
943 traditional semantics in directives. */
944 else if (!result
.unsignedp
945 && !(CPP_OPTION (pfile
, traditional
)
946 && pfile
->state
.in_directive
)
947 && !num_positive (result
, precision
))
949 /* This is for constants within the range of uintmax_t but
950 not that of intmax_t. For such decimal constants, a
951 diagnostic is required for C99 as the selected type must
952 be signed and not having a type is a constraint violation
953 (DR#298, TC3), so this must be a pedwarn. For C90,
954 unsigned long is specified to be used for a constant that
955 does not fit in signed long; if uintmax_t has the same
956 range as unsigned long this means only a warning is
957 appropriate here. C90 permits the preprocessor to use a
958 wider range than unsigned long in the compiler, so if
959 uintmax_t is wider than unsigned long no diagnostic is
960 required for such constants in preprocessor #if
961 expressions and the compiler will pedwarn for such
962 constants outside the range of unsigned long that reach
963 the compiler so a diagnostic is not required there
964 either; thus, pedwarn for C99 but use a plain warning for
967 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
970 "integer constant is so large that it is unsigned");
971 result
.unsignedp
= true;
978 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
980 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
985 cpp_num_part add_high
, add_low
;
987 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
988 need to worry about add_high overflowing. */
1002 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
1003 result
.high
= num
.high
<< shift
;
1004 result
.low
= num
.low
<< shift
;
1005 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
1006 result
.unsignedp
= num
.unsignedp
;
1010 add_low
= num
.low
<< 1;
1011 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
1014 add_high
= add_low
= 0;
1016 if (add_low
+ digit
< add_low
)
1020 if (result
.low
+ add_low
< result
.low
)
1022 if (result
.high
+ add_high
< result
.high
)
1025 result
.low
+= add_low
;
1026 result
.high
+= add_high
;
1027 result
.overflow
= overflow
;
1029 /* The above code catches overflow of a cpp_num type. This catches
1030 overflow of the (possibly shorter) target precision. */
1031 num
.low
= result
.low
;
1032 num
.high
= result
.high
;
1033 result
= num_trim (result
, precision
);
1034 if (!num_eq (result
, num
))
1035 result
.overflow
= true;
1040 /* Handle meeting "defined" in a preprocessor expression. */
1042 parse_defined (cpp_reader
*pfile
)
1046 cpp_hashnode
*node
= 0;
1047 const cpp_token
*token
;
1048 cpp_context
*initial_context
= pfile
->context
;
1050 /* Don't expand macros. */
1051 pfile
->state
.prevent_expansion
++;
1053 token
= cpp_get_token (pfile
);
1054 if (token
->type
== CPP_OPEN_PAREN
)
1057 token
= cpp_get_token (pfile
);
1060 if (token
->type
== CPP_NAME
)
1062 node
= token
->val
.node
.node
;
1063 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
1065 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
1071 cpp_error (pfile
, CPP_DL_ERROR
,
1072 "operator \"defined\" requires an identifier");
1073 if (token
->flags
& NAMED_OP
)
1078 op
.type
= token
->type
;
1079 cpp_error (pfile
, CPP_DL_ERROR
,
1080 "(\"%s\" is an alternative token for \"%s\" in C++)",
1081 cpp_token_as_text (pfile
, token
),
1082 cpp_token_as_text (pfile
, &op
));
1086 bool is_defined
= false;
1089 if ((pfile
->context
!= initial_context
1090 || initial_context
!= &pfile
->base_context
)
1091 && CPP_OPTION (pfile
, warn_expansion_to_defined
))
1092 cpp_pedwarning (pfile
, CPP_W_EXPANSION_TO_DEFINED
,
1093 "this use of \"defined\" may not be portable");
1094 is_defined
= _cpp_defined_macro_p (node
);
1095 if (!_cpp_maybe_notify_macro_use (pfile
, node
, token
->src_loc
))
1096 /* It wasn't a macro after all. */
1098 _cpp_mark_macro_used (node
);
1100 /* A possible controlling macro of the form #if !defined ().
1101 _cpp_parse_expr checks there was no other junk on the line. */
1102 pfile
->mi_ind_cmacro
= node
;
1105 pfile
->state
.prevent_expansion
--;
1107 /* Do not treat conditional macros as being defined. This is due to the
1108 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1109 to act as conditional keywords. This messes up tests like #ifndef
1111 result
.unsignedp
= false;
1113 result
.overflow
= false;
1114 result
.low
= is_defined
;
1118 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1119 number or character constant, or the result of the "defined" or "#"
1122 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
1123 location_t virtual_location
)
1129 result
.unsignedp
= false;
1130 result
.overflow
= false;
1132 switch (token
->type
)
1135 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1136 if (temp
& CPP_N_USERDEF
)
1137 cpp_error (pfile
, CPP_DL_ERROR
,
1138 "user-defined literal in preprocessor expression");
1139 switch (temp
& CPP_N_CATEGORY
)
1141 case CPP_N_FLOATING
:
1142 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1143 "floating constant in preprocessor expression");
1146 if (!(temp
& CPP_N_IMAGINARY
))
1147 return cpp_interpret_integer (pfile
, token
, temp
);
1148 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1149 "imaginary number in preprocessor expression");
1153 /* Error already issued. */
1156 result
.high
= result
.low
= 0;
1165 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1170 /* Sign-extend the result if necessary. */
1171 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1173 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1174 result
.low
|= ~(~(cpp_num_part
) 0
1175 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1176 result
.high
= ~(cpp_num_part
) 0;
1177 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1183 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1184 return parse_defined (pfile
);
1185 else if (CPP_OPTION (pfile
, true_false
)
1186 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1187 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1190 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1196 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1197 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1198 "\"%s\" is not defined, evaluates to 0",
1199 NODE_NAME (token
->val
.node
.node
));
1204 if (!pfile
->state
.skipping
)
1206 /* A pedantic warning takes precedence over a deprecated
1208 if (CPP_PEDANTIC (pfile
))
1209 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1210 virtual_location
, 0,
1211 "assertions are a GCC extension");
1212 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1213 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1214 "assertions are a deprecated extension");
1216 _cpp_test_assertion (pfile
, &temp
);
1225 result
.unsignedp
= !!unsignedp
;
1229 /* Operator precedence and flags table.
1231 After an operator is returned from the lexer, if it has priority less
1232 than the operator on the top of the stack, we reduce the stack by one
1233 operator and repeat the test. Since equal priorities do not reduce,
1234 this is naturally right-associative.
1236 We handle left-associative operators by decrementing the priority of
1237 just-lexed operators by one, but retaining the priority of operators
1238 already on the stack.
1240 The remaining cases are '(' and ')'. We handle '(' by skipping the
1241 reduction phase completely. ')' is given lower priority than
1242 everything else, including '(', effectively forcing a reduction of the
1243 parenthesized expression. If there is a matching '(', the routine
1244 reduce() exits immediately. If the normal exit route sees a ')', then
1245 there cannot have been a matching '(' and an error message is output.
1247 The parser assumes all shifted operators require a left operand unless
1248 the flag NO_L_OPERAND is set. These semantics are automatic; any
1249 extra semantics need to be handled with operator-specific code. */
1251 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1252 operand changes because of integer promotions. */
1253 #define NO_L_OPERAND (1 << 0)
1254 #define LEFT_ASSOC (1 << 1)
1255 #define CHECK_PROMOTION (1 << 2)
1257 /* Operator to priority map. Must be in the same order as the first
1258 N entries of enum cpp_ttype. */
1259 static const struct cpp_operator
1265 /* EQ */ {0, 0}, /* Shouldn't happen. */
1266 /* NOT */ {16, NO_L_OPERAND
},
1267 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1268 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1269 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1270 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1271 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1272 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1273 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1274 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1275 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1276 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1277 /* RSHIFT */ {13, LEFT_ASSOC
},
1278 /* LSHIFT */ {13, LEFT_ASSOC
},
1280 /* COMPL */ {16, NO_L_OPERAND
},
1281 /* AND_AND */ {6, LEFT_ASSOC
},
1282 /* OR_OR */ {5, LEFT_ASSOC
},
1283 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1284 However, there are some special cases for these in reduce(). */
1286 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1287 /* COMMA */ {4, LEFT_ASSOC
},
1288 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1289 /* CLOSE_PAREN */ {0, 0},
1291 /* EQ_EQ */ {11, LEFT_ASSOC
},
1292 /* NOT_EQ */ {11, LEFT_ASSOC
},
1293 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1294 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1295 /* UPLUS */ {16, NO_L_OPERAND
},
1296 /* UMINUS */ {16, NO_L_OPERAND
}
1299 /* Parse and evaluate a C expression, reading from PFILE.
1300 Returns the truth value of the expression.
1302 The implementation is an operator precedence parser, i.e. a
1303 bottom-up parser, using a stack for not-yet-reduced tokens.
1305 The stack base is op_stack, and the current stack pointer is 'top'.
1306 There is a stack element for each operator (only), and the most
1307 recently pushed operator is 'top->op'. An operand (value) is
1308 stored in the 'value' field of the stack element of the operator
1309 that precedes it. */
1311 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1313 struct op
*top
= pfile
->op_stack
;
1314 unsigned int lex_count
;
1315 bool saw_leading_not
, want_value
= true;
1316 location_t virtual_location
= 0;
1318 pfile
->state
.skip_eval
= 0;
1320 /* Set up detection of #if ! defined(). */
1321 pfile
->mi_ind_cmacro
= 0;
1322 saw_leading_not
= false;
1325 /* Lowest priority operator prevents further reductions. */
1333 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1334 op
.op
= op
.token
->type
;
1335 op
.loc
= virtual_location
;
1339 /* These tokens convert into values. */
1349 SYNTAX_ERROR2_AT (op
.loc
,
1350 "missing binary operator before token \"%s\"",
1351 cpp_token_as_text (pfile
, op
.token
));
1353 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1357 saw_leading_not
= lex_count
== 1;
1373 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1374 SYNTAX_ERROR2_AT (op
.loc
,
1375 "token \"%s\" is not valid in preprocessor expressions",
1376 cpp_token_as_text (pfile
, op
.token
));
1380 /* Check we have a value or operator as appropriate. */
1381 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1384 SYNTAX_ERROR2_AT (op
.loc
,
1385 "missing binary operator before token \"%s\"",
1386 cpp_token_as_text (pfile
, op
.token
));
1388 else if (want_value
)
1390 /* We want a number (or expression) and haven't got one.
1391 Try to emit a specific diagnostic. */
1392 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1393 SYNTAX_ERROR_AT (op
.loc
,
1394 "missing expression between '(' and ')'");
1396 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1397 SYNTAX_ERROR2_AT (op
.loc
,
1398 "%s with no expression", is_if
? "#if" : "#elif");
1400 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1401 SYNTAX_ERROR2_AT (op
.loc
,
1402 "operator '%s' has no right operand",
1403 cpp_token_as_text (pfile
, top
->token
));
1404 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1405 /* Complain about missing paren during reduction. */;
1407 SYNTAX_ERROR2_AT (op
.loc
,
1408 "operator '%s' has no left operand",
1409 cpp_token_as_text (pfile
, op
.token
));
1412 top
= reduce (pfile
, top
, op
.op
);
1416 if (op
.op
== CPP_EOF
)
1421 case CPP_CLOSE_PAREN
:
1424 if (!num_zerop (top
->value
))
1425 pfile
->state
.skip_eval
++;
1429 if (num_zerop (top
->value
))
1430 pfile
->state
.skip_eval
++;
1433 if (top
->op
!= CPP_QUERY
)
1434 SYNTAX_ERROR_AT (op
.loc
,
1435 " ':' without preceding '?'");
1436 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1437 pfile
->state
.skip_eval
++;
1439 pfile
->state
.skip_eval
--;
1446 /* Check for and handle stack overflow. */
1447 if (++top
== pfile
->op_limit
)
1448 top
= _cpp_expand_op_stack (pfile
);
1451 top
->token
= op
.token
;
1455 /* The controlling macro expression is only valid if we called lex 3
1456 times: <!> <defined expression> and <EOF>. push_conditional ()
1457 checks that we are at top-of-file. */
1458 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1459 pfile
->mi_ind_cmacro
= 0;
1461 if (top
!= pfile
->op_stack
)
1463 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1464 "unbalanced stack in %s",
1465 is_if
? "#if" : "#elif");
1467 return false; /* Return false on syntax error. */
1470 return !num_zerop (top
->value
);
1473 /* Reduce the operator / value stack if possible, in preparation for
1474 pushing operator OP. Returns NULL on error, otherwise the top of
1477 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1481 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1484 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1488 if (op
== CPP_OPEN_PAREN
)
1491 /* Decrement the priority of left-associative operators to force a
1492 reduction with operators of otherwise equal priority. */
1493 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1494 while (prio
< optab
[top
->op
].prio
)
1496 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1497 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1498 check_promotion (pfile
, top
);
1506 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1507 top
[-1].loc
= top
->loc
;
1515 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1516 top
->value
, top
->op
);
1517 top
[-1].loc
= top
->loc
;
1522 case CPP_GREATER_EQ
:
1525 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1526 top
[-1].loc
= top
->loc
;
1532 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1533 top
[-1].loc
= top
->loc
;
1540 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1541 top
[-1].loc
= top
->loc
;
1545 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1546 top
[-1].loc
= top
->loc
;
1551 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1552 top
->value
, top
->op
, top
->loc
);
1553 top
[-1].loc
= top
->loc
;
1558 if (!num_zerop (top
->value
))
1559 pfile
->state
.skip_eval
--;
1560 top
->value
.low
= (!num_zerop (top
->value
)
1561 || !num_zerop (top
[1].value
));
1562 top
->value
.high
= 0;
1563 top
->value
.unsignedp
= false;
1564 top
->value
.overflow
= false;
1565 top
->loc
= top
[1].loc
;
1570 if (num_zerop (top
->value
))
1571 pfile
->state
.skip_eval
--;
1572 top
->value
.low
= (!num_zerop (top
->value
)
1573 && !num_zerop (top
[1].value
));
1574 top
->value
.high
= 0;
1575 top
->value
.unsignedp
= false;
1576 top
->value
.overflow
= false;
1577 top
->loc
= top
[1].loc
;
1580 case CPP_OPEN_PAREN
:
1581 if (op
!= CPP_CLOSE_PAREN
)
1583 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1584 top
->token
->src_loc
,
1585 0, "missing ')' in expression");
1589 top
->value
= top
[1].value
;
1590 top
->loc
= top
[1].loc
;
1595 if (!num_zerop (top
->value
))
1597 pfile
->state
.skip_eval
--;
1598 top
->value
= top
[1].value
;
1599 top
->loc
= top
[1].loc
;
1603 top
->value
= top
[2].value
;
1604 top
->loc
= top
[2].loc
;
1606 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1607 || top
[2].value
.unsignedp
);
1611 /* COMMA and COLON should not reduce a QUERY operator. */
1612 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1614 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1622 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1623 cpp_error (pfile
, CPP_DL_PEDWARN
,
1624 "integer overflow in preprocessor expression");
1627 if (op
== CPP_CLOSE_PAREN
)
1629 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1636 /* Returns the position of the old top of stack after expansion. */
1638 _cpp_expand_op_stack (cpp_reader
*pfile
)
1640 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1641 size_t new_size
= old_size
* 2 + 20;
1643 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1644 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1646 return pfile
->op_stack
+ old_size
;
1649 /* Emits a warning if the effective sign of either operand of OP
1650 changes because of integer promotions. */
1652 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1654 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1657 if (op
->value
.unsignedp
)
1659 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1660 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1661 "the left operand of \"%s\" changes sign when promoted",
1662 cpp_token_as_text (pfile
, op
->token
));
1664 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1665 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1666 "the right operand of \"%s\" changes sign when promoted",
1667 cpp_token_as_text (pfile
, op
->token
));
1670 /* Clears the unused high order bits of the number pointed to by PNUM. */
1672 num_trim (cpp_num num
, size_t precision
)
1674 if (precision
> PART_PRECISION
)
1676 precision
-= PART_PRECISION
;
1677 if (precision
< PART_PRECISION
)
1678 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1682 if (precision
< PART_PRECISION
)
1683 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1690 /* True iff A (presumed signed) >= 0. */
1692 num_positive (cpp_num num
, size_t precision
)
1694 if (precision
> PART_PRECISION
)
1696 precision
-= PART_PRECISION
;
1697 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1700 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1703 /* Sign extend a number, with PRECISION significant bits and all
1704 others assumed clear, to fill out a cpp_num structure. */
1706 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1710 if (precision
> PART_PRECISION
)
1712 precision
-= PART_PRECISION
;
1713 if (precision
< PART_PRECISION
1714 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1715 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1717 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1719 if (precision
< PART_PRECISION
)
1720 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1721 num
.high
= ~(cpp_num_part
) 0;
1728 /* Returns the negative of NUM. */
1730 num_negate (cpp_num num
, size_t precision
)
1735 num
.high
= ~num
.high
;
1739 num
= num_trim (num
, precision
);
1740 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1745 /* Returns true if A >= B. */
1747 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1751 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1755 /* Both numbers have signed type. If they are of different
1756 sign, the answer is the sign of A. */
1757 unsignedp
= num_positive (pa
, precision
);
1759 if (unsignedp
!= num_positive (pb
, precision
))
1762 /* Otherwise we can do an unsigned comparison. */
1765 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1768 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1770 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1771 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1773 lhs
.overflow
= false;
1774 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1776 /* As excess precision is zeroed, there is no need to num_trim () as
1777 these operations cannot introduce a set bit there. */
1781 lhs
.high
&= rhs
.high
;
1783 else if (op
== CPP_OR
)
1786 lhs
.high
|= rhs
.high
;
1791 lhs
.high
^= rhs
.high
;
1797 /* Returns LHS OP RHS, where OP is an inequality. */
1799 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1802 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1804 if (op
== CPP_GREATER_EQ
)
1806 else if (op
== CPP_LESS
)
1808 else if (op
== CPP_GREATER
)
1809 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1810 else /* CPP_LESS_EQ. */
1811 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1814 lhs
.overflow
= false;
1815 lhs
.unsignedp
= false;
1819 /* Returns LHS OP RHS, where OP is == or !=. */
1821 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1822 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1824 /* Work around a 3.0.4 bug; see PR 6950. */
1825 bool eq
= num_eq (lhs
, rhs
);
1826 if (op
== CPP_NOT_EQ
)
1830 lhs
.overflow
= false;
1831 lhs
.unsignedp
= false;
1835 /* Shift NUM, of width PRECISION, right by N bits. */
1837 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1839 cpp_num_part sign_mask
;
1840 bool x
= num_positive (num
, precision
);
1842 if (num
.unsignedp
|| x
)
1845 sign_mask
= ~(cpp_num_part
) 0;
1848 num
.high
= num
.low
= sign_mask
;
1852 if (precision
< PART_PRECISION
)
1853 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1854 else if (precision
< 2 * PART_PRECISION
)
1855 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1857 if (n
>= PART_PRECISION
)
1859 n
-= PART_PRECISION
;
1861 num
.high
= sign_mask
;
1866 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1867 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1871 num
= num_trim (num
, precision
);
1872 num
.overflow
= false;
1876 /* Shift NUM, of width PRECISION, left by N bits. */
1878 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1882 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1883 num
.high
= num
.low
= 0;
1887 cpp_num orig
, maybe_orig
;
1891 if (m
>= PART_PRECISION
)
1893 m
-= PART_PRECISION
;
1899 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1902 num
= num_trim (num
, precision
);
1905 num
.overflow
= false;
1908 maybe_orig
= num_rshift (num
, precision
, n
);
1909 num
.overflow
= !num_eq (orig
, maybe_orig
);
1916 /* The four unary operators: +, -, ! and ~. */
1918 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1923 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1924 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1925 "traditional C rejects the unary plus operator");
1926 num
.overflow
= false;
1930 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1934 num
.high
= ~num
.high
;
1936 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1937 num
.overflow
= false;
1940 default: /* case CPP_NOT: */
1941 num
.low
= num_zerop (num
);
1943 num
.overflow
= false;
1944 num
.unsignedp
= false;
1951 /* The various binary operators. */
1953 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1956 size_t precision
= CPP_OPTION (pfile
, precision
);
1964 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1966 /* A negative shift is a positive shift the other way. */
1967 if (op
== CPP_LSHIFT
)
1971 rhs
= num_negate (rhs
, precision
);
1974 n
= ~0; /* Maximal. */
1977 if (op
== CPP_LSHIFT
)
1978 lhs
= num_lshift (lhs
, precision
, n
);
1980 lhs
= num_rshift (lhs
, precision
, n
);
1985 result
.low
= lhs
.low
- rhs
.low
;
1986 result
.high
= lhs
.high
- rhs
.high
;
1987 if (result
.low
> lhs
.low
)
1989 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1990 result
.overflow
= false;
1992 result
= num_trim (result
, precision
);
1993 if (!result
.unsignedp
)
1995 bool lhsp
= num_positive (lhs
, precision
);
1996 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1997 && lhsp
!= num_positive (result
, precision
));
2002 result
.low
= lhs
.low
+ rhs
.low
;
2003 result
.high
= lhs
.high
+ rhs
.high
;
2004 if (result
.low
< lhs
.low
)
2006 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2007 result
.overflow
= false;
2009 result
= num_trim (result
, precision
);
2010 if (!result
.unsignedp
)
2012 bool lhsp
= num_positive (lhs
, precision
);
2013 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
2014 && lhsp
!= num_positive (result
, precision
));
2019 default: /* case CPP_COMMA: */
2020 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
2021 || !pfile
->state
.skip_eval
))
2022 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2023 "comma operator in operand of #if");
2031 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2034 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
2037 cpp_num_part middle
[2], temp
;
2039 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
2040 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
2042 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
2043 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
2046 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
2047 if (result
.low
< temp
)
2051 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
2052 if (result
.low
< temp
)
2055 result
.high
+= HIGH_PART (middle
[0]);
2056 result
.high
+= HIGH_PART (middle
[1]);
2057 result
.unsignedp
= true;
2058 result
.overflow
= false;
2063 /* Multiply two preprocessing numbers. */
2065 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
2067 cpp_num result
, temp
;
2068 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2069 bool overflow
, negate
= false;
2070 size_t precision
= CPP_OPTION (pfile
, precision
);
2072 /* Prepare for unsigned multiplication. */
2075 if (!num_positive (lhs
, precision
))
2076 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
2077 if (!num_positive (rhs
, precision
))
2078 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2081 overflow
= lhs
.high
&& rhs
.high
;
2082 result
= num_part_mul (lhs
.low
, rhs
.low
);
2084 temp
= num_part_mul (lhs
.high
, rhs
.low
);
2085 result
.high
+= temp
.low
;
2089 temp
= num_part_mul (lhs
.low
, rhs
.high
);
2090 result
.high
+= temp
.low
;
2094 temp
.low
= result
.low
, temp
.high
= result
.high
;
2095 result
= num_trim (result
, precision
);
2096 if (!num_eq (result
, temp
))
2100 result
= num_negate (result
, precision
);
2103 result
.overflow
= false;
2105 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
2106 && !num_zerop (result
));
2107 result
.unsignedp
= unsignedp
;
2112 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2113 or the remainder depending upon OP. LOCATION is the source location
2114 of this operator (for diagnostics). */
2117 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
2118 location_t location
)
2120 cpp_num result
, sub
;
2122 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2123 bool negate
= false, lhs_neg
= false;
2124 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
2126 /* Prepare for unsigned division. */
2129 if (!num_positive (lhs
, precision
))
2130 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2131 if (!num_positive (rhs
, precision
))
2132 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2135 /* Find the high bit. */
2139 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2140 for (; ; i
--, mask
>>= 1)
2141 if (rhs
.high
& mask
)
2146 if (precision
> PART_PRECISION
)
2147 i
= precision
- PART_PRECISION
- 1;
2150 mask
= (cpp_num_part
) 1 << i
;
2151 for (; ; i
--, mask
>>= 1)
2157 if (!pfile
->state
.skip_eval
)
2158 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2159 "division by zero in #if");
2163 /* First nonzero bit of RHS is bit I. Do naive division by
2164 shifting the RHS fully left, and subtracting from LHS if LHS is
2165 at least as big, and then repeating but with one less shift.
2166 This is not very efficient, but is easy to understand. */
2168 rhs
.unsignedp
= true;
2169 lhs
.unsignedp
= true;
2170 i
= precision
- i
- 1;
2171 sub
= num_lshift (rhs
, precision
, i
);
2173 result
.high
= result
.low
= 0;
2176 if (num_greater_eq (lhs
, sub
, precision
))
2178 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2179 if (i
>= PART_PRECISION
)
2180 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2182 result
.low
|= (cpp_num_part
) 1 << i
;
2186 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2190 /* We divide so that the remainder has the sign of the LHS. */
2193 result
.unsignedp
= unsignedp
;
2194 result
.overflow
= false;
2198 result
= num_negate (result
, precision
);
2199 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2200 && !num_zerop (result
));
2207 lhs
.unsignedp
= unsignedp
;
2208 lhs
.overflow
= false;
2210 lhs
= num_negate (lhs
, precision
);