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

source code of libcpp/expr.cc