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
);
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
)
314 /* Return the user-defined char literal type corresponding to the input
315 char type. If the input type is not a char type return the input
318 cpp_userdef_char_add_type (enum cpp_ttype type
)
320 if (type
== CPP_CHAR
)
321 return CPP_CHAR_USERDEF
;
322 else if (type
== CPP_WCHAR
)
323 return CPP_WCHAR_USERDEF
;
324 else if (type
== CPP_CHAR16
)
325 return CPP_CHAR16_USERDEF
;
326 else if (type
== CPP_CHAR32
)
327 return CPP_CHAR32_USERDEF
;
332 /* Return true if the token type is a user-defined string literal. */
334 cpp_userdef_string_p (enum cpp_ttype type
)
336 if (type
== CPP_STRING_USERDEF
337 || type
== CPP_WSTRING_USERDEF
338 || type
== CPP_STRING16_USERDEF
339 || type
== CPP_STRING32_USERDEF
340 || type
== CPP_UTF8STRING_USERDEF
)
346 /* Return true if the token type is a user-defined char literal. */
348 cpp_userdef_char_p (enum cpp_ttype type
)
350 if (type
== CPP_CHAR_USERDEF
351 || type
== CPP_WCHAR_USERDEF
352 || type
== CPP_CHAR16_USERDEF
353 || type
== CPP_CHAR32_USERDEF
)
359 /* Extract the suffix from a user-defined literal string or char. */
361 cpp_get_userdef_suffix (const cpp_token
*tok
)
363 unsigned int len
= tok
->val
.str
.len
;
364 const char *text
= (const char *)tok
->val
.str
.text
;
367 for (i
= 0; i
< len
; ++i
)
368 if (text
[i
] == '\'' || text
[i
] == '"')
373 for (i
= len
; i
> 0; --i
)
374 if (text
[i
- 1] == delim
)
379 /* Categorize numeric constants according to their field (integer,
380 floating point, or invalid), radix (decimal, octal, hexadecimal),
383 TOKEN is the token that represents the numeric constant to
386 In C++0X if UD_SUFFIX is non null it will be assigned
387 any unrecognized suffix for a user-defined literal.
389 VIRTUAL_LOCATION is the virtual location for TOKEN. */
391 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
,
392 const char **ud_suffix
, source_location virtual_location
)
394 const uchar
*str
= token
->val
.str
.text
;
396 unsigned int max_digit
, result
, radix
;
397 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
404 /* If the lexer has done its job, length one can only be a single
405 digit. Fast-path this very common case. */
406 if (token
->val
.str
.len
== 1)
407 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
409 limit
= str
+ token
->val
.str
.len
;
410 float_flag
= NOT_FLOAT
;
414 seen_digit_sep
= false;
416 /* First, interpret the radix. */
422 /* Require at least one hex digit to classify it as hex. */
423 if (*str
== 'x' || *str
== 'X')
425 if (str
[1] == '.' || ISXDIGIT (str
[1]))
430 else if (DIGIT_SEP (str
[1]))
431 SYNTAX_ERROR_AT (virtual_location
,
432 "digit separator after base indicator");
434 else if (*str
== 'b' || *str
== 'B')
436 if (str
[1] == '0' || str
[1] == '1')
441 else if (DIGIT_SEP (str
[1]))
442 SYNTAX_ERROR_AT (virtual_location
,
443 "digit separator after base indicator");
447 /* Now scan for a well-formed integer or float. */
450 unsigned int c
= *str
++;
452 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
454 seen_digit_sep
= false;
460 else if (DIGIT_SEP (c
))
463 SYNTAX_ERROR_AT (virtual_location
, "adjacent digit separators");
464 seen_digit_sep
= true;
468 if (seen_digit_sep
|| DIGIT_SEP (*str
))
469 SYNTAX_ERROR_AT (virtual_location
,
470 "digit separator adjacent to decimal point");
471 seen_digit_sep
= false;
472 if (float_flag
== NOT_FLOAT
)
473 float_flag
= AFTER_POINT
;
475 SYNTAX_ERROR_AT (virtual_location
,
476 "too many decimal points in number");
478 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
479 || (radix
== 16 && (c
== 'p' || c
== 'P')))
481 if (seen_digit_sep
|| DIGIT_SEP (*str
))
482 SYNTAX_ERROR_AT (virtual_location
,
483 "digit separator adjacent to exponent");
484 float_flag
= AFTER_EXPON
;
489 /* Start of suffix. */
495 if (seen_digit_sep
&& float_flag
!= AFTER_EXPON
)
496 SYNTAX_ERROR_AT (virtual_location
,
497 "digit separator outside digit sequence");
499 /* The suffix may be for decimal fixed-point constants without exponent. */
500 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
502 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
503 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
505 result
|= CPP_N_FLOATING
;
506 /* We need to restore the radix to 10, if the radix is 8. */
510 if (CPP_PEDANTIC (pfile
))
511 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
512 "fixed-point constants are a GCC extension");
519 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
522 if (max_digit
>= radix
)
525 SYNTAX_ERROR2_AT (virtual_location
,
526 "invalid digit \"%c\" in binary constant", '0' + max_digit
);
528 SYNTAX_ERROR2_AT (virtual_location
,
529 "invalid digit \"%c\" in octal constant", '0' + max_digit
);
532 if (float_flag
!= NOT_FLOAT
)
536 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
537 "invalid prefix \"0b\" for floating constant");
538 return CPP_N_INVALID
;
541 if (radix
== 16 && !seen_digit
)
542 SYNTAX_ERROR_AT (virtual_location
,
543 "no digits in hexadecimal floating constant");
545 if (radix
== 16 && CPP_PEDANTIC (pfile
)
546 && !CPP_OPTION (pfile
, extended_numbers
))
548 if (CPP_OPTION (pfile
, cplusplus
))
549 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
550 "use of C++11 hexadecimal floating constant");
552 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
553 "use of C99 hexadecimal floating constant");
556 if (float_flag
== AFTER_EXPON
)
558 if (*str
== '+' || *str
== '-')
561 /* Exponent is decimal, even if string is a hex float. */
564 if (DIGIT_SEP (*str
))
565 SYNTAX_ERROR_AT (virtual_location
,
566 "digit separator adjacent to exponent");
568 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
572 seen_digit_sep
= DIGIT_SEP (*str
);
575 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
577 else if (radix
== 16)
578 SYNTAX_ERROR_AT (virtual_location
,
579 "hexadecimal floating constants require an exponent");
582 SYNTAX_ERROR_AT (virtual_location
,
583 "digit separator outside digit sequence");
585 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
588 if (CPP_OPTION (pfile
, user_literals
))
591 *ud_suffix
= (const char *) str
;
592 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
596 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
597 "invalid suffix \"%.*s\" on floating constant",
598 (int) (limit
- str
), str
);
599 return CPP_N_INVALID
;
603 /* Traditional C didn't accept any floating suffixes. */
605 && CPP_WTRADITIONAL (pfile
)
606 && ! cpp_sys_macro_p (pfile
))
607 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
608 "traditional C rejects the \"%.*s\" suffix",
609 (int) (limit
- str
), str
);
611 /* A suffix for double is a GCC extension via decimal float support.
612 If the suffix also specifies an imaginary value we'll catch that
614 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
615 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
616 "suffix for double constant is a GCC extension");
618 /* Radix must be 10 for decimal floats. */
619 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
621 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
622 "invalid suffix \"%.*s\" with hexadecimal floating constant",
623 (int) (limit
- str
), str
);
624 return CPP_N_INVALID
;
627 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
628 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
629 "fixed-point constants are a GCC extension");
631 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
632 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
633 "decimal float constants are a GCC extension");
635 result
|= CPP_N_FLOATING
;
639 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
642 if (CPP_OPTION (pfile
, user_literals
))
645 *ud_suffix
= (const char *) str
;
646 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
650 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
651 "invalid suffix \"%.*s\" on integer constant",
652 (int) (limit
- str
), str
);
653 return CPP_N_INVALID
;
657 /* Traditional C only accepted the 'L' suffix.
658 Suppress warning about 'LL' with -Wno-long-long. */
659 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
661 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
662 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
663 && CPP_OPTION (pfile
, cpp_warn_long_long
);
666 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
668 "traditional C rejects the \"%.*s\" suffix",
669 (int) (limit
- str
), str
);
672 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
673 && CPP_OPTION (pfile
, cpp_warn_long_long
))
675 const char *message
= CPP_OPTION (pfile
, cplusplus
)
676 ? N_("use of C++11 long long integer constant")
677 : N_("use of C99 long long integer constant");
679 if (CPP_OPTION (pfile
, c99
))
680 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
683 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
684 virtual_location
, 0, message
);
687 result
|= CPP_N_INTEGER
;
691 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
692 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
693 "imaginary constants are a GCC extension");
695 && !CPP_OPTION (pfile
, binary_constants
)
696 && CPP_PEDANTIC (pfile
))
697 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
698 CPP_OPTION (pfile
, cplusplus
)
699 ? "binary constants are a C++14 feature "
701 : "binary constants are a GCC extension");
704 result
|= CPP_N_DECIMAL
;
705 else if (radix
== 16)
708 result
|= CPP_N_BINARY
;
710 result
|= CPP_N_OCTAL
;
715 return CPP_N_INVALID
;
718 /* cpp_interpret_integer converts an integer constant into a cpp_num,
719 of precision options->precision.
721 We do not provide any interface for decimal->float conversion,
722 because the preprocessor doesn't need it and we don't want to
723 drag in GCC's floating point emulator. */
725 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
728 const uchar
*p
, *end
;
733 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
734 result
.overflow
= false;
736 p
= token
->val
.str
.text
;
737 end
= p
+ token
->val
.str
.len
;
739 /* Common case of a single digit. */
740 if (token
->val
.str
.len
== 1)
741 result
.low
= p
[0] - '0';
745 size_t precision
= CPP_OPTION (pfile
, precision
);
746 unsigned int base
= 10, c
= 0;
747 bool overflow
= false;
749 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
754 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
759 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
765 /* We can add a digit to numbers strictly less than this without
766 needing the precision and slowness of double integers. */
767 max
= ~(cpp_num_part
) 0;
768 if (precision
< PART_PRECISION
)
769 max
>>= PART_PRECISION
- precision
;
770 max
= (max
- base
+ 1) / base
+ 1;
776 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
778 else if (DIGIT_SEP (c
))
783 /* Strict inequality for when max is set to zero. */
784 if (result
.low
< max
)
785 result
.low
= result
.low
* base
+ c
;
788 result
= append_digit (result
, c
, base
, precision
);
789 overflow
|= result
.overflow
;
794 if (overflow
&& !(type
& CPP_N_USERDEF
))
795 cpp_error (pfile
, CPP_DL_PEDWARN
,
796 "integer constant is too large for its type");
797 /* If too big to be signed, consider it unsigned. Only warn for
798 decimal numbers. Traditional numbers were always signed (but
799 we still honor an explicit U suffix); but we only have
800 traditional semantics in directives. */
801 else if (!result
.unsignedp
802 && !(CPP_OPTION (pfile
, traditional
)
803 && pfile
->state
.in_directive
)
804 && !num_positive (result
, precision
))
806 /* This is for constants within the range of uintmax_t but
807 not that of intmax_t. For such decimal constants, a
808 diagnostic is required for C99 as the selected type must
809 be signed and not having a type is a constraint violation
810 (DR#298, TC3), so this must be a pedwarn. For C90,
811 unsigned long is specified to be used for a constant that
812 does not fit in signed long; if uintmax_t has the same
813 range as unsigned long this means only a warning is
814 appropriate here. C90 permits the preprocessor to use a
815 wider range than unsigned long in the compiler, so if
816 uintmax_t is wider than unsigned long no diagnostic is
817 required for such constants in preprocessor #if
818 expressions and the compiler will pedwarn for such
819 constants outside the range of unsigned long that reach
820 the compiler so a diagnostic is not required there
821 either; thus, pedwarn for C99 but use a plain warning for
824 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
827 "integer constant is so large that it is unsigned");
828 result
.unsignedp
= true;
835 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
837 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
842 cpp_num_part add_high
, add_low
;
844 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
845 need to worry about add_high overflowing. */
859 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
860 result
.high
= num
.high
<< shift
;
861 result
.low
= num
.low
<< shift
;
862 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
863 result
.unsignedp
= num
.unsignedp
;
867 add_low
= num
.low
<< 1;
868 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
871 add_high
= add_low
= 0;
873 if (add_low
+ digit
< add_low
)
877 if (result
.low
+ add_low
< result
.low
)
879 if (result
.high
+ add_high
< result
.high
)
882 result
.low
+= add_low
;
883 result
.high
+= add_high
;
884 result
.overflow
= overflow
;
886 /* The above code catches overflow of a cpp_num type. This catches
887 overflow of the (possibly shorter) target precision. */
888 num
.low
= result
.low
;
889 num
.high
= result
.high
;
890 result
= num_trim (result
, precision
);
891 if (!num_eq (result
, num
))
892 result
.overflow
= true;
897 /* Handle meeting "defined" in a preprocessor expression. */
899 parse_defined (cpp_reader
*pfile
)
903 cpp_hashnode
*node
= 0;
904 const cpp_token
*token
;
905 cpp_context
*initial_context
= pfile
->context
;
907 /* Don't expand macros. */
908 pfile
->state
.prevent_expansion
++;
910 token
= cpp_get_token (pfile
);
911 if (token
->type
== CPP_OPEN_PAREN
)
914 token
= cpp_get_token (pfile
);
917 if (token
->type
== CPP_NAME
)
919 node
= token
->val
.node
.node
;
920 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
922 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
928 cpp_error (pfile
, CPP_DL_ERROR
,
929 "operator \"defined\" requires an identifier");
930 if (token
->flags
& NAMED_OP
)
935 op
.type
= token
->type
;
936 cpp_error (pfile
, CPP_DL_ERROR
,
937 "(\"%s\" is an alternative token for \"%s\" in C++)",
938 cpp_token_as_text (pfile
, token
),
939 cpp_token_as_text (pfile
, &op
));
945 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
946 cpp_error (pfile
, CPP_DL_WARNING
,
947 "this use of \"defined\" may not be portable");
949 _cpp_mark_macro_used (node
);
950 if (!(node
->flags
& NODE_USED
))
952 node
->flags
|= NODE_USED
;
953 if (node
->type
== NT_MACRO
)
955 if ((node
->flags
& NODE_BUILTIN
)
956 && pfile
->cb
.user_builtin_macro
)
957 pfile
->cb
.user_builtin_macro (pfile
, node
);
958 if (pfile
->cb
.used_define
)
959 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
963 if (pfile
->cb
.used_undef
)
964 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
968 /* A possible controlling macro of the form #if !defined ().
969 _cpp_parse_expr checks there was no other junk on the line. */
970 pfile
->mi_ind_cmacro
= node
;
973 pfile
->state
.prevent_expansion
--;
975 /* Do not treat conditional macros as being defined. This is due to the
976 powerpc and spu ports using conditional macros for 'vector', 'bool', and
977 'pixel' to act as conditional keywords. This messes up tests like #ifndef
979 result
.unsignedp
= false;
981 result
.overflow
= false;
982 result
.low
= (node
&& node
->type
== NT_MACRO
983 && (node
->flags
& NODE_CONDITIONAL
) == 0);
987 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
988 number or character constant, or the result of the "defined" or "#"
991 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
992 source_location virtual_location
)
998 result
.unsignedp
= false;
999 result
.overflow
= false;
1001 switch (token
->type
)
1004 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
1005 if (temp
& CPP_N_USERDEF
)
1006 cpp_error (pfile
, CPP_DL_ERROR
,
1007 "user-defined literal in preprocessor expression");
1008 switch (temp
& CPP_N_CATEGORY
)
1010 case CPP_N_FLOATING
:
1011 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1012 "floating constant in preprocessor expression");
1015 if (!(temp
& CPP_N_IMAGINARY
))
1016 return cpp_interpret_integer (pfile
, token
, temp
);
1017 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1018 "imaginary number in preprocessor expression");
1022 /* Error already issued. */
1025 result
.high
= result
.low
= 0;
1033 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1038 /* Sign-extend the result if necessary. */
1039 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1041 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1042 result
.low
|= ~(~(cpp_num_part
) 0
1043 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1044 result
.high
= ~(cpp_num_part
) 0;
1045 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1051 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1052 return parse_defined (pfile
);
1053 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include__
)
1054 return parse_has_include (pfile
, IT_INCLUDE
);
1055 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include_next__
)
1056 return parse_has_include (pfile
, IT_INCLUDE_NEXT
);
1057 else if (CPP_OPTION (pfile
, cplusplus
)
1058 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1059 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1062 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1068 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1069 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1070 "\"%s\" is not defined",
1071 NODE_NAME (token
->val
.node
.node
));
1076 if (!pfile
->state
.skipping
)
1078 /* A pedantic warning takes precedence over a deprecated
1080 if (CPP_PEDANTIC (pfile
))
1081 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1082 virtual_location
, 0,
1083 "assertions are a GCC extension");
1084 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1085 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1086 "assertions are a deprecated extension");
1088 _cpp_test_assertion (pfile
, &temp
);
1097 result
.unsignedp
= !!unsignedp
;
1101 /* Operator precedence and flags table.
1103 After an operator is returned from the lexer, if it has priority less
1104 than the operator on the top of the stack, we reduce the stack by one
1105 operator and repeat the test. Since equal priorities do not reduce,
1106 this is naturally right-associative.
1108 We handle left-associative operators by decrementing the priority of
1109 just-lexed operators by one, but retaining the priority of operators
1110 already on the stack.
1112 The remaining cases are '(' and ')'. We handle '(' by skipping the
1113 reduction phase completely. ')' is given lower priority than
1114 everything else, including '(', effectively forcing a reduction of the
1115 parenthesized expression. If there is a matching '(', the routine
1116 reduce() exits immediately. If the normal exit route sees a ')', then
1117 there cannot have been a matching '(' and an error message is output.
1119 The parser assumes all shifted operators require a left operand unless
1120 the flag NO_L_OPERAND is set. These semantics are automatic; any
1121 extra semantics need to be handled with operator-specific code. */
1123 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1124 operand changes because of integer promotions. */
1125 #define NO_L_OPERAND (1 << 0)
1126 #define LEFT_ASSOC (1 << 1)
1127 #define CHECK_PROMOTION (1 << 2)
1129 /* Operator to priority map. Must be in the same order as the first
1130 N entries of enum cpp_ttype. */
1131 static const struct cpp_operator
1137 /* EQ */ {0, 0}, /* Shouldn't happen. */
1138 /* NOT */ {16, NO_L_OPERAND
},
1139 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1140 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1141 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1142 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1143 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1144 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1145 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1146 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1147 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1148 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1149 /* RSHIFT */ {13, LEFT_ASSOC
},
1150 /* LSHIFT */ {13, LEFT_ASSOC
},
1152 /* COMPL */ {16, NO_L_OPERAND
},
1153 /* AND_AND */ {6, LEFT_ASSOC
},
1154 /* OR_OR */ {5, LEFT_ASSOC
},
1155 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1156 However, there are some special cases for these in reduce(). */
1158 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1159 /* COMMA */ {4, LEFT_ASSOC
},
1160 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1161 /* CLOSE_PAREN */ {0, 0},
1163 /* EQ_EQ */ {11, LEFT_ASSOC
},
1164 /* NOT_EQ */ {11, LEFT_ASSOC
},
1165 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1166 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1167 /* UPLUS */ {16, NO_L_OPERAND
},
1168 /* UMINUS */ {16, NO_L_OPERAND
}
1171 /* Parse and evaluate a C expression, reading from PFILE.
1172 Returns the truth value of the expression.
1174 The implementation is an operator precedence parser, i.e. a
1175 bottom-up parser, using a stack for not-yet-reduced tokens.
1177 The stack base is op_stack, and the current stack pointer is 'top'.
1178 There is a stack element for each operator (only), and the most
1179 recently pushed operator is 'top->op'. An operand (value) is
1180 stored in the 'value' field of the stack element of the operator
1181 that precedes it. */
1183 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1185 struct op
*top
= pfile
->op_stack
;
1186 unsigned int lex_count
;
1187 bool saw_leading_not
, want_value
= true;
1188 source_location virtual_location
= 0;
1190 pfile
->state
.skip_eval
= 0;
1192 /* Set up detection of #if ! defined(). */
1193 pfile
->mi_ind_cmacro
= 0;
1194 saw_leading_not
= false;
1197 /* Lowest priority operator prevents further reductions. */
1205 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1206 op
.op
= op
.token
->type
;
1207 op
.loc
= virtual_location
;
1211 /* These tokens convert into values. */
1220 SYNTAX_ERROR2_AT (op
.loc
,
1221 "missing binary operator before token \"%s\"",
1222 cpp_token_as_text (pfile
, op
.token
));
1224 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1228 saw_leading_not
= lex_count
== 1;
1240 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1241 SYNTAX_ERROR2_AT (op
.loc
,
1242 "token \"%s\" is not valid in preprocessor expressions",
1243 cpp_token_as_text (pfile
, op
.token
));
1247 /* Check we have a value or operator as appropriate. */
1248 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1251 SYNTAX_ERROR2_AT (op
.loc
,
1252 "missing binary operator before token \"%s\"",
1253 cpp_token_as_text (pfile
, op
.token
));
1255 else if (want_value
)
1257 /* We want a number (or expression) and haven't got one.
1258 Try to emit a specific diagnostic. */
1259 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1260 SYNTAX_ERROR_AT (op
.loc
,
1261 "missing expression between '(' and ')'");
1263 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1264 SYNTAX_ERROR2_AT (op
.loc
,
1265 "%s with no expression", is_if
? "#if" : "#elif");
1267 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1268 SYNTAX_ERROR2_AT (op
.loc
,
1269 "operator '%s' has no right operand",
1270 cpp_token_as_text (pfile
, top
->token
));
1271 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1272 /* Complain about missing paren during reduction. */;
1274 SYNTAX_ERROR2_AT (op
.loc
,
1275 "operator '%s' has no left operand",
1276 cpp_token_as_text (pfile
, op
.token
));
1279 top
= reduce (pfile
, top
, op
.op
);
1283 if (op
.op
== CPP_EOF
)
1288 case CPP_CLOSE_PAREN
:
1291 if (!num_zerop (top
->value
))
1292 pfile
->state
.skip_eval
++;
1296 if (num_zerop (top
->value
))
1297 pfile
->state
.skip_eval
++;
1300 if (top
->op
!= CPP_QUERY
)
1301 SYNTAX_ERROR_AT (op
.loc
,
1302 " ':' without preceding '?'");
1303 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1304 pfile
->state
.skip_eval
++;
1306 pfile
->state
.skip_eval
--;
1313 /* Check for and handle stack overflow. */
1314 if (++top
== pfile
->op_limit
)
1315 top
= _cpp_expand_op_stack (pfile
);
1318 top
->token
= op
.token
;
1322 /* The controlling macro expression is only valid if we called lex 3
1323 times: <!> <defined expression> and <EOF>. push_conditional ()
1324 checks that we are at top-of-file. */
1325 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1326 pfile
->mi_ind_cmacro
= 0;
1328 if (top
!= pfile
->op_stack
)
1330 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1331 "unbalanced stack in %s",
1332 is_if
? "#if" : "#elif");
1334 return false; /* Return false on syntax error. */
1337 return !num_zerop (top
->value
);
1340 /* Reduce the operator / value stack if possible, in preparation for
1341 pushing operator OP. Returns NULL on error, otherwise the top of
1344 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1348 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1351 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1355 if (op
== CPP_OPEN_PAREN
)
1358 /* Decrement the priority of left-associative operators to force a
1359 reduction with operators of otherwise equal priority. */
1360 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1361 while (prio
< optab
[top
->op
].prio
)
1363 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1364 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1365 check_promotion (pfile
, top
);
1373 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1374 top
[-1].loc
= top
->loc
;
1382 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1383 top
->value
, top
->op
);
1384 top
[-1].loc
= top
->loc
;
1389 case CPP_GREATER_EQ
:
1392 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1393 top
[-1].loc
= top
->loc
;
1399 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1400 top
[-1].loc
= top
->loc
;
1407 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1408 top
[-1].loc
= top
->loc
;
1412 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1413 top
[-1].loc
= top
->loc
;
1418 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1419 top
->value
, top
->op
, top
->loc
);
1420 top
[-1].loc
= top
->loc
;
1425 if (!num_zerop (top
->value
))
1426 pfile
->state
.skip_eval
--;
1427 top
->value
.low
= (!num_zerop (top
->value
)
1428 || !num_zerop (top
[1].value
));
1429 top
->value
.high
= 0;
1430 top
->value
.unsignedp
= false;
1431 top
->value
.overflow
= false;
1432 top
->loc
= top
[1].loc
;
1437 if (num_zerop (top
->value
))
1438 pfile
->state
.skip_eval
--;
1439 top
->value
.low
= (!num_zerop (top
->value
)
1440 && !num_zerop (top
[1].value
));
1441 top
->value
.high
= 0;
1442 top
->value
.unsignedp
= false;
1443 top
->value
.overflow
= false;
1444 top
->loc
= top
[1].loc
;
1447 case CPP_OPEN_PAREN
:
1448 if (op
!= CPP_CLOSE_PAREN
)
1450 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1451 top
->token
->src_loc
,
1452 0, "missing ')' in expression");
1456 top
->value
= top
[1].value
;
1457 top
->loc
= top
[1].loc
;
1462 if (!num_zerop (top
->value
))
1464 pfile
->state
.skip_eval
--;
1465 top
->value
= top
[1].value
;
1466 top
->loc
= top
[1].loc
;
1470 top
->value
= top
[2].value
;
1471 top
->loc
= top
[2].loc
;
1473 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1474 || top
[2].value
.unsignedp
);
1478 /* COMMA and COLON should not reduce a QUERY operator. */
1479 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1481 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1489 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1490 cpp_error (pfile
, CPP_DL_PEDWARN
,
1491 "integer overflow in preprocessor expression");
1494 if (op
== CPP_CLOSE_PAREN
)
1496 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1503 /* Returns the position of the old top of stack after expansion. */
1505 _cpp_expand_op_stack (cpp_reader
*pfile
)
1507 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1508 size_t new_size
= old_size
* 2 + 20;
1510 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1511 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1513 return pfile
->op_stack
+ old_size
;
1516 /* Emits a warning if the effective sign of either operand of OP
1517 changes because of integer promotions. */
1519 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1521 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1524 if (op
->value
.unsignedp
)
1526 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1527 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1528 "the left operand of \"%s\" changes sign when promoted",
1529 cpp_token_as_text (pfile
, op
->token
));
1531 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1532 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1533 "the right operand of \"%s\" changes sign when promoted",
1534 cpp_token_as_text (pfile
, op
->token
));
1537 /* Clears the unused high order bits of the number pointed to by PNUM. */
1539 num_trim (cpp_num num
, size_t precision
)
1541 if (precision
> PART_PRECISION
)
1543 precision
-= PART_PRECISION
;
1544 if (precision
< PART_PRECISION
)
1545 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1549 if (precision
< PART_PRECISION
)
1550 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1557 /* True iff A (presumed signed) >= 0. */
1559 num_positive (cpp_num num
, size_t precision
)
1561 if (precision
> PART_PRECISION
)
1563 precision
-= PART_PRECISION
;
1564 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1567 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1570 /* Sign extend a number, with PRECISION significant bits and all
1571 others assumed clear, to fill out a cpp_num structure. */
1573 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1577 if (precision
> PART_PRECISION
)
1579 precision
-= PART_PRECISION
;
1580 if (precision
< PART_PRECISION
1581 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1582 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1584 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1586 if (precision
< PART_PRECISION
)
1587 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1588 num
.high
= ~(cpp_num_part
) 0;
1595 /* Returns the negative of NUM. */
1597 num_negate (cpp_num num
, size_t precision
)
1602 num
.high
= ~num
.high
;
1606 num
= num_trim (num
, precision
);
1607 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1612 /* Returns true if A >= B. */
1614 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1618 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1622 /* Both numbers have signed type. If they are of different
1623 sign, the answer is the sign of A. */
1624 unsignedp
= num_positive (pa
, precision
);
1626 if (unsignedp
!= num_positive (pb
, precision
))
1629 /* Otherwise we can do an unsigned comparison. */
1632 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1635 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1637 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1638 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1640 lhs
.overflow
= false;
1641 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1643 /* As excess precision is zeroed, there is no need to num_trim () as
1644 these operations cannot introduce a set bit there. */
1648 lhs
.high
&= rhs
.high
;
1650 else if (op
== CPP_OR
)
1653 lhs
.high
|= rhs
.high
;
1658 lhs
.high
^= rhs
.high
;
1664 /* Returns LHS OP RHS, where OP is an inequality. */
1666 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1669 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1671 if (op
== CPP_GREATER_EQ
)
1673 else if (op
== CPP_LESS
)
1675 else if (op
== CPP_GREATER
)
1676 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1677 else /* CPP_LESS_EQ. */
1678 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1681 lhs
.overflow
= false;
1682 lhs
.unsignedp
= false;
1686 /* Returns LHS OP RHS, where OP is == or !=. */
1688 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1689 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1691 /* Work around a 3.0.4 bug; see PR 6950. */
1692 bool eq
= num_eq (lhs
, rhs
);
1693 if (op
== CPP_NOT_EQ
)
1697 lhs
.overflow
= false;
1698 lhs
.unsignedp
= false;
1702 /* Shift NUM, of width PRECISION, right by N bits. */
1704 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1706 cpp_num_part sign_mask
;
1707 bool x
= num_positive (num
, precision
);
1709 if (num
.unsignedp
|| x
)
1712 sign_mask
= ~(cpp_num_part
) 0;
1715 num
.high
= num
.low
= sign_mask
;
1719 if (precision
< PART_PRECISION
)
1720 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1721 else if (precision
< 2 * PART_PRECISION
)
1722 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1724 if (n
>= PART_PRECISION
)
1726 n
-= PART_PRECISION
;
1728 num
.high
= sign_mask
;
1733 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1734 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1738 num
= num_trim (num
, precision
);
1739 num
.overflow
= false;
1743 /* Shift NUM, of width PRECISION, left by N bits. */
1745 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1749 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1750 num
.high
= num
.low
= 0;
1754 cpp_num orig
, maybe_orig
;
1758 if (m
>= PART_PRECISION
)
1760 m
-= PART_PRECISION
;
1766 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1769 num
= num_trim (num
, precision
);
1772 num
.overflow
= false;
1775 maybe_orig
= num_rshift (num
, precision
, n
);
1776 num
.overflow
= !num_eq (orig
, maybe_orig
);
1783 /* The four unary operators: +, -, ! and ~. */
1785 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1790 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1791 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1792 "traditional C rejects the unary plus operator");
1793 num
.overflow
= false;
1797 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1801 num
.high
= ~num
.high
;
1803 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1804 num
.overflow
= false;
1807 default: /* case CPP_NOT: */
1808 num
.low
= num_zerop (num
);
1810 num
.overflow
= false;
1811 num
.unsignedp
= false;
1818 /* The various binary operators. */
1820 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1823 size_t precision
= CPP_OPTION (pfile
, precision
);
1831 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1833 /* A negative shift is a positive shift the other way. */
1834 if (op
== CPP_LSHIFT
)
1838 rhs
= num_negate (rhs
, precision
);
1841 n
= ~0; /* Maximal. */
1844 if (op
== CPP_LSHIFT
)
1845 lhs
= num_lshift (lhs
, precision
, n
);
1847 lhs
= num_rshift (lhs
, precision
, n
);
1852 result
.low
= lhs
.low
- rhs
.low
;
1853 result
.high
= lhs
.high
- rhs
.high
;
1854 if (result
.low
> lhs
.low
)
1856 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1857 result
.overflow
= false;
1859 result
= num_trim (result
, precision
);
1860 if (!result
.unsignedp
)
1862 bool lhsp
= num_positive (lhs
, precision
);
1863 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1864 && lhsp
!= num_positive (result
, precision
));
1869 result
.low
= lhs
.low
+ rhs
.low
;
1870 result
.high
= lhs
.high
+ rhs
.high
;
1871 if (result
.low
< lhs
.low
)
1873 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1874 result
.overflow
= false;
1876 result
= num_trim (result
, precision
);
1877 if (!result
.unsignedp
)
1879 bool lhsp
= num_positive (lhs
, precision
);
1880 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1881 && lhsp
!= num_positive (result
, precision
));
1886 default: /* case CPP_COMMA: */
1887 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1888 || !pfile
->state
.skip_eval
))
1889 cpp_pedwarning (pfile
, CPP_W_PEDANTIC
,
1890 "comma operator in operand of #if");
1898 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1901 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1904 cpp_num_part middle
[2], temp
;
1906 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1907 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1909 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1910 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1913 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1914 if (result
.low
< temp
)
1918 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1919 if (result
.low
< temp
)
1922 result
.high
+= HIGH_PART (middle
[0]);
1923 result
.high
+= HIGH_PART (middle
[1]);
1924 result
.unsignedp
= true;
1925 result
.overflow
= false;
1930 /* Multiply two preprocessing numbers. */
1932 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1934 cpp_num result
, temp
;
1935 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1936 bool overflow
, negate
= false;
1937 size_t precision
= CPP_OPTION (pfile
, precision
);
1939 /* Prepare for unsigned multiplication. */
1942 if (!num_positive (lhs
, precision
))
1943 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1944 if (!num_positive (rhs
, precision
))
1945 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1948 overflow
= lhs
.high
&& rhs
.high
;
1949 result
= num_part_mul (lhs
.low
, rhs
.low
);
1951 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1952 result
.high
+= temp
.low
;
1956 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1957 result
.high
+= temp
.low
;
1961 temp
.low
= result
.low
, temp
.high
= result
.high
;
1962 result
= num_trim (result
, precision
);
1963 if (!num_eq (result
, temp
))
1967 result
= num_negate (result
, precision
);
1970 result
.overflow
= false;
1972 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1973 && !num_zerop (result
));
1974 result
.unsignedp
= unsignedp
;
1979 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1980 or the remainder depending upon OP. LOCATION is the source location
1981 of this operator (for diagnostics). */
1984 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1985 source_location location
)
1987 cpp_num result
, sub
;
1989 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1990 bool negate
= false, lhs_neg
= false;
1991 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1993 /* Prepare for unsigned division. */
1996 if (!num_positive (lhs
, precision
))
1997 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1998 if (!num_positive (rhs
, precision
))
1999 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
2002 /* Find the high bit. */
2006 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2007 for (; ; i
--, mask
>>= 1)
2008 if (rhs
.high
& mask
)
2013 if (precision
> PART_PRECISION
)
2014 i
= precision
- PART_PRECISION
- 1;
2017 mask
= (cpp_num_part
) 1 << i
;
2018 for (; ; i
--, mask
>>= 1)
2024 if (!pfile
->state
.skip_eval
)
2025 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2026 "division by zero in #if");
2030 /* First nonzero bit of RHS is bit I. Do naive division by
2031 shifting the RHS fully left, and subtracting from LHS if LHS is
2032 at least as big, and then repeating but with one less shift.
2033 This is not very efficient, but is easy to understand. */
2035 rhs
.unsignedp
= true;
2036 lhs
.unsignedp
= true;
2037 i
= precision
- i
- 1;
2038 sub
= num_lshift (rhs
, precision
, i
);
2040 result
.high
= result
.low
= 0;
2043 if (num_greater_eq (lhs
, sub
, precision
))
2045 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2046 if (i
>= PART_PRECISION
)
2047 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2049 result
.low
|= (cpp_num_part
) 1 << i
;
2053 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2057 /* We divide so that the remainder has the sign of the LHS. */
2060 result
.unsignedp
= unsignedp
;
2061 result
.overflow
= false;
2065 result
= num_negate (result
, precision
);
2066 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2067 && !num_zerop (result
));
2074 lhs
.unsignedp
= unsignedp
;
2075 lhs
.overflow
= false;
2077 lhs
= num_negate (lhs
, precision
);
2082 /* Handle meeting "__has_include__" in a preprocessor expression. */
2084 parse_has_include (cpp_reader
*pfile
, enum include_type type
)
2088 cpp_hashnode
*node
= 0;
2089 const cpp_token
*token
;
2090 bool bracket
= false;
2093 result
.unsignedp
= false;
2095 result
.overflow
= false;
2098 pfile
->state
.in__has_include__
++;
2100 token
= cpp_get_token (pfile
);
2101 if (token
->type
== CPP_OPEN_PAREN
)
2104 token
= cpp_get_token (pfile
);
2107 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
2109 if (token
->type
== CPP_HEADER_NAME
)
2111 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
2112 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
2113 fname
[token
->val
.str
.len
- 2] = '\0';
2114 node
= token
->val
.node
.node
;
2116 else if (token
->type
== CPP_LESS
)
2119 fname
= _cpp_bracket_include (pfile
);
2122 cpp_error (pfile
, CPP_DL_ERROR
,
2123 "operator \"__has_include__\" requires a header string");
2127 int angle_brackets
= (bracket
? 1 : 0);
2129 if (_cpp_has_header (pfile
, fname
, angle_brackets
, type
))
2137 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
2138 cpp_error (pfile
, CPP_DL_ERROR
,
2139 "missing ')' after \"__has_include__\"");
2141 /* A possible controlling macro of the form #if !__has_include__ ().
2142 _cpp_parse_expr checks there was no other junk on the line. */
2144 pfile
->mi_ind_cmacro
= node
;
2146 pfile
->state
.in__has_include__
--;