* xvasprintf.c: New file.
[official-gcc.git] / libcpp / expr.c
blob529709c8560f4b2ceabb2ec07f1b6c201cda155f
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 static cpp_num parse_has_include (cpp_reader *, enum include_type);
68 static cpp_num parse_has_attribute (cpp_reader *);
70 /* Token type abuse to create unary plus and minus operators. */
71 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
72 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
74 /* With -O2, gcc appears to produce nice code, moving the error
75 message load and subsequent jump completely out of the main path. */
76 #define SYNTAX_ERROR(msgid) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
78 #define SYNTAX_ERROR2(msgid, arg) \
79 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
80 while(0)
81 #define SYNTAX_ERROR_AT(loc, msgid) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
83 while(0)
84 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
85 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
86 while(0)
88 /* Subroutine of cpp_classify_number. S points to a float suffix of
89 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
90 flag vector describing the suffix. */
91 static unsigned int
92 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
94 size_t flags;
95 size_t f, d, l, w, q, i;
97 flags = 0;
98 f = d = l = w = q = i = 0;
100 /* Process decimal float suffixes, which are two letters starting
101 with d or D. Order and case are significant. */
102 if (len == 2 && (*s == 'd' || *s == 'D'))
104 bool uppercase = (*s == 'D');
105 switch (s[1])
107 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
108 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
109 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
110 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
111 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
112 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
113 default:
114 /* Additional two-character suffixes beginning with D are not
115 for decimal float constants. */
116 break;
120 if (CPP_OPTION (pfile, ext_numeric_literals))
122 /* Recognize a fixed-point suffix. */
123 if (len != 0)
124 switch (s[len-1])
126 case 'k': case 'K': flags = CPP_N_ACCUM; break;
127 case 'r': case 'R': flags = CPP_N_FRACT; break;
128 default: break;
131 /* Continue processing a fixed-point suffix. The suffix is case
132 insensitive except for ll or LL. Order is significant. */
133 if (flags)
135 if (len == 1)
136 return flags;
137 len--;
139 if (*s == 'u' || *s == 'U')
141 flags |= CPP_N_UNSIGNED;
142 if (len == 1)
143 return flags;
144 len--;
145 s++;
148 switch (*s)
150 case 'h': case 'H':
151 if (len == 1)
152 return flags |= CPP_N_SMALL;
153 break;
154 case 'l':
155 if (len == 1)
156 return flags |= CPP_N_MEDIUM;
157 if (len == 2 && s[1] == 'l')
158 return flags |= CPP_N_LARGE;
159 break;
160 case 'L':
161 if (len == 1)
162 return flags |= CPP_N_MEDIUM;
163 if (len == 2 && s[1] == 'L')
164 return flags |= CPP_N_LARGE;
165 break;
166 default:
167 break;
169 /* Anything left at this point is invalid. */
170 return 0;
174 /* In any remaining valid suffix, the case and order don't matter. */
175 while (len--)
176 switch (s[len])
178 case 'f': case 'F': f++; break;
179 case 'd': case 'D': d++; break;
180 case 'l': case 'L': l++; break;
181 case 'w': case 'W': w++; break;
182 case 'q': case 'Q': q++; break;
183 case 'i': case 'I':
184 case 'j': case 'J': i++; break;
185 default:
186 return 0;
189 if (f + d + l + w + q > 1 || i > 1)
190 return 0;
192 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
193 return 0;
195 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
196 return 0;
198 return ((i ? CPP_N_IMAGINARY : 0)
199 | (f ? CPP_N_SMALL :
200 d ? CPP_N_MEDIUM :
201 l ? CPP_N_LARGE :
202 w ? CPP_N_MD_W :
203 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
206 /* Return the classification flags for a float suffix. */
207 unsigned int
208 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
210 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
213 /* Subroutine of cpp_classify_number. S points to an integer suffix
214 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
215 flag vector describing the suffix. */
216 static unsigned int
217 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
219 size_t u, l, i;
221 u = l = i = 0;
223 while (len--)
224 switch (s[len])
226 case 'u': case 'U': u++; break;
227 case 'i': case 'I':
228 case 'j': case 'J': i++; break;
229 case 'l': case 'L': l++;
230 /* If there are two Ls, they must be adjacent and the same case. */
231 if (l == 2 && s[len] != s[len + 1])
232 return 0;
233 break;
234 default:
235 return 0;
238 if (l > 2 || u > 1 || i > 1)
239 return 0;
241 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
242 return 0;
244 return ((i ? CPP_N_IMAGINARY : 0)
245 | (u ? CPP_N_UNSIGNED : 0)
246 | ((l == 0) ? CPP_N_SMALL
247 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
250 /* Return the classification flags for an int suffix. */
251 unsigned int
252 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
254 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
257 /* Return the string type corresponding to the the input user-defined string
258 literal type. If the input type is not a user-defined string literal
259 type return the input type. */
260 enum cpp_ttype
261 cpp_userdef_string_remove_type (enum cpp_ttype type)
263 if (type == CPP_STRING_USERDEF)
264 return CPP_STRING;
265 else if (type == CPP_WSTRING_USERDEF)
266 return CPP_WSTRING;
267 else if (type == CPP_STRING16_USERDEF)
268 return CPP_STRING16;
269 else if (type == CPP_STRING32_USERDEF)
270 return CPP_STRING32;
271 else if (type == CPP_UTF8STRING_USERDEF)
272 return CPP_UTF8STRING;
273 else
274 return type;
277 /* Return the user-defined string literal type corresponding to the input
278 string type. If the input type is not a string type return the input
279 type. */
280 enum cpp_ttype
281 cpp_userdef_string_add_type (enum cpp_ttype type)
283 if (type == CPP_STRING)
284 return CPP_STRING_USERDEF;
285 else if (type == CPP_WSTRING)
286 return CPP_WSTRING_USERDEF;
287 else if (type == CPP_STRING16)
288 return CPP_STRING16_USERDEF;
289 else if (type == CPP_STRING32)
290 return CPP_STRING32_USERDEF;
291 else if (type == CPP_UTF8STRING)
292 return CPP_UTF8STRING_USERDEF;
293 else
294 return type;
297 /* Return the char type corresponding to the the input user-defined char
298 literal type. If the input type is not a user-defined char literal
299 type return the input type. */
300 enum cpp_ttype
301 cpp_userdef_char_remove_type (enum cpp_ttype type)
303 if (type == CPP_CHAR_USERDEF)
304 return CPP_CHAR;
305 else if (type == CPP_WCHAR_USERDEF)
306 return CPP_WCHAR;
307 else if (type == CPP_CHAR16_USERDEF)
308 return CPP_CHAR16;
309 else if (type == CPP_CHAR32_USERDEF)
310 return CPP_CHAR32;
311 else
312 return type;
315 /* Return the user-defined char literal type corresponding to the input
316 char type. If the input type is not a char type return the input
317 type. */
318 enum cpp_ttype
319 cpp_userdef_char_add_type (enum cpp_ttype type)
321 if (type == CPP_CHAR)
322 return CPP_CHAR_USERDEF;
323 else if (type == CPP_WCHAR)
324 return CPP_WCHAR_USERDEF;
325 else if (type == CPP_CHAR16)
326 return CPP_CHAR16_USERDEF;
327 else if (type == CPP_CHAR32)
328 return CPP_CHAR32_USERDEF;
329 else
330 return type;
333 /* Return true if the token type is a user-defined string literal. */
334 bool
335 cpp_userdef_string_p (enum cpp_ttype type)
337 if (type == CPP_STRING_USERDEF
338 || type == CPP_WSTRING_USERDEF
339 || type == CPP_STRING16_USERDEF
340 || type == CPP_STRING32_USERDEF
341 || type == CPP_UTF8STRING_USERDEF)
342 return true;
343 else
344 return false;
347 /* Return true if the token type is a user-defined char literal. */
348 bool
349 cpp_userdef_char_p (enum cpp_ttype type)
351 if (type == CPP_CHAR_USERDEF
352 || type == CPP_WCHAR_USERDEF
353 || type == CPP_CHAR16_USERDEF
354 || type == CPP_CHAR32_USERDEF)
355 return true;
356 else
357 return false;
360 /* Extract the suffix from a user-defined literal string or char. */
361 const char *
362 cpp_get_userdef_suffix (const cpp_token *tok)
364 unsigned int len = tok->val.str.len;
365 const char *text = (const char *)tok->val.str.text;
366 char delim;
367 unsigned int i;
368 for (i = 0; i < len; ++i)
369 if (text[i] == '\'' || text[i] == '"')
370 break;
371 if (i == len)
372 return text + len;
373 delim = text[i];
374 for (i = len; i > 0; --i)
375 if (text[i - 1] == delim)
376 break;
377 return text + i;
380 /* Categorize numeric constants according to their field (integer,
381 floating point, or invalid), radix (decimal, octal, hexadecimal),
382 and type suffixes.
384 TOKEN is the token that represents the numeric constant to
385 classify.
387 In C++0X if UD_SUFFIX is non null it will be assigned
388 any unrecognized suffix for a user-defined literal.
390 VIRTUAL_LOCATION is the virtual location for TOKEN. */
391 unsigned int
392 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
393 const char **ud_suffix, source_location virtual_location)
395 const uchar *str = token->val.str.text;
396 const uchar *limit;
397 unsigned int max_digit, result, radix;
398 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
399 bool seen_digit;
400 bool seen_digit_sep;
402 if (ud_suffix)
403 *ud_suffix = NULL;
405 /* If the lexer has done its job, length one can only be a single
406 digit. Fast-path this very common case. */
407 if (token->val.str.len == 1)
408 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
410 limit = str + token->val.str.len;
411 float_flag = NOT_FLOAT;
412 max_digit = 0;
413 radix = 10;
414 seen_digit = false;
415 seen_digit_sep = false;
417 /* First, interpret the radix. */
418 if (*str == '0')
420 radix = 8;
421 str++;
423 /* Require at least one hex digit to classify it as hex. */
424 if (*str == 'x' || *str == 'X')
426 if (str[1] == '.' || ISXDIGIT (str[1]))
428 radix = 16;
429 str++;
431 else if (DIGIT_SEP (str[1]))
432 SYNTAX_ERROR_AT (virtual_location,
433 "digit separator after base indicator");
435 else if (*str == 'b' || *str == 'B')
437 if (str[1] == '0' || str[1] == '1')
439 radix = 2;
440 str++;
442 else if (DIGIT_SEP (str[1]))
443 SYNTAX_ERROR_AT (virtual_location,
444 "digit separator after base indicator");
448 /* Now scan for a well-formed integer or float. */
449 for (;;)
451 unsigned int c = *str++;
453 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
455 seen_digit_sep = false;
456 seen_digit = true;
457 c = hex_value (c);
458 if (c > max_digit)
459 max_digit = c;
461 else if (DIGIT_SEP (c))
463 if (seen_digit_sep)
464 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
465 seen_digit_sep = true;
467 else if (c == '.')
469 if (seen_digit_sep || DIGIT_SEP (*str))
470 SYNTAX_ERROR_AT (virtual_location,
471 "digit separator adjacent to decimal point");
472 seen_digit_sep = false;
473 if (float_flag == NOT_FLOAT)
474 float_flag = AFTER_POINT;
475 else
476 SYNTAX_ERROR_AT (virtual_location,
477 "too many decimal points in number");
479 else if ((radix <= 10 && (c == 'e' || c == 'E'))
480 || (radix == 16 && (c == 'p' || c == 'P')))
482 if (seen_digit_sep || DIGIT_SEP (*str))
483 SYNTAX_ERROR_AT (virtual_location,
484 "digit separator adjacent to exponent");
485 float_flag = AFTER_EXPON;
486 break;
488 else
490 /* Start of suffix. */
491 str--;
492 break;
496 if (seen_digit_sep && float_flag != AFTER_EXPON)
497 SYNTAX_ERROR_AT (virtual_location,
498 "digit separator outside digit sequence");
500 /* The suffix may be for decimal fixed-point constants without exponent. */
501 if (radix != 16 && float_flag == NOT_FLOAT)
503 result = interpret_float_suffix (pfile, str, limit - str);
504 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
506 result |= CPP_N_FLOATING;
507 /* We need to restore the radix to 10, if the radix is 8. */
508 if (radix == 8)
509 radix = 10;
511 if (CPP_PEDANTIC (pfile))
512 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
513 "fixed-point constants are a GCC extension");
514 goto syntax_ok;
516 else
517 result = 0;
520 if (float_flag != NOT_FLOAT && radix == 8)
521 radix = 10;
523 if (max_digit >= radix)
525 if (radix == 2)
526 SYNTAX_ERROR2_AT (virtual_location,
527 "invalid digit \"%c\" in binary constant", '0' + max_digit);
528 else
529 SYNTAX_ERROR2_AT (virtual_location,
530 "invalid digit \"%c\" in octal constant", '0' + max_digit);
533 if (float_flag != NOT_FLOAT)
535 if (radix == 2)
537 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
538 "invalid prefix \"0b\" for floating constant");
539 return CPP_N_INVALID;
542 if (radix == 16 && !seen_digit)
543 SYNTAX_ERROR_AT (virtual_location,
544 "no digits in hexadecimal floating constant");
546 if (radix == 16 && CPP_PEDANTIC (pfile)
547 && !CPP_OPTION (pfile, extended_numbers))
549 if (CPP_OPTION (pfile, cplusplus))
550 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
551 "use of C++11 hexadecimal floating constant");
552 else
553 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
554 "use of C99 hexadecimal floating constant");
557 if (float_flag == AFTER_EXPON)
559 if (*str == '+' || *str == '-')
560 str++;
562 /* Exponent is decimal, even if string is a hex float. */
563 if (!ISDIGIT (*str))
565 if (DIGIT_SEP (*str))
566 SYNTAX_ERROR_AT (virtual_location,
567 "digit separator adjacent to exponent");
568 else
569 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
573 seen_digit_sep = DIGIT_SEP (*str);
574 str++;
576 while (ISDIGIT (*str) || DIGIT_SEP (*str));
578 else if (radix == 16)
579 SYNTAX_ERROR_AT (virtual_location,
580 "hexadecimal floating constants require an exponent");
582 if (seen_digit_sep)
583 SYNTAX_ERROR_AT (virtual_location,
584 "digit separator outside digit sequence");
586 result = interpret_float_suffix (pfile, str, limit - str);
587 if (result == 0)
589 if (CPP_OPTION (pfile, user_literals))
591 if (ud_suffix)
592 *ud_suffix = (const char *) str;
593 result = CPP_N_LARGE | CPP_N_USERDEF;
595 else
597 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
598 "invalid suffix \"%.*s\" on floating constant",
599 (int) (limit - str), str);
600 return CPP_N_INVALID;
604 /* Traditional C didn't accept any floating suffixes. */
605 if (limit != str
606 && CPP_WTRADITIONAL (pfile)
607 && ! cpp_sys_macro_p (pfile))
608 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
609 "traditional C rejects the \"%.*s\" suffix",
610 (int) (limit - str), str);
612 /* A suffix for double is a GCC extension via decimal float support.
613 If the suffix also specifies an imaginary value we'll catch that
614 later. */
615 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
616 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
617 "suffix for double constant is a GCC extension");
619 /* Radix must be 10 for decimal floats. */
620 if ((result & CPP_N_DFLOAT) && radix != 10)
622 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
623 "invalid suffix \"%.*s\" with hexadecimal floating constant",
624 (int) (limit - str), str);
625 return CPP_N_INVALID;
628 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
629 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
630 "fixed-point constants are a GCC extension");
632 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
633 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
634 "decimal float constants are a GCC extension");
636 result |= CPP_N_FLOATING;
638 else
640 result = interpret_int_suffix (pfile, str, limit - str);
641 if (result == 0)
643 if (CPP_OPTION (pfile, user_literals))
645 if (ud_suffix)
646 *ud_suffix = (const char *) str;
647 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
649 else
651 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
652 "invalid suffix \"%.*s\" on integer constant",
653 (int) (limit - str), str);
654 return CPP_N_INVALID;
658 /* Traditional C only accepted the 'L' suffix.
659 Suppress warning about 'LL' with -Wno-long-long. */
660 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
662 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
663 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
664 && CPP_OPTION (pfile, cpp_warn_long_long);
666 if (u_or_i || large)
667 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
668 virtual_location, 0,
669 "traditional C rejects the \"%.*s\" suffix",
670 (int) (limit - str), str);
673 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
674 && CPP_OPTION (pfile, cpp_warn_long_long))
676 const char *message = CPP_OPTION (pfile, cplusplus)
677 ? N_("use of C++11 long long integer constant")
678 : N_("use of C99 long long integer constant");
680 if (CPP_OPTION (pfile, c99))
681 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
682 0, message);
683 else
684 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
685 virtual_location, 0, message);
688 result |= CPP_N_INTEGER;
691 syntax_ok:
692 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
693 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
694 "imaginary constants are a GCC extension");
695 if (radix == 2
696 && !CPP_OPTION (pfile, binary_constants)
697 && CPP_PEDANTIC (pfile))
698 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
699 CPP_OPTION (pfile, cplusplus)
700 ? "binary constants are a C++14 feature "
701 "or GCC extension"
702 : "binary constants are a GCC extension");
704 if (radix == 10)
705 result |= CPP_N_DECIMAL;
706 else if (radix == 16)
707 result |= CPP_N_HEX;
708 else if (radix == 2)
709 result |= CPP_N_BINARY;
710 else
711 result |= CPP_N_OCTAL;
713 return result;
715 syntax_error:
716 return CPP_N_INVALID;
719 /* cpp_interpret_integer converts an integer constant into a cpp_num,
720 of precision options->precision.
722 We do not provide any interface for decimal->float conversion,
723 because the preprocessor doesn't need it and we don't want to
724 drag in GCC's floating point emulator. */
725 cpp_num
726 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
727 unsigned int type)
729 const uchar *p, *end;
730 cpp_num result;
732 result.low = 0;
733 result.high = 0;
734 result.unsignedp = !!(type & CPP_N_UNSIGNED);
735 result.overflow = false;
737 p = token->val.str.text;
738 end = p + token->val.str.len;
740 /* Common case of a single digit. */
741 if (token->val.str.len == 1)
742 result.low = p[0] - '0';
743 else
745 cpp_num_part max;
746 size_t precision = CPP_OPTION (pfile, precision);
747 unsigned int base = 10, c = 0;
748 bool overflow = false;
750 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
752 base = 8;
753 p++;
755 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
757 base = 16;
758 p += 2;
760 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
762 base = 2;
763 p += 2;
766 /* We can add a digit to numbers strictly less than this without
767 needing the precision and slowness of double integers. */
768 max = ~(cpp_num_part) 0;
769 if (precision < PART_PRECISION)
770 max >>= PART_PRECISION - precision;
771 max = (max - base + 1) / base + 1;
773 for (; p < end; p++)
775 c = *p;
777 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
778 c = hex_value (c);
779 else if (DIGIT_SEP (c))
780 continue;
781 else
782 break;
784 /* Strict inequality for when max is set to zero. */
785 if (result.low < max)
786 result.low = result.low * base + c;
787 else
789 result = append_digit (result, c, base, precision);
790 overflow |= result.overflow;
791 max = 0;
795 if (overflow && !(type & CPP_N_USERDEF))
796 cpp_error (pfile, CPP_DL_PEDWARN,
797 "integer constant is too large for its type");
798 /* If too big to be signed, consider it unsigned. Only warn for
799 decimal numbers. Traditional numbers were always signed (but
800 we still honor an explicit U suffix); but we only have
801 traditional semantics in directives. */
802 else if (!result.unsignedp
803 && !(CPP_OPTION (pfile, traditional)
804 && pfile->state.in_directive)
805 && !num_positive (result, precision))
807 /* This is for constants within the range of uintmax_t but
808 not that of intmax_t. For such decimal constants, a
809 diagnostic is required for C99 as the selected type must
810 be signed and not having a type is a constraint violation
811 (DR#298, TC3), so this must be a pedwarn. For C90,
812 unsigned long is specified to be used for a constant that
813 does not fit in signed long; if uintmax_t has the same
814 range as unsigned long this means only a warning is
815 appropriate here. C90 permits the preprocessor to use a
816 wider range than unsigned long in the compiler, so if
817 uintmax_t is wider than unsigned long no diagnostic is
818 required for such constants in preprocessor #if
819 expressions and the compiler will pedwarn for such
820 constants outside the range of unsigned long that reach
821 the compiler so a diagnostic is not required there
822 either; thus, pedwarn for C99 but use a plain warning for
823 C90. */
824 if (base == 10)
825 cpp_error (pfile, (CPP_OPTION (pfile, c99)
826 ? CPP_DL_PEDWARN
827 : CPP_DL_WARNING),
828 "integer constant is so large that it is unsigned");
829 result.unsignedp = true;
833 return result;
836 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
837 static cpp_num
838 append_digit (cpp_num num, int digit, int base, size_t precision)
840 cpp_num result;
841 unsigned int shift;
842 bool overflow;
843 cpp_num_part add_high, add_low;
845 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
846 need to worry about add_high overflowing. */
847 switch (base)
849 case 2:
850 shift = 1;
851 break;
853 case 16:
854 shift = 4;
855 break;
857 default:
858 shift = 3;
860 overflow = !!(num.high >> (PART_PRECISION - shift));
861 result.high = num.high << shift;
862 result.low = num.low << shift;
863 result.high |= num.low >> (PART_PRECISION - shift);
864 result.unsignedp = num.unsignedp;
866 if (base == 10)
868 add_low = num.low << 1;
869 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
871 else
872 add_high = add_low = 0;
874 if (add_low + digit < add_low)
875 add_high++;
876 add_low += digit;
878 if (result.low + add_low < result.low)
879 add_high++;
880 if (result.high + add_high < result.high)
881 overflow = true;
883 result.low += add_low;
884 result.high += add_high;
885 result.overflow = overflow;
887 /* The above code catches overflow of a cpp_num type. This catches
888 overflow of the (possibly shorter) target precision. */
889 num.low = result.low;
890 num.high = result.high;
891 result = num_trim (result, precision);
892 if (!num_eq (result, num))
893 result.overflow = true;
895 return result;
898 /* Handle meeting "defined" in a preprocessor expression. */
899 static cpp_num
900 parse_defined (cpp_reader *pfile)
902 cpp_num result;
903 int paren = 0;
904 cpp_hashnode *node = 0;
905 const cpp_token *token;
906 cpp_context *initial_context = pfile->context;
908 /* Don't expand macros. */
909 pfile->state.prevent_expansion++;
911 token = cpp_get_token (pfile);
912 if (token->type == CPP_OPEN_PAREN)
914 paren = 1;
915 token = cpp_get_token (pfile);
918 if (token->type == CPP_NAME)
920 node = token->val.node.node;
921 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
923 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
924 node = 0;
927 else
929 cpp_error (pfile, CPP_DL_ERROR,
930 "operator \"defined\" requires an identifier");
931 if (token->flags & NAMED_OP)
933 cpp_token op;
935 op.flags = 0;
936 op.type = token->type;
937 cpp_error (pfile, CPP_DL_ERROR,
938 "(\"%s\" is an alternative token for \"%s\" in C++)",
939 cpp_token_as_text (pfile, token),
940 cpp_token_as_text (pfile, &op));
944 if (node)
946 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
947 cpp_error (pfile, CPP_DL_WARNING,
948 "this use of \"defined\" may not be portable");
950 _cpp_mark_macro_used (node);
951 if (!(node->flags & NODE_USED))
953 node->flags |= NODE_USED;
954 if (node->type == NT_MACRO)
956 if ((node->flags & NODE_BUILTIN)
957 && pfile->cb.user_builtin_macro)
958 pfile->cb.user_builtin_macro (pfile, node);
959 if (pfile->cb.used_define)
960 pfile->cb.used_define (pfile, pfile->directive_line, node);
962 else
964 if (pfile->cb.used_undef)
965 pfile->cb.used_undef (pfile, pfile->directive_line, node);
969 /* A possible controlling macro of the form #if !defined ().
970 _cpp_parse_expr checks there was no other junk on the line. */
971 pfile->mi_ind_cmacro = node;
974 pfile->state.prevent_expansion--;
976 /* Do not treat conditional macros as being defined. This is due to the
977 powerpc and spu ports using conditional macros for 'vector', 'bool', and
978 'pixel' to act as conditional keywords. This messes up tests like #ifndef
979 bool. */
980 result.unsignedp = false;
981 result.high = 0;
982 result.overflow = false;
983 result.low = (node && node->type == NT_MACRO
984 && (node->flags & NODE_CONDITIONAL) == 0);
985 return result;
988 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
989 number or character constant, or the result of the "defined" or "#"
990 operators). */
991 static cpp_num
992 eval_token (cpp_reader *pfile, const cpp_token *token,
993 source_location virtual_location)
995 cpp_num result;
996 unsigned int temp;
997 int unsignedp = 0;
999 result.unsignedp = false;
1000 result.overflow = false;
1002 switch (token->type)
1004 case CPP_NUMBER:
1005 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1006 if (temp & CPP_N_USERDEF)
1007 cpp_error (pfile, CPP_DL_ERROR,
1008 "user-defined literal in preprocessor expression");
1009 switch (temp & CPP_N_CATEGORY)
1011 case CPP_N_FLOATING:
1012 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1013 "floating constant in preprocessor expression");
1014 break;
1015 case CPP_N_INTEGER:
1016 if (!(temp & CPP_N_IMAGINARY))
1017 return cpp_interpret_integer (pfile, token, temp);
1018 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1019 "imaginary number in preprocessor expression");
1020 break;
1022 case CPP_N_INVALID:
1023 /* Error already issued. */
1024 break;
1026 result.high = result.low = 0;
1027 break;
1029 case CPP_WCHAR:
1030 case CPP_CHAR:
1031 case CPP_CHAR16:
1032 case CPP_CHAR32:
1034 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1035 &temp, &unsignedp);
1037 result.high = 0;
1038 result.low = cc;
1039 /* Sign-extend the result if necessary. */
1040 if (!unsignedp && (cppchar_signed_t) cc < 0)
1042 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1043 result.low |= ~(~(cpp_num_part) 0
1044 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1045 result.high = ~(cpp_num_part) 0;
1046 result = num_trim (result, CPP_OPTION (pfile, precision));
1049 break;
1051 case CPP_NAME:
1052 if (token->val.node.node == pfile->spec_nodes.n_defined)
1053 return parse_defined (pfile);
1054 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1055 return parse_has_include (pfile, IT_INCLUDE);
1056 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1057 return parse_has_include (pfile, IT_INCLUDE_NEXT);
1058 else if (token->val.node.node == pfile->spec_nodes.n__has_attribute__)
1059 return parse_has_attribute (pfile);
1060 else if (CPP_OPTION (pfile, cplusplus)
1061 && (token->val.node.node == pfile->spec_nodes.n_true
1062 || token->val.node.node == pfile->spec_nodes.n_false))
1064 result.high = 0;
1065 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1067 else
1069 result.high = 0;
1070 result.low = 0;
1071 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1072 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1073 "\"%s\" is not defined",
1074 NODE_NAME (token->val.node.node));
1076 break;
1078 case CPP_HASH:
1079 if (!pfile->state.skipping)
1081 /* A pedantic warning takes precedence over a deprecated
1082 warning here. */
1083 if (CPP_PEDANTIC (pfile))
1084 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1085 virtual_location, 0,
1086 "assertions are a GCC extension");
1087 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1088 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1089 "assertions are a deprecated extension");
1091 _cpp_test_assertion (pfile, &temp);
1092 result.high = 0;
1093 result.low = temp;
1094 break;
1096 default:
1097 abort ();
1100 result.unsignedp = !!unsignedp;
1101 return result;
1104 /* Operator precedence and flags table.
1106 After an operator is returned from the lexer, if it has priority less
1107 than the operator on the top of the stack, we reduce the stack by one
1108 operator and repeat the test. Since equal priorities do not reduce,
1109 this is naturally right-associative.
1111 We handle left-associative operators by decrementing the priority of
1112 just-lexed operators by one, but retaining the priority of operators
1113 already on the stack.
1115 The remaining cases are '(' and ')'. We handle '(' by skipping the
1116 reduction phase completely. ')' is given lower priority than
1117 everything else, including '(', effectively forcing a reduction of the
1118 parenthesized expression. If there is a matching '(', the routine
1119 reduce() exits immediately. If the normal exit route sees a ')', then
1120 there cannot have been a matching '(' and an error message is output.
1122 The parser assumes all shifted operators require a left operand unless
1123 the flag NO_L_OPERAND is set. These semantics are automatic; any
1124 extra semantics need to be handled with operator-specific code. */
1126 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1127 operand changes because of integer promotions. */
1128 #define NO_L_OPERAND (1 << 0)
1129 #define LEFT_ASSOC (1 << 1)
1130 #define CHECK_PROMOTION (1 << 2)
1132 /* Operator to priority map. Must be in the same order as the first
1133 N entries of enum cpp_ttype. */
1134 static const struct cpp_operator
1136 uchar prio;
1137 uchar flags;
1138 } optab[] =
1140 /* EQ */ {0, 0}, /* Shouldn't happen. */
1141 /* NOT */ {16, NO_L_OPERAND},
1142 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1143 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1144 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1145 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1146 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1147 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1148 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1149 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1150 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1151 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1152 /* RSHIFT */ {13, LEFT_ASSOC},
1153 /* LSHIFT */ {13, LEFT_ASSOC},
1155 /* COMPL */ {16, NO_L_OPERAND},
1156 /* AND_AND */ {6, LEFT_ASSOC},
1157 /* OR_OR */ {5, LEFT_ASSOC},
1158 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1159 However, there are some special cases for these in reduce(). */
1160 /* QUERY */ {4, 0},
1161 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1162 /* COMMA */ {4, LEFT_ASSOC},
1163 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1164 /* CLOSE_PAREN */ {0, 0},
1165 /* EOF */ {0, 0},
1166 /* EQ_EQ */ {11, LEFT_ASSOC},
1167 /* NOT_EQ */ {11, LEFT_ASSOC},
1168 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1169 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1170 /* UPLUS */ {16, NO_L_OPERAND},
1171 /* UMINUS */ {16, NO_L_OPERAND}
1174 /* Parse and evaluate a C expression, reading from PFILE.
1175 Returns the truth value of the expression.
1177 The implementation is an operator precedence parser, i.e. a
1178 bottom-up parser, using a stack for not-yet-reduced tokens.
1180 The stack base is op_stack, and the current stack pointer is 'top'.
1181 There is a stack element for each operator (only), and the most
1182 recently pushed operator is 'top->op'. An operand (value) is
1183 stored in the 'value' field of the stack element of the operator
1184 that precedes it. */
1185 bool
1186 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1188 struct op *top = pfile->op_stack;
1189 unsigned int lex_count;
1190 bool saw_leading_not, want_value = true;
1191 source_location virtual_location = 0;
1193 pfile->state.skip_eval = 0;
1195 /* Set up detection of #if ! defined(). */
1196 pfile->mi_ind_cmacro = 0;
1197 saw_leading_not = false;
1198 lex_count = 0;
1200 /* Lowest priority operator prevents further reductions. */
1201 top->op = CPP_EOF;
1203 for (;;)
1205 struct op op;
1207 lex_count++;
1208 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1209 op.op = op.token->type;
1210 op.loc = virtual_location;
1212 switch (op.op)
1214 /* These tokens convert into values. */
1215 case CPP_NUMBER:
1216 case CPP_CHAR:
1217 case CPP_WCHAR:
1218 case CPP_CHAR16:
1219 case CPP_CHAR32:
1220 case CPP_NAME:
1221 case CPP_HASH:
1222 if (!want_value)
1223 SYNTAX_ERROR2_AT (op.loc,
1224 "missing binary operator before token \"%s\"",
1225 cpp_token_as_text (pfile, op.token));
1226 want_value = false;
1227 top->value = eval_token (pfile, op.token, op.loc);
1228 continue;
1230 case CPP_NOT:
1231 saw_leading_not = lex_count == 1;
1232 break;
1233 case CPP_PLUS:
1234 if (want_value)
1235 op.op = CPP_UPLUS;
1236 break;
1237 case CPP_MINUS:
1238 if (want_value)
1239 op.op = CPP_UMINUS;
1240 break;
1242 default:
1243 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1244 SYNTAX_ERROR2_AT (op.loc,
1245 "token \"%s\" is not valid in preprocessor expressions",
1246 cpp_token_as_text (pfile, op.token));
1247 break;
1250 /* Check we have a value or operator as appropriate. */
1251 if (optab[op.op].flags & NO_L_OPERAND)
1253 if (!want_value)
1254 SYNTAX_ERROR2_AT (op.loc,
1255 "missing binary operator before token \"%s\"",
1256 cpp_token_as_text (pfile, op.token));
1258 else if (want_value)
1260 /* We want a number (or expression) and haven't got one.
1261 Try to emit a specific diagnostic. */
1262 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1263 SYNTAX_ERROR_AT (op.loc,
1264 "missing expression between '(' and ')'");
1266 if (op.op == CPP_EOF && top->op == CPP_EOF)
1267 SYNTAX_ERROR2_AT (op.loc,
1268 "%s with no expression", is_if ? "#if" : "#elif");
1270 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1271 SYNTAX_ERROR2_AT (op.loc,
1272 "operator '%s' has no right operand",
1273 cpp_token_as_text (pfile, top->token));
1274 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1275 /* Complain about missing paren during reduction. */;
1276 else
1277 SYNTAX_ERROR2_AT (op.loc,
1278 "operator '%s' has no left operand",
1279 cpp_token_as_text (pfile, op.token));
1282 top = reduce (pfile, top, op.op);
1283 if (!top)
1284 goto syntax_error;
1286 if (op.op == CPP_EOF)
1287 break;
1289 switch (op.op)
1291 case CPP_CLOSE_PAREN:
1292 continue;
1293 case CPP_OR_OR:
1294 if (!num_zerop (top->value))
1295 pfile->state.skip_eval++;
1296 break;
1297 case CPP_AND_AND:
1298 case CPP_QUERY:
1299 if (num_zerop (top->value))
1300 pfile->state.skip_eval++;
1301 break;
1302 case CPP_COLON:
1303 if (top->op != CPP_QUERY)
1304 SYNTAX_ERROR_AT (op.loc,
1305 " ':' without preceding '?'");
1306 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1307 pfile->state.skip_eval++;
1308 else
1309 pfile->state.skip_eval--;
1310 default:
1311 break;
1314 want_value = true;
1316 /* Check for and handle stack overflow. */
1317 if (++top == pfile->op_limit)
1318 top = _cpp_expand_op_stack (pfile);
1320 top->op = op.op;
1321 top->token = op.token;
1322 top->loc = op.loc;
1325 /* The controlling macro expression is only valid if we called lex 3
1326 times: <!> <defined expression> and <EOF>. push_conditional ()
1327 checks that we are at top-of-file. */
1328 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1329 pfile->mi_ind_cmacro = 0;
1331 if (top != pfile->op_stack)
1333 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1334 "unbalanced stack in %s",
1335 is_if ? "#if" : "#elif");
1336 syntax_error:
1337 return false; /* Return false on syntax error. */
1340 return !num_zerop (top->value);
1343 /* Reduce the operator / value stack if possible, in preparation for
1344 pushing operator OP. Returns NULL on error, otherwise the top of
1345 the stack. */
1346 static struct op *
1347 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1349 unsigned int prio;
1351 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1353 bad_op:
1354 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1355 return 0;
1358 if (op == CPP_OPEN_PAREN)
1359 return top;
1361 /* Decrement the priority of left-associative operators to force a
1362 reduction with operators of otherwise equal priority. */
1363 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1364 while (prio < optab[top->op].prio)
1366 if (CPP_OPTION (pfile, warn_num_sign_change)
1367 && optab[top->op].flags & CHECK_PROMOTION)
1368 check_promotion (pfile, top);
1370 switch (top->op)
1372 case CPP_UPLUS:
1373 case CPP_UMINUS:
1374 case CPP_NOT:
1375 case CPP_COMPL:
1376 top[-1].value = num_unary_op (pfile, top->value, top->op);
1377 top[-1].loc = top->loc;
1378 break;
1380 case CPP_PLUS:
1381 case CPP_MINUS:
1382 case CPP_RSHIFT:
1383 case CPP_LSHIFT:
1384 case CPP_COMMA:
1385 top[-1].value = num_binary_op (pfile, top[-1].value,
1386 top->value, top->op);
1387 top[-1].loc = top->loc;
1388 break;
1390 case CPP_GREATER:
1391 case CPP_LESS:
1392 case CPP_GREATER_EQ:
1393 case CPP_LESS_EQ:
1394 top[-1].value
1395 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1396 top[-1].loc = top->loc;
1397 break;
1399 case CPP_EQ_EQ:
1400 case CPP_NOT_EQ:
1401 top[-1].value
1402 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1403 top[-1].loc = top->loc;
1404 break;
1406 case CPP_AND:
1407 case CPP_OR:
1408 case CPP_XOR:
1409 top[-1].value
1410 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1411 top[-1].loc = top->loc;
1412 break;
1414 case CPP_MULT:
1415 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1416 top[-1].loc = top->loc;
1417 break;
1419 case CPP_DIV:
1420 case CPP_MOD:
1421 top[-1].value = num_div_op (pfile, top[-1].value,
1422 top->value, top->op, top->loc);
1423 top[-1].loc = top->loc;
1424 break;
1426 case CPP_OR_OR:
1427 top--;
1428 if (!num_zerop (top->value))
1429 pfile->state.skip_eval--;
1430 top->value.low = (!num_zerop (top->value)
1431 || !num_zerop (top[1].value));
1432 top->value.high = 0;
1433 top->value.unsignedp = false;
1434 top->value.overflow = false;
1435 top->loc = top[1].loc;
1436 continue;
1438 case CPP_AND_AND:
1439 top--;
1440 if (num_zerop (top->value))
1441 pfile->state.skip_eval--;
1442 top->value.low = (!num_zerop (top->value)
1443 && !num_zerop (top[1].value));
1444 top->value.high = 0;
1445 top->value.unsignedp = false;
1446 top->value.overflow = false;
1447 top->loc = top[1].loc;
1448 continue;
1450 case CPP_OPEN_PAREN:
1451 if (op != CPP_CLOSE_PAREN)
1453 cpp_error_with_line (pfile, CPP_DL_ERROR,
1454 top->token->src_loc,
1455 0, "missing ')' in expression");
1456 return 0;
1458 top--;
1459 top->value = top[1].value;
1460 top->loc = top[1].loc;
1461 return top;
1463 case CPP_COLON:
1464 top -= 2;
1465 if (!num_zerop (top->value))
1467 pfile->state.skip_eval--;
1468 top->value = top[1].value;
1469 top->loc = top[1].loc;
1471 else
1473 top->value = top[2].value;
1474 top->loc = top[2].loc;
1476 top->value.unsignedp = (top[1].value.unsignedp
1477 || top[2].value.unsignedp);
1478 continue;
1480 case CPP_QUERY:
1481 /* COMMA and COLON should not reduce a QUERY operator. */
1482 if (op == CPP_COMMA || op == CPP_COLON)
1483 return top;
1484 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1485 return 0;
1487 default:
1488 goto bad_op;
1491 top--;
1492 if (top->value.overflow && !pfile->state.skip_eval)
1493 cpp_error (pfile, CPP_DL_PEDWARN,
1494 "integer overflow in preprocessor expression");
1497 if (op == CPP_CLOSE_PAREN)
1499 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1500 return 0;
1503 return top;
1506 /* Returns the position of the old top of stack after expansion. */
1507 struct op *
1508 _cpp_expand_op_stack (cpp_reader *pfile)
1510 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1511 size_t new_size = old_size * 2 + 20;
1513 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1514 pfile->op_limit = pfile->op_stack + new_size;
1516 return pfile->op_stack + old_size;
1519 /* Emits a warning if the effective sign of either operand of OP
1520 changes because of integer promotions. */
1521 static void
1522 check_promotion (cpp_reader *pfile, const struct op *op)
1524 if (op->value.unsignedp == op[-1].value.unsignedp)
1525 return;
1527 if (op->value.unsignedp)
1529 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1530 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1531 "the left operand of \"%s\" changes sign when promoted",
1532 cpp_token_as_text (pfile, op->token));
1534 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1535 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1536 "the right operand of \"%s\" changes sign when promoted",
1537 cpp_token_as_text (pfile, op->token));
1540 /* Clears the unused high order bits of the number pointed to by PNUM. */
1541 static cpp_num
1542 num_trim (cpp_num num, size_t precision)
1544 if (precision > PART_PRECISION)
1546 precision -= PART_PRECISION;
1547 if (precision < PART_PRECISION)
1548 num.high &= ((cpp_num_part) 1 << precision) - 1;
1550 else
1552 if (precision < PART_PRECISION)
1553 num.low &= ((cpp_num_part) 1 << precision) - 1;
1554 num.high = 0;
1557 return num;
1560 /* True iff A (presumed signed) >= 0. */
1561 static bool
1562 num_positive (cpp_num num, size_t precision)
1564 if (precision > PART_PRECISION)
1566 precision -= PART_PRECISION;
1567 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1570 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1573 /* Sign extend a number, with PRECISION significant bits and all
1574 others assumed clear, to fill out a cpp_num structure. */
1575 cpp_num
1576 cpp_num_sign_extend (cpp_num num, size_t precision)
1578 if (!num.unsignedp)
1580 if (precision > PART_PRECISION)
1582 precision -= PART_PRECISION;
1583 if (precision < PART_PRECISION
1584 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1585 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1587 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1589 if (precision < PART_PRECISION)
1590 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1591 num.high = ~(cpp_num_part) 0;
1595 return num;
1598 /* Returns the negative of NUM. */
1599 static cpp_num
1600 num_negate (cpp_num num, size_t precision)
1602 cpp_num copy;
1604 copy = num;
1605 num.high = ~num.high;
1606 num.low = ~num.low;
1607 if (++num.low == 0)
1608 num.high++;
1609 num = num_trim (num, precision);
1610 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1612 return num;
1615 /* Returns true if A >= B. */
1616 static bool
1617 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1619 bool unsignedp;
1621 unsignedp = pa.unsignedp || pb.unsignedp;
1623 if (!unsignedp)
1625 /* Both numbers have signed type. If they are of different
1626 sign, the answer is the sign of A. */
1627 unsignedp = num_positive (pa, precision);
1629 if (unsignedp != num_positive (pb, precision))
1630 return unsignedp;
1632 /* Otherwise we can do an unsigned comparison. */
1635 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1638 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1639 static cpp_num
1640 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1641 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1643 lhs.overflow = false;
1644 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1646 /* As excess precision is zeroed, there is no need to num_trim () as
1647 these operations cannot introduce a set bit there. */
1648 if (op == CPP_AND)
1650 lhs.low &= rhs.low;
1651 lhs.high &= rhs.high;
1653 else if (op == CPP_OR)
1655 lhs.low |= rhs.low;
1656 lhs.high |= rhs.high;
1658 else
1660 lhs.low ^= rhs.low;
1661 lhs.high ^= rhs.high;
1664 return lhs;
1667 /* Returns LHS OP RHS, where OP is an inequality. */
1668 static cpp_num
1669 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1670 enum cpp_ttype op)
1672 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1674 if (op == CPP_GREATER_EQ)
1675 lhs.low = gte;
1676 else if (op == CPP_LESS)
1677 lhs.low = !gte;
1678 else if (op == CPP_GREATER)
1679 lhs.low = gte && !num_eq (lhs, rhs);
1680 else /* CPP_LESS_EQ. */
1681 lhs.low = !gte || num_eq (lhs, rhs);
1683 lhs.high = 0;
1684 lhs.overflow = false;
1685 lhs.unsignedp = false;
1686 return lhs;
1689 /* Returns LHS OP RHS, where OP is == or !=. */
1690 static cpp_num
1691 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1692 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1694 /* Work around a 3.0.4 bug; see PR 6950. */
1695 bool eq = num_eq (lhs, rhs);
1696 if (op == CPP_NOT_EQ)
1697 eq = !eq;
1698 lhs.low = eq;
1699 lhs.high = 0;
1700 lhs.overflow = false;
1701 lhs.unsignedp = false;
1702 return lhs;
1705 /* Shift NUM, of width PRECISION, right by N bits. */
1706 static cpp_num
1707 num_rshift (cpp_num num, size_t precision, size_t n)
1709 cpp_num_part sign_mask;
1710 bool x = num_positive (num, precision);
1712 if (num.unsignedp || x)
1713 sign_mask = 0;
1714 else
1715 sign_mask = ~(cpp_num_part) 0;
1717 if (n >= precision)
1718 num.high = num.low = sign_mask;
1719 else
1721 /* Sign-extend. */
1722 if (precision < PART_PRECISION)
1723 num.high = sign_mask, num.low |= sign_mask << precision;
1724 else if (precision < 2 * PART_PRECISION)
1725 num.high |= sign_mask << (precision - PART_PRECISION);
1727 if (n >= PART_PRECISION)
1729 n -= PART_PRECISION;
1730 num.low = num.high;
1731 num.high = sign_mask;
1734 if (n)
1736 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1737 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1741 num = num_trim (num, precision);
1742 num.overflow = false;
1743 return num;
1746 /* Shift NUM, of width PRECISION, left by N bits. */
1747 static cpp_num
1748 num_lshift (cpp_num num, size_t precision, size_t n)
1750 if (n >= precision)
1752 num.overflow = !num.unsignedp && !num_zerop (num);
1753 num.high = num.low = 0;
1755 else
1757 cpp_num orig, maybe_orig;
1758 size_t m = n;
1760 orig = num;
1761 if (m >= PART_PRECISION)
1763 m -= PART_PRECISION;
1764 num.high = num.low;
1765 num.low = 0;
1767 if (m)
1769 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1770 num.low <<= m;
1772 num = num_trim (num, precision);
1774 if (num.unsignedp)
1775 num.overflow = false;
1776 else
1778 maybe_orig = num_rshift (num, precision, n);
1779 num.overflow = !num_eq (orig, maybe_orig);
1783 return num;
1786 /* The four unary operators: +, -, ! and ~. */
1787 static cpp_num
1788 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1790 switch (op)
1792 case CPP_UPLUS:
1793 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1794 cpp_warning (pfile, CPP_W_TRADITIONAL,
1795 "traditional C rejects the unary plus operator");
1796 num.overflow = false;
1797 break;
1799 case CPP_UMINUS:
1800 num = num_negate (num, CPP_OPTION (pfile, precision));
1801 break;
1803 case CPP_COMPL:
1804 num.high = ~num.high;
1805 num.low = ~num.low;
1806 num = num_trim (num, CPP_OPTION (pfile, precision));
1807 num.overflow = false;
1808 break;
1810 default: /* case CPP_NOT: */
1811 num.low = num_zerop (num);
1812 num.high = 0;
1813 num.overflow = false;
1814 num.unsignedp = false;
1815 break;
1818 return num;
1821 /* The various binary operators. */
1822 static cpp_num
1823 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1825 cpp_num result;
1826 size_t precision = CPP_OPTION (pfile, precision);
1827 size_t n;
1829 switch (op)
1831 /* Shifts. */
1832 case CPP_LSHIFT:
1833 case CPP_RSHIFT:
1834 if (!rhs.unsignedp && !num_positive (rhs, precision))
1836 /* A negative shift is a positive shift the other way. */
1837 if (op == CPP_LSHIFT)
1838 op = CPP_RSHIFT;
1839 else
1840 op = CPP_LSHIFT;
1841 rhs = num_negate (rhs, precision);
1843 if (rhs.high)
1844 n = ~0; /* Maximal. */
1845 else
1846 n = rhs.low;
1847 if (op == CPP_LSHIFT)
1848 lhs = num_lshift (lhs, precision, n);
1849 else
1850 lhs = num_rshift (lhs, precision, n);
1851 break;
1853 /* Arithmetic. */
1854 case CPP_MINUS:
1855 result.low = lhs.low - rhs.low;
1856 result.high = lhs.high - rhs.high;
1857 if (result.low > lhs.low)
1858 result.high--;
1859 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1860 result.overflow = false;
1862 result = num_trim (result, precision);
1863 if (!result.unsignedp)
1865 bool lhsp = num_positive (lhs, precision);
1866 result.overflow = (lhsp != num_positive (rhs, precision)
1867 && lhsp != num_positive (result, precision));
1869 return result;
1871 case CPP_PLUS:
1872 result.low = lhs.low + rhs.low;
1873 result.high = lhs.high + rhs.high;
1874 if (result.low < lhs.low)
1875 result.high++;
1876 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1877 result.overflow = false;
1879 result = num_trim (result, precision);
1880 if (!result.unsignedp)
1882 bool lhsp = num_positive (lhs, precision);
1883 result.overflow = (lhsp == num_positive (rhs, precision)
1884 && lhsp != num_positive (result, precision));
1886 return result;
1888 /* Comma. */
1889 default: /* case CPP_COMMA: */
1890 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1891 || !pfile->state.skip_eval))
1892 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1893 "comma operator in operand of #if");
1894 lhs = rhs;
1895 break;
1898 return lhs;
1901 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1902 cannot overflow. */
1903 static cpp_num
1904 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1906 cpp_num result;
1907 cpp_num_part middle[2], temp;
1909 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1910 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1912 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1913 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1915 temp = result.low;
1916 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1917 if (result.low < temp)
1918 result.high++;
1920 temp = result.low;
1921 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1922 if (result.low < temp)
1923 result.high++;
1925 result.high += HIGH_PART (middle[0]);
1926 result.high += HIGH_PART (middle[1]);
1927 result.unsignedp = true;
1928 result.overflow = false;
1930 return result;
1933 /* Multiply two preprocessing numbers. */
1934 static cpp_num
1935 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1937 cpp_num result, temp;
1938 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1939 bool overflow, negate = false;
1940 size_t precision = CPP_OPTION (pfile, precision);
1942 /* Prepare for unsigned multiplication. */
1943 if (!unsignedp)
1945 if (!num_positive (lhs, precision))
1946 negate = !negate, lhs = num_negate (lhs, precision);
1947 if (!num_positive (rhs, precision))
1948 negate = !negate, rhs = num_negate (rhs, precision);
1951 overflow = lhs.high && rhs.high;
1952 result = num_part_mul (lhs.low, rhs.low);
1954 temp = num_part_mul (lhs.high, rhs.low);
1955 result.high += temp.low;
1956 if (temp.high)
1957 overflow = true;
1959 temp = num_part_mul (lhs.low, rhs.high);
1960 result.high += temp.low;
1961 if (temp.high)
1962 overflow = true;
1964 temp.low = result.low, temp.high = result.high;
1965 result = num_trim (result, precision);
1966 if (!num_eq (result, temp))
1967 overflow = true;
1969 if (negate)
1970 result = num_negate (result, precision);
1972 if (unsignedp)
1973 result.overflow = false;
1974 else
1975 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1976 && !num_zerop (result));
1977 result.unsignedp = unsignedp;
1979 return result;
1982 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1983 or the remainder depending upon OP. LOCATION is the source location
1984 of this operator (for diagnostics). */
1986 static cpp_num
1987 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1988 source_location location)
1990 cpp_num result, sub;
1991 cpp_num_part mask;
1992 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1993 bool negate = false, lhs_neg = false;
1994 size_t i, precision = CPP_OPTION (pfile, precision);
1996 /* Prepare for unsigned division. */
1997 if (!unsignedp)
1999 if (!num_positive (lhs, precision))
2000 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2001 if (!num_positive (rhs, precision))
2002 negate = !negate, rhs = num_negate (rhs, precision);
2005 /* Find the high bit. */
2006 if (rhs.high)
2008 i = precision - 1;
2009 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2010 for (; ; i--, mask >>= 1)
2011 if (rhs.high & mask)
2012 break;
2014 else if (rhs.low)
2016 if (precision > PART_PRECISION)
2017 i = precision - PART_PRECISION - 1;
2018 else
2019 i = precision - 1;
2020 mask = (cpp_num_part) 1 << i;
2021 for (; ; i--, mask >>= 1)
2022 if (rhs.low & mask)
2023 break;
2025 else
2027 if (!pfile->state.skip_eval)
2028 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2029 "division by zero in #if");
2030 return lhs;
2033 /* First nonzero bit of RHS is bit I. Do naive division by
2034 shifting the RHS fully left, and subtracting from LHS if LHS is
2035 at least as big, and then repeating but with one less shift.
2036 This is not very efficient, but is easy to understand. */
2038 rhs.unsignedp = true;
2039 lhs.unsignedp = true;
2040 i = precision - i - 1;
2041 sub = num_lshift (rhs, precision, i);
2043 result.high = result.low = 0;
2044 for (;;)
2046 if (num_greater_eq (lhs, sub, precision))
2048 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2049 if (i >= PART_PRECISION)
2050 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2051 else
2052 result.low |= (cpp_num_part) 1 << i;
2054 if (i-- == 0)
2055 break;
2056 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2057 sub.high >>= 1;
2060 /* We divide so that the remainder has the sign of the LHS. */
2061 if (op == CPP_DIV)
2063 result.unsignedp = unsignedp;
2064 result.overflow = false;
2065 if (!unsignedp)
2067 if (negate)
2068 result = num_negate (result, precision);
2069 result.overflow = (num_positive (result, precision) ^ !negate
2070 && !num_zerop (result));
2073 return result;
2076 /* CPP_MOD. */
2077 lhs.unsignedp = unsignedp;
2078 lhs.overflow = false;
2079 if (lhs_neg)
2080 lhs = num_negate (lhs, precision);
2082 return lhs;
2085 /* Handle meeting "__has_include__" in a preprocessor expression. */
2086 static cpp_num
2087 parse_has_include (cpp_reader *pfile, enum include_type type)
2089 cpp_num result;
2090 bool paren = false;
2091 cpp_hashnode *node = 0;
2092 const cpp_token *token;
2093 bool bracket = false;
2094 char *fname = 0;
2096 result.unsignedp = false;
2097 result.high = 0;
2098 result.overflow = false;
2099 result.low = 0;
2101 pfile->state.in__has_include__++;
2103 token = cpp_get_token (pfile);
2104 if (token->type == CPP_OPEN_PAREN)
2106 paren = true;
2107 token = cpp_get_token (pfile);
2110 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2112 if (token->type == CPP_HEADER_NAME)
2113 bracket = true;
2114 fname = XNEWVEC (char, token->val.str.len - 1);
2115 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2116 fname[token->val.str.len - 2] = '\0';
2117 node = token->val.node.node;
2119 else if (token->type == CPP_LESS)
2121 bracket = true;
2122 fname = _cpp_bracket_include (pfile);
2124 else
2125 cpp_error (pfile, CPP_DL_ERROR,
2126 "operator \"__has_include__\" requires a header string");
2128 if (fname)
2130 int angle_brackets = (bracket ? 1 : 0);
2132 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2133 result.low = 1;
2134 else
2135 result.low = 0;
2137 XDELETEVEC (fname);
2140 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2141 cpp_error (pfile, CPP_DL_ERROR,
2142 "missing ')' after \"__has_include__\"");
2144 /* A possible controlling macro of the form #if !__has_include__ ().
2145 _cpp_parse_expr checks there was no other junk on the line. */
2146 if (node)
2147 pfile->mi_ind_cmacro = node;
2149 pfile->state.in__has_include__--;
2151 return result;
2154 /* Handle meeting "__has_attribute__" in a preprocessor expression. */
2155 static cpp_num
2156 parse_has_attribute (cpp_reader *pfile)
2158 pfile->state.in__has_attribute__++;
2160 cpp_num result;
2161 result.unsignedp = false;
2162 result.high = 0;
2163 result.overflow = false;
2165 result.low = pfile->cb.has_attribute (pfile);
2167 pfile->state.in__has_attribute__--;
2169 return result;