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