analyzer: fold -(-(VAL)) to VAL
[official-gcc.git] / libcpp / expr.cc
blob1d680643ad883f6b9ffb9dc288626a4a95a4e40e
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2022 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
8 later version.
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/>. */
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "internal.h"
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))
29 struct op
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;
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,
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);
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 *);
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))
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)
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)
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;
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C2X, are supported:
102 df, DF - _Decimal32.
103 dd, DD - _Decimal64.
104 dl, DL - _Decimal128.
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
112 (i) An optional u or U, for unsigned types.
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.
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
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:
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). */
136 /* Process decimal float suffixes, which are two letters starting
137 with d or D. Order and case are significant. */
138 if (len == 2 && (*s == 'd' || *s == 'D'))
140 bool uppercase = (*s == 'D');
141 switch (s[1])
143 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
144 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
145 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
146 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
147 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
148 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
149 default:
150 /* Additional two-character suffixes beginning with D are not
151 for decimal float constants. */
152 break;
156 if (CPP_OPTION (pfile, ext_numeric_literals))
158 /* Recognize a fixed-point suffix. */
159 if (len != 0)
160 switch (s[len-1])
162 case 'k': case 'K': flags = CPP_N_ACCUM; break;
163 case 'r': case 'R': flags = CPP_N_FRACT; break;
164 default: break;
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
169 if (flags)
171 if (len == 1)
172 return flags;
173 len--;
175 if (*s == 'u' || *s == 'U')
177 flags |= CPP_N_UNSIGNED;
178 if (len == 1)
179 return flags;
180 len--;
181 s++;
184 switch (*s)
186 case 'h': case 'H':
187 if (len == 1)
188 return flags |= CPP_N_SMALL;
189 break;
190 case 'l':
191 if (len == 1)
192 return flags |= CPP_N_MEDIUM;
193 if (len == 2 && s[1] == 'l')
194 return flags |= CPP_N_LARGE;
195 break;
196 case 'L':
197 if (len == 1)
198 return flags |= CPP_N_MEDIUM;
199 if (len == 2 && s[1] == 'L')
200 return flags |= CPP_N_LARGE;
201 break;
202 default:
203 break;
205 /* Anything left at this point is invalid. */
206 return 0;
210 /* In any remaining valid suffix, the case and order don't matter. */
211 while (len--)
213 switch (s[0])
215 case 'f': case 'F':
216 f++;
217 if (len > 0
218 && s[1] >= '1'
219 && s[1] <= '9'
220 && fn_bits == 0)
222 f--;
223 while (len > 0
224 && s[1] >= '0'
225 && s[1] <= '9'
226 && fn_bits < CPP_FLOATN_MAX)
228 fn_bits = fn_bits * 10 + (s[1] - '0');
229 len--;
230 s++;
232 if (len > 0 && s[1] == 'x')
234 fnx++;
235 len--;
236 s++;
238 else
239 fn++;
241 break;
242 case 'd': case 'D': d++; break;
243 case 'l': case 'L': l++; break;
244 case 'w': case 'W': w++; break;
245 case 'q': case 'Q': q++; break;
246 case 'i': case 'I':
247 case 'j': case 'J': i++; break;
248 default:
249 return 0;
251 s++;
254 /* Reject any case of multiple suffixes specifying types, multiple
255 suffixes specifying an imaginary constant, _FloatN or _FloatNx
256 suffixes for invalid values of N, and _FloatN suffixes for values
257 of N larger than can be represented in the return value. The
258 caller is responsible for rejecting _FloatN suffixes where
259 _FloatN is not supported on the chosen target. */
260 if (f + d + l + w + q + fn + fnx > 1 || i > 1)
261 return 0;
262 if (fn_bits > CPP_FLOATN_MAX)
263 return 0;
264 if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128)
265 return 0;
266 if (fn && fn_bits != 16 && fn_bits % 32 != 0)
267 return 0;
268 if (fn && fn_bits == 96)
269 return 0;
271 if (i)
273 if (!CPP_OPTION (pfile, ext_numeric_literals))
274 return 0;
276 /* In C++14 and up these suffixes are in the standard library, so treat
277 them as user-defined literals. */
278 if (CPP_OPTION (pfile, cplusplus)
279 && CPP_OPTION (pfile, lang) > CLK_CXX11
280 && orig_s[0] == 'i'
281 && (orig_len == 1
282 || (orig_len == 2
283 && (orig_s[1] == 'f' || orig_s[1] == 'l'))))
284 return 0;
287 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
288 return 0;
290 return ((i ? CPP_N_IMAGINARY : 0)
291 | (f ? CPP_N_SMALL :
292 d ? CPP_N_MEDIUM :
293 l ? CPP_N_LARGE :
294 w ? CPP_N_MD_W :
295 q ? CPP_N_MD_Q :
296 fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) :
297 fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) :
298 CPP_N_DEFAULT));
301 /* Return the classification flags for a float suffix. */
302 unsigned int
303 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
305 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
308 /* Subroutine of cpp_classify_number. S points to an integer suffix
309 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
310 flag vector describing the suffix. */
311 static unsigned int
312 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
314 size_t orig_len = len;
315 size_t u, l, i, z;
317 u = l = i = z = 0;
319 while (len--)
320 switch (s[len])
322 case 'z': case 'Z': z++; break;
323 case 'u': case 'U': u++; break;
324 case 'i': case 'I':
325 case 'j': case 'J': i++; break;
326 case 'l': case 'L': l++;
327 /* If there are two Ls, they must be adjacent and the same case. */
328 if (l == 2 && s[len] != s[len + 1])
329 return 0;
330 break;
331 default:
332 return 0;
335 if (l > 2 || u > 1 || i > 1 || z > 1)
336 return 0;
338 if (z)
340 if (l > 0 || i > 0)
341 return 0;
342 if (!CPP_OPTION (pfile, cplusplus))
343 return 0;
346 if (i)
348 if (!CPP_OPTION (pfile, ext_numeric_literals))
349 return 0;
351 /* In C++14 and up these suffixes are in the standard library, so treat
352 them as user-defined literals. */
353 if (CPP_OPTION (pfile, cplusplus)
354 && CPP_OPTION (pfile, lang) > CLK_CXX11
355 && s[0] == 'i'
356 && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
357 return 0;
360 return ((i ? CPP_N_IMAGINARY : 0)
361 | (u ? CPP_N_UNSIGNED : 0)
362 | ((l == 0) ? CPP_N_SMALL
363 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)
364 | (z ? CPP_N_SIZE_T : 0));
367 /* Return the classification flags for an int suffix. */
368 unsigned int
369 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
371 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
374 /* Return the string type corresponding to the the input user-defined string
375 literal type. If the input type is not a user-defined string literal
376 type return the input type. */
377 enum cpp_ttype
378 cpp_userdef_string_remove_type (enum cpp_ttype type)
380 if (type == CPP_STRING_USERDEF)
381 return CPP_STRING;
382 else if (type == CPP_WSTRING_USERDEF)
383 return CPP_WSTRING;
384 else if (type == CPP_STRING16_USERDEF)
385 return CPP_STRING16;
386 else if (type == CPP_STRING32_USERDEF)
387 return CPP_STRING32;
388 else if (type == CPP_UTF8STRING_USERDEF)
389 return CPP_UTF8STRING;
390 else
391 return type;
394 /* Return the user-defined string literal type corresponding to the input
395 string type. If the input type is not a string type return the input
396 type. */
397 enum cpp_ttype
398 cpp_userdef_string_add_type (enum cpp_ttype type)
400 if (type == CPP_STRING)
401 return CPP_STRING_USERDEF;
402 else if (type == CPP_WSTRING)
403 return CPP_WSTRING_USERDEF;
404 else if (type == CPP_STRING16)
405 return CPP_STRING16_USERDEF;
406 else if (type == CPP_STRING32)
407 return CPP_STRING32_USERDEF;
408 else if (type == CPP_UTF8STRING)
409 return CPP_UTF8STRING_USERDEF;
410 else
411 return type;
414 /* Return the char type corresponding to the the input user-defined char
415 literal type. If the input type is not a user-defined char literal
416 type return the input type. */
417 enum cpp_ttype
418 cpp_userdef_char_remove_type (enum cpp_ttype type)
420 if (type == CPP_CHAR_USERDEF)
421 return CPP_CHAR;
422 else if (type == CPP_WCHAR_USERDEF)
423 return CPP_WCHAR;
424 else if (type == CPP_CHAR16_USERDEF)
425 return CPP_CHAR16;
426 else if (type == CPP_CHAR32_USERDEF)
427 return CPP_CHAR32;
428 else if (type == CPP_UTF8CHAR_USERDEF)
429 return CPP_UTF8CHAR;
430 else
431 return type;
434 /* Return the user-defined char literal type corresponding to the input
435 char type. If the input type is not a char type return the input
436 type. */
437 enum cpp_ttype
438 cpp_userdef_char_add_type (enum cpp_ttype type)
440 if (type == CPP_CHAR)
441 return CPP_CHAR_USERDEF;
442 else if (type == CPP_WCHAR)
443 return CPP_WCHAR_USERDEF;
444 else if (type == CPP_CHAR16)
445 return CPP_CHAR16_USERDEF;
446 else if (type == CPP_CHAR32)
447 return CPP_CHAR32_USERDEF;
448 else if (type == CPP_UTF8CHAR)
449 return CPP_UTF8CHAR_USERDEF;
450 else
451 return type;
454 /* Return true if the token type is a user-defined string literal. */
455 bool
456 cpp_userdef_string_p (enum cpp_ttype type)
458 if (type == CPP_STRING_USERDEF
459 || type == CPP_WSTRING_USERDEF
460 || type == CPP_STRING16_USERDEF
461 || type == CPP_STRING32_USERDEF
462 || type == CPP_UTF8STRING_USERDEF)
463 return true;
464 else
465 return false;
468 /* Return true if the token type is a user-defined char literal. */
469 bool
470 cpp_userdef_char_p (enum cpp_ttype type)
472 if (type == CPP_CHAR_USERDEF
473 || type == CPP_WCHAR_USERDEF
474 || type == CPP_CHAR16_USERDEF
475 || type == CPP_CHAR32_USERDEF
476 || type == CPP_UTF8CHAR_USERDEF)
477 return true;
478 else
479 return false;
482 /* Extract the suffix from a user-defined literal string or char. */
483 const char *
484 cpp_get_userdef_suffix (const cpp_token *tok)
486 unsigned int len = tok->val.str.len;
487 const char *text = (const char *)tok->val.str.text;
488 char delim;
489 unsigned int i;
490 for (i = 0; i < len; ++i)
491 if (text[i] == '\'' || text[i] == '"')
492 break;
493 if (i == len)
494 return text + len;
495 delim = text[i];
496 for (i = len; i > 0; --i)
497 if (text[i - 1] == delim)
498 break;
499 return text + i;
502 /* Categorize numeric constants according to their field (integer,
503 floating point, or invalid), radix (decimal, octal, hexadecimal),
504 and type suffixes.
506 TOKEN is the token that represents the numeric constant to
507 classify.
509 In C++0X if UD_SUFFIX is non null it will be assigned
510 any unrecognized suffix for a user-defined literal.
512 VIRTUAL_LOCATION is the virtual location for TOKEN. */
513 unsigned int
514 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
515 const char **ud_suffix, location_t virtual_location)
517 const uchar *str = token->val.str.text;
518 const uchar *limit;
519 unsigned int max_digit, result, radix;
520 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
521 bool seen_digit;
522 bool seen_digit_sep;
524 if (ud_suffix)
525 *ud_suffix = NULL;
527 /* If the lexer has done its job, length one can only be a single
528 digit. Fast-path this very common case. */
529 if (token->val.str.len == 1)
530 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
532 limit = str + token->val.str.len;
533 float_flag = NOT_FLOAT;
534 max_digit = 0;
535 radix = 10;
536 seen_digit = false;
537 seen_digit_sep = false;
539 /* First, interpret the radix. */
540 if (*str == '0')
542 radix = 8;
543 str++;
545 /* Require at least one hex digit to classify it as hex. */
546 if (*str == 'x' || *str == 'X')
548 if (str[1] == '.' || ISXDIGIT (str[1]))
550 radix = 16;
551 str++;
553 else if (DIGIT_SEP (str[1]))
554 SYNTAX_ERROR_AT (virtual_location,
555 "digit separator after base indicator");
557 else if (*str == 'b' || *str == 'B')
559 if (str[1] == '0' || str[1] == '1')
561 radix = 2;
562 str++;
564 else if (DIGIT_SEP (str[1]))
565 SYNTAX_ERROR_AT (virtual_location,
566 "digit separator after base indicator");
570 /* Now scan for a well-formed integer or float. */
571 for (;;)
573 unsigned int c = *str++;
575 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
577 seen_digit_sep = false;
578 seen_digit = true;
579 c = hex_value (c);
580 if (c > max_digit)
581 max_digit = c;
583 else if (DIGIT_SEP (c))
584 seen_digit_sep = true;
585 else if (c == '.')
587 if (seen_digit_sep || DIGIT_SEP (*str))
588 SYNTAX_ERROR_AT (virtual_location,
589 "digit separator adjacent to decimal point");
590 seen_digit_sep = false;
591 if (float_flag == NOT_FLOAT)
592 float_flag = AFTER_POINT;
593 else
594 SYNTAX_ERROR_AT (virtual_location,
595 "too many decimal points in number");
597 else if ((radix <= 10 && (c == 'e' || c == 'E'))
598 || (radix == 16 && (c == 'p' || c == 'P')))
600 if (seen_digit_sep || DIGIT_SEP (*str))
601 SYNTAX_ERROR_AT (virtual_location,
602 "digit separator adjacent to exponent");
603 float_flag = AFTER_EXPON;
604 break;
606 else
608 /* Start of suffix. */
609 str--;
610 break;
614 if (seen_digit_sep && float_flag != AFTER_EXPON)
615 SYNTAX_ERROR_AT (virtual_location,
616 "digit separator outside digit sequence");
618 /* The suffix may be for decimal fixed-point constants without exponent. */
619 if (radix != 16 && float_flag == NOT_FLOAT)
621 result = interpret_float_suffix (pfile, str, limit - str);
622 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
624 result |= CPP_N_FLOATING;
625 /* We need to restore the radix to 10, if the radix is 8. */
626 if (radix == 8)
627 radix = 10;
629 if (CPP_PEDANTIC (pfile))
630 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
631 "fixed-point constants are a GCC extension");
632 goto syntax_ok;
634 else
635 result = 0;
638 if (float_flag != NOT_FLOAT && radix == 8)
639 radix = 10;
641 if (max_digit >= radix)
643 if (radix == 2)
644 SYNTAX_ERROR2_AT (virtual_location,
645 "invalid digit \"%c\" in binary constant", '0' + max_digit);
646 else
647 SYNTAX_ERROR2_AT (virtual_location,
648 "invalid digit \"%c\" in octal constant", '0' + max_digit);
651 if (float_flag != NOT_FLOAT)
653 if (radix == 2)
655 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
656 "invalid prefix \"0b\" for floating constant");
657 return CPP_N_INVALID;
660 if (radix == 16 && !seen_digit)
661 SYNTAX_ERROR_AT (virtual_location,
662 "no digits in hexadecimal floating constant");
664 if (radix == 16 && CPP_PEDANTIC (pfile)
665 && !CPP_OPTION (pfile, extended_numbers))
667 if (CPP_OPTION (pfile, cplusplus))
668 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
669 "use of C++17 hexadecimal floating constant");
670 else
671 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
672 "use of C99 hexadecimal floating constant");
675 if (float_flag == AFTER_EXPON)
677 if (*str == '+' || *str == '-')
678 str++;
680 /* Exponent is decimal, even if string is a hex float. */
681 if (!ISDIGIT (*str))
683 if (DIGIT_SEP (*str))
684 SYNTAX_ERROR_AT (virtual_location,
685 "digit separator adjacent to exponent");
686 else
687 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
691 seen_digit_sep = DIGIT_SEP (*str);
692 str++;
694 while (ISDIGIT (*str) || DIGIT_SEP (*str));
696 else if (radix == 16)
697 SYNTAX_ERROR_AT (virtual_location,
698 "hexadecimal floating constants require an exponent");
700 if (seen_digit_sep)
701 SYNTAX_ERROR_AT (virtual_location,
702 "digit separator outside digit sequence");
704 result = interpret_float_suffix (pfile, str, limit - str);
705 if (result == 0)
707 if (CPP_OPTION (pfile, user_literals))
709 if (ud_suffix)
710 *ud_suffix = (const char *) str;
711 result = CPP_N_LARGE | CPP_N_USERDEF;
713 else
715 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
716 "invalid suffix \"%.*s\" on floating constant",
717 (int) (limit - str), str);
718 return CPP_N_INVALID;
722 /* Traditional C didn't accept any floating suffixes. */
723 if (limit != str
724 && CPP_WTRADITIONAL (pfile)
725 && ! cpp_sys_macro_p (pfile))
726 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
727 "traditional C rejects the \"%.*s\" suffix",
728 (int) (limit - str), str);
730 /* A suffix for double is a GCC extension via decimal float support.
731 If the suffix also specifies an imaginary value we'll catch that
732 later. */
733 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
734 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
735 "suffix for double constant is a GCC extension");
737 /* Radix must be 10 for decimal floats. */
738 if ((result & CPP_N_DFLOAT) && radix != 10)
740 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
741 "invalid suffix \"%.*s\" with hexadecimal floating constant",
742 (int) (limit - str), str);
743 return CPP_N_INVALID;
746 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
747 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
748 "fixed-point constants are a GCC extension");
750 if (result & CPP_N_DFLOAT)
752 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, dfp_constants))
753 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
754 "decimal float constants are a C2X feature");
755 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) > 0)
756 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
757 virtual_location, 0,
758 "decimal float constants are a C2X feature");
761 result |= CPP_N_FLOATING;
763 else
765 result = interpret_int_suffix (pfile, str, limit - str);
766 if (result == 0)
768 if (CPP_OPTION (pfile, user_literals))
770 if (ud_suffix)
771 *ud_suffix = (const char *) str;
772 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
774 else
776 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
777 "invalid suffix \"%.*s\" on integer constant",
778 (int) (limit - str), str);
779 return CPP_N_INVALID;
783 /* Traditional C only accepted the 'L' suffix.
784 Suppress warning about 'LL' with -Wno-long-long. */
785 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
787 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
788 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
789 && CPP_OPTION (pfile, cpp_warn_long_long);
791 if (u_or_i || large)
792 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
793 virtual_location, 0,
794 "traditional C rejects the \"%.*s\" suffix",
795 (int) (limit - str), str);
798 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
799 && CPP_OPTION (pfile, cpp_warn_long_long))
801 const char *message = CPP_OPTION (pfile, cplusplus)
802 ? N_("use of C++11 long long integer constant")
803 : N_("use of C99 long long integer constant");
805 if (CPP_OPTION (pfile, c99))
806 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
807 0, message);
808 else
809 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
810 virtual_location, 0, message);
813 if ((result & CPP_N_SIZE_T) == CPP_N_SIZE_T
814 && !CPP_OPTION (pfile, size_t_literals))
816 const char *message = (result & CPP_N_UNSIGNED) == CPP_N_UNSIGNED
817 ? N_("use of C++23 %<size_t%> integer constant")
818 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant");
819 cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS,
820 virtual_location, 0, message);
823 result |= CPP_N_INTEGER;
826 syntax_ok:
827 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
828 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
829 "imaginary constants are a GCC extension");
830 if (radix == 2)
832 if (!CPP_OPTION (pfile, binary_constants)
833 && CPP_PEDANTIC (pfile))
834 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
835 CPP_OPTION (pfile, cplusplus)
836 ? N_("binary constants are a C++14 feature "
837 "or GCC extension")
838 : N_("binary constants are a C2X feature "
839 "or GCC extension"));
840 else if (CPP_OPTION (pfile, cpp_warn_c11_c2x_compat) > 0)
841 cpp_warning_with_line (pfile, CPP_W_C11_C2X_COMPAT,
842 virtual_location, 0,
843 "binary constants are a C2X feature");
846 if (radix == 10)
847 result |= CPP_N_DECIMAL;
848 else if (radix == 16)
849 result |= CPP_N_HEX;
850 else if (radix == 2)
851 result |= CPP_N_BINARY;
852 else
853 result |= CPP_N_OCTAL;
855 return result;
857 syntax_error:
858 return CPP_N_INVALID;
861 /* cpp_interpret_integer converts an integer constant into a cpp_num,
862 of precision options->precision.
864 We do not provide any interface for decimal->float conversion,
865 because the preprocessor doesn't need it and we don't want to
866 drag in GCC's floating point emulator. */
867 cpp_num
868 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
869 unsigned int type)
871 const uchar *p, *end;
872 cpp_num result;
874 result.low = 0;
875 result.high = 0;
876 result.unsignedp = !!(type & CPP_N_UNSIGNED);
877 result.overflow = false;
879 p = token->val.str.text;
880 end = p + token->val.str.len;
882 /* Common case of a single digit. */
883 if (token->val.str.len == 1)
884 result.low = p[0] - '0';
885 else
887 cpp_num_part max;
888 size_t precision = CPP_OPTION (pfile, precision);
889 unsigned int base = 10, c = 0;
890 bool overflow = false;
892 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
894 base = 8;
895 p++;
897 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
899 base = 16;
900 p += 2;
902 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
904 base = 2;
905 p += 2;
908 /* We can add a digit to numbers strictly less than this without
909 needing the precision and slowness of double integers. */
910 max = ~(cpp_num_part) 0;
911 if (precision < PART_PRECISION)
912 max >>= PART_PRECISION - precision;
913 max = (max - base + 1) / base + 1;
915 for (; p < end; p++)
917 c = *p;
919 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
920 c = hex_value (c);
921 else if (DIGIT_SEP (c))
922 continue;
923 else
924 break;
926 /* Strict inequality for when max is set to zero. */
927 if (result.low < max)
928 result.low = result.low * base + c;
929 else
931 result = append_digit (result, c, base, precision);
932 overflow |= result.overflow;
933 max = 0;
937 if (overflow && !(type & CPP_N_USERDEF))
938 cpp_error (pfile, CPP_DL_PEDWARN,
939 "integer constant is too large for its type");
940 /* If too big to be signed, consider it unsigned. Only warn for
941 decimal numbers. Traditional numbers were always signed (but
942 we still honor an explicit U suffix); but we only have
943 traditional semantics in directives. */
944 else if (!result.unsignedp
945 && !(CPP_OPTION (pfile, traditional)
946 && pfile->state.in_directive)
947 && !num_positive (result, precision))
949 /* This is for constants within the range of uintmax_t but
950 not that of intmax_t. For such decimal constants, a
951 diagnostic is required for C99 as the selected type must
952 be signed and not having a type is a constraint violation
953 (DR#298, TC3), so this must be a pedwarn. For C90,
954 unsigned long is specified to be used for a constant that
955 does not fit in signed long; if uintmax_t has the same
956 range as unsigned long this means only a warning is
957 appropriate here. C90 permits the preprocessor to use a
958 wider range than unsigned long in the compiler, so if
959 uintmax_t is wider than unsigned long no diagnostic is
960 required for such constants in preprocessor #if
961 expressions and the compiler will pedwarn for such
962 constants outside the range of unsigned long that reach
963 the compiler so a diagnostic is not required there
964 either; thus, pedwarn for C99 but use a plain warning for
965 C90. */
966 if (base == 10)
967 cpp_error (pfile, (CPP_OPTION (pfile, c99)
968 ? CPP_DL_PEDWARN
969 : CPP_DL_WARNING),
970 "integer constant is so large that it is unsigned");
971 result.unsignedp = true;
975 return result;
978 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
979 static cpp_num
980 append_digit (cpp_num num, int digit, int base, size_t precision)
982 cpp_num result;
983 unsigned int shift;
984 bool overflow;
985 cpp_num_part add_high, add_low;
987 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
988 need to worry about add_high overflowing. */
989 switch (base)
991 case 2:
992 shift = 1;
993 break;
995 case 16:
996 shift = 4;
997 break;
999 default:
1000 shift = 3;
1002 overflow = !!(num.high >> (PART_PRECISION - shift));
1003 result.high = num.high << shift;
1004 result.low = num.low << shift;
1005 result.high |= num.low >> (PART_PRECISION - shift);
1006 result.unsignedp = num.unsignedp;
1008 if (base == 10)
1010 add_low = num.low << 1;
1011 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
1013 else
1014 add_high = add_low = 0;
1016 if (add_low + digit < add_low)
1017 add_high++;
1018 add_low += digit;
1020 if (result.low + add_low < result.low)
1021 add_high++;
1022 if (result.high + add_high < result.high)
1023 overflow = true;
1025 result.low += add_low;
1026 result.high += add_high;
1027 result.overflow = overflow;
1029 /* The above code catches overflow of a cpp_num type. This catches
1030 overflow of the (possibly shorter) target precision. */
1031 num.low = result.low;
1032 num.high = result.high;
1033 result = num_trim (result, precision);
1034 if (!num_eq (result, num))
1035 result.overflow = true;
1037 return result;
1040 /* Handle meeting "defined" in a preprocessor expression. */
1041 static cpp_num
1042 parse_defined (cpp_reader *pfile)
1044 cpp_num result;
1045 int paren = 0;
1046 cpp_hashnode *node = 0;
1047 const cpp_token *token;
1048 cpp_context *initial_context = pfile->context;
1050 /* Don't expand macros. */
1051 pfile->state.prevent_expansion++;
1053 token = cpp_get_token (pfile);
1054 if (token->type == CPP_OPEN_PAREN)
1056 paren = 1;
1057 token = cpp_get_token (pfile);
1060 if (token->type == CPP_NAME)
1062 node = token->val.node.node;
1063 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1065 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1066 node = 0;
1069 else
1071 cpp_error (pfile, CPP_DL_ERROR,
1072 "operator \"defined\" requires an identifier");
1073 if (token->flags & NAMED_OP)
1075 cpp_token op;
1077 op.flags = 0;
1078 op.type = token->type;
1079 cpp_error (pfile, CPP_DL_ERROR,
1080 "(\"%s\" is an alternative token for \"%s\" in C++)",
1081 cpp_token_as_text (pfile, token),
1082 cpp_token_as_text (pfile, &op));
1086 bool is_defined = false;
1087 if (node)
1089 if ((pfile->context != initial_context
1090 || initial_context != &pfile->base_context)
1091 && CPP_OPTION (pfile, warn_expansion_to_defined))
1092 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1093 "this use of \"defined\" may not be portable");
1094 is_defined = _cpp_defined_macro_p (node);
1095 if (!_cpp_maybe_notify_macro_use (pfile, node, token->src_loc))
1096 /* It wasn't a macro after all. */
1097 is_defined = false;
1098 _cpp_mark_macro_used (node);
1100 /* A possible controlling macro of the form #if !defined ().
1101 _cpp_parse_expr checks there was no other junk on the line. */
1102 pfile->mi_ind_cmacro = node;
1105 pfile->state.prevent_expansion--;
1107 /* Do not treat conditional macros as being defined. This is due to the
1108 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1109 to act as conditional keywords. This messes up tests like #ifndef
1110 bool. */
1111 result.unsignedp = false;
1112 result.high = 0;
1113 result.overflow = false;
1114 result.low = is_defined;
1115 return result;
1118 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1119 number or character constant, or the result of the "defined" or "#"
1120 operators). */
1121 static cpp_num
1122 eval_token (cpp_reader *pfile, const cpp_token *token,
1123 location_t virtual_location)
1125 cpp_num result;
1126 unsigned int temp;
1127 int unsignedp = 0;
1129 result.unsignedp = false;
1130 result.overflow = false;
1132 switch (token->type)
1134 case CPP_NUMBER:
1135 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1136 if (temp & CPP_N_USERDEF)
1137 cpp_error (pfile, CPP_DL_ERROR,
1138 "user-defined literal in preprocessor expression");
1139 switch (temp & CPP_N_CATEGORY)
1141 case CPP_N_FLOATING:
1142 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1143 "floating constant in preprocessor expression");
1144 break;
1145 case CPP_N_INTEGER:
1146 if (!(temp & CPP_N_IMAGINARY))
1147 return cpp_interpret_integer (pfile, token, temp);
1148 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1149 "imaginary number in preprocessor expression");
1150 break;
1152 case CPP_N_INVALID:
1153 /* Error already issued. */
1154 break;
1156 result.high = result.low = 0;
1157 break;
1159 case CPP_WCHAR:
1160 case CPP_CHAR:
1161 case CPP_CHAR16:
1162 case CPP_CHAR32:
1163 case CPP_UTF8CHAR:
1165 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1166 &temp, &unsignedp);
1168 result.high = 0;
1169 result.low = cc;
1170 /* Sign-extend the result if necessary. */
1171 if (!unsignedp && (cppchar_signed_t) cc < 0)
1173 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1174 result.low |= ~(~(cpp_num_part) 0
1175 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1176 result.high = ~(cpp_num_part) 0;
1177 result = num_trim (result, CPP_OPTION (pfile, precision));
1180 break;
1182 case CPP_NAME:
1183 if (token->val.node.node == pfile->spec_nodes.n_defined)
1184 return parse_defined (pfile);
1185 else if (CPP_OPTION (pfile, true_false)
1186 && (token->val.node.node == pfile->spec_nodes.n_true
1187 || token->val.node.node == pfile->spec_nodes.n_false))
1189 result.high = 0;
1190 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1192 else
1194 result.high = 0;
1195 result.low = 0;
1196 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1197 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1198 "\"%s\" is not defined, evaluates to 0",
1199 NODE_NAME (token->val.node.node));
1201 break;
1203 case CPP_HASH:
1204 if (!pfile->state.skipping)
1206 /* A pedantic warning takes precedence over a deprecated
1207 warning here. */
1208 if (CPP_PEDANTIC (pfile))
1209 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1210 virtual_location, 0,
1211 "assertions are a GCC extension");
1212 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1213 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1214 "assertions are a deprecated extension");
1216 _cpp_test_assertion (pfile, &temp);
1217 result.high = 0;
1218 result.low = temp;
1219 break;
1221 default:
1222 abort ();
1225 result.unsignedp = !!unsignedp;
1226 return result;
1229 /* Operator precedence and flags table.
1231 After an operator is returned from the lexer, if it has priority less
1232 than the operator on the top of the stack, we reduce the stack by one
1233 operator and repeat the test. Since equal priorities do not reduce,
1234 this is naturally right-associative.
1236 We handle left-associative operators by decrementing the priority of
1237 just-lexed operators by one, but retaining the priority of operators
1238 already on the stack.
1240 The remaining cases are '(' and ')'. We handle '(' by skipping the
1241 reduction phase completely. ')' is given lower priority than
1242 everything else, including '(', effectively forcing a reduction of the
1243 parenthesized expression. If there is a matching '(', the routine
1244 reduce() exits immediately. If the normal exit route sees a ')', then
1245 there cannot have been a matching '(' and an error message is output.
1247 The parser assumes all shifted operators require a left operand unless
1248 the flag NO_L_OPERAND is set. These semantics are automatic; any
1249 extra semantics need to be handled with operator-specific code. */
1251 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1252 operand changes because of integer promotions. */
1253 #define NO_L_OPERAND (1 << 0)
1254 #define LEFT_ASSOC (1 << 1)
1255 #define CHECK_PROMOTION (1 << 2)
1257 /* Operator to priority map. Must be in the same order as the first
1258 N entries of enum cpp_ttype. */
1259 static const struct cpp_operator
1261 uchar prio;
1262 uchar flags;
1263 } optab[] =
1265 /* EQ */ {0, 0}, /* Shouldn't happen. */
1266 /* NOT */ {16, NO_L_OPERAND},
1267 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1268 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1269 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1270 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1271 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1272 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1273 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1274 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1275 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1276 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1277 /* RSHIFT */ {13, LEFT_ASSOC},
1278 /* LSHIFT */ {13, LEFT_ASSOC},
1280 /* COMPL */ {16, NO_L_OPERAND},
1281 /* AND_AND */ {6, LEFT_ASSOC},
1282 /* OR_OR */ {5, LEFT_ASSOC},
1283 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1284 However, there are some special cases for these in reduce(). */
1285 /* QUERY */ {4, 0},
1286 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1287 /* COMMA */ {4, LEFT_ASSOC},
1288 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1289 /* CLOSE_PAREN */ {0, 0},
1290 /* EOF */ {0, 0},
1291 /* EQ_EQ */ {11, LEFT_ASSOC},
1292 /* NOT_EQ */ {11, LEFT_ASSOC},
1293 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1294 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1295 /* UPLUS */ {16, NO_L_OPERAND},
1296 /* UMINUS */ {16, NO_L_OPERAND}
1299 /* Parse and evaluate a C expression, reading from PFILE.
1300 Returns the truth value of the expression.
1302 The implementation is an operator precedence parser, i.e. a
1303 bottom-up parser, using a stack for not-yet-reduced tokens.
1305 The stack base is op_stack, and the current stack pointer is 'top'.
1306 There is a stack element for each operator (only), and the most
1307 recently pushed operator is 'top->op'. An operand (value) is
1308 stored in the 'value' field of the stack element of the operator
1309 that precedes it. */
1310 bool
1311 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1313 struct op *top = pfile->op_stack;
1314 unsigned int lex_count;
1315 bool saw_leading_not, want_value = true;
1316 location_t virtual_location = 0;
1318 pfile->state.skip_eval = 0;
1320 /* Set up detection of #if ! defined(). */
1321 pfile->mi_ind_cmacro = 0;
1322 saw_leading_not = false;
1323 lex_count = 0;
1325 /* Lowest priority operator prevents further reductions. */
1326 top->op = CPP_EOF;
1328 for (;;)
1330 struct op op;
1332 lex_count++;
1333 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1334 op.op = op.token->type;
1335 op.loc = virtual_location;
1337 switch (op.op)
1339 /* These tokens convert into values. */
1340 case CPP_NUMBER:
1341 case CPP_CHAR:
1342 case CPP_WCHAR:
1343 case CPP_CHAR16:
1344 case CPP_CHAR32:
1345 case CPP_UTF8CHAR:
1346 case CPP_NAME:
1347 case CPP_HASH:
1348 if (!want_value)
1349 SYNTAX_ERROR2_AT (op.loc,
1350 "missing binary operator before token \"%s\"",
1351 cpp_token_as_text (pfile, op.token));
1352 want_value = false;
1353 top->value = eval_token (pfile, op.token, op.loc);
1354 continue;
1356 case CPP_NOT:
1357 saw_leading_not = lex_count == 1;
1358 break;
1359 case CPP_PLUS:
1360 if (want_value)
1361 op.op = CPP_UPLUS;
1362 break;
1363 case CPP_MINUS:
1364 if (want_value)
1365 op.op = CPP_UMINUS;
1366 break;
1368 case CPP_PADDING:
1369 lex_count--;
1370 continue;
1372 default:
1373 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1374 SYNTAX_ERROR2_AT (op.loc,
1375 "token \"%s\" is not valid in preprocessor expressions",
1376 cpp_token_as_text (pfile, op.token));
1377 break;
1380 /* Check we have a value or operator as appropriate. */
1381 if (optab[op.op].flags & NO_L_OPERAND)
1383 if (!want_value)
1384 SYNTAX_ERROR2_AT (op.loc,
1385 "missing binary operator before token \"%s\"",
1386 cpp_token_as_text (pfile, op.token));
1388 else if (want_value)
1390 /* We want a number (or expression) and haven't got one.
1391 Try to emit a specific diagnostic. */
1392 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1393 SYNTAX_ERROR_AT (op.loc,
1394 "missing expression between '(' and ')'");
1396 if (op.op == CPP_EOF && top->op == CPP_EOF)
1397 SYNTAX_ERROR2_AT (op.loc,
1398 "%s with no expression", is_if ? "#if" : "#elif");
1400 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1401 SYNTAX_ERROR2_AT (op.loc,
1402 "operator '%s' has no right operand",
1403 cpp_token_as_text (pfile, top->token));
1404 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1405 /* Complain about missing paren during reduction. */;
1406 else
1407 SYNTAX_ERROR2_AT (op.loc,
1408 "operator '%s' has no left operand",
1409 cpp_token_as_text (pfile, op.token));
1412 top = reduce (pfile, top, op.op);
1413 if (!top)
1414 goto syntax_error;
1416 if (op.op == CPP_EOF)
1417 break;
1419 switch (op.op)
1421 case CPP_CLOSE_PAREN:
1422 continue;
1423 case CPP_OR_OR:
1424 if (!num_zerop (top->value))
1425 pfile->state.skip_eval++;
1426 break;
1427 case CPP_AND_AND:
1428 case CPP_QUERY:
1429 if (num_zerop (top->value))
1430 pfile->state.skip_eval++;
1431 break;
1432 case CPP_COLON:
1433 if (top->op != CPP_QUERY)
1434 SYNTAX_ERROR_AT (op.loc,
1435 " ':' without preceding '?'");
1436 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1437 pfile->state.skip_eval++;
1438 else
1439 pfile->state.skip_eval--;
1440 default:
1441 break;
1444 want_value = true;
1446 /* Check for and handle stack overflow. */
1447 if (++top == pfile->op_limit)
1448 top = _cpp_expand_op_stack (pfile);
1450 top->op = op.op;
1451 top->token = op.token;
1452 top->loc = op.loc;
1455 /* The controlling macro expression is only valid if we called lex 3
1456 times: <!> <defined expression> and <EOF>. push_conditional ()
1457 checks that we are at top-of-file. */
1458 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1459 pfile->mi_ind_cmacro = 0;
1461 if (top != pfile->op_stack)
1463 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1464 "unbalanced stack in %s",
1465 is_if ? "#if" : "#elif");
1466 syntax_error:
1467 return false; /* Return false on syntax error. */
1470 return !num_zerop (top->value);
1473 /* Reduce the operator / value stack if possible, in preparation for
1474 pushing operator OP. Returns NULL on error, otherwise the top of
1475 the stack. */
1476 static struct op *
1477 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1479 unsigned int prio;
1481 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1483 bad_op:
1484 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1485 return 0;
1488 if (op == CPP_OPEN_PAREN)
1489 return top;
1491 /* Decrement the priority of left-associative operators to force a
1492 reduction with operators of otherwise equal priority. */
1493 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1494 while (prio < optab[top->op].prio)
1496 if (CPP_OPTION (pfile, warn_num_sign_change)
1497 && optab[top->op].flags & CHECK_PROMOTION)
1498 check_promotion (pfile, top);
1500 switch (top->op)
1502 case CPP_UPLUS:
1503 case CPP_UMINUS:
1504 case CPP_NOT:
1505 case CPP_COMPL:
1506 top[-1].value = num_unary_op (pfile, top->value, top->op);
1507 top[-1].loc = top->loc;
1508 break;
1510 case CPP_PLUS:
1511 case CPP_MINUS:
1512 case CPP_RSHIFT:
1513 case CPP_LSHIFT:
1514 case CPP_COMMA:
1515 top[-1].value = num_binary_op (pfile, top[-1].value,
1516 top->value, top->op);
1517 top[-1].loc = top->loc;
1518 break;
1520 case CPP_GREATER:
1521 case CPP_LESS:
1522 case CPP_GREATER_EQ:
1523 case CPP_LESS_EQ:
1524 top[-1].value
1525 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1526 top[-1].loc = top->loc;
1527 break;
1529 case CPP_EQ_EQ:
1530 case CPP_NOT_EQ:
1531 top[-1].value
1532 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1533 top[-1].loc = top->loc;
1534 break;
1536 case CPP_AND:
1537 case CPP_OR:
1538 case CPP_XOR:
1539 top[-1].value
1540 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1541 top[-1].loc = top->loc;
1542 break;
1544 case CPP_MULT:
1545 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1546 top[-1].loc = top->loc;
1547 break;
1549 case CPP_DIV:
1550 case CPP_MOD:
1551 top[-1].value = num_div_op (pfile, top[-1].value,
1552 top->value, top->op, top->loc);
1553 top[-1].loc = top->loc;
1554 break;
1556 case CPP_OR_OR:
1557 top--;
1558 if (!num_zerop (top->value))
1559 pfile->state.skip_eval--;
1560 top->value.low = (!num_zerop (top->value)
1561 || !num_zerop (top[1].value));
1562 top->value.high = 0;
1563 top->value.unsignedp = false;
1564 top->value.overflow = false;
1565 top->loc = top[1].loc;
1566 continue;
1568 case CPP_AND_AND:
1569 top--;
1570 if (num_zerop (top->value))
1571 pfile->state.skip_eval--;
1572 top->value.low = (!num_zerop (top->value)
1573 && !num_zerop (top[1].value));
1574 top->value.high = 0;
1575 top->value.unsignedp = false;
1576 top->value.overflow = false;
1577 top->loc = top[1].loc;
1578 continue;
1580 case CPP_OPEN_PAREN:
1581 if (op != CPP_CLOSE_PAREN)
1583 cpp_error_with_line (pfile, CPP_DL_ERROR,
1584 top->token->src_loc,
1585 0, "missing ')' in expression");
1586 return 0;
1588 top--;
1589 top->value = top[1].value;
1590 top->loc = top[1].loc;
1591 return top;
1593 case CPP_COLON:
1594 top -= 2;
1595 if (!num_zerop (top->value))
1597 pfile->state.skip_eval--;
1598 top->value = top[1].value;
1599 top->loc = top[1].loc;
1601 else
1603 top->value = top[2].value;
1604 top->loc = top[2].loc;
1606 top->value.unsignedp = (top[1].value.unsignedp
1607 || top[2].value.unsignedp);
1608 continue;
1610 case CPP_QUERY:
1611 /* COMMA and COLON should not reduce a QUERY operator. */
1612 if (op == CPP_COMMA || op == CPP_COLON)
1613 return top;
1614 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1615 return 0;
1617 default:
1618 goto bad_op;
1621 top--;
1622 if (top->value.overflow && !pfile->state.skip_eval)
1623 cpp_error (pfile, CPP_DL_PEDWARN,
1624 "integer overflow in preprocessor expression");
1627 if (op == CPP_CLOSE_PAREN)
1629 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1630 return 0;
1633 return top;
1636 /* Returns the position of the old top of stack after expansion. */
1637 struct op *
1638 _cpp_expand_op_stack (cpp_reader *pfile)
1640 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1641 size_t new_size = old_size * 2 + 20;
1643 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1644 pfile->op_limit = pfile->op_stack + new_size;
1646 return pfile->op_stack + old_size;
1649 /* Emits a warning if the effective sign of either operand of OP
1650 changes because of integer promotions. */
1651 static void
1652 check_promotion (cpp_reader *pfile, const struct op *op)
1654 if (op->value.unsignedp == op[-1].value.unsignedp)
1655 return;
1657 if (op->value.unsignedp)
1659 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1660 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1661 "the left operand of \"%s\" changes sign when promoted",
1662 cpp_token_as_text (pfile, op->token));
1664 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1665 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1666 "the right operand of \"%s\" changes sign when promoted",
1667 cpp_token_as_text (pfile, op->token));
1670 /* Clears the unused high order bits of the number pointed to by PNUM. */
1671 static cpp_num
1672 num_trim (cpp_num num, size_t precision)
1674 if (precision > PART_PRECISION)
1676 precision -= PART_PRECISION;
1677 if (precision < PART_PRECISION)
1678 num.high &= ((cpp_num_part) 1 << precision) - 1;
1680 else
1682 if (precision < PART_PRECISION)
1683 num.low &= ((cpp_num_part) 1 << precision) - 1;
1684 num.high = 0;
1687 return num;
1690 /* True iff A (presumed signed) >= 0. */
1691 static bool
1692 num_positive (cpp_num num, size_t precision)
1694 if (precision > PART_PRECISION)
1696 precision -= PART_PRECISION;
1697 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1700 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1703 /* Sign extend a number, with PRECISION significant bits and all
1704 others assumed clear, to fill out a cpp_num structure. */
1705 cpp_num
1706 cpp_num_sign_extend (cpp_num num, size_t precision)
1708 if (!num.unsignedp)
1710 if (precision > PART_PRECISION)
1712 precision -= PART_PRECISION;
1713 if (precision < PART_PRECISION
1714 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1715 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1717 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1719 if (precision < PART_PRECISION)
1720 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1721 num.high = ~(cpp_num_part) 0;
1725 return num;
1728 /* Returns the negative of NUM. */
1729 static cpp_num
1730 num_negate (cpp_num num, size_t precision)
1732 cpp_num copy;
1734 copy = num;
1735 num.high = ~num.high;
1736 num.low = ~num.low;
1737 if (++num.low == 0)
1738 num.high++;
1739 num = num_trim (num, precision);
1740 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1742 return num;
1745 /* Returns true if A >= B. */
1746 static bool
1747 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1749 bool unsignedp;
1751 unsignedp = pa.unsignedp || pb.unsignedp;
1753 if (!unsignedp)
1755 /* Both numbers have signed type. If they are of different
1756 sign, the answer is the sign of A. */
1757 unsignedp = num_positive (pa, precision);
1759 if (unsignedp != num_positive (pb, precision))
1760 return unsignedp;
1762 /* Otherwise we can do an unsigned comparison. */
1765 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1768 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1769 static cpp_num
1770 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1771 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1773 lhs.overflow = false;
1774 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1776 /* As excess precision is zeroed, there is no need to num_trim () as
1777 these operations cannot introduce a set bit there. */
1778 if (op == CPP_AND)
1780 lhs.low &= rhs.low;
1781 lhs.high &= rhs.high;
1783 else if (op == CPP_OR)
1785 lhs.low |= rhs.low;
1786 lhs.high |= rhs.high;
1788 else
1790 lhs.low ^= rhs.low;
1791 lhs.high ^= rhs.high;
1794 return lhs;
1797 /* Returns LHS OP RHS, where OP is an inequality. */
1798 static cpp_num
1799 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1800 enum cpp_ttype op)
1802 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1804 if (op == CPP_GREATER_EQ)
1805 lhs.low = gte;
1806 else if (op == CPP_LESS)
1807 lhs.low = !gte;
1808 else if (op == CPP_GREATER)
1809 lhs.low = gte && !num_eq (lhs, rhs);
1810 else /* CPP_LESS_EQ. */
1811 lhs.low = !gte || num_eq (lhs, rhs);
1813 lhs.high = 0;
1814 lhs.overflow = false;
1815 lhs.unsignedp = false;
1816 return lhs;
1819 /* Returns LHS OP RHS, where OP is == or !=. */
1820 static cpp_num
1821 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1822 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1824 /* Work around a 3.0.4 bug; see PR 6950. */
1825 bool eq = num_eq (lhs, rhs);
1826 if (op == CPP_NOT_EQ)
1827 eq = !eq;
1828 lhs.low = eq;
1829 lhs.high = 0;
1830 lhs.overflow = false;
1831 lhs.unsignedp = false;
1832 return lhs;
1835 /* Shift NUM, of width PRECISION, right by N bits. */
1836 static cpp_num
1837 num_rshift (cpp_num num, size_t precision, size_t n)
1839 cpp_num_part sign_mask;
1840 bool x = num_positive (num, precision);
1842 if (num.unsignedp || x)
1843 sign_mask = 0;
1844 else
1845 sign_mask = ~(cpp_num_part) 0;
1847 if (n >= precision)
1848 num.high = num.low = sign_mask;
1849 else
1851 /* Sign-extend. */
1852 if (precision < PART_PRECISION)
1853 num.high = sign_mask, num.low |= sign_mask << precision;
1854 else if (precision < 2 * PART_PRECISION)
1855 num.high |= sign_mask << (precision - PART_PRECISION);
1857 if (n >= PART_PRECISION)
1859 n -= PART_PRECISION;
1860 num.low = num.high;
1861 num.high = sign_mask;
1864 if (n)
1866 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1867 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1871 num = num_trim (num, precision);
1872 num.overflow = false;
1873 return num;
1876 /* Shift NUM, of width PRECISION, left by N bits. */
1877 static cpp_num
1878 num_lshift (cpp_num num, size_t precision, size_t n)
1880 if (n >= precision)
1882 num.overflow = !num.unsignedp && !num_zerop (num);
1883 num.high = num.low = 0;
1885 else
1887 cpp_num orig, maybe_orig;
1888 size_t m = n;
1890 orig = num;
1891 if (m >= PART_PRECISION)
1893 m -= PART_PRECISION;
1894 num.high = num.low;
1895 num.low = 0;
1897 if (m)
1899 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1900 num.low <<= m;
1902 num = num_trim (num, precision);
1904 if (num.unsignedp)
1905 num.overflow = false;
1906 else
1908 maybe_orig = num_rshift (num, precision, n);
1909 num.overflow = !num_eq (orig, maybe_orig);
1913 return num;
1916 /* The four unary operators: +, -, ! and ~. */
1917 static cpp_num
1918 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1920 switch (op)
1922 case CPP_UPLUS:
1923 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1924 cpp_warning (pfile, CPP_W_TRADITIONAL,
1925 "traditional C rejects the unary plus operator");
1926 num.overflow = false;
1927 break;
1929 case CPP_UMINUS:
1930 num = num_negate (num, CPP_OPTION (pfile, precision));
1931 break;
1933 case CPP_COMPL:
1934 num.high = ~num.high;
1935 num.low = ~num.low;
1936 num = num_trim (num, CPP_OPTION (pfile, precision));
1937 num.overflow = false;
1938 break;
1940 default: /* case CPP_NOT: */
1941 num.low = num_zerop (num);
1942 num.high = 0;
1943 num.overflow = false;
1944 num.unsignedp = false;
1945 break;
1948 return num;
1951 /* The various binary operators. */
1952 static cpp_num
1953 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1955 cpp_num result;
1956 size_t precision = CPP_OPTION (pfile, precision);
1957 size_t n;
1959 switch (op)
1961 /* Shifts. */
1962 case CPP_LSHIFT:
1963 case CPP_RSHIFT:
1964 if (!rhs.unsignedp && !num_positive (rhs, precision))
1966 /* A negative shift is a positive shift the other way. */
1967 if (op == CPP_LSHIFT)
1968 op = CPP_RSHIFT;
1969 else
1970 op = CPP_LSHIFT;
1971 rhs = num_negate (rhs, precision);
1973 if (rhs.high)
1974 n = ~0; /* Maximal. */
1975 else
1976 n = rhs.low;
1977 if (op == CPP_LSHIFT)
1978 lhs = num_lshift (lhs, precision, n);
1979 else
1980 lhs = num_rshift (lhs, precision, n);
1981 break;
1983 /* Arithmetic. */
1984 case CPP_MINUS:
1985 result.low = lhs.low - rhs.low;
1986 result.high = lhs.high - rhs.high;
1987 if (result.low > lhs.low)
1988 result.high--;
1989 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1990 result.overflow = false;
1992 result = num_trim (result, precision);
1993 if (!result.unsignedp)
1995 bool lhsp = num_positive (lhs, precision);
1996 result.overflow = (lhsp != num_positive (rhs, precision)
1997 && lhsp != num_positive (result, precision));
1999 return result;
2001 case CPP_PLUS:
2002 result.low = lhs.low + rhs.low;
2003 result.high = lhs.high + rhs.high;
2004 if (result.low < lhs.low)
2005 result.high++;
2006 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2007 result.overflow = false;
2009 result = num_trim (result, precision);
2010 if (!result.unsignedp)
2012 bool lhsp = num_positive (lhs, precision);
2013 result.overflow = (lhsp == num_positive (rhs, precision)
2014 && lhsp != num_positive (result, precision));
2016 return result;
2018 /* Comma. */
2019 default: /* case CPP_COMMA: */
2020 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
2021 || !pfile->state.skip_eval))
2022 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2023 "comma operator in operand of #if");
2024 lhs = rhs;
2025 break;
2028 return lhs;
2031 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2032 cannot overflow. */
2033 static cpp_num
2034 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2036 cpp_num result;
2037 cpp_num_part middle[2], temp;
2039 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2040 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2042 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2043 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2045 temp = result.low;
2046 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2047 if (result.low < temp)
2048 result.high++;
2050 temp = result.low;
2051 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2052 if (result.low < temp)
2053 result.high++;
2055 result.high += HIGH_PART (middle[0]);
2056 result.high += HIGH_PART (middle[1]);
2057 result.unsignedp = true;
2058 result.overflow = false;
2060 return result;
2063 /* Multiply two preprocessing numbers. */
2064 static cpp_num
2065 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2067 cpp_num result, temp;
2068 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2069 bool overflow, negate = false;
2070 size_t precision = CPP_OPTION (pfile, precision);
2072 /* Prepare for unsigned multiplication. */
2073 if (!unsignedp)
2075 if (!num_positive (lhs, precision))
2076 negate = !negate, lhs = num_negate (lhs, precision);
2077 if (!num_positive (rhs, precision))
2078 negate = !negate, rhs = num_negate (rhs, precision);
2081 overflow = lhs.high && rhs.high;
2082 result = num_part_mul (lhs.low, rhs.low);
2084 temp = num_part_mul (lhs.high, rhs.low);
2085 result.high += temp.low;
2086 if (temp.high)
2087 overflow = true;
2089 temp = num_part_mul (lhs.low, rhs.high);
2090 result.high += temp.low;
2091 if (temp.high)
2092 overflow = true;
2094 temp.low = result.low, temp.high = result.high;
2095 result = num_trim (result, precision);
2096 if (!num_eq (result, temp))
2097 overflow = true;
2099 if (negate)
2100 result = num_negate (result, precision);
2102 if (unsignedp)
2103 result.overflow = false;
2104 else
2105 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2106 && !num_zerop (result));
2107 result.unsignedp = unsignedp;
2109 return result;
2112 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2113 or the remainder depending upon OP. LOCATION is the source location
2114 of this operator (for diagnostics). */
2116 static cpp_num
2117 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2118 location_t location)
2120 cpp_num result, sub;
2121 cpp_num_part mask;
2122 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2123 bool negate = false, lhs_neg = false;
2124 size_t i, precision = CPP_OPTION (pfile, precision);
2126 /* Prepare for unsigned division. */
2127 if (!unsignedp)
2129 if (!num_positive (lhs, precision))
2130 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2131 if (!num_positive (rhs, precision))
2132 negate = !negate, rhs = num_negate (rhs, precision);
2135 /* Find the high bit. */
2136 if (rhs.high)
2138 i = precision - 1;
2139 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2140 for (; ; i--, mask >>= 1)
2141 if (rhs.high & mask)
2142 break;
2144 else if (rhs.low)
2146 if (precision > PART_PRECISION)
2147 i = precision - PART_PRECISION - 1;
2148 else
2149 i = precision - 1;
2150 mask = (cpp_num_part) 1 << i;
2151 for (; ; i--, mask >>= 1)
2152 if (rhs.low & mask)
2153 break;
2155 else
2157 if (!pfile->state.skip_eval)
2158 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2159 "division by zero in #if");
2160 return lhs;
2163 /* First nonzero bit of RHS is bit I. Do naive division by
2164 shifting the RHS fully left, and subtracting from LHS if LHS is
2165 at least as big, and then repeating but with one less shift.
2166 This is not very efficient, but is easy to understand. */
2168 rhs.unsignedp = true;
2169 lhs.unsignedp = true;
2170 i = precision - i - 1;
2171 sub = num_lshift (rhs, precision, i);
2173 result.high = result.low = 0;
2174 for (;;)
2176 if (num_greater_eq (lhs, sub, precision))
2178 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2179 if (i >= PART_PRECISION)
2180 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2181 else
2182 result.low |= (cpp_num_part) 1 << i;
2184 if (i-- == 0)
2185 break;
2186 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2187 sub.high >>= 1;
2190 /* We divide so that the remainder has the sign of the LHS. */
2191 if (op == CPP_DIV)
2193 result.unsignedp = unsignedp;
2194 result.overflow = false;
2195 if (!unsignedp)
2197 if (negate)
2198 result = num_negate (result, precision);
2199 result.overflow = (num_positive (result, precision) ^ !negate
2200 && !num_zerop (result));
2203 return result;
2206 /* CPP_MOD. */
2207 lhs.unsignedp = unsignedp;
2208 lhs.overflow = false;
2209 if (lhs_neg)
2210 lhs = num_negate (lhs, precision);
2212 return lhs;