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 '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)
340 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
343 /* In C++14 and up these suffixes are in the standard library, so treat
344 them as user-defined literals. */
345 if (CPP_OPTION (pfile
, cplusplus
)
346 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
348 && (orig_len
== 1 || (orig_len
== 2 && s
[1] == 'l')))
352 return ((i
? CPP_N_IMAGINARY
: 0)
353 | (u
? CPP_N_UNSIGNED
: 0)
354 | ((l
== 0) ? CPP_N_SMALL
355 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
358 /* Return the classification flags for an int suffix. */
360 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
362 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
365 /* Return the string type corresponding to the the input user-defined string
366 literal type. If the input type is not a user-defined string literal
367 type return the input type. */
369 cpp_userdef_string_remove_type (enum cpp_ttype type
)
371 if (type
== CPP_STRING_USERDEF
)
373 else if (type
== CPP_WSTRING_USERDEF
)
375 else if (type
== CPP_STRING16_USERDEF
)
377 else if (type
== CPP_STRING32_USERDEF
)
379 else if (type
== CPP_UTF8STRING_USERDEF
)
380 return CPP_UTF8STRING
;
385 /* Return the user-defined string literal type corresponding to the input
386 string type. If the input type is not a string type return the input
389 cpp_userdef_string_add_type (enum cpp_ttype type
)
391 if (type
== CPP_STRING
)
392 return CPP_STRING_USERDEF
;
393 else if (type
== CPP_WSTRING
)
394 return CPP_WSTRING_USERDEF
;
395 else if (type
== CPP_STRING16
)
396 return CPP_STRING16_USERDEF
;
397 else if (type
== CPP_STRING32
)
398 return CPP_STRING32_USERDEF
;
399 else if (type
== CPP_UTF8STRING
)
400 return CPP_UTF8STRING_USERDEF
;
405 /* Return the char type corresponding to the the input user-defined char
406 literal type. If the input type is not a user-defined char literal
407 type return the input type. */
409 cpp_userdef_char_remove_type (enum cpp_ttype type
)
411 if (type
== CPP_CHAR_USERDEF
)
413 else if (type
== CPP_WCHAR_USERDEF
)
415 else if (type
== CPP_CHAR16_USERDEF
)
417 else if (type
== CPP_CHAR32_USERDEF
)
419 else if (type
== CPP_UTF8CHAR_USERDEF
)
425 /* Return the user-defined char literal type corresponding to the input
426 char type. If the input type is not a char type return the input
429 cpp_userdef_char_add_type (enum cpp_ttype type
)
431 if (type
== CPP_CHAR
)
432 return CPP_CHAR_USERDEF
;
433 else if (type
== CPP_WCHAR
)
434 return CPP_WCHAR_USERDEF
;
435 else if (type
== CPP_CHAR16
)
436 return CPP_CHAR16_USERDEF
;
437 else if (type
== CPP_CHAR32
)
438 return CPP_CHAR32_USERDEF
;
439 else if (type
== CPP_UTF8CHAR
)
440 return CPP_UTF8CHAR_USERDEF
;
445 /* Return true if the token type is a user-defined string literal. */
447 cpp_userdef_string_p (enum cpp_ttype type
)
449 if (type
== CPP_STRING_USERDEF
450 || type
== CPP_WSTRING_USERDEF
451 || type
== CPP_STRING16_USERDEF
452 || type
== CPP_STRING32_USERDEF
453 || type
== CPP_UTF8STRING_USERDEF
)
459 /* Return true if the token type is a user-defined char literal. */
461 cpp_userdef_char_p (enum cpp_ttype type
)
463 if (type
== CPP_CHAR_USERDEF
464 || type
== CPP_WCHAR_USERDEF
465 || type
== CPP_CHAR16_USERDEF
466 || type
== CPP_CHAR32_USERDEF
467 || type
== CPP_UTF8CHAR_USERDEF
)
473 /* Extract the suffix from a user-defined literal string or char. */
475 cpp_get_userdef_suffix (const cpp_token
*tok
)
477 unsigned int len
= tok
->val
.str
.len
;
478 const char *text
= (const char *)tok
->val
.str
.text
;
481 for (i
= 0; i
< len
; ++i
)
482 if (text
[i
] == '\'' || text
[i
] == '"')
487 for (i
= len
; i
> 0; --i
)
488 if (text
[i
- 1] == delim
)
493 /* Categorize numeric constants according to their field (integer,
494 floating point, or invalid), radix (decimal, octal, hexadecimal),
497 TOKEN is the token that represents the numeric constant to
500 In C++0X if UD_SUFFIX is non null it will be assigned
501 any unrecognized suffix for a user-defined literal.
503 VIRTUAL_LOCATION is the virtual location for TOKEN. */
505 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
506 const char **ud_suffix
, location_t virtual_location
)
508 const uchar
*str
= token
->val
.str
.text
;
510 unsigned int max_digit
, result
, radix
;
511 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
518 /* If the lexer has done its job, length one can only be a single
519 digit. Fast-path this very common case. */
520 if (token
->val
.str
.len
== 1)
521 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
523 limit
= str
+ token
->val
.str
.len
;
524 float_flag
= NOT_FLOAT
;
528 seen_digit_sep
= false;
530 /* First, interpret the radix. */
536 /* Require at least one hex digit to classify it as hex. */
537 if (*str
== 'x' || *str
== 'X')
539 if (str
[1] == '.' || ISXDIGIT (str
[1]))
544 else if (DIGIT_SEP (str
[1]))
545 SYNTAX_ERROR_AT (virtual_location
,
546 "digit separator after base indicator");
548 else if (*str
== 'b' || *str
== 'B')
550 if (str
[1] == '0' || str
[1] == '1')
555 else if (DIGIT_SEP (str
[1]))
556 SYNTAX_ERROR_AT (virtual_location
,
557 "digit separator after base indicator");
561 /* Now scan for a well-formed integer or float. */
564 unsigned int c
= *str
++;
566 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
568 seen_digit_sep
= false;
574 else if (DIGIT_SEP (c
))
577 SYNTAX_ERROR_AT (virtual_location
, "adjacent digit separators");
578 seen_digit_sep
= true;
582 if (seen_digit_sep
|| DIGIT_SEP (*str
))
583 SYNTAX_ERROR_AT (virtual_location
,
584 "digit separator adjacent to decimal point");
585 seen_digit_sep
= false;
586 if (float_flag
== NOT_FLOAT
)
587 float_flag
= AFTER_POINT
;
589 SYNTAX_ERROR_AT (virtual_location
,
590 "too many decimal points in number");
592 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
593 || (radix
== 16 && (c
== 'p' || c
== 'P')))
595 if (seen_digit_sep
|| DIGIT_SEP (*str
))
596 SYNTAX_ERROR_AT (virtual_location
,
597 "digit separator adjacent to exponent");
598 float_flag
= AFTER_EXPON
;
603 /* Start of suffix. */
609 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
610 SYNTAX_ERROR_AT (virtual_location
,
611 "digit separator outside digit sequence");
613 /* The suffix may be for decimal fixed-point constants without exponent. */
614 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
616 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
617 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
619 result
|= CPP_N_FLOATING
;
620 /* We need to restore the radix to 10, if the radix is 8. */
624 if (CPP_PEDANTIC (pfile
))
625 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
626 "fixed-point constants are a GCC extension");
633 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
636 if (max_digit
>= radix
)
639 SYNTAX_ERROR2_AT (virtual_location
,
640 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
642 SYNTAX_ERROR2_AT (virtual_location
,
643 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
646 if (float_flag
!= NOT_FLOAT
)
650 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
651 "invalid prefix \"0b\" for floating constant");
652 return CPP_N_INVALID
;
655 if (radix
== 16 && !seen_digit
)
656 SYNTAX_ERROR_AT (virtual_location
,
657 "no digits in hexadecimal floating constant");
659 if (radix
== 16 && CPP_PEDANTIC (pfile
)
660 && !CPP_OPTION (pfile
, extended_numbers
))
662 if (CPP_OPTION (pfile
, cplusplus
))
663 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
664 "use of C++17 hexadecimal floating constant");
666 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
667 "use of C99 hexadecimal floating constant");
670 if (float_flag
== AFTER_EXPON
)
672 if (*str
== '+' || *str
== '-')
675 /* Exponent is decimal, even if string is a hex float. */
678 if (DIGIT_SEP (*str
))
679 SYNTAX_ERROR_AT (virtual_location
,
680 "digit separator adjacent to exponent");
682 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
686 seen_digit_sep
= DIGIT_SEP (*str
);
689 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
691 else if (radix
== 16)
692 SYNTAX_ERROR_AT (virtual_location
,
693 "hexadecimal floating constants require an exponent");
696 SYNTAX_ERROR_AT (virtual_location
,
697 "digit separator outside digit sequence");
699 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
702 if (CPP_OPTION (pfile
, user_literals
))
705 *ud_suffix
= (const char *) str
;
706 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
710 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
711 "invalid suffix \"%.*s\" on floating constant",
712 (int) (limit
- str
), str
);
713 return CPP_N_INVALID
;
717 /* Traditional C didn't accept any floating suffixes. */
719 && CPP_WTRADITIONAL (pfile
)
720 && ! cpp_sys_macro_p (pfile
))
721 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
722 "traditional C rejects the \"%.*s\" suffix",
723 (int) (limit
- str
), str
);
725 /* A suffix for double is a GCC extension via decimal float support.
726 If the suffix also specifies an imaginary value we'll catch that
728 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
729 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
730 "suffix for double constant is a GCC extension");
732 /* Radix must be 10 for decimal floats. */
733 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
735 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
736 "invalid suffix \"%.*s\" with hexadecimal floating constant",
737 (int) (limit
- str
), str
);
738 return CPP_N_INVALID
;
741 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
742 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
743 "fixed-point constants are a GCC extension");
745 if (result
& CPP_N_DFLOAT
)
747 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, dfp_constants
))
748 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
749 "decimal float constants are a C2X feature");
750 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
751 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
753 "decimal float constants are a C2X feature");
756 result
|= CPP_N_FLOATING
;
760 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
763 if (CPP_OPTION (pfile
, user_literals
))
766 *ud_suffix
= (const char *) str
;
767 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
771 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
772 "invalid suffix \"%.*s\" on integer constant",
773 (int) (limit
- str
), str
);
774 return CPP_N_INVALID
;
778 /* Traditional C only accepted the 'L' suffix.
779 Suppress warning about 'LL' with -Wno-long-long. */
780 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
782 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
783 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
784 && CPP_OPTION (pfile
, cpp_warn_long_long
);
787 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
789 "traditional C rejects the \"%.*s\" suffix",
790 (int) (limit
- str
), str
);
793 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
794 && CPP_OPTION (pfile
, cpp_warn_long_long
))
796 const char *message
= CPP_OPTION (pfile
, cplusplus
)
797 ? N_("use of C++11 long long integer constant")
798 : N_("use of C99 long long integer constant");
800 if (CPP_OPTION (pfile
, c99
))
801 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
804 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
805 virtual_location
, 0, message
);
808 result
|= CPP_N_INTEGER
;
812 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
813 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
814 "imaginary constants are a GCC extension");
817 if (!CPP_OPTION (pfile
, binary_constants
)
818 && CPP_PEDANTIC (pfile
))
819 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
820 CPP_OPTION (pfile
, cplusplus
)
821 ? N_("binary constants are a C++14 feature "
823 : N_("binary constants are a C2X feature "
824 "or GCC extension"));
825 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
826 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
828 "binary constants are a C2X feature");
832 result
|= CPP_N_DECIMAL
;
833 else if (radix
== 16)
836 result
|= CPP_N_BINARY
;
838 result
|= CPP_N_OCTAL
;
843 return CPP_N_INVALID
;
846 /* cpp_interpret_integer converts an integer constant into a cpp_num,
847 of precision options->precision.
849 We do not provide any interface for decimal->float conversion,
850 because the preprocessor doesn't need it and we don't want to
851 drag in GCC's floating point emulator. */
853 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
856 const uchar
*p
, *end
;
861 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
862 result
.overflow
= false;
864 p
= token
->val
.str
.text
;
865 end
= p
+ token
->val
.str
.len
;
867 /* Common case of a single digit. */
868 if (token
->val
.str
.len
== 1)
869 result
.low
= p
[0] - '0';
873 size_t precision
= CPP_OPTION (pfile
, precision
);
874 unsigned int base
= 10, c
= 0;
875 bool overflow
= false;
877 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
882 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
887 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
893 /* We can add a digit to numbers strictly less than this without
894 needing the precision and slowness of double integers. */
895 max
= ~(cpp_num_part
) 0;
896 if (precision
< PART_PRECISION
)
897 max
>>= PART_PRECISION
- precision
;
898 max
= (max
- base
+ 1) / base
+ 1;
904 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
906 else if (DIGIT_SEP (c
))
911 /* Strict inequality for when max is set to zero. */
912 if (result
.low
< max
)
913 result
.low
= result
.low
* base
+ c
;
916 result
= append_digit (result
, c
, base
, precision
);
917 overflow
|= result
.overflow
;
922 if (overflow
&& !(type
& CPP_N_USERDEF
))
923 cpp_error (pfile
, CPP_DL_PEDWARN
,
924 "integer constant is too large for its type");
925 /* If too big to be signed, consider it unsigned. Only warn for
926 decimal numbers. Traditional numbers were always signed (but
927 we still honor an explicit U suffix); but we only have
928 traditional semantics in directives. */
929 else if (!result
.unsignedp
930 && !(CPP_OPTION (pfile
, traditional
)
931 && pfile
->state
.in_directive
)
932 && !num_positive (result
, precision
))
934 /* This is for constants within the range of uintmax_t but
935 not that of intmax_t. For such decimal constants, a
936 diagnostic is required for C99 as the selected type must
937 be signed and not having a type is a constraint violation
938 (DR#298, TC3), so this must be a pedwarn. For C90,
939 unsigned long is specified to be used for a constant that
940 does not fit in signed long; if uintmax_t has the same
941 range as unsigned long this means only a warning is
942 appropriate here. C90 permits the preprocessor to use a
943 wider range than unsigned long in the compiler, so if
944 uintmax_t is wider than unsigned long no diagnostic is
945 required for such constants in preprocessor #if
946 expressions and the compiler will pedwarn for such
947 constants outside the range of unsigned long that reach
948 the compiler so a diagnostic is not required there
949 either; thus, pedwarn for C99 but use a plain warning for
952 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
955 "integer constant is so large that it is unsigned");
956 result
.unsignedp
= true;
963 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
965 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
970 cpp_num_part add_high
, add_low
;
972 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
973 need to worry about add_high overflowing. */
987 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
988 result
.high
= num
.high
<< shift
;
989 result
.low
= num
.low
<< shift
;
990 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
991 result
.unsignedp
= num
.unsignedp
;
995 add_low
= num
.low
<< 1;
996 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
999 add_high
= add_low
= 0;
1001 if (add_low
+ digit
< add_low
)
1005 if (result
.low
+ add_low
< result
.low
)
1007 if (result
.high
+ add_high
< result
.high
)
1010 result
.low
+= add_low
;
1011 result
.high
+= add_high
;
1012 result
.overflow
= overflow
;
1014 /* The above code catches overflow of a cpp_num type. This catches
1015 overflow of the (possibly shorter) target precision. */
1016 num
.low
= result
.low
;
1017 num
.high
= result
.high
;
1018 result
= num_trim (result
, precision
);
1019 if (!num_eq (result
, num
))
1020 result
.overflow
= true;
1025 /* Handle meeting "defined" in a preprocessor expression. */
1027 parse_defined (cpp_reader
*pfile
)
1031 cpp_hashnode
*node
= 0;
1032 const cpp_token
*token
;
1033 cpp_context
*initial_context
= pfile
->context
;
1035 /* Don't expand macros. */
1036 pfile
->state
.prevent_expansion
++;
1038 token
= cpp_get_token (pfile
);
1039 if (token
->type
== CPP_OPEN_PAREN
)
1042 token
= cpp_get_token (pfile
);
1045 if (token
->type
== CPP_NAME
)
1047 node
= token
->val
.node
.node
;
1048 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
1050 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
1056 cpp_error (pfile
, CPP_DL_ERROR
,
1057 "operator \"defined\" requires an identifier");
1058 if (token
->flags
& NAMED_OP
)
1063 op
.type
= token
->type
;
1064 cpp_error (pfile
, CPP_DL_ERROR
,
1065 "(\"%s\" is an alternative token for \"%s\" in C++)",
1066 cpp_token_as_text (pfile
, token
),
1067 cpp_token_as_text (pfile
, &op
));
1071 bool is_defined
= false;
1074 if ((pfile
->context
!= initial_context
1075 || initial_context
!= &pfile
->base_context
)
1076 && CPP_OPTION (pfile
, warn_expansion_to_defined
))
1077 cpp_pedwarning (pfile
, CPP_W_EXPANSION_TO_DEFINED
,
1078 "this use of \"defined\" may not be portable");
1079 is_defined
= _cpp_defined_macro_p (node
);
1080 if (!_cpp_maybe_notify_macro_use (pfile
, node
, token
->src_loc
))
1081 /* It wasn't a macro after all. */
1083 _cpp_mark_macro_used (node
);
1085 /* A possible controlling macro of the form #if !defined ().
1086 _cpp_parse_expr checks there was no other junk on the line. */
1087 pfile
->mi_ind_cmacro
= node
;
1090 pfile
->state
.prevent_expansion
--;
1092 /* Do not treat conditional macros as being defined. This is due to the
1093 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1094 to act as conditional keywords. This messes up tests like #ifndef
1096 result
.unsignedp
= false;
1098 result
.overflow
= false;
1099 result
.low
= is_defined
;
1103 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1104 number or character constant, or the result of the "defined" or "#"
1107 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
1108 location_t virtual_location
)
1114 result
.unsignedp
= false;
1115 result
.overflow
= false;
1117 switch (token
->type
)
1120 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1121 if (temp
& CPP_N_USERDEF
)
1122 cpp_error (pfile
, CPP_DL_ERROR
,
1123 "user-defined literal in preprocessor expression");
1124 switch (temp
& CPP_N_CATEGORY
)
1126 case CPP_N_FLOATING
:
1127 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1128 "floating constant in preprocessor expression");
1131 if (!(temp
& CPP_N_IMAGINARY
))
1132 return cpp_interpret_integer (pfile
, token
, temp
);
1133 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1134 "imaginary number in preprocessor expression");
1138 /* Error already issued. */
1141 result
.high
= result
.low
= 0;
1150 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1155 /* Sign-extend the result if necessary. */
1156 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1158 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1159 result
.low
|= ~(~(cpp_num_part
) 0
1160 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1161 result
.high
= ~(cpp_num_part
) 0;
1162 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1168 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1169 return parse_defined (pfile
);
1170 else if (CPP_OPTION (pfile
, cplusplus
)
1171 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1172 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1175 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1181 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1182 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1183 "\"%s\" is not defined, evaluates to 0",
1184 NODE_NAME (token
->val
.node
.node
));
1189 if (!pfile
->state
.skipping
)
1191 /* A pedantic warning takes precedence over a deprecated
1193 if (CPP_PEDANTIC (pfile
))
1194 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1195 virtual_location
, 0,
1196 "assertions are a GCC extension");
1197 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1198 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1199 "assertions are a deprecated extension");
1201 _cpp_test_assertion (pfile
, &temp
);
1210 result
.unsignedp
= !!unsignedp
;
1214 /* Operator precedence and flags table.
1216 After an operator is returned from the lexer, if it has priority less
1217 than the operator on the top of the stack, we reduce the stack by one
1218 operator and repeat the test. Since equal priorities do not reduce,
1219 this is naturally right-associative.
1221 We handle left-associative operators by decrementing the priority of
1222 just-lexed operators by one, but retaining the priority of operators
1223 already on the stack.
1225 The remaining cases are '(' and ')'. We handle '(' by skipping the
1226 reduction phase completely. ')' is given lower priority than
1227 everything else, including '(', effectively forcing a reduction of the
1228 parenthesized expression. If there is a matching '(', the routine
1229 reduce() exits immediately. If the normal exit route sees a ')', then
1230 there cannot have been a matching '(' and an error message is output.
1232 The parser assumes all shifted operators require a left operand unless
1233 the flag NO_L_OPERAND is set. These semantics are automatic; any
1234 extra semantics need to be handled with operator-specific code. */
1236 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1237 operand changes because of integer promotions. */
1238 #define NO_L_OPERAND (1 << 0)
1239 #define LEFT_ASSOC (1 << 1)
1240 #define CHECK_PROMOTION (1 << 2)
1242 /* Operator to priority map. Must be in the same order as the first
1243 N entries of enum cpp_ttype. */
1244 static const struct cpp_operator
1250 /* EQ */ {0, 0}, /* Shouldn't happen. */
1251 /* NOT */ {16, NO_L_OPERAND
},
1252 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1253 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1254 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1255 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1256 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1257 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1258 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1259 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1260 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1261 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1262 /* RSHIFT */ {13, LEFT_ASSOC
},
1263 /* LSHIFT */ {13, LEFT_ASSOC
},
1265 /* COMPL */ {16, NO_L_OPERAND
},
1266 /* AND_AND */ {6, LEFT_ASSOC
},
1267 /* OR_OR */ {5, LEFT_ASSOC
},
1268 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1269 However, there are some special cases for these in reduce(). */
1271 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1272 /* COMMA */ {4, LEFT_ASSOC
},
1273 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1274 /* CLOSE_PAREN */ {0, 0},
1276 /* EQ_EQ */ {11, LEFT_ASSOC
},
1277 /* NOT_EQ */ {11, LEFT_ASSOC
},
1278 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1279 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1280 /* UPLUS */ {16, NO_L_OPERAND
},
1281 /* UMINUS */ {16, NO_L_OPERAND
}
1284 /* Parse and evaluate a C expression, reading from PFILE.
1285 Returns the truth value of the expression.
1287 The implementation is an operator precedence parser, i.e. a
1288 bottom-up parser, using a stack for not-yet-reduced tokens.
1290 The stack base is op_stack, and the current stack pointer is 'top'.
1291 There is a stack element for each operator (only), and the most
1292 recently pushed operator is 'top->op'. An operand (value) is
1293 stored in the 'value' field of the stack element of the operator
1294 that precedes it. */
1296 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1298 struct op
*top
= pfile
->op_stack
;
1299 unsigned int lex_count
;
1300 bool saw_leading_not
, want_value
= true;
1301 location_t virtual_location
= 0;
1303 pfile
->state
.skip_eval
= 0;
1305 /* Set up detection of #if ! defined(). */
1306 pfile
->mi_ind_cmacro
= 0;
1307 saw_leading_not
= false;
1310 /* Lowest priority operator prevents further reductions. */
1318 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1319 op
.op
= op
.token
->type
;
1320 op
.loc
= virtual_location
;
1324 /* These tokens convert into values. */
1334 SYNTAX_ERROR2_AT (op
.loc
,
1335 "missing binary operator before token \"%s\"",
1336 cpp_token_as_text (pfile
, op
.token
));
1338 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1342 saw_leading_not
= lex_count
== 1;
1354 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1355 SYNTAX_ERROR2_AT (op
.loc
,
1356 "token \"%s\" is not valid in preprocessor expressions",
1357 cpp_token_as_text (pfile
, op
.token
));
1361 /* Check we have a value or operator as appropriate. */
1362 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1365 SYNTAX_ERROR2_AT (op
.loc
,
1366 "missing binary operator before token \"%s\"",
1367 cpp_token_as_text (pfile
, op
.token
));
1369 else if (want_value
)
1371 /* We want a number (or expression) and haven't got one.
1372 Try to emit a specific diagnostic. */
1373 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1374 SYNTAX_ERROR_AT (op
.loc
,
1375 "missing expression between '(' and ')'");
1377 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1378 SYNTAX_ERROR2_AT (op
.loc
,
1379 "%s with no expression", is_if
? "#if" : "#elif");
1381 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1382 SYNTAX_ERROR2_AT (op
.loc
,
1383 "operator '%s' has no right operand",
1384 cpp_token_as_text (pfile
, top
->token
));
1385 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1386 /* Complain about missing paren during reduction. */;
1388 SYNTAX_ERROR2_AT (op
.loc
,
1389 "operator '%s' has no left operand",
1390 cpp_token_as_text (pfile
, op
.token
));
1393 top
= reduce (pfile
, top
, op
.op
);
1397 if (op
.op
== CPP_EOF
)
1402 case CPP_CLOSE_PAREN
:
1405 if (!num_zerop (top
->value
))
1406 pfile
->state
.skip_eval
++;
1410 if (num_zerop (top
->value
))
1411 pfile
->state
.skip_eval
++;
1414 if (top
->op
!= CPP_QUERY
)
1415 SYNTAX_ERROR_AT (op
.loc
,
1416 " ':' without preceding '?'");
1417 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1418 pfile
->state
.skip_eval
++;
1420 pfile
->state
.skip_eval
--;
1427 /* Check for and handle stack overflow. */
1428 if (++top
== pfile
->op_limit
)
1429 top
= _cpp_expand_op_stack (pfile
);
1432 top
->token
= op
.token
;
1436 /* The controlling macro expression is only valid if we called lex 3
1437 times: <!> <defined expression> and <EOF>. push_conditional ()
1438 checks that we are at top-of-file. */
1439 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1440 pfile
->mi_ind_cmacro
= 0;
1442 if (top
!= pfile
->op_stack
)
1444 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1445 "unbalanced stack in %s",
1446 is_if
? "#if" : "#elif");
1448 return false; /* Return false on syntax error. */
1451 return !num_zerop (top
->value
);
1454 /* Reduce the operator / value stack if possible, in preparation for
1455 pushing operator OP. Returns NULL on error, otherwise the top of
1458 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1462 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1465 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1469 if (op
== CPP_OPEN_PAREN
)
1472 /* Decrement the priority of left-associative operators to force a
1473 reduction with operators of otherwise equal priority. */
1474 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1475 while (prio
< optab
[top
->op
].prio
)
1477 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1478 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1479 check_promotion (pfile
, top
);
1487 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1488 top
[-1].loc
= top
->loc
;
1496 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1497 top
->value
, top
->op
);
1498 top
[-1].loc
= top
->loc
;
1503 case CPP_GREATER_EQ
:
1506 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1507 top
[-1].loc
= top
->loc
;
1513 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1514 top
[-1].loc
= top
->loc
;
1521 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1522 top
[-1].loc
= top
->loc
;
1526 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1527 top
[-1].loc
= top
->loc
;
1532 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1533 top
->value
, top
->op
, top
->loc
);
1534 top
[-1].loc
= top
->loc
;
1539 if (!num_zerop (top
->value
))
1540 pfile
->state
.skip_eval
--;
1541 top
->value
.low
= (!num_zerop (top
->value
)
1542 || !num_zerop (top
[1].value
));
1543 top
->value
.high
= 0;
1544 top
->value
.unsignedp
= false;
1545 top
->value
.overflow
= false;
1546 top
->loc
= top
[1].loc
;
1551 if (num_zerop (top
->value
))
1552 pfile
->state
.skip_eval
--;
1553 top
->value
.low
= (!num_zerop (top
->value
)
1554 && !num_zerop (top
[1].value
));
1555 top
->value
.high
= 0;
1556 top
->value
.unsignedp
= false;
1557 top
->value
.overflow
= false;
1558 top
->loc
= top
[1].loc
;
1561 case CPP_OPEN_PAREN
:
1562 if (op
!= CPP_CLOSE_PAREN
)
1564 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1565 top
->token
->src_loc
,
1566 0, "missing ')' in expression");
1570 top
->value
= top
[1].value
;
1571 top
->loc
= top
[1].loc
;
1576 if (!num_zerop (top
->value
))
1578 pfile
->state
.skip_eval
--;
1579 top
->value
= top
[1].value
;
1580 top
->loc
= top
[1].loc
;
1584 top
->value
= top
[2].value
;
1585 top
->loc
= top
[2].loc
;
1587 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1588 || top
[2].value
.unsignedp
);
1592 /* COMMA and COLON should not reduce a QUERY operator. */
1593 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1595 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1603 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1604 cpp_error (pfile
, CPP_DL_PEDWARN
,
1605 "integer overflow in preprocessor expression");
1608 if (op
== CPP_CLOSE_PAREN
)
1610 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1617 /* Returns the position of the old top of stack after expansion. */
1619 _cpp_expand_op_stack (cpp_reader
*pfile
)
1621 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1622 size_t new_size
= old_size
* 2 + 20;
1624 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1625 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1627 return pfile
->op_stack
+ old_size
;
1630 /* Emits a warning if the effective sign of either operand of OP
1631 changes because of integer promotions. */
1633 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1635 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1638 if (op
->value
.unsignedp
)
1640 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1641 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1642 "the left operand of \"%s\" changes sign when promoted",
1643 cpp_token_as_text (pfile
, op
->token
));
1645 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1646 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1647 "the right operand of \"%s\" changes sign when promoted",
1648 cpp_token_as_text (pfile
, op
->token
));
1651 /* Clears the unused high order bits of the number pointed to by PNUM. */
1653 num_trim (cpp_num num
, size_t precision
)
1655 if (precision
> PART_PRECISION
)
1657 precision
-= PART_PRECISION
;
1658 if (precision
< PART_PRECISION
)
1659 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1663 if (precision
< PART_PRECISION
)
1664 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1671 /* True iff A (presumed signed) >= 0. */
1673 num_positive (cpp_num num
, size_t precision
)
1675 if (precision
> PART_PRECISION
)
1677 precision
-= PART_PRECISION
;
1678 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1681 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1684 /* Sign extend a number, with PRECISION significant bits and all
1685 others assumed clear, to fill out a cpp_num structure. */
1687 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1691 if (precision
> PART_PRECISION
)
1693 precision
-= PART_PRECISION
;
1694 if (precision
< PART_PRECISION
1695 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1696 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1698 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1700 if (precision
< PART_PRECISION
)
1701 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1702 num
.high
= ~(cpp_num_part
) 0;
1709 /* Returns the negative of NUM. */
1711 num_negate (cpp_num num
, size_t precision
)
1716 num
.high
= ~num
.high
;
1720 num
= num_trim (num
, precision
);
1721 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1726 /* Returns true if A >= B. */
1728 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1732 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1736 /* Both numbers have signed type. If they are of different
1737 sign, the answer is the sign of A. */
1738 unsignedp
= num_positive (pa
, precision
);
1740 if (unsignedp
!= num_positive (pb
, precision
))
1743 /* Otherwise we can do an unsigned comparison. */
1746 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1749 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1751 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1752 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1754 lhs
.overflow
= false;
1755 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1757 /* As excess precision is zeroed, there is no need to num_trim () as
1758 these operations cannot introduce a set bit there. */
1762 lhs
.high
&= rhs
.high
;
1764 else if (op
== CPP_OR
)
1767 lhs
.high
|= rhs
.high
;
1772 lhs
.high
^= rhs
.high
;
1778 /* Returns LHS OP RHS, where OP is an inequality. */
1780 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1783 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1785 if (op
== CPP_GREATER_EQ
)
1787 else if (op
== CPP_LESS
)
1789 else if (op
== CPP_GREATER
)
1790 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1791 else /* CPP_LESS_EQ. */
1792 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1795 lhs
.overflow
= false;
1796 lhs
.unsignedp
= false;
1800 /* Returns LHS OP RHS, where OP is == or !=. */
1802 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1803 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1805 /* Work around a 3.0.4 bug; see PR 6950. */
1806 bool eq
= num_eq (lhs
, rhs
);
1807 if (op
== CPP_NOT_EQ
)
1811 lhs
.overflow
= false;
1812 lhs
.unsignedp
= false;
1816 /* Shift NUM, of width PRECISION, right by N bits. */
1818 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1820 cpp_num_part sign_mask
;
1821 bool x
= num_positive (num
, precision
);
1823 if (num
.unsignedp
|| x
)
1826 sign_mask
= ~(cpp_num_part
) 0;
1829 num
.high
= num
.low
= sign_mask
;
1833 if (precision
< PART_PRECISION
)
1834 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1835 else if (precision
< 2 * PART_PRECISION
)
1836 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1838 if (n
>= PART_PRECISION
)
1840 n
-= PART_PRECISION
;
1842 num
.high
= sign_mask
;
1847 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1848 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1852 num
= num_trim (num
, precision
);
1853 num
.overflow
= false;
1857 /* Shift NUM, of width PRECISION, left by N bits. */
1859 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1863 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1864 num
.high
= num
.low
= 0;
1868 cpp_num orig
, maybe_orig
;
1872 if (m
>= PART_PRECISION
)
1874 m
-= PART_PRECISION
;
1880 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1883 num
= num_trim (num
, precision
);
1886 num
.overflow
= false;
1889 maybe_orig
= num_rshift (num
, precision
, n
);
1890 num
.overflow
= !num_eq (orig
, maybe_orig
);
1897 /* The four unary operators: +, -, ! and ~. */
1899 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1904 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1905 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1906 "traditional C rejects the unary plus operator");
1907 num
.overflow
= false;
1911 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1915 num
.high
= ~num
.high
;
1917 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1918 num
.overflow
= false;
1921 default: /* case CPP_NOT: */
1922 num
.low
= num_zerop (num
);
1924 num
.overflow
= false;
1925 num
.unsignedp
= false;
1932 /* The various binary operators. */
1934 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1937 size_t precision
= CPP_OPTION (pfile
, precision
);
1945 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1947 /* A negative shift is a positive shift the other way. */
1948 if (op
== CPP_LSHIFT
)
1952 rhs
= num_negate (rhs
, precision
);
1955 n
= ~0; /* Maximal. */
1958 if (op
== CPP_LSHIFT
)
1959 lhs
= num_lshift (lhs
, precision
, n
);
1961 lhs
= num_rshift (lhs
, precision
, n
);
1966 result
.low
= lhs
.low
- rhs
.low
;
1967 result
.high
= lhs
.high
- rhs
.high
;
1968 if (result
.low
> lhs
.low
)
1970 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1971 result
.overflow
= false;
1973 result
= num_trim (result
, precision
);
1974 if (!result
.unsignedp
)
1976 bool lhsp
= num_positive (lhs
, precision
);
1977 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1978 && lhsp
!= num_positive (result
, precision
));
1983 result
.low
= lhs
.low
+ rhs
.low
;
1984 result
.high
= lhs
.high
+ rhs
.high
;
1985 if (result
.low
< lhs
.low
)
1987 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1988 result
.overflow
= false;
1990 result
= num_trim (result
, precision
);
1991 if (!result
.unsignedp
)
1993 bool lhsp
= num_positive (lhs
, precision
);
1994 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1995 && lhsp
!= num_positive (result
, precision
));
2000 default: /* case CPP_COMMA: */
2001 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
2002 || !pfile
->state
.skip_eval
))
2003 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2004 "comma operator in operand of #if");
2012 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2015 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
2018 cpp_num_part middle
[2], temp
;
2020 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
2021 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
2023 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
2024 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
2027 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
2028 if (result
.low
< temp
)
2032 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
2033 if (result
.low
< temp
)
2036 result
.high
+= HIGH_PART (middle
[0]);
2037 result
.high
+= HIGH_PART (middle
[1]);
2038 result
.unsignedp
= true;
2039 result
.overflow
= false;
2044 /* Multiply two preprocessing numbers. */
2046 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
2048 cpp_num result
, temp
;
2049 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2050 bool overflow
, negate
= false;
2051 size_t precision
= CPP_OPTION (pfile
, precision
);
2053 /* Prepare for unsigned multiplication. */
2056 if (!num_positive (lhs
, precision
))
2057 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
2058 if (!num_positive (rhs
, precision
))
2059 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2062 overflow
= lhs
.high
&& rhs
.high
;
2063 result
= num_part_mul (lhs
.low
, rhs
.low
);
2065 temp
= num_part_mul (lhs
.high
, rhs
.low
);
2066 result
.high
+= temp
.low
;
2070 temp
= num_part_mul (lhs
.low
, rhs
.high
);
2071 result
.high
+= temp
.low
;
2075 temp
.low
= result
.low
, temp
.high
= result
.high
;
2076 result
= num_trim (result
, precision
);
2077 if (!num_eq (result
, temp
))
2081 result
= num_negate (result
, precision
);
2084 result
.overflow
= false;
2086 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
2087 && !num_zerop (result
));
2088 result
.unsignedp
= unsignedp
;
2093 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2094 or the remainder depending upon OP. LOCATION is the source location
2095 of this operator (for diagnostics). */
2098 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
2099 location_t location
)
2101 cpp_num result
, sub
;
2103 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2104 bool negate
= false, lhs_neg
= false;
2105 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
2107 /* Prepare for unsigned division. */
2110 if (!num_positive (lhs
, precision
))
2111 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2112 if (!num_positive (rhs
, precision
))
2113 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2116 /* Find the high bit. */
2120 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2121 for (; ; i
--, mask
>>= 1)
2122 if (rhs
.high
& mask
)
2127 if (precision
> PART_PRECISION
)
2128 i
= precision
- PART_PRECISION
- 1;
2131 mask
= (cpp_num_part
) 1 << i
;
2132 for (; ; i
--, mask
>>= 1)
2138 if (!pfile
->state
.skip_eval
)
2139 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2140 "division by zero in #if");
2144 /* First nonzero bit of RHS is bit I. Do naive division by
2145 shifting the RHS fully left, and subtracting from LHS if LHS is
2146 at least as big, and then repeating but with one less shift.
2147 This is not very efficient, but is easy to understand. */
2149 rhs
.unsignedp
= true;
2150 lhs
.unsignedp
= true;
2151 i
= precision
- i
- 1;
2152 sub
= num_lshift (rhs
, precision
, i
);
2154 result
.high
= result
.low
= 0;
2157 if (num_greater_eq (lhs
, sub
, precision
))
2159 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2160 if (i
>= PART_PRECISION
)
2161 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2163 result
.low
|= (cpp_num_part
) 1 << i
;
2167 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2171 /* We divide so that the remainder has the sign of the LHS. */
2174 result
.unsignedp
= unsignedp
;
2175 result
.overflow
= false;
2179 result
= num_negate (result
, precision
);
2180 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2181 && !num_zerop (result
));
2188 lhs
.unsignedp
= unsignedp
;
2189 lhs
.overflow
= false;
2191 lhs
= num_negate (lhs
, precision
);