Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_9 / libcpp / expr.c
blob29cb0fa519e2947d5b49a6690f036d7b8e800974
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);
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) && !CPP_OPTION (pfile, c99))
546 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
547 "use of C99 hexadecimal floating constant");
549 if (float_flag == AFTER_EXPON)
551 if (*str == '+' || *str == '-')
552 str++;
554 /* Exponent is decimal, even if string is a hex float. */
555 if (!ISDIGIT (*str))
557 if (DIGIT_SEP (*str))
558 SYNTAX_ERROR_AT (virtual_location,
559 "digit separator adjacent to exponent");
560 else
561 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
565 seen_digit_sep = DIGIT_SEP (*str);
566 str++;
568 while (ISDIGIT (*str) || DIGIT_SEP (*str));
570 else if (radix == 16)
571 SYNTAX_ERROR_AT (virtual_location,
572 "hexadecimal floating constants require an exponent");
574 if (seen_digit_sep)
575 SYNTAX_ERROR_AT (virtual_location,
576 "digit separator outside digit sequence");
578 result = interpret_float_suffix (pfile, str, limit - str);
579 if (result == 0)
581 if (CPP_OPTION (pfile, user_literals))
583 if (ud_suffix)
584 *ud_suffix = (const char *) str;
585 result = CPP_N_LARGE | CPP_N_USERDEF;
587 else
589 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
590 "invalid suffix \"%.*s\" on floating constant",
591 (int) (limit - str), str);
592 return CPP_N_INVALID;
596 /* Traditional C didn't accept any floating suffixes. */
597 if (limit != str
598 && CPP_WTRADITIONAL (pfile)
599 && ! cpp_sys_macro_p (pfile))
600 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
601 "traditional C rejects the \"%.*s\" suffix",
602 (int) (limit - str), str);
604 /* A suffix for double is a GCC extension via decimal float support.
605 If the suffix also specifies an imaginary value we'll catch that
606 later. */
607 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
608 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
609 "suffix for double constant is a GCC extension");
611 /* Radix must be 10 for decimal floats. */
612 if ((result & CPP_N_DFLOAT) && radix != 10)
614 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
615 "invalid suffix \"%.*s\" with hexadecimal floating constant",
616 (int) (limit - str), str);
617 return CPP_N_INVALID;
620 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
621 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
622 "fixed-point constants are a GCC extension");
624 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
625 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
626 "decimal float constants are a GCC extension");
628 result |= CPP_N_FLOATING;
630 else
632 result = interpret_int_suffix (pfile, str, limit - str);
633 if (result == 0)
635 if (CPP_OPTION (pfile, user_literals))
637 if (ud_suffix)
638 *ud_suffix = (const char *) str;
639 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
641 else
643 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
644 "invalid suffix \"%.*s\" on integer constant",
645 (int) (limit - str), str);
646 return CPP_N_INVALID;
650 /* Traditional C only accepted the 'L' suffix.
651 Suppress warning about 'LL' with -Wno-long-long. */
652 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
654 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
655 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
656 && CPP_OPTION (pfile, cpp_warn_long_long);
658 if (u_or_i || large)
659 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
660 virtual_location, 0,
661 "traditional C rejects the \"%.*s\" suffix",
662 (int) (limit - str), str);
665 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
666 && CPP_OPTION (pfile, cpp_warn_long_long))
668 const char *message = CPP_OPTION (pfile, cplusplus)
669 ? N_("use of C++11 long long integer constant")
670 : N_("use of C99 long long integer constant");
672 if (CPP_OPTION (pfile, c99))
673 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
674 0, message);
675 else
676 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
677 virtual_location, 0, message);
680 result |= CPP_N_INTEGER;
683 syntax_ok:
684 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
685 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
686 "imaginary constants are a GCC extension");
687 if (radix == 2
688 && !CPP_OPTION (pfile, binary_constants)
689 && CPP_PEDANTIC (pfile))
690 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
691 CPP_OPTION (pfile, cplusplus)
692 ? "binary constants are a C++1y feature "
693 "or GCC extension"
694 : "binary constants are a GCC extension");
696 if (radix == 10)
697 result |= CPP_N_DECIMAL;
698 else if (radix == 16)
699 result |= CPP_N_HEX;
700 else if (radix == 2)
701 result |= CPP_N_BINARY;
702 else
703 result |= CPP_N_OCTAL;
705 return result;
707 syntax_error:
708 return CPP_N_INVALID;
711 /* cpp_interpret_integer converts an integer constant into a cpp_num,
712 of precision options->precision.
714 We do not provide any interface for decimal->float conversion,
715 because the preprocessor doesn't need it and we don't want to
716 drag in GCC's floating point emulator. */
717 cpp_num
718 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
719 unsigned int type)
721 const uchar *p, *end;
722 cpp_num result;
724 result.low = 0;
725 result.high = 0;
726 result.unsignedp = !!(type & CPP_N_UNSIGNED);
727 result.overflow = false;
729 p = token->val.str.text;
730 end = p + token->val.str.len;
732 /* Common case of a single digit. */
733 if (token->val.str.len == 1)
734 result.low = p[0] - '0';
735 else
737 cpp_num_part max;
738 size_t precision = CPP_OPTION (pfile, precision);
739 unsigned int base = 10, c = 0;
740 bool overflow = false;
742 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
744 base = 8;
745 p++;
747 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
749 base = 16;
750 p += 2;
752 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
754 base = 2;
755 p += 2;
758 /* We can add a digit to numbers strictly less than this without
759 needing the precision and slowness of double integers. */
760 max = ~(cpp_num_part) 0;
761 if (precision < PART_PRECISION)
762 max >>= PART_PRECISION - precision;
763 max = (max - base + 1) / base + 1;
765 for (; p < end; p++)
767 c = *p;
769 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
770 c = hex_value (c);
771 else if (DIGIT_SEP (c))
772 continue;
773 else
774 break;
776 /* Strict inequality for when max is set to zero. */
777 if (result.low < max)
778 result.low = result.low * base + c;
779 else
781 result = append_digit (result, c, base, precision);
782 overflow |= result.overflow;
783 max = 0;
787 if (overflow && !(type & CPP_N_USERDEF))
788 cpp_error (pfile, CPP_DL_PEDWARN,
789 "integer constant is too large for its type");
790 /* If too big to be signed, consider it unsigned. Only warn for
791 decimal numbers. Traditional numbers were always signed (but
792 we still honor an explicit U suffix); but we only have
793 traditional semantics in directives. */
794 else if (!result.unsignedp
795 && !(CPP_OPTION (pfile, traditional)
796 && pfile->state.in_directive)
797 && !num_positive (result, precision))
799 /* This is for constants within the range of uintmax_t but
800 not that of intmax_t. For such decimal constants, a
801 diagnostic is required for C99 as the selected type must
802 be signed and not having a type is a constraint violation
803 (DR#298, TC3), so this must be a pedwarn. For C90,
804 unsigned long is specified to be used for a constant that
805 does not fit in signed long; if uintmax_t has the same
806 range as unsigned long this means only a warning is
807 appropriate here. C90 permits the preprocessor to use a
808 wider range than unsigned long in the compiler, so if
809 uintmax_t is wider than unsigned long no diagnostic is
810 required for such constants in preprocessor #if
811 expressions and the compiler will pedwarn for such
812 constants outside the range of unsigned long that reach
813 the compiler so a diagnostic is not required there
814 either; thus, pedwarn for C99 but use a plain warning for
815 C90. */
816 if (base == 10)
817 cpp_error (pfile, (CPP_OPTION (pfile, c99)
818 ? CPP_DL_PEDWARN
819 : CPP_DL_WARNING),
820 "integer constant is so large that it is unsigned");
821 result.unsignedp = true;
825 return result;
828 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
829 static cpp_num
830 append_digit (cpp_num num, int digit, int base, size_t precision)
832 cpp_num result;
833 unsigned int shift;
834 bool overflow;
835 cpp_num_part add_high, add_low;
837 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
838 need to worry about add_high overflowing. */
839 switch (base)
841 case 2:
842 shift = 1;
843 break;
845 case 16:
846 shift = 4;
847 break;
849 default:
850 shift = 3;
852 overflow = !!(num.high >> (PART_PRECISION - shift));
853 result.high = num.high << shift;
854 result.low = num.low << shift;
855 result.high |= num.low >> (PART_PRECISION - shift);
856 result.unsignedp = num.unsignedp;
858 if (base == 10)
860 add_low = num.low << 1;
861 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
863 else
864 add_high = add_low = 0;
866 if (add_low + digit < add_low)
867 add_high++;
868 add_low += digit;
870 if (result.low + add_low < result.low)
871 add_high++;
872 if (result.high + add_high < result.high)
873 overflow = true;
875 result.low += add_low;
876 result.high += add_high;
877 result.overflow = overflow;
879 /* The above code catches overflow of a cpp_num type. This catches
880 overflow of the (possibly shorter) target precision. */
881 num.low = result.low;
882 num.high = result.high;
883 result = num_trim (result, precision);
884 if (!num_eq (result, num))
885 result.overflow = true;
887 return result;
890 /* Handle meeting "defined" in a preprocessor expression. */
891 static cpp_num
892 parse_defined (cpp_reader *pfile)
894 cpp_num result;
895 int paren = 0;
896 cpp_hashnode *node = 0;
897 const cpp_token *token;
898 cpp_context *initial_context = pfile->context;
900 /* Don't expand macros. */
901 pfile->state.prevent_expansion++;
903 token = cpp_get_token (pfile);
904 if (token->type == CPP_OPEN_PAREN)
906 paren = 1;
907 token = cpp_get_token (pfile);
910 if (token->type == CPP_NAME)
912 node = token->val.node.node;
913 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
915 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
916 node = 0;
919 else
921 cpp_error (pfile, CPP_DL_ERROR,
922 "operator \"defined\" requires an identifier");
923 if (token->flags & NAMED_OP)
925 cpp_token op;
927 op.flags = 0;
928 op.type = token->type;
929 cpp_error (pfile, CPP_DL_ERROR,
930 "(\"%s\" is an alternative token for \"%s\" in C++)",
931 cpp_token_as_text (pfile, token),
932 cpp_token_as_text (pfile, &op));
936 if (node)
938 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
939 cpp_error (pfile, CPP_DL_WARNING,
940 "this use of \"defined\" may not be portable");
942 _cpp_mark_macro_used (node);
943 if (!(node->flags & NODE_USED))
945 node->flags |= NODE_USED;
946 if (node->type == NT_MACRO)
948 if ((node->flags & NODE_BUILTIN)
949 && pfile->cb.user_builtin_macro)
950 pfile->cb.user_builtin_macro (pfile, node);
951 if (pfile->cb.used_define)
952 pfile->cb.used_define (pfile, pfile->directive_line, node);
954 else
956 if (pfile->cb.used_undef)
957 pfile->cb.used_undef (pfile, pfile->directive_line, node);
961 /* A possible controlling macro of the form #if !defined ().
962 _cpp_parse_expr checks there was no other junk on the line. */
963 pfile->mi_ind_cmacro = node;
966 pfile->state.prevent_expansion--;
968 /* Do not treat conditional macros as being defined. This is due to the
969 powerpc and spu ports using conditional macros for 'vector', 'bool', and
970 'pixel' to act as conditional keywords. This messes up tests like #ifndef
971 bool. */
972 result.unsignedp = false;
973 result.high = 0;
974 result.overflow = false;
975 result.low = (node && node->type == NT_MACRO
976 && (node->flags & NODE_CONDITIONAL) == 0);
977 return result;
980 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
981 number or character constant, or the result of the "defined" or "#"
982 operators). */
983 static cpp_num
984 eval_token (cpp_reader *pfile, const cpp_token *token,
985 source_location virtual_location)
987 cpp_num result;
988 unsigned int temp;
989 int unsignedp = 0;
991 result.unsignedp = false;
992 result.overflow = false;
994 switch (token->type)
996 case CPP_NUMBER:
997 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
998 if (temp & CPP_N_USERDEF)
999 cpp_error (pfile, CPP_DL_ERROR,
1000 "user-defined literal in preprocessor expression");
1001 switch (temp & CPP_N_CATEGORY)
1003 case CPP_N_FLOATING:
1004 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1005 "floating constant in preprocessor expression");
1006 break;
1007 case CPP_N_INTEGER:
1008 if (!(temp & CPP_N_IMAGINARY))
1009 return cpp_interpret_integer (pfile, token, temp);
1010 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1011 "imaginary number in preprocessor expression");
1012 break;
1014 case CPP_N_INVALID:
1015 /* Error already issued. */
1016 break;
1018 result.high = result.low = 0;
1019 break;
1021 case CPP_WCHAR:
1022 case CPP_CHAR:
1023 case CPP_CHAR16:
1024 case CPP_CHAR32:
1026 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1027 &temp, &unsignedp);
1029 result.high = 0;
1030 result.low = cc;
1031 /* Sign-extend the result if necessary. */
1032 if (!unsignedp && (cppchar_signed_t) cc < 0)
1034 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1035 result.low |= ~(~(cpp_num_part) 0
1036 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1037 result.high = ~(cpp_num_part) 0;
1038 result = num_trim (result, CPP_OPTION (pfile, precision));
1041 break;
1043 case CPP_NAME:
1044 if (token->val.node.node == pfile->spec_nodes.n_defined)
1045 return parse_defined (pfile);
1046 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1047 return parse_has_include (pfile, IT_INCLUDE);
1048 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1049 return parse_has_include (pfile, IT_INCLUDE_NEXT);
1050 else if (CPP_OPTION (pfile, cplusplus)
1051 && (token->val.node.node == pfile->spec_nodes.n_true
1052 || token->val.node.node == pfile->spec_nodes.n_false))
1054 result.high = 0;
1055 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1057 else
1059 result.high = 0;
1060 result.low = 0;
1061 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1062 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1063 "\"%s\" is not defined",
1064 NODE_NAME (token->val.node.node));
1066 break;
1068 case CPP_HASH:
1069 if (!pfile->state.skipping)
1071 /* A pedantic warning takes precedence over a deprecated
1072 warning here. */
1073 if (CPP_PEDANTIC (pfile))
1074 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1075 virtual_location, 0,
1076 "assertions are a GCC extension");
1077 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1078 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1079 "assertions are a deprecated extension");
1081 _cpp_test_assertion (pfile, &temp);
1082 result.high = 0;
1083 result.low = temp;
1084 break;
1086 default:
1087 abort ();
1090 result.unsignedp = !!unsignedp;
1091 return result;
1094 /* Operator precedence and flags table.
1096 After an operator is returned from the lexer, if it has priority less
1097 than the operator on the top of the stack, we reduce the stack by one
1098 operator and repeat the test. Since equal priorities do not reduce,
1099 this is naturally right-associative.
1101 We handle left-associative operators by decrementing the priority of
1102 just-lexed operators by one, but retaining the priority of operators
1103 already on the stack.
1105 The remaining cases are '(' and ')'. We handle '(' by skipping the
1106 reduction phase completely. ')' is given lower priority than
1107 everything else, including '(', effectively forcing a reduction of the
1108 parenthesized expression. If there is a matching '(', the routine
1109 reduce() exits immediately. If the normal exit route sees a ')', then
1110 there cannot have been a matching '(' and an error message is output.
1112 The parser assumes all shifted operators require a left operand unless
1113 the flag NO_L_OPERAND is set. These semantics are automatic; any
1114 extra semantics need to be handled with operator-specific code. */
1116 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1117 operand changes because of integer promotions. */
1118 #define NO_L_OPERAND (1 << 0)
1119 #define LEFT_ASSOC (1 << 1)
1120 #define CHECK_PROMOTION (1 << 2)
1122 /* Operator to priority map. Must be in the same order as the first
1123 N entries of enum cpp_ttype. */
1124 static const struct cpp_operator
1126 uchar prio;
1127 uchar flags;
1128 } optab[] =
1130 /* EQ */ {0, 0}, /* Shouldn't happen. */
1131 /* NOT */ {16, NO_L_OPERAND},
1132 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1133 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1134 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1135 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1136 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1137 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1138 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1139 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1140 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1141 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1142 /* RSHIFT */ {13, LEFT_ASSOC},
1143 /* LSHIFT */ {13, LEFT_ASSOC},
1145 /* COMPL */ {16, NO_L_OPERAND},
1146 /* AND_AND */ {6, LEFT_ASSOC},
1147 /* OR_OR */ {5, LEFT_ASSOC},
1148 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1149 However, there are some special cases for these in reduce(). */
1150 /* QUERY */ {4, 0},
1151 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1152 /* COMMA */ {4, LEFT_ASSOC},
1153 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1154 /* CLOSE_PAREN */ {0, 0},
1155 /* EOF */ {0, 0},
1156 /* EQ_EQ */ {11, LEFT_ASSOC},
1157 /* NOT_EQ */ {11, LEFT_ASSOC},
1158 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1159 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1160 /* UPLUS */ {16, NO_L_OPERAND},
1161 /* UMINUS */ {16, NO_L_OPERAND}
1164 /* Parse and evaluate a C expression, reading from PFILE.
1165 Returns the truth value of the expression.
1167 The implementation is an operator precedence parser, i.e. a
1168 bottom-up parser, using a stack for not-yet-reduced tokens.
1170 The stack base is op_stack, and the current stack pointer is 'top'.
1171 There is a stack element for each operator (only), and the most
1172 recently pushed operator is 'top->op'. An operand (value) is
1173 stored in the 'value' field of the stack element of the operator
1174 that precedes it. */
1175 bool
1176 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1178 struct op *top = pfile->op_stack;
1179 unsigned int lex_count;
1180 bool saw_leading_not, want_value = true;
1181 source_location virtual_location = 0;
1183 pfile->state.skip_eval = 0;
1185 /* Set up detection of #if ! defined(). */
1186 pfile->mi_ind_cmacro = 0;
1187 saw_leading_not = false;
1188 lex_count = 0;
1190 /* Lowest priority operator prevents further reductions. */
1191 top->op = CPP_EOF;
1193 for (;;)
1195 struct op op;
1197 lex_count++;
1198 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1199 op.op = op.token->type;
1200 op.loc = virtual_location;
1202 switch (op.op)
1204 /* These tokens convert into values. */
1205 case CPP_NUMBER:
1206 case CPP_CHAR:
1207 case CPP_WCHAR:
1208 case CPP_CHAR16:
1209 case CPP_CHAR32:
1210 case CPP_NAME:
1211 case CPP_HASH:
1212 if (!want_value)
1213 SYNTAX_ERROR2_AT (op.loc,
1214 "missing binary operator before token \"%s\"",
1215 cpp_token_as_text (pfile, op.token));
1216 want_value = false;
1217 top->value = eval_token (pfile, op.token, op.loc);
1218 continue;
1220 case CPP_NOT:
1221 saw_leading_not = lex_count == 1;
1222 break;
1223 case CPP_PLUS:
1224 if (want_value)
1225 op.op = CPP_UPLUS;
1226 break;
1227 case CPP_MINUS:
1228 if (want_value)
1229 op.op = CPP_UMINUS;
1230 break;
1232 default:
1233 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1234 SYNTAX_ERROR2_AT (op.loc,
1235 "token \"%s\" is not valid in preprocessor expressions",
1236 cpp_token_as_text (pfile, op.token));
1237 break;
1240 /* Check we have a value or operator as appropriate. */
1241 if (optab[op.op].flags & NO_L_OPERAND)
1243 if (!want_value)
1244 SYNTAX_ERROR2_AT (op.loc,
1245 "missing binary operator before token \"%s\"",
1246 cpp_token_as_text (pfile, op.token));
1248 else if (want_value)
1250 /* We want a number (or expression) and haven't got one.
1251 Try to emit a specific diagnostic. */
1252 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1253 SYNTAX_ERROR_AT (op.loc,
1254 "missing expression between '(' and ')'");
1256 if (op.op == CPP_EOF && top->op == CPP_EOF)
1257 SYNTAX_ERROR2_AT (op.loc,
1258 "%s with no expression", is_if ? "#if" : "#elif");
1260 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1261 SYNTAX_ERROR2_AT (op.loc,
1262 "operator '%s' has no right operand",
1263 cpp_token_as_text (pfile, top->token));
1264 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1265 /* Complain about missing paren during reduction. */;
1266 else
1267 SYNTAX_ERROR2_AT (op.loc,
1268 "operator '%s' has no left operand",
1269 cpp_token_as_text (pfile, op.token));
1272 top = reduce (pfile, top, op.op);
1273 if (!top)
1274 goto syntax_error;
1276 if (op.op == CPP_EOF)
1277 break;
1279 switch (op.op)
1281 case CPP_CLOSE_PAREN:
1282 continue;
1283 case CPP_OR_OR:
1284 if (!num_zerop (top->value))
1285 pfile->state.skip_eval++;
1286 break;
1287 case CPP_AND_AND:
1288 case CPP_QUERY:
1289 if (num_zerop (top->value))
1290 pfile->state.skip_eval++;
1291 break;
1292 case CPP_COLON:
1293 if (top->op != CPP_QUERY)
1294 SYNTAX_ERROR_AT (op.loc,
1295 " ':' without preceding '?'");
1296 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1297 pfile->state.skip_eval++;
1298 else
1299 pfile->state.skip_eval--;
1300 default:
1301 break;
1304 want_value = true;
1306 /* Check for and handle stack overflow. */
1307 if (++top == pfile->op_limit)
1308 top = _cpp_expand_op_stack (pfile);
1310 top->op = op.op;
1311 top->token = op.token;
1312 top->loc = op.loc;
1315 /* The controlling macro expression is only valid if we called lex 3
1316 times: <!> <defined expression> and <EOF>. push_conditional ()
1317 checks that we are at top-of-file. */
1318 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1319 pfile->mi_ind_cmacro = 0;
1321 if (top != pfile->op_stack)
1323 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1324 "unbalanced stack in %s",
1325 is_if ? "#if" : "#elif");
1326 syntax_error:
1327 return false; /* Return false on syntax error. */
1330 return !num_zerop (top->value);
1333 /* Reduce the operator / value stack if possible, in preparation for
1334 pushing operator OP. Returns NULL on error, otherwise the top of
1335 the stack. */
1336 static struct op *
1337 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1339 unsigned int prio;
1341 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1343 bad_op:
1344 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1345 return 0;
1348 if (op == CPP_OPEN_PAREN)
1349 return top;
1351 /* Decrement the priority of left-associative operators to force a
1352 reduction with operators of otherwise equal priority. */
1353 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1354 while (prio < optab[top->op].prio)
1356 if (CPP_OPTION (pfile, warn_num_sign_change)
1357 && optab[top->op].flags & CHECK_PROMOTION)
1358 check_promotion (pfile, top);
1360 switch (top->op)
1362 case CPP_UPLUS:
1363 case CPP_UMINUS:
1364 case CPP_NOT:
1365 case CPP_COMPL:
1366 top[-1].value = num_unary_op (pfile, top->value, top->op);
1367 top[-1].loc = top->loc;
1368 break;
1370 case CPP_PLUS:
1371 case CPP_MINUS:
1372 case CPP_RSHIFT:
1373 case CPP_LSHIFT:
1374 case CPP_COMMA:
1375 top[-1].value = num_binary_op (pfile, top[-1].value,
1376 top->value, top->op);
1377 top[-1].loc = top->loc;
1378 break;
1380 case CPP_GREATER:
1381 case CPP_LESS:
1382 case CPP_GREATER_EQ:
1383 case CPP_LESS_EQ:
1384 top[-1].value
1385 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1386 top[-1].loc = top->loc;
1387 break;
1389 case CPP_EQ_EQ:
1390 case CPP_NOT_EQ:
1391 top[-1].value
1392 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1393 top[-1].loc = top->loc;
1394 break;
1396 case CPP_AND:
1397 case CPP_OR:
1398 case CPP_XOR:
1399 top[-1].value
1400 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1401 top[-1].loc = top->loc;
1402 break;
1404 case CPP_MULT:
1405 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1406 top[-1].loc = top->loc;
1407 break;
1409 case CPP_DIV:
1410 case CPP_MOD:
1411 top[-1].value = num_div_op (pfile, top[-1].value,
1412 top->value, top->op, top->loc);
1413 top[-1].loc = top->loc;
1414 break;
1416 case CPP_OR_OR:
1417 top--;
1418 if (!num_zerop (top->value))
1419 pfile->state.skip_eval--;
1420 top->value.low = (!num_zerop (top->value)
1421 || !num_zerop (top[1].value));
1422 top->value.high = 0;
1423 top->value.unsignedp = false;
1424 top->value.overflow = false;
1425 top->loc = top[1].loc;
1426 continue;
1428 case CPP_AND_AND:
1429 top--;
1430 if (num_zerop (top->value))
1431 pfile->state.skip_eval--;
1432 top->value.low = (!num_zerop (top->value)
1433 && !num_zerop (top[1].value));
1434 top->value.high = 0;
1435 top->value.unsignedp = false;
1436 top->value.overflow = false;
1437 top->loc = top[1].loc;
1438 continue;
1440 case CPP_OPEN_PAREN:
1441 if (op != CPP_CLOSE_PAREN)
1443 cpp_error_with_line (pfile, CPP_DL_ERROR,
1444 top->token->src_loc,
1445 0, "missing ')' in expression");
1446 return 0;
1448 top--;
1449 top->value = top[1].value;
1450 top->loc = top[1].loc;
1451 return top;
1453 case CPP_COLON:
1454 top -= 2;
1455 if (!num_zerop (top->value))
1457 pfile->state.skip_eval--;
1458 top->value = top[1].value;
1459 top->loc = top[1].loc;
1461 else
1463 top->value = top[2].value;
1464 top->loc = top[2].loc;
1466 top->value.unsignedp = (top[1].value.unsignedp
1467 || top[2].value.unsignedp);
1468 continue;
1470 case CPP_QUERY:
1471 /* COMMA and COLON should not reduce a QUERY operator. */
1472 if (op == CPP_COMMA || op == CPP_COLON)
1473 return top;
1474 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1475 return 0;
1477 default:
1478 goto bad_op;
1481 top--;
1482 if (top->value.overflow && !pfile->state.skip_eval)
1483 cpp_error (pfile, CPP_DL_PEDWARN,
1484 "integer overflow in preprocessor expression");
1487 if (op == CPP_CLOSE_PAREN)
1489 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1490 return 0;
1493 return top;
1496 /* Returns the position of the old top of stack after expansion. */
1497 struct op *
1498 _cpp_expand_op_stack (cpp_reader *pfile)
1500 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1501 size_t new_size = old_size * 2 + 20;
1503 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1504 pfile->op_limit = pfile->op_stack + new_size;
1506 return pfile->op_stack + old_size;
1509 /* Emits a warning if the effective sign of either operand of OP
1510 changes because of integer promotions. */
1511 static void
1512 check_promotion (cpp_reader *pfile, const struct op *op)
1514 if (op->value.unsignedp == op[-1].value.unsignedp)
1515 return;
1517 if (op->value.unsignedp)
1519 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1520 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1521 "the left operand of \"%s\" changes sign when promoted",
1522 cpp_token_as_text (pfile, op->token));
1524 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1525 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1526 "the right operand of \"%s\" changes sign when promoted",
1527 cpp_token_as_text (pfile, op->token));
1530 /* Clears the unused high order bits of the number pointed to by PNUM. */
1531 static cpp_num
1532 num_trim (cpp_num num, size_t precision)
1534 if (precision > PART_PRECISION)
1536 precision -= PART_PRECISION;
1537 if (precision < PART_PRECISION)
1538 num.high &= ((cpp_num_part) 1 << precision) - 1;
1540 else
1542 if (precision < PART_PRECISION)
1543 num.low &= ((cpp_num_part) 1 << precision) - 1;
1544 num.high = 0;
1547 return num;
1550 /* True iff A (presumed signed) >= 0. */
1551 static bool
1552 num_positive (cpp_num num, size_t precision)
1554 if (precision > PART_PRECISION)
1556 precision -= PART_PRECISION;
1557 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1560 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1563 /* Sign extend a number, with PRECISION significant bits and all
1564 others assumed clear, to fill out a cpp_num structure. */
1565 cpp_num
1566 cpp_num_sign_extend (cpp_num num, size_t precision)
1568 if (!num.unsignedp)
1570 if (precision > PART_PRECISION)
1572 precision -= PART_PRECISION;
1573 if (precision < PART_PRECISION
1574 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1575 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1577 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1579 if (precision < PART_PRECISION)
1580 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1581 num.high = ~(cpp_num_part) 0;
1585 return num;
1588 /* Returns the negative of NUM. */
1589 static cpp_num
1590 num_negate (cpp_num num, size_t precision)
1592 cpp_num copy;
1594 copy = num;
1595 num.high = ~num.high;
1596 num.low = ~num.low;
1597 if (++num.low == 0)
1598 num.high++;
1599 num = num_trim (num, precision);
1600 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1602 return num;
1605 /* Returns true if A >= B. */
1606 static bool
1607 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1609 bool unsignedp;
1611 unsignedp = pa.unsignedp || pb.unsignedp;
1613 if (!unsignedp)
1615 /* Both numbers have signed type. If they are of different
1616 sign, the answer is the sign of A. */
1617 unsignedp = num_positive (pa, precision);
1619 if (unsignedp != num_positive (pb, precision))
1620 return unsignedp;
1622 /* Otherwise we can do an unsigned comparison. */
1625 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1628 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1629 static cpp_num
1630 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1631 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1633 lhs.overflow = false;
1634 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1636 /* As excess precision is zeroed, there is no need to num_trim () as
1637 these operations cannot introduce a set bit there. */
1638 if (op == CPP_AND)
1640 lhs.low &= rhs.low;
1641 lhs.high &= rhs.high;
1643 else if (op == CPP_OR)
1645 lhs.low |= rhs.low;
1646 lhs.high |= rhs.high;
1648 else
1650 lhs.low ^= rhs.low;
1651 lhs.high ^= rhs.high;
1654 return lhs;
1657 /* Returns LHS OP RHS, where OP is an inequality. */
1658 static cpp_num
1659 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1660 enum cpp_ttype op)
1662 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1664 if (op == CPP_GREATER_EQ)
1665 lhs.low = gte;
1666 else if (op == CPP_LESS)
1667 lhs.low = !gte;
1668 else if (op == CPP_GREATER)
1669 lhs.low = gte && !num_eq (lhs, rhs);
1670 else /* CPP_LESS_EQ. */
1671 lhs.low = !gte || num_eq (lhs, rhs);
1673 lhs.high = 0;
1674 lhs.overflow = false;
1675 lhs.unsignedp = false;
1676 return lhs;
1679 /* Returns LHS OP RHS, where OP is == or !=. */
1680 static cpp_num
1681 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1682 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1684 /* Work around a 3.0.4 bug; see PR 6950. */
1685 bool eq = num_eq (lhs, rhs);
1686 if (op == CPP_NOT_EQ)
1687 eq = !eq;
1688 lhs.low = eq;
1689 lhs.high = 0;
1690 lhs.overflow = false;
1691 lhs.unsignedp = false;
1692 return lhs;
1695 /* Shift NUM, of width PRECISION, right by N bits. */
1696 static cpp_num
1697 num_rshift (cpp_num num, size_t precision, size_t n)
1699 cpp_num_part sign_mask;
1700 bool x = num_positive (num, precision);
1702 if (num.unsignedp || x)
1703 sign_mask = 0;
1704 else
1705 sign_mask = ~(cpp_num_part) 0;
1707 if (n >= precision)
1708 num.high = num.low = sign_mask;
1709 else
1711 /* Sign-extend. */
1712 if (precision < PART_PRECISION)
1713 num.high = sign_mask, num.low |= sign_mask << precision;
1714 else if (precision < 2 * PART_PRECISION)
1715 num.high |= sign_mask << (precision - PART_PRECISION);
1717 if (n >= PART_PRECISION)
1719 n -= PART_PRECISION;
1720 num.low = num.high;
1721 num.high = sign_mask;
1724 if (n)
1726 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1727 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1731 num = num_trim (num, precision);
1732 num.overflow = false;
1733 return num;
1736 /* Shift NUM, of width PRECISION, left by N bits. */
1737 static cpp_num
1738 num_lshift (cpp_num num, size_t precision, size_t n)
1740 if (n >= precision)
1742 num.overflow = !num.unsignedp && !num_zerop (num);
1743 num.high = num.low = 0;
1745 else
1747 cpp_num orig, maybe_orig;
1748 size_t m = n;
1750 orig = num;
1751 if (m >= PART_PRECISION)
1753 m -= PART_PRECISION;
1754 num.high = num.low;
1755 num.low = 0;
1757 if (m)
1759 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1760 num.low <<= m;
1762 num = num_trim (num, precision);
1764 if (num.unsignedp)
1765 num.overflow = false;
1766 else
1768 maybe_orig = num_rshift (num, precision, n);
1769 num.overflow = !num_eq (orig, maybe_orig);
1773 return num;
1776 /* The four unary operators: +, -, ! and ~. */
1777 static cpp_num
1778 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1780 switch (op)
1782 case CPP_UPLUS:
1783 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1784 cpp_warning (pfile, CPP_W_TRADITIONAL,
1785 "traditional C rejects the unary plus operator");
1786 num.overflow = false;
1787 break;
1789 case CPP_UMINUS:
1790 num = num_negate (num, CPP_OPTION (pfile, precision));
1791 break;
1793 case CPP_COMPL:
1794 num.high = ~num.high;
1795 num.low = ~num.low;
1796 num = num_trim (num, CPP_OPTION (pfile, precision));
1797 num.overflow = false;
1798 break;
1800 default: /* case CPP_NOT: */
1801 num.low = num_zerop (num);
1802 num.high = 0;
1803 num.overflow = false;
1804 num.unsignedp = false;
1805 break;
1808 return num;
1811 /* The various binary operators. */
1812 static cpp_num
1813 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1815 cpp_num result;
1816 size_t precision = CPP_OPTION (pfile, precision);
1817 size_t n;
1819 switch (op)
1821 /* Shifts. */
1822 case CPP_LSHIFT:
1823 case CPP_RSHIFT:
1824 if (!rhs.unsignedp && !num_positive (rhs, precision))
1826 /* A negative shift is a positive shift the other way. */
1827 if (op == CPP_LSHIFT)
1828 op = CPP_RSHIFT;
1829 else
1830 op = CPP_LSHIFT;
1831 rhs = num_negate (rhs, precision);
1833 if (rhs.high)
1834 n = ~0; /* Maximal. */
1835 else
1836 n = rhs.low;
1837 if (op == CPP_LSHIFT)
1838 lhs = num_lshift (lhs, precision, n);
1839 else
1840 lhs = num_rshift (lhs, precision, n);
1841 break;
1843 /* Arithmetic. */
1844 case CPP_MINUS:
1845 result.low = lhs.low - rhs.low;
1846 result.high = lhs.high - rhs.high;
1847 if (result.low > lhs.low)
1848 result.high--;
1849 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1850 result.overflow = false;
1852 result = num_trim (result, precision);
1853 if (!result.unsignedp)
1855 bool lhsp = num_positive (lhs, precision);
1856 result.overflow = (lhsp != num_positive (rhs, precision)
1857 && lhsp != num_positive (result, precision));
1859 return result;
1861 case CPP_PLUS:
1862 result.low = lhs.low + rhs.low;
1863 result.high = lhs.high + rhs.high;
1864 if (result.low < lhs.low)
1865 result.high++;
1866 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1867 result.overflow = false;
1869 result = num_trim (result, precision);
1870 if (!result.unsignedp)
1872 bool lhsp = num_positive (lhs, precision);
1873 result.overflow = (lhsp == num_positive (rhs, precision)
1874 && lhsp != num_positive (result, precision));
1876 return result;
1878 /* Comma. */
1879 default: /* case CPP_COMMA: */
1880 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1881 || !pfile->state.skip_eval))
1882 cpp_error (pfile, CPP_DL_PEDWARN,
1883 "comma operator in operand of #if");
1884 lhs = rhs;
1885 break;
1888 return lhs;
1891 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1892 cannot overflow. */
1893 static cpp_num
1894 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1896 cpp_num result;
1897 cpp_num_part middle[2], temp;
1899 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1900 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1902 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1903 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1905 temp = result.low;
1906 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1907 if (result.low < temp)
1908 result.high++;
1910 temp = result.low;
1911 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1912 if (result.low < temp)
1913 result.high++;
1915 result.high += HIGH_PART (middle[0]);
1916 result.high += HIGH_PART (middle[1]);
1917 result.unsignedp = true;
1918 result.overflow = false;
1920 return result;
1923 /* Multiply two preprocessing numbers. */
1924 static cpp_num
1925 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1927 cpp_num result, temp;
1928 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1929 bool overflow, negate = false;
1930 size_t precision = CPP_OPTION (pfile, precision);
1932 /* Prepare for unsigned multiplication. */
1933 if (!unsignedp)
1935 if (!num_positive (lhs, precision))
1936 negate = !negate, lhs = num_negate (lhs, precision);
1937 if (!num_positive (rhs, precision))
1938 negate = !negate, rhs = num_negate (rhs, precision);
1941 overflow = lhs.high && rhs.high;
1942 result = num_part_mul (lhs.low, rhs.low);
1944 temp = num_part_mul (lhs.high, rhs.low);
1945 result.high += temp.low;
1946 if (temp.high)
1947 overflow = true;
1949 temp = num_part_mul (lhs.low, rhs.high);
1950 result.high += temp.low;
1951 if (temp.high)
1952 overflow = true;
1954 temp.low = result.low, temp.high = result.high;
1955 result = num_trim (result, precision);
1956 if (!num_eq (result, temp))
1957 overflow = true;
1959 if (negate)
1960 result = num_negate (result, precision);
1962 if (unsignedp)
1963 result.overflow = false;
1964 else
1965 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1966 && !num_zerop (result));
1967 result.unsignedp = unsignedp;
1969 return result;
1972 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1973 or the remainder depending upon OP. LOCATION is the source location
1974 of this operator (for diagnostics). */
1976 static cpp_num
1977 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1978 source_location location)
1980 cpp_num result, sub;
1981 cpp_num_part mask;
1982 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1983 bool negate = false, lhs_neg = false;
1984 size_t i, precision = CPP_OPTION (pfile, precision);
1986 /* Prepare for unsigned division. */
1987 if (!unsignedp)
1989 if (!num_positive (lhs, precision))
1990 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1991 if (!num_positive (rhs, precision))
1992 negate = !negate, rhs = num_negate (rhs, precision);
1995 /* Find the high bit. */
1996 if (rhs.high)
1998 i = precision - 1;
1999 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2000 for (; ; i--, mask >>= 1)
2001 if (rhs.high & mask)
2002 break;
2004 else if (rhs.low)
2006 if (precision > PART_PRECISION)
2007 i = precision - PART_PRECISION - 1;
2008 else
2009 i = precision - 1;
2010 mask = (cpp_num_part) 1 << i;
2011 for (; ; i--, mask >>= 1)
2012 if (rhs.low & mask)
2013 break;
2015 else
2017 if (!pfile->state.skip_eval)
2018 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2019 "division by zero in #if");
2020 return lhs;
2023 /* First nonzero bit of RHS is bit I. Do naive division by
2024 shifting the RHS fully left, and subtracting from LHS if LHS is
2025 at least as big, and then repeating but with one less shift.
2026 This is not very efficient, but is easy to understand. */
2028 rhs.unsignedp = true;
2029 lhs.unsignedp = true;
2030 i = precision - i - 1;
2031 sub = num_lshift (rhs, precision, i);
2033 result.high = result.low = 0;
2034 for (;;)
2036 if (num_greater_eq (lhs, sub, precision))
2038 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2039 if (i >= PART_PRECISION)
2040 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2041 else
2042 result.low |= (cpp_num_part) 1 << i;
2044 if (i-- == 0)
2045 break;
2046 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2047 sub.high >>= 1;
2050 /* We divide so that the remainder has the sign of the LHS. */
2051 if (op == CPP_DIV)
2053 result.unsignedp = unsignedp;
2054 result.overflow = false;
2055 if (!unsignedp)
2057 if (negate)
2058 result = num_negate (result, precision);
2059 result.overflow = (num_positive (result, precision) ^ !negate
2060 && !num_zerop (result));
2063 return result;
2066 /* CPP_MOD. */
2067 lhs.unsignedp = unsignedp;
2068 lhs.overflow = false;
2069 if (lhs_neg)
2070 lhs = num_negate (lhs, precision);
2072 return lhs;
2075 /* Handle meeting "__has_include__" in a preprocessor expression. */
2076 static cpp_num
2077 parse_has_include (cpp_reader *pfile, enum include_type type)
2079 cpp_num result;
2080 bool paren = false;
2081 cpp_hashnode *node = 0;
2082 const cpp_token *token;
2083 bool bracket = false;
2084 char *fname = 0;
2086 result.unsignedp = false;
2087 result.high = 0;
2088 result.overflow = false;
2089 result.low = 0;
2091 pfile->state.in__has_include__++;
2093 token = cpp_get_token (pfile);
2094 if (token->type == CPP_OPEN_PAREN)
2096 paren = true;
2097 token = cpp_get_token (pfile);
2100 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2102 if (token->type == CPP_HEADER_NAME)
2103 bracket = true;
2104 fname = XNEWVEC (char, token->val.str.len - 1);
2105 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2106 fname[token->val.str.len - 2] = '\0';
2107 node = token->val.node.node;
2109 else if (token->type == CPP_LESS)
2111 bracket = true;
2112 fname = _cpp_bracket_include (pfile);
2114 else
2115 cpp_error (pfile, CPP_DL_ERROR,
2116 "operator \"__has_include__\" requires a header string");
2118 if (fname)
2120 int angle_brackets = (bracket ? 1 : 0);
2122 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2123 result.low = 1;
2124 else
2125 result.low = 0;
2127 XDELETEVEC (fname);
2130 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2131 cpp_error (pfile, CPP_DL_ERROR,
2132 "missing ')' after \"__has_include__\"");
2134 /* A possible controlling macro of the form #if !__has_include__ ().
2135 _cpp_parse_expr checks there was no other junk on the line. */
2136 if (node)
2137 pfile->mi_ind_cmacro = node;
2139 pfile->state.in__has_include__--;
2141 return result;