1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
24 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
25 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
26 #define LOW_PART(num_part) (num_part & HALF_MASK)
27 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
31 const cpp_token
*token
; /* The token forming op (for diagnostics). */
32 cpp_num value
; /* The value logically "right" of op. */
33 source_location loc
; /* The location of this value. */
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive (cpp_num
, size_t);
41 static bool num_greater_eq (cpp_num
, cpp_num
, size_t);
42 static cpp_num
num_trim (cpp_num
, size_t);
43 static cpp_num
num_part_mul (cpp_num_part
, cpp_num_part
);
45 static cpp_num
num_unary_op (cpp_reader
*, cpp_num
, enum cpp_ttype
);
46 static cpp_num
num_binary_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
47 static cpp_num
num_negate (cpp_num
, size_t);
48 static cpp_num
num_bitwise_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
49 static cpp_num
num_inequality_op (cpp_reader
*, cpp_num
, cpp_num
,
51 static cpp_num
num_equality_op (cpp_reader
*, cpp_num
, cpp_num
,
53 static cpp_num
num_mul (cpp_reader
*, cpp_num
, cpp_num
);
54 static cpp_num
num_div_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
,
56 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
57 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
59 static cpp_num
append_digit (cpp_num
, int, int, size_t);
60 static cpp_num
parse_defined (cpp_reader
*);
61 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*, source_location
);
62 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
63 static unsigned int interpret_float_suffix (cpp_reader
*, const uchar
*, size_t);
64 static unsigned int interpret_int_suffix (cpp_reader
*, const uchar
*, size_t);
65 static void check_promotion (cpp_reader
*, const struct op
*);
67 static cpp_num
parse_has_include (cpp_reader
*, enum include_type
);
68 static cpp_num
parse_has_attribute (cpp_reader
*);
70 /* Token type abuse to create unary plus and minus operators. */
71 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
72 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
74 /* With -O2, gcc appears to produce nice code, moving the error
75 message load and subsequent jump completely out of the main path. */
76 #define SYNTAX_ERROR(msgid) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
78 #define SYNTAX_ERROR2(msgid, arg) \
79 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
81 #define SYNTAX_ERROR_AT(loc, msgid) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
84 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
85 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
88 /* Subroutine of cpp_classify_number. S points to a float suffix of
89 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
90 flag vector describing the suffix. */
92 interpret_float_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
95 size_t f
, d
, l
, w
, q
, i
;
98 f
= d
= l
= w
= q
= i
= 0;
100 /* Process decimal float suffixes, which are two letters starting
101 with d or D. Order and case are significant. */
102 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
104 bool uppercase
= (*s
== 'D');
107 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
108 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
109 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
110 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
111 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
112 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
114 /* Additional two-character suffixes beginning with D are not
115 for decimal float constants. */
120 if (CPP_OPTION (pfile
, ext_numeric_literals
))
122 /* Recognize a fixed-point suffix. */
126 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
127 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
131 /* Continue processing a fixed-point suffix. The suffix is case
132 insensitive except for ll or LL. Order is significant. */
139 if (*s
== 'u' || *s
== 'U')
141 flags
|= CPP_N_UNSIGNED
;
152 return flags
|= CPP_N_SMALL
;
156 return flags
|= CPP_N_MEDIUM
;
157 if (len
== 2 && s
[1] == 'l')
158 return flags
|= CPP_N_LARGE
;
162 return flags
|= CPP_N_MEDIUM
;
163 if (len
== 2 && s
[1] == 'L')
164 return flags
|= CPP_N_LARGE
;
169 /* Anything left at this point is invalid. */
174 /* In any remaining valid suffix, the case and order don't matter. */
178 case 'f': case 'F': f
++; break;
179 case 'd': case 'D': d
++; break;
180 case 'l': case 'L': l
++; break;
181 case 'w': case 'W': w
++; break;
182 case 'q': case 'Q': q
++; break;
184 case 'j': case 'J': i
++; break;
189 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
192 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
195 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
198 return ((i
? CPP_N_IMAGINARY
: 0)
203 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
206 /* Return the classification flags for a float suffix. */
208 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
210 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
213 /* Subroutine of cpp_classify_number. S points to an integer suffix
214 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
215 flag vector describing the suffix. */
217 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
226 case 'u': case 'U': u
++; break;
228 case 'j': case 'J': i
++; break;
229 case 'l': case 'L': l
++;
230 /* If there are two Ls, they must be adjacent and the same case. */
231 if (l
== 2 && s
[len
] != s
[len
+ 1])
238 if (l
> 2 || u
> 1 || i
> 1)
241 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
244 return ((i
? CPP_N_IMAGINARY
: 0)
245 | (u
? CPP_N_UNSIGNED
: 0)
246 | ((l
== 0) ? CPP_N_SMALL
247 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
250 /* Return the classification flags for an int suffix. */
252 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
254 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
257 /* Return the string type corresponding to the the input user-defined string
258 literal type. If the input type is not a user-defined string literal
259 type return the input type. */
261 cpp_userdef_string_remove_type (enum cpp_ttype type
)
263 if (type
== CPP_STRING_USERDEF
)
265 else if (type
== CPP_WSTRING_USERDEF
)
267 else if (type
== CPP_STRING16_USERDEF
)
269 else if (type
== CPP_STRING32_USERDEF
)
271 else if (type
== CPP_UTF8STRING_USERDEF
)
272 return CPP_UTF8STRING
;
277 /* Return the user-defined string literal type corresponding to the input
278 string type. If the input type is not a string type return the input
281 cpp_userdef_string_add_type (enum cpp_ttype type
)
283 if (type
== CPP_STRING
)
284 return CPP_STRING_USERDEF
;
285 else if (type
== CPP_WSTRING
)
286 return CPP_WSTRING_USERDEF
;
287 else if (type
== CPP_STRING16
)
288 return CPP_STRING16_USERDEF
;
289 else if (type
== CPP_STRING32
)
290 return CPP_STRING32_USERDEF
;
291 else if (type
== CPP_UTF8STRING
)
292 return CPP_UTF8STRING_USERDEF
;
297 /* Return the char type corresponding to the the input user-defined char
298 literal type. If the input type is not a user-defined char literal
299 type return the input type. */
301 cpp_userdef_char_remove_type (enum cpp_ttype type
)
303 if (type
== CPP_CHAR_USERDEF
)
305 else if (type
== CPP_WCHAR_USERDEF
)
307 else if (type
== CPP_CHAR16_USERDEF
)
309 else if (type
== CPP_CHAR32_USERDEF
)
315 /* Return the user-defined char literal type corresponding to the input
316 char type. If the input type is not a char type return the input
319 cpp_userdef_char_add_type (enum cpp_ttype type
)
321 if (type
== CPP_CHAR
)
322 return CPP_CHAR_USERDEF
;
323 else if (type
== CPP_WCHAR
)
324 return CPP_WCHAR_USERDEF
;
325 else if (type
== CPP_CHAR16
)
326 return CPP_CHAR16_USERDEF
;
327 else if (type
== CPP_CHAR32
)
328 return CPP_CHAR32_USERDEF
;
333 /* Return true if the token type is a user-defined string literal. */
335 cpp_userdef_string_p (enum cpp_ttype type
)
337 if (type
== CPP_STRING_USERDEF
338 || type
== CPP_WSTRING_USERDEF
339 || type
== CPP_STRING16_USERDEF
340 || type
== CPP_STRING32_USERDEF
341 || type
== CPP_UTF8STRING_USERDEF
)
347 /* Return true if the token type is a user-defined char literal. */
349 cpp_userdef_char_p (enum cpp_ttype type
)
351 if (type
== CPP_CHAR_USERDEF
352 || type
== CPP_WCHAR_USERDEF
353 || type
== CPP_CHAR16_USERDEF
354 || type
== CPP_CHAR32_USERDEF
)
360 /* Extract the suffix from a user-defined literal string or char. */
362 cpp_get_userdef_suffix (const cpp_token
*tok
)
364 unsigned int len
= tok
->val
.str
.len
;
365 const char *text
= (const char *)tok
->val
.str
.text
;
368 for (i
= 0; i
< len
; ++i
)
369 if (text
[i
] == '\'' || text
[i
] == '"')
374 for (i
= len
; i
> 0; --i
)
375 if (text
[i
- 1] == delim
)
380 /* Categorize numeric constants according to their field (integer,
381 floating point, or invalid), radix (decimal, octal, hexadecimal),
384 TOKEN is the token that represents the numeric constant to
387 In C++0X if UD_SUFFIX is non null it will be assigned
388 any unrecognized suffix for a user-defined literal.
390 VIRTUAL_LOCATION is the virtual location for TOKEN. */
392 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
393 const char **ud_suffix
, source_location virtual_location
)
395 const uchar
*str
= token
->val
.str
.text
;
397 unsigned int max_digit
, result
, radix
;
398 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
405 /* If the lexer has done its job, length one can only be a single
406 digit. Fast-path this very common case. */
407 if (token
->val
.str
.len
== 1)
408 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
410 limit
= str
+ token
->val
.str
.len
;
411 float_flag
= NOT_FLOAT
;
415 seen_digit_sep
= false;
417 /* First, interpret the radix. */
423 /* Require at least one hex digit to classify it as hex. */
424 if (*str
== 'x' || *str
== 'X')
426 if (str
[1] == '.' || ISXDIGIT (str
[1]))
431 else if (DIGIT_SEP (str
[1]))
432 SYNTAX_ERROR_AT (virtual_location
,
433 "digit separator after base indicator");
435 else if (*str
== 'b' || *str
== 'B')
437 if (str
[1] == '0' || str
[1] == '1')
442 else if (DIGIT_SEP (str
[1]))
443 SYNTAX_ERROR_AT (virtual_location
,
444 "digit separator after base indicator");
448 /* Now scan for a well-formed integer or float. */
451 unsigned int c
= *str
++;
453 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
455 seen_digit_sep
= false;
461 else if (DIGIT_SEP (c
))
464 SYNTAX_ERROR_AT (virtual_location
, "adjacent digit separators");
465 seen_digit_sep
= true;
469 if (seen_digit_sep
|| DIGIT_SEP (*str
))
470 SYNTAX_ERROR_AT (virtual_location
,
471 "digit separator adjacent to decimal point");
472 seen_digit_sep
= false;
473 if (float_flag
== NOT_FLOAT
)
474 float_flag
= AFTER_POINT
;
476 SYNTAX_ERROR_AT (virtual_location
,
477 "too many decimal points in number");
479 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
480 || (radix
== 16 && (c
== 'p' || c
== 'P')))
482 if (seen_digit_sep
|| DIGIT_SEP (*str
))
483 SYNTAX_ERROR_AT (virtual_location
,
484 "digit separator adjacent to exponent");
485 float_flag
= AFTER_EXPON
;
490 /* Start of suffix. */
496 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
497 SYNTAX_ERROR_AT (virtual_location
,
498 "digit separator outside digit sequence");
500 /* The suffix may be for decimal fixed-point constants without exponent. */
501 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
503 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
504 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
506 result
|= CPP_N_FLOATING
;
507 /* We need to restore the radix to 10, if the radix is 8. */
511 if (CPP_PEDANTIC (pfile
))
512 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
513 "fixed-point constants are a GCC extension");
520 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
523 if (max_digit
>= radix
)
526 SYNTAX_ERROR2_AT (virtual_location
,
527 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
529 SYNTAX_ERROR2_AT (virtual_location
,
530 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
533 if (float_flag
!= NOT_FLOAT
)
537 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
538 "invalid prefix \"0b\" for floating constant");
539 return CPP_N_INVALID
;
542 if (radix
== 16 && !seen_digit
)
543 SYNTAX_ERROR_AT (virtual_location
,
544 "no digits in hexadecimal floating constant");
546 if (radix
== 16 && CPP_PEDANTIC (pfile
)
547 && !CPP_OPTION (pfile
, extended_numbers
))
549 if (CPP_OPTION (pfile
, cplusplus
))
550 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
551 "use of C++11 hexadecimal floating constant");
553 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
554 "use of C99 hexadecimal floating constant");
557 if (float_flag
== AFTER_EXPON
)
559 if (*str
== '+' || *str
== '-')
562 /* Exponent is decimal, even if string is a hex float. */
565 if (DIGIT_SEP (*str
))
566 SYNTAX_ERROR_AT (virtual_location
,
567 "digit separator adjacent to exponent");
569 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
573 seen_digit_sep
= DIGIT_SEP (*str
);
576 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
578 else if (radix
== 16)
579 SYNTAX_ERROR_AT (virtual_location
,
580 "hexadecimal floating constants require an exponent");
583 SYNTAX_ERROR_AT (virtual_location
,
584 "digit separator outside digit sequence");
586 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
589 if (CPP_OPTION (pfile
, user_literals
))
592 *ud_suffix
= (const char *) str
;
593 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
597 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
598 "invalid suffix \"%.*s\" on floating constant",
599 (int) (limit
- str
), str
);
600 return CPP_N_INVALID
;
604 /* Traditional C didn't accept any floating suffixes. */
606 && CPP_WTRADITIONAL (pfile
)
607 && ! cpp_sys_macro_p (pfile
))
608 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
609 "traditional C rejects the \"%.*s\" suffix",
610 (int) (limit
- str
), str
);
612 /* A suffix for double is a GCC extension via decimal float support.
613 If the suffix also specifies an imaginary value we'll catch that
615 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
616 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
617 "suffix for double constant is a GCC extension");
619 /* Radix must be 10 for decimal floats. */
620 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
622 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
623 "invalid suffix \"%.*s\" with hexadecimal floating constant",
624 (int) (limit
- str
), str
);
625 return CPP_N_INVALID
;
628 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
629 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
630 "fixed-point constants are a GCC extension");
632 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
633 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
634 "decimal float constants are a GCC extension");
636 result
|= CPP_N_FLOATING
;
640 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
643 if (CPP_OPTION (pfile
, user_literals
))
646 *ud_suffix
= (const char *) str
;
647 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
651 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
652 "invalid suffix \"%.*s\" on integer constant",
653 (int) (limit
- str
), str
);
654 return CPP_N_INVALID
;
658 /* Traditional C only accepted the 'L' suffix.
659 Suppress warning about 'LL' with -Wno-long-long. */
660 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
662 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
663 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
664 && CPP_OPTION (pfile
, cpp_warn_long_long
);
667 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
669 "traditional C rejects the \"%.*s\" suffix",
670 (int) (limit
- str
), str
);
673 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
674 && CPP_OPTION (pfile
, cpp_warn_long_long
))
676 const char *message
= CPP_OPTION (pfile
, cplusplus
)
677 ? N_("use of C++11 long long integer constant")
678 : N_("use of C99 long long integer constant");
680 if (CPP_OPTION (pfile
, c99
))
681 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
684 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
685 virtual_location
, 0, message
);
688 result
|= CPP_N_INTEGER
;
692 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
693 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
694 "imaginary constants are a GCC extension");
696 && !CPP_OPTION (pfile
, binary_constants
)
697 && CPP_PEDANTIC (pfile
))
698 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
699 CPP_OPTION (pfile
, cplusplus
)
700 ? "binary constants are a C++14 feature "
702 : "binary constants are a GCC extension");
705 result
|= CPP_N_DECIMAL
;
706 else if (radix
== 16)
709 result
|= CPP_N_BINARY
;
711 result
|= CPP_N_OCTAL
;
716 return CPP_N_INVALID
;
719 /* cpp_interpret_integer converts an integer constant into a cpp_num,
720 of precision options->precision.
722 We do not provide any interface for decimal->float conversion,
723 because the preprocessor doesn't need it and we don't want to
724 drag in GCC's floating point emulator. */
726 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
729 const uchar
*p
, *end
;
734 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
735 result
.overflow
= false;
737 p
= token
->val
.str
.text
;
738 end
= p
+ token
->val
.str
.len
;
740 /* Common case of a single digit. */
741 if (token
->val
.str
.len
== 1)
742 result
.low
= p
[0] - '0';
746 size_t precision
= CPP_OPTION (pfile
, precision
);
747 unsigned int base
= 10, c
= 0;
748 bool overflow
= false;
750 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
755 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
760 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
766 /* We can add a digit to numbers strictly less than this without
767 needing the precision and slowness of double integers. */
768 max
= ~(cpp_num_part
) 0;
769 if (precision
< PART_PRECISION
)
770 max
>>= PART_PRECISION
- precision
;
771 max
= (max
- base
+ 1) / base
+ 1;
777 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
779 else if (DIGIT_SEP (c
))
784 /* Strict inequality for when max is set to zero. */
785 if (result
.low
< max
)
786 result
.low
= result
.low
* base
+ c
;
789 result
= append_digit (result
, c
, base
, precision
);
790 overflow
|= result
.overflow
;
795 if (overflow
&& !(type
& CPP_N_USERDEF
))
796 cpp_error (pfile
, CPP_DL_PEDWARN
,
797 "integer constant is too large for its type");
798 /* If too big to be signed, consider it unsigned. Only warn for
799 decimal numbers. Traditional numbers were always signed (but
800 we still honor an explicit U suffix); but we only have
801 traditional semantics in directives. */
802 else if (!result
.unsignedp
803 && !(CPP_OPTION (pfile
, traditional
)
804 && pfile
->state
.in_directive
)
805 && !num_positive (result
, precision
))
807 /* This is for constants within the range of uintmax_t but
808 not that of intmax_t. For such decimal constants, a
809 diagnostic is required for C99 as the selected type must
810 be signed and not having a type is a constraint violation
811 (DR#298, TC3), so this must be a pedwarn. For C90,
812 unsigned long is specified to be used for a constant that
813 does not fit in signed long; if uintmax_t has the same
814 range as unsigned long this means only a warning is
815 appropriate here. C90 permits the preprocessor to use a
816 wider range than unsigned long in the compiler, so if
817 uintmax_t is wider than unsigned long no diagnostic is
818 required for such constants in preprocessor #if
819 expressions and the compiler will pedwarn for such
820 constants outside the range of unsigned long that reach
821 the compiler so a diagnostic is not required there
822 either; thus, pedwarn for C99 but use a plain warning for
825 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
828 "integer constant is so large that it is unsigned");
829 result
.unsignedp
= true;
836 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
838 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
843 cpp_num_part add_high
, add_low
;
845 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
846 need to worry about add_high overflowing. */
860 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
861 result
.high
= num
.high
<< shift
;
862 result
.low
= num
.low
<< shift
;
863 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
864 result
.unsignedp
= num
.unsignedp
;
868 add_low
= num
.low
<< 1;
869 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
872 add_high
= add_low
= 0;
874 if (add_low
+ digit
< add_low
)
878 if (result
.low
+ add_low
< result
.low
)
880 if (result
.high
+ add_high
< result
.high
)
883 result
.low
+= add_low
;
884 result
.high
+= add_high
;
885 result
.overflow
= overflow
;
887 /* The above code catches overflow of a cpp_num type. This catches
888 overflow of the (possibly shorter) target precision. */
889 num
.low
= result
.low
;
890 num
.high
= result
.high
;
891 result
= num_trim (result
, precision
);
892 if (!num_eq (result
, num
))
893 result
.overflow
= true;
898 /* Handle meeting "defined" in a preprocessor expression. */
900 parse_defined (cpp_reader
*pfile
)
904 cpp_hashnode
*node
= 0;
905 const cpp_token
*token
;
906 cpp_context
*initial_context
= pfile
->context
;
908 /* Don't expand macros. */
909 pfile
->state
.prevent_expansion
++;
911 token
= cpp_get_token (pfile
);
912 if (token
->type
== CPP_OPEN_PAREN
)
915 token
= cpp_get_token (pfile
);
918 if (token
->type
== CPP_NAME
)
920 node
= token
->val
.node
.node
;
921 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
923 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
929 cpp_error (pfile
, CPP_DL_ERROR
,
930 "operator \"defined\" requires an identifier");
931 if (token
->flags
& NAMED_OP
)
936 op
.type
= token
->type
;
937 cpp_error (pfile
, CPP_DL_ERROR
,
938 "(\"%s\" is an alternative token for \"%s\" in C++)",
939 cpp_token_as_text (pfile
, token
),
940 cpp_token_as_text (pfile
, &op
));
946 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
947 cpp_error (pfile
, CPP_DL_WARNING
,
948 "this use of \"defined\" may not be portable");
950 _cpp_mark_macro_used (node
);
951 if (!(node
->flags
& NODE_USED
))
953 node
->flags
|= NODE_USED
;
954 if (node
->type
== NT_MACRO
)
956 if ((node
->flags
& NODE_BUILTIN
)
957 && pfile
->cb
.user_builtin_macro
)
958 pfile
->cb
.user_builtin_macro (pfile
, node
);
959 if (pfile
->cb
.used_define
)
960 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
964 if (pfile
->cb
.used_undef
)
965 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
969 /* A possible controlling macro of the form #if !defined ().
970 _cpp_parse_expr checks there was no other junk on the line. */
971 pfile
->mi_ind_cmacro
= node
;
974 pfile
->state
.prevent_expansion
--;
976 /* Do not treat conditional macros as being defined. This is due to the
977 powerpc and spu ports using conditional macros for 'vector', 'bool', and
978 'pixel' to act as conditional keywords. This messes up tests like #ifndef
980 result
.unsignedp
= false;
982 result
.overflow
= false;
983 result
.low
= (node
&& node
->type
== NT_MACRO
984 && (node
->flags
& NODE_CONDITIONAL
) == 0);
988 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
989 number or character constant, or the result of the "defined" or "#"
992 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
993 source_location virtual_location
)
999 result
.unsignedp
= false;
1000 result
.overflow
= false;
1002 switch (token
->type
)
1005 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1006 if (temp
& CPP_N_USERDEF
)
1007 cpp_error (pfile
, CPP_DL_ERROR
,
1008 "user-defined literal in preprocessor expression");
1009 switch (temp
& CPP_N_CATEGORY
)
1011 case CPP_N_FLOATING
:
1012 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1013 "floating constant in preprocessor expression");
1016 if (!(temp
& CPP_N_IMAGINARY
))
1017 return cpp_interpret_integer (pfile
, token
, temp
);
1018 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1019 "imaginary number in preprocessor expression");
1023 /* Error already issued. */
1026 result
.high
= result
.low
= 0;
1034 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1039 /* Sign-extend the result if necessary. */
1040 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1042 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1043 result
.low
|= ~(~(cpp_num_part
) 0
1044 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1045 result
.high
= ~(cpp_num_part
) 0;
1046 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1052 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1053 return parse_defined (pfile
);
1054 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include__
)
1055 return parse_has_include (pfile
, IT_INCLUDE
);
1056 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include_next__
)
1057 return parse_has_include (pfile
, IT_INCLUDE_NEXT
);
1058 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_attribute__
)
1059 return parse_has_attribute (pfile
);
1060 else if (CPP_OPTION (pfile
, cplusplus
)
1061 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1062 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1065 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1071 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1072 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1073 "\"%s\" is not defined",
1074 NODE_NAME (token
->val
.node
.node
));
1079 if (!pfile
->state
.skipping
)
1081 /* A pedantic warning takes precedence over a deprecated
1083 if (CPP_PEDANTIC (pfile
))
1084 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1085 virtual_location
, 0,
1086 "assertions are a GCC extension");
1087 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1088 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1089 "assertions are a deprecated extension");
1091 _cpp_test_assertion (pfile
, &temp
);
1100 result
.unsignedp
= !!unsignedp
;
1104 /* Operator precedence and flags table.
1106 After an operator is returned from the lexer, if it has priority less
1107 than the operator on the top of the stack, we reduce the stack by one
1108 operator and repeat the test. Since equal priorities do not reduce,
1109 this is naturally right-associative.
1111 We handle left-associative operators by decrementing the priority of
1112 just-lexed operators by one, but retaining the priority of operators
1113 already on the stack.
1115 The remaining cases are '(' and ')'. We handle '(' by skipping the
1116 reduction phase completely. ')' is given lower priority than
1117 everything else, including '(', effectively forcing a reduction of the
1118 parenthesized expression. If there is a matching '(', the routine
1119 reduce() exits immediately. If the normal exit route sees a ')', then
1120 there cannot have been a matching '(' and an error message is output.
1122 The parser assumes all shifted operators require a left operand unless
1123 the flag NO_L_OPERAND is set. These semantics are automatic; any
1124 extra semantics need to be handled with operator-specific code. */
1126 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1127 operand changes because of integer promotions. */
1128 #define NO_L_OPERAND (1 << 0)
1129 #define LEFT_ASSOC (1 << 1)
1130 #define CHECK_PROMOTION (1 << 2)
1132 /* Operator to priority map. Must be in the same order as the first
1133 N entries of enum cpp_ttype. */
1134 static const struct cpp_operator
1140 /* EQ */ {0, 0}, /* Shouldn't happen. */
1141 /* NOT */ {16, NO_L_OPERAND
},
1142 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1143 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1144 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1145 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1146 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1147 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1148 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1149 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1150 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1151 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1152 /* RSHIFT */ {13, LEFT_ASSOC
},
1153 /* LSHIFT */ {13, LEFT_ASSOC
},
1155 /* COMPL */ {16, NO_L_OPERAND
},
1156 /* AND_AND */ {6, LEFT_ASSOC
},
1157 /* OR_OR */ {5, LEFT_ASSOC
},
1158 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1159 However, there are some special cases for these in reduce(). */
1161 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1162 /* COMMA */ {4, LEFT_ASSOC
},
1163 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1164 /* CLOSE_PAREN */ {0, 0},
1166 /* EQ_EQ */ {11, LEFT_ASSOC
},
1167 /* NOT_EQ */ {11, LEFT_ASSOC
},
1168 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1169 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1170 /* UPLUS */ {16, NO_L_OPERAND
},
1171 /* UMINUS */ {16, NO_L_OPERAND
}
1174 /* Parse and evaluate a C expression, reading from PFILE.
1175 Returns the truth value of the expression.
1177 The implementation is an operator precedence parser, i.e. a
1178 bottom-up parser, using a stack for not-yet-reduced tokens.
1180 The stack base is op_stack, and the current stack pointer is 'top'.
1181 There is a stack element for each operator (only), and the most
1182 recently pushed operator is 'top->op'. An operand (value) is
1183 stored in the 'value' field of the stack element of the operator
1184 that precedes it. */
1186 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1188 struct op
*top
= pfile
->op_stack
;
1189 unsigned int lex_count
;
1190 bool saw_leading_not
, want_value
= true;
1191 source_location virtual_location
= 0;
1193 pfile
->state
.skip_eval
= 0;
1195 /* Set up detection of #if ! defined(). */
1196 pfile
->mi_ind_cmacro
= 0;
1197 saw_leading_not
= false;
1200 /* Lowest priority operator prevents further reductions. */
1208 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1209 op
.op
= op
.token
->type
;
1210 op
.loc
= virtual_location
;
1214 /* These tokens convert into values. */
1223 SYNTAX_ERROR2_AT (op
.loc
,
1224 "missing binary operator before token \"%s\"",
1225 cpp_token_as_text (pfile
, op
.token
));
1227 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1231 saw_leading_not
= lex_count
== 1;
1243 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1244 SYNTAX_ERROR2_AT (op
.loc
,
1245 "token \"%s\" is not valid in preprocessor expressions",
1246 cpp_token_as_text (pfile
, op
.token
));
1250 /* Check we have a value or operator as appropriate. */
1251 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1254 SYNTAX_ERROR2_AT (op
.loc
,
1255 "missing binary operator before token \"%s\"",
1256 cpp_token_as_text (pfile
, op
.token
));
1258 else if (want_value
)
1260 /* We want a number (or expression) and haven't got one.
1261 Try to emit a specific diagnostic. */
1262 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1263 SYNTAX_ERROR_AT (op
.loc
,
1264 "missing expression between '(' and ')'");
1266 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1267 SYNTAX_ERROR2_AT (op
.loc
,
1268 "%s with no expression", is_if
? "#if" : "#elif");
1270 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1271 SYNTAX_ERROR2_AT (op
.loc
,
1272 "operator '%s' has no right operand",
1273 cpp_token_as_text (pfile
, top
->token
));
1274 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1275 /* Complain about missing paren during reduction. */;
1277 SYNTAX_ERROR2_AT (op
.loc
,
1278 "operator '%s' has no left operand",
1279 cpp_token_as_text (pfile
, op
.token
));
1282 top
= reduce (pfile
, top
, op
.op
);
1286 if (op
.op
== CPP_EOF
)
1291 case CPP_CLOSE_PAREN
:
1294 if (!num_zerop (top
->value
))
1295 pfile
->state
.skip_eval
++;
1299 if (num_zerop (top
->value
))
1300 pfile
->state
.skip_eval
++;
1303 if (top
->op
!= CPP_QUERY
)
1304 SYNTAX_ERROR_AT (op
.loc
,
1305 " ':' without preceding '?'");
1306 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1307 pfile
->state
.skip_eval
++;
1309 pfile
->state
.skip_eval
--;
1316 /* Check for and handle stack overflow. */
1317 if (++top
== pfile
->op_limit
)
1318 top
= _cpp_expand_op_stack (pfile
);
1321 top
->token
= op
.token
;
1325 /* The controlling macro expression is only valid if we called lex 3
1326 times: <!> <defined expression> and <EOF>. push_conditional ()
1327 checks that we are at top-of-file. */
1328 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1329 pfile
->mi_ind_cmacro
= 0;
1331 if (top
!= pfile
->op_stack
)
1333 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1334 "unbalanced stack in %s",
1335 is_if
? "#if" : "#elif");
1337 return false; /* Return false on syntax error. */
1340 return !num_zerop (top
->value
);
1343 /* Reduce the operator / value stack if possible, in preparation for
1344 pushing operator OP. Returns NULL on error, otherwise the top of
1347 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1351 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1354 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1358 if (op
== CPP_OPEN_PAREN
)
1361 /* Decrement the priority of left-associative operators to force a
1362 reduction with operators of otherwise equal priority. */
1363 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1364 while (prio
< optab
[top
->op
].prio
)
1366 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1367 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1368 check_promotion (pfile
, top
);
1376 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1377 top
[-1].loc
= top
->loc
;
1385 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1386 top
->value
, top
->op
);
1387 top
[-1].loc
= top
->loc
;
1392 case CPP_GREATER_EQ
:
1395 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1396 top
[-1].loc
= top
->loc
;
1402 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1403 top
[-1].loc
= top
->loc
;
1410 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1411 top
[-1].loc
= top
->loc
;
1415 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1416 top
[-1].loc
= top
->loc
;
1421 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1422 top
->value
, top
->op
, top
->loc
);
1423 top
[-1].loc
= top
->loc
;
1428 if (!num_zerop (top
->value
))
1429 pfile
->state
.skip_eval
--;
1430 top
->value
.low
= (!num_zerop (top
->value
)
1431 || !num_zerop (top
[1].value
));
1432 top
->value
.high
= 0;
1433 top
->value
.unsignedp
= false;
1434 top
->value
.overflow
= false;
1435 top
->loc
= top
[1].loc
;
1440 if (num_zerop (top
->value
))
1441 pfile
->state
.skip_eval
--;
1442 top
->value
.low
= (!num_zerop (top
->value
)
1443 && !num_zerop (top
[1].value
));
1444 top
->value
.high
= 0;
1445 top
->value
.unsignedp
= false;
1446 top
->value
.overflow
= false;
1447 top
->loc
= top
[1].loc
;
1450 case CPP_OPEN_PAREN
:
1451 if (op
!= CPP_CLOSE_PAREN
)
1453 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1454 top
->token
->src_loc
,
1455 0, "missing ')' in expression");
1459 top
->value
= top
[1].value
;
1460 top
->loc
= top
[1].loc
;
1465 if (!num_zerop (top
->value
))
1467 pfile
->state
.skip_eval
--;
1468 top
->value
= top
[1].value
;
1469 top
->loc
= top
[1].loc
;
1473 top
->value
= top
[2].value
;
1474 top
->loc
= top
[2].loc
;
1476 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1477 || top
[2].value
.unsignedp
);
1481 /* COMMA and COLON should not reduce a QUERY operator. */
1482 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1484 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1492 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1493 cpp_error (pfile
, CPP_DL_PEDWARN
,
1494 "integer overflow in preprocessor expression");
1497 if (op
== CPP_CLOSE_PAREN
)
1499 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1506 /* Returns the position of the old top of stack after expansion. */
1508 _cpp_expand_op_stack (cpp_reader
*pfile
)
1510 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1511 size_t new_size
= old_size
* 2 + 20;
1513 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1514 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1516 return pfile
->op_stack
+ old_size
;
1519 /* Emits a warning if the effective sign of either operand of OP
1520 changes because of integer promotions. */
1522 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1524 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1527 if (op
->value
.unsignedp
)
1529 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1530 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1531 "the left operand of \"%s\" changes sign when promoted",
1532 cpp_token_as_text (pfile
, op
->token
));
1534 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1535 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1536 "the right operand of \"%s\" changes sign when promoted",
1537 cpp_token_as_text (pfile
, op
->token
));
1540 /* Clears the unused high order bits of the number pointed to by PNUM. */
1542 num_trim (cpp_num num
, size_t precision
)
1544 if (precision
> PART_PRECISION
)
1546 precision
-= PART_PRECISION
;
1547 if (precision
< PART_PRECISION
)
1548 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1552 if (precision
< PART_PRECISION
)
1553 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1560 /* True iff A (presumed signed) >= 0. */
1562 num_positive (cpp_num num
, size_t precision
)
1564 if (precision
> PART_PRECISION
)
1566 precision
-= PART_PRECISION
;
1567 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1570 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1573 /* Sign extend a number, with PRECISION significant bits and all
1574 others assumed clear, to fill out a cpp_num structure. */
1576 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1580 if (precision
> PART_PRECISION
)
1582 precision
-= PART_PRECISION
;
1583 if (precision
< PART_PRECISION
1584 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1585 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1587 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1589 if (precision
< PART_PRECISION
)
1590 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1591 num
.high
= ~(cpp_num_part
) 0;
1598 /* Returns the negative of NUM. */
1600 num_negate (cpp_num num
, size_t precision
)
1605 num
.high
= ~num
.high
;
1609 num
= num_trim (num
, precision
);
1610 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1615 /* Returns true if A >= B. */
1617 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1621 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1625 /* Both numbers have signed type. If they are of different
1626 sign, the answer is the sign of A. */
1627 unsignedp
= num_positive (pa
, precision
);
1629 if (unsignedp
!= num_positive (pb
, precision
))
1632 /* Otherwise we can do an unsigned comparison. */
1635 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1638 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1640 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1641 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1643 lhs
.overflow
= false;
1644 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1646 /* As excess precision is zeroed, there is no need to num_trim () as
1647 these operations cannot introduce a set bit there. */
1651 lhs
.high
&= rhs
.high
;
1653 else if (op
== CPP_OR
)
1656 lhs
.high
|= rhs
.high
;
1661 lhs
.high
^= rhs
.high
;
1667 /* Returns LHS OP RHS, where OP is an inequality. */
1669 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1672 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1674 if (op
== CPP_GREATER_EQ
)
1676 else if (op
== CPP_LESS
)
1678 else if (op
== CPP_GREATER
)
1679 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1680 else /* CPP_LESS_EQ. */
1681 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1684 lhs
.overflow
= false;
1685 lhs
.unsignedp
= false;
1689 /* Returns LHS OP RHS, where OP is == or !=. */
1691 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1692 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1694 /* Work around a 3.0.4 bug; see PR 6950. */
1695 bool eq
= num_eq (lhs
, rhs
);
1696 if (op
== CPP_NOT_EQ
)
1700 lhs
.overflow
= false;
1701 lhs
.unsignedp
= false;
1705 /* Shift NUM, of width PRECISION, right by N bits. */
1707 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1709 cpp_num_part sign_mask
;
1710 bool x
= num_positive (num
, precision
);
1712 if (num
.unsignedp
|| x
)
1715 sign_mask
= ~(cpp_num_part
) 0;
1718 num
.high
= num
.low
= sign_mask
;
1722 if (precision
< PART_PRECISION
)
1723 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1724 else if (precision
< 2 * PART_PRECISION
)
1725 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1727 if (n
>= PART_PRECISION
)
1729 n
-= PART_PRECISION
;
1731 num
.high
= sign_mask
;
1736 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1737 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1741 num
= num_trim (num
, precision
);
1742 num
.overflow
= false;
1746 /* Shift NUM, of width PRECISION, left by N bits. */
1748 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1752 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1753 num
.high
= num
.low
= 0;
1757 cpp_num orig
, maybe_orig
;
1761 if (m
>= PART_PRECISION
)
1763 m
-= PART_PRECISION
;
1769 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1772 num
= num_trim (num
, precision
);
1775 num
.overflow
= false;
1778 maybe_orig
= num_rshift (num
, precision
, n
);
1779 num
.overflow
= !num_eq (orig
, maybe_orig
);
1786 /* The four unary operators: +, -, ! and ~. */
1788 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1793 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1794 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1795 "traditional C rejects the unary plus operator");
1796 num
.overflow
= false;
1800 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1804 num
.high
= ~num
.high
;
1806 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1807 num
.overflow
= false;
1810 default: /* case CPP_NOT: */
1811 num
.low
= num_zerop (num
);
1813 num
.overflow
= false;
1814 num
.unsignedp
= false;
1821 /* The various binary operators. */
1823 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1826 size_t precision
= CPP_OPTION (pfile
, precision
);
1834 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1836 /* A negative shift is a positive shift the other way. */
1837 if (op
== CPP_LSHIFT
)
1841 rhs
= num_negate (rhs
, precision
);
1844 n
= ~0; /* Maximal. */
1847 if (op
== CPP_LSHIFT
)
1848 lhs
= num_lshift (lhs
, precision
, n
);
1850 lhs
= num_rshift (lhs
, precision
, n
);
1855 result
.low
= lhs
.low
- rhs
.low
;
1856 result
.high
= lhs
.high
- rhs
.high
;
1857 if (result
.low
> lhs
.low
)
1859 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1860 result
.overflow
= false;
1862 result
= num_trim (result
, precision
);
1863 if (!result
.unsignedp
)
1865 bool lhsp
= num_positive (lhs
, precision
);
1866 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1867 && lhsp
!= num_positive (result
, precision
));
1872 result
.low
= lhs
.low
+ rhs
.low
;
1873 result
.high
= lhs
.high
+ rhs
.high
;
1874 if (result
.low
< lhs
.low
)
1876 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1877 result
.overflow
= false;
1879 result
= num_trim (result
, precision
);
1880 if (!result
.unsignedp
)
1882 bool lhsp
= num_positive (lhs
, precision
);
1883 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1884 && lhsp
!= num_positive (result
, precision
));
1889 default: /* case CPP_COMMA: */
1890 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1891 || !pfile
->state
.skip_eval
))
1892 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
1893 "comma operator in operand of #if");
1901 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1904 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1907 cpp_num_part middle
[2], temp
;
1909 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1910 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1912 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1913 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1916 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1917 if (result
.low
< temp
)
1921 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1922 if (result
.low
< temp
)
1925 result
.high
+= HIGH_PART (middle
[0]);
1926 result
.high
+= HIGH_PART (middle
[1]);
1927 result
.unsignedp
= true;
1928 result
.overflow
= false;
1933 /* Multiply two preprocessing numbers. */
1935 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1937 cpp_num result
, temp
;
1938 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1939 bool overflow
, negate
= false;
1940 size_t precision
= CPP_OPTION (pfile
, precision
);
1942 /* Prepare for unsigned multiplication. */
1945 if (!num_positive (lhs
, precision
))
1946 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1947 if (!num_positive (rhs
, precision
))
1948 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1951 overflow
= lhs
.high
&& rhs
.high
;
1952 result
= num_part_mul (lhs
.low
, rhs
.low
);
1954 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1955 result
.high
+= temp
.low
;
1959 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1960 result
.high
+= temp
.low
;
1964 temp
.low
= result
.low
, temp
.high
= result
.high
;
1965 result
= num_trim (result
, precision
);
1966 if (!num_eq (result
, temp
))
1970 result
= num_negate (result
, precision
);
1973 result
.overflow
= false;
1975 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1976 && !num_zerop (result
));
1977 result
.unsignedp
= unsignedp
;
1982 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1983 or the remainder depending upon OP. LOCATION is the source location
1984 of this operator (for diagnostics). */
1987 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1988 source_location location
)
1990 cpp_num result
, sub
;
1992 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1993 bool negate
= false, lhs_neg
= false;
1994 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1996 /* Prepare for unsigned division. */
1999 if (!num_positive (lhs
, precision
))
2000 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2001 if (!num_positive (rhs
, precision
))
2002 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2005 /* Find the high bit. */
2009 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2010 for (; ; i
--, mask
>>= 1)
2011 if (rhs
.high
& mask
)
2016 if (precision
> PART_PRECISION
)
2017 i
= precision
- PART_PRECISION
- 1;
2020 mask
= (cpp_num_part
) 1 << i
;
2021 for (; ; i
--, mask
>>= 1)
2027 if (!pfile
->state
.skip_eval
)
2028 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2029 "division by zero in #if");
2033 /* First nonzero bit of RHS is bit I. Do naive division by
2034 shifting the RHS fully left, and subtracting from LHS if LHS is
2035 at least as big, and then repeating but with one less shift.
2036 This is not very efficient, but is easy to understand. */
2038 rhs
.unsignedp
= true;
2039 lhs
.unsignedp
= true;
2040 i
= precision
- i
- 1;
2041 sub
= num_lshift (rhs
, precision
, i
);
2043 result
.high
= result
.low
= 0;
2046 if (num_greater_eq (lhs
, sub
, precision
))
2048 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2049 if (i
>= PART_PRECISION
)
2050 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2052 result
.low
|= (cpp_num_part
) 1 << i
;
2056 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2060 /* We divide so that the remainder has the sign of the LHS. */
2063 result
.unsignedp
= unsignedp
;
2064 result
.overflow
= false;
2068 result
= num_negate (result
, precision
);
2069 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2070 && !num_zerop (result
));
2077 lhs
.unsignedp
= unsignedp
;
2078 lhs
.overflow
= false;
2080 lhs
= num_negate (lhs
, precision
);
2085 /* Handle meeting "__has_include__" in a preprocessor expression. */
2087 parse_has_include (cpp_reader
*pfile
, enum include_type type
)
2091 cpp_hashnode
*node
= 0;
2092 const cpp_token
*token
;
2093 bool bracket
= false;
2096 result
.unsignedp
= false;
2098 result
.overflow
= false;
2101 pfile
->state
.in__has_include__
++;
2103 token
= cpp_get_token (pfile
);
2104 if (token
->type
== CPP_OPEN_PAREN
)
2107 token
= cpp_get_token (pfile
);
2110 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
2112 if (token
->type
== CPP_HEADER_NAME
)
2114 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
2115 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
2116 fname
[token
->val
.str
.len
- 2] = '\0';
2117 node
= token
->val
.node
.node
;
2119 else if (token
->type
== CPP_LESS
)
2122 fname
= _cpp_bracket_include (pfile
);
2125 cpp_error (pfile
, CPP_DL_ERROR
,
2126 "operator \"__has_include__\" requires a header string");
2130 int angle_brackets
= (bracket
? 1 : 0);
2132 if (_cpp_has_header (pfile
, fname
, angle_brackets
, type
))
2140 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
2141 cpp_error (pfile
, CPP_DL_ERROR
,
2142 "missing ')' after \"__has_include__\"");
2144 /* A possible controlling macro of the form #if !__has_include__ ().
2145 _cpp_parse_expr checks there was no other junk on the line. */
2147 pfile
->mi_ind_cmacro
= node
;
2149 pfile
->state
.in__has_include__
--;
2154 /* Handle meeting "__has_attribute__" in a preprocessor expression. */
2156 parse_has_attribute (cpp_reader
*pfile
)
2158 pfile
->state
.in__has_attribute__
++;
2161 result
.unsignedp
= false;
2163 result
.overflow
= false;
2165 result
.low
= pfile
->cb
.has_attribute (pfile
);
2167 pfile
->state
.in__has_attribute__
--;