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