2014-09-29 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libcpp / expr.c
blob003fcb0560ecb6812130a77ee9719541a8bfbfc8
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "internal.h"
24 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
25 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
26 #define LOW_PART(num_part) (num_part & HALF_MASK)
27 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
29 struct op
31 const cpp_token *token; /* The token forming op (for diagnostics). */
32 cpp_num value; /* The value logically "right" of op. */
33 source_location loc; /* The location of this value. */
34 enum cpp_ttype op;
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive (cpp_num, size_t);
41 static bool num_greater_eq (cpp_num, cpp_num, size_t);
42 static cpp_num num_trim (cpp_num, size_t);
43 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
45 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
46 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
47 static cpp_num num_negate (cpp_num, size_t);
48 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype);
51 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
54 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
55 source_location);
56 static cpp_num num_lshift (cpp_num, size_t, size_t);
57 static cpp_num num_rshift (cpp_num, size_t, size_t);
59 static cpp_num append_digit (cpp_num, int, int, size_t);
60 static cpp_num parse_defined (cpp_reader *);
61 static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
62 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63 static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
64 static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
65 static void check_promotion (cpp_reader *, const struct op *);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
78 #define SYNTAX_ERROR_AT(loc, msgid) \
79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
80 while(0)
81 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
83 while(0)
85 /* Subroutine of cpp_classify_number. S points to a float suffix of
86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
87 flag vector describing the suffix. */
88 static unsigned int
89 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
91 size_t flags;
92 size_t f, d, l, w, q, i;
94 flags = 0;
95 f = d = l = w = q = i = 0;
97 /* Process decimal float suffixes, which are two letters starting
98 with d or D. Order and case are significant. */
99 if (len == 2 && (*s == 'd' || *s == 'D'))
101 bool uppercase = (*s == 'D');
102 switch (s[1])
104 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
105 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
106 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
107 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
108 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
109 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
110 default:
111 /* Additional two-character suffixes beginning with D are not
112 for decimal float constants. */
113 break;
117 if (CPP_OPTION (pfile, ext_numeric_literals))
119 /* Recognize a fixed-point suffix. */
120 if (len != 0)
121 switch (s[len-1])
123 case 'k': case 'K': flags = CPP_N_ACCUM; break;
124 case 'r': case 'R': flags = CPP_N_FRACT; break;
125 default: break;
128 /* Continue processing a fixed-point suffix. The suffix is case
129 insensitive except for ll or LL. Order is significant. */
130 if (flags)
132 if (len == 1)
133 return flags;
134 len--;
136 if (*s == 'u' || *s == 'U')
138 flags |= CPP_N_UNSIGNED;
139 if (len == 1)
140 return flags;
141 len--;
142 s++;
145 switch (*s)
147 case 'h': case 'H':
148 if (len == 1)
149 return flags |= CPP_N_SMALL;
150 break;
151 case 'l':
152 if (len == 1)
153 return flags |= CPP_N_MEDIUM;
154 if (len == 2 && s[1] == 'l')
155 return flags |= CPP_N_LARGE;
156 break;
157 case 'L':
158 if (len == 1)
159 return flags |= CPP_N_MEDIUM;
160 if (len == 2 && s[1] == 'L')
161 return flags |= CPP_N_LARGE;
162 break;
163 default:
164 break;
166 /* Anything left at this point is invalid. */
167 return 0;
171 /* In any remaining valid suffix, the case and order don't matter. */
172 while (len--)
173 switch (s[len])
175 case 'f': case 'F': f++; break;
176 case 'd': case 'D': d++; break;
177 case 'l': case 'L': l++; break;
178 case 'w': case 'W': w++; break;
179 case 'q': case 'Q': q++; break;
180 case 'i': case 'I':
181 case 'j': case 'J': i++; break;
182 default:
183 return 0;
186 if (f + d + l + w + q > 1 || i > 1)
187 return 0;
189 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
190 return 0;
192 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
193 return 0;
195 return ((i ? CPP_N_IMAGINARY : 0)
196 | (f ? CPP_N_SMALL :
197 d ? CPP_N_MEDIUM :
198 l ? CPP_N_LARGE :
199 w ? CPP_N_MD_W :
200 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
203 /* Return the classification flags for a float suffix. */
204 unsigned int
205 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
207 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
210 /* Subroutine of cpp_classify_number. S points to an integer suffix
211 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
212 flag vector describing the suffix. */
213 static unsigned int
214 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
216 size_t u, l, i;
218 u = l = i = 0;
220 while (len--)
221 switch (s[len])
223 case 'u': case 'U': u++; break;
224 case 'i': case 'I':
225 case 'j': case 'J': i++; break;
226 case 'l': case 'L': l++;
227 /* If there are two Ls, they must be adjacent and the same case. */
228 if (l == 2 && s[len] != s[len + 1])
229 return 0;
230 break;
231 default:
232 return 0;
235 if (l > 2 || u > 1 || i > 1)
236 return 0;
238 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
239 return 0;
241 return ((i ? CPP_N_IMAGINARY : 0)
242 | (u ? CPP_N_UNSIGNED : 0)
243 | ((l == 0) ? CPP_N_SMALL
244 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
247 /* Return the classification flags for an int suffix. */
248 unsigned int
249 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
251 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
254 /* Return the string type corresponding to the the input user-defined string
255 literal type. If the input type is not a user-defined string literal
256 type return the input type. */
257 enum cpp_ttype
258 cpp_userdef_string_remove_type (enum cpp_ttype type)
260 if (type == CPP_STRING_USERDEF)
261 return CPP_STRING;
262 else if (type == CPP_WSTRING_USERDEF)
263 return CPP_WSTRING;
264 else if (type == CPP_STRING16_USERDEF)
265 return CPP_STRING16;
266 else if (type == CPP_STRING32_USERDEF)
267 return CPP_STRING32;
268 else if (type == CPP_UTF8STRING_USERDEF)
269 return CPP_UTF8STRING;
270 else
271 return type;
274 /* Return the user-defined string literal type corresponding to the input
275 string type. If the input type is not a string type return the input
276 type. */
277 enum cpp_ttype
278 cpp_userdef_string_add_type (enum cpp_ttype type)
280 if (type == CPP_STRING)
281 return CPP_STRING_USERDEF;
282 else if (type == CPP_WSTRING)
283 return CPP_WSTRING_USERDEF;
284 else if (type == CPP_STRING16)
285 return CPP_STRING16_USERDEF;
286 else if (type == CPP_STRING32)
287 return CPP_STRING32_USERDEF;
288 else if (type == CPP_UTF8STRING)
289 return CPP_UTF8STRING_USERDEF;
290 else
291 return type;
294 /* Return the char type corresponding to the the input user-defined char
295 literal type. If the input type is not a user-defined char literal
296 type return the input type. */
297 enum cpp_ttype
298 cpp_userdef_char_remove_type (enum cpp_ttype type)
300 if (type == CPP_CHAR_USERDEF)
301 return CPP_CHAR;
302 else if (type == CPP_WCHAR_USERDEF)
303 return CPP_WCHAR;
304 else if (type == CPP_CHAR16_USERDEF)
305 return CPP_CHAR16;
306 else if (type == CPP_CHAR32_USERDEF)
307 return CPP_CHAR32;
308 else
309 return type;
312 /* Return the user-defined char literal type corresponding to the input
313 char type. If the input type is not a char type return the input
314 type. */
315 enum cpp_ttype
316 cpp_userdef_char_add_type (enum cpp_ttype type)
318 if (type == CPP_CHAR)
319 return CPP_CHAR_USERDEF;
320 else if (type == CPP_WCHAR)
321 return CPP_WCHAR_USERDEF;
322 else if (type == CPP_CHAR16)
323 return CPP_CHAR16_USERDEF;
324 else if (type == CPP_CHAR32)
325 return CPP_CHAR32_USERDEF;
326 else
327 return type;
330 /* Return true if the token type is a user-defined string literal. */
331 bool
332 cpp_userdef_string_p (enum cpp_ttype type)
334 if (type == CPP_STRING_USERDEF
335 || type == CPP_WSTRING_USERDEF
336 || type == CPP_STRING16_USERDEF
337 || type == CPP_STRING32_USERDEF
338 || type == CPP_UTF8STRING_USERDEF)
339 return true;
340 else
341 return false;
344 /* Return true if the token type is a user-defined char literal. */
345 bool
346 cpp_userdef_char_p (enum cpp_ttype type)
348 if (type == CPP_CHAR_USERDEF
349 || type == CPP_WCHAR_USERDEF
350 || type == CPP_CHAR16_USERDEF
351 || type == CPP_CHAR32_USERDEF)
352 return true;
353 else
354 return false;
357 /* Extract the suffix from a user-defined literal string or char. */
358 const char *
359 cpp_get_userdef_suffix (const cpp_token *tok)
361 unsigned int len = tok->val.str.len;
362 const char *text = (const char *)tok->val.str.text;
363 char delim;
364 unsigned int i;
365 for (i = 0; i < len; ++i)
366 if (text[i] == '\'' || text[i] == '"')
367 break;
368 if (i == len)
369 return text + len;
370 delim = text[i];
371 for (i = len; i > 0; --i)
372 if (text[i - 1] == delim)
373 break;
374 return text + i;
377 /* Categorize numeric constants according to their field (integer,
378 floating point, or invalid), radix (decimal, octal, hexadecimal),
379 and type suffixes.
381 TOKEN is the token that represents the numeric constant to
382 classify.
384 In C++0X if UD_SUFFIX is non null it will be assigned
385 any unrecognized suffix for a user-defined literal.
387 VIRTUAL_LOCATION is the virtual location for TOKEN. */
388 unsigned int
389 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
390 const char **ud_suffix, source_location virtual_location)
392 const uchar *str = token->val.str.text;
393 const uchar *limit;
394 unsigned int max_digit, result, radix;
395 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
396 bool seen_digit;
397 bool seen_digit_sep;
399 if (ud_suffix)
400 *ud_suffix = NULL;
402 /* If the lexer has done its job, length one can only be a single
403 digit. Fast-path this very common case. */
404 if (token->val.str.len == 1)
405 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
407 limit = str + token->val.str.len;
408 float_flag = NOT_FLOAT;
409 max_digit = 0;
410 radix = 10;
411 seen_digit = false;
412 seen_digit_sep = false;
414 /* First, interpret the radix. */
415 if (*str == '0')
417 radix = 8;
418 str++;
420 /* Require at least one hex digit to classify it as hex. */
421 if (*str == 'x' || *str == 'X')
423 if (str[1] == '.' || ISXDIGIT (str[1]))
425 radix = 16;
426 str++;
428 else if (DIGIT_SEP (str[1]))
429 SYNTAX_ERROR_AT (virtual_location,
430 "digit separator after base indicator");
432 else if (*str == 'b' || *str == 'B')
434 if (str[1] == '0' || str[1] == '1')
436 radix = 2;
437 str++;
439 else if (DIGIT_SEP (str[1]))
440 SYNTAX_ERROR_AT (virtual_location,
441 "digit separator after base indicator");
445 /* Now scan for a well-formed integer or float. */
446 for (;;)
448 unsigned int c = *str++;
450 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
452 seen_digit_sep = false;
453 seen_digit = true;
454 c = hex_value (c);
455 if (c > max_digit)
456 max_digit = c;
458 else if (DIGIT_SEP (c))
460 if (seen_digit_sep)
461 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
462 seen_digit_sep = true;
464 else if (c == '.')
466 if (seen_digit_sep || DIGIT_SEP (*str))
467 SYNTAX_ERROR_AT (virtual_location,
468 "digit separator adjacent to decimal point");
469 seen_digit_sep = false;
470 if (float_flag == NOT_FLOAT)
471 float_flag = AFTER_POINT;
472 else
473 SYNTAX_ERROR_AT (virtual_location,
474 "too many decimal points in number");
476 else if ((radix <= 10 && (c == 'e' || c == 'E'))
477 || (radix == 16 && (c == 'p' || c == 'P')))
479 if (seen_digit_sep || DIGIT_SEP (*str))
480 SYNTAX_ERROR_AT (virtual_location,
481 "digit separator adjacent to exponent");
482 float_flag = AFTER_EXPON;
483 break;
485 else
487 /* Start of suffix. */
488 str--;
489 break;
493 if (seen_digit_sep && float_flag != AFTER_EXPON)
494 SYNTAX_ERROR_AT (virtual_location,
495 "digit separator outside digit sequence");
497 /* The suffix may be for decimal fixed-point constants without exponent. */
498 if (radix != 16 && float_flag == NOT_FLOAT)
500 result = interpret_float_suffix (pfile, str, limit - str);
501 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
503 result |= CPP_N_FLOATING;
504 /* We need to restore the radix to 10, if the radix is 8. */
505 if (radix == 8)
506 radix = 10;
508 if (CPP_PEDANTIC (pfile))
509 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
510 "fixed-point constants are a GCC extension");
511 goto syntax_ok;
513 else
514 result = 0;
517 if (float_flag != NOT_FLOAT && radix == 8)
518 radix = 10;
520 if (max_digit >= radix)
522 if (radix == 2)
523 SYNTAX_ERROR2_AT (virtual_location,
524 "invalid digit \"%c\" in binary constant", '0' + max_digit);
525 else
526 SYNTAX_ERROR2_AT (virtual_location,
527 "invalid digit \"%c\" in octal constant", '0' + max_digit);
530 if (float_flag != NOT_FLOAT)
532 if (radix == 2)
534 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
535 "invalid prefix \"0b\" for floating constant");
536 return CPP_N_INVALID;
539 if (radix == 16 && !seen_digit)
540 SYNTAX_ERROR_AT (virtual_location,
541 "no digits in hexadecimal floating constant");
543 if (radix == 16 && CPP_PEDANTIC (pfile)
544 && !CPP_OPTION (pfile, extended_numbers))
546 if (CPP_OPTION (pfile, cplusplus))
547 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
548 "use of C++11 hexadecimal floating constant");
549 else
550 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
551 "use of C99 hexadecimal floating constant");
554 if (float_flag == AFTER_EXPON)
556 if (*str == '+' || *str == '-')
557 str++;
559 /* Exponent is decimal, even if string is a hex float. */
560 if (!ISDIGIT (*str))
562 if (DIGIT_SEP (*str))
563 SYNTAX_ERROR_AT (virtual_location,
564 "digit separator adjacent to exponent");
565 else
566 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
570 seen_digit_sep = DIGIT_SEP (*str);
571 str++;
573 while (ISDIGIT (*str) || DIGIT_SEP (*str));
575 else if (radix == 16)
576 SYNTAX_ERROR_AT (virtual_location,
577 "hexadecimal floating constants require an exponent");
579 if (seen_digit_sep)
580 SYNTAX_ERROR_AT (virtual_location,
581 "digit separator outside digit sequence");
583 result = interpret_float_suffix (pfile, str, limit - str);
584 if (result == 0)
586 if (CPP_OPTION (pfile, user_literals))
588 if (ud_suffix)
589 *ud_suffix = (const char *) str;
590 result = CPP_N_LARGE | CPP_N_USERDEF;
592 else
594 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
595 "invalid suffix \"%.*s\" on floating constant",
596 (int) (limit - str), str);
597 return CPP_N_INVALID;
601 /* Traditional C didn't accept any floating suffixes. */
602 if (limit != str
603 && CPP_WTRADITIONAL (pfile)
604 && ! cpp_sys_macro_p (pfile))
605 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
606 "traditional C rejects the \"%.*s\" suffix",
607 (int) (limit - str), str);
609 /* A suffix for double is a GCC extension via decimal float support.
610 If the suffix also specifies an imaginary value we'll catch that
611 later. */
612 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
613 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
614 "suffix for double constant is a GCC extension");
616 /* Radix must be 10 for decimal floats. */
617 if ((result & CPP_N_DFLOAT) && radix != 10)
619 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
620 "invalid suffix \"%.*s\" with hexadecimal floating constant",
621 (int) (limit - str), str);
622 return CPP_N_INVALID;
625 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
626 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
627 "fixed-point constants are a GCC extension");
629 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
630 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
631 "decimal float constants are a GCC extension");
633 result |= CPP_N_FLOATING;
635 else
637 result = interpret_int_suffix (pfile, str, limit - str);
638 if (result == 0)
640 if (CPP_OPTION (pfile, user_literals))
642 if (ud_suffix)
643 *ud_suffix = (const char *) str;
644 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
646 else
648 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
649 "invalid suffix \"%.*s\" on integer constant",
650 (int) (limit - str), str);
651 return CPP_N_INVALID;
655 /* Traditional C only accepted the 'L' suffix.
656 Suppress warning about 'LL' with -Wno-long-long. */
657 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
659 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
660 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
661 && CPP_OPTION (pfile, cpp_warn_long_long);
663 if (u_or_i || large)
664 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
665 virtual_location, 0,
666 "traditional C rejects the \"%.*s\" suffix",
667 (int) (limit - str), str);
670 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
671 && CPP_OPTION (pfile, cpp_warn_long_long))
673 const char *message = CPP_OPTION (pfile, cplusplus)
674 ? N_("use of C++11 long long integer constant")
675 : N_("use of C99 long long integer constant");
677 if (CPP_OPTION (pfile, c99))
678 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
679 0, message);
680 else
681 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
682 virtual_location, 0, message);
685 result |= CPP_N_INTEGER;
688 syntax_ok:
689 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
690 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
691 "imaginary constants are a GCC extension");
692 if (radix == 2
693 && !CPP_OPTION (pfile, binary_constants)
694 && CPP_PEDANTIC (pfile))
695 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
696 CPP_OPTION (pfile, cplusplus)
697 ? "binary constants are a C++14 feature "
698 "or GCC extension"
699 : "binary constants are a GCC extension");
701 if (radix == 10)
702 result |= CPP_N_DECIMAL;
703 else if (radix == 16)
704 result |= CPP_N_HEX;
705 else if (radix == 2)
706 result |= CPP_N_BINARY;
707 else
708 result |= CPP_N_OCTAL;
710 return result;
712 syntax_error:
713 return CPP_N_INVALID;
716 /* cpp_interpret_integer converts an integer constant into a cpp_num,
717 of precision options->precision.
719 We do not provide any interface for decimal->float conversion,
720 because the preprocessor doesn't need it and we don't want to
721 drag in GCC's floating point emulator. */
722 cpp_num
723 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
724 unsigned int type)
726 const uchar *p, *end;
727 cpp_num result;
729 result.low = 0;
730 result.high = 0;
731 result.unsignedp = !!(type & CPP_N_UNSIGNED);
732 result.overflow = false;
734 p = token->val.str.text;
735 end = p + token->val.str.len;
737 /* Common case of a single digit. */
738 if (token->val.str.len == 1)
739 result.low = p[0] - '0';
740 else
742 cpp_num_part max;
743 size_t precision = CPP_OPTION (pfile, precision);
744 unsigned int base = 10, c = 0;
745 bool overflow = false;
747 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
749 base = 8;
750 p++;
752 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
754 base = 16;
755 p += 2;
757 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
759 base = 2;
760 p += 2;
763 /* We can add a digit to numbers strictly less than this without
764 needing the precision and slowness of double integers. */
765 max = ~(cpp_num_part) 0;
766 if (precision < PART_PRECISION)
767 max >>= PART_PRECISION - precision;
768 max = (max - base + 1) / base + 1;
770 for (; p < end; p++)
772 c = *p;
774 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
775 c = hex_value (c);
776 else if (DIGIT_SEP (c))
777 continue;
778 else
779 break;
781 /* Strict inequality for when max is set to zero. */
782 if (result.low < max)
783 result.low = result.low * base + c;
784 else
786 result = append_digit (result, c, base, precision);
787 overflow |= result.overflow;
788 max = 0;
792 if (overflow && !(type & CPP_N_USERDEF))
793 cpp_error (pfile, CPP_DL_PEDWARN,
794 "integer constant is too large for its type");
795 /* If too big to be signed, consider it unsigned. Only warn for
796 decimal numbers. Traditional numbers were always signed (but
797 we still honor an explicit U suffix); but we only have
798 traditional semantics in directives. */
799 else if (!result.unsignedp
800 && !(CPP_OPTION (pfile, traditional)
801 && pfile->state.in_directive)
802 && !num_positive (result, precision))
804 /* This is for constants within the range of uintmax_t but
805 not that of intmax_t. For such decimal constants, a
806 diagnostic is required for C99 as the selected type must
807 be signed and not having a type is a constraint violation
808 (DR#298, TC3), so this must be a pedwarn. For C90,
809 unsigned long is specified to be used for a constant that
810 does not fit in signed long; if uintmax_t has the same
811 range as unsigned long this means only a warning is
812 appropriate here. C90 permits the preprocessor to use a
813 wider range than unsigned long in the compiler, so if
814 uintmax_t is wider than unsigned long no diagnostic is
815 required for such constants in preprocessor #if
816 expressions and the compiler will pedwarn for such
817 constants outside the range of unsigned long that reach
818 the compiler so a diagnostic is not required there
819 either; thus, pedwarn for C99 but use a plain warning for
820 C90. */
821 if (base == 10)
822 cpp_error (pfile, (CPP_OPTION (pfile, c99)
823 ? CPP_DL_PEDWARN
824 : CPP_DL_WARNING),
825 "integer constant is so large that it is unsigned");
826 result.unsignedp = true;
830 return result;
833 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
834 static cpp_num
835 append_digit (cpp_num num, int digit, int base, size_t precision)
837 cpp_num result;
838 unsigned int shift;
839 bool overflow;
840 cpp_num_part add_high, add_low;
842 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
843 need to worry about add_high overflowing. */
844 switch (base)
846 case 2:
847 shift = 1;
848 break;
850 case 16:
851 shift = 4;
852 break;
854 default:
855 shift = 3;
857 overflow = !!(num.high >> (PART_PRECISION - shift));
858 result.high = num.high << shift;
859 result.low = num.low << shift;
860 result.high |= num.low >> (PART_PRECISION - shift);
861 result.unsignedp = num.unsignedp;
863 if (base == 10)
865 add_low = num.low << 1;
866 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
868 else
869 add_high = add_low = 0;
871 if (add_low + digit < add_low)
872 add_high++;
873 add_low += digit;
875 if (result.low + add_low < result.low)
876 add_high++;
877 if (result.high + add_high < result.high)
878 overflow = true;
880 result.low += add_low;
881 result.high += add_high;
882 result.overflow = overflow;
884 /* The above code catches overflow of a cpp_num type. This catches
885 overflow of the (possibly shorter) target precision. */
886 num.low = result.low;
887 num.high = result.high;
888 result = num_trim (result, precision);
889 if (!num_eq (result, num))
890 result.overflow = true;
892 return result;
895 /* Handle meeting "defined" in a preprocessor expression. */
896 static cpp_num
897 parse_defined (cpp_reader *pfile)
899 cpp_num result;
900 int paren = 0;
901 cpp_hashnode *node = 0;
902 const cpp_token *token;
903 cpp_context *initial_context = pfile->context;
905 /* Don't expand macros. */
906 pfile->state.prevent_expansion++;
908 token = cpp_get_token (pfile);
909 if (token->type == CPP_OPEN_PAREN)
911 paren = 1;
912 token = cpp_get_token (pfile);
915 if (token->type == CPP_NAME)
917 node = token->val.node.node;
918 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
920 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
921 node = 0;
924 else
926 cpp_error (pfile, CPP_DL_ERROR,
927 "operator \"defined\" requires an identifier");
928 if (token->flags & NAMED_OP)
930 cpp_token op;
932 op.flags = 0;
933 op.type = token->type;
934 cpp_error (pfile, CPP_DL_ERROR,
935 "(\"%s\" is an alternative token for \"%s\" in C++)",
936 cpp_token_as_text (pfile, token),
937 cpp_token_as_text (pfile, &op));
941 if (node)
943 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
944 cpp_error (pfile, CPP_DL_WARNING,
945 "this use of \"defined\" may not be portable");
947 _cpp_mark_macro_used (node);
948 if (!(node->flags & NODE_USED))
950 node->flags |= NODE_USED;
951 if (node->type == NT_MACRO)
953 if ((node->flags & NODE_BUILTIN)
954 && pfile->cb.user_builtin_macro)
955 pfile->cb.user_builtin_macro (pfile, node);
956 if (pfile->cb.used_define)
957 pfile->cb.used_define (pfile, pfile->directive_line, node);
959 else
961 if (pfile->cb.used_undef)
962 pfile->cb.used_undef (pfile, pfile->directive_line, node);
966 /* A possible controlling macro of the form #if !defined ().
967 _cpp_parse_expr checks there was no other junk on the line. */
968 pfile->mi_ind_cmacro = node;
971 pfile->state.prevent_expansion--;
973 /* Do not treat conditional macros as being defined. This is due to the
974 powerpc and spu ports using conditional macros for 'vector', 'bool', and
975 'pixel' to act as conditional keywords. This messes up tests like #ifndef
976 bool. */
977 result.unsignedp = false;
978 result.high = 0;
979 result.overflow = false;
980 result.low = (node && node->type == NT_MACRO
981 && (node->flags & NODE_CONDITIONAL) == 0);
982 return result;
985 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
986 number or character constant, or the result of the "defined" or "#"
987 operators). */
988 static cpp_num
989 eval_token (cpp_reader *pfile, const cpp_token *token,
990 source_location virtual_location)
992 cpp_num result;
993 unsigned int temp;
994 int unsignedp = 0;
996 result.unsignedp = false;
997 result.overflow = false;
999 switch (token->type)
1001 case CPP_NUMBER:
1002 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1003 if (temp & CPP_N_USERDEF)
1004 cpp_error (pfile, CPP_DL_ERROR,
1005 "user-defined literal in preprocessor expression");
1006 switch (temp & CPP_N_CATEGORY)
1008 case CPP_N_FLOATING:
1009 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1010 "floating constant in preprocessor expression");
1011 break;
1012 case CPP_N_INTEGER:
1013 if (!(temp & CPP_N_IMAGINARY))
1014 return cpp_interpret_integer (pfile, token, temp);
1015 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1016 "imaginary number in preprocessor expression");
1017 break;
1019 case CPP_N_INVALID:
1020 /* Error already issued. */
1021 break;
1023 result.high = result.low = 0;
1024 break;
1026 case CPP_WCHAR:
1027 case CPP_CHAR:
1028 case CPP_CHAR16:
1029 case CPP_CHAR32:
1031 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1032 &temp, &unsignedp);
1034 result.high = 0;
1035 result.low = cc;
1036 /* Sign-extend the result if necessary. */
1037 if (!unsignedp && (cppchar_signed_t) cc < 0)
1039 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1040 result.low |= ~(~(cpp_num_part) 0
1041 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1042 result.high = ~(cpp_num_part) 0;
1043 result = num_trim (result, CPP_OPTION (pfile, precision));
1046 break;
1048 case CPP_NAME:
1049 if (token->val.node.node == pfile->spec_nodes.n_defined)
1050 return parse_defined (pfile);
1051 else if (CPP_OPTION (pfile, cplusplus)
1052 && (token->val.node.node == pfile->spec_nodes.n_true
1053 || token->val.node.node == pfile->spec_nodes.n_false))
1055 result.high = 0;
1056 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1058 else
1060 result.high = 0;
1061 result.low = 0;
1062 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1063 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1064 "\"%s\" is not defined",
1065 NODE_NAME (token->val.node.node));
1067 break;
1069 case CPP_HASH:
1070 if (!pfile->state.skipping)
1072 /* A pedantic warning takes precedence over a deprecated
1073 warning here. */
1074 if (CPP_PEDANTIC (pfile))
1075 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1076 virtual_location, 0,
1077 "assertions are a GCC extension");
1078 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1079 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1080 "assertions are a deprecated extension");
1082 _cpp_test_assertion (pfile, &temp);
1083 result.high = 0;
1084 result.low = temp;
1085 break;
1087 default:
1088 abort ();
1091 result.unsignedp = !!unsignedp;
1092 return result;
1095 /* Operator precedence and flags table.
1097 After an operator is returned from the lexer, if it has priority less
1098 than the operator on the top of the stack, we reduce the stack by one
1099 operator and repeat the test. Since equal priorities do not reduce,
1100 this is naturally right-associative.
1102 We handle left-associative operators by decrementing the priority of
1103 just-lexed operators by one, but retaining the priority of operators
1104 already on the stack.
1106 The remaining cases are '(' and ')'. We handle '(' by skipping the
1107 reduction phase completely. ')' is given lower priority than
1108 everything else, including '(', effectively forcing a reduction of the
1109 parenthesized expression. If there is a matching '(', the routine
1110 reduce() exits immediately. If the normal exit route sees a ')', then
1111 there cannot have been a matching '(' and an error message is output.
1113 The parser assumes all shifted operators require a left operand unless
1114 the flag NO_L_OPERAND is set. These semantics are automatic; any
1115 extra semantics need to be handled with operator-specific code. */
1117 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1118 operand changes because of integer promotions. */
1119 #define NO_L_OPERAND (1 << 0)
1120 #define LEFT_ASSOC (1 << 1)
1121 #define CHECK_PROMOTION (1 << 2)
1123 /* Operator to priority map. Must be in the same order as the first
1124 N entries of enum cpp_ttype. */
1125 static const struct cpp_operator
1127 uchar prio;
1128 uchar flags;
1129 } optab[] =
1131 /* EQ */ {0, 0}, /* Shouldn't happen. */
1132 /* NOT */ {16, NO_L_OPERAND},
1133 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1134 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1135 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1136 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1137 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1138 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1139 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1140 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1141 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1142 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1143 /* RSHIFT */ {13, LEFT_ASSOC},
1144 /* LSHIFT */ {13, LEFT_ASSOC},
1146 /* COMPL */ {16, NO_L_OPERAND},
1147 /* AND_AND */ {6, LEFT_ASSOC},
1148 /* OR_OR */ {5, LEFT_ASSOC},
1149 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1150 However, there are some special cases for these in reduce(). */
1151 /* QUERY */ {4, 0},
1152 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1153 /* COMMA */ {4, LEFT_ASSOC},
1154 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1155 /* CLOSE_PAREN */ {0, 0},
1156 /* EOF */ {0, 0},
1157 /* EQ_EQ */ {11, LEFT_ASSOC},
1158 /* NOT_EQ */ {11, LEFT_ASSOC},
1159 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1160 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1161 /* UPLUS */ {16, NO_L_OPERAND},
1162 /* UMINUS */ {16, NO_L_OPERAND}
1165 /* Parse and evaluate a C expression, reading from PFILE.
1166 Returns the truth value of the expression.
1168 The implementation is an operator precedence parser, i.e. a
1169 bottom-up parser, using a stack for not-yet-reduced tokens.
1171 The stack base is op_stack, and the current stack pointer is 'top'.
1172 There is a stack element for each operator (only), and the most
1173 recently pushed operator is 'top->op'. An operand (value) is
1174 stored in the 'value' field of the stack element of the operator
1175 that precedes it. */
1176 bool
1177 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1179 struct op *top = pfile->op_stack;
1180 unsigned int lex_count;
1181 bool saw_leading_not, want_value = true;
1182 source_location virtual_location = 0;
1184 pfile->state.skip_eval = 0;
1186 /* Set up detection of #if ! defined(). */
1187 pfile->mi_ind_cmacro = 0;
1188 saw_leading_not = false;
1189 lex_count = 0;
1191 /* Lowest priority operator prevents further reductions. */
1192 top->op = CPP_EOF;
1194 for (;;)
1196 struct op op;
1198 lex_count++;
1199 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1200 op.op = op.token->type;
1201 op.loc = virtual_location;
1203 switch (op.op)
1205 /* These tokens convert into values. */
1206 case CPP_NUMBER:
1207 case CPP_CHAR:
1208 case CPP_WCHAR:
1209 case CPP_CHAR16:
1210 case CPP_CHAR32:
1211 case CPP_NAME:
1212 case CPP_HASH:
1213 if (!want_value)
1214 SYNTAX_ERROR2_AT (op.loc,
1215 "missing binary operator before token \"%s\"",
1216 cpp_token_as_text (pfile, op.token));
1217 want_value = false;
1218 top->value = eval_token (pfile, op.token, op.loc);
1219 continue;
1221 case CPP_NOT:
1222 saw_leading_not = lex_count == 1;
1223 break;
1224 case CPP_PLUS:
1225 if (want_value)
1226 op.op = CPP_UPLUS;
1227 break;
1228 case CPP_MINUS:
1229 if (want_value)
1230 op.op = CPP_UMINUS;
1231 break;
1233 default:
1234 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1235 SYNTAX_ERROR2_AT (op.loc,
1236 "token \"%s\" is not valid in preprocessor expressions",
1237 cpp_token_as_text (pfile, op.token));
1238 break;
1241 /* Check we have a value or operator as appropriate. */
1242 if (optab[op.op].flags & NO_L_OPERAND)
1244 if (!want_value)
1245 SYNTAX_ERROR2_AT (op.loc,
1246 "missing binary operator before token \"%s\"",
1247 cpp_token_as_text (pfile, op.token));
1249 else if (want_value)
1251 /* We want a number (or expression) and haven't got one.
1252 Try to emit a specific diagnostic. */
1253 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1254 SYNTAX_ERROR_AT (op.loc,
1255 "missing expression between '(' and ')'");
1257 if (op.op == CPP_EOF && top->op == CPP_EOF)
1258 SYNTAX_ERROR2_AT (op.loc,
1259 "%s with no expression", is_if ? "#if" : "#elif");
1261 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1262 SYNTAX_ERROR2_AT (op.loc,
1263 "operator '%s' has no right operand",
1264 cpp_token_as_text (pfile, top->token));
1265 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1266 /* Complain about missing paren during reduction. */;
1267 else
1268 SYNTAX_ERROR2_AT (op.loc,
1269 "operator '%s' has no left operand",
1270 cpp_token_as_text (pfile, op.token));
1273 top = reduce (pfile, top, op.op);
1274 if (!top)
1275 goto syntax_error;
1277 if (op.op == CPP_EOF)
1278 break;
1280 switch (op.op)
1282 case CPP_CLOSE_PAREN:
1283 continue;
1284 case CPP_OR_OR:
1285 if (!num_zerop (top->value))
1286 pfile->state.skip_eval++;
1287 break;
1288 case CPP_AND_AND:
1289 case CPP_QUERY:
1290 if (num_zerop (top->value))
1291 pfile->state.skip_eval++;
1292 break;
1293 case CPP_COLON:
1294 if (top->op != CPP_QUERY)
1295 SYNTAX_ERROR_AT (op.loc,
1296 " ':' without preceding '?'");
1297 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1298 pfile->state.skip_eval++;
1299 else
1300 pfile->state.skip_eval--;
1301 default:
1302 break;
1305 want_value = true;
1307 /* Check for and handle stack overflow. */
1308 if (++top == pfile->op_limit)
1309 top = _cpp_expand_op_stack (pfile);
1311 top->op = op.op;
1312 top->token = op.token;
1313 top->loc = op.loc;
1316 /* The controlling macro expression is only valid if we called lex 3
1317 times: <!> <defined expression> and <EOF>. push_conditional ()
1318 checks that we are at top-of-file. */
1319 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1320 pfile->mi_ind_cmacro = 0;
1322 if (top != pfile->op_stack)
1324 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1325 "unbalanced stack in %s",
1326 is_if ? "#if" : "#elif");
1327 syntax_error:
1328 return false; /* Return false on syntax error. */
1331 return !num_zerop (top->value);
1334 /* Reduce the operator / value stack if possible, in preparation for
1335 pushing operator OP. Returns NULL on error, otherwise the top of
1336 the stack. */
1337 static struct op *
1338 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1340 unsigned int prio;
1342 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1344 bad_op:
1345 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1346 return 0;
1349 if (op == CPP_OPEN_PAREN)
1350 return top;
1352 /* Decrement the priority of left-associative operators to force a
1353 reduction with operators of otherwise equal priority. */
1354 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1355 while (prio < optab[top->op].prio)
1357 if (CPP_OPTION (pfile, warn_num_sign_change)
1358 && optab[top->op].flags & CHECK_PROMOTION)
1359 check_promotion (pfile, top);
1361 switch (top->op)
1363 case CPP_UPLUS:
1364 case CPP_UMINUS:
1365 case CPP_NOT:
1366 case CPP_COMPL:
1367 top[-1].value = num_unary_op (pfile, top->value, top->op);
1368 top[-1].loc = top->loc;
1369 break;
1371 case CPP_PLUS:
1372 case CPP_MINUS:
1373 case CPP_RSHIFT:
1374 case CPP_LSHIFT:
1375 case CPP_COMMA:
1376 top[-1].value = num_binary_op (pfile, top[-1].value,
1377 top->value, top->op);
1378 top[-1].loc = top->loc;
1379 break;
1381 case CPP_GREATER:
1382 case CPP_LESS:
1383 case CPP_GREATER_EQ:
1384 case CPP_LESS_EQ:
1385 top[-1].value
1386 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1387 top[-1].loc = top->loc;
1388 break;
1390 case CPP_EQ_EQ:
1391 case CPP_NOT_EQ:
1392 top[-1].value
1393 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1394 top[-1].loc = top->loc;
1395 break;
1397 case CPP_AND:
1398 case CPP_OR:
1399 case CPP_XOR:
1400 top[-1].value
1401 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1402 top[-1].loc = top->loc;
1403 break;
1405 case CPP_MULT:
1406 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1407 top[-1].loc = top->loc;
1408 break;
1410 case CPP_DIV:
1411 case CPP_MOD:
1412 top[-1].value = num_div_op (pfile, top[-1].value,
1413 top->value, top->op, top->loc);
1414 top[-1].loc = top->loc;
1415 break;
1417 case CPP_OR_OR:
1418 top--;
1419 if (!num_zerop (top->value))
1420 pfile->state.skip_eval--;
1421 top->value.low = (!num_zerop (top->value)
1422 || !num_zerop (top[1].value));
1423 top->value.high = 0;
1424 top->value.unsignedp = false;
1425 top->value.overflow = false;
1426 top->loc = top[1].loc;
1427 continue;
1429 case CPP_AND_AND:
1430 top--;
1431 if (num_zerop (top->value))
1432 pfile->state.skip_eval--;
1433 top->value.low = (!num_zerop (top->value)
1434 && !num_zerop (top[1].value));
1435 top->value.high = 0;
1436 top->value.unsignedp = false;
1437 top->value.overflow = false;
1438 top->loc = top[1].loc;
1439 continue;
1441 case CPP_OPEN_PAREN:
1442 if (op != CPP_CLOSE_PAREN)
1444 cpp_error_with_line (pfile, CPP_DL_ERROR,
1445 top->token->src_loc,
1446 0, "missing ')' in expression");
1447 return 0;
1449 top--;
1450 top->value = top[1].value;
1451 top->loc = top[1].loc;
1452 return top;
1454 case CPP_COLON:
1455 top -= 2;
1456 if (!num_zerop (top->value))
1458 pfile->state.skip_eval--;
1459 top->value = top[1].value;
1460 top->loc = top[1].loc;
1462 else
1464 top->value = top[2].value;
1465 top->loc = top[2].loc;
1467 top->value.unsignedp = (top[1].value.unsignedp
1468 || top[2].value.unsignedp);
1469 continue;
1471 case CPP_QUERY:
1472 /* COMMA and COLON should not reduce a QUERY operator. */
1473 if (op == CPP_COMMA || op == CPP_COLON)
1474 return top;
1475 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1476 return 0;
1478 default:
1479 goto bad_op;
1482 top--;
1483 if (top->value.overflow && !pfile->state.skip_eval)
1484 cpp_error (pfile, CPP_DL_PEDWARN,
1485 "integer overflow in preprocessor expression");
1488 if (op == CPP_CLOSE_PAREN)
1490 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1491 return 0;
1494 return top;
1497 /* Returns the position of the old top of stack after expansion. */
1498 struct op *
1499 _cpp_expand_op_stack (cpp_reader *pfile)
1501 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1502 size_t new_size = old_size * 2 + 20;
1504 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1505 pfile->op_limit = pfile->op_stack + new_size;
1507 return pfile->op_stack + old_size;
1510 /* Emits a warning if the effective sign of either operand of OP
1511 changes because of integer promotions. */
1512 static void
1513 check_promotion (cpp_reader *pfile, const struct op *op)
1515 if (op->value.unsignedp == op[-1].value.unsignedp)
1516 return;
1518 if (op->value.unsignedp)
1520 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1521 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1522 "the left operand of \"%s\" changes sign when promoted",
1523 cpp_token_as_text (pfile, op->token));
1525 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1526 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1527 "the right operand of \"%s\" changes sign when promoted",
1528 cpp_token_as_text (pfile, op->token));
1531 /* Clears the unused high order bits of the number pointed to by PNUM. */
1532 static cpp_num
1533 num_trim (cpp_num num, size_t precision)
1535 if (precision > PART_PRECISION)
1537 precision -= PART_PRECISION;
1538 if (precision < PART_PRECISION)
1539 num.high &= ((cpp_num_part) 1 << precision) - 1;
1541 else
1543 if (precision < PART_PRECISION)
1544 num.low &= ((cpp_num_part) 1 << precision) - 1;
1545 num.high = 0;
1548 return num;
1551 /* True iff A (presumed signed) >= 0. */
1552 static bool
1553 num_positive (cpp_num num, size_t precision)
1555 if (precision > PART_PRECISION)
1557 precision -= PART_PRECISION;
1558 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1561 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1564 /* Sign extend a number, with PRECISION significant bits and all
1565 others assumed clear, to fill out a cpp_num structure. */
1566 cpp_num
1567 cpp_num_sign_extend (cpp_num num, size_t precision)
1569 if (!num.unsignedp)
1571 if (precision > PART_PRECISION)
1573 precision -= PART_PRECISION;
1574 if (precision < PART_PRECISION
1575 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1576 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1578 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1580 if (precision < PART_PRECISION)
1581 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1582 num.high = ~(cpp_num_part) 0;
1586 return num;
1589 /* Returns the negative of NUM. */
1590 static cpp_num
1591 num_negate (cpp_num num, size_t precision)
1593 cpp_num copy;
1595 copy = num;
1596 num.high = ~num.high;
1597 num.low = ~num.low;
1598 if (++num.low == 0)
1599 num.high++;
1600 num = num_trim (num, precision);
1601 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1603 return num;
1606 /* Returns true if A >= B. */
1607 static bool
1608 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1610 bool unsignedp;
1612 unsignedp = pa.unsignedp || pb.unsignedp;
1614 if (!unsignedp)
1616 /* Both numbers have signed type. If they are of different
1617 sign, the answer is the sign of A. */
1618 unsignedp = num_positive (pa, precision);
1620 if (unsignedp != num_positive (pb, precision))
1621 return unsignedp;
1623 /* Otherwise we can do an unsigned comparison. */
1626 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1629 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1630 static cpp_num
1631 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1632 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1634 lhs.overflow = false;
1635 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1637 /* As excess precision is zeroed, there is no need to num_trim () as
1638 these operations cannot introduce a set bit there. */
1639 if (op == CPP_AND)
1641 lhs.low &= rhs.low;
1642 lhs.high &= rhs.high;
1644 else if (op == CPP_OR)
1646 lhs.low |= rhs.low;
1647 lhs.high |= rhs.high;
1649 else
1651 lhs.low ^= rhs.low;
1652 lhs.high ^= rhs.high;
1655 return lhs;
1658 /* Returns LHS OP RHS, where OP is an inequality. */
1659 static cpp_num
1660 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1661 enum cpp_ttype op)
1663 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1665 if (op == CPP_GREATER_EQ)
1666 lhs.low = gte;
1667 else if (op == CPP_LESS)
1668 lhs.low = !gte;
1669 else if (op == CPP_GREATER)
1670 lhs.low = gte && !num_eq (lhs, rhs);
1671 else /* CPP_LESS_EQ. */
1672 lhs.low = !gte || num_eq (lhs, rhs);
1674 lhs.high = 0;
1675 lhs.overflow = false;
1676 lhs.unsignedp = false;
1677 return lhs;
1680 /* Returns LHS OP RHS, where OP is == or !=. */
1681 static cpp_num
1682 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1683 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1685 /* Work around a 3.0.4 bug; see PR 6950. */
1686 bool eq = num_eq (lhs, rhs);
1687 if (op == CPP_NOT_EQ)
1688 eq = !eq;
1689 lhs.low = eq;
1690 lhs.high = 0;
1691 lhs.overflow = false;
1692 lhs.unsignedp = false;
1693 return lhs;
1696 /* Shift NUM, of width PRECISION, right by N bits. */
1697 static cpp_num
1698 num_rshift (cpp_num num, size_t precision, size_t n)
1700 cpp_num_part sign_mask;
1701 bool x = num_positive (num, precision);
1703 if (num.unsignedp || x)
1704 sign_mask = 0;
1705 else
1706 sign_mask = ~(cpp_num_part) 0;
1708 if (n >= precision)
1709 num.high = num.low = sign_mask;
1710 else
1712 /* Sign-extend. */
1713 if (precision < PART_PRECISION)
1714 num.high = sign_mask, num.low |= sign_mask << precision;
1715 else if (precision < 2 * PART_PRECISION)
1716 num.high |= sign_mask << (precision - PART_PRECISION);
1718 if (n >= PART_PRECISION)
1720 n -= PART_PRECISION;
1721 num.low = num.high;
1722 num.high = sign_mask;
1725 if (n)
1727 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1728 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1732 num = num_trim (num, precision);
1733 num.overflow = false;
1734 return num;
1737 /* Shift NUM, of width PRECISION, left by N bits. */
1738 static cpp_num
1739 num_lshift (cpp_num num, size_t precision, size_t n)
1741 if (n >= precision)
1743 num.overflow = !num.unsignedp && !num_zerop (num);
1744 num.high = num.low = 0;
1746 else
1748 cpp_num orig, maybe_orig;
1749 size_t m = n;
1751 orig = num;
1752 if (m >= PART_PRECISION)
1754 m -= PART_PRECISION;
1755 num.high = num.low;
1756 num.low = 0;
1758 if (m)
1760 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1761 num.low <<= m;
1763 num = num_trim (num, precision);
1765 if (num.unsignedp)
1766 num.overflow = false;
1767 else
1769 maybe_orig = num_rshift (num, precision, n);
1770 num.overflow = !num_eq (orig, maybe_orig);
1774 return num;
1777 /* The four unary operators: +, -, ! and ~. */
1778 static cpp_num
1779 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1781 switch (op)
1783 case CPP_UPLUS:
1784 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1785 cpp_warning (pfile, CPP_W_TRADITIONAL,
1786 "traditional C rejects the unary plus operator");
1787 num.overflow = false;
1788 break;
1790 case CPP_UMINUS:
1791 num = num_negate (num, CPP_OPTION (pfile, precision));
1792 break;
1794 case CPP_COMPL:
1795 num.high = ~num.high;
1796 num.low = ~num.low;
1797 num = num_trim (num, CPP_OPTION (pfile, precision));
1798 num.overflow = false;
1799 break;
1801 default: /* case CPP_NOT: */
1802 num.low = num_zerop (num);
1803 num.high = 0;
1804 num.overflow = false;
1805 num.unsignedp = false;
1806 break;
1809 return num;
1812 /* The various binary operators. */
1813 static cpp_num
1814 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1816 cpp_num result;
1817 size_t precision = CPP_OPTION (pfile, precision);
1818 size_t n;
1820 switch (op)
1822 /* Shifts. */
1823 case CPP_LSHIFT:
1824 case CPP_RSHIFT:
1825 if (!rhs.unsignedp && !num_positive (rhs, precision))
1827 /* A negative shift is a positive shift the other way. */
1828 if (op == CPP_LSHIFT)
1829 op = CPP_RSHIFT;
1830 else
1831 op = CPP_LSHIFT;
1832 rhs = num_negate (rhs, precision);
1834 if (rhs.high)
1835 n = ~0; /* Maximal. */
1836 else
1837 n = rhs.low;
1838 if (op == CPP_LSHIFT)
1839 lhs = num_lshift (lhs, precision, n);
1840 else
1841 lhs = num_rshift (lhs, precision, n);
1842 break;
1844 /* Arithmetic. */
1845 case CPP_MINUS:
1846 result.low = lhs.low - rhs.low;
1847 result.high = lhs.high - rhs.high;
1848 if (result.low > lhs.low)
1849 result.high--;
1850 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1851 result.overflow = false;
1853 result = num_trim (result, precision);
1854 if (!result.unsignedp)
1856 bool lhsp = num_positive (lhs, precision);
1857 result.overflow = (lhsp != num_positive (rhs, precision)
1858 && lhsp != num_positive (result, precision));
1860 return result;
1862 case CPP_PLUS:
1863 result.low = lhs.low + rhs.low;
1864 result.high = lhs.high + rhs.high;
1865 if (result.low < lhs.low)
1866 result.high++;
1867 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1868 result.overflow = false;
1870 result = num_trim (result, precision);
1871 if (!result.unsignedp)
1873 bool lhsp = num_positive (lhs, precision);
1874 result.overflow = (lhsp == num_positive (rhs, precision)
1875 && lhsp != num_positive (result, precision));
1877 return result;
1879 /* Comma. */
1880 default: /* case CPP_COMMA: */
1881 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1882 || !pfile->state.skip_eval))
1883 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1884 "comma operator in operand of #if");
1885 lhs = rhs;
1886 break;
1889 return lhs;
1892 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1893 cannot overflow. */
1894 static cpp_num
1895 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1897 cpp_num result;
1898 cpp_num_part middle[2], temp;
1900 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1901 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1903 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1904 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1906 temp = result.low;
1907 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1908 if (result.low < temp)
1909 result.high++;
1911 temp = result.low;
1912 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1913 if (result.low < temp)
1914 result.high++;
1916 result.high += HIGH_PART (middle[0]);
1917 result.high += HIGH_PART (middle[1]);
1918 result.unsignedp = true;
1919 result.overflow = false;
1921 return result;
1924 /* Multiply two preprocessing numbers. */
1925 static cpp_num
1926 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1928 cpp_num result, temp;
1929 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1930 bool overflow, negate = false;
1931 size_t precision = CPP_OPTION (pfile, precision);
1933 /* Prepare for unsigned multiplication. */
1934 if (!unsignedp)
1936 if (!num_positive (lhs, precision))
1937 negate = !negate, lhs = num_negate (lhs, precision);
1938 if (!num_positive (rhs, precision))
1939 negate = !negate, rhs = num_negate (rhs, precision);
1942 overflow = lhs.high && rhs.high;
1943 result = num_part_mul (lhs.low, rhs.low);
1945 temp = num_part_mul (lhs.high, rhs.low);
1946 result.high += temp.low;
1947 if (temp.high)
1948 overflow = true;
1950 temp = num_part_mul (lhs.low, rhs.high);
1951 result.high += temp.low;
1952 if (temp.high)
1953 overflow = true;
1955 temp.low = result.low, temp.high = result.high;
1956 result = num_trim (result, precision);
1957 if (!num_eq (result, temp))
1958 overflow = true;
1960 if (negate)
1961 result = num_negate (result, precision);
1963 if (unsignedp)
1964 result.overflow = false;
1965 else
1966 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1967 && !num_zerop (result));
1968 result.unsignedp = unsignedp;
1970 return result;
1973 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1974 or the remainder depending upon OP. LOCATION is the source location
1975 of this operator (for diagnostics). */
1977 static cpp_num
1978 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1979 source_location location)
1981 cpp_num result, sub;
1982 cpp_num_part mask;
1983 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1984 bool negate = false, lhs_neg = false;
1985 size_t i, precision = CPP_OPTION (pfile, precision);
1987 /* Prepare for unsigned division. */
1988 if (!unsignedp)
1990 if (!num_positive (lhs, precision))
1991 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1992 if (!num_positive (rhs, precision))
1993 negate = !negate, rhs = num_negate (rhs, precision);
1996 /* Find the high bit. */
1997 if (rhs.high)
1999 i = precision - 1;
2000 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2001 for (; ; i--, mask >>= 1)
2002 if (rhs.high & mask)
2003 break;
2005 else if (rhs.low)
2007 if (precision > PART_PRECISION)
2008 i = precision - PART_PRECISION - 1;
2009 else
2010 i = precision - 1;
2011 mask = (cpp_num_part) 1 << i;
2012 for (; ; i--, mask >>= 1)
2013 if (rhs.low & mask)
2014 break;
2016 else
2018 if (!pfile->state.skip_eval)
2019 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2020 "division by zero in #if");
2021 return lhs;
2024 /* First nonzero bit of RHS is bit I. Do naive division by
2025 shifting the RHS fully left, and subtracting from LHS if LHS is
2026 at least as big, and then repeating but with one less shift.
2027 This is not very efficient, but is easy to understand. */
2029 rhs.unsignedp = true;
2030 lhs.unsignedp = true;
2031 i = precision - i - 1;
2032 sub = num_lshift (rhs, precision, i);
2034 result.high = result.low = 0;
2035 for (;;)
2037 if (num_greater_eq (lhs, sub, precision))
2039 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2040 if (i >= PART_PRECISION)
2041 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2042 else
2043 result.low |= (cpp_num_part) 1 << i;
2045 if (i-- == 0)
2046 break;
2047 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2048 sub.high >>= 1;
2051 /* We divide so that the remainder has the sign of the LHS. */
2052 if (op == CPP_DIV)
2054 result.unsignedp = unsignedp;
2055 result.overflow = false;
2056 if (!unsignedp)
2058 if (negate)
2059 result = num_negate (result, precision);
2060 result.overflow = (num_positive (result, precision) ^ !negate
2061 && !num_zerop (result));
2064 return result;
2067 /* CPP_MOD. */
2068 lhs.unsignedp = unsignedp;
2069 lhs.overflow = false;
2070 if (lhs_neg)
2071 lhs = num_negate (lhs, precision);
2073 return lhs;