OSDN Git Service

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