[hsa] Increase hsa symbol alignment to natural one
[official-gcc.git] / libcpp / expr.c
blob5cdca6f1bec6453cfd494fc4d8b39ed207246714
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2016 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 if (type == CPP_UTF8CHAR_USERDEF)
311 return CPP_UTF8CHAR;
312 else
313 return type;
316 /* Return the user-defined char literal type corresponding to the input
317 char type. If the input type is not a char type return the input
318 type. */
319 enum cpp_ttype
320 cpp_userdef_char_add_type (enum cpp_ttype type)
322 if (type == CPP_CHAR)
323 return CPP_CHAR_USERDEF;
324 else if (type == CPP_WCHAR)
325 return CPP_WCHAR_USERDEF;
326 else if (type == CPP_CHAR16)
327 return CPP_CHAR16_USERDEF;
328 else if (type == CPP_CHAR32)
329 return CPP_CHAR32_USERDEF;
330 else if (type == CPP_UTF8CHAR)
331 return CPP_UTF8CHAR_USERDEF;
332 else
333 return type;
336 /* Return true if the token type is a user-defined string literal. */
337 bool
338 cpp_userdef_string_p (enum cpp_ttype type)
340 if (type == CPP_STRING_USERDEF
341 || type == CPP_WSTRING_USERDEF
342 || type == CPP_STRING16_USERDEF
343 || type == CPP_STRING32_USERDEF
344 || type == CPP_UTF8STRING_USERDEF)
345 return true;
346 else
347 return false;
350 /* Return true if the token type is a user-defined char literal. */
351 bool
352 cpp_userdef_char_p (enum cpp_ttype type)
354 if (type == CPP_CHAR_USERDEF
355 || type == CPP_WCHAR_USERDEF
356 || type == CPP_CHAR16_USERDEF
357 || type == CPP_CHAR32_USERDEF
358 || type == CPP_UTF8CHAR_USERDEF)
359 return true;
360 else
361 return false;
364 /* Extract the suffix from a user-defined literal string or char. */
365 const char *
366 cpp_get_userdef_suffix (const cpp_token *tok)
368 unsigned int len = tok->val.str.len;
369 const char *text = (const char *)tok->val.str.text;
370 char delim;
371 unsigned int i;
372 for (i = 0; i < len; ++i)
373 if (text[i] == '\'' || text[i] == '"')
374 break;
375 if (i == len)
376 return text + len;
377 delim = text[i];
378 for (i = len; i > 0; --i)
379 if (text[i - 1] == delim)
380 break;
381 return text + i;
384 /* Categorize numeric constants according to their field (integer,
385 floating point, or invalid), radix (decimal, octal, hexadecimal),
386 and type suffixes.
388 TOKEN is the token that represents the numeric constant to
389 classify.
391 In C++0X if UD_SUFFIX is non null it will be assigned
392 any unrecognized suffix for a user-defined literal.
394 VIRTUAL_LOCATION is the virtual location for TOKEN. */
395 unsigned int
396 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
397 const char **ud_suffix, source_location virtual_location)
399 const uchar *str = token->val.str.text;
400 const uchar *limit;
401 unsigned int max_digit, result, radix;
402 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
403 bool seen_digit;
404 bool seen_digit_sep;
406 if (ud_suffix)
407 *ud_suffix = NULL;
409 /* If the lexer has done its job, length one can only be a single
410 digit. Fast-path this very common case. */
411 if (token->val.str.len == 1)
412 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
414 limit = str + token->val.str.len;
415 float_flag = NOT_FLOAT;
416 max_digit = 0;
417 radix = 10;
418 seen_digit = false;
419 seen_digit_sep = false;
421 /* First, interpret the radix. */
422 if (*str == '0')
424 radix = 8;
425 str++;
427 /* Require at least one hex digit to classify it as hex. */
428 if (*str == 'x' || *str == 'X')
430 if (str[1] == '.' || ISXDIGIT (str[1]))
432 radix = 16;
433 str++;
435 else if (DIGIT_SEP (str[1]))
436 SYNTAX_ERROR_AT (virtual_location,
437 "digit separator after base indicator");
439 else if (*str == 'b' || *str == 'B')
441 if (str[1] == '0' || str[1] == '1')
443 radix = 2;
444 str++;
446 else if (DIGIT_SEP (str[1]))
447 SYNTAX_ERROR_AT (virtual_location,
448 "digit separator after base indicator");
452 /* Now scan for a well-formed integer or float. */
453 for (;;)
455 unsigned int c = *str++;
457 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
459 seen_digit_sep = false;
460 seen_digit = true;
461 c = hex_value (c);
462 if (c > max_digit)
463 max_digit = c;
465 else if (DIGIT_SEP (c))
467 if (seen_digit_sep)
468 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
469 seen_digit_sep = true;
471 else if (c == '.')
473 if (seen_digit_sep || DIGIT_SEP (*str))
474 SYNTAX_ERROR_AT (virtual_location,
475 "digit separator adjacent to decimal point");
476 seen_digit_sep = false;
477 if (float_flag == NOT_FLOAT)
478 float_flag = AFTER_POINT;
479 else
480 SYNTAX_ERROR_AT (virtual_location,
481 "too many decimal points in number");
483 else if ((radix <= 10 && (c == 'e' || c == 'E'))
484 || (radix == 16 && (c == 'p' || c == 'P')))
486 if (seen_digit_sep || DIGIT_SEP (*str))
487 SYNTAX_ERROR_AT (virtual_location,
488 "digit separator adjacent to exponent");
489 float_flag = AFTER_EXPON;
490 break;
492 else
494 /* Start of suffix. */
495 str--;
496 break;
500 if (seen_digit_sep && float_flag != AFTER_EXPON)
501 SYNTAX_ERROR_AT (virtual_location,
502 "digit separator outside digit sequence");
504 /* The suffix may be for decimal fixed-point constants without exponent. */
505 if (radix != 16 && float_flag == NOT_FLOAT)
507 result = interpret_float_suffix (pfile, str, limit - str);
508 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
510 result |= CPP_N_FLOATING;
511 /* We need to restore the radix to 10, if the radix is 8. */
512 if (radix == 8)
513 radix = 10;
515 if (CPP_PEDANTIC (pfile))
516 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
517 "fixed-point constants are a GCC extension");
518 goto syntax_ok;
520 else
521 result = 0;
524 if (float_flag != NOT_FLOAT && radix == 8)
525 radix = 10;
527 if (max_digit >= radix)
529 if (radix == 2)
530 SYNTAX_ERROR2_AT (virtual_location,
531 "invalid digit \"%c\" in binary constant", '0' + max_digit);
532 else
533 SYNTAX_ERROR2_AT (virtual_location,
534 "invalid digit \"%c\" in octal constant", '0' + max_digit);
537 if (float_flag != NOT_FLOAT)
539 if (radix == 2)
541 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
542 "invalid prefix \"0b\" for floating constant");
543 return CPP_N_INVALID;
546 if (radix == 16 && !seen_digit)
547 SYNTAX_ERROR_AT (virtual_location,
548 "no digits in hexadecimal floating constant");
550 if (radix == 16 && CPP_PEDANTIC (pfile)
551 && !CPP_OPTION (pfile, extended_numbers))
553 if (CPP_OPTION (pfile, cplusplus))
554 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
555 "use of C++1z hexadecimal floating constant");
556 else
557 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
558 "use of C99 hexadecimal floating constant");
561 if (float_flag == AFTER_EXPON)
563 if (*str == '+' || *str == '-')
564 str++;
566 /* Exponent is decimal, even if string is a hex float. */
567 if (!ISDIGIT (*str))
569 if (DIGIT_SEP (*str))
570 SYNTAX_ERROR_AT (virtual_location,
571 "digit separator adjacent to exponent");
572 else
573 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
577 seen_digit_sep = DIGIT_SEP (*str);
578 str++;
580 while (ISDIGIT (*str) || DIGIT_SEP (*str));
582 else if (radix == 16)
583 SYNTAX_ERROR_AT (virtual_location,
584 "hexadecimal floating constants require an exponent");
586 if (seen_digit_sep)
587 SYNTAX_ERROR_AT (virtual_location,
588 "digit separator outside digit sequence");
590 result = interpret_float_suffix (pfile, str, limit - str);
591 if (result == 0)
593 if (CPP_OPTION (pfile, user_literals))
595 if (ud_suffix)
596 *ud_suffix = (const char *) str;
597 result = CPP_N_LARGE | CPP_N_USERDEF;
599 else
601 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
602 "invalid suffix \"%.*s\" on floating constant",
603 (int) (limit - str), str);
604 return CPP_N_INVALID;
608 /* Traditional C didn't accept any floating suffixes. */
609 if (limit != str
610 && CPP_WTRADITIONAL (pfile)
611 && ! cpp_sys_macro_p (pfile))
612 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
613 "traditional C rejects the \"%.*s\" suffix",
614 (int) (limit - str), str);
616 /* A suffix for double is a GCC extension via decimal float support.
617 If the suffix also specifies an imaginary value we'll catch that
618 later. */
619 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
620 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
621 "suffix for double constant is a GCC extension");
623 /* Radix must be 10 for decimal floats. */
624 if ((result & CPP_N_DFLOAT) && radix != 10)
626 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
627 "invalid suffix \"%.*s\" with hexadecimal floating constant",
628 (int) (limit - str), str);
629 return CPP_N_INVALID;
632 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
633 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
634 "fixed-point constants are a GCC extension");
636 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
637 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
638 "decimal float constants are a GCC extension");
640 result |= CPP_N_FLOATING;
642 else
644 result = interpret_int_suffix (pfile, str, limit - str);
645 if (result == 0)
647 if (CPP_OPTION (pfile, user_literals))
649 if (ud_suffix)
650 *ud_suffix = (const char *) str;
651 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
653 else
655 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
656 "invalid suffix \"%.*s\" on integer constant",
657 (int) (limit - str), str);
658 return CPP_N_INVALID;
662 /* Traditional C only accepted the 'L' suffix.
663 Suppress warning about 'LL' with -Wno-long-long. */
664 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
666 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
667 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
668 && CPP_OPTION (pfile, cpp_warn_long_long);
670 if (u_or_i || large)
671 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
672 virtual_location, 0,
673 "traditional C rejects the \"%.*s\" suffix",
674 (int) (limit - str), str);
677 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
678 && CPP_OPTION (pfile, cpp_warn_long_long))
680 const char *message = CPP_OPTION (pfile, cplusplus)
681 ? N_("use of C++11 long long integer constant")
682 : N_("use of C99 long long integer constant");
684 if (CPP_OPTION (pfile, c99))
685 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
686 0, message);
687 else
688 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
689 virtual_location, 0, message);
692 result |= CPP_N_INTEGER;
695 syntax_ok:
696 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
697 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
698 "imaginary constants are a GCC extension");
699 if (radix == 2
700 && !CPP_OPTION (pfile, binary_constants)
701 && CPP_PEDANTIC (pfile))
702 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
703 CPP_OPTION (pfile, cplusplus)
704 ? N_("binary constants are a C++14 feature "
705 "or GCC extension")
706 : N_("binary constants are a GCC extension"));
708 if (radix == 10)
709 result |= CPP_N_DECIMAL;
710 else if (radix == 16)
711 result |= CPP_N_HEX;
712 else if (radix == 2)
713 result |= CPP_N_BINARY;
714 else
715 result |= CPP_N_OCTAL;
717 return result;
719 syntax_error:
720 return CPP_N_INVALID;
723 /* cpp_interpret_integer converts an integer constant into a cpp_num,
724 of precision options->precision.
726 We do not provide any interface for decimal->float conversion,
727 because the preprocessor doesn't need it and we don't want to
728 drag in GCC's floating point emulator. */
729 cpp_num
730 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
731 unsigned int type)
733 const uchar *p, *end;
734 cpp_num result;
736 result.low = 0;
737 result.high = 0;
738 result.unsignedp = !!(type & CPP_N_UNSIGNED);
739 result.overflow = false;
741 p = token->val.str.text;
742 end = p + token->val.str.len;
744 /* Common case of a single digit. */
745 if (token->val.str.len == 1)
746 result.low = p[0] - '0';
747 else
749 cpp_num_part max;
750 size_t precision = CPP_OPTION (pfile, precision);
751 unsigned int base = 10, c = 0;
752 bool overflow = false;
754 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
756 base = 8;
757 p++;
759 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
761 base = 16;
762 p += 2;
764 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
766 base = 2;
767 p += 2;
770 /* We can add a digit to numbers strictly less than this without
771 needing the precision and slowness of double integers. */
772 max = ~(cpp_num_part) 0;
773 if (precision < PART_PRECISION)
774 max >>= PART_PRECISION - precision;
775 max = (max - base + 1) / base + 1;
777 for (; p < end; p++)
779 c = *p;
781 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
782 c = hex_value (c);
783 else if (DIGIT_SEP (c))
784 continue;
785 else
786 break;
788 /* Strict inequality for when max is set to zero. */
789 if (result.low < max)
790 result.low = result.low * base + c;
791 else
793 result = append_digit (result, c, base, precision);
794 overflow |= result.overflow;
795 max = 0;
799 if (overflow && !(type & CPP_N_USERDEF))
800 cpp_error (pfile, CPP_DL_PEDWARN,
801 "integer constant is too large for its type");
802 /* If too big to be signed, consider it unsigned. Only warn for
803 decimal numbers. Traditional numbers were always signed (but
804 we still honor an explicit U suffix); but we only have
805 traditional semantics in directives. */
806 else if (!result.unsignedp
807 && !(CPP_OPTION (pfile, traditional)
808 && pfile->state.in_directive)
809 && !num_positive (result, precision))
811 /* This is for constants within the range of uintmax_t but
812 not that of intmax_t. For such decimal constants, a
813 diagnostic is required for C99 as the selected type must
814 be signed and not having a type is a constraint violation
815 (DR#298, TC3), so this must be a pedwarn. For C90,
816 unsigned long is specified to be used for a constant that
817 does not fit in signed long; if uintmax_t has the same
818 range as unsigned long this means only a warning is
819 appropriate here. C90 permits the preprocessor to use a
820 wider range than unsigned long in the compiler, so if
821 uintmax_t is wider than unsigned long no diagnostic is
822 required for such constants in preprocessor #if
823 expressions and the compiler will pedwarn for such
824 constants outside the range of unsigned long that reach
825 the compiler so a diagnostic is not required there
826 either; thus, pedwarn for C99 but use a plain warning for
827 C90. */
828 if (base == 10)
829 cpp_error (pfile, (CPP_OPTION (pfile, c99)
830 ? CPP_DL_PEDWARN
831 : CPP_DL_WARNING),
832 "integer constant is so large that it is unsigned");
833 result.unsignedp = true;
837 return result;
840 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
841 static cpp_num
842 append_digit (cpp_num num, int digit, int base, size_t precision)
844 cpp_num result;
845 unsigned int shift;
846 bool overflow;
847 cpp_num_part add_high, add_low;
849 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
850 need to worry about add_high overflowing. */
851 switch (base)
853 case 2:
854 shift = 1;
855 break;
857 case 16:
858 shift = 4;
859 break;
861 default:
862 shift = 3;
864 overflow = !!(num.high >> (PART_PRECISION - shift));
865 result.high = num.high << shift;
866 result.low = num.low << shift;
867 result.high |= num.low >> (PART_PRECISION - shift);
868 result.unsignedp = num.unsignedp;
870 if (base == 10)
872 add_low = num.low << 1;
873 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
875 else
876 add_high = add_low = 0;
878 if (add_low + digit < add_low)
879 add_high++;
880 add_low += digit;
882 if (result.low + add_low < result.low)
883 add_high++;
884 if (result.high + add_high < result.high)
885 overflow = true;
887 result.low += add_low;
888 result.high += add_high;
889 result.overflow = overflow;
891 /* The above code catches overflow of a cpp_num type. This catches
892 overflow of the (possibly shorter) target precision. */
893 num.low = result.low;
894 num.high = result.high;
895 result = num_trim (result, precision);
896 if (!num_eq (result, num))
897 result.overflow = true;
899 return result;
902 /* Handle meeting "defined" in a preprocessor expression. */
903 static cpp_num
904 parse_defined (cpp_reader *pfile)
906 cpp_num result;
907 int paren = 0;
908 cpp_hashnode *node = 0;
909 const cpp_token *token;
910 cpp_context *initial_context = pfile->context;
912 /* Don't expand macros. */
913 pfile->state.prevent_expansion++;
915 token = cpp_get_token (pfile);
916 if (token->type == CPP_OPEN_PAREN)
918 paren = 1;
919 token = cpp_get_token (pfile);
922 if (token->type == CPP_NAME)
924 node = token->val.node.node;
925 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
927 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
928 node = 0;
931 else
933 cpp_error (pfile, CPP_DL_ERROR,
934 "operator \"defined\" requires an identifier");
935 if (token->flags & NAMED_OP)
937 cpp_token op;
939 op.flags = 0;
940 op.type = token->type;
941 cpp_error (pfile, CPP_DL_ERROR,
942 "(\"%s\" is an alternative token for \"%s\" in C++)",
943 cpp_token_as_text (pfile, token),
944 cpp_token_as_text (pfile, &op));
948 if (node)
950 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
951 cpp_error (pfile, CPP_DL_WARNING,
952 "this use of \"defined\" may not be portable");
954 _cpp_mark_macro_used (node);
955 if (!(node->flags & NODE_USED))
957 node->flags |= NODE_USED;
958 if (node->type == NT_MACRO)
960 if ((node->flags & NODE_BUILTIN)
961 && pfile->cb.user_builtin_macro)
962 pfile->cb.user_builtin_macro (pfile, node);
963 if (pfile->cb.used_define)
964 pfile->cb.used_define (pfile, pfile->directive_line, node);
966 else
968 if (pfile->cb.used_undef)
969 pfile->cb.used_undef (pfile, pfile->directive_line, node);
973 /* A possible controlling macro of the form #if !defined ().
974 _cpp_parse_expr checks there was no other junk on the line. */
975 pfile->mi_ind_cmacro = node;
978 pfile->state.prevent_expansion--;
980 /* Do not treat conditional macros as being defined. This is due to the
981 powerpc and spu ports using conditional macros for 'vector', 'bool', and
982 'pixel' to act as conditional keywords. This messes up tests like #ifndef
983 bool. */
984 result.unsignedp = false;
985 result.high = 0;
986 result.overflow = false;
987 result.low = (node && node->type == NT_MACRO
988 && (node->flags & NODE_CONDITIONAL) == 0);
989 return result;
992 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
993 number or character constant, or the result of the "defined" or "#"
994 operators). */
995 static cpp_num
996 eval_token (cpp_reader *pfile, const cpp_token *token,
997 source_location virtual_location)
999 cpp_num result;
1000 unsigned int temp;
1001 int unsignedp = 0;
1003 result.unsignedp = false;
1004 result.overflow = false;
1006 switch (token->type)
1008 case CPP_NUMBER:
1009 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1010 if (temp & CPP_N_USERDEF)
1011 cpp_error (pfile, CPP_DL_ERROR,
1012 "user-defined literal in preprocessor expression");
1013 switch (temp & CPP_N_CATEGORY)
1015 case CPP_N_FLOATING:
1016 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1017 "floating constant in preprocessor expression");
1018 break;
1019 case CPP_N_INTEGER:
1020 if (!(temp & CPP_N_IMAGINARY))
1021 return cpp_interpret_integer (pfile, token, temp);
1022 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1023 "imaginary number in preprocessor expression");
1024 break;
1026 case CPP_N_INVALID:
1027 /* Error already issued. */
1028 break;
1030 result.high = result.low = 0;
1031 break;
1033 case CPP_WCHAR:
1034 case CPP_CHAR:
1035 case CPP_CHAR16:
1036 case CPP_CHAR32:
1037 case CPP_UTF8CHAR:
1039 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1040 &temp, &unsignedp);
1042 result.high = 0;
1043 result.low = cc;
1044 /* Sign-extend the result if necessary. */
1045 if (!unsignedp && (cppchar_signed_t) cc < 0)
1047 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1048 result.low |= ~(~(cpp_num_part) 0
1049 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1050 result.high = ~(cpp_num_part) 0;
1051 result = num_trim (result, CPP_OPTION (pfile, precision));
1054 break;
1056 case CPP_NAME:
1057 if (token->val.node.node == pfile->spec_nodes.n_defined)
1058 return parse_defined (pfile);
1059 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1060 return parse_has_include (pfile, IT_INCLUDE);
1061 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1062 return parse_has_include (pfile, IT_INCLUDE_NEXT);
1063 else if (CPP_OPTION (pfile, cplusplus)
1064 && (token->val.node.node == pfile->spec_nodes.n_true
1065 || token->val.node.node == pfile->spec_nodes.n_false))
1067 result.high = 0;
1068 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1070 else
1072 result.high = 0;
1073 result.low = 0;
1074 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1075 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1076 "\"%s\" is not defined",
1077 NODE_NAME (token->val.node.node));
1079 break;
1081 case CPP_HASH:
1082 if (!pfile->state.skipping)
1084 /* A pedantic warning takes precedence over a deprecated
1085 warning here. */
1086 if (CPP_PEDANTIC (pfile))
1087 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1088 virtual_location, 0,
1089 "assertions are a GCC extension");
1090 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1091 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1092 "assertions are a deprecated extension");
1094 _cpp_test_assertion (pfile, &temp);
1095 result.high = 0;
1096 result.low = temp;
1097 break;
1099 default:
1100 abort ();
1103 result.unsignedp = !!unsignedp;
1104 return result;
1107 /* Operator precedence and flags table.
1109 After an operator is returned from the lexer, if it has priority less
1110 than the operator on the top of the stack, we reduce the stack by one
1111 operator and repeat the test. Since equal priorities do not reduce,
1112 this is naturally right-associative.
1114 We handle left-associative operators by decrementing the priority of
1115 just-lexed operators by one, but retaining the priority of operators
1116 already on the stack.
1118 The remaining cases are '(' and ')'. We handle '(' by skipping the
1119 reduction phase completely. ')' is given lower priority than
1120 everything else, including '(', effectively forcing a reduction of the
1121 parenthesized expression. If there is a matching '(', the routine
1122 reduce() exits immediately. If the normal exit route sees a ')', then
1123 there cannot have been a matching '(' and an error message is output.
1125 The parser assumes all shifted operators require a left operand unless
1126 the flag NO_L_OPERAND is set. These semantics are automatic; any
1127 extra semantics need to be handled with operator-specific code. */
1129 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1130 operand changes because of integer promotions. */
1131 #define NO_L_OPERAND (1 << 0)
1132 #define LEFT_ASSOC (1 << 1)
1133 #define CHECK_PROMOTION (1 << 2)
1135 /* Operator to priority map. Must be in the same order as the first
1136 N entries of enum cpp_ttype. */
1137 static const struct cpp_operator
1139 uchar prio;
1140 uchar flags;
1141 } optab[] =
1143 /* EQ */ {0, 0}, /* Shouldn't happen. */
1144 /* NOT */ {16, NO_L_OPERAND},
1145 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1146 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1147 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1148 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1149 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1150 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1151 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1152 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1153 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1154 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1155 /* RSHIFT */ {13, LEFT_ASSOC},
1156 /* LSHIFT */ {13, LEFT_ASSOC},
1158 /* COMPL */ {16, NO_L_OPERAND},
1159 /* AND_AND */ {6, LEFT_ASSOC},
1160 /* OR_OR */ {5, LEFT_ASSOC},
1161 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1162 However, there are some special cases for these in reduce(). */
1163 /* QUERY */ {4, 0},
1164 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1165 /* COMMA */ {4, LEFT_ASSOC},
1166 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1167 /* CLOSE_PAREN */ {0, 0},
1168 /* EOF */ {0, 0},
1169 /* EQ_EQ */ {11, LEFT_ASSOC},
1170 /* NOT_EQ */ {11, LEFT_ASSOC},
1171 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1172 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1173 /* UPLUS */ {16, NO_L_OPERAND},
1174 /* UMINUS */ {16, NO_L_OPERAND}
1177 /* Parse and evaluate a C expression, reading from PFILE.
1178 Returns the truth value of the expression.
1180 The implementation is an operator precedence parser, i.e. a
1181 bottom-up parser, using a stack for not-yet-reduced tokens.
1183 The stack base is op_stack, and the current stack pointer is 'top'.
1184 There is a stack element for each operator (only), and the most
1185 recently pushed operator is 'top->op'. An operand (value) is
1186 stored in the 'value' field of the stack element of the operator
1187 that precedes it. */
1188 bool
1189 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1191 struct op *top = pfile->op_stack;
1192 unsigned int lex_count;
1193 bool saw_leading_not, want_value = true;
1194 source_location virtual_location = 0;
1196 pfile->state.skip_eval = 0;
1198 /* Set up detection of #if ! defined(). */
1199 pfile->mi_ind_cmacro = 0;
1200 saw_leading_not = false;
1201 lex_count = 0;
1203 /* Lowest priority operator prevents further reductions. */
1204 top->op = CPP_EOF;
1206 for (;;)
1208 struct op op;
1210 lex_count++;
1211 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1212 op.op = op.token->type;
1213 op.loc = virtual_location;
1215 switch (op.op)
1217 /* These tokens convert into values. */
1218 case CPP_NUMBER:
1219 case CPP_CHAR:
1220 case CPP_WCHAR:
1221 case CPP_CHAR16:
1222 case CPP_CHAR32:
1223 case CPP_UTF8CHAR:
1224 case CPP_NAME:
1225 case CPP_HASH:
1226 if (!want_value)
1227 SYNTAX_ERROR2_AT (op.loc,
1228 "missing binary operator before token \"%s\"",
1229 cpp_token_as_text (pfile, op.token));
1230 want_value = false;
1231 top->value = eval_token (pfile, op.token, op.loc);
1232 continue;
1234 case CPP_NOT:
1235 saw_leading_not = lex_count == 1;
1236 break;
1237 case CPP_PLUS:
1238 if (want_value)
1239 op.op = CPP_UPLUS;
1240 break;
1241 case CPP_MINUS:
1242 if (want_value)
1243 op.op = CPP_UMINUS;
1244 break;
1246 default:
1247 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1248 SYNTAX_ERROR2_AT (op.loc,
1249 "token \"%s\" is not valid in preprocessor expressions",
1250 cpp_token_as_text (pfile, op.token));
1251 break;
1254 /* Check we have a value or operator as appropriate. */
1255 if (optab[op.op].flags & NO_L_OPERAND)
1257 if (!want_value)
1258 SYNTAX_ERROR2_AT (op.loc,
1259 "missing binary operator before token \"%s\"",
1260 cpp_token_as_text (pfile, op.token));
1262 else if (want_value)
1264 /* We want a number (or expression) and haven't got one.
1265 Try to emit a specific diagnostic. */
1266 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1267 SYNTAX_ERROR_AT (op.loc,
1268 "missing expression between '(' and ')'");
1270 if (op.op == CPP_EOF && top->op == CPP_EOF)
1271 SYNTAX_ERROR2_AT (op.loc,
1272 "%s with no expression", is_if ? "#if" : "#elif");
1274 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1275 SYNTAX_ERROR2_AT (op.loc,
1276 "operator '%s' has no right operand",
1277 cpp_token_as_text (pfile, top->token));
1278 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1279 /* Complain about missing paren during reduction. */;
1280 else
1281 SYNTAX_ERROR2_AT (op.loc,
1282 "operator '%s' has no left operand",
1283 cpp_token_as_text (pfile, op.token));
1286 top = reduce (pfile, top, op.op);
1287 if (!top)
1288 goto syntax_error;
1290 if (op.op == CPP_EOF)
1291 break;
1293 switch (op.op)
1295 case CPP_CLOSE_PAREN:
1296 continue;
1297 case CPP_OR_OR:
1298 if (!num_zerop (top->value))
1299 pfile->state.skip_eval++;
1300 break;
1301 case CPP_AND_AND:
1302 case CPP_QUERY:
1303 if (num_zerop (top->value))
1304 pfile->state.skip_eval++;
1305 break;
1306 case CPP_COLON:
1307 if (top->op != CPP_QUERY)
1308 SYNTAX_ERROR_AT (op.loc,
1309 " ':' without preceding '?'");
1310 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1311 pfile->state.skip_eval++;
1312 else
1313 pfile->state.skip_eval--;
1314 default:
1315 break;
1318 want_value = true;
1320 /* Check for and handle stack overflow. */
1321 if (++top == pfile->op_limit)
1322 top = _cpp_expand_op_stack (pfile);
1324 top->op = op.op;
1325 top->token = op.token;
1326 top->loc = op.loc;
1329 /* The controlling macro expression is only valid if we called lex 3
1330 times: <!> <defined expression> and <EOF>. push_conditional ()
1331 checks that we are at top-of-file. */
1332 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1333 pfile->mi_ind_cmacro = 0;
1335 if (top != pfile->op_stack)
1337 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1338 "unbalanced stack in %s",
1339 is_if ? "#if" : "#elif");
1340 syntax_error:
1341 return false; /* Return false on syntax error. */
1344 return !num_zerop (top->value);
1347 /* Reduce the operator / value stack if possible, in preparation for
1348 pushing operator OP. Returns NULL on error, otherwise the top of
1349 the stack. */
1350 static struct op *
1351 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1353 unsigned int prio;
1355 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1357 bad_op:
1358 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1359 return 0;
1362 if (op == CPP_OPEN_PAREN)
1363 return top;
1365 /* Decrement the priority of left-associative operators to force a
1366 reduction with operators of otherwise equal priority. */
1367 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1368 while (prio < optab[top->op].prio)
1370 if (CPP_OPTION (pfile, warn_num_sign_change)
1371 && optab[top->op].flags & CHECK_PROMOTION)
1372 check_promotion (pfile, top);
1374 switch (top->op)
1376 case CPP_UPLUS:
1377 case CPP_UMINUS:
1378 case CPP_NOT:
1379 case CPP_COMPL:
1380 top[-1].value = num_unary_op (pfile, top->value, top->op);
1381 top[-1].loc = top->loc;
1382 break;
1384 case CPP_PLUS:
1385 case CPP_MINUS:
1386 case CPP_RSHIFT:
1387 case CPP_LSHIFT:
1388 case CPP_COMMA:
1389 top[-1].value = num_binary_op (pfile, top[-1].value,
1390 top->value, top->op);
1391 top[-1].loc = top->loc;
1392 break;
1394 case CPP_GREATER:
1395 case CPP_LESS:
1396 case CPP_GREATER_EQ:
1397 case CPP_LESS_EQ:
1398 top[-1].value
1399 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1400 top[-1].loc = top->loc;
1401 break;
1403 case CPP_EQ_EQ:
1404 case CPP_NOT_EQ:
1405 top[-1].value
1406 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1407 top[-1].loc = top->loc;
1408 break;
1410 case CPP_AND:
1411 case CPP_OR:
1412 case CPP_XOR:
1413 top[-1].value
1414 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1415 top[-1].loc = top->loc;
1416 break;
1418 case CPP_MULT:
1419 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1420 top[-1].loc = top->loc;
1421 break;
1423 case CPP_DIV:
1424 case CPP_MOD:
1425 top[-1].value = num_div_op (pfile, top[-1].value,
1426 top->value, top->op, top->loc);
1427 top[-1].loc = top->loc;
1428 break;
1430 case CPP_OR_OR:
1431 top--;
1432 if (!num_zerop (top->value))
1433 pfile->state.skip_eval--;
1434 top->value.low = (!num_zerop (top->value)
1435 || !num_zerop (top[1].value));
1436 top->value.high = 0;
1437 top->value.unsignedp = false;
1438 top->value.overflow = false;
1439 top->loc = top[1].loc;
1440 continue;
1442 case CPP_AND_AND:
1443 top--;
1444 if (num_zerop (top->value))
1445 pfile->state.skip_eval--;
1446 top->value.low = (!num_zerop (top->value)
1447 && !num_zerop (top[1].value));
1448 top->value.high = 0;
1449 top->value.unsignedp = false;
1450 top->value.overflow = false;
1451 top->loc = top[1].loc;
1452 continue;
1454 case CPP_OPEN_PAREN:
1455 if (op != CPP_CLOSE_PAREN)
1457 cpp_error_with_line (pfile, CPP_DL_ERROR,
1458 top->token->src_loc,
1459 0, "missing ')' in expression");
1460 return 0;
1462 top--;
1463 top->value = top[1].value;
1464 top->loc = top[1].loc;
1465 return top;
1467 case CPP_COLON:
1468 top -= 2;
1469 if (!num_zerop (top->value))
1471 pfile->state.skip_eval--;
1472 top->value = top[1].value;
1473 top->loc = top[1].loc;
1475 else
1477 top->value = top[2].value;
1478 top->loc = top[2].loc;
1480 top->value.unsignedp = (top[1].value.unsignedp
1481 || top[2].value.unsignedp);
1482 continue;
1484 case CPP_QUERY:
1485 /* COMMA and COLON should not reduce a QUERY operator. */
1486 if (op == CPP_COMMA || op == CPP_COLON)
1487 return top;
1488 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1489 return 0;
1491 default:
1492 goto bad_op;
1495 top--;
1496 if (top->value.overflow && !pfile->state.skip_eval)
1497 cpp_error (pfile, CPP_DL_PEDWARN,
1498 "integer overflow in preprocessor expression");
1501 if (op == CPP_CLOSE_PAREN)
1503 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1504 return 0;
1507 return top;
1510 /* Returns the position of the old top of stack after expansion. */
1511 struct op *
1512 _cpp_expand_op_stack (cpp_reader *pfile)
1514 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1515 size_t new_size = old_size * 2 + 20;
1517 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1518 pfile->op_limit = pfile->op_stack + new_size;
1520 return pfile->op_stack + old_size;
1523 /* Emits a warning if the effective sign of either operand of OP
1524 changes because of integer promotions. */
1525 static void
1526 check_promotion (cpp_reader *pfile, const struct op *op)
1528 if (op->value.unsignedp == op[-1].value.unsignedp)
1529 return;
1531 if (op->value.unsignedp)
1533 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1534 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1535 "the left operand of \"%s\" changes sign when promoted",
1536 cpp_token_as_text (pfile, op->token));
1538 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1539 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1540 "the right operand of \"%s\" changes sign when promoted",
1541 cpp_token_as_text (pfile, op->token));
1544 /* Clears the unused high order bits of the number pointed to by PNUM. */
1545 static cpp_num
1546 num_trim (cpp_num num, size_t precision)
1548 if (precision > PART_PRECISION)
1550 precision -= PART_PRECISION;
1551 if (precision < PART_PRECISION)
1552 num.high &= ((cpp_num_part) 1 << precision) - 1;
1554 else
1556 if (precision < PART_PRECISION)
1557 num.low &= ((cpp_num_part) 1 << precision) - 1;
1558 num.high = 0;
1561 return num;
1564 /* True iff A (presumed signed) >= 0. */
1565 static bool
1566 num_positive (cpp_num num, size_t precision)
1568 if (precision > PART_PRECISION)
1570 precision -= PART_PRECISION;
1571 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1574 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1577 /* Sign extend a number, with PRECISION significant bits and all
1578 others assumed clear, to fill out a cpp_num structure. */
1579 cpp_num
1580 cpp_num_sign_extend (cpp_num num, size_t precision)
1582 if (!num.unsignedp)
1584 if (precision > PART_PRECISION)
1586 precision -= PART_PRECISION;
1587 if (precision < PART_PRECISION
1588 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1589 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1591 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1593 if (precision < PART_PRECISION)
1594 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1595 num.high = ~(cpp_num_part) 0;
1599 return num;
1602 /* Returns the negative of NUM. */
1603 static cpp_num
1604 num_negate (cpp_num num, size_t precision)
1606 cpp_num copy;
1608 copy = num;
1609 num.high = ~num.high;
1610 num.low = ~num.low;
1611 if (++num.low == 0)
1612 num.high++;
1613 num = num_trim (num, precision);
1614 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1616 return num;
1619 /* Returns true if A >= B. */
1620 static bool
1621 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1623 bool unsignedp;
1625 unsignedp = pa.unsignedp || pb.unsignedp;
1627 if (!unsignedp)
1629 /* Both numbers have signed type. If they are of different
1630 sign, the answer is the sign of A. */
1631 unsignedp = num_positive (pa, precision);
1633 if (unsignedp != num_positive (pb, precision))
1634 return unsignedp;
1636 /* Otherwise we can do an unsigned comparison. */
1639 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1642 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1643 static cpp_num
1644 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1645 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1647 lhs.overflow = false;
1648 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1650 /* As excess precision is zeroed, there is no need to num_trim () as
1651 these operations cannot introduce a set bit there. */
1652 if (op == CPP_AND)
1654 lhs.low &= rhs.low;
1655 lhs.high &= rhs.high;
1657 else if (op == CPP_OR)
1659 lhs.low |= rhs.low;
1660 lhs.high |= rhs.high;
1662 else
1664 lhs.low ^= rhs.low;
1665 lhs.high ^= rhs.high;
1668 return lhs;
1671 /* Returns LHS OP RHS, where OP is an inequality. */
1672 static cpp_num
1673 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1674 enum cpp_ttype op)
1676 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1678 if (op == CPP_GREATER_EQ)
1679 lhs.low = gte;
1680 else if (op == CPP_LESS)
1681 lhs.low = !gte;
1682 else if (op == CPP_GREATER)
1683 lhs.low = gte && !num_eq (lhs, rhs);
1684 else /* CPP_LESS_EQ. */
1685 lhs.low = !gte || num_eq (lhs, rhs);
1687 lhs.high = 0;
1688 lhs.overflow = false;
1689 lhs.unsignedp = false;
1690 return lhs;
1693 /* Returns LHS OP RHS, where OP is == or !=. */
1694 static cpp_num
1695 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1696 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1698 /* Work around a 3.0.4 bug; see PR 6950. */
1699 bool eq = num_eq (lhs, rhs);
1700 if (op == CPP_NOT_EQ)
1701 eq = !eq;
1702 lhs.low = eq;
1703 lhs.high = 0;
1704 lhs.overflow = false;
1705 lhs.unsignedp = false;
1706 return lhs;
1709 /* Shift NUM, of width PRECISION, right by N bits. */
1710 static cpp_num
1711 num_rshift (cpp_num num, size_t precision, size_t n)
1713 cpp_num_part sign_mask;
1714 bool x = num_positive (num, precision);
1716 if (num.unsignedp || x)
1717 sign_mask = 0;
1718 else
1719 sign_mask = ~(cpp_num_part) 0;
1721 if (n >= precision)
1722 num.high = num.low = sign_mask;
1723 else
1725 /* Sign-extend. */
1726 if (precision < PART_PRECISION)
1727 num.high = sign_mask, num.low |= sign_mask << precision;
1728 else if (precision < 2 * PART_PRECISION)
1729 num.high |= sign_mask << (precision - PART_PRECISION);
1731 if (n >= PART_PRECISION)
1733 n -= PART_PRECISION;
1734 num.low = num.high;
1735 num.high = sign_mask;
1738 if (n)
1740 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1741 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1745 num = num_trim (num, precision);
1746 num.overflow = false;
1747 return num;
1750 /* Shift NUM, of width PRECISION, left by N bits. */
1751 static cpp_num
1752 num_lshift (cpp_num num, size_t precision, size_t n)
1754 if (n >= precision)
1756 num.overflow = !num.unsignedp && !num_zerop (num);
1757 num.high = num.low = 0;
1759 else
1761 cpp_num orig, maybe_orig;
1762 size_t m = n;
1764 orig = num;
1765 if (m >= PART_PRECISION)
1767 m -= PART_PRECISION;
1768 num.high = num.low;
1769 num.low = 0;
1771 if (m)
1773 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1774 num.low <<= m;
1776 num = num_trim (num, precision);
1778 if (num.unsignedp)
1779 num.overflow = false;
1780 else
1782 maybe_orig = num_rshift (num, precision, n);
1783 num.overflow = !num_eq (orig, maybe_orig);
1787 return num;
1790 /* The four unary operators: +, -, ! and ~. */
1791 static cpp_num
1792 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1794 switch (op)
1796 case CPP_UPLUS:
1797 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1798 cpp_warning (pfile, CPP_W_TRADITIONAL,
1799 "traditional C rejects the unary plus operator");
1800 num.overflow = false;
1801 break;
1803 case CPP_UMINUS:
1804 num = num_negate (num, CPP_OPTION (pfile, precision));
1805 break;
1807 case CPP_COMPL:
1808 num.high = ~num.high;
1809 num.low = ~num.low;
1810 num = num_trim (num, CPP_OPTION (pfile, precision));
1811 num.overflow = false;
1812 break;
1814 default: /* case CPP_NOT: */
1815 num.low = num_zerop (num);
1816 num.high = 0;
1817 num.overflow = false;
1818 num.unsignedp = false;
1819 break;
1822 return num;
1825 /* The various binary operators. */
1826 static cpp_num
1827 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1829 cpp_num result;
1830 size_t precision = CPP_OPTION (pfile, precision);
1831 size_t n;
1833 switch (op)
1835 /* Shifts. */
1836 case CPP_LSHIFT:
1837 case CPP_RSHIFT:
1838 if (!rhs.unsignedp && !num_positive (rhs, precision))
1840 /* A negative shift is a positive shift the other way. */
1841 if (op == CPP_LSHIFT)
1842 op = CPP_RSHIFT;
1843 else
1844 op = CPP_LSHIFT;
1845 rhs = num_negate (rhs, precision);
1847 if (rhs.high)
1848 n = ~0; /* Maximal. */
1849 else
1850 n = rhs.low;
1851 if (op == CPP_LSHIFT)
1852 lhs = num_lshift (lhs, precision, n);
1853 else
1854 lhs = num_rshift (lhs, precision, n);
1855 break;
1857 /* Arithmetic. */
1858 case CPP_MINUS:
1859 result.low = lhs.low - rhs.low;
1860 result.high = lhs.high - rhs.high;
1861 if (result.low > lhs.low)
1862 result.high--;
1863 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1864 result.overflow = false;
1866 result = num_trim (result, precision);
1867 if (!result.unsignedp)
1869 bool lhsp = num_positive (lhs, precision);
1870 result.overflow = (lhsp != num_positive (rhs, precision)
1871 && lhsp != num_positive (result, precision));
1873 return result;
1875 case CPP_PLUS:
1876 result.low = lhs.low + rhs.low;
1877 result.high = lhs.high + rhs.high;
1878 if (result.low < lhs.low)
1879 result.high++;
1880 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1881 result.overflow = false;
1883 result = num_trim (result, precision);
1884 if (!result.unsignedp)
1886 bool lhsp = num_positive (lhs, precision);
1887 result.overflow = (lhsp == num_positive (rhs, precision)
1888 && lhsp != num_positive (result, precision));
1890 return result;
1892 /* Comma. */
1893 default: /* case CPP_COMMA: */
1894 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1895 || !pfile->state.skip_eval))
1896 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1897 "comma operator in operand of #if");
1898 lhs = rhs;
1899 break;
1902 return lhs;
1905 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1906 cannot overflow. */
1907 static cpp_num
1908 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1910 cpp_num result;
1911 cpp_num_part middle[2], temp;
1913 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1914 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1916 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1917 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1919 temp = result.low;
1920 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1921 if (result.low < temp)
1922 result.high++;
1924 temp = result.low;
1925 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1926 if (result.low < temp)
1927 result.high++;
1929 result.high += HIGH_PART (middle[0]);
1930 result.high += HIGH_PART (middle[1]);
1931 result.unsignedp = true;
1932 result.overflow = false;
1934 return result;
1937 /* Multiply two preprocessing numbers. */
1938 static cpp_num
1939 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1941 cpp_num result, temp;
1942 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1943 bool overflow, negate = false;
1944 size_t precision = CPP_OPTION (pfile, precision);
1946 /* Prepare for unsigned multiplication. */
1947 if (!unsignedp)
1949 if (!num_positive (lhs, precision))
1950 negate = !negate, lhs = num_negate (lhs, precision);
1951 if (!num_positive (rhs, precision))
1952 negate = !negate, rhs = num_negate (rhs, precision);
1955 overflow = lhs.high && rhs.high;
1956 result = num_part_mul (lhs.low, rhs.low);
1958 temp = num_part_mul (lhs.high, rhs.low);
1959 result.high += temp.low;
1960 if (temp.high)
1961 overflow = true;
1963 temp = num_part_mul (lhs.low, rhs.high);
1964 result.high += temp.low;
1965 if (temp.high)
1966 overflow = true;
1968 temp.low = result.low, temp.high = result.high;
1969 result = num_trim (result, precision);
1970 if (!num_eq (result, temp))
1971 overflow = true;
1973 if (negate)
1974 result = num_negate (result, precision);
1976 if (unsignedp)
1977 result.overflow = false;
1978 else
1979 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1980 && !num_zerop (result));
1981 result.unsignedp = unsignedp;
1983 return result;
1986 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1987 or the remainder depending upon OP. LOCATION is the source location
1988 of this operator (for diagnostics). */
1990 static cpp_num
1991 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1992 source_location location)
1994 cpp_num result, sub;
1995 cpp_num_part mask;
1996 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1997 bool negate = false, lhs_neg = false;
1998 size_t i, precision = CPP_OPTION (pfile, precision);
2000 /* Prepare for unsigned division. */
2001 if (!unsignedp)
2003 if (!num_positive (lhs, precision))
2004 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2005 if (!num_positive (rhs, precision))
2006 negate = !negate, rhs = num_negate (rhs, precision);
2009 /* Find the high bit. */
2010 if (rhs.high)
2012 i = precision - 1;
2013 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2014 for (; ; i--, mask >>= 1)
2015 if (rhs.high & mask)
2016 break;
2018 else if (rhs.low)
2020 if (precision > PART_PRECISION)
2021 i = precision - PART_PRECISION - 1;
2022 else
2023 i = precision - 1;
2024 mask = (cpp_num_part) 1 << i;
2025 for (; ; i--, mask >>= 1)
2026 if (rhs.low & mask)
2027 break;
2029 else
2031 if (!pfile->state.skip_eval)
2032 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2033 "division by zero in #if");
2034 return lhs;
2037 /* First nonzero bit of RHS is bit I. Do naive division by
2038 shifting the RHS fully left, and subtracting from LHS if LHS is
2039 at least as big, and then repeating but with one less shift.
2040 This is not very efficient, but is easy to understand. */
2042 rhs.unsignedp = true;
2043 lhs.unsignedp = true;
2044 i = precision - i - 1;
2045 sub = num_lshift (rhs, precision, i);
2047 result.high = result.low = 0;
2048 for (;;)
2050 if (num_greater_eq (lhs, sub, precision))
2052 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2053 if (i >= PART_PRECISION)
2054 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2055 else
2056 result.low |= (cpp_num_part) 1 << i;
2058 if (i-- == 0)
2059 break;
2060 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2061 sub.high >>= 1;
2064 /* We divide so that the remainder has the sign of the LHS. */
2065 if (op == CPP_DIV)
2067 result.unsignedp = unsignedp;
2068 result.overflow = false;
2069 if (!unsignedp)
2071 if (negate)
2072 result = num_negate (result, precision);
2073 result.overflow = (num_positive (result, precision) ^ !negate
2074 && !num_zerop (result));
2077 return result;
2080 /* CPP_MOD. */
2081 lhs.unsignedp = unsignedp;
2082 lhs.overflow = false;
2083 if (lhs_neg)
2084 lhs = num_negate (lhs, precision);
2086 return lhs;
2089 /* Handle meeting "__has_include__" in a preprocessor expression. */
2090 static cpp_num
2091 parse_has_include (cpp_reader *pfile, enum include_type type)
2093 cpp_num result;
2094 bool paren = false;
2095 cpp_hashnode *node = 0;
2096 const cpp_token *token;
2097 bool bracket = false;
2098 char *fname = 0;
2100 result.unsignedp = false;
2101 result.high = 0;
2102 result.overflow = false;
2103 result.low = 0;
2105 pfile->state.in__has_include__++;
2107 token = cpp_get_token (pfile);
2108 if (token->type == CPP_OPEN_PAREN)
2110 paren = true;
2111 token = cpp_get_token (pfile);
2114 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2116 if (token->type == CPP_HEADER_NAME)
2117 bracket = true;
2118 fname = XNEWVEC (char, token->val.str.len - 1);
2119 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2120 fname[token->val.str.len - 2] = '\0';
2121 node = token->val.node.node;
2123 else if (token->type == CPP_LESS)
2125 bracket = true;
2126 fname = _cpp_bracket_include (pfile);
2128 else
2129 cpp_error (pfile, CPP_DL_ERROR,
2130 "operator \"__has_include__\" requires a header string");
2132 if (fname)
2134 int angle_brackets = (bracket ? 1 : 0);
2136 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2137 result.low = 1;
2138 else
2139 result.low = 0;
2141 XDELETEVEC (fname);
2144 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2145 cpp_error (pfile, CPP_DL_ERROR,
2146 "missing ')' after \"__has_include__\"");
2148 /* A possible controlling macro of the form #if !__has_include__ ().
2149 _cpp_parse_expr checks there was no other junk on the line. */
2150 if (node)
2151 pfile->mi_ind_cmacro = node;
2153 pfile->state.in__has_include__--;
2155 return result;