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
) && !CPP_OPTION (pfile
, c99
))
546 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
547 "use of C99 hexadecimal floating constant");
549 if (float_flag
== AFTER_EXPON
)
551 if (*str
== '+' || *str
== '-')
554 /* Exponent is decimal, even if string is a hex float. */
557 if (DIGIT_SEP (*str
))
558 SYNTAX_ERROR_AT (virtual_location
,
559 "digit separator adjacent to exponent");
561 SYNTAX_ERROR_AT (virtual_location
, "exponent has no digits");
565 seen_digit_sep
= DIGIT_SEP (*str
);
568 while (ISDIGIT (*str
) || DIGIT_SEP (*str
));
570 else if (radix
== 16)
571 SYNTAX_ERROR_AT (virtual_location
,
572 "hexadecimal floating constants require an exponent");
575 SYNTAX_ERROR_AT (virtual_location
,
576 "digit separator outside digit sequence");
578 result
= interpret_float_suffix (pfile
, str
, limit
- str
);
581 if (CPP_OPTION (pfile
, user_literals
))
584 *ud_suffix
= (const char *) str
;
585 result
= CPP_N_LARGE
| CPP_N_USERDEF
;
589 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
590 "invalid suffix \"%.*s\" on floating constant",
591 (int) (limit
- str
), str
);
592 return CPP_N_INVALID
;
596 /* Traditional C didn't accept any floating suffixes. */
598 && CPP_WTRADITIONAL (pfile
)
599 && ! cpp_sys_macro_p (pfile
))
600 cpp_warning_with_line (pfile
, CPP_W_TRADITIONAL
, virtual_location
, 0,
601 "traditional C rejects the \"%.*s\" suffix",
602 (int) (limit
- str
), str
);
604 /* A suffix for double is a GCC extension via decimal float support.
605 If the suffix also specifies an imaginary value we'll catch that
607 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
608 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
609 "suffix for double constant is a GCC extension");
611 /* Radix must be 10 for decimal floats. */
612 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
614 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
615 "invalid suffix \"%.*s\" with hexadecimal floating constant",
616 (int) (limit
- str
), str
);
617 return CPP_N_INVALID
;
620 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
621 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
622 "fixed-point constants are a GCC extension");
624 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
625 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
626 "decimal float constants are a GCC extension");
628 result
|= CPP_N_FLOATING
;
632 result
= interpret_int_suffix (pfile
, str
, limit
- str
);
635 if (CPP_OPTION (pfile
, user_literals
))
638 *ud_suffix
= (const char *) str
;
639 result
= CPP_N_UNSIGNED
| CPP_N_LARGE
| CPP_N_USERDEF
;
643 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
644 "invalid suffix \"%.*s\" on integer constant",
645 (int) (limit
- str
), str
);
646 return CPP_N_INVALID
;
650 /* Traditional C only accepted the 'L' suffix.
651 Suppress warning about 'LL' with -Wno-long-long. */
652 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
654 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
655 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
656 && CPP_OPTION (pfile
, cpp_warn_long_long
);
659 cpp_warning_with_line (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
661 "traditional C rejects the \"%.*s\" suffix",
662 (int) (limit
- str
), str
);
665 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
666 && CPP_OPTION (pfile
, cpp_warn_long_long
))
668 const char *message
= CPP_OPTION (pfile
, cplusplus
)
669 ? N_("use of C++11 long long integer constant")
670 : N_("use of C99 long long integer constant");
672 if (CPP_OPTION (pfile
, c99
))
673 cpp_warning_with_line (pfile
, CPP_W_LONG_LONG
, virtual_location
,
676 cpp_pedwarning_with_line (pfile
, CPP_W_LONG_LONG
,
677 virtual_location
, 0, message
);
680 result
|= CPP_N_INTEGER
;
684 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
685 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
686 "imaginary constants are a GCC extension");
688 && !CPP_OPTION (pfile
, binary_constants
)
689 && CPP_PEDANTIC (pfile
))
690 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
, virtual_location
, 0,
691 CPP_OPTION (pfile
, cplusplus
)
692 ? "binary constants are a C++1y feature "
694 : "binary constants are a GCC extension");
697 result
|= CPP_N_DECIMAL
;
698 else if (radix
== 16)
701 result
|= CPP_N_BINARY
;
703 result
|= CPP_N_OCTAL
;
708 return CPP_N_INVALID
;
711 /* cpp_interpret_integer converts an integer constant into a cpp_num,
712 of precision options->precision.
714 We do not provide any interface for decimal->float conversion,
715 because the preprocessor doesn't need it and we don't want to
716 drag in GCC's floating point emulator. */
718 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
721 const uchar
*p
, *end
;
726 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
727 result
.overflow
= false;
729 p
= token
->val
.str
.text
;
730 end
= p
+ token
->val
.str
.len
;
732 /* Common case of a single digit. */
733 if (token
->val
.str
.len
== 1)
734 result
.low
= p
[0] - '0';
738 size_t precision
= CPP_OPTION (pfile
, precision
);
739 unsigned int base
= 10, c
= 0;
740 bool overflow
= false;
742 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
747 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
752 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
758 /* We can add a digit to numbers strictly less than this without
759 needing the precision and slowness of double integers. */
760 max
= ~(cpp_num_part
) 0;
761 if (precision
< PART_PRECISION
)
762 max
>>= PART_PRECISION
- precision
;
763 max
= (max
- base
+ 1) / base
+ 1;
769 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
771 else if (DIGIT_SEP (c
))
776 /* Strict inequality for when max is set to zero. */
777 if (result
.low
< max
)
778 result
.low
= result
.low
* base
+ c
;
781 result
= append_digit (result
, c
, base
, precision
);
782 overflow
|= result
.overflow
;
787 if (overflow
&& !(type
& CPP_N_USERDEF
))
788 cpp_error (pfile
, CPP_DL_PEDWARN
,
789 "integer constant is too large for its type");
790 /* If too big to be signed, consider it unsigned. Only warn for
791 decimal numbers. Traditional numbers were always signed (but
792 we still honor an explicit U suffix); but we only have
793 traditional semantics in directives. */
794 else if (!result
.unsignedp
795 && !(CPP_OPTION (pfile
, traditional
)
796 && pfile
->state
.in_directive
)
797 && !num_positive (result
, precision
))
799 /* This is for constants within the range of uintmax_t but
800 not that of intmax_t. For such decimal constants, a
801 diagnostic is required for C99 as the selected type must
802 be signed and not having a type is a constraint violation
803 (DR#298, TC3), so this must be a pedwarn. For C90,
804 unsigned long is specified to be used for a constant that
805 does not fit in signed long; if uintmax_t has the same
806 range as unsigned long this means only a warning is
807 appropriate here. C90 permits the preprocessor to use a
808 wider range than unsigned long in the compiler, so if
809 uintmax_t is wider than unsigned long no diagnostic is
810 required for such constants in preprocessor #if
811 expressions and the compiler will pedwarn for such
812 constants outside the range of unsigned long that reach
813 the compiler so a diagnostic is not required there
814 either; thus, pedwarn for C99 but use a plain warning for
817 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
820 "integer constant is so large that it is unsigned");
821 result
.unsignedp
= true;
828 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
830 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
835 cpp_num_part add_high
, add_low
;
837 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
838 need to worry about add_high overflowing. */
852 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
853 result
.high
= num
.high
<< shift
;
854 result
.low
= num
.low
<< shift
;
855 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
856 result
.unsignedp
= num
.unsignedp
;
860 add_low
= num
.low
<< 1;
861 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
864 add_high
= add_low
= 0;
866 if (add_low
+ digit
< add_low
)
870 if (result
.low
+ add_low
< result
.low
)
872 if (result
.high
+ add_high
< result
.high
)
875 result
.low
+= add_low
;
876 result
.high
+= add_high
;
877 result
.overflow
= overflow
;
879 /* The above code catches overflow of a cpp_num type. This catches
880 overflow of the (possibly shorter) target precision. */
881 num
.low
= result
.low
;
882 num
.high
= result
.high
;
883 result
= num_trim (result
, precision
);
884 if (!num_eq (result
, num
))
885 result
.overflow
= true;
890 /* Handle meeting "defined" in a preprocessor expression. */
892 parse_defined (cpp_reader
*pfile
)
896 cpp_hashnode
*node
= 0;
897 const cpp_token
*token
;
898 cpp_context
*initial_context
= pfile
->context
;
900 /* Don't expand macros. */
901 pfile
->state
.prevent_expansion
++;
903 token
= cpp_get_token (pfile
);
904 if (token
->type
== CPP_OPEN_PAREN
)
907 token
= cpp_get_token (pfile
);
910 if (token
->type
== CPP_NAME
)
912 node
= token
->val
.node
.node
;
913 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
915 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
921 cpp_error (pfile
, CPP_DL_ERROR
,
922 "operator \"defined\" requires an identifier");
923 if (token
->flags
& NAMED_OP
)
928 op
.type
= token
->type
;
929 cpp_error (pfile
, CPP_DL_ERROR
,
930 "(\"%s\" is an alternative token for \"%s\" in C++)",
931 cpp_token_as_text (pfile
, token
),
932 cpp_token_as_text (pfile
, &op
));
938 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
939 cpp_error (pfile
, CPP_DL_WARNING
,
940 "this use of \"defined\" may not be portable");
942 _cpp_mark_macro_used (node
);
943 if (!(node
->flags
& NODE_USED
))
945 node
->flags
|= NODE_USED
;
946 if (node
->type
== NT_MACRO
)
948 if ((node
->flags
& NODE_BUILTIN
)
949 && pfile
->cb
.user_builtin_macro
)
950 pfile
->cb
.user_builtin_macro (pfile
, node
);
951 if (pfile
->cb
.used_define
)
952 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
956 if (pfile
->cb
.used_undef
)
957 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
961 /* A possible controlling macro of the form #if !defined ().
962 _cpp_parse_expr checks there was no other junk on the line. */
963 pfile
->mi_ind_cmacro
= node
;
966 pfile
->state
.prevent_expansion
--;
968 /* Do not treat conditional macros as being defined. This is due to the
969 powerpc and spu ports using conditional macros for 'vector', 'bool', and
970 'pixel' to act as conditional keywords. This messes up tests like #ifndef
972 result
.unsignedp
= false;
974 result
.overflow
= false;
975 result
.low
= (node
&& node
->type
== NT_MACRO
976 && (node
->flags
& NODE_CONDITIONAL
) == 0);
980 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
981 number or character constant, or the result of the "defined" or "#"
984 eval_token (cpp_reader
*pfile
, const cpp_token
*token
,
985 source_location virtual_location
)
991 result
.unsignedp
= false;
992 result
.overflow
= false;
997 temp
= cpp_classify_number (pfile
, token
, NULL
, virtual_location
);
998 if (temp
& CPP_N_USERDEF
)
999 cpp_error (pfile
, CPP_DL_ERROR
,
1000 "user-defined literal in preprocessor expression");
1001 switch (temp
& CPP_N_CATEGORY
)
1003 case CPP_N_FLOATING
:
1004 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1005 "floating constant in preprocessor expression");
1008 if (!(temp
& CPP_N_IMAGINARY
))
1009 return cpp_interpret_integer (pfile
, token
, temp
);
1010 cpp_error_with_line (pfile
, CPP_DL_ERROR
, virtual_location
, 0,
1011 "imaginary number in preprocessor expression");
1015 /* Error already issued. */
1018 result
.high
= result
.low
= 0;
1026 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
1031 /* Sign-extend the result if necessary. */
1032 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
1034 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
1035 result
.low
|= ~(~(cpp_num_part
) 0
1036 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
1037 result
.high
= ~(cpp_num_part
) 0;
1038 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
1044 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
1045 return parse_defined (pfile
);
1046 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include__
)
1047 return parse_has_include (pfile
, IT_INCLUDE
);
1048 else if (token
->val
.node
.node
== pfile
->spec_nodes
.n__has_include_next__
)
1049 return parse_has_include (pfile
, IT_INCLUDE_NEXT
);
1050 else if (CPP_OPTION (pfile
, cplusplus
)
1051 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
1052 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
1055 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
1061 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
1062 cpp_warning_with_line (pfile
, CPP_W_UNDEF
, virtual_location
, 0,
1063 "\"%s\" is not defined",
1064 NODE_NAME (token
->val
.node
.node
));
1069 if (!pfile
->state
.skipping
)
1071 /* A pedantic warning takes precedence over a deprecated
1073 if (CPP_PEDANTIC (pfile
))
1074 cpp_error_with_line (pfile
, CPP_DL_PEDWARN
,
1075 virtual_location
, 0,
1076 "assertions are a GCC extension");
1077 else if (CPP_OPTION (pfile
, cpp_warn_deprecated
))
1078 cpp_warning_with_line (pfile
, CPP_W_DEPRECATED
, virtual_location
, 0,
1079 "assertions are a deprecated extension");
1081 _cpp_test_assertion (pfile
, &temp
);
1090 result
.unsignedp
= !!unsignedp
;
1094 /* Operator precedence and flags table.
1096 After an operator is returned from the lexer, if it has priority less
1097 than the operator on the top of the stack, we reduce the stack by one
1098 operator and repeat the test. Since equal priorities do not reduce,
1099 this is naturally right-associative.
1101 We handle left-associative operators by decrementing the priority of
1102 just-lexed operators by one, but retaining the priority of operators
1103 already on the stack.
1105 The remaining cases are '(' and ')'. We handle '(' by skipping the
1106 reduction phase completely. ')' is given lower priority than
1107 everything else, including '(', effectively forcing a reduction of the
1108 parenthesized expression. If there is a matching '(', the routine
1109 reduce() exits immediately. If the normal exit route sees a ')', then
1110 there cannot have been a matching '(' and an error message is output.
1112 The parser assumes all shifted operators require a left operand unless
1113 the flag NO_L_OPERAND is set. These semantics are automatic; any
1114 extra semantics need to be handled with operator-specific code. */
1116 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1117 operand changes because of integer promotions. */
1118 #define NO_L_OPERAND (1 << 0)
1119 #define LEFT_ASSOC (1 << 1)
1120 #define CHECK_PROMOTION (1 << 2)
1122 /* Operator to priority map. Must be in the same order as the first
1123 N entries of enum cpp_ttype. */
1124 static const struct cpp_operator
1130 /* EQ */ {0, 0}, /* Shouldn't happen. */
1131 /* NOT */ {16, NO_L_OPERAND
},
1132 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1133 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1134 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1135 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
1136 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1137 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1138 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
1139 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
1140 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
1141 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
1142 /* RSHIFT */ {13, LEFT_ASSOC
},
1143 /* LSHIFT */ {13, LEFT_ASSOC
},
1145 /* COMPL */ {16, NO_L_OPERAND
},
1146 /* AND_AND */ {6, LEFT_ASSOC
},
1147 /* OR_OR */ {5, LEFT_ASSOC
},
1148 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1149 However, there are some special cases for these in reduce(). */
1151 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
1152 /* COMMA */ {4, LEFT_ASSOC
},
1153 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
1154 /* CLOSE_PAREN */ {0, 0},
1156 /* EQ_EQ */ {11, LEFT_ASSOC
},
1157 /* NOT_EQ */ {11, LEFT_ASSOC
},
1158 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1159 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
1160 /* UPLUS */ {16, NO_L_OPERAND
},
1161 /* UMINUS */ {16, NO_L_OPERAND
}
1164 /* Parse and evaluate a C expression, reading from PFILE.
1165 Returns the truth value of the expression.
1167 The implementation is an operator precedence parser, i.e. a
1168 bottom-up parser, using a stack for not-yet-reduced tokens.
1170 The stack base is op_stack, and the current stack pointer is 'top'.
1171 There is a stack element for each operator (only), and the most
1172 recently pushed operator is 'top->op'. An operand (value) is
1173 stored in the 'value' field of the stack element of the operator
1174 that precedes it. */
1176 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
1178 struct op
*top
= pfile
->op_stack
;
1179 unsigned int lex_count
;
1180 bool saw_leading_not
, want_value
= true;
1181 source_location virtual_location
= 0;
1183 pfile
->state
.skip_eval
= 0;
1185 /* Set up detection of #if ! defined(). */
1186 pfile
->mi_ind_cmacro
= 0;
1187 saw_leading_not
= false;
1190 /* Lowest priority operator prevents further reductions. */
1198 op
.token
= cpp_get_token_with_location (pfile
, &virtual_location
);
1199 op
.op
= op
.token
->type
;
1200 op
.loc
= virtual_location
;
1204 /* These tokens convert into values. */
1213 SYNTAX_ERROR2_AT (op
.loc
,
1214 "missing binary operator before token \"%s\"",
1215 cpp_token_as_text (pfile
, op
.token
));
1217 top
->value
= eval_token (pfile
, op
.token
, op
.loc
);
1221 saw_leading_not
= lex_count
== 1;
1233 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
1234 SYNTAX_ERROR2_AT (op
.loc
,
1235 "token \"%s\" is not valid in preprocessor expressions",
1236 cpp_token_as_text (pfile
, op
.token
));
1240 /* Check we have a value or operator as appropriate. */
1241 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
1244 SYNTAX_ERROR2_AT (op
.loc
,
1245 "missing binary operator before token \"%s\"",
1246 cpp_token_as_text (pfile
, op
.token
));
1248 else if (want_value
)
1250 /* We want a number (or expression) and haven't got one.
1251 Try to emit a specific diagnostic. */
1252 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
1253 SYNTAX_ERROR_AT (op
.loc
,
1254 "missing expression between '(' and ')'");
1256 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
1257 SYNTAX_ERROR2_AT (op
.loc
,
1258 "%s with no expression", is_if
? "#if" : "#elif");
1260 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
1261 SYNTAX_ERROR2_AT (op
.loc
,
1262 "operator '%s' has no right operand",
1263 cpp_token_as_text (pfile
, top
->token
));
1264 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
1265 /* Complain about missing paren during reduction. */;
1267 SYNTAX_ERROR2_AT (op
.loc
,
1268 "operator '%s' has no left operand",
1269 cpp_token_as_text (pfile
, op
.token
));
1272 top
= reduce (pfile
, top
, op
.op
);
1276 if (op
.op
== CPP_EOF
)
1281 case CPP_CLOSE_PAREN
:
1284 if (!num_zerop (top
->value
))
1285 pfile
->state
.skip_eval
++;
1289 if (num_zerop (top
->value
))
1290 pfile
->state
.skip_eval
++;
1293 if (top
->op
!= CPP_QUERY
)
1294 SYNTAX_ERROR_AT (op
.loc
,
1295 " ':' without preceding '?'");
1296 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1297 pfile
->state
.skip_eval
++;
1299 pfile
->state
.skip_eval
--;
1306 /* Check for and handle stack overflow. */
1307 if (++top
== pfile
->op_limit
)
1308 top
= _cpp_expand_op_stack (pfile
);
1311 top
->token
= op
.token
;
1315 /* The controlling macro expression is only valid if we called lex 3
1316 times: <!> <defined expression> and <EOF>. push_conditional ()
1317 checks that we are at top-of-file. */
1318 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1319 pfile
->mi_ind_cmacro
= 0;
1321 if (top
!= pfile
->op_stack
)
1323 cpp_error_with_line (pfile
, CPP_DL_ICE
, top
->loc
, 0,
1324 "unbalanced stack in %s",
1325 is_if
? "#if" : "#elif");
1327 return false; /* Return false on syntax error. */
1330 return !num_zerop (top
->value
);
1333 /* Reduce the operator / value stack if possible, in preparation for
1334 pushing operator OP. Returns NULL on error, otherwise the top of
1337 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1341 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1344 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1348 if (op
== CPP_OPEN_PAREN
)
1351 /* Decrement the priority of left-associative operators to force a
1352 reduction with operators of otherwise equal priority. */
1353 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1354 while (prio
< optab
[top
->op
].prio
)
1356 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1357 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1358 check_promotion (pfile
, top
);
1366 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1367 top
[-1].loc
= top
->loc
;
1375 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1376 top
->value
, top
->op
);
1377 top
[-1].loc
= top
->loc
;
1382 case CPP_GREATER_EQ
:
1385 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1386 top
[-1].loc
= top
->loc
;
1392 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1393 top
[-1].loc
= top
->loc
;
1400 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1401 top
[-1].loc
= top
->loc
;
1405 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1406 top
[-1].loc
= top
->loc
;
1411 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1412 top
->value
, top
->op
, top
->loc
);
1413 top
[-1].loc
= top
->loc
;
1418 if (!num_zerop (top
->value
))
1419 pfile
->state
.skip_eval
--;
1420 top
->value
.low
= (!num_zerop (top
->value
)
1421 || !num_zerop (top
[1].value
));
1422 top
->value
.high
= 0;
1423 top
->value
.unsignedp
= false;
1424 top
->value
.overflow
= false;
1425 top
->loc
= top
[1].loc
;
1430 if (num_zerop (top
->value
))
1431 pfile
->state
.skip_eval
--;
1432 top
->value
.low
= (!num_zerop (top
->value
)
1433 && !num_zerop (top
[1].value
));
1434 top
->value
.high
= 0;
1435 top
->value
.unsignedp
= false;
1436 top
->value
.overflow
= false;
1437 top
->loc
= top
[1].loc
;
1440 case CPP_OPEN_PAREN
:
1441 if (op
!= CPP_CLOSE_PAREN
)
1443 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1444 top
->token
->src_loc
,
1445 0, "missing ')' in expression");
1449 top
->value
= top
[1].value
;
1450 top
->loc
= top
[1].loc
;
1455 if (!num_zerop (top
->value
))
1457 pfile
->state
.skip_eval
--;
1458 top
->value
= top
[1].value
;
1459 top
->loc
= top
[1].loc
;
1463 top
->value
= top
[2].value
;
1464 top
->loc
= top
[2].loc
;
1466 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1467 || top
[2].value
.unsignedp
);
1471 /* COMMA and COLON should not reduce a QUERY operator. */
1472 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1474 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1482 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1483 cpp_error (pfile
, CPP_DL_PEDWARN
,
1484 "integer overflow in preprocessor expression");
1487 if (op
== CPP_CLOSE_PAREN
)
1489 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1496 /* Returns the position of the old top of stack after expansion. */
1498 _cpp_expand_op_stack (cpp_reader
*pfile
)
1500 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1501 size_t new_size
= old_size
* 2 + 20;
1503 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1504 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1506 return pfile
->op_stack
+ old_size
;
1509 /* Emits a warning if the effective sign of either operand of OP
1510 changes because of integer promotions. */
1512 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1514 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1517 if (op
->value
.unsignedp
)
1519 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1520 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1521 "the left operand of \"%s\" changes sign when promoted",
1522 cpp_token_as_text (pfile
, op
->token
));
1524 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1525 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1526 "the right operand of \"%s\" changes sign when promoted",
1527 cpp_token_as_text (pfile
, op
->token
));
1530 /* Clears the unused high order bits of the number pointed to by PNUM. */
1532 num_trim (cpp_num num
, size_t precision
)
1534 if (precision
> PART_PRECISION
)
1536 precision
-= PART_PRECISION
;
1537 if (precision
< PART_PRECISION
)
1538 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1542 if (precision
< PART_PRECISION
)
1543 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1550 /* True iff A (presumed signed) >= 0. */
1552 num_positive (cpp_num num
, size_t precision
)
1554 if (precision
> PART_PRECISION
)
1556 precision
-= PART_PRECISION
;
1557 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1560 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1563 /* Sign extend a number, with PRECISION significant bits and all
1564 others assumed clear, to fill out a cpp_num structure. */
1566 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1570 if (precision
> PART_PRECISION
)
1572 precision
-= PART_PRECISION
;
1573 if (precision
< PART_PRECISION
1574 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1575 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1577 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1579 if (precision
< PART_PRECISION
)
1580 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1581 num
.high
= ~(cpp_num_part
) 0;
1588 /* Returns the negative of NUM. */
1590 num_negate (cpp_num num
, size_t precision
)
1595 num
.high
= ~num
.high
;
1599 num
= num_trim (num
, precision
);
1600 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1605 /* Returns true if A >= B. */
1607 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1611 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1615 /* Both numbers have signed type. If they are of different
1616 sign, the answer is the sign of A. */
1617 unsignedp
= num_positive (pa
, precision
);
1619 if (unsignedp
!= num_positive (pb
, precision
))
1622 /* Otherwise we can do an unsigned comparison. */
1625 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1628 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1630 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1631 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1633 lhs
.overflow
= false;
1634 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1636 /* As excess precision is zeroed, there is no need to num_trim () as
1637 these operations cannot introduce a set bit there. */
1641 lhs
.high
&= rhs
.high
;
1643 else if (op
== CPP_OR
)
1646 lhs
.high
|= rhs
.high
;
1651 lhs
.high
^= rhs
.high
;
1657 /* Returns LHS OP RHS, where OP is an inequality. */
1659 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1662 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1664 if (op
== CPP_GREATER_EQ
)
1666 else if (op
== CPP_LESS
)
1668 else if (op
== CPP_GREATER
)
1669 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1670 else /* CPP_LESS_EQ. */
1671 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1674 lhs
.overflow
= false;
1675 lhs
.unsignedp
= false;
1679 /* Returns LHS OP RHS, where OP is == or !=. */
1681 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1682 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1684 /* Work around a 3.0.4 bug; see PR 6950. */
1685 bool eq
= num_eq (lhs
, rhs
);
1686 if (op
== CPP_NOT_EQ
)
1690 lhs
.overflow
= false;
1691 lhs
.unsignedp
= false;
1695 /* Shift NUM, of width PRECISION, right by N bits. */
1697 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1699 cpp_num_part sign_mask
;
1700 bool x
= num_positive (num
, precision
);
1702 if (num
.unsignedp
|| x
)
1705 sign_mask
= ~(cpp_num_part
) 0;
1708 num
.high
= num
.low
= sign_mask
;
1712 if (precision
< PART_PRECISION
)
1713 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1714 else if (precision
< 2 * PART_PRECISION
)
1715 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1717 if (n
>= PART_PRECISION
)
1719 n
-= PART_PRECISION
;
1721 num
.high
= sign_mask
;
1726 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1727 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1731 num
= num_trim (num
, precision
);
1732 num
.overflow
= false;
1736 /* Shift NUM, of width PRECISION, left by N bits. */
1738 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1742 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1743 num
.high
= num
.low
= 0;
1747 cpp_num orig
, maybe_orig
;
1751 if (m
>= PART_PRECISION
)
1753 m
-= PART_PRECISION
;
1759 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1762 num
= num_trim (num
, precision
);
1765 num
.overflow
= false;
1768 maybe_orig
= num_rshift (num
, precision
, n
);
1769 num
.overflow
= !num_eq (orig
, maybe_orig
);
1776 /* The four unary operators: +, -, ! and ~. */
1778 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1783 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1784 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1785 "traditional C rejects the unary plus operator");
1786 num
.overflow
= false;
1790 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1794 num
.high
= ~num
.high
;
1796 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1797 num
.overflow
= false;
1800 default: /* case CPP_NOT: */
1801 num
.low
= num_zerop (num
);
1803 num
.overflow
= false;
1804 num
.unsignedp
= false;
1811 /* The various binary operators. */
1813 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1816 size_t precision
= CPP_OPTION (pfile
, precision
);
1824 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1826 /* A negative shift is a positive shift the other way. */
1827 if (op
== CPP_LSHIFT
)
1831 rhs
= num_negate (rhs
, precision
);
1834 n
= ~0; /* Maximal. */
1837 if (op
== CPP_LSHIFT
)
1838 lhs
= num_lshift (lhs
, precision
, n
);
1840 lhs
= num_rshift (lhs
, precision
, n
);
1845 result
.low
= lhs
.low
- rhs
.low
;
1846 result
.high
= lhs
.high
- rhs
.high
;
1847 if (result
.low
> lhs
.low
)
1849 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1850 result
.overflow
= false;
1852 result
= num_trim (result
, precision
);
1853 if (!result
.unsignedp
)
1855 bool lhsp
= num_positive (lhs
, precision
);
1856 result
.overflow
= (lhsp
!= num_positive (rhs
, precision
)
1857 && lhsp
!= num_positive (result
, precision
));
1862 result
.low
= lhs
.low
+ rhs
.low
;
1863 result
.high
= lhs
.high
+ rhs
.high
;
1864 if (result
.low
< lhs
.low
)
1866 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1867 result
.overflow
= false;
1869 result
= num_trim (result
, precision
);
1870 if (!result
.unsignedp
)
1872 bool lhsp
= num_positive (lhs
, precision
);
1873 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1874 && lhsp
!= num_positive (result
, precision
));
1879 default: /* case CPP_COMMA: */
1880 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1881 || !pfile
->state
.skip_eval
))
1882 cpp_error (pfile
, CPP_DL_PEDWARN
,
1883 "comma operator in operand of #if");
1891 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1894 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1897 cpp_num_part middle
[2], temp
;
1899 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1900 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1902 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1903 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1906 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1907 if (result
.low
< temp
)
1911 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1912 if (result
.low
< temp
)
1915 result
.high
+= HIGH_PART (middle
[0]);
1916 result
.high
+= HIGH_PART (middle
[1]);
1917 result
.unsignedp
= true;
1918 result
.overflow
= false;
1923 /* Multiply two preprocessing numbers. */
1925 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1927 cpp_num result
, temp
;
1928 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1929 bool overflow
, negate
= false;
1930 size_t precision
= CPP_OPTION (pfile
, precision
);
1932 /* Prepare for unsigned multiplication. */
1935 if (!num_positive (lhs
, precision
))
1936 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1937 if (!num_positive (rhs
, precision
))
1938 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1941 overflow
= lhs
.high
&& rhs
.high
;
1942 result
= num_part_mul (lhs
.low
, rhs
.low
);
1944 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1945 result
.high
+= temp
.low
;
1949 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1950 result
.high
+= temp
.low
;
1954 temp
.low
= result
.low
, temp
.high
= result
.high
;
1955 result
= num_trim (result
, precision
);
1956 if (!num_eq (result
, temp
))
1960 result
= num_negate (result
, precision
);
1963 result
.overflow
= false;
1965 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1966 && !num_zerop (result
));
1967 result
.unsignedp
= unsignedp
;
1972 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1973 or the remainder depending upon OP. LOCATION is the source location
1974 of this operator (for diagnostics). */
1977 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1978 source_location location
)
1980 cpp_num result
, sub
;
1982 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1983 bool negate
= false, lhs_neg
= false;
1984 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1986 /* Prepare for unsigned division. */
1989 if (!num_positive (lhs
, precision
))
1990 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1991 if (!num_positive (rhs
, precision
))
1992 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1995 /* Find the high bit. */
1999 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2000 for (; ; i
--, mask
>>= 1)
2001 if (rhs
.high
& mask
)
2006 if (precision
> PART_PRECISION
)
2007 i
= precision
- PART_PRECISION
- 1;
2010 mask
= (cpp_num_part
) 1 << i
;
2011 for (; ; i
--, mask
>>= 1)
2017 if (!pfile
->state
.skip_eval
)
2018 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
2019 "division by zero in #if");
2023 /* First nonzero bit of RHS is bit I. Do naive division by
2024 shifting the RHS fully left, and subtracting from LHS if LHS is
2025 at least as big, and then repeating but with one less shift.
2026 This is not very efficient, but is easy to understand. */
2028 rhs
.unsignedp
= true;
2029 lhs
.unsignedp
= true;
2030 i
= precision
- i
- 1;
2031 sub
= num_lshift (rhs
, precision
, i
);
2033 result
.high
= result
.low
= 0;
2036 if (num_greater_eq (lhs
, sub
, precision
))
2038 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
2039 if (i
>= PART_PRECISION
)
2040 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
2042 result
.low
|= (cpp_num_part
) 1 << i
;
2046 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
2050 /* We divide so that the remainder has the sign of the LHS. */
2053 result
.unsignedp
= unsignedp
;
2054 result
.overflow
= false;
2058 result
= num_negate (result
, precision
);
2059 result
.overflow
= (num_positive (result
, precision
) ^ !negate
2060 && !num_zerop (result
));
2067 lhs
.unsignedp
= unsignedp
;
2068 lhs
.overflow
= false;
2070 lhs
= num_negate (lhs
, precision
);
2075 /* Handle meeting "__has_include__" in a preprocessor expression. */
2077 parse_has_include (cpp_reader
*pfile
, enum include_type type
)
2081 cpp_hashnode
*node
= 0;
2082 const cpp_token
*token
;
2083 bool bracket
= false;
2086 result
.unsignedp
= false;
2088 result
.overflow
= false;
2091 pfile
->state
.in__has_include__
++;
2093 token
= cpp_get_token (pfile
);
2094 if (token
->type
== CPP_OPEN_PAREN
)
2097 token
= cpp_get_token (pfile
);
2100 if (token
->type
== CPP_STRING
|| token
->type
== CPP_HEADER_NAME
)
2102 if (token
->type
== CPP_HEADER_NAME
)
2104 fname
= XNEWVEC (char, token
->val
.str
.len
- 1);
2105 memcpy (fname
, token
->val
.str
.text
+ 1, token
->val
.str
.len
- 2);
2106 fname
[token
->val
.str
.len
- 2] = '\0';
2107 node
= token
->val
.node
.node
;
2109 else if (token
->type
== CPP_LESS
)
2112 fname
= _cpp_bracket_include (pfile
);
2115 cpp_error (pfile
, CPP_DL_ERROR
,
2116 "operator \"__has_include__\" requires a header string");
2120 int angle_brackets
= (bracket
? 1 : 0);
2122 if (_cpp_has_header (pfile
, fname
, angle_brackets
, type
))
2130 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
2131 cpp_error (pfile
, CPP_DL_ERROR
,
2132 "missing ')' after \"__has_include__\"");
2134 /* A possible controlling macro of the form #if !__has_include__ ().
2135 _cpp_parse_expr checks there was no other junk on the line. */
2137 pfile
->mi_ind_cmacro
= node
;
2139 pfile
->state
.in__has_include__
--;