blob: 4dbc98974afe00a1f1561bc8ccdba5a8910fad00 [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,
Joseph Myersdadab4f2010-01-01 18:08:17 +00003 2002, 2004, 2008, 2009, 2010 Free Software Foundation.
Richard Kennere38992e2000-04-18 20:42:00 +00004 Contributed by Per Bothner, 1994.
Per Bothner7f2935c1995-03-16 13:59:07 -08005
6This program is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +02008Free Software Foundation; either version 3, or (at your option) any
Per Bothner7f2935c1995-03-16 13:59:07 -08009later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020017along with this program; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
Per Bothner7f2935c1995-03-16 13:59:07 -080019
Per Bothner7f2935c1995-03-16 13:59:07 -080020#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000021#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000022#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000023#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080024
Neil Booth91318902002-05-26 18:42:21 +000025#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27#define LOW_PART(num_part) (num_part & HALF_MASK)
28#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
29
Zack Weinbergcf00a882000-07-08 02:33:00 +000030struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000031{
Neil Booth68e652752002-07-20 13:31:56 +000032 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000033 cpp_num value; /* The value logically "right" of op. */
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +000034 source_location loc; /* The location of this value. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000035 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080036};
Per Bothner7f2935c1995-03-16 13:59:07 -080037
Neil Booth91318902002-05-26 18:42:21 +000038/* Some simple utility routines on double integers. */
39#define num_zerop(num) ((num.low | num.high) == 0)
40#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000041static bool num_positive (cpp_num, size_t);
42static bool num_greater_eq (cpp_num, cpp_num, size_t);
43static cpp_num num_trim (cpp_num, size_t);
44static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000045
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000046static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
47static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
48static cpp_num num_negate (cpp_num, size_t);
49static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
50static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
51 enum cpp_ttype);
52static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
53 enum cpp_ttype);
54static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +000055static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
56 source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000057static cpp_num num_lshift (cpp_num, size_t, size_t);
58static cpp_num num_rshift (cpp_num, size_t, size_t);
Neil Booth91318902002-05-26 18:42:21 +000059
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000060static cpp_num append_digit (cpp_num, int, int, size_t);
61static cpp_num parse_defined (cpp_reader *);
62static cpp_num eval_token (cpp_reader *, const cpp_token *);
63static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
64static unsigned int interpret_float_suffix (const uchar *, size_t);
65static unsigned int interpret_int_suffix (const uchar *, size_t);
66static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000067
Neil Boothcd7ab832002-05-29 17:15:42 +000068/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000069#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000071
Zack Weinberg15dad1d2000-05-18 15:55:46 +000072/* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000074#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000075 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000076#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000077 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
78 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000079
Neil Boothcd7ab832002-05-29 17:15:42 +000080/* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
83static unsigned int
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000084interpret_float_suffix (const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080085{
Janis Johnsoneec49112009-04-01 17:04:42 +000086 size_t flags;
Janis Johnson839a3b82009-04-01 17:31:26 +000087 size_t f, d, l, w, q, i;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020088
Janis Johnsoneec49112009-04-01 17:04:42 +000089 flags = 0;
Janis Johnson839a3b82009-04-01 17:31:26 +000090 f = d = l = w = q = i = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000091
Janis Johnsoneec49112009-04-01 17:04:42 +000092 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */
94 if (len == 2 && (*s == 'd' || *s == 'D'))
95 {
96 bool uppercase = (*s == 'D');
97 switch (s[1])
98 {
99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
105 default:
Janis Johnson839a3b82009-04-01 17:31:26 +0000106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
108 break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000109 }
110 }
111
112 /* Recognize a fixed-point suffix. */
113 switch (s[len-1])
114 {
115 case 'k': case 'K': flags = CPP_N_ACCUM; break;
116 case 'r': case 'R': flags = CPP_N_FRACT; break;
117 default: break;
118 }
119
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
122 if (flags)
123 {
124 if (len == 1)
125 return flags;
126 len--;
127
128 if (*s == 'u' || *s == 'U')
129 {
130 flags |= CPP_N_UNSIGNED;
131 if (len == 1)
132 return flags;
133 len--;
134 s++;
135 }
136
137 switch (*s)
138 {
139 case 'h': case 'H':
140 if (len == 1)
141 return flags |= CPP_N_SMALL;
142 break;
143 case 'l':
144 if (len == 1)
145 return flags |= CPP_N_MEDIUM;
146 if (len == 2 && s[1] == 'l')
147 return flags |= CPP_N_LARGE;
148 break;
149 case 'L':
150 if (len == 1)
151 return flags |= CPP_N_MEDIUM;
152 if (len == 2 && s[1] == 'L')
153 return flags |= CPP_N_LARGE;
154 break;
155 default:
156 break;
157 }
158 /* Anything left at this point is invalid. */
159 return 0;
160 }
161
162 /* In any remaining valid suffix, the case and order don't matter. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000163 while (len--)
164 switch (s[len])
165 {
Janis Johnsoneec49112009-04-01 17:04:42 +0000166 case 'f': case 'F': f++; break;
Janis Johnson839a3b82009-04-01 17:31:26 +0000167 case 'd': case 'D': d++; break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000168 case 'l': case 'L': l++; break;
169 case 'w': case 'W': w++; break;
170 case 'q': case 'Q': q++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000171 case 'i': case 'I':
172 case 'j': case 'J': i++; break;
173 default:
174 return 0;
175 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000176
Janis Johnson839a3b82009-04-01 17:31:26 +0000177 if (f + d + l + w + q > 1 || i > 1)
Jon Grimmad6ed772005-12-06 23:13:15 +0000178 return 0;
179
Neil Boothcd7ab832002-05-29 17:15:42 +0000180 return ((i ? CPP_N_IMAGINARY : 0)
181 | (f ? CPP_N_SMALL :
Janis Johnson839a3b82009-04-01 17:31:26 +0000182 d ? CPP_N_MEDIUM :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200183 l ? CPP_N_LARGE :
184 w ? CPP_N_MD_W :
Janis Johnson839a3b82009-04-01 17:31:26 +0000185 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
Neil Boothcd7ab832002-05-29 17:15:42 +0000186}
187
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;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000232 bool seen_digit;
Neil Boothcd7ab832002-05-29 17:15:42 +0000233
234 /* If the lexer has done its job, length one can only be a single
235 digit. Fast-path this very common case. */
236 if (token->val.str.len == 1)
237 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
238
239 limit = str + token->val.str.len;
240 float_flag = NOT_FLOAT;
241 max_digit = 0;
242 radix = 10;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000243 seen_digit = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000244
245 /* First, interpret the radix. */
246 if (*str == '0')
247 {
248 radix = 8;
249 str++;
250
251 /* Require at least one hex digit to classify it as hex. */
Michael Matz7f1fc382003-03-31 15:50:53 +0000252 if ((*str == 'x' || *str == 'X')
253 && (str[1] == '.' || ISXDIGIT (str[1])))
Neil Boothcd7ab832002-05-29 17:15:42 +0000254 {
255 radix = 16;
256 str++;
257 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000258 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1'))
259 {
260 radix = 2;
261 str++;
262 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000263 }
264
265 /* Now scan for a well-formed integer or float. */
266 for (;;)
267 {
268 unsigned int c = *str++;
269
270 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
271 {
Joseph Myersdadab4f2010-01-01 18:08:17 +0000272 seen_digit = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000273 c = hex_value (c);
274 if (c > max_digit)
275 max_digit = c;
276 }
277 else if (c == '.')
278 {
279 if (float_flag == NOT_FLOAT)
280 float_flag = AFTER_POINT;
281 else
282 SYNTAX_ERROR ("too many decimal points in number");
283 }
284 else if ((radix <= 10 && (c == 'e' || c == 'E'))
285 || (radix == 16 && (c == 'p' || c == 'P')))
286 {
287 float_flag = AFTER_EXPON;
288 break;
289 }
290 else
291 {
292 /* Start of suffix. */
293 str--;
294 break;
295 }
296 }
297
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000298 /* The suffix may be for decimal fixed-point constants without exponent. */
299 if (radix != 16 && float_flag == NOT_FLOAT)
300 {
301 result = interpret_float_suffix (str, limit - str);
302 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
303 {
304 result |= CPP_N_FLOATING;
305 /* We need to restore the radix to 10, if the radix is 8. */
306 if (radix == 8)
307 radix = 10;
308
309 if (CPP_PEDANTIC (pfile))
310 cpp_error (pfile, CPP_DL_PEDWARN,
311 "fixed-point constants are a GCC extension");
312 goto syntax_ok;
313 }
314 else
315 result = 0;
316 }
317
Neil Boothcd7ab832002-05-29 17:15:42 +0000318 if (float_flag != NOT_FLOAT && radix == 8)
319 radix = 10;
320
321 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000322 {
323 if (radix == 2)
324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit);
325 else
326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
327 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000328
329 if (float_flag != NOT_FLOAT)
330 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000331 if (radix == 2)
332 {
333 cpp_error (pfile, CPP_DL_ERROR,
334 "invalid prefix \"0b\" for floating constant");
335 return CPP_N_INVALID;
336 }
337
Joseph Myersdadab4f2010-01-01 18:08:17 +0000338 if (radix == 16 && !seen_digit)
339 SYNTAX_ERROR ("no digits in hexadecimal floating constant");
340
Neil Boothcd7ab832002-05-29 17:15:42 +0000341 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
John David Anglin0527bc42003-11-01 22:56:54 +0000342 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000343 "use of C99 hexadecimal floating constant");
344
345 if (float_flag == AFTER_EXPON)
346 {
347 if (*str == '+' || *str == '-')
348 str++;
349
350 /* Exponent is decimal, even if string is a hex float. */
351 if (!ISDIGIT (*str))
352 SYNTAX_ERROR ("exponent has no digits");
353
354 do
355 str++;
356 while (ISDIGIT (*str));
357 }
358 else if (radix == 16)
359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
360
361 result = interpret_float_suffix (str, limit - str);
362 if (result == 0)
363 {
John David Anglin0527bc42003-11-01 22:56:54 +0000364 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000365 "invalid suffix \"%.*s\" on floating constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200366 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000367 return CPP_N_INVALID;
368 }
369
370 /* Traditional C didn't accept any floating suffixes. */
371 if (limit != str
372 && CPP_WTRADITIONAL (pfile)
373 && ! cpp_sys_macro_p (pfile))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000374 cpp_warning (pfile, CPP_W_TRADITIONAL,
375 "traditional C rejects the \"%.*s\" suffix",
376 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000377
Janis Johnson839a3b82009-04-01 17:31:26 +0000378 /* A suffix for double is a GCC extension via decimal float support.
379 If the suffix also specifies an imaginary value we'll catch that
380 later. */
381 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
382 cpp_error (pfile, CPP_DL_PEDWARN,
383 "suffix for double constant is a GCC extension");
384
Jon Grimmad6ed772005-12-06 23:13:15 +0000385 /* Radix must be 10 for decimal floats. */
386 if ((result & CPP_N_DFLOAT) && radix != 10)
387 {
388 cpp_error (pfile, CPP_DL_ERROR,
389 "invalid suffix \"%.*s\" with hexadecimal floating constant",
390 (int) (limit - str), str);
391 return CPP_N_INVALID;
392 }
393
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000394 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
395 cpp_error (pfile, CPP_DL_PEDWARN,
396 "fixed-point constants are a GCC extension");
397
Janis Johnson5a6bb572007-05-14 23:45:40 +0000398 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
399 cpp_error (pfile, CPP_DL_PEDWARN,
400 "decimal float constants are a GCC extension");
401
Neil Boothcd7ab832002-05-29 17:15:42 +0000402 result |= CPP_N_FLOATING;
403 }
404 else
405 {
406 result = interpret_int_suffix (str, limit - str);
407 if (result == 0)
408 {
John David Anglin0527bc42003-11-01 22:56:54 +0000409 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000410 "invalid suffix \"%.*s\" on integer constant",
Andreas Jaeger91b12472002-06-01 16:11:45 +0200411 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000412 return CPP_N_INVALID;
413 }
414
Zack Weinberg56da7202002-08-02 04:18:16 +0000415 /* Traditional C only accepted the 'L' suffix.
416 Suppress warning about 'LL' with -Wno-long-long. */
417 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
418 {
419 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
Simon Baldwin87cf0652010-04-07 17:18:10 +0000420 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
421 && CPP_OPTION (pfile, warn_long_long);
Zack Weinberg56da7202002-08-02 04:18:16 +0000422
Simon Baldwin87cf0652010-04-07 17:18:10 +0000423 if (u_or_i || large)
424 cpp_warning (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
425 "traditional C rejects the \"%.*s\" suffix",
426 (int) (limit - str), str);
Zack Weinberg56da7202002-08-02 04:18:16 +0000427 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000428
429 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
Neil Boothcd7ab832002-05-29 17:15:42 +0000430 && CPP_OPTION (pfile, warn_long_long))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000431 {
432 const char *message = CPP_OPTION (pfile, cplusplus)
433 ? N_("use of C++0x long long integer constant")
434 : N_("use of C99 long long integer constant");
435
436 if (CPP_OPTION (pfile, c99))
437 cpp_warning (pfile, CPP_W_LONG_LONG, message);
438 else
439 cpp_pedwarning (pfile, CPP_W_LONG_LONG, message);
440 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000441
442 result |= CPP_N_INTEGER;
443 }
444
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000445 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000446 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000447 cpp_error (pfile, CPP_DL_PEDWARN,
448 "imaginary constants are a GCC extension");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000449 if (radix == 2 && CPP_PEDANTIC (pfile))
450 cpp_error (pfile, CPP_DL_PEDWARN,
451 "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000452
453 if (radix == 10)
454 result |= CPP_N_DECIMAL;
455 else if (radix == 16)
456 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000457 else if (radix == 2)
458 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000459 else
460 result |= CPP_N_OCTAL;
461
462 return result;
463
464 syntax_error:
465 return CPP_N_INVALID;
466}
467
468/* cpp_interpret_integer converts an integer constant into a cpp_num,
469 of precision options->precision.
470
471 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000472 because the preprocessor doesn't need it and we don't want to
473 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000474cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000475cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
476 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000477{
478 const uchar *p, *end;
479 cpp_num result;
480
481 result.low = 0;
482 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000483 result.unsignedp = !!(type & CPP_N_UNSIGNED);
484 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000485
486 p = token->val.str.text;
487 end = p + token->val.str.len;
488
489 /* Common case of a single digit. */
490 if (token->val.str.len == 1)
491 result.low = p[0] - '0';
492 else
493 {
494 cpp_num_part max;
495 size_t precision = CPP_OPTION (pfile, precision);
496 unsigned int base = 10, c = 0;
497 bool overflow = false;
498
499 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
500 {
501 base = 8;
502 p++;
503 }
504 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
505 {
506 base = 16;
507 p += 2;
508 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000509 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
510 {
511 base = 2;
512 p += 2;
513 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000514
515 /* We can add a digit to numbers strictly less than this without
516 needing the precision and slowness of double integers. */
517 max = ~(cpp_num_part) 0;
518 if (precision < PART_PRECISION)
519 max >>= PART_PRECISION - precision;
520 max = (max - base + 1) / base + 1;
521
522 for (; p < end; p++)
523 {
524 c = *p;
525
526 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
527 c = hex_value (c);
528 else
529 break;
530
531 /* Strict inequality for when max is set to zero. */
532 if (result.low < max)
533 result.low = result.low * base + c;
534 else
535 {
536 result = append_digit (result, c, base, precision);
537 overflow |= result.overflow;
538 max = 0;
539 }
540 }
541
542 if (overflow)
John David Anglin0527bc42003-11-01 22:56:54 +0000543 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000544 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000545 /* If too big to be signed, consider it unsigned. Only warn for
546 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000547 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000548 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000549 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000550 && !(CPP_OPTION (pfile, traditional)
551 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000552 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000553 {
Joseph Myersf88d0772009-04-25 19:46:03 +0100554 /* This is for constants within the range of uintmax_t but
Joseph Myers813b9e72009-04-25 19:59:20 +0100555 not that of intmax_t. For such decimal constants, a
Joseph Myersf88d0772009-04-25 19:46:03 +0100556 diagnostic is required for C99 as the selected type must
557 be signed and not having a type is a constraint violation
558 (DR#298, TC3), so this must be a pedwarn. For C90,
559 unsigned long is specified to be used for a constant that
560 does not fit in signed long; if uintmax_t has the same
561 range as unsigned long this means only a warning is
562 appropriate here. C90 permits the preprocessor to use a
563 wider range than unsigned long in the compiler, so if
564 uintmax_t is wider than unsigned long no diagnostic is
565 required for such constants in preprocessor #if
566 expressions and the compiler will pedwarn for such
567 constants outside the range of unsigned long that reach
568 the compiler so a diagnostic is not required there
569 either; thus, pedwarn for C99 but use a plain warning for
570 C90. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000571 if (base == 10)
Joseph Myersf88d0772009-04-25 19:46:03 +0100572 cpp_error (pfile, (CPP_OPTION (pfile, c99)
573 ? CPP_DL_PEDWARN
574 : CPP_DL_WARNING),
Neil Boothcd7ab832002-05-29 17:15:42 +0000575 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000576 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000577 }
578 }
579
580 return result;
581}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000582
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000583/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000584static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000585append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000586{
587 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000588 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000589 bool overflow;
590 cpp_num_part add_high, add_low;
591
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000592 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000593 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000594 switch (base)
595 {
596 case 2:
597 shift = 1;
598 break;
599
600 case 16:
601 shift = 4;
602 break;
603
604 default:
605 shift = 3;
606 }
Neil Booth23ff0222002-07-17 17:27:14 +0000607 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000608 result.high = num.high << shift;
609 result.low = num.low << shift;
610 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400611 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000612
613 if (base == 10)
614 {
615 add_low = num.low << 1;
616 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
617 }
618 else
619 add_high = add_low = 0;
620
621 if (add_low + digit < add_low)
622 add_high++;
623 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000624
Neil Booth91318902002-05-26 18:42:21 +0000625 if (result.low + add_low < result.low)
626 add_high++;
627 if (result.high + add_high < result.high)
628 overflow = true;
629
630 result.low += add_low;
631 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400632 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000633
634 /* The above code catches overflow of a cpp_num type. This catches
635 overflow of the (possibly shorter) target precision. */
636 num.low = result.low;
637 num.high = result.high;
638 result = num_trim (result, precision);
639 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400640 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000641
Neil Booth91318902002-05-26 18:42:21 +0000642 return result;
643}
644
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000645/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000646static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000647parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000648{
Neil Booth91318902002-05-26 18:42:21 +0000649 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000650 int paren = 0;
651 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000652 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000653 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000654
Neil Booth93c803682000-10-28 17:59:06 +0000655 /* Don't expand macros. */
656 pfile->state.prevent_expansion++;
657
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000658 token = cpp_get_token (pfile);
659 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000660 {
661 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000662 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000663 }
664
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000665 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000666 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100667 node = token->val.node.node;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000668 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000669 {
John David Anglin0527bc42003-11-01 22:56:54 +0000670 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000671 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000672 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000673 }
Neil Booth93c803682000-10-28 17:59:06 +0000674 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000675 {
John David Anglin0527bc42003-11-01 22:56:54 +0000676 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000677 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000678 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000679 {
680 cpp_token op;
681
682 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000683 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000684 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000685 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000686 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000687 cpp_token_as_text (pfile, &op));
688 }
689 }
Neil Booth93c803682000-10-28 17:59:06 +0000690
Neil Booth91318902002-05-26 18:42:21 +0000691 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000692 {
Neil Booth335d03e2003-08-03 12:23:46 +0000693 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000694 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000695 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000696
Neil Bootha69cbaa2002-07-23 22:57:49 +0000697 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100698 if (!(node->flags & NODE_USED))
699 {
700 node->flags |= NODE_USED;
701 if (node->type == NT_MACRO)
702 {
703 if (pfile->cb.used_define)
704 pfile->cb.used_define (pfile, pfile->directive_line, node);
705 }
706 else
707 {
708 if (pfile->cb.used_undef)
709 pfile->cb.used_undef (pfile, pfile->directive_line, node);
710 }
711 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000712
Neil Booth6d18adb2001-07-29 17:27:57 +0000713 /* A possible controlling macro of the form #if !defined ().
714 _cpp_parse_expr checks there was no other junk on the line. */
715 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000716 }
717
718 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000719
Neil Booth23ff0222002-07-17 17:27:14 +0000720 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000721 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000722 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +0000723 result.low = node && node->type == NT_MACRO;
724 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000725}
726
Neil Booth60284a52002-04-28 23:14:56 +0000727/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
728 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000729 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000730static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000731eval_token (cpp_reader *pfile, const cpp_token *token)
Per Bothner7f2935c1995-03-16 13:59:07 -0800732{
Neil Booth91318902002-05-26 18:42:21 +0000733 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000734 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000735 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000736
Diego Novillo6de9cd92004-05-13 02:41:07 -0400737 result.unsignedp = false;
738 result.overflow = false;
739
Neil Booth93c803682000-10-28 17:59:06 +0000740 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +0000741 {
Per Bothner7f2935c1995-03-16 13:59:07 -0800742 case CPP_NUMBER:
Neil Boothcd7ab832002-05-29 17:15:42 +0000743 temp = cpp_classify_number (pfile, token);
744 switch (temp & CPP_N_CATEGORY)
745 {
746 case CPP_N_FLOATING:
John David Anglin0527bc42003-11-01 22:56:54 +0000747 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000748 "floating constant in preprocessor expression");
749 break;
750 case CPP_N_INTEGER:
751 if (!(temp & CPP_N_IMAGINARY))
752 return cpp_interpret_integer (pfile, token, temp);
John David Anglin0527bc42003-11-01 22:56:54 +0000753 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothcd7ab832002-05-29 17:15:42 +0000754 "imaginary number in preprocessor expression");
755 break;
756
757 case CPP_N_INVALID:
758 /* Error already issued. */
759 break;
760 }
761 result.high = result.low = 0;
762 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +0000763
Alexandre Oliva525bc952000-02-23 19:21:07 +0000764 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +0000765 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000766 case CPP_CHAR16:
767 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +0000768 {
Neil Booth91318902002-05-26 18:42:21 +0000769 cppchar_t cc = cpp_interpret_charconst (pfile, token,
770 &temp, &unsignedp);
771
772 result.high = 0;
773 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +0000774 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +0000775 if (!unsignedp && (cppchar_signed_t) cc < 0)
776 {
777 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
778 result.low |= ~(~(cpp_num_part) 0
779 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
780 result.high = ~(cpp_num_part) 0;
781 result = num_trim (result, CPP_OPTION (pfile, precision));
782 }
Neil Bootha5a49442002-05-06 22:53:10 +0000783 }
Neil Booth60284a52002-04-28 23:14:56 +0000784 break;
Zack Weinbergba412f12000-03-01 00:57:09 +0000785
Zack Weinberg92936ec2000-07-19 20:18:08 +0000786 case CPP_NAME:
Joseph Myers9a0c6182009-05-10 15:27:32 +0100787 if (token->val.node.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +0000788 return parse_defined (pfile);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000789 else if (CPP_OPTION (pfile, cplusplus)
Joseph Myers9a0c6182009-05-10 15:27:32 +0100790 && (token->val.node.node == pfile->spec_nodes.n_true
791 || token->val.node.node == pfile->spec_nodes.n_false))
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000792 {
Neil Booth91318902002-05-26 18:42:21 +0000793 result.high = 0;
Joseph Myers9a0c6182009-05-10 15:27:32 +0100794 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000795 }
796 else
797 {
Neil Booth91318902002-05-26 18:42:21 +0000798 result.high = 0;
799 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000800 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
Simon Baldwin87cf0652010-04-07 17:18:10 +0000801 cpp_warning (pfile, CPP_W_UNDEF, "\"%s\" is not defined",
802 NODE_NAME (token->val.node.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000803 }
Neil Booth60284a52002-04-28 23:14:56 +0000804 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +0000805
Tom Tromey899015a2008-05-13 14:50:27 +0000806 case CPP_HASH:
807 if (!pfile->state.skipping)
808 {
809 /* A pedantic warning takes precedence over a deprecated
810 warning here. */
811 if (CPP_PEDANTIC (pfile))
812 cpp_error (pfile, CPP_DL_PEDWARN,
813 "assertions are a GCC extension");
814 else if (CPP_OPTION (pfile, warn_deprecated))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000815 cpp_warning (pfile, CPP_W_DEPRECATED,
816 "assertions are a deprecated extension");
Tom Tromey899015a2008-05-13 14:50:27 +0000817 }
Neil Booth91318902002-05-26 18:42:21 +0000818 _cpp_test_assertion (pfile, &temp);
819 result.high = 0;
820 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +0000821 break;
822
823 default:
824 abort ();
Neil Booth93c803682000-10-28 17:59:06 +0000825 }
Per Bothner7f2935c1995-03-16 13:59:07 -0800826
Neil Booth23ff0222002-07-17 17:27:14 +0000827 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000828 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -0800829}
830
Neil Booth4063b942000-04-02 08:27:23 +0000831/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +0000832
833After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +0000834than the operator on the top of the stack, we reduce the stack by one
835operator and repeat the test. Since equal priorities do not reduce,
836this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +0000837
Neil Booth87ed1092002-04-28 19:42:54 +0000838We handle left-associative operators by decrementing the priority of
839just-lexed operators by one, but retaining the priority of operators
840already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +0000841
842The remaining cases are '(' and ')'. We handle '(' by skipping the
843reduction phase completely. ')' is given lower priority than
844everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +0000845parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +0000846reduce() exits immediately. If the normal exit route sees a ')', then
847there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +0000848
Neil Boothf8b954f2002-04-26 06:32:50 +0000849The parser assumes all shifted operators require a left operand unless
850the flag NO_L_OPERAND is set. These semantics are automatic; any
851extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -0800852
Neil Booth68e652752002-07-20 13:31:56 +0000853/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
854 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +0000855#define NO_L_OPERAND (1 << 0)
856#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +0000857#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +0000858
Zack Weinbergcf00a882000-07-08 02:33:00 +0000859/* Operator to priority map. Must be in the same order as the first
860 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +0000861static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +0000862{
Neil Booth60284a52002-04-28 23:14:56 +0000863 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +0000864 uchar flags;
865} optab[] =
866{
Neil Boothad28cff2002-07-18 22:08:35 +0000867 /* EQ */ {0, 0}, /* Shouldn't happen. */
868 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +0000869 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
870 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
871 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
872 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
873 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
874 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
875 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
876 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
877 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
878 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000879 /* RSHIFT */ {13, LEFT_ASSOC},
880 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +0000881
Neil Boothad28cff2002-07-18 22:08:35 +0000882 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +0000883 /* AND_AND */ {6, LEFT_ASSOC},
884 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +0000885 /* Note that QUERY, COLON, and COMMA must have the same precedence.
886 However, there are some special cases for these in reduce(). */
887 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +0000888 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +0000889 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +0000890 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +0000891 /* CLOSE_PAREN */ {0, 0},
892 /* EOF */ {0, 0},
893 /* EQ_EQ */ {11, LEFT_ASSOC},
894 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +0000895 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
896 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +0000897 /* UPLUS */ {16, NO_L_OPERAND},
898 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000899};
900
Per Bothner7f2935c1995-03-16 13:59:07 -0800901/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +0000902 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +0000903
904 The implementation is an operator precedence parser, i.e. a
905 bottom-up parser, using a stack for not-yet-reduced tokens.
906
907 The stack base is op_stack, and the current stack pointer is 'top'.
908 There is a stack element for each operator (only), and the most
909 recently pushed operator is 'top->op'. An operand (value) is
910 stored in the 'value' field of the stack element of the operator
911 that precedes it. */
912bool
Tom Tromeyd7508872008-05-30 14:25:09 +0000913_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -0800914{
Neil Booth87ed1092002-04-28 19:42:54 +0000915 struct op *top = pfile->op_stack;
916 unsigned int lex_count;
917 bool saw_leading_not, want_value = true;
Per Bothner7f2935c1995-03-16 13:59:07 -0800918
Neil Booth87ed1092002-04-28 19:42:54 +0000919 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -0800920
Neil Booth93c803682000-10-28 17:59:06 +0000921 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +0000922 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +0000923 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +0000924 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000925
Neil Booth87ed1092002-04-28 19:42:54 +0000926 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +0000927 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +0000928
Per Bothner7f2935c1995-03-16 13:59:07 -0800929 for (;;)
930 {
Zack Weinbergcf00a882000-07-08 02:33:00 +0000931 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -0800932
Neil Booth6d18adb2001-07-29 17:27:57 +0000933 lex_count++;
Neil Booth68e652752002-07-20 13:31:56 +0000934 op.token = cpp_get_token (pfile);
935 op.op = op.token->type;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +0000936 op.loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -0800937
Per Bothner7f2935c1995-03-16 13:59:07 -0800938 switch (op.op)
939 {
Neil Booth60284a52002-04-28 23:14:56 +0000940 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +0000941 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +0000942 case CPP_CHAR:
943 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +0000944 case CPP_CHAR16:
945 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +0000946 case CPP_NAME:
947 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +0000948 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000949 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000950 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000951 want_value = false;
Neil Booth68e652752002-07-20 13:31:56 +0000952 top->value = eval_token (pfile, op.token);
Neil Booth9ee703132000-04-01 07:42:37 +0000953 continue;
954
Neil Booth6d18adb2001-07-29 17:27:57 +0000955 case CPP_NOT:
956 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +0000957 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000958 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +0000959 if (want_value)
960 op.op = CPP_UPLUS;
961 break;
962 case CPP_MINUS:
963 if (want_value)
964 op.op = CPP_UMINUS;
965 break;
Neil Booth60284a52002-04-28 23:14:56 +0000966
Neil Boothf8b954f2002-04-26 06:32:50 +0000967 default:
Neil Booth60284a52002-04-28 23:14:56 +0000968 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Neil Boothcd7ab832002-05-29 17:15:42 +0000969 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
Neil Booth68e652752002-07-20 13:31:56 +0000970 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +0000971 break;
Per Bothner7f2935c1995-03-16 13:59:07 -0800972 }
973
Neil Booth87ed1092002-04-28 19:42:54 +0000974 /* Check we have a value or operator as appropriate. */
975 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +0000976 {
Neil Boothf8b954f2002-04-26 06:32:50 +0000977 if (!want_value)
Neil Booth60284a52002-04-28 23:14:56 +0000978 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
Neil Booth68e652752002-07-20 13:31:56 +0000979 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +0000980 }
Neil Booth87ed1092002-04-28 19:42:54 +0000981 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +0000982 {
Neil Bootha09d4742004-07-04 12:57:50 +0000983 /* We want a number (or expression) and haven't got one.
984 Try to emit a specific diagnostic. */
985 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
986 SYNTAX_ERROR ("missing expression between '(' and ')'");
987
988 if (op.op == CPP_EOF && top->op == CPP_EOF)
Tom Tromeyd7508872008-05-30 14:25:09 +0000989 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +0000990
991 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
992 SYNTAX_ERROR2 ("operator '%s' has no right operand",
993 cpp_token_as_text (pfile, top->token));
994 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
995 /* Complain about missing paren during reduction. */;
996 else
997 SYNTAX_ERROR2 ("operator '%s' has no left operand",
998 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +0000999 }
Neil Booth87ed1092002-04-28 19:42:54 +00001000
1001 top = reduce (pfile, top, op.op);
1002 if (!top)
1003 goto syntax_error;
1004
Neil Booth60284a52002-04-28 23:14:56 +00001005 if (op.op == CPP_EOF)
1006 break;
1007
Neil Booth87ed1092002-04-28 19:42:54 +00001008 switch (op.op)
1009 {
1010 case CPP_CLOSE_PAREN:
1011 continue;
Neil Booth87ed1092002-04-28 19:42:54 +00001012 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +00001013 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001014 pfile->state.skip_eval++;
1015 break;
1016 case CPP_AND_AND:
1017 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +00001018 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001019 pfile->state.skip_eval++;
1020 break;
1021 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +00001022 if (top->op != CPP_QUERY)
1023 SYNTAX_ERROR (" ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +00001024 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +00001025 pfile->state.skip_eval++;
1026 else
1027 pfile->state.skip_eval--;
1028 default:
1029 break;
1030 }
1031
Neil Boothf8b954f2002-04-26 06:32:50 +00001032 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +00001033
Mike Stump0f413021996-07-03 22:07:53 +00001034 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +00001035 if (++top == pfile->op_limit)
1036 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +00001037
Per Bothner7f2935c1995-03-16 13:59:07 -08001038 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +00001039 top->token = op.token;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001040 top->loc = op.token->src_loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001041 }
Neil Booth9ee703132000-04-01 07:42:37 +00001042
Neil Booth6d18adb2001-07-29 17:27:57 +00001043 /* The controlling macro expression is only valid if we called lex 3
1044 times: <!> <defined expression> and <EOF>. push_conditional ()
1045 checks that we are at top-of-file. */
1046 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1047 pfile->mi_ind_cmacro = 0;
1048
Neil Booth87ed1092002-04-28 19:42:54 +00001049 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +00001050 {
Tom Tromeyd7508872008-05-30 14:25:09 +00001051 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s",
1052 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +00001053 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +00001054 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +00001055 }
Neil Booth9ee703132000-04-01 07:42:37 +00001056
Neil Booth91318902002-05-26 18:42:21 +00001057 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001058}
1059
1060/* Reduce the operator / value stack if possible, in preparation for
1061 pushing operator OP. Returns NULL on error, otherwise the top of
1062 the stack. */
1063static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001064reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001065{
1066 unsigned int prio;
1067
Neil Booth91318902002-05-26 18:42:21 +00001068 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1069 {
1070 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001071 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001072 return 0;
1073 }
1074
Neil Booth87ed1092002-04-28 19:42:54 +00001075 if (op == CPP_OPEN_PAREN)
1076 return top;
1077
1078 /* Decrement the priority of left-associative operators to force a
1079 reduction with operators of otherwise equal priority. */
1080 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1081 while (prio < optab[top->op].prio)
1082 {
Neil Booth68e652752002-07-20 13:31:56 +00001083 if (CPP_OPTION (pfile, warn_num_sign_change)
1084 && optab[top->op].flags & CHECK_PROMOTION)
1085 check_promotion (pfile, top);
1086
Neil Booth75aef482002-07-19 19:24:43 +00001087 switch (top->op)
1088 {
1089 case CPP_UPLUS:
1090 case CPP_UMINUS:
1091 case CPP_NOT:
1092 case CPP_COMPL:
1093 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001094 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001095 break;
Neil Booth91318902002-05-26 18:42:21 +00001096
Neil Booth75aef482002-07-19 19:24:43 +00001097 case CPP_PLUS:
1098 case CPP_MINUS:
1099 case CPP_RSHIFT:
1100 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001101 case CPP_COMMA:
1102 top[-1].value = num_binary_op (pfile, top[-1].value,
1103 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001104 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001105 break;
Neil Booth91318902002-05-26 18:42:21 +00001106
Neil Booth75aef482002-07-19 19:24:43 +00001107 case CPP_GREATER:
1108 case CPP_LESS:
1109 case CPP_GREATER_EQ:
1110 case CPP_LESS_EQ:
1111 top[-1].value
1112 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001113 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001114 break;
Neil Booth91318902002-05-26 18:42:21 +00001115
Neil Booth75aef482002-07-19 19:24:43 +00001116 case CPP_EQ_EQ:
1117 case CPP_NOT_EQ:
1118 top[-1].value
1119 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001120 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001121 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001122
Neil Booth75aef482002-07-19 19:24:43 +00001123 case CPP_AND:
1124 case CPP_OR:
1125 case CPP_XOR:
1126 top[-1].value
1127 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001128 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001129 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001130
Neil Booth75aef482002-07-19 19:24:43 +00001131 case CPP_MULT:
1132 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001133 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001134 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001135
Neil Booth75aef482002-07-19 19:24:43 +00001136 case CPP_DIV:
1137 case CPP_MOD:
1138 top[-1].value = num_div_op (pfile, top[-1].value,
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001139 top->value, top->op, top->loc);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001140 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001141 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001142
Neil Booth75aef482002-07-19 19:24:43 +00001143 case CPP_OR_OR:
1144 top--;
1145 if (!num_zerop (top->value))
1146 pfile->state.skip_eval--;
1147 top->value.low = (!num_zerop (top->value)
1148 || !num_zerop (top[1].value));
1149 top->value.high = 0;
1150 top->value.unsignedp = false;
1151 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001152 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001153 continue;
1154
1155 case CPP_AND_AND:
1156 top--;
1157 if (num_zerop (top->value))
1158 pfile->state.skip_eval--;
1159 top->value.low = (!num_zerop (top->value)
1160 && !num_zerop (top[1].value));
1161 top->value.high = 0;
1162 top->value.unsignedp = false;
1163 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001164 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001165 continue;
1166
1167 case CPP_OPEN_PAREN:
1168 if (op != CPP_CLOSE_PAREN)
1169 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001170 cpp_error_with_line (pfile, CPP_DL_ERROR,
1171 top->token->src_loc,
1172 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001173 return 0;
1174 }
1175 top--;
1176 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001177 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001178 return top;
1179
1180 case CPP_COLON:
1181 top -= 2;
1182 if (!num_zerop (top->value))
1183 {
Neil Booth91318902002-05-26 18:42:21 +00001184 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001185 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001186 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001187 }
1188 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001189 {
1190 top->value = top[2].value;
1191 top->loc = top[2].loc;
1192 }
Neil Booth75aef482002-07-19 19:24:43 +00001193 top->value.unsignedp = (top[1].value.unsignedp
1194 || top[2].value.unsignedp);
1195 continue;
Neil Booth91318902002-05-26 18:42:21 +00001196
Neil Booth75aef482002-07-19 19:24:43 +00001197 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001198 /* COMMA and COLON should not reduce a QUERY operator. */
1199 if (op == CPP_COMMA || op == CPP_COLON)
1200 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001201 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001202 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001203
Neil Booth75aef482002-07-19 19:24:43 +00001204 default:
1205 goto bad_op;
1206 }
Neil Boothad28cff2002-07-18 22:08:35 +00001207
1208 top--;
Neil Booth91318902002-05-26 18:42:21 +00001209 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001210 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001211 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001212 }
1213
1214 if (op == CPP_CLOSE_PAREN)
1215 {
John David Anglin0527bc42003-11-01 22:56:54 +00001216 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001217 return 0;
1218 }
1219
1220 return top;
1221}
1222
1223/* Returns the position of the old top of stack after expansion. */
1224struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001225_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001226{
Neil Booth32fa4562002-05-09 22:27:31 +00001227 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1228 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001229
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001230 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001231 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001232
Neil Booth32fa4562002-05-09 22:27:31 +00001233 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001234}
Neil Booth91318902002-05-26 18:42:21 +00001235
Neil Booth68e652752002-07-20 13:31:56 +00001236/* Emits a warning if the effective sign of either operand of OP
1237 changes because of integer promotions. */
1238static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001239check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001240{
1241 if (op->value.unsignedp == op[-1].value.unsignedp)
1242 return;
1243
1244 if (op->value.unsignedp)
1245 {
1246 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001247 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1248 "the left operand of \"%s\" changes sign when promoted",
1249 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001250 }
1251 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001252 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001253 "the right operand of \"%s\" changes sign when promoted",
1254 cpp_token_as_text (pfile, op->token));
1255}
1256
Neil Booth91318902002-05-26 18:42:21 +00001257/* Clears the unused high order bits of the number pointed to by PNUM. */
1258static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001259num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001260{
1261 if (precision > PART_PRECISION)
1262 {
1263 precision -= PART_PRECISION;
1264 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001265 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001266 }
1267 else
1268 {
1269 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001270 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001271 num.high = 0;
1272 }
1273
1274 return num;
1275}
1276
1277/* True iff A (presumed signed) >= 0. */
1278static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001279num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001280{
1281 if (precision > PART_PRECISION)
1282 {
1283 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001284 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001285 }
1286
Neil Booth359b0be2002-05-28 05:44:06 +00001287 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001288}
1289
Neil Boothceeedfc2002-06-02 19:37:34 +00001290/* Sign extend a number, with PRECISION significant bits and all
1291 others assumed clear, to fill out a cpp_num structure. */
1292cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001293cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001294{
1295 if (!num.unsignedp)
1296 {
1297 if (precision > PART_PRECISION)
1298 {
1299 precision -= PART_PRECISION;
1300 if (precision < PART_PRECISION
1301 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1302 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1303 }
1304 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1305 {
1306 if (precision < PART_PRECISION)
1307 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1308 num.high = ~(cpp_num_part) 0;
1309 }
1310 }
1311
1312 return num;
1313}
1314
Neil Booth91318902002-05-26 18:42:21 +00001315/* Returns the negative of NUM. */
1316static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001317num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001318{
1319 cpp_num copy;
1320
1321 copy = num;
1322 num.high = ~num.high;
1323 num.low = ~num.low;
1324 if (++num.low == 0)
1325 num.high++;
1326 num = num_trim (num, precision);
1327 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1328
1329 return num;
1330}
1331
1332/* Returns true if A >= B. */
1333static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001334num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001335{
1336 bool unsignedp;
1337
1338 unsignedp = pa.unsignedp || pb.unsignedp;
1339
1340 if (!unsignedp)
1341 {
1342 /* Both numbers have signed type. If they are of different
1343 sign, the answer is the sign of A. */
1344 unsignedp = num_positive (pa, precision);
1345
1346 if (unsignedp != num_positive (pb, precision))
1347 return unsignedp;
1348
1349 /* Otherwise we can do an unsigned comparison. */
1350 }
1351
1352 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1353}
1354
1355/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1356static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001357num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1358 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001359{
1360 lhs.overflow = false;
1361 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1362
1363 /* As excess precision is zeroed, there is no need to num_trim () as
1364 these operations cannot introduce a set bit there. */
1365 if (op == CPP_AND)
1366 {
1367 lhs.low &= rhs.low;
1368 lhs.high &= rhs.high;
1369 }
1370 else if (op == CPP_OR)
1371 {
1372 lhs.low |= rhs.low;
1373 lhs.high |= rhs.high;
1374 }
1375 else
1376 {
1377 lhs.low ^= rhs.low;
1378 lhs.high ^= rhs.high;
1379 }
1380
1381 return lhs;
1382}
1383
1384/* Returns LHS OP RHS, where OP is an inequality. */
1385static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001386num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1387 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001388{
1389 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1390
1391 if (op == CPP_GREATER_EQ)
1392 lhs.low = gte;
1393 else if (op == CPP_LESS)
1394 lhs.low = !gte;
1395 else if (op == CPP_GREATER)
1396 lhs.low = gte && !num_eq (lhs, rhs);
1397 else /* CPP_LESS_EQ. */
1398 lhs.low = !gte || num_eq (lhs, rhs);
1399
1400 lhs.high = 0;
1401 lhs.overflow = false;
1402 lhs.unsignedp = false;
1403 return lhs;
1404}
1405
1406/* Returns LHS OP RHS, where OP is == or !=. */
1407static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001408num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1409 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001410{
Jason Merrill97459792002-06-07 09:29:17 -04001411 /* Work around a 3.0.4 bug; see PR 6950. */
1412 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001413 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001414 eq = !eq;
1415 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001416 lhs.high = 0;
1417 lhs.overflow = false;
1418 lhs.unsignedp = false;
1419 return lhs;
1420}
1421
1422/* Shift NUM, of width PRECISION, right by N bits. */
1423static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001424num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001425{
1426 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001427 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001428
Diego Novillo6de9cd92004-05-13 02:41:07 -04001429 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001430 sign_mask = 0;
1431 else
1432 sign_mask = ~(cpp_num_part) 0;
1433
1434 if (n >= precision)
1435 num.high = num.low = sign_mask;
1436 else
1437 {
1438 /* Sign-extend. */
1439 if (precision < PART_PRECISION)
1440 num.high = sign_mask, num.low |= sign_mask << precision;
1441 else if (precision < 2 * PART_PRECISION)
1442 num.high |= sign_mask << (precision - PART_PRECISION);
1443
1444 if (n >= PART_PRECISION)
1445 {
1446 n -= PART_PRECISION;
1447 num.low = num.high;
1448 num.high = sign_mask;
1449 }
1450
1451 if (n)
1452 {
1453 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1454 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1455 }
1456 }
1457
1458 num = num_trim (num, precision);
1459 num.overflow = false;
1460 return num;
1461}
1462
1463/* Shift NUM, of width PRECISION, left by N bits. */
1464static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001465num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001466{
1467 if (n >= precision)
1468 {
1469 num.overflow = !num.unsignedp && !num_zerop (num);
1470 num.high = num.low = 0;
1471 }
1472 else
1473 {
1474 cpp_num orig, maybe_orig;
1475 size_t m = n;
1476
1477 orig = num;
1478 if (m >= PART_PRECISION)
1479 {
1480 m -= PART_PRECISION;
1481 num.high = num.low;
1482 num.low = 0;
1483 }
1484 if (m)
1485 {
1486 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1487 num.low <<= m;
1488 }
1489 num = num_trim (num, precision);
1490
1491 if (num.unsignedp)
1492 num.overflow = false;
1493 else
1494 {
1495 maybe_orig = num_rshift (num, precision, n);
1496 num.overflow = !num_eq (orig, maybe_orig);
1497 }
1498 }
1499
1500 return num;
1501}
1502
1503/* The four unary operators: +, -, ! and ~. */
1504static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001505num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001506{
1507 switch (op)
1508 {
1509 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001510 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001511 cpp_warning (pfile, CPP_W_TRADITIONAL,
1512 "traditional C rejects the unary plus operator");
Neil Booth91318902002-05-26 18:42:21 +00001513 num.overflow = false;
1514 break;
1515
1516 case CPP_UMINUS:
1517 num = num_negate (num, CPP_OPTION (pfile, precision));
1518 break;
1519
1520 case CPP_COMPL:
1521 num.high = ~num.high;
1522 num.low = ~num.low;
1523 num = num_trim (num, CPP_OPTION (pfile, precision));
1524 num.overflow = false;
1525 break;
1526
1527 default: /* case CPP_NOT: */
1528 num.low = num_zerop (num);
1529 num.high = 0;
1530 num.overflow = false;
1531 num.unsignedp = false;
1532 break;
1533 }
1534
1535 return num;
1536}
1537
1538/* The various binary operators. */
1539static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001540num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001541{
1542 cpp_num result;
1543 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001544 size_t n;
1545
1546 switch (op)
1547 {
1548 /* Shifts. */
1549 case CPP_LSHIFT:
1550 case CPP_RSHIFT:
1551 if (!rhs.unsignedp && !num_positive (rhs, precision))
1552 {
1553 /* A negative shift is a positive shift the other way. */
1554 if (op == CPP_LSHIFT)
1555 op = CPP_RSHIFT;
1556 else
1557 op = CPP_LSHIFT;
1558 rhs = num_negate (rhs, precision);
1559 }
1560 if (rhs.high)
1561 n = ~0; /* Maximal. */
1562 else
1563 n = rhs.low;
1564 if (op == CPP_LSHIFT)
1565 lhs = num_lshift (lhs, precision, n);
1566 else
1567 lhs = num_rshift (lhs, precision, n);
1568 break;
1569
Neil Booth91318902002-05-26 18:42:21 +00001570 /* Arithmetic. */
1571 case CPP_MINUS:
1572 rhs = num_negate (rhs, precision);
1573 case CPP_PLUS:
1574 result.low = lhs.low + rhs.low;
1575 result.high = lhs.high + rhs.high;
1576 if (result.low < lhs.low)
1577 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001578 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1579 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001580
1581 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001582 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001583 {
1584 bool lhsp = num_positive (lhs, precision);
1585 result.overflow = (lhsp == num_positive (rhs, precision)
1586 && lhsp != num_positive (result, precision));
1587 }
1588 return result;
1589
1590 /* Comma. */
1591 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001592 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1593 || !pfile->state.skip_eval))
John David Anglin0527bc42003-11-01 22:56:54 +00001594 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001595 "comma operator in operand of #if");
1596 lhs = rhs;
1597 break;
1598 }
1599
1600 return lhs;
1601}
1602
1603/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1604 cannot overflow. */
1605static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001606num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001607{
1608 cpp_num result;
1609 cpp_num_part middle[2], temp;
1610
1611 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1612 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1613
1614 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1615 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1616
1617 temp = result.low;
1618 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1619 if (result.low < temp)
1620 result.high++;
1621
1622 temp = result.low;
1623 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1624 if (result.low < temp)
1625 result.high++;
1626
1627 result.high += HIGH_PART (middle[0]);
1628 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001629 result.unsignedp = true;
1630 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001631
1632 return result;
1633}
1634
1635/* Multiply two preprocessing numbers. */
1636static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001637num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001638{
1639 cpp_num result, temp;
1640 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1641 bool overflow, negate = false;
1642 size_t precision = CPP_OPTION (pfile, precision);
1643
1644 /* Prepare for unsigned multiplication. */
1645 if (!unsignedp)
1646 {
1647 if (!num_positive (lhs, precision))
1648 negate = !negate, lhs = num_negate (lhs, precision);
1649 if (!num_positive (rhs, precision))
1650 negate = !negate, rhs = num_negate (rhs, precision);
1651 }
1652
1653 overflow = lhs.high && rhs.high;
1654 result = num_part_mul (lhs.low, rhs.low);
1655
1656 temp = num_part_mul (lhs.high, rhs.low);
1657 result.high += temp.low;
1658 if (temp.high)
1659 overflow = true;
1660
1661 temp = num_part_mul (lhs.low, rhs.high);
1662 result.high += temp.low;
1663 if (temp.high)
1664 overflow = true;
1665
1666 temp.low = result.low, temp.high = result.high;
1667 result = num_trim (result, precision);
1668 if (!num_eq (result, temp))
1669 overflow = true;
1670
1671 if (negate)
1672 result = num_negate (result, precision);
1673
1674 if (unsignedp)
1675 result.overflow = false;
1676 else
1677 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1678 && !num_zerop (result));
1679 result.unsignedp = unsignedp;
1680
1681 return result;
1682}
1683
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001684/* Divide two preprocessing numbers, LHS and RHS, returning the answer
1685 or the remainder depending upon OP. LOCATION is the source location
1686 of this operator (for diagnostics). */
1687
Neil Booth91318902002-05-26 18:42:21 +00001688static cpp_num
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001689num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1690 source_location location)
Neil Booth91318902002-05-26 18:42:21 +00001691{
1692 cpp_num result, sub;
1693 cpp_num_part mask;
1694 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1695 bool negate = false, lhs_neg = false;
1696 size_t i, precision = CPP_OPTION (pfile, precision);
1697
1698 /* Prepare for unsigned division. */
1699 if (!unsignedp)
1700 {
1701 if (!num_positive (lhs, precision))
1702 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1703 if (!num_positive (rhs, precision))
1704 negate = !negate, rhs = num_negate (rhs, precision);
1705 }
1706
1707 /* Find the high bit. */
1708 if (rhs.high)
1709 {
1710 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001711 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001712 for (; ; i--, mask >>= 1)
1713 if (rhs.high & mask)
1714 break;
1715 }
1716 else if (rhs.low)
1717 {
1718 if (precision > PART_PRECISION)
1719 i = precision - PART_PRECISION - 1;
1720 else
1721 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00001722 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001723 for (; ; i--, mask >>= 1)
1724 if (rhs.low & mask)
1725 break;
1726 }
1727 else
1728 {
Neil Booth75aef482002-07-19 19:24:43 +00001729 if (!pfile->state.skip_eval)
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001730 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
1731 "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00001732 return lhs;
1733 }
1734
Kazu Hiratada7d8302002-09-22 02:03:17 +00001735 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00001736 shifting the RHS fully left, and subtracting from LHS if LHS is
1737 at least as big, and then repeating but with one less shift.
1738 This is not very efficient, but is easy to understand. */
1739
1740 rhs.unsignedp = true;
1741 lhs.unsignedp = true;
1742 i = precision - i - 1;
1743 sub = num_lshift (rhs, precision, i);
1744
1745 result.high = result.low = 0;
1746 for (;;)
1747 {
1748 if (num_greater_eq (lhs, sub, precision))
1749 {
1750 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1751 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001752 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00001753 else
Neil Booth359b0be2002-05-28 05:44:06 +00001754 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00001755 }
1756 if (i-- == 0)
1757 break;
1758 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1759 sub.high >>= 1;
1760 }
1761
1762 /* We divide so that the remainder has the sign of the LHS. */
1763 if (op == CPP_DIV)
1764 {
1765 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001766 result.overflow = false;
1767 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001768 {
1769 if (negate)
1770 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00001771 result.overflow = (num_positive (result, precision) ^ !negate
1772 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00001773 }
1774
1775 return result;
1776 }
1777
1778 /* CPP_MOD. */
1779 lhs.unsignedp = unsignedp;
1780 lhs.overflow = false;
1781 if (lhs_neg)
1782 lhs = num_negate (lhs, precision);
1783
1784 return lhs;
1785}