]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libcpp/expr.c
1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004 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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
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))
33 const cpp_token
*token
; /* The token forming op (for diagnostics). */
34 cpp_num value
; /* The value logically "right" of op. */
38 /* Some simple utility routines on double integers. */
39 #define num_zerop(num) ((num.low | num.high) == 0)
40 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
41 static bool num_positive (cpp_num
, size_t);
42 static bool num_greater_eq (cpp_num
, cpp_num
, size_t);
43 static cpp_num
num_trim (cpp_num
, size_t);
44 static cpp_num
num_part_mul (cpp_num_part
, cpp_num_part
);
46 static cpp_num
num_unary_op (cpp_reader
*, cpp_num
, enum cpp_ttype
);
47 static cpp_num
num_binary_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
48 static cpp_num
num_negate (cpp_num
, size_t);
49 static cpp_num
num_bitwise_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
50 static cpp_num
num_inequality_op (cpp_reader
*, cpp_num
, cpp_num
,
52 static cpp_num
num_equality_op (cpp_reader
*, cpp_num
, cpp_num
,
54 static cpp_num
num_mul (cpp_reader
*, cpp_num
, cpp_num
);
55 static cpp_num
num_div_op (cpp_reader
*, cpp_num
, cpp_num
, enum cpp_ttype
);
56 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
57 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
59 static cpp_num
append_digit (cpp_num
, int, int, size_t);
60 static cpp_num
parse_defined (cpp_reader
*);
61 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*);
62 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
63 static unsigned int interpret_float_suffix (const uchar
*, size_t);
64 static unsigned int interpret_int_suffix (const uchar
*, size_t);
65 static void check_promotion (cpp_reader
*, const struct op
*);
67 /* Token type abuse to create unary plus and minus operators. */
68 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
69 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
71 /* With -O2, gcc appears to produce nice code, moving the error
72 message load and subsequent jump completely out of the main path. */
73 #define SYNTAX_ERROR(msgid) \
74 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
79 /* Subroutine of cpp_classify_number. S points to a float suffix of
80 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
81 flag vector describing the suffix. */
83 interpret_float_suffix (const uchar
*s
, size_t len
)
85 size_t f
, l
, w
, q
, i
, d
;
88 f
= l
= w
= q
= i
= d
= 0;
94 case 'r': case 'R': r
++; break;
95 case 'k': case 'K': k
++; break;
96 case 'u': case 'U': u
++; break;
97 case 'h': case 'H': h
++; break;
107 /* If there are two Ls, they must be adjacent and the same case. */
108 if (l
== 2 && s
[len
] != s
[len
+ 1])
122 case 'j': case 'J': i
++; break;
123 case 'd': case 'D': d
++; break;
128 if (r
+ k
> 1 || h
> 1 || l
> 2 || u
> 1)
133 if (f
|| i
|| d
|| w
|| q
)
137 | (u
? CPP_N_UNSIGNED
: 0)
139 l
== 2 ? CPP_N_LARGE
:
140 l
== 1 ? CPP_N_MEDIUM
: 0));
145 if (f
|| i
|| d
|| w
|| q
)
149 | (u
? CPP_N_UNSIGNED
: 0)
151 l
== 2 ? CPP_N_LARGE
:
152 l
== 1 ? CPP_N_MEDIUM
: 0));
155 if (f
+ l
+ w
+ q
> 1 || i
> 1 || h
+ u
> 0)
158 /* Allow dd, df, dl suffixes for decimal float constants. */
159 if (d
&& ((d
+ f
+ l
!= 2) || i
))
162 return ((i
? CPP_N_IMAGINARY
: 0)
166 q
? CPP_N_MD_Q
: CPP_N_MEDIUM
)
167 | (d
? CPP_N_DFLOAT
: 0));
170 /* Subroutine of cpp_classify_number. S points to an integer suffix
171 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
172 flag vector describing the suffix. */
174 interpret_int_suffix (const uchar
*s
, size_t len
)
183 case 'u': case 'U': u
++; break;
185 case 'j': case 'J': i
++; break;
186 case 'l': case 'L': l
++;
187 /* If there are two Ls, they must be adjacent and the same case. */
188 if (l
== 2 && s
[len
] != s
[len
+ 1])
195 if (l
> 2 || u
> 1 || i
> 1)
198 return ((i
? CPP_N_IMAGINARY
: 0)
199 | (u
? CPP_N_UNSIGNED
: 0)
200 | ((l
== 0) ? CPP_N_SMALL
201 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
204 /* Categorize numeric constants according to their field (integer,
205 floating point, or invalid), radix (decimal, octal, hexadecimal),
206 and type suffixes. */
208 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
210 const uchar
*str
= token
->val
.str
.text
;
212 unsigned int max_digit
, result
, radix
;
213 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
215 /* If the lexer has done its job, length one can only be a single
216 digit. Fast-path this very common case. */
217 if (token
->val
.str
.len
== 1)
218 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
220 limit
= str
+ token
->val
.str
.len
;
221 float_flag
= NOT_FLOAT
;
225 /* First, interpret the radix. */
231 /* Require at least one hex digit to classify it as hex. */
232 if ((*str
== 'x' || *str
== 'X')
233 && (str
[1] == '.' || ISXDIGIT (str
[1])))
238 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
245 /* Now scan for a well-formed integer or float. */
248 unsigned int c
= *str
++;
250 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
258 if (float_flag
== NOT_FLOAT
)
259 float_flag
= AFTER_POINT
;
261 SYNTAX_ERROR ("too many decimal points in number");
263 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
264 || (radix
== 16 && (c
== 'p' || c
== 'P')))
266 float_flag
= AFTER_EXPON
;
271 /* Start of suffix. */
277 /* The suffix may be for decimal fixed-point constants without exponent. */
278 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
280 result
= interpret_float_suffix (str
, limit
- str
);
281 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
283 result
|= CPP_N_FLOATING
;
284 /* We need to restore the radix to 10, if the radix is 8. */
288 if (CPP_PEDANTIC (pfile
))
289 cpp_error (pfile
, CPP_DL_PEDWARN
,
290 "fixed-point constants are a GCC extension");
297 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
300 if (max_digit
>= radix
)
303 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit
);
305 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
308 if (float_flag
!= NOT_FLOAT
)
312 cpp_error (pfile
, CPP_DL_ERROR
,
313 "invalid prefix \"0b\" for floating constant");
314 return CPP_N_INVALID
;
317 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
318 cpp_error (pfile
, CPP_DL_PEDWARN
,
319 "use of C99 hexadecimal floating constant");
321 if (float_flag
== AFTER_EXPON
)
323 if (*str
== '+' || *str
== '-')
326 /* Exponent is decimal, even if string is a hex float. */
328 SYNTAX_ERROR ("exponent has no digits");
332 while (ISDIGIT (*str
));
334 else if (radix
== 16)
335 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
337 result
= interpret_float_suffix (str
, limit
- str
);
340 cpp_error (pfile
, CPP_DL_ERROR
,
341 "invalid suffix \"%.*s\" on floating constant",
342 (int) (limit
- str
), str
);
343 return CPP_N_INVALID
;
346 /* Traditional C didn't accept any floating suffixes. */
348 && CPP_WTRADITIONAL (pfile
)
349 && ! cpp_sys_macro_p (pfile
))
350 cpp_error (pfile
, CPP_DL_WARNING
,
351 "traditional C rejects the \"%.*s\" suffix",
352 (int) (limit
- str
), str
);
354 /* Radix must be 10 for decimal floats. */
355 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
357 cpp_error (pfile
, CPP_DL_ERROR
,
358 "invalid suffix \"%.*s\" with hexadecimal floating constant",
359 (int) (limit
- str
), str
);
360 return CPP_N_INVALID
;
363 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
364 cpp_error (pfile
, CPP_DL_PEDWARN
,
365 "fixed-point constants are a GCC extension");
367 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
368 cpp_error (pfile
, CPP_DL_PEDWARN
,
369 "decimal float constants are a GCC extension");
371 result
|= CPP_N_FLOATING
;
375 result
= interpret_int_suffix (str
, limit
- str
);
378 cpp_error (pfile
, CPP_DL_ERROR
,
379 "invalid suffix \"%.*s\" on integer constant",
380 (int) (limit
- str
), str
);
381 return CPP_N_INVALID
;
384 /* Traditional C only accepted the 'L' suffix.
385 Suppress warning about 'LL' with -Wno-long-long. */
386 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
388 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
389 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
391 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
392 cpp_error (pfile
, CPP_DL_WARNING
,
393 "traditional C rejects the \"%.*s\" suffix",
394 (int) (limit
- str
), str
);
397 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
398 && ! CPP_OPTION (pfile
, c99
)
399 && CPP_OPTION (pfile
, warn_long_long
))
400 cpp_error (pfile
, CPP_DL_PEDWARN
,
401 "use of C99 long long integer constant");
403 result
|= CPP_N_INTEGER
;
407 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
408 cpp_error (pfile
, CPP_DL_PEDWARN
,
409 "imaginary constants are a GCC extension");
410 if (radix
== 2 && CPP_PEDANTIC (pfile
))
411 cpp_error (pfile
, CPP_DL_PEDWARN
,
412 "binary constants are a GCC extension");
415 result
|= CPP_N_DECIMAL
;
416 else if (radix
== 16)
419 result
|= CPP_N_BINARY
;
421 result
|= CPP_N_OCTAL
;
426 return CPP_N_INVALID
;
429 /* cpp_interpret_integer converts an integer constant into a cpp_num,
430 of precision options->precision.
432 We do not provide any interface for decimal->float conversion,
433 because the preprocessor doesn't need it and we don't want to
434 drag in GCC's floating point emulator. */
436 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
439 const uchar
*p
, *end
;
444 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
445 result
.overflow
= false;
447 p
= token
->val
.str
.text
;
448 end
= p
+ token
->val
.str
.len
;
450 /* Common case of a single digit. */
451 if (token
->val
.str
.len
== 1)
452 result
.low
= p
[0] - '0';
456 size_t precision
= CPP_OPTION (pfile
, precision
);
457 unsigned int base
= 10, c
= 0;
458 bool overflow
= false;
460 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
465 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
470 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
476 /* We can add a digit to numbers strictly less than this without
477 needing the precision and slowness of double integers. */
478 max
= ~(cpp_num_part
) 0;
479 if (precision
< PART_PRECISION
)
480 max
>>= PART_PRECISION
- precision
;
481 max
= (max
- base
+ 1) / base
+ 1;
487 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
492 /* Strict inequality for when max is set to zero. */
493 if (result
.low
< max
)
494 result
.low
= result
.low
* base
+ c
;
497 result
= append_digit (result
, c
, base
, precision
);
498 overflow
|= result
.overflow
;
504 cpp_error (pfile
, CPP_DL_PEDWARN
,
505 "integer constant is too large for its type");
506 /* If too big to be signed, consider it unsigned. Only warn for
507 decimal numbers. Traditional numbers were always signed (but
508 we still honor an explicit U suffix); but we only have
509 traditional semantics in directives. */
510 else if (!result
.unsignedp
511 && !(CPP_OPTION (pfile
, traditional
)
512 && pfile
->state
.in_directive
)
513 && !num_positive (result
, precision
))
516 cpp_error (pfile
, CPP_DL_WARNING
,
517 "integer constant is so large that it is unsigned");
518 result
.unsignedp
= true;
525 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
527 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
532 cpp_num_part add_high
, add_low
;
534 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
535 need to worry about add_high overflowing. */
549 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
550 result
.high
= num
.high
<< shift
;
551 result
.low
= num
.low
<< shift
;
552 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
553 result
.unsignedp
= num
.unsignedp
;
557 add_low
= num
.low
<< 1;
558 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
561 add_high
= add_low
= 0;
563 if (add_low
+ digit
< add_low
)
567 if (result
.low
+ add_low
< result
.low
)
569 if (result
.high
+ add_high
< result
.high
)
572 result
.low
+= add_low
;
573 result
.high
+= add_high
;
574 result
.overflow
= overflow
;
576 /* The above code catches overflow of a cpp_num type. This catches
577 overflow of the (possibly shorter) target precision. */
578 num
.low
= result
.low
;
579 num
.high
= result
.high
;
580 result
= num_trim (result
, precision
);
581 if (!num_eq (result
, num
))
582 result
.overflow
= true;
587 /* Handle meeting "defined" in a preprocessor expression. */
589 parse_defined (cpp_reader
*pfile
)
593 cpp_hashnode
*node
= 0;
594 const cpp_token
*token
;
595 cpp_context
*initial_context
= pfile
->context
;
597 /* Don't expand macros. */
598 pfile
->state
.prevent_expansion
++;
600 token
= cpp_get_token (pfile
);
601 if (token
->type
== CPP_OPEN_PAREN
)
604 token
= cpp_get_token (pfile
);
607 if (token
->type
== CPP_NAME
)
609 node
= token
->val
.node
;
610 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
612 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
618 cpp_error (pfile
, CPP_DL_ERROR
,
619 "operator \"defined\" requires an identifier");
620 if (token
->flags
& NAMED_OP
)
625 op
.type
= token
->type
;
626 cpp_error (pfile
, CPP_DL_ERROR
,
627 "(\"%s\" is an alternative token for \"%s\" in C++)",
628 cpp_token_as_text (pfile
, token
),
629 cpp_token_as_text (pfile
, &op
));
635 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
636 cpp_error (pfile
, CPP_DL_WARNING
,
637 "this use of \"defined\" may not be portable");
639 _cpp_mark_macro_used (node
);
640 if (!(node
->flags
& NODE_USED
))
642 node
->flags
|= NODE_USED
;
643 if (node
->type
== NT_MACRO
)
645 if (pfile
->cb
.used_define
)
646 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
650 if (pfile
->cb
.used_undef
)
651 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
655 /* A possible controlling macro of the form #if !defined ().
656 _cpp_parse_expr checks there was no other junk on the line. */
657 pfile
->mi_ind_cmacro
= node
;
660 pfile
->state
.prevent_expansion
--;
662 result
.unsignedp
= false;
664 result
.overflow
= false;
665 result
.low
= node
&& node
->type
== NT_MACRO
;
669 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
670 number or character constant, or the result of the "defined" or "#"
673 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
679 result
.unsignedp
= false;
680 result
.overflow
= false;
685 temp
= cpp_classify_number (pfile
, token
);
686 switch (temp
& CPP_N_CATEGORY
)
689 cpp_error (pfile
, CPP_DL_ERROR
,
690 "floating constant in preprocessor expression");
693 if (!(temp
& CPP_N_IMAGINARY
))
694 return cpp_interpret_integer (pfile
, token
, temp
);
695 cpp_error (pfile
, CPP_DL_ERROR
,
696 "imaginary number in preprocessor expression");
700 /* Error already issued. */
703 result
.high
= result
.low
= 0;
709 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
714 /* Sign-extend the result if necessary. */
715 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
717 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
718 result
.low
|= ~(~(cpp_num_part
) 0
719 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
720 result
.high
= ~(cpp_num_part
) 0;
721 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
727 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
728 return parse_defined (pfile
);
729 else if (CPP_OPTION (pfile
, cplusplus
)
730 && (token
->val
.node
== pfile
->spec_nodes
.n_true
731 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
734 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
740 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
741 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
742 NODE_NAME (token
->val
.node
));
746 default: /* CPP_HASH */
747 _cpp_test_assertion (pfile
, &temp
);
752 result
.unsignedp
= !!unsignedp
;
756 /* Operator precedence and flags table.
758 After an operator is returned from the lexer, if it has priority less
759 than the operator on the top of the stack, we reduce the stack by one
760 operator and repeat the test. Since equal priorities do not reduce,
761 this is naturally right-associative.
763 We handle left-associative operators by decrementing the priority of
764 just-lexed operators by one, but retaining the priority of operators
765 already on the stack.
767 The remaining cases are '(' and ')'. We handle '(' by skipping the
768 reduction phase completely. ')' is given lower priority than
769 everything else, including '(', effectively forcing a reduction of the
770 parenthesized expression. If there is a matching '(', the routine
771 reduce() exits immediately. If the normal exit route sees a ')', then
772 there cannot have been a matching '(' and an error message is output.
774 The parser assumes all shifted operators require a left operand unless
775 the flag NO_L_OPERAND is set. These semantics are automatic; any
776 extra semantics need to be handled with operator-specific code. */
778 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
779 operand changes because of integer promotions. */
780 #define NO_L_OPERAND (1 << 0)
781 #define LEFT_ASSOC (1 << 1)
782 #define CHECK_PROMOTION (1 << 2)
784 /* Operator to priority map. Must be in the same order as the first
785 N entries of enum cpp_ttype. */
786 static const struct cpp_operator
792 /* EQ */ {0, 0}, /* Shouldn't happen. */
793 /* NOT */ {16, NO_L_OPERAND
},
794 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
795 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
796 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
797 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
798 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
799 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
800 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
801 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
802 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
803 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
804 /* RSHIFT */ {13, LEFT_ASSOC
},
805 /* LSHIFT */ {13, LEFT_ASSOC
},
807 /* COMPL */ {16, NO_L_OPERAND
},
808 /* AND_AND */ {6, LEFT_ASSOC
},
809 /* OR_OR */ {5, LEFT_ASSOC
},
811 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
812 /* COMMA */ {2, LEFT_ASSOC
},
813 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
814 /* CLOSE_PAREN */ {0, 0},
816 /* EQ_EQ */ {11, LEFT_ASSOC
},
817 /* NOT_EQ */ {11, LEFT_ASSOC
},
818 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
819 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
820 /* UPLUS */ {16, NO_L_OPERAND
},
821 /* UMINUS */ {16, NO_L_OPERAND
}
824 /* Parse and evaluate a C expression, reading from PFILE.
825 Returns the truth value of the expression.
827 The implementation is an operator precedence parser, i.e. a
828 bottom-up parser, using a stack for not-yet-reduced tokens.
830 The stack base is op_stack, and the current stack pointer is 'top'.
831 There is a stack element for each operator (only), and the most
832 recently pushed operator is 'top->op'. An operand (value) is
833 stored in the 'value' field of the stack element of the operator
836 _cpp_parse_expr (cpp_reader
*pfile
)
838 struct op
*top
= pfile
->op_stack
;
839 unsigned int lex_count
;
840 bool saw_leading_not
, want_value
= true;
842 pfile
->state
.skip_eval
= 0;
844 /* Set up detection of #if ! defined(). */
845 pfile
->mi_ind_cmacro
= 0;
846 saw_leading_not
= false;
849 /* Lowest priority operator prevents further reductions. */
857 op
.token
= cpp_get_token (pfile
);
858 op
.op
= op
.token
->type
;
862 /* These tokens convert into values. */
869 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
870 cpp_token_as_text (pfile
, op
.token
));
872 top
->value
= eval_token (pfile
, op
.token
);
876 saw_leading_not
= lex_count
== 1;
888 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
889 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
890 cpp_token_as_text (pfile
, op
.token
));
894 /* Check we have a value or operator as appropriate. */
895 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
898 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
899 cpp_token_as_text (pfile
, op
.token
));
903 /* We want a number (or expression) and haven't got one.
904 Try to emit a specific diagnostic. */
905 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
906 SYNTAX_ERROR ("missing expression between '(' and ')'");
908 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
909 SYNTAX_ERROR ("#if with no expression");
911 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
912 SYNTAX_ERROR2 ("operator '%s' has no right operand",
913 cpp_token_as_text (pfile
, top
->token
));
914 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
915 /* Complain about missing paren during reduction. */;
917 SYNTAX_ERROR2 ("operator '%s' has no left operand",
918 cpp_token_as_text (pfile
, op
.token
));
921 top
= reduce (pfile
, top
, op
.op
);
925 if (op
.op
== CPP_EOF
)
930 case CPP_CLOSE_PAREN
:
933 if (!num_zerop (top
->value
))
934 pfile
->state
.skip_eval
++;
938 if (num_zerop (top
->value
))
939 pfile
->state
.skip_eval
++;
942 if (top
->op
!= CPP_QUERY
)
943 SYNTAX_ERROR (" ':' without preceding '?'");
944 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
945 pfile
->state
.skip_eval
++;
947 pfile
->state
.skip_eval
--;
954 /* Check for and handle stack overflow. */
955 if (++top
== pfile
->op_limit
)
956 top
= _cpp_expand_op_stack (pfile
);
959 top
->token
= op
.token
;
962 /* The controlling macro expression is only valid if we called lex 3
963 times: <!> <defined expression> and <EOF>. push_conditional ()
964 checks that we are at top-of-file. */
965 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
966 pfile
->mi_ind_cmacro
= 0;
968 if (top
!= pfile
->op_stack
)
970 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in #if");
972 return false; /* Return false on syntax error. */
975 return !num_zerop (top
->value
);
978 /* Reduce the operator / value stack if possible, in preparation for
979 pushing operator OP. Returns NULL on error, otherwise the top of
982 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
986 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
989 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
993 if (op
== CPP_OPEN_PAREN
)
996 /* Decrement the priority of left-associative operators to force a
997 reduction with operators of otherwise equal priority. */
998 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
999 while (prio
< optab
[top
->op
].prio
)
1001 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1002 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1003 check_promotion (pfile
, top
);
1011 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1019 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1020 top
->value
, top
->op
);
1025 case CPP_GREATER_EQ
:
1028 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1034 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1041 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1045 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1050 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1051 top
->value
, top
->op
);
1056 if (!num_zerop (top
->value
))
1057 pfile
->state
.skip_eval
--;
1058 top
->value
.low
= (!num_zerop (top
->value
)
1059 || !num_zerop (top
[1].value
));
1060 top
->value
.high
= 0;
1061 top
->value
.unsignedp
= false;
1062 top
->value
.overflow
= false;
1067 if (num_zerop (top
->value
))
1068 pfile
->state
.skip_eval
--;
1069 top
->value
.low
= (!num_zerop (top
->value
)
1070 && !num_zerop (top
[1].value
));
1071 top
->value
.high
= 0;
1072 top
->value
.unsignedp
= false;
1073 top
->value
.overflow
= false;
1076 case CPP_OPEN_PAREN
:
1077 if (op
!= CPP_CLOSE_PAREN
)
1079 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in expression");
1083 top
->value
= top
[1].value
;
1088 if (!num_zerop (top
->value
))
1090 pfile
->state
.skip_eval
--;
1091 top
->value
= top
[1].value
;
1094 top
->value
= top
[2].value
;
1095 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1096 || top
[2].value
.unsignedp
);
1100 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1108 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1109 cpp_error (pfile
, CPP_DL_PEDWARN
,
1110 "integer overflow in preprocessor expression");
1113 if (op
== CPP_CLOSE_PAREN
)
1115 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1122 /* Returns the position of the old top of stack after expansion. */
1124 _cpp_expand_op_stack (cpp_reader
*pfile
)
1126 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1127 size_t new_size
= old_size
* 2 + 20;
1129 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1130 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1132 return pfile
->op_stack
+ old_size
;
1135 /* Emits a warning if the effective sign of either operand of OP
1136 changes because of integer promotions. */
1138 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1140 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1143 if (op
->value
.unsignedp
)
1145 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1146 cpp_error (pfile
, CPP_DL_WARNING
,
1147 "the left operand of \"%s\" changes sign when promoted",
1148 cpp_token_as_text (pfile
, op
->token
));
1150 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1151 cpp_error (pfile
, CPP_DL_WARNING
,
1152 "the right operand of \"%s\" changes sign when promoted",
1153 cpp_token_as_text (pfile
, op
->token
));
1156 /* Clears the unused high order bits of the number pointed to by PNUM. */
1158 num_trim (cpp_num num
, size_t precision
)
1160 if (precision
> PART_PRECISION
)
1162 precision
-= PART_PRECISION
;
1163 if (precision
< PART_PRECISION
)
1164 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1168 if (precision
< PART_PRECISION
)
1169 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1176 /* True iff A (presumed signed) >= 0. */
1178 num_positive (cpp_num num
, size_t precision
)
1180 if (precision
> PART_PRECISION
)
1182 precision
-= PART_PRECISION
;
1183 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1186 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1189 /* Sign extend a number, with PRECISION significant bits and all
1190 others assumed clear, to fill out a cpp_num structure. */
1192 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1196 if (precision
> PART_PRECISION
)
1198 precision
-= PART_PRECISION
;
1199 if (precision
< PART_PRECISION
1200 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1201 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1203 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1205 if (precision
< PART_PRECISION
)
1206 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1207 num
.high
= ~(cpp_num_part
) 0;
1214 /* Returns the negative of NUM. */
1216 num_negate (cpp_num num
, size_t precision
)
1221 num
.high
= ~num
.high
;
1225 num
= num_trim (num
, precision
);
1226 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1231 /* Returns true if A >= B. */
1233 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1237 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1241 /* Both numbers have signed type. If they are of different
1242 sign, the answer is the sign of A. */
1243 unsignedp
= num_positive (pa
, precision
);
1245 if (unsignedp
!= num_positive (pb
, precision
))
1248 /* Otherwise we can do an unsigned comparison. */
1251 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1254 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1256 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1257 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1259 lhs
.overflow
= false;
1260 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1262 /* As excess precision is zeroed, there is no need to num_trim () as
1263 these operations cannot introduce a set bit there. */
1267 lhs
.high
&= rhs
.high
;
1269 else if (op
== CPP_OR
)
1272 lhs
.high
|= rhs
.high
;
1277 lhs
.high
^= rhs
.high
;
1283 /* Returns LHS OP RHS, where OP is an inequality. */
1285 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1288 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1290 if (op
== CPP_GREATER_EQ
)
1292 else if (op
== CPP_LESS
)
1294 else if (op
== CPP_GREATER
)
1295 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1296 else /* CPP_LESS_EQ. */
1297 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1300 lhs
.overflow
= false;
1301 lhs
.unsignedp
= false;
1305 /* Returns LHS OP RHS, where OP is == or !=. */
1307 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1308 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1310 /* Work around a 3.0.4 bug; see PR 6950. */
1311 bool eq
= num_eq (lhs
, rhs
);
1312 if (op
== CPP_NOT_EQ
)
1316 lhs
.overflow
= false;
1317 lhs
.unsignedp
= false;
1321 /* Shift NUM, of width PRECISION, right by N bits. */
1323 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1325 cpp_num_part sign_mask
;
1326 bool x
= num_positive (num
, precision
);
1328 if (num
.unsignedp
|| x
)
1331 sign_mask
= ~(cpp_num_part
) 0;
1334 num
.high
= num
.low
= sign_mask
;
1338 if (precision
< PART_PRECISION
)
1339 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1340 else if (precision
< 2 * PART_PRECISION
)
1341 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1343 if (n
>= PART_PRECISION
)
1345 n
-= PART_PRECISION
;
1347 num
.high
= sign_mask
;
1352 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1353 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1357 num
= num_trim (num
, precision
);
1358 num
.overflow
= false;
1362 /* Shift NUM, of width PRECISION, left by N bits. */
1364 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1368 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1369 num
.high
= num
.low
= 0;
1373 cpp_num orig
, maybe_orig
;
1377 if (m
>= PART_PRECISION
)
1379 m
-= PART_PRECISION
;
1385 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1388 num
= num_trim (num
, precision
);
1391 num
.overflow
= false;
1394 maybe_orig
= num_rshift (num
, precision
, n
);
1395 num
.overflow
= !num_eq (orig
, maybe_orig
);
1402 /* The four unary operators: +, -, ! and ~. */
1404 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1409 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1410 cpp_error (pfile
, CPP_DL_WARNING
,
1411 "traditional C rejects the unary plus operator");
1412 num
.overflow
= false;
1416 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1420 num
.high
= ~num
.high
;
1422 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1423 num
.overflow
= false;
1426 default: /* case CPP_NOT: */
1427 num
.low
= num_zerop (num
);
1429 num
.overflow
= false;
1430 num
.unsignedp
= false;
1437 /* The various binary operators. */
1439 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1442 size_t precision
= CPP_OPTION (pfile
, precision
);
1450 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1452 /* A negative shift is a positive shift the other way. */
1453 if (op
== CPP_LSHIFT
)
1457 rhs
= num_negate (rhs
, precision
);
1460 n
= ~0; /* Maximal. */
1463 if (op
== CPP_LSHIFT
)
1464 lhs
= num_lshift (lhs
, precision
, n
);
1466 lhs
= num_rshift (lhs
, precision
, n
);
1471 rhs
= num_negate (rhs
, precision
);
1473 result
.low
= lhs
.low
+ rhs
.low
;
1474 result
.high
= lhs
.high
+ rhs
.high
;
1475 if (result
.low
< lhs
.low
)
1477 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1478 result
.overflow
= false;
1480 result
= num_trim (result
, precision
);
1481 if (!result
.unsignedp
)
1483 bool lhsp
= num_positive (lhs
, precision
);
1484 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1485 && lhsp
!= num_positive (result
, precision
));
1490 default: /* case CPP_COMMA: */
1491 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1492 || !pfile
->state
.skip_eval
))
1493 cpp_error (pfile
, CPP_DL_PEDWARN
,
1494 "comma operator in operand of #if");
1502 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1505 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1508 cpp_num_part middle
[2], temp
;
1510 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1511 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1513 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1514 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1517 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1518 if (result
.low
< temp
)
1522 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1523 if (result
.low
< temp
)
1526 result
.high
+= HIGH_PART (middle
[0]);
1527 result
.high
+= HIGH_PART (middle
[1]);
1528 result
.unsignedp
= true;
1529 result
.overflow
= false;
1534 /* Multiply two preprocessing numbers. */
1536 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1538 cpp_num result
, temp
;
1539 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1540 bool overflow
, negate
= false;
1541 size_t precision
= CPP_OPTION (pfile
, precision
);
1543 /* Prepare for unsigned multiplication. */
1546 if (!num_positive (lhs
, precision
))
1547 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1548 if (!num_positive (rhs
, precision
))
1549 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1552 overflow
= lhs
.high
&& rhs
.high
;
1553 result
= num_part_mul (lhs
.low
, rhs
.low
);
1555 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1556 result
.high
+= temp
.low
;
1560 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1561 result
.high
+= temp
.low
;
1565 temp
.low
= result
.low
, temp
.high
= result
.high
;
1566 result
= num_trim (result
, precision
);
1567 if (!num_eq (result
, temp
))
1571 result
= num_negate (result
, precision
);
1574 result
.overflow
= false;
1576 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1577 && !num_zerop (result
));
1578 result
.unsignedp
= unsignedp
;
1583 /* Divide two preprocessing numbers, returning the answer or the
1584 remainder depending upon OP. */
1586 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1588 cpp_num result
, sub
;
1590 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1591 bool negate
= false, lhs_neg
= false;
1592 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1594 /* Prepare for unsigned division. */
1597 if (!num_positive (lhs
, precision
))
1598 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1599 if (!num_positive (rhs
, precision
))
1600 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1603 /* Find the high bit. */
1607 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1608 for (; ; i
--, mask
>>= 1)
1609 if (rhs
.high
& mask
)
1614 if (precision
> PART_PRECISION
)
1615 i
= precision
- PART_PRECISION
- 1;
1618 mask
= (cpp_num_part
) 1 << i
;
1619 for (; ; i
--, mask
>>= 1)
1625 if (!pfile
->state
.skip_eval
)
1626 cpp_error (pfile
, CPP_DL_ERROR
, "division by zero in #if");
1630 /* First nonzero bit of RHS is bit I. Do naive division by
1631 shifting the RHS fully left, and subtracting from LHS if LHS is
1632 at least as big, and then repeating but with one less shift.
1633 This is not very efficient, but is easy to understand. */
1635 rhs
.unsignedp
= true;
1636 lhs
.unsignedp
= true;
1637 i
= precision
- i
- 1;
1638 sub
= num_lshift (rhs
, precision
, i
);
1640 result
.high
= result
.low
= 0;
1643 if (num_greater_eq (lhs
, sub
, precision
))
1645 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1646 if (i
>= PART_PRECISION
)
1647 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1649 result
.low
|= (cpp_num_part
) 1 << i
;
1653 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1657 /* We divide so that the remainder has the sign of the LHS. */
1660 result
.unsignedp
= unsignedp
;
1661 result
.overflow
= false;
1665 result
= num_negate (result
, precision
);
1666 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1667 && !num_zerop (result
));
1674 lhs
.unsignedp
= unsignedp
;
1675 lhs
.overflow
= false;
1677 lhs
= num_negate (lhs
, precision
);