1 /* Parse C expressions for cpplib. 2 Copyright (C) 1987-2024 Free Software Foundation, Inc. 3 Contributed by Per Bothner, 1994. 4 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 8 later version. 9 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. 14 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/>. */ 18 19 #include "config.h" 20 #include "system.h" 21 #include "cpplib.h" 22 #include "internal.h" 23 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)) 28 29 struct op 30 { 31 const cpp_token *token; /* The token forming op (for diagnostics). */ 32 cpp_num value; /* The value logically "right" of op. */ 33 location_t loc; /* The location of this value. */ 34 enum cpp_ttype op; 35 }; 36 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); 44 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, 50 enum cpp_ttype); 51 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, 52 enum cpp_ttype); 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, 55 location_t); 56 static cpp_num num_lshift (cpp_num, size_t, size_t); 57 static cpp_num num_rshift (cpp_num, size_t, size_t); 58 59 static cpp_num append_digit (cpp_num, int, int, size_t); 60 static cpp_num parse_defined (cpp_reader *); 61 static cpp_num eval_token (cpp_reader *, const cpp_token *, location_t); 62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); 63 static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t); 64 static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t); 65 static void check_promotion (cpp_reader *, const struct op *); 66 67 /* Token type abuse to create unary plus and minus operators. */ 68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) 69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) 70 71 /* With -O2, gcc appears to produce nice code, moving the error 72 message load and subsequent jump completely out of the main path. */ 73 #define SYNTAX_ERROR(msgid) \ 74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) 75 #define SYNTAX_ERROR2(msgid, arg) \ 76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ 77 while(0) 78 #define SYNTAX_ERROR_AT(loc, msgid) \ 79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \ 80 while(0) 81 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \ 82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \ 83 while(0) 84 85 /* Subroutine of cpp_classify_number. S points to a float suffix of 86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a 87 flag vector (of CPP_N_* bits) describing the suffix. */ 88 static unsigned int 89 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len) 90 { 91 size_t orig_len = len; 92 const uchar *orig_s = s; 93 size_t flags; 94 size_t f, d, l, w, q, i, fn, fnx, fn_bits, bf16; 95 96 flags = 0; 97 f = d = l = w = q = i = fn = fnx = fn_bits = bf16 = 0; 98 99 /* The following decimal float suffixes, from TR 24732:2009, TS 100 18661-2:2015 and C23, are supported: 101 102 df, DF - _Decimal32. 103 dd, DD - _Decimal64. 104 dl, DL - _Decimal128. 105 106 The dN and DN suffixes for _DecimalN, and dNx and DNx for 107 _DecimalNx, defined in TS 18661-3:2015, are not supported. 108 109 Fixed-point suffixes, from TR 18037:2008, are supported. They 110 consist of three parts, in order: 111 112 (i) An optional u or U, for unsigned types. 113 114 (ii) An optional h or H, for short types, or l or L, for long 115 types, or ll or LL, for long long types. Use of ll or LL is a 116 GNU extension. 117 118 (iii) r or R, for _Fract types, or k or K, for _Accum types. 119 120 Otherwise the suffix is for a binary or standard floating-point 121 type. Such a suffix, or the absence of a suffix, may be preceded 122 or followed by i, I, j or J, to indicate an imaginary number with 123 the corresponding complex type. The following suffixes for 124 binary or standard floating-point types are supported: 125 126 f, F - float (ISO C and C++). 127 l, L - long double (ISO C and C++). 128 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in 129 operation (from TR 24732:2009; the pragma and the suffix 130 are not included in TS 18661-2:2015). 131 w, W - machine-specific type such as __float80 (GNU extension). 132 q, Q - machine-specific type such as __float128 (GNU extension). 133 fN, FN - _FloatN (TS 18661-3:2015). 134 fNx, FNx - _FloatNx (TS 18661-3:2015). 135 bf16, BF16 - std::bfloat16_t (ISO C++23). */ 136 137 /* Process decimal float suffixes, which are two letters starting 138 with d or D. Order and case are significant. */ 139 if (len == 2 && (*s == 'd' || *s == 'D')) 140 { 141 bool uppercase = (*s == 'D'); 142 switch (s[1]) 143 { 144 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break; 145 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break; 146 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break; 147 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break; 148 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; 149 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; 150 default: 151 /* Additional two-character suffixes beginning with D are not 152 for decimal float constants. */ 153 break; 154 } 155 } 156 157 if (CPP_OPTION (pfile, ext_numeric_literals)) 158 { 159 /* Recognize a fixed-point suffix. */ 160 if (len != 0) 161 switch (s[len-1]) 162 { 163 case 'k': case 'K': flags = CPP_N_ACCUM; break; 164 case 'r': case 'R': flags = CPP_N_FRACT; break; 165 default: break; 166 } 167 168 /* Continue processing a fixed-point suffix. The suffix is case 169 insensitive except for ll or LL. Order is significant. */ 170 if (flags) 171 { 172 if (len == 1) 173 return flags; 174 len--; 175 176 if (*s == 'u' || *s == 'U') 177 { 178 flags |= CPP_N_UNSIGNED; 179 if (len == 1) 180 return flags; 181 len--; 182 s++; 183 } 184 185 switch (*s) 186 { 187 case 'h': case 'H': 188 if (len == 1) 189 return flags |= CPP_N_SMALL; 190 break; 191 case 'l': 192 if (len == 1) 193 return flags |= CPP_N_MEDIUM; 194 if (len == 2 && s[1] == 'l') 195 return flags |= CPP_N_LARGE; 196 break; 197 case 'L': 198 if (len == 1) 199 return flags |= CPP_N_MEDIUM; 200 if (len == 2 && s[1] == 'L') 201 return flags |= CPP_N_LARGE; 202 break; 203 default: 204 break; 205 } 206 /* Anything left at this point is invalid. */ 207 return 0; 208 } 209 } 210 211 /* In any remaining valid suffix, the case and order don't matter. */ 212 while (len--) 213 { 214 switch (s[0]) 215 { 216 case 'f': case 'F': 217 f++; 218 if (len > 0 219 && s[1] >= '1' 220 && s[1] <= '9' 221 && fn_bits == 0) 222 { 223 f--; 224 while (len > 0 225 && s[1] >= '0' 226 && s[1] <= '9' 227 && fn_bits < CPP_FLOATN_MAX) 228 { 229 fn_bits = fn_bits * 10 + (s[1] - '0'); 230 len--; 231 s++; 232 } 233 if (len > 0 && s[1] == 'x') 234 { 235 fnx++; 236 len--; 237 s++; 238 } 239 else 240 fn++; 241 } 242 break; 243 case 'b': case 'B': 244 if (len > 2 245 /* Except for bf16 / BF16 where case is significant. */ 246 && s[1] == (s[0] == 'b' ? 'f' : 'F') 247 && s[2] == '1' 248 && s[3] == '6') 249 { 250 bf16++; 251 len -= 3; 252 s += 3; 253 break; 254 } 255 return 0; 256 case 'd': case 'D': d++; break; 257 case 'l': case 'L': l++; break; 258 case 'w': case 'W': w++; break; 259 case 'q': case 'Q': q++; break; 260 case 'i': case 'I': 261 case 'j': case 'J': i++; break; 262 default: 263 return 0; 264 } 265 s++; 266 } 267 268 /* Reject any case of multiple suffixes specifying types, multiple 269 suffixes specifying an imaginary constant, _FloatN or _FloatNx 270 suffixes for invalid values of N, and _FloatN suffixes for values 271 of N larger than can be represented in the return value. The 272 caller is responsible for rejecting _FloatN suffixes where 273 _FloatN is not supported on the chosen target. */ 274 if (f + d + l + w + q + fn + fnx + bf16 > 1 || i > 1) 275 return 0; 276 if (fn_bits > CPP_FLOATN_MAX) 277 return 0; 278 if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128) 279 return 0; 280 if (fn && fn_bits != 16 && fn_bits % 32 != 0) 281 return 0; 282 if (fn && fn_bits == 96) 283 return 0; 284 285 if (i) 286 { 287 if (!CPP_OPTION (pfile, ext_numeric_literals)) 288 return 0; 289 290 /* In C++14 and up these suffixes are in the standard library, so treat 291 them as user-defined literals. */ 292 if (CPP_OPTION (pfile, cplusplus) 293 && CPP_OPTION (pfile, lang) > CLK_CXX11 294 && orig_s[0] == 'i' 295 && (orig_len == 1 296 || (orig_len == 2 297 && (orig_s[1] == 'f' || orig_s[1] == 'l')))) 298 return 0; 299 } 300 301 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals)) 302 return 0; 303 304 return ((i ? CPP_N_IMAGINARY : 0) 305 | (f ? CPP_N_SMALL : 306 d ? CPP_N_MEDIUM : 307 l ? CPP_N_LARGE : 308 w ? CPP_N_MD_W : 309 q ? CPP_N_MD_Q : 310 fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) : 311 fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) : 312 bf16 ? CPP_N_BFLOAT16 : 313 CPP_N_DEFAULT)); 314 } 315 316 /* Return the classification flags for a float suffix. */ 317 unsigned int 318 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len) 319 { 320 return interpret_float_suffix (pfile, (const unsigned char *)s, len); 321 } 322 323 /* Subroutine of cpp_classify_number. S points to an integer suffix 324 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a 325 flag vector describing the suffix. */ 326 static unsigned int 327 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len) 328 { 329 size_t orig_len = len; 330 size_t u, l, i, z, wb; 331 332 u = l = i = z = wb = 0; 333 334 while (len--) 335 switch (s[len]) 336 { 337 case 'z': case 'Z': z++; break; 338 case 'u': case 'U': u++; break; 339 case 'i': case 'I': 340 case 'j': case 'J': i++; break; 341 case 'l': case 'L': l++; 342 /* If there are two Ls, they must be adjacent and the same case. */ 343 if (l == 2 && s[len] != s[len + 1]) 344 return 0; 345 break; 346 case 'b': 347 if (len == 0 || s[len - 1] != 'w') 348 return 0; 349 wb++; 350 len--; 351 break; 352 case 'B': 353 if (len == 0 || s[len - 1] != 'W') 354 return 0; 355 wb++; 356 len--; 357 break; 358 default: 359 return 0; 360 } 361 362 if (l > 2 || u > 1 || i > 1 || z > 1 || wb > 1) 363 return 0; 364 365 if (z) 366 { 367 if (l > 0 || i > 0) 368 return 0; 369 if (!CPP_OPTION (pfile, cplusplus)) 370 return 0; 371 } 372 373 if (wb) 374 { 375 if (CPP_OPTION (pfile, cplusplus)) 376 return 0; 377 if (l > 0 || i > 0 || z > 0) 378 return 0; 379 } 380 381 if (i) 382 { 383 if (!CPP_OPTION (pfile, ext_numeric_literals)) 384 return 0; 385 386 /* In C++14 and up these suffixes are in the standard library, so treat 387 them as user-defined literals. */ 388 if (CPP_OPTION (pfile, cplusplus) 389 && CPP_OPTION (pfile, lang) > CLK_CXX11 390 && s[0] == 'i' 391 && (orig_len == 1 || (orig_len == 2 && s[1] == 'l'))) 392 return 0; 393 } 394 395 return ((i ? CPP_N_IMAGINARY : 0) 396 | (u ? CPP_N_UNSIGNED : 0) 397 | ((l == 0) ? CPP_N_SMALL 398 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE) 399 | (z ? CPP_N_SIZE_T : 0) 400 | (wb ? CPP_N_BITINT : 0)); 401 } 402 403 /* Return the classification flags for an int suffix. */ 404 unsigned int 405 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len) 406 { 407 return interpret_int_suffix (pfile, (const unsigned char *)s, len); 408 } 409 410 /* Return the string type corresponding to the the input user-defined string 411 literal type. If the input type is not a user-defined string literal 412 type return the input type. */ 413 enum cpp_ttype 414 cpp_userdef_string_remove_type (enum cpp_ttype type) 415 { 416 if (type == CPP_STRING_USERDEF) 417 return CPP_STRING; 418 else if (type == CPP_WSTRING_USERDEF) 419 return CPP_WSTRING; 420 else if (type == CPP_STRING16_USERDEF) 421 return CPP_STRING16; 422 else if (type == CPP_STRING32_USERDEF) 423 return CPP_STRING32; 424 else if (type == CPP_UTF8STRING_USERDEF) 425 return CPP_UTF8STRING; 426 else 427 return type; 428 } 429 430 /* Return the user-defined string literal type corresponding to the input 431 string type. If the input type is not a string type return the input 432 type. */ 433 enum cpp_ttype 434 cpp_userdef_string_add_type (enum cpp_ttype type) 435 { 436 if (type == CPP_STRING) 437 return CPP_STRING_USERDEF; 438 else if (type == CPP_WSTRING) 439 return CPP_WSTRING_USERDEF; 440 else if (type == CPP_STRING16) 441 return CPP_STRING16_USERDEF; 442 else if (type == CPP_STRING32) 443 return CPP_STRING32_USERDEF; 444 else if (type == CPP_UTF8STRING) 445 return CPP_UTF8STRING_USERDEF; 446 else 447 return type; 448 } 449 450 /* Return the char type corresponding to the the input user-defined char 451 literal type. If the input type is not a user-defined char literal 452 type return the input type. */ 453 enum cpp_ttype 454 cpp_userdef_char_remove_type (enum cpp_ttype type) 455 { 456 if (type == CPP_CHAR_USERDEF) 457 return CPP_CHAR; 458 else if (type == CPP_WCHAR_USERDEF) 459 return CPP_WCHAR; 460 else if (type == CPP_CHAR16_USERDEF) 461 return CPP_CHAR16; 462 else if (type == CPP_CHAR32_USERDEF) 463 return CPP_CHAR32; 464 else if (type == CPP_UTF8CHAR_USERDEF) 465 return CPP_UTF8CHAR; 466 else 467 return type; 468 } 469 470 /* Return the user-defined char literal type corresponding to the input 471 char type. If the input type is not a char type return the input 472 type. */ 473 enum cpp_ttype 474 cpp_userdef_char_add_type (enum cpp_ttype type) 475 { 476 if (type == CPP_CHAR) 477 return CPP_CHAR_USERDEF; 478 else if (type == CPP_WCHAR) 479 return CPP_WCHAR_USERDEF; 480 else if (type == CPP_CHAR16) 481 return CPP_CHAR16_USERDEF; 482 else if (type == CPP_CHAR32) 483 return CPP_CHAR32_USERDEF; 484 else if (type == CPP_UTF8CHAR) 485 return CPP_UTF8CHAR_USERDEF; 486 else 487 return type; 488 } 489 490 /* Return true if the token type is a user-defined string literal. */ 491 bool 492 cpp_userdef_string_p (enum cpp_ttype type) 493 { 494 if (type == CPP_STRING_USERDEF 495 || type == CPP_WSTRING_USERDEF 496 || type == CPP_STRING16_USERDEF 497 || type == CPP_STRING32_USERDEF 498 || type == CPP_UTF8STRING_USERDEF) 499 return true; 500 else 501 return false; 502 } 503 504 /* Return true if the token type is a user-defined char literal. */ 505 bool 506 cpp_userdef_char_p (enum cpp_ttype type) 507 { 508 if (type == CPP_CHAR_USERDEF 509 || type == CPP_WCHAR_USERDEF 510 || type == CPP_CHAR16_USERDEF 511 || type == CPP_CHAR32_USERDEF 512 || type == CPP_UTF8CHAR_USERDEF) 513 return true; 514 else 515 return false; 516 } 517 518 /* Extract the suffix from a user-defined literal string or char. */ 519 const char * 520 cpp_get_userdef_suffix (const cpp_token *tok) 521 { 522 unsigned int len = tok->val.str.len; 523 const char *text = (const char *)tok->val.str.text; 524 char delim; 525 unsigned int i; 526 for (i = 0; i < len; ++i) 527 if (text[i] == '\'' || text[i] == '"') 528 break; 529 if (i == len) 530 return text + len; 531 delim = text[i]; 532 for (i = len; i > 0; --i) 533 if (text[i - 1] == delim) 534 break; 535 return text + i; 536 } 537 538 /* Categorize numeric constants according to their field (integer, 539 floating point, or invalid), radix (decimal, octal, hexadecimal), 540 and type suffixes. 541 542 TOKEN is the token that represents the numeric constant to 543 classify. 544 545 In C++0X if UD_SUFFIX is non null it will be assigned 546 any unrecognized suffix for a user-defined literal. 547 548 VIRTUAL_LOCATION is the virtual location for TOKEN. */ 549 unsigned int 550 cpp_classify_number (cpp_reader *pfile, const cpp_token *token, 551 const char **ud_suffix, location_t virtual_location) 552 { 553 const uchar *str = token->val.str.text; 554 const uchar *limit; 555 unsigned int max_digit, result, radix; 556 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; 557 bool seen_digit; 558 bool seen_digit_sep; 559 560 if (ud_suffix) 561 *ud_suffix = NULL; 562 563 /* If the lexer has done its job, length one can only be a single 564 digit. Fast-path this very common case. */ 565 if (token->val.str.len == 1) 566 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; 567 568 limit = str + token->val.str.len; 569 float_flag = NOT_FLOAT; 570 max_digit = 0; 571 radix = 10; 572 seen_digit = false; 573 seen_digit_sep = false; 574 575 /* First, interpret the radix. */ 576 if (*str == '0') 577 { 578 radix = 8; 579 str++; 580 581 /* Require at least one hex digit to classify it as hex. */ 582 if (*str == 'x' || *str == 'X') 583 { 584 if (str[1] == '.' || ISXDIGIT (str[1])) 585 { 586 radix = 16; 587 str++; 588 } 589 else if (DIGIT_SEP (str[1])) 590 SYNTAX_ERROR_AT (virtual_location, 591 "digit separator after base indicator"); 592 } 593 else if (*str == 'b' || *str == 'B') 594 { 595 if (str[1] == '0' || str[1] == '1') 596 { 597 radix = 2; 598 str++; 599 } 600 else if (DIGIT_SEP (str[1])) 601 SYNTAX_ERROR_AT (virtual_location, 602 "digit separator after base indicator"); 603 } 604 } 605 606 /* Now scan for a well-formed integer or float. */ 607 for (;;) 608 { 609 unsigned int c = *str++; 610 611 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) 612 { 613 seen_digit_sep = false; 614 seen_digit = true; 615 c = hex_value (c); 616 if (c > max_digit) 617 max_digit = c; 618 } 619 else if (DIGIT_SEP (c)) 620 seen_digit_sep = true; 621 else if (c == '.') 622 { 623 if (seen_digit_sep || DIGIT_SEP (*str)) 624 SYNTAX_ERROR_AT (virtual_location, 625 "digit separator adjacent to decimal point"); 626 seen_digit_sep = false; 627 if (float_flag == NOT_FLOAT) 628 float_flag = AFTER_POINT; 629 else 630 SYNTAX_ERROR_AT (virtual_location, 631 "too many decimal points in number"); 632 } 633 else if ((radix <= 10 && (c == 'e' || c == 'E')) 634 || (radix == 16 && (c == 'p' || c == 'P'))) 635 { 636 if (seen_digit_sep || DIGIT_SEP (*str)) 637 SYNTAX_ERROR_AT (virtual_location, 638 "digit separator adjacent to exponent"); 639 float_flag = AFTER_EXPON; 640 break; 641 } 642 else 643 { 644 /* Start of suffix. */ 645 str--; 646 break; 647 } 648 } 649 650 if (seen_digit_sep && float_flag != AFTER_EXPON) 651 SYNTAX_ERROR_AT (virtual_location, 652 "digit separator outside digit sequence"); 653 654 /* The suffix may be for decimal fixed-point constants without exponent. */ 655 if (radix != 16 && float_flag == NOT_FLOAT) 656 { 657 result = interpret_float_suffix (pfile, str, limit - str); 658 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) 659 { 660 result |= CPP_N_FLOATING; 661 /* We need to restore the radix to 10, if the radix is 8. */ 662 if (radix == 8) 663 radix = 10; 664 665 if (CPP_PEDANTIC (pfile)) 666 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 667 "fixed-point constants are a GCC extension"); 668 goto syntax_ok; 669 } 670 else 671 result = 0; 672 } 673 674 if (float_flag != NOT_FLOAT && radix == 8) 675 radix = 10; 676 677 if (max_digit >= radix) 678 { 679 if (radix == 2) 680 SYNTAX_ERROR2_AT (virtual_location, 681 "invalid digit \"%c\" in binary constant", '0' + max_digit); 682 else 683 SYNTAX_ERROR2_AT (virtual_location, 684 "invalid digit \"%c\" in octal constant", '0' + max_digit); 685 } 686 687 if (float_flag != NOT_FLOAT) 688 { 689 if (radix == 2) 690 { 691 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 692 "invalid prefix \"0b\" for floating constant"); 693 return CPP_N_INVALID; 694 } 695 696 if (radix == 16 && !seen_digit) 697 SYNTAX_ERROR_AT (virtual_location, 698 "no digits in hexadecimal floating constant"); 699 700 if (radix == 16 && CPP_PEDANTIC (pfile) 701 && !CPP_OPTION (pfile, extended_numbers)) 702 { 703 if (CPP_OPTION (pfile, cplusplus)) 704 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 705 "use of C++17 hexadecimal floating constant"); 706 else 707 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 708 "use of C99 hexadecimal floating constant"); 709 } 710 711 if (float_flag == AFTER_EXPON) 712 { 713 if (*str == '+' || *str == '-') 714 str++; 715 716 /* Exponent is decimal, even if string is a hex float. */ 717 if (!ISDIGIT (*str)) 718 { 719 if (DIGIT_SEP (*str)) 720 SYNTAX_ERROR_AT (virtual_location, 721 "digit separator adjacent to exponent"); 722 else 723 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits"); 724 } 725 do 726 { 727 seen_digit_sep = DIGIT_SEP (*str); 728 str++; 729 } 730 while (ISDIGIT (*str) || DIGIT_SEP (*str)); 731 } 732 else if (radix == 16) 733 SYNTAX_ERROR_AT (virtual_location, 734 "hexadecimal floating constants require an exponent"); 735 736 if (seen_digit_sep) 737 SYNTAX_ERROR_AT (virtual_location, 738 "digit separator outside digit sequence"); 739 740 result = interpret_float_suffix (pfile, str, limit - str); 741 if (result == 0) 742 { 743 if (CPP_OPTION (pfile, user_literals)) 744 { 745 if (ud_suffix) 746 *ud_suffix = (const char *) str; 747 result = CPP_N_LARGE | CPP_N_USERDEF; 748 } 749 else 750 { 751 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 752 "invalid suffix \"%.*s\" on floating constant", 753 (int) (limit - str), str); 754 return CPP_N_INVALID; 755 } 756 } 757 758 /* Traditional C didn't accept any floating suffixes. */ 759 if (limit != str 760 && CPP_WTRADITIONAL (pfile) 761 && ! cpp_sys_macro_p (pfile)) 762 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0, 763 "traditional C rejects the \"%.*s\" suffix", 764 (int) (limit - str), str); 765 766 /* A suffix for double is a GCC extension via decimal float support. 767 If the suffix also specifies an imaginary value we'll catch that 768 later. */ 769 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) 770 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 771 "suffix for double constant is a GCC extension"); 772 773 /* Radix must be 10 for decimal floats. */ 774 if ((result & CPP_N_DFLOAT) && radix != 10) 775 { 776 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 777 "invalid suffix \"%.*s\" with hexadecimal floating constant", 778 (int) (limit - str), str); 779 return CPP_N_INVALID; 780 } 781 782 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) 783 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 784 "fixed-point constants are a GCC extension"); 785 786 if (result & CPP_N_DFLOAT) 787 { 788 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, dfp_constants)) 789 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 790 "decimal float constants are a C23 feature"); 791 else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0) 792 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT, 793 virtual_location, 0, 794 "decimal float constants are a C23 feature"); 795 } 796 797 result |= CPP_N_FLOATING; 798 } 799 else 800 { 801 result = interpret_int_suffix (pfile, str, limit - str); 802 if (result == 0) 803 { 804 if (CPP_OPTION (pfile, user_literals)) 805 { 806 if (ud_suffix) 807 *ud_suffix = (const char *) str; 808 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF; 809 } 810 else 811 { 812 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 813 "invalid suffix \"%.*s\" on integer constant", 814 (int) (limit - str), str); 815 return CPP_N_INVALID; 816 } 817 } 818 819 /* Traditional C only accepted the 'L' suffix. 820 Suppress warning about 'LL' with -Wno-long-long. */ 821 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) 822 { 823 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); 824 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE 825 && CPP_OPTION (pfile, cpp_warn_long_long); 826 827 if (u_or_i || large) 828 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL, 829 virtual_location, 0, 830 "traditional C rejects the \"%.*s\" suffix", 831 (int) (limit - str), str); 832 } 833 834 if ((result & CPP_N_WIDTH) == CPP_N_LARGE 835 && CPP_OPTION (pfile, cpp_warn_long_long)) 836 { 837 const char *message = CPP_OPTION (pfile, cplusplus) 838 ? N_("use of C++11 long long integer constant") 839 : N_("use of C99 long long integer constant"); 840 841 if (CPP_OPTION (pfile, c99)) 842 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location, 843 0, message); 844 else 845 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG, 846 virtual_location, 0, message); 847 } 848 849 if ((result & CPP_N_SIZE_T) == CPP_N_SIZE_T 850 && !CPP_OPTION (pfile, size_t_literals)) 851 { 852 const char *message = (result & CPP_N_UNSIGNED) == CPP_N_UNSIGNED 853 ? N_("use of C++23 %<size_t%> integer constant") 854 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant"); 855 cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS, 856 virtual_location, 0, message); 857 } 858 859 if ((result & CPP_N_BITINT) != 0 860 && CPP_OPTION (pfile, cpp_warn_c11_c23_compat) != 0) 861 { 862 if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0) 863 { 864 const char *message = N_("ISO C does not support literal " 865 "%<wb%> suffixes before C23"); 866 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, true_false)) 867 cpp_pedwarning_with_line (pfile, CPP_W_C11_C23_COMPAT, 868 virtual_location, 0, message); 869 else 870 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT, 871 virtual_location, 0, message); 872 } 873 else if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, true_false)) 874 { 875 const char *message = N_("ISO C does not support literal " 876 "%<wb%> suffixes before C23"); 877 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 878 message); 879 } 880 } 881 882 result |= CPP_N_INTEGER; 883 } 884 885 syntax_ok: 886 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) 887 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 888 "imaginary constants are a GCC extension"); 889 if (radix == 2) 890 { 891 if (!CPP_OPTION (pfile, binary_constants) 892 && CPP_PEDANTIC (pfile)) 893 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, 894 CPP_OPTION (pfile, cplusplus) 895 ? N_("binary constants are a C++14 feature " 896 "or GCC extension") 897 : N_("binary constants are a C23 feature " 898 "or GCC extension")); 899 else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0) 900 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT, 901 virtual_location, 0, 902 "binary constants are a C23 feature"); 903 } 904 905 if (radix == 10) 906 result |= CPP_N_DECIMAL; 907 else if (radix == 16) 908 result |= CPP_N_HEX; 909 else if (radix == 2) 910 result |= CPP_N_BINARY; 911 else 912 result |= CPP_N_OCTAL; 913 914 return result; 915 916 syntax_error: 917 return CPP_N_INVALID; 918 } 919 920 /* cpp_interpret_integer converts an integer constant into a cpp_num, 921 of precision options->precision. 922 923 We do not provide any interface for decimal->float conversion, 924 because the preprocessor doesn't need it and we don't want to 925 drag in GCC's floating point emulator. */ 926 cpp_num 927 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, 928 unsigned int type) 929 { 930 const uchar *p, *end; 931 cpp_num result; 932 933 result.low = 0; 934 result.high = 0; 935 result.unsignedp = !!(type & CPP_N_UNSIGNED); 936 result.overflow = false; 937 938 p = token->val.str.text; 939 end = p + token->val.str.len; 940 941 /* Common case of a single digit. */ 942 if (token->val.str.len == 1) 943 result.low = p[0] - '0'; 944 else 945 { 946 cpp_num_part max; 947 size_t precision = CPP_OPTION (pfile, precision); 948 unsigned int base = 10, c = 0; 949 bool overflow = false; 950 951 if ((type & CPP_N_RADIX) == CPP_N_OCTAL) 952 { 953 base = 8; 954 p++; 955 } 956 else if ((type & CPP_N_RADIX) == CPP_N_HEX) 957 { 958 base = 16; 959 p += 2; 960 } 961 else if ((type & CPP_N_RADIX) == CPP_N_BINARY) 962 { 963 base = 2; 964 p += 2; 965 } 966 967 /* We can add a digit to numbers strictly less than this without 968 needing the precision and slowness of double integers. */ 969 max = ~(cpp_num_part) 0; 970 if (precision < PART_PRECISION) 971 max >>= PART_PRECISION - precision; 972 max = (max - base + 1) / base + 1; 973 974 for (; p < end; p++) 975 { 976 c = *p; 977 978 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) 979 c = hex_value (c); 980 else if (DIGIT_SEP (c)) 981 continue; 982 else 983 break; 984 985 /* Strict inequality for when max is set to zero. */ 986 if (result.low < max) 987 result.low = result.low * base + c; 988 else 989 { 990 result = append_digit (result, c, base, precision); 991 overflow |= result.overflow; 992 max = 0; 993 } 994 } 995 996 if (overflow && !(type & CPP_N_USERDEF)) 997 cpp_error (pfile, CPP_DL_PEDWARN, 998 "integer constant is too large for its type"); 999 /* If too big to be signed, consider it unsigned. Only warn for 1000 decimal numbers. Traditional numbers were always signed (but 1001 we still honor an explicit U suffix); but we only have 1002 traditional semantics in directives. */ 1003 else if (!result.unsignedp 1004 && !(CPP_OPTION (pfile, traditional) 1005 && pfile->state.in_directive) 1006 && !num_positive (result, precision)) 1007 { 1008 /* This is for constants within the range of uintmax_t but 1009 not that of intmax_t. For such decimal constants, a 1010 diagnostic is required for C99 as the selected type must 1011 be signed and not having a type is a constraint violation 1012 (DR#298, TC3), so this must be a pedwarn. For C90, 1013 unsigned long is specified to be used for a constant that 1014 does not fit in signed long; if uintmax_t has the same 1015 range as unsigned long this means only a warning is 1016 appropriate here. C90 permits the preprocessor to use a 1017 wider range than unsigned long in the compiler, so if 1018 uintmax_t is wider than unsigned long no diagnostic is 1019 required for such constants in preprocessor #if 1020 expressions and the compiler will pedwarn for such 1021 constants outside the range of unsigned long that reach 1022 the compiler so a diagnostic is not required there 1023 either; thus, pedwarn for C99 but use a plain warning for 1024 C90. */ 1025 if (base == 10) 1026 cpp_error (pfile, (CPP_OPTION (pfile, c99) 1027 ? CPP_DL_PEDWARN 1028 : CPP_DL_WARNING), 1029 "integer constant is so large that it is unsigned"); 1030 result.unsignedp = true; 1031 } 1032 } 1033 1034 return result; 1035 } 1036 1037 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ 1038 static cpp_num 1039 append_digit (cpp_num num, int digit, int base, size_t precision) 1040 { 1041 cpp_num result; 1042 unsigned int shift; 1043 bool overflow; 1044 cpp_num_part add_high, add_low; 1045 1046 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't 1047 need to worry about add_high overflowing. */ 1048 switch (base) 1049 { 1050 case 2: 1051 shift = 1; 1052 break; 1053 1054 case 16: 1055 shift = 4; 1056 break; 1057 1058 default: 1059 shift = 3; 1060 } 1061 overflow = !!(num.high >> (PART_PRECISION - shift)); 1062 result.high = num.high << shift; 1063 result.low = num.low << shift; 1064 result.high |= num.low >> (PART_PRECISION - shift); 1065 result.unsignedp = num.unsignedp; 1066 1067 if (base == 10) 1068 { 1069 add_low = num.low << 1; 1070 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); 1071 } 1072 else 1073 add_high = add_low = 0; 1074 1075 if (add_low + digit < add_low) 1076 add_high++; 1077 add_low += digit; 1078 1079 if (result.low + add_low < result.low) 1080 add_high++; 1081 if (result.high + add_high < result.high) 1082 overflow = true; 1083 1084 result.low += add_low; 1085 result.high += add_high; 1086 result.overflow = overflow; 1087 1088 /* The above code catches overflow of a cpp_num type. This catches 1089 overflow of the (possibly shorter) target precision. */ 1090 num.low = result.low; 1091 num.high = result.high; 1092 result = num_trim (result, precision); 1093 if (!num_eq (result, num)) 1094 result.overflow = true; 1095 1096 return result; 1097 } 1098 1099 /* Handle meeting "defined" in a preprocessor expression. */ 1100 static cpp_num 1101 parse_defined (cpp_reader *pfile) 1102 { 1103 cpp_num result; 1104 int paren = 0; 1105 cpp_hashnode *node = 0; 1106 const cpp_token *token; 1107 cpp_context *initial_context = pfile->context; 1108 1109 /* Don't expand macros. */ 1110 pfile->state.prevent_expansion++; 1111 1112 token = cpp_get_token (pfile); 1113 if (token->type == CPP_OPEN_PAREN) 1114 { 1115 paren = 1; 1116 token = cpp_get_token (pfile); 1117 } 1118 1119 if (token->type == CPP_NAME) 1120 { 1121 node = token->val.node.node; 1122 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) 1123 { 1124 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); 1125 node = 0; 1126 } 1127 } 1128 else 1129 { 1130 cpp_error (pfile, CPP_DL_ERROR, 1131 "operator \"defined\" requires an identifier"); 1132 if (token->flags & NAMED_OP) 1133 { 1134 cpp_token op; 1135 1136 op.flags = 0; 1137 op.type = token->type; 1138 cpp_error (pfile, CPP_DL_ERROR, 1139 "(\"%s\" is an alternative token for \"%s\" in C++)", 1140 cpp_token_as_text (pfile, token), 1141 cpp_token_as_text (pfile, &op)); 1142 } 1143 } 1144 1145 bool is_defined = false; 1146 if (node) 1147 { 1148 if ((pfile->context != initial_context 1149 || initial_context != &pfile->base_context) 1150 && CPP_OPTION (pfile, warn_expansion_to_defined)) 1151 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED, 1152 "this use of \"defined\" may not be portable"); 1153 is_defined = _cpp_defined_macro_p (node); 1154 if (!_cpp_maybe_notify_macro_use (pfile, node, token->src_loc)) 1155 /* It wasn't a macro after all. */ 1156 is_defined = false; 1157 _cpp_mark_macro_used (node); 1158 1159 /* A possible controlling macro of the form #if !defined (). 1160 _cpp_parse_expr checks there was no other junk on the line. */ 1161 pfile->mi_ind_cmacro = node; 1162 } 1163 1164 pfile->state.prevent_expansion--; 1165 1166 /* Do not treat conditional macros as being defined. This is due to the 1167 powerpc port using conditional macros for 'vector', 'bool', and 'pixel' 1168 to act as conditional keywords. This messes up tests like #ifndef 1169 bool. */ 1170 result.unsignedp = false; 1171 result.high = 0; 1172 result.overflow = false; 1173 result.low = is_defined; 1174 return result; 1175 } 1176 1177 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing 1178 number or character constant, or the result of the "defined" or "#" 1179 operators). */ 1180 static cpp_num 1181 eval_token (cpp_reader *pfile, const cpp_token *token, 1182 location_t virtual_location) 1183 { 1184 cpp_num result; 1185 unsigned int temp; 1186 int unsignedp = 0; 1187 1188 result.unsignedp = false; 1189 result.overflow = false; 1190 1191 switch (token->type) 1192 { 1193 case CPP_NUMBER: 1194 temp = cpp_classify_number (pfile, token, NULL, virtual_location); 1195 if (temp & CPP_N_USERDEF) 1196 cpp_error (pfile, CPP_DL_ERROR, 1197 "user-defined literal in preprocessor expression"); 1198 switch (temp & CPP_N_CATEGORY) 1199 { 1200 case CPP_N_FLOATING: 1201 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 1202 "floating constant in preprocessor expression"); 1203 break; 1204 case CPP_N_INTEGER: 1205 if (!(temp & CPP_N_IMAGINARY)) 1206 return cpp_interpret_integer (pfile, token, temp); 1207 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, 1208 "imaginary number in preprocessor expression"); 1209 break; 1210 1211 case CPP_N_INVALID: 1212 /* Error already issued. */ 1213 break; 1214 } 1215 result.high = result.low = 0; 1216 break; 1217 1218 case CPP_WCHAR: 1219 case CPP_CHAR: 1220 case CPP_CHAR16: 1221 case CPP_CHAR32: 1222 case CPP_UTF8CHAR: 1223 { 1224 cppchar_t cc = cpp_interpret_charconst (pfile, token, 1225 &temp, &unsignedp); 1226 1227 result.high = 0; 1228 result.low = cc; 1229 /* Sign-extend the result if necessary. */ 1230 if (!unsignedp && (cppchar_signed_t) cc < 0) 1231 { 1232 if (PART_PRECISION > BITS_PER_CPPCHAR_T) 1233 result.low |= ~(~(cpp_num_part) 0 1234 >> (PART_PRECISION - BITS_PER_CPPCHAR_T)); 1235 result.high = ~(cpp_num_part) 0; 1236 result = num_trim (result, CPP_OPTION (pfile, precision)); 1237 } 1238 } 1239 break; 1240 1241 case CPP_NAME: 1242 if (token->val.node.node == pfile->spec_nodes.n_defined) 1243 return parse_defined (pfile); 1244 else if (CPP_OPTION (pfile, true_false) 1245 && (token->val.node.node == pfile->spec_nodes.n_true 1246 || token->val.node.node == pfile->spec_nodes.n_false)) 1247 { 1248 result.high = 0; 1249 result.low = (token->val.node.node == pfile->spec_nodes.n_true); 1250 } 1251 else 1252 { 1253 result.high = 0; 1254 result.low = 0; 1255 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) 1256 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0, 1257 "\"%s\" is not defined, evaluates to 0", 1258 NODE_NAME (token->val.node.node)); 1259 } 1260 break; 1261 1262 case CPP_HASH: 1263 if (!pfile->state.skipping) 1264 { 1265 /* A pedantic warning takes precedence over a deprecated 1266 warning here. */ 1267 if (CPP_PEDANTIC (pfile)) 1268 cpp_error_with_line (pfile, CPP_DL_PEDWARN, 1269 virtual_location, 0, 1270 "assertions are a GCC extension"); 1271 else if (CPP_OPTION (pfile, cpp_warn_deprecated)) 1272 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0, 1273 "assertions are a deprecated extension"); 1274 } 1275 _cpp_test_assertion (pfile, &temp); 1276 result.high = 0; 1277 result.low = temp; 1278 break; 1279 1280 default: 1281 abort (); 1282 } 1283 1284 result.unsignedp = !!unsignedp; 1285 return result; 1286 } 1287 1288 /* Operator precedence and flags table. 1290 1291 After an operator is returned from the lexer, if it has priority less 1292 than the operator on the top of the stack, we reduce the stack by one 1293 operator and repeat the test. Since equal priorities do not reduce, 1294 this is naturally right-associative. 1295 1296 We handle left-associative operators by decrementing the priority of 1297 just-lexed operators by one, but retaining the priority of operators 1298 already on the stack. 1299 1300 The remaining cases are '(' and ')'. We handle '(' by skipping the 1301 reduction phase completely. ')' is given lower priority than 1302 everything else, including '(', effectively forcing a reduction of the 1303 parenthesized expression. If there is a matching '(', the routine 1304 reduce() exits immediately. If the normal exit route sees a ')', then 1305 there cannot have been a matching '(' and an error message is output. 1306 1307 The parser assumes all shifted operators require a left operand unless 1308 the flag NO_L_OPERAND is set. These semantics are automatic; any 1309 extra semantics need to be handled with operator-specific code. */ 1310 1311 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an 1312 operand changes because of integer promotions. */ 1313 #define NO_L_OPERAND (1 << 0) 1314 #define LEFT_ASSOC (1 << 1) 1315 #define CHECK_PROMOTION (1 << 2) 1316 1317 /* Operator to priority map. Must be in the same order as the first 1318 N entries of enum cpp_ttype. */ 1319 static const struct cpp_operator 1320 { 1321 uchar prio; 1322 uchar flags; 1323 } optab[] = 1324 { 1325 /* EQ */ {0, 0}, /* Shouldn't happen. */ 1326 /* NOT */ {16, NO_L_OPERAND}, 1327 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 1328 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 1329 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, 1330 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, 1331 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 1332 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 1333 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION}, 1334 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION}, 1335 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION}, 1336 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION}, 1337 /* RSHIFT */ {13, LEFT_ASSOC}, 1338 /* LSHIFT */ {13, LEFT_ASSOC}, 1339 1340 /* COMPL */ {16, NO_L_OPERAND}, 1341 /* AND_AND */ {6, LEFT_ASSOC}, 1342 /* OR_OR */ {5, LEFT_ASSOC}, 1343 /* Note that QUERY, COLON, and COMMA must have the same precedence. 1344 However, there are some special cases for these in reduce(). */ 1345 /* QUERY */ {4, 0}, 1346 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, 1347 /* COMMA */ {4, LEFT_ASSOC}, 1348 /* OPEN_PAREN */ {1, NO_L_OPERAND}, 1349 /* CLOSE_PAREN */ {0, 0}, 1350 /* EOF */ {0, 0}, 1351 /* EQ_EQ */ {11, LEFT_ASSOC}, 1352 /* NOT_EQ */ {11, LEFT_ASSOC}, 1353 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 1354 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, 1355 /* UPLUS */ {16, NO_L_OPERAND}, 1356 /* UMINUS */ {16, NO_L_OPERAND} 1357 }; 1358 1359 /* Parse and evaluate a C expression, reading from PFILE. 1360 Returns the truth value of the expression. 1361 1362 The implementation is an operator precedence parser, i.e. a 1363 bottom-up parser, using a stack for not-yet-reduced tokens. 1364 1365 The stack base is op_stack, and the current stack pointer is 'top'. 1366 There is a stack element for each operator (only), and the most 1367 recently pushed operator is 'top->op'. An operand (value) is 1368 stored in the 'value' field of the stack element of the operator 1369 that precedes it. */ 1370 bool 1371 _cpp_parse_expr (cpp_reader *pfile, bool is_if) 1372 { 1373 struct op *top = pfile->op_stack; 1374 unsigned int lex_count; 1375 bool saw_leading_not, want_value = true; 1376 location_t virtual_location = 0; 1377 1378 pfile->state.skip_eval = 0; 1379 1380 /* Set up detection of #if ! defined(). */ 1381 pfile->mi_ind_cmacro = 0; 1382 saw_leading_not = false; 1383 lex_count = 0; 1384 1385 /* Lowest priority operator prevents further reductions. */ 1386 top->op = CPP_EOF; 1387 1388 for (;;) 1389 { 1390 struct op op; 1391 1392 lex_count++; 1393 op.token = cpp_get_token_with_location (pfile, &virtual_location); 1394 op.op = op.token->type; 1395 op.loc = virtual_location; 1396 1397 switch (op.op) 1398 { 1399 /* These tokens convert into values. */ 1400 case CPP_NUMBER: 1401 case CPP_CHAR: 1402 case CPP_WCHAR: 1403 case CPP_CHAR16: 1404 case CPP_CHAR32: 1405 case CPP_UTF8CHAR: 1406 case CPP_NAME: 1407 case CPP_HASH: 1408 if (!want_value) 1409 SYNTAX_ERROR2_AT (op.loc, 1410 "missing binary operator before token \"%s\"", 1411 cpp_token_as_text (pfile, op.token)); 1412 want_value = false; 1413 top->value = eval_token (pfile, op.token, op.loc); 1414 continue; 1415 1416 case CPP_NOT: 1417 saw_leading_not = lex_count == 1; 1418 break; 1419 case CPP_PLUS: 1420 if (want_value) 1421 op.op = CPP_UPLUS; 1422 break; 1423 case CPP_MINUS: 1424 if (want_value) 1425 op.op = CPP_UMINUS; 1426 break; 1427 1428 case CPP_PADDING: 1429 lex_count--; 1430 continue; 1431 1432 default: 1433 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) 1434 SYNTAX_ERROR2_AT (op.loc, 1435 "token \"%s\" is not valid in preprocessor expressions", 1436 cpp_token_as_text (pfile, op.token)); 1437 break; 1438 } 1439 1440 /* Check we have a value or operator as appropriate. */ 1441 if (optab[op.op].flags & NO_L_OPERAND) 1442 { 1443 if (!want_value) 1444 SYNTAX_ERROR2_AT (op.loc, 1445 "missing binary operator before token \"%s\"", 1446 cpp_token_as_text (pfile, op.token)); 1447 } 1448 else if (want_value) 1449 { 1450 /* We want a number (or expression) and haven't got one. 1451 Try to emit a specific diagnostic. */ 1452 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) 1453 SYNTAX_ERROR_AT (op.loc, 1454 "missing expression between '(' and ')'"); 1455 1456 if (op.op == CPP_EOF && top->op == CPP_EOF) 1457 SYNTAX_ERROR2_AT (op.loc, 1458 "%s with no expression", is_if ? "#if" : "#elif"); 1459 1460 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) 1461 SYNTAX_ERROR2_AT (op.loc, 1462 "operator '%s' has no right operand", 1463 cpp_token_as_text (pfile, top->token)); 1464 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) 1465 /* Complain about missing paren during reduction. */; 1466 else 1467 SYNTAX_ERROR2_AT (op.loc, 1468 "operator '%s' has no left operand", 1469 cpp_token_as_text (pfile, op.token)); 1470 } 1471 1472 top = reduce (pfile, top, op.op); 1473 if (!top) 1474 goto syntax_error; 1475 1476 if (op.op == CPP_EOF) 1477 break; 1478 1479 switch (op.op) 1480 { 1481 case CPP_CLOSE_PAREN: 1482 continue; 1483 case CPP_OR_OR: 1484 if (!num_zerop (top->value)) 1485 pfile->state.skip_eval++; 1486 break; 1487 case CPP_AND_AND: 1488 case CPP_QUERY: 1489 if (num_zerop (top->value)) 1490 pfile->state.skip_eval++; 1491 break; 1492 case CPP_COLON: 1493 if (top->op != CPP_QUERY) 1494 SYNTAX_ERROR_AT (op.loc, 1495 " ':' without preceding '?'"); 1496 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ 1497 pfile->state.skip_eval++; 1498 else 1499 pfile->state.skip_eval--; 1500 default: 1501 break; 1502 } 1503 1504 want_value = true; 1505 1506 /* Check for and handle stack overflow. */ 1507 if (++top == pfile->op_limit) 1508 top = _cpp_expand_op_stack (pfile); 1509 1510 top->op = op.op; 1511 top->token = op.token; 1512 top->loc = op.loc; 1513 } 1514 1515 /* The controlling macro expression is only valid if we called lex 3 1516 times: <!> <defined expression> and <EOF>. push_conditional () 1517 checks that we are at top-of-file. */ 1518 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) 1519 pfile->mi_ind_cmacro = 0; 1520 1521 if (top != pfile->op_stack) 1522 { 1523 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0, 1524 "unbalanced stack in %s", 1525 is_if ? "#if" : "#elif"); 1526 syntax_error: 1527 return false; /* Return false on syntax error. */ 1528 } 1529 1530 return !num_zerop (top->value); 1531 } 1532 1533 /* Reduce the operator / value stack if possible, in preparation for 1534 pushing operator OP. Returns NULL on error, otherwise the top of 1535 the stack. */ 1536 static struct op * 1537 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) 1538 { 1539 unsigned int prio; 1540 1541 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) 1542 { 1543 bad_op: 1544 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); 1545 return 0; 1546 } 1547 1548 if (op == CPP_OPEN_PAREN) 1549 return top; 1550 1551 /* Decrement the priority of left-associative operators to force a 1552 reduction with operators of otherwise equal priority. */ 1553 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0); 1554 while (prio < optab[top->op].prio) 1555 { 1556 if (CPP_OPTION (pfile, warn_num_sign_change) 1557 && optab[top->op].flags & CHECK_PROMOTION) 1558 check_promotion (pfile, top); 1559 1560 switch (top->op) 1561 { 1562 case CPP_UPLUS: 1563 case CPP_UMINUS: 1564 case CPP_NOT: 1565 case CPP_COMPL: 1566 top[-1].value = num_unary_op (pfile, top->value, top->op); 1567 top[-1].loc = top->loc; 1568 break; 1569 1570 case CPP_PLUS: 1571 case CPP_MINUS: 1572 case CPP_RSHIFT: 1573 case CPP_LSHIFT: 1574 case CPP_COMMA: 1575 top[-1].value = num_binary_op (pfile, top[-1].value, 1576 top->value, top->op); 1577 top[-1].loc = top->loc; 1578 break; 1579 1580 case CPP_GREATER: 1581 case CPP_LESS: 1582 case CPP_GREATER_EQ: 1583 case CPP_LESS_EQ: 1584 top[-1].value 1585 = num_inequality_op (pfile, top[-1].value, top->value, top->op); 1586 top[-1].loc = top->loc; 1587 break; 1588 1589 case CPP_EQ_EQ: 1590 case CPP_NOT_EQ: 1591 top[-1].value 1592 = num_equality_op (pfile, top[-1].value, top->value, top->op); 1593 top[-1].loc = top->loc; 1594 break; 1595 1596 case CPP_AND: 1597 case CPP_OR: 1598 case CPP_XOR: 1599 top[-1].value 1600 = num_bitwise_op (pfile, top[-1].value, top->value, top->op); 1601 top[-1].loc = top->loc; 1602 break; 1603 1604 case CPP_MULT: 1605 top[-1].value = num_mul (pfile, top[-1].value, top->value); 1606 top[-1].loc = top->loc; 1607 break; 1608 1609 case CPP_DIV: 1610 case CPP_MOD: 1611 top[-1].value = num_div_op (pfile, top[-1].value, 1612 top->value, top->op, top->loc); 1613 top[-1].loc = top->loc; 1614 break; 1615 1616 case CPP_OR_OR: 1617 top--; 1618 if (!num_zerop (top->value)) 1619 pfile->state.skip_eval--; 1620 top->value.low = (!num_zerop (top->value) 1621 || !num_zerop (top[1].value)); 1622 top->value.high = 0; 1623 top->value.unsignedp = false; 1624 top->value.overflow = false; 1625 top->loc = top[1].loc; 1626 continue; 1627 1628 case CPP_AND_AND: 1629 top--; 1630 if (num_zerop (top->value)) 1631 pfile->state.skip_eval--; 1632 top->value.low = (!num_zerop (top->value) 1633 && !num_zerop (top[1].value)); 1634 top->value.high = 0; 1635 top->value.unsignedp = false; 1636 top->value.overflow = false; 1637 top->loc = top[1].loc; 1638 continue; 1639 1640 case CPP_OPEN_PAREN: 1641 if (op != CPP_CLOSE_PAREN) 1642 { 1643 cpp_error_with_line (pfile, CPP_DL_ERROR, 1644 top->token->src_loc, 1645 0, "missing ')' in expression"); 1646 return 0; 1647 } 1648 top--; 1649 top->value = top[1].value; 1650 top->loc = top[1].loc; 1651 return top; 1652 1653 case CPP_COLON: 1654 top -= 2; 1655 if (!num_zerop (top->value)) 1656 { 1657 pfile->state.skip_eval--; 1658 top->value = top[1].value; 1659 top->loc = top[1].loc; 1660 } 1661 else 1662 { 1663 top->value = top[2].value; 1664 top->loc = top[2].loc; 1665 } 1666 top->value.unsignedp = (top[1].value.unsignedp 1667 || top[2].value.unsignedp); 1668 continue; 1669 1670 case CPP_QUERY: 1671 /* COMMA and COLON should not reduce a QUERY operator. */ 1672 if (op == CPP_COMMA || op == CPP_COLON) 1673 return top; 1674 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); 1675 return 0; 1676 1677 default: 1678 goto bad_op; 1679 } 1680 1681 top--; 1682 if (top->value.overflow && !pfile->state.skip_eval) 1683 cpp_error (pfile, CPP_DL_PEDWARN, 1684 "integer overflow in preprocessor expression"); 1685 } 1686 1687 if (op == CPP_CLOSE_PAREN) 1688 { 1689 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); 1690 return 0; 1691 } 1692 1693 return top; 1694 } 1695 1696 /* Returns the position of the old top of stack after expansion. */ 1697 struct op * 1698 _cpp_expand_op_stack (cpp_reader *pfile) 1699 { 1700 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); 1701 size_t new_size = old_size * 2 + 20; 1702 1703 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); 1704 pfile->op_limit = pfile->op_stack + new_size; 1705 1706 return pfile->op_stack + old_size; 1707 } 1708 1709 /* Emits a warning if the effective sign of either operand of OP 1710 changes because of integer promotions. */ 1711 static void 1712 check_promotion (cpp_reader *pfile, const struct op *op) 1713 { 1714 if (op->value.unsignedp == op[-1].value.unsignedp) 1715 return; 1716 1717 if (op->value.unsignedp) 1718 { 1719 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision))) 1720 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0, 1721 "the left operand of \"%s\" changes sign when promoted", 1722 cpp_token_as_text (pfile, op->token)); 1723 } 1724 else if (!num_positive (op->value, CPP_OPTION (pfile, precision))) 1725 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0, 1726 "the right operand of \"%s\" changes sign when promoted", 1727 cpp_token_as_text (pfile, op->token)); 1728 } 1729 1730 /* Clears the unused high order bits of the number pointed to by PNUM. */ 1731 static cpp_num 1732 num_trim (cpp_num num, size_t precision) 1733 { 1734 if (precision > PART_PRECISION) 1735 { 1736 precision -= PART_PRECISION; 1737 if (precision < PART_PRECISION) 1738 num.high &= ((cpp_num_part) 1 << precision) - 1; 1739 } 1740 else 1741 { 1742 if (precision < PART_PRECISION) 1743 num.low &= ((cpp_num_part) 1 << precision) - 1; 1744 num.high = 0; 1745 } 1746 1747 return num; 1748 } 1749 1750 /* True iff A (presumed signed) >= 0. */ 1751 static bool 1752 num_positive (cpp_num num, size_t precision) 1753 { 1754 if (precision > PART_PRECISION) 1755 { 1756 precision -= PART_PRECISION; 1757 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; 1758 } 1759 1760 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; 1761 } 1762 1763 /* Sign extend a number, with PRECISION significant bits and all 1764 others assumed clear, to fill out a cpp_num structure. */ 1765 cpp_num 1766 cpp_num_sign_extend (cpp_num num, size_t precision) 1767 { 1768 if (!num.unsignedp) 1769 { 1770 if (precision > PART_PRECISION) 1771 { 1772 precision -= PART_PRECISION; 1773 if (precision < PART_PRECISION 1774 && (num.high & (cpp_num_part) 1 << (precision - 1))) 1775 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); 1776 } 1777 else if (num.low & (cpp_num_part) 1 << (precision - 1)) 1778 { 1779 if (precision < PART_PRECISION) 1780 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); 1781 num.high = ~(cpp_num_part) 0; 1782 } 1783 } 1784 1785 return num; 1786 } 1787 1788 /* Returns the negative of NUM. */ 1789 static cpp_num 1790 num_negate (cpp_num num, size_t precision) 1791 { 1792 cpp_num copy; 1793 1794 copy = num; 1795 num.high = ~num.high; 1796 num.low = ~num.low; 1797 if (++num.low == 0) 1798 num.high++; 1799 num = num_trim (num, precision); 1800 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); 1801 1802 return num; 1803 } 1804 1805 /* Returns true if A >= B. */ 1806 static bool 1807 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) 1808 { 1809 bool unsignedp; 1810 1811 unsignedp = pa.unsignedp || pb.unsignedp; 1812 1813 if (!unsignedp) 1814 { 1815 /* Both numbers have signed type. If they are of different 1816 sign, the answer is the sign of A. */ 1817 unsignedp = num_positive (pa, precision); 1818 1819 if (unsignedp != num_positive (pb, precision)) 1820 return unsignedp; 1821 1822 /* Otherwise we can do an unsigned comparison. */ 1823 } 1824 1825 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); 1826 } 1827 1828 /* Returns LHS OP RHS, where OP is a bit-wise operation. */ 1829 static cpp_num 1830 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, 1831 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 1832 { 1833 lhs.overflow = false; 1834 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; 1835 1836 /* As excess precision is zeroed, there is no need to num_trim () as 1837 these operations cannot introduce a set bit there. */ 1838 if (op == CPP_AND) 1839 { 1840 lhs.low &= rhs.low; 1841 lhs.high &= rhs.high; 1842 } 1843 else if (op == CPP_OR) 1844 { 1845 lhs.low |= rhs.low; 1846 lhs.high |= rhs.high; 1847 } 1848 else 1849 { 1850 lhs.low ^= rhs.low; 1851 lhs.high ^= rhs.high; 1852 } 1853 1854 return lhs; 1855 } 1856 1857 /* Returns LHS OP RHS, where OP is an inequality. */ 1858 static cpp_num 1859 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, 1860 enum cpp_ttype op) 1861 { 1862 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); 1863 1864 if (op == CPP_GREATER_EQ) 1865 lhs.low = gte; 1866 else if (op == CPP_LESS) 1867 lhs.low = !gte; 1868 else if (op == CPP_GREATER) 1869 lhs.low = gte && !num_eq (lhs, rhs); 1870 else /* CPP_LESS_EQ. */ 1871 lhs.low = !gte || num_eq (lhs, rhs); 1872 1873 lhs.high = 0; 1874 lhs.overflow = false; 1875 lhs.unsignedp = false; 1876 return lhs; 1877 } 1878 1879 /* Returns LHS OP RHS, where OP is == or !=. */ 1880 static cpp_num 1881 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, 1882 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 1883 { 1884 /* Work around a 3.0.4 bug; see PR 6950. */ 1885 bool eq = num_eq (lhs, rhs); 1886 if (op == CPP_NOT_EQ) 1887 eq = !eq; 1888 lhs.low = eq; 1889 lhs.high = 0; 1890 lhs.overflow = false; 1891 lhs.unsignedp = false; 1892 return lhs; 1893 } 1894 1895 /* Shift NUM, of width PRECISION, right by N bits. */ 1896 static cpp_num 1897 num_rshift (cpp_num num, size_t precision, size_t n) 1898 { 1899 cpp_num_part sign_mask; 1900 bool x = num_positive (num, precision); 1901 1902 if (num.unsignedp || x) 1903 sign_mask = 0; 1904 else 1905 sign_mask = ~(cpp_num_part) 0; 1906 1907 if (n >= precision) 1908 num.high = num.low = sign_mask; 1909 else 1910 { 1911 /* Sign-extend. */ 1912 if (precision < PART_PRECISION) 1913 num.high = sign_mask, num.low |= sign_mask << precision; 1914 else if (precision < 2 * PART_PRECISION) 1915 num.high |= sign_mask << (precision - PART_PRECISION); 1916 1917 if (n >= PART_PRECISION) 1918 { 1919 n -= PART_PRECISION; 1920 num.low = num.high; 1921 num.high = sign_mask; 1922 } 1923 1924 if (n) 1925 { 1926 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); 1927 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); 1928 } 1929 } 1930 1931 num = num_trim (num, precision); 1932 num.overflow = false; 1933 return num; 1934 } 1935 1936 /* Shift NUM, of width PRECISION, left by N bits. */ 1937 static cpp_num 1938 num_lshift (cpp_num num, size_t precision, size_t n) 1939 { 1940 if (n >= precision) 1941 { 1942 num.overflow = !num.unsignedp && !num_zerop (num); 1943 num.high = num.low = 0; 1944 } 1945 else 1946 { 1947 cpp_num orig, maybe_orig; 1948 size_t m = n; 1949 1950 orig = num; 1951 if (m >= PART_PRECISION) 1952 { 1953 m -= PART_PRECISION; 1954 num.high = num.low; 1955 num.low = 0; 1956 } 1957 if (m) 1958 { 1959 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); 1960 num.low <<= m; 1961 } 1962 num = num_trim (num, precision); 1963 1964 if (num.unsignedp) 1965 num.overflow = false; 1966 else 1967 { 1968 maybe_orig = num_rshift (num, precision, n); 1969 num.overflow = !num_eq (orig, maybe_orig); 1970 } 1971 } 1972 1973 return num; 1974 } 1975 1976 /* The four unary operators: +, -, ! and ~. */ 1977 static cpp_num 1978 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) 1979 { 1980 switch (op) 1981 { 1982 case CPP_UPLUS: 1983 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval) 1984 cpp_warning (pfile, CPP_W_TRADITIONAL, 1985 "traditional C rejects the unary plus operator"); 1986 num.overflow = false; 1987 break; 1988 1989 case CPP_UMINUS: 1990 num = num_negate (num, CPP_OPTION (pfile, precision)); 1991 break; 1992 1993 case CPP_COMPL: 1994 num.high = ~num.high; 1995 num.low = ~num.low; 1996 num = num_trim (num, CPP_OPTION (pfile, precision)); 1997 num.overflow = false; 1998 break; 1999 2000 default: /* case CPP_NOT: */ 2001 num.low = num_zerop (num); 2002 num.high = 0; 2003 num.overflow = false; 2004 num.unsignedp = false; 2005 break; 2006 } 2007 2008 return num; 2009 } 2010 2011 /* The various binary operators. */ 2012 static cpp_num 2013 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) 2014 { 2015 cpp_num result; 2016 size_t precision = CPP_OPTION (pfile, precision); 2017 size_t n; 2018 2019 switch (op) 2020 { 2021 /* Shifts. */ 2022 case CPP_LSHIFT: 2023 case CPP_RSHIFT: 2024 if (!rhs.unsignedp && !num_positive (rhs, precision)) 2025 { 2026 /* A negative shift is a positive shift the other way. */ 2027 if (op == CPP_LSHIFT) 2028 op = CPP_RSHIFT; 2029 else 2030 op = CPP_LSHIFT; 2031 rhs = num_negate (rhs, precision); 2032 } 2033 if (rhs.high) 2034 n = ~0; /* Maximal. */ 2035 else 2036 n = rhs.low; 2037 if (op == CPP_LSHIFT) 2038 lhs = num_lshift (lhs, precision, n); 2039 else 2040 lhs = num_rshift (lhs, precision, n); 2041 break; 2042 2043 /* Arithmetic. */ 2044 case CPP_MINUS: 2045 result.low = lhs.low - rhs.low; 2046 result.high = lhs.high - rhs.high; 2047 if (result.low > lhs.low) 2048 result.high--; 2049 result.unsignedp = lhs.unsignedp || rhs.unsignedp; 2050 result.overflow = false; 2051 2052 result = num_trim (result, precision); 2053 if (!result.unsignedp) 2054 { 2055 bool lhsp = num_positive (lhs, precision); 2056 result.overflow = (lhsp != num_positive (rhs, precision) 2057 && lhsp != num_positive (result, precision)); 2058 } 2059 return result; 2060 2061 case CPP_PLUS: 2062 result.low = lhs.low + rhs.low; 2063 result.high = lhs.high + rhs.high; 2064 if (result.low < lhs.low) 2065 result.high++; 2066 result.unsignedp = lhs.unsignedp || rhs.unsignedp; 2067 result.overflow = false; 2068 2069 result = num_trim (result, precision); 2070 if (!result.unsignedp) 2071 { 2072 bool lhsp = num_positive (lhs, precision); 2073 result.overflow = (lhsp == num_positive (rhs, precision) 2074 && lhsp != num_positive (result, precision)); 2075 } 2076 return result; 2077 2078 /* Comma. */ 2079 default: /* case CPP_COMMA: */ 2080 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) 2081 || !pfile->state.skip_eval)) 2082 cpp_pedwarning (pfile, CPP_W_PEDANTIC, 2083 "comma operator in operand of #if"); 2084 lhs = rhs; 2085 break; 2086 } 2087 2088 return lhs; 2089 } 2090 2091 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This 2092 cannot overflow. */ 2093 static cpp_num 2094 num_part_mul (cpp_num_part lhs, cpp_num_part rhs) 2095 { 2096 cpp_num result; 2097 cpp_num_part middle[2], temp; 2098 2099 result.low = LOW_PART (lhs) * LOW_PART (rhs); 2100 result.high = HIGH_PART (lhs) * HIGH_PART (rhs); 2101 2102 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); 2103 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); 2104 2105 temp = result.low; 2106 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); 2107 if (result.low < temp) 2108 result.high++; 2109 2110 temp = result.low; 2111 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); 2112 if (result.low < temp) 2113 result.high++; 2114 2115 result.high += HIGH_PART (middle[0]); 2116 result.high += HIGH_PART (middle[1]); 2117 result.unsignedp = true; 2118 result.overflow = false; 2119 2120 return result; 2121 } 2122 2123 /* Multiply two preprocessing numbers. */ 2124 static cpp_num 2125 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) 2126 { 2127 cpp_num result, temp; 2128 bool unsignedp = lhs.unsignedp || rhs.unsignedp; 2129 bool overflow, negate = false; 2130 size_t precision = CPP_OPTION (pfile, precision); 2131 2132 /* Prepare for unsigned multiplication. */ 2133 if (!unsignedp) 2134 { 2135 if (!num_positive (lhs, precision)) 2136 negate = !negate, lhs = num_negate (lhs, precision); 2137 if (!num_positive (rhs, precision)) 2138 negate = !negate, rhs = num_negate (rhs, precision); 2139 } 2140 2141 overflow = lhs.high && rhs.high; 2142 result = num_part_mul (lhs.low, rhs.low); 2143 2144 temp = num_part_mul (lhs.high, rhs.low); 2145 result.high += temp.low; 2146 if (temp.high) 2147 overflow = true; 2148 2149 temp = num_part_mul (lhs.low, rhs.high); 2150 result.high += temp.low; 2151 if (temp.high) 2152 overflow = true; 2153 2154 temp.low = result.low, temp.high = result.high; 2155 result = num_trim (result, precision); 2156 if (!num_eq (result, temp)) 2157 overflow = true; 2158 2159 if (negate) 2160 result = num_negate (result, precision); 2161 2162 if (unsignedp) 2163 result.overflow = false; 2164 else 2165 result.overflow = overflow || (num_positive (result, precision) ^ !negate 2166 && !num_zerop (result)); 2167 result.unsignedp = unsignedp; 2168 2169 return result; 2170 } 2171 2172 /* Divide two preprocessing numbers, LHS and RHS, returning the answer 2173 or the remainder depending upon OP. LOCATION is the source location 2174 of this operator (for diagnostics). */ 2175 2176 static cpp_num 2177 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, 2178 location_t location) 2179 { 2180 cpp_num result, sub; 2181 cpp_num_part mask; 2182 bool unsignedp = lhs.unsignedp || rhs.unsignedp; 2183 bool negate = false, lhs_neg = false; 2184 size_t i, precision = CPP_OPTION (pfile, precision); 2185 2186 /* Prepare for unsigned division. */ 2187 if (!unsignedp) 2188 { 2189 if (!num_positive (lhs, precision)) 2190 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision); 2191 if (!num_positive (rhs, precision)) 2192 negate = !negate, rhs = num_negate (rhs, precision); 2193 } 2194 2195 /* Find the high bit. */ 2196 if (rhs.high) 2197 { 2198 i = precision - 1; 2199 mask = (cpp_num_part) 1 << (i - PART_PRECISION); 2200 for (; ; i--, mask >>= 1) 2201 if (rhs.high & mask) 2202 break; 2203 } 2204 else if (rhs.low) 2205 { 2206 if (precision > PART_PRECISION) 2207 i = precision - PART_PRECISION - 1; 2208 else 2209 i = precision - 1; 2210 mask = (cpp_num_part) 1 << i; 2211 for (; ; i--, mask >>= 1) 2212 if (rhs.low & mask) 2213 break; 2214 } 2215 else 2216 { 2217 if (!pfile->state.skip_eval) 2218 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, 2219 "division by zero in #if"); 2220 lhs.unsignedp = unsignedp; 2221 return lhs; 2222 } 2223 2224 /* First nonzero bit of RHS is bit I. Do naive division by 2225 shifting the RHS fully left, and subtracting from LHS if LHS is 2226 at least as big, and then repeating but with one less shift. 2227 This is not very efficient, but is easy to understand. */ 2228 2229 rhs.unsignedp = true; 2230 lhs.unsignedp = true; 2231 i = precision - i - 1; 2232 sub = num_lshift (rhs, precision, i); 2233 2234 result.high = result.low = 0; 2235 for (;;) 2236 { 2237 if (num_greater_eq (lhs, sub, precision)) 2238 { 2239 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS); 2240 if (i >= PART_PRECISION) 2241 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION); 2242 else 2243 result.low |= (cpp_num_part) 1 << i; 2244 } 2245 if (i-- == 0) 2246 break; 2247 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); 2248 sub.high >>= 1; 2249 } 2250 2251 /* We divide so that the remainder has the sign of the LHS. */ 2252 if (op == CPP_DIV) 2253 { 2254 result.unsignedp = unsignedp; 2255 result.overflow = false; 2256 if (!unsignedp) 2257 { 2258 if (negate) 2259 result = num_negate (result, precision); 2260 result.overflow = (num_positive (result, precision) ^ !negate 2261 && !num_zerop (result)); 2262 } 2263 2264 return result; 2265 } 2266 2267 /* CPP_MOD. */ 2268 lhs.unsignedp = unsignedp; 2269 lhs.overflow = false; 2270 if (lhs_neg) 2271 lhs = num_negate (lhs, precision); 2272 2273 return lhs; 2274 } 2275 2276