]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppexp.c
builtins.c (expand_builtin_setjmp): Set current_function_calls_setjmp.
[thirdparty/gcc.git] / gcc / cppexp.c
CommitLineData
b0699dad 1/* Parse C expressions for cpplib.
525bc95d 2 Copyright (C) 1987, 92, 94, 95, 97, 98, 1999, 2000 Free Software Foundation.
e38992e8 3 Contributed by Per Bothner, 1994.
7f2935c7
PB
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
940d9d63 17Foundation, 59 Temple Place - Suite 330,
e38992e8 18Boston, MA 02111-1307, USA. */
7f2935c7
PB
19
20/* Parse a C expression from text in a string */
21
22#include "config.h"
b04cd507 23#include "system.h"
487a6e06 24#include "cpplib.h"
88ae23e7 25#include "cpphash.h"
d8ea8f28 26#include "defaults.h"
7f2935c7
PB
27
28#ifndef MAX_CHAR_TYPE_SIZE
29#define MAX_CHAR_TYPE_SIZE CHAR_TYPE_SIZE
30#endif
31
32#ifndef MAX_INT_TYPE_SIZE
33#define MAX_INT_TYPE_SIZE INT_TYPE_SIZE
34#endif
35
36#ifndef MAX_LONG_TYPE_SIZE
37#define MAX_LONG_TYPE_SIZE LONG_TYPE_SIZE
38#endif
39
40#ifndef MAX_WCHAR_TYPE_SIZE
41#define MAX_WCHAR_TYPE_SIZE WCHAR_TYPE_SIZE
42#endif
43
e915b770 44#define MAX_CHAR_TYPE_MASK (MAX_CHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
b22ef131
NB
45 ? (~(~(HOST_WIDEST_INT) 0 << MAX_CHAR_TYPE_SIZE)) \
46 : ~ (HOST_WIDEST_INT) 0)
f1a86df6 47
e915b770 48#define MAX_WCHAR_TYPE_MASK (MAX_WCHAR_TYPE_SIZE < HOST_BITS_PER_WIDEST_INT \
b0699dad 49 ? ~(~(HOST_WIDEST_INT) 0 << MAX_WCHAR_TYPE_SIZE) \
e915b770 50 : ~ (HOST_WIDEST_INT) 0)
f1a86df6 51
7f2935c7
PB
52/* Yield nonzero if adding two numbers with A's and B's signs can yield a
53 number with SUM's sign, where A, B, and SUM are all C integers. */
54#define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
55
b04cd507 56static void integer_overflow PARAMS ((cpp_reader *));
b0699dad 57static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
e23c0ba3
ZW
58 unsigned int,
59 unsigned HOST_WIDEST_INT));
b0699dad 60static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
e23c0ba3
ZW
61 unsigned int,
62 unsigned HOST_WIDEST_INT));
cf00a885
ZW
63static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
64static struct op parse_charconst PARAMS ((cpp_reader *, const cpp_token *));
65static struct op parse_defined PARAMS ((cpp_reader *));
cf00a885
ZW
66static HOST_WIDEST_INT parse_escape PARAMS ((cpp_reader *, const U_CHAR **,
67 const U_CHAR *, HOST_WIDEST_INT));
93c80368
NB
68static struct op lex PARAMS ((cpp_reader *, int, cpp_token *));
69static const unsigned char *op_as_text PARAMS ((cpp_reader *, enum cpp_ttype));
cf00a885
ZW
70
71struct op
b0699dad 72{
cf00a885 73 enum cpp_ttype op;
4063b943 74 U_CHAR prio; /* Priority of op. */
e23c0ba3 75 U_CHAR flags;
4063b943
NB
76 U_CHAR unsignedp; /* True if value should be treated as unsigned. */
77 HOST_WIDEST_INT value; /* The value logically "right" of op. */
7f2935c7 78};
7f2935c7 79
cf00a885
ZW
80/* There is no "error" token, but we can't get comments in #if, so we can
81 abuse that token type. */
82#define CPP_ERROR CPP_COMMENT
83
15dad1d9
ZW
84/* With -O2, gcc appears to produce nice code, moving the error
85 message load and subsequent jump completely out of the main path. */
86#define CPP_ICE(msgid) \
87 do { cpp_ice (pfile, msgid); goto syntax_error; } while(0)
88#define SYNTAX_ERROR(msgid) \
89 do { cpp_error (pfile, msgid); goto syntax_error; } while(0)
90#define SYNTAX_ERROR2(msgid, arg) \
91 do { cpp_error (pfile, msgid, arg); goto syntax_error; } while(0)
92
f13eb63a
ZW
93/* Parse and convert an integer for #if. Accepts decimal, hex, or octal
94 with or without size suffixes. */
cf00a885
ZW
95struct suffix
96{
97 unsigned char s[4];
98 unsigned char u;
99 unsigned char l;
100};
101
102const struct suffix vsuf_1[] = {
103 { "u", 1, 0 }, { "U", 1, 0 },
104 { "l", 0, 1 }, { "L", 0, 1 }
105};
106
107const struct suffix vsuf_2[] = {
108 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
109 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
110 { "ll", 0, 2 }, { "LL", 0, 2 }
111};
7f2935c7 112
cf00a885
ZW
113const struct suffix vsuf_3[] = {
114 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
115 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
116};
117#define Nsuff(tab) (sizeof tab / sizeof (struct suffix))
118
119static struct op
120parse_number (pfile, tok)
7f2935c7 121 cpp_reader *pfile;
cf00a885 122 const cpp_token *tok;
7f2935c7 123{
cf00a885 124 struct op op;
bfb9dc7f
ZW
125 const U_CHAR *start = tok->val.str.text;
126 const U_CHAR *end = start + tok->val.str.len;
cf00a885 127 const U_CHAR *p = start;
bfb9dc7f 128 int c = 0, i, nsuff;
e915b770 129 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
f13eb63a
ZW
130 int base = 10;
131 int overflow = 0;
132 int digit, largest_digit = 0;
cf00a885 133 const struct suffix *sufftab;
7f2935c7
PB
134
135 op.unsignedp = 0;
136
f13eb63a
ZW
137 if (p[0] == '0')
138 {
139 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
140 {
141 p += 2;
142 base = 16;
143 }
144 else
145 {
146 p += 1;
147 base = 8;
148 }
7f2935c7
PB
149 }
150
0f41302f 151 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
e915b770
KG
152 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
153 / ((unsigned HOST_WIDEST_INT) base));
7f2935c7 154
cf00a885 155 for(; p < end; p++)
f13eb63a 156 {
cf00a885 157 c = *p;
f13eb63a
ZW
158
159 if (c >= '0' && c <= '9')
160 digit = c - '0';
cf00a885
ZW
161 /* We believe that in all live character sets, a-f are
162 consecutive, and so are A-F. */
b22ef131 163 else if (base == 16 && c >= 'a' && c <= 'f')
f13eb63a
ZW
164 digit = c - 'a' + 10;
165 else if (base == 16 && c >= 'A' && c <= 'F')
166 digit = c - 'A' + 10;
f13eb63a 167 else
cf00a885
ZW
168 break;
169
f13eb63a
ZW
170 if (largest_digit < digit)
171 largest_digit = digit;
172 nd = n * base + digit;
173 overflow |= MAX_over_base < n || nd < n;
174 n = nd;
7f2935c7 175 }
7f2935c7 176
cf00a885 177 if (p < end)
f13eb63a 178 {
cf00a885
ZW
179 /* Check for a floating point constant. Note that float constants
180 with an exponent or suffix but no decimal point are technically
5ef865d5 181 invalid (C99 6.4.4.2) but accepted elsewhere. */
cf00a885
ZW
182 if ((c == '.' || c == 'F' || c == 'f')
183 || (base == 10 && (c == 'E' || c == 'e')
184 && p+1 < end && (p[1] == '+' || p[1] == '-'))
185 || (base == 16 && (c == 'P' || c == 'p')
186 && p+1 < end && (p[1] == '+' || p[1] == '-')))
187 SYNTAX_ERROR ("floating point numbers are not valid in #if");
188
189 /* Determine the suffix. l means long, and u means unsigned.
190 See the suffix tables, above. */
191 switch (end - p)
192 {
193 case 1: sufftab = vsuf_1; nsuff = Nsuff(vsuf_1); break;
194 case 2: sufftab = vsuf_2; nsuff = Nsuff(vsuf_2); break;
195 case 3: sufftab = vsuf_3; nsuff = Nsuff(vsuf_3); break;
196 default: goto invalid_suffix;
197 }
198
199 for (i = 0; i < nsuff; i++)
200 if (memcmp (p, sufftab[i].s, end - p) == 0)
201 break;
202 if (i == nsuff)
203 goto invalid_suffix;
204 op.unsignedp = sufftab[i].u;
205
7da92c08
KG
206 if (CPP_WTRADITIONAL (pfile) && sufftab[i].u)
207 cpp_warning (pfile, "traditional C rejects the `U' suffix");
cf00a885
ZW
208 if (CPP_OPTION (pfile, c89) && sufftab[i].l == 2)
209 SYNTAX_ERROR ("too many 'l' suffixes in integer constant");
f13eb63a
ZW
210 }
211
7f2935c7 212 if (base <= largest_digit)
cf00a885 213 cpp_pedwarn (pfile, "integer constant contains digits beyond the radix");
7f2935c7
PB
214
215 if (overflow)
7c4033ff 216 cpp_pedwarn (pfile, "integer constant out of range");
7f2935c7
PB
217
218 /* If too big to be signed, consider it unsigned. */
e915b770 219 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
7f2935c7
PB
220 {
221 if (base == 10)
cf00a885 222 cpp_warning (pfile, "integer constant is so large that it is unsigned");
7f2935c7
PB
223 op.unsignedp = 1;
224 }
225
226 op.value = n;
cf00a885 227 op.op = CPP_INT;
7f2935c7 228 return op;
f13eb63a 229
cf00a885
ZW
230 invalid_suffix:
231 cpp_error (pfile, "invalid suffix '%.*s' on integer constant",
232 (int) (end - p), p);
233 syntax_error:
234 op.op = CPP_ERROR;
f13eb63a 235 return op;
7f2935c7
PB
236}
237
f13eb63a
ZW
238/* Parse and convert a character constant for #if. Understands backslash
239 escapes (\n, \031) and multibyte characters (if so configured). */
cf00a885
ZW
240static struct op
241parse_charconst (pfile, tok)
f13eb63a 242 cpp_reader *pfile;
cf00a885 243 const cpp_token *tok;
f13eb63a 244{
cf00a885 245 struct op op;
e915b770 246 HOST_WIDEST_INT result = 0;
f13eb63a
ZW
247 int num_chars = 0;
248 int num_bits;
249 unsigned int width = MAX_CHAR_TYPE_SIZE, mask = MAX_CHAR_TYPE_MASK;
250 int max_chars;
bfb9dc7f
ZW
251 const U_CHAR *ptr = tok->val.str.text;
252 const U_CHAR *end = ptr + tok->val.str.len;
f13eb63a 253
099a9dd0 254 int c = -1;
f13eb63a 255
cf00a885
ZW
256 if (tok->type == CPP_WCHAR)
257 width = MAX_WCHAR_TYPE_SIZE, mask = MAX_WCHAR_TYPE_MASK;
f13eb63a
ZW
258 max_chars = MAX_LONG_TYPE_SIZE / width;
259
f13eb63a
ZW
260 while (ptr < end)
261 {
f13eb63a 262 c = *ptr++;
64aaf407 263 if (c == '\'')
cf00a885 264 CPP_ICE ("unescaped ' in character constant");
f13eb63a
ZW
265 else if (c == '\\')
266 {
cf00a885 267 c = parse_escape (pfile, &ptr, end, mask);
f13eb63a
ZW
268 if (width < HOST_BITS_PER_INT
269 && (unsigned int) c >= (unsigned int)(1 << width))
b22ef131
NB
270 cpp_pedwarn (pfile,
271 "escape sequence out of range for character");
f13eb63a
ZW
272 }
273
274 /* Merge character into result; ignore excess chars. */
275 if (++num_chars <= max_chars)
276 {
277 if (width < HOST_BITS_PER_INT)
278 result = (result << width) | (c & ((1 << width) - 1));
279 else
280 result = c;
281 }
282 }
283
284 if (num_chars == 0)
cf00a885 285 SYNTAX_ERROR ("empty character constant");
f13eb63a 286 else if (num_chars > max_chars)
cf00a885 287 SYNTAX_ERROR ("character constant too long");
f9a0e96c 288 else if (num_chars != 1)
f13eb63a
ZW
289 cpp_warning (pfile, "multi-character character constant");
290
291 /* If char type is signed, sign-extend the constant. */
292 num_bits = num_chars * width;
293
93c80368 294 if (pfile->spec_nodes.n__CHAR_UNSIGNED__->type == NT_MACRO
f13eb63a 295 || ((result >> (num_bits - 1)) & 1) == 0)
ca261cb4
KG
296 op.value = result & ((unsigned HOST_WIDEST_INT) ~0
297 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
f13eb63a 298 else
ca261cb4
KG
299 op.value = result | ~((unsigned HOST_WIDEST_INT) ~0
300 >> (HOST_BITS_PER_WIDEST_INT - num_bits));
f13eb63a
ZW
301
302 /* This is always a signed type. */
303 op.unsignedp = 0;
cf00a885 304 op.op = CPP_INT;
f13eb63a
ZW
305 return op;
306
cf00a885
ZW
307 syntax_error:
308 op.op = CPP_ERROR;
f13eb63a
ZW
309 return op;
310}
311
cf00a885 312static struct op
ba412f14
ZW
313parse_defined (pfile)
314 cpp_reader *pfile;
315{
93c80368
NB
316 int paren = 0;
317 cpp_hashnode *node = 0;
318 cpp_token token;
cf00a885 319 struct op op;
ba412f14 320
93c80368
NB
321 /* Don't expand macros. */
322 pfile->state.prevent_expansion++;
323
324 _cpp_get_token (pfile, &token);
325 if (token.type == CPP_OPEN_PAREN)
ba412f14 326 {
cf00a885 327 paren = 1;
93c80368 328 _cpp_get_token (pfile, &token);
ba412f14
ZW
329 }
330
93c80368
NB
331 if (token.type == CPP_NAME)
332 {
333 node = token.val.node;
334 if (paren)
335 {
336 _cpp_get_token (pfile, &token);
337 if (token.type != CPP_CLOSE_PAREN)
338 {
339 cpp_error (pfile, "missing ')' after \"defined\"");
340 node = 0;
341 }
342 }
343 }
344 else
345 cpp_error (pfile, "\"defined\" without an identifier");
15dad1d9 346
93c80368
NB
347 if (!node)
348 op.op = CPP_ERROR;
349 else
15dad1d9 350 {
93c80368 351 op.value = node->type == NT_MACRO;
cf00a885 352 op.unsignedp = 0;
93c80368 353 op.op = CPP_INT;
15dad1d9 354
93c80368
NB
355 /* No macros? At top of file? */
356 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0
357 && pfile->mi_if_not_defined == MI_IND_NOT && pfile->mi_lexed == 1)
358 {
359 cpp_start_lookahead (pfile);
360 cpp_get_token (pfile, &token);
361 if (token.type == CPP_EOF)
362 pfile->mi_ind_cmacro = node;
363 cpp_stop_lookahead (pfile, 0);
364 }
15dad1d9 365 }
93c80368
NB
366
367 pfile->state.prevent_expansion--;
15dad1d9 368 return op;
15dad1d9
ZW
369}
370
0f41302f 371/* Read one token. */
7f2935c7 372
cf00a885 373static struct op
93c80368 374lex (pfile, skip_evaluation, token)
52529158 375 cpp_reader *pfile;
c9666c01 376 int skip_evaluation;
93c80368 377 cpp_token *token;
7f2935c7 378{
cf00a885 379 struct op op;
7f2935c7 380
93c80368 381 _cpp_get_token (pfile, token);
041c3194 382
93c80368 383 switch (token->type)
ba412f14 384 {
cf00a885 385 case CPP_INT:
7f2935c7 386 case CPP_NUMBER:
93c80368 387 return parse_number (pfile, token);
cf00a885
ZW
388 case CPP_CHAR:
389 case CPP_WCHAR:
93c80368 390 return parse_charconst (pfile, token);
cf00a885 391
7f2935c7 392 case CPP_STRING:
525bc95d 393 case CPP_WSTRING:
cf00a885 394 SYNTAX_ERROR ("string constants are not valid in #if");
525bc95d 395
cf00a885
ZW
396 case CPP_FLOAT:
397 SYNTAX_ERROR ("floating point numbers are not valid in #if");
398
399 case CPP_OTHER:
6c53ebff
NB
400 if (ISGRAPH (token->val.c))
401 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
cf00a885 402 else
6c53ebff 403 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
ba412f14 404
92936ecf 405 case CPP_NAME:
93c80368
NB
406 if (token->val.node == pfile->spec_nodes.n_defined)
407 {
408 if (pfile->context->prev && CPP_PEDANTIC (pfile))
409 cpp_pedwarn (pfile, "\"defined\" operator appears during macro expansion");
410
411 return parse_defined (pfile);
412 }
413 /* Controlling #if expressions cannot contain identifiers (they
414 could become macros in the future). */
415 pfile->mi_state = MI_FAILED;
416
cf00a885 417 op.op = CPP_INT;
f13eb63a
ZW
418 op.unsignedp = 0;
419 op.value = 0;
5dfa4da1 420
ae79697b 421 if (CPP_OPTION (pfile, warn_undef) && !skip_evaluation)
93c80368
NB
422 cpp_warning (pfile, "\"%s\" is not defined", token->val.node->name);
423
5dfa4da1
ZW
424 return op;
425
15dad1d9 426 case CPP_HASH:
93c80368
NB
427 {
428 int temp;
429
430 op.op = CPP_INT;
431 if (_cpp_test_assertion (pfile, &temp))
432 op.op = CPP_ERROR;
433 op.unsignedp = 0;
434 op.value = temp;
435 return op;
436 }
437
438 case CPP_NOT:
439 /* We don't worry about its position here. */
440 pfile->mi_if_not_defined = MI_IND_NOT;
441 /* Fall through. */
7f2935c7 442
041c3194 443 default:
93c80368
NB
444 if ((token->type > CPP_EQ && token->type < CPP_PLUS_EQ)
445 || token->type == CPP_EOF)
cf00a885 446 {
93c80368 447 op.op = token->type;
7f2935c7
PB
448 return op;
449 }
7f2935c7 450
93c80368
NB
451 SYNTAX_ERROR2 ("\"%s\" is not valid in #if expressions",
452 cpp_token_as_text (pfile, token));
453 }
7c3bb1de 454
cf00a885
ZW
455 syntax_error:
456 op.op = CPP_ERROR;
457 return op;
7c3bb1de 458}
7f2935c7
PB
459
460/* Parse a C escape sequence. STRING_PTR points to a variable
461 containing a pointer to the string to parse. That pointer
462 is updated past the characters we use. The value of the
463 escape sequence is returned.
464
7f2935c7
PB
465 If \ is followed by 000, we return 0 and leave the string pointer
466 after the zeros. A value of 0 does not mean end of string. */
467
a2c8e144 468static HOST_WIDEST_INT
cf00a885 469parse_escape (pfile, string_ptr, limit, result_mask)
7f2935c7 470 cpp_reader *pfile;
cf00a885
ZW
471 const U_CHAR **string_ptr;
472 const U_CHAR *limit;
e915b770 473 HOST_WIDEST_INT result_mask;
7f2935c7 474{
cf00a885
ZW
475 const U_CHAR *ptr = *string_ptr;
476 /* We know we have at least one following character. */
477 int c = *ptr++;
7f2935c7
PB
478 switch (c)
479 {
cf00a885
ZW
480 case 'a': c = TARGET_BELL; break;
481 case 'b': c = TARGET_BS; break;
482 case 'f': c = TARGET_FF; break;
483 case 'n': c = TARGET_NEWLINE; break;
484 case 'r': c = TARGET_CR; break;
485 case 't': c = TARGET_TAB; break;
486 case 'v': c = TARGET_VT; break;
487
488 case 'e': case 'E':
ae79697b 489 if (CPP_PEDANTIC (pfile))
041c3194 490 cpp_pedwarn (pfile, "non-ISO-standard escape sequence, '\\%c'", c);
cf00a885
ZW
491 c = TARGET_ESC;
492 break;
7f2935c7 493
cf00a885
ZW
494 case '0': case '1': case '2': case '3':
495 case '4': case '5': case '6': case '7':
7f2935c7 496 {
cf00a885
ZW
497 unsigned int i = c - '0';
498 int count = 0;
7f2935c7
PB
499 while (++count < 3)
500 {
cf00a885
ZW
501 if (ptr >= limit)
502 break;
503
504 c = *ptr;
505 if (c < '0' || c > '7')
506 break;
507 ptr++;
508 i = (i << 3) + c - '0';
7f2935c7 509 }
f1a86df6 510 if (i != (i & result_mask))
7f2935c7 511 {
f1a86df6
ZW
512 i &= result_mask;
513 cpp_pedwarn (pfile, "octal escape sequence out of range");
7f2935c7 514 }
cf00a885
ZW
515 c = i;
516 break;
7f2935c7 517 }
cf00a885 518
7f2935c7
PB
519 case 'x':
520 {
cf00a885
ZW
521 unsigned int i = 0, overflow = 0;
522 int digits_found = 0, digit;
7f2935c7
PB
523 for (;;)
524 {
cf00a885
ZW
525 if (ptr >= limit)
526 break;
527 c = *ptr;
7f2935c7
PB
528 if (c >= '0' && c <= '9')
529 digit = c - '0';
530 else if (c >= 'a' && c <= 'f')
531 digit = c - 'a' + 10;
532 else if (c >= 'A' && c <= 'F')
533 digit = c - 'A' + 10;
534 else
cf00a885
ZW
535 break;
536 ptr++;
7f2935c7
PB
537 overflow |= i ^ (i << 4 >> 4);
538 i = (i << 4) + digit;
539 digits_found = 1;
540 }
541 if (!digits_found)
542 cpp_error (pfile, "\\x used with no following hex digits");
f1a86df6 543 if (overflow | (i != (i & result_mask)))
7f2935c7 544 {
f1a86df6
ZW
545 i &= result_mask;
546 cpp_pedwarn (pfile, "hex escape sequence out of range");
7f2935c7 547 }
cf00a885
ZW
548 c = i;
549 break;
7f2935c7 550 }
7f2935c7 551 }
cf00a885
ZW
552 *string_ptr = ptr;
553 return c;
7f2935c7
PB
554}
555
556static void
557integer_overflow (pfile)
558 cpp_reader *pfile;
559{
560 if (CPP_PEDANTIC (pfile))
561 cpp_pedwarn (pfile, "integer overflow in preprocessor expression");
562}
563
ca261cb4 564static HOST_WIDEST_INT
7f2935c7
PB
565left_shift (pfile, a, unsignedp, b)
566 cpp_reader *pfile;
ca261cb4 567 HOST_WIDEST_INT a;
e23c0ba3 568 unsigned int unsignedp;
ca261cb4 569 unsigned HOST_WIDEST_INT b;
7f2935c7 570{
ca261cb4 571 if (b >= HOST_BITS_PER_WIDEST_INT)
7f2935c7
PB
572 {
573 if (! unsignedp && a != 0)
574 integer_overflow (pfile);
575 return 0;
576 }
577 else if (unsignedp)
ca261cb4 578 return (unsigned HOST_WIDEST_INT) a << b;
7f2935c7
PB
579 else
580 {
ca261cb4 581 HOST_WIDEST_INT l = a << b;
7f2935c7
PB
582 if (l >> b != a)
583 integer_overflow (pfile);
584 return l;
585 }
586}
587
ca261cb4 588static HOST_WIDEST_INT
7f2935c7 589right_shift (pfile, a, unsignedp, b)
d6f4ec51 590 cpp_reader *pfile ATTRIBUTE_UNUSED;
ca261cb4 591 HOST_WIDEST_INT a;
e23c0ba3 592 unsigned int unsignedp;
ca261cb4 593 unsigned HOST_WIDEST_INT b;
7f2935c7 594{
ca261cb4
KG
595 if (b >= HOST_BITS_PER_WIDEST_INT)
596 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
7f2935c7 597 else if (unsignedp)
ca261cb4 598 return (unsigned HOST_WIDEST_INT) a >> b;
7f2935c7
PB
599 else
600 return a >> b;
601}
602\f
4063b943 603/* Operator precedence and flags table.
dbac4aff
NB
604
605After an operator is returned from the lexer, if it has priority less
606than or equal to the operator on the top of the stack, we reduce the
4063b943
NB
607stack by one operator and repeat the test. Since equal priorities
608reduce, this is naturally left-associative.
dbac4aff
NB
609
610We handle right-associative operators by clearing the lower bit of all
611left-associative operators, and setting it for right-associative ones.
4063b943
NB
612After the reduction phase of a new operator, just before it is pushed
613onto the stack, its RIGHT_ASSOC bit is cleared. The effect is that
614during the reduction phase, the current right-associative operator has
615a priority one greater than any other operator of otherwise equal
616precedence that has been pushed on the top of the stack. This avoids
617a reduction pass, and effectively makes the logic right-associative.
dbac4aff
NB
618
619The remaining cases are '(' and ')'. We handle '(' by skipping the
620reduction phase completely. ')' is given lower priority than
621everything else, including '(', effectively forcing a reduction of the
4063b943
NB
622parenthesised expression. If there is no matching '(', the stack will
623be reduced all the way to the beginning, exiting the parser in the
624same way as the ultra-low priority end-of-expression dummy operator.
625The exit code checks to see if the operator that caused it is ')', and
626if so outputs an appropriate error message.
627
628The parser assumes all shifted operators require a right operand
629unless the flag NO_R_OPERAND is set, and similarly for NO_L_OPERAND.
630These semantics are automatically checked, any extra semantics need to
631be handled with operator-specific code. */
632
633#define FLAG_BITS 8
634#define FLAG_MASK ((1 << FLAG_BITS) - 1)
635#define PRIO_SHIFT (FLAG_BITS + 1)
636#define EXTRACT_PRIO(cnst) (cnst >> FLAG_BITS)
637#define EXTRACT_FLAGS(cnst) (cnst & FLAG_MASK)
638
639/* Flags. */
640#define HAVE_VALUE (1 << 0)
641#define NO_L_OPERAND (1 << 1)
642#define NO_R_OPERAND (1 << 2)
643#define SHORT_CIRCUIT (1 << 3)
644
645/* Priority and flag combinations. */
646#define RIGHT_ASSOC (1 << FLAG_BITS)
647#define FORCE_REDUCE_PRIO (0 << PRIO_SHIFT)
648#define CLOSE_PAREN_PRIO (1 << PRIO_SHIFT)
649#define OPEN_PAREN_PRIO ((2 << PRIO_SHIFT) | NO_L_OPERAND)
650#define COMMA_PRIO (3 << PRIO_SHIFT)
651#define COND_PRIO ((4 << PRIO_SHIFT) | RIGHT_ASSOC | SHORT_CIRCUIT)
652#define COLON_PRIO ((5 << PRIO_SHIFT) | SHORT_CIRCUIT)
653#define OROR_PRIO ((6 << PRIO_SHIFT) | SHORT_CIRCUIT)
654#define ANDAND_PRIO ((7 << PRIO_SHIFT) | SHORT_CIRCUIT)
655#define OR_PRIO (8 << PRIO_SHIFT)
656#define XOR_PRIO (9 << PRIO_SHIFT)
657#define AND_PRIO (10 << PRIO_SHIFT)
92936ecf
ZW
658#define MINMAX_PRIO (11 << PRIO_SHIFT)
659#define EQUAL_PRIO (12 << PRIO_SHIFT)
660#define LESS_PRIO (13 << PRIO_SHIFT)
661#define SHIFT_PRIO (14 << PRIO_SHIFT)
662#define PLUS_PRIO (15 << PRIO_SHIFT)
663#define MUL_PRIO (16 << PRIO_SHIFT)
664#define UNARY_PRIO ((17 << PRIO_SHIFT) | RIGHT_ASSOC | NO_L_OPERAND)
eba30526 665
cf00a885
ZW
666/* Operator to priority map. Must be in the same order as the first
667 N entries of enum cpp_ttype. */
668static const short
669op_to_prio[] =
670{
671 /* EQ */ 0, /* dummy entry - can't happen */
672 /* NOT */ UNARY_PRIO,
673 /* GREATER */ LESS_PRIO,
674 /* LESS */ LESS_PRIO,
675 /* PLUS */ UNARY_PRIO, /* note these two can be unary */
676 /* MINUS */ UNARY_PRIO, /* or binary */
677 /* MULT */ MUL_PRIO,
678 /* DIV */ MUL_PRIO,
679 /* MOD */ MUL_PRIO,
680 /* AND */ AND_PRIO,
681 /* OR */ OR_PRIO,
682 /* XOR */ XOR_PRIO,
683 /* RSHIFT */ SHIFT_PRIO,
684 /* LSHIFT */ SHIFT_PRIO,
92936ecf
ZW
685 /* MIN */ MINMAX_PRIO, /* C++ specific */
686 /* MAX */ MINMAX_PRIO, /* extensions */
cf00a885
ZW
687
688 /* COMPL */ UNARY_PRIO,
689 /* AND_AND */ ANDAND_PRIO,
690 /* OR_OR */ OROR_PRIO,
691 /* QUERY */ COND_PRIO,
692 /* COLON */ COLON_PRIO,
693 /* COMMA */ COMMA_PRIO,
694 /* OPEN_PAREN */ OPEN_PAREN_PRIO,
695 /* CLOSE_PAREN */ CLOSE_PAREN_PRIO,
696 /* EQ_EQ */ EQUAL_PRIO,
697 /* NOT_EQ */ EQUAL_PRIO,
698 /* GREATER_EQ */ LESS_PRIO,
699 /* LESS_EQ */ LESS_PRIO
700};
701
7f2935c7 702#define COMPARE(OP) \
4063b943 703 top->unsignedp = 0; \
b22ef131 704 top->value = (unsigned1 | unsigned2) \
4063b943
NB
705 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
706 : (v1 OP v2)
707#define EQUALITY(OP) \
708 top->value = v1 OP v2; \
709 top->unsignedp = 0;
cf00a885 710#define BITWISE(OP) \
4063b943 711 top->value = v1 OP v2; \
b22ef131 712 top->unsignedp = unsigned1 | unsigned2;
92936ecf
ZW
713#define MINMAX(OP) \
714 top->value = (v1 OP v2) ? v1 : v2; \
715 top->unsignedp = unsigned1 | unsigned2;
cf00a885
ZW
716#define UNARY(OP) \
717 top->value = OP v2; \
718 top->unsignedp = unsigned2; \
719 top->flags |= HAVE_VALUE;
cf00a885
ZW
720#define SHIFT(PSH, MSH) \
721 if (skip_evaluation) \
722 break; \
723 top->unsignedp = unsigned1; \
724 if (v2 < 0 && ! unsigned2) \
725 top->value = MSH (pfile, v1, unsigned1, -v2); \
726 else \
727 top->value = PSH (pfile, v1, unsigned1, v2);
b22ef131 728
7f2935c7 729/* Parse and evaluate a C expression, reading from PFILE.
88ae23e7 730 Returns the truth value of the expression. */
7f2935c7 731
88ae23e7 732int
b0699dad 733_cpp_parse_expr (pfile)
7f2935c7
PB
734 cpp_reader *pfile;
735{
4063b943
NB
736 /* The implementation is an operator precedence parser, i.e. a
737 bottom-up parser, using a stack for not-yet-reduced tokens.
7f2935c7
PB
738
739 The stack base is 'stack', and the current stack pointer is 'top'.
740 There is a stack element for each operator (only),
741 and the most recently pushed operator is 'top->op'.
742 An operand (value) is stored in the 'value' field of the stack
743 element of the operator that precedes it.
744 In that case the 'flags' field has the HAVE_VALUE flag set. */
745
746#define INIT_STACK_SIZE 20
cf00a885
ZW
747 struct op init_stack[INIT_STACK_SIZE];
748 struct op *stack = init_stack;
749 struct op *limit = stack + INIT_STACK_SIZE;
93c80368 750 cpp_token token;
cf00a885 751 register struct op *top = stack + 1;
4063b943 752 int skip_evaluation = 0;
45b966db 753 int result;
7f2935c7 754
93c80368
NB
755 /* Set up detection of #if ! defined(). */
756 pfile->mi_lexed = 0;
757 pfile->mi_if_not_defined = MI_IND_NONE;
758
4063b943 759 /* We've finished when we try to reduce this. */
cf00a885 760 top->op = CPP_EOF;
4063b943
NB
761 /* Nifty way to catch missing '('. */
762 top->prio = EXTRACT_PRIO(CLOSE_PAREN_PRIO);
763 /* Avoid missing right operand checks. */
764 top->flags = NO_R_OPERAND;
765
7f2935c7
PB
766 for (;;)
767 {
dbac4aff 768 unsigned int prio;
4063b943 769 unsigned int flags;
cf00a885 770 struct op op;
7f2935c7
PB
771
772 /* Read a token */
93c80368
NB
773 op = lex (pfile, skip_evaluation, &token);
774 pfile->mi_lexed++;
7f2935c7 775
4063b943
NB
776 /* If the token is an operand, push its value and get next
777 token. If it is an operator, get its priority and flags, and
778 try to reduce the expression on the stack. */
7f2935c7
PB
779 switch (op.op)
780 {
cf00a885 781 case CPP_ERROR:
9ee70313 782 goto syntax_error;
b22ef131 783 push_immediate:
cf00a885 784 case CPP_INT:
9ee70313 785 /* Push a value onto the stack. */
cf4ed945 786 if (top->flags & HAVE_VALUE)
b22ef131 787 SYNTAX_ERROR ("missing binary operator");
9ee70313
NB
788 top->value = op.value;
789 top->unsignedp = op.unsignedp;
790 top->flags |= HAVE_VALUE;
791 continue;
792
cf00a885
ZW
793 case CPP_EOF: prio = FORCE_REDUCE_PRIO; break;
794 case CPP_PLUS:
795 case CPP_MINUS: prio = PLUS_PRIO; if (top->flags & HAVE_VALUE) break;
4063b943 796 /* else unary; fall through */
cf00a885 797 default: prio = op_to_prio[op.op]; break;
7f2935c7 798 }
7f2935c7 799
4063b943
NB
800 /* Separate the operator's code into priority and flags. */
801 flags = EXTRACT_FLAGS(prio);
802 prio = EXTRACT_PRIO(prio);
cf00a885 803 if (prio == EXTRACT_PRIO(OPEN_PAREN_PRIO))
4063b943 804 goto skip_reduction;
cf4ed945 805
dbac4aff
NB
806 /* Check for reductions. Then push the operator. */
807 while (prio <= top->prio)
7f2935c7 808 {
4063b943
NB
809 HOST_WIDEST_INT v1, v2;
810 unsigned int unsigned1, unsigned2;
811
812 /* Most operators that can appear on the stack require a
813 right operand. Check this before trying to reduce. */
814 if ((top->flags & (HAVE_VALUE | NO_R_OPERAND)) == 0)
7f2935c7 815 {
cf00a885 816 if (top->op == CPP_OPEN_PAREN)
b22ef131 817 SYNTAX_ERROR ("void expression between '(' and ')'");
4063b943 818 else
b22ef131 819 SYNTAX_ERROR2 ("operator '%s' has no right operand",
93c80368 820 op_as_text (pfile, top->op));
7f2935c7 821 }
4063b943
NB
822
823 unsigned2 = top->unsignedp, v2 = top->value;
824 top--;
825 unsigned1 = top->unsignedp, v1 = top->value;
826
827 /* Now set top->value = (top[1].op)(v1, v2); */
7f2935c7
PB
828 switch (top[1].op)
829 {
cf00a885 830 default:
93c80368
NB
831 cpp_ice (pfile, "impossible operator '%s'",
832 op_as_text (pfile, top[1].op));
cf00a885
ZW
833 goto syntax_error;
834
835 case CPP_NOT: UNARY(!); break;
836 case CPP_COMPL: UNARY(~); break;
837 case CPP_LESS: COMPARE(<); break;
838 case CPP_GREATER: COMPARE(>); break;
839 case CPP_LESS_EQ: COMPARE(<=); break;
840 case CPP_GREATER_EQ: COMPARE(>=); break;
841 case CPP_EQ_EQ: EQUALITY(==); break;
842 case CPP_NOT_EQ: EQUALITY(!=); break;
843 case CPP_AND: BITWISE(&); break;
844 case CPP_XOR: BITWISE(^); break;
845 case CPP_OR: BITWISE(|); break;
cf00a885
ZW
846 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
847 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
92936ecf
ZW
848 case CPP_MIN: MINMAX(<); break;
849 case CPP_MAX: MINMAX(>); break;
cf00a885
ZW
850
851 case CPP_PLUS:
7f2935c7 852 if (!(top->flags & HAVE_VALUE))
cf00a885 853 {
0080e892
ZW
854 /* Can't use UNARY(+) because K+R C did not have unary
855 plus. Can't use UNARY() because some compilers object
856 to the empty argument. */
857 top->value = v2;
858 top->unsignedp = unsigned2;
859 top->flags |= HAVE_VALUE;
6f4280ef
ZW
860
861 if (CPP_WTRADITIONAL (pfile))
862 cpp_warning (pfile,
863 "traditional C rejects the unary plus operator");
7f2935c7
PB
864 }
865 else
866 {
867 top->value = v1 + v2;
b22ef131 868 top->unsignedp = unsigned1 | unsigned2;
a4a315ef 869 if (! top->unsignedp && ! skip_evaluation
7f2935c7
PB
870 && ! possible_sum_sign (v1, v2, top->value))
871 integer_overflow (pfile);
872 }
873 break;
cf00a885 874 case CPP_MINUS:
52529158 875 if (!(top->flags & HAVE_VALUE))
cf00a885
ZW
876 {
877 UNARY(-);
878 if (!skip_evaluation && (top->value & v2) < 0 && !unsigned2)
7f2935c7 879 integer_overflow (pfile);
7f2935c7
PB
880 }
881 else
882 { /* Binary '-' */
883 top->value = v1 - v2;
b22ef131 884 top->unsignedp = unsigned1 | unsigned2;
52529158 885 if (! top->unsignedp && ! skip_evaluation
7f2935c7
PB
886 && ! possible_sum_sign (top->value, v2, v1))
887 integer_overflow (pfile);
888 }
889 break;
cf00a885 890 case CPP_MULT:
b22ef131 891 top->unsignedp = unsigned1 | unsigned2;
7f2935c7 892 if (top->unsignedp)
ca261cb4 893 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
a4a315ef 894 else if (!skip_evaluation)
7f2935c7
PB
895 {
896 top->value = v1 * v2;
4063b943
NB
897 if (v1 && (top->value / v1 != v2
898 || (top->value & v1 & v2) < 0))
7f2935c7
PB
899 integer_overflow (pfile);
900 }
901 break;
cf00a885
ZW
902 case CPP_DIV:
903 case CPP_MOD:
a4a315ef
PB
904 if (skip_evaluation)
905 break;
7f2935c7 906 if (v2 == 0)
b22ef131
NB
907 SYNTAX_ERROR ("division by zero in #if");
908 top->unsignedp = unsigned1 | unsigned2;
cf00a885 909 if (top[1].op == CPP_DIV)
7f2935c7 910 {
4063b943
NB
911 if (top->unsignedp)
912 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
913 else
914 {
915 top->value = v1 / v2;
916 if ((top->value & v1 & v2) < 0)
917 integer_overflow (pfile);
918 }
7f2935c7 919 }
4063b943 920 else
7f2935c7 921 {
4063b943
NB
922 if (top->unsignedp)
923 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
924 else
925 top->value = v1 % v2;
7f2935c7 926 }
7f2935c7 927 break;
cf00a885 928
0080e892
ZW
929 case CPP_OR_OR:
930 top->value = v1 || v2;
931 top->unsignedp = 0;
932 if (v1) skip_evaluation--;
933 break;
934 case CPP_AND_AND:
935 top->value = v1 && v2;
936 top->unsignedp = 0;
937 if (!v1) skip_evaluation--;
938 break;
cf00a885 939 case CPP_COMMA:
7f2935c7 940 if (CPP_PEDANTIC (pfile))
b22ef131 941 cpp_pedwarn (pfile, "comma operator in operand of #if");
7f2935c7
PB
942 top->value = v2;
943 top->unsignedp = unsigned2;
944 break;
cf00a885 945 case CPP_QUERY:
b22ef131 946 SYNTAX_ERROR ("syntax error '?' without following ':'");
cf00a885
ZW
947 case CPP_COLON:
948 if (top[0].op != CPP_QUERY)
b22ef131 949 SYNTAX_ERROR ("syntax error ':' without preceding '?'");
4063b943
NB
950 top--;
951 if (top->value) skip_evaluation--;
952 top->value = top->value ? v1 : v2;
b22ef131 953 top->unsignedp = unsigned1 | unsigned2;
7f2935c7 954 break;
cf00a885
ZW
955 case CPP_OPEN_PAREN:
956 if (op.op != CPP_CLOSE_PAREN)
b22ef131 957 SYNTAX_ERROR ("missing ')' in expression");
9ee70313
NB
958 op.value = v2;
959 op.unsignedp = unsigned2;
960 goto push_immediate;
cf00a885 961 case CPP_EOF:
4063b943 962 /* Reducing this dummy operator indicates we've finished. */
cf00a885 963 if (op.op == CPP_CLOSE_PAREN)
b22ef131 964 SYNTAX_ERROR ("missing '(' in expression");
4063b943 965 goto done;
7f2935c7
PB
966 }
967 }
9ee70313 968
4063b943
NB
969 /* Handle short-circuit evaluations. */
970 if (flags & SHORT_CIRCUIT)
971 switch (op.op)
972 {
cf00a885
ZW
973 case CPP_OR_OR: if (top->value) skip_evaluation++; break;
974 case CPP_AND_AND:
975 case CPP_QUERY: if (!top->value) skip_evaluation++; break;
976 case CPP_COLON:
4063b943
NB
977 if (top[-1].value) /* Was '?' condition true? */
978 skip_evaluation++;
979 else
980 skip_evaluation--;
cf00a885
ZW
981 default:
982 break;
4063b943 983 }
9ee70313
NB
984
985 skip_reduction:
4063b943 986 /* Check we have a left operand iff we need one. */
b22ef131 987 if (flags & NO_L_OPERAND)
4063b943 988 {
b22ef131
NB
989 if (top->flags & HAVE_VALUE)
990 SYNTAX_ERROR2 ("missing binary operator before '%s'",
93c80368 991 op_as_text (pfile, top->op));
b22ef131
NB
992 }
993 else
994 {
995 if (!(top->flags & HAVE_VALUE))
996 SYNTAX_ERROR2 ("operator '%s' has no left operand",
93c80368 997 op_as_text (pfile, top->op));
4063b943
NB
998 }
999
0f41302f 1000 /* Check for and handle stack overflow. */
9ee70313 1001 top++;
7f2935c7
PB
1002 if (top == limit)
1003 {
cf00a885 1004 struct op *new_stack;
0f41302f 1005 int old_size = (char *) limit - (char *) stack;
7f2935c7
PB
1006 int new_size = 2 * old_size;
1007 if (stack != init_stack)
cf00a885 1008 new_stack = (struct op *) xrealloc (stack, new_size);
7f2935c7
PB
1009 else
1010 {
cf00a885 1011 new_stack = (struct op *) xmalloc (new_size);
b0699dad 1012 memcpy (new_stack, stack, old_size);
7f2935c7
PB
1013 }
1014 stack = new_stack;
cf00a885
ZW
1015 top = (struct op *) ((char *) new_stack + old_size);
1016 limit = (struct op *) ((char *) new_stack + new_size);
7f2935c7
PB
1017 }
1018
1019 top->flags = flags;
4063b943 1020 top->prio = prio & ~EXTRACT_PRIO(RIGHT_ASSOC);
7f2935c7
PB
1021 top->op = op.op;
1022 }
9ee70313 1023
4063b943
NB
1024 done:
1025 result = (top[1].value != 0);
9ee70313 1026 if (top != stack)
cf00a885 1027 CPP_ICE ("unbalanced stack in #if");
4063b943
NB
1028 else if (!(top[1].flags & HAVE_VALUE))
1029 {
b22ef131 1030 SYNTAX_ERROR ("#if with no expression");
4063b943 1031 syntax_error:
4063b943
NB
1032 result = 0; /* Return 0 on syntax error. */
1033 }
9ee70313 1034
4063b943 1035 /* Free dynamic stack if we allocated one. */
7f2935c7
PB
1036 if (stack != init_stack)
1037 free (stack);
45b966db 1038 return result;
7f2935c7 1039}
93c80368
NB
1040
1041static const unsigned char *
1042op_as_text (pfile, op)
1043 cpp_reader *pfile;
1044 enum cpp_ttype op;
1045{
1046 cpp_token token;
1047
1048 token.type = op;
1049 token.flags = 0;
1050 return cpp_token_as_text (pfile, &token);
1051}