1/* Parse C expressions for cpplib.
2 Copyright (C) 1987-2025 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
1464 /* Set up detection of #if ! defined(). */
1465 pfile->mi_ind_cmacro = 0;
1466 saw_leading_not = false;
1467 lex_count = 0;
1468
1469 /* Lowest priority operator prevents further reductions. */
1470 top->op = CPP_EOF;
1471
1472 if (pfile->state.in_directive == 3)
1473 {
1474 ++top;
1475 top->op = CPP_OPEN_PAREN;
1476 top->token = open_paren;
1477 top->loc = open_paren->src_loc;
1478 }
1479
1480 for (;;)
1481 {
1482 struct op op;
1483
1484 lex_count++;
1485 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1486 op.op = op.token->type;
1487 op.loc = virtual_location;
1488
1489 switch (op.op)
1490 {
1491 /* These tokens convert into values. */
1492 case CPP_NUMBER:
1493 case CPP_CHAR:
1494 case CPP_WCHAR:
1495 case CPP_CHAR16:
1496 case CPP_CHAR32:
1497 case CPP_UTF8CHAR:
1498 case CPP_NAME:
1499 case CPP_HASH:
1500 if (!want_value)
1501 SYNTAX_ERROR2_AT (op.loc,
1502 "missing binary operator before token %qs",
1503 cpp_token_as_text (pfile, op.token));
1504 want_value = false;
1505 top->value = eval_token (pfile, token: op.token, virtual_location: op.loc);
1506 continue;
1507
1508 case CPP_NOT:
1509 saw_leading_not = lex_count == 1;
1510 break;
1511 case CPP_PLUS:
1512 if (want_value)
1513 op.op = CPP_UPLUS;
1514 break;
1515 case CPP_MINUS:
1516 if (want_value)
1517 op.op = CPP_UMINUS;
1518 break;
1519
1520 case CPP_PADDING:
1521 lex_count--;
1522 continue;
1523
1524 default:
1525 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1526 SYNTAX_ERROR2_AT (op.loc,
1527 "token %qs is not valid in preprocessor "
1528 "expressions",
1529 cpp_token_as_text (pfile, op.token));
1530 break;
1531 }
1532
1533 /* Check we have a value or operator as appropriate. */
1534 if (optab[op.op].flags & NO_L_OPERAND)
1535 {
1536 if (!want_value)
1537 SYNTAX_ERROR2_AT (op.loc,
1538 "missing binary operator before token %qs",
1539 cpp_token_as_text (pfile, op.token));
1540 }
1541 else if (want_value)
1542 {
1543 /* We want a number (or expression) and haven't got one.
1544 Try to emit a specific diagnostic. */
1545 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1546 SYNTAX_ERROR_AT (op.loc,
1547 "missing expression between %<(%> and %<)%>");
1548
1549 if (op.op == CPP_EOF && top->op == CPP_EOF)
1550 SYNTAX_ERROR2_AT (op.loc,
1551 "%s with no expression", dir);
1552
1553 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1554 SYNTAX_ERROR2_AT (op.loc,
1555 "operator %qs has no right operand",
1556 cpp_token_as_text (pfile, top->token));
1557 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1558 /* Complain about missing paren during reduction. */;
1559 else
1560 SYNTAX_ERROR2_AT (op.loc,
1561 "operator %qs has no left operand",
1562 cpp_token_as_text (pfile, op.token));
1563 }
1564
1565 top = reduce (pfile, top, op.op);
1566 if (!top)
1567 goto syntax_error;
1568
1569 if (op.op == CPP_EOF)
1570 break;
1571
1572 switch (op.op)
1573 {
1574 case CPP_CLOSE_PAREN:
1575 if (pfile->state.in_directive == 3 && top == pfile->op_stack)
1576 goto embed_done;
1577 continue;
1578 case CPP_OR_OR:
1579 if (!num_zerop (top->value))
1580 pfile->state.skip_eval++;
1581 break;
1582 case CPP_AND_AND:
1583 case CPP_QUERY:
1584 if (num_zerop (top->value))
1585 pfile->state.skip_eval++;
1586 break;
1587 case CPP_COLON:
1588 if (top->op != CPP_QUERY)
1589 SYNTAX_ERROR_AT (op.loc,
1590 " %<:%> without preceding %<?%>");
1591 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1592 pfile->state.skip_eval++;
1593 else
1594 pfile->state.skip_eval--;
1595 default:
1596 break;
1597 }
1598
1599 want_value = true;
1600
1601 /* Check for and handle stack overflow. */
1602 if (++top == pfile->op_limit)
1603 top = _cpp_expand_op_stack (pfile);
1604
1605 top->op = op.op;
1606 top->token = op.token;
1607 top->loc = op.loc;
1608 }
1609
1610 /* The controlling macro expression is only valid if we called lex 3
1611 times: <!> <defined expression> and <EOF>. push_conditional ()
1612 checks that we are at top-of-file. */
1613 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1614 pfile->mi_ind_cmacro = 0;
1615
1616 if (top != pfile->op_stack)
1617 {
1618 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1619 msgid: "unbalanced stack in %s", dir);
1620 syntax_error:
1621 return false; /* Return false on syntax error. */
1622 }
1623
1624 if (pfile->state.in_directive == 3)
1625 {
1626 embed_done:
1627 if (num_zerop (top->value))
1628 return 0;
1629 if (!top->value.unsignedp
1630 && !num_positive (top->value, CPP_OPTION (pfile, precision)))
1631 {
1632 cpp_error_with_line (pfile, CPP_DL_ERROR, top->loc, 0,
1633 msgid: "negative embed parameter operand");
1634 return 1;
1635 }
1636 if (top->value.high)
1637 {
1638 cpp_error_with_line (pfile, CPP_DL_ERROR, top->loc, 0,
1639 msgid: "too large embed parameter operand");
1640 return 1;
1641 }
1642 return top->value.low;
1643 }
1644 return !num_zerop (top->value);
1645}
1646
1647/* Reduce the operator / value stack if possible, in preparation for
1648 pushing operator OP. Returns NULL on error, otherwise the top of
1649 the stack. */
1650static struct op *
1651reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1652{
1653 unsigned int prio;
1654
1655 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1656 {
1657 bad_op:
1658 cpp_error (pfile, CPP_DL_ICE, msgid: "impossible operator '%u'", top->op);
1659 return 0;
1660 }
1661
1662 if (op == CPP_OPEN_PAREN)
1663 return top;
1664
1665 /* Decrement the priority of left-associative operators to force a
1666 reduction with operators of otherwise equal priority. */
1667 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1668 while (prio < optab[top->op].prio)
1669 {
1670 if (CPP_OPTION (pfile, warn_num_sign_change)
1671 && optab[top->op].flags & CHECK_PROMOTION)
1672 check_promotion (pfile, top);
1673
1674 switch (top->op)
1675 {
1676 case CPP_UPLUS:
1677 case CPP_UMINUS:
1678 case CPP_NOT:
1679 case CPP_COMPL:
1680 top[-1].value = num_unary_op (pfile, top->value, top->op);
1681 top[-1].loc = top->loc;
1682 break;
1683
1684 case CPP_PLUS:
1685 case CPP_MINUS:
1686 case CPP_RSHIFT:
1687 case CPP_LSHIFT:
1688 case CPP_COMMA:
1689 top[-1].value = num_binary_op (pfile, top[-1].value,
1690 top->value, top->op);
1691 top[-1].loc = top->loc;
1692 break;
1693
1694 case CPP_GREATER:
1695 case CPP_LESS:
1696 case CPP_GREATER_EQ:
1697 case CPP_LESS_EQ:
1698 top[-1].value
1699 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1700 top[-1].loc = top->loc;
1701 break;
1702
1703 case CPP_EQ_EQ:
1704 case CPP_NOT_EQ:
1705 top[-1].value
1706 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1707 top[-1].loc = top->loc;
1708 break;
1709
1710 case CPP_AND:
1711 case CPP_OR:
1712 case CPP_XOR:
1713 top[-1].value
1714 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1715 top[-1].loc = top->loc;
1716 break;
1717
1718 case CPP_MULT:
1719 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1720 top[-1].loc = top->loc;
1721 break;
1722
1723 case CPP_DIV:
1724 case CPP_MOD:
1725 top[-1].value = num_div_op (pfile, top[-1].value,
1726 top->value, top->op, top->loc);
1727 top[-1].loc = top->loc;
1728 break;
1729
1730 case CPP_OR_OR:
1731 top--;
1732 if (!num_zerop (top->value))
1733 pfile->state.skip_eval--;
1734 top->value.low = (!num_zerop (top->value)
1735 || !num_zerop (top[1].value));
1736 top->value.high = 0;
1737 top->value.unsignedp = false;
1738 top->value.overflow = false;
1739 top->loc = top[1].loc;
1740 continue;
1741
1742 case CPP_AND_AND:
1743 top--;
1744 if (num_zerop (top->value))
1745 pfile->state.skip_eval--;
1746 top->value.low = (!num_zerop (top->value)
1747 && !num_zerop (top[1].value));
1748 top->value.high = 0;
1749 top->value.unsignedp = false;
1750 top->value.overflow = false;
1751 top->loc = top[1].loc;
1752 continue;
1753
1754 case CPP_OPEN_PAREN:
1755 if (op != CPP_CLOSE_PAREN)
1756 {
1757 cpp_error_with_line (pfile, CPP_DL_ERROR,
1758 top->token->src_loc,
1759 0, msgid: "missing %<)%> in expression");
1760 return 0;
1761 }
1762 top--;
1763 top->value = top[1].value;
1764 top->loc = top[1].loc;
1765 return top;
1766
1767 case CPP_COLON:
1768 top -= 2;
1769 if (!num_zerop (top->value))
1770 {
1771 pfile->state.skip_eval--;
1772 top->value = top[1].value;
1773 top->loc = top[1].loc;
1774 }
1775 else
1776 {
1777 top->value = top[2].value;
1778 top->loc = top[2].loc;
1779 }
1780 top->value.unsignedp = (top[1].value.unsignedp
1781 || top[2].value.unsignedp);
1782 continue;
1783
1784 case CPP_QUERY:
1785 /* COMMA and COLON should not reduce a QUERY operator. */
1786 if (op == CPP_COMMA || op == CPP_COLON)
1787 return top;
1788 cpp_error (pfile, CPP_DL_ERROR, msgid: "%<?%> without following %<:%>");
1789 return 0;
1790
1791 default:
1792 goto bad_op;
1793 }
1794
1795 top--;
1796 if (top->value.overflow && !pfile->state.skip_eval)
1797 cpp_error (pfile, CPP_DL_PEDWARN,
1798 msgid: "integer overflow in preprocessor expression");
1799 }
1800
1801 if (op == CPP_CLOSE_PAREN)
1802 {
1803 cpp_error (pfile, CPP_DL_ERROR, msgid: "missing %<(%> in expression");
1804 return 0;
1805 }
1806
1807 return top;
1808}
1809
1810/* Returns the position of the old top of stack after expansion. */
1811struct op *
1812_cpp_expand_op_stack (cpp_reader *pfile)
1813{
1814 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1815 size_t new_size = old_size * 2 + 20;
1816
1817 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1818 pfile->op_limit = pfile->op_stack + new_size;
1819
1820 return pfile->op_stack + old_size;
1821}
1822
1823/* Emits a warning if the effective sign of either operand of OP
1824 changes because of integer promotions. */
1825static void
1826check_promotion (cpp_reader *pfile, const struct op *op)
1827{
1828 if (op->value.unsignedp == op[-1].value.unsignedp)
1829 return;
1830
1831 if (op->value.unsignedp)
1832 {
1833 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1834 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1835 msgid: "the left operand of %qs changes sign when "
1836 "promoted", cpp_token_as_text (pfile, op->token));
1837 }
1838 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1839 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1840 msgid: "the right operand of %qs changes sign when promoted",
1841 cpp_token_as_text (pfile, op->token));
1842}
1843
1844/* Clears the unused high order bits of the number pointed to by PNUM. */
1845static cpp_num
1846num_trim (cpp_num num, size_t precision)
1847{
1848 if (precision > PART_PRECISION)
1849 {
1850 precision -= PART_PRECISION;
1851 if (precision < PART_PRECISION)
1852 num.high &= ((cpp_num_part) 1 << precision) - 1;
1853 }
1854 else
1855 {
1856 if (precision < PART_PRECISION)
1857 num.low &= ((cpp_num_part) 1 << precision) - 1;
1858 num.high = 0;
1859 }
1860
1861 return num;
1862}
1863
1864/* True iff A (presumed signed) >= 0. */
1865static bool
1866num_positive (cpp_num num, size_t precision)
1867{
1868 if (precision > PART_PRECISION)
1869 {
1870 precision -= PART_PRECISION;
1871 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1872 }
1873
1874 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1875}
1876
1877/* Sign extend a number, with PRECISION significant bits and all
1878 others assumed clear, to fill out a cpp_num structure. */
1879cpp_num
1880cpp_num_sign_extend (cpp_num num, size_t precision)
1881{
1882 if (!num.unsignedp)
1883 {
1884 if (precision > PART_PRECISION)
1885 {
1886 precision -= PART_PRECISION;
1887 if (precision < PART_PRECISION
1888 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1889 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1890 }
1891 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1892 {
1893 if (precision < PART_PRECISION)
1894 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1895 num.high = ~(cpp_num_part) 0;
1896 }
1897 }
1898
1899 return num;
1900}
1901
1902/* Returns the negative of NUM. */
1903static cpp_num
1904num_negate (cpp_num num, size_t precision)
1905{
1906 cpp_num copy;
1907
1908 copy = num;
1909 num.high = ~num.high;
1910 num.low = ~num.low;
1911 if (++num.low == 0)
1912 num.high++;
1913 num = num_trim (num, precision);
1914 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1915
1916 return num;
1917}
1918
1919/* Returns true if A >= B. */
1920static bool
1921num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1922{
1923 bool unsignedp;
1924
1925 unsignedp = pa.unsignedp || pb.unsignedp;
1926
1927 if (!unsignedp)
1928 {
1929 /* Both numbers have signed type. If they are of different
1930 sign, the answer is the sign of A. */
1931 unsignedp = num_positive (num: pa, precision);
1932
1933 if (unsignedp != num_positive (num: pb, precision))
1934 return unsignedp;
1935
1936 /* Otherwise we can do an unsigned comparison. */
1937 }
1938
1939 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1940}
1941
1942/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1943static cpp_num
1944num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1945 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1946{
1947 lhs.overflow = false;
1948 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1949
1950 /* As excess precision is zeroed, there is no need to num_trim () as
1951 these operations cannot introduce a set bit there. */
1952 if (op == CPP_AND)
1953 {
1954 lhs.low &= rhs.low;
1955 lhs.high &= rhs.high;
1956 }
1957 else if (op == CPP_OR)
1958 {
1959 lhs.low |= rhs.low;
1960 lhs.high |= rhs.high;
1961 }
1962 else
1963 {
1964 lhs.low ^= rhs.low;
1965 lhs.high ^= rhs.high;
1966 }
1967
1968 return lhs;
1969}
1970
1971/* Returns LHS OP RHS, where OP is an inequality. */
1972static cpp_num
1973num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1974 enum cpp_ttype op)
1975{
1976 bool gte = num_greater_eq (pa: lhs, pb: rhs, CPP_OPTION (pfile, precision));
1977
1978 if (op == CPP_GREATER_EQ)
1979 lhs.low = gte;
1980 else if (op == CPP_LESS)
1981 lhs.low = !gte;
1982 else if (op == CPP_GREATER)
1983 lhs.low = gte && !num_eq (lhs, rhs);
1984 else /* CPP_LESS_EQ. */
1985 lhs.low = !gte || num_eq (lhs, rhs);
1986
1987 lhs.high = 0;
1988 lhs.overflow = false;
1989 lhs.unsignedp = false;
1990 return lhs;
1991}
1992
1993/* Returns LHS OP RHS, where OP is == or !=. */
1994static cpp_num
1995num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1996 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1997{
1998 /* Work around a 3.0.4 bug; see PR 6950. */
1999 bool eq = num_eq (lhs, rhs);
2000 if (op == CPP_NOT_EQ)
2001 eq = !eq;
2002 lhs.low = eq;
2003 lhs.high = 0;
2004 lhs.overflow = false;
2005 lhs.unsignedp = false;
2006 return lhs;
2007}
2008
2009/* Shift NUM, of width PRECISION, right by N bits. */
2010static cpp_num
2011num_rshift (cpp_num num, size_t precision, size_t n)
2012{
2013 cpp_num_part sign_mask;
2014 bool x = num_positive (num, precision);
2015
2016 if (num.unsignedp || x)
2017 sign_mask = 0;
2018 else
2019 sign_mask = ~(cpp_num_part) 0;
2020
2021 if (n >= precision)
2022 num.high = num.low = sign_mask;
2023 else
2024 {
2025 /* Sign-extend. */
2026 if (precision < PART_PRECISION)
2027 num.high = sign_mask, num.low |= sign_mask << precision;
2028 else if (precision < 2 * PART_PRECISION)
2029 num.high |= sign_mask << (precision - PART_PRECISION);
2030
2031 if (n >= PART_PRECISION)
2032 {
2033 n -= PART_PRECISION;
2034 num.low = num.high;
2035 num.high = sign_mask;
2036 }
2037
2038 if (n)
2039 {
2040 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
2041 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
2042 }
2043 }
2044
2045 num = num_trim (num, precision);
2046 num.overflow = false;
2047 return num;
2048}
2049
2050/* Shift NUM, of width PRECISION, left by N bits. */
2051static cpp_num
2052num_lshift (cpp_reader *pfile, cpp_num num, size_t precision, size_t n)
2053{
2054 if (n >= precision)
2055 {
2056 num.overflow = !num.unsignedp && !num_zerop (num);
2057 num.high = num.low = 0;
2058 }
2059 else
2060 {
2061 cpp_num orig, maybe_orig;
2062 size_t m = n;
2063
2064 orig = num;
2065 if (m >= PART_PRECISION)
2066 {
2067 m -= PART_PRECISION;
2068 num.high = num.low;
2069 num.low = 0;
2070 }
2071 if (m)
2072 {
2073 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
2074 num.low <<= m;
2075 }
2076 num = num_trim (num, precision);
2077
2078 if (num.unsignedp
2079 /* For C++20 or later since P1236R1, there is no overflow for signed
2080 left shifts, it is as if the shift was in uintmax_t and cast
2081 back to intmax_t afterwards. */
2082 || (CPP_OPTION (pfile, cplusplus)
2083 && CPP_OPTION (pfile, lang) >= CLK_GNUCXX20))
2084 num.overflow = false;
2085 else if (CPP_OPTION (pfile, cplusplus)
2086 && CPP_OPTION (pfile, lang) >= CLK_GNUCXX11
2087 && num_positive (num: orig, precision))
2088 {
2089 /* For C++11 - C++17 since CWG1457, 1 << 63 is allowed because it is
2090 representable in uintmax_t, but 3 << 63 is not.
2091 Test whether num >> (precision - 1 - n) as logical
2092 shift is > 1. */
2093 maybe_orig = orig;
2094 maybe_orig.unsignedp = true;
2095 maybe_orig = num_rshift (num: maybe_orig, precision, n: precision - 1 - n);
2096 num.overflow = maybe_orig.high || maybe_orig.low > 1;
2097 }
2098 else
2099 {
2100 maybe_orig = num_rshift (num, precision, n);
2101 num.overflow = !num_eq (orig, maybe_orig);
2102 }
2103 }
2104
2105 return num;
2106}
2107
2108/* The four unary operators: +, -, ! and ~. */
2109static cpp_num
2110num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
2111{
2112 switch (op)
2113 {
2114 case CPP_UPLUS:
2115 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
2116 cpp_warning (pfile, CPP_W_TRADITIONAL,
2117 msgid: "traditional C rejects the unary plus operator");
2118 num.overflow = false;
2119 break;
2120
2121 case CPP_UMINUS:
2122 num = num_negate (num, CPP_OPTION (pfile, precision));
2123 break;
2124
2125 case CPP_COMPL:
2126 num.high = ~num.high;
2127 num.low = ~num.low;
2128 num = num_trim (num, CPP_OPTION (pfile, precision));
2129 num.overflow = false;
2130 break;
2131
2132 default: /* case CPP_NOT: */
2133 num.low = num_zerop (num);
2134 num.high = 0;
2135 num.overflow = false;
2136 num.unsignedp = false;
2137 break;
2138 }
2139
2140 return num;
2141}
2142
2143/* The various binary operators. */
2144static cpp_num
2145num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
2146{
2147 cpp_num result;
2148 size_t precision = CPP_OPTION (pfile, precision);
2149 size_t n;
2150
2151 switch (op)
2152 {
2153 /* Shifts. */
2154 case CPP_LSHIFT:
2155 case CPP_RSHIFT:
2156 if (!rhs.unsignedp && !num_positive (num: rhs, precision))
2157 {
2158 /* A negative shift is a positive shift the other way. */
2159 if (op == CPP_LSHIFT)
2160 op = CPP_RSHIFT;
2161 else
2162 op = CPP_LSHIFT;
2163 rhs = num_negate (num: rhs, precision);
2164 }
2165 if (rhs.high)
2166 n = ~0; /* Maximal. */
2167 else
2168 n = rhs.low;
2169 if (op == CPP_LSHIFT)
2170 lhs = num_lshift (pfile, num: lhs, precision, n);
2171 else
2172 lhs = num_rshift (num: lhs, precision, n);
2173 break;
2174
2175 /* Arithmetic. */
2176 case CPP_MINUS:
2177 result.low = lhs.low - rhs.low;
2178 result.high = lhs.high - rhs.high;
2179 if (result.low > lhs.low)
2180 result.high--;
2181 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2182 result.overflow = false;
2183
2184 result = num_trim (num: result, precision);
2185 if (!result.unsignedp)
2186 {
2187 bool lhsp = num_positive (num: lhs, precision);
2188 result.overflow = (lhsp != num_positive (num: rhs, precision)
2189 && lhsp != num_positive (num: result, precision));
2190 }
2191 return result;
2192
2193 case CPP_PLUS:
2194 result.low = lhs.low + rhs.low;
2195 result.high = lhs.high + rhs.high;
2196 if (result.low < lhs.low)
2197 result.high++;
2198 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
2199 result.overflow = false;
2200
2201 result = num_trim (num: result, precision);
2202 if (!result.unsignedp)
2203 {
2204 bool lhsp = num_positive (num: lhs, precision);
2205 result.overflow = (lhsp == num_positive (num: rhs, precision)
2206 && lhsp != num_positive (num: result, precision));
2207 }
2208 return result;
2209
2210 /* Comma. */
2211 default: /* case CPP_COMMA: */
2212 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
2213 || !pfile->state.skip_eval))
2214 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2215 msgid: "comma operator in operand of #%s",
2216 pfile->state.in_directive == 3
2217 ? "embed" : "if");
2218 lhs = rhs;
2219 break;
2220 }
2221
2222 return lhs;
2223}
2224
2225/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
2226 cannot overflow. */
2227static cpp_num
2228num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2229{
2230 cpp_num result;
2231 cpp_num_part middle[2], temp;
2232
2233 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2234 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2235
2236 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2237 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2238
2239 temp = result.low;
2240 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2241 if (result.low < temp)
2242 result.high++;
2243
2244 temp = result.low;
2245 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2246 if (result.low < temp)
2247 result.high++;
2248
2249 result.high += HIGH_PART (middle[0]);
2250 result.high += HIGH_PART (middle[1]);
2251 result.unsignedp = true;
2252 result.overflow = false;
2253
2254 return result;
2255}
2256
2257/* Multiply two preprocessing numbers. */
2258static cpp_num
2259num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2260{
2261 cpp_num result, temp;
2262 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2263 bool overflow, negate = false;
2264 size_t precision = CPP_OPTION (pfile, precision);
2265
2266 /* Prepare for unsigned multiplication. */
2267 if (!unsignedp)
2268 {
2269 if (!num_positive (num: lhs, precision))
2270 negate = !negate, lhs = num_negate (num: lhs, precision);
2271 if (!num_positive (num: rhs, precision))
2272 negate = !negate, rhs = num_negate (num: rhs, precision);
2273 }
2274
2275 overflow = lhs.high && rhs.high;
2276 result = num_part_mul (lhs: lhs.low, rhs: rhs.low);
2277
2278 temp = num_part_mul (lhs: lhs.high, rhs: rhs.low);
2279 result.high += temp.low;
2280 if (temp.high)
2281 overflow = true;
2282
2283 temp = num_part_mul (lhs: lhs.low, rhs: rhs.high);
2284 result.high += temp.low;
2285 if (temp.high)
2286 overflow = true;
2287
2288 temp.low = result.low, temp.high = result.high;
2289 result = num_trim (num: result, precision);
2290 if (!num_eq (result, temp))
2291 overflow = true;
2292
2293 if (negate)
2294 result = num_negate (num: result, precision);
2295
2296 if (unsignedp)
2297 result.overflow = false;
2298 else
2299 result.overflow = overflow || (num_positive (num: result, precision) ^ !negate
2300 && !num_zerop (result));
2301 result.unsignedp = unsignedp;
2302
2303 return result;
2304}
2305
2306/* Divide two preprocessing numbers, LHS and RHS, returning the answer
2307 or the remainder depending upon OP. LOCATION is the source location
2308 of this operator (for diagnostics). */
2309
2310static cpp_num
2311num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2312 location_t location)
2313{
2314 cpp_num result, sub;
2315 cpp_num_part mask;
2316 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2317 bool negate = false, lhs_neg = false;
2318 size_t i, precision = CPP_OPTION (pfile, precision);
2319
2320 /* Prepare for unsigned division. */
2321 if (!unsignedp)
2322 {
2323 if (!num_positive (num: lhs, precision))
2324 negate = !negate, lhs_neg = true, lhs = num_negate (num: lhs, precision);
2325 if (!num_positive (num: rhs, precision))
2326 negate = !negate, rhs = num_negate (num: rhs, precision);
2327 }
2328
2329 /* Find the high bit. */
2330 if (rhs.high)
2331 {
2332 i = precision - 1;
2333 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2334 for (; ; i--, mask >>= 1)
2335 if (rhs.high & mask)
2336 break;
2337 }
2338 else if (rhs.low)
2339 {
2340 if (precision > PART_PRECISION)
2341 i = precision - PART_PRECISION - 1;
2342 else
2343 i = precision - 1;
2344 mask = (cpp_num_part) 1 << i;
2345 for (; ; i--, mask >>= 1)
2346 if (rhs.low & mask)
2347 break;
2348 }
2349 else
2350 {
2351 if (!pfile->state.skip_eval)
2352 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2353 msgid: "division by zero in #%s",
2354 pfile->state.in_directive == 3
2355 ? "embed" : "if");
2356 lhs.unsignedp = unsignedp;
2357 return lhs;
2358 }
2359
2360 /* First nonzero bit of RHS is bit I. Do naive division by
2361 shifting the RHS fully left, and subtracting from LHS if LHS is
2362 at least as big, and then repeating but with one less shift.
2363 This is not very efficient, but is easy to understand. */
2364
2365 rhs.unsignedp = true;
2366 lhs.unsignedp = true;
2367 i = precision - i - 1;
2368 sub = num_lshift (pfile, num: rhs, precision, n: i);
2369
2370 result.high = result.low = 0;
2371 for (;;)
2372 {
2373 if (num_greater_eq (pa: lhs, pb: sub, precision))
2374 {
2375 lhs = num_binary_op (pfile, lhs, rhs: sub, op: CPP_MINUS);
2376 if (i >= PART_PRECISION)
2377 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2378 else
2379 result.low |= (cpp_num_part) 1 << i;
2380 }
2381 if (i-- == 0)
2382 break;
2383 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2384 sub.high >>= 1;
2385 }
2386
2387 /* We divide so that the remainder has the sign of the LHS. */
2388 if (op == CPP_DIV)
2389 {
2390 result.unsignedp = unsignedp;
2391 result.overflow = false;
2392 if (!unsignedp)
2393 {
2394 if (negate)
2395 result = num_negate (num: result, precision);
2396 result.overflow = (num_positive (num: result, precision) ^ !negate
2397 && !num_zerop (result));
2398 }
2399
2400 return result;
2401 }
2402
2403 /* CPP_MOD. */
2404 lhs.unsignedp = unsignedp;
2405 lhs.overflow = false;
2406 if (lhs_neg)
2407 lhs = num_negate (num: lhs, precision);
2408
2409 return lhs;
2410}
2411
2412

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libcpp/expr.cc