Add parloops-exit-first-loop-alt-{5,6,7}.c
[official-gcc.git] / libcpp / expr.c
blobb8e88c50f330ff9fab1c91e73cb7e876512798cb
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2015 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);
69 /* Token type abuse to create unary plus and minus operators. */
70 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
71 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
73 /* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
75 #define SYNTAX_ERROR(msgid) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
79 while(0)
80 #define SYNTAX_ERROR_AT(loc, msgid) \
81 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
82 while(0)
83 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
84 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
85 while(0)
87 /* Subroutine of cpp_classify_number. S points to a float suffix of
88 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
89 flag vector describing the suffix. */
90 static unsigned int
91 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
93 size_t flags;
94 size_t f, d, l, w, q, i;
96 flags = 0;
97 f = d = l = w = q = i = 0;
99 /* Process decimal float suffixes, which are two letters starting
100 with d or D. Order and case are significant. */
101 if (len == 2 && (*s == 'd' || *s == 'D'))
103 bool uppercase = (*s == 'D');
104 switch (s[1])
106 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
107 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
108 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
109 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
110 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
111 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
112 default:
113 /* Additional two-character suffixes beginning with D are not
114 for decimal float constants. */
115 break;
119 if (CPP_OPTION (pfile, ext_numeric_literals))
121 /* Recognize a fixed-point suffix. */
122 if (len != 0)
123 switch (s[len-1])
125 case 'k': case 'K': flags = CPP_N_ACCUM; break;
126 case 'r': case 'R': flags = CPP_N_FRACT; break;
127 default: break;
130 /* Continue processing a fixed-point suffix. The suffix is case
131 insensitive except for ll or LL. Order is significant. */
132 if (flags)
134 if (len == 1)
135 return flags;
136 len--;
138 if (*s == 'u' || *s == 'U')
140 flags |= CPP_N_UNSIGNED;
141 if (len == 1)
142 return flags;
143 len--;
144 s++;
147 switch (*s)
149 case 'h': case 'H':
150 if (len == 1)
151 return flags |= CPP_N_SMALL;
152 break;
153 case 'l':
154 if (len == 1)
155 return flags |= CPP_N_MEDIUM;
156 if (len == 2 && s[1] == 'l')
157 return flags |= CPP_N_LARGE;
158 break;
159 case 'L':
160 if (len == 1)
161 return flags |= CPP_N_MEDIUM;
162 if (len == 2 && s[1] == 'L')
163 return flags |= CPP_N_LARGE;
164 break;
165 default:
166 break;
168 /* Anything left at this point is invalid. */
169 return 0;
173 /* In any remaining valid suffix, the case and order don't matter. */
174 while (len--)
175 switch (s[len])
177 case 'f': case 'F': f++; break;
178 case 'd': case 'D': d++; break;
179 case 'l': case 'L': l++; break;
180 case 'w': case 'W': w++; break;
181 case 'q': case 'Q': q++; break;
182 case 'i': case 'I':
183 case 'j': case 'J': i++; break;
184 default:
185 return 0;
188 if (f + d + l + w + q > 1 || i > 1)
189 return 0;
191 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
192 return 0;
194 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
195 return 0;
197 return ((i ? CPP_N_IMAGINARY : 0)
198 | (f ? CPP_N_SMALL :
199 d ? CPP_N_MEDIUM :
200 l ? CPP_N_LARGE :
201 w ? CPP_N_MD_W :
202 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
205 /* Return the classification flags for a float suffix. */
206 unsigned int
207 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
209 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
212 /* Subroutine of cpp_classify_number. S points to an integer suffix
213 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
214 flag vector describing the suffix. */
215 static unsigned int
216 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
218 size_t u, l, i;
220 u = l = i = 0;
222 while (len--)
223 switch (s[len])
225 case 'u': case 'U': u++; break;
226 case 'i': case 'I':
227 case 'j': case 'J': i++; break;
228 case 'l': case 'L': l++;
229 /* If there are two Ls, they must be adjacent and the same case. */
230 if (l == 2 && s[len] != s[len + 1])
231 return 0;
232 break;
233 default:
234 return 0;
237 if (l > 2 || u > 1 || i > 1)
238 return 0;
240 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
241 return 0;
243 return ((i ? CPP_N_IMAGINARY : 0)
244 | (u ? CPP_N_UNSIGNED : 0)
245 | ((l == 0) ? CPP_N_SMALL
246 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
249 /* Return the classification flags for an int suffix. */
250 unsigned int
251 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
253 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
256 /* Return the string type corresponding to the the input user-defined string
257 literal type. If the input type is not a user-defined string literal
258 type return the input type. */
259 enum cpp_ttype
260 cpp_userdef_string_remove_type (enum cpp_ttype type)
262 if (type == CPP_STRING_USERDEF)
263 return CPP_STRING;
264 else if (type == CPP_WSTRING_USERDEF)
265 return CPP_WSTRING;
266 else if (type == CPP_STRING16_USERDEF)
267 return CPP_STRING16;
268 else if (type == CPP_STRING32_USERDEF)
269 return CPP_STRING32;
270 else if (type == CPP_UTF8STRING_USERDEF)
271 return CPP_UTF8STRING;
272 else
273 return type;
276 /* Return the user-defined string literal type corresponding to the input
277 string type. If the input type is not a string type return the input
278 type. */
279 enum cpp_ttype
280 cpp_userdef_string_add_type (enum cpp_ttype type)
282 if (type == CPP_STRING)
283 return CPP_STRING_USERDEF;
284 else if (type == CPP_WSTRING)
285 return CPP_WSTRING_USERDEF;
286 else if (type == CPP_STRING16)
287 return CPP_STRING16_USERDEF;
288 else if (type == CPP_STRING32)
289 return CPP_STRING32_USERDEF;
290 else if (type == CPP_UTF8STRING)
291 return CPP_UTF8STRING_USERDEF;
292 else
293 return type;
296 /* Return the char type corresponding to the the input user-defined char
297 literal type. If the input type is not a user-defined char literal
298 type return the input type. */
299 enum cpp_ttype
300 cpp_userdef_char_remove_type (enum cpp_ttype type)
302 if (type == CPP_CHAR_USERDEF)
303 return CPP_CHAR;
304 else if (type == CPP_WCHAR_USERDEF)
305 return CPP_WCHAR;
306 else if (type == CPP_CHAR16_USERDEF)
307 return CPP_CHAR16;
308 else if (type == CPP_CHAR32_USERDEF)
309 return CPP_CHAR32;
310 else
311 return type;
314 /* Return the user-defined char literal type corresponding to the input
315 char type. If the input type is not a char type return the input
316 type. */
317 enum cpp_ttype
318 cpp_userdef_char_add_type (enum cpp_ttype type)
320 if (type == CPP_CHAR)
321 return CPP_CHAR_USERDEF;
322 else if (type == CPP_WCHAR)
323 return CPP_WCHAR_USERDEF;
324 else if (type == CPP_CHAR16)
325 return CPP_CHAR16_USERDEF;
326 else if (type == CPP_CHAR32)
327 return CPP_CHAR32_USERDEF;
328 else
329 return type;
332 /* Return true if the token type is a user-defined string literal. */
333 bool
334 cpp_userdef_string_p (enum cpp_ttype type)
336 if (type == CPP_STRING_USERDEF
337 || type == CPP_WSTRING_USERDEF
338 || type == CPP_STRING16_USERDEF
339 || type == CPP_STRING32_USERDEF
340 || type == CPP_UTF8STRING_USERDEF)
341 return true;
342 else
343 return false;
346 /* Return true if the token type is a user-defined char literal. */
347 bool
348 cpp_userdef_char_p (enum cpp_ttype type)
350 if (type == CPP_CHAR_USERDEF
351 || type == CPP_WCHAR_USERDEF
352 || type == CPP_CHAR16_USERDEF
353 || type == CPP_CHAR32_USERDEF)
354 return true;
355 else
356 return false;
359 /* Extract the suffix from a user-defined literal string or char. */
360 const char *
361 cpp_get_userdef_suffix (const cpp_token *tok)
363 unsigned int len = tok->val.str.len;
364 const char *text = (const char *)tok->val.str.text;
365 char delim;
366 unsigned int i;
367 for (i = 0; i < len; ++i)
368 if (text[i] == '\'' || text[i] == '"')
369 break;
370 if (i == len)
371 return text + len;
372 delim = text[i];
373 for (i = len; i > 0; --i)
374 if (text[i - 1] == delim)
375 break;
376 return text + i;
379 /* Categorize numeric constants according to their field (integer,
380 floating point, or invalid), radix (decimal, octal, hexadecimal),
381 and type suffixes.
383 TOKEN is the token that represents the numeric constant to
384 classify.
386 In C++0X if UD_SUFFIX is non null it will be assigned
387 any unrecognized suffix for a user-defined literal.
389 VIRTUAL_LOCATION is the virtual location for TOKEN. */
390 unsigned int
391 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
392 const char **ud_suffix, source_location virtual_location)
394 const uchar *str = token->val.str.text;
395 const uchar *limit;
396 unsigned int max_digit, result, radix;
397 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
398 bool seen_digit;
399 bool seen_digit_sep;
401 if (ud_suffix)
402 *ud_suffix = NULL;
404 /* If the lexer has done its job, length one can only be a single
405 digit. Fast-path this very common case. */
406 if (token->val.str.len == 1)
407 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
409 limit = str + token->val.str.len;
410 float_flag = NOT_FLOAT;
411 max_digit = 0;
412 radix = 10;
413 seen_digit = false;
414 seen_digit_sep = false;
416 /* First, interpret the radix. */
417 if (*str == '0')
419 radix = 8;
420 str++;
422 /* Require at least one hex digit to classify it as hex. */
423 if (*str == 'x' || *str == 'X')
425 if (str[1] == '.' || ISXDIGIT (str[1]))
427 radix = 16;
428 str++;
430 else if (DIGIT_SEP (str[1]))
431 SYNTAX_ERROR_AT (virtual_location,
432 "digit separator after base indicator");
434 else if (*str == 'b' || *str == 'B')
436 if (str[1] == '0' || str[1] == '1')
438 radix = 2;
439 str++;
441 else if (DIGIT_SEP (str[1]))
442 SYNTAX_ERROR_AT (virtual_location,
443 "digit separator after base indicator");
447 /* Now scan for a well-formed integer or float. */
448 for (;;)
450 unsigned int c = *str++;
452 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
454 seen_digit_sep = false;
455 seen_digit = true;
456 c = hex_value (c);
457 if (c > max_digit)
458 max_digit = c;
460 else if (DIGIT_SEP (c))
462 if (seen_digit_sep)
463 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
464 seen_digit_sep = true;
466 else if (c == '.')
468 if (seen_digit_sep || DIGIT_SEP (*str))
469 SYNTAX_ERROR_AT (virtual_location,
470 "digit separator adjacent to decimal point");
471 seen_digit_sep = false;
472 if (float_flag == NOT_FLOAT)
473 float_flag = AFTER_POINT;
474 else
475 SYNTAX_ERROR_AT (virtual_location,
476 "too many decimal points in number");
478 else if ((radix <= 10 && (c == 'e' || c == 'E'))
479 || (radix == 16 && (c == 'p' || c == 'P')))
481 if (seen_digit_sep || DIGIT_SEP (*str))
482 SYNTAX_ERROR_AT (virtual_location,
483 "digit separator adjacent to exponent");
484 float_flag = AFTER_EXPON;
485 break;
487 else
489 /* Start of suffix. */
490 str--;
491 break;
495 if (seen_digit_sep && float_flag != AFTER_EXPON)
496 SYNTAX_ERROR_AT (virtual_location,
497 "digit separator outside digit sequence");
499 /* The suffix may be for decimal fixed-point constants without exponent. */
500 if (radix != 16 && float_flag == NOT_FLOAT)
502 result = interpret_float_suffix (pfile, str, limit - str);
503 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
505 result |= CPP_N_FLOATING;
506 /* We need to restore the radix to 10, if the radix is 8. */
507 if (radix == 8)
508 radix = 10;
510 if (CPP_PEDANTIC (pfile))
511 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
512 "fixed-point constants are a GCC extension");
513 goto syntax_ok;
515 else
516 result = 0;
519 if (float_flag != NOT_FLOAT && radix == 8)
520 radix = 10;
522 if (max_digit >= radix)
524 if (radix == 2)
525 SYNTAX_ERROR2_AT (virtual_location,
526 "invalid digit \"%c\" in binary constant", '0' + max_digit);
527 else
528 SYNTAX_ERROR2_AT (virtual_location,
529 "invalid digit \"%c\" in octal constant", '0' + max_digit);
532 if (float_flag != NOT_FLOAT)
534 if (radix == 2)
536 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
537 "invalid prefix \"0b\" for floating constant");
538 return CPP_N_INVALID;
541 if (radix == 16 && !seen_digit)
542 SYNTAX_ERROR_AT (virtual_location,
543 "no digits in hexadecimal floating constant");
545 if (radix == 16 && CPP_PEDANTIC (pfile)
546 && !CPP_OPTION (pfile, extended_numbers))
548 if (CPP_OPTION (pfile, cplusplus))
549 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
550 "use of C++11 hexadecimal floating constant");
551 else
552 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
553 "use of C99 hexadecimal floating constant");
556 if (float_flag == AFTER_EXPON)
558 if (*str == '+' || *str == '-')
559 str++;
561 /* Exponent is decimal, even if string is a hex float. */
562 if (!ISDIGIT (*str))
564 if (DIGIT_SEP (*str))
565 SYNTAX_ERROR_AT (virtual_location,
566 "digit separator adjacent to exponent");
567 else
568 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
572 seen_digit_sep = DIGIT_SEP (*str);
573 str++;
575 while (ISDIGIT (*str) || DIGIT_SEP (*str));
577 else if (radix == 16)
578 SYNTAX_ERROR_AT (virtual_location,
579 "hexadecimal floating constants require an exponent");
581 if (seen_digit_sep)
582 SYNTAX_ERROR_AT (virtual_location,
583 "digit separator outside digit sequence");
585 result = interpret_float_suffix (pfile, str, limit - str);
586 if (result == 0)
588 if (CPP_OPTION (pfile, user_literals))
590 if (ud_suffix)
591 *ud_suffix = (const char *) str;
592 result = CPP_N_LARGE | CPP_N_USERDEF;
594 else
596 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
597 "invalid suffix \"%.*s\" on floating constant",
598 (int) (limit - str), str);
599 return CPP_N_INVALID;
603 /* Traditional C didn't accept any floating suffixes. */
604 if (limit != str
605 && CPP_WTRADITIONAL (pfile)
606 && ! cpp_sys_macro_p (pfile))
607 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
608 "traditional C rejects the \"%.*s\" suffix",
609 (int) (limit - str), str);
611 /* A suffix for double is a GCC extension via decimal float support.
612 If the suffix also specifies an imaginary value we'll catch that
613 later. */
614 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
615 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
616 "suffix for double constant is a GCC extension");
618 /* Radix must be 10 for decimal floats. */
619 if ((result & CPP_N_DFLOAT) && radix != 10)
621 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
622 "invalid suffix \"%.*s\" with hexadecimal floating constant",
623 (int) (limit - str), str);
624 return CPP_N_INVALID;
627 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
628 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
629 "fixed-point constants are a GCC extension");
631 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
632 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
633 "decimal float constants are a GCC extension");
635 result |= CPP_N_FLOATING;
637 else
639 result = interpret_int_suffix (pfile, str, limit - str);
640 if (result == 0)
642 if (CPP_OPTION (pfile, user_literals))
644 if (ud_suffix)
645 *ud_suffix = (const char *) str;
646 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
648 else
650 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
651 "invalid suffix \"%.*s\" on integer constant",
652 (int) (limit - str), str);
653 return CPP_N_INVALID;
657 /* Traditional C only accepted the 'L' suffix.
658 Suppress warning about 'LL' with -Wno-long-long. */
659 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
661 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
662 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
663 && CPP_OPTION (pfile, cpp_warn_long_long);
665 if (u_or_i || large)
666 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
667 virtual_location, 0,
668 "traditional C rejects the \"%.*s\" suffix",
669 (int) (limit - str), str);
672 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
673 && CPP_OPTION (pfile, cpp_warn_long_long))
675 const char *message = CPP_OPTION (pfile, cplusplus)
676 ? N_("use of C++11 long long integer constant")
677 : N_("use of C99 long long integer constant");
679 if (CPP_OPTION (pfile, c99))
680 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
681 0, message);
682 else
683 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
684 virtual_location, 0, message);
687 result |= CPP_N_INTEGER;
690 syntax_ok:
691 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
692 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
693 "imaginary constants are a GCC extension");
694 if (radix == 2
695 && !CPP_OPTION (pfile, binary_constants)
696 && CPP_PEDANTIC (pfile))
697 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
698 CPP_OPTION (pfile, cplusplus)
699 ? N_("binary constants are a C++14 feature "
700 "or GCC extension")
701 : N_("binary constants are a GCC extension"));
703 if (radix == 10)
704 result |= CPP_N_DECIMAL;
705 else if (radix == 16)
706 result |= CPP_N_HEX;
707 else if (radix == 2)
708 result |= CPP_N_BINARY;
709 else
710 result |= CPP_N_OCTAL;
712 return result;
714 syntax_error:
715 return CPP_N_INVALID;
718 /* cpp_interpret_integer converts an integer constant into a cpp_num,
719 of precision options->precision.
721 We do not provide any interface for decimal->float conversion,
722 because the preprocessor doesn't need it and we don't want to
723 drag in GCC's floating point emulator. */
724 cpp_num
725 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
726 unsigned int type)
728 const uchar *p, *end;
729 cpp_num result;
731 result.low = 0;
732 result.high = 0;
733 result.unsignedp = !!(type & CPP_N_UNSIGNED);
734 result.overflow = false;
736 p = token->val.str.text;
737 end = p + token->val.str.len;
739 /* Common case of a single digit. */
740 if (token->val.str.len == 1)
741 result.low = p[0] - '0';
742 else
744 cpp_num_part max;
745 size_t precision = CPP_OPTION (pfile, precision);
746 unsigned int base = 10, c = 0;
747 bool overflow = false;
749 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
751 base = 8;
752 p++;
754 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
756 base = 16;
757 p += 2;
759 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
761 base = 2;
762 p += 2;
765 /* We can add a digit to numbers strictly less than this without
766 needing the precision and slowness of double integers. */
767 max = ~(cpp_num_part) 0;
768 if (precision < PART_PRECISION)
769 max >>= PART_PRECISION - precision;
770 max = (max - base + 1) / base + 1;
772 for (; p < end; p++)
774 c = *p;
776 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
777 c = hex_value (c);
778 else if (DIGIT_SEP (c))
779 continue;
780 else
781 break;
783 /* Strict inequality for when max is set to zero. */
784 if (result.low < max)
785 result.low = result.low * base + c;
786 else
788 result = append_digit (result, c, base, precision);
789 overflow |= result.overflow;
790 max = 0;
794 if (overflow && !(type & CPP_N_USERDEF))
795 cpp_error (pfile, CPP_DL_PEDWARN,
796 "integer constant is too large for its type");
797 /* If too big to be signed, consider it unsigned. Only warn for
798 decimal numbers. Traditional numbers were always signed (but
799 we still honor an explicit U suffix); but we only have
800 traditional semantics in directives. */
801 else if (!result.unsignedp
802 && !(CPP_OPTION (pfile, traditional)
803 && pfile->state.in_directive)
804 && !num_positive (result, precision))
806 /* This is for constants within the range of uintmax_t but
807 not that of intmax_t. For such decimal constants, a
808 diagnostic is required for C99 as the selected type must
809 be signed and not having a type is a constraint violation
810 (DR#298, TC3), so this must be a pedwarn. For C90,
811 unsigned long is specified to be used for a constant that
812 does not fit in signed long; if uintmax_t has the same
813 range as unsigned long this means only a warning is
814 appropriate here. C90 permits the preprocessor to use a
815 wider range than unsigned long in the compiler, so if
816 uintmax_t is wider than unsigned long no diagnostic is
817 required for such constants in preprocessor #if
818 expressions and the compiler will pedwarn for such
819 constants outside the range of unsigned long that reach
820 the compiler so a diagnostic is not required there
821 either; thus, pedwarn for C99 but use a plain warning for
822 C90. */
823 if (base == 10)
824 cpp_error (pfile, (CPP_OPTION (pfile, c99)
825 ? CPP_DL_PEDWARN
826 : CPP_DL_WARNING),
827 "integer constant is so large that it is unsigned");
828 result.unsignedp = true;
832 return result;
835 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
836 static cpp_num
837 append_digit (cpp_num num, int digit, int base, size_t precision)
839 cpp_num result;
840 unsigned int shift;
841 bool overflow;
842 cpp_num_part add_high, add_low;
844 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
845 need to worry about add_high overflowing. */
846 switch (base)
848 case 2:
849 shift = 1;
850 break;
852 case 16:
853 shift = 4;
854 break;
856 default:
857 shift = 3;
859 overflow = !!(num.high >> (PART_PRECISION - shift));
860 result.high = num.high << shift;
861 result.low = num.low << shift;
862 result.high |= num.low >> (PART_PRECISION - shift);
863 result.unsignedp = num.unsignedp;
865 if (base == 10)
867 add_low = num.low << 1;
868 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
870 else
871 add_high = add_low = 0;
873 if (add_low + digit < add_low)
874 add_high++;
875 add_low += digit;
877 if (result.low + add_low < result.low)
878 add_high++;
879 if (result.high + add_high < result.high)
880 overflow = true;
882 result.low += add_low;
883 result.high += add_high;
884 result.overflow = overflow;
886 /* The above code catches overflow of a cpp_num type. This catches
887 overflow of the (possibly shorter) target precision. */
888 num.low = result.low;
889 num.high = result.high;
890 result = num_trim (result, precision);
891 if (!num_eq (result, num))
892 result.overflow = true;
894 return result;
897 /* Handle meeting "defined" in a preprocessor expression. */
898 static cpp_num
899 parse_defined (cpp_reader *pfile)
901 cpp_num result;
902 int paren = 0;
903 cpp_hashnode *node = 0;
904 const cpp_token *token;
905 cpp_context *initial_context = pfile->context;
907 /* Don't expand macros. */
908 pfile->state.prevent_expansion++;
910 token = cpp_get_token (pfile);
911 if (token->type == CPP_OPEN_PAREN)
913 paren = 1;
914 token = cpp_get_token (pfile);
917 if (token->type == CPP_NAME)
919 node = token->val.node.node;
920 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
922 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
923 node = 0;
926 else
928 cpp_error (pfile, CPP_DL_ERROR,
929 "operator \"defined\" requires an identifier");
930 if (token->flags & NAMED_OP)
932 cpp_token op;
934 op.flags = 0;
935 op.type = token->type;
936 cpp_error (pfile, CPP_DL_ERROR,
937 "(\"%s\" is an alternative token for \"%s\" in C++)",
938 cpp_token_as_text (pfile, token),
939 cpp_token_as_text (pfile, &op));
943 if (node)
945 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
946 cpp_error (pfile, CPP_DL_WARNING,
947 "this use of \"defined\" may not be portable");
949 _cpp_mark_macro_used (node);
950 if (!(node->flags & NODE_USED))
952 node->flags |= NODE_USED;
953 if (node->type == NT_MACRO)
955 if ((node->flags & NODE_BUILTIN)
956 && pfile->cb.user_builtin_macro)
957 pfile->cb.user_builtin_macro (pfile, node);
958 if (pfile->cb.used_define)
959 pfile->cb.used_define (pfile, pfile->directive_line, node);
961 else
963 if (pfile->cb.used_undef)
964 pfile->cb.used_undef (pfile, pfile->directive_line, node);
968 /* A possible controlling macro of the form #if !defined ().
969 _cpp_parse_expr checks there was no other junk on the line. */
970 pfile->mi_ind_cmacro = node;
973 pfile->state.prevent_expansion--;
975 /* Do not treat conditional macros as being defined. This is due to the
976 powerpc and spu ports using conditional macros for 'vector', 'bool', and
977 'pixel' to act as conditional keywords. This messes up tests like #ifndef
978 bool. */
979 result.unsignedp = false;
980 result.high = 0;
981 result.overflow = false;
982 result.low = (node && node->type == NT_MACRO
983 && (node->flags & NODE_CONDITIONAL) == 0);
984 return result;
987 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
988 number or character constant, or the result of the "defined" or "#"
989 operators). */
990 static cpp_num
991 eval_token (cpp_reader *pfile, const cpp_token *token,
992 source_location virtual_location)
994 cpp_num result;
995 unsigned int temp;
996 int unsignedp = 0;
998 result.unsignedp = false;
999 result.overflow = false;
1001 switch (token->type)
1003 case CPP_NUMBER:
1004 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1005 if (temp & CPP_N_USERDEF)
1006 cpp_error (pfile, CPP_DL_ERROR,
1007 "user-defined literal in preprocessor expression");
1008 switch (temp & CPP_N_CATEGORY)
1010 case CPP_N_FLOATING:
1011 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1012 "floating constant in preprocessor expression");
1013 break;
1014 case CPP_N_INTEGER:
1015 if (!(temp & CPP_N_IMAGINARY))
1016 return cpp_interpret_integer (pfile, token, temp);
1017 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1018 "imaginary number in preprocessor expression");
1019 break;
1021 case CPP_N_INVALID:
1022 /* Error already issued. */
1023 break;
1025 result.high = result.low = 0;
1026 break;
1028 case CPP_WCHAR:
1029 case CPP_CHAR:
1030 case CPP_CHAR16:
1031 case CPP_CHAR32:
1033 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1034 &temp, &unsignedp);
1036 result.high = 0;
1037 result.low = cc;
1038 /* Sign-extend the result if necessary. */
1039 if (!unsignedp && (cppchar_signed_t) cc < 0)
1041 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1042 result.low |= ~(~(cpp_num_part) 0
1043 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1044 result.high = ~(cpp_num_part) 0;
1045 result = num_trim (result, CPP_OPTION (pfile, precision));
1048 break;
1050 case CPP_NAME:
1051 if (token->val.node.node == pfile->spec_nodes.n_defined)
1052 return parse_defined (pfile);
1053 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1054 return parse_has_include (pfile, IT_INCLUDE);
1055 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1056 return parse_has_include (pfile, IT_INCLUDE_NEXT);
1057 else if (CPP_OPTION (pfile, cplusplus)
1058 && (token->val.node.node == pfile->spec_nodes.n_true
1059 || token->val.node.node == pfile->spec_nodes.n_false))
1061 result.high = 0;
1062 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1064 else
1066 result.high = 0;
1067 result.low = 0;
1068 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1069 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1070 "\"%s\" is not defined",
1071 NODE_NAME (token->val.node.node));
1073 break;
1075 case CPP_HASH:
1076 if (!pfile->state.skipping)
1078 /* A pedantic warning takes precedence over a deprecated
1079 warning here. */
1080 if (CPP_PEDANTIC (pfile))
1081 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1082 virtual_location, 0,
1083 "assertions are a GCC extension");
1084 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1085 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1086 "assertions are a deprecated extension");
1088 _cpp_test_assertion (pfile, &temp);
1089 result.high = 0;
1090 result.low = temp;
1091 break;
1093 default:
1094 abort ();
1097 result.unsignedp = !!unsignedp;
1098 return result;
1101 /* Operator precedence and flags table.
1103 After an operator is returned from the lexer, if it has priority less
1104 than the operator on the top of the stack, we reduce the stack by one
1105 operator and repeat the test. Since equal priorities do not reduce,
1106 this is naturally right-associative.
1108 We handle left-associative operators by decrementing the priority of
1109 just-lexed operators by one, but retaining the priority of operators
1110 already on the stack.
1112 The remaining cases are '(' and ')'. We handle '(' by skipping the
1113 reduction phase completely. ')' is given lower priority than
1114 everything else, including '(', effectively forcing a reduction of the
1115 parenthesized expression. If there is a matching '(', the routine
1116 reduce() exits immediately. If the normal exit route sees a ')', then
1117 there cannot have been a matching '(' and an error message is output.
1119 The parser assumes all shifted operators require a left operand unless
1120 the flag NO_L_OPERAND is set. These semantics are automatic; any
1121 extra semantics need to be handled with operator-specific code. */
1123 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1124 operand changes because of integer promotions. */
1125 #define NO_L_OPERAND (1 << 0)
1126 #define LEFT_ASSOC (1 << 1)
1127 #define CHECK_PROMOTION (1 << 2)
1129 /* Operator to priority map. Must be in the same order as the first
1130 N entries of enum cpp_ttype. */
1131 static const struct cpp_operator
1133 uchar prio;
1134 uchar flags;
1135 } optab[] =
1137 /* EQ */ {0, 0}, /* Shouldn't happen. */
1138 /* NOT */ {16, NO_L_OPERAND},
1139 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1140 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1141 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1142 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1143 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1144 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1145 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1146 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1147 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1148 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1149 /* RSHIFT */ {13, LEFT_ASSOC},
1150 /* LSHIFT */ {13, LEFT_ASSOC},
1152 /* COMPL */ {16, NO_L_OPERAND},
1153 /* AND_AND */ {6, LEFT_ASSOC},
1154 /* OR_OR */ {5, LEFT_ASSOC},
1155 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1156 However, there are some special cases for these in reduce(). */
1157 /* QUERY */ {4, 0},
1158 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1159 /* COMMA */ {4, LEFT_ASSOC},
1160 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1161 /* CLOSE_PAREN */ {0, 0},
1162 /* EOF */ {0, 0},
1163 /* EQ_EQ */ {11, LEFT_ASSOC},
1164 /* NOT_EQ */ {11, LEFT_ASSOC},
1165 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1166 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1167 /* UPLUS */ {16, NO_L_OPERAND},
1168 /* UMINUS */ {16, NO_L_OPERAND}
1171 /* Parse and evaluate a C expression, reading from PFILE.
1172 Returns the truth value of the expression.
1174 The implementation is an operator precedence parser, i.e. a
1175 bottom-up parser, using a stack for not-yet-reduced tokens.
1177 The stack base is op_stack, and the current stack pointer is 'top'.
1178 There is a stack element for each operator (only), and the most
1179 recently pushed operator is 'top->op'. An operand (value) is
1180 stored in the 'value' field of the stack element of the operator
1181 that precedes it. */
1182 bool
1183 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1185 struct op *top = pfile->op_stack;
1186 unsigned int lex_count;
1187 bool saw_leading_not, want_value = true;
1188 source_location virtual_location = 0;
1190 pfile->state.skip_eval = 0;
1192 /* Set up detection of #if ! defined(). */
1193 pfile->mi_ind_cmacro = 0;
1194 saw_leading_not = false;
1195 lex_count = 0;
1197 /* Lowest priority operator prevents further reductions. */
1198 top->op = CPP_EOF;
1200 for (;;)
1202 struct op op;
1204 lex_count++;
1205 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1206 op.op = op.token->type;
1207 op.loc = virtual_location;
1209 switch (op.op)
1211 /* These tokens convert into values. */
1212 case CPP_NUMBER:
1213 case CPP_CHAR:
1214 case CPP_WCHAR:
1215 case CPP_CHAR16:
1216 case CPP_CHAR32:
1217 case CPP_NAME:
1218 case CPP_HASH:
1219 if (!want_value)
1220 SYNTAX_ERROR2_AT (op.loc,
1221 "missing binary operator before token \"%s\"",
1222 cpp_token_as_text (pfile, op.token));
1223 want_value = false;
1224 top->value = eval_token (pfile, op.token, op.loc);
1225 continue;
1227 case CPP_NOT:
1228 saw_leading_not = lex_count == 1;
1229 break;
1230 case CPP_PLUS:
1231 if (want_value)
1232 op.op = CPP_UPLUS;
1233 break;
1234 case CPP_MINUS:
1235 if (want_value)
1236 op.op = CPP_UMINUS;
1237 break;
1239 default:
1240 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1241 SYNTAX_ERROR2_AT (op.loc,
1242 "token \"%s\" is not valid in preprocessor expressions",
1243 cpp_token_as_text (pfile, op.token));
1244 break;
1247 /* Check we have a value or operator as appropriate. */
1248 if (optab[op.op].flags & NO_L_OPERAND)
1250 if (!want_value)
1251 SYNTAX_ERROR2_AT (op.loc,
1252 "missing binary operator before token \"%s\"",
1253 cpp_token_as_text (pfile, op.token));
1255 else if (want_value)
1257 /* We want a number (or expression) and haven't got one.
1258 Try to emit a specific diagnostic. */
1259 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1260 SYNTAX_ERROR_AT (op.loc,
1261 "missing expression between '(' and ')'");
1263 if (op.op == CPP_EOF && top->op == CPP_EOF)
1264 SYNTAX_ERROR2_AT (op.loc,
1265 "%s with no expression", is_if ? "#if" : "#elif");
1267 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1268 SYNTAX_ERROR2_AT (op.loc,
1269 "operator '%s' has no right operand",
1270 cpp_token_as_text (pfile, top->token));
1271 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1272 /* Complain about missing paren during reduction. */;
1273 else
1274 SYNTAX_ERROR2_AT (op.loc,
1275 "operator '%s' has no left operand",
1276 cpp_token_as_text (pfile, op.token));
1279 top = reduce (pfile, top, op.op);
1280 if (!top)
1281 goto syntax_error;
1283 if (op.op == CPP_EOF)
1284 break;
1286 switch (op.op)
1288 case CPP_CLOSE_PAREN:
1289 continue;
1290 case CPP_OR_OR:
1291 if (!num_zerop (top->value))
1292 pfile->state.skip_eval++;
1293 break;
1294 case CPP_AND_AND:
1295 case CPP_QUERY:
1296 if (num_zerop (top->value))
1297 pfile->state.skip_eval++;
1298 break;
1299 case CPP_COLON:
1300 if (top->op != CPP_QUERY)
1301 SYNTAX_ERROR_AT (op.loc,
1302 " ':' without preceding '?'");
1303 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1304 pfile->state.skip_eval++;
1305 else
1306 pfile->state.skip_eval--;
1307 default:
1308 break;
1311 want_value = true;
1313 /* Check for and handle stack overflow. */
1314 if (++top == pfile->op_limit)
1315 top = _cpp_expand_op_stack (pfile);
1317 top->op = op.op;
1318 top->token = op.token;
1319 top->loc = op.loc;
1322 /* The controlling macro expression is only valid if we called lex 3
1323 times: <!> <defined expression> and <EOF>. push_conditional ()
1324 checks that we are at top-of-file. */
1325 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1326 pfile->mi_ind_cmacro = 0;
1328 if (top != pfile->op_stack)
1330 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1331 "unbalanced stack in %s",
1332 is_if ? "#if" : "#elif");
1333 syntax_error:
1334 return false; /* Return false on syntax error. */
1337 return !num_zerop (top->value);
1340 /* Reduce the operator / value stack if possible, in preparation for
1341 pushing operator OP. Returns NULL on error, otherwise the top of
1342 the stack. */
1343 static struct op *
1344 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1346 unsigned int prio;
1348 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1350 bad_op:
1351 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1352 return 0;
1355 if (op == CPP_OPEN_PAREN)
1356 return top;
1358 /* Decrement the priority of left-associative operators to force a
1359 reduction with operators of otherwise equal priority. */
1360 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1361 while (prio < optab[top->op].prio)
1363 if (CPP_OPTION (pfile, warn_num_sign_change)
1364 && optab[top->op].flags & CHECK_PROMOTION)
1365 check_promotion (pfile, top);
1367 switch (top->op)
1369 case CPP_UPLUS:
1370 case CPP_UMINUS:
1371 case CPP_NOT:
1372 case CPP_COMPL:
1373 top[-1].value = num_unary_op (pfile, top->value, top->op);
1374 top[-1].loc = top->loc;
1375 break;
1377 case CPP_PLUS:
1378 case CPP_MINUS:
1379 case CPP_RSHIFT:
1380 case CPP_LSHIFT:
1381 case CPP_COMMA:
1382 top[-1].value = num_binary_op (pfile, top[-1].value,
1383 top->value, top->op);
1384 top[-1].loc = top->loc;
1385 break;
1387 case CPP_GREATER:
1388 case CPP_LESS:
1389 case CPP_GREATER_EQ:
1390 case CPP_LESS_EQ:
1391 top[-1].value
1392 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1393 top[-1].loc = top->loc;
1394 break;
1396 case CPP_EQ_EQ:
1397 case CPP_NOT_EQ:
1398 top[-1].value
1399 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1400 top[-1].loc = top->loc;
1401 break;
1403 case CPP_AND:
1404 case CPP_OR:
1405 case CPP_XOR:
1406 top[-1].value
1407 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1408 top[-1].loc = top->loc;
1409 break;
1411 case CPP_MULT:
1412 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1413 top[-1].loc = top->loc;
1414 break;
1416 case CPP_DIV:
1417 case CPP_MOD:
1418 top[-1].value = num_div_op (pfile, top[-1].value,
1419 top->value, top->op, top->loc);
1420 top[-1].loc = top->loc;
1421 break;
1423 case CPP_OR_OR:
1424 top--;
1425 if (!num_zerop (top->value))
1426 pfile->state.skip_eval--;
1427 top->value.low = (!num_zerop (top->value)
1428 || !num_zerop (top[1].value));
1429 top->value.high = 0;
1430 top->value.unsignedp = false;
1431 top->value.overflow = false;
1432 top->loc = top[1].loc;
1433 continue;
1435 case CPP_AND_AND:
1436 top--;
1437 if (num_zerop (top->value))
1438 pfile->state.skip_eval--;
1439 top->value.low = (!num_zerop (top->value)
1440 && !num_zerop (top[1].value));
1441 top->value.high = 0;
1442 top->value.unsignedp = false;
1443 top->value.overflow = false;
1444 top->loc = top[1].loc;
1445 continue;
1447 case CPP_OPEN_PAREN:
1448 if (op != CPP_CLOSE_PAREN)
1450 cpp_error_with_line (pfile, CPP_DL_ERROR,
1451 top->token->src_loc,
1452 0, "missing ')' in expression");
1453 return 0;
1455 top--;
1456 top->value = top[1].value;
1457 top->loc = top[1].loc;
1458 return top;
1460 case CPP_COLON:
1461 top -= 2;
1462 if (!num_zerop (top->value))
1464 pfile->state.skip_eval--;
1465 top->value = top[1].value;
1466 top->loc = top[1].loc;
1468 else
1470 top->value = top[2].value;
1471 top->loc = top[2].loc;
1473 top->value.unsignedp = (top[1].value.unsignedp
1474 || top[2].value.unsignedp);
1475 continue;
1477 case CPP_QUERY:
1478 /* COMMA and COLON should not reduce a QUERY operator. */
1479 if (op == CPP_COMMA || op == CPP_COLON)
1480 return top;
1481 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1482 return 0;
1484 default:
1485 goto bad_op;
1488 top--;
1489 if (top->value.overflow && !pfile->state.skip_eval)
1490 cpp_error (pfile, CPP_DL_PEDWARN,
1491 "integer overflow in preprocessor expression");
1494 if (op == CPP_CLOSE_PAREN)
1496 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1497 return 0;
1500 return top;
1503 /* Returns the position of the old top of stack after expansion. */
1504 struct op *
1505 _cpp_expand_op_stack (cpp_reader *pfile)
1507 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1508 size_t new_size = old_size * 2 + 20;
1510 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1511 pfile->op_limit = pfile->op_stack + new_size;
1513 return pfile->op_stack + old_size;
1516 /* Emits a warning if the effective sign of either operand of OP
1517 changes because of integer promotions. */
1518 static void
1519 check_promotion (cpp_reader *pfile, const struct op *op)
1521 if (op->value.unsignedp == op[-1].value.unsignedp)
1522 return;
1524 if (op->value.unsignedp)
1526 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1527 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1528 "the left operand of \"%s\" changes sign when promoted",
1529 cpp_token_as_text (pfile, op->token));
1531 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1532 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1533 "the right operand of \"%s\" changes sign when promoted",
1534 cpp_token_as_text (pfile, op->token));
1537 /* Clears the unused high order bits of the number pointed to by PNUM. */
1538 static cpp_num
1539 num_trim (cpp_num num, size_t precision)
1541 if (precision > PART_PRECISION)
1543 precision -= PART_PRECISION;
1544 if (precision < PART_PRECISION)
1545 num.high &= ((cpp_num_part) 1 << precision) - 1;
1547 else
1549 if (precision < PART_PRECISION)
1550 num.low &= ((cpp_num_part) 1 << precision) - 1;
1551 num.high = 0;
1554 return num;
1557 /* True iff A (presumed signed) >= 0. */
1558 static bool
1559 num_positive (cpp_num num, size_t precision)
1561 if (precision > PART_PRECISION)
1563 precision -= PART_PRECISION;
1564 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1567 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1570 /* Sign extend a number, with PRECISION significant bits and all
1571 others assumed clear, to fill out a cpp_num structure. */
1572 cpp_num
1573 cpp_num_sign_extend (cpp_num num, size_t precision)
1575 if (!num.unsignedp)
1577 if (precision > PART_PRECISION)
1579 precision -= PART_PRECISION;
1580 if (precision < PART_PRECISION
1581 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1582 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1584 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1586 if (precision < PART_PRECISION)
1587 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1588 num.high = ~(cpp_num_part) 0;
1592 return num;
1595 /* Returns the negative of NUM. */
1596 static cpp_num
1597 num_negate (cpp_num num, size_t precision)
1599 cpp_num copy;
1601 copy = num;
1602 num.high = ~num.high;
1603 num.low = ~num.low;
1604 if (++num.low == 0)
1605 num.high++;
1606 num = num_trim (num, precision);
1607 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1609 return num;
1612 /* Returns true if A >= B. */
1613 static bool
1614 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1616 bool unsignedp;
1618 unsignedp = pa.unsignedp || pb.unsignedp;
1620 if (!unsignedp)
1622 /* Both numbers have signed type. If they are of different
1623 sign, the answer is the sign of A. */
1624 unsignedp = num_positive (pa, precision);
1626 if (unsignedp != num_positive (pb, precision))
1627 return unsignedp;
1629 /* Otherwise we can do an unsigned comparison. */
1632 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1635 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1636 static cpp_num
1637 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1638 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1640 lhs.overflow = false;
1641 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1643 /* As excess precision is zeroed, there is no need to num_trim () as
1644 these operations cannot introduce a set bit there. */
1645 if (op == CPP_AND)
1647 lhs.low &= rhs.low;
1648 lhs.high &= rhs.high;
1650 else if (op == CPP_OR)
1652 lhs.low |= rhs.low;
1653 lhs.high |= rhs.high;
1655 else
1657 lhs.low ^= rhs.low;
1658 lhs.high ^= rhs.high;
1661 return lhs;
1664 /* Returns LHS OP RHS, where OP is an inequality. */
1665 static cpp_num
1666 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1667 enum cpp_ttype op)
1669 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1671 if (op == CPP_GREATER_EQ)
1672 lhs.low = gte;
1673 else if (op == CPP_LESS)
1674 lhs.low = !gte;
1675 else if (op == CPP_GREATER)
1676 lhs.low = gte && !num_eq (lhs, rhs);
1677 else /* CPP_LESS_EQ. */
1678 lhs.low = !gte || num_eq (lhs, rhs);
1680 lhs.high = 0;
1681 lhs.overflow = false;
1682 lhs.unsignedp = false;
1683 return lhs;
1686 /* Returns LHS OP RHS, where OP is == or !=. */
1687 static cpp_num
1688 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1689 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1691 /* Work around a 3.0.4 bug; see PR 6950. */
1692 bool eq = num_eq (lhs, rhs);
1693 if (op == CPP_NOT_EQ)
1694 eq = !eq;
1695 lhs.low = eq;
1696 lhs.high = 0;
1697 lhs.overflow = false;
1698 lhs.unsignedp = false;
1699 return lhs;
1702 /* Shift NUM, of width PRECISION, right by N bits. */
1703 static cpp_num
1704 num_rshift (cpp_num num, size_t precision, size_t n)
1706 cpp_num_part sign_mask;
1707 bool x = num_positive (num, precision);
1709 if (num.unsignedp || x)
1710 sign_mask = 0;
1711 else
1712 sign_mask = ~(cpp_num_part) 0;
1714 if (n >= precision)
1715 num.high = num.low = sign_mask;
1716 else
1718 /* Sign-extend. */
1719 if (precision < PART_PRECISION)
1720 num.high = sign_mask, num.low |= sign_mask << precision;
1721 else if (precision < 2 * PART_PRECISION)
1722 num.high |= sign_mask << (precision - PART_PRECISION);
1724 if (n >= PART_PRECISION)
1726 n -= PART_PRECISION;
1727 num.low = num.high;
1728 num.high = sign_mask;
1731 if (n)
1733 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1734 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1738 num = num_trim (num, precision);
1739 num.overflow = false;
1740 return num;
1743 /* Shift NUM, of width PRECISION, left by N bits. */
1744 static cpp_num
1745 num_lshift (cpp_num num, size_t precision, size_t n)
1747 if (n >= precision)
1749 num.overflow = !num.unsignedp && !num_zerop (num);
1750 num.high = num.low = 0;
1752 else
1754 cpp_num orig, maybe_orig;
1755 size_t m = n;
1757 orig = num;
1758 if (m >= PART_PRECISION)
1760 m -= PART_PRECISION;
1761 num.high = num.low;
1762 num.low = 0;
1764 if (m)
1766 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1767 num.low <<= m;
1769 num = num_trim (num, precision);
1771 if (num.unsignedp)
1772 num.overflow = false;
1773 else
1775 maybe_orig = num_rshift (num, precision, n);
1776 num.overflow = !num_eq (orig, maybe_orig);
1780 return num;
1783 /* The four unary operators: +, -, ! and ~. */
1784 static cpp_num
1785 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1787 switch (op)
1789 case CPP_UPLUS:
1790 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1791 cpp_warning (pfile, CPP_W_TRADITIONAL,
1792 "traditional C rejects the unary plus operator");
1793 num.overflow = false;
1794 break;
1796 case CPP_UMINUS:
1797 num = num_negate (num, CPP_OPTION (pfile, precision));
1798 break;
1800 case CPP_COMPL:
1801 num.high = ~num.high;
1802 num.low = ~num.low;
1803 num = num_trim (num, CPP_OPTION (pfile, precision));
1804 num.overflow = false;
1805 break;
1807 default: /* case CPP_NOT: */
1808 num.low = num_zerop (num);
1809 num.high = 0;
1810 num.overflow = false;
1811 num.unsignedp = false;
1812 break;
1815 return num;
1818 /* The various binary operators. */
1819 static cpp_num
1820 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1822 cpp_num result;
1823 size_t precision = CPP_OPTION (pfile, precision);
1824 size_t n;
1826 switch (op)
1828 /* Shifts. */
1829 case CPP_LSHIFT:
1830 case CPP_RSHIFT:
1831 if (!rhs.unsignedp && !num_positive (rhs, precision))
1833 /* A negative shift is a positive shift the other way. */
1834 if (op == CPP_LSHIFT)
1835 op = CPP_RSHIFT;
1836 else
1837 op = CPP_LSHIFT;
1838 rhs = num_negate (rhs, precision);
1840 if (rhs.high)
1841 n = ~0; /* Maximal. */
1842 else
1843 n = rhs.low;
1844 if (op == CPP_LSHIFT)
1845 lhs = num_lshift (lhs, precision, n);
1846 else
1847 lhs = num_rshift (lhs, precision, n);
1848 break;
1850 /* Arithmetic. */
1851 case CPP_MINUS:
1852 result.low = lhs.low - rhs.low;
1853 result.high = lhs.high - rhs.high;
1854 if (result.low > lhs.low)
1855 result.high--;
1856 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1857 result.overflow = false;
1859 result = num_trim (result, precision);
1860 if (!result.unsignedp)
1862 bool lhsp = num_positive (lhs, precision);
1863 result.overflow = (lhsp != num_positive (rhs, precision)
1864 && lhsp != num_positive (result, precision));
1866 return result;
1868 case CPP_PLUS:
1869 result.low = lhs.low + rhs.low;
1870 result.high = lhs.high + rhs.high;
1871 if (result.low < lhs.low)
1872 result.high++;
1873 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1874 result.overflow = false;
1876 result = num_trim (result, precision);
1877 if (!result.unsignedp)
1879 bool lhsp = num_positive (lhs, precision);
1880 result.overflow = (lhsp == num_positive (rhs, precision)
1881 && lhsp != num_positive (result, precision));
1883 return result;
1885 /* Comma. */
1886 default: /* case CPP_COMMA: */
1887 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1888 || !pfile->state.skip_eval))
1889 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1890 "comma operator in operand of #if");
1891 lhs = rhs;
1892 break;
1895 return lhs;
1898 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1899 cannot overflow. */
1900 static cpp_num
1901 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1903 cpp_num result;
1904 cpp_num_part middle[2], temp;
1906 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1907 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1909 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1910 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1912 temp = result.low;
1913 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1914 if (result.low < temp)
1915 result.high++;
1917 temp = result.low;
1918 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1919 if (result.low < temp)
1920 result.high++;
1922 result.high += HIGH_PART (middle[0]);
1923 result.high += HIGH_PART (middle[1]);
1924 result.unsignedp = true;
1925 result.overflow = false;
1927 return result;
1930 /* Multiply two preprocessing numbers. */
1931 static cpp_num
1932 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1934 cpp_num result, temp;
1935 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1936 bool overflow, negate = false;
1937 size_t precision = CPP_OPTION (pfile, precision);
1939 /* Prepare for unsigned multiplication. */
1940 if (!unsignedp)
1942 if (!num_positive (lhs, precision))
1943 negate = !negate, lhs = num_negate (lhs, precision);
1944 if (!num_positive (rhs, precision))
1945 negate = !negate, rhs = num_negate (rhs, precision);
1948 overflow = lhs.high && rhs.high;
1949 result = num_part_mul (lhs.low, rhs.low);
1951 temp = num_part_mul (lhs.high, rhs.low);
1952 result.high += temp.low;
1953 if (temp.high)
1954 overflow = true;
1956 temp = num_part_mul (lhs.low, rhs.high);
1957 result.high += temp.low;
1958 if (temp.high)
1959 overflow = true;
1961 temp.low = result.low, temp.high = result.high;
1962 result = num_trim (result, precision);
1963 if (!num_eq (result, temp))
1964 overflow = true;
1966 if (negate)
1967 result = num_negate (result, precision);
1969 if (unsignedp)
1970 result.overflow = false;
1971 else
1972 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1973 && !num_zerop (result));
1974 result.unsignedp = unsignedp;
1976 return result;
1979 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1980 or the remainder depending upon OP. LOCATION is the source location
1981 of this operator (for diagnostics). */
1983 static cpp_num
1984 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1985 source_location location)
1987 cpp_num result, sub;
1988 cpp_num_part mask;
1989 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1990 bool negate = false, lhs_neg = false;
1991 size_t i, precision = CPP_OPTION (pfile, precision);
1993 /* Prepare for unsigned division. */
1994 if (!unsignedp)
1996 if (!num_positive (lhs, precision))
1997 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1998 if (!num_positive (rhs, precision))
1999 negate = !negate, rhs = num_negate (rhs, precision);
2002 /* Find the high bit. */
2003 if (rhs.high)
2005 i = precision - 1;
2006 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2007 for (; ; i--, mask >>= 1)
2008 if (rhs.high & mask)
2009 break;
2011 else if (rhs.low)
2013 if (precision > PART_PRECISION)
2014 i = precision - PART_PRECISION - 1;
2015 else
2016 i = precision - 1;
2017 mask = (cpp_num_part) 1 << i;
2018 for (; ; i--, mask >>= 1)
2019 if (rhs.low & mask)
2020 break;
2022 else
2024 if (!pfile->state.skip_eval)
2025 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2026 "division by zero in #if");
2027 return lhs;
2030 /* First nonzero bit of RHS is bit I. Do naive division by
2031 shifting the RHS fully left, and subtracting from LHS if LHS is
2032 at least as big, and then repeating but with one less shift.
2033 This is not very efficient, but is easy to understand. */
2035 rhs.unsignedp = true;
2036 lhs.unsignedp = true;
2037 i = precision - i - 1;
2038 sub = num_lshift (rhs, precision, i);
2040 result.high = result.low = 0;
2041 for (;;)
2043 if (num_greater_eq (lhs, sub, precision))
2045 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2046 if (i >= PART_PRECISION)
2047 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2048 else
2049 result.low |= (cpp_num_part) 1 << i;
2051 if (i-- == 0)
2052 break;
2053 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2054 sub.high >>= 1;
2057 /* We divide so that the remainder has the sign of the LHS. */
2058 if (op == CPP_DIV)
2060 result.unsignedp = unsignedp;
2061 result.overflow = false;
2062 if (!unsignedp)
2064 if (negate)
2065 result = num_negate (result, precision);
2066 result.overflow = (num_positive (result, precision) ^ !negate
2067 && !num_zerop (result));
2070 return result;
2073 /* CPP_MOD. */
2074 lhs.unsignedp = unsignedp;
2075 lhs.overflow = false;
2076 if (lhs_neg)
2077 lhs = num_negate (lhs, precision);
2079 return lhs;
2082 /* Handle meeting "__has_include__" in a preprocessor expression. */
2083 static cpp_num
2084 parse_has_include (cpp_reader *pfile, enum include_type type)
2086 cpp_num result;
2087 bool paren = false;
2088 cpp_hashnode *node = 0;
2089 const cpp_token *token;
2090 bool bracket = false;
2091 char *fname = 0;
2093 result.unsignedp = false;
2094 result.high = 0;
2095 result.overflow = false;
2096 result.low = 0;
2098 pfile->state.in__has_include__++;
2100 token = cpp_get_token (pfile);
2101 if (token->type == CPP_OPEN_PAREN)
2103 paren = true;
2104 token = cpp_get_token (pfile);
2107 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2109 if (token->type == CPP_HEADER_NAME)
2110 bracket = true;
2111 fname = XNEWVEC (char, token->val.str.len - 1);
2112 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2113 fname[token->val.str.len - 2] = '\0';
2114 node = token->val.node.node;
2116 else if (token->type == CPP_LESS)
2118 bracket = true;
2119 fname = _cpp_bracket_include (pfile);
2121 else
2122 cpp_error (pfile, CPP_DL_ERROR,
2123 "operator \"__has_include__\" requires a header string");
2125 if (fname)
2127 int angle_brackets = (bracket ? 1 : 0);
2129 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2130 result.low = 1;
2131 else
2132 result.low = 0;
2134 XDELETEVEC (fname);
2137 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2138 cpp_error (pfile, CPP_DL_ERROR,
2139 "missing ')' after \"__has_include__\"");
2141 /* A possible controlling macro of the form #if !__has_include__ ().
2142 _cpp_parse_expr checks there was no other junk on the line. */
2143 if (node)
2144 pfile->mi_ind_cmacro = node;
2146 pfile->state.in__has_include__--;
2148 return result;