xref: /dragonfly/contrib/gcc-8.0/libcpp/expr.c (revision e6d22e9b)
1 /* Parse C expressions for cpplib.
2    Copyright (C) 1987-2018 Free Software Foundation, Inc.
3    Contributed by Per Bothner, 1994.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "internal.h"
23 
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))
28 
29 struct op
30 {
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;
35 };
36 
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);
44 
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);
58 
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 *);
66 
67 static cpp_num parse_has_include (cpp_reader *, enum include_type);
68 
69 /* Token type abuse to create unary plus and minus operators.  */
70 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
71 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
72 
73 /* With -O2, gcc appears to produce nice code, moving the error
74    message load and subsequent jump completely out of the main path.  */
75 #define SYNTAX_ERROR(msgid) \
76   do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78   do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
79   while(0)
80 #define SYNTAX_ERROR_AT(loc, msgid) \
81   do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
82   while(0)
83 #define SYNTAX_ERROR2_AT(loc, msgid, arg)					\
84   do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
85   while(0)
86 
87 /* Subroutine of cpp_classify_number.  S points to a float suffix of
88    length LEN, possibly zero.  Returns 0 for an invalid suffix, or a
89    flag vector (of CPP_N_* bits) describing the suffix.  */
90 static unsigned int
91 interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
92 {
93   size_t orig_len = len;
94   const uchar *orig_s = s;
95   size_t flags;
96   size_t f, d, l, w, q, i, fn, fnx, fn_bits;
97 
98   flags = 0;
99   f = d = l = w = q = i = fn = fnx = fn_bits = 0;
100 
101   /* The following decimal float suffixes, from TR 24732:2009 and TS
102      18661-2:2015, are supported:
103 
104      df, DF - _Decimal32.
105      dd, DD - _Decimal64.
106      dl, DL - _Decimal128.
107 
108      The dN and DN suffixes for _DecimalN, and dNx and DNx for
109      _DecimalNx, defined in TS 18661-3:2015, are not supported.
110 
111      Fixed-point suffixes, from TR 18037:2008, are supported.  They
112      consist of three parts, in order:
113 
114      (i) An optional u or U, for unsigned types.
115 
116      (ii) An optional h or H, for short types, or l or L, for long
117      types, or ll or LL, for long long types.  Use of ll or LL is a
118      GNU extension.
119 
120      (iii) r or R, for _Fract types, or k or K, for _Accum types.
121 
122      Otherwise the suffix is for a binary or standard floating-point
123      type.  Such a suffix, or the absence of a suffix, may be preceded
124      or followed by i, I, j or J, to indicate an imaginary number with
125      the corresponding complex type.  The following suffixes for
126      binary or standard floating-point types are supported:
127 
128      f, F - float (ISO C and C++).
129      l, L - long double (ISO C and C++).
130      d, D - double, even with the FLOAT_CONST_DECIMAL64 pragma in
131 	    operation (from TR 24732:2009; the pragma and the suffix
132 	    are not included in TS 18661-2:2015).
133      w, W - machine-specific type such as __float80 (GNU extension).
134      q, Q - machine-specific type such as __float128 (GNU extension).
135      fN, FN - _FloatN (TS 18661-3:2015).
136      fNx, FNx - _FloatNx (TS 18661-3:2015).  */
137 
138   /* Process decimal float suffixes, which are two letters starting
139      with d or D.  Order and case are significant.  */
140   if (len == 2 && (*s == 'd' || *s == 'D'))
141     {
142       bool uppercase = (*s == 'D');
143       switch (s[1])
144       {
145       case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
146       case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
147       case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
148       case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
149       case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
150       case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
151       default:
152 	/* Additional two-character suffixes beginning with D are not
153 	   for decimal float constants.  */
154 	break;
155       }
156     }
157 
158   if (CPP_OPTION (pfile, ext_numeric_literals))
159     {
160       /* Recognize a fixed-point suffix.  */
161       if (len != 0)
162 	switch (s[len-1])
163 	  {
164 	  case 'k': case 'K': flags = CPP_N_ACCUM; break;
165 	  case 'r': case 'R': flags = CPP_N_FRACT; break;
166 	  default: break;
167 	  }
168 
169       /* Continue processing a fixed-point suffix.  The suffix is case
170 	 insensitive except for ll or LL.  Order is significant.  */
171       if (flags)
172 	{
173 	  if (len == 1)
174 	    return flags;
175 	  len--;
176 
177 	  if (*s == 'u' || *s == 'U')
178 	    {
179 	      flags |= CPP_N_UNSIGNED;
180 	      if (len == 1)
181 		return flags;
182 	      len--;
183 	      s++;
184             }
185 
186 	  switch (*s)
187 	  {
188 	  case 'h': case 'H':
189 	    if (len == 1)
190 	      return flags |= CPP_N_SMALL;
191 	    break;
192 	  case 'l':
193 	    if (len == 1)
194 	      return flags |= CPP_N_MEDIUM;
195 	    if (len == 2 && s[1] == 'l')
196 	      return flags |= CPP_N_LARGE;
197 	    break;
198 	  case 'L':
199 	    if (len == 1)
200 	      return flags |= CPP_N_MEDIUM;
201 	    if (len == 2 && s[1] == 'L')
202 	      return flags |= CPP_N_LARGE;
203 	    break;
204 	  default:
205 	    break;
206 	  }
207 	  /* Anything left at this point is invalid.  */
208 	  return 0;
209 	}
210     }
211 
212   /* In any remaining valid suffix, the case and order don't matter.  */
213   while (len--)
214     {
215       switch (s[0])
216 	{
217 	case 'f': case 'F':
218 	  f++;
219 	  if (len > 0
220 	      && !CPP_OPTION (pfile, cplusplus)
221 	      && s[1] >= '1'
222 	      && s[1] <= '9'
223 	      && fn_bits == 0)
224 	    {
225 	      f--;
226 	      while (len > 0
227 		     && s[1] >= '0'
228 		     && s[1] <= '9'
229 		     && fn_bits < CPP_FLOATN_MAX)
230 		{
231 		  fn_bits = fn_bits * 10 + (s[1] - '0');
232 		  len--;
233 		  s++;
234 		}
235 	      if (len > 0 && s[1] == 'x')
236 		{
237 		  fnx++;
238 		  len--;
239 		  s++;
240 		}
241 	      else
242 		fn++;
243 	    }
244 	  break;
245 	case 'd': case 'D': d++; break;
246 	case 'l': case 'L': l++; break;
247 	case 'w': case 'W': w++; break;
248 	case 'q': case 'Q': q++; break;
249 	case 'i': case 'I':
250 	case 'j': case 'J': i++; break;
251 	default:
252 	  return 0;
253 	}
254       s++;
255     }
256 
257   /* Reject any case of multiple suffixes specifying types, multiple
258      suffixes specifying an imaginary constant, _FloatN or _FloatNx
259      suffixes for invalid values of N, and _FloatN suffixes for values
260      of N larger than can be represented in the return value.  The
261      caller is responsible for rejecting _FloatN suffixes where
262      _FloatN is not supported on the chosen target.  */
263   if (f + d + l + w + q + fn + fnx > 1 || i > 1)
264     return 0;
265   if (fn_bits > CPP_FLOATN_MAX)
266     return 0;
267   if (fnx && fn_bits != 32 && fn_bits != 64 && fn_bits != 128)
268     return 0;
269   if (fn && fn_bits != 16 && fn_bits % 32 != 0)
270     return 0;
271   if (fn && fn_bits == 96)
272     return 0;
273 
274   if (i)
275     {
276       if (!CPP_OPTION (pfile, ext_numeric_literals))
277 	return 0;
278 
279       /* In C++14 and up these suffixes are in the standard library, so treat
280 	 them as user-defined literals.  */
281       if (CPP_OPTION (pfile, cplusplus)
282 	  && CPP_OPTION (pfile, lang) > CLK_CXX11
283 	  && orig_s[0] == 'i'
284 	  && (orig_len == 1
285 	      || (orig_len == 2
286 		  && (orig_s[1] == 'f' || orig_s[1] == 'l'))))
287 	return 0;
288     }
289 
290   if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
291     return 0;
292 
293   return ((i ? CPP_N_IMAGINARY : 0)
294 	  | (f ? CPP_N_SMALL :
295 	     d ? CPP_N_MEDIUM :
296 	     l ? CPP_N_LARGE :
297 	     w ? CPP_N_MD_W :
298 	     q ? CPP_N_MD_Q :
299 	     fn ? CPP_N_FLOATN | (fn_bits << CPP_FLOATN_SHIFT) :
300 	     fnx ? CPP_N_FLOATNX | (fn_bits << CPP_FLOATN_SHIFT) :
301 	     CPP_N_DEFAULT));
302 }
303 
304 /* Return the classification flags for a float suffix.  */
305 unsigned int
306 cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
307 {
308   return interpret_float_suffix (pfile, (const unsigned char *)s, len);
309 }
310 
311 /* Subroutine of cpp_classify_number.  S points to an integer suffix
312    of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
313    flag vector describing the suffix.  */
314 static unsigned int
315 interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
316 {
317   size_t orig_len = len;
318   size_t u, l, i;
319 
320   u = l = i = 0;
321 
322   while (len--)
323     switch (s[len])
324       {
325       case 'u': case 'U':	u++; break;
326       case 'i': case 'I':
327       case 'j': case 'J':	i++; break;
328       case 'l': case 'L':	l++;
329 	/* If there are two Ls, they must be adjacent and the same case.  */
330 	if (l == 2 && s[len] != s[len + 1])
331 	  return 0;
332 	break;
333       default:
334 	return 0;
335       }
336 
337   if (l > 2 || u > 1 || i > 1)
338     return 0;
339 
340   if (i)
341     {
342       if (!CPP_OPTION (pfile, ext_numeric_literals))
343 	return 0;
344 
345       /* In C++14 and up these suffixes are in the standard library, so treat
346 	 them as user-defined literals.  */
347       if (CPP_OPTION (pfile, cplusplus)
348 	  && CPP_OPTION (pfile, lang) > CLK_CXX11
349 	  && s[0] == 'i'
350 	  && (orig_len == 1 || (orig_len == 2 && s[1] == 'l')))
351 	return 0;
352     }
353 
354   return ((i ? CPP_N_IMAGINARY : 0)
355 	  | (u ? CPP_N_UNSIGNED : 0)
356 	  | ((l == 0) ? CPP_N_SMALL
357 	     : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
358 }
359 
360 /* Return the classification flags for an int suffix.  */
361 unsigned int
362 cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
363 {
364   return interpret_int_suffix (pfile, (const unsigned char *)s, len);
365 }
366 
367 /* Return the string type corresponding to the the input user-defined string
368    literal type.  If the input type is not a user-defined string literal
369    type return the input type.  */
370 enum cpp_ttype
371 cpp_userdef_string_remove_type (enum cpp_ttype type)
372 {
373   if (type == CPP_STRING_USERDEF)
374     return CPP_STRING;
375   else if (type == CPP_WSTRING_USERDEF)
376     return CPP_WSTRING;
377   else if (type == CPP_STRING16_USERDEF)
378     return CPP_STRING16;
379   else if (type == CPP_STRING32_USERDEF)
380     return CPP_STRING32;
381   else if (type == CPP_UTF8STRING_USERDEF)
382     return CPP_UTF8STRING;
383   else
384     return type;
385 }
386 
387 /* Return the user-defined string literal type corresponding to the input
388    string type.  If the input type is not a string type return the input
389    type.  */
390 enum cpp_ttype
391 cpp_userdef_string_add_type (enum cpp_ttype type)
392 {
393   if (type == CPP_STRING)
394     return CPP_STRING_USERDEF;
395   else if (type == CPP_WSTRING)
396     return CPP_WSTRING_USERDEF;
397   else if (type == CPP_STRING16)
398     return CPP_STRING16_USERDEF;
399   else if (type == CPP_STRING32)
400     return CPP_STRING32_USERDEF;
401   else if (type == CPP_UTF8STRING)
402     return CPP_UTF8STRING_USERDEF;
403   else
404     return type;
405 }
406 
407 /* Return the char type corresponding to the the input user-defined char
408    literal type.  If the input type is not a user-defined char literal
409    type return the input type.  */
410 enum cpp_ttype
411 cpp_userdef_char_remove_type (enum cpp_ttype type)
412 {
413   if (type == CPP_CHAR_USERDEF)
414     return CPP_CHAR;
415   else if (type == CPP_WCHAR_USERDEF)
416     return CPP_WCHAR;
417   else if (type == CPP_CHAR16_USERDEF)
418     return CPP_CHAR16;
419   else if (type == CPP_CHAR32_USERDEF)
420     return CPP_CHAR32;
421   else if (type == CPP_UTF8CHAR_USERDEF)
422     return CPP_UTF8CHAR;
423   else
424     return type;
425 }
426 
427 /* Return the user-defined char literal type corresponding to the input
428    char type.  If the input type is not a char type return the input
429    type.  */
430 enum cpp_ttype
431 cpp_userdef_char_add_type (enum cpp_ttype type)
432 {
433   if (type == CPP_CHAR)
434     return CPP_CHAR_USERDEF;
435   else if (type == CPP_WCHAR)
436     return CPP_WCHAR_USERDEF;
437   else if (type == CPP_CHAR16)
438     return CPP_CHAR16_USERDEF;
439   else if (type == CPP_CHAR32)
440     return CPP_CHAR32_USERDEF;
441   else if (type == CPP_UTF8CHAR)
442     return CPP_UTF8CHAR_USERDEF;
443   else
444     return type;
445 }
446 
447 /* Return true if the token type is a user-defined string literal.  */
448 bool
449 cpp_userdef_string_p (enum cpp_ttype type)
450 {
451   if (type == CPP_STRING_USERDEF
452    || type == CPP_WSTRING_USERDEF
453    || type == CPP_STRING16_USERDEF
454    || type == CPP_STRING32_USERDEF
455    || type == CPP_UTF8STRING_USERDEF)
456     return true;
457   else
458     return false;
459 }
460 
461 /* Return true if the token type is a user-defined char literal.  */
462 bool
463 cpp_userdef_char_p (enum cpp_ttype type)
464 {
465   if (type == CPP_CHAR_USERDEF
466    || type == CPP_WCHAR_USERDEF
467    || type == CPP_CHAR16_USERDEF
468    || type == CPP_CHAR32_USERDEF
469    || type == CPP_UTF8CHAR_USERDEF)
470     return true;
471   else
472     return false;
473 }
474 
475 /* Extract the suffix from a user-defined literal string or char.  */
476 const char *
477 cpp_get_userdef_suffix (const cpp_token *tok)
478 {
479   unsigned int len = tok->val.str.len;
480   const char *text = (const char *)tok->val.str.text;
481   char delim;
482   unsigned int i;
483   for (i = 0; i < len; ++i)
484     if (text[i] == '\'' || text[i] == '"')
485       break;
486   if (i == len)
487     return text + len;
488   delim = text[i];
489   for (i = len; i > 0; --i)
490     if (text[i - 1] == delim)
491       break;
492   return text + i;
493 }
494 
495 /* Categorize numeric constants according to their field (integer,
496    floating point, or invalid), radix (decimal, octal, hexadecimal),
497    and type suffixes.
498 
499    TOKEN is the token that represents the numeric constant to
500    classify.
501 
502    In C++0X if UD_SUFFIX is non null it will be assigned
503    any unrecognized suffix for a user-defined literal.
504 
505    VIRTUAL_LOCATION is the virtual location for TOKEN.  */
506 unsigned int
507 cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
508 		     const char **ud_suffix, source_location virtual_location)
509 {
510   const uchar *str = token->val.str.text;
511   const uchar *limit;
512   unsigned int max_digit, result, radix;
513   enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
514   bool seen_digit;
515   bool seen_digit_sep;
516 
517   if (ud_suffix)
518     *ud_suffix = NULL;
519 
520   /* If the lexer has done its job, length one can only be a single
521      digit.  Fast-path this very common case.  */
522   if (token->val.str.len == 1)
523     return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
524 
525   limit = str + token->val.str.len;
526   float_flag = NOT_FLOAT;
527   max_digit = 0;
528   radix = 10;
529   seen_digit = false;
530   seen_digit_sep = false;
531 
532   /* First, interpret the radix.  */
533   if (*str == '0')
534     {
535       radix = 8;
536       str++;
537 
538       /* Require at least one hex digit to classify it as hex.  */
539       if (*str == 'x' || *str == 'X')
540 	{
541 	  if (str[1] == '.' || ISXDIGIT (str[1]))
542 	    {
543 	      radix = 16;
544 	      str++;
545 	    }
546 	  else if (DIGIT_SEP (str[1]))
547 	    SYNTAX_ERROR_AT (virtual_location,
548 			     "digit separator after base indicator");
549 	}
550       else if (*str == 'b' || *str == 'B')
551 	{
552 	  if (str[1] == '0' || str[1] == '1')
553 	    {
554 	      radix = 2;
555 	      str++;
556 	    }
557 	  else if (DIGIT_SEP (str[1]))
558 	    SYNTAX_ERROR_AT (virtual_location,
559 			     "digit separator after base indicator");
560 	}
561     }
562 
563   /* Now scan for a well-formed integer or float.  */
564   for (;;)
565     {
566       unsigned int c = *str++;
567 
568       if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
569 	{
570 	  seen_digit_sep = false;
571 	  seen_digit = true;
572 	  c = hex_value (c);
573 	  if (c > max_digit)
574 	    max_digit = c;
575 	}
576       else if (DIGIT_SEP (c))
577 	{
578 	  if (seen_digit_sep)
579 	    SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
580 	  seen_digit_sep = true;
581 	}
582       else if (c == '.')
583 	{
584 	  if (seen_digit_sep || DIGIT_SEP (*str))
585 	    SYNTAX_ERROR_AT (virtual_location,
586 			     "digit separator adjacent to decimal point");
587 	  seen_digit_sep = false;
588 	  if (float_flag == NOT_FLOAT)
589 	    float_flag = AFTER_POINT;
590 	  else
591 	    SYNTAX_ERROR_AT (virtual_location,
592 			     "too many decimal points in number");
593 	}
594       else if ((radix <= 10 && (c == 'e' || c == 'E'))
595 	       || (radix == 16 && (c == 'p' || c == 'P')))
596 	{
597 	  if (seen_digit_sep || DIGIT_SEP (*str))
598 	    SYNTAX_ERROR_AT (virtual_location,
599 			     "digit separator adjacent to exponent");
600 	  float_flag = AFTER_EXPON;
601 	  break;
602 	}
603       else
604 	{
605 	  /* Start of suffix.  */
606 	  str--;
607 	  break;
608 	}
609     }
610 
611   if (seen_digit_sep && float_flag != AFTER_EXPON)
612     SYNTAX_ERROR_AT (virtual_location,
613 		     "digit separator outside digit sequence");
614 
615   /* The suffix may be for decimal fixed-point constants without exponent.  */
616   if (radix != 16 && float_flag == NOT_FLOAT)
617     {
618       result = interpret_float_suffix (pfile, str, limit - str);
619       if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
620 	{
621 	  result |= CPP_N_FLOATING;
622 	  /* We need to restore the radix to 10, if the radix is 8.  */
623 	  if (radix == 8)
624 	    radix = 10;
625 
626 	  if (CPP_PEDANTIC (pfile))
627 	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
628 				 "fixed-point constants are a GCC extension");
629 	  goto syntax_ok;
630 	}
631       else
632 	result = 0;
633     }
634 
635   if (float_flag != NOT_FLOAT && radix == 8)
636     radix = 10;
637 
638   if (max_digit >= radix)
639     {
640       if (radix == 2)
641 	SYNTAX_ERROR2_AT (virtual_location,
642 			  "invalid digit \"%c\" in binary constant", '0' + max_digit);
643       else
644 	SYNTAX_ERROR2_AT (virtual_location,
645 			  "invalid digit \"%c\" in octal constant", '0' + max_digit);
646     }
647 
648   if (float_flag != NOT_FLOAT)
649     {
650       if (radix == 2)
651 	{
652 	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
653 			       "invalid prefix \"0b\" for floating constant");
654 	  return CPP_N_INVALID;
655 	}
656 
657       if (radix == 16 && !seen_digit)
658 	SYNTAX_ERROR_AT (virtual_location,
659 			 "no digits in hexadecimal floating constant");
660 
661       if (radix == 16 && CPP_PEDANTIC (pfile)
662 	  && !CPP_OPTION (pfile, extended_numbers))
663 	{
664 	  if (CPP_OPTION (pfile, cplusplus))
665 	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
666 				 "use of C++17 hexadecimal floating constant");
667 	  else
668 	    cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
669 				 "use of C99 hexadecimal floating constant");
670 	}
671 
672       if (float_flag == AFTER_EXPON)
673 	{
674 	  if (*str == '+' || *str == '-')
675 	    str++;
676 
677 	  /* Exponent is decimal, even if string is a hex float.  */
678 	  if (!ISDIGIT (*str))
679 	    {
680 	      if (DIGIT_SEP (*str))
681 		SYNTAX_ERROR_AT (virtual_location,
682 				 "digit separator adjacent to exponent");
683 	      else
684 		SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
685 	    }
686 	  do
687 	    {
688 	      seen_digit_sep = DIGIT_SEP (*str);
689 	      str++;
690 	    }
691 	  while (ISDIGIT (*str) || DIGIT_SEP (*str));
692 	}
693       else if (radix == 16)
694 	SYNTAX_ERROR_AT (virtual_location,
695 			 "hexadecimal floating constants require an exponent");
696 
697       if (seen_digit_sep)
698 	SYNTAX_ERROR_AT (virtual_location,
699 			 "digit separator outside digit sequence");
700 
701       result = interpret_float_suffix (pfile, str, limit - str);
702       if (result == 0)
703 	{
704 	  if (CPP_OPTION (pfile, user_literals))
705 	    {
706 	      if (ud_suffix)
707 		*ud_suffix = (const char *) str;
708 	      result = CPP_N_LARGE | CPP_N_USERDEF;
709 	    }
710 	  else
711 	    {
712 	      cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
713 				   "invalid suffix \"%.*s\" on floating constant",
714 				   (int) (limit - str), str);
715 	      return CPP_N_INVALID;
716 	    }
717 	}
718 
719       /* Traditional C didn't accept any floating suffixes.  */
720       if (limit != str
721 	  && CPP_WTRADITIONAL (pfile)
722 	  && ! cpp_sys_macro_p (pfile))
723 	cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
724 			       "traditional C rejects the \"%.*s\" suffix",
725 			       (int) (limit - str), str);
726 
727       /* A suffix for double is a GCC extension via decimal float support.
728 	 If the suffix also specifies an imaginary value we'll catch that
729 	 later.  */
730       if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
731 	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
732 			     "suffix for double constant is a GCC extension");
733 
734       /* Radix must be 10 for decimal floats.  */
735       if ((result & CPP_N_DFLOAT) && radix != 10)
736         {
737           cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
738 			       "invalid suffix \"%.*s\" with hexadecimal floating constant",
739 			       (int) (limit - str), str);
740           return CPP_N_INVALID;
741         }
742 
743       if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
744 	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
745 			     "fixed-point constants are a GCC extension");
746 
747       if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
748 	cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
749 			     "decimal float constants are a GCC extension");
750 
751       result |= CPP_N_FLOATING;
752     }
753   else
754     {
755       result = interpret_int_suffix (pfile, str, limit - str);
756       if (result == 0)
757 	{
758 	  if (CPP_OPTION (pfile, user_literals))
759 	    {
760 	      if (ud_suffix)
761 		*ud_suffix = (const char *) str;
762 	      result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
763 	    }
764 	  else
765 	    {
766 	      cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
767 				   "invalid suffix \"%.*s\" on integer constant",
768 				   (int) (limit - str), str);
769 	      return CPP_N_INVALID;
770 	    }
771 	}
772 
773       /* Traditional C only accepted the 'L' suffix.
774          Suppress warning about 'LL' with -Wno-long-long.  */
775       if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
776 	{
777 	  int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
778 	  int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
779 		       && CPP_OPTION (pfile, cpp_warn_long_long);
780 
781 	  if (u_or_i || large)
782 	    cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
783 				   virtual_location, 0,
784 				   "traditional C rejects the \"%.*s\" suffix",
785 				   (int) (limit - str), str);
786 	}
787 
788       if ((result & CPP_N_WIDTH) == CPP_N_LARGE
789 	  && CPP_OPTION (pfile, cpp_warn_long_long))
790         {
791           const char *message = CPP_OPTION (pfile, cplusplus)
792 				? N_("use of C++11 long long integer constant")
793 		                : N_("use of C99 long long integer constant");
794 
795 	  if (CPP_OPTION (pfile, c99))
796             cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
797 				   0, message);
798           else
799             cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
800 				      virtual_location, 0, message);
801         }
802 
803       result |= CPP_N_INTEGER;
804     }
805 
806  syntax_ok:
807   if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
808     cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
809 			 "imaginary constants are a GCC extension");
810   if (radix == 2
811       && !CPP_OPTION (pfile, binary_constants)
812       && CPP_PEDANTIC (pfile))
813     cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
814 			 CPP_OPTION (pfile, cplusplus)
815 			 ? N_("binary constants are a C++14 feature "
816 			      "or GCC extension")
817 			 : N_("binary constants are a GCC extension"));
818 
819   if (radix == 10)
820     result |= CPP_N_DECIMAL;
821   else if (radix == 16)
822     result |= CPP_N_HEX;
823   else if (radix == 2)
824     result |= CPP_N_BINARY;
825   else
826     result |= CPP_N_OCTAL;
827 
828   return result;
829 
830  syntax_error:
831   return CPP_N_INVALID;
832 }
833 
834 /* cpp_interpret_integer converts an integer constant into a cpp_num,
835    of precision options->precision.
836 
837    We do not provide any interface for decimal->float conversion,
838    because the preprocessor doesn't need it and we don't want to
839    drag in GCC's floating point emulator.  */
840 cpp_num
841 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
842 		       unsigned int type)
843 {
844   const uchar *p, *end;
845   cpp_num result;
846 
847   result.low = 0;
848   result.high = 0;
849   result.unsignedp = !!(type & CPP_N_UNSIGNED);
850   result.overflow = false;
851 
852   p = token->val.str.text;
853   end = p + token->val.str.len;
854 
855   /* Common case of a single digit.  */
856   if (token->val.str.len == 1)
857     result.low = p[0] - '0';
858   else
859     {
860       cpp_num_part max;
861       size_t precision = CPP_OPTION (pfile, precision);
862       unsigned int base = 10, c = 0;
863       bool overflow = false;
864 
865       if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
866 	{
867 	  base = 8;
868 	  p++;
869 	}
870       else if ((type & CPP_N_RADIX) == CPP_N_HEX)
871 	{
872 	  base = 16;
873 	  p += 2;
874 	}
875       else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
876 	{
877 	  base = 2;
878 	  p += 2;
879 	}
880 
881       /* We can add a digit to numbers strictly less than this without
882 	 needing the precision and slowness of double integers.  */
883       max = ~(cpp_num_part) 0;
884       if (precision < PART_PRECISION)
885 	max >>= PART_PRECISION - precision;
886       max = (max - base + 1) / base + 1;
887 
888       for (; p < end; p++)
889 	{
890 	  c = *p;
891 
892 	  if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
893 	    c = hex_value (c);
894 	  else if (DIGIT_SEP (c))
895 	    continue;
896 	  else
897 	    break;
898 
899 	  /* Strict inequality for when max is set to zero.  */
900 	  if (result.low < max)
901 	    result.low = result.low * base + c;
902 	  else
903 	    {
904 	      result = append_digit (result, c, base, precision);
905 	      overflow |= result.overflow;
906 	      max = 0;
907 	    }
908 	}
909 
910       if (overflow && !(type & CPP_N_USERDEF))
911 	cpp_error (pfile, CPP_DL_PEDWARN,
912 		   "integer constant is too large for its type");
913       /* If too big to be signed, consider it unsigned.  Only warn for
914 	 decimal numbers.  Traditional numbers were always signed (but
915 	 we still honor an explicit U suffix); but we only have
916 	 traditional semantics in directives.  */
917       else if (!result.unsignedp
918 	       && !(CPP_OPTION (pfile, traditional)
919 		    && pfile->state.in_directive)
920 	       && !num_positive (result, precision))
921 	{
922 	  /* This is for constants within the range of uintmax_t but
923 	     not that of intmax_t.  For such decimal constants, a
924 	     diagnostic is required for C99 as the selected type must
925 	     be signed and not having a type is a constraint violation
926 	     (DR#298, TC3), so this must be a pedwarn.  For C90,
927 	     unsigned long is specified to be used for a constant that
928 	     does not fit in signed long; if uintmax_t has the same
929 	     range as unsigned long this means only a warning is
930 	     appropriate here.  C90 permits the preprocessor to use a
931 	     wider range than unsigned long in the compiler, so if
932 	     uintmax_t is wider than unsigned long no diagnostic is
933 	     required for such constants in preprocessor #if
934 	     expressions and the compiler will pedwarn for such
935 	     constants outside the range of unsigned long that reach
936 	     the compiler so a diagnostic is not required there
937 	     either; thus, pedwarn for C99 but use a plain warning for
938 	     C90.  */
939 	  if (base == 10)
940 	    cpp_error (pfile, (CPP_OPTION (pfile, c99)
941 			       ? CPP_DL_PEDWARN
942 			       : CPP_DL_WARNING),
943 		       "integer constant is so large that it is unsigned");
944 	  result.unsignedp = true;
945 	}
946     }
947 
948   return result;
949 }
950 
951 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE.  */
952 static cpp_num
953 append_digit (cpp_num num, int digit, int base, size_t precision)
954 {
955   cpp_num result;
956   unsigned int shift;
957   bool overflow;
958   cpp_num_part add_high, add_low;
959 
960   /* Multiply by 2, 8 or 16.  Catching this overflow here means we don't
961      need to worry about add_high overflowing.  */
962   switch (base)
963     {
964     case 2:
965       shift = 1;
966       break;
967 
968     case 16:
969       shift = 4;
970       break;
971 
972     default:
973       shift = 3;
974     }
975   overflow = !!(num.high >> (PART_PRECISION - shift));
976   result.high = num.high << shift;
977   result.low = num.low << shift;
978   result.high |= num.low >> (PART_PRECISION - shift);
979   result.unsignedp = num.unsignedp;
980 
981   if (base == 10)
982     {
983       add_low = num.low << 1;
984       add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
985     }
986   else
987     add_high = add_low = 0;
988 
989   if (add_low + digit < add_low)
990     add_high++;
991   add_low += digit;
992 
993   if (result.low + add_low < result.low)
994     add_high++;
995   if (result.high + add_high < result.high)
996     overflow = true;
997 
998   result.low += add_low;
999   result.high += add_high;
1000   result.overflow = overflow;
1001 
1002   /* The above code catches overflow of a cpp_num type.  This catches
1003      overflow of the (possibly shorter) target precision.  */
1004   num.low = result.low;
1005   num.high = result.high;
1006   result = num_trim (result, precision);
1007   if (!num_eq (result, num))
1008     result.overflow = true;
1009 
1010   return result;
1011 }
1012 
1013 /* Handle meeting "defined" in a preprocessor expression.  */
1014 static cpp_num
1015 parse_defined (cpp_reader *pfile)
1016 {
1017   cpp_num result;
1018   int paren = 0;
1019   cpp_hashnode *node = 0;
1020   const cpp_token *token;
1021   cpp_context *initial_context = pfile->context;
1022 
1023   /* Don't expand macros.  */
1024   pfile->state.prevent_expansion++;
1025 
1026   token = cpp_get_token (pfile);
1027   if (token->type == CPP_OPEN_PAREN)
1028     {
1029       paren = 1;
1030       token = cpp_get_token (pfile);
1031     }
1032 
1033   if (token->type == CPP_NAME)
1034     {
1035       node = token->val.node.node;
1036       if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
1037 	{
1038 	  cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
1039 	  node = 0;
1040 	}
1041     }
1042   else
1043     {
1044       cpp_error (pfile, CPP_DL_ERROR,
1045 		 "operator \"defined\" requires an identifier");
1046       if (token->flags & NAMED_OP)
1047 	{
1048 	  cpp_token op;
1049 
1050 	  op.flags = 0;
1051 	  op.type = token->type;
1052 	  cpp_error (pfile, CPP_DL_ERROR,
1053 		     "(\"%s\" is an alternative token for \"%s\" in C++)",
1054 		     cpp_token_as_text (pfile, token),
1055 		     cpp_token_as_text (pfile, &op));
1056 	}
1057     }
1058 
1059   if (node)
1060     {
1061       if ((pfile->context != initial_context
1062 	   || initial_context != &pfile->base_context)
1063 	  && CPP_OPTION (pfile, warn_expansion_to_defined))
1064         cpp_pedwarning (pfile, CPP_W_EXPANSION_TO_DEFINED,
1065 		        "this use of \"defined\" may not be portable");
1066 
1067       _cpp_mark_macro_used (node);
1068       if (!(node->flags & NODE_USED))
1069 	{
1070 	  node->flags |= NODE_USED;
1071 	  if (node->type == NT_MACRO)
1072 	    {
1073 	      if ((node->flags & NODE_BUILTIN)
1074 		  && pfile->cb.user_builtin_macro)
1075 		pfile->cb.user_builtin_macro (pfile, node);
1076 	      if (pfile->cb.used_define)
1077 		pfile->cb.used_define (pfile, pfile->directive_line, node);
1078 	    }
1079 	  else
1080 	    {
1081 	      if (pfile->cb.used_undef)
1082 		pfile->cb.used_undef (pfile, pfile->directive_line, node);
1083 	    }
1084 	}
1085 
1086       /* A possible controlling macro of the form #if !defined ().
1087 	 _cpp_parse_expr checks there was no other junk on the line.  */
1088       pfile->mi_ind_cmacro = node;
1089     }
1090 
1091   pfile->state.prevent_expansion--;
1092 
1093   /* Do not treat conditional macros as being defined.  This is due to the
1094      powerpc and spu ports using conditional macros for 'vector', 'bool', and
1095      'pixel' to act as conditional keywords.  This messes up tests like #ifndef
1096      bool.  */
1097   result.unsignedp = false;
1098   result.high = 0;
1099   result.overflow = false;
1100   result.low = (node && node->type == NT_MACRO
1101 		&& (node->flags & NODE_CONDITIONAL) == 0);
1102   return result;
1103 }
1104 
1105 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
1106    number or character constant, or the result of the "defined" or "#"
1107    operators).  */
1108 static cpp_num
1109 eval_token (cpp_reader *pfile, const cpp_token *token,
1110 	    source_location virtual_location)
1111 {
1112   cpp_num result;
1113   unsigned int temp;
1114   int unsignedp = 0;
1115 
1116   result.unsignedp = false;
1117   result.overflow = false;
1118 
1119   switch (token->type)
1120     {
1121     case CPP_NUMBER:
1122       temp = cpp_classify_number (pfile, token, NULL, virtual_location);
1123       if (temp & CPP_N_USERDEF)
1124 	cpp_error (pfile, CPP_DL_ERROR,
1125 		   "user-defined literal in preprocessor expression");
1126       switch (temp & CPP_N_CATEGORY)
1127 	{
1128 	case CPP_N_FLOATING:
1129 	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1130 			       "floating constant in preprocessor expression");
1131 	  break;
1132 	case CPP_N_INTEGER:
1133 	  if (!(temp & CPP_N_IMAGINARY))
1134 	    return cpp_interpret_integer (pfile, token, temp);
1135 	  cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1136 			       "imaginary number in preprocessor expression");
1137 	  break;
1138 
1139 	case CPP_N_INVALID:
1140 	  /* Error already issued.  */
1141 	  break;
1142 	}
1143       result.high = result.low = 0;
1144       break;
1145 
1146     case CPP_WCHAR:
1147     case CPP_CHAR:
1148     case CPP_CHAR16:
1149     case CPP_CHAR32:
1150     case CPP_UTF8CHAR:
1151       {
1152 	cppchar_t cc = cpp_interpret_charconst (pfile, token,
1153 						&temp, &unsignedp);
1154 
1155 	result.high = 0;
1156 	result.low = cc;
1157 	/* Sign-extend the result if necessary.  */
1158 	if (!unsignedp && (cppchar_signed_t) cc < 0)
1159 	  {
1160 	    if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1161 	      result.low |= ~(~(cpp_num_part) 0
1162 			      >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1163 	    result.high = ~(cpp_num_part) 0;
1164 	    result = num_trim (result, CPP_OPTION (pfile, precision));
1165 	  }
1166       }
1167       break;
1168 
1169     case CPP_NAME:
1170       if (token->val.node.node == pfile->spec_nodes.n_defined)
1171 	return parse_defined (pfile);
1172       else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1173 	return parse_has_include (pfile, IT_INCLUDE);
1174       else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1175 	return parse_has_include (pfile, IT_INCLUDE_NEXT);
1176       else if (CPP_OPTION (pfile, cplusplus)
1177 	       && (token->val.node.node == pfile->spec_nodes.n_true
1178 		   || token->val.node.node == pfile->spec_nodes.n_false))
1179 	{
1180 	  result.high = 0;
1181 	  result.low = (token->val.node.node == pfile->spec_nodes.n_true);
1182 	}
1183       else
1184 	{
1185 	  result.high = 0;
1186 	  result.low = 0;
1187 	  if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
1188 	    cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1189 				   "\"%s\" is not defined, evaluates to 0",
1190 				   NODE_NAME (token->val.node.node));
1191 	}
1192       break;
1193 
1194     case CPP_HASH:
1195       if (!pfile->state.skipping)
1196 	{
1197 	  /* A pedantic warning takes precedence over a deprecated
1198 	     warning here.  */
1199 	  if (CPP_PEDANTIC (pfile))
1200 	    cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1201 				 virtual_location, 0,
1202 				 "assertions are a GCC extension");
1203 	  else if (CPP_OPTION (pfile, cpp_warn_deprecated))
1204 	    cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1205 				   "assertions are a deprecated extension");
1206 	}
1207       _cpp_test_assertion (pfile, &temp);
1208       result.high = 0;
1209       result.low = temp;
1210       break;
1211 
1212     default:
1213       abort ();
1214     }
1215 
1216   result.unsignedp = !!unsignedp;
1217   return result;
1218 }
1219 
1220 /* Operator precedence and flags table.
1221 
1222 After an operator is returned from the lexer, if it has priority less
1223 than the operator on the top of the stack, we reduce the stack by one
1224 operator and repeat the test.  Since equal priorities do not reduce,
1225 this is naturally right-associative.
1226 
1227 We handle left-associative operators by decrementing the priority of
1228 just-lexed operators by one, but retaining the priority of operators
1229 already on the stack.
1230 
1231 The remaining cases are '(' and ')'.  We handle '(' by skipping the
1232 reduction phase completely.  ')' is given lower priority than
1233 everything else, including '(', effectively forcing a reduction of the
1234 parenthesized expression.  If there is a matching '(', the routine
1235 reduce() exits immediately.  If the normal exit route sees a ')', then
1236 there cannot have been a matching '(' and an error message is output.
1237 
1238 The parser assumes all shifted operators require a left operand unless
1239 the flag NO_L_OPERAND is set.  These semantics are automatic; any
1240 extra semantics need to be handled with operator-specific code.  */
1241 
1242 /* Flags.  If CHECK_PROMOTION, we warn if the effective sign of an
1243    operand changes because of integer promotions.  */
1244 #define NO_L_OPERAND	(1 << 0)
1245 #define LEFT_ASSOC	(1 << 1)
1246 #define CHECK_PROMOTION	(1 << 2)
1247 
1248 /* Operator to priority map.  Must be in the same order as the first
1249    N entries of enum cpp_ttype.  */
1250 static const struct cpp_operator
1251 {
1252   uchar prio;
1253   uchar flags;
1254 } optab[] =
1255 {
1256   /* EQ */		{0, 0},	/* Shouldn't happen.  */
1257   /* NOT */		{16, NO_L_OPERAND},
1258   /* GREATER */		{12, LEFT_ASSOC | CHECK_PROMOTION},
1259   /* LESS */		{12, LEFT_ASSOC | CHECK_PROMOTION},
1260   /* PLUS */		{14, LEFT_ASSOC | CHECK_PROMOTION},
1261   /* MINUS */		{14, LEFT_ASSOC | CHECK_PROMOTION},
1262   /* MULT */		{15, LEFT_ASSOC | CHECK_PROMOTION},
1263   /* DIV */		{15, LEFT_ASSOC | CHECK_PROMOTION},
1264   /* MOD */		{15, LEFT_ASSOC | CHECK_PROMOTION},
1265   /* AND */		{9, LEFT_ASSOC | CHECK_PROMOTION},
1266   /* OR */		{7, LEFT_ASSOC | CHECK_PROMOTION},
1267   /* XOR */		{8, LEFT_ASSOC | CHECK_PROMOTION},
1268   /* RSHIFT */		{13, LEFT_ASSOC},
1269   /* LSHIFT */		{13, LEFT_ASSOC},
1270 
1271   /* COMPL */		{16, NO_L_OPERAND},
1272   /* AND_AND */		{6, LEFT_ASSOC},
1273   /* OR_OR */		{5, LEFT_ASSOC},
1274   /* Note that QUERY, COLON, and COMMA must have the same precedence.
1275      However, there are some special cases for these in reduce().  */
1276   /* QUERY */		{4, 0},
1277   /* COLON */		{4, LEFT_ASSOC | CHECK_PROMOTION},
1278   /* COMMA */		{4, LEFT_ASSOC},
1279   /* OPEN_PAREN */	{1, NO_L_OPERAND},
1280   /* CLOSE_PAREN */	{0, 0},
1281   /* EOF */		{0, 0},
1282   /* EQ_EQ */		{11, LEFT_ASSOC},
1283   /* NOT_EQ */		{11, LEFT_ASSOC},
1284   /* GREATER_EQ */	{12, LEFT_ASSOC | CHECK_PROMOTION},
1285   /* LESS_EQ */		{12, LEFT_ASSOC | CHECK_PROMOTION},
1286   /* UPLUS */		{16, NO_L_OPERAND},
1287   /* UMINUS */		{16, NO_L_OPERAND}
1288 };
1289 
1290 /* Parse and evaluate a C expression, reading from PFILE.
1291    Returns the truth value of the expression.
1292 
1293    The implementation is an operator precedence parser, i.e. a
1294    bottom-up parser, using a stack for not-yet-reduced tokens.
1295 
1296    The stack base is op_stack, and the current stack pointer is 'top'.
1297    There is a stack element for each operator (only), and the most
1298    recently pushed operator is 'top->op'.  An operand (value) is
1299    stored in the 'value' field of the stack element of the operator
1300    that precedes it.  */
1301 bool
1302 _cpp_parse_expr (cpp_reader *pfile, bool is_if)
1303 {
1304   struct op *top = pfile->op_stack;
1305   unsigned int lex_count;
1306   bool saw_leading_not, want_value = true;
1307   source_location virtual_location = 0;
1308 
1309   pfile->state.skip_eval = 0;
1310 
1311   /* Set up detection of #if ! defined().  */
1312   pfile->mi_ind_cmacro = 0;
1313   saw_leading_not = false;
1314   lex_count = 0;
1315 
1316   /* Lowest priority operator prevents further reductions.  */
1317   top->op = CPP_EOF;
1318 
1319   for (;;)
1320     {
1321       struct op op;
1322 
1323       lex_count++;
1324       op.token = cpp_get_token_with_location (pfile, &virtual_location);
1325       op.op = op.token->type;
1326       op.loc = virtual_location;
1327 
1328       switch (op.op)
1329 	{
1330 	  /* These tokens convert into values.  */
1331 	case CPP_NUMBER:
1332 	case CPP_CHAR:
1333 	case CPP_WCHAR:
1334 	case CPP_CHAR16:
1335 	case CPP_CHAR32:
1336 	case CPP_UTF8CHAR:
1337 	case CPP_NAME:
1338 	case CPP_HASH:
1339 	  if (!want_value)
1340 	    SYNTAX_ERROR2_AT (op.loc,
1341 			      "missing binary operator before token \"%s\"",
1342 			      cpp_token_as_text (pfile, op.token));
1343 	  want_value = false;
1344 	  top->value = eval_token (pfile, op.token, op.loc);
1345 	  continue;
1346 
1347 	case CPP_NOT:
1348 	  saw_leading_not = lex_count == 1;
1349 	  break;
1350 	case CPP_PLUS:
1351 	  if (want_value)
1352 	    op.op = CPP_UPLUS;
1353 	  break;
1354 	case CPP_MINUS:
1355 	  if (want_value)
1356 	    op.op = CPP_UMINUS;
1357 	  break;
1358 
1359 	default:
1360 	  if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
1361 	    SYNTAX_ERROR2_AT (op.loc,
1362 			      "token \"%s\" is not valid in preprocessor expressions",
1363 			      cpp_token_as_text (pfile, op.token));
1364 	  break;
1365 	}
1366 
1367       /* Check we have a value or operator as appropriate.  */
1368       if (optab[op.op].flags & NO_L_OPERAND)
1369 	{
1370 	  if (!want_value)
1371 	    SYNTAX_ERROR2_AT (op.loc,
1372 			      "missing binary operator before token \"%s\"",
1373 			      cpp_token_as_text (pfile, op.token));
1374 	}
1375       else if (want_value)
1376 	{
1377 	  /* We want a number (or expression) and haven't got one.
1378 	     Try to emit a specific diagnostic.  */
1379 	  if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1380 	    SYNTAX_ERROR_AT (op.loc,
1381 			     "missing expression between '(' and ')'");
1382 
1383 	  if (op.op == CPP_EOF && top->op == CPP_EOF)
1384  	    SYNTAX_ERROR2_AT (op.loc,
1385 			      "%s with no expression", is_if ? "#if" : "#elif");
1386 
1387  	  if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1388  	    SYNTAX_ERROR2_AT (op.loc,
1389 			      "operator '%s' has no right operand",
1390 			      cpp_token_as_text (pfile, top->token));
1391 	  else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1392 	    /* Complain about missing paren during reduction.  */;
1393 	  else
1394 	    SYNTAX_ERROR2_AT (op.loc,
1395 			      "operator '%s' has no left operand",
1396 			      cpp_token_as_text (pfile, op.token));
1397 	}
1398 
1399       top = reduce (pfile, top, op.op);
1400       if (!top)
1401 	goto syntax_error;
1402 
1403       if (op.op == CPP_EOF)
1404 	break;
1405 
1406       switch (op.op)
1407 	{
1408 	case CPP_CLOSE_PAREN:
1409 	  continue;
1410 	case CPP_OR_OR:
1411 	  if (!num_zerop (top->value))
1412 	    pfile->state.skip_eval++;
1413 	  break;
1414 	case CPP_AND_AND:
1415 	case CPP_QUERY:
1416 	  if (num_zerop (top->value))
1417 	    pfile->state.skip_eval++;
1418 	  break;
1419 	case CPP_COLON:
1420 	  if (top->op != CPP_QUERY)
1421 	    SYNTAX_ERROR_AT (op.loc,
1422 			     " ':' without preceding '?'");
1423 	  if (!num_zerop (top[-1].value)) /* Was '?' condition true?  */
1424 	    pfile->state.skip_eval++;
1425 	  else
1426 	    pfile->state.skip_eval--;
1427 	default:
1428 	  break;
1429 	}
1430 
1431       want_value = true;
1432 
1433       /* Check for and handle stack overflow.  */
1434       if (++top == pfile->op_limit)
1435 	top = _cpp_expand_op_stack (pfile);
1436 
1437       top->op = op.op;
1438       top->token = op.token;
1439       top->loc = op.loc;
1440     }
1441 
1442   /* The controlling macro expression is only valid if we called lex 3
1443      times: <!> <defined expression> and <EOF>.  push_conditional ()
1444      checks that we are at top-of-file.  */
1445   if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1446     pfile->mi_ind_cmacro = 0;
1447 
1448   if (top != pfile->op_stack)
1449     {
1450       cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1451 			   "unbalanced stack in %s",
1452 			   is_if ? "#if" : "#elif");
1453     syntax_error:
1454       return false;  /* Return false on syntax error.  */
1455     }
1456 
1457   return !num_zerop (top->value);
1458 }
1459 
1460 /* Reduce the operator / value stack if possible, in preparation for
1461    pushing operator OP.  Returns NULL on error, otherwise the top of
1462    the stack.  */
1463 static struct op *
1464 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
1465 {
1466   unsigned int prio;
1467 
1468   if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1469     {
1470     bad_op:
1471       cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
1472       return 0;
1473     }
1474 
1475   if (op == CPP_OPEN_PAREN)
1476     return top;
1477 
1478   /* Decrement the priority of left-associative operators to force a
1479      reduction with operators of otherwise equal priority.  */
1480   prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1481   while (prio < optab[top->op].prio)
1482     {
1483       if (CPP_OPTION (pfile, warn_num_sign_change)
1484 	  && optab[top->op].flags & CHECK_PROMOTION)
1485 	check_promotion (pfile, top);
1486 
1487       switch (top->op)
1488 	{
1489 	case CPP_UPLUS:
1490 	case CPP_UMINUS:
1491 	case CPP_NOT:
1492 	case CPP_COMPL:
1493 	  top[-1].value = num_unary_op (pfile, top->value, top->op);
1494 	  top[-1].loc = top->loc;
1495 	  break;
1496 
1497 	case CPP_PLUS:
1498 	case CPP_MINUS:
1499 	case CPP_RSHIFT:
1500 	case CPP_LSHIFT:
1501 	case CPP_COMMA:
1502 	  top[-1].value = num_binary_op (pfile, top[-1].value,
1503 					 top->value, top->op);
1504 	  top[-1].loc = top->loc;
1505 	  break;
1506 
1507 	case CPP_GREATER:
1508 	case CPP_LESS:
1509 	case CPP_GREATER_EQ:
1510 	case CPP_LESS_EQ:
1511 	  top[-1].value
1512 	    = num_inequality_op (pfile, top[-1].value, top->value, top->op);
1513 	  top[-1].loc = top->loc;
1514 	  break;
1515 
1516 	case CPP_EQ_EQ:
1517 	case CPP_NOT_EQ:
1518 	  top[-1].value
1519 	    = num_equality_op (pfile, top[-1].value, top->value, top->op);
1520 	  top[-1].loc = top->loc;
1521 	  break;
1522 
1523 	case CPP_AND:
1524 	case CPP_OR:
1525 	case CPP_XOR:
1526 	  top[-1].value
1527 	    = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
1528 	  top[-1].loc = top->loc;
1529 	  break;
1530 
1531 	case CPP_MULT:
1532 	  top[-1].value = num_mul (pfile, top[-1].value, top->value);
1533 	  top[-1].loc = top->loc;
1534 	  break;
1535 
1536 	case CPP_DIV:
1537 	case CPP_MOD:
1538 	  top[-1].value = num_div_op (pfile, top[-1].value,
1539 				      top->value, top->op, top->loc);
1540 	  top[-1].loc = top->loc;
1541 	  break;
1542 
1543 	case CPP_OR_OR:
1544 	  top--;
1545 	  if (!num_zerop (top->value))
1546 	    pfile->state.skip_eval--;
1547 	  top->value.low = (!num_zerop (top->value)
1548 			    || !num_zerop (top[1].value));
1549 	  top->value.high = 0;
1550 	  top->value.unsignedp = false;
1551 	  top->value.overflow = false;
1552 	  top->loc = top[1].loc;
1553 	  continue;
1554 
1555 	case CPP_AND_AND:
1556 	  top--;
1557 	  if (num_zerop (top->value))
1558 	    pfile->state.skip_eval--;
1559 	  top->value.low = (!num_zerop (top->value)
1560 			    && !num_zerop (top[1].value));
1561 	  top->value.high = 0;
1562 	  top->value.unsignedp = false;
1563 	  top->value.overflow = false;
1564 	  top->loc = top[1].loc;
1565 	  continue;
1566 
1567 	case CPP_OPEN_PAREN:
1568 	  if (op != CPP_CLOSE_PAREN)
1569 	    {
1570 	      cpp_error_with_line (pfile, CPP_DL_ERROR,
1571 				   top->token->src_loc,
1572 				   0, "missing ')' in expression");
1573 	      return 0;
1574 	    }
1575 	  top--;
1576 	  top->value = top[1].value;
1577 	  top->loc = top[1].loc;
1578 	  return top;
1579 
1580 	case CPP_COLON:
1581 	  top -= 2;
1582 	  if (!num_zerop (top->value))
1583 	    {
1584 	      pfile->state.skip_eval--;
1585 	      top->value = top[1].value;
1586 	      top->loc = top[1].loc;
1587 	    }
1588 	  else
1589 	    {
1590 	      top->value = top[2].value;
1591 	      top->loc = top[2].loc;
1592 	    }
1593 	  top->value.unsignedp = (top[1].value.unsignedp
1594 				  || top[2].value.unsignedp);
1595 	  continue;
1596 
1597 	case CPP_QUERY:
1598 	  /* COMMA and COLON should not reduce a QUERY operator.  */
1599 	  if (op == CPP_COMMA || op == CPP_COLON)
1600 	    return top;
1601 	  cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
1602 	  return 0;
1603 
1604 	default:
1605 	  goto bad_op;
1606 	}
1607 
1608       top--;
1609       if (top->value.overflow && !pfile->state.skip_eval)
1610 	cpp_error (pfile, CPP_DL_PEDWARN,
1611 		   "integer overflow in preprocessor expression");
1612     }
1613 
1614   if (op == CPP_CLOSE_PAREN)
1615     {
1616       cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
1617       return 0;
1618     }
1619 
1620   return top;
1621 }
1622 
1623 /* Returns the position of the old top of stack after expansion.  */
1624 struct op *
1625 _cpp_expand_op_stack (cpp_reader *pfile)
1626 {
1627   size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1628   size_t new_size = old_size * 2 + 20;
1629 
1630   pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
1631   pfile->op_limit = pfile->op_stack + new_size;
1632 
1633   return pfile->op_stack + old_size;
1634 }
1635 
1636 /* Emits a warning if the effective sign of either operand of OP
1637    changes because of integer promotions.  */
1638 static void
1639 check_promotion (cpp_reader *pfile, const struct op *op)
1640 {
1641   if (op->value.unsignedp == op[-1].value.unsignedp)
1642     return;
1643 
1644   if (op->value.unsignedp)
1645     {
1646       if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
1647 	cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1648 			     "the left operand of \"%s\" changes sign when promoted",
1649 			     cpp_token_as_text (pfile, op->token));
1650     }
1651   else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
1652     cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
1653 	       "the right operand of \"%s\" changes sign when promoted",
1654 	       cpp_token_as_text (pfile, op->token));
1655 }
1656 
1657 /* Clears the unused high order bits of the number pointed to by PNUM.  */
1658 static cpp_num
1659 num_trim (cpp_num num, size_t precision)
1660 {
1661   if (precision > PART_PRECISION)
1662     {
1663       precision -= PART_PRECISION;
1664       if (precision < PART_PRECISION)
1665 	num.high &= ((cpp_num_part) 1 << precision) - 1;
1666     }
1667   else
1668     {
1669       if (precision < PART_PRECISION)
1670 	num.low &= ((cpp_num_part) 1 << precision) - 1;
1671       num.high = 0;
1672     }
1673 
1674   return num;
1675 }
1676 
1677 /* True iff A (presumed signed) >= 0.  */
1678 static bool
1679 num_positive (cpp_num num, size_t precision)
1680 {
1681   if (precision > PART_PRECISION)
1682     {
1683       precision -= PART_PRECISION;
1684       return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
1685     }
1686 
1687   return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
1688 }
1689 
1690 /* Sign extend a number, with PRECISION significant bits and all
1691    others assumed clear, to fill out a cpp_num structure.  */
1692 cpp_num
1693 cpp_num_sign_extend (cpp_num num, size_t precision)
1694 {
1695   if (!num.unsignedp)
1696     {
1697       if (precision > PART_PRECISION)
1698 	{
1699 	  precision -= PART_PRECISION;
1700 	  if (precision < PART_PRECISION
1701 	      && (num.high & (cpp_num_part) 1 << (precision - 1)))
1702 	    num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1703 	}
1704       else if (num.low & (cpp_num_part) 1 << (precision - 1))
1705 	{
1706 	  if (precision < PART_PRECISION)
1707 	    num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1708 	  num.high = ~(cpp_num_part) 0;
1709 	}
1710     }
1711 
1712   return num;
1713 }
1714 
1715 /* Returns the negative of NUM.  */
1716 static cpp_num
1717 num_negate (cpp_num num, size_t precision)
1718 {
1719   cpp_num copy;
1720 
1721   copy = num;
1722   num.high = ~num.high;
1723   num.low = ~num.low;
1724   if (++num.low == 0)
1725     num.high++;
1726   num = num_trim (num, precision);
1727   num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1728 
1729   return num;
1730 }
1731 
1732 /* Returns true if A >= B.  */
1733 static bool
1734 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
1735 {
1736   bool unsignedp;
1737 
1738   unsignedp = pa.unsignedp || pb.unsignedp;
1739 
1740   if (!unsignedp)
1741     {
1742       /* Both numbers have signed type.  If they are of different
1743        sign, the answer is the sign of A.  */
1744       unsignedp = num_positive (pa, precision);
1745 
1746       if (unsignedp != num_positive (pb, precision))
1747 	return unsignedp;
1748 
1749       /* Otherwise we can do an unsigned comparison.  */
1750     }
1751 
1752   return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1753 }
1754 
1755 /* Returns LHS OP RHS, where OP is a bit-wise operation.  */
1756 static cpp_num
1757 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1758 		cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1759 {
1760   lhs.overflow = false;
1761   lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1762 
1763   /* As excess precision is zeroed, there is no need to num_trim () as
1764      these operations cannot introduce a set bit there.  */
1765   if (op == CPP_AND)
1766     {
1767       lhs.low &= rhs.low;
1768       lhs.high &= rhs.high;
1769     }
1770   else if (op == CPP_OR)
1771     {
1772       lhs.low |= rhs.low;
1773       lhs.high |= rhs.high;
1774     }
1775   else
1776     {
1777       lhs.low ^= rhs.low;
1778       lhs.high ^= rhs.high;
1779     }
1780 
1781   return lhs;
1782 }
1783 
1784 /* Returns LHS OP RHS, where OP is an inequality.  */
1785 static cpp_num
1786 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1787 		   enum cpp_ttype op)
1788 {
1789   bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1790 
1791   if (op == CPP_GREATER_EQ)
1792     lhs.low = gte;
1793   else if (op == CPP_LESS)
1794     lhs.low = !gte;
1795   else if (op == CPP_GREATER)
1796     lhs.low = gte && !num_eq (lhs, rhs);
1797   else /* CPP_LESS_EQ.  */
1798     lhs.low = !gte || num_eq (lhs, rhs);
1799 
1800   lhs.high = 0;
1801   lhs.overflow = false;
1802   lhs.unsignedp = false;
1803   return lhs;
1804 }
1805 
1806 /* Returns LHS OP RHS, where OP is == or !=.  */
1807 static cpp_num
1808 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1809 		 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1810 {
1811   /* Work around a 3.0.4 bug; see PR 6950.  */
1812   bool eq = num_eq (lhs, rhs);
1813   if (op == CPP_NOT_EQ)
1814     eq = !eq;
1815   lhs.low = eq;
1816   lhs.high = 0;
1817   lhs.overflow = false;
1818   lhs.unsignedp = false;
1819   return lhs;
1820 }
1821 
1822 /* Shift NUM, of width PRECISION, right by N bits.  */
1823 static cpp_num
1824 num_rshift (cpp_num num, size_t precision, size_t n)
1825 {
1826   cpp_num_part sign_mask;
1827   bool x = num_positive (num, precision);
1828 
1829   if (num.unsignedp || x)
1830     sign_mask = 0;
1831   else
1832     sign_mask = ~(cpp_num_part) 0;
1833 
1834   if (n >= precision)
1835     num.high = num.low = sign_mask;
1836   else
1837     {
1838       /* Sign-extend.  */
1839       if (precision < PART_PRECISION)
1840 	num.high = sign_mask, num.low |= sign_mask << precision;
1841       else if (precision < 2 * PART_PRECISION)
1842 	num.high |= sign_mask << (precision - PART_PRECISION);
1843 
1844       if (n >= PART_PRECISION)
1845 	{
1846 	  n -= PART_PRECISION;
1847 	  num.low = num.high;
1848 	  num.high = sign_mask;
1849 	}
1850 
1851       if (n)
1852 	{
1853 	  num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1854 	  num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1855 	}
1856     }
1857 
1858   num = num_trim (num, precision);
1859   num.overflow = false;
1860   return num;
1861 }
1862 
1863 /* Shift NUM, of width PRECISION, left by N bits.  */
1864 static cpp_num
1865 num_lshift (cpp_num num, size_t precision, size_t n)
1866 {
1867   if (n >= precision)
1868     {
1869       num.overflow = !num.unsignedp && !num_zerop (num);
1870       num.high = num.low = 0;
1871     }
1872   else
1873     {
1874       cpp_num orig, maybe_orig;
1875       size_t m = n;
1876 
1877       orig = num;
1878       if (m >= PART_PRECISION)
1879 	{
1880 	  m -= PART_PRECISION;
1881 	  num.high = num.low;
1882 	  num.low = 0;
1883 	}
1884       if (m)
1885 	{
1886 	  num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1887 	  num.low <<= m;
1888 	}
1889       num = num_trim (num, precision);
1890 
1891       if (num.unsignedp)
1892 	num.overflow = false;
1893       else
1894 	{
1895 	  maybe_orig = num_rshift (num, precision, n);
1896 	  num.overflow = !num_eq (orig, maybe_orig);
1897 	}
1898     }
1899 
1900   return num;
1901 }
1902 
1903 /* The four unary operators: +, -, ! and ~.  */
1904 static cpp_num
1905 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
1906 {
1907   switch (op)
1908     {
1909     case CPP_UPLUS:
1910       if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
1911 	cpp_warning (pfile, CPP_W_TRADITIONAL,
1912 		     "traditional C rejects the unary plus operator");
1913       num.overflow = false;
1914       break;
1915 
1916     case CPP_UMINUS:
1917       num = num_negate (num, CPP_OPTION (pfile, precision));
1918       break;
1919 
1920     case CPP_COMPL:
1921       num.high = ~num.high;
1922       num.low = ~num.low;
1923       num = num_trim (num, CPP_OPTION (pfile, precision));
1924       num.overflow = false;
1925       break;
1926 
1927     default: /* case CPP_NOT: */
1928       num.low = num_zerop (num);
1929       num.high = 0;
1930       num.overflow = false;
1931       num.unsignedp = false;
1932       break;
1933     }
1934 
1935   return num;
1936 }
1937 
1938 /* The various binary operators.  */
1939 static cpp_num
1940 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
1941 {
1942   cpp_num result;
1943   size_t precision = CPP_OPTION (pfile, precision);
1944   size_t n;
1945 
1946   switch (op)
1947     {
1948       /* Shifts.  */
1949     case CPP_LSHIFT:
1950     case CPP_RSHIFT:
1951       if (!rhs.unsignedp && !num_positive (rhs, precision))
1952 	{
1953 	  /* A negative shift is a positive shift the other way.  */
1954 	  if (op == CPP_LSHIFT)
1955 	    op = CPP_RSHIFT;
1956 	  else
1957 	    op = CPP_LSHIFT;
1958 	  rhs = num_negate (rhs, precision);
1959 	}
1960       if (rhs.high)
1961 	n = ~0;			/* Maximal.  */
1962       else
1963 	n = rhs.low;
1964       if (op == CPP_LSHIFT)
1965 	lhs = num_lshift (lhs, precision, n);
1966       else
1967 	lhs = num_rshift (lhs, precision, n);
1968       break;
1969 
1970       /* Arithmetic.  */
1971     case CPP_MINUS:
1972       result.low = lhs.low - rhs.low;
1973       result.high = lhs.high - rhs.high;
1974       if (result.low > lhs.low)
1975 	result.high--;
1976       result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1977       result.overflow = false;
1978 
1979       result = num_trim (result, precision);
1980       if (!result.unsignedp)
1981 	{
1982 	  bool lhsp = num_positive (lhs, precision);
1983 	  result.overflow = (lhsp != num_positive (rhs, precision)
1984 			     && lhsp != num_positive (result, precision));
1985 	}
1986       return result;
1987 
1988     case CPP_PLUS:
1989       result.low = lhs.low + rhs.low;
1990       result.high = lhs.high + rhs.high;
1991       if (result.low < lhs.low)
1992 	result.high++;
1993       result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1994       result.overflow = false;
1995 
1996       result = num_trim (result, precision);
1997       if (!result.unsignedp)
1998 	{
1999 	  bool lhsp = num_positive (lhs, precision);
2000 	  result.overflow = (lhsp == num_positive (rhs, precision)
2001 			     && lhsp != num_positive (result, precision));
2002 	}
2003       return result;
2004 
2005       /* Comma.  */
2006     default: /* case CPP_COMMA: */
2007       if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
2008 				   || !pfile->state.skip_eval))
2009 	cpp_pedwarning (pfile, CPP_W_PEDANTIC,
2010 			"comma operator in operand of #if");
2011       lhs = rhs;
2012       break;
2013     }
2014 
2015   return lhs;
2016 }
2017 
2018 /* Multiplies two unsigned cpp_num_parts to give a cpp_num.  This
2019    cannot overflow.  */
2020 static cpp_num
2021 num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
2022 {
2023   cpp_num result;
2024   cpp_num_part middle[2], temp;
2025 
2026   result.low = LOW_PART (lhs) * LOW_PART (rhs);
2027   result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
2028 
2029   middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
2030   middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
2031 
2032   temp = result.low;
2033   result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
2034   if (result.low < temp)
2035     result.high++;
2036 
2037   temp = result.low;
2038   result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
2039   if (result.low < temp)
2040     result.high++;
2041 
2042   result.high += HIGH_PART (middle[0]);
2043   result.high += HIGH_PART (middle[1]);
2044   result.unsignedp = true;
2045   result.overflow = false;
2046 
2047   return result;
2048 }
2049 
2050 /* Multiply two preprocessing numbers.  */
2051 static cpp_num
2052 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
2053 {
2054   cpp_num result, temp;
2055   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2056   bool overflow, negate = false;
2057   size_t precision = CPP_OPTION (pfile, precision);
2058 
2059   /* Prepare for unsigned multiplication.  */
2060   if (!unsignedp)
2061     {
2062       if (!num_positive (lhs, precision))
2063 	negate = !negate, lhs = num_negate (lhs, precision);
2064       if (!num_positive (rhs, precision))
2065 	negate = !negate, rhs = num_negate (rhs, precision);
2066     }
2067 
2068   overflow = lhs.high && rhs.high;
2069   result = num_part_mul (lhs.low, rhs.low);
2070 
2071   temp = num_part_mul (lhs.high, rhs.low);
2072   result.high += temp.low;
2073   if (temp.high)
2074     overflow = true;
2075 
2076   temp = num_part_mul (lhs.low, rhs.high);
2077   result.high += temp.low;
2078   if (temp.high)
2079     overflow = true;
2080 
2081   temp.low = result.low, temp.high = result.high;
2082   result = num_trim (result, precision);
2083   if (!num_eq (result, temp))
2084     overflow = true;
2085 
2086   if (negate)
2087     result = num_negate (result, precision);
2088 
2089   if (unsignedp)
2090     result.overflow = false;
2091   else
2092     result.overflow = overflow || (num_positive (result, precision) ^ !negate
2093 				   && !num_zerop (result));
2094   result.unsignedp = unsignedp;
2095 
2096   return result;
2097 }
2098 
2099 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
2100    or the remainder depending upon OP. LOCATION is the source location
2101    of this operator (for diagnostics).  */
2102 
2103 static cpp_num
2104 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
2105 	    source_location location)
2106 {
2107   cpp_num result, sub;
2108   cpp_num_part mask;
2109   bool unsignedp = lhs.unsignedp || rhs.unsignedp;
2110   bool negate = false, lhs_neg = false;
2111   size_t i, precision = CPP_OPTION (pfile, precision);
2112 
2113   /* Prepare for unsigned division.  */
2114   if (!unsignedp)
2115     {
2116       if (!num_positive (lhs, precision))
2117 	negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
2118       if (!num_positive (rhs, precision))
2119 	negate = !negate, rhs = num_negate (rhs, precision);
2120     }
2121 
2122   /* Find the high bit.  */
2123   if (rhs.high)
2124     {
2125       i = precision - 1;
2126       mask = (cpp_num_part) 1 << (i - PART_PRECISION);
2127       for (; ; i--, mask >>= 1)
2128 	if (rhs.high & mask)
2129 	  break;
2130     }
2131   else if (rhs.low)
2132     {
2133       if (precision > PART_PRECISION)
2134 	i = precision - PART_PRECISION - 1;
2135       else
2136 	i = precision - 1;
2137       mask = (cpp_num_part) 1 << i;
2138       for (; ; i--, mask >>= 1)
2139 	if (rhs.low & mask)
2140 	  break;
2141     }
2142   else
2143     {
2144       if (!pfile->state.skip_eval)
2145 	cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2146 			     "division by zero in #if");
2147       return lhs;
2148     }
2149 
2150   /* First nonzero bit of RHS is bit I.  Do naive division by
2151      shifting the RHS fully left, and subtracting from LHS if LHS is
2152      at least as big, and then repeating but with one less shift.
2153      This is not very efficient, but is easy to understand.  */
2154 
2155   rhs.unsignedp = true;
2156   lhs.unsignedp = true;
2157   i = precision - i - 1;
2158   sub = num_lshift (rhs, precision, i);
2159 
2160   result.high = result.low = 0;
2161   for (;;)
2162     {
2163       if (num_greater_eq (lhs, sub, precision))
2164 	{
2165 	  lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2166 	  if (i >= PART_PRECISION)
2167 	    result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
2168 	  else
2169 	    result.low |= (cpp_num_part) 1 << i;
2170 	}
2171       if (i-- == 0)
2172 	break;
2173       sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2174       sub.high >>= 1;
2175     }
2176 
2177   /* We divide so that the remainder has the sign of the LHS.  */
2178   if (op == CPP_DIV)
2179     {
2180       result.unsignedp = unsignedp;
2181       result.overflow = false;
2182       if (!unsignedp)
2183 	{
2184 	  if (negate)
2185 	    result = num_negate (result, precision);
2186 	  result.overflow = (num_positive (result, precision) ^ !negate
2187 			     && !num_zerop (result));
2188 	}
2189 
2190       return result;
2191     }
2192 
2193   /* CPP_MOD.  */
2194   lhs.unsignedp = unsignedp;
2195   lhs.overflow = false;
2196   if (lhs_neg)
2197     lhs = num_negate (lhs, precision);
2198 
2199   return lhs;
2200 }
2201 
2202 /* Handle meeting "__has_include__" in a preprocessor expression.  */
2203 static cpp_num
2204 parse_has_include (cpp_reader *pfile, enum include_type type)
2205 {
2206   cpp_num result;
2207   bool paren = false;
2208   cpp_hashnode *node = 0;
2209   const cpp_token *token;
2210   bool bracket = false;
2211   char *fname = 0;
2212 
2213   result.unsignedp = false;
2214   result.high = 0;
2215   result.overflow = false;
2216   result.low = 0;
2217 
2218   pfile->state.in__has_include__++;
2219 
2220   token = cpp_get_token (pfile);
2221   if (token->type == CPP_OPEN_PAREN)
2222     {
2223       paren = true;
2224       token = cpp_get_token (pfile);
2225     }
2226 
2227   if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2228     {
2229       if (token->type == CPP_HEADER_NAME)
2230 	bracket = true;
2231       fname = XNEWVEC (char, token->val.str.len - 1);
2232       memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2233       fname[token->val.str.len - 2] = '\0';
2234       node = token->val.node.node;
2235     }
2236   else if (token->type == CPP_LESS)
2237     {
2238       bracket = true;
2239       fname = _cpp_bracket_include (pfile);
2240     }
2241   else
2242     cpp_error (pfile, CPP_DL_ERROR,
2243 	       "operator \"__has_include__\" requires a header string");
2244 
2245   if (fname)
2246     {
2247       int angle_brackets = (bracket ? 1 : 0);
2248 
2249       if (_cpp_has_header (pfile, fname, angle_brackets, type))
2250 	result.low = 1;
2251       else
2252 	result.low = 0;
2253 
2254       XDELETEVEC (fname);
2255     }
2256 
2257   if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2258     cpp_error (pfile, CPP_DL_ERROR,
2259 	       "missing ')' after \"__has_include__\"");
2260 
2261   /* A possible controlling macro of the form #if !__has_include__ ().
2262      _cpp_parse_expr checks there was no other junk on the line.  */
2263   if (node)
2264     pfile->mi_ind_cmacro = node;
2265 
2266   pfile->state.in__has_include__--;
2267 
2268   return result;
2269 }
2270