* doc/xml/manual/policy_data_structures.xml: Simplify and
[official-gcc.git] / libcpp / expr.c
blob3eac3768bbde6c0fac11966a1878fffb29c09876
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2017 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 (of CPP_N_* bits) 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, fn, fnx, fn_bits;
96 flags = 0;
97 f = d = l = w = q = i = fn = fnx = fn_bits = 0;
99 /* The following decimal float suffixes, from TR 24732:2009 and TS
100 18661-2:2015, are supported:
102 df, DF - _Decimal32.
103 dd, DD - _Decimal64.
104 dl, DL - _Decimal128.
106 The dN and DN suffixes for _DecimalN, and dNx and DNx for
107 _DecimalNx, defined in TS 18661-3:2015, are not supported.
109 Fixed-point suffixes, from TR 18037:2008, are supported. They
110 consist of three parts, in order:
112 (i) An optional u or U, for unsigned types.
114 (ii) An optional h or H, for short types, or l or L, for long
115 types, or ll or LL, for long long types. Use of ll or LL is a
116 GNU extension.
118 (iii) r or R, for _Fract types, or k or K, for _Accum types.
120 Otherwise the suffix is for a binary or standard floating-point
121 type. Such a suffix, or the absence of a suffix, may be preceded
122 or followed by i, I, j or J, to indicate an imaginary number with
123 the corresponding complex type. The following suffixes for
124 binary or standard floating-point types are supported:
126 f, F - float (ISO C and C++).
127 l, L - long double (ISO C and C++).
128 d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
129 operation (from TR 24732:2009; the pragma and the suffix
130 are not included in TS 18661-2:2015).
131 w, W - machine-specific type such as __float80 (GNU extension).
132 q, Q - machine-specific type such as __float128 (GNU extension).
133 fN, FN - _FloatN (TS 18661-3:2015).
134 fNx, FNx - _FloatNx (TS 18661-3:2015). */
136 /* Process decimal float suffixes, which are two letters starting
137 with d or D. Order and case are significant. */
138 if (len == 2 && (*s == 'd' || *s == 'D'))
140 bool uppercase = (*s == 'D');
141 switch (s[1])
143 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
144 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
145 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
146 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
147 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
148 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
149 default:
150 /* Additional two-character suffixes beginning with D are not
151 for decimal float constants. */
152 break;
156 if (CPP_OPTION (pfile, ext_numeric_literals))
158 /* Recognize a fixed-point suffix. */
159 if (len != 0)
160 switch (s[len-1])
162 case 'k': case 'K': flags = CPP_N_ACCUM; break;
163 case 'r': case 'R': flags = CPP_N_FRACT; break;
164 default: break;
167 /* Continue processing a fixed-point suffix. The suffix is case
168 insensitive except for ll or LL. Order is significant. */
169 if (flags)
171 if (len == 1)
172 return flags;
173 len--;
175 if (*s == 'u' || *s == 'U')
177 flags |= CPP_N_UNSIGNED;
178 if (len == 1)
179 return flags;
180 len--;
181 s++;
184 switch (*s)
186 case 'h': case 'H':
187 if (len == 1)
188 return flags |= CPP_N_SMALL;
189 break;
190 case 'l':
191 if (len == 1)
192 return flags |= CPP_N_MEDIUM;
193 if (len == 2 && s[1] == 'l')
194 return flags |= CPP_N_LARGE;
195 break;
196 case 'L':
197 if (len == 1)
198 return flags |= CPP_N_MEDIUM;
199 if (len == 2 && s[1] == 'L')
200 return flags |= CPP_N_LARGE;
201 break;
202 default:
203 break;
205 /* Anything left at this point is invalid. */
206 return 0;
210 /* In any remaining valid suffix, the case and order don't matter. */
211 while (len--)
213 switch (s[0])
215 case 'f': case 'F':
216 f++;
217 if (len > 0
218 && !CPP_OPTION (pfile, cplusplus)
219 && s[1] >= '1'
220 && s[1] <= '9'
221 && fn_bits == 0)
223 f--;
224 while (len > 0
225 && s[1] >= '0'
226 && s[1] <= '9'
227 && fn_bits < CPP_FLOATN_MAX)
229 fn_bits = fn_bits * 10 + (s[1] - '0');
230 len--;
231 s++;
233 if (len > 0 && s[1] == 'x')
235 fnx++;
236 len--;
237 s++;
239 else
240 fn++;
242 break;
243 case 'd': case 'D': d++; break;
244 case 'l': case 'L': l++; break;
245 case 'w': case 'W': w++; break;
246 case 'q': case 'Q': q++; break;
247 case 'i': case 'I':
248 case 'j': case 'J': i++; break;
249 default:
250 return 0;
252 s++;
255 /* Reject any case of multiple suffixes specifying types, multiple
256 suffixes specifying an imaginary constant, _FloatN or _FloatNx
257 suffixes for invalid values of N, and _FloatN suffixes for values
258 of N larger than can be represented in the return value. The
259 caller is responsible for rejecting _FloatN suffixes where
260 _FloatN is not supported on the chosen target. */
261 if (f + d + l + w + q + fn + fnx > 1 || i > 1)
262 return 0;
263 if (fn_bits > CPP_FLOATN_MAX)
264 return 0;
265 if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128)
266 return 0;
267 if (fn && fn_bits != 16 && fn_bits % 32 != 0)
268 return 0;
269 if (fn && fn_bits == 96)
270 return 0;
272 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
273 return 0;
275 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
276 return 0;
278 return ((i ? CPP_N_IMAGINARY : 0)
279 | (f ? CPP_N_SMALL :
280 d ? CPP_N_MEDIUM :
281 l ? CPP_N_LARGE :
282 w ? CPP_N_MD_W :
283 q ? CPP_N_MD_Q :
284 fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) :
285 fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) :
286 CPP_N_DEFAULT));
289 /* Return the classification flags for a float suffix. */
290 unsigned int
291 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
293 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
296 /* Subroutine of cpp_classify_number. S points to an integer suffix
297 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
298 flag vector describing the suffix. */
299 static unsigned int
300 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
302 size_t u, l, i;
304 u = l = i = 0;
306 while (len--)
307 switch (s[len])
309 case 'u': case 'U': u++; break;
310 case 'i': case 'I':
311 case 'j': case 'J': i++; break;
312 case 'l': case 'L': l++;
313 /* If there are two Ls, they must be adjacent and the same case. */
314 if (l == 2 && s[len] != s[len + 1])
315 return 0;
316 break;
317 default:
318 return 0;
321 if (l > 2 || u > 1 || i > 1)
322 return 0;
324 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
325 return 0;
327 return ((i ? CPP_N_IMAGINARY : 0)
328 | (u ? CPP_N_UNSIGNED : 0)
329 | ((l == 0) ? CPP_N_SMALL
330 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
333 /* Return the classification flags for an int suffix. */
334 unsigned int
335 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
337 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
340 /* Return the string type corresponding to the the input user-defined string
341 literal type. If the input type is not a user-defined string literal
342 type return the input type. */
343 enum cpp_ttype
344 cpp_userdef_string_remove_type (enum cpp_ttype type)
346 if (type == CPP_STRING_USERDEF)
347 return CPP_STRING;
348 else if (type == CPP_WSTRING_USERDEF)
349 return CPP_WSTRING;
350 else if (type == CPP_STRING16_USERDEF)
351 return CPP_STRING16;
352 else if (type == CPP_STRING32_USERDEF)
353 return CPP_STRING32;
354 else if (type == CPP_UTF8STRING_USERDEF)
355 return CPP_UTF8STRING;
356 else
357 return type;
360 /* Return the user-defined string literal type corresponding to the input
361 string type. If the input type is not a string type return the input
362 type. */
363 enum cpp_ttype
364 cpp_userdef_string_add_type (enum cpp_ttype type)
366 if (type == CPP_STRING)
367 return CPP_STRING_USERDEF;
368 else if (type == CPP_WSTRING)
369 return CPP_WSTRING_USERDEF;
370 else if (type == CPP_STRING16)
371 return CPP_STRING16_USERDEF;
372 else if (type == CPP_STRING32)
373 return CPP_STRING32_USERDEF;
374 else if (type == CPP_UTF8STRING)
375 return CPP_UTF8STRING_USERDEF;
376 else
377 return type;
380 /* Return the char type corresponding to the the input user-defined char
381 literal type. If the input type is not a user-defined char literal
382 type return the input type. */
383 enum cpp_ttype
384 cpp_userdef_char_remove_type (enum cpp_ttype type)
386 if (type == CPP_CHAR_USERDEF)
387 return CPP_CHAR;
388 else if (type == CPP_WCHAR_USERDEF)
389 return CPP_WCHAR;
390 else if (type == CPP_CHAR16_USERDEF)
391 return CPP_CHAR16;
392 else if (type == CPP_CHAR32_USERDEF)
393 return CPP_CHAR32;
394 else if (type == CPP_UTF8CHAR_USERDEF)
395 return CPP_UTF8CHAR;
396 else
397 return type;
400 /* Return the user-defined char literal type corresponding to the input
401 char type. If the input type is not a char type return the input
402 type. */
403 enum cpp_ttype
404 cpp_userdef_char_add_type (enum cpp_ttype type)
406 if (type == CPP_CHAR)
407 return CPP_CHAR_USERDEF;
408 else if (type == CPP_WCHAR)
409 return CPP_WCHAR_USERDEF;
410 else if (type == CPP_CHAR16)
411 return CPP_CHAR16_USERDEF;
412 else if (type == CPP_CHAR32)
413 return CPP_CHAR32_USERDEF;
414 else if (type == CPP_UTF8CHAR)
415 return CPP_UTF8CHAR_USERDEF;
416 else
417 return type;
420 /* Return true if the token type is a user-defined string literal. */
421 bool
422 cpp_userdef_string_p (enum cpp_ttype type)
424 if (type == CPP_STRING_USERDEF
425 || type == CPP_WSTRING_USERDEF
426 || type == CPP_STRING16_USERDEF
427 || type == CPP_STRING32_USERDEF
428 || type == CPP_UTF8STRING_USERDEF)
429 return true;
430 else
431 return false;
434 /* Return true if the token type is a user-defined char literal. */
435 bool
436 cpp_userdef_char_p (enum cpp_ttype type)
438 if (type == CPP_CHAR_USERDEF
439 || type == CPP_WCHAR_USERDEF
440 || type == CPP_CHAR16_USERDEF
441 || type == CPP_CHAR32_USERDEF
442 || type == CPP_UTF8CHAR_USERDEF)
443 return true;
444 else
445 return false;
448 /* Extract the suffix from a user-defined literal string or char. */
449 const char *
450 cpp_get_userdef_suffix (const cpp_token *tok)
452 unsigned int len = tok->val.str.len;
453 const char *text = (const char *)tok->val.str.text;
454 char delim;
455 unsigned int i;
456 for (i = 0; i < len; ++i)
457 if (text[i] == '\'' || text[i] == '"')
458 break;
459 if (i == len)
460 return text + len;
461 delim = text[i];
462 for (i = len; i > 0; --i)
463 if (text[i - 1] == delim)
464 break;
465 return text + i;
468 /* Categorize numeric constants according to their field (integer,
469 floating point, or invalid), radix (decimal, octal, hexadecimal),
470 and type suffixes.
472 TOKEN is the token that represents the numeric constant to
473 classify.
475 In C++0X if UD_SUFFIX is non null it will be assigned
476 any unrecognized suffix for a user-defined literal.
478 VIRTUAL_LOCATION is the virtual location for TOKEN. */
479 unsigned int
480 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
481 const char **ud_suffix, source_location virtual_location)
483 const uchar *str = token->val.str.text;
484 const uchar *limit;
485 unsigned int max_digit, result, radix;
486 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
487 bool seen_digit;
488 bool seen_digit_sep;
490 if (ud_suffix)
491 *ud_suffix = NULL;
493 /* If the lexer has done its job, length one can only be a single
494 digit. Fast-path this very common case. */
495 if (token->val.str.len == 1)
496 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
498 limit = str + token->val.str.len;
499 float_flag = NOT_FLOAT;
500 max_digit = 0;
501 radix = 10;
502 seen_digit = false;
503 seen_digit_sep = false;
505 /* First, interpret the radix. */
506 if (*str == '0')
508 radix = 8;
509 str++;
511 /* Require at least one hex digit to classify it as hex. */
512 if (*str == 'x' || *str == 'X')
514 if (str[1] == '.' || ISXDIGIT (str[1]))
516 radix = 16;
517 str++;
519 else if (DIGIT_SEP (str[1]))
520 SYNTAX_ERROR_AT (virtual_location,
521 "digit separator after base indicator");
523 else if (*str == 'b' || *str == 'B')
525 if (str[1] == '0' || str[1] == '1')
527 radix = 2;
528 str++;
530 else if (DIGIT_SEP (str[1]))
531 SYNTAX_ERROR_AT (virtual_location,
532 "digit separator after base indicator");
536 /* Now scan for a well-formed integer or float. */
537 for (;;)
539 unsigned int c = *str++;
541 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
543 seen_digit_sep = false;
544 seen_digit = true;
545 c = hex_value (c);
546 if (c > max_digit)
547 max_digit = c;
549 else if (DIGIT_SEP (c))
551 if (seen_digit_sep)
552 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
553 seen_digit_sep = true;
555 else if (c == '.')
557 if (seen_digit_sep || DIGIT_SEP (*str))
558 SYNTAX_ERROR_AT (virtual_location,
559 "digit separator adjacent to decimal point");
560 seen_digit_sep = false;
561 if (float_flag == NOT_FLOAT)
562 float_flag = AFTER_POINT;
563 else
564 SYNTAX_ERROR_AT (virtual_location,
565 "too many decimal points in number");
567 else if ((radix <= 10 && (c == 'e' || c == 'E'))
568 || (radix == 16 && (c == 'p' || c == 'P')))
570 if (seen_digit_sep || DIGIT_SEP (*str))
571 SYNTAX_ERROR_AT (virtual_location,
572 "digit separator adjacent to exponent");
573 float_flag = AFTER_EXPON;
574 break;
576 else
578 /* Start of suffix. */
579 str--;
580 break;
584 if (seen_digit_sep && float_flag != AFTER_EXPON)
585 SYNTAX_ERROR_AT (virtual_location,
586 "digit separator outside digit sequence");
588 /* The suffix may be for decimal fixed-point constants without exponent. */
589 if (radix != 16 && float_flag == NOT_FLOAT)
591 result = interpret_float_suffix (pfile, str, limit - str);
592 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
594 result |= CPP_N_FLOATING;
595 /* We need to restore the radix to 10, if the radix is 8. */
596 if (radix == 8)
597 radix = 10;
599 if (CPP_PEDANTIC (pfile))
600 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
601 "fixed-point constants are a GCC extension");
602 goto syntax_ok;
604 else
605 result = 0;
608 if (float_flag != NOT_FLOAT && radix == 8)
609 radix = 10;
611 if (max_digit >= radix)
613 if (radix == 2)
614 SYNTAX_ERROR2_AT (virtual_location,
615 "invalid digit \"%c\" in binary constant", '0' + max_digit);
616 else
617 SYNTAX_ERROR2_AT (virtual_location,
618 "invalid digit \"%c\" in octal constant", '0' + max_digit);
621 if (float_flag != NOT_FLOAT)
623 if (radix == 2)
625 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
626 "invalid prefix \"0b\" for floating constant");
627 return CPP_N_INVALID;
630 if (radix == 16 && !seen_digit)
631 SYNTAX_ERROR_AT (virtual_location,
632 "no digits in hexadecimal floating constant");
634 if (radix == 16 && CPP_PEDANTIC (pfile)
635 && !CPP_OPTION (pfile, extended_numbers))
637 if (CPP_OPTION (pfile, cplusplus))
638 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
639 "use of C++1z hexadecimal floating constant");
640 else
641 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
642 "use of C99 hexadecimal floating constant");
645 if (float_flag == AFTER_EXPON)
647 if (*str == '+' || *str == '-')
648 str++;
650 /* Exponent is decimal, even if string is a hex float. */
651 if (!ISDIGIT (*str))
653 if (DIGIT_SEP (*str))
654 SYNTAX_ERROR_AT (virtual_location,
655 "digit separator adjacent to exponent");
656 else
657 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
661 seen_digit_sep = DIGIT_SEP (*str);
662 str++;
664 while (ISDIGIT (*str) || DIGIT_SEP (*str));
666 else if (radix == 16)
667 SYNTAX_ERROR_AT (virtual_location,
668 "hexadecimal floating constants require an exponent");
670 if (seen_digit_sep)
671 SYNTAX_ERROR_AT (virtual_location,
672 "digit separator outside digit sequence");
674 result = interpret_float_suffix (pfile, str, limit - str);
675 if (result == 0)
677 if (CPP_OPTION (pfile, user_literals))
679 if (ud_suffix)
680 *ud_suffix = (const char *) str;
681 result = CPP_N_LARGE | CPP_N_USERDEF;
683 else
685 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
686 "invalid suffix \"%.*s\" on floating constant",
687 (int) (limit - str), str);
688 return CPP_N_INVALID;
692 /* Traditional C didn't accept any floating suffixes. */
693 if (limit != str
694 && CPP_WTRADITIONAL (pfile)
695 && ! cpp_sys_macro_p (pfile))
696 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
697 "traditional C rejects the \"%.*s\" suffix",
698 (int) (limit - str), str);
700 /* A suffix for double is a GCC extension via decimal float support.
701 If the suffix also specifies an imaginary value we'll catch that
702 later. */
703 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
704 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
705 "suffix for double constant is a GCC extension");
707 /* Radix must be 10 for decimal floats. */
708 if ((result & CPP_N_DFLOAT) && radix != 10)
710 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
711 "invalid suffix \"%.*s\" with hexadecimal floating constant",
712 (int) (limit - str), str);
713 return CPP_N_INVALID;
716 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
717 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
718 "fixed-point constants are a GCC extension");
720 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
721 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
722 "decimal float constants are a GCC extension");
724 result |= CPP_N_FLOATING;
726 else
728 result = interpret_int_suffix (pfile, str, limit - str);
729 if (result == 0)
731 if (CPP_OPTION (pfile, user_literals))
733 if (ud_suffix)
734 *ud_suffix = (const char *) str;
735 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
737 else
739 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
740 "invalid suffix \"%.*s\" on integer constant",
741 (int) (limit - str), str);
742 return CPP_N_INVALID;
746 /* Traditional C only accepted the 'L' suffix.
747 Suppress warning about 'LL' with -Wno-long-long. */
748 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
750 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
751 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
752 && CPP_OPTION (pfile, cpp_warn_long_long);
754 if (u_or_i || large)
755 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
756 virtual_location, 0,
757 "traditional C rejects the \"%.*s\" suffix",
758 (int) (limit - str), str);
761 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
762 && CPP_OPTION (pfile, cpp_warn_long_long))
764 const char *message = CPP_OPTION (pfile, cplusplus)
765 ? N_("use of C++11 long long integer constant")
766 : N_("use of C99 long long integer constant");
768 if (CPP_OPTION (pfile, c99))
769 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
770 0, message);
771 else
772 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
773 virtual_location, 0, message);
776 result |= CPP_N_INTEGER;
779 syntax_ok:
780 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
781 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
782 "imaginary constants are a GCC extension");
783 if (radix == 2
784 && !CPP_OPTION (pfile, binary_constants)
785 && CPP_PEDANTIC (pfile))
786 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
787 CPP_OPTION (pfile, cplusplus)
788 ? N_("binary constants are a C++14 feature "
789 "or GCC extension")
790 : N_("binary constants are a GCC extension"));
792 if (radix == 10)
793 result |= CPP_N_DECIMAL;
794 else if (radix == 16)
795 result |= CPP_N_HEX;
796 else if (radix == 2)
797 result |= CPP_N_BINARY;
798 else
799 result |= CPP_N_OCTAL;
801 return result;
803 syntax_error:
804 return CPP_N_INVALID;
807 /* cpp_interpret_integer converts an integer constant into a cpp_num,
808 of precision options->precision.
810 We do not provide any interface for decimal->float conversion,
811 because the preprocessor doesn't need it and we don't want to
812 drag in GCC's floating point emulator. */
813 cpp_num
814 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
815 unsigned int type)
817 const uchar *p, *end;
818 cpp_num result;
820 result.low = 0;
821 result.high = 0;
822 result.unsignedp = !!(type & CPP_N_UNSIGNED);
823 result.overflow = false;
825 p = token->val.str.text;
826 end = p + token->val.str.len;
828 /* Common case of a single digit. */
829 if (token->val.str.len == 1)
830 result.low = p[0] - '0';
831 else
833 cpp_num_part max;
834 size_t precision = CPP_OPTION (pfile, precision);
835 unsigned int base = 10, c = 0;
836 bool overflow = false;
838 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
840 base = 8;
841 p++;
843 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
845 base = 16;
846 p += 2;
848 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
850 base = 2;
851 p += 2;
854 /* We can add a digit to numbers strictly less than this without
855 needing the precision and slowness of double integers. */
856 max = ~(cpp_num_part) 0;
857 if (precision < PART_PRECISION)
858 max >>= PART_PRECISION - precision;
859 max = (max - base + 1) / base + 1;
861 for (; p < end; p++)
863 c = *p;
865 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
866 c = hex_value (c);
867 else if (DIGIT_SEP (c))
868 continue;
869 else
870 break;
872 /* Strict inequality for when max is set to zero. */
873 if (result.low < max)
874 result.low = result.low * base + c;
875 else
877 result = append_digit (result, c, base, precision);
878 overflow |= result.overflow;
879 max = 0;
883 if (overflow && !(type & CPP_N_USERDEF))
884 cpp_error (pfile, CPP_DL_PEDWARN,
885 "integer constant is too large for its type");
886 /* If too big to be signed, consider it unsigned. Only warn for
887 decimal numbers. Traditional numbers were always signed (but
888 we still honor an explicit U suffix); but we only have
889 traditional semantics in directives. */
890 else if (!result.unsignedp
891 && !(CPP_OPTION (pfile, traditional)
892 && pfile->state.in_directive)
893 && !num_positive (result, precision))
895 /* This is for constants within the range of uintmax_t but
896 not that of intmax_t. For such decimal constants, a
897 diagnostic is required for C99 as the selected type must
898 be signed and not having a type is a constraint violation
899 (DR#298, TC3), so this must be a pedwarn. For C90,
900 unsigned long is specified to be used for a constant that
901 does not fit in signed long; if uintmax_t has the same
902 range as unsigned long this means only a warning is
903 appropriate here. C90 permits the preprocessor to use a
904 wider range than unsigned long in the compiler, so if
905 uintmax_t is wider than unsigned long no diagnostic is
906 required for such constants in preprocessor #if
907 expressions and the compiler will pedwarn for such
908 constants outside the range of unsigned long that reach
909 the compiler so a diagnostic is not required there
910 either; thus, pedwarn for C99 but use a plain warning for
911 C90. */
912 if (base == 10)
913 cpp_error (pfile, (CPP_OPTION (pfile, c99)
914 ? CPP_DL_PEDWARN
915 : CPP_DL_WARNING),
916 "integer constant is so large that it is unsigned");
917 result.unsignedp = true;
921 return result;
924 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
925 static cpp_num
926 append_digit (cpp_num num, int digit, int base, size_t precision)
928 cpp_num result;
929 unsigned int shift;
930 bool overflow;
931 cpp_num_part add_high, add_low;
933 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
934 need to worry about add_high overflowing. */
935 switch (base)
937 case 2:
938 shift = 1;
939 break;
941 case 16:
942 shift = 4;
943 break;
945 default:
946 shift = 3;
948 overflow = !!(num.high >> (PART_PRECISION - shift));
949 result.high = num.high << shift;
950 result.low = num.low << shift;
951 result.high |= num.low >> (PART_PRECISION - shift);
952 result.unsignedp = num.unsignedp;
954 if (base == 10)
956 add_low = num.low << 1;
957 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
959 else
960 add_high = add_low = 0;
962 if (add_low + digit < add_low)
963 add_high++;
964 add_low += digit;
966 if (result.low + add_low < result.low)
967 add_high++;
968 if (result.high + add_high < result.high)
969 overflow = true;
971 result.low += add_low;
972 result.high += add_high;
973 result.overflow = overflow;
975 /* The above code catches overflow of a cpp_num type. This catches
976 overflow of the (possibly shorter) target precision. */
977 num.low = result.low;
978 num.high = result.high;
979 result = num_trim (result, precision);
980 if (!num_eq (result, num))
981 result.overflow = true;
983 return result;
986 /* Handle meeting "defined" in a preprocessor expression. */
987 static cpp_num
988 parse_defined (cpp_reader *pfile)
990 cpp_num result;
991 int paren = 0;
992 cpp_hashnode *node = 0;
993 const cpp_token *token;
994 cpp_context *initial_context = pfile->context;
996 /* Don't expand macros. */
997 pfile->state.prevent_expansion++;
999 token = cpp_get_token (pfile);
1000 if (token->type == CPP_OPEN_PAREN)
1002 paren = 1;
1003 token = cpp_get_token (pfile);
1006 if (token->type == CPP_NAME)
1008 node = token->val.node.node;
1009 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1011 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1012 node = 0;
1015 else
1017 cpp_error (pfile, CPP_DL_ERROR,
1018 "operator \"defined\" requires an identifier");
1019 if (token->flags & NAMED_OP)
1021 cpp_token op;
1023 op.flags = 0;
1024 op.type = token->type;
1025 cpp_error (pfile, CPP_DL_ERROR,
1026 "(\"%s\" is an alternative token for \"%s\" in C++)",
1027 cpp_token_as_text (pfile, token),
1028 cpp_token_as_text (pfile, &op));
1032 if (node)
1034 if ((pfile->context != initial_context
1035 || initial_context != &pfile->base_context)
1036 && CPP_OPTION (pfile, warn_expansion_to_defined))
1037 cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1038 "this use of \"defined\" may not be portable");
1040 _cpp_mark_macro_used (node);
1041 if (!(node->flags & NODE_USED))
1043 node->flags |= NODE_USED;
1044 if (node->type == NT_MACRO)
1046 if ((node->flags & NODE_BUILTIN)
1047 && pfile->cb.user_builtin_macro)
1048 pfile->cb.user_builtin_macro (pfile, node);
1049 if (pfile->cb.used_define)
1050 pfile->cb.used_define (pfile, pfile->directive_line, node);
1052 else
1054 if (pfile->cb.used_undef)
1055 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1059 /* A possible controlling macro of the form #if !defined ().
1060 _cpp_parse_expr checks there was no other junk on the line. */
1061 pfile->mi_ind_cmacro = node;
1064 pfile->state.prevent_expansion--;
1066 /* Do not treat conditional macros as being defined. This is due to the
1067 powerpc and spu ports using conditional macros for 'vector', 'bool', and
1068 'pixel' to act as conditional keywords. This messes up tests like #ifndef
1069 bool. */
1070 result.unsignedp = false;
1071 result.high = 0;
1072 result.overflow = false;
1073 result.low = (node && node->type == NT_MACRO
1074 && (node->flags & NODE_CONDITIONAL) == 0);
1075 return result;
1078 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1079 number or character constant, or the result of the "defined" or "#"
1080 operators). */
1081 static cpp_num
1082 eval_token (cpp_reader *pfile, const cpp_token *token,
1083 source_location virtual_location)
1085 cpp_num result;
1086 unsigned int temp;
1087 int unsignedp = 0;
1089 result.unsignedp = false;
1090 result.overflow = false;
1092 switch (token->type)
1094 case CPP_NUMBER:
1095 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1096 if (temp & CPP_N_USERDEF)
1097 cpp_error (pfile, CPP_DL_ERROR,
1098 "user-defined literal in preprocessor expression");
1099 switch (temp & CPP_N_CATEGORY)
1101 case CPP_N_FLOATING:
1102 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1103 "floating constant in preprocessor expression");
1104 break;
1105 case CPP_N_INTEGER:
1106 if (!(temp & CPP_N_IMAGINARY))
1107 return cpp_interpret_integer (pfile, token, temp);
1108 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1109 "imaginary number in preprocessor expression");
1110 break;
1112 case CPP_N_INVALID:
1113 /* Error already issued. */
1114 break;
1116 result.high = result.low = 0;
1117 break;
1119 case CPP_WCHAR:
1120 case CPP_CHAR:
1121 case CPP_CHAR16:
1122 case CPP_CHAR32:
1123 case CPP_UTF8CHAR:
1125 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1126 &temp, &unsignedp);
1128 result.high = 0;
1129 result.low = cc;
1130 /* Sign-extend the result if necessary. */
1131 if (!unsignedp && (cppchar_signed_t) cc < 0)
1133 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1134 result.low |= ~(~(cpp_num_part) 0
1135 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1136 result.high = ~(cpp_num_part) 0;
1137 result = num_trim (result, CPP_OPTION (pfile, precision));
1140 break;
1142 case CPP_NAME:
1143 if (token->val.node.node == pfile->spec_nodes.n_defined)
1144 return parse_defined (pfile);
1145 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1146 return parse_has_include (pfile, IT_INCLUDE);
1147 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1148 return parse_has_include (pfile, IT_INCLUDE_NEXT);
1149 else if (CPP_OPTION (pfile, cplusplus)
1150 && (token->val.node.node == pfile->spec_nodes.n_true
1151 || token->val.node.node == pfile->spec_nodes.n_false))
1153 result.high = 0;
1154 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1156 else
1158 result.high = 0;
1159 result.low = 0;
1160 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1161 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1162 "\"%s\" is not defined, evaluates to 0",
1163 NODE_NAME (token->val.node.node));
1165 break;
1167 case CPP_HASH:
1168 if (!pfile->state.skipping)
1170 /* A pedantic warning takes precedence over a deprecated
1171 warning here. */
1172 if (CPP_PEDANTIC (pfile))
1173 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1174 virtual_location, 0,
1175 "assertions are a GCC extension");
1176 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1177 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1178 "assertions are a deprecated extension");
1180 _cpp_test_assertion (pfile, &temp);
1181 result.high = 0;
1182 result.low = temp;
1183 break;
1185 default:
1186 abort ();
1189 result.unsignedp = !!unsignedp;
1190 return result;
1193 /* Operator precedence and flags table.
1195 After an operator is returned from the lexer, if it has priority less
1196 than the operator on the top of the stack, we reduce the stack by one
1197 operator and repeat the test. Since equal priorities do not reduce,
1198 this is naturally right-associative.
1200 We handle left-associative operators by decrementing the priority of
1201 just-lexed operators by one, but retaining the priority of operators
1202 already on the stack.
1204 The remaining cases are '(' and ')'. We handle '(' by skipping the
1205 reduction phase completely. ')' is given lower priority than
1206 everything else, including '(', effectively forcing a reduction of the
1207 parenthesized expression. If there is a matching '(', the routine
1208 reduce() exits immediately. If the normal exit route sees a ')', then
1209 there cannot have been a matching '(' and an error message is output.
1211 The parser assumes all shifted operators require a left operand unless
1212 the flag NO_L_OPERAND is set. These semantics are automatic; any
1213 extra semantics need to be handled with operator-specific code. */
1215 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1216 operand changes because of integer promotions. */
1217 #define NO_L_OPERAND (1 << 0)
1218 #define LEFT_ASSOC (1 << 1)
1219 #define CHECK_PROMOTION (1 << 2)
1221 /* Operator to priority map. Must be in the same order as the first
1222 N entries of enum cpp_ttype. */
1223 static const struct cpp_operator
1225 uchar prio;
1226 uchar flags;
1227 } optab[] =
1229 /* EQ */ {0, 0}, /* Shouldn't happen. */
1230 /* NOT */ {16, NO_L_OPERAND},
1231 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1232 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1233 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1234 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1235 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1236 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1237 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1238 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1239 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1240 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1241 /* RSHIFT */ {13, LEFT_ASSOC},
1242 /* LSHIFT */ {13, LEFT_ASSOC},
1244 /* COMPL */ {16, NO_L_OPERAND},
1245 /* AND_AND */ {6, LEFT_ASSOC},
1246 /* OR_OR */ {5, LEFT_ASSOC},
1247 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1248 However, there are some special cases for these in reduce(). */
1249 /* QUERY */ {4, 0},
1250 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1251 /* COMMA */ {4, LEFT_ASSOC},
1252 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1253 /* CLOSE_PAREN */ {0, 0},
1254 /* EOF */ {0, 0},
1255 /* EQ_EQ */ {11, LEFT_ASSOC},
1256 /* NOT_EQ */ {11, LEFT_ASSOC},
1257 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1258 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1259 /* UPLUS */ {16, NO_L_OPERAND},
1260 /* UMINUS */ {16, NO_L_OPERAND}
1263 /* Parse and evaluate a C expression, reading from PFILE.
1264 Returns the truth value of the expression.
1266 The implementation is an operator precedence parser, i.e. a
1267 bottom-up parser, using a stack for not-yet-reduced tokens.
1269 The stack base is op_stack, and the current stack pointer is 'top'.
1270 There is a stack element for each operator (only), and the most
1271 recently pushed operator is 'top->op'. An operand (value) is
1272 stored in the 'value' field of the stack element of the operator
1273 that precedes it. */
1274 bool
1275 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1277 struct op *top = pfile->op_stack;
1278 unsigned int lex_count;
1279 bool saw_leading_not, want_value = true;
1280 source_location virtual_location = 0;
1282 pfile->state.skip_eval = 0;
1284 /* Set up detection of #if ! defined(). */
1285 pfile->mi_ind_cmacro = 0;
1286 saw_leading_not = false;
1287 lex_count = 0;
1289 /* Lowest priority operator prevents further reductions. */
1290 top->op = CPP_EOF;
1292 for (;;)
1294 struct op op;
1296 lex_count++;
1297 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1298 op.op = op.token->type;
1299 op.loc = virtual_location;
1301 switch (op.op)
1303 /* These tokens convert into values. */
1304 case CPP_NUMBER:
1305 case CPP_CHAR:
1306 case CPP_WCHAR:
1307 case CPP_CHAR16:
1308 case CPP_CHAR32:
1309 case CPP_UTF8CHAR:
1310 case CPP_NAME:
1311 case CPP_HASH:
1312 if (!want_value)
1313 SYNTAX_ERROR2_AT (op.loc,
1314 "missing binary operator before token \"%s\"",
1315 cpp_token_as_text (pfile, op.token));
1316 want_value = false;
1317 top->value = eval_token (pfile, op.token, op.loc);
1318 continue;
1320 case CPP_NOT:
1321 saw_leading_not = lex_count == 1;
1322 break;
1323 case CPP_PLUS:
1324 if (want_value)
1325 op.op = CPP_UPLUS;
1326 break;
1327 case CPP_MINUS:
1328 if (want_value)
1329 op.op = CPP_UMINUS;
1330 break;
1332 default:
1333 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1334 SYNTAX_ERROR2_AT (op.loc,
1335 "token \"%s\" is not valid in preprocessor expressions",
1336 cpp_token_as_text (pfile, op.token));
1337 break;
1340 /* Check we have a value or operator as appropriate. */
1341 if (optab[op.op].flags & NO_L_OPERAND)
1343 if (!want_value)
1344 SYNTAX_ERROR2_AT (op.loc,
1345 "missing binary operator before token \"%s\"",
1346 cpp_token_as_text (pfile, op.token));
1348 else if (want_value)
1350 /* We want a number (or expression) and haven't got one.
1351 Try to emit a specific diagnostic. */
1352 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1353 SYNTAX_ERROR_AT (op.loc,
1354 "missing expression between '(' and ')'");
1356 if (op.op == CPP_EOF && top->op == CPP_EOF)
1357 SYNTAX_ERROR2_AT (op.loc,
1358 "%s with no expression", is_if ? "#if" : "#elif");
1360 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1361 SYNTAX_ERROR2_AT (op.loc,
1362 "operator '%s' has no right operand",
1363 cpp_token_as_text (pfile, top->token));
1364 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1365 /* Complain about missing paren during reduction. */;
1366 else
1367 SYNTAX_ERROR2_AT (op.loc,
1368 "operator '%s' has no left operand",
1369 cpp_token_as_text (pfile, op.token));
1372 top = reduce (pfile, top, op.op);
1373 if (!top)
1374 goto syntax_error;
1376 if (op.op == CPP_EOF)
1377 break;
1379 switch (op.op)
1381 case CPP_CLOSE_PAREN:
1382 continue;
1383 case CPP_OR_OR:
1384 if (!num_zerop (top->value))
1385 pfile->state.skip_eval++;
1386 break;
1387 case CPP_AND_AND:
1388 case CPP_QUERY:
1389 if (num_zerop (top->value))
1390 pfile->state.skip_eval++;
1391 break;
1392 case CPP_COLON:
1393 if (top->op != CPP_QUERY)
1394 SYNTAX_ERROR_AT (op.loc,
1395 " ':' without preceding '?'");
1396 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1397 pfile->state.skip_eval++;
1398 else
1399 pfile->state.skip_eval--;
1400 default:
1401 break;
1404 want_value = true;
1406 /* Check for and handle stack overflow. */
1407 if (++top == pfile->op_limit)
1408 top = _cpp_expand_op_stack (pfile);
1410 top->op = op.op;
1411 top->token = op.token;
1412 top->loc = op.loc;
1415 /* The controlling macro expression is only valid if we called lex 3
1416 times: <!> <defined expression> and <EOF>. push_conditional ()
1417 checks that we are at top-of-file. */
1418 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1419 pfile->mi_ind_cmacro = 0;
1421 if (top != pfile->op_stack)
1423 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1424 "unbalanced stack in %s",
1425 is_if ? "#if" : "#elif");
1426 syntax_error:
1427 return false; /* Return false on syntax error. */
1430 return !num_zerop (top->value);
1433 /* Reduce the operator / value stack if possible, in preparation for
1434 pushing operator OP. Returns NULL on error, otherwise the top of
1435 the stack. */
1436 static struct op *
1437 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1439 unsigned int prio;
1441 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1443 bad_op:
1444 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1445 return 0;
1448 if (op == CPP_OPEN_PAREN)
1449 return top;
1451 /* Decrement the priority of left-associative operators to force a
1452 reduction with operators of otherwise equal priority. */
1453 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1454 while (prio < optab[top->op].prio)
1456 if (CPP_OPTION (pfile, warn_num_sign_change)
1457 && optab[top->op].flags & CHECK_PROMOTION)
1458 check_promotion (pfile, top);
1460 switch (top->op)
1462 case CPP_UPLUS:
1463 case CPP_UMINUS:
1464 case CPP_NOT:
1465 case CPP_COMPL:
1466 top[-1].value = num_unary_op (pfile, top->value, top->op);
1467 top[-1].loc = top->loc;
1468 break;
1470 case CPP_PLUS:
1471 case CPP_MINUS:
1472 case CPP_RSHIFT:
1473 case CPP_LSHIFT:
1474 case CPP_COMMA:
1475 top[-1].value = num_binary_op (pfile, top[-1].value,
1476 top->value, top->op);
1477 top[-1].loc = top->loc;
1478 break;
1480 case CPP_GREATER:
1481 case CPP_LESS:
1482 case CPP_GREATER_EQ:
1483 case CPP_LESS_EQ:
1484 top[-1].value
1485 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1486 top[-1].loc = top->loc;
1487 break;
1489 case CPP_EQ_EQ:
1490 case CPP_NOT_EQ:
1491 top[-1].value
1492 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1493 top[-1].loc = top->loc;
1494 break;
1496 case CPP_AND:
1497 case CPP_OR:
1498 case CPP_XOR:
1499 top[-1].value
1500 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1501 top[-1].loc = top->loc;
1502 break;
1504 case CPP_MULT:
1505 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1506 top[-1].loc = top->loc;
1507 break;
1509 case CPP_DIV:
1510 case CPP_MOD:
1511 top[-1].value = num_div_op (pfile, top[-1].value,
1512 top->value, top->op, top->loc);
1513 top[-1].loc = top->loc;
1514 break;
1516 case CPP_OR_OR:
1517 top--;
1518 if (!num_zerop (top->value))
1519 pfile->state.skip_eval--;
1520 top->value.low = (!num_zerop (top->value)
1521 || !num_zerop (top[1].value));
1522 top->value.high = 0;
1523 top->value.unsignedp = false;
1524 top->value.overflow = false;
1525 top->loc = top[1].loc;
1526 continue;
1528 case CPP_AND_AND:
1529 top--;
1530 if (num_zerop (top->value))
1531 pfile->state.skip_eval--;
1532 top->value.low = (!num_zerop (top->value)
1533 && !num_zerop (top[1].value));
1534 top->value.high = 0;
1535 top->value.unsignedp = false;
1536 top->value.overflow = false;
1537 top->loc = top[1].loc;
1538 continue;
1540 case CPP_OPEN_PAREN:
1541 if (op != CPP_CLOSE_PAREN)
1543 cpp_error_with_line (pfile, CPP_DL_ERROR,
1544 top->token->src_loc,
1545 0, "missing ')' in expression");
1546 return 0;
1548 top--;
1549 top->value = top[1].value;
1550 top->loc = top[1].loc;
1551 return top;
1553 case CPP_COLON:
1554 top -= 2;
1555 if (!num_zerop (top->value))
1557 pfile->state.skip_eval--;
1558 top->value = top[1].value;
1559 top->loc = top[1].loc;
1561 else
1563 top->value = top[2].value;
1564 top->loc = top[2].loc;
1566 top->value.unsignedp = (top[1].value.unsignedp
1567 || top[2].value.unsignedp);
1568 continue;
1570 case CPP_QUERY:
1571 /* COMMA and COLON should not reduce a QUERY operator. */
1572 if (op == CPP_COMMA || op == CPP_COLON)
1573 return top;
1574 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1575 return 0;
1577 default:
1578 goto bad_op;
1581 top--;
1582 if (top->value.overflow && !pfile->state.skip_eval)
1583 cpp_error (pfile, CPP_DL_PEDWARN,
1584 "integer overflow in preprocessor expression");
1587 if (op == CPP_CLOSE_PAREN)
1589 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1590 return 0;
1593 return top;
1596 /* Returns the position of the old top of stack after expansion. */
1597 struct op *
1598 _cpp_expand_op_stack (cpp_reader *pfile)
1600 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1601 size_t new_size = old_size * 2 + 20;
1603 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1604 pfile->op_limit = pfile->op_stack + new_size;
1606 return pfile->op_stack + old_size;
1609 /* Emits a warning if the effective sign of either operand of OP
1610 changes because of integer promotions. */
1611 static void
1612 check_promotion (cpp_reader *pfile, const struct op *op)
1614 if (op->value.unsignedp == op[-1].value.unsignedp)
1615 return;
1617 if (op->value.unsignedp)
1619 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1620 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1621 "the left operand of \"%s\" changes sign when promoted",
1622 cpp_token_as_text (pfile, op->token));
1624 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1625 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1626 "the right operand of \"%s\" changes sign when promoted",
1627 cpp_token_as_text (pfile, op->token));
1630 /* Clears the unused high order bits of the number pointed to by PNUM. */
1631 static cpp_num
1632 num_trim (cpp_num num, size_t precision)
1634 if (precision > PART_PRECISION)
1636 precision -= PART_PRECISION;
1637 if (precision < PART_PRECISION)
1638 num.high &= ((cpp_num_part) 1 << precision) - 1;
1640 else
1642 if (precision < PART_PRECISION)
1643 num.low &= ((cpp_num_part) 1 << precision) - 1;
1644 num.high = 0;
1647 return num;
1650 /* True iff A (presumed signed) >= 0. */
1651 static bool
1652 num_positive (cpp_num num, size_t precision)
1654 if (precision > PART_PRECISION)
1656 precision -= PART_PRECISION;
1657 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1660 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1663 /* Sign extend a number, with PRECISION significant bits and all
1664 others assumed clear, to fill out a cpp_num structure. */
1665 cpp_num
1666 cpp_num_sign_extend (cpp_num num, size_t precision)
1668 if (!num.unsignedp)
1670 if (precision > PART_PRECISION)
1672 precision -= PART_PRECISION;
1673 if (precision < PART_PRECISION
1674 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1675 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1677 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1679 if (precision < PART_PRECISION)
1680 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1681 num.high = ~(cpp_num_part) 0;
1685 return num;
1688 /* Returns the negative of NUM. */
1689 static cpp_num
1690 num_negate (cpp_num num, size_t precision)
1692 cpp_num copy;
1694 copy = num;
1695 num.high = ~num.high;
1696 num.low = ~num.low;
1697 if (++num.low == 0)
1698 num.high++;
1699 num = num_trim (num, precision);
1700 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1702 return num;
1705 /* Returns true if A >= B. */
1706 static bool
1707 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1709 bool unsignedp;
1711 unsignedp = pa.unsignedp || pb.unsignedp;
1713 if (!unsignedp)
1715 /* Both numbers have signed type. If they are of different
1716 sign, the answer is the sign of A. */
1717 unsignedp = num_positive (pa, precision);
1719 if (unsignedp != num_positive (pb, precision))
1720 return unsignedp;
1722 /* Otherwise we can do an unsigned comparison. */
1725 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1728 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1729 static cpp_num
1730 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1731 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1733 lhs.overflow = false;
1734 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1736 /* As excess precision is zeroed, there is no need to num_trim () as
1737 these operations cannot introduce a set bit there. */
1738 if (op == CPP_AND)
1740 lhs.low &= rhs.low;
1741 lhs.high &= rhs.high;
1743 else if (op == CPP_OR)
1745 lhs.low |= rhs.low;
1746 lhs.high |= rhs.high;
1748 else
1750 lhs.low ^= rhs.low;
1751 lhs.high ^= rhs.high;
1754 return lhs;
1757 /* Returns LHS OP RHS, where OP is an inequality. */
1758 static cpp_num
1759 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1760 enum cpp_ttype op)
1762 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1764 if (op == CPP_GREATER_EQ)
1765 lhs.low = gte;
1766 else if (op == CPP_LESS)
1767 lhs.low = !gte;
1768 else if (op == CPP_GREATER)
1769 lhs.low = gte && !num_eq (lhs, rhs);
1770 else /* CPP_LESS_EQ. */
1771 lhs.low = !gte || num_eq (lhs, rhs);
1773 lhs.high = 0;
1774 lhs.overflow = false;
1775 lhs.unsignedp = false;
1776 return lhs;
1779 /* Returns LHS OP RHS, where OP is == or !=. */
1780 static cpp_num
1781 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1782 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1784 /* Work around a 3.0.4 bug; see PR 6950. */
1785 bool eq = num_eq (lhs, rhs);
1786 if (op == CPP_NOT_EQ)
1787 eq = !eq;
1788 lhs.low = eq;
1789 lhs.high = 0;
1790 lhs.overflow = false;
1791 lhs.unsignedp = false;
1792 return lhs;
1795 /* Shift NUM, of width PRECISION, right by N bits. */
1796 static cpp_num
1797 num_rshift (cpp_num num, size_t precision, size_t n)
1799 cpp_num_part sign_mask;
1800 bool x = num_positive (num, precision);
1802 if (num.unsignedp || x)
1803 sign_mask = 0;
1804 else
1805 sign_mask = ~(cpp_num_part) 0;
1807 if (n >= precision)
1808 num.high = num.low = sign_mask;
1809 else
1811 /* Sign-extend. */
1812 if (precision < PART_PRECISION)
1813 num.high = sign_mask, num.low |= sign_mask << precision;
1814 else if (precision < 2 * PART_PRECISION)
1815 num.high |= sign_mask << (precision - PART_PRECISION);
1817 if (n >= PART_PRECISION)
1819 n -= PART_PRECISION;
1820 num.low = num.high;
1821 num.high = sign_mask;
1824 if (n)
1826 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1827 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1831 num = num_trim (num, precision);
1832 num.overflow = false;
1833 return num;
1836 /* Shift NUM, of width PRECISION, left by N bits. */
1837 static cpp_num
1838 num_lshift (cpp_num num, size_t precision, size_t n)
1840 if (n >= precision)
1842 num.overflow = !num.unsignedp && !num_zerop (num);
1843 num.high = num.low = 0;
1845 else
1847 cpp_num orig, maybe_orig;
1848 size_t m = n;
1850 orig = num;
1851 if (m >= PART_PRECISION)
1853 m -= PART_PRECISION;
1854 num.high = num.low;
1855 num.low = 0;
1857 if (m)
1859 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1860 num.low <<= m;
1862 num = num_trim (num, precision);
1864 if (num.unsignedp)
1865 num.overflow = false;
1866 else
1868 maybe_orig = num_rshift (num, precision, n);
1869 num.overflow = !num_eq (orig, maybe_orig);
1873 return num;
1876 /* The four unary operators: +, -, ! and ~. */
1877 static cpp_num
1878 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1880 switch (op)
1882 case CPP_UPLUS:
1883 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1884 cpp_warning (pfile, CPP_W_TRADITIONAL,
1885 "traditional C rejects the unary plus operator");
1886 num.overflow = false;
1887 break;
1889 case CPP_UMINUS:
1890 num = num_negate (num, CPP_OPTION (pfile, precision));
1891 break;
1893 case CPP_COMPL:
1894 num.high = ~num.high;
1895 num.low = ~num.low;
1896 num = num_trim (num, CPP_OPTION (pfile, precision));
1897 num.overflow = false;
1898 break;
1900 default: /* case CPP_NOT: */
1901 num.low = num_zerop (num);
1902 num.high = 0;
1903 num.overflow = false;
1904 num.unsignedp = false;
1905 break;
1908 return num;
1911 /* The various binary operators. */
1912 static cpp_num
1913 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1915 cpp_num result;
1916 size_t precision = CPP_OPTION (pfile, precision);
1917 size_t n;
1919 switch (op)
1921 /* Shifts. */
1922 case CPP_LSHIFT:
1923 case CPP_RSHIFT:
1924 if (!rhs.unsignedp && !num_positive (rhs, precision))
1926 /* A negative shift is a positive shift the other way. */
1927 if (op == CPP_LSHIFT)
1928 op = CPP_RSHIFT;
1929 else
1930 op = CPP_LSHIFT;
1931 rhs = num_negate (rhs, precision);
1933 if (rhs.high)
1934 n = ~0; /* Maximal. */
1935 else
1936 n = rhs.low;
1937 if (op == CPP_LSHIFT)
1938 lhs = num_lshift (lhs, precision, n);
1939 else
1940 lhs = num_rshift (lhs, precision, n);
1941 break;
1943 /* Arithmetic. */
1944 case CPP_MINUS:
1945 result.low = lhs.low - rhs.low;
1946 result.high = lhs.high - rhs.high;
1947 if (result.low > lhs.low)
1948 result.high--;
1949 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1950 result.overflow = false;
1952 result = num_trim (result, precision);
1953 if (!result.unsignedp)
1955 bool lhsp = num_positive (lhs, precision);
1956 result.overflow = (lhsp != num_positive (rhs, precision)
1957 && lhsp != num_positive (result, precision));
1959 return result;
1961 case CPP_PLUS:
1962 result.low = lhs.low + rhs.low;
1963 result.high = lhs.high + rhs.high;
1964 if (result.low < lhs.low)
1965 result.high++;
1966 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1967 result.overflow = false;
1969 result = num_trim (result, precision);
1970 if (!result.unsignedp)
1972 bool lhsp = num_positive (lhs, precision);
1973 result.overflow = (lhsp == num_positive (rhs, precision)
1974 && lhsp != num_positive (result, precision));
1976 return result;
1978 /* Comma. */
1979 default: /* case CPP_COMMA: */
1980 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1981 || !pfile->state.skip_eval))
1982 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1983 "comma operator in operand of #if");
1984 lhs = rhs;
1985 break;
1988 return lhs;
1991 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1992 cannot overflow. */
1993 static cpp_num
1994 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1996 cpp_num result;
1997 cpp_num_part middle[2], temp;
1999 result.low = LOW_PART (lhs) * LOW_PART (rhs);
2000 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2002 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2003 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2005 temp = result.low;
2006 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2007 if (result.low < temp)
2008 result.high++;
2010 temp = result.low;
2011 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2012 if (result.low < temp)
2013 result.high++;
2015 result.high += HIGH_PART (middle[0]);
2016 result.high += HIGH_PART (middle[1]);
2017 result.unsignedp = true;
2018 result.overflow = false;
2020 return result;
2023 /* Multiply two preprocessing numbers. */
2024 static cpp_num
2025 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2027 cpp_num result, temp;
2028 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2029 bool overflow, negate = false;
2030 size_t precision = CPP_OPTION (pfile, precision);
2032 /* Prepare for unsigned multiplication. */
2033 if (!unsignedp)
2035 if (!num_positive (lhs, precision))
2036 negate = !negate, lhs = num_negate (lhs, precision);
2037 if (!num_positive (rhs, precision))
2038 negate = !negate, rhs = num_negate (rhs, precision);
2041 overflow = lhs.high && rhs.high;
2042 result = num_part_mul (lhs.low, rhs.low);
2044 temp = num_part_mul (lhs.high, rhs.low);
2045 result.high += temp.low;
2046 if (temp.high)
2047 overflow = true;
2049 temp = num_part_mul (lhs.low, rhs.high);
2050 result.high += temp.low;
2051 if (temp.high)
2052 overflow = true;
2054 temp.low = result.low, temp.high = result.high;
2055 result = num_trim (result, precision);
2056 if (!num_eq (result, temp))
2057 overflow = true;
2059 if (negate)
2060 result = num_negate (result, precision);
2062 if (unsignedp)
2063 result.overflow = false;
2064 else
2065 result.overflow = overflow || (num_positive (result, precision) ^ !negate
2066 && !num_zerop (result));
2067 result.unsignedp = unsignedp;
2069 return result;
2072 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2073 or the remainder depending upon OP. LOCATION is the source location
2074 of this operator (for diagnostics). */
2076 static cpp_num
2077 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2078 source_location location)
2080 cpp_num result, sub;
2081 cpp_num_part mask;
2082 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2083 bool negate = false, lhs_neg = false;
2084 size_t i, precision = CPP_OPTION (pfile, precision);
2086 /* Prepare for unsigned division. */
2087 if (!unsignedp)
2089 if (!num_positive (lhs, precision))
2090 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2091 if (!num_positive (rhs, precision))
2092 negate = !negate, rhs = num_negate (rhs, precision);
2095 /* Find the high bit. */
2096 if (rhs.high)
2098 i = precision - 1;
2099 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2100 for (; ; i--, mask >>= 1)
2101 if (rhs.high & mask)
2102 break;
2104 else if (rhs.low)
2106 if (precision > PART_PRECISION)
2107 i = precision - PART_PRECISION - 1;
2108 else
2109 i = precision - 1;
2110 mask = (cpp_num_part) 1 << i;
2111 for (; ; i--, mask >>= 1)
2112 if (rhs.low & mask)
2113 break;
2115 else
2117 if (!pfile->state.skip_eval)
2118 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2119 "division by zero in #if");
2120 return lhs;
2123 /* First nonzero bit of RHS is bit I. Do naive division by
2124 shifting the RHS fully left, and subtracting from LHS if LHS is
2125 at least as big, and then repeating but with one less shift.
2126 This is not very efficient, but is easy to understand. */
2128 rhs.unsignedp = true;
2129 lhs.unsignedp = true;
2130 i = precision - i - 1;
2131 sub = num_lshift (rhs, precision, i);
2133 result.high = result.low = 0;
2134 for (;;)
2136 if (num_greater_eq (lhs, sub, precision))
2138 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2139 if (i >= PART_PRECISION)
2140 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2141 else
2142 result.low |= (cpp_num_part) 1 << i;
2144 if (i-- == 0)
2145 break;
2146 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2147 sub.high >>= 1;
2150 /* We divide so that the remainder has the sign of the LHS. */
2151 if (op == CPP_DIV)
2153 result.unsignedp = unsignedp;
2154 result.overflow = false;
2155 if (!unsignedp)
2157 if (negate)
2158 result = num_negate (result, precision);
2159 result.overflow = (num_positive (result, precision) ^ !negate
2160 && !num_zerop (result));
2163 return result;
2166 /* CPP_MOD. */
2167 lhs.unsignedp = unsignedp;
2168 lhs.overflow = false;
2169 if (lhs_neg)
2170 lhs = num_negate (lhs, precision);
2172 return lhs;
2175 /* Handle meeting "__has_include__" in a preprocessor expression. */
2176 static cpp_num
2177 parse_has_include (cpp_reader *pfile, enum include_type type)
2179 cpp_num result;
2180 bool paren = false;
2181 cpp_hashnode *node = 0;
2182 const cpp_token *token;
2183 bool bracket = false;
2184 char *fname = 0;
2186 result.unsignedp = false;
2187 result.high = 0;
2188 result.overflow = false;
2189 result.low = 0;
2191 pfile->state.in__has_include__++;
2193 token = cpp_get_token (pfile);
2194 if (token->type == CPP_OPEN_PAREN)
2196 paren = true;
2197 token = cpp_get_token (pfile);
2200 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2202 if (token->type == CPP_HEADER_NAME)
2203 bracket = true;
2204 fname = XNEWVEC (char, token->val.str.len - 1);
2205 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2206 fname[token->val.str.len - 2] = '\0';
2207 node = token->val.node.node;
2209 else if (token->type == CPP_LESS)
2211 bracket = true;
2212 fname = _cpp_bracket_include (pfile);
2214 else
2215 cpp_error (pfile, CPP_DL_ERROR,
2216 "operator \"__has_include__\" requires a header string");
2218 if (fname)
2220 int angle_brackets = (bracket ? 1 : 0);
2222 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2223 result.low = 1;
2224 else
2225 result.low = 0;
2227 XDELETEVEC (fname);
2230 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2231 cpp_error (pfile, CPP_DL_ERROR,
2232 "missing ')' after \"__has_include__\"");
2234 /* A possible controlling macro of the form #if !__has_include__ ().
2235 _cpp_parse_expr checks there was no other junk on the line. */
2236 if (node)
2237 pfile->mi_ind_cmacro = node;
2239 pfile->state.in__has_include__--;
2241 return result;