]> gcc.gnu.org Git - gcc.git/blame - gcc/cppexp.c
Daily bump.
[gcc.git] / gcc / cppexp.c
CommitLineData
b0699dad 1/* Parse C expressions for cpplib.
5d8ebbd8
NB
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
e38992e8 4 Contributed by Per Bothner, 1994.
7f2935c7
PB
5
6This program is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
940d9d63 18Foundation, 59 Temple Place - Suite 330,
e38992e8 19Boston, MA 02111-1307, USA. */
7f2935c7 20
7f2935c7 21#include "config.h"
b04cd507 22#include "system.h"
487a6e06 23#include "cpplib.h"
88ae23e7 24#include "cpphash.h"
7f2935c7 25
7f2935c7
PB
26/* Yield nonzero if adding two numbers with A's and B's signs can yield a
27 number with SUM's sign, where A, B, and SUM are all C integers. */
28#define possible_sum_sign(a, b, sum) ((((a) ^ (b)) | ~ ((a) ^ (sum))) < 0)
29
b04cd507 30static void integer_overflow PARAMS ((cpp_reader *));
b0699dad 31static HOST_WIDEST_INT left_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
e23c0ba3
ZW
32 unsigned int,
33 unsigned HOST_WIDEST_INT));
b0699dad 34static HOST_WIDEST_INT right_shift PARAMS ((cpp_reader *, HOST_WIDEST_INT,
e23c0ba3
ZW
35 unsigned int,
36 unsigned HOST_WIDEST_INT));
cf00a885 37static struct op parse_number PARAMS ((cpp_reader *, const cpp_token *));
cf00a885 38static struct op parse_defined PARAMS ((cpp_reader *));
60284a59 39static struct op eval_token PARAMS ((cpp_reader *, const cpp_token *));
87ed109f 40static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
cf00a885
ZW
41
42struct op
b0699dad 43{
cf00a885 44 enum cpp_ttype op;
562a5c27 45 uchar unsignedp; /* True if value should be treated as unsigned. */
4063b943 46 HOST_WIDEST_INT value; /* The value logically "right" of op. */
7f2935c7 47};
7f2935c7 48
f8b954fc
NB
49/* Token type abuse. There is no "error" token, but we can't get
50 comments in #if, so we can abuse that token type. Similarly,
51 create unary plus and minus operators. */
cf00a885 52#define CPP_ERROR CPP_COMMENT
f8b954fc
NB
53#define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
54#define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
cf00a885 55
15dad1d9
ZW
56/* With -O2, gcc appears to produce nice code, moving the error
57 message load and subsequent jump completely out of the main path. */
15dad1d9 58#define SYNTAX_ERROR(msgid) \
ebef4e8c 59 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
15dad1d9 60#define SYNTAX_ERROR2(msgid, arg) \
ebef4e8c 61 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
15dad1d9 62
cf00a885
ZW
63struct suffix
64{
8b60264b
KG
65 const unsigned char s[4];
66 const unsigned char u;
67 const unsigned char l;
cf00a885
ZW
68};
69
8b60264b 70static const struct suffix vsuf_1[] = {
cf00a885
ZW
71 { "u", 1, 0 }, { "U", 1, 0 },
72 { "l", 0, 1 }, { "L", 0, 1 }
73};
74
8b60264b 75static const struct suffix vsuf_2[] = {
cf00a885
ZW
76 { "ul", 1, 1 }, { "UL", 1, 1 }, { "uL", 1, 1 }, { "Ul", 1, 1 },
77 { "lu", 1, 1 }, { "LU", 1, 1 }, { "Lu", 1, 1 }, { "lU", 1, 1 },
78 { "ll", 0, 2 }, { "LL", 0, 2 }
79};
7f2935c7 80
8b60264b 81static const struct suffix vsuf_3[] = {
cf00a885
ZW
82 { "ull", 1, 2 }, { "ULL", 1, 2 }, { "uLL", 1, 2 }, { "Ull", 1, 2 },
83 { "llu", 1, 2 }, { "LLU", 1, 2 }, { "LLu", 1, 2 }, { "llU", 1, 2 }
84};
cf00a885 85
5d8ebbd8
NB
86/* Parse and convert what is presumably an integer in TOK. Accepts
87 decimal, hex, or octal with or without size suffixes. Returned op
88 is CPP_ERROR on error, otherwise it is a CPP_NUMBER. */
cf00a885
ZW
89static struct op
90parse_number (pfile, tok)
7f2935c7 91 cpp_reader *pfile;
cf00a885 92 const cpp_token *tok;
7f2935c7 93{
cf00a885 94 struct op op;
562a5c27
NB
95 const uchar *start = tok->val.str.text;
96 const uchar *end = start + tok->val.str.len;
97 const uchar *p = start;
bfb9dc7f 98 int c = 0, i, nsuff;
e915b770 99 unsigned HOST_WIDEST_INT n = 0, nd, MAX_over_base;
f13eb63a
ZW
100 int base = 10;
101 int overflow = 0;
102 int digit, largest_digit = 0;
cf00a885 103 const struct suffix *sufftab;
7f2935c7
PB
104
105 op.unsignedp = 0;
106
f13eb63a
ZW
107 if (p[0] == '0')
108 {
109 if (end - start >= 3 && (p[1] == 'x' || p[1] == 'X'))
110 {
111 p += 2;
112 base = 16;
113 }
114 else
115 {
116 p += 1;
117 base = 8;
118 }
7f2935c7
PB
119 }
120
0f41302f 121 /* Some buggy compilers (e.g. MPW C) seem to need both casts. */
e915b770
KG
122 MAX_over_base = (((unsigned HOST_WIDEST_INT) -1)
123 / ((unsigned HOST_WIDEST_INT) base));
7f2935c7 124
cf00a885 125 for(; p < end; p++)
f13eb63a 126 {
cf00a885 127 c = *p;
f13eb63a 128
9e1ac915
KG
129 if (ISDIGIT (c)
130 || (base == 16 && ISXDIGIT (c)))
131 digit = hex_value (c);
f13eb63a 132 else
cf00a885
ZW
133 break;
134
f13eb63a
ZW
135 if (largest_digit < digit)
136 largest_digit = digit;
137 nd = n * base + digit;
138 overflow |= MAX_over_base < n || nd < n;
139 n = nd;
7f2935c7 140 }
7f2935c7 141
cf00a885 142 if (p < end)
f13eb63a 143 {
cf00a885
ZW
144 /* Check for a floating point constant. Note that float constants
145 with an exponent or suffix but no decimal point are technically
5ef865d5 146 invalid (C99 6.4.4.2) but accepted elsewhere. */
cf00a885
ZW
147 if ((c == '.' || c == 'F' || c == 'f')
148 || (base == 10 && (c == 'E' || c == 'e')
149 && p+1 < end && (p[1] == '+' || p[1] == '-'))
150 || (base == 16 && (c == 'P' || c == 'p')
151 && p+1 < end && (p[1] == '+' || p[1] == '-')))
152 SYNTAX_ERROR ("floating point numbers are not valid in #if");
153
154 /* Determine the suffix. l means long, and u means unsigned.
155 See the suffix tables, above. */
156 switch (end - p)
157 {
ca7558fc
KG
158 case 1: sufftab = vsuf_1; nsuff = ARRAY_SIZE (vsuf_1); break;
159 case 2: sufftab = vsuf_2; nsuff = ARRAY_SIZE (vsuf_2); break;
160 case 3: sufftab = vsuf_3; nsuff = ARRAY_SIZE (vsuf_3); break;
cf00a885
ZW
161 default: goto invalid_suffix;
162 }
163
164 for (i = 0; i < nsuff; i++)
165 if (memcmp (p, sufftab[i].s, end - p) == 0)
166 break;
167 if (i == nsuff)
168 goto invalid_suffix;
169 op.unsignedp = sufftab[i].u;
170
7065e130
NB
171 if (CPP_WTRADITIONAL (pfile)
172 && sufftab[i].u
c691145a 173 && ! cpp_sys_macro_p (pfile))
ebef4e8c 174 cpp_error (pfile, DL_WARNING, "traditional C rejects the `U' suffix");
fef1de5d
NB
175 if (sufftab[i].l == 2 && CPP_OPTION (pfile, pedantic)
176 && ! CPP_OPTION (pfile, c99))
ebef4e8c
NB
177 cpp_error (pfile, DL_PEDWARN,
178 "too many 'l' suffixes in integer constant");
f13eb63a
ZW
179 }
180
7f2935c7 181 if (base <= largest_digit)
ebef4e8c
NB
182 cpp_error (pfile, DL_PEDWARN,
183 "integer constant contains digits beyond the radix");
7f2935c7
PB
184
185 if (overflow)
ebef4e8c 186 cpp_error (pfile, DL_PEDWARN, "integer constant out of range");
7f2935c7
PB
187
188 /* If too big to be signed, consider it unsigned. */
e915b770 189 else if ((HOST_WIDEST_INT) n < 0 && ! op.unsignedp)
7f2935c7
PB
190 {
191 if (base == 10)
ebef4e8c
NB
192 cpp_error (pfile, DL_WARNING,
193 "integer constant is so large that it is unsigned");
7f2935c7
PB
194 op.unsignedp = 1;
195 }
196
197 op.value = n;
c60e94a7 198 op.op = CPP_NUMBER;
7f2935c7 199 return op;
f13eb63a 200
cf00a885 201 invalid_suffix:
ebef4e8c 202 cpp_error (pfile, DL_ERROR, "invalid suffix '%.*s' on integer constant",
cf00a885
ZW
203 (int) (end - p), p);
204 syntax_error:
205 op.op = CPP_ERROR;
f13eb63a 206 return op;
7f2935c7
PB
207}
208
5d8ebbd8 209/* Handle meeting "defined" in a preprocessor expression. */
cf00a885 210static struct op
ba412f14
ZW
211parse_defined (pfile)
212 cpp_reader *pfile;
213{
93c80368
NB
214 int paren = 0;
215 cpp_hashnode *node = 0;
4ed5bcfb 216 const cpp_token *token;
cf00a885 217 struct op op;
63d75005 218 cpp_context *initial_context = pfile->context;
ba412f14 219
93c80368
NB
220 /* Don't expand macros. */
221 pfile->state.prevent_expansion++;
222
4ed5bcfb
NB
223 token = cpp_get_token (pfile);
224 if (token->type == CPP_OPEN_PAREN)
ba412f14 225 {
cf00a885 226 paren = 1;
4ed5bcfb 227 token = cpp_get_token (pfile);
ba412f14
ZW
228 }
229
4ed5bcfb 230 if (token->type == CPP_NAME)
93c80368 231 {
4ed5bcfb
NB
232 node = token->val.node;
233 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
93c80368 234 {
ebef4e8c 235 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
4ed5bcfb 236 node = 0;
93c80368
NB
237 }
238 }
239 else
3c8465d0 240 {
ebef4e8c
NB
241 cpp_error (pfile, DL_ERROR,
242 "operator \"defined\" requires an identifier");
4ed5bcfb 243 if (token->flags & NAMED_OP)
3c8465d0
NB
244 {
245 cpp_token op;
246
247 op.flags = 0;
4ed5bcfb 248 op.type = token->type;
ebef4e8c 249 cpp_error (pfile, DL_ERROR,
3c8465d0 250 "(\"%s\" is an alternative token for \"%s\" in C++)",
4ed5bcfb 251 cpp_token_as_text (pfile, token),
3c8465d0
NB
252 cpp_token_as_text (pfile, &op));
253 }
254 }
15dad1d9 255
93c80368
NB
256 if (!node)
257 op.op = CPP_ERROR;
258 else
15dad1d9 259 {
63d75005 260 if (pfile->context != initial_context)
ebef4e8c
NB
261 cpp_error (pfile, DL_WARNING,
262 "this use of \"defined\" may not be portable");
63d75005 263
93c80368 264 op.value = node->type == NT_MACRO;
cf00a885 265 op.unsignedp = 0;
c60e94a7 266 op.op = CPP_NUMBER;
15dad1d9 267
6d18adbc
NB
268 /* A possible controlling macro of the form #if !defined ().
269 _cpp_parse_expr checks there was no other junk on the line. */
270 pfile->mi_ind_cmacro = node;
15dad1d9 271 }
93c80368
NB
272
273 pfile->state.prevent_expansion--;
15dad1d9 274 return op;
15dad1d9
ZW
275}
276
60284a59
NB
277/* Convert a token into a CPP_NUMBER (an interpreted preprocessing
278 number or character constant, or the result of the "defined" or "#"
279 operators), or CPP_ERROR on error. */
cf00a885 280static struct op
60284a59 281eval_token (pfile, token)
52529158 282 cpp_reader *pfile;
60284a59 283 const cpp_token *token;
7f2935c7 284{
60284a59 285 unsigned int temp;
cf00a885 286 struct op op;
60284a59
NB
287
288 op.op = CPP_NUMBER;
289 op.unsignedp = 0;
041c3194 290
93c80368 291 switch (token->type)
ba412f14 292 {
7f2935c7 293 case CPP_NUMBER:
93c80368 294 return parse_number (pfile, token);
7f2f1a66 295
cf00a885 296 case CPP_WCHAR:
60284a59
NB
297 op.unsignedp = WCHAR_UNSIGNED;
298 case CPP_CHAR: /* Always unsigned. */
299 op.value = cpp_interpret_charconst (pfile, token, 1, &temp);
300 break;
ba412f14 301
92936ecf 302 case CPP_NAME:
93c80368 303 if (token->val.node == pfile->spec_nodes.n_defined)
63d75005 304 return parse_defined (pfile);
7d4918a2
ZW
305 else if (CPP_OPTION (pfile, cplusplus)
306 && (token->val.node == pfile->spec_nodes.n_true
307 || token->val.node == pfile->spec_nodes.n_false))
308 {
7d4918a2
ZW
309 op.value = (token->val.node == pfile->spec_nodes.n_true);
310
311 /* Warn about use of true or false in #if when pedantic
312 and stdbool.h has not been included. */
313 if (CPP_PEDANTIC (pfile)
314 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
ebef4e8c
NB
315 cpp_error (pfile, DL_PEDWARN,
316 "ISO C++ does not permit \"%s\" in #if",
317 NODE_NAME (token->val.node));
7d4918a2
ZW
318 }
319 else
320 {
7d4918a2 321 op.value = 0;
87ed109f 322 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
ebef4e8c
NB
323 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
324 NODE_NAME (token->val.node));
7f2935c7 325 }
60284a59 326 break;
7f2935c7 327
60284a59
NB
328 default: /* CPP_HASH */
329 if (_cpp_test_assertion (pfile, &temp))
330 op.op = CPP_ERROR;
331 op.value = temp;
93c80368 332 }
7c3bb1de 333
cf00a885 334 return op;
7c3bb1de 335}
7f2935c7 336
5d8ebbd8 337/* Warn if appropriate on overflow. */
7f2935c7
PB
338static void
339integer_overflow (pfile)
340 cpp_reader *pfile;
341{
342 if (CPP_PEDANTIC (pfile))
ebef4e8c
NB
343 cpp_error (pfile, DL_PEDWARN,
344 "integer overflow in preprocessor expression");
7f2935c7
PB
345}
346
5d8ebbd8
NB
347/* Handle shifting A left by B bits. UNSIGNEDP is non-zero if A is
348 unsigned. */
ca261cb4 349static HOST_WIDEST_INT
7f2935c7
PB
350left_shift (pfile, a, unsignedp, b)
351 cpp_reader *pfile;
ca261cb4 352 HOST_WIDEST_INT a;
e23c0ba3 353 unsigned int unsignedp;
ca261cb4 354 unsigned HOST_WIDEST_INT b;
7f2935c7 355{
ca261cb4 356 if (b >= HOST_BITS_PER_WIDEST_INT)
7f2935c7
PB
357 {
358 if (! unsignedp && a != 0)
359 integer_overflow (pfile);
360 return 0;
361 }
362 else if (unsignedp)
ca261cb4 363 return (unsigned HOST_WIDEST_INT) a << b;
7f2935c7
PB
364 else
365 {
ca261cb4 366 HOST_WIDEST_INT l = a << b;
7f2935c7
PB
367 if (l >> b != a)
368 integer_overflow (pfile);
369 return l;
370 }
371}
372
5d8ebbd8
NB
373/* Handle shifting A right by B bits. UNSIGNEDP is non-zero if A is
374 unsigned. */
ca261cb4 375static HOST_WIDEST_INT
7f2935c7 376right_shift (pfile, a, unsignedp, b)
d6f4ec51 377 cpp_reader *pfile ATTRIBUTE_UNUSED;
ca261cb4 378 HOST_WIDEST_INT a;
e23c0ba3 379 unsigned int unsignedp;
ca261cb4 380 unsigned HOST_WIDEST_INT b;
7f2935c7 381{
ca261cb4
KG
382 if (b >= HOST_BITS_PER_WIDEST_INT)
383 return unsignedp ? 0 : a >> (HOST_BITS_PER_WIDEST_INT - 1);
7f2935c7 384 else if (unsignedp)
ca261cb4 385 return (unsigned HOST_WIDEST_INT) a >> b;
7f2935c7
PB
386 else
387 return a >> b;
388}
389\f
4063b943 390/* Operator precedence and flags table.
dbac4aff
NB
391
392After an operator is returned from the lexer, if it has priority less
87ed109f
NB
393than the operator on the top of the stack, we reduce the stack by one
394operator and repeat the test. Since equal priorities do not reduce,
395this is naturally right-associative.
396
397We handle left-associative operators by decrementing the priority of
398just-lexed operators by one, but retaining the priority of operators
399already on the stack.
dbac4aff
NB
400
401The remaining cases are '(' and ')'. We handle '(' by skipping the
402reduction phase completely. ')' is given lower priority than
403everything else, including '(', effectively forcing a reduction of the
87ed109f
NB
404parenthesised expression. If there is a matching '(', the routine
405reduce() exits immediately. If the normal exit route sees a ')', then
406there cannot have been a matching '(' and an error message is output.
4063b943 407
f8b954fc
NB
408The parser assumes all shifted operators require a left operand unless
409the flag NO_L_OPERAND is set. These semantics are automatic; any
410extra semantics need to be handled with operator-specific code. */
4063b943 411
4063b943 412/* Flags. */
87ed109f
NB
413#define NO_L_OPERAND (1 << 0)
414#define LEFT_ASSOC (1 << 1)
eba30526 415
cf00a885
ZW
416/* Operator to priority map. Must be in the same order as the first
417 N entries of enum cpp_ttype. */
87ed109f
NB
418static const struct operator
419{
60284a59 420 uchar prio;
87ed109f
NB
421 uchar flags;
422} optab[] =
cf00a885 423{
87ed109f
NB
424 /* EQ */ {0, 0}, /* Shouldn't happen. */
425 /* NOT */ {16, NO_L_OPERAND},
426 /* GREATER */ {12, LEFT_ASSOC},
427 /* LESS */ {12, LEFT_ASSOC},
428 /* PLUS */ {14, LEFT_ASSOC},
429 /* MINUS */ {14, LEFT_ASSOC},
430 /* MULT */ {15, LEFT_ASSOC},
431 /* DIV */ {15, LEFT_ASSOC},
432 /* MOD */ {15, LEFT_ASSOC},
433 /* AND */ {9, LEFT_ASSOC},
434 /* OR */ {7, LEFT_ASSOC},
435 /* XOR */ {8, LEFT_ASSOC},
436 /* RSHIFT */ {13, LEFT_ASSOC},
437 /* LSHIFT */ {13, LEFT_ASSOC},
438 /* MIN */ {10, LEFT_ASSOC}, /* C++ specific */
439 /* MAX */ {10, LEFT_ASSOC}, /* extensions */
440
441 /* COMPL */ {16, NO_L_OPERAND},
442 /* AND_AND */ {6, LEFT_ASSOC},
443 /* OR_OR */ {5, LEFT_ASSOC},
444 /* QUERY */ {3, 0},
445 /* COLON */ {4, LEFT_ASSOC},
446 /* COMMA */ {2, LEFT_ASSOC},
447 /* OPEN_PAREN */ {1, NO_L_OPERAND},
448 /* CLOSE_PAREN */ {0, 0},
449 /* EOF */ {0, 0},
450 /* EQ_EQ */ {11, LEFT_ASSOC},
451 /* NOT_EQ */ {11, LEFT_ASSOC},
452 /* GREATER_EQ */ {12, LEFT_ASSOC},
453 /* LESS_EQ */ {12, LEFT_ASSOC},
454 /* UPLUS */ {16, NO_L_OPERAND},
455 /* UMINUS */ {16, NO_L_OPERAND}
cf00a885
ZW
456};
457
7f2935c7 458#define COMPARE(OP) \
4063b943 459 top->unsignedp = 0; \
b22ef131 460 top->value = (unsigned1 | unsigned2) \
4063b943
NB
461 ? (unsigned HOST_WIDEST_INT) v1 OP (unsigned HOST_WIDEST_INT) v2 \
462 : (v1 OP v2)
463#define EQUALITY(OP) \
464 top->value = v1 OP v2; \
465 top->unsignedp = 0;
cf00a885 466#define BITWISE(OP) \
4063b943 467 top->value = v1 OP v2; \
b22ef131 468 top->unsignedp = unsigned1 | unsigned2;
92936ecf
ZW
469#define MINMAX(OP) \
470 top->value = (v1 OP v2) ? v1 : v2; \
471 top->unsignedp = unsigned1 | unsigned2;
cf00a885
ZW
472#define UNARY(OP) \
473 top->value = OP v2; \
f8b954fc 474 top->unsignedp = unsigned2;
cf00a885 475#define SHIFT(PSH, MSH) \
87ed109f 476 if (pfile->state.skip_eval) \
cf00a885
ZW
477 break; \
478 top->unsignedp = unsigned1; \
479 if (v2 < 0 && ! unsigned2) \
480 top->value = MSH (pfile, v1, unsigned1, -v2); \
481 else \
482 top->value = PSH (pfile, v1, unsigned1, v2);
b22ef131 483
7f2935c7 484/* Parse and evaluate a C expression, reading from PFILE.
87ed109f
NB
485 Returns the truth value of the expression.
486
487 The implementation is an operator precedence parser, i.e. a
488 bottom-up parser, using a stack for not-yet-reduced tokens.
489
490 The stack base is op_stack, and the current stack pointer is 'top'.
491 There is a stack element for each operator (only), and the most
492 recently pushed operator is 'top->op'. An operand (value) is
493 stored in the 'value' field of the stack element of the operator
494 that precedes it. */
495bool
b0699dad 496_cpp_parse_expr (pfile)
7f2935c7
PB
497 cpp_reader *pfile;
498{
87ed109f 499 struct op *top = pfile->op_stack;
60284a59 500 const cpp_token *token = NULL, *prev_token;
87ed109f
NB
501 unsigned int lex_count;
502 bool saw_leading_not, want_value = true;
503
504 pfile->state.skip_eval = 0;
7f2935c7 505
93c80368 506 /* Set up detection of #if ! defined(). */
6d18adbc 507 pfile->mi_ind_cmacro = 0;
87ed109f 508 saw_leading_not = false;
6d18adbc 509 lex_count = 0;
93c80368 510
87ed109f 511 /* Lowest priority operator prevents further reductions. */
cf00a885 512 top->op = CPP_EOF;
4063b943 513
7f2935c7
PB
514 for (;;)
515 {
cf00a885 516 struct op op;
7f2935c7 517
60284a59
NB
518 prev_token = token;
519 token = cpp_get_token (pfile);
6d18adbc 520 lex_count++;
60284a59 521 op.op = token->type;
7f2935c7 522
7f2935c7
PB
523 switch (op.op)
524 {
60284a59 525 /* These tokens convert into values. */
c60e94a7 526 case CPP_NUMBER:
60284a59
NB
527 case CPP_CHAR:
528 case CPP_WCHAR:
529 case CPP_NAME:
530 case CPP_HASH:
f8b954fc 531 if (!want_value)
60284a59
NB
532 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
533 cpp_token_as_text (pfile, token));
f8b954fc 534 want_value = false;
60284a59
NB
535 op = eval_token (pfile, token);
536 if (op.op == CPP_ERROR)
537 goto syntax_error;
9ee70313
NB
538 top->value = op.value;
539 top->unsignedp = op.unsignedp;
9ee70313
NB
540 continue;
541
6d18adbc
NB
542 case CPP_NOT:
543 saw_leading_not = lex_count == 1;
6d18adbc 544 break;
cf00a885 545 case CPP_PLUS:
f8b954fc
NB
546 if (want_value)
547 op.op = CPP_UPLUS;
548 break;
549 case CPP_MINUS:
550 if (want_value)
551 op.op = CPP_UMINUS;
552 break;
60284a59
NB
553 case CPP_OTHER:
554 if (ISGRAPH (token->val.c))
555 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
556 else
557 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
558
f8b954fc 559 default:
60284a59
NB
560 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
561 SYNTAX_ERROR2 ("token \"%s\" is not valid in #if expressions",
562 cpp_token_as_text (pfile, token));
f8b954fc 563 break;
7f2935c7 564 }
7f2935c7 565
87ed109f
NB
566 /* Check we have a value or operator as appropriate. */
567 if (optab[op.op].flags & NO_L_OPERAND)
7f2935c7 568 {
87ed109f 569 if (!want_value)
60284a59
NB
570 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
571 cpp_token_as_text (pfile, token));
87ed109f
NB
572 }
573 else if (want_value)
574 {
60284a59
NB
575 /* Ordering here is subtle and intended to favour the
576 missing parenthesis diagnostics over alternatives. */
87ed109f 577 if (op.op == CPP_CLOSE_PAREN)
7f2935c7 578 {
cf00a885 579 if (top->op == CPP_OPEN_PAREN)
b22ef131 580 SYNTAX_ERROR ("void expression between '(' and ')'");
7f2935c7 581 }
87ed109f
NB
582 else if (top->op == CPP_EOF)
583 SYNTAX_ERROR ("#if with no expression");
584 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
585 SYNTAX_ERROR2 ("operator '%s' has no right operand",
60284a59 586 cpp_token_as_text (pfile, prev_token));
7f2935c7 587 }
9ee70313 588
87ed109f
NB
589 top = reduce (pfile, top, op.op);
590 if (!top)
591 goto syntax_error;
9ee70313 592
60284a59
NB
593 if (op.op == CPP_EOF)
594 break;
595
87ed109f 596 switch (op.op)
b22ef131 597 {
87ed109f
NB
598 case CPP_CLOSE_PAREN:
599 continue;
87ed109f
NB
600 case CPP_OR_OR:
601 if (top->value)
602 pfile->state.skip_eval++;
603 break;
604 case CPP_AND_AND:
605 case CPP_QUERY:
606 if (!top->value)
607 pfile->state.skip_eval++;
608 break;
609 case CPP_COLON:
60284a59
NB
610 if (top->op != CPP_QUERY)
611 SYNTAX_ERROR (" ':' without preceding '?'");
87ed109f
NB
612 if (top[-1].value) /* Was '?' condition true? */
613 pfile->state.skip_eval++;
614 else
615 pfile->state.skip_eval--;
616 default:
617 break;
4063b943 618 }
87ed109f 619
f8b954fc 620 want_value = true;
4063b943 621
0f41302f 622 /* Check for and handle stack overflow. */
87ed109f
NB
623 if (++top == pfile->op_limit)
624 top = _cpp_expand_op_stack (pfile);
7f2935c7 625
7f2935c7
PB
626 top->op = op.op;
627 }
9ee70313 628
6d18adbc
NB
629 /* The controlling macro expression is only valid if we called lex 3
630 times: <!> <defined expression> and <EOF>. push_conditional ()
631 checks that we are at top-of-file. */
632 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
633 pfile->mi_ind_cmacro = 0;
634
87ed109f 635 if (top != pfile->op_stack)
ebef4e8c
NB
636 {
637 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
4063b943 638 syntax_error:
87ed109f 639 return false; /* Return false on syntax error. */
4063b943 640 }
9ee70313 641
87ed109f
NB
642 return top->value != 0;
643}
644
645/* Reduce the operator / value stack if possible, in preparation for
646 pushing operator OP. Returns NULL on error, otherwise the top of
647 the stack. */
648static struct op *
649reduce (pfile, top, op)
650 cpp_reader *pfile;
651 struct op *top;
652 enum cpp_ttype op;
653{
654 unsigned int prio;
655
656 if (op == CPP_OPEN_PAREN)
657 return top;
658
659 /* Decrement the priority of left-associative operators to force a
660 reduction with operators of otherwise equal priority. */
661 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
662 while (prio < optab[top->op].prio)
663 {
664 HOST_WIDEST_INT v1, v2;
665 unsigned int unsigned1, unsigned2;
666
667 unsigned2 = top->unsignedp, v2 = top->value;
668 top--;
669 unsigned1 = top->unsignedp, v1 = top->value;
670
671 /* Now set top->value = (top[1].op)(v1, v2); */
672 switch (top[1].op)
673 {
674 default:
60284a59 675 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top[1].op);
87ed109f
NB
676 return 0;
677
678 case CPP_NOT: UNARY(!); break;
679 case CPP_COMPL: UNARY(~); break;
680 case CPP_LESS: COMPARE(<); break;
681 case CPP_GREATER: COMPARE(>); break;
682 case CPP_LESS_EQ: COMPARE(<=); break;
683 case CPP_GREATER_EQ: COMPARE(>=); break;
684 case CPP_EQ_EQ: EQUALITY(==); break;
685 case CPP_NOT_EQ: EQUALITY(!=); break;
686 case CPP_AND: BITWISE(&); break;
687 case CPP_XOR: BITWISE(^); break;
688 case CPP_OR: BITWISE(|); break;
689 case CPP_LSHIFT: SHIFT(left_shift, right_shift); break;
690 case CPP_RSHIFT: SHIFT(right_shift, left_shift); break;
691 case CPP_MIN: MINMAX(<); break;
692 case CPP_MAX: MINMAX(>); break;
693
694 case CPP_UPLUS:
695 /* Can't use UNARY(+) because K+R C did not have unary
696 plus. Can't use UNARY() because some compilers object
697 to the empty argument. */
698 top->value = v2;
699 top->unsignedp = unsigned2;
700 if (CPP_WTRADITIONAL (pfile))
701 cpp_error (pfile, DL_WARNING,
702 "traditional C rejects the unary plus operator");
703 break;
704 case CPP_UMINUS:
705 UNARY(-);
706 if (!pfile->state.skip_eval && (top->value & v2) < 0 && !unsigned2)
707 integer_overflow (pfile);
708 break;
709
710 case CPP_PLUS:
711 top->value = v1 + v2;
712 top->unsignedp = unsigned1 | unsigned2;
713 if (! top->unsignedp && ! pfile->state.skip_eval
714 && ! possible_sum_sign (v1, v2, top->value))
715 integer_overflow (pfile);
716 break;
717 case CPP_MINUS:
718 top->value = v1 - v2;
719 top->unsignedp = unsigned1 | unsigned2;
720 if (! top->unsignedp && ! pfile->state.skip_eval
721 && ! possible_sum_sign (top->value, v2, v1))
722 integer_overflow (pfile);
723 break;
724 case CPP_MULT:
725 top->unsignedp = unsigned1 | unsigned2;
726 if (top->unsignedp)
727 top->value = (unsigned HOST_WIDEST_INT) v1 * v2;
728 else if (!pfile->state.skip_eval)
729 {
730 top->value = v1 * v2;
731 if (v1 && (top->value / v1 != v2
732 || (top->value & v1 & v2) < 0))
733 integer_overflow (pfile);
734 }
735 break;
736 case CPP_DIV:
737 case CPP_MOD:
738 if (pfile->state.skip_eval)
739 break;
740 if (v2 == 0)
741 {
742 cpp_error (pfile, DL_ERROR, "division by zero in #if");
743 return 0;
744 }
745 top->unsignedp = unsigned1 | unsigned2;
746 if (top[1].op == CPP_DIV)
747 {
748 if (top->unsignedp)
749 top->value = (unsigned HOST_WIDEST_INT) v1 / v2;
750 else
751 {
752 top->value = v1 / v2;
753 if ((top->value & v1 & v2) < 0)
754 integer_overflow (pfile);
755 }
756 }
757 else
758 {
759 if (top->unsignedp)
760 top->value = (unsigned HOST_WIDEST_INT) v1 % v2;
761 else
762 top->value = v1 % v2;
763 }
764 break;
765
766 case CPP_OR_OR:
767 top->value = v1 || v2;
768 top->unsignedp = 0;
769 if (v1) pfile->state.skip_eval--;
770 break;
771 case CPP_AND_AND:
772 top->value = v1 && v2;
773 top->unsignedp = 0;
774 if (!v1) pfile->state.skip_eval--;
775 break;
776 case CPP_COMMA:
777 if (CPP_PEDANTIC (pfile))
778 cpp_error (pfile, DL_PEDWARN,
779 "comma operator in operand of #if");
780 top->value = v2;
781 top->unsignedp = unsigned2;
782 break;
783 case CPP_QUERY:
784 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
785 return 0;
786 case CPP_COLON:
87ed109f
NB
787 top--;
788 if (top->value) pfile->state.skip_eval--;
789 top->value = top->value ? v1 : v2;
790 top->unsignedp = unsigned1 | unsigned2;
791 break;
792 case CPP_OPEN_PAREN:
793 if (op != CPP_CLOSE_PAREN)
794 {
795 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
796 return 0;
797 }
798 top->value = v2;
799 top->unsignedp = unsigned2;
800 return top;
801 }
802 }
803
804 if (op == CPP_CLOSE_PAREN)
805 {
806 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
807 return 0;
808 }
809
810 return top;
811}
812
813/* Returns the position of the old top of stack after expansion. */
814struct op *
815_cpp_expand_op_stack (pfile)
816 cpp_reader *pfile;
817{
818 size_t n = (size_t) (pfile->op_limit - pfile->op_stack);
819
820 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
821 (n * 2 + 20) * sizeof (struct op));
822
823 return pfile->op_stack + n;
7f2935c7 824}
This page took 0.888235 seconds and 5 git commands to generate.