Enable push/pop for latest x86 CPUs
[official-gcc.git] / libcpp / expr.c
blobdf8d96553fde3a1562f02062fd42593340581f37
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009, 2010, 2011 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "cpplib.h"
23 #include "internal.h"
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30 struct op
32 const cpp_token *token; /* The token forming op (for diagnostics). */
33 cpp_num value; /* The value logically "right" of op. */
34 source_location loc; /* The location of this value. */
35 enum cpp_ttype op;
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num, size_t);
42 static bool num_greater_eq (cpp_num, cpp_num, size_t);
43 static cpp_num num_trim (cpp_num, size_t);
44 static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
46 static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47 static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48 static cpp_num num_negate (cpp_num, size_t);
49 static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50 static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
56 source_location);
57 static cpp_num num_lshift (cpp_num, size_t, size_t);
58 static cpp_num num_rshift (cpp_num, size_t, size_t);
60 static cpp_num append_digit (cpp_num, int, int, size_t);
61 static cpp_num parse_defined (cpp_reader *);
62 static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
63 static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64 static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
65 static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
66 static void check_promotion (cpp_reader *, const struct op *);
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
79 #define SYNTAX_ERROR_AT(loc, msgid) \
80 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
81 while(0)
82 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
83 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
84 while(0)
86 /* Subroutine of cpp_classify_number. S points to a float suffix of
87 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
88 flag vector describing the suffix. */
89 static unsigned int
90 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
92 size_t flags;
93 size_t f, d, l, w, q, i;
95 flags = 0;
96 f = d = l = w = q = i = 0;
98 /* Process decimal float suffixes, which are two letters starting
99 with d or D. Order and case are significant. */
100 if (len == 2 && (*s == 'd' || *s == 'D'))
102 bool uppercase = (*s == 'D');
103 switch (s[1])
105 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
106 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
107 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
108 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
109 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
110 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
111 default:
112 /* Additional two-character suffixes beginning with D are not
113 for decimal float constants. */
114 break;
118 if (CPP_OPTION (pfile, ext_numeric_literals))
120 /* Recognize a fixed-point suffix. */
121 if (len != 0)
122 switch (s[len-1])
124 case 'k': case 'K': flags = CPP_N_ACCUM; break;
125 case 'r': case 'R': flags = CPP_N_FRACT; break;
126 default: break;
129 /* Continue processing a fixed-point suffix. The suffix is case
130 insensitive except for ll or LL. Order is significant. */
131 if (flags)
133 if (len == 1)
134 return flags;
135 len--;
137 if (*s == 'u' || *s == 'U')
139 flags |= CPP_N_UNSIGNED;
140 if (len == 1)
141 return flags;
142 len--;
143 s++;
146 switch (*s)
148 case 'h': case 'H':
149 if (len == 1)
150 return flags |= CPP_N_SMALL;
151 break;
152 case 'l':
153 if (len == 1)
154 return flags |= CPP_N_MEDIUM;
155 if (len == 2 && s[1] == 'l')
156 return flags |= CPP_N_LARGE;
157 break;
158 case 'L':
159 if (len == 1)
160 return flags |= CPP_N_MEDIUM;
161 if (len == 2 && s[1] == 'L')
162 return flags |= CPP_N_LARGE;
163 break;
164 default:
165 break;
167 /* Anything left at this point is invalid. */
168 return 0;
172 /* In any remaining valid suffix, the case and order don't matter. */
173 while (len--)
174 switch (s[len])
176 case 'f': case 'F': f++; break;
177 case 'd': case 'D': d++; break;
178 case 'l': case 'L': l++; break;
179 case 'w': case 'W': w++; break;
180 case 'q': case 'Q': q++; break;
181 case 'i': case 'I':
182 case 'j': case 'J': i++; break;
183 default:
184 return 0;
187 if (f + d + l + w + q > 1 || i > 1)
188 return 0;
190 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
191 return 0;
193 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
194 return 0;
196 return ((i ? CPP_N_IMAGINARY : 0)
197 | (f ? CPP_N_SMALL :
198 d ? CPP_N_MEDIUM :
199 l ? CPP_N_LARGE :
200 w ? CPP_N_MD_W :
201 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
204 /* Return the classification flags for a float suffix. */
205 unsigned int
206 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
208 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
211 /* Subroutine of cpp_classify_number. S points to an integer suffix
212 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
213 flag vector describing the suffix. */
214 static unsigned int
215 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
217 size_t u, l, i;
219 u = l = i = 0;
221 while (len--)
222 switch (s[len])
224 case 'u': case 'U': u++; break;
225 case 'i': case 'I':
226 case 'j': case 'J': i++; break;
227 case 'l': case 'L': l++;
228 /* If there are two Ls, they must be adjacent and the same case. */
229 if (l == 2 && s[len] != s[len + 1])
230 return 0;
231 break;
232 default:
233 return 0;
236 if (l > 2 || u > 1 || i > 1)
237 return 0;
239 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
240 return 0;
242 return ((i ? CPP_N_IMAGINARY : 0)
243 | (u ? CPP_N_UNSIGNED : 0)
244 | ((l == 0) ? CPP_N_SMALL
245 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
248 /* Return the classification flags for an int suffix. */
249 unsigned int
250 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
252 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
255 /* Return the string type corresponding to the the input user-defined string
256 literal type. If the input type is not a user-defined string literal
257 type return the input type. */
258 enum cpp_ttype
259 cpp_userdef_string_remove_type (enum cpp_ttype type)
261 if (type == CPP_STRING_USERDEF)
262 return CPP_STRING;
263 else if (type == CPP_WSTRING_USERDEF)
264 return CPP_WSTRING;
265 else if (type == CPP_STRING16_USERDEF)
266 return CPP_STRING16;
267 else if (type == CPP_STRING32_USERDEF)
268 return CPP_STRING32;
269 else if (type == CPP_UTF8STRING_USERDEF)
270 return CPP_UTF8STRING;
271 else
272 return type;
275 /* Return the user-defined string literal type corresponding to the input
276 string type. If the input type is not a string type return the input
277 type. */
278 enum cpp_ttype
279 cpp_userdef_string_add_type (enum cpp_ttype type)
281 if (type == CPP_STRING)
282 return CPP_STRING_USERDEF;
283 else if (type == CPP_WSTRING)
284 return CPP_WSTRING_USERDEF;
285 else if (type == CPP_STRING16)
286 return CPP_STRING16_USERDEF;
287 else if (type == CPP_STRING32)
288 return CPP_STRING32_USERDEF;
289 else if (type == CPP_UTF8STRING)
290 return CPP_UTF8STRING_USERDEF;
291 else
292 return type;
295 /* Return the char type corresponding to the the input user-defined char
296 literal type. If the input type is not a user-defined char literal
297 type return the input type. */
298 enum cpp_ttype
299 cpp_userdef_char_remove_type (enum cpp_ttype type)
301 if (type == CPP_CHAR_USERDEF)
302 return CPP_CHAR;
303 else if (type == CPP_WCHAR_USERDEF)
304 return CPP_WCHAR;
305 else if (type == CPP_CHAR16_USERDEF)
306 return CPP_CHAR16;
307 else if (type == CPP_CHAR32_USERDEF)
308 return CPP_CHAR32;
309 else
310 return type;
313 /* Return the user-defined char literal type corresponding to the input
314 char type. If the input type is not a char type return the input
315 type. */
316 enum cpp_ttype
317 cpp_userdef_char_add_type (enum cpp_ttype type)
319 if (type == CPP_CHAR)
320 return CPP_CHAR_USERDEF;
321 else if (type == CPP_WCHAR)
322 return CPP_WCHAR_USERDEF;
323 else if (type == CPP_CHAR16)
324 return CPP_CHAR16_USERDEF;
325 else if (type == CPP_CHAR32)
326 return CPP_CHAR32_USERDEF;
327 else
328 return type;
331 /* Return true if the token type is a user-defined string literal. */
332 bool
333 cpp_userdef_string_p (enum cpp_ttype type)
335 if (type == CPP_STRING_USERDEF
336 || type == CPP_WSTRING_USERDEF
337 || type == CPP_STRING16_USERDEF
338 || type == CPP_STRING32_USERDEF
339 || type == CPP_UTF8STRING_USERDEF)
340 return true;
341 else
342 return false;
345 /* Return true if the token type is a user-defined char literal. */
346 bool
347 cpp_userdef_char_p (enum cpp_ttype type)
349 if (type == CPP_CHAR_USERDEF
350 || type == CPP_WCHAR_USERDEF
351 || type == CPP_CHAR16_USERDEF
352 || type == CPP_CHAR32_USERDEF)
353 return true;
354 else
355 return false;
358 /* Extract the suffix from a user-defined literal string or char. */
359 const char *
360 cpp_get_userdef_suffix (const cpp_token *tok)
362 unsigned int len = tok->val.str.len;
363 const char *text = (const char *)tok->val.str.text;
364 char delim;
365 unsigned int i;
366 for (i = 0; i < len; ++i)
367 if (text[i] == '\'' || text[i] == '"')
368 break;
369 if (i == len)
370 return text + len;
371 delim = text[i];
372 for (i = len; i > 0; --i)
373 if (text[i - 1] == delim)
374 break;
375 return text + i;
378 /* Categorize numeric constants according to their field (integer,
379 floating point, or invalid), radix (decimal, octal, hexadecimal),
380 and type suffixes.
382 TOKEN is the token that represents the numeric constant to
383 classify.
385 In C++0X if UD_SUFFIX is non null it will be assigned
386 any unrecognized suffix for a user-defined literal.
388 VIRTUAL_LOCATION is the virtual location for TOKEN. */
389 unsigned int
390 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
391 const char **ud_suffix, source_location virtual_location)
393 const uchar *str = token->val.str.text;
394 const uchar *limit;
395 unsigned int max_digit, result, radix;
396 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
397 bool seen_digit;
399 if (ud_suffix)
400 *ud_suffix = NULL;
402 /* If the lexer has done its job, length one can only be a single
403 digit. Fast-path this very common case. */
404 if (token->val.str.len == 1)
405 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
407 limit = str + token->val.str.len;
408 float_flag = NOT_FLOAT;
409 max_digit = 0;
410 radix = 10;
411 seen_digit = false;
413 /* First, interpret the radix. */
414 if (*str == '0')
416 radix = 8;
417 str++;
419 /* Require at least one hex digit to classify it as hex. */
420 if ((*str == 'x' || *str == 'X')
421 && (str[1] == '.' || ISXDIGIT (str[1])))
423 radix = 16;
424 str++;
426 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
428 radix = 2;
429 str++;
433 /* Now scan for a well-formed integer or float. */
434 for (;;)
436 unsigned int c = *str++;
438 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
440 seen_digit = true;
441 c = hex_value (c);
442 if (c > max_digit)
443 max_digit = c;
445 else if (c == '.')
447 if (float_flag == NOT_FLOAT)
448 float_flag = AFTER_POINT;
449 else
450 SYNTAX_ERROR_AT (virtual_location,
451 "too many decimal points in number");
453 else if ((radix <= 10 && (c == 'e' || c == 'E'))
454 || (radix == 16 && (c == 'p' || c == 'P')))
456 float_flag = AFTER_EXPON;
457 break;
459 else
461 /* Start of suffix. */
462 str--;
463 break;
467 /* The suffix may be for decimal fixed-point constants without exponent. */
468 if (radix != 16 && float_flag == NOT_FLOAT)
470 result = interpret_float_suffix (pfile, str, limit - str);
471 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
473 result |= CPP_N_FLOATING;
474 /* We need to restore the radix to 10, if the radix is 8. */
475 if (radix == 8)
476 radix = 10;
478 if (CPP_PEDANTIC (pfile))
479 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
480 "fixed-point constants are a GCC extension");
481 goto syntax_ok;
483 else
484 result = 0;
487 if (float_flag != NOT_FLOAT && radix == 8)
488 radix = 10;
490 if (max_digit >= radix)
492 if (radix == 2)
493 SYNTAX_ERROR2_AT (virtual_location,
494 "invalid digit \"%c\" in binary constant", '0' + max_digit);
495 else
496 SYNTAX_ERROR2_AT (virtual_location,
497 "invalid digit \"%c\" in octal constant", '0' + max_digit);
500 if (float_flag != NOT_FLOAT)
502 if (radix == 2)
504 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
505 "invalid prefix \"0b\" for floating constant");
506 return CPP_N_INVALID;
509 if (radix == 16 && !seen_digit)
510 SYNTAX_ERROR_AT (virtual_location,
511 "no digits in hexadecimal floating constant");
513 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
514 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
515 "use of C99 hexadecimal floating constant");
517 if (float_flag == AFTER_EXPON)
519 if (*str == '+' || *str == '-')
520 str++;
522 /* Exponent is decimal, even if string is a hex float. */
523 if (!ISDIGIT (*str))
524 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
527 str++;
528 while (ISDIGIT (*str));
530 else if (radix == 16)
531 SYNTAX_ERROR_AT (virtual_location,
532 "hexadecimal floating constants require an exponent");
534 result = interpret_float_suffix (pfile, str, limit - str);
535 if (result == 0)
537 if (CPP_OPTION (pfile, user_literals))
539 if (ud_suffix)
540 *ud_suffix = (const char *) str;
541 result = CPP_N_LARGE | CPP_N_USERDEF;
543 else
545 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
546 "invalid suffix \"%.*s\" on floating constant",
547 (int) (limit - str), str);
548 return CPP_N_INVALID;
552 /* Traditional C didn't accept any floating suffixes. */
553 if (limit != str
554 && CPP_WTRADITIONAL (pfile)
555 && ! cpp_sys_macro_p (pfile))
556 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
557 "traditional C rejects the \"%.*s\" suffix",
558 (int) (limit - str), str);
560 /* A suffix for double is a GCC extension via decimal float support.
561 If the suffix also specifies an imaginary value we'll catch that
562 later. */
563 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
564 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
565 "suffix for double constant is a GCC extension");
567 /* Radix must be 10 for decimal floats. */
568 if ((result & CPP_N_DFLOAT) && radix != 10)
570 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
571 "invalid suffix \"%.*s\" with hexadecimal floating constant",
572 (int) (limit - str), str);
573 return CPP_N_INVALID;
576 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
577 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
578 "fixed-point constants are a GCC extension");
580 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
581 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
582 "decimal float constants are a GCC extension");
584 result |= CPP_N_FLOATING;
586 else
588 result = interpret_int_suffix (pfile, str, limit - str);
589 if (result == 0)
591 if (CPP_OPTION (pfile, user_literals))
593 if (ud_suffix)
594 *ud_suffix = (const char *) str;
595 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
597 else
599 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
600 "invalid suffix \"%.*s\" on integer constant",
601 (int) (limit - str), str);
602 return CPP_N_INVALID;
606 /* Traditional C only accepted the 'L' suffix.
607 Suppress warning about 'LL' with -Wno-long-long. */
608 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
610 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
611 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
612 && CPP_OPTION (pfile, cpp_warn_long_long);
614 if (u_or_i || large)
615 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
616 virtual_location, 0,
617 "traditional C rejects the \"%.*s\" suffix",
618 (int) (limit - str), str);
621 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
622 && CPP_OPTION (pfile, cpp_warn_long_long))
624 const char *message = CPP_OPTION (pfile, cplusplus)
625 ? N_("use of C++0x long long integer constant")
626 : N_("use of C99 long long integer constant");
628 if (CPP_OPTION (pfile, c99))
629 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
630 0, message);
631 else
632 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
633 virtual_location, 0, message);
636 result |= CPP_N_INTEGER;
639 syntax_ok:
640 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
641 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
642 "imaginary constants are a GCC extension");
643 if (radix == 2 && CPP_PEDANTIC (pfile))
644 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
645 "binary constants are a GCC extension");
647 if (radix == 10)
648 result |= CPP_N_DECIMAL;
649 else if (radix == 16)
650 result |= CPP_N_HEX;
651 else if (radix == 2)
652 result |= CPP_N_BINARY;
653 else
654 result |= CPP_N_OCTAL;
656 return result;
658 syntax_error:
659 return CPP_N_INVALID;
662 /* cpp_interpret_integer converts an integer constant into a cpp_num,
663 of precision options->precision.
665 We do not provide any interface for decimal->float conversion,
666 because the preprocessor doesn't need it and we don't want to
667 drag in GCC's floating point emulator. */
668 cpp_num
669 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
670 unsigned int type)
672 const uchar *p, *end;
673 cpp_num result;
675 result.low = 0;
676 result.high = 0;
677 result.unsignedp = !!(type & CPP_N_UNSIGNED);
678 result.overflow = false;
680 p = token->val.str.text;
681 end = p + token->val.str.len;
683 /* Common case of a single digit. */
684 if (token->val.str.len == 1)
685 result.low = p[0] - '0';
686 else
688 cpp_num_part max;
689 size_t precision = CPP_OPTION (pfile, precision);
690 unsigned int base = 10, c = 0;
691 bool overflow = false;
693 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
695 base = 8;
696 p++;
698 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
700 base = 16;
701 p += 2;
703 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
705 base = 2;
706 p += 2;
709 /* We can add a digit to numbers strictly less than this without
710 needing the precision and slowness of double integers. */
711 max = ~(cpp_num_part) 0;
712 if (precision < PART_PRECISION)
713 max >>= PART_PRECISION - precision;
714 max = (max - base + 1) / base + 1;
716 for (; p < end; p++)
718 c = *p;
720 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
721 c = hex_value (c);
722 else
723 break;
725 /* Strict inequality for when max is set to zero. */
726 if (result.low < max)
727 result.low = result.low * base + c;
728 else
730 result = append_digit (result, c, base, precision);
731 overflow |= result.overflow;
732 max = 0;
736 if (overflow && !(type & CPP_N_USERDEF))
737 cpp_error (pfile, CPP_DL_PEDWARN,
738 "integer constant is too large for its type");
739 /* If too big to be signed, consider it unsigned. Only warn for
740 decimal numbers. Traditional numbers were always signed (but
741 we still honor an explicit U suffix); but we only have
742 traditional semantics in directives. */
743 else if (!result.unsignedp
744 && !(CPP_OPTION (pfile, traditional)
745 && pfile->state.in_directive)
746 && !num_positive (result, precision))
748 /* This is for constants within the range of uintmax_t but
749 not that of intmax_t. For such decimal constants, a
750 diagnostic is required for C99 as the selected type must
751 be signed and not having a type is a constraint violation
752 (DR#298, TC3), so this must be a pedwarn. For C90,
753 unsigned long is specified to be used for a constant that
754 does not fit in signed long; if uintmax_t has the same
755 range as unsigned long this means only a warning is
756 appropriate here. C90 permits the preprocessor to use a
757 wider range than unsigned long in the compiler, so if
758 uintmax_t is wider than unsigned long no diagnostic is
759 required for such constants in preprocessor #if
760 expressions and the compiler will pedwarn for such
761 constants outside the range of unsigned long that reach
762 the compiler so a diagnostic is not required there
763 either; thus, pedwarn for C99 but use a plain warning for
764 C90. */
765 if (base == 10)
766 cpp_error (pfile, (CPP_OPTION (pfile, c99)
767 ? CPP_DL_PEDWARN
768 : CPP_DL_WARNING),
769 "integer constant is so large that it is unsigned");
770 result.unsignedp = true;
774 return result;
777 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
778 static cpp_num
779 append_digit (cpp_num num, int digit, int base, size_t precision)
781 cpp_num result;
782 unsigned int shift;
783 bool overflow;
784 cpp_num_part add_high, add_low;
786 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
787 need to worry about add_high overflowing. */
788 switch (base)
790 case 2:
791 shift = 1;
792 break;
794 case 16:
795 shift = 4;
796 break;
798 default:
799 shift = 3;
801 overflow = !!(num.high >> (PART_PRECISION - shift));
802 result.high = num.high << shift;
803 result.low = num.low << shift;
804 result.high |= num.low >> (PART_PRECISION - shift);
805 result.unsignedp = num.unsignedp;
807 if (base == 10)
809 add_low = num.low << 1;
810 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
812 else
813 add_high = add_low = 0;
815 if (add_low + digit < add_low)
816 add_high++;
817 add_low += digit;
819 if (result.low + add_low < result.low)
820 add_high++;
821 if (result.high + add_high < result.high)
822 overflow = true;
824 result.low += add_low;
825 result.high += add_high;
826 result.overflow = overflow;
828 /* The above code catches overflow of a cpp_num type. This catches
829 overflow of the (possibly shorter) target precision. */
830 num.low = result.low;
831 num.high = result.high;
832 result = num_trim (result, precision);
833 if (!num_eq (result, num))
834 result.overflow = true;
836 return result;
839 /* Handle meeting "defined" in a preprocessor expression. */
840 static cpp_num
841 parse_defined (cpp_reader *pfile)
843 cpp_num result;
844 int paren = 0;
845 cpp_hashnode *node = 0;
846 const cpp_token *token;
847 cpp_context *initial_context = pfile->context;
849 /* Don't expand macros. */
850 pfile->state.prevent_expansion++;
852 token = cpp_get_token (pfile);
853 if (token->type == CPP_OPEN_PAREN)
855 paren = 1;
856 token = cpp_get_token (pfile);
859 if (token->type == CPP_NAME)
861 node = token->val.node.node;
862 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
864 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
865 node = 0;
868 else
870 cpp_error (pfile, CPP_DL_ERROR,
871 "operator \"defined\" requires an identifier");
872 if (token->flags & NAMED_OP)
874 cpp_token op;
876 op.flags = 0;
877 op.type = token->type;
878 cpp_error (pfile, CPP_DL_ERROR,
879 "(\"%s\" is an alternative token for \"%s\" in C++)",
880 cpp_token_as_text (pfile, token),
881 cpp_token_as_text (pfile, &op));
885 if (node)
887 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
888 cpp_error (pfile, CPP_DL_WARNING,
889 "this use of \"defined\" may not be portable");
891 _cpp_mark_macro_used (node);
892 if (!(node->flags & NODE_USED))
894 node->flags |= NODE_USED;
895 if (node->type == NT_MACRO)
897 if ((node->flags & NODE_BUILTIN)
898 && pfile->cb.user_builtin_macro)
899 pfile->cb.user_builtin_macro (pfile, node);
900 if (pfile->cb.used_define)
901 pfile->cb.used_define (pfile, pfile->directive_line, node);
903 else
905 if (pfile->cb.used_undef)
906 pfile->cb.used_undef (pfile, pfile->directive_line, node);
910 /* A possible controlling macro of the form #if !defined ().
911 _cpp_parse_expr checks there was no other junk on the line. */
912 pfile->mi_ind_cmacro = node;
915 pfile->state.prevent_expansion--;
917 /* Do not treat conditional macros as being defined. This is due to the
918 powerpc and spu ports using conditional macros for 'vector', 'bool', and
919 'pixel' to act as conditional keywords. This messes up tests like #ifndef
920 bool. */
921 result.unsignedp = false;
922 result.high = 0;
923 result.overflow = false;
924 result.low = (node && node->type == NT_MACRO
925 && (node->flags & NODE_CONDITIONAL) == 0);
926 return result;
929 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
930 number or character constant, or the result of the "defined" or "#"
931 operators). */
932 static cpp_num
933 eval_token (cpp_reader *pfile, const cpp_token *token,
934 source_location virtual_location)
936 cpp_num result;
937 unsigned int temp;
938 int unsignedp = 0;
940 result.unsignedp = false;
941 result.overflow = false;
943 switch (token->type)
945 case CPP_NUMBER:
946 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
947 if (temp & CPP_N_USERDEF)
948 cpp_error (pfile, CPP_DL_ERROR,
949 "user-defined literal in preprocessor expression");
950 switch (temp & CPP_N_CATEGORY)
952 case CPP_N_FLOATING:
953 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
954 "floating constant in preprocessor expression");
955 break;
956 case CPP_N_INTEGER:
957 if (!(temp & CPP_N_IMAGINARY))
958 return cpp_interpret_integer (pfile, token, temp);
959 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
960 "imaginary number in preprocessor expression");
961 break;
963 case CPP_N_INVALID:
964 /* Error already issued. */
965 break;
967 result.high = result.low = 0;
968 break;
970 case CPP_WCHAR:
971 case CPP_CHAR:
972 case CPP_CHAR16:
973 case CPP_CHAR32:
975 cppchar_t cc = cpp_interpret_charconst (pfile, token,
976 &temp, &unsignedp);
978 result.high = 0;
979 result.low = cc;
980 /* Sign-extend the result if necessary. */
981 if (!unsignedp && (cppchar_signed_t) cc < 0)
983 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
984 result.low |= ~(~(cpp_num_part) 0
985 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
986 result.high = ~(cpp_num_part) 0;
987 result = num_trim (result, CPP_OPTION (pfile, precision));
990 break;
992 case CPP_NAME:
993 if (token->val.node.node == pfile->spec_nodes.n_defined)
994 return parse_defined (pfile);
995 else if (CPP_OPTION (pfile, cplusplus)
996 && (token->val.node.node == pfile->spec_nodes.n_true
997 || token->val.node.node == pfile->spec_nodes.n_false))
999 result.high = 0;
1000 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1002 else
1004 result.high = 0;
1005 result.low = 0;
1006 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1007 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1008 "\"%s\" is not defined",
1009 NODE_NAME (token->val.node.node));
1011 break;
1013 case CPP_HASH:
1014 if (!pfile->state.skipping)
1016 /* A pedantic warning takes precedence over a deprecated
1017 warning here. */
1018 if (CPP_PEDANTIC (pfile))
1019 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1020 virtual_location, 0,
1021 "assertions are a GCC extension");
1022 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1023 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1024 "assertions are a deprecated extension");
1026 _cpp_test_assertion (pfile, &temp);
1027 result.high = 0;
1028 result.low = temp;
1029 break;
1031 default:
1032 abort ();
1035 result.unsignedp = !!unsignedp;
1036 return result;
1039 /* Operator precedence and flags table.
1041 After an operator is returned from the lexer, if it has priority less
1042 than the operator on the top of the stack, we reduce the stack by one
1043 operator and repeat the test. Since equal priorities do not reduce,
1044 this is naturally right-associative.
1046 We handle left-associative operators by decrementing the priority of
1047 just-lexed operators by one, but retaining the priority of operators
1048 already on the stack.
1050 The remaining cases are '(' and ')'. We handle '(' by skipping the
1051 reduction phase completely. ')' is given lower priority than
1052 everything else, including '(', effectively forcing a reduction of the
1053 parenthesized expression. If there is a matching '(', the routine
1054 reduce() exits immediately. If the normal exit route sees a ')', then
1055 there cannot have been a matching '(' and an error message is output.
1057 The parser assumes all shifted operators require a left operand unless
1058 the flag NO_L_OPERAND is set. These semantics are automatic; any
1059 extra semantics need to be handled with operator-specific code. */
1061 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1062 operand changes because of integer promotions. */
1063 #define NO_L_OPERAND (1 << 0)
1064 #define LEFT_ASSOC (1 << 1)
1065 #define CHECK_PROMOTION (1 << 2)
1067 /* Operator to priority map. Must be in the same order as the first
1068 N entries of enum cpp_ttype. */
1069 static const struct cpp_operator
1071 uchar prio;
1072 uchar flags;
1073 } optab[] =
1075 /* EQ */ {0, 0}, /* Shouldn't happen. */
1076 /* NOT */ {16, NO_L_OPERAND},
1077 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1078 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1079 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1080 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1081 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1082 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1083 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1084 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1085 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1086 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1087 /* RSHIFT */ {13, LEFT_ASSOC},
1088 /* LSHIFT */ {13, LEFT_ASSOC},
1090 /* COMPL */ {16, NO_L_OPERAND},
1091 /* AND_AND */ {6, LEFT_ASSOC},
1092 /* OR_OR */ {5, LEFT_ASSOC},
1093 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1094 However, there are some special cases for these in reduce(). */
1095 /* QUERY */ {4, 0},
1096 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1097 /* COMMA */ {4, LEFT_ASSOC},
1098 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1099 /* CLOSE_PAREN */ {0, 0},
1100 /* EOF */ {0, 0},
1101 /* EQ_EQ */ {11, LEFT_ASSOC},
1102 /* NOT_EQ */ {11, LEFT_ASSOC},
1103 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1104 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1105 /* UPLUS */ {16, NO_L_OPERAND},
1106 /* UMINUS */ {16, NO_L_OPERAND}
1109 /* Parse and evaluate a C expression, reading from PFILE.
1110 Returns the truth value of the expression.
1112 The implementation is an operator precedence parser, i.e. a
1113 bottom-up parser, using a stack for not-yet-reduced tokens.
1115 The stack base is op_stack, and the current stack pointer is 'top'.
1116 There is a stack element for each operator (only), and the most
1117 recently pushed operator is 'top->op'. An operand (value) is
1118 stored in the 'value' field of the stack element of the operator
1119 that precedes it. */
1120 bool
1121 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1123 struct op *top = pfile->op_stack;
1124 unsigned int lex_count;
1125 bool saw_leading_not, want_value = true;
1126 source_location virtual_location = 0;
1128 pfile->state.skip_eval = 0;
1130 /* Set up detection of #if ! defined(). */
1131 pfile->mi_ind_cmacro = 0;
1132 saw_leading_not = false;
1133 lex_count = 0;
1135 /* Lowest priority operator prevents further reductions. */
1136 top->op = CPP_EOF;
1138 for (;;)
1140 struct op op;
1142 lex_count++;
1143 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1144 op.op = op.token->type;
1145 op.loc = virtual_location;
1147 switch (op.op)
1149 /* These tokens convert into values. */
1150 case CPP_NUMBER:
1151 case CPP_CHAR:
1152 case CPP_WCHAR:
1153 case CPP_CHAR16:
1154 case CPP_CHAR32:
1155 case CPP_NAME:
1156 case CPP_HASH:
1157 if (!want_value)
1158 SYNTAX_ERROR2_AT (op.loc,
1159 "missing binary operator before token \"%s\"",
1160 cpp_token_as_text (pfile, op.token));
1161 want_value = false;
1162 top->value = eval_token (pfile, op.token, op.loc);
1163 continue;
1165 case CPP_NOT:
1166 saw_leading_not = lex_count == 1;
1167 break;
1168 case CPP_PLUS:
1169 if (want_value)
1170 op.op = CPP_UPLUS;
1171 break;
1172 case CPP_MINUS:
1173 if (want_value)
1174 op.op = CPP_UMINUS;
1175 break;
1177 default:
1178 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1179 SYNTAX_ERROR2_AT (op.loc,
1180 "token \"%s\" is not valid in preprocessor expressions",
1181 cpp_token_as_text (pfile, op.token));
1182 break;
1185 /* Check we have a value or operator as appropriate. */
1186 if (optab[op.op].flags & NO_L_OPERAND)
1188 if (!want_value)
1189 SYNTAX_ERROR2_AT (op.loc,
1190 "missing binary operator before token \"%s\"",
1191 cpp_token_as_text (pfile, op.token));
1193 else if (want_value)
1195 /* We want a number (or expression) and haven't got one.
1196 Try to emit a specific diagnostic. */
1197 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1198 SYNTAX_ERROR_AT (op.loc,
1199 "missing expression between '(' and ')'");
1201 if (op.op == CPP_EOF && top->op == CPP_EOF)
1202 SYNTAX_ERROR2_AT (op.loc,
1203 "%s with no expression", is_if ? "#if" : "#elif");
1205 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1206 SYNTAX_ERROR2_AT (op.loc,
1207 "operator '%s' has no right operand",
1208 cpp_token_as_text (pfile, top->token));
1209 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1210 /* Complain about missing paren during reduction. */;
1211 else
1212 SYNTAX_ERROR2_AT (op.loc,
1213 "operator '%s' has no left operand",
1214 cpp_token_as_text (pfile, op.token));
1217 top = reduce (pfile, top, op.op);
1218 if (!top)
1219 goto syntax_error;
1221 if (op.op == CPP_EOF)
1222 break;
1224 switch (op.op)
1226 case CPP_CLOSE_PAREN:
1227 continue;
1228 case CPP_OR_OR:
1229 if (!num_zerop (top->value))
1230 pfile->state.skip_eval++;
1231 break;
1232 case CPP_AND_AND:
1233 case CPP_QUERY:
1234 if (num_zerop (top->value))
1235 pfile->state.skip_eval++;
1236 break;
1237 case CPP_COLON:
1238 if (top->op != CPP_QUERY)
1239 SYNTAX_ERROR_AT (op.loc,
1240 " ':' without preceding '?'");
1241 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1242 pfile->state.skip_eval++;
1243 else
1244 pfile->state.skip_eval--;
1245 default:
1246 break;
1249 want_value = true;
1251 /* Check for and handle stack overflow. */
1252 if (++top == pfile->op_limit)
1253 top = _cpp_expand_op_stack (pfile);
1255 top->op = op.op;
1256 top->token = op.token;
1257 top->loc = op.loc;
1260 /* The controlling macro expression is only valid if we called lex 3
1261 times: <!> <defined expression> and <EOF>. push_conditional ()
1262 checks that we are at top-of-file. */
1263 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1264 pfile->mi_ind_cmacro = 0;
1266 if (top != pfile->op_stack)
1268 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1269 "unbalanced stack in %s",
1270 is_if ? "#if" : "#elif");
1271 syntax_error:
1272 return false; /* Return false on syntax error. */
1275 return !num_zerop (top->value);
1278 /* Reduce the operator / value stack if possible, in preparation for
1279 pushing operator OP. Returns NULL on error, otherwise the top of
1280 the stack. */
1281 static struct op *
1282 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1284 unsigned int prio;
1286 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1288 bad_op:
1289 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1290 return 0;
1293 if (op == CPP_OPEN_PAREN)
1294 return top;
1296 /* Decrement the priority of left-associative operators to force a
1297 reduction with operators of otherwise equal priority. */
1298 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1299 while (prio < optab[top->op].prio)
1301 if (CPP_OPTION (pfile, warn_num_sign_change)
1302 && optab[top->op].flags & CHECK_PROMOTION)
1303 check_promotion (pfile, top);
1305 switch (top->op)
1307 case CPP_UPLUS:
1308 case CPP_UMINUS:
1309 case CPP_NOT:
1310 case CPP_COMPL:
1311 top[-1].value = num_unary_op (pfile, top->value, top->op);
1312 top[-1].loc = top->loc;
1313 break;
1315 case CPP_PLUS:
1316 case CPP_MINUS:
1317 case CPP_RSHIFT:
1318 case CPP_LSHIFT:
1319 case CPP_COMMA:
1320 top[-1].value = num_binary_op (pfile, top[-1].value,
1321 top->value, top->op);
1322 top[-1].loc = top->loc;
1323 break;
1325 case CPP_GREATER:
1326 case CPP_LESS:
1327 case CPP_GREATER_EQ:
1328 case CPP_LESS_EQ:
1329 top[-1].value
1330 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1331 top[-1].loc = top->loc;
1332 break;
1334 case CPP_EQ_EQ:
1335 case CPP_NOT_EQ:
1336 top[-1].value
1337 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1338 top[-1].loc = top->loc;
1339 break;
1341 case CPP_AND:
1342 case CPP_OR:
1343 case CPP_XOR:
1344 top[-1].value
1345 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1346 top[-1].loc = top->loc;
1347 break;
1349 case CPP_MULT:
1350 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1351 top[-1].loc = top->loc;
1352 break;
1354 case CPP_DIV:
1355 case CPP_MOD:
1356 top[-1].value = num_div_op (pfile, top[-1].value,
1357 top->value, top->op, top->loc);
1358 top[-1].loc = top->loc;
1359 break;
1361 case CPP_OR_OR:
1362 top--;
1363 if (!num_zerop (top->value))
1364 pfile->state.skip_eval--;
1365 top->value.low = (!num_zerop (top->value)
1366 || !num_zerop (top[1].value));
1367 top->value.high = 0;
1368 top->value.unsignedp = false;
1369 top->value.overflow = false;
1370 top->loc = top[1].loc;
1371 continue;
1373 case CPP_AND_AND:
1374 top--;
1375 if (num_zerop (top->value))
1376 pfile->state.skip_eval--;
1377 top->value.low = (!num_zerop (top->value)
1378 && !num_zerop (top[1].value));
1379 top->value.high = 0;
1380 top->value.unsignedp = false;
1381 top->value.overflow = false;
1382 top->loc = top[1].loc;
1383 continue;
1385 case CPP_OPEN_PAREN:
1386 if (op != CPP_CLOSE_PAREN)
1388 cpp_error_with_line (pfile, CPP_DL_ERROR,
1389 top->token->src_loc,
1390 0, "missing ')' in expression");
1391 return 0;
1393 top--;
1394 top->value = top[1].value;
1395 top->loc = top[1].loc;
1396 return top;
1398 case CPP_COLON:
1399 top -= 2;
1400 if (!num_zerop (top->value))
1402 pfile->state.skip_eval--;
1403 top->value = top[1].value;
1404 top->loc = top[1].loc;
1406 else
1408 top->value = top[2].value;
1409 top->loc = top[2].loc;
1411 top->value.unsignedp = (top[1].value.unsignedp
1412 || top[2].value.unsignedp);
1413 continue;
1415 case CPP_QUERY:
1416 /* COMMA and COLON should not reduce a QUERY operator. */
1417 if (op == CPP_COMMA || op == CPP_COLON)
1418 return top;
1419 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1420 return 0;
1422 default:
1423 goto bad_op;
1426 top--;
1427 if (top->value.overflow && !pfile->state.skip_eval)
1428 cpp_error (pfile, CPP_DL_PEDWARN,
1429 "integer overflow in preprocessor expression");
1432 if (op == CPP_CLOSE_PAREN)
1434 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1435 return 0;
1438 return top;
1441 /* Returns the position of the old top of stack after expansion. */
1442 struct op *
1443 _cpp_expand_op_stack (cpp_reader *pfile)
1445 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1446 size_t new_size = old_size * 2 + 20;
1448 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1449 pfile->op_limit = pfile->op_stack + new_size;
1451 return pfile->op_stack + old_size;
1454 /* Emits a warning if the effective sign of either operand of OP
1455 changes because of integer promotions. */
1456 static void
1457 check_promotion (cpp_reader *pfile, const struct op *op)
1459 if (op->value.unsignedp == op[-1].value.unsignedp)
1460 return;
1462 if (op->value.unsignedp)
1464 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1465 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1466 "the left operand of \"%s\" changes sign when promoted",
1467 cpp_token_as_text (pfile, op->token));
1469 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1470 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1471 "the right operand of \"%s\" changes sign when promoted",
1472 cpp_token_as_text (pfile, op->token));
1475 /* Clears the unused high order bits of the number pointed to by PNUM. */
1476 static cpp_num
1477 num_trim (cpp_num num, size_t precision)
1479 if (precision > PART_PRECISION)
1481 precision -= PART_PRECISION;
1482 if (precision < PART_PRECISION)
1483 num.high &= ((cpp_num_part) 1 << precision) - 1;
1485 else
1487 if (precision < PART_PRECISION)
1488 num.low &= ((cpp_num_part) 1 << precision) - 1;
1489 num.high = 0;
1492 return num;
1495 /* True iff A (presumed signed) >= 0. */
1496 static bool
1497 num_positive (cpp_num num, size_t precision)
1499 if (precision > PART_PRECISION)
1501 precision -= PART_PRECISION;
1502 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1505 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1508 /* Sign extend a number, with PRECISION significant bits and all
1509 others assumed clear, to fill out a cpp_num structure. */
1510 cpp_num
1511 cpp_num_sign_extend (cpp_num num, size_t precision)
1513 if (!num.unsignedp)
1515 if (precision > PART_PRECISION)
1517 precision -= PART_PRECISION;
1518 if (precision < PART_PRECISION
1519 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1520 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1522 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1524 if (precision < PART_PRECISION)
1525 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1526 num.high = ~(cpp_num_part) 0;
1530 return num;
1533 /* Returns the negative of NUM. */
1534 static cpp_num
1535 num_negate (cpp_num num, size_t precision)
1537 cpp_num copy;
1539 copy = num;
1540 num.high = ~num.high;
1541 num.low = ~num.low;
1542 if (++num.low == 0)
1543 num.high++;
1544 num = num_trim (num, precision);
1545 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1547 return num;
1550 /* Returns true if A >= B. */
1551 static bool
1552 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1554 bool unsignedp;
1556 unsignedp = pa.unsignedp || pb.unsignedp;
1558 if (!unsignedp)
1560 /* Both numbers have signed type. If they are of different
1561 sign, the answer is the sign of A. */
1562 unsignedp = num_positive (pa, precision);
1564 if (unsignedp != num_positive (pb, precision))
1565 return unsignedp;
1567 /* Otherwise we can do an unsigned comparison. */
1570 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1573 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1574 static cpp_num
1575 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1576 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1578 lhs.overflow = false;
1579 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1581 /* As excess precision is zeroed, there is no need to num_trim () as
1582 these operations cannot introduce a set bit there. */
1583 if (op == CPP_AND)
1585 lhs.low &= rhs.low;
1586 lhs.high &= rhs.high;
1588 else if (op == CPP_OR)
1590 lhs.low |= rhs.low;
1591 lhs.high |= rhs.high;
1593 else
1595 lhs.low ^= rhs.low;
1596 lhs.high ^= rhs.high;
1599 return lhs;
1602 /* Returns LHS OP RHS, where OP is an inequality. */
1603 static cpp_num
1604 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1605 enum cpp_ttype op)
1607 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1609 if (op == CPP_GREATER_EQ)
1610 lhs.low = gte;
1611 else if (op == CPP_LESS)
1612 lhs.low = !gte;
1613 else if (op == CPP_GREATER)
1614 lhs.low = gte && !num_eq (lhs, rhs);
1615 else /* CPP_LESS_EQ. */
1616 lhs.low = !gte || num_eq (lhs, rhs);
1618 lhs.high = 0;
1619 lhs.overflow = false;
1620 lhs.unsignedp = false;
1621 return lhs;
1624 /* Returns LHS OP RHS, where OP is == or !=. */
1625 static cpp_num
1626 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1627 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1629 /* Work around a 3.0.4 bug; see PR 6950. */
1630 bool eq = num_eq (lhs, rhs);
1631 if (op == CPP_NOT_EQ)
1632 eq = !eq;
1633 lhs.low = eq;
1634 lhs.high = 0;
1635 lhs.overflow = false;
1636 lhs.unsignedp = false;
1637 return lhs;
1640 /* Shift NUM, of width PRECISION, right by N bits. */
1641 static cpp_num
1642 num_rshift (cpp_num num, size_t precision, size_t n)
1644 cpp_num_part sign_mask;
1645 bool x = num_positive (num, precision);
1647 if (num.unsignedp || x)
1648 sign_mask = 0;
1649 else
1650 sign_mask = ~(cpp_num_part) 0;
1652 if (n >= precision)
1653 num.high = num.low = sign_mask;
1654 else
1656 /* Sign-extend. */
1657 if (precision < PART_PRECISION)
1658 num.high = sign_mask, num.low |= sign_mask << precision;
1659 else if (precision < 2 * PART_PRECISION)
1660 num.high |= sign_mask << (precision - PART_PRECISION);
1662 if (n >= PART_PRECISION)
1664 n -= PART_PRECISION;
1665 num.low = num.high;
1666 num.high = sign_mask;
1669 if (n)
1671 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1672 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1676 num = num_trim (num, precision);
1677 num.overflow = false;
1678 return num;
1681 /* Shift NUM, of width PRECISION, left by N bits. */
1682 static cpp_num
1683 num_lshift (cpp_num num, size_t precision, size_t n)
1685 if (n >= precision)
1687 num.overflow = !num.unsignedp && !num_zerop (num);
1688 num.high = num.low = 0;
1690 else
1692 cpp_num orig, maybe_orig;
1693 size_t m = n;
1695 orig = num;
1696 if (m >= PART_PRECISION)
1698 m -= PART_PRECISION;
1699 num.high = num.low;
1700 num.low = 0;
1702 if (m)
1704 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1705 num.low <<= m;
1707 num = num_trim (num, precision);
1709 if (num.unsignedp)
1710 num.overflow = false;
1711 else
1713 maybe_orig = num_rshift (num, precision, n);
1714 num.overflow = !num_eq (orig, maybe_orig);
1718 return num;
1721 /* The four unary operators: +, -, ! and ~. */
1722 static cpp_num
1723 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1725 switch (op)
1727 case CPP_UPLUS:
1728 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1729 cpp_warning (pfile, CPP_W_TRADITIONAL,
1730 "traditional C rejects the unary plus operator");
1731 num.overflow = false;
1732 break;
1734 case CPP_UMINUS:
1735 num = num_negate (num, CPP_OPTION (pfile, precision));
1736 break;
1738 case CPP_COMPL:
1739 num.high = ~num.high;
1740 num.low = ~num.low;
1741 num = num_trim (num, CPP_OPTION (pfile, precision));
1742 num.overflow = false;
1743 break;
1745 default: /* case CPP_NOT: */
1746 num.low = num_zerop (num);
1747 num.high = 0;
1748 num.overflow = false;
1749 num.unsignedp = false;
1750 break;
1753 return num;
1756 /* The various binary operators. */
1757 static cpp_num
1758 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1760 cpp_num result;
1761 size_t precision = CPP_OPTION (pfile, precision);
1762 size_t n;
1764 switch (op)
1766 /* Shifts. */
1767 case CPP_LSHIFT:
1768 case CPP_RSHIFT:
1769 if (!rhs.unsignedp && !num_positive (rhs, precision))
1771 /* A negative shift is a positive shift the other way. */
1772 if (op == CPP_LSHIFT)
1773 op = CPP_RSHIFT;
1774 else
1775 op = CPP_LSHIFT;
1776 rhs = num_negate (rhs, precision);
1778 if (rhs.high)
1779 n = ~0; /* Maximal. */
1780 else
1781 n = rhs.low;
1782 if (op == CPP_LSHIFT)
1783 lhs = num_lshift (lhs, precision, n);
1784 else
1785 lhs = num_rshift (lhs, precision, n);
1786 break;
1788 /* Arithmetic. */
1789 case CPP_MINUS:
1790 rhs = num_negate (rhs, precision);
1791 case CPP_PLUS:
1792 result.low = lhs.low + rhs.low;
1793 result.high = lhs.high + rhs.high;
1794 if (result.low < lhs.low)
1795 result.high++;
1796 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1797 result.overflow = false;
1799 result = num_trim (result, precision);
1800 if (!result.unsignedp)
1802 bool lhsp = num_positive (lhs, precision);
1803 result.overflow = (lhsp == num_positive (rhs, precision)
1804 && lhsp != num_positive (result, precision));
1806 return result;
1808 /* Comma. */
1809 default: /* case CPP_COMMA: */
1810 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1811 || !pfile->state.skip_eval))
1812 cpp_error (pfile, CPP_DL_PEDWARN,
1813 "comma operator in operand of #if");
1814 lhs = rhs;
1815 break;
1818 return lhs;
1821 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1822 cannot overflow. */
1823 static cpp_num
1824 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1826 cpp_num result;
1827 cpp_num_part middle[2], temp;
1829 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1830 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1832 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1833 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1835 temp = result.low;
1836 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1837 if (result.low < temp)
1838 result.high++;
1840 temp = result.low;
1841 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1842 if (result.low < temp)
1843 result.high++;
1845 result.high += HIGH_PART (middle[0]);
1846 result.high += HIGH_PART (middle[1]);
1847 result.unsignedp = true;
1848 result.overflow = false;
1850 return result;
1853 /* Multiply two preprocessing numbers. */
1854 static cpp_num
1855 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1857 cpp_num result, temp;
1858 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1859 bool overflow, negate = false;
1860 size_t precision = CPP_OPTION (pfile, precision);
1862 /* Prepare for unsigned multiplication. */
1863 if (!unsignedp)
1865 if (!num_positive (lhs, precision))
1866 negate = !negate, lhs = num_negate (lhs, precision);
1867 if (!num_positive (rhs, precision))
1868 negate = !negate, rhs = num_negate (rhs, precision);
1871 overflow = lhs.high && rhs.high;
1872 result = num_part_mul (lhs.low, rhs.low);
1874 temp = num_part_mul (lhs.high, rhs.low);
1875 result.high += temp.low;
1876 if (temp.high)
1877 overflow = true;
1879 temp = num_part_mul (lhs.low, rhs.high);
1880 result.high += temp.low;
1881 if (temp.high)
1882 overflow = true;
1884 temp.low = result.low, temp.high = result.high;
1885 result = num_trim (result, precision);
1886 if (!num_eq (result, temp))
1887 overflow = true;
1889 if (negate)
1890 result = num_negate (result, precision);
1892 if (unsignedp)
1893 result.overflow = false;
1894 else
1895 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1896 && !num_zerop (result));
1897 result.unsignedp = unsignedp;
1899 return result;
1902 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1903 or the remainder depending upon OP. LOCATION is the source location
1904 of this operator (for diagnostics). */
1906 static cpp_num
1907 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1908 source_location location)
1910 cpp_num result, sub;
1911 cpp_num_part mask;
1912 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1913 bool negate = false, lhs_neg = false;
1914 size_t i, precision = CPP_OPTION (pfile, precision);
1916 /* Prepare for unsigned division. */
1917 if (!unsignedp)
1919 if (!num_positive (lhs, precision))
1920 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1921 if (!num_positive (rhs, precision))
1922 negate = !negate, rhs = num_negate (rhs, precision);
1925 /* Find the high bit. */
1926 if (rhs.high)
1928 i = precision - 1;
1929 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1930 for (; ; i--, mask >>= 1)
1931 if (rhs.high & mask)
1932 break;
1934 else if (rhs.low)
1936 if (precision > PART_PRECISION)
1937 i = precision - PART_PRECISION - 1;
1938 else
1939 i = precision - 1;
1940 mask = (cpp_num_part) 1 << i;
1941 for (; ; i--, mask >>= 1)
1942 if (rhs.low & mask)
1943 break;
1945 else
1947 if (!pfile->state.skip_eval)
1948 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1949 "division by zero in #if");
1950 return lhs;
1953 /* First nonzero bit of RHS is bit I. Do naive division by
1954 shifting the RHS fully left, and subtracting from LHS if LHS is
1955 at least as big, and then repeating but with one less shift.
1956 This is not very efficient, but is easy to understand. */
1958 rhs.unsignedp = true;
1959 lhs.unsignedp = true;
1960 i = precision - i - 1;
1961 sub = num_lshift (rhs, precision, i);
1963 result.high = result.low = 0;
1964 for (;;)
1966 if (num_greater_eq (lhs, sub, precision))
1968 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1969 if (i >= PART_PRECISION)
1970 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1971 else
1972 result.low |= (cpp_num_part) 1 << i;
1974 if (i-- == 0)
1975 break;
1976 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1977 sub.high >>= 1;
1980 /* We divide so that the remainder has the sign of the LHS. */
1981 if (op == CPP_DIV)
1983 result.unsignedp = unsignedp;
1984 result.overflow = false;
1985 if (!unsignedp)
1987 if (negate)
1988 result = num_negate (result, precision);
1989 result.overflow = (num_positive (result, precision) ^ !negate
1990 && !num_zerop (result));
1993 return result;
1996 /* CPP_MOD. */
1997 lhs.unsignedp = unsignedp;
1998 lhs.overflow = false;
1999 if (lhs_neg)
2000 lhs = num_negate (lhs, precision);
2002 return lhs;