blob: 688439799f926d66d7b3aa8e377499dde463bcd4 [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,
Tom Tromey71c10032008-05-06 17:15:07 +00003 2002, 2004, 2008 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
8Free Software Foundation; either version 2, or (at your option) any
9later 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
17along with this program; if not, write to the Free Software
Kelley Cook200031d2005-06-29 02:34:39 +000018Foundation, 51 Franklin Street, Fifth Floor,
19Boston, MA 02110-1301, USA. */
Per Bothner7f2935c1995-03-16 13:59:07 -080020
Per Bothner7f2935c1995-03-16 13:59:07 -080021#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000022#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000023#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000024#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080025
Neil Booth91318902002-05-26 18:42:21 +000026#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28#define LOW_PART(num_part) (num_part & HALF_MASK)
29#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
30
Zack Weinbergcf00a882000-07-08 02:33:00 +000031struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000032{
Neil Booth68e652752002-07-20 13:31:56 +000033 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000034 cpp_num value; /* The value logically "right" of op. */
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +000035 source_location loc; /* The location of this value. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000036 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080037};
Per Bothner7f2935c1995-03-16 13:59:07 -080038
Neil Booth91318902002-05-26 18:42:21 +000039/* Some simple utility routines on double integers. */
40#define num_zerop(num) ((num.low | num.high) == 0)
41#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000042static bool num_positive (cpp_num, size_t);
43static bool num_greater_eq (cpp_num, cpp_num, size_t);
44static cpp_num num_trim (cpp_num, size_t);
45static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000046
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000047static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
48static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49static cpp_num num_negate (cpp_num, size_t);
50static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
51static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
54 enum cpp_ttype);
55static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
56static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
57static 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
188/* Subroutine of cpp_classify_number. S points to an integer suffix
189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
190 flag vector describing the suffix. */
191static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000192interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000193{
194 size_t u, l, i;
195
196 u = l = i = 0;
197
198 while (len--)
199 switch (s[len])
200 {
201 case 'u': case 'U': u++; break;
202 case 'i': case 'I':
203 case 'j': case 'J': i++; break;
204 case 'l': case 'L': l++;
205 /* If there are two Ls, they must be adjacent and the same case. */
206 if (l == 2 && s[len] != s[len + 1])
207 return 0;
208 break;
209 default:
210 return 0;
211 }
212
213 if (l > 2 || u > 1 || i > 1)
214 return 0;
215
216 return ((i ? CPP_N_IMAGINARY : 0)
217 | (u ? CPP_N_UNSIGNED : 0)
218 | ((l == 0) ? CPP_N_SMALL
219 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
220}
221
222/* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */
225unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000226cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
Neil Boothcd7ab832002-05-29 17:15:42 +0000227{
228 const uchar *str = token->val.str.text;
229 const uchar *limit;
230 unsigned int max_digit, result, radix;
231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
232
233 /* If the lexer has done its job, length one can only be a single
234 digit. Fast-path this very common case. */
235 if (token->val.str.len == 1)
236 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
237
238 limit = str + token->val.str.len;
239 float_flag = NOT_FLOAT;
240 max_digit = 0;
241 radix = 10;
242
243 /* First, interpret the radix. */
244 if (*str == '0')
245 {
246 radix = 8;
247 str++;
248
249 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000250 if ((*str == 'x' || *str == 'X')
251 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000252 {
253 radix = 16;
254 str++;
255 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000256 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
257 {
258 radix = 2;
259 str++;
260 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000261 }
262
263 /* Now scan for a well-formed integer or float. */
264 for (;;)
265 {
266 unsigned int c = *str++;
267
268 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
269 {
270 c = hex_value (c);
271 if (c > max_digit)
272 max_digit = c;
273 }
274 else if (c == '.')
275 {
276 if (float_flag == NOT_FLOAT)
277 float_flag = AFTER_POINT;
278 else
279 SYNTAX_ERROR ("too many decimal points in number");
280 }
281 else if ((radix <= 10 && (c == 'e' || c == 'E'))
282 || (radix == 16 && (c == 'p' || c == 'P')))
283 {
284 float_flag = AFTER_EXPON;
285 break;
286 }
287 else
288 {
289 /* Start of suffix. */
290 str--;
291 break;
292 }
293 }
294
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000295 /* The suffix may be for decimal fixed-point constants without exponent. */
296 if (radix != 16 && float_flag == NOT_FLOAT)
297 {
298 result = interpret_float_suffix (str, limit - str);
299 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
300 {
301 result |= CPP_N_FLOATING;
302 /* We need to restore the radix to 10, if the radix is 8. */
303 if (radix == 8)
304 radix = 10;
305
306 if (CPP_PEDANTIC (pfile))
307 cpp_error (pfile, CPP_DL_PEDWARN,
308 "fixed-point constants are a GCC extension");
309 goto syntax_ok;
310 }
311 else
312 result = 0;
313 }
314
Neil Boothcd7ab832002-05-29 17:15:42 +0000315 if (float_flag != NOT_FLOAT && radix == 8)
316 radix = 10;
317
318 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000319 {
320 if (radix == 2)
321 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
322 else
323 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
324 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000325
326 if (float_flag != NOT_FLOAT)
327 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000328 if (radix == 2)
329 {
330 cpp_error (pfile, CPP_DL_ERROR,
331 "invalid prefix \"0b\" for floating constant");
332 return CPP_N_INVALID;
333 }
334
Neil Boothcd7ab832002-05-29 17:15:42 +0000335 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000336 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000337 "use of C99 hexadecimal floating constant");
338
339 if (float_flag == AFTER_EXPON)
340 {
341 if (*str == '+' || *str == '-')
342 str++;
343
344 /* Exponent is decimal, even if string is a hex float. */
345 if (!ISDIGIT (*str))
346 SYNTAX_ERROR ("exponent has no digits");
347
348 do
349 str++;
350 while (ISDIGIT (*str));
351 }
352 else if (radix == 16)
353 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
354
355 result = interpret_float_suffix (str, limit - str);
356 if (result == 0)
357 {
John David Anglin0527bc42003-11-01 22:56:54 +0000358 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000359 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200360 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000361 return CPP_N_INVALID;
362 }
363
364 /* Traditional C didn't accept any floating suffixes. */
365 if (limit != str
366 && CPP_WTRADITIONAL (pfile)
367 && ! cpp_sys_macro_p (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000368 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000369 "traditional C rejects the \"%.*s\" suffix",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200370 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000371
Janis Johnson839a3b82009-04-01 17:31:26 +0000372 /* A suffix for double is a GCC extension via decimal float support.
373 If the suffix also specifies an imaginary value we'll catch that
374 later. */
375 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
376 cpp_error (pfile, CPP_DL_PEDWARN,
377 "suffix for double constant is a GCC extension");
378
Jon Grimmad6ed772005-12-06 23:13:15 +0000379 /* Radix must be 10 for decimal floats. */
380 if ((result & CPP_N_DFLOAT) && radix != 10)
381 {
382 cpp_error (pfile, CPP_DL_ERROR,
383 "invalid suffix \"%.*s\" with hexadecimal floating constant",
384 (int) (limit - str), str);
385 return CPP_N_INVALID;
386 }
387
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000388 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
389 cpp_error (pfile, CPP_DL_PEDWARN,
390 "fixed-point constants are a GCC extension");
391
Janis Johnson5a6bb572007-05-14 23:45:40 +0000392 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
393 cpp_error (pfile, CPP_DL_PEDWARN,
394 "decimal float constants are a GCC extension");
395
Neil Boothcd7ab832002-05-29 17:15:42 +0000396 result |= CPP_N_FLOATING;
397 }
398 else
399 {
400 result = interpret_int_suffix (str, limit - str);
401 if (result == 0)
402 {
John David Anglin0527bc42003-11-01 22:56:54 +0000403 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000404 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200405 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000406 return CPP_N_INVALID;
407 }
408
Zack Weinberg56da7202002-08-02 04:18:16 +0000409 /* Traditional C only accepted the 'L' suffix.
410 Suppress warning about 'LL' with -Wno-long-long. */
411 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
412 {
413 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
414 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
415
416 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
John David Anglin0527bc42003-11-01 22:56:54 +0000417 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000418 "traditional C rejects the \"%.*s\" suffix",
419 (int) (limit - str), str);
420 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000421
422 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
423 && ! CPP_OPTION (pfile, c99)
424 && CPP_OPTION (pfile, warn_long_long))
John David Anglin0527bc42003-11-01 22:56:54 +0000425 cpp_error (pfile, CPP_DL_PEDWARN,
426 "use of C99 long long integer constant");
Neil Boothcd7ab832002-05-29 17:15:42 +0000427
428 result |= CPP_N_INTEGER;
429 }
430
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000431 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000432 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000433 cpp_error (pfile, CPP_DL_PEDWARN,
434 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000435 if (radix == 2 && CPP_PEDANTIC (pfile))
436 cpp_error (pfile, CPP_DL_PEDWARN,
437 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000438
439 if (radix == 10)
440 result |= CPP_N_DECIMAL;
441 else if (radix == 16)
442 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000443 else if (radix == 2)
444 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000445 else
446 result |= CPP_N_OCTAL;
447
448 return result;
449
450 syntax_error:
451 return CPP_N_INVALID;
452}
453
454/* cpp_interpret_integer converts an integer constant into a cpp_num,
455 of precision options->precision.
456
457 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000458 because the preprocessor doesn't need it and we don't want to
459 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000460cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000461cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
462 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000463{
464 const uchar *p, *end;
465 cpp_num result;
466
467 result.low = 0;
468 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000469 result.unsignedp = !!(type & CPP_N_UNSIGNED);
470 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000471
472 p = token->val.str.text;
473 end = p + token->val.str.len;
474
475 /* Common case of a single digit. */
476 if (token->val.str.len == 1)
477 result.low = p[0] - '0';
478 else
479 {
480 cpp_num_part max;
481 size_t precision = CPP_OPTION (pfile, precision);
482 unsigned int base = 10, c = 0;
483 bool overflow = false;
484
485 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
486 {
487 base = 8;
488 p++;
489 }
490 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
491 {
492 base = 16;
493 p += 2;
494 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000495 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
496 {
497 base = 2;
498 p += 2;
499 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000500
501 /* We can add a digit to numbers strictly less than this without
502 needing the precision and slowness of double integers. */
503 max = ~(cpp_num_part) 0;
504 if (precision < PART_PRECISION)
505 max >>= PART_PRECISION - precision;
506 max = (max - base + 1) / base + 1;
507
508 for (; p < end; p++)
509 {
510 c = *p;
511
512 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
513 c = hex_value (c);
514 else
515 break;
516
517 /* Strict inequality for when max is set to zero. */
518 if (result.low < max)
519 result.low = result.low * base + c;
520 else
521 {
522 result = append_digit (result, c, base, precision);
523 overflow |= result.overflow;
524 max = 0;
525 }
526 }
527
528 if (overflow)
John David Anglin0527bc42003-11-01 22:56:54 +0000529 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000530 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000531 /* If too big to be signed, consider it unsigned. Only warn for
532 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000533 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000534 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000535 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000536 && !(CPP_OPTION (pfile, traditional)
537 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000538 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000539 {
Neil Boothcd7ab832002-05-29 17:15:42 +0000540 if (base == 10)
John David Anglin0527bc42003-11-01 22:56:54 +0000541 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000542 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000543 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000544 }
545 }
546
547 return result;
548}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000549
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000550/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000551static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000552append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000553{
554 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000555 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000556 bool overflow;
557 cpp_num_part add_high, add_low;
558
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000559 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000560 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000561 switch (base)
562 {
563 case 2:
564 shift = 1;
565 break;
566
567 case 16:
568 shift = 4;
569 break;
570
571 default:
572 shift = 3;
573 }
Neil Booth23ff0222002-07-17 17:27:14 +0000574 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000575 result.high = num.high << shift;
576 result.low = num.low << shift;
577 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400578 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000579
580 if (base == 10)
581 {
582 add_low = num.low << 1;
583 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
584 }
585 else
586 add_high = add_low = 0;
587
588 if (add_low + digit < add_low)
589 add_high++;
590 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000591
Neil Booth91318902002-05-26 18:42:21 +0000592 if (result.low + add_low < result.low)
593 add_high++;
594 if (result.high + add_high < result.high)
595 overflow = true;
596
597 result.low += add_low;
598 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400599 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000600
601 /* The above code catches overflow of a cpp_num type. This catches
602 overflow of the (possibly shorter) target precision. */
603 num.low = result.low;
604 num.high = result.high;
605 result = num_trim (result, precision);
606 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400607 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000608
Neil Booth91318902002-05-26 18:42:21 +0000609 return result;
610}
611
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000612/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000613static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000614parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000615{
Neil Booth91318902002-05-26 18:42:21 +0000616 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000617 int paren = 0;
618 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000619 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000620 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000621
Neil Booth93c803682000-10-28 17:59:06 +0000622 /* Don't expand macros. */
623 pfile->state.prevent_expansion++;
624
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000625 token = cpp_get_token (pfile);
626 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000627 {
628 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000629 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000630 }
631
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000632 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000633 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000634 node = token->val.node;
635 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000636 {
John David Anglin0527bc42003-11-01 22:56:54 +0000637 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000638 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000639 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000640 }
Neil Booth93c803682000-10-28 17:59:06 +0000641 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000642 {
John David Anglin0527bc42003-11-01 22:56:54 +0000643 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000644 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000645 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000646 {
647 cpp_token op;
648
649 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000650 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000651 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000652 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000653 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000654 cpp_token_as_text (pfile, &op));
655 }
656 }
Neil Booth93c803682000-10-28 17:59:06 +0000657
Neil Booth91318902002-05-26 18:42:21 +0000658 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000659 {
Neil Booth335d03e2003-08-03 12:23:46 +0000660 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000661 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000662 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000663
Neil Bootha69cbaa2002-07-23 22:57:49 +0000664 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100665 if (!(node->flags & NODE_USED))
666 {
667 node->flags |= NODE_USED;
668 if (node->type == NT_MACRO)
669 {
670 if (pfile->cb.used_define)
671 pfile->cb.used_define (pfile, pfile->directive_line, node);
672 }
673 else
674 {
675 if (pfile->cb.used_undef)
676 pfile->cb.used_undef (pfile, pfile->directive_line, node);
677 }
678 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000679
Neil Booth6d18adb2001-07-29 17:27:57 +0000680 /* A possible controlling macro of the form #if !defined ().
681 _cpp_parse_expr checks there was no other junk on the line. */
682 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000683 }
684
685 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000686
Neil Booth23ff0222002-07-17 17:27:14 +0000687 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000688 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000689 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000690 result.low = node && node->type == NT_MACRO;
691 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000692}
693
Neil Booth60284a52002-04-28 23:14:56 +0000694/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
695 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000696 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000697static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000698eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800699{
Neil Booth91318902002-05-26 18:42:21 +0000700 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000701 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000702 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000703
Diego Novillo6de9cd92004-05-13 02:41:07 -0400704 result.unsignedp = false;
705 result.overflow = false;
706
Neil Booth93c803682000-10-28 17:59:06 +0000707 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000708 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800709 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000710 temp = cpp_classify_number (pfile, token);
711 switch (temp & CPP_N_CATEGORY)
712 {
713 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000714 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000715 "floating constant in preprocessor expression");
716 break;
717 case CPP_N_INTEGER:
718 if (!(temp & CPP_N_IMAGINARY))
719 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000720 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000721 "imaginary number in preprocessor expression");
722 break;
723
724 case CPP_N_INVALID:
725 /* Error already issued. */
726 break;
727 }
728 result.high = result.low = 0;
729 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000730
Alexandre Oliva525bc952000-02-23 19:21:07 +0000731 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000732 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000733 case CPP_CHAR16:
734 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +0000735 {
Neil Booth91318902002-05-26 18:42:21 +0000736 cppchar_t cc = cpp_interpret_charconst (pfile, token,
737 &temp, &unsignedp);
738
739 result.high = 0;
740 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000741 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000742 if (!unsignedp && (cppchar_signed_t) cc < 0)
743 {
744 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
745 result.low |= ~(~(cpp_num_part) 0
746 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
747 result.high = ~(cpp_num_part) 0;
748 result = num_trim (result, CPP_OPTION (pfile, precision));
749 }
Neil Bootha5a49442002-05-06 22:53:10 +0000750 }
Neil Booth60284a52002-04-28 23:14:56 +0000751 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000752
Zack Weinberg92936ec2000-07-19 20:18:08 +0000753 case CPP_NAME:
Neil Booth93c803682000-10-28 17:59:06 +0000754 if (token->val.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000755 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000756 else if (CPP_OPTION (pfile, cplusplus)
757 && (token->val.node == pfile->spec_nodes.n_true
758 || token->val.node == pfile->spec_nodes.n_false))
759 {
Neil Booth91318902002-05-26 18:42:21 +0000760 result.high = 0;
761 result.low = (token->val.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000762 }
763 else
764 {
Neil Booth91318902002-05-26 18:42:21 +0000765 result.high = 0;
766 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000767 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000768 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
Neil Boothebef4e82002-04-14 18:42:47 +0000769 NODE_NAME (token->val.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000770 }
Neil Booth60284a52002-04-28 23:14:56 +0000771 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000772
Tom Tromey899015a2008-05-13 14:50:27 +0000773 case CPP_HASH:
774 if (!pfile->state.skipping)
775 {
776 /* A pedantic warning takes precedence over a deprecated
777 warning here. */
778 if (CPP_PEDANTIC (pfile))
779 cpp_error (pfile, CPP_DL_PEDWARN,
780 "assertions are a GCC extension");
781 else if (CPP_OPTION (pfile, warn_deprecated))
782 cpp_error (pfile, CPP_DL_WARNING,
783 "assertions are a deprecated extension");
784 }
Neil Booth91318902002-05-26 18:42:21 +0000785 _cpp_test_assertion (pfile, &temp);
786 result.high = 0;
787 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +0000788 break;
789
790 default:
791 abort ();
Neil Booth93c803682000-10-28 17:59:06 +0000792 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800793
Neil Booth23ff0222002-07-17 17:27:14 +0000794 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000795 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800796}
797
Neil Booth4063b942000-04-02 08:27:23 +0000798/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000799
800After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000801than the operator on the top of the stack, we reduce the stack by one
802operator and repeat the test. Since equal priorities do not reduce,
803this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000804
Neil Booth87ed1092002-04-28 19:42:54 +0000805We handle left-associative operators by decrementing the priority of
806just-lexed operators by one, but retaining the priority of operators
807already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000808
809The remaining cases are '(' and ')'. We handle '(' by skipping the
810reduction phase completely. ')' is given lower priority than
811everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000812parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000813reduce() exits immediately. If the normal exit route sees a ')', then
814there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000815
Neil Boothf8b954f2002-04-26 06:32:50 +0000816The parser assumes all shifted operators require a left operand unless
817the flag NO_L_OPERAND is set. These semantics are automatic; any
818extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800819
Neil Booth68e652752002-07-20 13:31:56 +0000820/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
821 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000822#define NO_L_OPERAND (1 << 0)
823#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000824#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000825
Zack Weinbergcf00a882000-07-08 02:33:00 +0000826/* Operator to priority map. Must be in the same order as the first
827 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000828static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000829{
Neil Booth60284a52002-04-28 23:14:56 +0000830 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000831 uchar flags;
832} optab[] =
833{
Neil Boothad28cff2002-07-18 22:08:35 +0000834 /* EQ */ {0, 0}, /* Shouldn't happen. */
835 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000836 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
837 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
838 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
839 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
840 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
841 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
842 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
843 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
844 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
845 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000846 /* RSHIFT */ {13, LEFT_ASSOC},
847 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000848
Neil Boothad28cff2002-07-18 22:08:35 +0000849 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000850 /* AND_AND */ {6, LEFT_ASSOC},
851 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +0000852 /* Note that QUERY, COLON, and COMMA must have the same precedence.
853 However, there are some special cases for these in reduce(). */
854 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000855 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +0000856 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000857 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000858 /* CLOSE_PAREN */ {0, 0},
859 /* EOF */ {0, 0},
860 /* EQ_EQ */ {11, LEFT_ASSOC},
861 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000862 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
863 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000864 /* UPLUS */ {16, NO_L_OPERAND},
865 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000866};
867
Per Bothner7f2935c1995-03-16 13:59:07 -0800868/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000869 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000870
871 The implementation is an operator precedence parser, i.e. a
872 bottom-up parser, using a stack for not-yet-reduced tokens.
873
874 The stack base is op_stack, and the current stack pointer is 'top'.
875 There is a stack element for each operator (only), and the most
876 recently pushed operator is 'top->op'. An operand (value) is
877 stored in the 'value' field of the stack element of the operator
878 that precedes it. */
879bool
Tom Tromeyd7508872008-05-30 14:25:09 +0000880_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -0800881{
Neil Booth87ed1092002-04-28 19:42:54 +0000882 struct op *top = pfile->op_stack;
883 unsigned int lex_count;
884 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800885
Neil Booth87ed1092002-04-28 19:42:54 +0000886 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800887
Neil Booth93c803682000-10-28 17:59:06 +0000888 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000889 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000890 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000891 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000892
Neil Booth87ed1092002-04-28 19:42:54 +0000893 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000894 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000895
Per Bothner7f2935c1995-03-16 13:59:07 -0800896 for (;;)
897 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000898 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800899
Neil Booth6d18adb2001-07-29 17:27:57 +0000900 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000901 op.token = cpp_get_token (pfile);
902 op.op = op.token->type;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +0000903 op.loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -0800904
Per Bothner7f2935c1995-03-16 13:59:07 -0800905 switch (op.op)
906 {
Neil Booth60284a52002-04-28 23:14:56 +0000907 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000908 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000909 case CPP_CHAR:
910 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000911 case CPP_CHAR16:
912 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +0000913 case CPP_NAME:
914 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000915 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000916 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000917 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000918 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000919 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000920 continue;
921
Neil Booth6d18adb2001-07-29 17:27:57 +0000922 case CPP_NOT:
923 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000924 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000925 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000926 if (want_value)
927 op.op = CPP_UPLUS;
928 break;
929 case CPP_MINUS:
930 if (want_value)
931 op.op = CPP_UMINUS;
932 break;
Neil Booth60284a52002-04-28 23:14:56 +0000933
Neil Boothf8b954f2002-04-26 06:32:50 +0000934 default:
Neil Booth60284a52002-04-28 23:14:56 +0000935 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000936 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000937 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000938 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800939 }
940
Neil Booth87ed1092002-04-28 19:42:54 +0000941 /* Check we have a value or operator as appropriate. */
942 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000943 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000944 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000945 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000946 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000947 }
Neil Booth87ed1092002-04-28 19:42:54 +0000948 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000949 {
Neil Bootha09d4742004-07-04 12:57:50 +0000950 /* We want a number (or expression) and haven't got one.
951 Try to emit a specific diagnostic. */
952 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
953 SYNTAX_ERROR ("missing expression between '(' and ')'");
954
955 if (op.op == CPP_EOF && top->op == CPP_EOF)
Tom Tromeyd7508872008-05-30 14:25:09 +0000956 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +0000957
958 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
959 SYNTAX_ERROR2 ("operator '%s' has no right operand",
960 cpp_token_as_text (pfile, top->token));
961 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
962 /* Complain about missing paren during reduction. */;
963 else
964 SYNTAX_ERROR2 ("operator '%s' has no left operand",
965 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000966 }
Neil Booth87ed1092002-04-28 19:42:54 +0000967
968 top = reduce (pfile, top, op.op);
969 if (!top)
970 goto syntax_error;
971
Neil Booth60284a52002-04-28 23:14:56 +0000972 if (op.op == CPP_EOF)
973 break;
974
Neil Booth87ed1092002-04-28 19:42:54 +0000975 switch (op.op)
976 {
977 case CPP_CLOSE_PAREN:
978 continue;
Neil Booth87ed1092002-04-28 19:42:54 +0000979 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +0000980 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000981 pfile->state.skip_eval++;
982 break;
983 case CPP_AND_AND:
984 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +0000985 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000986 pfile->state.skip_eval++;
987 break;
988 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +0000989 if (top->op != CPP_QUERY)
990 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +0000991 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +0000992 pfile->state.skip_eval++;
993 else
994 pfile->state.skip_eval--;
995 default:
996 break;
997 }
998
Neil Boothf8b954f2002-04-26 06:32:50 +0000999 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +00001000
Mike Stump0f413021996-07-03 22:07:53 +00001001 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +00001002 if (++top == pfile->op_limit)
1003 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +00001004
Per Bothner7f2935c1995-03-16 13:59:07 -08001005 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +00001006 top->token = op.token;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001007 top->loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001008 }
Neil Booth9ee703132000-04-01 07:42:37 +00001009
Neil Booth6d18adb2001-07-29 17:27:57 +00001010 /* The controlling macro expression is only valid if we called lex 3
1011 times: <!> <defined expression> and <EOF>. push_conditional ()
1012 checks that we are at top-of-file. */
1013 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1014 pfile->mi_ind_cmacro = 0;
1015
Neil Booth87ed1092002-04-28 19:42:54 +00001016 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +00001017 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001018 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1019 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +00001020 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +00001021 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +00001022 }
Neil Booth9ee703132000-04-01 07:42:37 +00001023
Neil Booth91318902002-05-26 18:42:21 +00001024 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001025}
1026
1027/* Reduce the operator / value stack if possible, in preparation for
1028 pushing operator OP. Returns NULL on error, otherwise the top of
1029 the stack. */
1030static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001031reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001032{
1033 unsigned int prio;
1034
Neil Booth91318902002-05-26 18:42:21 +00001035 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1036 {
1037 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001038 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001039 return 0;
1040 }
1041
Neil Booth87ed1092002-04-28 19:42:54 +00001042 if (op == CPP_OPEN_PAREN)
1043 return top;
1044
1045 /* Decrement the priority of left-associative operators to force a
1046 reduction with operators of otherwise equal priority. */
1047 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1048 while (prio < optab[top->op].prio)
1049 {
Neil Booth68e652752002-07-20 13:31:56 +00001050 if (CPP_OPTION (pfile, warn_num_sign_change)
1051 && optab[top->op].flags & CHECK_PROMOTION)
1052 check_promotion (pfile, top);
1053
Neil Booth75aef482002-07-19 19:24:43 +00001054 switch (top->op)
1055 {
1056 case CPP_UPLUS:
1057 case CPP_UMINUS:
1058 case CPP_NOT:
1059 case CPP_COMPL:
1060 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001061 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001062 break;
Neil Booth91318902002-05-26 18:42:21 +00001063
Neil Booth75aef482002-07-19 19:24:43 +00001064 case CPP_PLUS:
1065 case CPP_MINUS:
1066 case CPP_RSHIFT:
1067 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001068 case CPP_COMMA:
1069 top[-1].value = num_binary_op (pfile, top[-1].value,
1070 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001071 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001072 break;
Neil Booth91318902002-05-26 18:42:21 +00001073
Neil Booth75aef482002-07-19 19:24:43 +00001074 case CPP_GREATER:
1075 case CPP_LESS:
1076 case CPP_GREATER_EQ:
1077 case CPP_LESS_EQ:
1078 top[-1].value
1079 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001080 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001081 break;
Neil Booth91318902002-05-26 18:42:21 +00001082
Neil Booth75aef482002-07-19 19:24:43 +00001083 case CPP_EQ_EQ:
1084 case CPP_NOT_EQ:
1085 top[-1].value
1086 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001087 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001088 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001089
Neil Booth75aef482002-07-19 19:24:43 +00001090 case CPP_AND:
1091 case CPP_OR:
1092 case CPP_XOR:
1093 top[-1].value
1094 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001095 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001096 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001097
Neil Booth75aef482002-07-19 19:24:43 +00001098 case CPP_MULT:
1099 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001100 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001101 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001102
Neil Booth75aef482002-07-19 19:24:43 +00001103 case CPP_DIV:
1104 case CPP_MOD:
1105 top[-1].value = num_div_op (pfile, top[-1].value,
1106 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001107 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001108 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001109
Neil Booth75aef482002-07-19 19:24:43 +00001110 case CPP_OR_OR:
1111 top--;
1112 if (!num_zerop (top->value))
1113 pfile->state.skip_eval--;
1114 top->value.low = (!num_zerop (top->value)
1115 || !num_zerop (top[1].value));
1116 top->value.high = 0;
1117 top->value.unsignedp = false;
1118 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001119 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001120 continue;
1121
1122 case CPP_AND_AND:
1123 top--;
1124 if (num_zerop (top->value))
1125 pfile->state.skip_eval--;
1126 top->value.low = (!num_zerop (top->value)
1127 && !num_zerop (top[1].value));
1128 top->value.high = 0;
1129 top->value.unsignedp = false;
1130 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001131 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001132 continue;
1133
1134 case CPP_OPEN_PAREN:
1135 if (op != CPP_CLOSE_PAREN)
1136 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001137 cpp_error_with_line (pfile, CPP_DL_ERROR,
1138 top->token->src_loc,
1139 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001140 return 0;
1141 }
1142 top--;
1143 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001144 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001145 return top;
1146
1147 case CPP_COLON:
1148 top -= 2;
1149 if (!num_zerop (top->value))
1150 {
Neil Booth91318902002-05-26 18:42:21 +00001151 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001152 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001153 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001154 }
1155 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001156 {
1157 top->value = top[2].value;
1158 top->loc = top[2].loc;
1159 }
Neil Booth75aef482002-07-19 19:24:43 +00001160 top->value.unsignedp = (top[1].value.unsignedp
1161 || top[2].value.unsignedp);
1162 continue;
Neil Booth91318902002-05-26 18:42:21 +00001163
Neil Booth75aef482002-07-19 19:24:43 +00001164 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001165 /* COMMA and COLON should not reduce a QUERY operator. */
1166 if (op == CPP_COMMA || op == CPP_COLON)
1167 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001168 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001169 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001170
Neil Booth75aef482002-07-19 19:24:43 +00001171 default:
1172 goto bad_op;
1173 }
Neil Boothad28cff2002-07-18 22:08:35 +00001174
1175 top--;
Neil Booth91318902002-05-26 18:42:21 +00001176 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001177 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001178 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001179 }
1180
1181 if (op == CPP_CLOSE_PAREN)
1182 {
John David Anglin0527bc42003-11-01 22:56:54 +00001183 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001184 return 0;
1185 }
1186
1187 return top;
1188}
1189
1190/* Returns the position of the old top of stack after expansion. */
1191struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001192_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001193{
Neil Booth32fa4562002-05-09 22:27:31 +00001194 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1195 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001196
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001197 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001198 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001199
Neil Booth32fa4562002-05-09 22:27:31 +00001200 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001201}
Neil Booth91318902002-05-26 18:42:21 +00001202
Neil Booth68e652752002-07-20 13:31:56 +00001203/* Emits a warning if the effective sign of either operand of OP
1204 changes because of integer promotions. */
1205static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001206check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001207{
1208 if (op->value.unsignedp == op[-1].value.unsignedp)
1209 return;
1210
1211 if (op->value.unsignedp)
1212 {
1213 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001214 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1215 "the left operand of \"%s\" changes sign when promoted",
1216 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001217 }
1218 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001219 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001220 "the right operand of \"%s\" changes sign when promoted",
1221 cpp_token_as_text (pfile, op->token));
1222}
1223
Neil Booth91318902002-05-26 18:42:21 +00001224/* Clears the unused high order bits of the number pointed to by PNUM. */
1225static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001226num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001227{
1228 if (precision > PART_PRECISION)
1229 {
1230 precision -= PART_PRECISION;
1231 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001232 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001233 }
1234 else
1235 {
1236 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001237 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001238 num.high = 0;
1239 }
1240
1241 return num;
1242}
1243
1244/* True iff A (presumed signed) >= 0. */
1245static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001246num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001247{
1248 if (precision > PART_PRECISION)
1249 {
1250 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001251 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001252 }
1253
Neil Booth359b0be2002-05-28 05:44:06 +00001254 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001255}
1256
Neil Boothceeedfc2002-06-02 19:37:34 +00001257/* Sign extend a number, with PRECISION significant bits and all
1258 others assumed clear, to fill out a cpp_num structure. */
1259cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001260cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001261{
1262 if (!num.unsignedp)
1263 {
1264 if (precision > PART_PRECISION)
1265 {
1266 precision -= PART_PRECISION;
1267 if (precision < PART_PRECISION
1268 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1269 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1270 }
1271 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1272 {
1273 if (precision < PART_PRECISION)
1274 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1275 num.high = ~(cpp_num_part) 0;
1276 }
1277 }
1278
1279 return num;
1280}
1281
Neil Booth91318902002-05-26 18:42:21 +00001282/* Returns the negative of NUM. */
1283static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001284num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001285{
1286 cpp_num copy;
1287
1288 copy = num;
1289 num.high = ~num.high;
1290 num.low = ~num.low;
1291 if (++num.low == 0)
1292 num.high++;
1293 num = num_trim (num, precision);
1294 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1295
1296 return num;
1297}
1298
1299/* Returns true if A >= B. */
1300static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001301num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001302{
1303 bool unsignedp;
1304
1305 unsignedp = pa.unsignedp || pb.unsignedp;
1306
1307 if (!unsignedp)
1308 {
1309 /* Both numbers have signed type. If they are of different
1310 sign, the answer is the sign of A. */
1311 unsignedp = num_positive (pa, precision);
1312
1313 if (unsignedp != num_positive (pb, precision))
1314 return unsignedp;
1315
1316 /* Otherwise we can do an unsigned comparison. */
1317 }
1318
1319 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1320}
1321
1322/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1323static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001324num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1325 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001326{
1327 lhs.overflow = false;
1328 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1329
1330 /* As excess precision is zeroed, there is no need to num_trim () as
1331 these operations cannot introduce a set bit there. */
1332 if (op == CPP_AND)
1333 {
1334 lhs.low &= rhs.low;
1335 lhs.high &= rhs.high;
1336 }
1337 else if (op == CPP_OR)
1338 {
1339 lhs.low |= rhs.low;
1340 lhs.high |= rhs.high;
1341 }
1342 else
1343 {
1344 lhs.low ^= rhs.low;
1345 lhs.high ^= rhs.high;
1346 }
1347
1348 return lhs;
1349}
1350
1351/* Returns LHS OP RHS, where OP is an inequality. */
1352static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001353num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1354 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001355{
1356 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1357
1358 if (op == CPP_GREATER_EQ)
1359 lhs.low = gte;
1360 else if (op == CPP_LESS)
1361 lhs.low = !gte;
1362 else if (op == CPP_GREATER)
1363 lhs.low = gte && !num_eq (lhs, rhs);
1364 else /* CPP_LESS_EQ. */
1365 lhs.low = !gte || num_eq (lhs, rhs);
1366
1367 lhs.high = 0;
1368 lhs.overflow = false;
1369 lhs.unsignedp = false;
1370 return lhs;
1371}
1372
1373/* Returns LHS OP RHS, where OP is == or !=. */
1374static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001375num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1376 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001377{
Jason Merrill97459792002-06-07 09:29:17 -04001378 /* Work around a 3.0.4 bug; see PR 6950. */
1379 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001380 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001381 eq = !eq;
1382 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001383 lhs.high = 0;
1384 lhs.overflow = false;
1385 lhs.unsignedp = false;
1386 return lhs;
1387}
1388
1389/* Shift NUM, of width PRECISION, right by N bits. */
1390static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001391num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001392{
1393 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001394 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001395
Diego Novillo6de9cd92004-05-13 02:41:07 -04001396 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001397 sign_mask = 0;
1398 else
1399 sign_mask = ~(cpp_num_part) 0;
1400
1401 if (n >= precision)
1402 num.high = num.low = sign_mask;
1403 else
1404 {
1405 /* Sign-extend. */
1406 if (precision < PART_PRECISION)
1407 num.high = sign_mask, num.low |= sign_mask << precision;
1408 else if (precision < 2 * PART_PRECISION)
1409 num.high |= sign_mask << (precision - PART_PRECISION);
1410
1411 if (n >= PART_PRECISION)
1412 {
1413 n -= PART_PRECISION;
1414 num.low = num.high;
1415 num.high = sign_mask;
1416 }
1417
1418 if (n)
1419 {
1420 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1421 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1422 }
1423 }
1424
1425 num = num_trim (num, precision);
1426 num.overflow = false;
1427 return num;
1428}
1429
1430/* Shift NUM, of width PRECISION, left by N bits. */
1431static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001432num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001433{
1434 if (n >= precision)
1435 {
1436 num.overflow = !num.unsignedp && !num_zerop (num);
1437 num.high = num.low = 0;
1438 }
1439 else
1440 {
1441 cpp_num orig, maybe_orig;
1442 size_t m = n;
1443
1444 orig = num;
1445 if (m >= PART_PRECISION)
1446 {
1447 m -= PART_PRECISION;
1448 num.high = num.low;
1449 num.low = 0;
1450 }
1451 if (m)
1452 {
1453 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1454 num.low <<= m;
1455 }
1456 num = num_trim (num, precision);
1457
1458 if (num.unsignedp)
1459 num.overflow = false;
1460 else
1461 {
1462 maybe_orig = num_rshift (num, precision, n);
1463 num.overflow = !num_eq (orig, maybe_orig);
1464 }
1465 }
1466
1467 return num;
1468}
1469
1470/* The four unary operators: +, -, ! and ~. */
1471static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001472num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001473{
1474 switch (op)
1475 {
1476 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001477 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001478 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth91318902002-05-26 18:42:21 +00001479 "traditional C rejects the unary plus operator");
1480 num.overflow = false;
1481 break;
1482
1483 case CPP_UMINUS:
1484 num = num_negate (num, CPP_OPTION (pfile, precision));
1485 break;
1486
1487 case CPP_COMPL:
1488 num.high = ~num.high;
1489 num.low = ~num.low;
1490 num = num_trim (num, CPP_OPTION (pfile, precision));
1491 num.overflow = false;
1492 break;
1493
1494 default: /* case CPP_NOT: */
1495 num.low = num_zerop (num);
1496 num.high = 0;
1497 num.overflow = false;
1498 num.unsignedp = false;
1499 break;
1500 }
1501
1502 return num;
1503}
1504
1505/* The various binary operators. */
1506static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001507num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001508{
1509 cpp_num result;
1510 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001511 size_t n;
1512
1513 switch (op)
1514 {
1515 /* Shifts. */
1516 case CPP_LSHIFT:
1517 case CPP_RSHIFT:
1518 if (!rhs.unsignedp && !num_positive (rhs, precision))
1519 {
1520 /* A negative shift is a positive shift the other way. */
1521 if (op == CPP_LSHIFT)
1522 op = CPP_RSHIFT;
1523 else
1524 op = CPP_LSHIFT;
1525 rhs = num_negate (rhs, precision);
1526 }
1527 if (rhs.high)
1528 n = ~0; /* Maximal. */
1529 else
1530 n = rhs.low;
1531 if (op == CPP_LSHIFT)
1532 lhs = num_lshift (lhs, precision, n);
1533 else
1534 lhs = num_rshift (lhs, precision, n);
1535 break;
1536
Neil Booth91318902002-05-26 18:42:21 +00001537 /* Arithmetic. */
1538 case CPP_MINUS:
1539 rhs = num_negate (rhs, precision);
1540 case CPP_PLUS:
1541 result.low = lhs.low + rhs.low;
1542 result.high = lhs.high + rhs.high;
1543 if (result.low < lhs.low)
1544 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001545 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1546 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001547
1548 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001549 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001550 {
1551 bool lhsp = num_positive (lhs, precision);
1552 result.overflow = (lhsp == num_positive (rhs, precision)
1553 && lhsp != num_positive (result, precision));
1554 }
1555 return result;
1556
1557 /* Comma. */
1558 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001559 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1560 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001561 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001562 "comma operator in operand of #if");
1563 lhs = rhs;
1564 break;
1565 }
1566
1567 return lhs;
1568}
1569
1570/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1571 cannot overflow. */
1572static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001573num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001574{
1575 cpp_num result;
1576 cpp_num_part middle[2], temp;
1577
1578 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1579 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1580
1581 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1582 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1583
1584 temp = result.low;
1585 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1586 if (result.low < temp)
1587 result.high++;
1588
1589 temp = result.low;
1590 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1591 if (result.low < temp)
1592 result.high++;
1593
1594 result.high += HIGH_PART (middle[0]);
1595 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001596 result.unsignedp = true;
1597 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001598
1599 return result;
1600}
1601
1602/* Multiply two preprocessing numbers. */
1603static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001604num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001605{
1606 cpp_num result, temp;
1607 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1608 bool overflow, negate = false;
1609 size_t precision = CPP_OPTION (pfile, precision);
1610
1611 /* Prepare for unsigned multiplication. */
1612 if (!unsignedp)
1613 {
1614 if (!num_positive (lhs, precision))
1615 negate = !negate, lhs = num_negate (lhs, precision);
1616 if (!num_positive (rhs, precision))
1617 negate = !negate, rhs = num_negate (rhs, precision);
1618 }
1619
1620 overflow = lhs.high && rhs.high;
1621 result = num_part_mul (lhs.low, rhs.low);
1622
1623 temp = num_part_mul (lhs.high, rhs.low);
1624 result.high += temp.low;
1625 if (temp.high)
1626 overflow = true;
1627
1628 temp = num_part_mul (lhs.low, rhs.high);
1629 result.high += temp.low;
1630 if (temp.high)
1631 overflow = true;
1632
1633 temp.low = result.low, temp.high = result.high;
1634 result = num_trim (result, precision);
1635 if (!num_eq (result, temp))
1636 overflow = true;
1637
1638 if (negate)
1639 result = num_negate (result, precision);
1640
1641 if (unsignedp)
1642 result.overflow = false;
1643 else
1644 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1645 && !num_zerop (result));
1646 result.unsignedp = unsignedp;
1647
1648 return result;
1649}
1650
1651/* Divide two preprocessing numbers, returning the answer or the
1652 remainder depending upon OP. */
1653static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001654num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001655{
1656 cpp_num result, sub;
1657 cpp_num_part mask;
1658 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1659 bool negate = false, lhs_neg = false;
1660 size_t i, precision = CPP_OPTION (pfile, precision);
1661
1662 /* Prepare for unsigned division. */
1663 if (!unsignedp)
1664 {
1665 if (!num_positive (lhs, precision))
1666 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1667 if (!num_positive (rhs, precision))
1668 negate = !negate, rhs = num_negate (rhs, precision);
1669 }
1670
1671 /* Find the high bit. */
1672 if (rhs.high)
1673 {
1674 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001675 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001676 for (; ; i--, mask >>= 1)
1677 if (rhs.high & mask)
1678 break;
1679 }
1680 else if (rhs.low)
1681 {
1682 if (precision > PART_PRECISION)
1683 i = precision - PART_PRECISION - 1;
1684 else
1685 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001686 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001687 for (; ; i--, mask >>= 1)
1688 if (rhs.low & mask)
1689 break;
1690 }
1691 else
1692 {
Neil Booth75aef482002-07-19 19:24:43 +00001693 if (!pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001694 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001695 return lhs;
1696 }
1697
Kazu Hiratada7d8302002-09-22 02:03:17 +00001698 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001699 shifting the RHS fully left, and subtracting from LHS if LHS is
1700 at least as big, and then repeating but with one less shift.
1701 This is not very efficient, but is easy to understand. */
1702
1703 rhs.unsignedp = true;
1704 lhs.unsignedp = true;
1705 i = precision - i - 1;
1706 sub = num_lshift (rhs, precision, i);
1707
1708 result.high = result.low = 0;
1709 for (;;)
1710 {
1711 if (num_greater_eq (lhs, sub, precision))
1712 {
1713 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1714 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001715 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001716 else
Neil Booth359b0be2002-05-28 05:44:06 +00001717 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001718 }
1719 if (i-- == 0)
1720 break;
1721 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1722 sub.high >>= 1;
1723 }
1724
1725 /* We divide so that the remainder has the sign of the LHS. */
1726 if (op == CPP_DIV)
1727 {
1728 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001729 result.overflow = false;
1730 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001731 {
1732 if (negate)
1733 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001734 result.overflow = (num_positive (result, precision) ^ !negate
1735 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001736 }
1737
1738 return result;
1739 }
1740
1741 /* CPP_MOD. */
1742 lhs.unsignedp = unsignedp;
1743 lhs.overflow = false;
1744 if (lhs_neg)
1745 lhs = num_negate (lhs, precision);
1746
1747 return lhs;
1748}