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
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. */
23 #include "coretypes.h"
28 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
29 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
30 #define LOW_PART(num_part) (num_part & HALF_MASK)
31 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
35 const cpp_token
*token
; /* The token forming op (for diagnostics). */
36 cpp_num value
; /* The value logically "right" of op. */
40 /* Some simple utility routines on double integers. */
41 #define num_zerop(num) ((num.low | num.high) == 0)
42 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
43 static bool num_positive
PARAMS ((cpp_num
, size_t));
44 static bool num_greater_eq
PARAMS ((cpp_num
, cpp_num
, size_t));
45 static cpp_num num_trim
PARAMS ((cpp_num
, size_t));
46 static cpp_num num_part_mul
PARAMS ((cpp_num_part
, cpp_num_part
));
48 static cpp_num num_unary_op
PARAMS ((cpp_reader
*, cpp_num
, enum cpp_ttype
));
49 static cpp_num num_binary_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
51 static cpp_num num_negate
PARAMS ((cpp_num
, size_t));
52 static cpp_num num_bitwise_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
54 static cpp_num num_inequality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
56 static cpp_num num_equality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
58 static cpp_num num_mul
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
));
59 static cpp_num num_div_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
61 static cpp_num num_lshift
PARAMS ((cpp_num
, size_t, size_t));
62 static cpp_num num_rshift
PARAMS ((cpp_num
, size_t, size_t));
64 static cpp_num append_digit
PARAMS ((cpp_num
, int, int, size_t));
65 static cpp_num parse_defined
PARAMS ((cpp_reader
*));
66 static cpp_num eval_token
PARAMS ((cpp_reader
*, const cpp_token
*));
67 static struct op
*reduce
PARAMS ((cpp_reader
*, struct op
*, enum cpp_ttype
));
68 static unsigned int interpret_float_suffix
PARAMS ((const uchar
*, size_t));
69 static unsigned int interpret_int_suffix
PARAMS ((const uchar
*, size_t));
70 static void check_promotion
PARAMS ((cpp_reader
*, const struct op
*));
72 /* Token type abuse to create unary plus and minus operators. */
73 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
74 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
76 /* With -O2, gcc appears to produce nice code, moving the error
77 message load and subsequent jump completely out of the main path. */
78 #define SYNTAX_ERROR(msgid) \
79 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
80 #define SYNTAX_ERROR2(msgid, arg) \
81 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
83 /* Subroutine of cpp_classify_number. S points to a float suffix of
84 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
85 flag vector describing the suffix. */
87 interpret_float_suffix (s
, len
)
91 size_t f
= 0, l
= 0, i
= 0;
96 case 'f': case 'F': f
++; break;
97 case 'l': case 'L': l
++; break;
99 case 'j': case 'J': i
++; break;
104 if (f
+ l
> 1 || i
> 1)
107 return ((i
? CPP_N_IMAGINARY
: 0)
109 l
? CPP_N_LARGE
: CPP_N_MEDIUM
));
112 /* Subroutine of cpp_classify_number. S points to an integer suffix
113 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
114 flag vector describing the suffix. */
116 interpret_int_suffix (s
, len
)
127 case 'u': case 'U': u
++; break;
129 case 'j': case 'J': i
++; break;
130 case 'l': case 'L': l
++;
131 /* If there are two Ls, they must be adjacent and the same case. */
132 if (l
== 2 && s
[len
] != s
[len
+ 1])
139 if (l
> 2 || u
> 1 || i
> 1)
142 return ((i
? CPP_N_IMAGINARY
: 0)
143 | (u
? CPP_N_UNSIGNED
: 0)
144 | ((l
== 0) ? CPP_N_SMALL
145 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
148 /* Categorize numeric constants according to their field (integer,
149 floating point, or invalid), radix (decimal, octal, hexadecimal),
150 and type suffixes. */
152 cpp_classify_number (pfile
, token
)
154 const cpp_token
*token
;
156 const uchar
*str
= token
->val
.str
.text
;
158 unsigned int max_digit
, result
, radix
;
159 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
161 /* If the lexer has done its job, length one can only be a single
162 digit. Fast-path this very common case. */
163 if (token
->val
.str
.len
== 1)
164 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
166 limit
= str
+ token
->val
.str
.len
;
167 float_flag
= NOT_FLOAT
;
171 /* First, interpret the radix. */
177 /* Require at least one hex digit to classify it as hex. */
178 if ((*str
== 'x' || *str
== 'X')
179 && (str
[1] == '.' || ISXDIGIT (str
[1])))
186 /* Now scan for a well-formed integer or float. */
189 unsigned int c
= *str
++;
191 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
199 if (float_flag
== NOT_FLOAT
)
200 float_flag
= AFTER_POINT
;
202 SYNTAX_ERROR ("too many decimal points in number");
204 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
205 || (radix
== 16 && (c
== 'p' || c
== 'P')))
207 float_flag
= AFTER_EXPON
;
212 /* Start of suffix. */
218 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
221 if (max_digit
>= radix
)
222 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
224 if (float_flag
!= NOT_FLOAT
)
226 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
227 cpp_error (pfile
, DL_PEDWARN
,
228 "use of C99 hexadecimal floating constant");
230 if (float_flag
== AFTER_EXPON
)
232 if (*str
== '+' || *str
== '-')
235 /* Exponent is decimal, even if string is a hex float. */
237 SYNTAX_ERROR ("exponent has no digits");
241 while (ISDIGIT (*str
));
243 else if (radix
== 16)
244 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
246 result
= interpret_float_suffix (str
, limit
- str
);
249 cpp_error (pfile
, DL_ERROR
,
250 "invalid suffix \"%.*s\" on floating constant",
251 (int) (limit
- str
), str
);
252 return CPP_N_INVALID
;
255 /* Traditional C didn't accept any floating suffixes. */
257 && CPP_WTRADITIONAL (pfile
)
258 && ! cpp_sys_macro_p (pfile
))
259 cpp_error (pfile
, DL_WARNING
,
260 "traditional C rejects the \"%.*s\" suffix",
261 (int) (limit
- str
), str
);
263 result
|= CPP_N_FLOATING
;
267 result
= interpret_int_suffix (str
, limit
- str
);
270 cpp_error (pfile
, DL_ERROR
,
271 "invalid suffix \"%.*s\" on integer constant",
272 (int) (limit
- str
), str
);
273 return CPP_N_INVALID
;
276 /* Traditional C only accepted the 'L' suffix.
277 Suppress warning about 'LL' with -Wno-long-long. */
278 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
280 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
281 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
283 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
284 cpp_error (pfile
, DL_WARNING
,
285 "traditional C rejects the \"%.*s\" suffix",
286 (int) (limit
- str
), str
);
289 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
290 && ! CPP_OPTION (pfile
, c99
)
291 && CPP_OPTION (pfile
, warn_long_long
))
292 cpp_error (pfile
, DL_PEDWARN
, "use of C99 long long integer constant");
294 result
|= CPP_N_INTEGER
;
297 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
298 cpp_error (pfile
, DL_PEDWARN
, "imaginary constants are a GCC extension");
301 result
|= CPP_N_DECIMAL
;
302 else if (radix
== 16)
305 result
|= CPP_N_OCTAL
;
310 return CPP_N_INVALID
;
313 /* cpp_interpret_integer converts an integer constant into a cpp_num,
314 of precision options->precision.
316 We do not provide any interface for decimal->float conversion,
317 because the preprocessor doesn't need it and the floating point
318 handling in GCC proper is too ugly to speak of. */
320 cpp_interpret_integer (pfile
, token
, type
)
322 const cpp_token
*token
;
325 const uchar
*p
, *end
;
330 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
331 result
.overflow
= false;
333 p
= token
->val
.str
.text
;
334 end
= p
+ token
->val
.str
.len
;
336 /* Common case of a single digit. */
337 if (token
->val
.str
.len
== 1)
338 result
.low
= p
[0] - '0';
342 size_t precision
= CPP_OPTION (pfile
, precision
);
343 unsigned int base
= 10, c
= 0;
344 bool overflow
= false;
346 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
351 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
357 /* We can add a digit to numbers strictly less than this without
358 needing the precision and slowness of double integers. */
359 max
= ~(cpp_num_part
) 0;
360 if (precision
< PART_PRECISION
)
361 max
>>= PART_PRECISION
- precision
;
362 max
= (max
- base
+ 1) / base
+ 1;
368 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
373 /* Strict inequality for when max is set to zero. */
374 if (result
.low
< max
)
375 result
.low
= result
.low
* base
+ c
;
378 result
= append_digit (result
, c
, base
, precision
);
379 overflow
|= result
.overflow
;
385 cpp_error (pfile
, DL_PEDWARN
,
386 "integer constant is too large for its type");
387 /* If too big to be signed, consider it unsigned. Only warn for
388 decimal numbers. Traditional numbers were always signed (but
389 we still honor an explicit U suffix); but we only have
390 traditional semantics in directives. */
391 else if (!result
.unsignedp
392 && !(CPP_OPTION (pfile
, traditional
)
393 && pfile
->state
.in_directive
)
394 && !num_positive (result
, precision
))
397 cpp_error (pfile
, DL_WARNING
,
398 "integer constant is so large that it is unsigned");
399 result
.unsignedp
= true;
406 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
409 append_digit (num
, digit
, base
, precision
)
415 unsigned int shift
= 3 + (base
== 16);
417 cpp_num_part add_high
, add_low
;
419 /* Multiply by 8 or 16. Catching this overflow here means we don't
420 need to worry about add_high overflowing. */
421 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
422 result
.high
= num
.high
<< shift
;
423 result
.low
= num
.low
<< shift
;
424 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
428 add_low
= num
.low
<< 1;
429 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
432 add_high
= add_low
= 0;
434 if (add_low
+ digit
< add_low
)
438 if (result
.low
+ add_low
< result
.low
)
440 if (result
.high
+ add_high
< result
.high
)
443 result
.low
+= add_low
;
444 result
.high
+= add_high
;
446 /* The above code catches overflow of a cpp_num type. This catches
447 overflow of the (possibly shorter) target precision. */
448 num
.low
= result
.low
;
449 num
.high
= result
.high
;
450 result
= num_trim (result
, precision
);
451 if (!num_eq (result
, num
))
454 result
.unsignedp
= num
.unsignedp
;
455 result
.overflow
= overflow
;
459 /* Handle meeting "defined" in a preprocessor expression. */
461 parse_defined (pfile
)
466 cpp_hashnode
*node
= 0;
467 const cpp_token
*token
;
468 cpp_context
*initial_context
= pfile
->context
;
470 /* Don't expand macros. */
471 pfile
->state
.prevent_expansion
++;
473 token
= cpp_get_token (pfile
);
474 if (token
->type
== CPP_OPEN_PAREN
)
477 token
= cpp_get_token (pfile
);
480 if (token
->type
== CPP_NAME
)
482 node
= token
->val
.node
;
483 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
485 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
491 cpp_error (pfile
, DL_ERROR
,
492 "operator \"defined\" requires an identifier");
493 if (token
->flags
& NAMED_OP
)
498 op
.type
= token
->type
;
499 cpp_error (pfile
, DL_ERROR
,
500 "(\"%s\" is an alternative token for \"%s\" in C++)",
501 cpp_token_as_text (pfile
, token
),
502 cpp_token_as_text (pfile
, &op
));
508 if (pfile
->context
!= initial_context
)
509 cpp_error (pfile
, DL_WARNING
,
510 "this use of \"defined\" may not be portable");
512 _cpp_mark_macro_used (node
);
514 /* A possible controlling macro of the form #if !defined ().
515 _cpp_parse_expr checks there was no other junk on the line. */
516 pfile
->mi_ind_cmacro
= node
;
519 pfile
->state
.prevent_expansion
--;
521 result
.unsignedp
= false;
523 result
.overflow
= false;
524 result
.low
= node
&& node
->type
== NT_MACRO
;
528 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
529 number or character constant, or the result of the "defined" or "#"
532 eval_token (pfile
, token
)
534 const cpp_token
*token
;
543 temp
= cpp_classify_number (pfile
, token
);
544 switch (temp
& CPP_N_CATEGORY
)
547 cpp_error (pfile
, DL_ERROR
,
548 "floating constant in preprocessor expression");
551 if (!(temp
& CPP_N_IMAGINARY
))
552 return cpp_interpret_integer (pfile
, token
, temp
);
553 cpp_error (pfile
, DL_ERROR
,
554 "imaginary number in preprocessor expression");
558 /* Error already issued. */
561 result
.high
= result
.low
= 0;
567 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
572 /* Sign-extend the result if necessary. */
573 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
575 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
576 result
.low
|= ~(~(cpp_num_part
) 0
577 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
578 result
.high
= ~(cpp_num_part
) 0;
579 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
585 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
586 return parse_defined (pfile
);
587 else if (CPP_OPTION (pfile
, cplusplus
)
588 && (token
->val
.node
== pfile
->spec_nodes
.n_true
589 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
592 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
598 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
599 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
600 NODE_NAME (token
->val
.node
));
604 default: /* CPP_HASH */
605 _cpp_test_assertion (pfile
, &temp
);
610 result
.unsignedp
= !!unsignedp
;
611 result
.overflow
= false;
615 /* Operator precedence and flags table.
617 After an operator is returned from the lexer, if it has priority less
618 than the operator on the top of the stack, we reduce the stack by one
619 operator and repeat the test. Since equal priorities do not reduce,
620 this is naturally right-associative.
622 We handle left-associative operators by decrementing the priority of
623 just-lexed operators by one, but retaining the priority of operators
624 already on the stack.
626 The remaining cases are '(' and ')'. We handle '(' by skipping the
627 reduction phase completely. ')' is given lower priority than
628 everything else, including '(', effectively forcing a reduction of the
629 parenthesized expression. If there is a matching '(', the routine
630 reduce() exits immediately. If the normal exit route sees a ')', then
631 there cannot have been a matching '(' and an error message is output.
633 The parser assumes all shifted operators require a left operand unless
634 the flag NO_L_OPERAND is set. These semantics are automatic; any
635 extra semantics need to be handled with operator-specific code. */
637 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
638 operand changes because of integer promotions. */
639 #define NO_L_OPERAND (1 << 0)
640 #define LEFT_ASSOC (1 << 1)
641 #define CHECK_PROMOTION (1 << 2)
643 /* Operator to priority map. Must be in the same order as the first
644 N entries of enum cpp_ttype. */
645 static const struct operator
651 /* EQ */ {0, 0}, /* Shouldn't happen. */
652 /* NOT */ {16, NO_L_OPERAND
},
653 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
654 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
655 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
656 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
657 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
658 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
659 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
660 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
661 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
662 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
663 /* RSHIFT */ {13, LEFT_ASSOC
},
664 /* LSHIFT */ {13, LEFT_ASSOC
},
666 /* MIN */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
667 /* MAX */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
669 /* COMPL */ {16, NO_L_OPERAND
},
670 /* AND_AND */ {6, LEFT_ASSOC
},
671 /* OR_OR */ {5, LEFT_ASSOC
},
673 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
674 /* COMMA */ {2, LEFT_ASSOC
},
675 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
676 /* CLOSE_PAREN */ {0, 0},
678 /* EQ_EQ */ {11, LEFT_ASSOC
},
679 /* NOT_EQ */ {11, LEFT_ASSOC
},
680 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
681 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
682 /* UPLUS */ {16, NO_L_OPERAND
},
683 /* UMINUS */ {16, NO_L_OPERAND
}
686 /* Parse and evaluate a C expression, reading from PFILE.
687 Returns the truth value of the expression.
689 The implementation is an operator precedence parser, i.e. a
690 bottom-up parser, using a stack for not-yet-reduced tokens.
692 The stack base is op_stack, and the current stack pointer is 'top'.
693 There is a stack element for each operator (only), and the most
694 recently pushed operator is 'top->op'. An operand (value) is
695 stored in the 'value' field of the stack element of the operator
698 _cpp_parse_expr (pfile
)
701 struct op
*top
= pfile
->op_stack
;
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;
712 /* Lowest priority operator prevents further reductions. */
720 op
.token
= cpp_get_token (pfile
);
721 op
.op
= op
.token
->type
;
725 /* These tokens convert into values. */
732 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
733 cpp_token_as_text (pfile
, op
.token
));
735 top
->value
= eval_token (pfile
, op
.token
);
739 saw_leading_not
= lex_count
== 1;
751 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
752 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
753 cpp_token_as_text (pfile
, op
.token
));
757 /* Check we have a value or operator as appropriate. */
758 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
761 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
762 cpp_token_as_text (pfile
, op
.token
));
766 /* Ordering here is subtle and intended to favor the
767 missing parenthesis diagnostics over alternatives. */
768 if (op
.op
== CPP_CLOSE_PAREN
)
770 if (top
->op
== CPP_OPEN_PAREN
)
771 SYNTAX_ERROR ("void expression between '(' and ')'");
773 else if (top
->op
== CPP_EOF
)
774 SYNTAX_ERROR ("#if with no expression");
775 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
776 SYNTAX_ERROR2 ("operator '%s' has no right operand",
777 cpp_token_as_text (pfile
, top
->token
));
780 top
= reduce (pfile
, top
, op
.op
);
784 if (op
.op
== CPP_EOF
)
789 case CPP_CLOSE_PAREN
:
792 if (!num_zerop (top
->value
))
793 pfile
->state
.skip_eval
++;
797 if (num_zerop (top
->value
))
798 pfile
->state
.skip_eval
++;
801 if (top
->op
!= CPP_QUERY
)
802 SYNTAX_ERROR (" ':' without preceding '?'");
803 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
804 pfile
->state
.skip_eval
++;
806 pfile
->state
.skip_eval
--;
813 /* Check for and handle stack overflow. */
814 if (++top
== pfile
->op_limit
)
815 top
= _cpp_expand_op_stack (pfile
);
818 top
->token
= op
.token
;
821 /* The controlling macro expression is only valid if we called lex 3
822 times: <!> <defined expression> and <EOF>. push_conditional ()
823 checks that we are at top-of-file. */
824 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
825 pfile
->mi_ind_cmacro
= 0;
827 if (top
!= pfile
->op_stack
)
829 cpp_error (pfile
, DL_ICE
, "unbalanced stack in #if");
831 return false; /* Return false on syntax error. */
834 return !num_zerop (top
->value
);
837 /* Reduce the operator / value stack if possible, in preparation for
838 pushing operator OP. Returns NULL on error, otherwise the top of
841 reduce (pfile
, top
, op
)
848 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
851 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
855 if (op
== CPP_OPEN_PAREN
)
858 /* Decrement the priority of left-associative operators to force a
859 reduction with operators of otherwise equal priority. */
860 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
861 while (prio
< optab
[top
->op
].prio
)
863 if (CPP_OPTION (pfile
, warn_num_sign_change
)
864 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
865 check_promotion (pfile
, top
);
873 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
883 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
884 top
->value
, top
->op
);
892 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
898 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
905 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
909 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
914 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
915 top
->value
, top
->op
);
920 if (!num_zerop (top
->value
))
921 pfile
->state
.skip_eval
--;
922 top
->value
.low
= (!num_zerop (top
->value
)
923 || !num_zerop (top
[1].value
));
925 top
->value
.unsignedp
= false;
926 top
->value
.overflow
= false;
931 if (num_zerop (top
->value
))
932 pfile
->state
.skip_eval
--;
933 top
->value
.low
= (!num_zerop (top
->value
)
934 && !num_zerop (top
[1].value
));
936 top
->value
.unsignedp
= false;
937 top
->value
.overflow
= false;
941 if (op
!= CPP_CLOSE_PAREN
)
943 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
947 top
->value
= top
[1].value
;
952 if (!num_zerop (top
->value
))
954 pfile
->state
.skip_eval
--;
955 top
->value
= top
[1].value
;
958 top
->value
= top
[2].value
;
959 top
->value
.unsignedp
= (top
[1].value
.unsignedp
960 || top
[2].value
.unsignedp
);
964 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
972 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
973 cpp_error (pfile
, DL_PEDWARN
,
974 "integer overflow in preprocessor expression");
977 if (op
== CPP_CLOSE_PAREN
)
979 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
986 /* Returns the position of the old top of stack after expansion. */
988 _cpp_expand_op_stack (pfile
)
991 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
992 size_t new_size
= old_size
* 2 + 20;
994 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
995 new_size
* sizeof (struct op
));
996 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
998 return pfile
->op_stack
+ old_size
;
1001 /* Emits a warning if the effective sign of either operand of OP
1002 changes because of integer promotions. */
1004 check_promotion (pfile
, op
)
1006 const struct op
*op
;
1008 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1011 if (op
->value
.unsignedp
)
1013 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1014 cpp_error (pfile
, DL_WARNING
,
1015 "the left operand of \"%s\" changes sign when promoted",
1016 cpp_token_as_text (pfile
, op
->token
));
1018 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1019 cpp_error (pfile
, DL_WARNING
,
1020 "the right operand of \"%s\" changes sign when promoted",
1021 cpp_token_as_text (pfile
, op
->token
));
1024 /* Clears the unused high order bits of the number pointed to by PNUM. */
1026 num_trim (num
, precision
)
1030 if (precision
> PART_PRECISION
)
1032 precision
-= PART_PRECISION
;
1033 if (precision
< PART_PRECISION
)
1034 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1038 if (precision
< PART_PRECISION
)
1039 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1046 /* True iff A (presumed signed) >= 0. */
1048 num_positive (num
, precision
)
1052 if (precision
> PART_PRECISION
)
1054 precision
-= PART_PRECISION
;
1055 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1058 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1061 /* Sign extend a number, with PRECISION significant bits and all
1062 others assumed clear, to fill out a cpp_num structure. */
1064 cpp_num_sign_extend (num
, precision
)
1070 if (precision
> PART_PRECISION
)
1072 precision
-= PART_PRECISION
;
1073 if (precision
< PART_PRECISION
1074 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1075 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1077 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1079 if (precision
< PART_PRECISION
)
1080 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1081 num
.high
= ~(cpp_num_part
) 0;
1088 /* Returns the negative of NUM. */
1090 num_negate (num
, precision
)
1097 num
.high
= ~num
.high
;
1101 num
= num_trim (num
, precision
);
1102 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1107 /* Returns true if A >= B. */
1109 num_greater_eq (pa
, pb
, precision
)
1115 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1119 /* Both numbers have signed type. If they are of different
1120 sign, the answer is the sign of A. */
1121 unsignedp
= num_positive (pa
, precision
);
1123 if (unsignedp
!= num_positive (pb
, precision
))
1126 /* Otherwise we can do an unsigned comparison. */
1129 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1132 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1134 num_bitwise_op (pfile
, lhs
, rhs
, op
)
1135 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1139 lhs
.overflow
= false;
1140 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1142 /* As excess precision is zeroed, there is no need to num_trim () as
1143 these operations cannot introduce a set bit there. */
1147 lhs
.high
&= rhs
.high
;
1149 else if (op
== CPP_OR
)
1152 lhs
.high
|= rhs
.high
;
1157 lhs
.high
^= rhs
.high
;
1163 /* Returns LHS OP RHS, where OP is an inequality. */
1165 num_inequality_op (pfile
, lhs
, rhs
, op
)
1170 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1172 if (op
== CPP_GREATER_EQ
)
1174 else if (op
== CPP_LESS
)
1176 else if (op
== CPP_GREATER
)
1177 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1178 else /* CPP_LESS_EQ. */
1179 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1182 lhs
.overflow
= false;
1183 lhs
.unsignedp
= false;
1187 /* Returns LHS OP RHS, where OP is == or !=. */
1189 num_equality_op (pfile
, lhs
, rhs
, op
)
1190 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1194 /* Work around a 3.0.4 bug; see PR 6950. */
1195 bool eq
= num_eq (lhs
, rhs
);
1196 if (op
== CPP_NOT_EQ
)
1200 lhs
.overflow
= false;
1201 lhs
.unsignedp
= false;
1205 /* Shift NUM, of width PRECISION, right by N bits. */
1207 num_rshift (num
, precision
, n
)
1209 size_t precision
, n
;
1211 cpp_num_part sign_mask
;
1213 if (num
.unsignedp
|| num_positive (num
, precision
))
1216 sign_mask
= ~(cpp_num_part
) 0;
1219 num
.high
= num
.low
= sign_mask
;
1223 if (precision
< PART_PRECISION
)
1224 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1225 else if (precision
< 2 * PART_PRECISION
)
1226 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1228 if (n
>= PART_PRECISION
)
1230 n
-= PART_PRECISION
;
1232 num
.high
= sign_mask
;
1237 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1238 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1242 num
= num_trim (num
, precision
);
1243 num
.overflow
= false;
1247 /* Shift NUM, of width PRECISION, left by N bits. */
1249 num_lshift (num
, precision
, n
)
1251 size_t precision
, n
;
1255 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1256 num
.high
= num
.low
= 0;
1260 cpp_num orig
, maybe_orig
;
1264 if (m
>= PART_PRECISION
)
1266 m
-= PART_PRECISION
;
1272 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1275 num
= num_trim (num
, precision
);
1278 num
.overflow
= false;
1281 maybe_orig
= num_rshift (num
, precision
, n
);
1282 num
.overflow
= !num_eq (orig
, maybe_orig
);
1289 /* The four unary operators: +, -, ! and ~. */
1291 num_unary_op (pfile
, num
, op
)
1299 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1300 cpp_error (pfile
, DL_WARNING
,
1301 "traditional C rejects the unary plus operator");
1302 num
.overflow
= false;
1306 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1310 num
.high
= ~num
.high
;
1312 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1313 num
.overflow
= false;
1316 default: /* case CPP_NOT: */
1317 num
.low
= num_zerop (num
);
1319 num
.overflow
= false;
1320 num
.unsignedp
= false;
1327 /* The various binary operators. */
1329 num_binary_op (pfile
, lhs
, rhs
, op
)
1335 size_t precision
= CPP_OPTION (pfile
, precision
);
1344 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1346 /* A negative shift is a positive shift the other way. */
1347 if (op
== CPP_LSHIFT
)
1351 rhs
= num_negate (rhs
, precision
);
1354 n
= ~0; /* Maximal. */
1357 if (op
== CPP_LSHIFT
)
1358 lhs
= num_lshift (lhs
, precision
, n
);
1360 lhs
= num_rshift (lhs
, precision
, n
);
1367 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1369 gte
= num_greater_eq (lhs
, rhs
, precision
);
1374 lhs
.unsignedp
= unsignedp
;
1380 rhs
= num_negate (rhs
, precision
);
1382 result
.low
= lhs
.low
+ rhs
.low
;
1383 result
.high
= lhs
.high
+ rhs
.high
;
1384 if (result
.low
< lhs
.low
)
1387 result
= num_trim (result
, precision
);
1388 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1389 if (result
.unsignedp
)
1390 result
.overflow
= false;
1393 bool lhsp
= num_positive (lhs
, precision
);
1394 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1395 && lhsp
!= num_positive (result
, precision
));
1400 default: /* case CPP_COMMA: */
1401 if (CPP_PEDANTIC (pfile
) && !pfile
->state
.skip_eval
)
1402 cpp_error (pfile
, DL_PEDWARN
,
1403 "comma operator in operand of #if");
1411 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1414 num_part_mul (lhs
, rhs
)
1415 cpp_num_part lhs
, rhs
;
1418 cpp_num_part middle
[2], temp
;
1420 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1421 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1423 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1424 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1427 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1428 if (result
.low
< temp
)
1432 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1433 if (result
.low
< temp
)
1436 result
.high
+= HIGH_PART (middle
[0]);
1437 result
.high
+= HIGH_PART (middle
[1]);
1438 result
.unsignedp
= 1;
1443 /* Multiply two preprocessing numbers. */
1445 num_mul (pfile
, lhs
, rhs
)
1449 cpp_num result
, temp
;
1450 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1451 bool overflow
, negate
= false;
1452 size_t precision
= CPP_OPTION (pfile
, precision
);
1454 /* Prepare for unsigned multiplication. */
1457 if (!num_positive (lhs
, precision
))
1458 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1459 if (!num_positive (rhs
, precision
))
1460 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1463 overflow
= lhs
.high
&& rhs
.high
;
1464 result
= num_part_mul (lhs
.low
, rhs
.low
);
1466 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1467 result
.high
+= temp
.low
;
1471 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1472 result
.high
+= temp
.low
;
1476 temp
.low
= result
.low
, temp
.high
= result
.high
;
1477 result
= num_trim (result
, precision
);
1478 if (!num_eq (result
, temp
))
1482 result
= num_negate (result
, precision
);
1485 result
.overflow
= false;
1487 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1488 && !num_zerop (result
));
1489 result
.unsignedp
= unsignedp
;
1494 /* Divide two preprocessing numbers, returning the answer or the
1495 remainder depending upon OP. */
1497 num_div_op (pfile
, lhs
, rhs
, op
)
1502 cpp_num result
, sub
;
1504 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1505 bool negate
= false, lhs_neg
= false;
1506 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1508 /* Prepare for unsigned division. */
1511 if (!num_positive (lhs
, precision
))
1512 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1513 if (!num_positive (rhs
, precision
))
1514 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1517 /* Find the high bit. */
1521 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1522 for (; ; i
--, mask
>>= 1)
1523 if (rhs
.high
& mask
)
1528 if (precision
> PART_PRECISION
)
1529 i
= precision
- PART_PRECISION
- 1;
1532 mask
= (cpp_num_part
) 1 << i
;
1533 for (; ; i
--, mask
>>= 1)
1539 if (!pfile
->state
.skip_eval
)
1540 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1544 /* First nonzero bit of RHS is bit I. Do naive division by
1545 shifting the RHS fully left, and subtracting from LHS if LHS is
1546 at least as big, and then repeating but with one less shift.
1547 This is not very efficient, but is easy to understand. */
1549 rhs
.unsignedp
= true;
1550 lhs
.unsignedp
= true;
1551 i
= precision
- i
- 1;
1552 sub
= num_lshift (rhs
, precision
, i
);
1554 result
.high
= result
.low
= 0;
1557 if (num_greater_eq (lhs
, sub
, precision
))
1559 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1560 if (i
>= PART_PRECISION
)
1561 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1563 result
.low
|= (cpp_num_part
) 1 << i
;
1567 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1571 /* We divide so that the remainder has the sign of the LHS. */
1574 result
.unsignedp
= unsignedp
;
1576 result
.overflow
= false;
1580 result
= num_negate (result
, precision
);
1581 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1588 lhs
.unsignedp
= unsignedp
;
1589 lhs
.overflow
= false;
1591 lhs
= num_negate (lhs
, precision
);