* config/arm/netbsd.h (TARGET_OS_CPP_BUILTINS): Use
[official-gcc.git] / gcc / cppexp.c
blob73a892ff79bf6627f9ebd419d38c1314ae31cdc4
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
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.
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.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
26 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
27 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
28 #define LOW_PART(num_part) (num_part & HALF_MASK)
29 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
31 struct op
33 cpp_num value; /* The value logically "right" of op. */
34 enum cpp_ttype op;
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)
40 static bool num_positive PARAMS ((cpp_num, size_t));
41 static bool num_greater_eq PARAMS ((cpp_num, cpp_num, size_t));
42 static cpp_num num_trim PARAMS ((cpp_num, size_t));
43 static cpp_num num_part_mul PARAMS ((cpp_num_part, cpp_num_part));
45 static cpp_num num_unary_op PARAMS ((cpp_reader *, cpp_num, enum cpp_ttype));
46 static cpp_num num_binary_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
47 enum cpp_ttype));
48 static cpp_num num_negate PARAMS ((cpp_num, size_t));
49 static cpp_num num_bitwise_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
50 enum cpp_ttype));
51 static cpp_num num_inequality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
52 enum cpp_ttype));
53 static cpp_num num_equality_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
54 enum cpp_ttype));
55 static cpp_num num_mul PARAMS ((cpp_reader *, cpp_num, cpp_num,
56 enum cpp_ttype));
57 static cpp_num num_div_op PARAMS ((cpp_reader *, cpp_num, cpp_num,
58 enum cpp_ttype));
59 static cpp_num num_lshift PARAMS ((cpp_num, size_t, size_t));
60 static cpp_num num_rshift PARAMS ((cpp_num, size_t, size_t));
62 static cpp_num append_digit PARAMS ((cpp_num, int, int, size_t));
63 static cpp_num parse_defined PARAMS ((cpp_reader *));
64 static cpp_num eval_token PARAMS ((cpp_reader *, const cpp_token *));
65 static struct op *reduce PARAMS ((cpp_reader *, struct op *, enum cpp_ttype));
66 static unsigned int interpret_float_suffix PARAMS ((const uchar *, size_t));
67 static unsigned int interpret_int_suffix PARAMS ((const uchar *, size_t));
69 /* Token type abuse to create unary plus and minus operators. */
70 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
71 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
73 /* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
75 #define SYNTAX_ERROR(msgid) \
76 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
80 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
83 static unsigned int
84 interpret_float_suffix (s, len)
85 const uchar *s;
86 size_t len;
88 size_t f = 0, l = 0, i = 0;
90 while (len--)
91 switch (s[len])
93 case 'f': case 'F': f++; break;
94 case 'l': case 'L': l++; break;
95 case 'i': case 'I':
96 case 'j': case 'J': i++; break;
97 default:
98 return 0;
101 if (f + l > 1 || i > 1)
102 return 0;
104 return ((i ? CPP_N_IMAGINARY : 0)
105 | (f ? CPP_N_SMALL :
106 l ? CPP_N_LARGE : CPP_N_MEDIUM));
109 /* Subroutine of cpp_classify_number. S points to an integer suffix
110 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
111 flag vector describing the suffix. */
112 static unsigned int
113 interpret_int_suffix (s, len)
114 const uchar *s;
115 size_t len;
117 size_t u, l, i;
119 u = l = i = 0;
121 while (len--)
122 switch (s[len])
124 case 'u': case 'U': u++; break;
125 case 'i': case 'I':
126 case 'j': case 'J': i++; break;
127 case 'l': case 'L': l++;
128 /* If there are two Ls, they must be adjacent and the same case. */
129 if (l == 2 && s[len] != s[len + 1])
130 return 0;
131 break;
132 default:
133 return 0;
136 if (l > 2 || u > 1 || i > 1)
137 return 0;
139 return ((i ? CPP_N_IMAGINARY : 0)
140 | (u ? CPP_N_UNSIGNED : 0)
141 | ((l == 0) ? CPP_N_SMALL
142 : (l == 1) ? CPP_N_MEDIUM : CPP_N_LARGE));
145 /* Categorize numeric constants according to their field (integer,
146 floating point, or invalid), radix (decimal, octal, hexadecimal),
147 and type suffixes. */
148 unsigned int
149 cpp_classify_number (pfile, token)
150 cpp_reader *pfile;
151 const cpp_token *token;
153 const uchar *str = token->val.str.text;
154 const uchar *limit;
155 unsigned int max_digit, result, radix;
156 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag;
158 /* If the lexer has done its job, length one can only be a single
159 digit. Fast-path this very common case. */
160 if (token->val.str.len == 1)
161 return CPP_N_INTEGER | CPP_N_SMALL | CPP_N_DECIMAL;
163 limit = str + token->val.str.len;
164 float_flag = NOT_FLOAT;
165 max_digit = 0;
166 radix = 10;
168 /* First, interpret the radix. */
169 if (*str == '0')
171 radix = 8;
172 str++;
174 /* Require at least one hex digit to classify it as hex. */
175 if ((*str == 'x' || *str == 'X') && ISXDIGIT (str[1]))
177 radix = 16;
178 str++;
182 /* Now scan for a well-formed integer or float. */
183 for (;;)
185 unsigned int c = *str++;
187 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16))
189 c = hex_value (c);
190 if (c > max_digit)
191 max_digit = c;
193 else if (c == '.')
195 if (float_flag == NOT_FLOAT)
196 float_flag = AFTER_POINT;
197 else
198 SYNTAX_ERROR ("too many decimal points in number");
200 else if ((radix <= 10 && (c == 'e' || c == 'E'))
201 || (radix == 16 && (c == 'p' || c == 'P')))
203 float_flag = AFTER_EXPON;
204 break;
206 else
208 /* Start of suffix. */
209 str--;
210 break;
214 if (float_flag != NOT_FLOAT && radix == 8)
215 radix = 10;
217 if (max_digit >= radix)
218 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit);
220 if (float_flag != NOT_FLOAT)
222 if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99))
223 cpp_error (pfile, DL_PEDWARN,
224 "use of C99 hexadecimal floating constant");
226 if (float_flag == AFTER_EXPON)
228 if (*str == '+' || *str == '-')
229 str++;
231 /* Exponent is decimal, even if string is a hex float. */
232 if (!ISDIGIT (*str))
233 SYNTAX_ERROR ("exponent has no digits");
236 str++;
237 while (ISDIGIT (*str));
239 else if (radix == 16)
240 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
242 result = interpret_float_suffix (str, limit - str);
243 if (result == 0)
245 cpp_error (pfile, DL_ERROR,
246 "invalid suffix \"%.*s\" on floating constant",
247 limit - str, str);
248 return CPP_N_INVALID;
251 /* Traditional C didn't accept any floating suffixes. */
252 if (limit != str
253 && CPP_WTRADITIONAL (pfile)
254 && ! cpp_sys_macro_p (pfile))
255 cpp_error (pfile, DL_WARNING,
256 "traditional C rejects the \"%.*s\" suffix",
257 limit - str, str);
259 result |= CPP_N_FLOATING;
261 else
263 result = interpret_int_suffix (str, limit - str);
264 if (result == 0)
266 cpp_error (pfile, DL_ERROR,
267 "invalid suffix \"%.*s\" on integer constant",
268 limit - str, str);
269 return CPP_N_INVALID;
272 /* Traditional C only accepted the 'L' suffix. */
273 if (result != CPP_N_SMALL && result != CPP_N_MEDIUM
274 && CPP_WTRADITIONAL (pfile)
275 && ! cpp_sys_macro_p (pfile))
276 cpp_error (pfile, DL_WARNING,
277 "traditional C rejects the \"%.*s\" suffix",
278 limit - str, str);
280 if ((result & CPP_N_WIDTH) == CPP_N_LARGE
281 && ! CPP_OPTION (pfile, c99)
282 && CPP_OPTION (pfile, warn_long_long))
283 cpp_error (pfile, DL_PEDWARN, "use of C99 long long integer constant");
285 result |= CPP_N_INTEGER;
288 if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile))
289 cpp_error (pfile, DL_PEDWARN, "imaginary constants are a GCC extension");
291 if (radix == 10)
292 result |= CPP_N_DECIMAL;
293 else if (radix == 16)
294 result |= CPP_N_HEX;
295 else
296 result |= CPP_N_OCTAL;
298 return result;
300 syntax_error:
301 return CPP_N_INVALID;
304 /* cpp_interpret_integer converts an integer constant into a cpp_num,
305 of precision options->precision.
307 We do not provide any interface for decimal->float conversion,
308 because the preprocessor doesn't need it and the floating point
309 handling in GCC proper is too ugly to speak of. */
310 cpp_num
311 cpp_interpret_integer (pfile, token, type)
312 cpp_reader *pfile;
313 const cpp_token *token;
314 unsigned int type;
316 const uchar *p, *end;
317 cpp_num result;
319 result.low = 0;
320 result.high = 0;
321 result.unsignedp = type & CPP_N_UNSIGNED;
322 result.overflow = 0;
324 p = token->val.str.text;
325 end = p + token->val.str.len;
327 /* Common case of a single digit. */
328 if (token->val.str.len == 1)
329 result.low = p[0] - '0';
330 else
332 cpp_num_part max;
333 size_t precision = CPP_OPTION (pfile, precision);
334 unsigned int base = 10, c = 0;
335 bool overflow = false;
337 if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
339 base = 8;
340 p++;
342 else if ((type & CPP_N_RADIX) == CPP_N_HEX)
344 base = 16;
345 p += 2;
348 /* We can add a digit to numbers strictly less than this without
349 needing the precision and slowness of double integers. */
350 max = ~(cpp_num_part) 0;
351 if (precision < PART_PRECISION)
352 max >>= PART_PRECISION - precision;
353 max = (max - base + 1) / base + 1;
355 for (; p < end; p++)
357 c = *p;
359 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
360 c = hex_value (c);
361 else
362 break;
364 /* Strict inequality for when max is set to zero. */
365 if (result.low < max)
366 result.low = result.low * base + c;
367 else
369 result = append_digit (result, c, base, precision);
370 overflow |= result.overflow;
371 max = 0;
375 if (overflow)
376 cpp_error (pfile, DL_PEDWARN,
377 "integer constant is too large for its type");
378 else if (!result.unsignedp && !num_positive (result, precision))
380 /* If too big to be signed, consider it unsigned. Only warn
381 for decimal numbers. */
382 if (base == 10)
383 cpp_error (pfile, DL_WARNING,
384 "integer constant is so large that it is unsigned");
385 result.unsignedp = 1;
389 return result;
392 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
393 BASE. */
394 static cpp_num
395 append_digit (num, digit, base, precision)
396 cpp_num num;
397 int digit, base;
398 size_t precision;
400 cpp_num result;
401 unsigned int shift = 3 + (base == 16);
402 bool overflow;
403 cpp_num_part add_high, add_low;
405 /* Multiply by 8 or 16. Catching this overflow here means we don't
406 need to worry about add_high overflowing. */
407 overflow = num.high >> (PART_PRECISION - shift);
408 result.high = num.high << shift;
409 result.low = num.low << shift;
410 result.high |= num.low >> (PART_PRECISION - shift);
412 if (base == 10)
414 add_low = num.low << 1;
415 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1));
417 else
418 add_high = add_low = 0;
420 if (add_low + digit < add_low)
421 add_high++;
422 add_low += digit;
424 if (result.low + add_low < result.low)
425 add_high++;
426 if (result.high + add_high < result.high)
427 overflow = true;
429 result.low += add_low;
430 result.high += add_high;
432 /* The above code catches overflow of a cpp_num type. This catches
433 overflow of the (possibly shorter) target precision. */
434 num.low = result.low;
435 num.high = result.high;
436 result = num_trim (result, precision);
437 if (!num_eq (result, num))
438 overflow = true;
440 result.unsignedp = num.unsignedp;
441 result.overflow = overflow;
442 return result;
445 /* Handle meeting "defined" in a preprocessor expression. */
446 static cpp_num
447 parse_defined (pfile)
448 cpp_reader *pfile;
450 cpp_num result;
451 int paren = 0;
452 cpp_hashnode *node = 0;
453 const cpp_token *token;
454 cpp_context *initial_context = pfile->context;
456 /* Don't expand macros. */
457 pfile->state.prevent_expansion++;
459 token = cpp_get_token (pfile);
460 if (token->type == CPP_OPEN_PAREN)
462 paren = 1;
463 token = cpp_get_token (pfile);
466 if (token->type == CPP_NAME)
468 node = token->val.node;
469 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN)
471 cpp_error (pfile, DL_ERROR, "missing ')' after \"defined\"");
472 node = 0;
475 else
477 cpp_error (pfile, DL_ERROR,
478 "operator \"defined\" requires an identifier");
479 if (token->flags & NAMED_OP)
481 cpp_token op;
483 op.flags = 0;
484 op.type = token->type;
485 cpp_error (pfile, DL_ERROR,
486 "(\"%s\" is an alternative token for \"%s\" in C++)",
487 cpp_token_as_text (pfile, token),
488 cpp_token_as_text (pfile, &op));
492 if (node)
494 if (pfile->context != initial_context)
495 cpp_error (pfile, DL_WARNING,
496 "this use of \"defined\" may not be portable");
498 /* A possible controlling macro of the form #if !defined ().
499 _cpp_parse_expr checks there was no other junk on the line. */
500 pfile->mi_ind_cmacro = node;
503 pfile->state.prevent_expansion--;
505 result.unsignedp = 0;
506 result.high = 0;
507 result.overflow = 0;
508 result.low = node && node->type == NT_MACRO;
509 return result;
512 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
513 number or character constant, or the result of the "defined" or "#"
514 operators). */
515 static cpp_num
516 eval_token (pfile, token)
517 cpp_reader *pfile;
518 const cpp_token *token;
520 cpp_num result;
521 unsigned int temp;
522 int unsignedp = 0;
524 switch (token->type)
526 case CPP_NUMBER:
527 temp = cpp_classify_number (pfile, token);
528 switch (temp & CPP_N_CATEGORY)
530 case CPP_N_FLOATING:
531 cpp_error (pfile, DL_ERROR,
532 "floating constant in preprocessor expression");
533 break;
534 case CPP_N_INTEGER:
535 if (!(temp & CPP_N_IMAGINARY))
536 return cpp_interpret_integer (pfile, token, temp);
537 cpp_error (pfile, DL_ERROR,
538 "imaginary number in preprocessor expression");
539 break;
541 case CPP_N_INVALID:
542 /* Error already issued. */
543 break;
545 result.high = result.low = 0;
546 break;
548 case CPP_WCHAR:
549 case CPP_CHAR:
551 cppchar_t cc = cpp_interpret_charconst (pfile, token,
552 &temp, &unsignedp);
554 result.high = 0;
555 result.low = cc;
556 /* Sign-extend the result if necessary. */
557 if (!unsignedp && (cppchar_signed_t) cc < 0)
559 if (PART_PRECISION > BITS_PER_CPPCHAR_T)
560 result.low |= ~(~(cpp_num_part) 0
561 >> (PART_PRECISION - BITS_PER_CPPCHAR_T));
562 result.high = ~(cpp_num_part) 0;
563 result = num_trim (result, CPP_OPTION (pfile, precision));
566 break;
568 case CPP_NAME:
569 if (token->val.node == pfile->spec_nodes.n_defined)
570 return parse_defined (pfile);
571 else if (CPP_OPTION (pfile, cplusplus)
572 && (token->val.node == pfile->spec_nodes.n_true
573 || token->val.node == pfile->spec_nodes.n_false))
575 result.high = 0;
576 result.low = (token->val.node == pfile->spec_nodes.n_true);
578 /* Warn about use of true or false in #if when pedantic
579 and stdbool.h has not been included. */
580 if (CPP_PEDANTIC (pfile)
581 && ! cpp_defined (pfile, DSC("__bool_true_false_are_defined")))
582 cpp_error (pfile, DL_PEDWARN,
583 "ISO C++ does not permit \"%s\" in #if",
584 NODE_NAME (token->val.node));
586 else
588 result.high = 0;
589 result.low = 0;
590 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval)
591 cpp_error (pfile, DL_WARNING, "\"%s\" is not defined",
592 NODE_NAME (token->val.node));
594 break;
596 default: /* CPP_HASH */
597 _cpp_test_assertion (pfile, &temp);
598 result.high = 0;
599 result.low = temp;
602 result.unsignedp = unsignedp;
603 result.overflow = 0;
604 return result;
607 /* Operator precedence and flags table.
609 After an operator is returned from the lexer, if it has priority less
610 than the operator on the top of the stack, we reduce the stack by one
611 operator and repeat the test. Since equal priorities do not reduce,
612 this is naturally right-associative.
614 We handle left-associative operators by decrementing the priority of
615 just-lexed operators by one, but retaining the priority of operators
616 already on the stack.
618 The remaining cases are '(' and ')'. We handle '(' by skipping the
619 reduction phase completely. ')' is given lower priority than
620 everything else, including '(', effectively forcing a reduction of the
621 parenthesised expression. If there is a matching '(', the routine
622 reduce() exits immediately. If the normal exit route sees a ')', then
623 there cannot have been a matching '(' and an error message is output.
625 The parser assumes all shifted operators require a left operand unless
626 the flag NO_L_OPERAND is set. These semantics are automatic; any
627 extra semantics need to be handled with operator-specific code. */
629 /* Flags. */
630 #define NO_L_OPERAND (1 << 0)
631 #define LEFT_ASSOC (1 << 1)
633 /* Arity. */
634 #define UNARY (1 << 0)
635 #define BINARY (1 << 1)
636 #define OTHER (1 << 2)
638 typedef cpp_num (*binary_handler) PARAMS ((cpp_reader *, cpp_num, cpp_num,
639 enum cpp_ttype));
640 /* Operator to priority map. Must be in the same order as the first
641 N entries of enum cpp_ttype. */
642 static const struct operator
644 uchar prio;
645 uchar flags;
646 uchar arity;
647 binary_handler handler;
648 } optab[] =
650 /* EQ */ {0, 0, OTHER, NULL}, /* Shouldn't happen. */
651 /* NOT */ {16, NO_L_OPERAND, UNARY, NULL},
652 /* GREATER */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
653 /* LESS */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
654 /* PLUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
655 /* MINUS */ {14, LEFT_ASSOC, BINARY, num_binary_op},
656 /* MULT */ {15, LEFT_ASSOC, BINARY, num_mul},
657 /* DIV */ {15, LEFT_ASSOC, BINARY, num_div_op},
658 /* MOD */ {15, LEFT_ASSOC, BINARY, num_div_op},
659 /* AND */ {9, LEFT_ASSOC, BINARY, num_bitwise_op},
660 /* OR */ {7, LEFT_ASSOC, BINARY, num_bitwise_op},
661 /* XOR */ {8, LEFT_ASSOC, BINARY, num_bitwise_op},
662 /* RSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
663 /* LSHIFT */ {13, LEFT_ASSOC, BINARY, num_binary_op},
665 /* MIN */ {10, LEFT_ASSOC, BINARY, num_binary_op},
666 /* MAX */ {10, LEFT_ASSOC, BINARY, num_binary_op},
668 /* COMPL */ {16, NO_L_OPERAND, UNARY, NULL},
669 /* AND_AND */ {6, LEFT_ASSOC, OTHER, NULL},
670 /* OR_OR */ {5, LEFT_ASSOC, OTHER, NULL},
671 /* QUERY */ {3, 0, OTHER, NULL},
672 /* COLON */ {4, LEFT_ASSOC, OTHER, NULL},
673 /* COMMA */ {2, LEFT_ASSOC, BINARY, num_binary_op},
674 /* OPEN_PAREN */ {1, NO_L_OPERAND, OTHER, NULL},
675 /* CLOSE_PAREN */ {0, 0, OTHER, NULL},
676 /* EOF */ {0, 0, OTHER, NULL},
677 /* EQ_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
678 /* NOT_EQ */ {11, LEFT_ASSOC, BINARY, num_equality_op},
679 /* GREATER_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
680 /* LESS_EQ */ {12, LEFT_ASSOC, BINARY, num_inequality_op},
681 /* UPLUS */ {16, NO_L_OPERAND, UNARY, NULL},
682 /* UMINUS */ {16, NO_L_OPERAND, UNARY, NULL}
685 /* Parse and evaluate a C expression, reading from PFILE.
686 Returns the truth value of the expression.
688 The implementation is an operator precedence parser, i.e. a
689 bottom-up parser, using a stack for not-yet-reduced tokens.
691 The stack base is op_stack, and the current stack pointer is 'top'.
692 There is a stack element for each operator (only), and the most
693 recently pushed operator is 'top->op'. An operand (value) is
694 stored in the 'value' field of the stack element of the operator
695 that precedes it. */
696 bool
697 _cpp_parse_expr (pfile)
698 cpp_reader *pfile;
700 struct op *top = pfile->op_stack;
701 const cpp_token *token = NULL, *prev_token;
702 unsigned int lex_count;
703 bool saw_leading_not, want_value = true;
705 pfile->state.skip_eval = 0;
707 /* Set up detection of #if ! defined(). */
708 pfile->mi_ind_cmacro = 0;
709 saw_leading_not = false;
710 lex_count = 0;
712 /* Lowest priority operator prevents further reductions. */
713 top->op = CPP_EOF;
715 for (;;)
717 struct op op;
719 prev_token = token;
720 token = cpp_get_token (pfile);
721 lex_count++;
722 op.op = token->type;
724 switch (op.op)
726 /* These tokens convert into values. */
727 case CPP_NUMBER:
728 case CPP_CHAR:
729 case CPP_WCHAR:
730 case CPP_NAME:
731 case CPP_HASH:
732 if (!want_value)
733 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
734 cpp_token_as_text (pfile, token));
735 want_value = false;
736 top->value = eval_token (pfile, token);
737 continue;
739 case CPP_NOT:
740 saw_leading_not = lex_count == 1;
741 break;
742 case CPP_PLUS:
743 if (want_value)
744 op.op = CPP_UPLUS;
745 break;
746 case CPP_MINUS:
747 if (want_value)
748 op.op = CPP_UMINUS;
749 break;
750 case CPP_OTHER:
751 if (ISGRAPH (token->val.c))
752 SYNTAX_ERROR2 ("invalid character '%c' in #if", token->val.c);
753 else
754 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token->val.c);
756 default:
757 if ((int) op.op <= (int) CPP_EQ || (int) op.op >= (int) CPP_PLUS_EQ)
758 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
759 cpp_token_as_text (pfile, token));
760 break;
763 /* Check we have a value or operator as appropriate. */
764 if (optab[op.op].flags & NO_L_OPERAND)
766 if (!want_value)
767 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
768 cpp_token_as_text (pfile, token));
770 else if (want_value)
772 /* Ordering here is subtle and intended to favour the
773 missing parenthesis diagnostics over alternatives. */
774 if (op.op == CPP_CLOSE_PAREN)
776 if (top->op == CPP_OPEN_PAREN)
777 SYNTAX_ERROR ("void expression between '(' and ')'");
779 else if (top->op == CPP_EOF)
780 SYNTAX_ERROR ("#if with no expression");
781 if (top->op != CPP_EOF && top->op != CPP_OPEN_PAREN)
782 SYNTAX_ERROR2 ("operator '%s' has no right operand",
783 cpp_token_as_text (pfile, prev_token));
786 top = reduce (pfile, top, op.op);
787 if (!top)
788 goto syntax_error;
790 if (op.op == CPP_EOF)
791 break;
793 switch (op.op)
795 case CPP_CLOSE_PAREN:
796 continue;
797 case CPP_OR_OR:
798 if (!num_zerop (top->value))
799 pfile->state.skip_eval++;
800 break;
801 case CPP_AND_AND:
802 case CPP_QUERY:
803 if (num_zerop (top->value))
804 pfile->state.skip_eval++;
805 break;
806 case CPP_COLON:
807 if (top->op != CPP_QUERY)
808 SYNTAX_ERROR (" ':' without preceding '?'");
809 if (!num_zerop (top[-1].value)) /* Was '?' condition true? */
810 pfile->state.skip_eval++;
811 else
812 pfile->state.skip_eval--;
813 default:
814 break;
817 want_value = true;
819 /* Check for and handle stack overflow. */
820 if (++top == pfile->op_limit)
821 top = _cpp_expand_op_stack (pfile);
823 top->op = op.op;
826 /* The controlling macro expression is only valid if we called lex 3
827 times: <!> <defined expression> and <EOF>. push_conditional ()
828 checks that we are at top-of-file. */
829 if (pfile->mi_ind_cmacro && !(saw_leading_not && lex_count == 3))
830 pfile->mi_ind_cmacro = 0;
832 if (top != pfile->op_stack)
834 cpp_error (pfile, DL_ICE, "unbalanced stack in #if");
835 syntax_error:
836 return false; /* Return false on syntax error. */
839 return !num_zerop (top->value);
842 /* Reduce the operator / value stack if possible, in preparation for
843 pushing operator OP. Returns NULL on error, otherwise the top of
844 the stack. */
845 static struct op *
846 reduce (pfile, top, op)
847 cpp_reader *pfile;
848 struct op *top;
849 enum cpp_ttype op;
851 unsigned int prio;
853 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2)
855 bad_op:
856 cpp_error (pfile, DL_ICE, "impossible operator '%u'", top->op);
857 return 0;
860 if (op == CPP_OPEN_PAREN)
861 return top;
863 /* Decrement the priority of left-associative operators to force a
864 reduction with operators of otherwise equal priority. */
865 prio = optab[op].prio - ((optab[op].flags & LEFT_ASSOC) != 0);
866 while (prio < optab[top->op].prio)
868 if (optab[top->op].arity == UNARY)
870 if (!pfile->state.skip_eval)
871 top[-1].value = num_unary_op (pfile, top->value, top->op);
872 top--;
874 else if (optab[top->op].arity == BINARY)
876 if (!pfile->state.skip_eval)
877 top[-1].value = (* (binary_handler) optab[top->op].handler)
878 (pfile, top[-1].value, top->value, top->op);
879 top--;
881 /* Anything changing skip_eval has to be handled here. */
882 else switch (top--->op)
884 case CPP_OR_OR:
885 if (!num_zerop (top->value))
886 pfile->state.skip_eval--;
887 top->value.low = !num_zerop (top->value) || !num_zerop (top[1].value);
888 top->value.high = 0;
889 top->value.unsignedp = false;
890 top->value.overflow = false;
891 break;
893 case CPP_AND_AND:
894 if (num_zerop (top->value))
895 pfile->state.skip_eval--;
896 top->value.low = !num_zerop (top->value) && !num_zerop (top[1].value);
897 top->value.high = 0;
898 top->value.unsignedp = false;
899 top->value.overflow = false;
900 break;
902 case CPP_OPEN_PAREN:
903 if (op != CPP_CLOSE_PAREN)
905 cpp_error (pfile, DL_ERROR, "missing ')' in expression");
906 return 0;
908 top->value = top[1].value;
909 return top;
911 case CPP_COLON:
912 top--;
913 if (!num_zerop (top->value))
915 pfile->state.skip_eval--;
916 top->value = top[1].value;
918 else
919 top->value = top[2].value;
920 top->value.unsignedp = (top[1].value.unsignedp
921 || top[2].value.unsignedp);
922 break;
924 case CPP_QUERY:
925 cpp_error (pfile, DL_ERROR, "'?' without following ':'");
926 return 0;
928 default:
929 goto bad_op;
932 if (top->value.overflow && !pfile->state.skip_eval)
933 cpp_error (pfile, DL_PEDWARN,
934 "integer overflow in preprocessor expression");
937 if (op == CPP_CLOSE_PAREN)
939 cpp_error (pfile, DL_ERROR, "missing '(' in expression");
940 return 0;
943 return top;
946 /* Returns the position of the old top of stack after expansion. */
947 struct op *
948 _cpp_expand_op_stack (pfile)
949 cpp_reader *pfile;
951 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack);
952 size_t new_size = old_size * 2 + 20;
954 pfile->op_stack = (struct op *) xrealloc (pfile->op_stack,
955 new_size * sizeof (struct op));
956 pfile->op_limit = pfile->op_stack + new_size;
958 return pfile->op_stack + old_size;
961 /* Clears the unused high order bits of the number pointed to by PNUM. */
962 static cpp_num
963 num_trim (num, precision)
964 cpp_num num;
965 size_t precision;
967 if (precision > PART_PRECISION)
969 precision -= PART_PRECISION;
970 if (precision < PART_PRECISION)
971 num.high &= ((cpp_num_part) 1 << precision) - 1;
973 else
975 if (precision < PART_PRECISION)
976 num.low &= ((cpp_num_part) 1 << precision) - 1;
977 num.high = 0;
980 return num;
983 /* True iff A (presumed signed) >= 0. */
984 static bool
985 num_positive (num, precision)
986 cpp_num num;
987 size_t precision;
989 if (precision > PART_PRECISION)
991 precision -= PART_PRECISION;
992 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0;
995 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0;
998 /* Returns the negative of NUM. */
999 static cpp_num
1000 num_negate (num, precision)
1001 cpp_num num;
1002 size_t precision;
1004 cpp_num copy;
1006 copy = num;
1007 num.high = ~num.high;
1008 num.low = ~num.low;
1009 if (++num.low == 0)
1010 num.high++;
1011 num = num_trim (num, precision);
1012 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num));
1014 return num;
1017 /* Returns true if A >= B. */
1018 static bool
1019 num_greater_eq (pa, pb, precision)
1020 cpp_num pa, pb;
1021 size_t precision;
1023 bool unsignedp;
1025 unsignedp = pa.unsignedp || pb.unsignedp;
1027 if (!unsignedp)
1029 /* Both numbers have signed type. If they are of different
1030 sign, the answer is the sign of A. */
1031 unsignedp = num_positive (pa, precision);
1033 if (unsignedp != num_positive (pb, precision))
1034 return unsignedp;
1036 /* Otherwise we can do an unsigned comparison. */
1039 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low);
1042 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1043 static cpp_num
1044 num_bitwise_op (pfile, lhs, rhs, op)
1045 cpp_reader *pfile ATTRIBUTE_UNUSED;
1046 cpp_num lhs, rhs;
1047 enum cpp_ttype op;
1049 lhs.overflow = false;
1050 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp;
1052 /* As excess precision is zeroed, there is no need to num_trim () as
1053 these operations cannot introduce a set bit there. */
1054 if (op == CPP_AND)
1056 lhs.low &= rhs.low;
1057 lhs.high &= rhs.high;
1059 else if (op == CPP_OR)
1061 lhs.low |= rhs.low;
1062 lhs.high |= rhs.high;
1064 else
1066 lhs.low ^= rhs.low;
1067 lhs.high ^= rhs.high;
1070 return lhs;
1073 /* Returns LHS OP RHS, where OP is an inequality. */
1074 static cpp_num
1075 num_inequality_op (pfile, lhs, rhs, op)
1076 cpp_reader *pfile;
1077 cpp_num lhs, rhs;
1078 enum cpp_ttype op;
1080 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision));
1082 if (op == CPP_GREATER_EQ)
1083 lhs.low = gte;
1084 else if (op == CPP_LESS)
1085 lhs.low = !gte;
1086 else if (op == CPP_GREATER)
1087 lhs.low = gte && !num_eq (lhs, rhs);
1088 else /* CPP_LESS_EQ. */
1089 lhs.low = !gte || num_eq (lhs, rhs);
1091 lhs.high = 0;
1092 lhs.overflow = false;
1093 lhs.unsignedp = false;
1094 return lhs;
1097 /* Returns LHS OP RHS, where OP is == or !=. */
1098 static cpp_num
1099 num_equality_op (pfile, lhs, rhs, op)
1100 cpp_reader *pfile ATTRIBUTE_UNUSED;
1101 cpp_num lhs, rhs;
1102 enum cpp_ttype op;
1104 lhs.low = num_eq (lhs, rhs);
1105 if (op == CPP_NOT_EQ)
1106 lhs.low = !lhs.low;
1107 lhs.high = 0;
1108 lhs.overflow = false;
1109 lhs.unsignedp = false;
1110 return lhs;
1113 /* Shift NUM, of width PRECISION, right by N bits. */
1114 static cpp_num
1115 num_rshift (num, precision, n)
1116 cpp_num num;
1117 size_t precision, n;
1119 cpp_num_part sign_mask;
1121 if (num.unsignedp || num_positive (num, precision))
1122 sign_mask = 0;
1123 else
1124 sign_mask = ~(cpp_num_part) 0;
1126 if (n >= precision)
1127 num.high = num.low = sign_mask;
1128 else
1130 /* Sign-extend. */
1131 if (precision < PART_PRECISION)
1132 num.high = sign_mask, num.low |= sign_mask << precision;
1133 else if (precision < 2 * PART_PRECISION)
1134 num.high |= sign_mask << (precision - PART_PRECISION);
1136 if (n >= PART_PRECISION)
1138 n -= PART_PRECISION;
1139 num.low = num.high;
1140 num.high = sign_mask;
1143 if (n)
1145 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n));
1146 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n));
1150 num = num_trim (num, precision);
1151 num.overflow = false;
1152 return num;
1155 /* Shift NUM, of width PRECISION, left by N bits. */
1156 static cpp_num
1157 num_lshift (num, precision, n)
1158 cpp_num num;
1159 size_t precision, n;
1161 if (n >= precision)
1163 num.overflow = !num.unsignedp && !num_zerop (num);
1164 num.high = num.low = 0;
1166 else
1168 cpp_num orig, maybe_orig;
1169 size_t m = n;
1171 orig = num;
1172 if (m >= PART_PRECISION)
1174 m -= PART_PRECISION;
1175 num.high = num.low;
1176 num.low = 0;
1178 if (m)
1180 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m));
1181 num.low <<= m;
1183 num = num_trim (num, precision);
1185 if (num.unsignedp)
1186 num.overflow = false;
1187 else
1189 maybe_orig = num_rshift (num, precision, n);
1190 num.overflow = !num_eq (orig, maybe_orig);
1194 return num;
1197 /* The four unary operators: +, -, ! and ~. */
1198 static cpp_num
1199 num_unary_op (pfile, num, op)
1200 cpp_reader *pfile;
1201 cpp_num num;
1202 enum cpp_ttype op;
1204 switch (op)
1206 case CPP_UPLUS:
1207 if (CPP_WTRADITIONAL (pfile))
1208 cpp_error (pfile, DL_WARNING,
1209 "traditional C rejects the unary plus operator");
1210 num.overflow = false;
1211 break;
1213 case CPP_UMINUS:
1214 num = num_negate (num, CPP_OPTION (pfile, precision));
1215 break;
1217 case CPP_COMPL:
1218 num.high = ~num.high;
1219 num.low = ~num.low;
1220 num = num_trim (num, CPP_OPTION (pfile, precision));
1221 num.overflow = false;
1222 break;
1224 default: /* case CPP_NOT: */
1225 num.low = num_zerop (num);
1226 num.high = 0;
1227 num.overflow = false;
1228 num.unsignedp = false;
1229 break;
1232 return num;
1235 /* The various binary operators. */
1236 static cpp_num
1237 num_binary_op (pfile, lhs, rhs, op)
1238 cpp_reader *pfile;
1239 cpp_num lhs, rhs;
1240 enum cpp_ttype op;
1242 cpp_num result;
1243 size_t precision = CPP_OPTION (pfile, precision);
1244 bool gte;
1245 size_t n;
1247 switch (op)
1249 /* Shifts. */
1250 case CPP_LSHIFT:
1251 case CPP_RSHIFT:
1252 if (!rhs.unsignedp && !num_positive (rhs, precision))
1254 /* A negative shift is a positive shift the other way. */
1255 if (op == CPP_LSHIFT)
1256 op = CPP_RSHIFT;
1257 else
1258 op = CPP_LSHIFT;
1259 rhs = num_negate (rhs, precision);
1261 if (rhs.high)
1262 n = ~0; /* Maximal. */
1263 else
1264 n = rhs.low;
1265 if (op == CPP_LSHIFT)
1266 lhs = num_lshift (lhs, precision, n);
1267 else
1268 lhs = num_rshift (lhs, precision, n);
1269 break;
1271 /* Min / Max. */
1272 case CPP_MIN:
1273 case CPP_MAX:
1275 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1277 gte = num_greater_eq (lhs, rhs, precision);
1278 if (op == CPP_MIN)
1279 gte = !gte;
1280 if (!gte)
1281 lhs = rhs;
1282 lhs.unsignedp = unsignedp;
1284 break;
1286 /* Arithmetic. */
1287 case CPP_MINUS:
1288 rhs = num_negate (rhs, precision);
1289 case CPP_PLUS:
1290 result.low = lhs.low + rhs.low;
1291 result.high = lhs.high + rhs.high;
1292 if (result.low < lhs.low)
1293 result.high++;
1295 result = num_trim (result, precision);
1296 result.unsignedp = lhs.unsignedp || rhs.unsignedp;
1297 if (result.unsignedp)
1298 result.overflow = false;
1299 else
1301 bool lhsp = num_positive (lhs, precision);
1302 result.overflow = (lhsp == num_positive (rhs, precision)
1303 && lhsp != num_positive (result, precision));
1305 return result;
1307 /* Comma. */
1308 default: /* case CPP_COMMA: */
1309 if (CPP_PEDANTIC (pfile))
1310 cpp_error (pfile, DL_PEDWARN,
1311 "comma operator in operand of #if");
1312 lhs = rhs;
1313 break;
1316 return lhs;
1319 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1320 cannot overflow. */
1321 static cpp_num
1322 num_part_mul (lhs, rhs)
1323 cpp_num_part lhs, rhs;
1325 cpp_num result;
1326 cpp_num_part middle[2], temp;
1328 result.low = LOW_PART (lhs) * LOW_PART (rhs);
1329 result.high = HIGH_PART (lhs) * HIGH_PART (rhs);
1331 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs);
1332 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs);
1334 temp = result.low;
1335 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2);
1336 if (result.low < temp)
1337 result.high++;
1339 temp = result.low;
1340 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2);
1341 if (result.low < temp)
1342 result.high++;
1344 result.high += HIGH_PART (middle[0]);
1345 result.high += HIGH_PART (middle[1]);
1347 return result;
1350 /* Multiply two preprocessing numbers. */
1351 static cpp_num
1352 num_mul (pfile, lhs, rhs, op)
1353 cpp_reader *pfile;
1354 cpp_num lhs, rhs;
1355 enum cpp_ttype op ATTRIBUTE_UNUSED;
1357 cpp_num result, temp;
1358 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1359 bool overflow, negate = false;
1360 size_t precision = CPP_OPTION (pfile, precision);
1362 /* Prepare for unsigned multiplication. */
1363 if (!unsignedp)
1365 if (!num_positive (lhs, precision))
1366 negate = !negate, lhs = num_negate (lhs, precision);
1367 if (!num_positive (rhs, precision))
1368 negate = !negate, rhs = num_negate (rhs, precision);
1371 overflow = lhs.high && rhs.high;
1372 result = num_part_mul (lhs.low, rhs.low);
1374 temp = num_part_mul (lhs.high, rhs.low);
1375 result.high += temp.low;
1376 if (temp.high)
1377 overflow = true;
1379 temp = num_part_mul (lhs.low, rhs.high);
1380 result.high += temp.low;
1381 if (temp.high)
1382 overflow = true;
1384 temp.low = result.low, temp.high = result.high;
1385 result = num_trim (result, precision);
1386 if (!num_eq (result, temp))
1387 overflow = true;
1389 if (negate)
1390 result = num_negate (result, precision);
1392 if (unsignedp)
1393 result.overflow = false;
1394 else
1395 result.overflow = overflow || (num_positive (result, precision) ^ !negate
1396 && !num_zerop (result));
1397 result.unsignedp = unsignedp;
1399 return result;
1402 /* Divide two preprocessing numbers, returning the answer or the
1403 remainder depending upon OP. */
1404 static cpp_num
1405 num_div_op (pfile, lhs, rhs, op)
1406 cpp_reader *pfile;
1407 cpp_num lhs, rhs;
1408 enum cpp_ttype op;
1410 cpp_num result, sub;
1411 cpp_num_part mask;
1412 bool unsignedp = lhs.unsignedp || rhs.unsignedp;
1413 bool negate = false, lhs_neg = false;
1414 size_t i, precision = CPP_OPTION (pfile, precision);
1416 /* Prepare for unsigned division. */
1417 if (!unsignedp)
1419 if (!num_positive (lhs, precision))
1420 negate = !negate, lhs_neg = true, lhs = num_negate (lhs, precision);
1421 if (!num_positive (rhs, precision))
1422 negate = !negate, rhs = num_negate (rhs, precision);
1425 /* Find the high bit. */
1426 if (rhs.high)
1428 i = precision - 1;
1429 mask = (cpp_num_part) 1 << (i - PART_PRECISION);
1430 for (; ; i--, mask >>= 1)
1431 if (rhs.high & mask)
1432 break;
1434 else if (rhs.low)
1436 if (precision > PART_PRECISION)
1437 i = precision - PART_PRECISION - 1;
1438 else
1439 i = precision - 1;
1440 mask = (cpp_num_part) 1 << i;
1441 for (; ; i--, mask >>= 1)
1442 if (rhs.low & mask)
1443 break;
1445 else
1447 cpp_error (pfile, DL_ERROR, "division by zero in #if");
1448 return lhs;
1451 /* First non-zero bit of RHS is bit I. Do naive division by
1452 shifting the RHS fully left, and subtracting from LHS if LHS is
1453 at least as big, and then repeating but with one less shift.
1454 This is not very efficient, but is easy to understand. */
1456 rhs.unsignedp = true;
1457 lhs.unsignedp = true;
1458 i = precision - i - 1;
1459 sub = num_lshift (rhs, precision, i);
1461 result.high = result.low = 0;
1462 for (;;)
1464 if (num_greater_eq (lhs, sub, precision))
1466 lhs = num_binary_op (pfile, lhs, sub, CPP_MINUS);
1467 if (i >= PART_PRECISION)
1468 result.high |= (cpp_num_part) 1 << (i - PART_PRECISION);
1469 else
1470 result.low |= (cpp_num_part) 1 << i;
1472 if (i-- == 0)
1473 break;
1474 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1));
1475 sub.high >>= 1;
1478 /* We divide so that the remainder has the sign of the LHS. */
1479 if (op == CPP_DIV)
1481 result.unsignedp = unsignedp;
1482 if (unsignedp)
1483 result.overflow = false;
1484 else
1486 if (negate)
1487 result = num_negate (result, precision);
1488 result.overflow = num_positive (result, precision) ^ !negate;
1491 return result;
1494 /* CPP_MOD. */
1495 lhs.unsignedp = unsignedp;
1496 lhs.overflow = false;
1497 if (lhs_neg)
1498 lhs = num_negate (lhs, precision);
1500 return lhs;