blob: c24b640ba16a2bc416213002ee567cad688e40fe [file] [log] [blame]
Zack Weinbergb0699da2000-03-07 20:58:47 +00001/* Parse C expressions for cpplib.
Richard Sandiford35c3d612014-01-02 22:24:45 +00002 Copyright (C) 1987-2014 Free Software Foundation, Inc.
Richard Kennere38992e2000-04-18 20:42:00 +00003 Contributed by Per Bothner, 1994.
Per Bothner7f2935c1995-03-16 13:59:07 -08004
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
Jakub Jelinek748086b2009-04-09 17:00:19 +02007Free Software Foundation; either version 3, or (at your option) any
Per Bothner7f2935c1995-03-16 13:59:07 -08008later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
Jakub Jelinek748086b2009-04-09 17:00:19 +020016along with this program; see the file COPYING3. If not see
17<http://www.gnu.org/licenses/>. */
Per Bothner7f2935c1995-03-16 13:59:07 -080018
Per Bothner7f2935c1995-03-16 13:59:07 -080019#include "config.h"
Kaveh R. Ghazib04cd5071998-03-30 12:05:54 +000020#include "system.h"
Kaveh R. Ghazi487a6e01998-05-19 08:42:48 +000021#include "cpplib.h"
Paolo Bonzini4f4e53dd2004-05-24 10:50:45 +000022#include "internal.h"
Per Bothner7f2935c1995-03-16 13:59:07 -080023
Neil Booth91318902002-05-26 18:42:21 +000024#define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
25#define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
26#define LOW_PART(num_part) (num_part & HALF_MASK)
27#define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
28
Zack Weinbergcf00a882000-07-08 02:33:00 +000029struct op
Zack Weinbergb0699da2000-03-07 20:58:47 +000030{
Neil Booth68e652752002-07-20 13:31:56 +000031 const cpp_token *token; /* The token forming op (for diagnostics). */
Neil Boothad28cff2002-07-18 22:08:35 +000032 cpp_num value; /* The value logically "right" of op. */
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +000033 source_location loc; /* The location of this value. */
Zack Weinbergcf00a882000-07-08 02:33:00 +000034 enum cpp_ttype op;
Per Bothner7f2935c1995-03-16 13:59:07 -080035};
Per Bothner7f2935c1995-03-16 13:59:07 -080036
Neil Booth91318902002-05-26 18:42:21 +000037/* Some simple utility routines on double integers. */
38#define num_zerop(num) ((num.low | num.high) == 0)
39#define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000040static bool num_positive (cpp_num, size_t);
41static bool num_greater_eq (cpp_num, cpp_num, size_t);
42static cpp_num num_trim (cpp_num, size_t);
43static cpp_num num_part_mul (cpp_num_part, cpp_num_part);
Neil Booth91318902002-05-26 18:42:21 +000044
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000045static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype);
46static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
47static cpp_num num_negate (cpp_num, size_t);
48static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype);
49static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype);
51static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype);
53static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num);
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +000054static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype,
55 source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000056static 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 *);
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +000061static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000062static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype);
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +000063static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t);
64static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t);
Zack Weinberg6cf87ca2003-06-17 06:17:44 +000065static void check_promotion (cpp_reader *, const struct op *);
Neil Booth91318902002-05-26 18:42:21 +000066
Edward Smith-Rowlanda15f7cb2014-10-01 11:49:23 +000067static cpp_num parse_has_include (cpp_reader *, enum include_type);
68
Neil Boothcd7ab832002-05-29 17:15:42 +000069/* Token type abuse to create unary plus and minus operators. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +000070#define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
71#define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
Zack Weinbergcf00a882000-07-08 02:33:00 +000072
Zack Weinberg15dad1d2000-05-18 15:55:46 +000073/* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
Zack Weinberg15dad1d2000-05-18 15:55:46 +000075#define SYNTAX_ERROR(msgid) \
John David Anglin0527bc42003-11-01 22:56:54 +000076 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000077#define SYNTAX_ERROR2(msgid, arg) \
John David Anglin0527bc42003-11-01 22:56:54 +000078 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
79 while(0)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +000080#define SYNTAX_ERROR_AT(loc, msgid) \
81 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \
82 while(0)
83#define SYNTAX_ERROR2_AT(loc, msgid, arg) \
84 do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \
85 while(0)
Zack Weinberg15dad1d2000-05-18 15:55:46 +000086
Neil Boothcd7ab832002-05-29 17:15:42 +000087/* Subroutine of cpp_classify_number. S points to a float suffix of
88 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
89 flag vector describing the suffix. */
90static unsigned int
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +000091interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len)
Per Bothner7f2935c1995-03-16 13:59:07 -080092{
Janis Johnsoneec49112009-04-01 17:04:42 +000093 size_t flags;
Janis Johnson839a3b82009-04-01 17:31:26 +000094 size_t f, d, l, w, q, i;
Uros Bizjakc77cd3d2007-07-03 07:53:58 +020095
Janis Johnsoneec49112009-04-01 17:04:42 +000096 flags = 0;
Janis Johnson839a3b82009-04-01 17:31:26 +000097 f = d = l = w = q = i = 0;
Zack Weinbergcf00a882000-07-08 02:33:00 +000098
Janis Johnsoneec49112009-04-01 17:04:42 +000099 /* Process decimal float suffixes, which are two letters starting
100 with d or D. Order and case are significant. */
101 if (len == 2 && (*s == 'd' || *s == 'D'))
102 {
103 bool uppercase = (*s == 'D');
104 switch (s[1])
105 {
106 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break;
107 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break;
108 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break;
109 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break;
110 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
111 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break;
112 default:
Janis Johnson839a3b82009-04-01 17:31:26 +0000113 /* Additional two-character suffixes beginning with D are not
114 for decimal float constants. */
115 break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000116 }
117 }
118
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000119 if (CPP_OPTION (pfile, ext_numeric_literals))
Janis Johnsoneec49112009-04-01 17:04:42 +0000120 {
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000121 /* Recognize a fixed-point suffix. */
122 if (len != 0)
123 switch (s[len-1])
124 {
125 case 'k': case 'K': flags = CPP_N_ACCUM; break;
126 case 'r': case 'R': flags = CPP_N_FRACT; break;
127 default: break;
128 }
Janis Johnsoneec49112009-04-01 17:04:42 +0000129
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000130 /* Continue processing a fixed-point suffix. The suffix is case
131 insensitive except for ll or LL. Order is significant. */
132 if (flags)
Janis Johnsoneec49112009-04-01 17:04:42 +0000133 {
Janis Johnsoneec49112009-04-01 17:04:42 +0000134 if (len == 1)
135 return flags;
136 len--;
Janis Johnsoneec49112009-04-01 17:04:42 +0000137
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000138 if (*s == 'u' || *s == 'U')
139 {
140 flags |= CPP_N_UNSIGNED;
141 if (len == 1)
142 return flags;
143 len--;
144 s++;
145 }
146
147 switch (*s)
148 {
149 case 'h': case 'H':
150 if (len == 1)
151 return flags |= CPP_N_SMALL;
152 break;
153 case 'l':
154 if (len == 1)
155 return flags |= CPP_N_MEDIUM;
156 if (len == 2 && s[1] == 'l')
157 return flags |= CPP_N_LARGE;
158 break;
159 case 'L':
160 if (len == 1)
161 return flags |= CPP_N_MEDIUM;
162 if (len == 2 && s[1] == 'L')
163 return flags |= CPP_N_LARGE;
164 break;
165 default:
166 break;
167 }
168 /* Anything left at this point is invalid. */
169 return 0;
170 }
Janis Johnsoneec49112009-04-01 17:04:42 +0000171 }
172
173 /* In any remaining valid suffix, the case and order don't matter. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000174 while (len--)
175 switch (s[len])
176 {
Janis Johnsoneec49112009-04-01 17:04:42 +0000177 case 'f': case 'F': f++; break;
Janis Johnson839a3b82009-04-01 17:31:26 +0000178 case 'd': case 'D': d++; break;
Janis Johnsoneec49112009-04-01 17:04:42 +0000179 case 'l': case 'L': l++; break;
180 case 'w': case 'W': w++; break;
181 case 'q': case 'Q': q++; break;
Neil Boothcd7ab832002-05-29 17:15:42 +0000182 case 'i': case 'I':
183 case 'j': case 'J': i++; break;
184 default:
185 return 0;
186 }
Zack Weinbergcf00a882000-07-08 02:33:00 +0000187
Janis Johnson839a3b82009-04-01 17:31:26 +0000188 if (f + d + l + w + q > 1 || i > 1)
Jon Grimmad6ed772005-12-06 23:13:15 +0000189 return 0;
190
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000191 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
192 return 0;
193
194 if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals))
195 return 0;
196
Neil Boothcd7ab832002-05-29 17:15:42 +0000197 return ((i ? CPP_N_IMAGINARY : 0)
198 | (f ? CPP_N_SMALL :
Janis Johnson839a3b82009-04-01 17:31:26 +0000199 d ? CPP_N_MEDIUM :
Uros Bizjakc77cd3d2007-07-03 07:53:58 +0200200 l ? CPP_N_LARGE :
201 w ? CPP_N_MD_W :
Janis Johnson839a3b82009-04-01 17:31:26 +0000202 q ? CPP_N_MD_Q : CPP_N_DEFAULT));
Neil Boothcd7ab832002-05-29 17:15:42 +0000203}
204
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000205/* Return the classification flags for a float suffix. */
206unsigned int
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000207cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len)
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000208{
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000209 return interpret_float_suffix (pfile, (const unsigned char *)s, len);
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000210}
211
Neil Boothcd7ab832002-05-29 17:15:42 +0000212/* Subroutine of cpp_classify_number. S points to an integer suffix
213 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
214 flag vector describing the suffix. */
215static unsigned int
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000216interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len)
Neil Boothcd7ab832002-05-29 17:15:42 +0000217{
218 size_t u, l, i;
219
220 u = l = i = 0;
221
222 while (len--)
223 switch (s[len])
224 {
225 case 'u': case 'U': u++; break;
226 case 'i': case 'I':
227 case 'j': case 'J': i++; break;
228 case 'l': case 'L': l++;
229 /* If there are two Ls, they must be adjacent and the same case. */
230 if (l == 2 && s[len] != s[len + 1])
231 return 0;
232 break;
233 default:
234 return 0;
235 }
236
237 if (l > 2 || u > 1 || i > 1)
238 return 0;
239
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000240 if (i && !CPP_OPTION (pfile, ext_numeric_literals))
241 return 0;
242
Neil Boothcd7ab832002-05-29 17:15:42 +0000243 return ((i ? CPP_N_IMAGINARY : 0)
244 | (u ? CPP_N_UNSIGNED : 0)
245 | ((l == 0) ? CPP_N_SMALL
246 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
247}
248
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000249/* Return the classification flags for an int suffix. */
250unsigned int
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000251cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len)
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000252{
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000253 return interpret_int_suffix (pfile, (const unsigned char *)s, len);
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000254}
255
256/* Return the string type corresponding to the the input user-defined string
257 literal type. If the input type is not a user-defined string literal
258 type return the input type. */
259enum cpp_ttype
260cpp_userdef_string_remove_type (enum cpp_ttype type)
261{
262 if (type == CPP_STRING_USERDEF)
263 return CPP_STRING;
264 else if (type == CPP_WSTRING_USERDEF)
265 return CPP_WSTRING;
266 else if (type == CPP_STRING16_USERDEF)
267 return CPP_STRING16;
268 else if (type == CPP_STRING32_USERDEF)
269 return CPP_STRING32;
270 else if (type == CPP_UTF8STRING_USERDEF)
271 return CPP_UTF8STRING;
272 else
273 return type;
274}
275
276/* Return the user-defined string literal type corresponding to the input
277 string type. If the input type is not a string type return the input
278 type. */
279enum cpp_ttype
280cpp_userdef_string_add_type (enum cpp_ttype type)
281{
282 if (type == CPP_STRING)
283 return CPP_STRING_USERDEF;
284 else if (type == CPP_WSTRING)
285 return CPP_WSTRING_USERDEF;
286 else if (type == CPP_STRING16)
287 return CPP_STRING16_USERDEF;
288 else if (type == CPP_STRING32)
289 return CPP_STRING32_USERDEF;
290 else if (type == CPP_UTF8STRING)
291 return CPP_UTF8STRING_USERDEF;
292 else
293 return type;
294}
295
296/* Return the char type corresponding to the the input user-defined char
297 literal type. If the input type is not a user-defined char literal
298 type return the input type. */
299enum cpp_ttype
300cpp_userdef_char_remove_type (enum cpp_ttype type)
301{
302 if (type == CPP_CHAR_USERDEF)
303 return CPP_CHAR;
304 else if (type == CPP_WCHAR_USERDEF)
305 return CPP_WCHAR;
306 else if (type == CPP_CHAR16_USERDEF)
Ed Smith-Rowland7e74ce32011-11-21 19:27:30 +0000307 return CPP_CHAR16;
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000308 else if (type == CPP_CHAR32_USERDEF)
Ed Smith-Rowland7e74ce32011-11-21 19:27:30 +0000309 return CPP_CHAR32;
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000310 else
311 return type;
312}
313
314/* Return the user-defined char literal type corresponding to the input
315 char type. If the input type is not a char type return the input
316 type. */
317enum cpp_ttype
318cpp_userdef_char_add_type (enum cpp_ttype type)
319{
320 if (type == CPP_CHAR)
321 return CPP_CHAR_USERDEF;
322 else if (type == CPP_WCHAR)
323 return CPP_WCHAR_USERDEF;
324 else if (type == CPP_CHAR16)
325 return CPP_CHAR16_USERDEF;
326 else if (type == CPP_CHAR32)
327 return CPP_CHAR32_USERDEF;
328 else
329 return type;
330}
331
332/* Return true if the token type is a user-defined string literal. */
333bool
334cpp_userdef_string_p (enum cpp_ttype type)
335{
336 if (type == CPP_STRING_USERDEF
337 || type == CPP_WSTRING_USERDEF
338 || type == CPP_STRING16_USERDEF
339 || type == CPP_STRING32_USERDEF
340 || type == CPP_UTF8STRING_USERDEF)
341 return true;
342 else
343 return false;
344}
345
346/* Return true if the token type is a user-defined char literal. */
347bool
348cpp_userdef_char_p (enum cpp_ttype type)
349{
350 if (type == CPP_CHAR_USERDEF
351 || type == CPP_WCHAR_USERDEF
352 || type == CPP_CHAR16_USERDEF
353 || type == CPP_CHAR32_USERDEF)
354 return true;
355 else
356 return false;
357}
358
359/* Extract the suffix from a user-defined literal string or char. */
360const char *
361cpp_get_userdef_suffix (const cpp_token *tok)
362{
363 unsigned int len = tok->val.str.len;
364 const char *text = (const char *)tok->val.str.text;
365 char delim;
366 unsigned int i;
367 for (i = 0; i < len; ++i)
368 if (text[i] == '\'' || text[i] == '"')
369 break;
370 if (i == len)
371 return text + len;
372 delim = text[i];
373 for (i = len; i > 0; --i)
374 if (text[i - 1] == delim)
375 break;
376 return text + i;
377}
378
Neil Boothcd7ab832002-05-29 17:15:42 +0000379/* Categorize numeric constants according to their field (integer,
380 floating point, or invalid), radix (decimal, octal, hexadecimal),
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000381 and type suffixes.
382
383 TOKEN is the token that represents the numeric constant to
384 classify.
385
386 In C++0X if UD_SUFFIX is non null it will be assigned
387 any unrecognized suffix for a user-defined literal.
388
389 VIRTUAL_LOCATION is the virtual location for TOKEN. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000390unsigned int
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000391cpp_classify_number (cpp_reader *pfile, const cpp_token *token,
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000392 const char **ud_suffix, source_location virtual_location)
Neil Boothcd7ab832002-05-29 17:15:42 +0000393{
394 const uchar *str = token->val.str.text;
395 const uchar *limit;
396 unsigned int max_digit, result, radix;
397 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000398 bool seen_digit;
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000399 bool seen_digit_sep;
Neil Boothcd7ab832002-05-29 17:15:42 +0000400
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000401 if (ud_suffix)
402 *ud_suffix = NULL;
403
Neil Boothcd7ab832002-05-29 17:15:42 +0000404 /* If the lexer has done its job, length one can only be a single
405 digit. Fast-path this very common case. */
406 if (token->val.str.len == 1)
407 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
408
409 limit = str + token->val.str.len;
410 float_flag = NOT_FLOAT;
411 max_digit = 0;
412 radix = 10;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000413 seen_digit = false;
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000414 seen_digit_sep = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000415
416 /* First, interpret the radix. */
417 if (*str == '0')
418 {
419 radix = 8;
420 str++;
421
422 /* Require at least one hex digit to classify it as hex. */
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000423 if (*str == 'x' || *str == 'X')
Neil Boothcd7ab832002-05-29 17:15:42 +0000424 {
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000425 if (str[1] == '.' || ISXDIGIT (str[1]))
426 {
427 radix = 16;
428 str++;
429 }
430 else if (DIGIT_SEP (str[1]))
431 SYNTAX_ERROR_AT (virtual_location,
432 "digit separator after base indicator");
Neil Boothcd7ab832002-05-29 17:15:42 +0000433 }
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000434 else if (*str == 'b' || *str == 'B')
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000435 {
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000436 if (str[1] == '0' || str[1] == '1')
437 {
438 radix = 2;
439 str++;
440 }
441 else if (DIGIT_SEP (str[1]))
442 SYNTAX_ERROR_AT (virtual_location,
443 "digit separator after base indicator");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000444 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000445 }
446
447 /* Now scan for a well-formed integer or float. */
448 for (;;)
449 {
450 unsigned int c = *str++;
451
452 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
453 {
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000454 seen_digit_sep = false;
Joseph Myersdadab4f2010-01-01 18:08:17 +0000455 seen_digit = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000456 c = hex_value (c);
457 if (c > max_digit)
458 max_digit = c;
459 }
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000460 else if (DIGIT_SEP (c))
461 {
462 if (seen_digit_sep)
463 SYNTAX_ERROR_AT (virtual_location, "adjacent digit separators");
464 seen_digit_sep = true;
465 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000466 else if (c == '.')
467 {
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000468 if (seen_digit_sep || DIGIT_SEP (*str))
469 SYNTAX_ERROR_AT (virtual_location,
470 "digit separator adjacent to decimal point");
471 seen_digit_sep = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000472 if (float_flag == NOT_FLOAT)
473 float_flag = AFTER_POINT;
474 else
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000475 SYNTAX_ERROR_AT (virtual_location,
476 "too many decimal points in number");
Neil Boothcd7ab832002-05-29 17:15:42 +0000477 }
478 else if ((radix <= 10 && (c == 'e' || c == 'E'))
479 || (radix == 16 && (c == 'p' || c == 'P')))
480 {
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000481 if (seen_digit_sep || DIGIT_SEP (*str))
482 SYNTAX_ERROR_AT (virtual_location,
483 "digit separator adjacent to exponent");
Neil Boothcd7ab832002-05-29 17:15:42 +0000484 float_flag = AFTER_EXPON;
485 break;
486 }
487 else
488 {
489 /* Start of suffix. */
490 str--;
491 break;
492 }
493 }
494
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000495 if (seen_digit_sep && float_flag != AFTER_EXPON)
496 SYNTAX_ERROR_AT (virtual_location,
497 "digit separator outside digit sequence");
498
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000499 /* The suffix may be for decimal fixed-point constants without exponent. */
500 if (radix != 16 && float_flag == NOT_FLOAT)
501 {
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000502 result = interpret_float_suffix (pfile, str, limit - str);
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000503 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM))
504 {
505 result |= CPP_N_FLOATING;
506 /* We need to restore the radix to 10, if the radix is 8. */
507 if (radix == 8)
508 radix = 10;
509
510 if (CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000511 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
512 "fixed-point constants are a GCC extension");
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000513 goto syntax_ok;
514 }
515 else
516 result = 0;
517 }
518
Neil Boothcd7ab832002-05-29 17:15:42 +0000519 if (float_flag != NOT_FLOAT && radix == 8)
520 radix = 10;
521
522 if (max_digit >= radix)
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000523 {
524 if (radix == 2)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000525 SYNTAX_ERROR2_AT (virtual_location,
526 "invalid digit \"%c\" in binary constant", '0' + max_digit);
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000527 else
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000528 SYNTAX_ERROR2_AT (virtual_location,
529 "invalid digit \"%c\" in octal constant", '0' + max_digit);
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000530 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000531
532 if (float_flag != NOT_FLOAT)
533 {
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000534 if (radix == 2)
535 {
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000536 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
537 "invalid prefix \"0b\" for floating constant");
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000538 return CPP_N_INVALID;
539 }
540
Joseph Myersdadab4f2010-01-01 18:08:17 +0000541 if (radix == 16 && !seen_digit)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000542 SYNTAX_ERROR_AT (virtual_location,
543 "no digits in hexadecimal floating constant");
Joseph Myersdadab4f2010-01-01 18:08:17 +0000544
Edward Smith-Rowland7c05e502014-08-28 02:38:24 +0000545 if (radix == 16 && CPP_PEDANTIC (pfile)
546 && !CPP_OPTION (pfile, extended_numbers))
547 {
548 if (CPP_OPTION (pfile, cplusplus))
549 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
550 "use of C++11 hexadecimal floating constant");
551 else
552 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
553 "use of C99 hexadecimal floating constant");
554 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000555
556 if (float_flag == AFTER_EXPON)
557 {
558 if (*str == '+' || *str == '-')
559 str++;
560
561 /* Exponent is decimal, even if string is a hex float. */
562 if (!ISDIGIT (*str))
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000563 {
564 if (DIGIT_SEP (*str))
565 SYNTAX_ERROR_AT (virtual_location,
566 "digit separator adjacent to exponent");
567 else
568 SYNTAX_ERROR_AT (virtual_location, "exponent has no digits");
569 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000570 do
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000571 {
572 seen_digit_sep = DIGIT_SEP (*str);
573 str++;
574 }
575 while (ISDIGIT (*str) || DIGIT_SEP (*str));
Neil Boothcd7ab832002-05-29 17:15:42 +0000576 }
577 else if (radix == 16)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000578 SYNTAX_ERROR_AT (virtual_location,
579 "hexadecimal floating constants require an exponent");
Neil Boothcd7ab832002-05-29 17:15:42 +0000580
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000581 if (seen_digit_sep)
582 SYNTAX_ERROR_AT (virtual_location,
583 "digit separator outside digit sequence");
584
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000585 result = interpret_float_suffix (pfile, str, limit - str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000586 if (result == 0)
587 {
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000588 if (CPP_OPTION (pfile, user_literals))
589 {
590 if (ud_suffix)
591 *ud_suffix = (const char *) str;
592 result = CPP_N_LARGE | CPP_N_USERDEF;
593 }
594 else
595 {
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000596 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
597 "invalid suffix \"%.*s\" on floating constant",
598 (int) (limit - str), str);
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000599 return CPP_N_INVALID;
600 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000601 }
602
603 /* Traditional C didn't accept any floating suffixes. */
604 if (limit != str
605 && CPP_WTRADITIONAL (pfile)
606 && ! cpp_sys_macro_p (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000607 cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0,
608 "traditional C rejects the \"%.*s\" suffix",
609 (int) (limit - str), str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000610
Janis Johnson839a3b82009-04-01 17:31:26 +0000611 /* A suffix for double is a GCC extension via decimal float support.
612 If the suffix also specifies an imaginary value we'll catch that
613 later. */
614 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000615 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
616 "suffix for double constant is a GCC extension");
Janis Johnson839a3b82009-04-01 17:31:26 +0000617
Jon Grimmad6ed772005-12-06 23:13:15 +0000618 /* Radix must be 10 for decimal floats. */
619 if ((result & CPP_N_DFLOAT) && radix != 10)
620 {
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000621 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
622 "invalid suffix \"%.*s\" with hexadecimal floating constant",
623 (int) (limit - str), str);
Jon Grimmad6ed772005-12-06 23:13:15 +0000624 return CPP_N_INVALID;
625 }
626
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000627 if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000628 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
629 "fixed-point constants are a GCC extension");
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000630
Janis Johnson5a6bb572007-05-14 23:45:40 +0000631 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000632 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
633 "decimal float constants are a GCC extension");
Janis Johnson5a6bb572007-05-14 23:45:40 +0000634
Neil Boothcd7ab832002-05-29 17:15:42 +0000635 result |= CPP_N_FLOATING;
636 }
637 else
638 {
Ed Smith-Rowlanda4a00162012-11-10 00:08:49 +0000639 result = interpret_int_suffix (pfile, str, limit - str);
Neil Boothcd7ab832002-05-29 17:15:42 +0000640 if (result == 0)
641 {
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000642 if (CPP_OPTION (pfile, user_literals))
643 {
644 if (ud_suffix)
645 *ud_suffix = (const char *) str;
646 result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF;
647 }
648 else
649 {
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000650 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
651 "invalid suffix \"%.*s\" on integer constant",
652 (int) (limit - str), str);
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000653 return CPP_N_INVALID;
654 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000655 }
656
Zack Weinberg56da7202002-08-02 04:18:16 +0000657 /* Traditional C only accepted the 'L' suffix.
658 Suppress warning about 'LL' with -Wno-long-long. */
659 if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile))
660 {
661 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY));
Simon Baldwin87cf0652010-04-07 17:18:10 +0000662 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE
Joseph Myerse3339d02010-09-29 15:49:14 +0100663 && CPP_OPTION (pfile, cpp_warn_long_long);
Zack Weinberg56da7202002-08-02 04:18:16 +0000664
Simon Baldwin87cf0652010-04-07 17:18:10 +0000665 if (u_or_i || large)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000666 cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL,
667 virtual_location, 0,
668 "traditional C rejects the \"%.*s\" suffix",
669 (int) (limit - str), str);
Zack Weinberg56da7202002-08-02 04:18:16 +0000670 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000671
672 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
Joseph Myerse3339d02010-09-29 15:49:14 +0100673 && CPP_OPTION (pfile, cpp_warn_long_long))
Simon Baldwin87cf0652010-04-07 17:18:10 +0000674 {
675 const char *message = CPP_OPTION (pfile, cplusplus)
Jakub Jelinek01187df2013-04-28 23:36:57 +0200676 ? N_("use of C++11 long long integer constant")
Simon Baldwin87cf0652010-04-07 17:18:10 +0000677 : N_("use of C99 long long integer constant");
678
679 if (CPP_OPTION (pfile, c99))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000680 cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location,
681 0, message);
Simon Baldwin87cf0652010-04-07 17:18:10 +0000682 else
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000683 cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG,
684 virtual_location, 0, message);
Simon Baldwin87cf0652010-04-07 17:18:10 +0000685 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000686
687 result |= CPP_N_INTEGER;
688 }
689
Chao-ying Fuac6b1c62007-08-30 23:05:17 +0000690 syntax_ok:
Neil Boothcd7ab832002-05-29 17:15:42 +0000691 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000692 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
693 "imaginary constants are a GCC extension");
Jakub Jelinek01187df2013-04-28 23:36:57 +0200694 if (radix == 2
695 && !CPP_OPTION (pfile, binary_constants)
696 && CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000697 cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0,
Jakub Jelinek01187df2013-04-28 23:36:57 +0200698 CPP_OPTION (pfile, cplusplus)
Edward Smith-Rowlande4276ba2014-08-23 16:50:22 +0000699 ? "binary constants are a C++14 feature "
Jakub Jelinek01187df2013-04-28 23:36:57 +0200700 "or GCC extension"
701 : "binary constants are a GCC extension");
Neil Boothcd7ab832002-05-29 17:15:42 +0000702
703 if (radix == 10)
704 result |= CPP_N_DECIMAL;
705 else if (radix == 16)
706 result |= CPP_N_HEX;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000707 else if (radix == 2)
708 result |= CPP_N_BINARY;
Neil Boothcd7ab832002-05-29 17:15:42 +0000709 else
710 result |= CPP_N_OCTAL;
711
712 return result;
713
714 syntax_error:
715 return CPP_N_INVALID;
716}
717
718/* cpp_interpret_integer converts an integer constant into a cpp_num,
719 of precision options->precision.
720
721 We do not provide any interface for decimal->float conversion,
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000722 because the preprocessor doesn't need it and we don't want to
723 drag in GCC's floating point emulator. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000724cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000725cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
726 unsigned int type)
Neil Boothcd7ab832002-05-29 17:15:42 +0000727{
728 const uchar *p, *end;
729 cpp_num result;
730
731 result.low = 0;
732 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000733 result.unsignedp = !!(type & CPP_N_UNSIGNED);
734 result.overflow = false;
Neil Boothcd7ab832002-05-29 17:15:42 +0000735
736 p = token->val.str.text;
737 end = p + token->val.str.len;
738
739 /* Common case of a single digit. */
740 if (token->val.str.len == 1)
741 result.low = p[0] - '0';
742 else
743 {
744 cpp_num_part max;
745 size_t precision = CPP_OPTION (pfile, precision);
746 unsigned int base = 10, c = 0;
747 bool overflow = false;
748
749 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
750 {
751 base = 8;
752 p++;
753 }
754 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
755 {
756 base = 16;
757 p += 2;
758 }
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000759 else if ((type & CPP_N_RADIX) == CPP_N_BINARY)
760 {
761 base = 2;
762 p += 2;
763 }
Neil Boothcd7ab832002-05-29 17:15:42 +0000764
765 /* We can add a digit to numbers strictly less than this without
766 needing the precision and slowness of double integers. */
767 max = ~(cpp_num_part) 0;
768 if (precision < PART_PRECISION)
769 max >>= PART_PRECISION - precision;
770 max = (max - base + 1) / base + 1;
771
772 for (; p < end; p++)
773 {
774 c = *p;
775
776 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
777 c = hex_value (c);
Edward Smith-Rowland7057e642013-10-31 14:01:23 +0000778 else if (DIGIT_SEP (c))
779 continue;
Neil Boothcd7ab832002-05-29 17:15:42 +0000780 else
781 break;
782
783 /* Strict inequality for when max is set to zero. */
784 if (result.low < max)
785 result.low = result.low * base + c;
786 else
787 {
788 result = append_digit (result, c, base, precision);
789 overflow |= result.overflow;
790 max = 0;
791 }
792 }
793
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +0000794 if (overflow && !(type & CPP_N_USERDEF))
John David Anglin0527bc42003-11-01 22:56:54 +0000795 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Boothcd7ab832002-05-29 17:15:42 +0000796 "integer constant is too large for its type");
Neil Booth017acb42002-06-20 20:34:19 +0000797 /* If too big to be signed, consider it unsigned. Only warn for
798 decimal numbers. Traditional numbers were always signed (but
Kazu Hirata8d9afc4e2002-09-16 11:42:00 +0000799 we still honor an explicit U suffix); but we only have
Neil Boothcd98faa2002-07-09 22:21:37 +0000800 traditional semantics in directives. */
Neil Booth017acb42002-06-20 20:34:19 +0000801 else if (!result.unsignedp
Neil Boothcd98faa2002-07-09 22:21:37 +0000802 && !(CPP_OPTION (pfile, traditional)
803 && pfile->state.in_directive)
Neil Booth017acb42002-06-20 20:34:19 +0000804 && !num_positive (result, precision))
Neil Boothcd7ab832002-05-29 17:15:42 +0000805 {
Joseph Myersf88d0772009-04-25 19:46:03 +0100806 /* This is for constants within the range of uintmax_t but
Joseph Myers813b9e72009-04-25 19:59:20 +0100807 not that of intmax_t. For such decimal constants, a
Joseph Myersf88d0772009-04-25 19:46:03 +0100808 diagnostic is required for C99 as the selected type must
809 be signed and not having a type is a constraint violation
810 (DR#298, TC3), so this must be a pedwarn. For C90,
811 unsigned long is specified to be used for a constant that
812 does not fit in signed long; if uintmax_t has the same
813 range as unsigned long this means only a warning is
814 appropriate here. C90 permits the preprocessor to use a
815 wider range than unsigned long in the compiler, so if
816 uintmax_t is wider than unsigned long no diagnostic is
817 required for such constants in preprocessor #if
818 expressions and the compiler will pedwarn for such
819 constants outside the range of unsigned long that reach
820 the compiler so a diagnostic is not required there
821 either; thus, pedwarn for C99 but use a plain warning for
822 C90. */
Neil Boothcd7ab832002-05-29 17:15:42 +0000823 if (base == 10)
Joseph Myersf88d0772009-04-25 19:46:03 +0100824 cpp_error (pfile, (CPP_OPTION (pfile, c99)
825 ? CPP_DL_PEDWARN
826 : CPP_DL_WARNING),
Neil Boothcd7ab832002-05-29 17:15:42 +0000827 "integer constant is so large that it is unsigned");
Neil Booth23ff0222002-07-17 17:27:14 +0000828 result.unsignedp = true;
Neil Boothcd7ab832002-05-29 17:15:42 +0000829 }
830 }
831
832 return result;
833}
Zack Weinbergcf00a882000-07-08 02:33:00 +0000834
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000835/* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
Neil Booth91318902002-05-26 18:42:21 +0000836static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000837append_digit (cpp_num num, int digit, int base, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +0000838{
839 cpp_num result;
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000840 unsigned int shift;
Neil Booth91318902002-05-26 18:42:21 +0000841 bool overflow;
842 cpp_num_part add_high, add_low;
843
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000844 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
Neil Booth91318902002-05-26 18:42:21 +0000845 need to worry about add_high overflowing. */
Joerg Wunschf7fd7752007-06-05 22:25:27 +0000846 switch (base)
847 {
848 case 2:
849 shift = 1;
850 break;
851
852 case 16:
853 shift = 4;
854 break;
855
856 default:
857 shift = 3;
858 }
Neil Booth23ff0222002-07-17 17:27:14 +0000859 overflow = !!(num.high >> (PART_PRECISION - shift));
Neil Booth91318902002-05-26 18:42:21 +0000860 result.high = num.high << shift;
861 result.low = num.low << shift;
862 result.high |= num.low >> (PART_PRECISION - shift);
Diego Novillo6de9cd92004-05-13 02:41:07 -0400863 result.unsignedp = num.unsignedp;
Neil Booth91318902002-05-26 18:42:21 +0000864
865 if (base == 10)
866 {
867 add_low = num.low << 1;
868 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
869 }
870 else
871 add_high = add_low = 0;
872
873 if (add_low + digit < add_low)
874 add_high++;
875 add_low += digit;
Eric Christopher22a8a522007-05-02 21:57:50 +0000876
Neil Booth91318902002-05-26 18:42:21 +0000877 if (result.low + add_low < result.low)
878 add_high++;
879 if (result.high + add_high < result.high)
880 overflow = true;
881
882 result.low += add_low;
883 result.high += add_high;
Diego Novillo6de9cd92004-05-13 02:41:07 -0400884 result.overflow = overflow;
Neil Booth91318902002-05-26 18:42:21 +0000885
886 /* The above code catches overflow of a cpp_num type. This catches
887 overflow of the (possibly shorter) target precision. */
888 num.low = result.low;
889 num.high = result.high;
890 result = num_trim (result, precision);
891 if (!num_eq (result, num))
Diego Novillo6de9cd92004-05-13 02:41:07 -0400892 result.overflow = true;
Neil Booth91318902002-05-26 18:42:21 +0000893
Neil Booth91318902002-05-26 18:42:21 +0000894 return result;
895}
896
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000897/* Handle meeting "defined" in a preprocessor expression. */
Neil Booth91318902002-05-26 18:42:21 +0000898static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +0000899parse_defined (cpp_reader *pfile)
Zack Weinbergba412f12000-03-01 00:57:09 +0000900{
Neil Booth91318902002-05-26 18:42:21 +0000901 cpp_num result;
Neil Booth93c803682000-10-28 17:59:06 +0000902 int paren = 0;
903 cpp_hashnode *node = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000904 const cpp_token *token;
Neil Booth63d75002001-11-05 22:26:13 +0000905 cpp_context *initial_context = pfile->context;
Zack Weinbergcf00a882000-07-08 02:33:00 +0000906
Neil Booth93c803682000-10-28 17:59:06 +0000907 /* Don't expand macros. */
908 pfile->state.prevent_expansion++;
909
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000910 token = cpp_get_token (pfile);
911 if (token->type == CPP_OPEN_PAREN)
Zack Weinbergcf00a882000-07-08 02:33:00 +0000912 {
913 paren = 1;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000914 token = cpp_get_token (pfile);
Zack Weinbergcf00a882000-07-08 02:33:00 +0000915 }
916
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000917 if (token->type == CPP_NAME)
Zack Weinberg041c3192000-07-04 01:58:21 +0000918 {
Joseph Myers9a0c6182009-05-10 15:27:32 +0100919 node = token->val.node.node;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000920 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
Neil Booth93c803682000-10-28 17:59:06 +0000921 {
John David Anglin0527bc42003-11-01 22:56:54 +0000922 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\"");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000923 node = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000924 }
Zack Weinberg041c3192000-07-04 01:58:21 +0000925 }
Neil Booth93c803682000-10-28 17:59:06 +0000926 else
Neil Booth3c8465d2001-02-06 20:07:07 +0000927 {
John David Anglin0527bc42003-11-01 22:56:54 +0000928 cpp_error (pfile, CPP_DL_ERROR,
Neil Boothebef4e82002-04-14 18:42:47 +0000929 "operator \"defined\" requires an identifier");
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000930 if (token->flags & NAMED_OP)
Neil Booth3c8465d2001-02-06 20:07:07 +0000931 {
932 cpp_token op;
933
934 op.flags = 0;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000935 op.type = token->type;
John David Anglin0527bc42003-11-01 22:56:54 +0000936 cpp_error (pfile, CPP_DL_ERROR,
Neil Booth3c8465d2001-02-06 20:07:07 +0000937 "(\"%s\" is an alternative token for \"%s\" in C++)",
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000938 cpp_token_as_text (pfile, token),
Neil Booth3c8465d2001-02-06 20:07:07 +0000939 cpp_token_as_text (pfile, &op));
940 }
941 }
Neil Booth93c803682000-10-28 17:59:06 +0000942
Neil Booth91318902002-05-26 18:42:21 +0000943 if (node)
Neil Booth93c803682000-10-28 17:59:06 +0000944 {
Neil Booth335d03e2003-08-03 12:23:46 +0000945 if (pfile->context != initial_context && CPP_PEDANTIC (pfile))
John David Anglin0527bc42003-11-01 22:56:54 +0000946 cpp_error (pfile, CPP_DL_WARNING,
Neil Boothebef4e82002-04-14 18:42:47 +0000947 "this use of \"defined\" may not be portable");
Neil Booth63d75002001-11-05 22:26:13 +0000948
Neil Bootha69cbaa2002-07-23 22:57:49 +0000949 _cpp_mark_macro_used (node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100950 if (!(node->flags & NODE_USED))
951 {
952 node->flags |= NODE_USED;
953 if (node->type == NT_MACRO)
954 {
Jakub Jelineka69d2522010-09-01 00:47:25 +0200955 if ((node->flags & NODE_BUILTIN)
956 && pfile->cb.user_builtin_macro)
957 pfile->cb.user_builtin_macro (pfile, node);
Joseph Myers93d45d92008-04-02 20:42:53 +0100958 if (pfile->cb.used_define)
959 pfile->cb.used_define (pfile, pfile->directive_line, node);
960 }
961 else
962 {
963 if (pfile->cb.used_undef)
964 pfile->cb.used_undef (pfile, pfile->directive_line, node);
965 }
966 }
Neil Bootha69cbaa2002-07-23 22:57:49 +0000967
Neil Booth6d18adb2001-07-29 17:27:57 +0000968 /* A possible controlling macro of the form #if !defined ().
969 _cpp_parse_expr checks there was no other junk on the line. */
970 pfile->mi_ind_cmacro = node;
Neil Booth93c803682000-10-28 17:59:06 +0000971 }
972
973 pfile->state.prevent_expansion--;
Neil Booth91318902002-05-26 18:42:21 +0000974
Michael Meissnerf3c33d92011-03-21 16:21:30 +0000975 /* Do not treat conditional macros as being defined. This is due to the
976 powerpc and spu ports using conditional macros for 'vector', 'bool', and
977 'pixel' to act as conditional keywords. This messes up tests like #ifndef
978 bool. */
Neil Booth23ff0222002-07-17 17:27:14 +0000979 result.unsignedp = false;
Neil Booth91318902002-05-26 18:42:21 +0000980 result.high = 0;
Neil Booth23ff0222002-07-17 17:27:14 +0000981 result.overflow = false;
Michael Meissnerf3c33d92011-03-21 16:21:30 +0000982 result.low = (node && node->type == NT_MACRO
983 && (node->flags & NODE_CONDITIONAL) == 0);
Neil Booth91318902002-05-26 18:42:21 +0000984 return result;
Zack Weinberg15dad1d2000-05-18 15:55:46 +0000985}
986
Neil Booth60284a52002-04-28 23:14:56 +0000987/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
988 number or character constant, or the result of the "defined" or "#"
Neil Boothcd7ab832002-05-29 17:15:42 +0000989 operators). */
Neil Booth91318902002-05-26 18:42:21 +0000990static cpp_num
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +0000991eval_token (cpp_reader *pfile, const cpp_token *token,
992 source_location virtual_location)
Per Bothner7f2935c1995-03-16 13:59:07 -0800993{
Neil Booth91318902002-05-26 18:42:21 +0000994 cpp_num result;
Neil Booth60284a52002-04-28 23:14:56 +0000995 unsigned int temp;
Neil Booth4268e8b2002-05-04 07:30:32 +0000996 int unsignedp = 0;
Zack Weinberg041c3192000-07-04 01:58:21 +0000997
Diego Novillo6de9cd92004-05-13 02:41:07 -0400998 result.unsignedp = false;
999 result.overflow = false;
1000
Neil Booth93c803682000-10-28 17:59:06 +00001001 switch (token->type)
Zack Weinbergba412f12000-03-01 00:57:09 +00001002 {
Per Bothner7f2935c1995-03-16 13:59:07 -08001003 case CPP_NUMBER:
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001004 temp = cpp_classify_number (pfile, token, NULL, virtual_location);
Ed Smith-Rowland3ce4f9e2011-10-26 19:30:59 +00001005 if (temp & CPP_N_USERDEF)
1006 cpp_error (pfile, CPP_DL_ERROR,
1007 "user-defined literal in preprocessor expression");
Neil Boothcd7ab832002-05-29 17:15:42 +00001008 switch (temp & CPP_N_CATEGORY)
1009 {
1010 case CPP_N_FLOATING:
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001011 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1012 "floating constant in preprocessor expression");
Neil Boothcd7ab832002-05-29 17:15:42 +00001013 break;
1014 case CPP_N_INTEGER:
1015 if (!(temp & CPP_N_IMAGINARY))
1016 return cpp_interpret_integer (pfile, token, temp);
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001017 cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0,
1018 "imaginary number in preprocessor expression");
Neil Boothcd7ab832002-05-29 17:15:42 +00001019 break;
1020
1021 case CPP_N_INVALID:
1022 /* Error already issued. */
1023 break;
1024 }
1025 result.high = result.low = 0;
1026 break;
Neil Booth7f2f1a62000-11-14 18:32:06 +00001027
Alexandre Oliva525bc952000-02-23 19:21:07 +00001028 case CPP_WCHAR:
Neil Booth4268e8b2002-05-04 07:30:32 +00001029 case CPP_CHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +00001030 case CPP_CHAR16:
1031 case CPP_CHAR32:
Neil Bootha5a49442002-05-06 22:53:10 +00001032 {
Neil Booth91318902002-05-26 18:42:21 +00001033 cppchar_t cc = cpp_interpret_charconst (pfile, token,
1034 &temp, &unsignedp);
1035
1036 result.high = 0;
1037 result.low = cc;
Neil Bootha5a49442002-05-06 22:53:10 +00001038 /* Sign-extend the result if necessary. */
Neil Booth91318902002-05-26 18:42:21 +00001039 if (!unsignedp && (cppchar_signed_t) cc < 0)
1040 {
1041 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
1042 result.low |= ~(~(cpp_num_part) 0
1043 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
1044 result.high = ~(cpp_num_part) 0;
1045 result = num_trim (result, CPP_OPTION (pfile, precision));
1046 }
Neil Bootha5a49442002-05-06 22:53:10 +00001047 }
Neil Booth60284a52002-04-28 23:14:56 +00001048 break;
Zack Weinbergba412f12000-03-01 00:57:09 +00001049
Zack Weinberg92936ec2000-07-19 20:18:08 +00001050 case CPP_NAME:
Joseph Myers9a0c6182009-05-10 15:27:32 +01001051 if (token->val.node.node == pfile->spec_nodes.n_defined)
Neil Booth63d75002001-11-05 22:26:13 +00001052 return parse_defined (pfile);
Edward Smith-Rowlanda15f7cb2014-10-01 11:49:23 +00001053 else if (token->val.node.node == pfile->spec_nodes.n__has_include__)
1054 return parse_has_include (pfile, IT_INCLUDE);
1055 else if (token->val.node.node == pfile->spec_nodes.n__has_include_next__)
1056 return parse_has_include (pfile, IT_INCLUDE_NEXT);
Zack Weinberg7d4918a2001-02-07 18:32:42 +00001057 else if (CPP_OPTION (pfile, cplusplus)
Joseph Myers9a0c6182009-05-10 15:27:32 +01001058 && (token->val.node.node == pfile->spec_nodes.n_true
1059 || token->val.node.node == pfile->spec_nodes.n_false))
Zack Weinberg7d4918a2001-02-07 18:32:42 +00001060 {
Neil Booth91318902002-05-26 18:42:21 +00001061 result.high = 0;
Joseph Myers9a0c6182009-05-10 15:27:32 +01001062 result.low = (token->val.node.node == pfile->spec_nodes.n_true);
Zack Weinberg7d4918a2001-02-07 18:32:42 +00001063 }
1064 else
1065 {
Neil Booth91318902002-05-26 18:42:21 +00001066 result.high = 0;
1067 result.low = 0;
Neil Booth87ed1092002-04-28 19:42:54 +00001068 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001069 cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0,
1070 "\"%s\" is not defined",
1071 NODE_NAME (token->val.node.node));
Zack Weinberg7d4918a2001-02-07 18:32:42 +00001072 }
Neil Booth60284a52002-04-28 23:14:56 +00001073 break;
Zack Weinberg5dfa4da1999-02-08 20:27:27 +00001074
Tom Tromey899015a2008-05-13 14:50:27 +00001075 case CPP_HASH:
1076 if (!pfile->state.skipping)
1077 {
1078 /* A pedantic warning takes precedence over a deprecated
1079 warning here. */
1080 if (CPP_PEDANTIC (pfile))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001081 cpp_error_with_line (pfile, CPP_DL_PEDWARN,
1082 virtual_location, 0,
1083 "assertions are a GCC extension");
Joseph Myerse3339d02010-09-29 15:49:14 +01001084 else if (CPP_OPTION (pfile, cpp_warn_deprecated))
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001085 cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0,
1086 "assertions are a deprecated extension");
Tom Tromey899015a2008-05-13 14:50:27 +00001087 }
Neil Booth91318902002-05-26 18:42:21 +00001088 _cpp_test_assertion (pfile, &temp);
1089 result.high = 0;
1090 result.low = temp;
Tom Tromey899015a2008-05-13 14:50:27 +00001091 break;
1092
1093 default:
1094 abort ();
Neil Booth93c803682000-10-28 17:59:06 +00001095 }
Per Bothner7f2935c1995-03-16 13:59:07 -08001096
Neil Booth23ff0222002-07-17 17:27:14 +00001097 result.unsignedp = !!unsignedp;
Neil Booth91318902002-05-26 18:42:21 +00001098 return result;
Per Bothner7f2935c1995-03-16 13:59:07 -08001099}
1100
Neil Booth4063b942000-04-02 08:27:23 +00001101/* Operator precedence and flags table.
Neil Boothdbac4af2000-04-01 07:48:59 +00001102
1103After an operator is returned from the lexer, if it has priority less
Neil Booth87ed1092002-04-28 19:42:54 +00001104than the operator on the top of the stack, we reduce the stack by one
1105operator and repeat the test. Since equal priorities do not reduce,
1106this is naturally right-associative.
Neil Boothdbac4af2000-04-01 07:48:59 +00001107
Neil Booth87ed1092002-04-28 19:42:54 +00001108We handle left-associative operators by decrementing the priority of
1109just-lexed operators by one, but retaining the priority of operators
1110already on the stack.
Neil Boothdbac4af2000-04-01 07:48:59 +00001111
1112The remaining cases are '(' and ')'. We handle '(' by skipping the
1113reduction phase completely. ')' is given lower priority than
1114everything else, including '(', effectively forcing a reduction of the
Kazu Hirata272d0be2002-12-19 05:18:13 +00001115parenthesized expression. If there is a matching '(', the routine
Neil Booth87ed1092002-04-28 19:42:54 +00001116reduce() exits immediately. If the normal exit route sees a ')', then
1117there cannot have been a matching '(' and an error message is output.
Neil Boothdbac4af2000-04-01 07:48:59 +00001118
Neil Boothf8b954f2002-04-26 06:32:50 +00001119The parser assumes all shifted operators require a left operand unless
1120the flag NO_L_OPERAND is set. These semantics are automatic; any
1121extra semantics need to be handled with operator-specific code. */
Per Bothner7f2935c1995-03-16 13:59:07 -08001122
Neil Booth68e652752002-07-20 13:31:56 +00001123/* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
1124 operand changes because of integer promotions. */
Neil Booth87ed1092002-04-28 19:42:54 +00001125#define NO_L_OPERAND (1 << 0)
1126#define LEFT_ASSOC (1 << 1)
Neil Booth68e652752002-07-20 13:31:56 +00001127#define CHECK_PROMOTION (1 << 2)
Neil Bootheba30522000-03-31 22:23:59 +00001128
Zack Weinbergcf00a882000-07-08 02:33:00 +00001129/* Operator to priority map. Must be in the same order as the first
1130 N entries of enum cpp_ttype. */
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001131static const struct cpp_operator
Zack Weinbergcf00a882000-07-08 02:33:00 +00001132{
Neil Booth60284a52002-04-28 23:14:56 +00001133 uchar prio;
Neil Booth87ed1092002-04-28 19:42:54 +00001134 uchar flags;
1135} optab[] =
1136{
Neil Boothad28cff2002-07-18 22:08:35 +00001137 /* EQ */ {0, 0}, /* Shouldn't happen. */
1138 /* NOT */ {16, NO_L_OPERAND},
Neil Booth68e652752002-07-20 13:31:56 +00001139 /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1140 /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1141 /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1142 /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION},
1143 /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1144 /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1145 /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION},
1146 /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION},
1147 /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION},
1148 /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +00001149 /* RSHIFT */ {13, LEFT_ASSOC},
1150 /* LSHIFT */ {13, LEFT_ASSOC},
Zack Weinbergcf00a882000-07-08 02:33:00 +00001151
Neil Boothad28cff2002-07-18 22:08:35 +00001152 /* COMPL */ {16, NO_L_OPERAND},
Neil Booth75aef482002-07-19 19:24:43 +00001153 /* AND_AND */ {6, LEFT_ASSOC},
1154 /* OR_OR */ {5, LEFT_ASSOC},
Tom Tromey71c10032008-05-06 17:15:07 +00001155 /* Note that QUERY, COLON, and COMMA must have the same precedence.
1156 However, there are some special cases for these in reduce(). */
1157 /* QUERY */ {4, 0},
Neil Booth68e652752002-07-20 13:31:56 +00001158 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION},
Tom Tromey71c10032008-05-06 17:15:07 +00001159 /* COMMA */ {4, LEFT_ASSOC},
Neil Booth75aef482002-07-19 19:24:43 +00001160 /* OPEN_PAREN */ {1, NO_L_OPERAND},
Neil Boothad28cff2002-07-18 22:08:35 +00001161 /* CLOSE_PAREN */ {0, 0},
1162 /* EOF */ {0, 0},
1163 /* EQ_EQ */ {11, LEFT_ASSOC},
1164 /* NOT_EQ */ {11, LEFT_ASSOC},
Neil Booth68e652752002-07-20 13:31:56 +00001165 /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
1166 /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION},
Neil Boothad28cff2002-07-18 22:08:35 +00001167 /* UPLUS */ {16, NO_L_OPERAND},
1168 /* UMINUS */ {16, NO_L_OPERAND}
Zack Weinbergcf00a882000-07-08 02:33:00 +00001169};
1170
Per Bothner7f2935c1995-03-16 13:59:07 -08001171/* Parse and evaluate a C expression, reading from PFILE.
Kazu Hiratadf383482002-05-22 22:02:16 +00001172 Returns the truth value of the expression.
Neil Booth87ed1092002-04-28 19:42:54 +00001173
1174 The implementation is an operator precedence parser, i.e. a
1175 bottom-up parser, using a stack for not-yet-reduced tokens.
1176
1177 The stack base is op_stack, and the current stack pointer is 'top'.
1178 There is a stack element for each operator (only), and the most
1179 recently pushed operator is 'top->op'. An operand (value) is
1180 stored in the 'value' field of the stack element of the operator
1181 that precedes it. */
1182bool
Tom Tromeyd7508872008-05-30 14:25:09 +00001183_cpp_parse_expr (cpp_reader *pfile, bool is_if)
Per Bothner7f2935c1995-03-16 13:59:07 -08001184{
Neil Booth87ed1092002-04-28 19:42:54 +00001185 struct op *top = pfile->op_stack;
1186 unsigned int lex_count;
1187 bool saw_leading_not, want_value = true;
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001188 source_location virtual_location = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001189
Neil Booth87ed1092002-04-28 19:42:54 +00001190 pfile->state.skip_eval = 0;
Per Bothner7f2935c1995-03-16 13:59:07 -08001191
Neil Booth93c803682000-10-28 17:59:06 +00001192 /* Set up detection of #if ! defined(). */
Neil Booth6d18adb2001-07-29 17:27:57 +00001193 pfile->mi_ind_cmacro = 0;
Neil Booth87ed1092002-04-28 19:42:54 +00001194 saw_leading_not = false;
Neil Booth6d18adb2001-07-29 17:27:57 +00001195 lex_count = 0;
Neil Booth93c803682000-10-28 17:59:06 +00001196
Neil Booth87ed1092002-04-28 19:42:54 +00001197 /* Lowest priority operator prevents further reductions. */
Zack Weinbergcf00a882000-07-08 02:33:00 +00001198 top->op = CPP_EOF;
Neil Booth4063b942000-04-02 08:27:23 +00001199
Per Bothner7f2935c1995-03-16 13:59:07 -08001200 for (;;)
1201 {
Zack Weinbergcf00a882000-07-08 02:33:00 +00001202 struct op op;
Per Bothner7f2935c1995-03-16 13:59:07 -08001203
Neil Booth6d18adb2001-07-29 17:27:57 +00001204 lex_count++;
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001205 op.token = cpp_get_token_with_location (pfile, &virtual_location);
Neil Booth68e652752002-07-20 13:31:56 +00001206 op.op = op.token->type;
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001207 op.loc = virtual_location;
Per Bothner7f2935c1995-03-16 13:59:07 -08001208
Per Bothner7f2935c1995-03-16 13:59:07 -08001209 switch (op.op)
1210 {
Neil Booth60284a52002-04-28 23:14:56 +00001211 /* These tokens convert into values. */
Neil Boothc60e94a2001-07-19 06:12:50 +00001212 case CPP_NUMBER:
Neil Booth60284a52002-04-28 23:14:56 +00001213 case CPP_CHAR:
1214 case CPP_WCHAR:
Kris Van Heesb6baa672008-04-18 13:58:08 +00001215 case CPP_CHAR16:
1216 case CPP_CHAR32:
Neil Booth60284a52002-04-28 23:14:56 +00001217 case CPP_NAME:
1218 case CPP_HASH:
Neil Boothf8b954f2002-04-26 06:32:50 +00001219 if (!want_value)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001220 SYNTAX_ERROR2_AT (op.loc,
1221 "missing binary operator before token \"%s\"",
1222 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +00001223 want_value = false;
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001224 top->value = eval_token (pfile, op.token, op.loc);
Neil Booth9ee703132000-04-01 07:42:37 +00001225 continue;
1226
Neil Booth6d18adb2001-07-29 17:27:57 +00001227 case CPP_NOT:
1228 saw_leading_not = lex_count == 1;
Neil Booth6d18adb2001-07-29 17:27:57 +00001229 break;
Zack Weinbergcf00a882000-07-08 02:33:00 +00001230 case CPP_PLUS:
Neil Boothf8b954f2002-04-26 06:32:50 +00001231 if (want_value)
1232 op.op = CPP_UPLUS;
1233 break;
1234 case CPP_MINUS:
1235 if (want_value)
1236 op.op = CPP_UMINUS;
1237 break;
Neil Booth60284a52002-04-28 23:14:56 +00001238
Neil Boothf8b954f2002-04-26 06:32:50 +00001239 default:
Neil Booth60284a52002-04-28 23:14:56 +00001240 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001241 SYNTAX_ERROR2_AT (op.loc,
1242 "token \"%s\" is not valid in preprocessor expressions",
1243 cpp_token_as_text (pfile, op.token));
Neil Boothf8b954f2002-04-26 06:32:50 +00001244 break;
Per Bothner7f2935c1995-03-16 13:59:07 -08001245 }
1246
Neil Booth87ed1092002-04-28 19:42:54 +00001247 /* Check we have a value or operator as appropriate. */
1248 if (optab[op.op].flags & NO_L_OPERAND)
Neil Booth4063b942000-04-02 08:27:23 +00001249 {
Neil Boothf8b954f2002-04-26 06:32:50 +00001250 if (!want_value)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001251 SYNTAX_ERROR2_AT (op.loc,
1252 "missing binary operator before token \"%s\"",
1253 cpp_token_as_text (pfile, op.token));
Neil Boothb22ef132000-04-03 22:33:12 +00001254 }
Neil Booth87ed1092002-04-28 19:42:54 +00001255 else if (want_value)
Neil Boothb22ef132000-04-03 22:33:12 +00001256 {
Neil Bootha09d4742004-07-04 12:57:50 +00001257 /* We want a number (or expression) and haven't got one.
1258 Try to emit a specific diagnostic. */
1259 if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001260 SYNTAX_ERROR_AT (op.loc,
1261 "missing expression between '(' and ')'");
Neil Bootha09d4742004-07-04 12:57:50 +00001262
1263 if (op.op == CPP_EOF && top->op == CPP_EOF)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001264 SYNTAX_ERROR2_AT (op.loc,
1265 "%s with no expression", is_if ? "#if" : "#elif");
Neil Bootha09d4742004-07-04 12:57:50 +00001266
1267 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001268 SYNTAX_ERROR2_AT (op.loc,
1269 "operator '%s' has no right operand",
1270 cpp_token_as_text (pfile, top->token));
Neil Bootha09d4742004-07-04 12:57:50 +00001271 else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF)
1272 /* Complain about missing paren during reduction. */;
1273 else
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001274 SYNTAX_ERROR2_AT (op.loc,
1275 "operator '%s' has no left operand",
1276 cpp_token_as_text (pfile, op.token));
Neil Booth4063b942000-04-02 08:27:23 +00001277 }
Neil Booth87ed1092002-04-28 19:42:54 +00001278
1279 top = reduce (pfile, top, op.op);
1280 if (!top)
1281 goto syntax_error;
1282
Neil Booth60284a52002-04-28 23:14:56 +00001283 if (op.op == CPP_EOF)
1284 break;
1285
Neil Booth87ed1092002-04-28 19:42:54 +00001286 switch (op.op)
1287 {
1288 case CPP_CLOSE_PAREN:
1289 continue;
Neil Booth87ed1092002-04-28 19:42:54 +00001290 case CPP_OR_OR:
Neil Booth91318902002-05-26 18:42:21 +00001291 if (!num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001292 pfile->state.skip_eval++;
1293 break;
1294 case CPP_AND_AND:
1295 case CPP_QUERY:
Neil Booth91318902002-05-26 18:42:21 +00001296 if (num_zerop (top->value))
Neil Booth87ed1092002-04-28 19:42:54 +00001297 pfile->state.skip_eval++;
1298 break;
1299 case CPP_COLON:
Neil Booth60284a52002-04-28 23:14:56 +00001300 if (top->op != CPP_QUERY)
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001301 SYNTAX_ERROR_AT (op.loc,
1302 " ':' without preceding '?'");
Neil Booth91318902002-05-26 18:42:21 +00001303 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
Neil Booth87ed1092002-04-28 19:42:54 +00001304 pfile->state.skip_eval++;
1305 else
1306 pfile->state.skip_eval--;
1307 default:
1308 break;
1309 }
1310
Neil Boothf8b954f2002-04-26 06:32:50 +00001311 want_value = true;
Neil Booth4063b942000-04-02 08:27:23 +00001312
Mike Stump0f413021996-07-03 22:07:53 +00001313 /* Check for and handle stack overflow. */
Neil Booth87ed1092002-04-28 19:42:54 +00001314 if (++top == pfile->op_limit)
1315 top = _cpp_expand_op_stack (pfile);
Kazu Hiratadf383482002-05-22 22:02:16 +00001316
Per Bothner7f2935c1995-03-16 13:59:07 -08001317 top->op = op.op;
Neil Booth68e652752002-07-20 13:31:56 +00001318 top->token = op.token;
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001319 top->loc = op.loc;
Per Bothner7f2935c1995-03-16 13:59:07 -08001320 }
Neil Booth9ee703132000-04-01 07:42:37 +00001321
Neil Booth6d18adb2001-07-29 17:27:57 +00001322 /* The controlling macro expression is only valid if we called lex 3
1323 times: <!> <defined expression> and <EOF>. push_conditional ()
1324 checks that we are at top-of-file. */
1325 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
1326 pfile->mi_ind_cmacro = 0;
1327
Neil Booth87ed1092002-04-28 19:42:54 +00001328 if (top != pfile->op_stack)
Neil Boothebef4e82002-04-14 18:42:47 +00001329 {
Dodji Seketeli0b2c4be2012-05-16 10:51:15 +00001330 cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0,
1331 "unbalanced stack in %s",
1332 is_if ? "#if" : "#elif");
Neil Booth4063b942000-04-02 08:27:23 +00001333 syntax_error:
Neil Booth87ed1092002-04-28 19:42:54 +00001334 return false; /* Return false on syntax error. */
Neil Booth4063b942000-04-02 08:27:23 +00001335 }
Neil Booth9ee703132000-04-01 07:42:37 +00001336
Neil Booth91318902002-05-26 18:42:21 +00001337 return !num_zerop (top->value);
Neil Booth87ed1092002-04-28 19:42:54 +00001338}
1339
1340/* Reduce the operator / value stack if possible, in preparation for
1341 pushing operator OP. Returns NULL on error, otherwise the top of
1342 the stack. */
1343static struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001344reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op)
Neil Booth87ed1092002-04-28 19:42:54 +00001345{
1346 unsigned int prio;
1347
Neil Booth91318902002-05-26 18:42:21 +00001348 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
1349 {
1350 bad_op:
John David Anglin0527bc42003-11-01 22:56:54 +00001351 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op);
Neil Booth91318902002-05-26 18:42:21 +00001352 return 0;
1353 }
1354
Neil Booth87ed1092002-04-28 19:42:54 +00001355 if (op == CPP_OPEN_PAREN)
1356 return top;
1357
1358 /* Decrement the priority of left-associative operators to force a
1359 reduction with operators of otherwise equal priority. */
1360 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
1361 while (prio < optab[top->op].prio)
1362 {
Neil Booth68e652752002-07-20 13:31:56 +00001363 if (CPP_OPTION (pfile, warn_num_sign_change)
1364 && optab[top->op].flags & CHECK_PROMOTION)
1365 check_promotion (pfile, top);
1366
Neil Booth75aef482002-07-19 19:24:43 +00001367 switch (top->op)
1368 {
1369 case CPP_UPLUS:
1370 case CPP_UMINUS:
1371 case CPP_NOT:
1372 case CPP_COMPL:
1373 top[-1].value = num_unary_op (pfile, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001374 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001375 break;
Neil Booth91318902002-05-26 18:42:21 +00001376
Neil Booth75aef482002-07-19 19:24:43 +00001377 case CPP_PLUS:
1378 case CPP_MINUS:
1379 case CPP_RSHIFT:
1380 case CPP_LSHIFT:
Neil Booth75aef482002-07-19 19:24:43 +00001381 case CPP_COMMA:
1382 top[-1].value = num_binary_op (pfile, top[-1].value,
1383 top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001384 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001385 break;
Neil Booth91318902002-05-26 18:42:21 +00001386
Neil Booth75aef482002-07-19 19:24:43 +00001387 case CPP_GREATER:
1388 case CPP_LESS:
1389 case CPP_GREATER_EQ:
1390 case CPP_LESS_EQ:
1391 top[-1].value
1392 = num_inequality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001393 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001394 break;
Neil Booth91318902002-05-26 18:42:21 +00001395
Neil Booth75aef482002-07-19 19:24:43 +00001396 case CPP_EQ_EQ:
1397 case CPP_NOT_EQ:
1398 top[-1].value
1399 = num_equality_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001400 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001401 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001402
Neil Booth75aef482002-07-19 19:24:43 +00001403 case CPP_AND:
1404 case CPP_OR:
1405 case CPP_XOR:
1406 top[-1].value
1407 = num_bitwise_op (pfile, top[-1].value, top->value, top->op);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001408 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001409 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001410
Neil Booth75aef482002-07-19 19:24:43 +00001411 case CPP_MULT:
1412 top[-1].value = num_mul (pfile, top[-1].value, top->value);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001413 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001414 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001415
Neil Booth75aef482002-07-19 19:24:43 +00001416 case CPP_DIV:
1417 case CPP_MOD:
1418 top[-1].value = num_div_op (pfile, top[-1].value,
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001419 top->value, top->op, top->loc);
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001420 top[-1].loc = top->loc;
Neil Booth75aef482002-07-19 19:24:43 +00001421 break;
Neil Boothad28cff2002-07-18 22:08:35 +00001422
Neil Booth75aef482002-07-19 19:24:43 +00001423 case CPP_OR_OR:
1424 top--;
1425 if (!num_zerop (top->value))
1426 pfile->state.skip_eval--;
1427 top->value.low = (!num_zerop (top->value)
1428 || !num_zerop (top[1].value));
1429 top->value.high = 0;
1430 top->value.unsignedp = false;
1431 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001432 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001433 continue;
1434
1435 case CPP_AND_AND:
1436 top--;
1437 if (num_zerop (top->value))
1438 pfile->state.skip_eval--;
1439 top->value.low = (!num_zerop (top->value)
1440 && !num_zerop (top[1].value));
1441 top->value.high = 0;
1442 top->value.unsignedp = false;
1443 top->value.overflow = false;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001444 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001445 continue;
1446
1447 case CPP_OPEN_PAREN:
1448 if (op != CPP_CLOSE_PAREN)
1449 {
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001450 cpp_error_with_line (pfile, CPP_DL_ERROR,
1451 top->token->src_loc,
1452 0, "missing ')' in expression");
Neil Booth75aef482002-07-19 19:24:43 +00001453 return 0;
1454 }
1455 top--;
1456 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001457 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001458 return top;
1459
1460 case CPP_COLON:
1461 top -= 2;
1462 if (!num_zerop (top->value))
1463 {
Neil Booth91318902002-05-26 18:42:21 +00001464 pfile->state.skip_eval--;
Neil Booth75aef482002-07-19 19:24:43 +00001465 top->value = top[1].value;
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001466 top->loc = top[1].loc;
Neil Booth75aef482002-07-19 19:24:43 +00001467 }
1468 else
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001469 {
1470 top->value = top[2].value;
1471 top->loc = top[2].loc;
1472 }
Neil Booth75aef482002-07-19 19:24:43 +00001473 top->value.unsignedp = (top[1].value.unsignedp
1474 || top[2].value.unsignedp);
1475 continue;
Neil Booth91318902002-05-26 18:42:21 +00001476
Neil Booth75aef482002-07-19 19:24:43 +00001477 case CPP_QUERY:
Tom Tromey71c10032008-05-06 17:15:07 +00001478 /* COMMA and COLON should not reduce a QUERY operator. */
1479 if (op == CPP_COMMA || op == CPP_COLON)
1480 return top;
John David Anglin0527bc42003-11-01 22:56:54 +00001481 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'");
Neil Booth75aef482002-07-19 19:24:43 +00001482 return 0;
Neil Booth91318902002-05-26 18:42:21 +00001483
Neil Booth75aef482002-07-19 19:24:43 +00001484 default:
1485 goto bad_op;
1486 }
Neil Boothad28cff2002-07-18 22:08:35 +00001487
1488 top--;
Neil Booth91318902002-05-26 18:42:21 +00001489 if (top->value.overflow && !pfile->state.skip_eval)
John David Anglin0527bc42003-11-01 22:56:54 +00001490 cpp_error (pfile, CPP_DL_PEDWARN,
Neil Booth91318902002-05-26 18:42:21 +00001491 "integer overflow in preprocessor expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001492 }
1493
1494 if (op == CPP_CLOSE_PAREN)
1495 {
John David Anglin0527bc42003-11-01 22:56:54 +00001496 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression");
Neil Booth87ed1092002-04-28 19:42:54 +00001497 return 0;
1498 }
1499
1500 return top;
1501}
1502
1503/* Returns the position of the old top of stack after expansion. */
1504struct op *
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001505_cpp_expand_op_stack (cpp_reader *pfile)
Neil Booth87ed1092002-04-28 19:42:54 +00001506{
Neil Booth32fa4562002-05-09 22:27:31 +00001507 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
1508 size_t new_size = old_size * 2 + 20;
Neil Booth87ed1092002-04-28 19:42:54 +00001509
Gabriel Dos Reisc3f829c2005-05-28 15:52:48 +00001510 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size);
Neil Booth32fa4562002-05-09 22:27:31 +00001511 pfile->op_limit = pfile->op_stack + new_size;
Neil Booth87ed1092002-04-28 19:42:54 +00001512
Neil Booth32fa4562002-05-09 22:27:31 +00001513 return pfile->op_stack + old_size;
Per Bothner7f2935c1995-03-16 13:59:07 -08001514}
Neil Booth91318902002-05-26 18:42:21 +00001515
Neil Booth68e652752002-07-20 13:31:56 +00001516/* Emits a warning if the effective sign of either operand of OP
1517 changes because of integer promotions. */
1518static void
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001519check_promotion (cpp_reader *pfile, const struct op *op)
Neil Booth68e652752002-07-20 13:31:56 +00001520{
1521 if (op->value.unsignedp == op[-1].value.unsignedp)
1522 return;
1523
1524 if (op->value.unsignedp)
1525 {
1526 if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001527 cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0,
1528 "the left operand of \"%s\" changes sign when promoted",
1529 cpp_token_as_text (pfile, op->token));
Neil Booth68e652752002-07-20 13:31:56 +00001530 }
1531 else if (!num_positive (op->value, CPP_OPTION (pfile, precision)))
Manuel López-Ibáñez47960aa2008-10-31 22:00:37 +00001532 cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0,
Neil Booth68e652752002-07-20 13:31:56 +00001533 "the right operand of \"%s\" changes sign when promoted",
1534 cpp_token_as_text (pfile, op->token));
1535}
1536
Neil Booth91318902002-05-26 18:42:21 +00001537/* Clears the unused high order bits of the number pointed to by PNUM. */
1538static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001539num_trim (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001540{
1541 if (precision > PART_PRECISION)
1542 {
1543 precision -= PART_PRECISION;
1544 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001545 num.high &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001546 }
1547 else
1548 {
1549 if (precision < PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00001550 num.low &= ((cpp_num_part) 1 << precision) - 1;
Neil Booth91318902002-05-26 18:42:21 +00001551 num.high = 0;
1552 }
1553
1554 return num;
1555}
1556
1557/* True iff A (presumed signed) >= 0. */
1558static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001559num_positive (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001560{
1561 if (precision > PART_PRECISION)
1562 {
1563 precision -= PART_PRECISION;
Neil Booth359b0be2002-05-28 05:44:06 +00001564 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001565 }
1566
Neil Booth359b0be2002-05-28 05:44:06 +00001567 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
Neil Booth91318902002-05-26 18:42:21 +00001568}
1569
Neil Boothceeedfc2002-06-02 19:37:34 +00001570/* Sign extend a number, with PRECISION significant bits and all
1571 others assumed clear, to fill out a cpp_num structure. */
1572cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001573cpp_num_sign_extend (cpp_num num, size_t precision)
Neil Boothceeedfc2002-06-02 19:37:34 +00001574{
1575 if (!num.unsignedp)
1576 {
1577 if (precision > PART_PRECISION)
1578 {
1579 precision -= PART_PRECISION;
1580 if (precision < PART_PRECISION
1581 && (num.high & (cpp_num_part) 1 << (precision - 1)))
1582 num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1583 }
1584 else if (num.low & (cpp_num_part) 1 << (precision - 1))
1585 {
1586 if (precision < PART_PRECISION)
1587 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision));
1588 num.high = ~(cpp_num_part) 0;
1589 }
1590 }
1591
1592 return num;
1593}
1594
Neil Booth91318902002-05-26 18:42:21 +00001595/* Returns the negative of NUM. */
1596static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001597num_negate (cpp_num num, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001598{
1599 cpp_num copy;
1600
1601 copy = num;
1602 num.high = ~num.high;
1603 num.low = ~num.low;
1604 if (++num.low == 0)
1605 num.high++;
1606 num = num_trim (num, precision);
1607 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1608
1609 return num;
1610}
1611
1612/* Returns true if A >= B. */
1613static bool
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001614num_greater_eq (cpp_num pa, cpp_num pb, size_t precision)
Neil Booth91318902002-05-26 18:42:21 +00001615{
1616 bool unsignedp;
1617
1618 unsignedp = pa.unsignedp || pb.unsignedp;
1619
1620 if (!unsignedp)
1621 {
1622 /* Both numbers have signed type. If they are of different
1623 sign, the answer is the sign of A. */
1624 unsignedp = num_positive (pa, precision);
1625
1626 if (unsignedp != num_positive (pb, precision))
1627 return unsignedp;
1628
1629 /* Otherwise we can do an unsigned comparison. */
1630 }
1631
1632 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1633}
1634
1635/* Returns LHS OP RHS, where OP is a bit-wise operation. */
1636static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001637num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1638 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001639{
1640 lhs.overflow = false;
1641 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1642
1643 /* As excess precision is zeroed, there is no need to num_trim () as
1644 these operations cannot introduce a set bit there. */
1645 if (op == CPP_AND)
1646 {
1647 lhs.low &= rhs.low;
1648 lhs.high &= rhs.high;
1649 }
1650 else if (op == CPP_OR)
1651 {
1652 lhs.low |= rhs.low;
1653 lhs.high |= rhs.high;
1654 }
1655 else
1656 {
1657 lhs.low ^= rhs.low;
1658 lhs.high ^= rhs.high;
1659 }
1660
1661 return lhs;
1662}
1663
1664/* Returns LHS OP RHS, where OP is an inequality. */
1665static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001666num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs,
1667 enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001668{
1669 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1670
1671 if (op == CPP_GREATER_EQ)
1672 lhs.low = gte;
1673 else if (op == CPP_LESS)
1674 lhs.low = !gte;
1675 else if (op == CPP_GREATER)
1676 lhs.low = gte && !num_eq (lhs, rhs);
1677 else /* CPP_LESS_EQ. */
1678 lhs.low = !gte || num_eq (lhs, rhs);
1679
1680 lhs.high = 0;
1681 lhs.overflow = false;
1682 lhs.unsignedp = false;
1683 return lhs;
1684}
1685
1686/* Returns LHS OP RHS, where OP is == or !=. */
1687static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001688num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED,
1689 cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001690{
Jason Merrill97459792002-06-07 09:29:17 -04001691 /* Work around a 3.0.4 bug; see PR 6950. */
1692 bool eq = num_eq (lhs, rhs);
Neil Booth91318902002-05-26 18:42:21 +00001693 if (op == CPP_NOT_EQ)
Jason Merrill97459792002-06-07 09:29:17 -04001694 eq = !eq;
1695 lhs.low = eq;
Neil Booth91318902002-05-26 18:42:21 +00001696 lhs.high = 0;
1697 lhs.overflow = false;
1698 lhs.unsignedp = false;
1699 return lhs;
1700}
1701
1702/* Shift NUM, of width PRECISION, right by N bits. */
1703static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001704num_rshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001705{
1706 cpp_num_part sign_mask;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001707 bool x = num_positive (num, precision);
Neil Booth91318902002-05-26 18:42:21 +00001708
Diego Novillo6de9cd92004-05-13 02:41:07 -04001709 if (num.unsignedp || x)
Neil Booth91318902002-05-26 18:42:21 +00001710 sign_mask = 0;
1711 else
1712 sign_mask = ~(cpp_num_part) 0;
1713
1714 if (n >= precision)
1715 num.high = num.low = sign_mask;
1716 else
1717 {
1718 /* Sign-extend. */
1719 if (precision < PART_PRECISION)
1720 num.high = sign_mask, num.low |= sign_mask << precision;
1721 else if (precision < 2 * PART_PRECISION)
1722 num.high |= sign_mask << (precision - PART_PRECISION);
1723
1724 if (n >= PART_PRECISION)
1725 {
1726 n -= PART_PRECISION;
1727 num.low = num.high;
1728 num.high = sign_mask;
1729 }
1730
1731 if (n)
1732 {
1733 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1734 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1735 }
1736 }
1737
1738 num = num_trim (num, precision);
1739 num.overflow = false;
1740 return num;
1741}
1742
1743/* Shift NUM, of width PRECISION, left by N bits. */
1744static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001745num_lshift (cpp_num num, size_t precision, size_t n)
Neil Booth91318902002-05-26 18:42:21 +00001746{
1747 if (n >= precision)
1748 {
1749 num.overflow = !num.unsignedp && !num_zerop (num);
1750 num.high = num.low = 0;
1751 }
1752 else
1753 {
1754 cpp_num orig, maybe_orig;
1755 size_t m = n;
1756
1757 orig = num;
1758 if (m >= PART_PRECISION)
1759 {
1760 m -= PART_PRECISION;
1761 num.high = num.low;
1762 num.low = 0;
1763 }
1764 if (m)
1765 {
1766 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1767 num.low <<= m;
1768 }
1769 num = num_trim (num, precision);
1770
1771 if (num.unsignedp)
1772 num.overflow = false;
1773 else
1774 {
1775 maybe_orig = num_rshift (num, precision, n);
1776 num.overflow = !num_eq (orig, maybe_orig);
1777 }
1778 }
1779
1780 return num;
1781}
1782
1783/* The four unary operators: +, -, ! and ~. */
1784static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001785num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001786{
1787 switch (op)
1788 {
1789 case CPP_UPLUS:
Neil Booth75aef482002-07-19 19:24:43 +00001790 if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval)
Simon Baldwin87cf0652010-04-07 17:18:10 +00001791 cpp_warning (pfile, CPP_W_TRADITIONAL,
1792 "traditional C rejects the unary plus operator");
Neil Booth91318902002-05-26 18:42:21 +00001793 num.overflow = false;
1794 break;
1795
1796 case CPP_UMINUS:
1797 num = num_negate (num, CPP_OPTION (pfile, precision));
1798 break;
1799
1800 case CPP_COMPL:
1801 num.high = ~num.high;
1802 num.low = ~num.low;
1803 num = num_trim (num, CPP_OPTION (pfile, precision));
1804 num.overflow = false;
1805 break;
1806
1807 default: /* case CPP_NOT: */
1808 num.low = num_zerop (num);
1809 num.high = 0;
1810 num.overflow = false;
1811 num.unsignedp = false;
1812 break;
1813 }
1814
1815 return num;
1816}
1817
1818/* The various binary operators. */
1819static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001820num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op)
Neil Booth91318902002-05-26 18:42:21 +00001821{
1822 cpp_num result;
1823 size_t precision = CPP_OPTION (pfile, precision);
Neil Booth91318902002-05-26 18:42:21 +00001824 size_t n;
1825
1826 switch (op)
1827 {
1828 /* Shifts. */
1829 case CPP_LSHIFT:
1830 case CPP_RSHIFT:
1831 if (!rhs.unsignedp && !num_positive (rhs, precision))
1832 {
1833 /* A negative shift is a positive shift the other way. */
1834 if (op == CPP_LSHIFT)
1835 op = CPP_RSHIFT;
1836 else
1837 op = CPP_LSHIFT;
1838 rhs = num_negate (rhs, precision);
1839 }
1840 if (rhs.high)
1841 n = ~0; /* Maximal. */
1842 else
1843 n = rhs.low;
1844 if (op == CPP_LSHIFT)
1845 lhs = num_lshift (lhs, precision, n);
1846 else
1847 lhs = num_rshift (lhs, precision, n);
1848 break;
1849
Neil Booth91318902002-05-26 18:42:21 +00001850 /* Arithmetic. */
1851 case CPP_MINUS:
Joseph Myers3a4efce2013-12-10 01:23:37 +00001852 result.low = lhs.low - rhs.low;
1853 result.high = lhs.high - rhs.high;
1854 if (result.low > lhs.low)
1855 result.high--;
1856 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1857 result.overflow = false;
1858
1859 result = num_trim (result, precision);
1860 if (!result.unsignedp)
1861 {
1862 bool lhsp = num_positive (lhs, precision);
1863 result.overflow = (lhsp != num_positive (rhs, precision)
1864 && lhsp != num_positive (result, precision));
1865 }
1866 return result;
1867
Neil Booth91318902002-05-26 18:42:21 +00001868 case CPP_PLUS:
1869 result.low = lhs.low + rhs.low;
1870 result.high = lhs.high + rhs.high;
1871 if (result.low < lhs.low)
1872 result.high++;
Diego Novillo6de9cd92004-05-13 02:41:07 -04001873 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1874 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001875
1876 result = num_trim (result, precision);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001877 if (!result.unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00001878 {
1879 bool lhsp = num_positive (lhs, precision);
1880 result.overflow = (lhsp == num_positive (rhs, precision)
1881 && lhsp != num_positive (result, precision));
1882 }
1883 return result;
1884
1885 /* Comma. */
1886 default: /* case CPP_COMMA: */
Joseph Myers32e8aa92004-02-11 23:50:45 +00001887 if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
1888 || !pfile->state.skip_eval))
Manuel López-Ibáñez2b71f4a2014-09-04 15:13:40 +00001889 cpp_pedwarning (pfile, CPP_W_PEDANTIC,
1890 "comma operator in operand of #if");
Neil Booth91318902002-05-26 18:42:21 +00001891 lhs = rhs;
1892 break;
1893 }
1894
1895 return lhs;
1896}
1897
1898/* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1899 cannot overflow. */
1900static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001901num_part_mul (cpp_num_part lhs, cpp_num_part rhs)
Neil Booth91318902002-05-26 18:42:21 +00001902{
1903 cpp_num result;
1904 cpp_num_part middle[2], temp;
1905
1906 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1907 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1908
1909 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1910 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1911
1912 temp = result.low;
1913 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1914 if (result.low < temp)
1915 result.high++;
1916
1917 temp = result.low;
1918 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1919 if (result.low < temp)
1920 result.high++;
1921
1922 result.high += HIGH_PART (middle[0]);
1923 result.high += HIGH_PART (middle[1]);
Diego Novillo6de9cd92004-05-13 02:41:07 -04001924 result.unsignedp = true;
1925 result.overflow = false;
Neil Booth91318902002-05-26 18:42:21 +00001926
1927 return result;
1928}
1929
1930/* Multiply two preprocessing numbers. */
1931static cpp_num
Zack Weinberg6cf87ca2003-06-17 06:17:44 +00001932num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs)
Neil Booth91318902002-05-26 18:42:21 +00001933{
1934 cpp_num result, temp;
1935 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1936 bool overflow, negate = false;
1937 size_t precision = CPP_OPTION (pfile, precision);
1938
1939 /* Prepare for unsigned multiplication. */
1940 if (!unsignedp)
1941 {
1942 if (!num_positive (lhs, precision))
1943 negate = !negate, lhs = num_negate (lhs, precision);
1944 if (!num_positive (rhs, precision))
1945 negate = !negate, rhs = num_negate (rhs, precision);
1946 }
1947
1948 overflow = lhs.high && rhs.high;
1949 result = num_part_mul (lhs.low, rhs.low);
1950
1951 temp = num_part_mul (lhs.high, rhs.low);
1952 result.high += temp.low;
1953 if (temp.high)
1954 overflow = true;
1955
1956 temp = num_part_mul (lhs.low, rhs.high);
1957 result.high += temp.low;
1958 if (temp.high)
1959 overflow = true;
1960
1961 temp.low = result.low, temp.high = result.high;
1962 result = num_trim (result, precision);
1963 if (!num_eq (result, temp))
1964 overflow = true;
1965
1966 if (negate)
1967 result = num_negate (result, precision);
1968
1969 if (unsignedp)
1970 result.overflow = false;
1971 else
1972 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1973 && !num_zerop (result));
1974 result.unsignedp = unsignedp;
1975
1976 return result;
1977}
1978
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001979/* Divide two preprocessing numbers, LHS and RHS, returning the answer
1980 or the remainder depending upon OP. LOCATION is the source location
1981 of this operator (for diagnostics). */
1982
Neil Booth91318902002-05-26 18:42:21 +00001983static cpp_num
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00001984num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op,
1985 source_location location)
Neil Booth91318902002-05-26 18:42:21 +00001986{
1987 cpp_num result, sub;
1988 cpp_num_part mask;
1989 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1990 bool negate = false, lhs_neg = false;
1991 size_t i, precision = CPP_OPTION (pfile, precision);
1992
1993 /* Prepare for unsigned division. */
1994 if (!unsignedp)
1995 {
1996 if (!num_positive (lhs, precision))
1997 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1998 if (!num_positive (rhs, precision))
1999 negate = !negate, rhs = num_negate (rhs, precision);
2000 }
2001
2002 /* Find the high bit. */
2003 if (rhs.high)
2004 {
2005 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00002006 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00002007 for (; ; i--, mask >>= 1)
2008 if (rhs.high & mask)
2009 break;
2010 }
2011 else if (rhs.low)
2012 {
2013 if (precision > PART_PRECISION)
2014 i = precision - PART_PRECISION - 1;
2015 else
2016 i = precision - 1;
Neil Booth359b0be2002-05-28 05:44:06 +00002017 mask = (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00002018 for (; ; i--, mask >>= 1)
2019 if (rhs.low & mask)
2020 break;
2021 }
2022 else
2023 {
Neil Booth75aef482002-07-19 19:24:43 +00002024 if (!pfile->state.skip_eval)
Manuel López-Ibáñezb506a5a2009-06-18 15:10:23 +00002025 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
2026 "division by zero in #if");
Neil Booth91318902002-05-26 18:42:21 +00002027 return lhs;
2028 }
2029
Kazu Hiratada7d8302002-09-22 02:03:17 +00002030 /* First nonzero bit of RHS is bit I. Do naive division by
Neil Booth91318902002-05-26 18:42:21 +00002031 shifting the RHS fully left, and subtracting from LHS if LHS is
2032 at least as big, and then repeating but with one less shift.
2033 This is not very efficient, but is easy to understand. */
2034
2035 rhs.unsignedp = true;
2036 lhs.unsignedp = true;
2037 i = precision - i - 1;
2038 sub = num_lshift (rhs, precision, i);
2039
2040 result.high = result.low = 0;
2041 for (;;)
2042 {
2043 if (num_greater_eq (lhs, sub, precision))
2044 {
2045 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
2046 if (i >= PART_PRECISION)
Neil Booth359b0be2002-05-28 05:44:06 +00002047 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
Neil Booth91318902002-05-26 18:42:21 +00002048 else
Neil Booth359b0be2002-05-28 05:44:06 +00002049 result.low |= (cpp_num_part) 1 << i;
Neil Booth91318902002-05-26 18:42:21 +00002050 }
2051 if (i-- == 0)
2052 break;
2053 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
2054 sub.high >>= 1;
2055 }
2056
2057 /* We divide so that the remainder has the sign of the LHS. */
2058 if (op == CPP_DIV)
2059 {
2060 result.unsignedp = unsignedp;
Diego Novillo6de9cd92004-05-13 02:41:07 -04002061 result.overflow = false;
2062 if (!unsignedp)
Neil Booth91318902002-05-26 18:42:21 +00002063 {
2064 if (negate)
2065 result = num_negate (result, precision);
Eric Christopher22a8a522007-05-02 21:57:50 +00002066 result.overflow = (num_positive (result, precision) ^ !negate
2067 && !num_zerop (result));
Neil Booth91318902002-05-26 18:42:21 +00002068 }
2069
2070 return result;
2071 }
2072
2073 /* CPP_MOD. */
2074 lhs.unsignedp = unsignedp;
2075 lhs.overflow = false;
2076 if (lhs_neg)
2077 lhs = num_negate (lhs, precision);
2078
2079 return lhs;
2080}
Edward Smith-Rowlanda15f7cb2014-10-01 11:49:23 +00002081
2082/* Handle meeting "__has_include__" in a preprocessor expression. */
2083static cpp_num
2084parse_has_include (cpp_reader *pfile, enum include_type type)
2085{
2086 cpp_num result;
2087 bool paren = false;
2088 cpp_hashnode *node = 0;
2089 const cpp_token *token;
2090 bool bracket = false;
2091 char *fname = 0;
2092
2093 result.unsignedp = false;
2094 result.high = 0;
2095 result.overflow = false;
2096 result.low = 0;
2097
2098 pfile->state.in__has_include__++;
2099
2100 token = cpp_get_token (pfile);
2101 if (token->type == CPP_OPEN_PAREN)
2102 {
2103 paren = true;
2104 token = cpp_get_token (pfile);
2105 }
2106
2107 if (token->type == CPP_STRING || token->type == CPP_HEADER_NAME)
2108 {
2109 if (token->type == CPP_HEADER_NAME)
2110 bracket = true;
2111 fname = XNEWVEC (char, token->val.str.len - 1);
2112 memcpy (fname, token->val.str.text + 1, token->val.str.len - 2);
2113 fname[token->val.str.len - 2] = '\0';
2114 node = token->val.node.node;
2115 }
2116 else if (token->type == CPP_LESS)
2117 {
2118 bracket = true;
2119 fname = _cpp_bracket_include (pfile);
2120 }
2121 else
2122 cpp_error (pfile, CPP_DL_ERROR,
2123 "operator \"__has_include__\" requires a header string");
2124
2125 if (fname)
2126 {
2127 int angle_brackets = (bracket ? 1 : 0);
2128
2129 if (_cpp_has_header (pfile, fname, angle_brackets, type))
2130 result.low = 1;
2131 else
2132 result.low = 0;
2133
2134 XDELETEVEC (fname);
2135 }
2136
2137 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
2138 cpp_error (pfile, CPP_DL_ERROR,
2139 "missing ')' after \"__has_include__\"");
2140
2141 /* A possible controlling macro of the form #if !__has_include__ ().
2142 _cpp_parse_expr checks there was no other junk on the line. */
2143 if (node)
2144 pfile->mi_ind_cmacro = node;
2145
2146 pfile->state.in__has_include__--;
2147
2148 return result;
2149}