Mark ChangeLog
[official-gcc.git] / libcpp / expr.c
blob46d9fd386ab7c3a2d71ebed5098063b759b2bbf6
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987-2013 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 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
78 #define SYNTAX_ERROR_AT(loc, msgid) \
79 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
80 while(0)
81 #define SYNTAX_ERROR2_AT(loc, msgid, arg) \
82 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
83 while(0)
85 /* Subroutine of cpp_classify_number. S points to a float suffix of
86 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
87 flag vector describing the suffix. */
88 static unsigned int
89 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
91 size_t flags;
92 size_t f, d, l, w, q, i;
94 flags = 0;
95 f = d = l = w = q = i = 0;
97 /* Process decimal float suffixes, which are two letters starting
98 with d or D. Order and case are significant. */
99 if (len == 2 && (*s == 'd' || *s == 'D'))
101 bool uppercase = (*s == 'D');
102 switch (s[1])
104 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
105 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
106 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
107 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
108 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
109 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
110 default:
111 /* Additional two-character suffixes beginning with D are not
112 for decimal float constants. */
113 break;
117 if (CPP_OPTION (pfile, ext_numeric_literals))
119 /* Recognize a fixed-point suffix. */
120 if (len != 0)
121 switch (s[len-1])
123 case 'k': case 'K': flags = CPP_N_ACCUM; break;
124 case 'r': case 'R': flags = CPP_N_FRACT; break;
125 default: break;
128 /* Continue processing a fixed-point suffix. The suffix is case
129 insensitive except for ll or LL. Order is significant. */
130 if (flags)
132 if (len == 1)
133 return flags;
134 len--;
136 if (*s == 'u' || *s == 'U')
138 flags |= CPP_N_UNSIGNED;
139 if (len == 1)
140 return flags;
141 len--;
142 s++;
145 switch (*s)
147 case 'h': case 'H':
148 if (len == 1)
149 return flags |= CPP_N_SMALL;
150 break;
151 case 'l':
152 if (len == 1)
153 return flags |= CPP_N_MEDIUM;
154 if (len == 2 && s[1] == 'l')
155 return flags |= CPP_N_LARGE;
156 break;
157 case 'L':
158 if (len == 1)
159 return flags |= CPP_N_MEDIUM;
160 if (len == 2 && s[1] == 'L')
161 return flags |= CPP_N_LARGE;
162 break;
163 default:
164 break;
166 /* Anything left at this point is invalid. */
167 return 0;
171 /* In any remaining valid suffix, the case and order don't matter. */
172 while (len--)
173 switch (s[len])
175 case 'f': case 'F': f++; break;
176 case 'd': case 'D': d++; break;
177 case 'l': case 'L': l++; break;
178 case 'w': case 'W': w++; break;
179 case 'q': case 'Q': q++; break;
180 case 'i': case 'I':
181 case 'j': case 'J': i++; break;
182 default:
183 return 0;
186 if (f + d + l + w + q > 1 || i > 1)
187 return 0;
189 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
190 return 0;
192 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
193 return 0;
195 return ((i ? CPP_N_IMAGINARY : 0)
196 | (f ? CPP_N_SMALL :
197 d ? CPP_N_MEDIUM :
198 l ? CPP_N_LARGE :
199 w ? CPP_N_MD_W :
200 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
203 /* Return the classification flags for a float suffix. */
204 unsigned int
205 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
207 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
210 /* Subroutine of cpp_classify_number. S points to an integer suffix
211 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
212 flag vector describing the suffix. */
213 static unsigned int
214 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
216 size_t u, l, i;
218 u = l = i = 0;
220 while (len--)
221 switch (s[len])
223 case 'u': case 'U': u++; break;
224 case 'i': case 'I':
225 case 'j': case 'J': i++; break;
226 case 'l': case 'L': l++;
227 /* If there are two Ls, they must be adjacent and the same case. */
228 if (l == 2 && s[len] != s[len + 1])
229 return 0;
230 break;
231 default:
232 return 0;
235 if (l > 2 || u > 1 || i > 1)
236 return 0;
238 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
239 return 0;
241 return ((i ? CPP_N_IMAGINARY : 0)
242 | (u ? CPP_N_UNSIGNED : 0)
243 | ((l == 0) ? CPP_N_SMALL
244 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
247 /* Return the classification flags for an int suffix. */
248 unsigned int
249 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
251 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
254 /* Return the string type corresponding to the the input user-defined string
255 literal type. If the input type is not a user-defined string literal
256 type return the input type. */
257 enum cpp_ttype
258 cpp_userdef_string_remove_type (enum cpp_ttype type)
260 if (type == CPP_STRING_USERDEF)
261 return CPP_STRING;
262 else if (type == CPP_WSTRING_USERDEF)
263 return CPP_WSTRING;
264 else if (type == CPP_STRING16_USERDEF)
265 return CPP_STRING16;
266 else if (type == CPP_STRING32_USERDEF)
267 return CPP_STRING32;
268 else if (type == CPP_UTF8STRING_USERDEF)
269 return CPP_UTF8STRING;
270 else
271 return type;
274 /* Return the user-defined string literal type corresponding to the input
275 string type. If the input type is not a string type return the input
276 type. */
277 enum cpp_ttype
278 cpp_userdef_string_add_type (enum cpp_ttype type)
280 if (type == CPP_STRING)
281 return CPP_STRING_USERDEF;
282 else if (type == CPP_WSTRING)
283 return CPP_WSTRING_USERDEF;
284 else if (type == CPP_STRING16)
285 return CPP_STRING16_USERDEF;
286 else if (type == CPP_STRING32)
287 return CPP_STRING32_USERDEF;
288 else if (type == CPP_UTF8STRING)
289 return CPP_UTF8STRING_USERDEF;
290 else
291 return type;
294 /* Return the char type corresponding to the the input user-defined char
295 literal type. If the input type is not a user-defined char literal
296 type return the input type. */
297 enum cpp_ttype
298 cpp_userdef_char_remove_type (enum cpp_ttype type)
300 if (type == CPP_CHAR_USERDEF)
301 return CPP_CHAR;
302 else if (type == CPP_WCHAR_USERDEF)
303 return CPP_WCHAR;
304 else if (type == CPP_CHAR16_USERDEF)
305 return CPP_CHAR16;
306 else if (type == CPP_CHAR32_USERDEF)
307 return CPP_CHAR32;
308 else
309 return type;
312 /* Return the user-defined char literal type corresponding to the input
313 char type. If the input type is not a char type return the input
314 type. */
315 enum cpp_ttype
316 cpp_userdef_char_add_type (enum cpp_ttype type)
318 if (type == CPP_CHAR)
319 return CPP_CHAR_USERDEF;
320 else if (type == CPP_WCHAR)
321 return CPP_WCHAR_USERDEF;
322 else if (type == CPP_CHAR16)
323 return CPP_CHAR16_USERDEF;
324 else if (type == CPP_CHAR32)
325 return CPP_CHAR32_USERDEF;
326 else
327 return type;
330 /* Return true if the token type is a user-defined string literal. */
331 bool
332 cpp_userdef_string_p (enum cpp_ttype type)
334 if (type == CPP_STRING_USERDEF
335 || type == CPP_WSTRING_USERDEF
336 || type == CPP_STRING16_USERDEF
337 || type == CPP_STRING32_USERDEF
338 || type == CPP_UTF8STRING_USERDEF)
339 return true;
340 else
341 return false;
344 /* Return true if the token type is a user-defined char literal. */
345 bool
346 cpp_userdef_char_p (enum cpp_ttype type)
348 if (type == CPP_CHAR_USERDEF
349 || type == CPP_WCHAR_USERDEF
350 || type == CPP_CHAR16_USERDEF
351 || type == CPP_CHAR32_USERDEF)
352 return true;
353 else
354 return false;
357 /* Extract the suffix from a user-defined literal string or char. */
358 const char *
359 cpp_get_userdef_suffix (const cpp_token *tok)
361 unsigned int len = tok->val.str.len;
362 const char *text = (const char *)tok->val.str.text;
363 char delim;
364 unsigned int i;
365 for (i = 0; i < len; ++i)
366 if (text[i] == '\'' || text[i] == '"')
367 break;
368 if (i == len)
369 return text + len;
370 delim = text[i];
371 for (i = len; i > 0; --i)
372 if (text[i - 1] == delim)
373 break;
374 return text + i;
377 /* Categorize numeric constants according to their field (integer,
378 floating point, or invalid), radix (decimal, octal, hexadecimal),
379 and type suffixes.
381 TOKEN is the token that represents the numeric constant to
382 classify.
384 In C++0X if UD_SUFFIX is non null it will be assigned
385 any unrecognized suffix for a user-defined literal.
387 VIRTUAL_LOCATION is the virtual location for TOKEN. */
388 unsigned int
389 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
390 const char **ud_suffix, source_location virtual_location)
392 const uchar *str = token->val.str.text;
393 const uchar *limit;
394 unsigned int max_digit, result, radix;
395 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
396 bool seen_digit;
398 if (ud_suffix)
399 *ud_suffix = NULL;
401 /* If the lexer has done its job, length one can only be a single
402 digit. Fast-path this very common case. */
403 if (token->val.str.len == 1)
404 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
406 limit = str + token->val.str.len;
407 float_flag = NOT_FLOAT;
408 max_digit = 0;
409 radix = 10;
410 seen_digit = false;
412 /* First, interpret the radix. */
413 if (*str == '0')
415 radix = 8;
416 str++;
418 /* Require at least one hex digit to classify it as hex. */
419 if ((*str == 'x' || *str == 'X')
420 && (str[1] == '.' || ISXDIGIT (str[1])))
422 radix = 16;
423 str++;
425 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
427 radix = 2;
428 str++;
432 /* Now scan for a well-formed integer or float. */
433 for (;;)
435 unsigned int c = *str++;
437 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
439 seen_digit = true;
440 c = hex_value (c);
441 if (c > max_digit)
442 max_digit = c;
444 else if (c == '.')
446 if (float_flag == NOT_FLOAT)
447 float_flag = AFTER_POINT;
448 else
449 SYNTAX_ERROR_AT (virtual_location,
450 "too many decimal points in number");
452 else if ((radix <= 10 && (c == 'e' || c == 'E'))
453 || (radix == 16 && (c == 'p' || c == 'P')))
455 float_flag = AFTER_EXPON;
456 break;
458 else
460 /* Start of suffix. */
461 str--;
462 break;
466 /* The suffix may be for decimal fixed-point constants without exponent. */
467 if (radix != 16 && float_flag == NOT_FLOAT)
469 result = interpret_float_suffix (pfile, str, limit - str);
470 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
472 result |= CPP_N_FLOATING;
473 /* We need to restore the radix to 10, if the radix is 8. */
474 if (radix == 8)
475 radix = 10;
477 if (CPP_PEDANTIC (pfile))
478 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
479 "fixed-point constants are a GCC extension");
480 goto syntax_ok;
482 else
483 result = 0;
486 if (float_flag != NOT_FLOAT && radix == 8)
487 radix = 10;
489 if (max_digit >= radix)
491 if (radix == 2)
492 SYNTAX_ERROR2_AT (virtual_location,
493 "invalid digit \"%c\" in binary constant", '0' + max_digit);
494 else
495 SYNTAX_ERROR2_AT (virtual_location,
496 "invalid digit \"%c\" in octal constant", '0' + max_digit);
499 if (float_flag != NOT_FLOAT)
501 if (radix == 2)
503 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
504 "invalid prefix \"0b\" for floating constant");
505 return CPP_N_INVALID;
508 if (radix == 16 && !seen_digit)
509 SYNTAX_ERROR_AT (virtual_location,
510 "no digits in hexadecimal floating constant");
512 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
513 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
514 "use of C99 hexadecimal floating constant");
516 if (float_flag == AFTER_EXPON)
518 if (*str == '+' || *str == '-')
519 str++;
521 /* Exponent is decimal, even if string is a hex float. */
522 if (!ISDIGIT (*str))
523 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
526 str++;
527 while (ISDIGIT (*str));
529 else if (radix == 16)
530 SYNTAX_ERROR_AT (virtual_location,
531 "hexadecimal floating constants require an exponent");
533 result = interpret_float_suffix (pfile, str, limit - str);
534 if (result == 0)
536 if (CPP_OPTION (pfile, user_literals))
538 if (ud_suffix)
539 *ud_suffix = (const char *) str;
540 result = CPP_N_LARGE | CPP_N_USERDEF;
542 else
544 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
545 "invalid suffix \"%.*s\" on floating constant",
546 (int) (limit - str), str);
547 return CPP_N_INVALID;
551 /* Traditional C didn't accept any floating suffixes. */
552 if (limit != str
553 && CPP_WTRADITIONAL (pfile)
554 && ! cpp_sys_macro_p (pfile))
555 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
556 "traditional C rejects the \"%.*s\" suffix",
557 (int) (limit - str), str);
559 /* A suffix for double is a GCC extension via decimal float support.
560 If the suffix also specifies an imaginary value we'll catch that
561 later. */
562 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
563 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
564 "suffix for double constant is a GCC extension");
566 /* Radix must be 10 for decimal floats. */
567 if ((result & CPP_N_DFLOAT) && radix != 10)
569 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
570 "invalid suffix \"%.*s\" with hexadecimal floating constant",
571 (int) (limit - str), str);
572 return CPP_N_INVALID;
575 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
576 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
577 "fixed-point constants are a GCC extension");
579 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
580 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
581 "decimal float constants are a GCC extension");
583 result |= CPP_N_FLOATING;
585 else
587 result = interpret_int_suffix (pfile, str, limit - str);
588 if (result == 0)
590 if (CPP_OPTION (pfile, user_literals))
592 if (ud_suffix)
593 *ud_suffix = (const char *) str;
594 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
596 else
598 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
599 "invalid suffix \"%.*s\" on integer constant",
600 (int) (limit - str), str);
601 return CPP_N_INVALID;
605 /* Traditional C only accepted the 'L' suffix.
606 Suppress warning about 'LL' with -Wno-long-long. */
607 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
609 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
610 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
611 && CPP_OPTION (pfile, cpp_warn_long_long);
613 if (u_or_i || large)
614 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
615 virtual_location, 0,
616 "traditional C rejects the \"%.*s\" suffix",
617 (int) (limit - str), str);
620 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
621 && CPP_OPTION (pfile, cpp_warn_long_long))
623 const char *message = CPP_OPTION (pfile, cplusplus)
624 ? N_("use of C++0x long long integer constant")
625 : N_("use of C99 long long integer constant");
627 if (CPP_OPTION (pfile, c99))
628 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
629 0, message);
630 else
631 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
632 virtual_location, 0, message);
635 result |= CPP_N_INTEGER;
638 syntax_ok:
639 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
640 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
641 "imaginary constants are a GCC extension");
642 if (radix == 2 && CPP_PEDANTIC (pfile))
643 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
644 "binary constants are a GCC extension");
646 if (radix == 10)
647 result |= CPP_N_DECIMAL;
648 else if (radix == 16)
649 result |= CPP_N_HEX;
650 else if (radix == 2)
651 result |= CPP_N_BINARY;
652 else
653 result |= CPP_N_OCTAL;
655 return result;
657 syntax_error:
658 return CPP_N_INVALID;
661 /* cpp_interpret_integer converts an integer constant into a cpp_num,
662 of precision options->precision.
664 We do not provide any interface for decimal->float conversion,
665 because the preprocessor doesn't need it and we don't want to
666 drag in GCC's floating point emulator. */
667 cpp_num
668 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
669 unsigned int type)
671 const uchar *p, *end;
672 cpp_num result;
674 result.low = 0;
675 result.high = 0;
676 result.unsignedp = !!(type & CPP_N_UNSIGNED);
677 result.overflow = false;
679 p = token->val.str.text;
680 end = p + token->val.str.len;
682 /* Common case of a single digit. */
683 if (token->val.str.len == 1)
684 result.low = p[0] - '0';
685 else
687 cpp_num_part max;
688 size_t precision = CPP_OPTION (pfile, precision);
689 unsigned int base = 10, c = 0;
690 bool overflow = false;
692 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
694 base = 8;
695 p++;
697 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
699 base = 16;
700 p += 2;
702 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
704 base = 2;
705 p += 2;
708 /* We can add a digit to numbers strictly less than this without
709 needing the precision and slowness of double integers. */
710 max = ~(cpp_num_part) 0;
711 if (precision < PART_PRECISION)
712 max >>= PART_PRECISION - precision;
713 max = (max - base + 1) / base + 1;
715 for (; p < end; p++)
717 c = *p;
719 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
720 c = hex_value (c);
721 else
722 break;
724 /* Strict inequality for when max is set to zero. */
725 if (result.low < max)
726 result.low = result.low * base + c;
727 else
729 result = append_digit (result, c, base, precision);
730 overflow |= result.overflow;
731 max = 0;
735 if (overflow && !(type & CPP_N_USERDEF))
736 cpp_error (pfile, CPP_DL_PEDWARN,
737 "integer constant is too large for its type");
738 /* If too big to be signed, consider it unsigned. Only warn for
739 decimal numbers. Traditional numbers were always signed (but
740 we still honor an explicit U suffix); but we only have
741 traditional semantics in directives. */
742 else if (!result.unsignedp
743 && !(CPP_OPTION (pfile, traditional)
744 && pfile->state.in_directive)
745 && !num_positive (result, precision))
747 /* This is for constants within the range of uintmax_t but
748 not that of intmax_t. For such decimal constants, a
749 diagnostic is required for C99 as the selected type must
750 be signed and not having a type is a constraint violation
751 (DR#298, TC3), so this must be a pedwarn. For C90,
752 unsigned long is specified to be used for a constant that
753 does not fit in signed long; if uintmax_t has the same
754 range as unsigned long this means only a warning is
755 appropriate here. C90 permits the preprocessor to use a
756 wider range than unsigned long in the compiler, so if
757 uintmax_t is wider than unsigned long no diagnostic is
758 required for such constants in preprocessor #if
759 expressions and the compiler will pedwarn for such
760 constants outside the range of unsigned long that reach
761 the compiler so a diagnostic is not required there
762 either; thus, pedwarn for C99 but use a plain warning for
763 C90. */
764 if (base == 10)
765 cpp_error (pfile, (CPP_OPTION (pfile, c99)
766 ? CPP_DL_PEDWARN
767 : CPP_DL_WARNING),
768 "integer constant is so large that it is unsigned");
769 result.unsignedp = true;
773 return result;
776 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
777 static cpp_num
778 append_digit (cpp_num num, int digit, int base, size_t precision)
780 cpp_num result;
781 unsigned int shift;
782 bool overflow;
783 cpp_num_part add_high, add_low;
785 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
786 need to worry about add_high overflowing. */
787 switch (base)
789 case 2:
790 shift = 1;
791 break;
793 case 16:
794 shift = 4;
795 break;
797 default:
798 shift = 3;
800 overflow = !!(num.high >> (PART_PRECISION - shift));
801 result.high = num.high << shift;
802 result.low = num.low << shift;
803 result.high |= num.low >> (PART_PRECISION - shift);
804 result.unsignedp = num.unsignedp;
806 if (base == 10)
808 add_low = num.low << 1;
809 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
811 else
812 add_high = add_low = 0;
814 if (add_low + digit < add_low)
815 add_high++;
816 add_low += digit;
818 if (result.low + add_low < result.low)
819 add_high++;
820 if (result.high + add_high < result.high)
821 overflow = true;
823 result.low += add_low;
824 result.high += add_high;
825 result.overflow = overflow;
827 /* The above code catches overflow of a cpp_num type. This catches
828 overflow of the (possibly shorter) target precision. */
829 num.low = result.low;
830 num.high = result.high;
831 result = num_trim (result, precision);
832 if (!num_eq (result, num))
833 result.overflow = true;
835 return result;
838 /* Handle meeting "defined" in a preprocessor expression. */
839 static cpp_num
840 parse_defined (cpp_reader *pfile)
842 cpp_num result;
843 int paren = 0;
844 cpp_hashnode *node = 0;
845 const cpp_token *token;
846 cpp_context *initial_context = pfile->context;
848 /* Don't expand macros. */
849 pfile->state.prevent_expansion++;
851 token = cpp_get_token (pfile);
852 if (token->type == CPP_OPEN_PAREN)
854 paren = 1;
855 token = cpp_get_token (pfile);
858 if (token->type == CPP_NAME)
860 node = token->val.node.node;
861 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
863 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
864 node = 0;
867 else
869 cpp_error (pfile, CPP_DL_ERROR,
870 "operator \"defined\" requires an identifier");
871 if (token->flags & NAMED_OP)
873 cpp_token op;
875 op.flags = 0;
876 op.type = token->type;
877 cpp_error (pfile, CPP_DL_ERROR,
878 "(\"%s\" is an alternative token for \"%s\" in C++)",
879 cpp_token_as_text (pfile, token),
880 cpp_token_as_text (pfile, &op));
884 if (node)
886 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
887 cpp_error (pfile, CPP_DL_WARNING,
888 "this use of \"defined\" may not be portable");
890 _cpp_mark_macro_used (node);
891 if (!(node->flags & NODE_USED))
893 node->flags |= NODE_USED;
894 if (node->type == NT_MACRO)
896 if ((node->flags & NODE_BUILTIN)
897 && pfile->cb.user_builtin_macro)
898 pfile->cb.user_builtin_macro (pfile, node);
899 if (pfile->cb.used_define)
900 pfile->cb.used_define (pfile, pfile->directive_line, node);
902 else
904 if (pfile->cb.used_undef)
905 pfile->cb.used_undef (pfile, pfile->directive_line, node);
909 /* A possible controlling macro of the form #if !defined ().
910 _cpp_parse_expr checks there was no other junk on the line. */
911 pfile->mi_ind_cmacro = node;
914 pfile->state.prevent_expansion--;
916 /* Do not treat conditional macros as being defined. This is due to the
917 powerpc and spu ports using conditional macros for 'vector', 'bool', and
918 'pixel' to act as conditional keywords. This messes up tests like #ifndef
919 bool. */
920 result.unsignedp = false;
921 result.high = 0;
922 result.overflow = false;
923 result.low = (node && node->type == NT_MACRO
924 && (node->flags & NODE_CONDITIONAL) == 0);
925 return result;
928 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
929 number or character constant, or the result of the "defined" or "#"
930 operators). */
931 static cpp_num
932 eval_token (cpp_reader *pfile, const cpp_token *token,
933 source_location virtual_location)
935 cpp_num result;
936 unsigned int temp;
937 int unsignedp = 0;
939 result.unsignedp = false;
940 result.overflow = false;
942 switch (token->type)
944 case CPP_NUMBER:
945 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
946 if (temp & CPP_N_USERDEF)
947 cpp_error (pfile, CPP_DL_ERROR,
948 "user-defined literal in preprocessor expression");
949 switch (temp & CPP_N_CATEGORY)
951 case CPP_N_FLOATING:
952 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
953 "floating constant in preprocessor expression");
954 break;
955 case CPP_N_INTEGER:
956 if (!(temp & CPP_N_IMAGINARY))
957 return cpp_interpret_integer (pfile, token, temp);
958 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
959 "imaginary number in preprocessor expression");
960 break;
962 case CPP_N_INVALID:
963 /* Error already issued. */
964 break;
966 result.high = result.low = 0;
967 break;
969 case CPP_WCHAR:
970 case CPP_CHAR:
971 case CPP_CHAR16:
972 case CPP_CHAR32:
974 cppchar_t cc = cpp_interpret_charconst (pfile, token,
975 &temp, &unsignedp);
977 result.high = 0;
978 result.low = cc;
979 /* Sign-extend the result if necessary. */
980 if (!unsignedp && (cppchar_signed_t) cc < 0)
982 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
983 result.low |= ~(~(cpp_num_part) 0
984 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
985 result.high = ~(cpp_num_part) 0;
986 result = num_trim (result, CPP_OPTION (pfile, precision));
989 break;
991 case CPP_NAME:
992 if (token->val.node.node == pfile->spec_nodes.n_defined)
993 return parse_defined (pfile);
994 else if (CPP_OPTION (pfile, cplusplus)
995 && (token->val.node.node == pfile->spec_nodes.n_true
996 || token->val.node.node == pfile->spec_nodes.n_false))
998 result.high = 0;
999 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1001 else
1003 result.high = 0;
1004 result.low = 0;
1005 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1006 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1007 "\"%s\" is not defined",
1008 NODE_NAME (token->val.node.node));
1010 break;
1012 case CPP_HASH:
1013 if (!pfile->state.skipping)
1015 /* A pedantic warning takes precedence over a deprecated
1016 warning here. */
1017 if (CPP_PEDANTIC (pfile))
1018 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1019 virtual_location, 0,
1020 "assertions are a GCC extension");
1021 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1022 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1023 "assertions are a deprecated extension");
1025 _cpp_test_assertion (pfile, &temp);
1026 result.high = 0;
1027 result.low = temp;
1028 break;
1030 default:
1031 abort ();
1034 result.unsignedp = !!unsignedp;
1035 return result;
1038 /* Operator precedence and flags table.
1040 After an operator is returned from the lexer, if it has priority less
1041 than the operator on the top of the stack, we reduce the stack by one
1042 operator and repeat the test. Since equal priorities do not reduce,
1043 this is naturally right-associative.
1045 We handle left-associative operators by decrementing the priority of
1046 just-lexed operators by one, but retaining the priority of operators
1047 already on the stack.
1049 The remaining cases are '(' and ')'. We handle '(' by skipping the
1050 reduction phase completely. ')' is given lower priority than
1051 everything else, including '(', effectively forcing a reduction of the
1052 parenthesized expression. If there is a matching '(', the routine
1053 reduce() exits immediately. If the normal exit route sees a ')', then
1054 there cannot have been a matching '(' and an error message is output.
1056 The parser assumes all shifted operators require a left operand unless
1057 the flag NO_L_OPERAND is set. These semantics are automatic; any
1058 extra semantics need to be handled with operator-specific code. */
1060 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1061 operand changes because of integer promotions. */
1062 #define NO_L_OPERAND (1 << 0)
1063 #define LEFT_ASSOC (1 << 1)
1064 #define CHECK_PROMOTION (1 << 2)
1066 /* Operator to priority map. Must be in the same order as the first
1067 N entries of enum cpp_ttype. */
1068 static const struct cpp_operator
1070 uchar prio;
1071 uchar flags;
1072 } optab[] =
1074 /* EQ */ {0, 0}, /* Shouldn't happen. */
1075 /* NOT */ {16, NO_L_OPERAND},
1076 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1077 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1078 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1079 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1080 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1081 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1082 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1083 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1084 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1085 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
1086 /* RSHIFT */ {13, LEFT_ASSOC},
1087 /* LSHIFT */ {13, LEFT_ASSOC},
1089 /* COMPL */ {16, NO_L_OPERAND},
1090 /* AND_AND */ {6, LEFT_ASSOC},
1091 /* OR_OR */ {5, LEFT_ASSOC},
1092 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1093 However, there are some special cases for these in reduce(). */
1094 /* QUERY */ {4, 0},
1095 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
1096 /* COMMA */ {4, LEFT_ASSOC},
1097 /* OPEN_PAREN */ {1, NO_L_OPERAND},
1098 /* CLOSE_PAREN */ {0, 0},
1099 /* EOF */ {0, 0},
1100 /* EQ_EQ */ {11, LEFT_ASSOC},
1101 /* NOT_EQ */ {11, LEFT_ASSOC},
1102 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1103 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1104 /* UPLUS */ {16, NO_L_OPERAND},
1105 /* UMINUS */ {16, NO_L_OPERAND}
1108 /* Parse and evaluate a C expression, reading from PFILE.
1109 Returns the truth value of the expression.
1111 The implementation is an operator precedence parser, i.e. a
1112 bottom-up parser, using a stack for not-yet-reduced tokens.
1114 The stack base is op_stack, and the current stack pointer is 'top'.
1115 There is a stack element for each operator (only), and the most
1116 recently pushed operator is 'top->op'. An operand (value) is
1117 stored in the 'value' field of the stack element of the operator
1118 that precedes it. */
1119 bool
1120 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1122 struct op *top = pfile->op_stack;
1123 unsigned int lex_count;
1124 bool saw_leading_not, want_value = true;
1125 source_location virtual_location = 0;
1127 pfile->state.skip_eval = 0;
1129 /* Set up detection of #if ! defined(). */
1130 pfile->mi_ind_cmacro = 0;
1131 saw_leading_not = false;
1132 lex_count = 0;
1134 /* Lowest priority operator prevents further reductions. */
1135 top->op = CPP_EOF;
1137 for (;;)
1139 struct op op;
1141 lex_count++;
1142 op.token = cpp_get_token_with_location (pfile, &virtual_location);
1143 op.op = op.token->type;
1144 op.loc = virtual_location;
1146 switch (op.op)
1148 /* These tokens convert into values. */
1149 case CPP_NUMBER:
1150 case CPP_CHAR:
1151 case CPP_WCHAR:
1152 case CPP_CHAR16:
1153 case CPP_CHAR32:
1154 case CPP_NAME:
1155 case CPP_HASH:
1156 if (!want_value)
1157 SYNTAX_ERROR2_AT (op.loc,
1158 "missing binary operator before token \"%s\"",
1159 cpp_token_as_text (pfile, op.token));
1160 want_value = false;
1161 top->value = eval_token (pfile, op.token, op.loc);
1162 continue;
1164 case CPP_NOT:
1165 saw_leading_not = lex_count == 1;
1166 break;
1167 case CPP_PLUS:
1168 if (want_value)
1169 op.op = CPP_UPLUS;
1170 break;
1171 case CPP_MINUS:
1172 if (want_value)
1173 op.op = CPP_UMINUS;
1174 break;
1176 default:
1177 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1178 SYNTAX_ERROR2_AT (op.loc,
1179 "token \"%s\" is not valid in preprocessor expressions",
1180 cpp_token_as_text (pfile, op.token));
1181 break;
1184 /* Check we have a value or operator as appropriate. */
1185 if (optab[op.op].flags & NO_L_OPERAND)
1187 if (!want_value)
1188 SYNTAX_ERROR2_AT (op.loc,
1189 "missing binary operator before token \"%s\"",
1190 cpp_token_as_text (pfile, op.token));
1192 else if (want_value)
1194 /* We want a number (or expression) and haven't got one.
1195 Try to emit a specific diagnostic. */
1196 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1197 SYNTAX_ERROR_AT (op.loc,
1198 "missing expression between '(' and ')'");
1200 if (op.op == CPP_EOF && top->op == CPP_EOF)
1201 SYNTAX_ERROR2_AT (op.loc,
1202 "%s with no expression", is_if ? "#if" : "#elif");
1204 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1205 SYNTAX_ERROR2_AT (op.loc,
1206 "operator '%s' has no right operand",
1207 cpp_token_as_text (pfile, top->token));
1208 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1209 /* Complain about missing paren during reduction. */;
1210 else
1211 SYNTAX_ERROR2_AT (op.loc,
1212 "operator '%s' has no left operand",
1213 cpp_token_as_text (pfile, op.token));
1216 top = reduce (pfile, top, op.op);
1217 if (!top)
1218 goto syntax_error;
1220 if (op.op == CPP_EOF)
1221 break;
1223 switch (op.op)
1225 case CPP_CLOSE_PAREN:
1226 continue;
1227 case CPP_OR_OR:
1228 if (!num_zerop (top->value))
1229 pfile->state.skip_eval++;
1230 break;
1231 case CPP_AND_AND:
1232 case CPP_QUERY:
1233 if (num_zerop (top->value))
1234 pfile->state.skip_eval++;
1235 break;
1236 case CPP_COLON:
1237 if (top->op != CPP_QUERY)
1238 SYNTAX_ERROR_AT (op.loc,
1239 " ':' without preceding '?'");
1240 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
1241 pfile->state.skip_eval++;
1242 else
1243 pfile->state.skip_eval--;
1244 default:
1245 break;
1248 want_value = true;
1250 /* Check for and handle stack overflow. */
1251 if (++top == pfile->op_limit)
1252 top = _cpp_expand_op_stack (pfile);
1254 top->op = op.op;
1255 top->token = op.token;
1256 top->loc = op.loc;
1259 /* The controlling macro expression is only valid if we called lex 3
1260 times: <!> <defined expression> and <EOF>. push_conditional ()
1261 checks that we are at top-of-file. */
1262 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1263 pfile->mi_ind_cmacro = 0;
1265 if (top != pfile->op_stack)
1267 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1268 "unbalanced stack in %s",
1269 is_if ? "#if" : "#elif");
1270 syntax_error:
1271 return false; /* Return false on syntax error. */
1274 return !num_zerop (top->value);
1277 /* Reduce the operator / value stack if possible, in preparation for
1278 pushing operator OP. Returns NULL on error, otherwise the top of
1279 the stack. */
1280 static struct op *
1281 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1283 unsigned int prio;
1285 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1287 bad_op:
1288 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1289 return 0;
1292 if (op == CPP_OPEN_PAREN)
1293 return top;
1295 /* Decrement the priority of left-associative operators to force a
1296 reduction with operators of otherwise equal priority. */
1297 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1298 while (prio < optab[top->op].prio)
1300 if (CPP_OPTION (pfile, warn_num_sign_change)
1301 && optab[top->op].flags & CHECK_PROMOTION)
1302 check_promotion (pfile, top);
1304 switch (top->op)
1306 case CPP_UPLUS:
1307 case CPP_UMINUS:
1308 case CPP_NOT:
1309 case CPP_COMPL:
1310 top[-1].value = num_unary_op (pfile, top->value, top->op);
1311 top[-1].loc = top->loc;
1312 break;
1314 case CPP_PLUS:
1315 case CPP_MINUS:
1316 case CPP_RSHIFT:
1317 case CPP_LSHIFT:
1318 case CPP_COMMA:
1319 top[-1].value = num_binary_op (pfile, top[-1].value,
1320 top->value, top->op);
1321 top[-1].loc = top->loc;
1322 break;
1324 case CPP_GREATER:
1325 case CPP_LESS:
1326 case CPP_GREATER_EQ:
1327 case CPP_LESS_EQ:
1328 top[-1].value
1329 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1330 top[-1].loc = top->loc;
1331 break;
1333 case CPP_EQ_EQ:
1334 case CPP_NOT_EQ:
1335 top[-1].value
1336 = num_equality_op (pfile, top[-1].value, top->value, top->op);
1337 top[-1].loc = top->loc;
1338 break;
1340 case CPP_AND:
1341 case CPP_OR:
1342 case CPP_XOR:
1343 top[-1].value
1344 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1345 top[-1].loc = top->loc;
1346 break;
1348 case CPP_MULT:
1349 top[-1].value = num_mul (pfile, top[-1].value, top->value);
1350 top[-1].loc = top->loc;
1351 break;
1353 case CPP_DIV:
1354 case CPP_MOD:
1355 top[-1].value = num_div_op (pfile, top[-1].value,
1356 top->value, top->op, top->loc);
1357 top[-1].loc = top->loc;
1358 break;
1360 case CPP_OR_OR:
1361 top--;
1362 if (!num_zerop (top->value))
1363 pfile->state.skip_eval--;
1364 top->value.low = (!num_zerop (top->value)
1365 || !num_zerop (top[1].value));
1366 top->value.high = 0;
1367 top->value.unsignedp = false;
1368 top->value.overflow = false;
1369 top->loc = top[1].loc;
1370 continue;
1372 case CPP_AND_AND:
1373 top--;
1374 if (num_zerop (top->value))
1375 pfile->state.skip_eval--;
1376 top->value.low = (!num_zerop (top->value)
1377 && !num_zerop (top[1].value));
1378 top->value.high = 0;
1379 top->value.unsignedp = false;
1380 top->value.overflow = false;
1381 top->loc = top[1].loc;
1382 continue;
1384 case CPP_OPEN_PAREN:
1385 if (op != CPP_CLOSE_PAREN)
1387 cpp_error_with_line (pfile, CPP_DL_ERROR,
1388 top->token->src_loc,
1389 0, "missing ')' in expression");
1390 return 0;
1392 top--;
1393 top->value = top[1].value;
1394 top->loc = top[1].loc;
1395 return top;
1397 case CPP_COLON:
1398 top -= 2;
1399 if (!num_zerop (top->value))
1401 pfile->state.skip_eval--;
1402 top->value = top[1].value;
1403 top->loc = top[1].loc;
1405 else
1407 top->value = top[2].value;
1408 top->loc = top[2].loc;
1410 top->value.unsignedp = (top[1].value.unsignedp
1411 || top[2].value.unsignedp);
1412 continue;
1414 case CPP_QUERY:
1415 /* COMMA and COLON should not reduce a QUERY operator. */
1416 if (op == CPP_COMMA || op == CPP_COLON)
1417 return top;
1418 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1419 return 0;
1421 default:
1422 goto bad_op;
1425 top--;
1426 if (top->value.overflow && !pfile->state.skip_eval)
1427 cpp_error (pfile, CPP_DL_PEDWARN,
1428 "integer overflow in preprocessor expression");
1431 if (op == CPP_CLOSE_PAREN)
1433 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1434 return 0;
1437 return top;
1440 /* Returns the position of the old top of stack after expansion. */
1441 struct op *
1442 _cpp_expand_op_stack (cpp_reader *pfile)
1444 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1445 size_t new_size = old_size * 2 + 20;
1447 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1448 pfile->op_limit = pfile->op_stack + new_size;
1450 return pfile->op_stack + old_size;
1453 /* Emits a warning if the effective sign of either operand of OP
1454 changes because of integer promotions. */
1455 static void
1456 check_promotion (cpp_reader *pfile, const struct op *op)
1458 if (op->value.unsignedp == op[-1].value.unsignedp)
1459 return;
1461 if (op->value.unsignedp)
1463 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1464 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1465 "the left operand of \"%s\" changes sign when promoted",
1466 cpp_token_as_text (pfile, op->token));
1468 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1469 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1470 "the right operand of \"%s\" changes sign when promoted",
1471 cpp_token_as_text (pfile, op->token));
1474 /* Clears the unused high order bits of the number pointed to by PNUM. */
1475 static cpp_num
1476 num_trim (cpp_num num, size_t precision)
1478 if (precision > PART_PRECISION)
1480 precision -= PART_PRECISION;
1481 if (precision < PART_PRECISION)
1482 num.high &= ((cpp_num_part) 1 << precision) - 1;
1484 else
1486 if (precision < PART_PRECISION)
1487 num.low &= ((cpp_num_part) 1 << precision) - 1;
1488 num.high = 0;
1491 return num;
1494 /* True iff A (presumed signed) >= 0. */
1495 static bool
1496 num_positive (cpp_num num, size_t precision)
1498 if (precision > PART_PRECISION)
1500 precision -= PART_PRECISION;
1501 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1504 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1507 /* Sign extend a number, with PRECISION significant bits and all
1508 others assumed clear, to fill out a cpp_num structure. */
1509 cpp_num
1510 cpp_num_sign_extend (cpp_num num, size_t precision)
1512 if (!num.unsignedp)
1514 if (precision > PART_PRECISION)
1516 precision -= PART_PRECISION;
1517 if (precision < PART_PRECISION
1518 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1519 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1521 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1523 if (precision < PART_PRECISION)
1524 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1525 num.high = ~(cpp_num_part) 0;
1529 return num;
1532 /* Returns the negative of NUM. */
1533 static cpp_num
1534 num_negate (cpp_num num, size_t precision)
1536 cpp_num copy;
1538 copy = num;
1539 num.high = ~num.high;
1540 num.low = ~num.low;
1541 if (++num.low == 0)
1542 num.high++;
1543 num = num_trim (num, precision);
1544 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1546 return num;
1549 /* Returns true if A >= B. */
1550 static bool
1551 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1553 bool unsignedp;
1555 unsignedp = pa.unsignedp || pb.unsignedp;
1557 if (!unsignedp)
1559 /* Both numbers have signed type. If they are of different
1560 sign, the answer is the sign of A. */
1561 unsignedp = num_positive (pa, precision);
1563 if (unsignedp != num_positive (pb, precision))
1564 return unsignedp;
1566 /* Otherwise we can do an unsigned comparison. */
1569 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1572 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1573 static cpp_num
1574 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1575 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1577 lhs.overflow = false;
1578 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1580 /* As excess precision is zeroed, there is no need to num_trim () as
1581 these operations cannot introduce a set bit there. */
1582 if (op == CPP_AND)
1584 lhs.low &= rhs.low;
1585 lhs.high &= rhs.high;
1587 else if (op == CPP_OR)
1589 lhs.low |= rhs.low;
1590 lhs.high |= rhs.high;
1592 else
1594 lhs.low ^= rhs.low;
1595 lhs.high ^= rhs.high;
1598 return lhs;
1601 /* Returns LHS OP RHS, where OP is an inequality. */
1602 static cpp_num
1603 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1604 enum cpp_ttype op)
1606 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1608 if (op == CPP_GREATER_EQ)
1609 lhs.low = gte;
1610 else if (op == CPP_LESS)
1611 lhs.low = !gte;
1612 else if (op == CPP_GREATER)
1613 lhs.low = gte && !num_eq (lhs, rhs);
1614 else /* CPP_LESS_EQ. */
1615 lhs.low = !gte || num_eq (lhs, rhs);
1617 lhs.high = 0;
1618 lhs.overflow = false;
1619 lhs.unsignedp = false;
1620 return lhs;
1623 /* Returns LHS OP RHS, where OP is == or !=. */
1624 static cpp_num
1625 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1626 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1628 /* Work around a 3.0.4 bug; see PR 6950. */
1629 bool eq = num_eq (lhs, rhs);
1630 if (op == CPP_NOT_EQ)
1631 eq = !eq;
1632 lhs.low = eq;
1633 lhs.high = 0;
1634 lhs.overflow = false;
1635 lhs.unsignedp = false;
1636 return lhs;
1639 /* Shift NUM, of width PRECISION, right by N bits. */
1640 static cpp_num
1641 num_rshift (cpp_num num, size_t precision, size_t n)
1643 cpp_num_part sign_mask;
1644 bool x = num_positive (num, precision);
1646 if (num.unsignedp || x)
1647 sign_mask = 0;
1648 else
1649 sign_mask = ~(cpp_num_part) 0;
1651 if (n >= precision)
1652 num.high = num.low = sign_mask;
1653 else
1655 /* Sign-extend. */
1656 if (precision < PART_PRECISION)
1657 num.high = sign_mask, num.low |= sign_mask << precision;
1658 else if (precision < 2 * PART_PRECISION)
1659 num.high |= sign_mask << (precision - PART_PRECISION);
1661 if (n >= PART_PRECISION)
1663 n -= PART_PRECISION;
1664 num.low = num.high;
1665 num.high = sign_mask;
1668 if (n)
1670 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1671 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1675 num = num_trim (num, precision);
1676 num.overflow = false;
1677 return num;
1680 /* Shift NUM, of width PRECISION, left by N bits. */
1681 static cpp_num
1682 num_lshift (cpp_num num, size_t precision, size_t n)
1684 if (n >= precision)
1686 num.overflow = !num.unsignedp && !num_zerop (num);
1687 num.high = num.low = 0;
1689 else
1691 cpp_num orig, maybe_orig;
1692 size_t m = n;
1694 orig = num;
1695 if (m >= PART_PRECISION)
1697 m -= PART_PRECISION;
1698 num.high = num.low;
1699 num.low = 0;
1701 if (m)
1703 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1704 num.low <<= m;
1706 num = num_trim (num, precision);
1708 if (num.unsignedp)
1709 num.overflow = false;
1710 else
1712 maybe_orig = num_rshift (num, precision, n);
1713 num.overflow = !num_eq (orig, maybe_orig);
1717 return num;
1720 /* The four unary operators: +, -, ! and ~. */
1721 static cpp_num
1722 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1724 switch (op)
1726 case CPP_UPLUS:
1727 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1728 cpp_warning (pfile, CPP_W_TRADITIONAL,
1729 "traditional C rejects the unary plus operator");
1730 num.overflow = false;
1731 break;
1733 case CPP_UMINUS:
1734 num = num_negate (num, CPP_OPTION (pfile, precision));
1735 break;
1737 case CPP_COMPL:
1738 num.high = ~num.high;
1739 num.low = ~num.low;
1740 num = num_trim (num, CPP_OPTION (pfile, precision));
1741 num.overflow = false;
1742 break;
1744 default: /* case CPP_NOT: */
1745 num.low = num_zerop (num);
1746 num.high = 0;
1747 num.overflow = false;
1748 num.unsignedp = false;
1749 break;
1752 return num;
1755 /* The various binary operators. */
1756 static cpp_num
1757 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1759 cpp_num result;
1760 size_t precision = CPP_OPTION (pfile, precision);
1761 size_t n;
1763 switch (op)
1765 /* Shifts. */
1766 case CPP_LSHIFT:
1767 case CPP_RSHIFT:
1768 if (!rhs.unsignedp && !num_positive (rhs, precision))
1770 /* A negative shift is a positive shift the other way. */
1771 if (op == CPP_LSHIFT)
1772 op = CPP_RSHIFT;
1773 else
1774 op = CPP_LSHIFT;
1775 rhs = num_negate (rhs, precision);
1777 if (rhs.high)
1778 n = ~0; /* Maximal. */
1779 else
1780 n = rhs.low;
1781 if (op == CPP_LSHIFT)
1782 lhs = num_lshift (lhs, precision, n);
1783 else
1784 lhs = num_rshift (lhs, precision, n);
1785 break;
1787 /* Arithmetic. */
1788 case CPP_MINUS:
1789 rhs = num_negate (rhs, precision);
1790 case CPP_PLUS:
1791 result.low = lhs.low + rhs.low;
1792 result.high = lhs.high + rhs.high;
1793 if (result.low < lhs.low)
1794 result.high++;
1795 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1796 result.overflow = false;
1798 result = num_trim (result, precision);
1799 if (!result.unsignedp)
1801 bool lhsp = num_positive (lhs, precision);
1802 result.overflow = (lhsp == num_positive (rhs, precision)
1803 && lhsp != num_positive (result, precision));
1805 return result;
1807 /* Comma. */
1808 default: /* case CPP_COMMA: */
1809 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1810 || !pfile->state.skip_eval))
1811 cpp_error (pfile, CPP_DL_PEDWARN,
1812 "comma operator in operand of #if");
1813 lhs = rhs;
1814 break;
1817 return lhs;
1820 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1821 cannot overflow. */
1822 static cpp_num
1823 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
1825 cpp_num result;
1826 cpp_num_part middle[2], temp;
1828 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1829 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1831 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1832 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1834 temp = result.low;
1835 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1836 if (result.low < temp)
1837 result.high++;
1839 temp = result.low;
1840 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1841 if (result.low < temp)
1842 result.high++;
1844 result.high += HIGH_PART (middle[0]);
1845 result.high += HIGH_PART (middle[1]);
1846 result.unsignedp = true;
1847 result.overflow = false;
1849 return result;
1852 /* Multiply two preprocessing numbers. */
1853 static cpp_num
1854 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
1856 cpp_num result, temp;
1857 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1858 bool overflow, negate = false;
1859 size_t precision = CPP_OPTION (pfile, precision);
1861 /* Prepare for unsigned multiplication. */
1862 if (!unsignedp)
1864 if (!num_positive (lhs, precision))
1865 negate = !negate, lhs = num_negate (lhs, precision);
1866 if (!num_positive (rhs, precision))
1867 negate = !negate, rhs = num_negate (rhs, precision);
1870 overflow = lhs.high && rhs.high;
1871 result = num_part_mul (lhs.low, rhs.low);
1873 temp = num_part_mul (lhs.high, rhs.low);
1874 result.high += temp.low;
1875 if (temp.high)
1876 overflow = true;
1878 temp = num_part_mul (lhs.low, rhs.high);
1879 result.high += temp.low;
1880 if (temp.high)
1881 overflow = true;
1883 temp.low = result.low, temp.high = result.high;
1884 result = num_trim (result, precision);
1885 if (!num_eq (result, temp))
1886 overflow = true;
1888 if (negate)
1889 result = num_negate (result, precision);
1891 if (unsignedp)
1892 result.overflow = false;
1893 else
1894 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1895 && !num_zerop (result));
1896 result.unsignedp = unsignedp;
1898 return result;
1901 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1902 or the remainder depending upon OP. LOCATION is the source location
1903 of this operator (for diagnostics). */
1905 static cpp_num
1906 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1907 source_location location)
1909 cpp_num result, sub;
1910 cpp_num_part mask;
1911 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1912 bool negate = false, lhs_neg = false;
1913 size_t i, precision = CPP_OPTION (pfile, precision);
1915 /* Prepare for unsigned division. */
1916 if (!unsignedp)
1918 if (!num_positive (lhs, precision))
1919 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1920 if (!num_positive (rhs, precision))
1921 negate = !negate, rhs = num_negate (rhs, precision);
1924 /* Find the high bit. */
1925 if (rhs.high)
1927 i = precision - 1;
1928 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1929 for (; ; i--, mask >>= 1)
1930 if (rhs.high & mask)
1931 break;
1933 else if (rhs.low)
1935 if (precision > PART_PRECISION)
1936 i = precision - PART_PRECISION - 1;
1937 else
1938 i = precision - 1;
1939 mask = (cpp_num_part) 1 << i;
1940 for (; ; i--, mask >>= 1)
1941 if (rhs.low & mask)
1942 break;
1944 else
1946 if (!pfile->state.skip_eval)
1947 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1948 "division by zero in #if");
1949 return lhs;
1952 /* First nonzero bit of RHS is bit I. Do naive division by
1953 shifting the RHS fully left, and subtracting from LHS if LHS is
1954 at least as big, and then repeating but with one less shift.
1955 This is not very efficient, but is easy to understand. */
1957 rhs.unsignedp = true;
1958 lhs.unsignedp = true;
1959 i = precision - i - 1;
1960 sub = num_lshift (rhs, precision, i);
1962 result.high = result.low = 0;
1963 for (;;)
1965 if (num_greater_eq (lhs, sub, precision))
1967 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1968 if (i >= PART_PRECISION)
1969 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1970 else
1971 result.low |= (cpp_num_part) 1 << i;
1973 if (i-- == 0)
1974 break;
1975 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1976 sub.high >>= 1;
1979 /* We divide so that the remainder has the sign of the LHS. */
1980 if (op == CPP_DIV)
1982 result.unsignedp = unsignedp;
1983 result.overflow = false;
1984 if (!unsignedp)
1986 if (negate)
1987 result = num_negate (result, precision);
1988 result.overflow = (num_positive (result, precision) ^ !negate
1989 && !num_zerop (result));
1992 return result;
1995 /* CPP_MOD. */
1996 lhs.unsignedp = unsignedp;
1997 lhs.overflow = false;
1998 if (lhs_neg)
1999 lhs = num_negate (lhs, precision);
2001 return lhs;