blob: d56e56a631114b98ee8f0e49ae7acadce8fca8d3 [file] [log] [blame]
Zack Weinbergb0699da2000-03-07 20:58:47 +00001/* Parse C expressions for cpplib.
Neil Booth5d8ebbd2002-01-03 21:43:09 +00002 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +00003 2002, 2004, 2008, 2009, 2010, 2011 Free Software Foundation.
Richard Kennere38992e2000-04-18 20:42:00 +00004 Contributed by Per Bothner, 1994.
Per Bothner7f2935c1995-03-16 13:59:07 -08005
6This program is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +02008Free Software Foundation; either version 3, or (at your option) any
Per Bothner7f2935c1995-03-16 13:59:07 -08009later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020017along with this program; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
Per Bothner7f2935c1995-03-16 13:59:07 -080019
Per Bothner7f2935c1995-03-16 13:59:07 -080020#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000021#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000022#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000023#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080024
Neil Booth91318902002-05-26 18:42:21 +000025#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
Zack Weinbergcf00a882000-07-08 02:33:00 +000030struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000031{
Neil Booth68e652752002-07-20 13:31:56 +000032 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000033 cpp_num value; /* The value logically "right" of op. */
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +000034 source_location loc; /* The location of this value. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000035 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080036};
Per Bothner7f2935c1995-03-16 13:59:07 -080037
Neil Booth91318902002-05-26 18:42:21 +000038/* 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)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000041static bool num_positive (cpp_num, size_t);
42static bool num_greater_eq (cpp_num, cpp_num, size_t);
43static cpp_num num_trim (cpp_num, size_t);
44static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000045
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000046static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48static cpp_num num_negate (cpp_num, size_t);
49static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +000055static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
56 source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000057static cpp_num num_lshift (cpp_num, size_t, size_t);
58static cpp_num num_rshift (cpp_num, size_t, size_t);
Neil Booth91318902002-05-26 18:42:21 +000059
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000060static cpp_num append_digit (cpp_num, int, int, size_t);
61static cpp_num parse_defined (cpp_reader *);
62static cpp_num eval_token (cpp_reader *, const cpp_token *);
63static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64static unsigned int interpret_float_suffix (const uchar *, size_t);
65static unsigned int interpret_int_suffix (const uchar *, size_t);
66static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000067
Neil Boothcd7ab832002-05-29 17:15:42 +000068/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000069#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000071
Zack Weinberg15dad1d2000-05-18 15:55:46 +000072/* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000074#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000075 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000076#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000077 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000079
Neil Boothcd7ab832002-05-29 17:15:42 +000080/* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
83static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000084interpret_float_suffix (const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080085{
Janis Johnsoneec49112009-04-01 17:04:42 +000086 size_t flags;
Janis Johnson839a3b82009-04-01 17:31:26 +000087 size_t f, d, l, w, q, i;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020088
Janis Johnsoneec49112009-04-01 17:04:42 +000089 flags = 0;
Janis Johnson839a3b82009-04-01 17:31:26 +000090 f = d = l = w = q = i = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000091
Janis Johnsoneec49112009-04-01 17:04:42 +000092 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */
94 if (len == 2 && (*s == 'd' || *s == 'D'))
95 {
96 bool uppercase = (*s == 'D');
97 switch (s[1])
98 {
99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
105 default:
Janis Johnson839a3b82009-04-01 17:31:26 +0000106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
108 break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000109 }
110 }
111
112 /* Recognize a fixed-point suffix. */
113 switch (s[len-1])
114 {
115 case 'k': case 'K': flags = CPP_N_ACCUM; break;
116 case 'r': case 'R': flags = CPP_N_FRACT; break;
117 default: break;
118 }
119
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
122 if (flags)
123 {
124 if (len == 1)
125 return flags;
126 len--;
127
128 if (*s == 'u' || *s == 'U')
129 {
130 flags |= CPP_N_UNSIGNED;
131 if (len == 1)
132 return flags;
133 len--;
134 s++;
135 }
136
137 switch (*s)
138 {
139 case 'h': case 'H':
140 if (len == 1)
141 return flags |= CPP_N_SMALL;
142 break;
143 case 'l':
144 if (len == 1)
145 return flags |= CPP_N_MEDIUM;
146 if (len == 2 && s[1] == 'l')
147 return flags |= CPP_N_LARGE;
148 break;
149 case 'L':
150 if (len == 1)
151 return flags |= CPP_N_MEDIUM;
152 if (len == 2 && s[1] == 'L')
153 return flags |= CPP_N_LARGE;
154 break;
155 default:
156 break;
157 }
158 /* Anything left at this point is invalid. */
159 return 0;
160 }
161
162 /* In any remaining valid suffix, the case and order don't matter. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000163 while (len--)
164 switch (s[len])
165 {
Janis Johnsoneec49112009-04-01 17:04:42 +0000166 case 'f': case 'F': f++; break;
Janis Johnson839a3b82009-04-01 17:31:26 +0000167 case 'd': case 'D': d++; break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000168 case 'l': case 'L': l++; break;
169 case 'w': case 'W': w++; break;
170 case 'q': case 'Q': q++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000171 case 'i': case 'I':
172 case 'j': case 'J': i++; break;
173 default:
174 return 0;
175 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000176
Janis Johnson839a3b82009-04-01 17:31:26 +0000177 if (f + d + l + w + q > 1 || i > 1)
Jon Grimmad6ed772005-12-06 23:13:15 +0000178 return 0;
179
Neil Boothcd7ab832002-05-29 17:15:42 +0000180 return ((i ? CPP_N_IMAGINARY : 0)
181 | (f ? CPP_N_SMALL :
Janis Johnson839a3b82009-04-01 17:31:26 +0000182 d ? CPP_N_MEDIUM :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200183 l ? CPP_N_LARGE :
184 w ? CPP_N_MD_W :
Janis Johnson839a3b82009-04-01 17:31:26 +0000185 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
Neil Boothcd7ab832002-05-29 17:15:42 +0000186}
187
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000188/* Return the classification flags for a float suffix. */
189unsigned int
190cpp_interpret_float_suffix (const char *s, size_t len)
191{
192 return interpret_float_suffix ((const unsigned char *)s, len);
193}
194
Neil Boothcd7ab832002-05-29 17:15:42 +0000195/* Subroutine of cpp_classify_number. S points to an integer suffix
196 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
197 flag vector describing the suffix. */
198static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000199interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000200{
201 size_t u, l, i;
202
203 u = l = i = 0;
204
205 while (len--)
206 switch (s[len])
207 {
208 case 'u': case 'U': u++; break;
209 case 'i': case 'I':
210 case 'j': case 'J': i++; break;
211 case 'l': case 'L': l++;
212 /* If there are two Ls, they must be adjacent and the same case. */
213 if (l == 2 && s[len] != s[len + 1])
214 return 0;
215 break;
216 default:
217 return 0;
218 }
219
220 if (l > 2 || u > 1 || i > 1)
221 return 0;
222
223 return ((i ? CPP_N_IMAGINARY : 0)
224 | (u ? CPP_N_UNSIGNED : 0)
225 | ((l == 0) ? CPP_N_SMALL
226 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
227}
228
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000229/* Return the classification flags for an int suffix. */
230unsigned int
231cpp_interpret_int_suffix (const char *s, size_t len)
232{
233 return interpret_int_suffix ((const unsigned char *)s, len);
234}
235
236/* Return the string type corresponding to the the input user-defined string
237 literal type. If the input type is not a user-defined string literal
238 type return the input type. */
239enum cpp_ttype
240cpp_userdef_string_remove_type (enum cpp_ttype type)
241{
242 if (type == CPP_STRING_USERDEF)
243 return CPP_STRING;
244 else if (type == CPP_WSTRING_USERDEF)
245 return CPP_WSTRING;
246 else if (type == CPP_STRING16_USERDEF)
247 return CPP_STRING16;
248 else if (type == CPP_STRING32_USERDEF)
249 return CPP_STRING32;
250 else if (type == CPP_UTF8STRING_USERDEF)
251 return CPP_UTF8STRING;
252 else
253 return type;
254}
255
256/* Return the user-defined string literal type corresponding to the input
257 string type. If the input type is not a string type return the input
258 type. */
259enum cpp_ttype
260cpp_userdef_string_add_type (enum cpp_ttype type)
261{
262 if (type == CPP_STRING)
263 return CPP_STRING_USERDEF;
264 else if (type == CPP_WSTRING)
265 return CPP_WSTRING_USERDEF;
266 else if (type == CPP_STRING16)
267 return CPP_STRING16_USERDEF;
268 else if (type == CPP_STRING32)
269 return CPP_STRING32_USERDEF;
270 else if (type == CPP_UTF8STRING)
271 return CPP_UTF8STRING_USERDEF;
272 else
273 return type;
274}
275
276/* Return the char type corresponding to the the input user-defined char
277 literal type. If the input type is not a user-defined char literal
278 type return the input type. */
279enum cpp_ttype
280cpp_userdef_char_remove_type (enum cpp_ttype type)
281{
282 if (type == CPP_CHAR_USERDEF)
283 return CPP_CHAR;
284 else if (type == CPP_WCHAR_USERDEF)
285 return CPP_WCHAR;
286 else if (type == CPP_CHAR16_USERDEF)
Ed Smith-Rowland7e74ce32011-11-21 19:27:30 +0000287 return CPP_CHAR16;
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000288 else if (type == CPP_CHAR32_USERDEF)
Ed Smith-Rowland7e74ce32011-11-21 19:27:30 +0000289 return CPP_CHAR32;
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000290 else
291 return type;
292}
293
294/* Return the user-defined char literal type corresponding to the input
295 char type. If the input type is not a char type return the input
296 type. */
297enum cpp_ttype
298cpp_userdef_char_add_type (enum cpp_ttype type)
299{
300 if (type == CPP_CHAR)
301 return CPP_CHAR_USERDEF;
302 else if (type == CPP_WCHAR)
303 return CPP_WCHAR_USERDEF;
304 else if (type == CPP_CHAR16)
305 return CPP_CHAR16_USERDEF;
306 else if (type == CPP_CHAR32)
307 return CPP_CHAR32_USERDEF;
308 else
309 return type;
310}
311
312/* Return true if the token type is a user-defined string literal. */
313bool
314cpp_userdef_string_p (enum cpp_ttype type)
315{
316 if (type == CPP_STRING_USERDEF
317 || type == CPP_WSTRING_USERDEF
318 || type == CPP_STRING16_USERDEF
319 || type == CPP_STRING32_USERDEF
320 || type == CPP_UTF8STRING_USERDEF)
321 return true;
322 else
323 return false;
324}
325
326/* Return true if the token type is a user-defined char literal. */
327bool
328cpp_userdef_char_p (enum cpp_ttype type)
329{
330 if (type == CPP_CHAR_USERDEF
331 || type == CPP_WCHAR_USERDEF
332 || type == CPP_CHAR16_USERDEF
333 || type == CPP_CHAR32_USERDEF)
334 return true;
335 else
336 return false;
337}
338
339/* Extract the suffix from a user-defined literal string or char. */
340const char *
341cpp_get_userdef_suffix (const cpp_token *tok)
342{
343 unsigned int len = tok->val.str.len;
344 const char *text = (const char *)tok->val.str.text;
345 char delim;
346 unsigned int i;
347 for (i = 0; i < len; ++i)
348 if (text[i] == '\'' || text[i] == '"')
349 break;
350 if (i == len)
351 return text + len;
352 delim = text[i];
353 for (i = len; i > 0; --i)
354 if (text[i - 1] == delim)
355 break;
356 return text + i;
357}
358
Neil Boothcd7ab832002-05-29 17:15:42 +0000359/* Categorize numeric constants according to their field (integer,
360 floating point, or invalid), radix (decimal, octal, hexadecimal),
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000361 and type suffixes. In C++0X if UD_SUFFIX is non null it will be
362 assigned any unrecognized suffix for a user-defined literal. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000363unsigned int
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000364cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
365 const char **ud_suffix)
Neil Boothcd7ab832002-05-29 17:15:42 +0000366{
367 const uchar *str = token->val.str.text;
368 const uchar *limit;
369 unsigned int max_digit, result, radix;
370 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000371 bool seen_digit;
Neil Boothcd7ab832002-05-29 17:15:42 +0000372
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000373 if (ud_suffix)
374 *ud_suffix = NULL;
375
Neil Boothcd7ab832002-05-29 17:15:42 +0000376 /* If the lexer has done its job, length one can only be a single
377 digit. Fast-path this very common case. */
378 if (token->val.str.len == 1)
379 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
380
381 limit = str + token->val.str.len;
382 float_flag = NOT_FLOAT;
383 max_digit = 0;
384 radix = 10;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000385 seen_digit = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000386
387 /* First, interpret the radix. */
388 if (*str == '0')
389 {
390 radix = 8;
391 str++;
392
393 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000394 if ((*str == 'x' || *str == 'X')
395 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000396 {
397 radix = 16;
398 str++;
399 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000400 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
401 {
402 radix = 2;
403 str++;
404 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000405 }
406
407 /* Now scan for a well-formed integer or float. */
408 for (;;)
409 {
410 unsigned int c = *str++;
411
412 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
413 {
Joseph Myersdadab4f2010-01-01 18:08:17 +0000414 seen_digit = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000415 c = hex_value (c);
416 if (c > max_digit)
417 max_digit = c;
418 }
419 else if (c == '.')
420 {
421 if (float_flag == NOT_FLOAT)
422 float_flag = AFTER_POINT;
423 else
424 SYNTAX_ERROR ("too many decimal points in number");
425 }
426 else if ((radix <= 10 && (c == 'e' || c == 'E'))
427 || (radix == 16 && (c == 'p' || c == 'P')))
428 {
429 float_flag = AFTER_EXPON;
430 break;
431 }
432 else
433 {
434 /* Start of suffix. */
435 str--;
436 break;
437 }
438 }
439
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000440 /* The suffix may be for decimal fixed-point constants without exponent. */
441 if (radix != 16 && float_flag == NOT_FLOAT)
442 {
443 result = interpret_float_suffix (str, limit - str);
444 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
445 {
446 result |= CPP_N_FLOATING;
447 /* We need to restore the radix to 10, if the radix is 8. */
448 if (radix == 8)
449 radix = 10;
450
451 if (CPP_PEDANTIC (pfile))
452 cpp_error (pfile, CPP_DL_PEDWARN,
453 "fixed-point constants are a GCC extension");
454 goto syntax_ok;
455 }
456 else
457 result = 0;
458 }
459
Neil Boothcd7ab832002-05-29 17:15:42 +0000460 if (float_flag != NOT_FLOAT && radix == 8)
461 radix = 10;
462
463 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000464 {
465 if (radix == 2)
466 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
467 else
468 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
469 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000470
471 if (float_flag != NOT_FLOAT)
472 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000473 if (radix == 2)
474 {
475 cpp_error (pfile, CPP_DL_ERROR,
476 "invalid prefix \"0b\" for floating constant");
477 return CPP_N_INVALID;
478 }
479
Joseph Myersdadab4f2010-01-01 18:08:17 +0000480 if (radix == 16 && !seen_digit)
481 SYNTAX_ERROR ("no digits in hexadecimal floating constant");
482
Neil Boothcd7ab832002-05-29 17:15:42 +0000483 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000484 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000485 "use of C99 hexadecimal floating constant");
486
487 if (float_flag == AFTER_EXPON)
488 {
489 if (*str == '+' || *str == '-')
490 str++;
491
492 /* Exponent is decimal, even if string is a hex float. */
493 if (!ISDIGIT (*str))
494 SYNTAX_ERROR ("exponent has no digits");
495
496 do
497 str++;
498 while (ISDIGIT (*str));
499 }
500 else if (radix == 16)
501 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
502
503 result = interpret_float_suffix (str, limit - str);
504 if (result == 0)
505 {
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000506 if (CPP_OPTION (pfile, user_literals))
507 {
508 if (ud_suffix)
509 *ud_suffix = (const char *) str;
510 result = CPP_N_LARGE | CPP_N_USERDEF;
511 }
512 else
513 {
514 cpp_error (pfile, CPP_DL_ERROR,
515 "invalid suffix \"%.*s\" on floating constant",
516 (int) (limit - str), str);
517 return CPP_N_INVALID;
518 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000519 }
520
521 /* Traditional C didn't accept any floating suffixes. */
522 if (limit != str
523 && CPP_WTRADITIONAL (pfile)
524 && ! cpp_sys_macro_p (pfile))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000525 cpp_warning (pfile, CPP_W_TRADITIONAL,
526 "traditional C rejects the \"%.*s\" suffix",
527 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000528
Janis Johnson839a3b82009-04-01 17:31:26 +0000529 /* A suffix for double is a GCC extension via decimal float support.
530 If the suffix also specifies an imaginary value we'll catch that
531 later. */
532 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
533 cpp_error (pfile, CPP_DL_PEDWARN,
534 "suffix for double constant is a GCC extension");
535
Jon Grimmad6ed772005-12-06 23:13:15 +0000536 /* Radix must be 10 for decimal floats. */
537 if ((result & CPP_N_DFLOAT) && radix != 10)
538 {
539 cpp_error (pfile, CPP_DL_ERROR,
540 "invalid suffix \"%.*s\" with hexadecimal floating constant",
541 (int) (limit - str), str);
542 return CPP_N_INVALID;
543 }
544
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000545 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
546 cpp_error (pfile, CPP_DL_PEDWARN,
547 "fixed-point constants are a GCC extension");
548
Janis Johnson5a6bb572007-05-14 23:45:40 +0000549 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
550 cpp_error (pfile, CPP_DL_PEDWARN,
551 "decimal float constants are a GCC extension");
552
Neil Boothcd7ab832002-05-29 17:15:42 +0000553 result |= CPP_N_FLOATING;
554 }
555 else
556 {
557 result = interpret_int_suffix (str, limit - str);
558 if (result == 0)
559 {
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000560 if (CPP_OPTION (pfile, user_literals))
561 {
562 if (ud_suffix)
563 *ud_suffix = (const char *) str;
564 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
565 }
566 else
567 {
568 cpp_error (pfile, CPP_DL_ERROR,
569 "invalid suffix \"%.*s\" on integer constant",
570 (int) (limit - str), str);
571 return CPP_N_INVALID;
572 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000573 }
574
Zack Weinberg56da7202002-08-02 04:18:16 +0000575 /* Traditional C only accepted the 'L' suffix.
576 Suppress warning about 'LL' with -Wno-long-long. */
577 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
578 {
579 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
Simon Baldwin87cf0652010-04-07 17:18:10 +0000580 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
Joseph Myerse3339d02010-09-29 15:49:14 +0100581 && CPP_OPTION (pfile, cpp_warn_long_long);
Zack Weinberg56da7202002-08-02 04:18:16 +0000582
Simon Baldwin87cf0652010-04-07 17:18:10 +0000583 if (u_or_i || large)
584 cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
585 "traditional C rejects the \"%.*s\" suffix",
586 (int) (limit - str), str);
Zack Weinberg56da7202002-08-02 04:18:16 +0000587 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000588
589 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
Joseph Myerse3339d02010-09-29 15:49:14 +0100590 && CPP_OPTION (pfile, cpp_warn_long_long))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000591 {
592 const char *message = CPP_OPTION (pfile, cplusplus)
593 ? N_("use of C++0x long long integer constant")
594 : N_("use of C99 long long integer constant");
595
596 if (CPP_OPTION (pfile, c99))
597 cpp_warning (pfile, CPP_W_LONG_LONG, message);
598 else
599 cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
600 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000601
602 result |= CPP_N_INTEGER;
603 }
604
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000605 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000606 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000607 cpp_error (pfile, CPP_DL_PEDWARN,
608 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000609 if (radix == 2 && CPP_PEDANTIC (pfile))
610 cpp_error (pfile, CPP_DL_PEDWARN,
611 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000612
613 if (radix == 10)
614 result |= CPP_N_DECIMAL;
615 else if (radix == 16)
616 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000617 else if (radix == 2)
618 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000619 else
620 result |= CPP_N_OCTAL;
621
622 return result;
623
624 syntax_error:
625 return CPP_N_INVALID;
626}
627
628/* cpp_interpret_integer converts an integer constant into a cpp_num,
629 of precision options->precision.
630
631 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000632 because the preprocessor doesn't need it and we don't want to
633 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000634cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000635cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
636 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000637{
638 const uchar *p, *end;
639 cpp_num result;
640
641 result.low = 0;
642 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000643 result.unsignedp = !!(type & CPP_N_UNSIGNED);
644 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000645
646 p = token->val.str.text;
647 end = p + token->val.str.len;
648
649 /* Common case of a single digit. */
650 if (token->val.str.len == 1)
651 result.low = p[0] - '0';
652 else
653 {
654 cpp_num_part max;
655 size_t precision = CPP_OPTION (pfile, precision);
656 unsigned int base = 10, c = 0;
657 bool overflow = false;
658
659 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
660 {
661 base = 8;
662 p++;
663 }
664 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
665 {
666 base = 16;
667 p += 2;
668 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000669 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
670 {
671 base = 2;
672 p += 2;
673 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000674
675 /* We can add a digit to numbers strictly less than this without
676 needing the precision and slowness of double integers. */
677 max = ~(cpp_num_part) 0;
678 if (precision < PART_PRECISION)
679 max >>= PART_PRECISION - precision;
680 max = (max - base + 1) / base + 1;
681
682 for (; p < end; p++)
683 {
684 c = *p;
685
686 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
687 c = hex_value (c);
688 else
689 break;
690
691 /* Strict inequality for when max is set to zero. */
692 if (result.low < max)
693 result.low = result.low * base + c;
694 else
695 {
696 result = append_digit (result, c, base, precision);
697 overflow |= result.overflow;
698 max = 0;
699 }
700 }
701
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000702 if (overflow && !(type & CPP_N_USERDEF))
John David Anglin0527bc42003-11-01 22:56:54 +0000703 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000704 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000705 /* If too big to be signed, consider it unsigned. Only warn for
706 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000707 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000708 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000709 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000710 && !(CPP_OPTION (pfile, traditional)
711 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000712 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000713 {
Joseph Myersf88d0772009-04-25 19:46:03 +0100714 /* This is for constants within the range of uintmax_t but
Joseph Myers813b9e72009-04-25 19:59:20 +0100715 not that of intmax_t. For such decimal constants, a
Joseph Myersf88d0772009-04-25 19:46:03 +0100716 diagnostic is required for C99 as the selected type must
717 be signed and not having a type is a constraint violation
718 (DR#298, TC3), so this must be a pedwarn. For C90,
719 unsigned long is specified to be used for a constant that
720 does not fit in signed long; if uintmax_t has the same
721 range as unsigned long this means only a warning is
722 appropriate here. C90 permits the preprocessor to use a
723 wider range than unsigned long in the compiler, so if
724 uintmax_t is wider than unsigned long no diagnostic is
725 required for such constants in preprocessor #if
726 expressions and the compiler will pedwarn for such
727 constants outside the range of unsigned long that reach
728 the compiler so a diagnostic is not required there
729 either; thus, pedwarn for C99 but use a plain warning for
730 C90. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000731 if (base == 10)
Joseph Myersf88d0772009-04-25 19:46:03 +0100732 cpp_error (pfile, (CPP_OPTION (pfile, c99)
733 ? CPP_DL_PEDWARN
734 : CPP_DL_WARNING),
Neil Boothcd7ab832002-05-29 17:15:42 +0000735 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000736 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000737 }
738 }
739
740 return result;
741}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000742
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000743/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000744static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000745append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000746{
747 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000748 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000749 bool overflow;
750 cpp_num_part add_high, add_low;
751
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000752 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000753 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000754 switch (base)
755 {
756 case 2:
757 shift = 1;
758 break;
759
760 case 16:
761 shift = 4;
762 break;
763
764 default:
765 shift = 3;
766 }
Neil Booth23ff0222002-07-17 17:27:14 +0000767 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000768 result.high = num.high << shift;
769 result.low = num.low << shift;
770 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400771 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000772
773 if (base == 10)
774 {
775 add_low = num.low << 1;
776 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
777 }
778 else
779 add_high = add_low = 0;
780
781 if (add_low + digit < add_low)
782 add_high++;
783 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000784
Neil Booth91318902002-05-26 18:42:21 +0000785 if (result.low + add_low < result.low)
786 add_high++;
787 if (result.high + add_high < result.high)
788 overflow = true;
789
790 result.low += add_low;
791 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400792 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000793
794 /* The above code catches overflow of a cpp_num type. This catches
795 overflow of the (possibly shorter) target precision. */
796 num.low = result.low;
797 num.high = result.high;
798 result = num_trim (result, precision);
799 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400800 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000801
Neil Booth91318902002-05-26 18:42:21 +0000802 return result;
803}
804
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000805/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000806static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000807parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000808{
Neil Booth91318902002-05-26 18:42:21 +0000809 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000810 int paren = 0;
811 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000812 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000813 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000814
Neil Booth93c803682000-10-28 17:59:06 +0000815 /* Don't expand macros. */
816 pfile->state.prevent_expansion++;
817
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000818 token = cpp_get_token (pfile);
819 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000820 {
821 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000822 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000823 }
824
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000825 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000826 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100827 node = token->val.node.node;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000828 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000829 {
John David Anglin0527bc42003-11-01 22:56:54 +0000830 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000831 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000832 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000833 }
Neil Booth93c803682000-10-28 17:59:06 +0000834 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000835 {
John David Anglin0527bc42003-11-01 22:56:54 +0000836 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000837 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000838 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000839 {
840 cpp_token op;
841
842 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000843 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000844 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000845 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000846 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000847 cpp_token_as_text (pfile, &op));
848 }
849 }
Neil Booth93c803682000-10-28 17:59:06 +0000850
Neil Booth91318902002-05-26 18:42:21 +0000851 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000852 {
Neil Booth335d03e2003-08-03 12:23:46 +0000853 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000854 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000855 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000856
Neil Bootha69cbaa2002-07-23 22:57:49 +0000857 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100858 if (!(node->flags & NODE_USED))
859 {
860 node->flags |= NODE_USED;
861 if (node->type == NT_MACRO)
862 {
Jakub Jelineka69d2522010-09-01 00:47:25 +0200863 if ((node->flags & NODE_BUILTIN)
864 && pfile->cb.user_builtin_macro)
865 pfile->cb.user_builtin_macro (pfile, node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100866 if (pfile->cb.used_define)
867 pfile->cb.used_define (pfile, pfile->directive_line, node);
868 }
869 else
870 {
871 if (pfile->cb.used_undef)
872 pfile->cb.used_undef (pfile, pfile->directive_line, node);
873 }
874 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000875
Neil Booth6d18adb2001-07-29 17:27:57 +0000876 /* A possible controlling macro of the form #if !defined ().
877 _cpp_parse_expr checks there was no other junk on the line. */
878 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000879 }
880
881 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000882
Michael Meissnerf3c33d92011-03-21 16:21:30 +0000883 /* Do not treat conditional macros as being defined. This is due to the
884 powerpc and spu ports using conditional macros for 'vector', 'bool', and
885 'pixel' to act as conditional keywords. This messes up tests like #ifndef
886 bool. */
Neil Booth23ff0222002-07-17 17:27:14 +0000887 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000888 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000889 result.overflow = false;
Michael Meissnerf3c33d92011-03-21 16:21:30 +0000890 result.low = (node && node->type == NT_MACRO
891 && (node->flags & NODE_CONDITIONAL) == 0);
Neil Booth91318902002-05-26 18:42:21 +0000892 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000893}
894
Neil Booth60284a52002-04-28 23:14:56 +0000895/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
896 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000897 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000898static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000899eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800900{
Neil Booth91318902002-05-26 18:42:21 +0000901 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000902 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000903 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000904
Diego Novillo6de9cd92004-05-13 02:41:07 -0400905 result.unsignedp = false;
906 result.overflow = false;
907
Neil Booth93c803682000-10-28 17:59:06 +0000908 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000909 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800910 case CPP_NUMBER:
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000911 temp = cpp_classify_number (pfile, token, NULL);
912 if (temp & CPP_N_USERDEF)
913 cpp_error (pfile, CPP_DL_ERROR,
914 "user-defined literal in preprocessor expression");
Neil Boothcd7ab832002-05-29 17:15:42 +0000915 switch (temp & CPP_N_CATEGORY)
916 {
917 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000918 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000919 "floating constant in preprocessor expression");
920 break;
921 case CPP_N_INTEGER:
922 if (!(temp & CPP_N_IMAGINARY))
923 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000924 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000925 "imaginary number in preprocessor expression");
926 break;
927
928 case CPP_N_INVALID:
929 /* Error already issued. */
930 break;
931 }
932 result.high = result.low = 0;
933 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000934
Alexandre Oliva525bc952000-02-23 19:21:07 +0000935 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000936 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000937 case CPP_CHAR16:
938 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +0000939 {
Neil Booth91318902002-05-26 18:42:21 +0000940 cppchar_t cc = cpp_interpret_charconst (pfile, token,
941 &temp, &unsignedp);
942
943 result.high = 0;
944 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000945 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000946 if (!unsignedp && (cppchar_signed_t) cc < 0)
947 {
948 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
949 result.low |= ~(~(cpp_num_part) 0
950 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
951 result.high = ~(cpp_num_part) 0;
952 result = num_trim (result, CPP_OPTION (pfile, precision));
953 }
Neil Bootha5a49442002-05-06 22:53:10 +0000954 }
Neil Booth60284a52002-04-28 23:14:56 +0000955 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000956
Zack Weinberg92936ec2000-07-19 20:18:08 +0000957 case CPP_NAME:
Joseph Myers9a0c6182009-05-10 15:27:32 +0100958 if (token->val.node.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000959 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000960 else if (CPP_OPTION (pfile, cplusplus)
Joseph Myers9a0c6182009-05-10 15:27:32 +0100961 && (token->val.node.node == pfile->spec_nodes.n_true
962 || token->val.node.node == pfile->spec_nodes.n_false))
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000963 {
Neil Booth91318902002-05-26 18:42:21 +0000964 result.high = 0;
Joseph Myers9a0c6182009-05-10 15:27:32 +0100965 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000966 }
967 else
968 {
Neil Booth91318902002-05-26 18:42:21 +0000969 result.high = 0;
970 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000971 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
Simon Baldwin87cf0652010-04-07 17:18:10 +0000972 cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
973 NODE_NAME (token->val.node.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000974 }
Neil Booth60284a52002-04-28 23:14:56 +0000975 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000976
Tom Tromey899015a2008-05-13 14:50:27 +0000977 case CPP_HASH:
978 if (!pfile->state.skipping)
979 {
980 /* A pedantic warning takes precedence over a deprecated
981 warning here. */
982 if (CPP_PEDANTIC (pfile))
983 cpp_error (pfile, CPP_DL_PEDWARN,
984 "assertions are a GCC extension");
Joseph Myerse3339d02010-09-29 15:49:14 +0100985 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000986 cpp_warning (pfile, CPP_W_DEPRECATED,
987 "assertions are a deprecated extension");
Tom Tromey899015a2008-05-13 14:50:27 +0000988 }
Neil Booth91318902002-05-26 18:42:21 +0000989 _cpp_test_assertion (pfile, &temp);
990 result.high = 0;
991 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +0000992 break;
993
994 default:
995 abort ();
Neil Booth93c803682000-10-28 17:59:06 +0000996 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800997
Neil Booth23ff0222002-07-17 17:27:14 +0000998 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000999 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -08001000}
1001
Neil Booth4063b942000-04-02 08:27:23 +00001002/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +00001003
1004After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +00001005than the operator on the top of the stack, we reduce the stack by one
1006operator and repeat the test. Since equal priorities do not reduce,
1007this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +00001008
Neil Booth87ed1092002-04-28 19:42:54 +00001009We handle left-associative operators by decrementing the priority of
1010just-lexed operators by one, but retaining the priority of operators
1011already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +00001012
1013The remaining cases are '(' and ')'. We handle '(' by skipping the
1014reduction phase completely. ')' is given lower priority than
1015everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +00001016parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +00001017reduce() exits immediately. If the normal exit route sees a ')', then
1018there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +00001019
Neil Boothf8b954f2002-04-26 06:32:50 +00001020The parser assumes all shifted operators require a left operand unless
1021the flag NO_L_OPERAND is set. These semantics are automatic; any
1022extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001023
Neil Booth68e652752002-07-20 13:31:56 +00001024/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1025 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +00001026#define NO_L_OPERAND (1 << 0)
1027#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +00001028#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +00001029
Zack Weinbergcf00a882000-07-08 02:33:00 +00001030/* Operator to priority map. Must be in the same order as the first
1031 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001032static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +00001033{
Neil Booth60284a52002-04-28 23:14:56 +00001034 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +00001035 uchar flags;
1036} optab[] =
1037{
Neil Boothad28cff2002-07-18 22:08:35 +00001038 /* EQ */ {0, 0}, /* Shouldn't happen. */
1039 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +00001040 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1041 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1042 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1043 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1044 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1045 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1046 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1047 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1048 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1049 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +00001050 /* RSHIFT */ {13, LEFT_ASSOC},
1051 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +00001052
Neil Boothad28cff2002-07-18 22:08:35 +00001053 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +00001054 /* AND_AND */ {6, LEFT_ASSOC},
1055 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +00001056 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1057 However, there are some special cases for these in reduce(). */
1058 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +00001059 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +00001060 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +00001061 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +00001062 /* CLOSE_PAREN */ {0, 0},
1063 /* EOF */ {0, 0},
1064 /* EQ_EQ */ {11, LEFT_ASSOC},
1065 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +00001066 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1067 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +00001068 /* UPLUS */ {16, NO_L_OPERAND},
1069 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +00001070};
1071
Per Bothner7f2935c1995-03-16 13:59:07 -08001072/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +00001073 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +00001074
1075 The implementation is an operator precedence parser, i.e. a
1076 bottom-up parser, using a stack for not-yet-reduced tokens.
1077
1078 The stack base is op_stack, and the current stack pointer is 'top'.
1079 There is a stack element for each operator (only), and the most
1080 recently pushed operator is 'top->op'. An operand (value) is
1081 stored in the 'value' field of the stack element of the operator
1082 that precedes it. */
1083bool
Tom Tromeyd7508872008-05-30 14:25:09 +00001084_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -08001085{
Neil Booth87ed1092002-04-28 19:42:54 +00001086 struct op *top = pfile->op_stack;
1087 unsigned int lex_count;
1088 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -08001089
Neil Booth87ed1092002-04-28 19:42:54 +00001090 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001091
Neil Booth93c803682000-10-28 17:59:06 +00001092 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +00001093 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +00001094 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +00001095 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001096
Neil Booth87ed1092002-04-28 19:42:54 +00001097 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +00001098 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +00001099
Per Bothner7f2935c1995-03-16 13:59:07 -08001100 for (;;)
1101 {
Zack Weinbergcf00a882000-07-08 02:33:00 +00001102 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -08001103
Neil Booth6d18adb2001-07-29 17:27:57 +00001104 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +00001105 op.token = cpp_get_token (pfile);
1106 op.op = op.token->type;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001107 op.loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001108
Per Bothner7f2935c1995-03-16 13:59:07 -08001109 switch (op.op)
1110 {
Neil Booth60284a52002-04-28 23:14:56 +00001111 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +00001112 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +00001113 case CPP_CHAR:
1114 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +00001115 case CPP_CHAR16:
1116 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +00001117 case CPP_NAME:
1118 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +00001119 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +00001120 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +00001121 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +00001122 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +00001123 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +00001124 continue;
1125
Neil Booth6d18adb2001-07-29 17:27:57 +00001126 case CPP_NOT:
1127 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +00001128 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +00001129 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +00001130 if (want_value)
1131 op.op = CPP_UPLUS;
1132 break;
1133 case CPP_MINUS:
1134 if (want_value)
1135 op.op = CPP_UMINUS;
1136 break;
Neil Booth60284a52002-04-28 23:14:56 +00001137
Neil Boothf8b954f2002-04-26 06:32:50 +00001138 default:
Neil Booth60284a52002-04-28 23:14:56 +00001139 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +00001140 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +00001141 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +00001142 break;
Per Bothner7f2935c1995-03-16 13:59:07 -08001143 }
1144
Neil Booth87ed1092002-04-28 19:42:54 +00001145 /* Check we have a value or operator as appropriate. */
1146 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +00001147 {
Neil Boothf8b954f2002-04-26 06:32:50 +00001148 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +00001149 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +00001150 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +00001151 }
Neil Booth87ed1092002-04-28 19:42:54 +00001152 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +00001153 {
Neil Bootha09d4742004-07-04 12:57:50 +00001154 /* We want a number (or expression) and haven't got one.
1155 Try to emit a specific diagnostic. */
1156 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
1157 SYNTAX_ERROR ("missing expression between '(' and ')'");
1158
1159 if (op.op == CPP_EOF && top->op == CPP_EOF)
Tom Tromeyd7508872008-05-30 14:25:09 +00001160 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +00001161
1162 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
1163 SYNTAX_ERROR2 ("operator '%s' has no right operand",
1164 cpp_token_as_text (pfile, top->token));
1165 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1166 /* Complain about missing paren during reduction. */;
1167 else
1168 SYNTAX_ERROR2 ("operator '%s' has no left operand",
1169 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +00001170 }
Neil Booth87ed1092002-04-28 19:42:54 +00001171
1172 top = reduce (pfile, top, op.op);
1173 if (!top)
1174 goto syntax_error;
1175
Neil Booth60284a52002-04-28 23:14:56 +00001176 if (op.op == CPP_EOF)
1177 break;
1178
Neil Booth87ed1092002-04-28 19:42:54 +00001179 switch (op.op)
1180 {
1181 case CPP_CLOSE_PAREN:
1182 continue;
Neil Booth87ed1092002-04-28 19:42:54 +00001183 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +00001184 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001185 pfile->state.skip_eval++;
1186 break;
1187 case CPP_AND_AND:
1188 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +00001189 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001190 pfile->state.skip_eval++;
1191 break;
1192 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +00001193 if (top->op != CPP_QUERY)
1194 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +00001195 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +00001196 pfile->state.skip_eval++;
1197 else
1198 pfile->state.skip_eval--;
1199 default:
1200 break;
1201 }
1202
Neil Boothf8b954f2002-04-26 06:32:50 +00001203 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +00001204
Mike Stump0f413021996-07-03 22:07:53 +00001205 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +00001206 if (++top == pfile->op_limit)
1207 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +00001208
Per Bothner7f2935c1995-03-16 13:59:07 -08001209 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +00001210 top->token = op.token;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001211 top->loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001212 }
Neil Booth9ee703132000-04-01 07:42:37 +00001213
Neil Booth6d18adb2001-07-29 17:27:57 +00001214 /* The controlling macro expression is only valid if we called lex 3
1215 times: <!> <defined expression> and <EOF>. push_conditional ()
1216 checks that we are at top-of-file. */
1217 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1218 pfile->mi_ind_cmacro = 0;
1219
Neil Booth87ed1092002-04-28 19:42:54 +00001220 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +00001221 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001222 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1223 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +00001224 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +00001225 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +00001226 }
Neil Booth9ee703132000-04-01 07:42:37 +00001227
Neil Booth91318902002-05-26 18:42:21 +00001228 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001229}
1230
1231/* Reduce the operator / value stack if possible, in preparation for
1232 pushing operator OP. Returns NULL on error, otherwise the top of
1233 the stack. */
1234static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001235reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001236{
1237 unsigned int prio;
1238
Neil Booth91318902002-05-26 18:42:21 +00001239 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1240 {
1241 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001242 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001243 return 0;
1244 }
1245
Neil Booth87ed1092002-04-28 19:42:54 +00001246 if (op == CPP_OPEN_PAREN)
1247 return top;
1248
1249 /* Decrement the priority of left-associative operators to force a
1250 reduction with operators of otherwise equal priority. */
1251 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1252 while (prio < optab[top->op].prio)
1253 {
Neil Booth68e652752002-07-20 13:31:56 +00001254 if (CPP_OPTION (pfile, warn_num_sign_change)
1255 && optab[top->op].flags & CHECK_PROMOTION)
1256 check_promotion (pfile, top);
1257
Neil Booth75aef482002-07-19 19:24:43 +00001258 switch (top->op)
1259 {
1260 case CPP_UPLUS:
1261 case CPP_UMINUS:
1262 case CPP_NOT:
1263 case CPP_COMPL:
1264 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001265 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001266 break;
Neil Booth91318902002-05-26 18:42:21 +00001267
Neil Booth75aef482002-07-19 19:24:43 +00001268 case CPP_PLUS:
1269 case CPP_MINUS:
1270 case CPP_RSHIFT:
1271 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001272 case CPP_COMMA:
1273 top[-1].value = num_binary_op (pfile, top[-1].value,
1274 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001275 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001276 break;
Neil Booth91318902002-05-26 18:42:21 +00001277
Neil Booth75aef482002-07-19 19:24:43 +00001278 case CPP_GREATER:
1279 case CPP_LESS:
1280 case CPP_GREATER_EQ:
1281 case CPP_LESS_EQ:
1282 top[-1].value
1283 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001284 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001285 break;
Neil Booth91318902002-05-26 18:42:21 +00001286
Neil Booth75aef482002-07-19 19:24:43 +00001287 case CPP_EQ_EQ:
1288 case CPP_NOT_EQ:
1289 top[-1].value
1290 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001291 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001292 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001293
Neil Booth75aef482002-07-19 19:24:43 +00001294 case CPP_AND:
1295 case CPP_OR:
1296 case CPP_XOR:
1297 top[-1].value
1298 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001299 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001300 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001301
Neil Booth75aef482002-07-19 19:24:43 +00001302 case CPP_MULT:
1303 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001304 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001305 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001306
Neil Booth75aef482002-07-19 19:24:43 +00001307 case CPP_DIV:
1308 case CPP_MOD:
1309 top[-1].value = num_div_op (pfile, top[-1].value,
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001310 top->value, top->op, top->loc);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001311 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001312 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001313
Neil Booth75aef482002-07-19 19:24:43 +00001314 case CPP_OR_OR:
1315 top--;
1316 if (!num_zerop (top->value))
1317 pfile->state.skip_eval--;
1318 top->value.low = (!num_zerop (top->value)
1319 || !num_zerop (top[1].value));
1320 top->value.high = 0;
1321 top->value.unsignedp = false;
1322 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001323 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001324 continue;
1325
1326 case CPP_AND_AND:
1327 top--;
1328 if (num_zerop (top->value))
1329 pfile->state.skip_eval--;
1330 top->value.low = (!num_zerop (top->value)
1331 && !num_zerop (top[1].value));
1332 top->value.high = 0;
1333 top->value.unsignedp = false;
1334 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001335 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001336 continue;
1337
1338 case CPP_OPEN_PAREN:
1339 if (op != CPP_CLOSE_PAREN)
1340 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001341 cpp_error_with_line (pfile, CPP_DL_ERROR,
1342 top->token->src_loc,
1343 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001344 return 0;
1345 }
1346 top--;
1347 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001348 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001349 return top;
1350
1351 case CPP_COLON:
1352 top -= 2;
1353 if (!num_zerop (top->value))
1354 {
Neil Booth91318902002-05-26 18:42:21 +00001355 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001356 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001357 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001358 }
1359 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001360 {
1361 top->value = top[2].value;
1362 top->loc = top[2].loc;
1363 }
Neil Booth75aef482002-07-19 19:24:43 +00001364 top->value.unsignedp = (top[1].value.unsignedp
1365 || top[2].value.unsignedp);
1366 continue;
Neil Booth91318902002-05-26 18:42:21 +00001367
Neil Booth75aef482002-07-19 19:24:43 +00001368 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001369 /* COMMA and COLON should not reduce a QUERY operator. */
1370 if (op == CPP_COMMA || op == CPP_COLON)
1371 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001372 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001373 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001374
Neil Booth75aef482002-07-19 19:24:43 +00001375 default:
1376 goto bad_op;
1377 }
Neil Boothad28cff2002-07-18 22:08:35 +00001378
1379 top--;
Neil Booth91318902002-05-26 18:42:21 +00001380 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001381 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001382 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001383 }
1384
1385 if (op == CPP_CLOSE_PAREN)
1386 {
John David Anglin0527bc42003-11-01 22:56:54 +00001387 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001388 return 0;
1389 }
1390
1391 return top;
1392}
1393
1394/* Returns the position of the old top of stack after expansion. */
1395struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001396_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001397{
Neil Booth32fa4562002-05-09 22:27:31 +00001398 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1399 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001400
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001401 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001402 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001403
Neil Booth32fa4562002-05-09 22:27:31 +00001404 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001405}
Neil Booth91318902002-05-26 18:42:21 +00001406
Neil Booth68e652752002-07-20 13:31:56 +00001407/* Emits a warning if the effective sign of either operand of OP
1408 changes because of integer promotions. */
1409static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001410check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001411{
1412 if (op->value.unsignedp == op[-1].value.unsignedp)
1413 return;
1414
1415 if (op->value.unsignedp)
1416 {
1417 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001418 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1419 "the left operand of \"%s\" changes sign when promoted",
1420 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001421 }
1422 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001423 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001424 "the right operand of \"%s\" changes sign when promoted",
1425 cpp_token_as_text (pfile, op->token));
1426}
1427
Neil Booth91318902002-05-26 18:42:21 +00001428/* Clears the unused high order bits of the number pointed to by PNUM. */
1429static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001430num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001431{
1432 if (precision > PART_PRECISION)
1433 {
1434 precision -= PART_PRECISION;
1435 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001436 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001437 }
1438 else
1439 {
1440 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001441 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001442 num.high = 0;
1443 }
1444
1445 return num;
1446}
1447
1448/* True iff A (presumed signed) >= 0. */
1449static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001450num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001451{
1452 if (precision > PART_PRECISION)
1453 {
1454 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001455 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001456 }
1457
Neil Booth359b0be2002-05-28 05:44:06 +00001458 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001459}
1460
Neil Boothceeedfc2002-06-02 19:37:34 +00001461/* Sign extend a number, with PRECISION significant bits and all
1462 others assumed clear, to fill out a cpp_num structure. */
1463cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001464cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001465{
1466 if (!num.unsignedp)
1467 {
1468 if (precision > PART_PRECISION)
1469 {
1470 precision -= PART_PRECISION;
1471 if (precision < PART_PRECISION
1472 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1473 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1474 }
1475 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1476 {
1477 if (precision < PART_PRECISION)
1478 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1479 num.high = ~(cpp_num_part) 0;
1480 }
1481 }
1482
1483 return num;
1484}
1485
Neil Booth91318902002-05-26 18:42:21 +00001486/* Returns the negative of NUM. */
1487static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001488num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001489{
1490 cpp_num copy;
1491
1492 copy = num;
1493 num.high = ~num.high;
1494 num.low = ~num.low;
1495 if (++num.low == 0)
1496 num.high++;
1497 num = num_trim (num, precision);
1498 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1499
1500 return num;
1501}
1502
1503/* Returns true if A >= B. */
1504static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001505num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001506{
1507 bool unsignedp;
1508
1509 unsignedp = pa.unsignedp || pb.unsignedp;
1510
1511 if (!unsignedp)
1512 {
1513 /* Both numbers have signed type. If they are of different
1514 sign, the answer is the sign of A. */
1515 unsignedp = num_positive (pa, precision);
1516
1517 if (unsignedp != num_positive (pb, precision))
1518 return unsignedp;
1519
1520 /* Otherwise we can do an unsigned comparison. */
1521 }
1522
1523 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1524}
1525
1526/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1527static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001528num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1529 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001530{
1531 lhs.overflow = false;
1532 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1533
1534 /* As excess precision is zeroed, there is no need to num_trim () as
1535 these operations cannot introduce a set bit there. */
1536 if (op == CPP_AND)
1537 {
1538 lhs.low &= rhs.low;
1539 lhs.high &= rhs.high;
1540 }
1541 else if (op == CPP_OR)
1542 {
1543 lhs.low |= rhs.low;
1544 lhs.high |= rhs.high;
1545 }
1546 else
1547 {
1548 lhs.low ^= rhs.low;
1549 lhs.high ^= rhs.high;
1550 }
1551
1552 return lhs;
1553}
1554
1555/* Returns LHS OP RHS, where OP is an inequality. */
1556static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001557num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1558 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001559{
1560 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1561
1562 if (op == CPP_GREATER_EQ)
1563 lhs.low = gte;
1564 else if (op == CPP_LESS)
1565 lhs.low = !gte;
1566 else if (op == CPP_GREATER)
1567 lhs.low = gte && !num_eq (lhs, rhs);
1568 else /* CPP_LESS_EQ. */
1569 lhs.low = !gte || num_eq (lhs, rhs);
1570
1571 lhs.high = 0;
1572 lhs.overflow = false;
1573 lhs.unsignedp = false;
1574 return lhs;
1575}
1576
1577/* Returns LHS OP RHS, where OP is == or !=. */
1578static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001579num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1580 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001581{
Jason Merrill97459792002-06-07 09:29:17 -04001582 /* Work around a 3.0.4 bug; see PR 6950. */
1583 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001584 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001585 eq = !eq;
1586 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001587 lhs.high = 0;
1588 lhs.overflow = false;
1589 lhs.unsignedp = false;
1590 return lhs;
1591}
1592
1593/* Shift NUM, of width PRECISION, right by N bits. */
1594static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001595num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001596{
1597 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001598 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001599
Diego Novillo6de9cd92004-05-13 02:41:07 -04001600 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001601 sign_mask = 0;
1602 else
1603 sign_mask = ~(cpp_num_part) 0;
1604
1605 if (n >= precision)
1606 num.high = num.low = sign_mask;
1607 else
1608 {
1609 /* Sign-extend. */
1610 if (precision < PART_PRECISION)
1611 num.high = sign_mask, num.low |= sign_mask << precision;
1612 else if (precision < 2 * PART_PRECISION)
1613 num.high |= sign_mask << (precision - PART_PRECISION);
1614
1615 if (n >= PART_PRECISION)
1616 {
1617 n -= PART_PRECISION;
1618 num.low = num.high;
1619 num.high = sign_mask;
1620 }
1621
1622 if (n)
1623 {
1624 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1625 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1626 }
1627 }
1628
1629 num = num_trim (num, precision);
1630 num.overflow = false;
1631 return num;
1632}
1633
1634/* Shift NUM, of width PRECISION, left by N bits. */
1635static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001636num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001637{
1638 if (n >= precision)
1639 {
1640 num.overflow = !num.unsignedp && !num_zerop (num);
1641 num.high = num.low = 0;
1642 }
1643 else
1644 {
1645 cpp_num orig, maybe_orig;
1646 size_t m = n;
1647
1648 orig = num;
1649 if (m >= PART_PRECISION)
1650 {
1651 m -= PART_PRECISION;
1652 num.high = num.low;
1653 num.low = 0;
1654 }
1655 if (m)
1656 {
1657 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1658 num.low <<= m;
1659 }
1660 num = num_trim (num, precision);
1661
1662 if (num.unsignedp)
1663 num.overflow = false;
1664 else
1665 {
1666 maybe_orig = num_rshift (num, precision, n);
1667 num.overflow = !num_eq (orig, maybe_orig);
1668 }
1669 }
1670
1671 return num;
1672}
1673
1674/* The four unary operators: +, -, ! and ~. */
1675static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001676num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001677{
1678 switch (op)
1679 {
1680 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001681 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001682 cpp_warning (pfile, CPP_W_TRADITIONAL,
1683 "traditional C rejects the unary plus operator");
Neil Booth91318902002-05-26 18:42:21 +00001684 num.overflow = false;
1685 break;
1686
1687 case CPP_UMINUS:
1688 num = num_negate (num, CPP_OPTION (pfile, precision));
1689 break;
1690
1691 case CPP_COMPL:
1692 num.high = ~num.high;
1693 num.low = ~num.low;
1694 num = num_trim (num, CPP_OPTION (pfile, precision));
1695 num.overflow = false;
1696 break;
1697
1698 default: /* case CPP_NOT: */
1699 num.low = num_zerop (num);
1700 num.high = 0;
1701 num.overflow = false;
1702 num.unsignedp = false;
1703 break;
1704 }
1705
1706 return num;
1707}
1708
1709/* The various binary operators. */
1710static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001711num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001712{
1713 cpp_num result;
1714 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001715 size_t n;
1716
1717 switch (op)
1718 {
1719 /* Shifts. */
1720 case CPP_LSHIFT:
1721 case CPP_RSHIFT:
1722 if (!rhs.unsignedp && !num_positive (rhs, precision))
1723 {
1724 /* A negative shift is a positive shift the other way. */
1725 if (op == CPP_LSHIFT)
1726 op = CPP_RSHIFT;
1727 else
1728 op = CPP_LSHIFT;
1729 rhs = num_negate (rhs, precision);
1730 }
1731 if (rhs.high)
1732 n = ~0; /* Maximal. */
1733 else
1734 n = rhs.low;
1735 if (op == CPP_LSHIFT)
1736 lhs = num_lshift (lhs, precision, n);
1737 else
1738 lhs = num_rshift (lhs, precision, n);
1739 break;
1740
Neil Booth91318902002-05-26 18:42:21 +00001741 /* Arithmetic. */
1742 case CPP_MINUS:
1743 rhs = num_negate (rhs, precision);
1744 case CPP_PLUS:
1745 result.low = lhs.low + rhs.low;
1746 result.high = lhs.high + rhs.high;
1747 if (result.low < lhs.low)
1748 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001749 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1750 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001751
1752 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001753 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001754 {
1755 bool lhsp = num_positive (lhs, precision);
1756 result.overflow = (lhsp == num_positive (rhs, precision)
1757 && lhsp != num_positive (result, precision));
1758 }
1759 return result;
1760
1761 /* Comma. */
1762 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001763 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1764 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001765 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001766 "comma operator in operand of #if");
1767 lhs = rhs;
1768 break;
1769 }
1770
1771 return lhs;
1772}
1773
1774/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1775 cannot overflow. */
1776static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001777num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001778{
1779 cpp_num result;
1780 cpp_num_part middle[2], temp;
1781
1782 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1783 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1784
1785 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1786 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1787
1788 temp = result.low;
1789 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1790 if (result.low < temp)
1791 result.high++;
1792
1793 temp = result.low;
1794 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1795 if (result.low < temp)
1796 result.high++;
1797
1798 result.high += HIGH_PART (middle[0]);
1799 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001800 result.unsignedp = true;
1801 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001802
1803 return result;
1804}
1805
1806/* Multiply two preprocessing numbers. */
1807static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001808num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001809{
1810 cpp_num result, temp;
1811 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1812 bool overflow, negate = false;
1813 size_t precision = CPP_OPTION (pfile, precision);
1814
1815 /* Prepare for unsigned multiplication. */
1816 if (!unsignedp)
1817 {
1818 if (!num_positive (lhs, precision))
1819 negate = !negate, lhs = num_negate (lhs, precision);
1820 if (!num_positive (rhs, precision))
1821 negate = !negate, rhs = num_negate (rhs, precision);
1822 }
1823
1824 overflow = lhs.high && rhs.high;
1825 result = num_part_mul (lhs.low, rhs.low);
1826
1827 temp = num_part_mul (lhs.high, rhs.low);
1828 result.high += temp.low;
1829 if (temp.high)
1830 overflow = true;
1831
1832 temp = num_part_mul (lhs.low, rhs.high);
1833 result.high += temp.low;
1834 if (temp.high)
1835 overflow = true;
1836
1837 temp.low = result.low, temp.high = result.high;
1838 result = num_trim (result, precision);
1839 if (!num_eq (result, temp))
1840 overflow = true;
1841
1842 if (negate)
1843 result = num_negate (result, precision);
1844
1845 if (unsignedp)
1846 result.overflow = false;
1847 else
1848 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1849 && !num_zerop (result));
1850 result.unsignedp = unsignedp;
1851
1852 return result;
1853}
1854
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001855/* Divide two preprocessing numbers, LHS and RHS, returning the answer
1856 or the remainder depending upon OP. LOCATION is the source location
1857 of this operator (for diagnostics). */
1858
Neil Booth91318902002-05-26 18:42:21 +00001859static cpp_num
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001860num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1861 source_location location)
Neil Booth91318902002-05-26 18:42:21 +00001862{
1863 cpp_num result, sub;
1864 cpp_num_part mask;
1865 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1866 bool negate = false, lhs_neg = false;
1867 size_t i, precision = CPP_OPTION (pfile, precision);
1868
1869 /* Prepare for unsigned division. */
1870 if (!unsignedp)
1871 {
1872 if (!num_positive (lhs, precision))
1873 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1874 if (!num_positive (rhs, precision))
1875 negate = !negate, rhs = num_negate (rhs, precision);
1876 }
1877
1878 /* Find the high bit. */
1879 if (rhs.high)
1880 {
1881 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001882 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001883 for (; ; i--, mask >>= 1)
1884 if (rhs.high & mask)
1885 break;
1886 }
1887 else if (rhs.low)
1888 {
1889 if (precision > PART_PRECISION)
1890 i = precision - PART_PRECISION - 1;
1891 else
1892 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001893 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001894 for (; ; i--, mask >>= 1)
1895 if (rhs.low & mask)
1896 break;
1897 }
1898 else
1899 {
Neil Booth75aef482002-07-19 19:24:43 +00001900 if (!pfile->state.skip_eval)
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001901 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1902 "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001903 return lhs;
1904 }
1905
Kazu Hiratada7d8302002-09-22 02:03:17 +00001906 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001907 shifting the RHS fully left, and subtracting from LHS if LHS is
1908 at least as big, and then repeating but with one less shift.
1909 This is not very efficient, but is easy to understand. */
1910
1911 rhs.unsignedp = true;
1912 lhs.unsignedp = true;
1913 i = precision - i - 1;
1914 sub = num_lshift (rhs, precision, i);
1915
1916 result.high = result.low = 0;
1917 for (;;)
1918 {
1919 if (num_greater_eq (lhs, sub, precision))
1920 {
1921 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1922 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001923 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001924 else
Neil Booth359b0be2002-05-28 05:44:06 +00001925 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001926 }
1927 if (i-- == 0)
1928 break;
1929 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1930 sub.high >>= 1;
1931 }
1932
1933 /* We divide so that the remainder has the sign of the LHS. */
1934 if (op == CPP_DIV)
1935 {
1936 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001937 result.overflow = false;
1938 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001939 {
1940 if (negate)
1941 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001942 result.overflow = (num_positive (result, precision) ^ !negate
1943 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001944 }
1945
1946 return result;
1947 }
1948
1949 /* CPP_MOD. */
1950 lhs.unsignedp = unsignedp;
1951 lhs.overflow = false;
1952 if (lhs_neg)
1953 lhs = num_negate (lhs, precision);
1954
1955 return lhs;
1956}