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