blob: b9a17db9361a10a225f5be0db2acc82a87da0292 [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,
Jakub Jelinek748086b2009-04-09 17:00:19 +02003 2002, 2004, 2008, 2009 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);
55static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
56static cpp_num num_lshift (cpp_num, size_t, size_t);
57static cpp_num num_rshift (cpp_num, size_t, size_t);
Neil Booth91318902002-05-26 18:42:21 +000058
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000059static cpp_num append_digit (cpp_num, int, int, size_t);
60static cpp_num parse_defined (cpp_reader *);
61static cpp_num eval_token (cpp_reader *, const cpp_token *);
62static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
63static unsigned int interpret_float_suffix (const uchar *, size_t);
64static unsigned int interpret_int_suffix (const uchar *, size_t);
65static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000066
Neil Boothcd7ab832002-05-29 17:15:42 +000067/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000068#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000070
Zack Weinberg15dad1d2000-05-18 15:55:46 +000071/* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000073#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000074 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000075#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000076 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
77 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000078
Neil Boothcd7ab832002-05-29 17:15:42 +000079/* Subroutine of cpp_classify_number. S points to a float suffix of
80 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
81 flag vector describing the suffix. */
82static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000083interpret_float_suffix (const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080084{
Janis Johnsoneec49112009-04-01 17:04:42 +000085 size_t flags;
Janis Johnson839a3b82009-04-01 17:31:26 +000086 size_t f, d, l, w, q, i;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020087
Janis Johnsoneec49112009-04-01 17:04:42 +000088 flags = 0;
Janis Johnson839a3b82009-04-01 17:31:26 +000089 f = d = l = w = q = i = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000090
Janis Johnsoneec49112009-04-01 17:04:42 +000091 /* Process decimal float suffixes, which are two letters starting
92 with d or D. Order and case are significant. */
93 if (len == 2 && (*s == 'd' || *s == 'D'))
94 {
95 bool uppercase = (*s == 'D');
96 switch (s[1])
97 {
98 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
99 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
100 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
101 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
102 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
103 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 default:
Janis Johnson839a3b82009-04-01 17:31:26 +0000105 /* Additional two-character suffixes beginning with D are not
106 for decimal float constants. */
107 break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000108 }
109 }
110
111 /* Recognize a fixed-point suffix. */
112 switch (s[len-1])
113 {
114 case 'k': case 'K': flags = CPP_N_ACCUM; break;
115 case 'r': case 'R': flags = CPP_N_FRACT; break;
116 default: break;
117 }
118
119 /* Continue processing a fixed-point suffix. The suffix is case
120 insensitive except for ll or LL. Order is significant. */
121 if (flags)
122 {
123 if (len == 1)
124 return flags;
125 len--;
126
127 if (*s == 'u' || *s == 'U')
128 {
129 flags |= CPP_N_UNSIGNED;
130 if (len == 1)
131 return flags;
132 len--;
133 s++;
134 }
135
136 switch (*s)
137 {
138 case 'h': case 'H':
139 if (len == 1)
140 return flags |= CPP_N_SMALL;
141 break;
142 case 'l':
143 if (len == 1)
144 return flags |= CPP_N_MEDIUM;
145 if (len == 2 && s[1] == 'l')
146 return flags |= CPP_N_LARGE;
147 break;
148 case 'L':
149 if (len == 1)
150 return flags |= CPP_N_MEDIUM;
151 if (len == 2 && s[1] == 'L')
152 return flags |= CPP_N_LARGE;
153 break;
154 default:
155 break;
156 }
157 /* Anything left at this point is invalid. */
158 return 0;
159 }
160
161 /* In any remaining valid suffix, the case and order don't matter. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000162 while (len--)
163 switch (s[len])
164 {
Janis Johnsoneec49112009-04-01 17:04:42 +0000165 case 'f': case 'F': f++; break;
Janis Johnson839a3b82009-04-01 17:31:26 +0000166 case 'd': case 'D': d++; break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000167 case 'l': case 'L': l++; break;
168 case 'w': case 'W': w++; break;
169 case 'q': case 'Q': q++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000170 case 'i': case 'I':
171 case 'j': case 'J': i++; break;
172 default:
173 return 0;
174 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000175
Janis Johnson839a3b82009-04-01 17:31:26 +0000176 if (f + d + l + w + q > 1 || i > 1)
Jon Grimmad6ed772005-12-06 23:13:15 +0000177 return 0;
178
Neil Boothcd7ab832002-05-29 17:15:42 +0000179 return ((i ? CPP_N_IMAGINARY : 0)
180 | (f ? CPP_N_SMALL :
Janis Johnson839a3b82009-04-01 17:31:26 +0000181 d ? CPP_N_MEDIUM :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200182 l ? CPP_N_LARGE :
183 w ? CPP_N_MD_W :
Janis Johnson839a3b82009-04-01 17:31:26 +0000184 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
Neil Boothcd7ab832002-05-29 17:15:42 +0000185}
186
187/* Subroutine of cpp_classify_number. S points to an integer suffix
188 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
189 flag vector describing the suffix. */
190static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000191interpret_int_suffix (const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000192{
193 size_t u, l, i;
194
195 u = l = i = 0;
196
197 while (len--)
198 switch (s[len])
199 {
200 case 'u': case 'U': u++; break;
201 case 'i': case 'I':
202 case 'j': case 'J': i++; break;
203 case 'l': case 'L': l++;
204 /* If there are two Ls, they must be adjacent and the same case. */
205 if (l == 2 && s[len] != s[len + 1])
206 return 0;
207 break;
208 default:
209 return 0;
210 }
211
212 if (l > 2 || u > 1 || i > 1)
213 return 0;
214
215 return ((i ? CPP_N_IMAGINARY : 0)
216 | (u ? CPP_N_UNSIGNED : 0)
217 | ((l == 0) ? CPP_N_SMALL
218 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
219}
220
221/* Categorize numeric constants according to their field (integer,
222 floating point, or invalid), radix (decimal, octal, hexadecimal),
223 and type suffixes. */
224unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000225cpp_classify_number (cpp_reader *pfile, const cpp_token *token)
Neil Boothcd7ab832002-05-29 17:15:42 +0000226{
227 const uchar *str = token->val.str.text;
228 const uchar *limit;
229 unsigned int max_digit, result, radix;
230 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
231
232 /* If the lexer has done its job, length one can only be a single
233 digit. Fast-path this very common case. */
234 if (token->val.str.len == 1)
235 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
236
237 limit = str + token->val.str.len;
238 float_flag = NOT_FLOAT;
239 max_digit = 0;
240 radix = 10;
241
242 /* First, interpret the radix. */
243 if (*str == '0')
244 {
245 radix = 8;
246 str++;
247
248 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000249 if ((*str == 'x' || *str == 'X')
250 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000251 {
252 radix = 16;
253 str++;
254 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000255 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
256 {
257 radix = 2;
258 str++;
259 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000260 }
261
262 /* Now scan for a well-formed integer or float. */
263 for (;;)
264 {
265 unsigned int c = *str++;
266
267 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
268 {
269 c = hex_value (c);
270 if (c > max_digit)
271 max_digit = c;
272 }
273 else if (c == '.')
274 {
275 if (float_flag == NOT_FLOAT)
276 float_flag = AFTER_POINT;
277 else
278 SYNTAX_ERROR ("too many decimal points in number");
279 }
280 else if ((radix <= 10 && (c == 'e' || c == 'E'))
281 || (radix == 16 && (c == 'p' || c == 'P')))
282 {
283 float_flag = AFTER_EXPON;
284 break;
285 }
286 else
287 {
288 /* Start of suffix. */
289 str--;
290 break;
291 }
292 }
293
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000294 /* The suffix may be for decimal fixed-point constants without exponent. */
295 if (radix != 16 && float_flag == NOT_FLOAT)
296 {
297 result = interpret_float_suffix (str, limit - str);
298 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
299 {
300 result |= CPP_N_FLOATING;
301 /* We need to restore the radix to 10, if the radix is 8. */
302 if (radix == 8)
303 radix = 10;
304
305 if (CPP_PEDANTIC (pfile))
306 cpp_error (pfile, CPP_DL_PEDWARN,
307 "fixed-point constants are a GCC extension");
308 goto syntax_ok;
309 }
310 else
311 result = 0;
312 }
313
Neil Boothcd7ab832002-05-29 17:15:42 +0000314 if (float_flag != NOT_FLOAT && radix == 8)
315 radix = 10;
316
317 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000318 {
319 if (radix == 2)
320 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
321 else
322 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
323 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000324
325 if (float_flag != NOT_FLOAT)
326 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000327 if (radix == 2)
328 {
329 cpp_error (pfile, CPP_DL_ERROR,
330 "invalid prefix \"0b\" for floating constant");
331 return CPP_N_INVALID;
332 }
333
Neil Boothcd7ab832002-05-29 17:15:42 +0000334 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000335 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000336 "use of C99 hexadecimal floating constant");
337
338 if (float_flag == AFTER_EXPON)
339 {
340 if (*str == '+' || *str == '-')
341 str++;
342
343 /* Exponent is decimal, even if string is a hex float. */
344 if (!ISDIGIT (*str))
345 SYNTAX_ERROR ("exponent has no digits");
346
347 do
348 str++;
349 while (ISDIGIT (*str));
350 }
351 else if (radix == 16)
352 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
353
354 result = interpret_float_suffix (str, limit - str);
355 if (result == 0)
356 {
John David Anglin0527bc42003-11-01 22:56:54 +0000357 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000358 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200359 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000360 return CPP_N_INVALID;
361 }
362
363 /* Traditional C didn't accept any floating suffixes. */
364 if (limit != str
365 && CPP_WTRADITIONAL (pfile)
366 && ! cpp_sys_macro_p (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000367 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000368 "traditional C rejects the \"%.*s\" suffix",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200369 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000370
Janis Johnson839a3b82009-04-01 17:31:26 +0000371 /* A suffix for double is a GCC extension via decimal float support.
372 If the suffix also specifies an imaginary value we'll catch that
373 later. */
374 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
375 cpp_error (pfile, CPP_DL_PEDWARN,
376 "suffix for double constant is a GCC extension");
377
Jon Grimmad6ed772005-12-06 23:13:15 +0000378 /* Radix must be 10 for decimal floats. */
379 if ((result & CPP_N_DFLOAT) && radix != 10)
380 {
381 cpp_error (pfile, CPP_DL_ERROR,
382 "invalid suffix \"%.*s\" with hexadecimal floating constant",
383 (int) (limit - str), str);
384 return CPP_N_INVALID;
385 }
386
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000387 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
388 cpp_error (pfile, CPP_DL_PEDWARN,
389 "fixed-point constants are a GCC extension");
390
Janis Johnson5a6bb572007-05-14 23:45:40 +0000391 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
392 cpp_error (pfile, CPP_DL_PEDWARN,
393 "decimal float constants are a GCC extension");
394
Neil Boothcd7ab832002-05-29 17:15:42 +0000395 result |= CPP_N_FLOATING;
396 }
397 else
398 {
399 result = interpret_int_suffix (str, limit - str);
400 if (result == 0)
401 {
John David Anglin0527bc42003-11-01 22:56:54 +0000402 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000403 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200404 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000405 return CPP_N_INVALID;
406 }
407
Zack Weinberg56da7202002-08-02 04:18:16 +0000408 /* Traditional C only accepted the 'L' suffix.
409 Suppress warning about 'LL' with -Wno-long-long. */
410 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
411 {
412 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
413 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE;
414
415 if (u_or_i || (large && CPP_OPTION (pfile, warn_long_long)))
John David Anglin0527bc42003-11-01 22:56:54 +0000416 cpp_error (pfile, CPP_DL_WARNING,
Zack Weinberg56da7202002-08-02 04:18:16 +0000417 "traditional C rejects the \"%.*s\" suffix",
418 (int) (limit - str), str);
419 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000420
421 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
Neil Boothcd7ab832002-05-29 17:15:42 +0000422 && CPP_OPTION (pfile, warn_long_long))
Manuel López-Ibáñez9c650d92009-04-20 22:12:52 +0000423 cpp_error (pfile,
424 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN,
425 CPP_OPTION (pfile, cplusplus)
426 ? "use of C++0x long long integer constant"
427 : "use of C99 long long integer constant");
Neil Boothcd7ab832002-05-29 17:15:42 +0000428
429 result |= CPP_N_INTEGER;
430 }
431
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000432 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000433 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000434 cpp_error (pfile, CPP_DL_PEDWARN,
435 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000436 if (radix == 2 && CPP_PEDANTIC (pfile))
437 cpp_error (pfile, CPP_DL_PEDWARN,
438 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000439
440 if (radix == 10)
441 result |= CPP_N_DECIMAL;
442 else if (radix == 16)
443 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000444 else if (radix == 2)
445 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000446 else
447 result |= CPP_N_OCTAL;
448
449 return result;
450
451 syntax_error:
452 return CPP_N_INVALID;
453}
454
455/* cpp_interpret_integer converts an integer constant into a cpp_num,
456 of precision options->precision.
457
458 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000459 because the preprocessor doesn't need it and we don't want to
460 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000461cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000462cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
463 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000464{
465 const uchar *p, *end;
466 cpp_num result;
467
468 result.low = 0;
469 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000470 result.unsignedp = !!(type & CPP_N_UNSIGNED);
471 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000472
473 p = token->val.str.text;
474 end = p + token->val.str.len;
475
476 /* Common case of a single digit. */
477 if (token->val.str.len == 1)
478 result.low = p[0] - '0';
479 else
480 {
481 cpp_num_part max;
482 size_t precision = CPP_OPTION (pfile, precision);
483 unsigned int base = 10, c = 0;
484 bool overflow = false;
485
486 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
487 {
488 base = 8;
489 p++;
490 }
491 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
492 {
493 base = 16;
494 p += 2;
495 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000496 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
497 {
498 base = 2;
499 p += 2;
500 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000501
502 /* We can add a digit to numbers strictly less than this without
503 needing the precision and slowness of double integers. */
504 max = ~(cpp_num_part) 0;
505 if (precision < PART_PRECISION)
506 max >>= PART_PRECISION - precision;
507 max = (max - base + 1) / base + 1;
508
509 for (; p < end; p++)
510 {
511 c = *p;
512
513 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
514 c = hex_value (c);
515 else
516 break;
517
518 /* Strict inequality for when max is set to zero. */
519 if (result.low < max)
520 result.low = result.low * base + c;
521 else
522 {
523 result = append_digit (result, c, base, precision);
524 overflow |= result.overflow;
525 max = 0;
526 }
527 }
528
529 if (overflow)
John David Anglin0527bc42003-11-01 22:56:54 +0000530 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000531 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000532 /* If too big to be signed, consider it unsigned. Only warn for
533 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000534 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000535 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000536 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000537 && !(CPP_OPTION (pfile, traditional)
538 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000539 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000540 {
Neil Boothcd7ab832002-05-29 17:15:42 +0000541 if (base == 10)
John David Anglin0527bc42003-11-01 22:56:54 +0000542 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothcd7ab832002-05-29 17:15:42 +0000543 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000544 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000545 }
546 }
547
548 return result;
549}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000550
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000551/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000552static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000553append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000554{
555 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000556 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000557 bool overflow;
558 cpp_num_part add_high, add_low;
559
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000560 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000561 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000562 switch (base)
563 {
564 case 2:
565 shift = 1;
566 break;
567
568 case 16:
569 shift = 4;
570 break;
571
572 default:
573 shift = 3;
574 }
Neil Booth23ff0222002-07-17 17:27:14 +0000575 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000576 result.high = num.high << shift;
577 result.low = num.low << shift;
578 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400579 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000580
581 if (base == 10)
582 {
583 add_low = num.low << 1;
584 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
585 }
586 else
587 add_high = add_low = 0;
588
589 if (add_low + digit < add_low)
590 add_high++;
591 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000592
Neil Booth91318902002-05-26 18:42:21 +0000593 if (result.low + add_low < result.low)
594 add_high++;
595 if (result.high + add_high < result.high)
596 overflow = true;
597
598 result.low += add_low;
599 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400600 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000601
602 /* The above code catches overflow of a cpp_num type. This catches
603 overflow of the (possibly shorter) target precision. */
604 num.low = result.low;
605 num.high = result.high;
606 result = num_trim (result, precision);
607 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400608 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000609
Neil Booth91318902002-05-26 18:42:21 +0000610 return result;
611}
612
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000613/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000614static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000615parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000616{
Neil Booth91318902002-05-26 18:42:21 +0000617 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000618 int paren = 0;
619 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000620 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000621 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000622
Neil Booth93c803682000-10-28 17:59:06 +0000623 /* Don't expand macros. */
624 pfile->state.prevent_expansion++;
625
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000626 token = cpp_get_token (pfile);
627 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000628 {
629 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000630 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000631 }
632
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000633 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000634 {
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000635 node = token->val.node;
636 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000637 {
John David Anglin0527bc42003-11-01 22:56:54 +0000638 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000639 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000640 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000641 }
Neil Booth93c803682000-10-28 17:59:06 +0000642 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000643 {
John David Anglin0527bc42003-11-01 22:56:54 +0000644 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000645 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000646 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000647 {
648 cpp_token op;
649
650 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000651 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000652 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000653 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000654 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000655 cpp_token_as_text (pfile, &op));
656 }
657 }
Neil Booth93c803682000-10-28 17:59:06 +0000658
Neil Booth91318902002-05-26 18:42:21 +0000659 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000660 {
Neil Booth335d03e2003-08-03 12:23:46 +0000661 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000662 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000663 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000664
Neil Bootha69cbaa2002-07-23 22:57:49 +0000665 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100666 if (!(node->flags & NODE_USED))
667 {
668 node->flags |= NODE_USED;
669 if (node->type == NT_MACRO)
670 {
671 if (pfile->cb.used_define)
672 pfile->cb.used_define (pfile, pfile->directive_line, node);
673 }
674 else
675 {
676 if (pfile->cb.used_undef)
677 pfile->cb.used_undef (pfile, pfile->directive_line, node);
678 }
679 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000680
Neil Booth6d18adb2001-07-29 17:27:57 +0000681 /* A possible controlling macro of the form #if !defined ().
682 _cpp_parse_expr checks there was no other junk on the line. */
683 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000684 }
685
686 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000687
Neil Booth23ff0222002-07-17 17:27:14 +0000688 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000689 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000690 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000691 result.low = node && node->type == NT_MACRO;
692 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000693}
694
Neil Booth60284a52002-04-28 23:14:56 +0000695/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
696 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000697 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000698static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000699eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800700{
Neil Booth91318902002-05-26 18:42:21 +0000701 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000702 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000703 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000704
Diego Novillo6de9cd92004-05-13 02:41:07 -0400705 result.unsignedp = false;
706 result.overflow = false;
707
Neil Booth93c803682000-10-28 17:59:06 +0000708 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000709 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800710 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000711 temp = cpp_classify_number (pfile, token);
712 switch (temp & CPP_N_CATEGORY)
713 {
714 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000715 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000716 "floating constant in preprocessor expression");
717 break;
718 case CPP_N_INTEGER:
719 if (!(temp & CPP_N_IMAGINARY))
720 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000721 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000722 "imaginary number in preprocessor expression");
723 break;
724
725 case CPP_N_INVALID:
726 /* Error already issued. */
727 break;
728 }
729 result.high = result.low = 0;
730 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000731
Alexandre Oliva525bc952000-02-23 19:21:07 +0000732 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000733 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000734 case CPP_CHAR16:
735 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +0000736 {
Neil Booth91318902002-05-26 18:42:21 +0000737 cppchar_t cc = cpp_interpret_charconst (pfile, token,
738 &temp, &unsignedp);
739
740 result.high = 0;
741 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000742 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000743 if (!unsignedp && (cppchar_signed_t) cc < 0)
744 {
745 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
746 result.low |= ~(~(cpp_num_part) 0
747 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
748 result.high = ~(cpp_num_part) 0;
749 result = num_trim (result, CPP_OPTION (pfile, precision));
750 }
Neil Bootha5a49442002-05-06 22:53:10 +0000751 }
Neil Booth60284a52002-04-28 23:14:56 +0000752 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000753
Zack Weinberg92936ec2000-07-19 20:18:08 +0000754 case CPP_NAME:
Neil Booth93c803682000-10-28 17:59:06 +0000755 if (token->val.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000756 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000757 else if (CPP_OPTION (pfile, cplusplus)
758 && (token->val.node == pfile->spec_nodes.n_true
759 || token->val.node == pfile->spec_nodes.n_false))
760 {
Neil Booth91318902002-05-26 18:42:21 +0000761 result.high = 0;
762 result.low = (token->val.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000763 }
764 else
765 {
Neil Booth91318902002-05-26 18:42:21 +0000766 result.high = 0;
767 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000768 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +0000769 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined",
Neil Boothebef4e82002-04-14 18:42:47 +0000770 NODE_NAME (token->val.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000771 }
Neil Booth60284a52002-04-28 23:14:56 +0000772 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000773
Tom Tromey899015a2008-05-13 14:50:27 +0000774 case CPP_HASH:
775 if (!pfile->state.skipping)
776 {
777 /* A pedantic warning takes precedence over a deprecated
778 warning here. */
779 if (CPP_PEDANTIC (pfile))
780 cpp_error (pfile, CPP_DL_PEDWARN,
781 "assertions are a GCC extension");
782 else if (CPP_OPTION (pfile, warn_deprecated))
783 cpp_error (pfile, CPP_DL_WARNING,
784 "assertions are a deprecated extension");
785 }
Neil Booth91318902002-05-26 18:42:21 +0000786 _cpp_test_assertion (pfile, &temp);
787 result.high = 0;
788 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +0000789 break;
790
791 default:
792 abort ();
Neil Booth93c803682000-10-28 17:59:06 +0000793 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800794
Neil Booth23ff0222002-07-17 17:27:14 +0000795 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000796 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800797}
798
Neil Booth4063b942000-04-02 08:27:23 +0000799/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000800
801After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000802than the operator on the top of the stack, we reduce the stack by one
803operator and repeat the test. Since equal priorities do not reduce,
804this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000805
Neil Booth87ed1092002-04-28 19:42:54 +0000806We handle left-associative operators by decrementing the priority of
807just-lexed operators by one, but retaining the priority of operators
808already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000809
810The remaining cases are '(' and ')'. We handle '(' by skipping the
811reduction phase completely. ')' is given lower priority than
812everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000813parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000814reduce() exits immediately. If the normal exit route sees a ')', then
815there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000816
Neil Boothf8b954f2002-04-26 06:32:50 +0000817The parser assumes all shifted operators require a left operand unless
818the flag NO_L_OPERAND is set. These semantics are automatic; any
819extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800820
Neil Booth68e652752002-07-20 13:31:56 +0000821/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
822 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000823#define NO_L_OPERAND (1 << 0)
824#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000825#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000826
Zack Weinbergcf00a882000-07-08 02:33:00 +0000827/* Operator to priority map. Must be in the same order as the first
828 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000829static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000830{
Neil Booth60284a52002-04-28 23:14:56 +0000831 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000832 uchar flags;
833} optab[] =
834{
Neil Boothad28cff2002-07-18 22:08:35 +0000835 /* EQ */ {0, 0}, /* Shouldn't happen. */
836 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000837 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
838 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
839 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
840 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
841 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
842 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
843 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
844 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
845 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
846 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000847 /* RSHIFT */ {13, LEFT_ASSOC},
848 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000849
Neil Boothad28cff2002-07-18 22:08:35 +0000850 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000851 /* AND_AND */ {6, LEFT_ASSOC},
852 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +0000853 /* Note that QUERY, COLON, and COMMA must have the same precedence.
854 However, there are some special cases for these in reduce(). */
855 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000856 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +0000857 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000858 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000859 /* CLOSE_PAREN */ {0, 0},
860 /* EOF */ {0, 0},
861 /* EQ_EQ */ {11, LEFT_ASSOC},
862 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000863 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
864 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000865 /* UPLUS */ {16, NO_L_OPERAND},
866 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000867};
868
Per Bothner7f2935c1995-03-16 13:59:07 -0800869/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000870 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000871
872 The implementation is an operator precedence parser, i.e. a
873 bottom-up parser, using a stack for not-yet-reduced tokens.
874
875 The stack base is op_stack, and the current stack pointer is 'top'.
876 There is a stack element for each operator (only), and the most
877 recently pushed operator is 'top->op'. An operand (value) is
878 stored in the 'value' field of the stack element of the operator
879 that precedes it. */
880bool
Tom Tromeyd7508872008-05-30 14:25:09 +0000881_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -0800882{
Neil Booth87ed1092002-04-28 19:42:54 +0000883 struct op *top = pfile->op_stack;
884 unsigned int lex_count;
885 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800886
Neil Booth87ed1092002-04-28 19:42:54 +0000887 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800888
Neil Booth93c803682000-10-28 17:59:06 +0000889 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000890 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000891 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000892 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000893
Neil Booth87ed1092002-04-28 19:42:54 +0000894 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000895 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000896
Per Bothner7f2935c1995-03-16 13:59:07 -0800897 for (;;)
898 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000899 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800900
Neil Booth6d18adb2001-07-29 17:27:57 +0000901 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000902 op.token = cpp_get_token (pfile);
903 op.op = op.token->type;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +0000904 op.loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -0800905
Per Bothner7f2935c1995-03-16 13:59:07 -0800906 switch (op.op)
907 {
Neil Booth60284a52002-04-28 23:14:56 +0000908 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000909 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000910 case CPP_CHAR:
911 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000912 case CPP_CHAR16:
913 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +0000914 case CPP_NAME:
915 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000916 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000917 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000918 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000919 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000920 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000921 continue;
922
Neil Booth6d18adb2001-07-29 17:27:57 +0000923 case CPP_NOT:
924 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000925 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000926 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000927 if (want_value)
928 op.op = CPP_UPLUS;
929 break;
930 case CPP_MINUS:
931 if (want_value)
932 op.op = CPP_UMINUS;
933 break;
Neil Booth60284a52002-04-28 23:14:56 +0000934
Neil Boothf8b954f2002-04-26 06:32:50 +0000935 default:
Neil Booth60284a52002-04-28 23:14:56 +0000936 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000937 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000938 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000939 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800940 }
941
Neil Booth87ed1092002-04-28 19:42:54 +0000942 /* Check we have a value or operator as appropriate. */
943 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000944 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000945 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000946 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000947 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000948 }
Neil Booth87ed1092002-04-28 19:42:54 +0000949 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000950 {
Neil Bootha09d4742004-07-04 12:57:50 +0000951 /* We want a number (or expression) and haven't got one.
952 Try to emit a specific diagnostic. */
953 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
954 SYNTAX_ERROR ("missing expression between '(' and ')'");
955
956 if (op.op == CPP_EOF && top->op == CPP_EOF)
Tom Tromeyd7508872008-05-30 14:25:09 +0000957 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +0000958
959 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
960 SYNTAX_ERROR2 ("operator '%s' has no right operand",
961 cpp_token_as_text (pfile, top->token));
962 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
963 /* Complain about missing paren during reduction. */;
964 else
965 SYNTAX_ERROR2 ("operator '%s' has no left operand",
966 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000967 }
Neil Booth87ed1092002-04-28 19:42:54 +0000968
969 top = reduce (pfile, top, op.op);
970 if (!top)
971 goto syntax_error;
972
Neil Booth60284a52002-04-28 23:14:56 +0000973 if (op.op == CPP_EOF)
974 break;
975
Neil Booth87ed1092002-04-28 19:42:54 +0000976 switch (op.op)
977 {
978 case CPP_CLOSE_PAREN:
979 continue;
Neil Booth87ed1092002-04-28 19:42:54 +0000980 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +0000981 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000982 pfile->state.skip_eval++;
983 break;
984 case CPP_AND_AND:
985 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +0000986 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +0000987 pfile->state.skip_eval++;
988 break;
989 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +0000990 if (top->op != CPP_QUERY)
991 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +0000992 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +0000993 pfile->state.skip_eval++;
994 else
995 pfile->state.skip_eval--;
996 default:
997 break;
998 }
999
Neil Boothf8b954f2002-04-26 06:32:50 +00001000 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +00001001
Mike Stump0f413021996-07-03 22:07:53 +00001002 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +00001003 if (++top == pfile->op_limit)
1004 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +00001005
Per Bothner7f2935c1995-03-16 13:59:07 -08001006 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +00001007 top->token = op.token;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001008 top->loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001009 }
Neil Booth9ee703132000-04-01 07:42:37 +00001010
Neil Booth6d18adb2001-07-29 17:27:57 +00001011 /* The controlling macro expression is only valid if we called lex 3
1012 times: <!> <defined expression> and <EOF>. push_conditional ()
1013 checks that we are at top-of-file. */
1014 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1015 pfile->mi_ind_cmacro = 0;
1016
Neil Booth87ed1092002-04-28 19:42:54 +00001017 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +00001018 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001019 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1020 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +00001021 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +00001022 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +00001023 }
Neil Booth9ee703132000-04-01 07:42:37 +00001024
Neil Booth91318902002-05-26 18:42:21 +00001025 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001026}
1027
1028/* Reduce the operator / value stack if possible, in preparation for
1029 pushing operator OP. Returns NULL on error, otherwise the top of
1030 the stack. */
1031static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001032reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001033{
1034 unsigned int prio;
1035
Neil Booth91318902002-05-26 18:42:21 +00001036 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1037 {
1038 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001039 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001040 return 0;
1041 }
1042
Neil Booth87ed1092002-04-28 19:42:54 +00001043 if (op == CPP_OPEN_PAREN)
1044 return top;
1045
1046 /* Decrement the priority of left-associative operators to force a
1047 reduction with operators of otherwise equal priority. */
1048 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1049 while (prio < optab[top->op].prio)
1050 {
Neil Booth68e652752002-07-20 13:31:56 +00001051 if (CPP_OPTION (pfile, warn_num_sign_change)
1052 && optab[top->op].flags & CHECK_PROMOTION)
1053 check_promotion (pfile, top);
1054
Neil Booth75aef482002-07-19 19:24:43 +00001055 switch (top->op)
1056 {
1057 case CPP_UPLUS:
1058 case CPP_UMINUS:
1059 case CPP_NOT:
1060 case CPP_COMPL:
1061 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001062 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001063 break;
Neil Booth91318902002-05-26 18:42:21 +00001064
Neil Booth75aef482002-07-19 19:24:43 +00001065 case CPP_PLUS:
1066 case CPP_MINUS:
1067 case CPP_RSHIFT:
1068 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001069 case CPP_COMMA:
1070 top[-1].value = num_binary_op (pfile, top[-1].value,
1071 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001072 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001073 break;
Neil Booth91318902002-05-26 18:42:21 +00001074
Neil Booth75aef482002-07-19 19:24:43 +00001075 case CPP_GREATER:
1076 case CPP_LESS:
1077 case CPP_GREATER_EQ:
1078 case CPP_LESS_EQ:
1079 top[-1].value
1080 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001081 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001082 break;
Neil Booth91318902002-05-26 18:42:21 +00001083
Neil Booth75aef482002-07-19 19:24:43 +00001084 case CPP_EQ_EQ:
1085 case CPP_NOT_EQ:
1086 top[-1].value
1087 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001088 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001089 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001090
Neil Booth75aef482002-07-19 19:24:43 +00001091 case CPP_AND:
1092 case CPP_OR:
1093 case CPP_XOR:
1094 top[-1].value
1095 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001096 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001097 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001098
Neil Booth75aef482002-07-19 19:24:43 +00001099 case CPP_MULT:
1100 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001101 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001102 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001103
Neil Booth75aef482002-07-19 19:24:43 +00001104 case CPP_DIV:
1105 case CPP_MOD:
1106 top[-1].value = num_div_op (pfile, top[-1].value,
1107 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001108 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001109 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001110
Neil Booth75aef482002-07-19 19:24:43 +00001111 case CPP_OR_OR:
1112 top--;
1113 if (!num_zerop (top->value))
1114 pfile->state.skip_eval--;
1115 top->value.low = (!num_zerop (top->value)
1116 || !num_zerop (top[1].value));
1117 top->value.high = 0;
1118 top->value.unsignedp = false;
1119 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001120 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001121 continue;
1122
1123 case CPP_AND_AND:
1124 top--;
1125 if (num_zerop (top->value))
1126 pfile->state.skip_eval--;
1127 top->value.low = (!num_zerop (top->value)
1128 && !num_zerop (top[1].value));
1129 top->value.high = 0;
1130 top->value.unsignedp = false;
1131 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001132 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001133 continue;
1134
1135 case CPP_OPEN_PAREN:
1136 if (op != CPP_CLOSE_PAREN)
1137 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001138 cpp_error_with_line (pfile, CPP_DL_ERROR,
1139 top->token->src_loc,
1140 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001141 return 0;
1142 }
1143 top--;
1144 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001145 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001146 return top;
1147
1148 case CPP_COLON:
1149 top -= 2;
1150 if (!num_zerop (top->value))
1151 {
Neil Booth91318902002-05-26 18:42:21 +00001152 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001153 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001154 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001155 }
1156 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001157 {
1158 top->value = top[2].value;
1159 top->loc = top[2].loc;
1160 }
Neil Booth75aef482002-07-19 19:24:43 +00001161 top->value.unsignedp = (top[1].value.unsignedp
1162 || top[2].value.unsignedp);
1163 continue;
Neil Booth91318902002-05-26 18:42:21 +00001164
Neil Booth75aef482002-07-19 19:24:43 +00001165 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001166 /* COMMA and COLON should not reduce a QUERY operator. */
1167 if (op == CPP_COMMA || op == CPP_COLON)
1168 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001169 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001170 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001171
Neil Booth75aef482002-07-19 19:24:43 +00001172 default:
1173 goto bad_op;
1174 }
Neil Boothad28cff2002-07-18 22:08:35 +00001175
1176 top--;
Neil Booth91318902002-05-26 18:42:21 +00001177 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001178 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001179 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001180 }
1181
1182 if (op == CPP_CLOSE_PAREN)
1183 {
John David Anglin0527bc42003-11-01 22:56:54 +00001184 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001185 return 0;
1186 }
1187
1188 return top;
1189}
1190
1191/* Returns the position of the old top of stack after expansion. */
1192struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001193_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001194{
Neil Booth32fa4562002-05-09 22:27:31 +00001195 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1196 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001197
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001198 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001199 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001200
Neil Booth32fa4562002-05-09 22:27:31 +00001201 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001202}
Neil Booth91318902002-05-26 18:42:21 +00001203
Neil Booth68e652752002-07-20 13:31:56 +00001204/* Emits a warning if the effective sign of either operand of OP
1205 changes because of integer promotions. */
1206static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001207check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001208{
1209 if (op->value.unsignedp == op[-1].value.unsignedp)
1210 return;
1211
1212 if (op->value.unsignedp)
1213 {
1214 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001215 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1216 "the left operand of \"%s\" changes sign when promoted",
1217 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001218 }
1219 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001220 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001221 "the right operand of \"%s\" changes sign when promoted",
1222 cpp_token_as_text (pfile, op->token));
1223}
1224
Neil Booth91318902002-05-26 18:42:21 +00001225/* Clears the unused high order bits of the number pointed to by PNUM. */
1226static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001227num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001228{
1229 if (precision > PART_PRECISION)
1230 {
1231 precision -= PART_PRECISION;
1232 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001233 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001234 }
1235 else
1236 {
1237 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001238 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001239 num.high = 0;
1240 }
1241
1242 return num;
1243}
1244
1245/* True iff A (presumed signed) >= 0. */
1246static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001247num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001248{
1249 if (precision > PART_PRECISION)
1250 {
1251 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001252 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001253 }
1254
Neil Booth359b0be2002-05-28 05:44:06 +00001255 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001256}
1257
Neil Boothceeedfc2002-06-02 19:37:34 +00001258/* Sign extend a number, with PRECISION significant bits and all
1259 others assumed clear, to fill out a cpp_num structure. */
1260cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001261cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001262{
1263 if (!num.unsignedp)
1264 {
1265 if (precision > PART_PRECISION)
1266 {
1267 precision -= PART_PRECISION;
1268 if (precision < PART_PRECISION
1269 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1270 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1271 }
1272 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1273 {
1274 if (precision < PART_PRECISION)
1275 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1276 num.high = ~(cpp_num_part) 0;
1277 }
1278 }
1279
1280 return num;
1281}
1282
Neil Booth91318902002-05-26 18:42:21 +00001283/* Returns the negative of NUM. */
1284static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001285num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001286{
1287 cpp_num copy;
1288
1289 copy = num;
1290 num.high = ~num.high;
1291 num.low = ~num.low;
1292 if (++num.low == 0)
1293 num.high++;
1294 num = num_trim (num, precision);
1295 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1296
1297 return num;
1298}
1299
1300/* Returns true if A >= B. */
1301static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001302num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001303{
1304 bool unsignedp;
1305
1306 unsignedp = pa.unsignedp || pb.unsignedp;
1307
1308 if (!unsignedp)
1309 {
1310 /* Both numbers have signed type. If they are of different
1311 sign, the answer is the sign of A. */
1312 unsignedp = num_positive (pa, precision);
1313
1314 if (unsignedp != num_positive (pb, precision))
1315 return unsignedp;
1316
1317 /* Otherwise we can do an unsigned comparison. */
1318 }
1319
1320 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1321}
1322
1323/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1324static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001325num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1326 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001327{
1328 lhs.overflow = false;
1329 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1330
1331 /* As excess precision is zeroed, there is no need to num_trim () as
1332 these operations cannot introduce a set bit there. */
1333 if (op == CPP_AND)
1334 {
1335 lhs.low &= rhs.low;
1336 lhs.high &= rhs.high;
1337 }
1338 else if (op == CPP_OR)
1339 {
1340 lhs.low |= rhs.low;
1341 lhs.high |= rhs.high;
1342 }
1343 else
1344 {
1345 lhs.low ^= rhs.low;
1346 lhs.high ^= rhs.high;
1347 }
1348
1349 return lhs;
1350}
1351
1352/* Returns LHS OP RHS, where OP is an inequality. */
1353static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001354num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1355 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001356{
1357 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1358
1359 if (op == CPP_GREATER_EQ)
1360 lhs.low = gte;
1361 else if (op == CPP_LESS)
1362 lhs.low = !gte;
1363 else if (op == CPP_GREATER)
1364 lhs.low = gte && !num_eq (lhs, rhs);
1365 else /* CPP_LESS_EQ. */
1366 lhs.low = !gte || num_eq (lhs, rhs);
1367
1368 lhs.high = 0;
1369 lhs.overflow = false;
1370 lhs.unsignedp = false;
1371 return lhs;
1372}
1373
1374/* Returns LHS OP RHS, where OP is == or !=. */
1375static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001376num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1377 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001378{
Jason Merrill97459792002-06-07 09:29:17 -04001379 /* Work around a 3.0.4 bug; see PR 6950. */
1380 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001381 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001382 eq = !eq;
1383 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001384 lhs.high = 0;
1385 lhs.overflow = false;
1386 lhs.unsignedp = false;
1387 return lhs;
1388}
1389
1390/* Shift NUM, of width PRECISION, right by N bits. */
1391static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001392num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001393{
1394 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001395 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001396
Diego Novillo6de9cd92004-05-13 02:41:07 -04001397 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001398 sign_mask = 0;
1399 else
1400 sign_mask = ~(cpp_num_part) 0;
1401
1402 if (n >= precision)
1403 num.high = num.low = sign_mask;
1404 else
1405 {
1406 /* Sign-extend. */
1407 if (precision < PART_PRECISION)
1408 num.high = sign_mask, num.low |= sign_mask << precision;
1409 else if (precision < 2 * PART_PRECISION)
1410 num.high |= sign_mask << (precision - PART_PRECISION);
1411
1412 if (n >= PART_PRECISION)
1413 {
1414 n -= PART_PRECISION;
1415 num.low = num.high;
1416 num.high = sign_mask;
1417 }
1418
1419 if (n)
1420 {
1421 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1422 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1423 }
1424 }
1425
1426 num = num_trim (num, precision);
1427 num.overflow = false;
1428 return num;
1429}
1430
1431/* Shift NUM, of width PRECISION, left by N bits. */
1432static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001433num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001434{
1435 if (n >= precision)
1436 {
1437 num.overflow = !num.unsignedp && !num_zerop (num);
1438 num.high = num.low = 0;
1439 }
1440 else
1441 {
1442 cpp_num orig, maybe_orig;
1443 size_t m = n;
1444
1445 orig = num;
1446 if (m >= PART_PRECISION)
1447 {
1448 m -= PART_PRECISION;
1449 num.high = num.low;
1450 num.low = 0;
1451 }
1452 if (m)
1453 {
1454 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1455 num.low <<= m;
1456 }
1457 num = num_trim (num, precision);
1458
1459 if (num.unsignedp)
1460 num.overflow = false;
1461 else
1462 {
1463 maybe_orig = num_rshift (num, precision, n);
1464 num.overflow = !num_eq (orig, maybe_orig);
1465 }
1466 }
1467
1468 return num;
1469}
1470
1471/* The four unary operators: +, -, ! and ~. */
1472static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001473num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001474{
1475 switch (op)
1476 {
1477 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001478 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001479 cpp_error (pfile, CPP_DL_WARNING,
Neil Booth91318902002-05-26 18:42:21 +00001480 "traditional C rejects the unary plus operator");
1481 num.overflow = false;
1482 break;
1483
1484 case CPP_UMINUS:
1485 num = num_negate (num, CPP_OPTION (pfile, precision));
1486 break;
1487
1488 case CPP_COMPL:
1489 num.high = ~num.high;
1490 num.low = ~num.low;
1491 num = num_trim (num, CPP_OPTION (pfile, precision));
1492 num.overflow = false;
1493 break;
1494
1495 default: /* case CPP_NOT: */
1496 num.low = num_zerop (num);
1497 num.high = 0;
1498 num.overflow = false;
1499 num.unsignedp = false;
1500 break;
1501 }
1502
1503 return num;
1504}
1505
1506/* The various binary operators. */
1507static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001508num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001509{
1510 cpp_num result;
1511 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001512 size_t n;
1513
1514 switch (op)
1515 {
1516 /* Shifts. */
1517 case CPP_LSHIFT:
1518 case CPP_RSHIFT:
1519 if (!rhs.unsignedp && !num_positive (rhs, precision))
1520 {
1521 /* A negative shift is a positive shift the other way. */
1522 if (op == CPP_LSHIFT)
1523 op = CPP_RSHIFT;
1524 else
1525 op = CPP_LSHIFT;
1526 rhs = num_negate (rhs, precision);
1527 }
1528 if (rhs.high)
1529 n = ~0; /* Maximal. */
1530 else
1531 n = rhs.low;
1532 if (op == CPP_LSHIFT)
1533 lhs = num_lshift (lhs, precision, n);
1534 else
1535 lhs = num_rshift (lhs, precision, n);
1536 break;
1537
Neil Booth91318902002-05-26 18:42:21 +00001538 /* Arithmetic. */
1539 case CPP_MINUS:
1540 rhs = num_negate (rhs, precision);
1541 case CPP_PLUS:
1542 result.low = lhs.low + rhs.low;
1543 result.high = lhs.high + rhs.high;
1544 if (result.low < lhs.low)
1545 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001546 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1547 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001548
1549 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001550 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001551 {
1552 bool lhsp = num_positive (lhs, precision);
1553 result.overflow = (lhsp == num_positive (rhs, precision)
1554 && lhsp != num_positive (result, precision));
1555 }
1556 return result;
1557
1558 /* Comma. */
1559 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001560 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1561 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001562 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001563 "comma operator in operand of #if");
1564 lhs = rhs;
1565 break;
1566 }
1567
1568 return lhs;
1569}
1570
1571/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1572 cannot overflow. */
1573static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001574num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001575{
1576 cpp_num result;
1577 cpp_num_part middle[2], temp;
1578
1579 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1580 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1581
1582 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1583 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1584
1585 temp = result.low;
1586 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1587 if (result.low < temp)
1588 result.high++;
1589
1590 temp = result.low;
1591 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1592 if (result.low < temp)
1593 result.high++;
1594
1595 result.high += HIGH_PART (middle[0]);
1596 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001597 result.unsignedp = true;
1598 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001599
1600 return result;
1601}
1602
1603/* Multiply two preprocessing numbers. */
1604static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001605num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001606{
1607 cpp_num result, temp;
1608 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1609 bool overflow, negate = false;
1610 size_t precision = CPP_OPTION (pfile, precision);
1611
1612 /* Prepare for unsigned multiplication. */
1613 if (!unsignedp)
1614 {
1615 if (!num_positive (lhs, precision))
1616 negate = !negate, lhs = num_negate (lhs, precision);
1617 if (!num_positive (rhs, precision))
1618 negate = !negate, rhs = num_negate (rhs, precision);
1619 }
1620
1621 overflow = lhs.high && rhs.high;
1622 result = num_part_mul (lhs.low, rhs.low);
1623
1624 temp = num_part_mul (lhs.high, rhs.low);
1625 result.high += temp.low;
1626 if (temp.high)
1627 overflow = true;
1628
1629 temp = num_part_mul (lhs.low, rhs.high);
1630 result.high += temp.low;
1631 if (temp.high)
1632 overflow = true;
1633
1634 temp.low = result.low, temp.high = result.high;
1635 result = num_trim (result, precision);
1636 if (!num_eq (result, temp))
1637 overflow = true;
1638
1639 if (negate)
1640 result = num_negate (result, precision);
1641
1642 if (unsignedp)
1643 result.overflow = false;
1644 else
1645 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1646 && !num_zerop (result));
1647 result.unsignedp = unsignedp;
1648
1649 return result;
1650}
1651
1652/* Divide two preprocessing numbers, returning the answer or the
1653 remainder depending upon OP. */
1654static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001655num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001656{
1657 cpp_num result, sub;
1658 cpp_num_part mask;
1659 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1660 bool negate = false, lhs_neg = false;
1661 size_t i, precision = CPP_OPTION (pfile, precision);
1662
1663 /* Prepare for unsigned division. */
1664 if (!unsignedp)
1665 {
1666 if (!num_positive (lhs, precision))
1667 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1668 if (!num_positive (rhs, precision))
1669 negate = !negate, rhs = num_negate (rhs, precision);
1670 }
1671
1672 /* Find the high bit. */
1673 if (rhs.high)
1674 {
1675 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001676 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001677 for (; ; i--, mask >>= 1)
1678 if (rhs.high & mask)
1679 break;
1680 }
1681 else if (rhs.low)
1682 {
1683 if (precision > PART_PRECISION)
1684 i = precision - PART_PRECISION - 1;
1685 else
1686 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001687 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001688 for (; ; i--, mask >>= 1)
1689 if (rhs.low & mask)
1690 break;
1691 }
1692 else
1693 {
Neil Booth75aef482002-07-19 19:24:43 +00001694 if (!pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001695 cpp_error (pfile, CPP_DL_ERROR, "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001696 return lhs;
1697 }
1698
Kazu Hiratada7d8302002-09-22 02:03:17 +00001699 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001700 shifting the RHS fully left, and subtracting from LHS if LHS is
1701 at least as big, and then repeating but with one less shift.
1702 This is not very efficient, but is easy to understand. */
1703
1704 rhs.unsignedp = true;
1705 lhs.unsignedp = true;
1706 i = precision - i - 1;
1707 sub = num_lshift (rhs, precision, i);
1708
1709 result.high = result.low = 0;
1710 for (;;)
1711 {
1712 if (num_greater_eq (lhs, sub, precision))
1713 {
1714 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1715 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001716 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001717 else
Neil Booth359b0be2002-05-28 05:44:06 +00001718 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001719 }
1720 if (i-- == 0)
1721 break;
1722 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1723 sub.high >>= 1;
1724 }
1725
1726 /* We divide so that the remainder has the sign of the LHS. */
1727 if (op == CPP_DIV)
1728 {
1729 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001730 result.overflow = false;
1731 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001732 {
1733 if (negate)
1734 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001735 result.overflow = (num_positive (result, precision) ^ !negate
1736 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001737 }
1738
1739 return result;
1740 }
1741
1742 /* CPP_MOD. */
1743 lhs.unsignedp = unsignedp;
1744 lhs.overflow = false;
1745 if (lhs_neg)
1746 lhs = num_negate (lhs, precision);
1747
1748 return lhs;
1749}