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