1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2019 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 static cpp_num
parse_has_include (cpp_reader
*, enum include_type
);
69 /* Token type abuse to create unary plus and minus operators. */
70 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
71 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
73 /* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
75 #define SYNTAX_ERROR(msgid) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
80 #define SYNTAX_ERROR_AT(loc, msgid) \
81 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
83 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
84 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
87 /* Subroutine of cpp_classify_number. S points to a float suffix of
88 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
89 flag vector (of CPP_N_* bits) describing the suffix. */
91 interpret_float_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
93 size_t orig_len
= len
;
94 const uchar
*orig_s
= s
;
96 size_t f
, d
, l
, w
, q
, i
, fn
, fnx
, fn_bits
;
99 f
= d
= l
= w
= q
= i
= fn
= fnx
= fn_bits
= 0;
101 /* The following decimal float suffixes, from TR 24732:2009, TS
102 18661-2:2015 and C2X, are supported:
106 dl, DL - _Decimal128.
108 The dN and DN suffixes for _DecimalN, and dNx and DNx for
109 _DecimalNx, defined in TS 18661-3:2015, are not supported.
111 Fixed-point suffixes, from TR 18037:2008, are supported. They
112 consist of three parts, in order:
114 (i) An optional u or U, for unsigned types.
116 (ii) An optional h or H, for short types, or l or L, for long
117 types, or ll or LL, for long long types. Use of ll or LL is a
120 (iii) r or R, for _Fract types, or k or K, for _Accum types.
122 Otherwise the suffix is for a binary or standard floating-point
123 type. Such a suffix, or the absence of a suffix, may be preceded
124 or followed by i, I, j or J, to indicate an imaginary number with
125 the corresponding complex type. The following suffixes for
126 binary or standard floating-point types are supported:
128 f, F - float (ISO C and C++).
129 l, L - long double (ISO C and C++).
130 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
131 operation (from TR 24732:2009; the pragma and the suffix
132 are not included in TS 18661-2:2015).
133 w, W - machine-specific type such as __float80 (GNU extension).
134 q, Q - machine-specific type such as __float128 (GNU extension).
135 fN, FN - _FloatN (TS 18661-3:2015).
136 fNx, FNx - _FloatNx (TS 18661-3:2015). */
138 /* Process decimal float suffixes, which are two letters starting
139 with d or D. Order and case are significant. */
140 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
142 bool uppercase
= (*s
== 'D');
145 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
146 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
147 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
148 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
149 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
150 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
152 /* Additional two-character suffixes beginning with D are not
153 for decimal float constants. */
158 if (CPP_OPTION (pfile
, ext_numeric_literals
))
160 /* Recognize a fixed-point suffix. */
164 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
165 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
169 /* Continue processing a fixed-point suffix. The suffix is case
170 insensitive except for ll or LL. Order is significant. */
177 if (*s
== 'u' || *s
== 'U')
179 flags
|= CPP_N_UNSIGNED
;
190 return flags
|= CPP_N_SMALL
;
194 return flags
|= CPP_N_MEDIUM
;
195 if (len
== 2 && s
[1] == 'l')
196 return flags
|= CPP_N_LARGE
;
200 return flags
|= CPP_N_MEDIUM
;
201 if (len
== 2 && s
[1] == 'L')
202 return flags
|= CPP_N_LARGE
;
207 /* Anything left at this point is invalid. */
212 /* In any remaining valid suffix, the case and order don't matter. */
220 && !CPP_OPTION (pfile
, cplusplus
)
229 && fn_bits
< CPP_FLOATN_MAX
)
231 fn_bits
= fn_bits
* 10 + (s
[1] - '0');
235 if (len
> 0 && s
[1] == 'x')
245 case 'd': case 'D': d
++; break;
246 case 'l': case 'L': l
++; break;
247 case 'w': case 'W': w
++; break;
248 case 'q': case 'Q': q
++; break;
250 case 'j': case 'J': i
++; break;
257 /* Reject any case of multiple suffixes specifying types, multiple
258 suffixes specifying an imaginary constant, _FloatN or _FloatNx
259 suffixes for invalid values of N, and _FloatN suffixes for values
260 of N larger than can be represented in the return value. The
261 caller is responsible for rejecting _FloatN suffixes where
262 _FloatN is not supported on the chosen target. */
263 if (f
+ d
+ l
+ w
+ q
+ fn
+ fnx
> 1 || i
> 1)
265 if (fn_bits
> CPP_FLOATN_MAX
)
267 if (fnx
&& fn_bits
!= 32 && fn_bits
!= 64 && fn_bits
!= 128)
269 if (fn
&& fn_bits
!= 16 && fn_bits
% 32 != 0)
271 if (fn
&& fn_bits
== 96)
276 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
279 /* In C++14 and up these suffixes are in the standard library, so treat
280 them as user-defined literals. */
281 if (CPP_OPTION (pfile
, cplusplus
)
282 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
286 && (orig_s
[1] == 'f' || orig_s
[1] == 'l'))))
290 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
293 return ((i
? CPP_N_IMAGINARY
: 0)
299 fn
? CPP_N_FLOATN
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
300 fnx
? CPP_N_FLOATNX
| (fn_bits
<< CPP_FLOATN_SHIFT
) :
304 /* Return the classification flags for a float suffix. */
306 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
308 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
311 /* Subroutine of cpp_classify_number. S points to an integer suffix
312 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
313 flag vector describing the suffix. */
315 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
317 size_t orig_len
= len
;
325 case 'u': case 'U': u
++; break;
327 case 'j': case 'J': i
++; break;
328 case 'l': case 'L': l
++;
329 /* If there are two Ls, they must be adjacent and the same case. */
330 if (l
== 2 && s
[len
] != s
[len
+ 1])
337 if (l
> 2 || u
> 1 || i
> 1)
342 if (!CPP_OPTION (pfile
, ext_numeric_literals
))
345 /* In C++14 and up these suffixes are in the standard library, so treat
346 them as user-defined literals. */
347 if (CPP_OPTION (pfile
, cplusplus
)
348 && CPP_OPTION (pfile
, lang
) > CLK_CXX11
350 && (orig_len
== 1 || (orig_len
== 2 && s
[1] == 'l')))
354 return ((i
? CPP_N_IMAGINARY
: 0)
355 | (u
? CPP_N_UNSIGNED
: 0)
356 | ((l
== 0) ? CPP_N_SMALL
357 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
360 /* Return the classification flags for an int suffix. */
362 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
364 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
367 /* Return the string type corresponding to the the input user-defined string
368 literal type. If the input type is not a user-defined string literal
369 type return the input type. */
371 cpp_userdef_string_remove_type (enum cpp_ttype type
)
373 if (type
== CPP_STRING_USERDEF
)
375 else if (type
== CPP_WSTRING_USERDEF
)
377 else if (type
== CPP_STRING16_USERDEF
)
379 else if (type
== CPP_STRING32_USERDEF
)
381 else if (type
== CPP_UTF8STRING_USERDEF
)
382 return CPP_UTF8STRING
;
387 /* Return the user-defined string literal type corresponding to the input
388 string type. If the input type is not a string type return the input
391 cpp_userdef_string_add_type (enum cpp_ttype type
)
393 if (type
== CPP_STRING
)
394 return CPP_STRING_USERDEF
;
395 else if (type
== CPP_WSTRING
)
396 return CPP_WSTRING_USERDEF
;
397 else if (type
== CPP_STRING16
)
398 return CPP_STRING16_USERDEF
;
399 else if (type
== CPP_STRING32
)
400 return CPP_STRING32_USERDEF
;
401 else if (type
== CPP_UTF8STRING
)
402 return CPP_UTF8STRING_USERDEF
;
407 /* Return the char type corresponding to the the input user-defined char
408 literal type. If the input type is not a user-defined char literal
409 type return the input type. */
411 cpp_userdef_char_remove_type (enum cpp_ttype type
)
413 if (type
== CPP_CHAR_USERDEF
)
415 else if (type
== CPP_WCHAR_USERDEF
)
417 else if (type
== CPP_CHAR16_USERDEF
)
419 else if (type
== CPP_CHAR32_USERDEF
)
421 else if (type
== CPP_UTF8CHAR_USERDEF
)
427 /* Return the user-defined char literal type corresponding to the input
428 char type. If the input type is not a char type return the input
431 cpp_userdef_char_add_type (enum cpp_ttype type
)
433 if (type
== CPP_CHAR
)
434 return CPP_CHAR_USERDEF
;
435 else if (type
== CPP_WCHAR
)
436 return CPP_WCHAR_USERDEF
;
437 else if (type
== CPP_CHAR16
)
438 return CPP_CHAR16_USERDEF
;
439 else if (type
== CPP_CHAR32
)
440 return CPP_CHAR32_USERDEF
;
441 else if (type
== CPP_UTF8CHAR
)
442 return CPP_UTF8CHAR_USERDEF
;
447 /* Return true if the token type is a user-defined string literal. */
449 cpp_userdef_string_p (enum cpp_ttype type
)
451 if (type
== CPP_STRING_USERDEF
452 || type
== CPP_WSTRING_USERDEF
453 || type
== CPP_STRING16_USERDEF
454 || type
== CPP_STRING32_USERDEF
455 || type
== CPP_UTF8STRING_USERDEF
)
461 /* Return true if the token type is a user-defined char literal. */
463 cpp_userdef_char_p (enum cpp_ttype type
)
465 if (type
== CPP_CHAR_USERDEF
466 || type
== CPP_WCHAR_USERDEF
467 || type
== CPP_CHAR16_USERDEF
468 || type
== CPP_CHAR32_USERDEF
469 || type
== CPP_UTF8CHAR_USERDEF
)
475 /* Extract the suffix from a user-defined literal string or char. */
477 cpp_get_userdef_suffix (const cpp_token
*tok
)
479 unsigned int len
= tok
->val
.str
.len
;
480 const char *text
= (const char *)tok
->val
.str
.text
;
483 for (i
= 0; i
< len
; ++i
)
484 if (text
[i
] == '\'' || text
[i
] == '"')
489 for (i
= len
; i
> 0; --i
)
490 if (text
[i
- 1] == delim
)
495 /* Categorize numeric constants according to their field (integer,
496 floating point, or invalid), radix (decimal, octal, hexadecimal),
499 TOKEN is the token that represents the numeric constant to
502 In C++0X if UD_SUFFIX is non null it will be assigned
503 any unrecognized suffix for a user-defined literal.
505 VIRTUAL_LOCATION is the virtual location for TOKEN. */
507 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
508 const char **ud_suffix
, location_t virtual_location
)
510 const uchar
*str
= token
->val
.str
.text
;
512 unsigned int max_digit
, result
, radix
;
513 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
520 /* If the lexer has done its job, length one can only be a single
521 digit. Fast-path this very common case. */
522 if (token
->val
.str
.len
== 1)
523 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
525 limit
= str
+ token
->val
.str
.len
;
526 float_flag
= NOT_FLOAT
;
530 seen_digit_sep
= false;
532 /* First, interpret the radix. */
538 /* Require at least one hex digit to classify it as hex. */
539 if (*str
== 'x' || *str
== 'X')
541 if (str
[1] == '.' || ISXDIGIT (str
[1]))
546 else if (DIGIT_SEP (str
[1]))
547 SYNTAX_ERROR_AT (virtual_location
,
548 "digit separator after base indicator");
550 else if (*str
== 'b' || *str
== 'B')
552 if (str
[1] == '0' || str
[1] == '1')
557 else if (DIGIT_SEP (str
[1]))
558 SYNTAX_ERROR_AT (virtual_location
,
559 "digit separator after base indicator");
563 /* Now scan for a well-formed integer or float. */
566 unsigned int c
= *str
++;
568 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
570 seen_digit_sep
= false;
576 else if (DIGIT_SEP (c
))
579 SYNTAX_ERROR_AT (virtual_location
, "adjacent digit separators");
580 seen_digit_sep
= true;
584 if (seen_digit_sep
|| DIGIT_SEP (*str
))
585 SYNTAX_ERROR_AT (virtual_location
,
586 "digit separator adjacent to decimal point");
587 seen_digit_sep
= false;
588 if (float_flag
== NOT_FLOAT
)
589 float_flag
= AFTER_POINT
;
591 SYNTAX_ERROR_AT (virtual_location
,
592 "too many decimal points in number");
594 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
595 || (radix
== 16 && (c
== 'p' || c
== 'P')))
597 if (seen_digit_sep
|| DIGIT_SEP (*str
))
598 SYNTAX_ERROR_AT (virtual_location
,
599 "digit separator adjacent to exponent");
600 float_flag
= AFTER_EXPON
;
605 /* Start of suffix. */
611 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
612 SYNTAX_ERROR_AT (virtual_location
,
613 "digit separator outside digit sequence");
615 /* The suffix may be for decimal fixed-point constants without exponent. */
616 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
618 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
619 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
621 result
|= CPP_N_FLOATING
;
622 /* We need to restore the radix to 10, if the radix is 8. */
626 if (CPP_PEDANTIC (pfile
))
627 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
628 "fixed-point constants are a GCC extension");
635 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
638 if (max_digit
>= radix
)
641 SYNTAX_ERROR2_AT (virtual_location
,
642 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
644 SYNTAX_ERROR2_AT (virtual_location
,
645 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
648 if (float_flag
!= NOT_FLOAT
)
652 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
653 "invalid prefix \"0b\" for floating constant");
654 return CPP_N_INVALID
;
657 if (radix
== 16 && !seen_digit
)
658 SYNTAX_ERROR_AT (virtual_location
,
659 "no digits in hexadecimal floating constant");
661 if (radix
== 16 && CPP_PEDANTIC (pfile
)
662 && !CPP_OPTION (pfile
, extended_numbers
))
664 if (CPP_OPTION (pfile
, cplusplus
))
665 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
666 "use of C++17 hexadecimal floating constant");
668 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
669 "use of C99 hexadecimal floating constant");
672 if (float_flag
== AFTER_EXPON
)
674 if (*str
== '+' || *str
== '-')
677 /* Exponent is decimal, even if string is a hex float. */
680 if (DIGIT_SEP (*str
))
681 SYNTAX_ERROR_AT (virtual_location
,
682 "digit separator adjacent to exponent");
684 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
688 seen_digit_sep
= DIGIT_SEP (*str
);
691 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
693 else if (radix
== 16)
694 SYNTAX_ERROR_AT (virtual_location
,
695 "hexadecimal floating constants require an exponent");
698 SYNTAX_ERROR_AT (virtual_location
,
699 "digit separator outside digit sequence");
701 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
704 if (CPP_OPTION (pfile
, user_literals
))
707 *ud_suffix
= (const char *) str
;
708 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
712 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
713 "invalid suffix \"%.*s\" on floating constant",
714 (int) (limit
- str
), str
);
715 return CPP_N_INVALID
;
719 /* Traditional C didn't accept any floating suffixes. */
721 && CPP_WTRADITIONAL (pfile
)
722 && ! cpp_sys_macro_p (pfile
))
723 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
724 "traditional C rejects the \"%.*s\" suffix",
725 (int) (limit
- str
), str
);
727 /* A suffix for double is a GCC extension via decimal float support.
728 If the suffix also specifies an imaginary value we'll catch that
730 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
731 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
732 "suffix for double constant is a GCC extension");
734 /* Radix must be 10 for decimal floats. */
735 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
737 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
738 "invalid suffix \"%.*s\" with hexadecimal floating constant",
739 (int) (limit
- str
), str
);
740 return CPP_N_INVALID
;
743 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
744 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
745 "fixed-point constants are a GCC extension");
747 if (result
& CPP_N_DFLOAT
)
749 if (CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, dfp_constants
))
750 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
751 "decimal float constants are a C2X feature");
752 else if (CPP_OPTION (pfile
, cpp_warn_c11_c2x_compat
) > 0)
753 cpp_warning_with_line (pfile
, CPP_W_C11_C2X_COMPAT
,
755 "decimal float constants are a C2X feature");
758 result
|= CPP_N_FLOATING
;
762 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
765 if (CPP_OPTION (pfile
, user_literals
))
768 *ud_suffix
= (const char *) str
;
769 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
773 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
774 "invalid suffix \"%.*s\" on integer constant",
775 (int) (limit
- str
), str
);
776 return CPP_N_INVALID
;
780 /* Traditional C only accepted the 'L' suffix.
781 Suppress warning about 'LL' with -Wno-long-long. */
782 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
784 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
785 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
786 && CPP_OPTION (pfile
, cpp_warn_long_long
);
789 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
791 "traditional C rejects the \"%.*s\" suffix",
792 (int) (limit
- str
), str
);
795 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
796 && CPP_OPTION (pfile
, cpp_warn_long_long
))
798 const char *message
= CPP_OPTION (pfile
, cplusplus
)
799 ? N_("use of C++11 long long integer constant")
800 : N_("use of C99 long long integer constant");
802 if (CPP_OPTION (pfile
, c99
))
803 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
806 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
807 virtual_location
, 0, message
);
810 result
|= CPP_N_INTEGER
;
814 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
815 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
816 "imaginary constants are a GCC extension");
818 && !CPP_OPTION (pfile
, binary_constants
)
819 && CPP_PEDANTIC (pfile
))
820 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
821 CPP_OPTION (pfile
, cplusplus
)
822 ? N_("binary constants are a C++14 feature "
824 : N_("binary constants are a GCC extension"));
827 result
|= CPP_N_DECIMAL
;
828 else if (radix
== 16)
831 result
|= CPP_N_BINARY
;
833 result
|= CPP_N_OCTAL
;
838 return CPP_N_INVALID
;
841 /* cpp_interpret_integer converts an integer constant into a cpp_num,
842 of precision options->precision.
844 We do not provide any interface for decimal->float conversion,
845 because the preprocessor doesn't need it and we don't want to
846 drag in GCC's floating point emulator. */
848 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
851 const uchar
*p
, *end
;
856 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
857 result
.overflow
= false;
859 p
= token
->val
.str
.text
;
860 end
= p
+ token
->val
.str
.len
;
862 /* Common case of a single digit. */
863 if (token
->val
.str
.len
== 1)
864 result
.low
= p
[0] - '0';
868 size_t precision
= CPP_OPTION (pfile
, precision
);
869 unsigned int base
= 10, c
= 0;
870 bool overflow
= false;
872 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
877 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
882 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
888 /* We can add a digit to numbers strictly less than this without
889 needing the precision and slowness of double integers. */
890 max
= ~(cpp_num_part
) 0;
891 if (precision
< PART_PRECISION
)
892 max
>>= PART_PRECISION
- precision
;
893 max
= (max
- base
+ 1) / base
+ 1;
899 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
901 else if (DIGIT_SEP (c
))
906 /* Strict inequality for when max is set to zero. */
907 if (result
.low
< max
)
908 result
.low
= result
.low
* base
+ c
;
911 result
= append_digit (result
, c
, base
, precision
);
912 overflow
|= result
.overflow
;
917 if (overflow
&& !(type
& CPP_N_USERDEF
))
918 cpp_error (pfile
, CPP_DL_PEDWARN
,
919 "integer constant is too large for its type");
920 /* If too big to be signed, consider it unsigned. Only warn for
921 decimal numbers. Traditional numbers were always signed (but
922 we still honor an explicit U suffix); but we only have
923 traditional semantics in directives. */
924 else if (!result
.unsignedp
925 && !(CPP_OPTION (pfile
, traditional
)
926 && pfile
->state
.in_directive
)
927 && !num_positive (result
, precision
))
929 /* This is for constants within the range of uintmax_t but
930 not that of intmax_t. For such decimal constants, a
931 diagnostic is required for C99 as the selected type must
932 be signed and not having a type is a constraint violation
933 (DR#298, TC3), so this must be a pedwarn. For C90,
934 unsigned long is specified to be used for a constant that
935 does not fit in signed long; if uintmax_t has the same
936 range as unsigned long this means only a warning is
937 appropriate here. C90 permits the preprocessor to use a
938 wider range than unsigned long in the compiler, so if
939 uintmax_t is wider than unsigned long no diagnostic is
940 required for such constants in preprocessor #if
941 expressions and the compiler will pedwarn for such
942 constants outside the range of unsigned long that reach
943 the compiler so a diagnostic is not required there
944 either; thus, pedwarn for C99 but use a plain warning for
947 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
950 "integer constant is so large that it is unsigned");
951 result
.unsignedp
= true;
958 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
960 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
965 cpp_num_part add_high
, add_low
;
967 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
968 need to worry about add_high overflowing. */
982 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
983 result
.high
= num
.high
<< shift
;
984 result
.low
= num
.low
<< shift
;
985 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
986 result
.unsignedp
= num
.unsignedp
;
990 add_low
= num
.low
<< 1;
991 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
994 add_high
= add_low
= 0;
996 if (add_low
+ digit
< add_low
)
1000 if (result
.low
+ add_low
< result
.low
)
1002 if (result
.high
+ add_high
< result
.high
)
1005 result
.low
+= add_low
;
1006 result
.high
+= add_high
;
1007 result
.overflow
= overflow
;
1009 /* The above code catches overflow of a cpp_num type. This catches
1010 overflow of the (possibly shorter) target precision. */
1011 num
.low
= result
.low
;
1012 num
.high
= result
.high
;
1013 result
= num_trim (result
, precision
);
1014 if (!num_eq (result
, num
))
1015 result
.overflow
= true;
1020 /* Handle meeting "defined" in a preprocessor expression. */
1022 parse_defined (cpp_reader
*pfile
)
1026 cpp_hashnode
*node
= 0;
1027 const cpp_token
*token
;
1028 cpp_context
*initial_context
= pfile
->context
;
1030 /* Don't expand macros. */
1031 pfile
->state
.prevent_expansion
++;
1033 token
= cpp_get_token (pfile
);
1034 if (token
->type
== CPP_OPEN_PAREN
)
1037 token
= cpp_get_token (pfile
);
1040 if (token
->type
== CPP_NAME
)
1042 node
= token
->val
.node
.node
;
1043 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
1045 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
1051 cpp_error (pfile
, CPP_DL_ERROR
,
1052 "operator \"defined\" requires an identifier");
1053 if (token
->flags
& NAMED_OP
)
1058 op
.type
= token
->type
;
1059 cpp_error (pfile
, CPP_DL_ERROR
,
1060 "(\"%s\" is an alternative token for \"%s\" in C++)",
1061 cpp_token_as_text (pfile
, token
),
1062 cpp_token_as_text (pfile
, &op
));
1068 if ((pfile
->context
!= initial_context
1069 || initial_context
!= &pfile
->base_context
)
1070 && CPP_OPTION (pfile
, warn_expansion_to_defined
))
1071 cpp_pedwarning (pfile
, CPP_W_EXPANSION_TO_DEFINED
,
1072 "this use of \"defined\" may not be portable");
1074 _cpp_mark_macro_used (node
);
1075 _cpp_maybe_notify_macro_use (pfile
, node
);
1077 /* A possible controlling macro of the form #if !defined ().
1078 _cpp_parse_expr checks there was no other junk on the line. */
1079 pfile
->mi_ind_cmacro
= node
;
1082 pfile
->state
.prevent_expansion
--;
1084 /* Do not treat conditional macros as being defined. This is due to the
1085 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1086 to act as conditional keywords. This messes up tests like #ifndef
1088 result
.unsignedp
= false;
1090 result
.overflow
= false;
1091 result
.low
= (node
&& cpp_macro_p (node
)
1092 && !(node
->flags
& NODE_CONDITIONAL
));
1096 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1097 number or character constant, or the result of the "defined" or "#"
1100 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
1101 location_t virtual_location
)
1107 result
.unsignedp
= false;
1108 result
.overflow
= false;
1110 switch (token
->type
)
1113 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1114 if (temp
& CPP_N_USERDEF
)
1115 cpp_error (pfile
, CPP_DL_ERROR
,
1116 "user-defined literal in preprocessor expression");
1117 switch (temp
& CPP_N_CATEGORY
)
1119 case CPP_N_FLOATING
:
1120 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1121 "floating constant in preprocessor expression");
1124 if (!(temp
& CPP_N_IMAGINARY
))
1125 return cpp_interpret_integer (pfile
, token
, temp
);
1126 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1127 "imaginary number in preprocessor expression");
1131 /* Error already issued. */
1134 result
.high
= result
.low
= 0;
1143 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1148 /* Sign-extend the result if necessary. */
1149 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1151 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1152 result
.low
|= ~(~(cpp_num_part
) 0
1153 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1154 result
.high
= ~(cpp_num_part
) 0;
1155 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1161 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1162 return parse_defined (pfile
);
1163 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include__
)
1164 return parse_has_include (pfile
, IT_INCLUDE
);
1165 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include_next__
)
1166 return parse_has_include (pfile
, IT_INCLUDE_NEXT
);
1167 else if (CPP_OPTION (pfile
, cplusplus
)
1168 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1169 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1172 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1178 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1179 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1180 "\"%s\" is not defined, evaluates to 0",
1181 NODE_NAME (token
->val
.node
.node
));
1186 if (!pfile
->state
.skipping
)
1188 /* A pedantic warning takes precedence over a deprecated
1190 if (CPP_PEDANTIC (pfile
))
1191 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1192 virtual_location
, 0,
1193 "assertions are a GCC extension");
1194 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1195 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1196 "assertions are a deprecated extension");
1198 _cpp_test_assertion (pfile
, &temp
);
1207 result
.unsignedp
= !!unsignedp
;
1211 /* Operator precedence and flags table.
1213 After an operator is returned from the lexer, if it has priority less
1214 than the operator on the top of the stack, we reduce the stack by one
1215 operator and repeat the test. Since equal priorities do not reduce,
1216 this is naturally right-associative.
1218 We handle left-associative operators by decrementing the priority of
1219 just-lexed operators by one, but retaining the priority of operators
1220 already on the stack.
1222 The remaining cases are '(' and ')'. We handle '(' by skipping the
1223 reduction phase completely. ')' is given lower priority than
1224 everything else, including '(', effectively forcing a reduction of the
1225 parenthesized expression. If there is a matching '(', the routine
1226 reduce() exits immediately. If the normal exit route sees a ')', then
1227 there cannot have been a matching '(' and an error message is output.
1229 The parser assumes all shifted operators require a left operand unless
1230 the flag NO_L_OPERAND is set. These semantics are automatic; any
1231 extra semantics need to be handled with operator-specific code. */
1233 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1234 operand changes because of integer promotions. */
1235 #define NO_L_OPERAND (1 << 0)
1236 #define LEFT_ASSOC (1 << 1)
1237 #define CHECK_PROMOTION (1 << 2)
1239 /* Operator to priority map. Must be in the same order as the first
1240 N entries of enum cpp_ttype. */
1241 static const struct cpp_operator
1247 /* EQ */ {0, 0}, /* Shouldn't happen. */
1248 /* NOT */ {16, NO_L_OPERAND
},
1249 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1250 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1251 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1252 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1253 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1254 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1255 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1256 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1257 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1258 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1259 /* RSHIFT */ {13, LEFT_ASSOC
},
1260 /* LSHIFT */ {13, LEFT_ASSOC
},
1262 /* COMPL */ {16, NO_L_OPERAND
},
1263 /* AND_AND */ {6, LEFT_ASSOC
},
1264 /* OR_OR */ {5, LEFT_ASSOC
},
1265 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1266 However, there are some special cases for these in reduce(). */
1268 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1269 /* COMMA */ {4, LEFT_ASSOC
},
1270 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1271 /* CLOSE_PAREN */ {0, 0},
1273 /* EQ_EQ */ {11, LEFT_ASSOC
},
1274 /* NOT_EQ */ {11, LEFT_ASSOC
},
1275 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1276 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1277 /* UPLUS */ {16, NO_L_OPERAND
},
1278 /* UMINUS */ {16, NO_L_OPERAND
}
1281 /* Parse and evaluate a C expression, reading from PFILE.
1282 Returns the truth value of the expression.
1284 The implementation is an operator precedence parser, i.e. a
1285 bottom-up parser, using a stack for not-yet-reduced tokens.
1287 The stack base is op_stack, and the current stack pointer is 'top'.
1288 There is a stack element for each operator (only), and the most
1289 recently pushed operator is 'top->op'. An operand (value) is
1290 stored in the 'value' field of the stack element of the operator
1291 that precedes it. */
1293 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1295 struct op
*top
= pfile
->op_stack
;
1296 unsigned int lex_count
;
1297 bool saw_leading_not
, want_value
= true;
1298 location_t virtual_location
= 0;
1300 pfile
->state
.skip_eval
= 0;
1302 /* Set up detection of #if ! defined(). */
1303 pfile
->mi_ind_cmacro
= 0;
1304 saw_leading_not
= false;
1307 /* Lowest priority operator prevents further reductions. */
1315 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1316 op
.op
= op
.token
->type
;
1317 op
.loc
= virtual_location
;
1321 /* These tokens convert into values. */
1331 SYNTAX_ERROR2_AT (op
.loc
,
1332 "missing binary operator before token \"%s\"",
1333 cpp_token_as_text (pfile
, op
.token
));
1335 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1339 saw_leading_not
= lex_count
== 1;
1351 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1352 SYNTAX_ERROR2_AT (op
.loc
,
1353 "token \"%s\" is not valid in preprocessor expressions",
1354 cpp_token_as_text (pfile
, op
.token
));
1358 /* Check we have a value or operator as appropriate. */
1359 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1362 SYNTAX_ERROR2_AT (op
.loc
,
1363 "missing binary operator before token \"%s\"",
1364 cpp_token_as_text (pfile
, op
.token
));
1366 else if (want_value
)
1368 /* We want a number (or expression) and haven't got one.
1369 Try to emit a specific diagnostic. */
1370 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1371 SYNTAX_ERROR_AT (op
.loc
,
1372 "missing expression between '(' and ')'");
1374 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1375 SYNTAX_ERROR2_AT (op
.loc
,
1376 "%s with no expression", is_if
? "#if" : "#elif");
1378 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1379 SYNTAX_ERROR2_AT (op
.loc
,
1380 "operator '%s' has no right operand",
1381 cpp_token_as_text (pfile
, top
->token
));
1382 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1383 /* Complain about missing paren during reduction. */;
1385 SYNTAX_ERROR2_AT (op
.loc
,
1386 "operator '%s' has no left operand",
1387 cpp_token_as_text (pfile
, op
.token
));
1390 top
= reduce (pfile
, top
, op
.op
);
1394 if (op
.op
== CPP_EOF
)
1399 case CPP_CLOSE_PAREN
:
1402 if (!num_zerop (top
->value
))
1403 pfile
->state
.skip_eval
++;
1407 if (num_zerop (top
->value
))
1408 pfile
->state
.skip_eval
++;
1411 if (top
->op
!= CPP_QUERY
)
1412 SYNTAX_ERROR_AT (op
.loc
,
1413 " ':' without preceding '?'");
1414 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1415 pfile
->state
.skip_eval
++;
1417 pfile
->state
.skip_eval
--;
1424 /* Check for and handle stack overflow. */
1425 if (++top
== pfile
->op_limit
)
1426 top
= _cpp_expand_op_stack (pfile
);
1429 top
->token
= op
.token
;
1433 /* The controlling macro expression is only valid if we called lex 3
1434 times: <!> <defined expression> and <EOF>. push_conditional ()
1435 checks that we are at top-of-file. */
1436 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1437 pfile
->mi_ind_cmacro
= 0;
1439 if (top
!= pfile
->op_stack
)
1441 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1442 "unbalanced stack in %s",
1443 is_if
? "#if" : "#elif");
1445 return false; /* Return false on syntax error. */
1448 return !num_zerop (top
->value
);
1451 /* Reduce the operator / value stack if possible, in preparation for
1452 pushing operator OP. Returns NULL on error, otherwise the top of
1455 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1459 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1462 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1466 if (op
== CPP_OPEN_PAREN
)
1469 /* Decrement the priority of left-associative operators to force a
1470 reduction with operators of otherwise equal priority. */
1471 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1472 while (prio
< optab
[top
->op
].prio
)
1474 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1475 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1476 check_promotion (pfile
, top
);
1484 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1485 top
[-1].loc
= top
->loc
;
1493 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1494 top
->value
, top
->op
);
1495 top
[-1].loc
= top
->loc
;
1500 case CPP_GREATER_EQ
:
1503 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1504 top
[-1].loc
= top
->loc
;
1510 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1511 top
[-1].loc
= top
->loc
;
1518 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1519 top
[-1].loc
= top
->loc
;
1523 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1524 top
[-1].loc
= top
->loc
;
1529 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1530 top
->value
, top
->op
, top
->loc
);
1531 top
[-1].loc
= top
->loc
;
1536 if (!num_zerop (top
->value
))
1537 pfile
->state
.skip_eval
--;
1538 top
->value
.low
= (!num_zerop (top
->value
)
1539 || !num_zerop (top
[1].value
));
1540 top
->value
.high
= 0;
1541 top
->value
.unsignedp
= false;
1542 top
->value
.overflow
= false;
1543 top
->loc
= top
[1].loc
;
1548 if (num_zerop (top
->value
))
1549 pfile
->state
.skip_eval
--;
1550 top
->value
.low
= (!num_zerop (top
->value
)
1551 && !num_zerop (top
[1].value
));
1552 top
->value
.high
= 0;
1553 top
->value
.unsignedp
= false;
1554 top
->value
.overflow
= false;
1555 top
->loc
= top
[1].loc
;
1558 case CPP_OPEN_PAREN
:
1559 if (op
!= CPP_CLOSE_PAREN
)
1561 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1562 top
->token
->src_loc
,
1563 0, "missing ')' in expression");
1567 top
->value
= top
[1].value
;
1568 top
->loc
= top
[1].loc
;
1573 if (!num_zerop (top
->value
))
1575 pfile
->state
.skip_eval
--;
1576 top
->value
= top
[1].value
;
1577 top
->loc
= top
[1].loc
;
1581 top
->value
= top
[2].value
;
1582 top
->loc
= top
[2].loc
;
1584 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1585 || top
[2].value
.unsignedp
);
1589 /* COMMA and COLON should not reduce a QUERY operator. */
1590 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1592 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1600 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1601 cpp_error (pfile
, CPP_DL_PEDWARN
,
1602 "integer overflow in preprocessor expression");
1605 if (op
== CPP_CLOSE_PAREN
)
1607 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1614 /* Returns the position of the old top of stack after expansion. */
1616 _cpp_expand_op_stack (cpp_reader
*pfile
)
1618 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1619 size_t new_size
= old_size
* 2 + 20;
1621 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1622 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1624 return pfile
->op_stack
+ old_size
;
1627 /* Emits a warning if the effective sign of either operand of OP
1628 changes because of integer promotions. */
1630 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1632 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1635 if (op
->value
.unsignedp
)
1637 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1638 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1639 "the left operand of \"%s\" changes sign when promoted",
1640 cpp_token_as_text (pfile
, op
->token
));
1642 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1643 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1644 "the right operand of \"%s\" changes sign when promoted",
1645 cpp_token_as_text (pfile
, op
->token
));
1648 /* Clears the unused high order bits of the number pointed to by PNUM. */
1650 num_trim (cpp_num num
, size_t precision
)
1652 if (precision
> PART_PRECISION
)
1654 precision
-= PART_PRECISION
;
1655 if (precision
< PART_PRECISION
)
1656 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1660 if (precision
< PART_PRECISION
)
1661 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1668 /* True iff A (presumed signed) >= 0. */
1670 num_positive (cpp_num num
, size_t precision
)
1672 if (precision
> PART_PRECISION
)
1674 precision
-= PART_PRECISION
;
1675 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1678 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1681 /* Sign extend a number, with PRECISION significant bits and all
1682 others assumed clear, to fill out a cpp_num structure. */
1684 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1688 if (precision
> PART_PRECISION
)
1690 precision
-= PART_PRECISION
;
1691 if (precision
< PART_PRECISION
1692 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1693 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1695 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1697 if (precision
< PART_PRECISION
)
1698 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1699 num
.high
= ~(cpp_num_part
) 0;
1706 /* Returns the negative of NUM. */
1708 num_negate (cpp_num num
, size_t precision
)
1713 num
.high
= ~num
.high
;
1717 num
= num_trim (num
, precision
);
1718 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1723 /* Returns true if A >= B. */
1725 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1729 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1733 /* Both numbers have signed type. If they are of different
1734 sign, the answer is the sign of A. */
1735 unsignedp
= num_positive (pa
, precision
);
1737 if (unsignedp
!= num_positive (pb
, precision
))
1740 /* Otherwise we can do an unsigned comparison. */
1743 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1746 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1748 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1749 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1751 lhs
.overflow
= false;
1752 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1754 /* As excess precision is zeroed, there is no need to num_trim () as
1755 these operations cannot introduce a set bit there. */
1759 lhs
.high
&= rhs
.high
;
1761 else if (op
== CPP_OR
)
1764 lhs
.high
|= rhs
.high
;
1769 lhs
.high
^= rhs
.high
;
1775 /* Returns LHS OP RHS, where OP is an inequality. */
1777 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1780 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1782 if (op
== CPP_GREATER_EQ
)
1784 else if (op
== CPP_LESS
)
1786 else if (op
== CPP_GREATER
)
1787 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1788 else /* CPP_LESS_EQ. */
1789 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1792 lhs
.overflow
= false;
1793 lhs
.unsignedp
= false;
1797 /* Returns LHS OP RHS, where OP is == or !=. */
1799 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1800 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1802 /* Work around a 3.0.4 bug; see PR 6950. */
1803 bool eq
= num_eq (lhs
, rhs
);
1804 if (op
== CPP_NOT_EQ
)
1808 lhs
.overflow
= false;
1809 lhs
.unsignedp
= false;
1813 /* Shift NUM, of width PRECISION, right by N bits. */
1815 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1817 cpp_num_part sign_mask
;
1818 bool x
= num_positive (num
, precision
);
1820 if (num
.unsignedp
|| x
)
1823 sign_mask
= ~(cpp_num_part
) 0;
1826 num
.high
= num
.low
= sign_mask
;
1830 if (precision
< PART_PRECISION
)
1831 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1832 else if (precision
< 2 * PART_PRECISION
)
1833 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1835 if (n
>= PART_PRECISION
)
1837 n
-= PART_PRECISION
;
1839 num
.high
= sign_mask
;
1844 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1845 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1849 num
= num_trim (num
, precision
);
1850 num
.overflow
= false;
1854 /* Shift NUM, of width PRECISION, left by N bits. */
1856 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1860 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1861 num
.high
= num
.low
= 0;
1865 cpp_num orig
, maybe_orig
;
1869 if (m
>= PART_PRECISION
)
1871 m
-= PART_PRECISION
;
1877 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1880 num
= num_trim (num
, precision
);
1883 num
.overflow
= false;
1886 maybe_orig
= num_rshift (num
, precision
, n
);
1887 num
.overflow
= !num_eq (orig
, maybe_orig
);
1894 /* The four unary operators: +, -, ! and ~. */
1896 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1901 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1902 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1903 "traditional C rejects the unary plus operator");
1904 num
.overflow
= false;
1908 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1912 num
.high
= ~num
.high
;
1914 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1915 num
.overflow
= false;
1918 default: /* case CPP_NOT: */
1919 num
.low
= num_zerop (num
);
1921 num
.overflow
= false;
1922 num
.unsignedp
= false;
1929 /* The various binary operators. */
1931 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1934 size_t precision
= CPP_OPTION (pfile
, precision
);
1942 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1944 /* A negative shift is a positive shift the other way. */
1945 if (op
== CPP_LSHIFT
)
1949 rhs
= num_negate (rhs
, precision
);
1952 n
= ~0; /* Maximal. */
1955 if (op
== CPP_LSHIFT
)
1956 lhs
= num_lshift (lhs
, precision
, n
);
1958 lhs
= num_rshift (lhs
, precision
, n
);
1963 result
.low
= lhs
.low
- rhs
.low
;
1964 result
.high
= lhs
.high
- rhs
.high
;
1965 if (result
.low
> lhs
.low
)
1967 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1968 result
.overflow
= false;
1970 result
= num_trim (result
, precision
);
1971 if (!result
.unsignedp
)
1973 bool lhsp
= num_positive (lhs
, precision
);
1974 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1975 && lhsp
!= num_positive (result
, precision
));
1980 result
.low
= lhs
.low
+ rhs
.low
;
1981 result
.high
= lhs
.high
+ rhs
.high
;
1982 if (result
.low
< lhs
.low
)
1984 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1985 result
.overflow
= false;
1987 result
= num_trim (result
, precision
);
1988 if (!result
.unsignedp
)
1990 bool lhsp
= num_positive (lhs
, precision
);
1991 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1992 && lhsp
!= num_positive (result
, precision
));
1997 default: /* case CPP_COMMA: */
1998 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1999 || !pfile
->state
.skip_eval
))
2000 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
2001 "comma operator in operand of #if");
2009 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2012 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
2015 cpp_num_part middle
[2], temp
;
2017 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
2018 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
2020 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
2021 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
2024 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
2025 if (result
.low
< temp
)
2029 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
2030 if (result
.low
< temp
)
2033 result
.high
+= HIGH_PART (middle
[0]);
2034 result
.high
+= HIGH_PART (middle
[1]);
2035 result
.unsignedp
= true;
2036 result
.overflow
= false;
2041 /* Multiply two preprocessing numbers. */
2043 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
2045 cpp_num result
, temp
;
2046 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2047 bool overflow
, negate
= false;
2048 size_t precision
= CPP_OPTION (pfile
, precision
);
2050 /* Prepare for unsigned multiplication. */
2053 if (!num_positive (lhs
, precision
))
2054 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
2055 if (!num_positive (rhs
, precision
))
2056 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2059 overflow
= lhs
.high
&& rhs
.high
;
2060 result
= num_part_mul (lhs
.low
, rhs
.low
);
2062 temp
= num_part_mul (lhs
.high
, rhs
.low
);
2063 result
.high
+= temp
.low
;
2067 temp
= num_part_mul (lhs
.low
, rhs
.high
);
2068 result
.high
+= temp
.low
;
2072 temp
.low
= result
.low
, temp
.high
= result
.high
;
2073 result
= num_trim (result
, precision
);
2074 if (!num_eq (result
, temp
))
2078 result
= num_negate (result
, precision
);
2081 result
.overflow
= false;
2083 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
2084 && !num_zerop (result
));
2085 result
.unsignedp
= unsignedp
;
2090 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2091 or the remainder depending upon OP. LOCATION is the source location
2092 of this operator (for diagnostics). */
2095 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
2096 location_t location
)
2098 cpp_num result
, sub
;
2100 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
2101 bool negate
= false, lhs_neg
= false;
2102 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
2104 /* Prepare for unsigned division. */
2107 if (!num_positive (lhs
, precision
))
2108 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2109 if (!num_positive (rhs
, precision
))
2110 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2113 /* Find the high bit. */
2117 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2118 for (; ; i
--, mask
>>= 1)
2119 if (rhs
.high
& mask
)
2124 if (precision
> PART_PRECISION
)
2125 i
= precision
- PART_PRECISION
- 1;
2128 mask
= (cpp_num_part
) 1 << i
;
2129 for (; ; i
--, mask
>>= 1)
2135 if (!pfile
->state
.skip_eval
)
2136 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2137 "division by zero in #if");
2141 /* First nonzero bit of RHS is bit I. Do naive division by
2142 shifting the RHS fully left, and subtracting from LHS if LHS is
2143 at least as big, and then repeating but with one less shift.
2144 This is not very efficient, but is easy to understand. */
2146 rhs
.unsignedp
= true;
2147 lhs
.unsignedp
= true;
2148 i
= precision
- i
- 1;
2149 sub
= num_lshift (rhs
, precision
, i
);
2151 result
.high
= result
.low
= 0;
2154 if (num_greater_eq (lhs
, sub
, precision
))
2156 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2157 if (i
>= PART_PRECISION
)
2158 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2160 result
.low
|= (cpp_num_part
) 1 << i
;
2164 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2168 /* We divide so that the remainder has the sign of the LHS. */
2171 result
.unsignedp
= unsignedp
;
2172 result
.overflow
= false;
2176 result
= num_negate (result
, precision
);
2177 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2178 && !num_zerop (result
));
2185 lhs
.unsignedp
= unsignedp
;
2186 lhs
.overflow
= false;
2188 lhs
= num_negate (lhs
, precision
);
2193 /* Handle meeting "__has_include__" in a preprocessor expression. */
2195 parse_has_include (cpp_reader
*pfile
, enum include_type type
)
2199 cpp_hashnode
*node
= 0;
2200 const cpp_token
*token
;
2201 bool bracket
= false;
2204 result
.unsignedp
= false;
2206 result
.overflow
= false;
2209 pfile
->state
.in__has_include__
++;
2211 token
= cpp_get_token (pfile
);
2212 if (token
->type
== CPP_OPEN_PAREN
)
2215 token
= cpp_get_token (pfile
);
2218 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
2220 if (token
->type
== CPP_HEADER_NAME
)
2222 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
2223 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
2224 fname
[token
->val
.str
.len
- 2] = '\0';
2225 node
= token
->val
.node
.node
;
2227 else if (token
->type
== CPP_LESS
)
2230 fname
= _cpp_bracket_include (pfile
);
2233 cpp_error (pfile
, CPP_DL_ERROR
,
2234 "operator \"__has_include__\" requires a header string");
2238 int angle_brackets
= (bracket
? 1 : 0);
2240 if (_cpp_has_header (pfile
, fname
, angle_brackets
, type
))
2248 if (paren
&& !SEEN_EOL () && cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
2249 cpp_error (pfile
, CPP_DL_ERROR
,
2250 "missing ')' after \"__has_include__\"");
2252 /* A possible controlling macro of the form #if !__has_include__ ().
2253 _cpp_parse_expr checks there was no other junk on the line. */
2255 pfile
->mi_ind_cmacro
= node
;
2257 pfile
->state
.in__has_include__
--;