1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2021 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. */
218 && !CPP_OPTION (pfile
, cplusplus
)
227 && fn_bits
< CPP_FLOATN_MAX
)
229 fn_bits
= fn_bits
* 10 + (s
[1] - '0');
233 if (len
> 0 && s
[1] == 'x')
243 case 'd': case 'D': d
++; break;
244 case 'l': case 'L': l
++; break;
245 case 'w': case 'W': w
++; break;
246 case 'q': case 'Q': q
++; break;
248 case 'j': case 'J': i
++; break;
255 /* Reject any case of multiple suffixes specifying types, multiple
256 suffixes specifying an imaginary constant, _FloatN or _FloatNx
257 suffixes for invalid values of N, and _FloatN suffixes for values
258 of N larger than can be represented in the return value. The
259 caller is responsible for rejecting _FloatN suffixes where
260 _FloatN is not supported on the chosen target. */
261 if (f
+ d
+ l
+ w
+ q
+ fn
+ fnx
> 1 || i
> 1)
263 if (fn_bits
> CPP_FLOATN_MAX
)
265 if (fnx
&& fn_bits
!= 32 && fn_bits
!= 64 && fn_bits
!= 128)
267 if (fn
&& fn_bits
!= 16 && fn_bits
% 32 != 0)
269 if (fn
&& fn_bits
== 96)
274 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
277 /* In C++14 and up these suffixes are in the standard library, so treat
278 them as user-defined literals. */
279 if (CPP_OPTION (pfile
, cplusplus
)
280 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
284 && (orig_s
[1] == 'f' || orig_s
[1] == 'l'))))
288 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
291 return ((i
? CPP_N_IMAGINARY
: 0)
297 fn
? CPP_N_FLOATN
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
298 fnx
? CPP_N_FLOATNX
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
302 /* Return the classification flags for a float suffix. */
304 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
306 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
309 /* Subroutine of cpp_classify_number. S points to an integer suffix
310 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
311 flag vector describing the suffix. */
313 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
315 size_t orig_len
= len
;
323 case 'z': case 'Z': z
++; break;
324 case 'u': case 'U': u
++; break;
326 case 'j': case 'J': i
++; break;
327 case 'l': case 'L': l
++;
328 /* If there are two Ls, they must be adjacent and the same case. */
329 if (l
== 2 && s
[len
] != s
[len
+ 1])
336 if (l
> 2 || u
> 1 || i
> 1 || z
> 1)
343 if (!CPP_OPTION (pfile
, cplusplus
))
349 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
352 /* In C++14 and up these suffixes are in the standard library, so treat
353 them as user-defined literals. */
354 if (CPP_OPTION (pfile
, cplusplus
)
355 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
357 && (orig_len
== 1 || (orig_len
== 2 && s
[1] == 'l')))
361 return ((i
? CPP_N_IMAGINARY
: 0)
362 | (u
? CPP_N_UNSIGNED
: 0)
363 | ((l
== 0) ? CPP_N_SMALL
364 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
)
365 | (z
? CPP_N_SIZE_T
: 0));
368 /* Return the classification flags for an int suffix. */
370 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
372 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
375 /* Return the string type corresponding to the the input user-defined string
376 literal type. If the input type is not a user-defined string literal
377 type return the input type. */
379 cpp_userdef_string_remove_type (enum cpp_ttype type
)
381 if (type
== CPP_STRING_USERDEF
)
383 else if (type
== CPP_WSTRING_USERDEF
)
385 else if (type
== CPP_STRING16_USERDEF
)
387 else if (type
== CPP_STRING32_USERDEF
)
389 else if (type
== CPP_UTF8STRING_USERDEF
)
390 return CPP_UTF8STRING
;
395 /* Return the user-defined string literal type corresponding to the input
396 string type. If the input type is not a string type return the input
399 cpp_userdef_string_add_type (enum cpp_ttype type
)
401 if (type
== CPP_STRING
)
402 return CPP_STRING_USERDEF
;
403 else if (type
== CPP_WSTRING
)
404 return CPP_WSTRING_USERDEF
;
405 else if (type
== CPP_STRING16
)
406 return CPP_STRING16_USERDEF
;
407 else if (type
== CPP_STRING32
)
408 return CPP_STRING32_USERDEF
;
409 else if (type
== CPP_UTF8STRING
)
410 return CPP_UTF8STRING_USERDEF
;
415 /* Return the char type corresponding to the the input user-defined char
416 literal type. If the input type is not a user-defined char literal
417 type return the input type. */
419 cpp_userdef_char_remove_type (enum cpp_ttype type
)
421 if (type
== CPP_CHAR_USERDEF
)
423 else if (type
== CPP_WCHAR_USERDEF
)
425 else if (type
== CPP_CHAR16_USERDEF
)
427 else if (type
== CPP_CHAR32_USERDEF
)
429 else if (type
== CPP_UTF8CHAR_USERDEF
)
435 /* Return the user-defined char literal type corresponding to the input
436 char type. If the input type is not a char type return the input
439 cpp_userdef_char_add_type (enum cpp_ttype type
)
441 if (type
== CPP_CHAR
)
442 return CPP_CHAR_USERDEF
;
443 else if (type
== CPP_WCHAR
)
444 return CPP_WCHAR_USERDEF
;
445 else if (type
== CPP_CHAR16
)
446 return CPP_CHAR16_USERDEF
;
447 else if (type
== CPP_CHAR32
)
448 return CPP_CHAR32_USERDEF
;
449 else if (type
== CPP_UTF8CHAR
)
450 return CPP_UTF8CHAR_USERDEF
;
455 /* Return true if the token type is a user-defined string literal. */
457 cpp_userdef_string_p (enum cpp_ttype type
)
459 if (type
== CPP_STRING_USERDEF
460 || type
== CPP_WSTRING_USERDEF
461 || type
== CPP_STRING16_USERDEF
462 || type
== CPP_STRING32_USERDEF
463 || type
== CPP_UTF8STRING_USERDEF
)
469 /* Return true if the token type is a user-defined char literal. */
471 cpp_userdef_char_p (enum cpp_ttype type
)
473 if (type
== CPP_CHAR_USERDEF
474 || type
== CPP_WCHAR_USERDEF
475 || type
== CPP_CHAR16_USERDEF
476 || type
== CPP_CHAR32_USERDEF
477 || type
== CPP_UTF8CHAR_USERDEF
)
483 /* Extract the suffix from a user-defined literal string or char. */
485 cpp_get_userdef_suffix (const cpp_token
*tok
)
487 unsigned int len
= tok
->val
.str
.len
;
488 const char *text
= (const char *)tok
->val
.str
.text
;
491 for (i
= 0; i
< len
; ++i
)
492 if (text
[i
] == '\'' || text
[i
] == '"')
497 for (i
= len
; i
> 0; --i
)
498 if (text
[i
- 1] == delim
)
503 /* Categorize numeric constants according to their field (integer,
504 floating point, or invalid), radix (decimal, octal, hexadecimal),
507 TOKEN is the token that represents the numeric constant to
510 In C++0X if UD_SUFFIX is non null it will be assigned
511 any unrecognized suffix for a user-defined literal.
513 VIRTUAL_LOCATION is the virtual location for TOKEN. */
515 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
516 const char **ud_suffix
, location_t virtual_location
)
518 const uchar
*str
= token
->val
.str
.text
;
520 unsigned int max_digit
, result
, radix
;
521 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
528 /* If the lexer has done its job, length one can only be a single
529 digit. Fast-path this very common case. */
530 if (token
->val
.str
.len
== 1)
531 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
533 limit
= str
+ token
->val
.str
.len
;
534 float_flag
= NOT_FLOAT
;
538 seen_digit_sep
= false;
540 /* First, interpret the radix. */
546 /* Require at least one hex digit to classify it as hex. */
547 if (*str
== 'x' || *str
== 'X')
549 if (str
[1] == '.' || ISXDIGIT (str
[1]))
554 else if (DIGIT_SEP (str
[1]))
555 SYNTAX_ERROR_AT (virtual_location
,
556 "digit separator after base indicator");
558 else if (*str
== 'b' || *str
== 'B')
560 if (str
[1] == '0' || str
[1] == '1')
565 else if (DIGIT_SEP (str
[1]))
566 SYNTAX_ERROR_AT (virtual_location
,
567 "digit separator after base indicator");
571 /* Now scan for a well-formed integer or float. */
574 unsigned int c
= *str
++;
576 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
578 seen_digit_sep
= false;
584 else if (DIGIT_SEP (c
))
585 seen_digit_sep
= true;
588 if (seen_digit_sep
|| DIGIT_SEP (*str
))
589 SYNTAX_ERROR_AT (virtual_location
,
590 "digit separator adjacent to decimal point");
591 seen_digit_sep
= false;
592 if (float_flag
== NOT_FLOAT
)
593 float_flag
= AFTER_POINT
;
595 SYNTAX_ERROR_AT (virtual_location
,
596 "too many decimal points in number");
598 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
599 || (radix
== 16 && (c
== 'p' || c
== 'P')))
601 if (seen_digit_sep
|| DIGIT_SEP (*str
))
602 SYNTAX_ERROR_AT (virtual_location
,
603 "digit separator adjacent to exponent");
604 float_flag
= AFTER_EXPON
;
609 /* Start of suffix. */
615 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
616 SYNTAX_ERROR_AT (virtual_location
,
617 "digit separator outside digit sequence");
619 /* The suffix may be for decimal fixed-point constants without exponent. */
620 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
622 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
623 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
625 result
|= CPP_N_FLOATING
;
626 /* We need to restore the radix to 10, if the radix is 8. */
630 if (CPP_PEDANTIC (pfile
))
631 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
632 "fixed-point constants are a GCC extension");
639 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
642 if (max_digit
>= radix
)
645 SYNTAX_ERROR2_AT (virtual_location
,
646 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
648 SYNTAX_ERROR2_AT (virtual_location
,
649 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
652 if (float_flag
!= NOT_FLOAT
)
656 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
657 "invalid prefix \"0b\" for floating constant");
658 return CPP_N_INVALID
;
661 if (radix
== 16 && !seen_digit
)
662 SYNTAX_ERROR_AT (virtual_location
,
663 "no digits in hexadecimal floating constant");
665 if (radix
== 16 && CPP_PEDANTIC (pfile
)
666 && !CPP_OPTION (pfile
, extended_numbers
))
668 if (CPP_OPTION (pfile
, cplusplus
))
669 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
670 "use of C++17 hexadecimal floating constant");
672 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
673 "use of C99 hexadecimal floating constant");
676 if (float_flag
== AFTER_EXPON
)
678 if (*str
== '+' || *str
== '-')
681 /* Exponent is decimal, even if string is a hex float. */
684 if (DIGIT_SEP (*str
))
685 SYNTAX_ERROR_AT (virtual_location
,
686 "digit separator adjacent to exponent");
688 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
692 seen_digit_sep
= DIGIT_SEP (*str
);
695 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
697 else if (radix
== 16)
698 SYNTAX_ERROR_AT (virtual_location
,
699 "hexadecimal floating constants require an exponent");
702 SYNTAX_ERROR_AT (virtual_location
,
703 "digit separator outside digit sequence");
705 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
708 if (CPP_OPTION (pfile
, user_literals
))
711 *ud_suffix
= (const char *) str
;
712 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
716 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
717 "invalid suffix \"%.*s\" on floating constant",
718 (int) (limit
- str
), str
);
719 return CPP_N_INVALID
;
723 /* Traditional C didn't accept any floating suffixes. */
725 && CPP_WTRADITIONAL (pfile
)
726 && ! cpp_sys_macro_p (pfile
))
727 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
728 "traditional C rejects the \"%.*s\" suffix",
729 (int) (limit
- str
), str
);
731 /* A suffix for double is a GCC extension via decimal float support.
732 If the suffix also specifies an imaginary value we'll catch that
734 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
735 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
736 "suffix for double constant is a GCC extension");
738 /* Radix must be 10 for decimal floats. */
739 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
741 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
742 "invalid suffix \"%.*s\" with hexadecimal floating constant",
743 (int) (limit
- str
), str
);
744 return CPP_N_INVALID
;
747 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
748 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
749 "fixed-point constants are a GCC extension");
751 if (result
& CPP_N_DFLOAT
)
753 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, dfp_constants
))
754 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
755 "decimal float constants are a C2X feature");
756 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
757 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
759 "decimal float constants are a C2X feature");
762 result
|= CPP_N_FLOATING
;
766 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
769 if (CPP_OPTION (pfile
, user_literals
))
772 *ud_suffix
= (const char *) str
;
773 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
777 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
778 "invalid suffix \"%.*s\" on integer constant",
779 (int) (limit
- str
), str
);
780 return CPP_N_INVALID
;
784 /* Traditional C only accepted the 'L' suffix.
785 Suppress warning about 'LL' with -Wno-long-long. */
786 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
788 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
789 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
790 && CPP_OPTION (pfile
, cpp_warn_long_long
);
793 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
795 "traditional C rejects the \"%.*s\" suffix",
796 (int) (limit
- str
), str
);
799 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
800 && CPP_OPTION (pfile
, cpp_warn_long_long
))
802 const char *message
= CPP_OPTION (pfile
, cplusplus
)
803 ? N_("use of C++11 long long integer constant")
804 : N_("use of C99 long long integer constant");
806 if (CPP_OPTION (pfile
, c99
))
807 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
810 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
811 virtual_location
, 0, message
);
814 if ((result
& CPP_N_SIZE_T
) == CPP_N_SIZE_T
815 && !CPP_OPTION (pfile
, size_t_literals
))
817 const char *message
= (result
& CPP_N_UNSIGNED
) == CPP_N_UNSIGNED
818 ? N_("use of C++23 %<size_t%> integer constant")
819 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant");
820 cpp_warning_with_line (pfile
, CPP_W_SIZE_T_LITERALS
,
821 virtual_location
, 0, message
);
824 result
|= CPP_N_INTEGER
;
828 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
829 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
830 "imaginary constants are a GCC extension");
833 if (!CPP_OPTION (pfile
, binary_constants
)
834 && CPP_PEDANTIC (pfile
))
835 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
836 CPP_OPTION (pfile
, cplusplus
)
837 ? N_("binary constants are a C++14 feature "
839 : N_("binary constants are a C2X feature "
840 "or GCC extension"));
841 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
842 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
844 "binary constants are a C2X feature");
848 result
|= CPP_N_DECIMAL
;
849 else if (radix
== 16)
852 result
|= CPP_N_BINARY
;
854 result
|= CPP_N_OCTAL
;
859 return CPP_N_INVALID
;
862 /* cpp_interpret_integer converts an integer constant into a cpp_num,
863 of precision options->precision.
865 We do not provide any interface for decimal->float conversion,
866 because the preprocessor doesn't need it and we don't want to
867 drag in GCC's floating point emulator. */
869 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
872 const uchar
*p
, *end
;
877 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
878 result
.overflow
= false;
880 p
= token
->val
.str
.text
;
881 end
= p
+ token
->val
.str
.len
;
883 /* Common case of a single digit. */
884 if (token
->val
.str
.len
== 1)
885 result
.low
= p
[0] - '0';
889 size_t precision
= CPP_OPTION (pfile
, precision
);
890 unsigned int base
= 10, c
= 0;
891 bool overflow
= false;
893 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
898 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
903 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
909 /* We can add a digit to numbers strictly less than this without
910 needing the precision and slowness of double integers. */
911 max
= ~(cpp_num_part
) 0;
912 if (precision
< PART_PRECISION
)
913 max
>>= PART_PRECISION
- precision
;
914 max
= (max
- base
+ 1) / base
+ 1;
920 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
922 else if (DIGIT_SEP (c
))
927 /* Strict inequality for when max is set to zero. */
928 if (result
.low
< max
)
929 result
.low
= result
.low
* base
+ c
;
932 result
= append_digit (result
, c
, base
, precision
);
933 overflow
|= result
.overflow
;
938 if (overflow
&& !(type
& CPP_N_USERDEF
))
939 cpp_error (pfile
, CPP_DL_PEDWARN
,
940 "integer constant is too large for its type");
941 /* If too big to be signed, consider it unsigned. Only warn for
942 decimal numbers. Traditional numbers were always signed (but
943 we still honor an explicit U suffix); but we only have
944 traditional semantics in directives. */
945 else if (!result
.unsignedp
946 && !(CPP_OPTION (pfile
, traditional
)
947 && pfile
->state
.in_directive
)
948 && !num_positive (result
, precision
))
950 /* This is for constants within the range of uintmax_t but
951 not that of intmax_t. For such decimal constants, a
952 diagnostic is required for C99 as the selected type must
953 be signed and not having a type is a constraint violation
954 (DR#298, TC3), so this must be a pedwarn. For C90,
955 unsigned long is specified to be used for a constant that
956 does not fit in signed long; if uintmax_t has the same
957 range as unsigned long this means only a warning is
958 appropriate here. C90 permits the preprocessor to use a
959 wider range than unsigned long in the compiler, so if
960 uintmax_t is wider than unsigned long no diagnostic is
961 required for such constants in preprocessor #if
962 expressions and the compiler will pedwarn for such
963 constants outside the range of unsigned long that reach
964 the compiler so a diagnostic is not required there
965 either; thus, pedwarn for C99 but use a plain warning for
968 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
971 "integer constant is so large that it is unsigned");
972 result
.unsignedp
= true;
979 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
981 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
986 cpp_num_part add_high
, add_low
;
988 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
989 need to worry about add_high overflowing. */
1003 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
1004 result
.high
= num
.high
<< shift
;
1005 result
.low
= num
.low
<< shift
;
1006 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
1007 result
.unsignedp
= num
.unsignedp
;
1011 add_low
= num
.low
<< 1;
1012 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
1015 add_high
= add_low
= 0;
1017 if (add_low
+ digit
< add_low
)
1021 if (result
.low
+ add_low
< result
.low
)
1023 if (result
.high
+ add_high
< result
.high
)
1026 result
.low
+= add_low
;
1027 result
.high
+= add_high
;
1028 result
.overflow
= overflow
;
1030 /* The above code catches overflow of a cpp_num type. This catches
1031 overflow of the (possibly shorter) target precision. */
1032 num
.low
= result
.low
;
1033 num
.high
= result
.high
;
1034 result
= num_trim (result
, precision
);
1035 if (!num_eq (result
, num
))
1036 result
.overflow
= true;
1041 /* Handle meeting "defined" in a preprocessor expression. */
1043 parse_defined (cpp_reader
*pfile
)
1047 cpp_hashnode
*node
= 0;
1048 const cpp_token
*token
;
1049 cpp_context
*initial_context
= pfile
->context
;
1051 /* Don't expand macros. */
1052 pfile
->state
.prevent_expansion
++;
1054 token
= cpp_get_token (pfile
);
1055 if (token
->type
== CPP_OPEN_PAREN
)
1058 token
= cpp_get_token (pfile
);
1061 if (token
->type
== CPP_NAME
)
1063 node
= token
->val
.node
.node
;
1064 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
1066 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
1072 cpp_error (pfile
, CPP_DL_ERROR
,
1073 "operator \"defined\" requires an identifier");
1074 if (token
->flags
& NAMED_OP
)
1079 op
.type
= token
->type
;
1080 cpp_error (pfile
, CPP_DL_ERROR
,
1081 "(\"%s\" is an alternative token for \"%s\" in C++)",
1082 cpp_token_as_text (pfile
, token
),
1083 cpp_token_as_text (pfile
, &op
));
1087 bool is_defined
= false;
1090 if ((pfile
->context
!= initial_context
1091 || initial_context
!= &pfile
->base_context
)
1092 && CPP_OPTION (pfile
, warn_expansion_to_defined
))
1093 cpp_pedwarning (pfile
, CPP_W_EXPANSION_TO_DEFINED
,
1094 "this use of \"defined\" may not be portable");
1095 is_defined
= _cpp_defined_macro_p (node
);
1096 if (!_cpp_maybe_notify_macro_use (pfile
, node
, token
->src_loc
))
1097 /* It wasn't a macro after all. */
1099 _cpp_mark_macro_used (node
);
1101 /* A possible controlling macro of the form #if !defined ().
1102 _cpp_parse_expr checks there was no other junk on the line. */
1103 pfile
->mi_ind_cmacro
= node
;
1106 pfile
->state
.prevent_expansion
--;
1108 /* Do not treat conditional macros as being defined. This is due to the
1109 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1110 to act as conditional keywords. This messes up tests like #ifndef
1112 result
.unsignedp
= false;
1114 result
.overflow
= false;
1115 result
.low
= is_defined
;
1119 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1120 number or character constant, or the result of the "defined" or "#"
1123 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
1124 location_t virtual_location
)
1130 result
.unsignedp
= false;
1131 result
.overflow
= false;
1133 switch (token
->type
)
1136 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1137 if (temp
& CPP_N_USERDEF
)
1138 cpp_error (pfile
, CPP_DL_ERROR
,
1139 "user-defined literal in preprocessor expression");
1140 switch (temp
& CPP_N_CATEGORY
)
1142 case CPP_N_FLOATING
:
1143 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1144 "floating constant in preprocessor expression");
1147 if (!(temp
& CPP_N_IMAGINARY
))
1148 return cpp_interpret_integer (pfile
, token
, temp
);
1149 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1150 "imaginary number in preprocessor expression");
1154 /* Error already issued. */
1157 result
.high
= result
.low
= 0;
1166 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1171 /* Sign-extend the result if necessary. */
1172 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1174 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1175 result
.low
|= ~(~(cpp_num_part
) 0
1176 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1177 result
.high
= ~(cpp_num_part
) 0;
1178 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1184 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1185 return parse_defined (pfile
);
1186 else if (CPP_OPTION (pfile
, cplusplus
)
1187 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1188 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1191 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1197 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1198 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1199 "\"%s\" is not defined, evaluates to 0",
1200 NODE_NAME (token
->val
.node
.node
));
1205 if (!pfile
->state
.skipping
)
1207 /* A pedantic warning takes precedence over a deprecated
1209 if (CPP_PEDANTIC (pfile
))
1210 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1211 virtual_location
, 0,
1212 "assertions are a GCC extension");
1213 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1214 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1215 "assertions are a deprecated extension");
1217 _cpp_test_assertion (pfile
, &temp
);
1226 result
.unsignedp
= !!unsignedp
;
1230 /* Operator precedence and flags table.
1232 After an operator is returned from the lexer, if it has priority less
1233 than the operator on the top of the stack, we reduce the stack by one
1234 operator and repeat the test. Since equal priorities do not reduce,
1235 this is naturally right-associative.
1237 We handle left-associative operators by decrementing the priority of
1238 just-lexed operators by one, but retaining the priority of operators
1239 already on the stack.
1241 The remaining cases are '(' and ')'. We handle '(' by skipping the
1242 reduction phase completely. ')' is given lower priority than
1243 everything else, including '(', effectively forcing a reduction of the
1244 parenthesized expression. If there is a matching '(', the routine
1245 reduce() exits immediately. If the normal exit route sees a ')', then
1246 there cannot have been a matching '(' and an error message is output.
1248 The parser assumes all shifted operators require a left operand unless
1249 the flag NO_L_OPERAND is set. These semantics are automatic; any
1250 extra semantics need to be handled with operator-specific code. */
1252 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1253 operand changes because of integer promotions. */
1254 #define NO_L_OPERAND (1 << 0)
1255 #define LEFT_ASSOC (1 << 1)
1256 #define CHECK_PROMOTION (1 << 2)
1258 /* Operator to priority map. Must be in the same order as the first
1259 N entries of enum cpp_ttype. */
1260 static const struct cpp_operator
1266 /* EQ */ {0, 0}, /* Shouldn't happen. */
1267 /* NOT */ {16, NO_L_OPERAND
},
1268 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1269 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1270 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1271 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1272 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1273 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1274 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1275 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1276 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1277 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1278 /* RSHIFT */ {13, LEFT_ASSOC
},
1279 /* LSHIFT */ {13, LEFT_ASSOC
},
1281 /* COMPL */ {16, NO_L_OPERAND
},
1282 /* AND_AND */ {6, LEFT_ASSOC
},
1283 /* OR_OR */ {5, LEFT_ASSOC
},
1284 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1285 However, there are some special cases for these in reduce(). */
1287 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1288 /* COMMA */ {4, LEFT_ASSOC
},
1289 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1290 /* CLOSE_PAREN */ {0, 0},
1292 /* EQ_EQ */ {11, LEFT_ASSOC
},
1293 /* NOT_EQ */ {11, LEFT_ASSOC
},
1294 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1295 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1296 /* UPLUS */ {16, NO_L_OPERAND
},
1297 /* UMINUS */ {16, NO_L_OPERAND
}
1300 /* Parse and evaluate a C expression, reading from PFILE.
1301 Returns the truth value of the expression.
1303 The implementation is an operator precedence parser, i.e. a
1304 bottom-up parser, using a stack for not-yet-reduced tokens.
1306 The stack base is op_stack, and the current stack pointer is 'top'.
1307 There is a stack element for each operator (only), and the most
1308 recently pushed operator is 'top->op'. An operand (value) is
1309 stored in the 'value' field of the stack element of the operator
1310 that precedes it. */
1312 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1314 struct op
*top
= pfile
->op_stack
;
1315 unsigned int lex_count
;
1316 bool saw_leading_not
, want_value
= true;
1317 location_t virtual_location
= 0;
1319 pfile
->state
.skip_eval
= 0;
1321 /* Set up detection of #if ! defined(). */
1322 pfile
->mi_ind_cmacro
= 0;
1323 saw_leading_not
= false;
1326 /* Lowest priority operator prevents further reductions. */
1334 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1335 op
.op
= op
.token
->type
;
1336 op
.loc
= virtual_location
;
1340 /* These tokens convert into values. */
1350 SYNTAX_ERROR2_AT (op
.loc
,
1351 "missing binary operator before token \"%s\"",
1352 cpp_token_as_text (pfile
, op
.token
));
1354 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1358 saw_leading_not
= lex_count
== 1;
1370 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1371 SYNTAX_ERROR2_AT (op
.loc
,
1372 "token \"%s\" is not valid in preprocessor expressions",
1373 cpp_token_as_text (pfile
, op
.token
));
1377 /* Check we have a value or operator as appropriate. */
1378 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1381 SYNTAX_ERROR2_AT (op
.loc
,
1382 "missing binary operator before token \"%s\"",
1383 cpp_token_as_text (pfile
, op
.token
));
1385 else if (want_value
)
1387 /* We want a number (or expression) and haven't got one.
1388 Try to emit a specific diagnostic. */
1389 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1390 SYNTAX_ERROR_AT (op
.loc
,
1391 "missing expression between '(' and ')'");
1393 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1394 SYNTAX_ERROR2_AT (op
.loc
,
1395 "%s with no expression", is_if
? "#if" : "#elif");
1397 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1398 SYNTAX_ERROR2_AT (op
.loc
,
1399 "operator '%s' has no right operand",
1400 cpp_token_as_text (pfile
, top
->token
));
1401 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1402 /* Complain about missing paren during reduction. */;
1404 SYNTAX_ERROR2_AT (op
.loc
,
1405 "operator '%s' has no left operand",
1406 cpp_token_as_text (pfile
, op
.token
));
1409 top
= reduce (pfile
, top
, op
.op
);
1413 if (op
.op
== CPP_EOF
)
1418 case CPP_CLOSE_PAREN
:
1421 if (!num_zerop (top
->value
))
1422 pfile
->state
.skip_eval
++;
1426 if (num_zerop (top
->value
))
1427 pfile
->state
.skip_eval
++;
1430 if (top
->op
!= CPP_QUERY
)
1431 SYNTAX_ERROR_AT (op
.loc
,
1432 " ':' without preceding '?'");
1433 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1434 pfile
->state
.skip_eval
++;
1436 pfile
->state
.skip_eval
--;
1443 /* Check for and handle stack overflow. */
1444 if (++top
== pfile
->op_limit
)
1445 top
= _cpp_expand_op_stack (pfile
);
1448 top
->token
= op
.token
;
1452 /* The controlling macro expression is only valid if we called lex 3
1453 times: <!> <defined expression> and <EOF>. push_conditional ()
1454 checks that we are at top-of-file. */
1455 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1456 pfile
->mi_ind_cmacro
= 0;
1458 if (top
!= pfile
->op_stack
)
1460 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1461 "unbalanced stack in %s",
1462 is_if
? "#if" : "#elif");
1464 return false; /* Return false on syntax error. */
1467 return !num_zerop (top
->value
);
1470 /* Reduce the operator / value stack if possible, in preparation for
1471 pushing operator OP. Returns NULL on error, otherwise the top of
1474 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1478 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1481 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1485 if (op
== CPP_OPEN_PAREN
)
1488 /* Decrement the priority of left-associative operators to force a
1489 reduction with operators of otherwise equal priority. */
1490 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1491 while (prio
< optab
[top
->op
].prio
)
1493 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1494 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1495 check_promotion (pfile
, top
);
1503 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1504 top
[-1].loc
= top
->loc
;
1512 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1513 top
->value
, top
->op
);
1514 top
[-1].loc
= top
->loc
;
1519 case CPP_GREATER_EQ
:
1522 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1523 top
[-1].loc
= top
->loc
;
1529 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1530 top
[-1].loc
= top
->loc
;
1537 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1538 top
[-1].loc
= top
->loc
;
1542 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1543 top
[-1].loc
= top
->loc
;
1548 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1549 top
->value
, top
->op
, top
->loc
);
1550 top
[-1].loc
= top
->loc
;
1555 if (!num_zerop (top
->value
))
1556 pfile
->state
.skip_eval
--;
1557 top
->value
.low
= (!num_zerop (top
->value
)
1558 || !num_zerop (top
[1].value
));
1559 top
->value
.high
= 0;
1560 top
->value
.unsignedp
= false;
1561 top
->value
.overflow
= false;
1562 top
->loc
= top
[1].loc
;
1567 if (num_zerop (top
->value
))
1568 pfile
->state
.skip_eval
--;
1569 top
->value
.low
= (!num_zerop (top
->value
)
1570 && !num_zerop (top
[1].value
));
1571 top
->value
.high
= 0;
1572 top
->value
.unsignedp
= false;
1573 top
->value
.overflow
= false;
1574 top
->loc
= top
[1].loc
;
1577 case CPP_OPEN_PAREN
:
1578 if (op
!= CPP_CLOSE_PAREN
)
1580 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1581 top
->token
->src_loc
,
1582 0, "missing ')' in expression");
1586 top
->value
= top
[1].value
;
1587 top
->loc
= top
[1].loc
;
1592 if (!num_zerop (top
->value
))
1594 pfile
->state
.skip_eval
--;
1595 top
->value
= top
[1].value
;
1596 top
->loc
= top
[1].loc
;
1600 top
->value
= top
[2].value
;
1601 top
->loc
= top
[2].loc
;
1603 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1604 || top
[2].value
.unsignedp
);
1608 /* COMMA and COLON should not reduce a QUERY operator. */
1609 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1611 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1619 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1620 cpp_error (pfile
, CPP_DL_PEDWARN
,
1621 "integer overflow in preprocessor expression");
1624 if (op
== CPP_CLOSE_PAREN
)
1626 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1633 /* Returns the position of the old top of stack after expansion. */
1635 _cpp_expand_op_stack (cpp_reader
*pfile
)
1637 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1638 size_t new_size
= old_size
* 2 + 20;
1640 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1641 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1643 return pfile
->op_stack
+ old_size
;
1646 /* Emits a warning if the effective sign of either operand of OP
1647 changes because of integer promotions. */
1649 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1651 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1654 if (op
->value
.unsignedp
)
1656 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1657 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1658 "the left operand of \"%s\" changes sign when promoted",
1659 cpp_token_as_text (pfile
, op
->token
));
1661 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1662 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1663 "the right operand of \"%s\" changes sign when promoted",
1664 cpp_token_as_text (pfile
, op
->token
));
1667 /* Clears the unused high order bits of the number pointed to by PNUM. */
1669 num_trim (cpp_num num
, size_t precision
)
1671 if (precision
> PART_PRECISION
)
1673 precision
-= PART_PRECISION
;
1674 if (precision
< PART_PRECISION
)
1675 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1679 if (precision
< PART_PRECISION
)
1680 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1687 /* True iff A (presumed signed) >= 0. */
1689 num_positive (cpp_num num
, size_t precision
)
1691 if (precision
> PART_PRECISION
)
1693 precision
-= PART_PRECISION
;
1694 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1697 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1700 /* Sign extend a number, with PRECISION significant bits and all
1701 others assumed clear, to fill out a cpp_num structure. */
1703 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1707 if (precision
> PART_PRECISION
)
1709 precision
-= PART_PRECISION
;
1710 if (precision
< PART_PRECISION
1711 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1712 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1714 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1716 if (precision
< PART_PRECISION
)
1717 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1718 num
.high
= ~(cpp_num_part
) 0;
1725 /* Returns the negative of NUM. */
1727 num_negate (cpp_num num
, size_t precision
)
1732 num
.high
= ~num
.high
;
1736 num
= num_trim (num
, precision
);
1737 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1742 /* Returns true if A >= B. */
1744 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1748 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1752 /* Both numbers have signed type. If they are of different
1753 sign, the answer is the sign of A. */
1754 unsignedp
= num_positive (pa
, precision
);
1756 if (unsignedp
!= num_positive (pb
, precision
))
1759 /* Otherwise we can do an unsigned comparison. */
1762 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1765 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1767 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1768 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1770 lhs
.overflow
= false;
1771 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1773 /* As excess precision is zeroed, there is no need to num_trim () as
1774 these operations cannot introduce a set bit there. */
1778 lhs
.high
&= rhs
.high
;
1780 else if (op
== CPP_OR
)
1783 lhs
.high
|= rhs
.high
;
1788 lhs
.high
^= rhs
.high
;
1794 /* Returns LHS OP RHS, where OP is an inequality. */
1796 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1799 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1801 if (op
== CPP_GREATER_EQ
)
1803 else if (op
== CPP_LESS
)
1805 else if (op
== CPP_GREATER
)
1806 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1807 else /* CPP_LESS_EQ. */
1808 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1811 lhs
.overflow
= false;
1812 lhs
.unsignedp
= false;
1816 /* Returns LHS OP RHS, where OP is == or !=. */
1818 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1819 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1821 /* Work around a 3.0.4 bug; see PR 6950. */
1822 bool eq
= num_eq (lhs
, rhs
);
1823 if (op
== CPP_NOT_EQ
)
1827 lhs
.overflow
= false;
1828 lhs
.unsignedp
= false;
1832 /* Shift NUM, of width PRECISION, right by N bits. */
1834 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1836 cpp_num_part sign_mask
;
1837 bool x
= num_positive (num
, precision
);
1839 if (num
.unsignedp
|| x
)
1842 sign_mask
= ~(cpp_num_part
) 0;
1845 num
.high
= num
.low
= sign_mask
;
1849 if (precision
< PART_PRECISION
)
1850 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1851 else if (precision
< 2 * PART_PRECISION
)
1852 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1854 if (n
>= PART_PRECISION
)
1856 n
-= PART_PRECISION
;
1858 num
.high
= sign_mask
;
1863 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1864 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1868 num
= num_trim (num
, precision
);
1869 num
.overflow
= false;
1873 /* Shift NUM, of width PRECISION, left by N bits. */
1875 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1879 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1880 num
.high
= num
.low
= 0;
1884 cpp_num orig
, maybe_orig
;
1888 if (m
>= PART_PRECISION
)
1890 m
-= PART_PRECISION
;
1896 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1899 num
= num_trim (num
, precision
);
1902 num
.overflow
= false;
1905 maybe_orig
= num_rshift (num
, precision
, n
);
1906 num
.overflow
= !num_eq (orig
, maybe_orig
);
1913 /* The four unary operators: +, -, ! and ~. */
1915 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1920 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1921 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1922 "traditional C rejects the unary plus operator");
1923 num
.overflow
= false;
1927 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1931 num
.high
= ~num
.high
;
1933 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1934 num
.overflow
= false;
1937 default: /* case CPP_NOT: */
1938 num
.low
= num_zerop (num
);
1940 num
.overflow
= false;
1941 num
.unsignedp
= false;
1948 /* The various binary operators. */
1950 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1953 size_t precision
= CPP_OPTION (pfile
, precision
);
1961 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1963 /* A negative shift is a positive shift the other way. */
1964 if (op
== CPP_LSHIFT
)
1968 rhs
= num_negate (rhs
, precision
);
1971 n
= ~0; /* Maximal. */
1974 if (op
== CPP_LSHIFT
)
1975 lhs
= num_lshift (lhs
, precision
, n
);
1977 lhs
= num_rshift (lhs
, precision
, n
);
1982 result
.low
= lhs
.low
- rhs
.low
;
1983 result
.high
= lhs
.high
- rhs
.high
;
1984 if (result
.low
> lhs
.low
)
1986 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1987 result
.overflow
= false;
1989 result
= num_trim (result
, precision
);
1990 if (!result
.unsignedp
)
1992 bool lhsp
= num_positive (lhs
, precision
);
1993 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1994 && lhsp
!= num_positive (result
, precision
));
1999 result
.low
= lhs
.low
+ rhs
.low
;
2000 result
.high
= lhs
.high
+ rhs
.high
;
2001 if (result
.low
< lhs
.low
)
2003 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2004 result
.overflow
= false;
2006 result
= num_trim (result
, precision
);
2007 if (!result
.unsignedp
)
2009 bool lhsp
= num_positive (lhs
, precision
);
2010 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
2011 && lhsp
!= num_positive (result
, precision
));
2016 default: /* case CPP_COMMA: */
2017 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
2018 || !pfile
->state
.skip_eval
))
2019 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2020 "comma operator in operand of #if");
2028 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2031 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
2034 cpp_num_part middle
[2], temp
;
2036 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
2037 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
2039 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
2040 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
2043 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
2044 if (result
.low
< temp
)
2048 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
2049 if (result
.low
< temp
)
2052 result
.high
+= HIGH_PART (middle
[0]);
2053 result
.high
+= HIGH_PART (middle
[1]);
2054 result
.unsignedp
= true;
2055 result
.overflow
= false;
2060 /* Multiply two preprocessing numbers. */
2062 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
2064 cpp_num result
, temp
;
2065 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2066 bool overflow
, negate
= false;
2067 size_t precision
= CPP_OPTION (pfile
, precision
);
2069 /* Prepare for unsigned multiplication. */
2072 if (!num_positive (lhs
, precision
))
2073 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
2074 if (!num_positive (rhs
, precision
))
2075 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2078 overflow
= lhs
.high
&& rhs
.high
;
2079 result
= num_part_mul (lhs
.low
, rhs
.low
);
2081 temp
= num_part_mul (lhs
.high
, rhs
.low
);
2082 result
.high
+= temp
.low
;
2086 temp
= num_part_mul (lhs
.low
, rhs
.high
);
2087 result
.high
+= temp
.low
;
2091 temp
.low
= result
.low
, temp
.high
= result
.high
;
2092 result
= num_trim (result
, precision
);
2093 if (!num_eq (result
, temp
))
2097 result
= num_negate (result
, precision
);
2100 result
.overflow
= false;
2102 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
2103 && !num_zerop (result
));
2104 result
.unsignedp
= unsignedp
;
2109 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2110 or the remainder depending upon OP. LOCATION is the source location
2111 of this operator (for diagnostics). */
2114 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
2115 location_t location
)
2117 cpp_num result
, sub
;
2119 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2120 bool negate
= false, lhs_neg
= false;
2121 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
2123 /* Prepare for unsigned division. */
2126 if (!num_positive (lhs
, precision
))
2127 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2128 if (!num_positive (rhs
, precision
))
2129 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2132 /* Find the high bit. */
2136 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2137 for (; ; i
--, mask
>>= 1)
2138 if (rhs
.high
& mask
)
2143 if (precision
> PART_PRECISION
)
2144 i
= precision
- PART_PRECISION
- 1;
2147 mask
= (cpp_num_part
) 1 << i
;
2148 for (; ; i
--, mask
>>= 1)
2154 if (!pfile
->state
.skip_eval
)
2155 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2156 "division by zero in #if");
2160 /* First nonzero bit of RHS is bit I. Do naive division by
2161 shifting the RHS fully left, and subtracting from LHS if LHS is
2162 at least as big, and then repeating but with one less shift.
2163 This is not very efficient, but is easy to understand. */
2165 rhs
.unsignedp
= true;
2166 lhs
.unsignedp
= true;
2167 i
= precision
- i
- 1;
2168 sub
= num_lshift (rhs
, precision
, i
);
2170 result
.high
= result
.low
= 0;
2173 if (num_greater_eq (lhs
, sub
, precision
))
2175 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2176 if (i
>= PART_PRECISION
)
2177 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2179 result
.low
|= (cpp_num_part
) 1 << i
;
2183 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2187 /* We divide so that the remainder has the sign of the LHS. */
2190 result
.unsignedp
= unsignedp
;
2191 result
.overflow
= false;
2195 result
= num_negate (result
, precision
);
2196 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2197 && !num_zerop (result
));
2204 lhs
.unsignedp
= unsignedp
;
2205 lhs
.overflow
= false;
2207 lhs
= num_negate (lhs
, precision
);