]>
Commit | Line | Data |
---|---|---|
b0699dad | 1 | /* Parse C expressions for cpplib. |
5d8ebbd8 | 2 | Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001, |
3ce4f9e4 | 3 | 2002, 2004, 2008, 2009, 2010, 2011 Free Software Foundation. |
e38992e8 | 4 | Contributed by Per Bothner, 1994. |
7f2935c7 PB |
5 | |
6 | This program is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
748086b7 | 8 | Free Software Foundation; either version 3, or (at your option) any |
7f2935c7 PB |
9 | later version. |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
748086b7 JJ |
17 | along with this program; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
7f2935c7 | 19 | |
7f2935c7 | 20 | #include "config.h" |
b04cd507 | 21 | #include "system.h" |
487a6e06 | 22 | #include "cpplib.h" |
4f4e53dd | 23 | #include "internal.h" |
7f2935c7 | 24 | |
91318908 NB |
25 | #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT) |
26 | #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2)) | |
27 | #define LOW_PART(num_part) (num_part & HALF_MASK) | |
28 | #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2)) | |
29 | ||
cf00a885 | 30 | struct op |
b0699dad | 31 | { |
68e65275 | 32 | const cpp_token *token; /* The token forming op (for diagnostics). */ |
ad28cff7 | 33 | cpp_num value; /* The value logically "right" of op. */ |
47960aaf | 34 | source_location loc; /* The location of this value. */ |
cf00a885 | 35 | enum cpp_ttype op; |
7f2935c7 | 36 | }; |
7f2935c7 | 37 | |
91318908 NB |
38 | /* Some simple utility routines on double integers. */ |
39 | #define num_zerop(num) ((num.low | num.high) == 0) | |
40 | #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high) | |
6cf87ca4 ZW |
41 | static bool num_positive (cpp_num, size_t); |
42 | static bool num_greater_eq (cpp_num, cpp_num, size_t); | |
43 | static cpp_num num_trim (cpp_num, size_t); | |
44 | static cpp_num num_part_mul (cpp_num_part, cpp_num_part); | |
45 | ||
46 | static cpp_num num_unary_op (cpp_reader *, cpp_num, enum cpp_ttype); | |
47 | static cpp_num num_binary_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); | |
48 | static cpp_num num_negate (cpp_num, size_t); | |
49 | static cpp_num num_bitwise_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype); | |
50 | static cpp_num num_inequality_op (cpp_reader *, cpp_num, cpp_num, | |
51 | enum cpp_ttype); | |
52 | static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, | |
53 | enum cpp_ttype); | |
54 | static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num); | |
b506a5a2 MLI |
55 | static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype, |
56 | source_location); | |
6cf87ca4 ZW |
57 | static cpp_num num_lshift (cpp_num, size_t, size_t); |
58 | static cpp_num num_rshift (cpp_num, size_t, size_t); | |
59 | ||
60 | static cpp_num append_digit (cpp_num, int, int, size_t); | |
61 | static cpp_num parse_defined (cpp_reader *); | |
0b2c4be5 | 62 | static cpp_num eval_token (cpp_reader *, const cpp_token *, source_location); |
6cf87ca4 | 63 | static struct op *reduce (cpp_reader *, struct op *, enum cpp_ttype); |
a4a0016d ESR |
64 | static unsigned int interpret_float_suffix (cpp_reader *, const uchar *, size_t); |
65 | static unsigned int interpret_int_suffix (cpp_reader *, const uchar *, size_t); | |
6cf87ca4 | 66 | static void check_promotion (cpp_reader *, const struct op *); |
91318908 | 67 | |
cd7ab83f | 68 | /* Token type abuse to create unary plus and minus operators. */ |
c3f829c1 GDR |
69 | #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1)) |
70 | #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2)) | |
cf00a885 | 71 | |
15dad1d9 ZW |
72 | /* With -O2, gcc appears to produce nice code, moving the error |
73 | message load and subsequent jump completely out of the main path. */ | |
15dad1d9 | 74 | #define SYNTAX_ERROR(msgid) \ |
0527bc4e | 75 | do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0) |
15dad1d9 | 76 | #define SYNTAX_ERROR2(msgid, arg) \ |
0527bc4e JDA |
77 | do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \ |
78 | while(0) | |
0b2c4be5 DS |
79 | #define SYNTAX_ERROR_AT(loc, msgid) \ |
80 | do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid); goto syntax_error; } \ | |
81 | while(0) | |
82 | #define SYNTAX_ERROR2_AT(loc, msgid, arg) \ | |
83 | do { cpp_error_with_line (pfile, CPP_DL_ERROR, (loc), 0, msgid, arg); goto syntax_error; } \ | |
84 | while(0) | |
15dad1d9 | 85 | |
cd7ab83f NB |
86 | /* Subroutine of cpp_classify_number. S points to a float suffix of |
87 | length LEN, possibly zero. Returns 0 for an invalid suffix, or a | |
88 | flag vector describing the suffix. */ | |
89 | static unsigned int | |
a4a0016d | 90 | interpret_float_suffix (cpp_reader *pfile, const uchar *s, size_t len) |
cf00a885 | 91 | { |
eec49116 | 92 | size_t flags; |
839a3b8a | 93 | size_t f, d, l, w, q, i; |
c77cd3d1 | 94 | |
eec49116 | 95 | flags = 0; |
839a3b8a | 96 | f = d = l = w = q = i = 0; |
cf00a885 | 97 | |
eec49116 JJ |
98 | /* Process decimal float suffixes, which are two letters starting |
99 | with d or D. Order and case are significant. */ | |
100 | if (len == 2 && (*s == 'd' || *s == 'D')) | |
101 | { | |
102 | bool uppercase = (*s == 'D'); | |
103 | switch (s[1]) | |
cd7ab83f | 104 | { |
eec49116 JJ |
105 | case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break; |
106 | case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break; | |
107 | case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break; | |
108 | case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break; | |
109 | case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; | |
110 | case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; | |
cd7ab83f | 111 | default: |
839a3b8a JJ |
112 | /* Additional two-character suffixes beginning with D are not |
113 | for decimal float constants. */ | |
114 | break; | |
cd7ab83f | 115 | } |
eec49116 | 116 | } |
cf00a885 | 117 | |
a4a0016d | 118 | if (CPP_OPTION (pfile, ext_numeric_literals)) |
ac6b1c67 | 119 | { |
a4a0016d ESR |
120 | /* Recognize a fixed-point suffix. */ |
121 | if (len != 0) | |
122 | switch (s[len-1]) | |
123 | { | |
124 | case 'k': case 'K': flags = CPP_N_ACCUM; break; | |
125 | case 'r': case 'R': flags = CPP_N_FRACT; break; | |
126 | default: break; | |
127 | } | |
128 | ||
129 | /* Continue processing a fixed-point suffix. The suffix is case | |
130 | insensitive except for ll or LL. Order is significant. */ | |
131 | if (flags) | |
eec49116 | 132 | { |
eec49116 JJ |
133 | if (len == 1) |
134 | return flags; | |
135 | len--; | |
eec49116 | 136 | |
a4a0016d ESR |
137 | if (*s == 'u' || *s == 'U') |
138 | { | |
139 | flags |= CPP_N_UNSIGNED; | |
140 | if (len == 1) | |
141 | return flags; | |
142 | len--; | |
143 | s++; | |
144 | } | |
145 | ||
146 | switch (*s) | |
147 | { | |
148 | case 'h': case 'H': | |
149 | if (len == 1) | |
150 | return flags |= CPP_N_SMALL; | |
151 | break; | |
152 | case 'l': | |
153 | if (len == 1) | |
154 | return flags |= CPP_N_MEDIUM; | |
155 | if (len == 2 && s[1] == 'l') | |
156 | return flags |= CPP_N_LARGE; | |
157 | break; | |
158 | case 'L': | |
159 | if (len == 1) | |
160 | return flags |= CPP_N_MEDIUM; | |
161 | if (len == 2 && s[1] == 'L') | |
162 | return flags |= CPP_N_LARGE; | |
163 | break; | |
164 | default: | |
165 | break; | |
166 | } | |
167 | /* Anything left at this point is invalid. */ | |
168 | return 0; | |
169 | } | |
ac6b1c67 CF |
170 | } |
171 | ||
eec49116 JJ |
172 | /* In any remaining valid suffix, the case and order don't matter. */ |
173 | while (len--) | |
174 | switch (s[len]) | |
175 | { | |
176 | case 'f': case 'F': f++; break; | |
839a3b8a | 177 | case 'd': case 'D': d++; break; |
eec49116 JJ |
178 | case 'l': case 'L': l++; break; |
179 | case 'w': case 'W': w++; break; | |
180 | case 'q': case 'Q': q++; break; | |
181 | case 'i': case 'I': | |
182 | case 'j': case 'J': i++; break; | |
183 | default: | |
184 | return 0; | |
185 | } | |
7f2935c7 | 186 | |
839a3b8a | 187 | if (f + d + l + w + q > 1 || i > 1) |
ad6ed77e JG |
188 | return 0; |
189 | ||
a4a0016d ESR |
190 | if (i && !CPP_OPTION (pfile, ext_numeric_literals)) |
191 | return 0; | |
192 | ||
193 | if ((w || q) && !CPP_OPTION (pfile, ext_numeric_literals)) | |
194 | return 0; | |
195 | ||
cd7ab83f NB |
196 | return ((i ? CPP_N_IMAGINARY : 0) |
197 | | (f ? CPP_N_SMALL : | |
839a3b8a | 198 | d ? CPP_N_MEDIUM : |
c77cd3d1 UB |
199 | l ? CPP_N_LARGE : |
200 | w ? CPP_N_MD_W : | |
839a3b8a | 201 | q ? CPP_N_MD_Q : CPP_N_DEFAULT)); |
cd7ab83f NB |
202 | } |
203 | ||
3ce4f9e4 ESR |
204 | /* Return the classification flags for a float suffix. */ |
205 | unsigned int | |
a4a0016d | 206 | cpp_interpret_float_suffix (cpp_reader *pfile, const char *s, size_t len) |
3ce4f9e4 | 207 | { |
a4a0016d | 208 | return interpret_float_suffix (pfile, (const unsigned char *)s, len); |
3ce4f9e4 ESR |
209 | } |
210 | ||
cd7ab83f NB |
211 | /* Subroutine of cpp_classify_number. S points to an integer suffix |
212 | of length LEN, possibly zero. Returns 0 for an invalid suffix, or a | |
213 | flag vector describing the suffix. */ | |
214 | static unsigned int | |
a4a0016d | 215 | interpret_int_suffix (cpp_reader *pfile, const uchar *s, size_t len) |
cd7ab83f NB |
216 | { |
217 | size_t u, l, i; | |
218 | ||
219 | u = l = i = 0; | |
220 | ||
221 | while (len--) | |
222 | switch (s[len]) | |
223 | { | |
224 | case 'u': case 'U': u++; break; | |
225 | case 'i': case 'I': | |
226 | case 'j': case 'J': i++; break; | |
227 | case 'l': case 'L': l++; | |
228 | /* If there are two Ls, they must be adjacent and the same case. */ | |
229 | if (l == 2 && s[len] != s[len + 1]) | |
230 | return 0; | |
231 | break; | |
232 | default: | |
233 | return 0; | |
234 | } | |
235 | ||
236 | if (l > 2 || u > 1 || i > 1) | |
237 | return 0; | |
238 | ||
a4a0016d ESR |
239 | if (i && !CPP_OPTION (pfile, ext_numeric_literals)) |
240 | return 0; | |
241 | ||
cd7ab83f NB |
242 | return ((i ? CPP_N_IMAGINARY : 0) |
243 | | (u ? CPP_N_UNSIGNED : 0) | |
244 | | ((l == 0) ? CPP_N_SMALL | |
245 | : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE)); | |
246 | } | |
247 | ||
3ce4f9e4 ESR |
248 | /* Return the classification flags for an int suffix. */ |
249 | unsigned int | |
a4a0016d | 250 | cpp_interpret_int_suffix (cpp_reader *pfile, const char *s, size_t len) |
3ce4f9e4 | 251 | { |
a4a0016d | 252 | return interpret_int_suffix (pfile, (const unsigned char *)s, len); |
3ce4f9e4 ESR |
253 | } |
254 | ||
255 | /* Return the string type corresponding to the the input user-defined string | |
256 | literal type. If the input type is not a user-defined string literal | |
257 | type return the input type. */ | |
258 | enum cpp_ttype | |
259 | cpp_userdef_string_remove_type (enum cpp_ttype type) | |
260 | { | |
261 | if (type == CPP_STRING_USERDEF) | |
262 | return CPP_STRING; | |
263 | else if (type == CPP_WSTRING_USERDEF) | |
264 | return CPP_WSTRING; | |
265 | else if (type == CPP_STRING16_USERDEF) | |
266 | return CPP_STRING16; | |
267 | else if (type == CPP_STRING32_USERDEF) | |
268 | return CPP_STRING32; | |
269 | else if (type == CPP_UTF8STRING_USERDEF) | |
270 | return CPP_UTF8STRING; | |
271 | else | |
272 | return type; | |
273 | } | |
274 | ||
275 | /* Return the user-defined string literal type corresponding to the input | |
276 | string type. If the input type is not a string type return the input | |
277 | type. */ | |
278 | enum cpp_ttype | |
279 | cpp_userdef_string_add_type (enum cpp_ttype type) | |
280 | { | |
281 | if (type == CPP_STRING) | |
282 | return CPP_STRING_USERDEF; | |
283 | else if (type == CPP_WSTRING) | |
284 | return CPP_WSTRING_USERDEF; | |
285 | else if (type == CPP_STRING16) | |
286 | return CPP_STRING16_USERDEF; | |
287 | else if (type == CPP_STRING32) | |
288 | return CPP_STRING32_USERDEF; | |
289 | else if (type == CPP_UTF8STRING) | |
290 | return CPP_UTF8STRING_USERDEF; | |
291 | else | |
292 | return type; | |
293 | } | |
294 | ||
295 | /* Return the char type corresponding to the the input user-defined char | |
296 | literal type. If the input type is not a user-defined char literal | |
297 | type return the input type. */ | |
298 | enum cpp_ttype | |
299 | cpp_userdef_char_remove_type (enum cpp_ttype type) | |
300 | { | |
301 | if (type == CPP_CHAR_USERDEF) | |
302 | return CPP_CHAR; | |
303 | else if (type == CPP_WCHAR_USERDEF) | |
304 | return CPP_WCHAR; | |
305 | else if (type == CPP_CHAR16_USERDEF) | |
7e74ce3f | 306 | return CPP_CHAR16; |
3ce4f9e4 | 307 | else if (type == CPP_CHAR32_USERDEF) |
7e74ce3f | 308 | return CPP_CHAR32; |
3ce4f9e4 ESR |
309 | else |
310 | return type; | |
311 | } | |
312 | ||
313 | /* Return the user-defined char literal type corresponding to the input | |
314 | char type. If the input type is not a char type return the input | |
315 | type. */ | |
316 | enum cpp_ttype | |
317 | cpp_userdef_char_add_type (enum cpp_ttype type) | |
318 | { | |
319 | if (type == CPP_CHAR) | |
320 | return CPP_CHAR_USERDEF; | |
321 | else if (type == CPP_WCHAR) | |
322 | return CPP_WCHAR_USERDEF; | |
323 | else if (type == CPP_CHAR16) | |
324 | return CPP_CHAR16_USERDEF; | |
325 | else if (type == CPP_CHAR32) | |
326 | return CPP_CHAR32_USERDEF; | |
327 | else | |
328 | return type; | |
329 | } | |
330 | ||
331 | /* Return true if the token type is a user-defined string literal. */ | |
332 | bool | |
333 | cpp_userdef_string_p (enum cpp_ttype type) | |
334 | { | |
335 | if (type == CPP_STRING_USERDEF | |
336 | || type == CPP_WSTRING_USERDEF | |
337 | || type == CPP_STRING16_USERDEF | |
338 | || type == CPP_STRING32_USERDEF | |
339 | || type == CPP_UTF8STRING_USERDEF) | |
340 | return true; | |
341 | else | |
342 | return false; | |
343 | } | |
344 | ||
345 | /* Return true if the token type is a user-defined char literal. */ | |
346 | bool | |
347 | cpp_userdef_char_p (enum cpp_ttype type) | |
348 | { | |
349 | if (type == CPP_CHAR_USERDEF | |
350 | || type == CPP_WCHAR_USERDEF | |
351 | || type == CPP_CHAR16_USERDEF | |
352 | || type == CPP_CHAR32_USERDEF) | |
353 | return true; | |
354 | else | |
355 | return false; | |
356 | } | |
357 | ||
358 | /* Extract the suffix from a user-defined literal string or char. */ | |
359 | const char * | |
360 | cpp_get_userdef_suffix (const cpp_token *tok) | |
361 | { | |
362 | unsigned int len = tok->val.str.len; | |
363 | const char *text = (const char *)tok->val.str.text; | |
364 | char delim; | |
365 | unsigned int i; | |
366 | for (i = 0; i < len; ++i) | |
367 | if (text[i] == '\'' || text[i] == '"') | |
368 | break; | |
369 | if (i == len) | |
370 | return text + len; | |
371 | delim = text[i]; | |
372 | for (i = len; i > 0; --i) | |
373 | if (text[i - 1] == delim) | |
374 | break; | |
375 | return text + i; | |
376 | } | |
377 | ||
cd7ab83f NB |
378 | /* Categorize numeric constants according to their field (integer, |
379 | floating point, or invalid), radix (decimal, octal, hexadecimal), | |
0b2c4be5 DS |
380 | and type suffixes. |
381 | ||
382 | TOKEN is the token that represents the numeric constant to | |
383 | classify. | |
384 | ||
385 | In C++0X if UD_SUFFIX is non null it will be assigned | |
386 | any unrecognized suffix for a user-defined literal. | |
387 | ||
388 | VIRTUAL_LOCATION is the virtual location for TOKEN. */ | |
cd7ab83f | 389 | unsigned int |
3ce4f9e4 | 390 | cpp_classify_number (cpp_reader *pfile, const cpp_token *token, |
0b2c4be5 | 391 | const char **ud_suffix, source_location virtual_location) |
cd7ab83f NB |
392 | { |
393 | const uchar *str = token->val.str.text; | |
394 | const uchar *limit; | |
395 | unsigned int max_digit, result, radix; | |
396 | enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; | |
dadab4fd | 397 | bool seen_digit; |
cd7ab83f | 398 | |
3ce4f9e4 ESR |
399 | if (ud_suffix) |
400 | *ud_suffix = NULL; | |
401 | ||
cd7ab83f NB |
402 | /* If the lexer has done its job, length one can only be a single |
403 | digit. Fast-path this very common case. */ | |
404 | if (token->val.str.len == 1) | |
405 | return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL; | |
406 | ||
407 | limit = str + token->val.str.len; | |
408 | float_flag = NOT_FLOAT; | |
409 | max_digit = 0; | |
410 | radix = 10; | |
dadab4fd | 411 | seen_digit = false; |
cd7ab83f NB |
412 | |
413 | /* First, interpret the radix. */ | |
414 | if (*str == '0') | |
415 | { | |
416 | radix = 8; | |
417 | str++; | |
418 | ||
419 | /* Require at least one hex digit to classify it as hex. */ | |
7f1fc38e MM |
420 | if ((*str == 'x' || *str == 'X') |
421 | && (str[1] == '.' || ISXDIGIT (str[1]))) | |
cd7ab83f NB |
422 | { |
423 | radix = 16; | |
424 | str++; | |
425 | } | |
f7fd775f JW |
426 | else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) |
427 | { | |
428 | radix = 2; | |
429 | str++; | |
430 | } | |
cd7ab83f NB |
431 | } |
432 | ||
433 | /* Now scan for a well-formed integer or float. */ | |
434 | for (;;) | |
435 | { | |
436 | unsigned int c = *str++; | |
437 | ||
438 | if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) | |
439 | { | |
dadab4fd | 440 | seen_digit = true; |
cd7ab83f NB |
441 | c = hex_value (c); |
442 | if (c > max_digit) | |
443 | max_digit = c; | |
444 | } | |
445 | else if (c == '.') | |
446 | { | |
447 | if (float_flag == NOT_FLOAT) | |
448 | float_flag = AFTER_POINT; | |
449 | else | |
0b2c4be5 DS |
450 | SYNTAX_ERROR_AT (virtual_location, |
451 | "too many decimal points in number"); | |
cd7ab83f NB |
452 | } |
453 | else if ((radix <= 10 && (c == 'e' || c == 'E')) | |
454 | || (radix == 16 && (c == 'p' || c == 'P'))) | |
455 | { | |
456 | float_flag = AFTER_EXPON; | |
457 | break; | |
458 | } | |
459 | else | |
460 | { | |
461 | /* Start of suffix. */ | |
462 | str--; | |
463 | break; | |
464 | } | |
465 | } | |
466 | ||
ac6b1c67 CF |
467 | /* The suffix may be for decimal fixed-point constants without exponent. */ |
468 | if (radix != 16 && float_flag == NOT_FLOAT) | |
469 | { | |
a4a0016d | 470 | result = interpret_float_suffix (pfile, str, limit - str); |
ac6b1c67 CF |
471 | if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) |
472 | { | |
473 | result |= CPP_N_FLOATING; | |
474 | /* We need to restore the radix to 10, if the radix is 8. */ | |
475 | if (radix == 8) | |
476 | radix = 10; | |
477 | ||
478 | if (CPP_PEDANTIC (pfile)) | |
0b2c4be5 DS |
479 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
480 | "fixed-point constants are a GCC extension"); | |
ac6b1c67 CF |
481 | goto syntax_ok; |
482 | } | |
483 | else | |
484 | result = 0; | |
485 | } | |
486 | ||
cd7ab83f NB |
487 | if (float_flag != NOT_FLOAT && radix == 8) |
488 | radix = 10; | |
489 | ||
490 | if (max_digit >= radix) | |
f7fd775f JW |
491 | { |
492 | if (radix == 2) | |
0b2c4be5 DS |
493 | SYNTAX_ERROR2_AT (virtual_location, |
494 | "invalid digit \"%c\" in binary constant", '0' + max_digit); | |
f7fd775f | 495 | else |
0b2c4be5 DS |
496 | SYNTAX_ERROR2_AT (virtual_location, |
497 | "invalid digit \"%c\" in octal constant", '0' + max_digit); | |
f7fd775f | 498 | } |
cd7ab83f NB |
499 | |
500 | if (float_flag != NOT_FLOAT) | |
501 | { | |
f7fd775f JW |
502 | if (radix == 2) |
503 | { | |
0b2c4be5 DS |
504 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
505 | "invalid prefix \"0b\" for floating constant"); | |
f7fd775f JW |
506 | return CPP_N_INVALID; |
507 | } | |
508 | ||
dadab4fd | 509 | if (radix == 16 && !seen_digit) |
0b2c4be5 DS |
510 | SYNTAX_ERROR_AT (virtual_location, |
511 | "no digits in hexadecimal floating constant"); | |
dadab4fd | 512 | |
cd7ab83f | 513 | if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99)) |
0b2c4be5 DS |
514 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
515 | "use of C99 hexadecimal floating constant"); | |
cd7ab83f NB |
516 | |
517 | if (float_flag == AFTER_EXPON) | |
518 | { | |
519 | if (*str == '+' || *str == '-') | |
520 | str++; | |
521 | ||
522 | /* Exponent is decimal, even if string is a hex float. */ | |
523 | if (!ISDIGIT (*str)) | |
0b2c4be5 | 524 | SYNTAX_ERROR_AT (virtual_location, "exponent has no digits"); |
cd7ab83f NB |
525 | |
526 | do | |
527 | str++; | |
528 | while (ISDIGIT (*str)); | |
529 | } | |
530 | else if (radix == 16) | |
0b2c4be5 DS |
531 | SYNTAX_ERROR_AT (virtual_location, |
532 | "hexadecimal floating constants require an exponent"); | |
cd7ab83f | 533 | |
a4a0016d | 534 | result = interpret_float_suffix (pfile, str, limit - str); |
cd7ab83f NB |
535 | if (result == 0) |
536 | { | |
3ce4f9e4 ESR |
537 | if (CPP_OPTION (pfile, user_literals)) |
538 | { | |
539 | if (ud_suffix) | |
540 | *ud_suffix = (const char *) str; | |
541 | result = CPP_N_LARGE | CPP_N_USERDEF; | |
542 | } | |
543 | else | |
544 | { | |
0b2c4be5 DS |
545 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
546 | "invalid suffix \"%.*s\" on floating constant", | |
547 | (int) (limit - str), str); | |
3ce4f9e4 ESR |
548 | return CPP_N_INVALID; |
549 | } | |
cd7ab83f NB |
550 | } |
551 | ||
552 | /* Traditional C didn't accept any floating suffixes. */ | |
553 | if (limit != str | |
554 | && CPP_WTRADITIONAL (pfile) | |
555 | && ! cpp_sys_macro_p (pfile)) | |
0b2c4be5 DS |
556 | cpp_warning_with_line (pfile, CPP_W_TRADITIONAL, virtual_location, 0, |
557 | "traditional C rejects the \"%.*s\" suffix", | |
558 | (int) (limit - str), str); | |
cd7ab83f | 559 | |
839a3b8a JJ |
560 | /* A suffix for double is a GCC extension via decimal float support. |
561 | If the suffix also specifies an imaginary value we'll catch that | |
562 | later. */ | |
563 | if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) | |
0b2c4be5 DS |
564 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
565 | "suffix for double constant is a GCC extension"); | |
839a3b8a | 566 | |
ad6ed77e JG |
567 | /* Radix must be 10 for decimal floats. */ |
568 | if ((result & CPP_N_DFLOAT) && radix != 10) | |
569 | { | |
0b2c4be5 DS |
570 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
571 | "invalid suffix \"%.*s\" with hexadecimal floating constant", | |
572 | (int) (limit - str), str); | |
ad6ed77e JG |
573 | return CPP_N_INVALID; |
574 | } | |
575 | ||
ac6b1c67 | 576 | if ((result & (CPP_N_FRACT | CPP_N_ACCUM)) && CPP_PEDANTIC (pfile)) |
0b2c4be5 DS |
577 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
578 | "fixed-point constants are a GCC extension"); | |
ac6b1c67 | 579 | |
5a6bb57e | 580 | if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) |
0b2c4be5 DS |
581 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
582 | "decimal float constants are a GCC extension"); | |
5a6bb57e | 583 | |
cd7ab83f NB |
584 | result |= CPP_N_FLOATING; |
585 | } | |
586 | else | |
587 | { | |
a4a0016d | 588 | result = interpret_int_suffix (pfile, str, limit - str); |
cd7ab83f NB |
589 | if (result == 0) |
590 | { | |
3ce4f9e4 ESR |
591 | if (CPP_OPTION (pfile, user_literals)) |
592 | { | |
593 | if (ud_suffix) | |
594 | *ud_suffix = (const char *) str; | |
595 | result = CPP_N_UNSIGNED | CPP_N_LARGE | CPP_N_USERDEF; | |
596 | } | |
597 | else | |
598 | { | |
0b2c4be5 DS |
599 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
600 | "invalid suffix \"%.*s\" on integer constant", | |
601 | (int) (limit - str), str); | |
3ce4f9e4 ESR |
602 | return CPP_N_INVALID; |
603 | } | |
cd7ab83f NB |
604 | } |
605 | ||
56da7207 ZW |
606 | /* Traditional C only accepted the 'L' suffix. |
607 | Suppress warning about 'LL' with -Wno-long-long. */ | |
608 | if (CPP_WTRADITIONAL (pfile) && ! cpp_sys_macro_p (pfile)) | |
609 | { | |
610 | int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); | |
87cf0651 | 611 | int large = (result & CPP_N_WIDTH) == CPP_N_LARGE |
e3339d0f | 612 | && CPP_OPTION (pfile, cpp_warn_long_long); |
56da7207 | 613 | |
87cf0651 | 614 | if (u_or_i || large) |
0b2c4be5 DS |
615 | cpp_warning_with_line (pfile, large ? CPP_W_LONG_LONG : CPP_W_TRADITIONAL, |
616 | virtual_location, 0, | |
617 | "traditional C rejects the \"%.*s\" suffix", | |
618 | (int) (limit - str), str); | |
56da7207 | 619 | } |
cd7ab83f NB |
620 | |
621 | if ((result & CPP_N_WIDTH) == CPP_N_LARGE | |
e3339d0f | 622 | && CPP_OPTION (pfile, cpp_warn_long_long)) |
87cf0651 SB |
623 | { |
624 | const char *message = CPP_OPTION (pfile, cplusplus) | |
625 | ? N_("use of C++0x long long integer constant") | |
626 | : N_("use of C99 long long integer constant"); | |
627 | ||
628 | if (CPP_OPTION (pfile, c99)) | |
0b2c4be5 DS |
629 | cpp_warning_with_line (pfile, CPP_W_LONG_LONG, virtual_location, |
630 | 0, message); | |
87cf0651 | 631 | else |
0b2c4be5 DS |
632 | cpp_pedwarning_with_line (pfile, CPP_W_LONG_LONG, |
633 | virtual_location, 0, message); | |
87cf0651 | 634 | } |
cd7ab83f NB |
635 | |
636 | result |= CPP_N_INTEGER; | |
637 | } | |
638 | ||
ac6b1c67 | 639 | syntax_ok: |
cd7ab83f | 640 | if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) |
0b2c4be5 DS |
641 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
642 | "imaginary constants are a GCC extension"); | |
f7fd775f | 643 | if (radix == 2 && CPP_PEDANTIC (pfile)) |
0b2c4be5 DS |
644 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, virtual_location, 0, |
645 | "binary constants are a GCC extension"); | |
cd7ab83f NB |
646 | |
647 | if (radix == 10) | |
648 | result |= CPP_N_DECIMAL; | |
649 | else if (radix == 16) | |
650 | result |= CPP_N_HEX; | |
f7fd775f JW |
651 | else if (radix == 2) |
652 | result |= CPP_N_BINARY; | |
cd7ab83f NB |
653 | else |
654 | result |= CPP_N_OCTAL; | |
655 | ||
656 | return result; | |
657 | ||
658 | syntax_error: | |
659 | return CPP_N_INVALID; | |
660 | } | |
661 | ||
662 | /* cpp_interpret_integer converts an integer constant into a cpp_num, | |
663 | of precision options->precision. | |
664 | ||
665 | We do not provide any interface for decimal->float conversion, | |
6cf87ca4 ZW |
666 | because the preprocessor doesn't need it and we don't want to |
667 | drag in GCC's floating point emulator. */ | |
cd7ab83f | 668 | cpp_num |
6cf87ca4 ZW |
669 | cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, |
670 | unsigned int type) | |
cd7ab83f NB |
671 | { |
672 | const uchar *p, *end; | |
673 | cpp_num result; | |
674 | ||
675 | result.low = 0; | |
676 | result.high = 0; | |
23ff0223 NB |
677 | result.unsignedp = !!(type & CPP_N_UNSIGNED); |
678 | result.overflow = false; | |
cd7ab83f NB |
679 | |
680 | p = token->val.str.text; | |
681 | end = p + token->val.str.len; | |
682 | ||
683 | /* Common case of a single digit. */ | |
684 | if (token->val.str.len == 1) | |
685 | result.low = p[0] - '0'; | |
686 | else | |
687 | { | |
688 | cpp_num_part max; | |
689 | size_t precision = CPP_OPTION (pfile, precision); | |
690 | unsigned int base = 10, c = 0; | |
691 | bool overflow = false; | |
692 | ||
693 | if ((type & CPP_N_RADIX) == CPP_N_OCTAL) | |
694 | { | |
695 | base = 8; | |
696 | p++; | |
697 | } | |
698 | else if ((type & CPP_N_RADIX) == CPP_N_HEX) | |
699 | { | |
700 | base = 16; | |
701 | p += 2; | |
702 | } | |
f7fd775f JW |
703 | else if ((type & CPP_N_RADIX) == CPP_N_BINARY) |
704 | { | |
705 | base = 2; | |
706 | p += 2; | |
707 | } | |
cd7ab83f NB |
708 | |
709 | /* We can add a digit to numbers strictly less than this without | |
710 | needing the precision and slowness of double integers. */ | |
711 | max = ~(cpp_num_part) 0; | |
712 | if (precision < PART_PRECISION) | |
713 | max >>= PART_PRECISION - precision; | |
714 | max = (max - base + 1) / base + 1; | |
715 | ||
716 | for (; p < end; p++) | |
717 | { | |
718 | c = *p; | |
719 | ||
720 | if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) | |
721 | c = hex_value (c); | |
722 | else | |
723 | break; | |
724 | ||
725 | /* Strict inequality for when max is set to zero. */ | |
726 | if (result.low < max) | |
727 | result.low = result.low * base + c; | |
728 | else | |
729 | { | |
730 | result = append_digit (result, c, base, precision); | |
731 | overflow |= result.overflow; | |
732 | max = 0; | |
733 | } | |
734 | } | |
735 | ||
3ce4f9e4 | 736 | if (overflow && !(type & CPP_N_USERDEF)) |
0527bc4e | 737 | cpp_error (pfile, CPP_DL_PEDWARN, |
cd7ab83f | 738 | "integer constant is too large for its type"); |
017acb41 NB |
739 | /* If too big to be signed, consider it unsigned. Only warn for |
740 | decimal numbers. Traditional numbers were always signed (but | |
8d9afc4e | 741 | we still honor an explicit U suffix); but we only have |
cd98faa1 | 742 | traditional semantics in directives. */ |
017acb41 | 743 | else if (!result.unsignedp |
cd98faa1 NB |
744 | && !(CPP_OPTION (pfile, traditional) |
745 | && pfile->state.in_directive) | |
017acb41 | 746 | && !num_positive (result, precision)) |
cd7ab83f | 747 | { |
f88d0772 | 748 | /* This is for constants within the range of uintmax_t but |
813b9e7e | 749 | not that of intmax_t. For such decimal constants, a |
f88d0772 JM |
750 | diagnostic is required for C99 as the selected type must |
751 | be signed and not having a type is a constraint violation | |
752 | (DR#298, TC3), so this must be a pedwarn. For C90, | |
753 | unsigned long is specified to be used for a constant that | |
754 | does not fit in signed long; if uintmax_t has the same | |
755 | range as unsigned long this means only a warning is | |
756 | appropriate here. C90 permits the preprocessor to use a | |
757 | wider range than unsigned long in the compiler, so if | |
758 | uintmax_t is wider than unsigned long no diagnostic is | |
759 | required for such constants in preprocessor #if | |
760 | expressions and the compiler will pedwarn for such | |
761 | constants outside the range of unsigned long that reach | |
762 | the compiler so a diagnostic is not required there | |
763 | either; thus, pedwarn for C99 but use a plain warning for | |
764 | C90. */ | |
cd7ab83f | 765 | if (base == 10) |
f88d0772 JM |
766 | cpp_error (pfile, (CPP_OPTION (pfile, c99) |
767 | ? CPP_DL_PEDWARN | |
768 | : CPP_DL_WARNING), | |
cd7ab83f | 769 | "integer constant is so large that it is unsigned"); |
23ff0223 | 770 | result.unsignedp = true; |
cd7ab83f NB |
771 | } |
772 | } | |
773 | ||
774 | return result; | |
775 | } | |
cf00a885 | 776 | |
6cf87ca4 | 777 | /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ |
91318908 | 778 | static cpp_num |
6cf87ca4 | 779 | append_digit (cpp_num num, int digit, int base, size_t precision) |
91318908 NB |
780 | { |
781 | cpp_num result; | |
f7fd775f | 782 | unsigned int shift; |
91318908 NB |
783 | bool overflow; |
784 | cpp_num_part add_high, add_low; | |
785 | ||
f7fd775f | 786 | /* Multiply by 2, 8 or 16. Catching this overflow here means we don't |
91318908 | 787 | need to worry about add_high overflowing. */ |
f7fd775f JW |
788 | switch (base) |
789 | { | |
790 | case 2: | |
791 | shift = 1; | |
792 | break; | |
793 | ||
794 | case 16: | |
795 | shift = 4; | |
796 | break; | |
797 | ||
798 | default: | |
799 | shift = 3; | |
800 | } | |
23ff0223 | 801 | overflow = !!(num.high >> (PART_PRECISION - shift)); |
91318908 NB |
802 | result.high = num.high << shift; |
803 | result.low = num.low << shift; | |
804 | result.high |= num.low >> (PART_PRECISION - shift); | |
6de9cd9a | 805 | result.unsignedp = num.unsignedp; |
91318908 NB |
806 | |
807 | if (base == 10) | |
808 | { | |
809 | add_low = num.low << 1; | |
810 | add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); | |
811 | } | |
812 | else | |
813 | add_high = add_low = 0; | |
814 | ||
815 | if (add_low + digit < add_low) | |
816 | add_high++; | |
817 | add_low += digit; | |
22a8a52d | 818 | |
91318908 NB |
819 | if (result.low + add_low < result.low) |
820 | add_high++; | |
821 | if (result.high + add_high < result.high) | |
822 | overflow = true; | |
823 | ||
824 | result.low += add_low; | |
825 | result.high += add_high; | |
6de9cd9a | 826 | result.overflow = overflow; |
91318908 NB |
827 | |
828 | /* The above code catches overflow of a cpp_num type. This catches | |
829 | overflow of the (possibly shorter) target precision. */ | |
830 | num.low = result.low; | |
831 | num.high = result.high; | |
832 | result = num_trim (result, precision); | |
833 | if (!num_eq (result, num)) | |
6de9cd9a | 834 | result.overflow = true; |
91318908 | 835 | |
91318908 NB |
836 | return result; |
837 | } | |
838 | ||
5d8ebbd8 | 839 | /* Handle meeting "defined" in a preprocessor expression. */ |
91318908 | 840 | static cpp_num |
6cf87ca4 | 841 | parse_defined (cpp_reader *pfile) |
ba412f14 | 842 | { |
91318908 | 843 | cpp_num result; |
93c80368 NB |
844 | int paren = 0; |
845 | cpp_hashnode *node = 0; | |
4ed5bcfb | 846 | const cpp_token *token; |
63d75005 | 847 | cpp_context *initial_context = pfile->context; |
ba412f14 | 848 | |
93c80368 NB |
849 | /* Don't expand macros. */ |
850 | pfile->state.prevent_expansion++; | |
851 | ||
4ed5bcfb NB |
852 | token = cpp_get_token (pfile); |
853 | if (token->type == CPP_OPEN_PAREN) | |
ba412f14 | 854 | { |
cf00a885 | 855 | paren = 1; |
4ed5bcfb | 856 | token = cpp_get_token (pfile); |
ba412f14 ZW |
857 | } |
858 | ||
4ed5bcfb | 859 | if (token->type == CPP_NAME) |
93c80368 | 860 | { |
9a0c6187 | 861 | node = token->val.node.node; |
4ed5bcfb | 862 | if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) |
93c80368 | 863 | { |
0527bc4e | 864 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); |
4ed5bcfb | 865 | node = 0; |
93c80368 NB |
866 | } |
867 | } | |
868 | else | |
3c8465d0 | 869 | { |
0527bc4e | 870 | cpp_error (pfile, CPP_DL_ERROR, |
ebef4e8c | 871 | "operator \"defined\" requires an identifier"); |
4ed5bcfb | 872 | if (token->flags & NAMED_OP) |
3c8465d0 NB |
873 | { |
874 | cpp_token op; | |
875 | ||
876 | op.flags = 0; | |
4ed5bcfb | 877 | op.type = token->type; |
0527bc4e | 878 | cpp_error (pfile, CPP_DL_ERROR, |
3c8465d0 | 879 | "(\"%s\" is an alternative token for \"%s\" in C++)", |
4ed5bcfb | 880 | cpp_token_as_text (pfile, token), |
3c8465d0 NB |
881 | cpp_token_as_text (pfile, &op)); |
882 | } | |
883 | } | |
15dad1d9 | 884 | |
91318908 | 885 | if (node) |
15dad1d9 | 886 | { |
335d03ec | 887 | if (pfile->context != initial_context && CPP_PEDANTIC (pfile)) |
0527bc4e | 888 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 889 | "this use of \"defined\" may not be portable"); |
63d75005 | 890 | |
a69cbaac | 891 | _cpp_mark_macro_used (node); |
93d45d9e JM |
892 | if (!(node->flags & NODE_USED)) |
893 | { | |
894 | node->flags |= NODE_USED; | |
895 | if (node->type == NT_MACRO) | |
896 | { | |
a69d2520 JJ |
897 | if ((node->flags & NODE_BUILTIN) |
898 | && pfile->cb.user_builtin_macro) | |
899 | pfile->cb.user_builtin_macro (pfile, node); | |
93d45d9e JM |
900 | if (pfile->cb.used_define) |
901 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
902 | } | |
903 | else | |
904 | { | |
905 | if (pfile->cb.used_undef) | |
906 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
907 | } | |
908 | } | |
a69cbaac | 909 | |
6d18adbc NB |
910 | /* A possible controlling macro of the form #if !defined (). |
911 | _cpp_parse_expr checks there was no other junk on the line. */ | |
912 | pfile->mi_ind_cmacro = node; | |
15dad1d9 | 913 | } |
93c80368 NB |
914 | |
915 | pfile->state.prevent_expansion--; | |
91318908 | 916 | |
f3c33d9d MM |
917 | /* Do not treat conditional macros as being defined. This is due to the |
918 | powerpc and spu ports using conditional macros for 'vector', 'bool', and | |
919 | 'pixel' to act as conditional keywords. This messes up tests like #ifndef | |
920 | bool. */ | |
23ff0223 | 921 | result.unsignedp = false; |
91318908 | 922 | result.high = 0; |
23ff0223 | 923 | result.overflow = false; |
f3c33d9d MM |
924 | result.low = (node && node->type == NT_MACRO |
925 | && (node->flags & NODE_CONDITIONAL) == 0); | |
91318908 | 926 | return result; |
15dad1d9 ZW |
927 | } |
928 | ||
60284a59 NB |
929 | /* Convert a token into a CPP_NUMBER (an interpreted preprocessing |
930 | number or character constant, or the result of the "defined" or "#" | |
cd7ab83f | 931 | operators). */ |
91318908 | 932 | static cpp_num |
0b2c4be5 DS |
933 | eval_token (cpp_reader *pfile, const cpp_token *token, |
934 | source_location virtual_location) | |
7f2935c7 | 935 | { |
91318908 | 936 | cpp_num result; |
60284a59 | 937 | unsigned int temp; |
4268e8bb | 938 | int unsignedp = 0; |
041c3194 | 939 | |
6de9cd9a DN |
940 | result.unsignedp = false; |
941 | result.overflow = false; | |
942 | ||
93c80368 | 943 | switch (token->type) |
ba412f14 | 944 | { |
7f2935c7 | 945 | case CPP_NUMBER: |
0b2c4be5 | 946 | temp = cpp_classify_number (pfile, token, NULL, virtual_location); |
3ce4f9e4 ESR |
947 | if (temp & CPP_N_USERDEF) |
948 | cpp_error (pfile, CPP_DL_ERROR, | |
949 | "user-defined literal in preprocessor expression"); | |
cd7ab83f NB |
950 | switch (temp & CPP_N_CATEGORY) |
951 | { | |
952 | case CPP_N_FLOATING: | |
0b2c4be5 DS |
953 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
954 | "floating constant in preprocessor expression"); | |
cd7ab83f NB |
955 | break; |
956 | case CPP_N_INTEGER: | |
957 | if (!(temp & CPP_N_IMAGINARY)) | |
958 | return cpp_interpret_integer (pfile, token, temp); | |
0b2c4be5 DS |
959 | cpp_error_with_line (pfile, CPP_DL_ERROR, virtual_location, 0, |
960 | "imaginary number in preprocessor expression"); | |
cd7ab83f NB |
961 | break; |
962 | ||
963 | case CPP_N_INVALID: | |
964 | /* Error already issued. */ | |
965 | break; | |
966 | } | |
967 | result.high = result.low = 0; | |
968 | break; | |
7f2f1a66 | 969 | |
cf00a885 | 970 | case CPP_WCHAR: |
4268e8bb | 971 | case CPP_CHAR: |
b6baa67d KVH |
972 | case CPP_CHAR16: |
973 | case CPP_CHAR32: | |
a5a49440 | 974 | { |
91318908 NB |
975 | cppchar_t cc = cpp_interpret_charconst (pfile, token, |
976 | &temp, &unsignedp); | |
977 | ||
978 | result.high = 0; | |
979 | result.low = cc; | |
a5a49440 | 980 | /* Sign-extend the result if necessary. */ |
91318908 NB |
981 | if (!unsignedp && (cppchar_signed_t) cc < 0) |
982 | { | |
983 | if (PART_PRECISION > BITS_PER_CPPCHAR_T) | |
984 | result.low |= ~(~(cpp_num_part) 0 | |
985 | >> (PART_PRECISION - BITS_PER_CPPCHAR_T)); | |
986 | result.high = ~(cpp_num_part) 0; | |
987 | result = num_trim (result, CPP_OPTION (pfile, precision)); | |
988 | } | |
a5a49440 | 989 | } |
60284a59 | 990 | break; |
ba412f14 | 991 | |
92936ecf | 992 | case CPP_NAME: |
9a0c6187 | 993 | if (token->val.node.node == pfile->spec_nodes.n_defined) |
63d75005 | 994 | return parse_defined (pfile); |
7d4918a2 | 995 | else if (CPP_OPTION (pfile, cplusplus) |
9a0c6187 JM |
996 | && (token->val.node.node == pfile->spec_nodes.n_true |
997 | || token->val.node.node == pfile->spec_nodes.n_false)) | |
7d4918a2 | 998 | { |
91318908 | 999 | result.high = 0; |
9a0c6187 | 1000 | result.low = (token->val.node.node == pfile->spec_nodes.n_true); |
7d4918a2 ZW |
1001 | } |
1002 | else | |
1003 | { | |
91318908 NB |
1004 | result.high = 0; |
1005 | result.low = 0; | |
87ed109f | 1006 | if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) |
0b2c4be5 DS |
1007 | cpp_warning_with_line (pfile, CPP_W_UNDEF, virtual_location, 0, |
1008 | "\"%s\" is not defined", | |
1009 | NODE_NAME (token->val.node.node)); | |
7f2935c7 | 1010 | } |
60284a59 | 1011 | break; |
7f2935c7 | 1012 | |
899015a0 TT |
1013 | case CPP_HASH: |
1014 | if (!pfile->state.skipping) | |
1015 | { | |
1016 | /* A pedantic warning takes precedence over a deprecated | |
1017 | warning here. */ | |
1018 | if (CPP_PEDANTIC (pfile)) | |
0b2c4be5 DS |
1019 | cpp_error_with_line (pfile, CPP_DL_PEDWARN, |
1020 | virtual_location, 0, | |
1021 | "assertions are a GCC extension"); | |
e3339d0f | 1022 | else if (CPP_OPTION (pfile, cpp_warn_deprecated)) |
0b2c4be5 DS |
1023 | cpp_warning_with_line (pfile, CPP_W_DEPRECATED, virtual_location, 0, |
1024 | "assertions are a deprecated extension"); | |
899015a0 | 1025 | } |
91318908 NB |
1026 | _cpp_test_assertion (pfile, &temp); |
1027 | result.high = 0; | |
1028 | result.low = temp; | |
899015a0 TT |
1029 | break; |
1030 | ||
1031 | default: | |
1032 | abort (); | |
93c80368 | 1033 | } |
7c3bb1de | 1034 | |
23ff0223 | 1035 | result.unsignedp = !!unsignedp; |
91318908 | 1036 | return result; |
7f2935c7 PB |
1037 | } |
1038 | \f | |
4063b943 | 1039 | /* Operator precedence and flags table. |
dbac4aff NB |
1040 | |
1041 | After an operator is returned from the lexer, if it has priority less | |
87ed109f NB |
1042 | than the operator on the top of the stack, we reduce the stack by one |
1043 | operator and repeat the test. Since equal priorities do not reduce, | |
1044 | this is naturally right-associative. | |
1045 | ||
1046 | We handle left-associative operators by decrementing the priority of | |
1047 | just-lexed operators by one, but retaining the priority of operators | |
1048 | already on the stack. | |
dbac4aff NB |
1049 | |
1050 | The remaining cases are '(' and ')'. We handle '(' by skipping the | |
1051 | reduction phase completely. ')' is given lower priority than | |
1052 | everything else, including '(', effectively forcing a reduction of the | |
272d0bee | 1053 | parenthesized expression. If there is a matching '(', the routine |
87ed109f NB |
1054 | reduce() exits immediately. If the normal exit route sees a ')', then |
1055 | there cannot have been a matching '(' and an error message is output. | |
4063b943 | 1056 | |
f8b954fc NB |
1057 | The parser assumes all shifted operators require a left operand unless |
1058 | the flag NO_L_OPERAND is set. These semantics are automatic; any | |
1059 | extra semantics need to be handled with operator-specific code. */ | |
4063b943 | 1060 | |
68e65275 NB |
1061 | /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an |
1062 | operand changes because of integer promotions. */ | |
87ed109f NB |
1063 | #define NO_L_OPERAND (1 << 0) |
1064 | #define LEFT_ASSOC (1 << 1) | |
68e65275 | 1065 | #define CHECK_PROMOTION (1 << 2) |
eba30526 | 1066 | |
cf00a885 ZW |
1067 | /* Operator to priority map. Must be in the same order as the first |
1068 | N entries of enum cpp_ttype. */ | |
c3f829c1 | 1069 | static const struct cpp_operator |
87ed109f | 1070 | { |
60284a59 | 1071 | uchar prio; |
87ed109f NB |
1072 | uchar flags; |
1073 | } optab[] = | |
cf00a885 | 1074 | { |
ad28cff7 NB |
1075 | /* EQ */ {0, 0}, /* Shouldn't happen. */ |
1076 | /* NOT */ {16, NO_L_OPERAND}, | |
68e65275 NB |
1077 | /* GREATER */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
1078 | /* LESS */ {12, LEFT_ASSOC | CHECK_PROMOTION}, | |
1079 | /* PLUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, | |
1080 | /* MINUS */ {14, LEFT_ASSOC | CHECK_PROMOTION}, | |
1081 | /* MULT */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
1082 | /* DIV */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
1083 | /* MOD */ {15, LEFT_ASSOC | CHECK_PROMOTION}, | |
1084 | /* AND */ {9, LEFT_ASSOC | CHECK_PROMOTION}, | |
1085 | /* OR */ {7, LEFT_ASSOC | CHECK_PROMOTION}, | |
1086 | /* XOR */ {8, LEFT_ASSOC | CHECK_PROMOTION}, | |
ad28cff7 NB |
1087 | /* RSHIFT */ {13, LEFT_ASSOC}, |
1088 | /* LSHIFT */ {13, LEFT_ASSOC}, | |
1089 | ||
ad28cff7 | 1090 | /* COMPL */ {16, NO_L_OPERAND}, |
75aef48a NB |
1091 | /* AND_AND */ {6, LEFT_ASSOC}, |
1092 | /* OR_OR */ {5, LEFT_ASSOC}, | |
71c10038 TT |
1093 | /* Note that QUERY, COLON, and COMMA must have the same precedence. |
1094 | However, there are some special cases for these in reduce(). */ | |
1095 | /* QUERY */ {4, 0}, | |
68e65275 | 1096 | /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, |
71c10038 | 1097 | /* COMMA */ {4, LEFT_ASSOC}, |
75aef48a | 1098 | /* OPEN_PAREN */ {1, NO_L_OPERAND}, |
ad28cff7 NB |
1099 | /* CLOSE_PAREN */ {0, 0}, |
1100 | /* EOF */ {0, 0}, | |
1101 | /* EQ_EQ */ {11, LEFT_ASSOC}, | |
1102 | /* NOT_EQ */ {11, LEFT_ASSOC}, | |
68e65275 NB |
1103 | /* GREATER_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, |
1104 | /* LESS_EQ */ {12, LEFT_ASSOC | CHECK_PROMOTION}, | |
ad28cff7 NB |
1105 | /* UPLUS */ {16, NO_L_OPERAND}, |
1106 | /* UMINUS */ {16, NO_L_OPERAND} | |
cf00a885 ZW |
1107 | }; |
1108 | ||
7f2935c7 | 1109 | /* Parse and evaluate a C expression, reading from PFILE. |
df383483 | 1110 | Returns the truth value of the expression. |
87ed109f NB |
1111 | |
1112 | The implementation is an operator precedence parser, i.e. a | |
1113 | bottom-up parser, using a stack for not-yet-reduced tokens. | |
1114 | ||
1115 | The stack base is op_stack, and the current stack pointer is 'top'. | |
1116 | There is a stack element for each operator (only), and the most | |
1117 | recently pushed operator is 'top->op'. An operand (value) is | |
1118 | stored in the 'value' field of the stack element of the operator | |
1119 | that precedes it. */ | |
1120 | bool | |
d750887f | 1121 | _cpp_parse_expr (cpp_reader *pfile, bool is_if) |
7f2935c7 | 1122 | { |
87ed109f NB |
1123 | struct op *top = pfile->op_stack; |
1124 | unsigned int lex_count; | |
1125 | bool saw_leading_not, want_value = true; | |
0b2c4be5 | 1126 | source_location virtual_location = 0; |
87ed109f NB |
1127 | |
1128 | pfile->state.skip_eval = 0; | |
7f2935c7 | 1129 | |
93c80368 | 1130 | /* Set up detection of #if ! defined(). */ |
6d18adbc | 1131 | pfile->mi_ind_cmacro = 0; |
87ed109f | 1132 | saw_leading_not = false; |
6d18adbc | 1133 | lex_count = 0; |
93c80368 | 1134 | |
87ed109f | 1135 | /* Lowest priority operator prevents further reductions. */ |
cf00a885 | 1136 | top->op = CPP_EOF; |
4063b943 | 1137 | |
7f2935c7 PB |
1138 | for (;;) |
1139 | { | |
cf00a885 | 1140 | struct op op; |
7f2935c7 | 1141 | |
6d18adbc | 1142 | lex_count++; |
0b2c4be5 | 1143 | op.token = cpp_get_token_with_location (pfile, &virtual_location); |
68e65275 | 1144 | op.op = op.token->type; |
0b2c4be5 | 1145 | op.loc = virtual_location; |
7f2935c7 | 1146 | |
7f2935c7 PB |
1147 | switch (op.op) |
1148 | { | |
60284a59 | 1149 | /* These tokens convert into values. */ |
c60e94a7 | 1150 | case CPP_NUMBER: |
60284a59 NB |
1151 | case CPP_CHAR: |
1152 | case CPP_WCHAR: | |
b6baa67d KVH |
1153 | case CPP_CHAR16: |
1154 | case CPP_CHAR32: | |
60284a59 NB |
1155 | case CPP_NAME: |
1156 | case CPP_HASH: | |
f8b954fc | 1157 | if (!want_value) |
0b2c4be5 DS |
1158 | SYNTAX_ERROR2_AT (op.loc, |
1159 | "missing binary operator before token \"%s\"", | |
1160 | cpp_token_as_text (pfile, op.token)); | |
f8b954fc | 1161 | want_value = false; |
0b2c4be5 | 1162 | top->value = eval_token (pfile, op.token, op.loc); |
9ee70313 NB |
1163 | continue; |
1164 | ||
6d18adbc NB |
1165 | case CPP_NOT: |
1166 | saw_leading_not = lex_count == 1; | |
6d18adbc | 1167 | break; |
cf00a885 | 1168 | case CPP_PLUS: |
f8b954fc NB |
1169 | if (want_value) |
1170 | op.op = CPP_UPLUS; | |
1171 | break; | |
1172 | case CPP_MINUS: | |
1173 | if (want_value) | |
1174 | op.op = CPP_UMINUS; | |
1175 | break; | |
60284a59 | 1176 | |
f8b954fc | 1177 | default: |
60284a59 | 1178 | if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ) |
0b2c4be5 DS |
1179 | SYNTAX_ERROR2_AT (op.loc, |
1180 | "token \"%s\" is not valid in preprocessor expressions", | |
1181 | cpp_token_as_text (pfile, op.token)); | |
f8b954fc | 1182 | break; |
7f2935c7 | 1183 | } |
7f2935c7 | 1184 | |
87ed109f NB |
1185 | /* Check we have a value or operator as appropriate. */ |
1186 | if (optab[op.op].flags & NO_L_OPERAND) | |
7f2935c7 | 1187 | { |
87ed109f | 1188 | if (!want_value) |
0b2c4be5 DS |
1189 | SYNTAX_ERROR2_AT (op.loc, |
1190 | "missing binary operator before token \"%s\"", | |
1191 | cpp_token_as_text (pfile, op.token)); | |
87ed109f NB |
1192 | } |
1193 | else if (want_value) | |
1194 | { | |
a09d4744 NB |
1195 | /* We want a number (or expression) and haven't got one. |
1196 | Try to emit a specific diagnostic. */ | |
1197 | if (op.op == CPP_CLOSE_PAREN && top->op == CPP_OPEN_PAREN) | |
0b2c4be5 DS |
1198 | SYNTAX_ERROR_AT (op.loc, |
1199 | "missing expression between '(' and ')'"); | |
a09d4744 NB |
1200 | |
1201 | if (op.op == CPP_EOF && top->op == CPP_EOF) | |
0b2c4be5 DS |
1202 | SYNTAX_ERROR2_AT (op.loc, |
1203 | "%s with no expression", is_if ? "#if" : "#elif"); | |
a09d4744 NB |
1204 | |
1205 | if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN) | |
0b2c4be5 DS |
1206 | SYNTAX_ERROR2_AT (op.loc, |
1207 | "operator '%s' has no right operand", | |
1208 | cpp_token_as_text (pfile, top->token)); | |
a09d4744 NB |
1209 | else if (op.op == CPP_CLOSE_PAREN || op.op == CPP_EOF) |
1210 | /* Complain about missing paren during reduction. */; | |
1211 | else | |
0b2c4be5 DS |
1212 | SYNTAX_ERROR2_AT (op.loc, |
1213 | "operator '%s' has no left operand", | |
1214 | cpp_token_as_text (pfile, op.token)); | |
7f2935c7 | 1215 | } |
9ee70313 | 1216 | |
87ed109f NB |
1217 | top = reduce (pfile, top, op.op); |
1218 | if (!top) | |
1219 | goto syntax_error; | |
9ee70313 | 1220 | |
60284a59 NB |
1221 | if (op.op == CPP_EOF) |
1222 | break; | |
1223 | ||
87ed109f | 1224 | switch (op.op) |
b22ef131 | 1225 | { |
87ed109f NB |
1226 | case CPP_CLOSE_PAREN: |
1227 | continue; | |
87ed109f | 1228 | case CPP_OR_OR: |
91318908 | 1229 | if (!num_zerop (top->value)) |
87ed109f NB |
1230 | pfile->state.skip_eval++; |
1231 | break; | |
1232 | case CPP_AND_AND: | |
1233 | case CPP_QUERY: | |
91318908 | 1234 | if (num_zerop (top->value)) |
87ed109f NB |
1235 | pfile->state.skip_eval++; |
1236 | break; | |
1237 | case CPP_COLON: | |
60284a59 | 1238 | if (top->op != CPP_QUERY) |
0b2c4be5 DS |
1239 | SYNTAX_ERROR_AT (op.loc, |
1240 | " ':' without preceding '?'"); | |
91318908 | 1241 | if (!num_zerop (top[-1].value)) /* Was '?' condition true? */ |
87ed109f NB |
1242 | pfile->state.skip_eval++; |
1243 | else | |
1244 | pfile->state.skip_eval--; | |
1245 | default: | |
1246 | break; | |
4063b943 | 1247 | } |
87ed109f | 1248 | |
f8b954fc | 1249 | want_value = true; |
4063b943 | 1250 | |
0f41302f | 1251 | /* Check for and handle stack overflow. */ |
87ed109f NB |
1252 | if (++top == pfile->op_limit) |
1253 | top = _cpp_expand_op_stack (pfile); | |
df383483 | 1254 | |
7f2935c7 | 1255 | top->op = op.op; |
68e65275 | 1256 | top->token = op.token; |
0b2c4be5 | 1257 | top->loc = op.loc; |
7f2935c7 | 1258 | } |
9ee70313 | 1259 | |
6d18adbc NB |
1260 | /* The controlling macro expression is only valid if we called lex 3 |
1261 | times: <!> <defined expression> and <EOF>. push_conditional () | |
1262 | checks that we are at top-of-file. */ | |
1263 | if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3)) | |
1264 | pfile->mi_ind_cmacro = 0; | |
1265 | ||
87ed109f | 1266 | if (top != pfile->op_stack) |
ebef4e8c | 1267 | { |
0b2c4be5 DS |
1268 | cpp_error_with_line (pfile, CPP_DL_ICE, top->loc, 0, |
1269 | "unbalanced stack in %s", | |
1270 | is_if ? "#if" : "#elif"); | |
4063b943 | 1271 | syntax_error: |
87ed109f | 1272 | return false; /* Return false on syntax error. */ |
4063b943 | 1273 | } |
9ee70313 | 1274 | |
91318908 | 1275 | return !num_zerop (top->value); |
87ed109f NB |
1276 | } |
1277 | ||
1278 | /* Reduce the operator / value stack if possible, in preparation for | |
1279 | pushing operator OP. Returns NULL on error, otherwise the top of | |
1280 | the stack. */ | |
1281 | static struct op * | |
6cf87ca4 | 1282 | reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) |
87ed109f NB |
1283 | { |
1284 | unsigned int prio; | |
1285 | ||
91318908 NB |
1286 | if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) |
1287 | { | |
1288 | bad_op: | |
0527bc4e | 1289 | cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); |
91318908 NB |
1290 | return 0; |
1291 | } | |
1292 | ||
87ed109f NB |
1293 | if (op == CPP_OPEN_PAREN) |
1294 | return top; | |
1295 | ||
1296 | /* Decrement the priority of left-associative operators to force a | |
1297 | reduction with operators of otherwise equal priority. */ | |
1298 | prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0); | |
1299 | while (prio < optab[top->op].prio) | |
1300 | { | |
68e65275 NB |
1301 | if (CPP_OPTION (pfile, warn_num_sign_change) |
1302 | && optab[top->op].flags & CHECK_PROMOTION) | |
1303 | check_promotion (pfile, top); | |
1304 | ||
75aef48a NB |
1305 | switch (top->op) |
1306 | { | |
1307 | case CPP_UPLUS: | |
1308 | case CPP_UMINUS: | |
1309 | case CPP_NOT: | |
1310 | case CPP_COMPL: | |
1311 | top[-1].value = num_unary_op (pfile, top->value, top->op); | |
47960aaf | 1312 | top[-1].loc = top->loc; |
75aef48a | 1313 | break; |
87ed109f | 1314 | |
75aef48a NB |
1315 | case CPP_PLUS: |
1316 | case CPP_MINUS: | |
1317 | case CPP_RSHIFT: | |
1318 | case CPP_LSHIFT: | |
75aef48a NB |
1319 | case CPP_COMMA: |
1320 | top[-1].value = num_binary_op (pfile, top[-1].value, | |
1321 | top->value, top->op); | |
47960aaf | 1322 | top[-1].loc = top->loc; |
75aef48a | 1323 | break; |
91318908 | 1324 | |
75aef48a NB |
1325 | case CPP_GREATER: |
1326 | case CPP_LESS: | |
1327 | case CPP_GREATER_EQ: | |
1328 | case CPP_LESS_EQ: | |
1329 | top[-1].value | |
1330 | = num_inequality_op (pfile, top[-1].value, top->value, top->op); | |
47960aaf | 1331 | top[-1].loc = top->loc; |
75aef48a | 1332 | break; |
91318908 | 1333 | |
75aef48a NB |
1334 | case CPP_EQ_EQ: |
1335 | case CPP_NOT_EQ: | |
1336 | top[-1].value | |
1337 | = num_equality_op (pfile, top[-1].value, top->value, top->op); | |
47960aaf | 1338 | top[-1].loc = top->loc; |
75aef48a | 1339 | break; |
87ed109f | 1340 | |
75aef48a NB |
1341 | case CPP_AND: |
1342 | case CPP_OR: | |
1343 | case CPP_XOR: | |
1344 | top[-1].value | |
1345 | = num_bitwise_op (pfile, top[-1].value, top->value, top->op); | |
47960aaf | 1346 | top[-1].loc = top->loc; |
75aef48a | 1347 | break; |
91318908 | 1348 | |
75aef48a NB |
1349 | case CPP_MULT: |
1350 | top[-1].value = num_mul (pfile, top[-1].value, top->value); | |
47960aaf | 1351 | top[-1].loc = top->loc; |
75aef48a | 1352 | break; |
ad28cff7 | 1353 | |
75aef48a NB |
1354 | case CPP_DIV: |
1355 | case CPP_MOD: | |
1356 | top[-1].value = num_div_op (pfile, top[-1].value, | |
b506a5a2 | 1357 | top->value, top->op, top->loc); |
47960aaf | 1358 | top[-1].loc = top->loc; |
75aef48a | 1359 | break; |
ad28cff7 | 1360 | |
75aef48a NB |
1361 | case CPP_OR_OR: |
1362 | top--; | |
1363 | if (!num_zerop (top->value)) | |
1364 | pfile->state.skip_eval--; | |
1365 | top->value.low = (!num_zerop (top->value) | |
1366 | || !num_zerop (top[1].value)); | |
1367 | top->value.high = 0; | |
1368 | top->value.unsignedp = false; | |
1369 | top->value.overflow = false; | |
47960aaf | 1370 | top->loc = top[1].loc; |
75aef48a NB |
1371 | continue; |
1372 | ||
1373 | case CPP_AND_AND: | |
1374 | top--; | |
1375 | if (num_zerop (top->value)) | |
1376 | pfile->state.skip_eval--; | |
1377 | top->value.low = (!num_zerop (top->value) | |
1378 | && !num_zerop (top[1].value)); | |
1379 | top->value.high = 0; | |
1380 | top->value.unsignedp = false; | |
1381 | top->value.overflow = false; | |
47960aaf | 1382 | top->loc = top[1].loc; |
75aef48a NB |
1383 | continue; |
1384 | ||
1385 | case CPP_OPEN_PAREN: | |
1386 | if (op != CPP_CLOSE_PAREN) | |
1387 | { | |
47960aaf MLI |
1388 | cpp_error_with_line (pfile, CPP_DL_ERROR, |
1389 | top->token->src_loc, | |
1390 | 0, "missing ')' in expression"); | |
75aef48a NB |
1391 | return 0; |
1392 | } | |
1393 | top--; | |
1394 | top->value = top[1].value; | |
47960aaf | 1395 | top->loc = top[1].loc; |
75aef48a NB |
1396 | return top; |
1397 | ||
1398 | case CPP_COLON: | |
1399 | top -= 2; | |
1400 | if (!num_zerop (top->value)) | |
1401 | { | |
ad28cff7 | 1402 | pfile->state.skip_eval--; |
75aef48a | 1403 | top->value = top[1].value; |
47960aaf | 1404 | top->loc = top[1].loc; |
75aef48a NB |
1405 | } |
1406 | else | |
47960aaf MLI |
1407 | { |
1408 | top->value = top[2].value; | |
1409 | top->loc = top[2].loc; | |
1410 | } | |
75aef48a NB |
1411 | top->value.unsignedp = (top[1].value.unsignedp |
1412 | || top[2].value.unsignedp); | |
1413 | continue; | |
1414 | ||
1415 | case CPP_QUERY: | |
71c10038 TT |
1416 | /* COMMA and COLON should not reduce a QUERY operator. */ |
1417 | if (op == CPP_COMMA || op == CPP_COLON) | |
1418 | return top; | |
0527bc4e | 1419 | cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); |
75aef48a NB |
1420 | return 0; |
1421 | ||
1422 | default: | |
1423 | goto bad_op; | |
1424 | } | |
91318908 | 1425 | |
ad28cff7 | 1426 | top--; |
91318908 | 1427 | if (top->value.overflow && !pfile->state.skip_eval) |
0527bc4e | 1428 | cpp_error (pfile, CPP_DL_PEDWARN, |
91318908 | 1429 | "integer overflow in preprocessor expression"); |
87ed109f NB |
1430 | } |
1431 | ||
1432 | if (op == CPP_CLOSE_PAREN) | |
1433 | { | |
0527bc4e | 1434 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); |
87ed109f NB |
1435 | return 0; |
1436 | } | |
1437 | ||
1438 | return top; | |
1439 | } | |
1440 | ||
1441 | /* Returns the position of the old top of stack after expansion. */ | |
1442 | struct op * | |
6cf87ca4 | 1443 | _cpp_expand_op_stack (cpp_reader *pfile) |
87ed109f | 1444 | { |
32fa4565 NB |
1445 | size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); |
1446 | size_t new_size = old_size * 2 + 20; | |
87ed109f | 1447 | |
c3f829c1 | 1448 | pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); |
32fa4565 | 1449 | pfile->op_limit = pfile->op_stack + new_size; |
87ed109f | 1450 | |
32fa4565 | 1451 | return pfile->op_stack + old_size; |
7f2935c7 | 1452 | } |
91318908 | 1453 | |
68e65275 NB |
1454 | /* Emits a warning if the effective sign of either operand of OP |
1455 | changes because of integer promotions. */ | |
1456 | static void | |
6cf87ca4 | 1457 | check_promotion (cpp_reader *pfile, const struct op *op) |
68e65275 NB |
1458 | { |
1459 | if (op->value.unsignedp == op[-1].value.unsignedp) | |
1460 | return; | |
1461 | ||
1462 | if (op->value.unsignedp) | |
1463 | { | |
1464 | if (!num_positive (op[-1].value, CPP_OPTION (pfile, precision))) | |
47960aaf MLI |
1465 | cpp_error_with_line (pfile, CPP_DL_WARNING, op[-1].loc, 0, |
1466 | "the left operand of \"%s\" changes sign when promoted", | |
1467 | cpp_token_as_text (pfile, op->token)); | |
68e65275 NB |
1468 | } |
1469 | else if (!num_positive (op->value, CPP_OPTION (pfile, precision))) | |
47960aaf | 1470 | cpp_error_with_line (pfile, CPP_DL_WARNING, op->loc, 0, |
68e65275 NB |
1471 | "the right operand of \"%s\" changes sign when promoted", |
1472 | cpp_token_as_text (pfile, op->token)); | |
1473 | } | |
1474 | ||
91318908 NB |
1475 | /* Clears the unused high order bits of the number pointed to by PNUM. */ |
1476 | static cpp_num | |
6cf87ca4 | 1477 | num_trim (cpp_num num, size_t precision) |
91318908 NB |
1478 | { |
1479 | if (precision > PART_PRECISION) | |
1480 | { | |
1481 | precision -= PART_PRECISION; | |
1482 | if (precision < PART_PRECISION) | |
359b0bec | 1483 | num.high &= ((cpp_num_part) 1 << precision) - 1; |
91318908 NB |
1484 | } |
1485 | else | |
1486 | { | |
1487 | if (precision < PART_PRECISION) | |
359b0bec | 1488 | num.low &= ((cpp_num_part) 1 << precision) - 1; |
91318908 NB |
1489 | num.high = 0; |
1490 | } | |
1491 | ||
1492 | return num; | |
1493 | } | |
1494 | ||
1495 | /* True iff A (presumed signed) >= 0. */ | |
1496 | static bool | |
6cf87ca4 | 1497 | num_positive (cpp_num num, size_t precision) |
91318908 NB |
1498 | { |
1499 | if (precision > PART_PRECISION) | |
1500 | { | |
1501 | precision -= PART_PRECISION; | |
359b0bec | 1502 | return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; |
91318908 NB |
1503 | } |
1504 | ||
359b0bec | 1505 | return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; |
91318908 NB |
1506 | } |
1507 | ||
ceeedfc1 NB |
1508 | /* Sign extend a number, with PRECISION significant bits and all |
1509 | others assumed clear, to fill out a cpp_num structure. */ | |
1510 | cpp_num | |
6cf87ca4 | 1511 | cpp_num_sign_extend (cpp_num num, size_t precision) |
ceeedfc1 NB |
1512 | { |
1513 | if (!num.unsignedp) | |
1514 | { | |
1515 | if (precision > PART_PRECISION) | |
1516 | { | |
1517 | precision -= PART_PRECISION; | |
1518 | if (precision < PART_PRECISION | |
1519 | && (num.high & (cpp_num_part) 1 << (precision - 1))) | |
1520 | num.high |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); | |
1521 | } | |
1522 | else if (num.low & (cpp_num_part) 1 << (precision - 1)) | |
1523 | { | |
1524 | if (precision < PART_PRECISION) | |
1525 | num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); | |
1526 | num.high = ~(cpp_num_part) 0; | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | return num; | |
1531 | } | |
1532 | ||
91318908 NB |
1533 | /* Returns the negative of NUM. */ |
1534 | static cpp_num | |
6cf87ca4 | 1535 | num_negate (cpp_num num, size_t precision) |
91318908 NB |
1536 | { |
1537 | cpp_num copy; | |
1538 | ||
1539 | copy = num; | |
1540 | num.high = ~num.high; | |
1541 | num.low = ~num.low; | |
1542 | if (++num.low == 0) | |
1543 | num.high++; | |
1544 | num = num_trim (num, precision); | |
1545 | num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); | |
1546 | ||
1547 | return num; | |
1548 | } | |
1549 | ||
1550 | /* Returns true if A >= B. */ | |
1551 | static bool | |
6cf87ca4 | 1552 | num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) |
91318908 NB |
1553 | { |
1554 | bool unsignedp; | |
1555 | ||
1556 | unsignedp = pa.unsignedp || pb.unsignedp; | |
1557 | ||
1558 | if (!unsignedp) | |
1559 | { | |
1560 | /* Both numbers have signed type. If they are of different | |
1561 | sign, the answer is the sign of A. */ | |
1562 | unsignedp = num_positive (pa, precision); | |
1563 | ||
1564 | if (unsignedp != num_positive (pb, precision)) | |
1565 | return unsignedp; | |
1566 | ||
1567 | /* Otherwise we can do an unsigned comparison. */ | |
1568 | } | |
1569 | ||
1570 | return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); | |
1571 | } | |
1572 | ||
1573 | /* Returns LHS OP RHS, where OP is a bit-wise operation. */ | |
1574 | static cpp_num | |
6cf87ca4 ZW |
1575 | num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
1576 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
91318908 NB |
1577 | { |
1578 | lhs.overflow = false; | |
1579 | lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1580 | ||
1581 | /* As excess precision is zeroed, there is no need to num_trim () as | |
1582 | these operations cannot introduce a set bit there. */ | |
1583 | if (op == CPP_AND) | |
1584 | { | |
1585 | lhs.low &= rhs.low; | |
1586 | lhs.high &= rhs.high; | |
1587 | } | |
1588 | else if (op == CPP_OR) | |
1589 | { | |
1590 | lhs.low |= rhs.low; | |
1591 | lhs.high |= rhs.high; | |
1592 | } | |
1593 | else | |
1594 | { | |
1595 | lhs.low ^= rhs.low; | |
1596 | lhs.high ^= rhs.high; | |
1597 | } | |
1598 | ||
1599 | return lhs; | |
1600 | } | |
1601 | ||
1602 | /* Returns LHS OP RHS, where OP is an inequality. */ | |
1603 | static cpp_num | |
6cf87ca4 ZW |
1604 | num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, |
1605 | enum cpp_ttype op) | |
91318908 NB |
1606 | { |
1607 | bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); | |
1608 | ||
1609 | if (op == CPP_GREATER_EQ) | |
1610 | lhs.low = gte; | |
1611 | else if (op == CPP_LESS) | |
1612 | lhs.low = !gte; | |
1613 | else if (op == CPP_GREATER) | |
1614 | lhs.low = gte && !num_eq (lhs, rhs); | |
1615 | else /* CPP_LESS_EQ. */ | |
1616 | lhs.low = !gte || num_eq (lhs, rhs); | |
1617 | ||
1618 | lhs.high = 0; | |
1619 | lhs.overflow = false; | |
1620 | lhs.unsignedp = false; | |
1621 | return lhs; | |
1622 | } | |
1623 | ||
1624 | /* Returns LHS OP RHS, where OP is == or !=. */ | |
1625 | static cpp_num | |
6cf87ca4 ZW |
1626 | num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, |
1627 | cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
91318908 | 1628 | { |
97459791 JM |
1629 | /* Work around a 3.0.4 bug; see PR 6950. */ |
1630 | bool eq = num_eq (lhs, rhs); | |
91318908 | 1631 | if (op == CPP_NOT_EQ) |
97459791 JM |
1632 | eq = !eq; |
1633 | lhs.low = eq; | |
91318908 NB |
1634 | lhs.high = 0; |
1635 | lhs.overflow = false; | |
1636 | lhs.unsignedp = false; | |
1637 | return lhs; | |
1638 | } | |
1639 | ||
1640 | /* Shift NUM, of width PRECISION, right by N bits. */ | |
1641 | static cpp_num | |
6cf87ca4 | 1642 | num_rshift (cpp_num num, size_t precision, size_t n) |
91318908 NB |
1643 | { |
1644 | cpp_num_part sign_mask; | |
6de9cd9a | 1645 | bool x = num_positive (num, precision); |
91318908 | 1646 | |
6de9cd9a | 1647 | if (num.unsignedp || x) |
91318908 NB |
1648 | sign_mask = 0; |
1649 | else | |
1650 | sign_mask = ~(cpp_num_part) 0; | |
1651 | ||
1652 | if (n >= precision) | |
1653 | num.high = num.low = sign_mask; | |
1654 | else | |
1655 | { | |
1656 | /* Sign-extend. */ | |
1657 | if (precision < PART_PRECISION) | |
1658 | num.high = sign_mask, num.low |= sign_mask << precision; | |
1659 | else if (precision < 2 * PART_PRECISION) | |
1660 | num.high |= sign_mask << (precision - PART_PRECISION); | |
1661 | ||
1662 | if (n >= PART_PRECISION) | |
1663 | { | |
1664 | n -= PART_PRECISION; | |
1665 | num.low = num.high; | |
1666 | num.high = sign_mask; | |
1667 | } | |
1668 | ||
1669 | if (n) | |
1670 | { | |
1671 | num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); | |
1672 | num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | num = num_trim (num, precision); | |
1677 | num.overflow = false; | |
1678 | return num; | |
1679 | } | |
1680 | ||
1681 | /* Shift NUM, of width PRECISION, left by N bits. */ | |
1682 | static cpp_num | |
6cf87ca4 | 1683 | num_lshift (cpp_num num, size_t precision, size_t n) |
91318908 NB |
1684 | { |
1685 | if (n >= precision) | |
1686 | { | |
1687 | num.overflow = !num.unsignedp && !num_zerop (num); | |
1688 | num.high = num.low = 0; | |
1689 | } | |
1690 | else | |
1691 | { | |
1692 | cpp_num orig, maybe_orig; | |
1693 | size_t m = n; | |
1694 | ||
1695 | orig = num; | |
1696 | if (m >= PART_PRECISION) | |
1697 | { | |
1698 | m -= PART_PRECISION; | |
1699 | num.high = num.low; | |
1700 | num.low = 0; | |
1701 | } | |
1702 | if (m) | |
1703 | { | |
1704 | num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); | |
1705 | num.low <<= m; | |
1706 | } | |
1707 | num = num_trim (num, precision); | |
1708 | ||
1709 | if (num.unsignedp) | |
1710 | num.overflow = false; | |
1711 | else | |
1712 | { | |
1713 | maybe_orig = num_rshift (num, precision, n); | |
1714 | num.overflow = !num_eq (orig, maybe_orig); | |
1715 | } | |
1716 | } | |
1717 | ||
1718 | return num; | |
1719 | } | |
1720 | ||
1721 | /* The four unary operators: +, -, ! and ~. */ | |
1722 | static cpp_num | |
6cf87ca4 | 1723 | num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) |
91318908 NB |
1724 | { |
1725 | switch (op) | |
1726 | { | |
1727 | case CPP_UPLUS: | |
75aef48a | 1728 | if (CPP_WTRADITIONAL (pfile) && !pfile->state.skip_eval) |
87cf0651 SB |
1729 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
1730 | "traditional C rejects the unary plus operator"); | |
91318908 NB |
1731 | num.overflow = false; |
1732 | break; | |
1733 | ||
1734 | case CPP_UMINUS: | |
1735 | num = num_negate (num, CPP_OPTION (pfile, precision)); | |
1736 | break; | |
1737 | ||
1738 | case CPP_COMPL: | |
1739 | num.high = ~num.high; | |
1740 | num.low = ~num.low; | |
1741 | num = num_trim (num, CPP_OPTION (pfile, precision)); | |
1742 | num.overflow = false; | |
1743 | break; | |
1744 | ||
1745 | default: /* case CPP_NOT: */ | |
1746 | num.low = num_zerop (num); | |
1747 | num.high = 0; | |
1748 | num.overflow = false; | |
1749 | num.unsignedp = false; | |
1750 | break; | |
1751 | } | |
1752 | ||
1753 | return num; | |
1754 | } | |
1755 | ||
1756 | /* The various binary operators. */ | |
1757 | static cpp_num | |
6cf87ca4 | 1758 | num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) |
91318908 NB |
1759 | { |
1760 | cpp_num result; | |
1761 | size_t precision = CPP_OPTION (pfile, precision); | |
91318908 NB |
1762 | size_t n; |
1763 | ||
1764 | switch (op) | |
1765 | { | |
1766 | /* Shifts. */ | |
1767 | case CPP_LSHIFT: | |
1768 | case CPP_RSHIFT: | |
1769 | if (!rhs.unsignedp && !num_positive (rhs, precision)) | |
1770 | { | |
1771 | /* A negative shift is a positive shift the other way. */ | |
1772 | if (op == CPP_LSHIFT) | |
1773 | op = CPP_RSHIFT; | |
1774 | else | |
1775 | op = CPP_LSHIFT; | |
1776 | rhs = num_negate (rhs, precision); | |
1777 | } | |
1778 | if (rhs.high) | |
1779 | n = ~0; /* Maximal. */ | |
1780 | else | |
1781 | n = rhs.low; | |
1782 | if (op == CPP_LSHIFT) | |
1783 | lhs = num_lshift (lhs, precision, n); | |
1784 | else | |
1785 | lhs = num_rshift (lhs, precision, n); | |
1786 | break; | |
1787 | ||
91318908 NB |
1788 | /* Arithmetic. */ |
1789 | case CPP_MINUS: | |
1790 | rhs = num_negate (rhs, precision); | |
1791 | case CPP_PLUS: | |
1792 | result.low = lhs.low + rhs.low; | |
1793 | result.high = lhs.high + rhs.high; | |
1794 | if (result.low < lhs.low) | |
1795 | result.high++; | |
6de9cd9a DN |
1796 | result.unsignedp = lhs.unsignedp || rhs.unsignedp; |
1797 | result.overflow = false; | |
91318908 NB |
1798 | |
1799 | result = num_trim (result, precision); | |
6de9cd9a | 1800 | if (!result.unsignedp) |
91318908 NB |
1801 | { |
1802 | bool lhsp = num_positive (lhs, precision); | |
1803 | result.overflow = (lhsp == num_positive (rhs, precision) | |
1804 | && lhsp != num_positive (result, precision)); | |
1805 | } | |
1806 | return result; | |
1807 | ||
1808 | /* Comma. */ | |
1809 | default: /* case CPP_COMMA: */ | |
32e8aa9a JM |
1810 | if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99) |
1811 | || !pfile->state.skip_eval)) | |
0527bc4e | 1812 | cpp_error (pfile, CPP_DL_PEDWARN, |
91318908 NB |
1813 | "comma operator in operand of #if"); |
1814 | lhs = rhs; | |
1815 | break; | |
1816 | } | |
1817 | ||
1818 | return lhs; | |
1819 | } | |
1820 | ||
1821 | /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This | |
1822 | cannot overflow. */ | |
1823 | static cpp_num | |
6cf87ca4 | 1824 | num_part_mul (cpp_num_part lhs, cpp_num_part rhs) |
91318908 NB |
1825 | { |
1826 | cpp_num result; | |
1827 | cpp_num_part middle[2], temp; | |
1828 | ||
1829 | result.low = LOW_PART (lhs) * LOW_PART (rhs); | |
1830 | result.high = HIGH_PART (lhs) * HIGH_PART (rhs); | |
1831 | ||
1832 | middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); | |
1833 | middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); | |
1834 | ||
1835 | temp = result.low; | |
1836 | result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); | |
1837 | if (result.low < temp) | |
1838 | result.high++; | |
1839 | ||
1840 | temp = result.low; | |
1841 | result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); | |
1842 | if (result.low < temp) | |
1843 | result.high++; | |
1844 | ||
1845 | result.high += HIGH_PART (middle[0]); | |
1846 | result.high += HIGH_PART (middle[1]); | |
6de9cd9a DN |
1847 | result.unsignedp = true; |
1848 | result.overflow = false; | |
91318908 NB |
1849 | |
1850 | return result; | |
1851 | } | |
1852 | ||
1853 | /* Multiply two preprocessing numbers. */ | |
1854 | static cpp_num | |
6cf87ca4 | 1855 | num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) |
91318908 NB |
1856 | { |
1857 | cpp_num result, temp; | |
1858 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1859 | bool overflow, negate = false; | |
1860 | size_t precision = CPP_OPTION (pfile, precision); | |
1861 | ||
1862 | /* Prepare for unsigned multiplication. */ | |
1863 | if (!unsignedp) | |
1864 | { | |
1865 | if (!num_positive (lhs, precision)) | |
1866 | negate = !negate, lhs = num_negate (lhs, precision); | |
1867 | if (!num_positive (rhs, precision)) | |
1868 | negate = !negate, rhs = num_negate (rhs, precision); | |
1869 | } | |
1870 | ||
1871 | overflow = lhs.high && rhs.high; | |
1872 | result = num_part_mul (lhs.low, rhs.low); | |
1873 | ||
1874 | temp = num_part_mul (lhs.high, rhs.low); | |
1875 | result.high += temp.low; | |
1876 | if (temp.high) | |
1877 | overflow = true; | |
1878 | ||
1879 | temp = num_part_mul (lhs.low, rhs.high); | |
1880 | result.high += temp.low; | |
1881 | if (temp.high) | |
1882 | overflow = true; | |
1883 | ||
1884 | temp.low = result.low, temp.high = result.high; | |
1885 | result = num_trim (result, precision); | |
1886 | if (!num_eq (result, temp)) | |
1887 | overflow = true; | |
1888 | ||
1889 | if (negate) | |
1890 | result = num_negate (result, precision); | |
1891 | ||
1892 | if (unsignedp) | |
1893 | result.overflow = false; | |
1894 | else | |
1895 | result.overflow = overflow || (num_positive (result, precision) ^ !negate | |
1896 | && !num_zerop (result)); | |
1897 | result.unsignedp = unsignedp; | |
1898 | ||
1899 | return result; | |
1900 | } | |
1901 | ||
b506a5a2 MLI |
1902 | /* Divide two preprocessing numbers, LHS and RHS, returning the answer |
1903 | or the remainder depending upon OP. LOCATION is the source location | |
1904 | of this operator (for diagnostics). */ | |
1905 | ||
91318908 | 1906 | static cpp_num |
b506a5a2 MLI |
1907 | num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, |
1908 | source_location location) | |
91318908 NB |
1909 | { |
1910 | cpp_num result, sub; | |
1911 | cpp_num_part mask; | |
1912 | bool unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1913 | bool negate = false, lhs_neg = false; | |
1914 | size_t i, precision = CPP_OPTION (pfile, precision); | |
1915 | ||
1916 | /* Prepare for unsigned division. */ | |
1917 | if (!unsignedp) | |
1918 | { | |
1919 | if (!num_positive (lhs, precision)) | |
1920 | negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision); | |
1921 | if (!num_positive (rhs, precision)) | |
1922 | negate = !negate, rhs = num_negate (rhs, precision); | |
1923 | } | |
1924 | ||
1925 | /* Find the high bit. */ | |
1926 | if (rhs.high) | |
1927 | { | |
1928 | i = precision - 1; | |
359b0bec | 1929 | mask = (cpp_num_part) 1 << (i - PART_PRECISION); |
91318908 NB |
1930 | for (; ; i--, mask >>= 1) |
1931 | if (rhs.high & mask) | |
1932 | break; | |
1933 | } | |
1934 | else if (rhs.low) | |
1935 | { | |
1936 | if (precision > PART_PRECISION) | |
1937 | i = precision - PART_PRECISION - 1; | |
1938 | else | |
1939 | i = precision - 1; | |
359b0bec | 1940 | mask = (cpp_num_part) 1 << i; |
91318908 NB |
1941 | for (; ; i--, mask >>= 1) |
1942 | if (rhs.low & mask) | |
1943 | break; | |
1944 | } | |
1945 | else | |
1946 | { | |
75aef48a | 1947 | if (!pfile->state.skip_eval) |
b506a5a2 MLI |
1948 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
1949 | "division by zero in #if"); | |
91318908 NB |
1950 | return lhs; |
1951 | } | |
1952 | ||
da7d8304 | 1953 | /* First nonzero bit of RHS is bit I. Do naive division by |
91318908 NB |
1954 | shifting the RHS fully left, and subtracting from LHS if LHS is |
1955 | at least as big, and then repeating but with one less shift. | |
1956 | This is not very efficient, but is easy to understand. */ | |
1957 | ||
1958 | rhs.unsignedp = true; | |
1959 | lhs.unsignedp = true; | |
1960 | i = precision - i - 1; | |
1961 | sub = num_lshift (rhs, precision, i); | |
1962 | ||
1963 | result.high = result.low = 0; | |
1964 | for (;;) | |
1965 | { | |
1966 | if (num_greater_eq (lhs, sub, precision)) | |
1967 | { | |
1968 | lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS); | |
1969 | if (i >= PART_PRECISION) | |
359b0bec | 1970 | result.high |= (cpp_num_part) 1 << (i - PART_PRECISION); |
91318908 | 1971 | else |
359b0bec | 1972 | result.low |= (cpp_num_part) 1 << i; |
91318908 NB |
1973 | } |
1974 | if (i-- == 0) | |
1975 | break; | |
1976 | sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); | |
1977 | sub.high >>= 1; | |
1978 | } | |
1979 | ||
1980 | /* We divide so that the remainder has the sign of the LHS. */ | |
1981 | if (op == CPP_DIV) | |
1982 | { | |
1983 | result.unsignedp = unsignedp; | |
6de9cd9a DN |
1984 | result.overflow = false; |
1985 | if (!unsignedp) | |
91318908 NB |
1986 | { |
1987 | if (negate) | |
1988 | result = num_negate (result, precision); | |
22a8a52d EC |
1989 | result.overflow = (num_positive (result, precision) ^ !negate |
1990 | && !num_zerop (result)); | |
91318908 NB |
1991 | } |
1992 | ||
1993 | return result; | |
1994 | } | |
1995 | ||
1996 | /* CPP_MOD. */ | |
1997 | lhs.unsignedp = unsignedp; | |
1998 | lhs.overflow = false; | |
1999 | if (lhs_neg) | |
2000 | lhs = num_negate (lhs, precision); | |
2001 | ||
2002 | return lhs; | |
2003 | } |