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