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