Daily bump.
[official-gcc.git] / libcpp / expr.cc
blob089bf3eddde70dcbe4926888e595f018e92d4ed9
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2024 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, bf16;
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = bf16 = 0;
99 /* The following decimal float suffixes, from TR 24732:2009, TS
100 18661-2:2015 and C23, 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).
135 bf16, BF16 - std::bfloat16_t (ISO C++23). */
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'))
141 bool uppercase = (*s == 'D');
142 switch (s[1])
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;
157 if (CPP_OPTION (pfile, ext_numeric_literals))
159 /* Recognize a fixed-point suffix. */
160 if (len != 0)
161 switch (s[len-1])
163 case 'k': case 'K': flags = CPP_N_ACCUM; break;
164 case 'r': case 'R': flags = CPP_N_FRACT; break;
165 default: break;
168 /* Continue processing a fixed-point suffix. The suffix is case
169 insensitive except for ll or LL. Order is significant. */
170 if (flags)
172 if (len == 1)
173 return flags;
174 len--;
176 if (*s == 'u' || *s == 'U')
178 flags |= CPP_N_UNSIGNED;
179 if (len == 1)
180 return flags;
181 len--;
182 s++;
185 switch (*s)
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;
206 /* Anything left at this point is invalid. */
207 return 0;
211 /* In any remaining valid suffix, the case and order don't matter. */
212 while (len--)
214 switch (s[0])
216 case 'f': case 'F':
217 f++;
218 if (len > 0
219 && s[1] >= '1'
220 && s[1] <= '9'
221 && fn_bits == 0)
223 f--;
224 while (len > 0
225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX)
229 fn_bits = fn_bits * 10 + (s[1] - '0');
230 len--;
231 s++;
233 if (len > 0 && s[1] == 'x')
235 fnx++;
236 len--;
237 s++;
239 else
240 fn++;
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')
250 bf16++;
251 len -= 3;
252 s += 3;
253 break;
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;
265 s++;
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;
285 if (i)
287 if (!CPP_OPTION (pfile, ext_numeric_literals))
288 return 0;
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;
301 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
302 return 0;
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));
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)
320 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
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)
329 size_t orig_len = len;
330 size_t u, l, i, z, wb;
332 u = l = i = z = wb = 0;
334 while (len--)
335 switch (s[len])
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;
362 if (l > 2 || u > 1 || i > 1 || z > 1 || wb > 1)
363 return 0;
365 if (z)
367 if (l > 0 || i > 0)
368 return 0;
369 if (!CPP_OPTION (pfile, cplusplus))
370 return 0;
373 if (wb)
375 if (CPP_OPTION (pfile, cplusplus))
376 return 0;
377 if (l > 0 || i > 0 || z > 0)
378 return 0;
381 if (i)
383 if (!CPP_OPTION (pfile, ext_numeric_literals))
384 return 0;
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;
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));
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)
407 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
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)
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;
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)
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;
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)
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;
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)
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;
490 /* Return true if the token type is a user-defined string literal. */
491 bool
492 cpp_userdef_string_p (enum cpp_ttype type)
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;
504 /* Return true if the token type is a user-defined char literal. */
505 bool
506 cpp_userdef_char_p (enum cpp_ttype type)
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;
518 /* Extract the suffix from a user-defined literal string or char. */
519 const char *
520 cpp_get_userdef_suffix (const cpp_token *tok)
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;
538 /* Categorize numeric constants according to their field (integer,
539 floating point, or invalid), radix (decimal, octal, hexadecimal),
540 and type suffixes.
542 TOKEN is the token that represents the numeric constant to
543 classify.
545 In C++0X if UD_SUFFIX is non null it will be assigned
546 any unrecognized suffix for a user-defined literal.
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)
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;
560 if (ud_suffix)
561 *ud_suffix = NULL;
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;
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;
575 /* First, interpret the radix. */
576 if (*str == '0')
578 radix = 8;
579 str++;
581 /* Require at least one hex digit to classify it as hex. */
582 if (*str == 'x' || *str == 'X')
584 if (str[1] == '.' || ISXDIGIT (str[1]))
586 radix = 16;
587 str++;
589 else if (DIGIT_SEP (str[1]))
590 SYNTAX_ERROR_AT (virtual_location,
591 "digit separator after base indicator");
593 else if (*str == 'b' || *str == 'B')
595 if (str[1] == '0' || str[1] == '1')
597 radix = 2;
598 str++;
600 else if (DIGIT_SEP (str[1]))
601 SYNTAX_ERROR_AT (virtual_location,
602 "digit separator after base indicator");
606 /* Now scan for a well-formed integer or float. */
607 for (;;)
609 unsigned int c = *str++;
611 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
613 seen_digit_sep = false;
614 seen_digit = true;
615 c = hex_value (c);
616 if (c > max_digit)
617 max_digit = c;
619 else if (DIGIT_SEP (c))
620 seen_digit_sep = true;
621 else if (c == '.')
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");
633 else if ((radix <= 10 && (c == 'e' || c == 'E'))
634 || (radix == 16 && (c == 'p' || c == 'P')))
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;
642 else
644 /* Start of suffix. */
645 str--;
646 break;
650 if (seen_digit_sep && float_flag != AFTER_EXPON)
651 SYNTAX_ERROR_AT (virtual_location,
652 "digit separator outside digit sequence");
654 /* The suffix may be for decimal fixed-point constants without exponent. */
655 if (radix != 16 && float_flag == NOT_FLOAT)
657 result = interpret_float_suffix (pfile, str, limit - str);
658 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
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;
665 cpp_pedwarning_with_line
666 (pfile, CPP_W_PEDANTIC, virtual_location, 0,
667 "fixed-point constants are a GCC extension");
668 goto syntax_ok;
670 else
671 result = 0;
674 if (float_flag != NOT_FLOAT && radix == 8)
675 radix = 10;
677 if (max_digit >= radix)
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);
687 if (float_flag != NOT_FLOAT)
689 if (radix == 2)
691 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
692 "invalid prefix \"0b\" for floating constant");
693 return CPP_N_INVALID;
696 if (radix == 16 && !seen_digit)
697 SYNTAX_ERROR_AT (virtual_location,
698 "no digits in hexadecimal floating constant");
700 if (radix == 16 && CPP_PEDANTIC (pfile)
701 && !CPP_OPTION (pfile, extended_numbers))
703 if (CPP_OPTION (pfile, cplusplus))
704 cpp_pedwarning_with_line (pfile, CPP_W_CXX17_EXTENSIONS,
705 virtual_location, 0, "use of C++17 "
706 "hexadecimal floating constant");
707 else
708 cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC,
709 virtual_location, 0, "use of C99 "
710 "hexadecimal floating constant");
713 if (float_flag == AFTER_EXPON)
715 if (*str == '+' || *str == '-')
716 str++;
718 /* Exponent is decimal, even if string is a hex float. */
719 if (!ISDIGIT (*str))
721 if (DIGIT_SEP (*str))
722 SYNTAX_ERROR_AT (virtual_location,
723 "digit separator adjacent to exponent");
724 else
725 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
729 seen_digit_sep = DIGIT_SEP (*str);
730 str++;
732 while (ISDIGIT (*str) || DIGIT_SEP (*str));
734 else if (radix == 16)
735 SYNTAX_ERROR_AT (virtual_location,
736 "hexadecimal floating constants require an exponent");
738 if (seen_digit_sep)
739 SYNTAX_ERROR_AT (virtual_location,
740 "digit separator outside digit sequence");
742 result = interpret_float_suffix (pfile, str, limit - str);
743 if (result == 0)
745 if (CPP_OPTION (pfile, user_literals))
747 if (ud_suffix)
748 *ud_suffix = (const char *) str;
749 result = CPP_N_LARGE | CPP_N_USERDEF;
751 else
753 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
754 "invalid suffix \"%.*s\" on floating constant",
755 (int) (limit - str), str);
756 return CPP_N_INVALID;
760 /* Traditional C didn't accept any floating suffixes. */
761 if (limit != str
762 && CPP_WTRADITIONAL (pfile)
763 && ! cpp_sys_macro_p (pfile))
764 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
765 "traditional C rejects the \"%.*s\" suffix",
766 (int) (limit - str), str);
768 /* A suffix for double is a GCC extension via decimal float support.
769 If the suffix also specifies an imaginary value we'll catch that
770 later. */
771 if (result == CPP_N_MEDIUM)
772 cpp_pedwarning_with_line
773 (pfile, CPP_W_PEDANTIC, virtual_location, 0,
774 "suffix for double constant is a GCC extension");
776 /* Radix must be 10 for decimal floats. */
777 if ((result & CPP_N_DFLOAT) && radix != 10)
779 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
780 "invalid suffix \"%.*s\" with hexadecimal floating constant",
781 (int) (limit - str), str);
782 return CPP_N_INVALID;
785 if (result & (CPP_N_FRACT | CPP_N_ACCUM))
786 cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
787 "fixed-point constants are a GCC extension");
789 if (result & CPP_N_DFLOAT)
791 if (!CPP_OPTION (pfile, dfp_constants))
792 cpp_pedwarning_with_line
793 (pfile, CPP_W_PEDANTIC, virtual_location, 0,
794 "decimal float constants are a C23 feature");
795 else if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
796 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
797 virtual_location, 0,
798 "decimal float constants are a C23 feature");
801 result |= CPP_N_FLOATING;
803 else
805 result = interpret_int_suffix (pfile, str, limit - str);
806 if (result == 0)
808 if (CPP_OPTION (pfile, user_literals))
810 if (ud_suffix)
811 *ud_suffix = (const char *) str;
812 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
814 else
816 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
817 "invalid suffix \"%.*s\" on integer constant",
818 (int) (limit - str), str);
819 return CPP_N_INVALID;
823 /* Traditional C only accepted the 'L' suffix.
824 Suppress warning about 'LL' with -Wno-long-long. */
825 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
827 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
828 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
829 && CPP_OPTION (pfile, cpp_warn_long_long);
831 if (u_or_i || large)
832 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
833 virtual_location, 0,
834 "traditional C rejects the \"%.*s\" suffix",
835 (int) (limit - str), str);
838 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
839 && CPP_OPTION (pfile, cpp_warn_long_long))
841 const char *message = CPP_OPTION (pfile, cplusplus)
842 ? N_("use of C++11 long long integer constant")
843 : N_("use of C99 long long integer constant");
845 if (CPP_OPTION (pfile, c99))
846 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
847 0, message);
848 else
849 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
850 virtual_location, 0, message);
853 if ((result & CPP_N_SIZE_T) == CPP_N_SIZE_T
854 && !CPP_OPTION (pfile, size_t_literals))
856 const char *message = (result & CPP_N_UNSIGNED) == CPP_N_UNSIGNED
857 ? N_("use of C++23 %<size_t%> integer constant")
858 : N_("use of C++23 %<make_signed_t<size_t>%> integer constant");
859 cpp_warning_with_line (pfile, CPP_W_SIZE_T_LITERALS,
860 virtual_location, 0, message);
863 if ((result & CPP_N_BITINT) != 0
864 && CPP_OPTION (pfile, cpp_warn_c11_c23_compat) != 0)
866 if (CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
868 const char *message = N_("ISO C does not support literal "
869 "%<wb%> suffixes before C23");
870 if (CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, true_false))
871 cpp_pedwarning_with_line (pfile, CPP_W_C11_C23_COMPAT,
872 virtual_location, 0, message);
873 else
874 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
875 virtual_location, 0, message);
877 else if (!CPP_OPTION (pfile, true_false))
879 const char *message = N_("ISO C does not support literal "
880 "%<wb%> suffixes before C23");
881 cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC,
882 virtual_location, 0, message);
886 result |= CPP_N_INTEGER;
889 syntax_ok:
890 if (result & CPP_N_IMAGINARY)
891 cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC, virtual_location, 0,
892 "imaginary constants are a GCC extension");
893 if (radix == 2)
895 bool warned = false;
896 if (!CPP_OPTION (pfile, binary_constants)
897 && CPP_PEDANTIC (pfile))
899 if (CPP_OPTION (pfile, cplusplus))
900 warned
901 = (cpp_pedwarning_with_line
902 (pfile, CPP_W_CXX14_EXTENSIONS, virtual_location, 0,
903 "binary constants are a C++14 feature or GCC extension"));
904 else
905 warned
906 = (cpp_pedwarning_with_line
907 (pfile, CPP_W_PEDANTIC, virtual_location, 0,
908 "binary constants are a C23 feature or GCC extension"));
910 if (!warned && CPP_OPTION (pfile, cpp_warn_c11_c23_compat) > 0)
911 cpp_warning_with_line (pfile, CPP_W_C11_C23_COMPAT,
912 virtual_location, 0,
913 "binary constants are a C23 feature");
916 if (radix == 10)
917 result |= CPP_N_DECIMAL;
918 else if (radix == 16)
919 result |= CPP_N_HEX;
920 else if (radix == 2)
921 result |= CPP_N_BINARY;
922 else
923 result |= CPP_N_OCTAL;
925 return result;
927 syntax_error:
928 return CPP_N_INVALID;
931 /* cpp_interpret_integer converts an integer constant into a cpp_num,
932 of precision options->precision.
934 We do not provide any interface for decimal->float conversion,
935 because the preprocessor doesn't need it and we don't want to
936 drag in GCC's floating point emulator. */
937 cpp_num
938 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
939 unsigned int type)
941 const uchar *p, *end;
942 cpp_num result;
944 result.low = 0;
945 result.high = 0;
946 result.unsignedp = !!(type & CPP_N_UNSIGNED);
947 result.overflow = false;
949 p = token->val.str.text;
950 end = p + token->val.str.len;
952 /* Common case of a single digit. */
953 if (token->val.str.len == 1)
954 result.low = p[0] - '0';
955 else
957 cpp_num_part max;
958 size_t precision = CPP_OPTION (pfile, precision);
959 unsigned int base = 10, c = 0;
960 bool overflow = false;
962 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
964 base = 8;
965 p++;
967 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
969 base = 16;
970 p += 2;
972 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
974 base = 2;
975 p += 2;
978 /* We can add a digit to numbers strictly less than this without
979 needing the precision and slowness of double integers. */
980 max = ~(cpp_num_part) 0;
981 if (precision < PART_PRECISION)
982 max >>= PART_PRECISION - precision;
983 max = (max - base + 1) / base + 1;
985 for (; p < end; p++)
987 c = *p;
989 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
990 c = hex_value (c);
991 else if (DIGIT_SEP (c))
992 continue;
993 else
994 break;
996 /* Strict inequality for when max is set to zero. */
997 if (result.low < max)
998 result.low = result.low * base + c;
999 else
1001 result = append_digit (result, c, base, precision);
1002 overflow |= result.overflow;
1003 max = 0;
1007 if (overflow && !(type & CPP_N_USERDEF))
1008 cpp_error (pfile, CPP_DL_PEDWARN,
1009 "integer constant is too large for its type");
1010 /* If too big to be signed, consider it unsigned. Only warn for
1011 decimal numbers. Traditional numbers were always signed (but
1012 we still honor an explicit U suffix); but we only have
1013 traditional semantics in directives. */
1014 else if (!result.unsignedp
1015 && !(CPP_OPTION (pfile, traditional)
1016 && pfile->state.in_directive)
1017 && !num_positive (result, precision))
1019 /* This is for constants within the range of uintmax_t but
1020 not that of intmax_t. For such decimal constants, a
1021 diagnostic is required for C99 as the selected type must
1022 be signed and not having a type is a constraint violation
1023 (DR#298, TC3), so this must be a pedwarn. For C90,
1024 unsigned long is specified to be used for a constant that
1025 does not fit in signed long; if uintmax_t has the same
1026 range as unsigned long this means only a warning is
1027 appropriate here. C90 permits the preprocessor to use a
1028 wider range than unsigned long in the compiler, so if
1029 uintmax_t is wider than unsigned long no diagnostic is
1030 required for such constants in preprocessor #if
1031 expressions and the compiler will pedwarn for such
1032 constants outside the range of unsigned long that reach
1033 the compiler so a diagnostic is not required there
1034 either; thus, pedwarn for C99 but use a plain warning for
1035 C90. */
1036 if (base == 10)
1037 cpp_error (pfile, (CPP_OPTION (pfile, c99)
1038 ? CPP_DL_PEDWARN
1039 : CPP_DL_WARNING),
1040 "integer constant is so large that it is unsigned");
1041 result.unsignedp = true;
1045 return result;
1048 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
1049 static cpp_num
1050 append_digit (cpp_num num, int digit, int base, size_t precision)
1052 cpp_num result;
1053 unsigned int shift;
1054 bool overflow;
1055 cpp_num_part add_high, add_low;
1057 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
1058 need to worry about add_high overflowing. */
1059 switch (base)
1061 case 2:
1062 shift = 1;
1063 break;
1065 case 16:
1066 shift = 4;
1067 break;
1069 default:
1070 shift = 3;
1072 overflow = !!(num.high >> (PART_PRECISION - shift));
1073 result.high = num.high << shift;
1074 result.low = num.low << shift;
1075 result.high |= num.low >> (PART_PRECISION - shift);
1076 result.unsignedp = num.unsignedp;
1078 if (base == 10)
1080 add_low = num.low << 1;
1081 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
1083 else
1084 add_high = add_low = 0;
1086 if (add_low + digit < add_low)
1087 add_high++;
1088 add_low += digit;
1090 if (result.low + add_low < result.low)
1091 add_high++;
1092 if (result.high + add_high < result.high)
1093 overflow = true;
1095 result.low += add_low;
1096 result.high += add_high;
1097 result.overflow = overflow;
1099 /* The above code catches overflow of a cpp_num type. This catches
1100 overflow of the (possibly shorter) target precision. */
1101 num.low = result.low;
1102 num.high = result.high;
1103 result = num_trim (result, precision);
1104 if (!num_eq (result, num))
1105 result.overflow = true;
1107 return result;
1110 /* Handle meeting "defined" in a preprocessor expression. */
1111 static cpp_num
1112 parse_defined (cpp_reader *pfile)
1114 cpp_num result;
1115 int paren = 0;
1116 cpp_hashnode *node = 0;
1117 const cpp_token *token;
1118 cpp_context *initial_context = pfile->context;
1120 if (pfile->state.in_directive == 3)
1121 cpp_error (pfile, CPP_DL_ERROR, "'defined' in #embed parameter");
1123 /* Don't expand macros. */
1124 pfile->state.prevent_expansion++;
1126 token = cpp_get_token (pfile);
1127 if (token->type == CPP_OPEN_PAREN)
1129 paren = 1;
1130 token = cpp_get_token (pfile);
1133 if (token->type == CPP_NAME)
1135 node = token->val.node.node;
1136 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1138 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1139 node = 0;
1142 else
1144 cpp_error (pfile, CPP_DL_ERROR,
1145 "operator \"defined\" requires an identifier");
1146 if (token->flags & NAMED_OP)
1148 cpp_token op;
1150 op.flags = 0;
1151 op.type = token->type;
1152 cpp_error (pfile, CPP_DL_ERROR,
1153 "(\"%s\" is an alternative token for \"%s\" in C++)",
1154 cpp_token_as_text (pfile, token),
1155 cpp_token_as_text (pfile, &op));
1159 bool is_defined = false;
1160 if (node)
1162 if ((pfile->context != initial_context
1163 || initial_context != &pfile->base_context)
1164 && CPP_OPTION (pfile, warn_expansion_to_defined))
1165 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1166 "this use of \"defined\" may not be portable");
1167 is_defined = _cpp_defined_macro_p (node);
1168 if (!_cpp_maybe_notify_macro_use (pfile, node, token->src_loc))
1169 /* It wasn't a macro after all. */
1170 is_defined = false;
1171 _cpp_mark_macro_used (node);
1173 /* A possible controlling macro of the form #if !defined ().
1174 _cpp_parse_expr checks there was no other junk on the line. */
1175 pfile->mi_ind_cmacro = node;
1178 pfile->state.prevent_expansion--;
1180 /* Do not treat conditional macros as being defined. This is due to the
1181 powerpc port using conditional macros for 'vector', 'bool', and 'pixel'
1182 to act as conditional keywords. This messes up tests like #ifndef
1183 bool. */
1184 result.unsignedp = false;
1185 result.high = 0;
1186 result.overflow = false;
1187 result.low = is_defined;
1188 return result;
1191 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1192 number or character constant, or the result of the "defined" or "#"
1193 operators). */
1194 static cpp_num
1195 eval_token (cpp_reader *pfile, const cpp_token *token,
1196 location_t virtual_location)
1198 cpp_num result;
1199 unsigned int temp;
1200 int unsignedp = 0;
1202 result.unsignedp = false;
1203 result.overflow = false;
1205 switch (token->type)
1207 case CPP_NUMBER:
1208 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1209 if (temp & CPP_N_USERDEF)
1210 cpp_error (pfile, CPP_DL_ERROR,
1211 "user-defined literal in preprocessor expression");
1212 switch (temp & CPP_N_CATEGORY)
1214 case CPP_N_FLOATING:
1215 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1216 "floating constant in preprocessor expression");
1217 break;
1218 case CPP_N_INTEGER:
1219 if (!(temp & CPP_N_IMAGINARY))
1220 return cpp_interpret_integer (pfile, token, temp);
1221 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1222 "imaginary number in preprocessor expression");
1223 break;
1225 case CPP_N_INVALID:
1226 /* Error already issued. */
1227 break;
1229 result.high = result.low = 0;
1230 break;
1232 case CPP_WCHAR:
1233 case CPP_CHAR:
1234 case CPP_CHAR16:
1235 case CPP_CHAR32:
1236 case CPP_UTF8CHAR:
1238 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1239 &temp, &unsignedp);
1241 result.high = 0;
1242 result.low = cc;
1243 /* Sign-extend the result if necessary. */
1244 if (!unsignedp && (cppchar_signed_t) cc < 0)
1246 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1247 result.low |= ~(~(cpp_num_part) 0
1248 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1249 result.high = ~(cpp_num_part) 0;
1250 result = num_trim (result, CPP_OPTION (pfile, precision));
1253 break;
1255 case CPP_NAME:
1256 if (token->val.node.node == pfile->spec_nodes.n_defined)
1257 return parse_defined (pfile);
1258 else if (CPP_OPTION (pfile, true_false)
1259 && (token->val.node.node == pfile->spec_nodes.n_true
1260 || token->val.node.node == pfile->spec_nodes.n_false))
1262 result.high = 0;
1263 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1265 else
1267 result.high = 0;
1268 result.low = 0;
1269 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1270 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1271 "\"%s\" is not defined, evaluates to 0",
1272 NODE_NAME (token->val.node.node));
1274 break;
1276 case CPP_HASH:
1277 if (!pfile->state.skipping)
1279 /* A pedantic warning takes precedence over a deprecated
1280 warning here. */
1281 if (cpp_pedwarning_with_line (pfile, CPP_W_PEDANTIC,
1282 virtual_location, 0,
1283 "assertions are a GCC extension"))
1285 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1286 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1287 "assertions are a deprecated extension");
1289 _cpp_test_assertion (pfile, &temp);
1290 result.high = 0;
1291 result.low = temp;
1292 break;
1294 default:
1295 abort ();
1298 result.unsignedp = !!unsignedp;
1299 return result;
1302 /* Operator precedence and flags table.
1304 After an operator is returned from the lexer, if it has priority less
1305 than the operator on the top of the stack, we reduce the stack by one
1306 operator and repeat the test. Since equal priorities do not reduce,
1307 this is naturally right-associative.
1309 We handle left-associative operators by decrementing the priority of
1310 just-lexed operators by one, but retaining the priority of operators
1311 already on the stack.
1313 The remaining cases are '(' and ')'. We handle '(' by skipping the
1314 reduction phase completely. ')' is given lower priority than
1315 everything else, including '(', effectively forcing a reduction of the
1316 parenthesized expression. If there is a matching '(', the routine
1317 reduce() exits immediately. If the normal exit route sees a ')', then
1318 there cannot have been a matching '(' and an error message is output.
1320 The parser assumes all shifted operators require a left operand unless
1321 the flag NO_L_OPERAND is set. These semantics are automatic; any
1322 extra semantics need to be handled with operator-specific code. */
1324 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1325 operand changes because of integer promotions. */
1326 #define NO_L_OPERAND (1 << 0)
1327 #define LEFT_ASSOC (1 << 1)
1328 #define CHECK_PROMOTION (1 << 2)
1330 /* Operator to priority map. Must be in the same order as the first
1331 N entries of enum cpp_ttype. */
1332 static const struct cpp_operator
1334 uchar prio;
1335 uchar flags;
1336 } optab[] =
1338 /* EQ */ {0, 0}, /* Shouldn't happen. */
1339 /* NOT */ {16, NO_L_OPERAND},
1340 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1341 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1342 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1343 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1344 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1345 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1346 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1347 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1348 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1349 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1350 /* RSHIFT */ {13, LEFT_ASSOC},
1351 /* LSHIFT */ {13, LEFT_ASSOC},
1353 /* COMPL */ {16, NO_L_OPERAND},
1354 /* AND_AND */ {6, LEFT_ASSOC},
1355 /* OR_OR */ {5, LEFT_ASSOC},
1356 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1357 However, there are some special cases for these in reduce(). */
1358 /* QUERY */ {4, 0},
1359 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1360 /* COMMA */ {4, LEFT_ASSOC},
1361 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1362 /* CLOSE_PAREN */ {0, 0},
1363 /* EOF */ {0, 0},
1364 /* EQ_EQ */ {11, LEFT_ASSOC},
1365 /* NOT_EQ */ {11, LEFT_ASSOC},
1366 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1367 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1368 /* UPLUS */ {16, NO_L_OPERAND},
1369 /* UMINUS */ {16, NO_L_OPERAND}
1372 /* Parse and evaluate a C expression, reading from PFILE.
1373 Returns the truth value of the expression if OPEN_PAREN
1374 is NULL, otherwise the low 64-bits of the result (when parsing
1375 #embed/__has_embed parameters).
1377 The implementation is an operator precedence parser, i.e. a
1378 bottom-up parser, using a stack for not-yet-reduced tokens.
1380 The stack base is op_stack, and the current stack pointer is 'top'.
1381 There is a stack element for each operator (only), and the most
1382 recently pushed operator is 'top->op'. An operand (value) is
1383 stored in the 'value' field of the stack element of the operator
1384 that precedes it. */
1385 cpp_num_part
1386 _cpp_parse_expr (cpp_reader *pfile, const char *dir,
1387 const cpp_token *open_paren)
1389 struct op *top = pfile->op_stack;
1390 unsigned int lex_count;
1391 bool saw_leading_not, want_value = true;
1392 location_t virtual_location = 0;
1394 pfile->state.skip_eval = 0;
1396 /* Set up detection of #if ! defined(). */
1397 pfile->mi_ind_cmacro = 0;
1398 saw_leading_not = false;
1399 lex_count = 0;
1401 /* Lowest priority operator prevents further reductions. */
1402 top->op = CPP_EOF;
1404 if (pfile->state.in_directive == 3)
1406 ++top;
1407 top->op = CPP_OPEN_PAREN;
1408 top->token = open_paren;
1409 top->loc = open_paren->src_loc;
1412 for (;;)
1414 struct op op;
1416 lex_count++;
1417 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1418 op.op = op.token->type;
1419 op.loc = virtual_location;
1421 switch (op.op)
1423 /* These tokens convert into values. */
1424 case CPP_NUMBER:
1425 case CPP_CHAR:
1426 case CPP_WCHAR:
1427 case CPP_CHAR16:
1428 case CPP_CHAR32:
1429 case CPP_UTF8CHAR:
1430 case CPP_NAME:
1431 case CPP_HASH:
1432 if (!want_value)
1433 SYNTAX_ERROR2_AT (op.loc,
1434 "missing binary operator before token \"%s\"",
1435 cpp_token_as_text (pfile, op.token));
1436 want_value = false;
1437 top->value = eval_token (pfile, op.token, op.loc);
1438 continue;
1440 case CPP_NOT:
1441 saw_leading_not = lex_count == 1;
1442 break;
1443 case CPP_PLUS:
1444 if (want_value)
1445 op.op = CPP_UPLUS;
1446 break;
1447 case CPP_MINUS:
1448 if (want_value)
1449 op.op = CPP_UMINUS;
1450 break;
1452 case CPP_PADDING:
1453 lex_count--;
1454 continue;
1456 default:
1457 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1458 SYNTAX_ERROR2_AT (op.loc,
1459 "token \"%s\" is not valid in preprocessor expressions",
1460 cpp_token_as_text (pfile, op.token));
1461 break;
1464 /* Check we have a value or operator as appropriate. */
1465 if (optab[op.op].flags & NO_L_OPERAND)
1467 if (!want_value)
1468 SYNTAX_ERROR2_AT (op.loc,
1469 "missing binary operator before token \"%s\"",
1470 cpp_token_as_text (pfile, op.token));
1472 else if (want_value)
1474 /* We want a number (or expression) and haven't got one.
1475 Try to emit a specific diagnostic. */
1476 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1477 SYNTAX_ERROR_AT (op.loc,
1478 "missing expression between '(' and ')'");
1480 if (op.op == CPP_EOF && top->op == CPP_EOF)
1481 SYNTAX_ERROR2_AT (op.loc,
1482 "%s with no expression", dir);
1484 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1485 SYNTAX_ERROR2_AT (op.loc,
1486 "operator '%s' has no right operand",
1487 cpp_token_as_text (pfile, top->token));
1488 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1489 /* Complain about missing paren during reduction. */;
1490 else
1491 SYNTAX_ERROR2_AT (op.loc,
1492 "operator '%s' has no left operand",
1493 cpp_token_as_text (pfile, op.token));
1496 top = reduce (pfile, top, op.op);
1497 if (!top)
1498 goto syntax_error;
1500 if (op.op == CPP_EOF)
1501 break;
1503 switch (op.op)
1505 case CPP_CLOSE_PAREN:
1506 if (pfile->state.in_directive == 3 && top == pfile->op_stack)
1507 goto embed_done;
1508 continue;
1509 case CPP_OR_OR:
1510 if (!num_zerop (top->value))
1511 pfile->state.skip_eval++;
1512 break;
1513 case CPP_AND_AND:
1514 case CPP_QUERY:
1515 if (num_zerop (top->value))
1516 pfile->state.skip_eval++;
1517 break;
1518 case CPP_COLON:
1519 if (top->op != CPP_QUERY)
1520 SYNTAX_ERROR_AT (op.loc,
1521 " ':' without preceding '?'");
1522 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1523 pfile->state.skip_eval++;
1524 else
1525 pfile->state.skip_eval--;
1526 default:
1527 break;
1530 want_value = true;
1532 /* Check for and handle stack overflow. */
1533 if (++top == pfile->op_limit)
1534 top = _cpp_expand_op_stack (pfile);
1536 top->op = op.op;
1537 top->token = op.token;
1538 top->loc = op.loc;
1541 /* The controlling macro expression is only valid if we called lex 3
1542 times: <!> <defined expression> and <EOF>. push_conditional ()
1543 checks that we are at top-of-file. */
1544 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1545 pfile->mi_ind_cmacro = 0;
1547 if (top != pfile->op_stack)
1549 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1550 "unbalanced stack in %s", dir);
1551 syntax_error:
1552 return false; /* Return false on syntax error. */
1555 if (pfile->state.in_directive == 3)
1557 embed_done:
1558 if (num_zerop (top->value))
1559 return 0;
1560 if (!top->value.unsignedp
1561 && !num_positive (top->value, CPP_OPTION (pfile, precision)))
1563 cpp_error_with_line (pfile, CPP_DL_ERROR, top->loc, 0,
1564 "negative embed parameter operand");
1565 return 1;
1567 if (top->value.high)
1569 cpp_error_with_line (pfile, CPP_DL_ERROR, top->loc, 0,
1570 "too large embed parameter operand");
1571 return 1;
1573 return top->value.low;
1575 return !num_zerop (top->value);
1578 /* Reduce the operator / value stack if possible, in preparation for
1579 pushing operator OP. Returns NULL on error, otherwise the top of
1580 the stack. */
1581 static struct op *
1582 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1584 unsigned int prio;
1586 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1588 bad_op:
1589 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1590 return 0;
1593 if (op == CPP_OPEN_PAREN)
1594 return top;
1596 /* Decrement the priority of left-associative operators to force a
1597 reduction with operators of otherwise equal priority. */
1598 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1599 while (prio < optab[top->op].prio)
1601 if (CPP_OPTION (pfile, warn_num_sign_change)
1602 && optab[top->op].flags & CHECK_PROMOTION)
1603 check_promotion (pfile, top);
1605 switch (top->op)
1607 case CPP_UPLUS:
1608 case CPP_UMINUS:
1609 case CPP_NOT:
1610 case CPP_COMPL:
1611 top[-1].value = num_unary_op (pfile, top->value, top->op);
1612 top[-1].loc = top->loc;
1613 break;
1615 case CPP_PLUS:
1616 case CPP_MINUS:
1617 case CPP_RSHIFT:
1618 case CPP_LSHIFT:
1619 case CPP_COMMA:
1620 top[-1].value = num_binary_op (pfile, top[-1].value,
1621 top->value, top->op);
1622 top[-1].loc = top->loc;
1623 break;
1625 case CPP_GREATER:
1626 case CPP_LESS:
1627 case CPP_GREATER_EQ:
1628 case CPP_LESS_EQ:
1629 top[-1].value
1630 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1631 top[-1].loc = top->loc;
1632 break;
1634 case CPP_EQ_EQ:
1635 case CPP_NOT_EQ:
1636 top[-1].value
1637 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1638 top[-1].loc = top->loc;
1639 break;
1641 case CPP_AND:
1642 case CPP_OR:
1643 case CPP_XOR:
1644 top[-1].value
1645 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1646 top[-1].loc = top->loc;
1647 break;
1649 case CPP_MULT:
1650 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1651 top[-1].loc = top->loc;
1652 break;
1654 case CPP_DIV:
1655 case CPP_MOD:
1656 top[-1].value = num_div_op (pfile, top[-1].value,
1657 top->value, top->op, top->loc);
1658 top[-1].loc = top->loc;
1659 break;
1661 case CPP_OR_OR:
1662 top--;
1663 if (!num_zerop (top->value))
1664 pfile->state.skip_eval--;
1665 top->value.low = (!num_zerop (top->value)
1666 || !num_zerop (top[1].value));
1667 top->value.high = 0;
1668 top->value.unsignedp = false;
1669 top->value.overflow = false;
1670 top->loc = top[1].loc;
1671 continue;
1673 case CPP_AND_AND:
1674 top--;
1675 if (num_zerop (top->value))
1676 pfile->state.skip_eval--;
1677 top->value.low = (!num_zerop (top->value)
1678 && !num_zerop (top[1].value));
1679 top->value.high = 0;
1680 top->value.unsignedp = false;
1681 top->value.overflow = false;
1682 top->loc = top[1].loc;
1683 continue;
1685 case CPP_OPEN_PAREN:
1686 if (op != CPP_CLOSE_PAREN)
1688 cpp_error_with_line (pfile, CPP_DL_ERROR,
1689 top->token->src_loc,
1690 0, "missing ')' in expression");
1691 return 0;
1693 top--;
1694 top->value = top[1].value;
1695 top->loc = top[1].loc;
1696 return top;
1698 case CPP_COLON:
1699 top -= 2;
1700 if (!num_zerop (top->value))
1702 pfile->state.skip_eval--;
1703 top->value = top[1].value;
1704 top->loc = top[1].loc;
1706 else
1708 top->value = top[2].value;
1709 top->loc = top[2].loc;
1711 top->value.unsignedp = (top[1].value.unsignedp
1712 || top[2].value.unsignedp);
1713 continue;
1715 case CPP_QUERY:
1716 /* COMMA and COLON should not reduce a QUERY operator. */
1717 if (op == CPP_COMMA || op == CPP_COLON)
1718 return top;
1719 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1720 return 0;
1722 default:
1723 goto bad_op;
1726 top--;
1727 if (top->value.overflow && !pfile->state.skip_eval)
1728 cpp_error (pfile, CPP_DL_PEDWARN,
1729 "integer overflow in preprocessor expression");
1732 if (op == CPP_CLOSE_PAREN)
1734 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1735 return 0;
1738 return top;
1741 /* Returns the position of the old top of stack after expansion. */
1742 struct op *
1743 _cpp_expand_op_stack (cpp_reader *pfile)
1745 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1746 size_t new_size = old_size * 2 + 20;
1748 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1749 pfile->op_limit = pfile->op_stack + new_size;
1751 return pfile->op_stack + old_size;
1754 /* Emits a warning if the effective sign of either operand of OP
1755 changes because of integer promotions. */
1756 static void
1757 check_promotion (cpp_reader *pfile, const struct op *op)
1759 if (op->value.unsignedp == op[-1].value.unsignedp)
1760 return;
1762 if (op->value.unsignedp)
1764 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1765 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1766 "the left operand of \"%s\" changes sign when promoted",
1767 cpp_token_as_text (pfile, op->token));
1769 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1770 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1771 "the right operand of \"%s\" changes sign when promoted",
1772 cpp_token_as_text (pfile, op->token));
1775 /* Clears the unused high order bits of the number pointed to by PNUM. */
1776 static cpp_num
1777 num_trim (cpp_num num, size_t precision)
1779 if (precision > PART_PRECISION)
1781 precision -= PART_PRECISION;
1782 if (precision < PART_PRECISION)
1783 num.high &= ((cpp_num_part) 1 << precision) - 1;
1785 else
1787 if (precision < PART_PRECISION)
1788 num.low &= ((cpp_num_part) 1 << precision) - 1;
1789 num.high = 0;
1792 return num;
1795 /* True iff A (presumed signed) >= 0. */
1796 static bool
1797 num_positive (cpp_num num, size_t precision)
1799 if (precision > PART_PRECISION)
1801 precision -= PART_PRECISION;
1802 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1805 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1808 /* Sign extend a number, with PRECISION significant bits and all
1809 others assumed clear, to fill out a cpp_num structure. */
1810 cpp_num
1811 cpp_num_sign_extend (cpp_num num, size_t precision)
1813 if (!num.unsignedp)
1815 if (precision > PART_PRECISION)
1817 precision -= PART_PRECISION;
1818 if (precision < PART_PRECISION
1819 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1820 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1822 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1824 if (precision < PART_PRECISION)
1825 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1826 num.high = ~(cpp_num_part) 0;
1830 return num;
1833 /* Returns the negative of NUM. */
1834 static cpp_num
1835 num_negate (cpp_num num, size_t precision)
1837 cpp_num copy;
1839 copy = num;
1840 num.high = ~num.high;
1841 num.low = ~num.low;
1842 if (++num.low == 0)
1843 num.high++;
1844 num = num_trim (num, precision);
1845 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1847 return num;
1850 /* Returns true if A >= B. */
1851 static bool
1852 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1854 bool unsignedp;
1856 unsignedp = pa.unsignedp || pb.unsignedp;
1858 if (!unsignedp)
1860 /* Both numbers have signed type. If they are of different
1861 sign, the answer is the sign of A. */
1862 unsignedp = num_positive (pa, precision);
1864 if (unsignedp != num_positive (pb, precision))
1865 return unsignedp;
1867 /* Otherwise we can do an unsigned comparison. */
1870 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1873 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1874 static cpp_num
1875 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1876 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1878 lhs.overflow = false;
1879 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1881 /* As excess precision is zeroed, there is no need to num_trim () as
1882 these operations cannot introduce a set bit there. */
1883 if (op == CPP_AND)
1885 lhs.low &= rhs.low;
1886 lhs.high &= rhs.high;
1888 else if (op == CPP_OR)
1890 lhs.low |= rhs.low;
1891 lhs.high |= rhs.high;
1893 else
1895 lhs.low ^= rhs.low;
1896 lhs.high ^= rhs.high;
1899 return lhs;
1902 /* Returns LHS OP RHS, where OP is an inequality. */
1903 static cpp_num
1904 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1905 enum cpp_ttype op)
1907 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1909 if (op == CPP_GREATER_EQ)
1910 lhs.low = gte;
1911 else if (op == CPP_LESS)
1912 lhs.low = !gte;
1913 else if (op == CPP_GREATER)
1914 lhs.low = gte && !num_eq (lhs, rhs);
1915 else /* CPP_LESS_EQ. */
1916 lhs.low = !gte || num_eq (lhs, rhs);
1918 lhs.high = 0;
1919 lhs.overflow = false;
1920 lhs.unsignedp = false;
1921 return lhs;
1924 /* Returns LHS OP RHS, where OP is == or !=. */
1925 static cpp_num
1926 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1927 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1929 /* Work around a 3.0.4 bug; see PR 6950. */
1930 bool eq = num_eq (lhs, rhs);
1931 if (op == CPP_NOT_EQ)
1932 eq = !eq;
1933 lhs.low = eq;
1934 lhs.high = 0;
1935 lhs.overflow = false;
1936 lhs.unsignedp = false;
1937 return lhs;
1940 /* Shift NUM, of width PRECISION, right by N bits. */
1941 static cpp_num
1942 num_rshift (cpp_num num, size_t precision, size_t n)
1944 cpp_num_part sign_mask;
1945 bool x = num_positive (num, precision);
1947 if (num.unsignedp || x)
1948 sign_mask = 0;
1949 else
1950 sign_mask = ~(cpp_num_part) 0;
1952 if (n >= precision)
1953 num.high = num.low = sign_mask;
1954 else
1956 /* Sign-extend. */
1957 if (precision < PART_PRECISION)
1958 num.high = sign_mask, num.low |= sign_mask << precision;
1959 else if (precision < 2 * PART_PRECISION)
1960 num.high |= sign_mask << (precision - PART_PRECISION);
1962 if (n >= PART_PRECISION)
1964 n -= PART_PRECISION;
1965 num.low = num.high;
1966 num.high = sign_mask;
1969 if (n)
1971 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1972 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1976 num = num_trim (num, precision);
1977 num.overflow = false;
1978 return num;
1981 /* Shift NUM, of width PRECISION, left by N bits. */
1982 static cpp_num
1983 num_lshift (cpp_num num, size_t precision, size_t n)
1985 if (n >= precision)
1987 num.overflow = !num.unsignedp && !num_zerop (num);
1988 num.high = num.low = 0;
1990 else
1992 cpp_num orig, maybe_orig;
1993 size_t m = n;
1995 orig = num;
1996 if (m >= PART_PRECISION)
1998 m -= PART_PRECISION;
1999 num.high = num.low;
2000 num.low = 0;
2002 if (m)
2004 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
2005 num.low <<= m;
2007 num = num_trim (num, precision);
2009 if (num.unsignedp)
2010 num.overflow = false;
2011 else
2013 maybe_orig = num_rshift (num, precision, n);
2014 num.overflow = !num_eq (orig, maybe_orig);
2018 return num;
2021 /* The four unary operators: +, -, ! and ~. */
2022 static cpp_num
2023 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
2025 switch (op)
2027 case CPP_UPLUS:
2028 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
2029 cpp_warning (pfile, CPP_W_TRADITIONAL,
2030 "traditional C rejects the unary plus operator");
2031 num.overflow = false;
2032 break;
2034 case CPP_UMINUS:
2035 num = num_negate (num, CPP_OPTION (pfile, precision));
2036 break;
2038 case CPP_COMPL:
2039 num.high = ~num.high;
2040 num.low = ~num.low;
2041 num = num_trim (num, CPP_OPTION (pfile, precision));
2042 num.overflow = false;
2043 break;
2045 default: /* case CPP_NOT: */
2046 num.low = num_zerop (num);
2047 num.high = 0;
2048 num.overflow = false;
2049 num.unsignedp = false;
2050 break;
2053 return num;
2056 /* The various binary operators. */
2057 static cpp_num
2058 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
2060 cpp_num result;
2061 size_t precision = CPP_OPTION (pfile, precision);
2062 size_t n;
2064 switch (op)
2066 /* Shifts. */
2067 case CPP_LSHIFT:
2068 case CPP_RSHIFT:
2069 if (!rhs.unsignedp && !num_positive (rhs, precision))
2071 /* A negative shift is a positive shift the other way. */
2072 if (op == CPP_LSHIFT)
2073 op = CPP_RSHIFT;
2074 else
2075 op = CPP_LSHIFT;
2076 rhs = num_negate (rhs, precision);
2078 if (rhs.high)
2079 n = ~0; /* Maximal. */
2080 else
2081 n = rhs.low;
2082 if (op == CPP_LSHIFT)
2083 lhs = num_lshift (lhs, precision, n);
2084 else
2085 lhs = num_rshift (lhs, precision, n);
2086 break;
2088 /* Arithmetic. */
2089 case CPP_MINUS:
2090 result.low = lhs.low - rhs.low;
2091 result.high = lhs.high - rhs.high;
2092 if (result.low > lhs.low)
2093 result.high--;
2094 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2095 result.overflow = false;
2097 result = num_trim (result, precision);
2098 if (!result.unsignedp)
2100 bool lhsp = num_positive (lhs, precision);
2101 result.overflow = (lhsp != num_positive (rhs, precision)
2102 && lhsp != num_positive (result, precision));
2104 return result;
2106 case CPP_PLUS:
2107 result.low = lhs.low + rhs.low;
2108 result.high = lhs.high + rhs.high;
2109 if (result.low < lhs.low)
2110 result.high++;
2111 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2112 result.overflow = false;
2114 result = num_trim (result, precision);
2115 if (!result.unsignedp)
2117 bool lhsp = num_positive (lhs, precision);
2118 result.overflow = (lhsp == num_positive (rhs, precision)
2119 && lhsp != num_positive (result, precision));
2121 return result;
2123 /* Comma. */
2124 default: /* case CPP_COMMA: */
2125 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
2126 || !pfile->state.skip_eval))
2127 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2128 "comma operator in operand of #%s",
2129 pfile->state.in_directive == 3
2130 ? "embed" : "if");
2131 lhs = rhs;
2132 break;
2135 return lhs;
2138 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2139 cannot overflow. */
2140 static cpp_num
2141 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2143 cpp_num result;
2144 cpp_num_part middle[2], temp;
2146 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2147 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2149 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2150 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2152 temp = result.low;
2153 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2154 if (result.low < temp)
2155 result.high++;
2157 temp = result.low;
2158 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2159 if (result.low < temp)
2160 result.high++;
2162 result.high += HIGH_PART (middle[0]);
2163 result.high += HIGH_PART (middle[1]);
2164 result.unsignedp = true;
2165 result.overflow = false;
2167 return result;
2170 /* Multiply two preprocessing numbers. */
2171 static cpp_num
2172 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2174 cpp_num result, temp;
2175 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2176 bool overflow, negate = false;
2177 size_t precision = CPP_OPTION (pfile, precision);
2179 /* Prepare for unsigned multiplication. */
2180 if (!unsignedp)
2182 if (!num_positive (lhs, precision))
2183 negate = !negate, lhs = num_negate (lhs, precision);
2184 if (!num_positive (rhs, precision))
2185 negate = !negate, rhs = num_negate (rhs, precision);
2188 overflow = lhs.high && rhs.high;
2189 result = num_part_mul (lhs.low, rhs.low);
2191 temp = num_part_mul (lhs.high, rhs.low);
2192 result.high += temp.low;
2193 if (temp.high)
2194 overflow = true;
2196 temp = num_part_mul (lhs.low, rhs.high);
2197 result.high += temp.low;
2198 if (temp.high)
2199 overflow = true;
2201 temp.low = result.low, temp.high = result.high;
2202 result = num_trim (result, precision);
2203 if (!num_eq (result, temp))
2204 overflow = true;
2206 if (negate)
2207 result = num_negate (result, precision);
2209 if (unsignedp)
2210 result.overflow = false;
2211 else
2212 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2213 && !num_zerop (result));
2214 result.unsignedp = unsignedp;
2216 return result;
2219 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2220 or the remainder depending upon OP. LOCATION is the source location
2221 of this operator (for diagnostics). */
2223 static cpp_num
2224 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2225 location_t location)
2227 cpp_num result, sub;
2228 cpp_num_part mask;
2229 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2230 bool negate = false, lhs_neg = false;
2231 size_t i, precision = CPP_OPTION (pfile, precision);
2233 /* Prepare for unsigned division. */
2234 if (!unsignedp)
2236 if (!num_positive (lhs, precision))
2237 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2238 if (!num_positive (rhs, precision))
2239 negate = !negate, rhs = num_negate (rhs, precision);
2242 /* Find the high bit. */
2243 if (rhs.high)
2245 i = precision - 1;
2246 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2247 for (; ; i--, mask >>= 1)
2248 if (rhs.high & mask)
2249 break;
2251 else if (rhs.low)
2253 if (precision > PART_PRECISION)
2254 i = precision - PART_PRECISION - 1;
2255 else
2256 i = precision - 1;
2257 mask = (cpp_num_part) 1 << i;
2258 for (; ; i--, mask >>= 1)
2259 if (rhs.low & mask)
2260 break;
2262 else
2264 if (!pfile->state.skip_eval)
2265 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2266 "division by zero in #%s",
2267 pfile->state.in_directive == 3
2268 ? "embed" : "if");
2269 lhs.unsignedp = unsignedp;
2270 return lhs;
2273 /* First nonzero bit of RHS is bit I. Do naive division by
2274 shifting the RHS fully left, and subtracting from LHS if LHS is
2275 at least as big, and then repeating but with one less shift.
2276 This is not very efficient, but is easy to understand. */
2278 rhs.unsignedp = true;
2279 lhs.unsignedp = true;
2280 i = precision - i - 1;
2281 sub = num_lshift (rhs, precision, i);
2283 result.high = result.low = 0;
2284 for (;;)
2286 if (num_greater_eq (lhs, sub, precision))
2288 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2289 if (i >= PART_PRECISION)
2290 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2291 else
2292 result.low |= (cpp_num_part) 1 << i;
2294 if (i-- == 0)
2295 break;
2296 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2297 sub.high >>= 1;
2300 /* We divide so that the remainder has the sign of the LHS. */
2301 if (op == CPP_DIV)
2303 result.unsignedp = unsignedp;
2304 result.overflow = false;
2305 if (!unsignedp)
2307 if (negate)
2308 result = num_negate (result, precision);
2309 result.overflow = (num_positive (result, precision) ^ !negate
2310 && !num_zerop (result));
2313 return result;
2316 /* CPP_MOD. */
2317 lhs.unsignedp = unsignedp;
2318 lhs.overflow = false;
2319 if (lhs_neg)
2320 lhs = num_negate (lhs, precision);
2322 return lhs;