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