1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2015 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
);
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 describing the suffix. */
91 interpret_float_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
94 size_t f
, d
, l
, w
, q
, i
;
97 f
= d
= l
= w
= q
= i
= 0;
99 /* Process decimal float suffixes, which are two letters starting
100 with d or D. Order and case are significant. */
101 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
103 bool uppercase
= (*s
== 'D');
106 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
107 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
108 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
109 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
110 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
111 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
113 /* Additional two-character suffixes beginning with D are not
114 for decimal float constants. */
119 if (CPP_OPTION (pfile
, ext_numeric_literals
))
121 /* Recognize a fixed-point suffix. */
125 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
126 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
130 /* Continue processing a fixed-point suffix. The suffix is case
131 insensitive except for ll or LL. Order is significant. */
138 if (*s
== 'u' || *s
== 'U')
140 flags
|= CPP_N_UNSIGNED
;
151 return flags
|= CPP_N_SMALL
;
155 return flags
|= CPP_N_MEDIUM
;
156 if (len
== 2 && s
[1] == 'l')
157 return flags
|= CPP_N_LARGE
;
161 return flags
|= CPP_N_MEDIUM
;
162 if (len
== 2 && s
[1] == 'L')
163 return flags
|= CPP_N_LARGE
;
168 /* Anything left at this point is invalid. */
173 /* In any remaining valid suffix, the case and order don't matter. */
177 case 'f': case 'F': f
++; break;
178 case 'd': case 'D': d
++; break;
179 case 'l': case 'L': l
++; break;
180 case 'w': case 'W': w
++; break;
181 case 'q': case 'Q': q
++; break;
183 case 'j': case 'J': i
++; break;
188 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
191 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
194 if ((w
|| q
) && !CPP_OPTION (pfile
, ext_numeric_literals
))
197 return ((i
? CPP_N_IMAGINARY
: 0)
202 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
205 /* Return the classification flags for a float suffix. */
207 cpp_interpret_float_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
209 return interpret_float_suffix (pfile
, (const unsigned char *)s
, len
);
212 /* Subroutine of cpp_classify_number. S points to an integer suffix
213 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
214 flag vector describing the suffix. */
216 interpret_int_suffix (cpp_reader
*pfile
, const uchar
*s
, size_t len
)
225 case 'u': case 'U': u
++; break;
227 case 'j': case 'J': i
++; break;
228 case 'l': case 'L': l
++;
229 /* If there are two Ls, they must be adjacent and the same case. */
230 if (l
== 2 && s
[len
] != s
[len
+ 1])
237 if (l
> 2 || u
> 1 || i
> 1)
240 if (i
&& !CPP_OPTION (pfile
, ext_numeric_literals
))
243 return ((i
? CPP_N_IMAGINARY
: 0)
244 | (u
? CPP_N_UNSIGNED
: 0)
245 | ((l
== 0) ? CPP_N_SMALL
246 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
249 /* Return the classification flags for an int suffix. */
251 cpp_interpret_int_suffix (cpp_reader
*pfile
, const char *s
, size_t len
)
253 return interpret_int_suffix (pfile
, (const unsigned char *)s
, len
);
256 /* Return the string type corresponding to the the input user-defined string
257 literal type. If the input type is not a user-defined string literal
258 type return the input type. */
260 cpp_userdef_string_remove_type (enum cpp_ttype type
)
262 if (type
== CPP_STRING_USERDEF
)
264 else if (type
== CPP_WSTRING_USERDEF
)
266 else if (type
== CPP_STRING16_USERDEF
)
268 else if (type
== CPP_STRING32_USERDEF
)
270 else if (type
== CPP_UTF8STRING_USERDEF
)
271 return CPP_UTF8STRING
;
276 /* Return the user-defined string literal type corresponding to the input
277 string type. If the input type is not a string type return the input
280 cpp_userdef_string_add_type (enum cpp_ttype type
)
282 if (type
== CPP_STRING
)
283 return CPP_STRING_USERDEF
;
284 else if (type
== CPP_WSTRING
)
285 return CPP_WSTRING_USERDEF
;
286 else if (type
== CPP_STRING16
)
287 return CPP_STRING16_USERDEF
;
288 else if (type
== CPP_STRING32
)
289 return CPP_STRING32_USERDEF
;
290 else if (type
== CPP_UTF8STRING
)
291 return CPP_UTF8STRING_USERDEF
;
296 /* Return the char type corresponding to the the input user-defined char
297 literal type. If the input type is not a user-defined char literal
298 type return the input type. */
300 cpp_userdef_char_remove_type (enum cpp_ttype type
)
302 if (type
== CPP_CHAR_USERDEF
)
304 else if (type
== CPP_WCHAR_USERDEF
)
306 else if (type
== CPP_CHAR16_USERDEF
)
308 else if (type
== CPP_CHAR32_USERDEF
)
310 else if (type
== CPP_UTF8CHAR_USERDEF
)
316 /* Return the user-defined char literal type corresponding to the input
317 char type. If the input type is not a char type return the input
320 cpp_userdef_char_add_type (enum cpp_ttype type
)
322 if (type
== CPP_CHAR
)
323 return CPP_CHAR_USERDEF
;
324 else if (type
== CPP_WCHAR
)
325 return CPP_WCHAR_USERDEF
;
326 else if (type
== CPP_CHAR16
)
327 return CPP_CHAR16_USERDEF
;
328 else if (type
== CPP_CHAR32
)
329 return CPP_CHAR32_USERDEF
;
330 else if (type
== CPP_UTF8CHAR
)
331 return CPP_UTF8CHAR_USERDEF
;
336 /* Return true if the token type is a user-defined string literal. */
338 cpp_userdef_string_p (enum cpp_ttype type
)
340 if (type
== CPP_STRING_USERDEF
341 || type
== CPP_WSTRING_USERDEF
342 || type
== CPP_STRING16_USERDEF
343 || type
== CPP_STRING32_USERDEF
344 || type
== CPP_UTF8STRING_USERDEF
)
350 /* Return true if the token type is a user-defined char literal. */
352 cpp_userdef_char_p (enum cpp_ttype type
)
354 if (type
== CPP_CHAR_USERDEF
355 || type
== CPP_WCHAR_USERDEF
356 || type
== CPP_CHAR16_USERDEF
357 || type
== CPP_CHAR32_USERDEF
358 || type
== CPP_UTF8CHAR_USERDEF
)
364 /* Extract the suffix from a user-defined literal string or char. */
366 cpp_get_userdef_suffix (const cpp_token
*tok
)
368 unsigned int len
= tok
->val
.str
.len
;
369 const char *text
= (const char *)tok
->val
.str
.text
;
372 for (i
= 0; i
< len
; ++i
)
373 if (text
[i
] == '\'' || text
[i
] == '"')
378 for (i
= len
; i
> 0; --i
)
379 if (text
[i
- 1] == delim
)
384 /* Categorize numeric constants according to their field (integer,
385 floating point, or invalid), radix (decimal, octal, hexadecimal),
388 TOKEN is the token that represents the numeric constant to
391 In C++0X if UD_SUFFIX is non null it will be assigned
392 any unrecognized suffix for a user-defined literal.
394 VIRTUAL_LOCATION is the virtual location for TOKEN. */
396 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
397 const char **ud_suffix
, source_location virtual_location
)
399 const uchar
*str
= token
->val
.str
.text
;
401 unsigned int max_digit
, result
, radix
;
402 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
409 /* If the lexer has done its job, length one can only be a single
410 digit. Fast-path this very common case. */
411 if (token
->val
.str
.len
== 1)
412 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
414 limit
= str
+ token
->val
.str
.len
;
415 float_flag
= NOT_FLOAT
;
419 seen_digit_sep
= false;
421 /* First, interpret the radix. */
427 /* Require at least one hex digit to classify it as hex. */
428 if (*str
== 'x' || *str
== 'X')
430 if (str
[1] == '.' || ISXDIGIT (str
[1]))
435 else if (DIGIT_SEP (str
[1]))
436 SYNTAX_ERROR_AT (virtual_location
,
437 "digit separator after base indicator");
439 else if (*str
== 'b' || *str
== 'B')
441 if (str
[1] == '0' || str
[1] == '1')
446 else if (DIGIT_SEP (str
[1]))
447 SYNTAX_ERROR_AT (virtual_location
,
448 "digit separator after base indicator");
452 /* Now scan for a well-formed integer or float. */
455 unsigned int c
= *str
++;
457 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
459 seen_digit_sep
= false;
465 else if (DIGIT_SEP (c
))
468 SYNTAX_ERROR_AT (virtual_location
, "adjacent digit separators");
469 seen_digit_sep
= true;
473 if (seen_digit_sep
|| DIGIT_SEP (*str
))
474 SYNTAX_ERROR_AT (virtual_location
,
475 "digit separator adjacent to decimal point");
476 seen_digit_sep
= false;
477 if (float_flag
== NOT_FLOAT
)
478 float_flag
= AFTER_POINT
;
480 SYNTAX_ERROR_AT (virtual_location
,
481 "too many decimal points in number");
483 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
484 || (radix
== 16 && (c
== 'p' || c
== 'P')))
486 if (seen_digit_sep
|| DIGIT_SEP (*str
))
487 SYNTAX_ERROR_AT (virtual_location
,
488 "digit separator adjacent to exponent");
489 float_flag
= AFTER_EXPON
;
494 /* Start of suffix. */
500 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
501 SYNTAX_ERROR_AT (virtual_location
,
502 "digit separator outside digit sequence");
504 /* The suffix may be for decimal fixed-point constants without exponent. */
505 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
507 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
508 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
510 result
|= CPP_N_FLOATING
;
511 /* We need to restore the radix to 10, if the radix is 8. */
515 if (CPP_PEDANTIC (pfile
))
516 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
517 "fixed-point constants are a GCC extension");
524 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
527 if (max_digit
>= radix
)
530 SYNTAX_ERROR2_AT (virtual_location
,
531 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
533 SYNTAX_ERROR2_AT (virtual_location
,
534 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
537 if (float_flag
!= NOT_FLOAT
)
541 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
542 "invalid prefix \"0b\" for floating constant");
543 return CPP_N_INVALID
;
546 if (radix
== 16 && !seen_digit
)
547 SYNTAX_ERROR_AT (virtual_location
,
548 "no digits in hexadecimal floating constant");
550 if (radix
== 16 && CPP_PEDANTIC (pfile
)
551 && !CPP_OPTION (pfile
, extended_numbers
))
553 if (CPP_OPTION (pfile
, cplusplus
))
554 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
555 "use of C++11 hexadecimal floating constant");
557 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
558 "use of C99 hexadecimal floating constant");
561 if (float_flag
== AFTER_EXPON
)
563 if (*str
== '+' || *str
== '-')
566 /* Exponent is decimal, even if string is a hex float. */
569 if (DIGIT_SEP (*str
))
570 SYNTAX_ERROR_AT (virtual_location
,
571 "digit separator adjacent to exponent");
573 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
577 seen_digit_sep
= DIGIT_SEP (*str
);
580 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
582 else if (radix
== 16)
583 SYNTAX_ERROR_AT (virtual_location
,
584 "hexadecimal floating constants require an exponent");
587 SYNTAX_ERROR_AT (virtual_location
,
588 "digit separator outside digit sequence");
590 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
593 if (CPP_OPTION (pfile
, user_literals
))
596 *ud_suffix
= (const char *) str
;
597 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
601 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
602 "invalid suffix \"%.*s\" on floating constant",
603 (int) (limit
- str
), str
);
604 return CPP_N_INVALID
;
608 /* Traditional C didn't accept any floating suffixes. */
610 && CPP_WTRADITIONAL (pfile
)
611 && ! cpp_sys_macro_p (pfile
))
612 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
613 "traditional C rejects the \"%.*s\" suffix",
614 (int) (limit
- str
), str
);
616 /* A suffix for double is a GCC extension via decimal float support.
617 If the suffix also specifies an imaginary value we'll catch that
619 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
620 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
621 "suffix for double constant is a GCC extension");
623 /* Radix must be 10 for decimal floats. */
624 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
626 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
627 "invalid suffix \"%.*s\" with hexadecimal floating constant",
628 (int) (limit
- str
), str
);
629 return CPP_N_INVALID
;
632 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
633 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
634 "fixed-point constants are a GCC extension");
636 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
637 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
638 "decimal float constants are a GCC extension");
640 result
|= CPP_N_FLOATING
;
644 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
647 if (CPP_OPTION (pfile
, user_literals
))
650 *ud_suffix
= (const char *) str
;
651 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
655 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
656 "invalid suffix \"%.*s\" on integer constant",
657 (int) (limit
- str
), str
);
658 return CPP_N_INVALID
;
662 /* Traditional C only accepted the 'L' suffix.
663 Suppress warning about 'LL' with -Wno-long-long. */
664 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
666 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
667 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
668 && CPP_OPTION (pfile
, cpp_warn_long_long
);
671 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
673 "traditional C rejects the \"%.*s\" suffix",
674 (int) (limit
- str
), str
);
677 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
678 && CPP_OPTION (pfile
, cpp_warn_long_long
))
680 const char *message
= CPP_OPTION (pfile
, cplusplus
)
681 ? N_("use of C++11 long long integer constant")
682 : N_("use of C99 long long integer constant");
684 if (CPP_OPTION (pfile
, c99
))
685 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
688 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
689 virtual_location
, 0, message
);
692 result
|= CPP_N_INTEGER
;
696 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
697 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
698 "imaginary constants are a GCC extension");
700 && !CPP_OPTION (pfile
, binary_constants
)
701 && CPP_PEDANTIC (pfile
))
702 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
703 CPP_OPTION (pfile
, cplusplus
)
704 ? N_("binary constants are a C++14 feature "
706 : N_("binary constants are a GCC extension"));
709 result
|= CPP_N_DECIMAL
;
710 else if (radix
== 16)
713 result
|= CPP_N_BINARY
;
715 result
|= CPP_N_OCTAL
;
720 return CPP_N_INVALID
;
723 /* cpp_interpret_integer converts an integer constant into a cpp_num,
724 of precision options->precision.
726 We do not provide any interface for decimal->float conversion,
727 because the preprocessor doesn't need it and we don't want to
728 drag in GCC's floating point emulator. */
730 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
733 const uchar
*p
, *end
;
738 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
739 result
.overflow
= false;
741 p
= token
->val
.str
.text
;
742 end
= p
+ token
->val
.str
.len
;
744 /* Common case of a single digit. */
745 if (token
->val
.str
.len
== 1)
746 result
.low
= p
[0] - '0';
750 size_t precision
= CPP_OPTION (pfile
, precision
);
751 unsigned int base
= 10, c
= 0;
752 bool overflow
= false;
754 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
759 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
764 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
770 /* We can add a digit to numbers strictly less than this without
771 needing the precision and slowness of double integers. */
772 max
= ~(cpp_num_part
) 0;
773 if (precision
< PART_PRECISION
)
774 max
>>= PART_PRECISION
- precision
;
775 max
= (max
- base
+ 1) / base
+ 1;
781 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
783 else if (DIGIT_SEP (c
))
788 /* Strict inequality for when max is set to zero. */
789 if (result
.low
< max
)
790 result
.low
= result
.low
* base
+ c
;
793 result
= append_digit (result
, c
, base
, precision
);
794 overflow
|= result
.overflow
;
799 if (overflow
&& !(type
& CPP_N_USERDEF
))
800 cpp_error (pfile
, CPP_DL_PEDWARN
,
801 "integer constant is too large for its type");
802 /* If too big to be signed, consider it unsigned. Only warn for
803 decimal numbers. Traditional numbers were always signed (but
804 we still honor an explicit U suffix); but we only have
805 traditional semantics in directives. */
806 else if (!result
.unsignedp
807 && !(CPP_OPTION (pfile
, traditional
)
808 && pfile
->state
.in_directive
)
809 && !num_positive (result
, precision
))
811 /* This is for constants within the range of uintmax_t but
812 not that of intmax_t. For such decimal constants, a
813 diagnostic is required for C99 as the selected type must
814 be signed and not having a type is a constraint violation
815 (DR#298, TC3), so this must be a pedwarn. For C90,
816 unsigned long is specified to be used for a constant that
817 does not fit in signed long; if uintmax_t has the same
818 range as unsigned long this means only a warning is
819 appropriate here. C90 permits the preprocessor to use a
820 wider range than unsigned long in the compiler, so if
821 uintmax_t is wider than unsigned long no diagnostic is
822 required for such constants in preprocessor #if
823 expressions and the compiler will pedwarn for such
824 constants outside the range of unsigned long that reach
825 the compiler so a diagnostic is not required there
826 either; thus, pedwarn for C99 but use a plain warning for
829 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
832 "integer constant is so large that it is unsigned");
833 result
.unsignedp
= true;
840 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
842 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
847 cpp_num_part add_high
, add_low
;
849 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
850 need to worry about add_high overflowing. */
864 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
865 result
.high
= num
.high
<< shift
;
866 result
.low
= num
.low
<< shift
;
867 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
868 result
.unsignedp
= num
.unsignedp
;
872 add_low
= num
.low
<< 1;
873 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
876 add_high
= add_low
= 0;
878 if (add_low
+ digit
< add_low
)
882 if (result
.low
+ add_low
< result
.low
)
884 if (result
.high
+ add_high
< result
.high
)
887 result
.low
+= add_low
;
888 result
.high
+= add_high
;
889 result
.overflow
= overflow
;
891 /* The above code catches overflow of a cpp_num type. This catches
892 overflow of the (possibly shorter) target precision. */
893 num
.low
= result
.low
;
894 num
.high
= result
.high
;
895 result
= num_trim (result
, precision
);
896 if (!num_eq (result
, num
))
897 result
.overflow
= true;
902 /* Handle meeting "defined" in a preprocessor expression. */
904 parse_defined (cpp_reader
*pfile
)
908 cpp_hashnode
*node
= 0;
909 const cpp_token
*token
;
910 cpp_context
*initial_context
= pfile
->context
;
912 /* Don't expand macros. */
913 pfile
->state
.prevent_expansion
++;
915 token
= cpp_get_token (pfile
);
916 if (token
->type
== CPP_OPEN_PAREN
)
919 token
= cpp_get_token (pfile
);
922 if (token
->type
== CPP_NAME
)
924 node
= token
->val
.node
.node
;
925 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
927 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
933 cpp_error (pfile
, CPP_DL_ERROR
,
934 "operator \"defined\" requires an identifier");
935 if (token
->flags
& NAMED_OP
)
940 op
.type
= token
->type
;
941 cpp_error (pfile
, CPP_DL_ERROR
,
942 "(\"%s\" is an alternative token for \"%s\" in C++)",
943 cpp_token_as_text (pfile
, token
),
944 cpp_token_as_text (pfile
, &op
));
950 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
951 cpp_error (pfile
, CPP_DL_WARNING
,
952 "this use of \"defined\" may not be portable");
954 _cpp_mark_macro_used (node
);
955 if (!(node
->flags
& NODE_USED
))
957 node
->flags
|= NODE_USED
;
958 if (node
->type
== NT_MACRO
)
960 if ((node
->flags
& NODE_BUILTIN
)
961 && pfile
->cb
.user_builtin_macro
)
962 pfile
->cb
.user_builtin_macro (pfile
, node
);
963 if (pfile
->cb
.used_define
)
964 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
968 if (pfile
->cb
.used_undef
)
969 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
973 /* A possible controlling macro of the form #if !defined ().
974 _cpp_parse_expr checks there was no other junk on the line. */
975 pfile
->mi_ind_cmacro
= node
;
978 pfile
->state
.prevent_expansion
--;
980 /* Do not treat conditional macros as being defined. This is due to the
981 powerpc and spu ports using conditional macros for 'vector', 'bool', and
982 'pixel' to act as conditional keywords. This messes up tests like #ifndef
984 result
.unsignedp
= false;
986 result
.overflow
= false;
987 result
.low
= (node
&& node
->type
== NT_MACRO
988 && (node
->flags
& NODE_CONDITIONAL
) == 0);
992 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
993 number or character constant, or the result of the "defined" or "#"
996 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
997 source_location virtual_location
)
1003 result
.unsignedp
= false;
1004 result
.overflow
= false;
1006 switch (token
->type
)
1009 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1010 if (temp
& CPP_N_USERDEF
)
1011 cpp_error (pfile
, CPP_DL_ERROR
,
1012 "user-defined literal in preprocessor expression");
1013 switch (temp
& CPP_N_CATEGORY
)
1015 case CPP_N_FLOATING
:
1016 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1017 "floating constant in preprocessor expression");
1020 if (!(temp
& CPP_N_IMAGINARY
))
1021 return cpp_interpret_integer (pfile
, token
, temp
);
1022 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1023 "imaginary number in preprocessor expression");
1027 /* Error already issued. */
1030 result
.high
= result
.low
= 0;
1039 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1044 /* Sign-extend the result if necessary. */
1045 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1047 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1048 result
.low
|= ~(~(cpp_num_part
) 0
1049 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1050 result
.high
= ~(cpp_num_part
) 0;
1051 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1057 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1058 return parse_defined (pfile
);
1059 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include__
)
1060 return parse_has_include (pfile
, IT_INCLUDE
);
1061 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include_next__
)
1062 return parse_has_include (pfile
, IT_INCLUDE_NEXT
);
1063 else if (CPP_OPTION (pfile
, cplusplus
)
1064 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1065 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1068 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1074 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1075 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1076 "\"%s\" is not defined",
1077 NODE_NAME (token
->val
.node
.node
));
1082 if (!pfile
->state
.skipping
)
1084 /* A pedantic warning takes precedence over a deprecated
1086 if (CPP_PEDANTIC (pfile
))
1087 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1088 virtual_location
, 0,
1089 "assertions are a GCC extension");
1090 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1091 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1092 "assertions are a deprecated extension");
1094 _cpp_test_assertion (pfile
, &temp
);
1103 result
.unsignedp
= !!unsignedp
;
1107 /* Operator precedence and flags table.
1109 After an operator is returned from the lexer, if it has priority less
1110 than the operator on the top of the stack, we reduce the stack by one
1111 operator and repeat the test. Since equal priorities do not reduce,
1112 this is naturally right-associative.
1114 We handle left-associative operators by decrementing the priority of
1115 just-lexed operators by one, but retaining the priority of operators
1116 already on the stack.
1118 The remaining cases are '(' and ')'. We handle '(' by skipping the
1119 reduction phase completely. ')' is given lower priority than
1120 everything else, including '(', effectively forcing a reduction of the
1121 parenthesized expression. If there is a matching '(', the routine
1122 reduce() exits immediately. If the normal exit route sees a ')', then
1123 there cannot have been a matching '(' and an error message is output.
1125 The parser assumes all shifted operators require a left operand unless
1126 the flag NO_L_OPERAND is set. These semantics are automatic; any
1127 extra semantics need to be handled with operator-specific code. */
1129 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1130 operand changes because of integer promotions. */
1131 #define NO_L_OPERAND (1 << 0)
1132 #define LEFT_ASSOC (1 << 1)
1133 #define CHECK_PROMOTION (1 << 2)
1135 /* Operator to priority map. Must be in the same order as the first
1136 N entries of enum cpp_ttype. */
1137 static const struct cpp_operator
1143 /* EQ */ {0, 0}, /* Shouldn't happen. */
1144 /* NOT */ {16, NO_L_OPERAND
},
1145 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1146 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1147 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1148 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1149 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1150 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1151 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1152 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1153 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1154 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1155 /* RSHIFT */ {13, LEFT_ASSOC
},
1156 /* LSHIFT */ {13, LEFT_ASSOC
},
1158 /* COMPL */ {16, NO_L_OPERAND
},
1159 /* AND_AND */ {6, LEFT_ASSOC
},
1160 /* OR_OR */ {5, LEFT_ASSOC
},
1161 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1162 However, there are some special cases for these in reduce(). */
1164 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1165 /* COMMA */ {4, LEFT_ASSOC
},
1166 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1167 /* CLOSE_PAREN */ {0, 0},
1169 /* EQ_EQ */ {11, LEFT_ASSOC
},
1170 /* NOT_EQ */ {11, LEFT_ASSOC
},
1171 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1172 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1173 /* UPLUS */ {16, NO_L_OPERAND
},
1174 /* UMINUS */ {16, NO_L_OPERAND
}
1177 /* Parse and evaluate a C expression, reading from PFILE.
1178 Returns the truth value of the expression.
1180 The implementation is an operator precedence parser, i.e. a
1181 bottom-up parser, using a stack for not-yet-reduced tokens.
1183 The stack base is op_stack, and the current stack pointer is 'top'.
1184 There is a stack element for each operator (only), and the most
1185 recently pushed operator is 'top->op'. An operand (value) is
1186 stored in the 'value' field of the stack element of the operator
1187 that precedes it. */
1189 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1191 struct op
*top
= pfile
->op_stack
;
1192 unsigned int lex_count
;
1193 bool saw_leading_not
, want_value
= true;
1194 source_location virtual_location
= 0;
1196 pfile
->state
.skip_eval
= 0;
1198 /* Set up detection of #if ! defined(). */
1199 pfile
->mi_ind_cmacro
= 0;
1200 saw_leading_not
= false;
1203 /* Lowest priority operator prevents further reductions. */
1211 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1212 op
.op
= op
.token
->type
;
1213 op
.loc
= virtual_location
;
1217 /* These tokens convert into values. */
1227 SYNTAX_ERROR2_AT (op
.loc
,
1228 "missing binary operator before token \"%s\"",
1229 cpp_token_as_text (pfile
, op
.token
));
1231 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1235 saw_leading_not
= lex_count
== 1;
1247 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1248 SYNTAX_ERROR2_AT (op
.loc
,
1249 "token \"%s\" is not valid in preprocessor expressions",
1250 cpp_token_as_text (pfile
, op
.token
));
1254 /* Check we have a value or operator as appropriate. */
1255 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1258 SYNTAX_ERROR2_AT (op
.loc
,
1259 "missing binary operator before token \"%s\"",
1260 cpp_token_as_text (pfile
, op
.token
));
1262 else if (want_value
)
1264 /* We want a number (or expression) and haven't got one.
1265 Try to emit a specific diagnostic. */
1266 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1267 SYNTAX_ERROR_AT (op
.loc
,
1268 "missing expression between '(' and ')'");
1270 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1271 SYNTAX_ERROR2_AT (op
.loc
,
1272 "%s with no expression", is_if
? "#if" : "#elif");
1274 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1275 SYNTAX_ERROR2_AT (op
.loc
,
1276 "operator '%s' has no right operand",
1277 cpp_token_as_text (pfile
, top
->token
));
1278 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1279 /* Complain about missing paren during reduction. */;
1281 SYNTAX_ERROR2_AT (op
.loc
,
1282 "operator '%s' has no left operand",
1283 cpp_token_as_text (pfile
, op
.token
));
1286 top
= reduce (pfile
, top
, op
.op
);
1290 if (op
.op
== CPP_EOF
)
1295 case CPP_CLOSE_PAREN
:
1298 if (!num_zerop (top
->value
))
1299 pfile
->state
.skip_eval
++;
1303 if (num_zerop (top
->value
))
1304 pfile
->state
.skip_eval
++;
1307 if (top
->op
!= CPP_QUERY
)
1308 SYNTAX_ERROR_AT (op
.loc
,
1309 " ':' without preceding '?'");
1310 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1311 pfile
->state
.skip_eval
++;
1313 pfile
->state
.skip_eval
--;
1320 /* Check for and handle stack overflow. */
1321 if (++top
== pfile
->op_limit
)
1322 top
= _cpp_expand_op_stack (pfile
);
1325 top
->token
= op
.token
;
1329 /* The controlling macro expression is only valid if we called lex 3
1330 times: <!> <defined expression> and <EOF>. push_conditional ()
1331 checks that we are at top-of-file. */
1332 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1333 pfile
->mi_ind_cmacro
= 0;
1335 if (top
!= pfile
->op_stack
)
1337 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1338 "unbalanced stack in %s",
1339 is_if
? "#if" : "#elif");
1341 return false; /* Return false on syntax error. */
1344 return !num_zerop (top
->value
);
1347 /* Reduce the operator / value stack if possible, in preparation for
1348 pushing operator OP. Returns NULL on error, otherwise the top of
1351 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1355 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1358 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1362 if (op
== CPP_OPEN_PAREN
)
1365 /* Decrement the priority of left-associative operators to force a
1366 reduction with operators of otherwise equal priority. */
1367 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1368 while (prio
< optab
[top
->op
].prio
)
1370 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1371 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1372 check_promotion (pfile
, top
);
1380 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1381 top
[-1].loc
= top
->loc
;
1389 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1390 top
->value
, top
->op
);
1391 top
[-1].loc
= top
->loc
;
1396 case CPP_GREATER_EQ
:
1399 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1400 top
[-1].loc
= top
->loc
;
1406 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1407 top
[-1].loc
= top
->loc
;
1414 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1415 top
[-1].loc
= top
->loc
;
1419 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1420 top
[-1].loc
= top
->loc
;
1425 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1426 top
->value
, top
->op
, top
->loc
);
1427 top
[-1].loc
= top
->loc
;
1432 if (!num_zerop (top
->value
))
1433 pfile
->state
.skip_eval
--;
1434 top
->value
.low
= (!num_zerop (top
->value
)
1435 || !num_zerop (top
[1].value
));
1436 top
->value
.high
= 0;
1437 top
->value
.unsignedp
= false;
1438 top
->value
.overflow
= false;
1439 top
->loc
= top
[1].loc
;
1444 if (num_zerop (top
->value
))
1445 pfile
->state
.skip_eval
--;
1446 top
->value
.low
= (!num_zerop (top
->value
)
1447 && !num_zerop (top
[1].value
));
1448 top
->value
.high
= 0;
1449 top
->value
.unsignedp
= false;
1450 top
->value
.overflow
= false;
1451 top
->loc
= top
[1].loc
;
1454 case CPP_OPEN_PAREN
:
1455 if (op
!= CPP_CLOSE_PAREN
)
1457 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1458 top
->token
->src_loc
,
1459 0, "missing ')' in expression");
1463 top
->value
= top
[1].value
;
1464 top
->loc
= top
[1].loc
;
1469 if (!num_zerop (top
->value
))
1471 pfile
->state
.skip_eval
--;
1472 top
->value
= top
[1].value
;
1473 top
->loc
= top
[1].loc
;
1477 top
->value
= top
[2].value
;
1478 top
->loc
= top
[2].loc
;
1480 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1481 || top
[2].value
.unsignedp
);
1485 /* COMMA and COLON should not reduce a QUERY operator. */
1486 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1488 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1496 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1497 cpp_error (pfile
, CPP_DL_PEDWARN
,
1498 "integer overflow in preprocessor expression");
1501 if (op
== CPP_CLOSE_PAREN
)
1503 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1510 /* Returns the position of the old top of stack after expansion. */
1512 _cpp_expand_op_stack (cpp_reader
*pfile
)
1514 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1515 size_t new_size
= old_size
* 2 + 20;
1517 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1518 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1520 return pfile
->op_stack
+ old_size
;
1523 /* Emits a warning if the effective sign of either operand of OP
1524 changes because of integer promotions. */
1526 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1528 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1531 if (op
->value
.unsignedp
)
1533 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1534 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1535 "the left operand of \"%s\" changes sign when promoted",
1536 cpp_token_as_text (pfile
, op
->token
));
1538 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1539 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1540 "the right operand of \"%s\" changes sign when promoted",
1541 cpp_token_as_text (pfile
, op
->token
));
1544 /* Clears the unused high order bits of the number pointed to by PNUM. */
1546 num_trim (cpp_num num
, size_t precision
)
1548 if (precision
> PART_PRECISION
)
1550 precision
-= PART_PRECISION
;
1551 if (precision
< PART_PRECISION
)
1552 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1556 if (precision
< PART_PRECISION
)
1557 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1564 /* True iff A (presumed signed) >= 0. */
1566 num_positive (cpp_num num
, size_t precision
)
1568 if (precision
> PART_PRECISION
)
1570 precision
-= PART_PRECISION
;
1571 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1574 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1577 /* Sign extend a number, with PRECISION significant bits and all
1578 others assumed clear, to fill out a cpp_num structure. */
1580 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1584 if (precision
> PART_PRECISION
)
1586 precision
-= PART_PRECISION
;
1587 if (precision
< PART_PRECISION
1588 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1589 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1591 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1593 if (precision
< PART_PRECISION
)
1594 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1595 num
.high
= ~(cpp_num_part
) 0;
1602 /* Returns the negative of NUM. */
1604 num_negate (cpp_num num
, size_t precision
)
1609 num
.high
= ~num
.high
;
1613 num
= num_trim (num
, precision
);
1614 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1619 /* Returns true if A >= B. */
1621 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1625 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1629 /* Both numbers have signed type. If they are of different
1630 sign, the answer is the sign of A. */
1631 unsignedp
= num_positive (pa
, precision
);
1633 if (unsignedp
!= num_positive (pb
, precision
))
1636 /* Otherwise we can do an unsigned comparison. */
1639 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1642 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1644 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1645 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1647 lhs
.overflow
= false;
1648 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1650 /* As excess precision is zeroed, there is no need to num_trim () as
1651 these operations cannot introduce a set bit there. */
1655 lhs
.high
&= rhs
.high
;
1657 else if (op
== CPP_OR
)
1660 lhs
.high
|= rhs
.high
;
1665 lhs
.high
^= rhs
.high
;
1671 /* Returns LHS OP RHS, where OP is an inequality. */
1673 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1676 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1678 if (op
== CPP_GREATER_EQ
)
1680 else if (op
== CPP_LESS
)
1682 else if (op
== CPP_GREATER
)
1683 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1684 else /* CPP_LESS_EQ. */
1685 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1688 lhs
.overflow
= false;
1689 lhs
.unsignedp
= false;
1693 /* Returns LHS OP RHS, where OP is == or !=. */
1695 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1696 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1698 /* Work around a 3.0.4 bug; see PR 6950. */
1699 bool eq
= num_eq (lhs
, rhs
);
1700 if (op
== CPP_NOT_EQ
)
1704 lhs
.overflow
= false;
1705 lhs
.unsignedp
= false;
1709 /* Shift NUM, of width PRECISION, right by N bits. */
1711 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1713 cpp_num_part sign_mask
;
1714 bool x
= num_positive (num
, precision
);
1716 if (num
.unsignedp
|| x
)
1719 sign_mask
= ~(cpp_num_part
) 0;
1722 num
.high
= num
.low
= sign_mask
;
1726 if (precision
< PART_PRECISION
)
1727 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1728 else if (precision
< 2 * PART_PRECISION
)
1729 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1731 if (n
>= PART_PRECISION
)
1733 n
-= PART_PRECISION
;
1735 num
.high
= sign_mask
;
1740 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1741 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1745 num
= num_trim (num
, precision
);
1746 num
.overflow
= false;
1750 /* Shift NUM, of width PRECISION, left by N bits. */
1752 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1756 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1757 num
.high
= num
.low
= 0;
1761 cpp_num orig
, maybe_orig
;
1765 if (m
>= PART_PRECISION
)
1767 m
-= PART_PRECISION
;
1773 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1776 num
= num_trim (num
, precision
);
1779 num
.overflow
= false;
1782 maybe_orig
= num_rshift (num
, precision
, n
);
1783 num
.overflow
= !num_eq (orig
, maybe_orig
);
1790 /* The four unary operators: +, -, ! and ~. */
1792 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1797 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1798 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1799 "traditional C rejects the unary plus operator");
1800 num
.overflow
= false;
1804 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1808 num
.high
= ~num
.high
;
1810 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1811 num
.overflow
= false;
1814 default: /* case CPP_NOT: */
1815 num
.low
= num_zerop (num
);
1817 num
.overflow
= false;
1818 num
.unsignedp
= false;
1825 /* The various binary operators. */
1827 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1830 size_t precision
= CPP_OPTION (pfile
, precision
);
1838 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1840 /* A negative shift is a positive shift the other way. */
1841 if (op
== CPP_LSHIFT
)
1845 rhs
= num_negate (rhs
, precision
);
1848 n
= ~0; /* Maximal. */
1851 if (op
== CPP_LSHIFT
)
1852 lhs
= num_lshift (lhs
, precision
, n
);
1854 lhs
= num_rshift (lhs
, precision
, n
);
1859 result
.low
= lhs
.low
- rhs
.low
;
1860 result
.high
= lhs
.high
- rhs
.high
;
1861 if (result
.low
> lhs
.low
)
1863 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1864 result
.overflow
= false;
1866 result
= num_trim (result
, precision
);
1867 if (!result
.unsignedp
)
1869 bool lhsp
= num_positive (lhs
, precision
);
1870 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1871 && lhsp
!= num_positive (result
, precision
));
1876 result
.low
= lhs
.low
+ rhs
.low
;
1877 result
.high
= lhs
.high
+ rhs
.high
;
1878 if (result
.low
< lhs
.low
)
1880 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1881 result
.overflow
= false;
1883 result
= num_trim (result
, precision
);
1884 if (!result
.unsignedp
)
1886 bool lhsp
= num_positive (lhs
, precision
);
1887 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1888 && lhsp
!= num_positive (result
, precision
));
1893 default: /* case CPP_COMMA: */
1894 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1895 || !pfile
->state
.skip_eval
))
1896 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
1897 "comma operator in operand of #if");
1905 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1908 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1911 cpp_num_part middle
[2], temp
;
1913 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1914 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1916 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1917 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1920 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1921 if (result
.low
< temp
)
1925 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1926 if (result
.low
< temp
)
1929 result
.high
+= HIGH_PART (middle
[0]);
1930 result
.high
+= HIGH_PART (middle
[1]);
1931 result
.unsignedp
= true;
1932 result
.overflow
= false;
1937 /* Multiply two preprocessing numbers. */
1939 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1941 cpp_num result
, temp
;
1942 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1943 bool overflow
, negate
= false;
1944 size_t precision
= CPP_OPTION (pfile
, precision
);
1946 /* Prepare for unsigned multiplication. */
1949 if (!num_positive (lhs
, precision
))
1950 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1951 if (!num_positive (rhs
, precision
))
1952 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1955 overflow
= lhs
.high
&& rhs
.high
;
1956 result
= num_part_mul (lhs
.low
, rhs
.low
);
1958 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1959 result
.high
+= temp
.low
;
1963 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1964 result
.high
+= temp
.low
;
1968 temp
.low
= result
.low
, temp
.high
= result
.high
;
1969 result
= num_trim (result
, precision
);
1970 if (!num_eq (result
, temp
))
1974 result
= num_negate (result
, precision
);
1977 result
.overflow
= false;
1979 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1980 && !num_zerop (result
));
1981 result
.unsignedp
= unsignedp
;
1986 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1987 or the remainder depending upon OP. LOCATION is the source location
1988 of this operator (for diagnostics). */
1991 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1992 source_location location
)
1994 cpp_num result
, sub
;
1996 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1997 bool negate
= false, lhs_neg
= false;
1998 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
2000 /* Prepare for unsigned division. */
2003 if (!num_positive (lhs
, precision
))
2004 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
2005 if (!num_positive (rhs
, precision
))
2006 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2009 /* Find the high bit. */
2013 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2014 for (; ; i
--, mask
>>= 1)
2015 if (rhs
.high
& mask
)
2020 if (precision
> PART_PRECISION
)
2021 i
= precision
- PART_PRECISION
- 1;
2024 mask
= (cpp_num_part
) 1 << i
;
2025 for (; ; i
--, mask
>>= 1)
2031 if (!pfile
->state
.skip_eval
)
2032 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2033 "division by zero in #if");
2037 /* First nonzero bit of RHS is bit I. Do naive division by
2038 shifting the RHS fully left, and subtracting from LHS if LHS is
2039 at least as big, and then repeating but with one less shift.
2040 This is not very efficient, but is easy to understand. */
2042 rhs
.unsignedp
= true;
2043 lhs
.unsignedp
= true;
2044 i
= precision
- i
- 1;
2045 sub
= num_lshift (rhs
, precision
, i
);
2047 result
.high
= result
.low
= 0;
2050 if (num_greater_eq (lhs
, sub
, precision
))
2052 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2053 if (i
>= PART_PRECISION
)
2054 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2056 result
.low
|= (cpp_num_part
) 1 << i
;
2060 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2064 /* We divide so that the remainder has the sign of the LHS. */
2067 result
.unsignedp
= unsignedp
;
2068 result
.overflow
= false;
2072 result
= num_negate (result
, precision
);
2073 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2074 && !num_zerop (result
));
2081 lhs
.unsignedp
= unsignedp
;
2082 lhs
.overflow
= false;
2084 lhs
= num_negate (lhs
, precision
);
2089 /* Handle meeting "__has_include__" in a preprocessor expression. */
2091 parse_has_include (cpp_reader
*pfile
, enum include_type type
)
2095 cpp_hashnode
*node
= 0;
2096 const cpp_token
*token
;
2097 bool bracket
= false;
2100 result
.unsignedp
= false;
2102 result
.overflow
= false;
2105 pfile
->state
.in__has_include__
++;
2107 token
= cpp_get_token (pfile
);
2108 if (token
->type
== CPP_OPEN_PAREN
)
2111 token
= cpp_get_token (pfile
);
2114 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
2116 if (token
->type
== CPP_HEADER_NAME
)
2118 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
2119 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
2120 fname
[token
->val
.str
.len
- 2] = '\0';
2121 node
= token
->val
.node
.node
;
2123 else if (token
->type
== CPP_LESS
)
2126 fname
= _cpp_bracket_include (pfile
);
2129 cpp_error (pfile
, CPP_DL_ERROR
,
2130 "operator \"__has_include__\" requires a header string");
2134 int angle_brackets
= (bracket
? 1 : 0);
2136 if (_cpp_has_header (pfile
, fname
, angle_brackets
, type
))
2144 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
2145 cpp_error (pfile
, CPP_DL_ERROR
,
2146 "missing ')' after \"__has_include__\"");
2148 /* A possible controlling macro of the form #if !__has_include__ ().
2149 _cpp_parse_expr checks there was no other junk on the line. */
2151 pfile
->mi_ind_cmacro
= node
;
2153 pfile
->state
.in__has_include__
--;