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
;
87 f
= l
= w
= q
= i
= d
= 0;
113 case 'j': case 'J': i
++; break;
114 case 'd': case 'D': d
++; break;
119 if (f
+ l
+ w
+ q
> 1 || i
> 1)
122 /* Allow dd, df, dl suffixes for decimal float constants. */
123 if (d
&& ((d
+ f
+ l
!= 2) || i
))
126 return ((i
? CPP_N_IMAGINARY
: 0)
130 q
? CPP_N_MD_Q
: CPP_N_MEDIUM
)
131 | (d
? CPP_N_DFLOAT
: 0));
134 /* Subroutine of cpp_classify_number. S points to an integer suffix
135 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
136 flag vector describing the suffix. */
138 interpret_int_suffix (const uchar
*s
, size_t len
)
147 case 'u': case 'U': u
++; break;
149 case 'j': case 'J': i
++; break;
150 case 'l': case 'L': l
++;
151 /* If there are two Ls, they must be adjacent and the same case. */
152 if (l
== 2 && s
[len
] != s
[len
+ 1])
159 if (l
> 2 || u
> 1 || i
> 1)
162 return ((i
? CPP_N_IMAGINARY
: 0)
163 | (u
? CPP_N_UNSIGNED
: 0)
164 | ((l
== 0) ? CPP_N_SMALL
165 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
168 /* Categorize numeric constants according to their field (integer,
169 floating point, or invalid), radix (decimal, octal, hexadecimal),
170 and type suffixes. */
172 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
174 const uchar
*str
= token
->val
.str
.text
;
176 unsigned int max_digit
, result
, radix
;
177 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
179 /* If the lexer has done its job, length one can only be a single
180 digit. Fast-path this very common case. */
181 if (token
->val
.str
.len
== 1)
182 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
184 limit
= str
+ token
->val
.str
.len
;
185 float_flag
= NOT_FLOAT
;
189 /* First, interpret the radix. */
195 /* Require at least one hex digit to classify it as hex. */
196 if ((*str
== 'x' || *str
== 'X')
197 && (str
[1] == '.' || ISXDIGIT (str
[1])))
202 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
209 /* Now scan for a well-formed integer or float. */
212 unsigned int c
= *str
++;
214 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
222 if (float_flag
== NOT_FLOAT
)
223 float_flag
= AFTER_POINT
;
225 SYNTAX_ERROR ("too many decimal points in number");
227 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
228 || (radix
== 16 && (c
== 'p' || c
== 'P')))
230 float_flag
= AFTER_EXPON
;
235 /* Start of suffix. */
241 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
244 if (max_digit
>= radix
)
247 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit
);
249 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
252 if (float_flag
!= NOT_FLOAT
)
256 cpp_error (pfile
, CPP_DL_ERROR
,
257 "invalid prefix \"0b\" for floating constant");
258 return CPP_N_INVALID
;
261 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
262 cpp_error (pfile
, CPP_DL_PEDWARN
,
263 "use of C99 hexadecimal floating constant");
265 if (float_flag
== AFTER_EXPON
)
267 if (*str
== '+' || *str
== '-')
270 /* Exponent is decimal, even if string is a hex float. */
272 SYNTAX_ERROR ("exponent has no digits");
276 while (ISDIGIT (*str
));
278 else if (radix
== 16)
279 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
281 result
= interpret_float_suffix (str
, limit
- str
);
284 cpp_error (pfile
, CPP_DL_ERROR
,
285 "invalid suffix \"%.*s\" on floating constant",
286 (int) (limit
- str
), str
);
287 return CPP_N_INVALID
;
290 /* Traditional C didn't accept any floating suffixes. */
292 && CPP_WTRADITIONAL (pfile
)
293 && ! cpp_sys_macro_p (pfile
))
294 cpp_error (pfile
, CPP_DL_WARNING
,
295 "traditional C rejects the \"%.*s\" suffix",
296 (int) (limit
- str
), str
);
298 /* Radix must be 10 for decimal floats. */
299 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
301 cpp_error (pfile
, CPP_DL_ERROR
,
302 "invalid suffix \"%.*s\" with hexadecimal floating constant",
303 (int) (limit
- str
), str
);
304 return CPP_N_INVALID
;
307 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
308 cpp_error (pfile
, CPP_DL_PEDWARN
,
309 "decimal float constants are a GCC extension");
311 result
|= CPP_N_FLOATING
;
315 result
= interpret_int_suffix (str
, limit
- str
);
318 cpp_error (pfile
, CPP_DL_ERROR
,
319 "invalid suffix \"%.*s\" on integer constant",
320 (int) (limit
- str
), str
);
321 return CPP_N_INVALID
;
324 /* Traditional C only accepted the 'L' suffix.
325 Suppress warning about 'LL' with -Wno-long-long. */
326 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
328 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
329 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
331 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
332 cpp_error (pfile
, CPP_DL_WARNING
,
333 "traditional C rejects the \"%.*s\" suffix",
334 (int) (limit
- str
), str
);
337 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
338 && ! CPP_OPTION (pfile
, c99
)
339 && CPP_OPTION (pfile
, warn_long_long
))
340 cpp_error (pfile
, CPP_DL_PEDWARN
,
341 "use of C99 long long integer constant");
343 result
|= CPP_N_INTEGER
;
346 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
347 cpp_error (pfile
, CPP_DL_PEDWARN
,
348 "imaginary constants are a GCC extension");
349 if (radix
== 2 && CPP_PEDANTIC (pfile
))
350 cpp_error (pfile
, CPP_DL_PEDWARN
,
351 "binary constants are a GCC extension");
354 result
|= CPP_N_DECIMAL
;
355 else if (radix
== 16)
358 result
|= CPP_N_BINARY
;
360 result
|= CPP_N_OCTAL
;
365 return CPP_N_INVALID
;
368 /* cpp_interpret_integer converts an integer constant into a cpp_num,
369 of precision options->precision.
371 We do not provide any interface for decimal->float conversion,
372 because the preprocessor doesn't need it and we don't want to
373 drag in GCC's floating point emulator. */
375 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
378 const uchar
*p
, *end
;
383 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
384 result
.overflow
= false;
386 p
= token
->val
.str
.text
;
387 end
= p
+ token
->val
.str
.len
;
389 /* Common case of a single digit. */
390 if (token
->val
.str
.len
== 1)
391 result
.low
= p
[0] - '0';
395 size_t precision
= CPP_OPTION (pfile
, precision
);
396 unsigned int base
= 10, c
= 0;
397 bool overflow
= false;
399 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
404 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
409 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
415 /* We can add a digit to numbers strictly less than this without
416 needing the precision and slowness of double integers. */
417 max
= ~(cpp_num_part
) 0;
418 if (precision
< PART_PRECISION
)
419 max
>>= PART_PRECISION
- precision
;
420 max
= (max
- base
+ 1) / base
+ 1;
426 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
431 /* Strict inequality for when max is set to zero. */
432 if (result
.low
< max
)
433 result
.low
= result
.low
* base
+ c
;
436 result
= append_digit (result
, c
, base
, precision
);
437 overflow
|= result
.overflow
;
443 cpp_error (pfile
, CPP_DL_PEDWARN
,
444 "integer constant is too large for its type");
445 /* If too big to be signed, consider it unsigned. Only warn for
446 decimal numbers. Traditional numbers were always signed (but
447 we still honor an explicit U suffix); but we only have
448 traditional semantics in directives. */
449 else if (!result
.unsignedp
450 && !(CPP_OPTION (pfile
, traditional
)
451 && pfile
->state
.in_directive
)
452 && !num_positive (result
, precision
))
455 cpp_error (pfile
, CPP_DL_WARNING
,
456 "integer constant is so large that it is unsigned");
457 result
.unsignedp
= true;
464 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
466 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
471 cpp_num_part add_high
, add_low
;
473 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
474 need to worry about add_high overflowing. */
488 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
489 result
.high
= num
.high
<< shift
;
490 result
.low
= num
.low
<< shift
;
491 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
492 result
.unsignedp
= num
.unsignedp
;
496 add_low
= num
.low
<< 1;
497 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
500 add_high
= add_low
= 0;
502 if (add_low
+ digit
< add_low
)
506 if (result
.low
+ add_low
< result
.low
)
508 if (result
.high
+ add_high
< result
.high
)
511 result
.low
+= add_low
;
512 result
.high
+= add_high
;
513 result
.overflow
= overflow
;
515 /* The above code catches overflow of a cpp_num type. This catches
516 overflow of the (possibly shorter) target precision. */
517 num
.low
= result
.low
;
518 num
.high
= result
.high
;
519 result
= num_trim (result
, precision
);
520 if (!num_eq (result
, num
))
521 result
.overflow
= true;
526 /* Handle meeting "defined" in a preprocessor expression. */
528 parse_defined (cpp_reader
*pfile
)
532 cpp_hashnode
*node
= 0;
533 const cpp_token
*token
;
534 cpp_context
*initial_context
= pfile
->context
;
536 /* Don't expand macros. */
537 pfile
->state
.prevent_expansion
++;
539 token
= cpp_get_token (pfile
);
540 if (token
->type
== CPP_OPEN_PAREN
)
543 token
= cpp_get_token (pfile
);
546 if (token
->type
== CPP_NAME
)
548 node
= token
->val
.node
;
549 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
551 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
557 cpp_error (pfile
, CPP_DL_ERROR
,
558 "operator \"defined\" requires an identifier");
559 if (token
->flags
& NAMED_OP
)
564 op
.type
= token
->type
;
565 cpp_error (pfile
, CPP_DL_ERROR
,
566 "(\"%s\" is an alternative token for \"%s\" in C++)",
567 cpp_token_as_text (pfile
, token
),
568 cpp_token_as_text (pfile
, &op
));
574 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
575 cpp_error (pfile
, CPP_DL_WARNING
,
576 "this use of \"defined\" may not be portable");
578 _cpp_mark_macro_used (node
);
580 /* A possible controlling macro of the form #if !defined ().
581 _cpp_parse_expr checks there was no other junk on the line. */
582 pfile
->mi_ind_cmacro
= node
;
585 pfile
->state
.prevent_expansion
--;
587 result
.unsignedp
= false;
589 result
.overflow
= false;
590 result
.low
= node
&& node
->type
== NT_MACRO
;
594 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
595 number or character constant, or the result of the "defined" or "#"
598 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
604 result
.unsignedp
= false;
605 result
.overflow
= false;
610 temp
= cpp_classify_number (pfile
, token
);
611 switch (temp
& CPP_N_CATEGORY
)
614 cpp_error (pfile
, CPP_DL_ERROR
,
615 "floating constant in preprocessor expression");
618 if (!(temp
& CPP_N_IMAGINARY
))
619 return cpp_interpret_integer (pfile
, token
, temp
);
620 cpp_error (pfile
, CPP_DL_ERROR
,
621 "imaginary number in preprocessor expression");
625 /* Error already issued. */
628 result
.high
= result
.low
= 0;
634 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
639 /* Sign-extend the result if necessary. */
640 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
642 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
643 result
.low
|= ~(~(cpp_num_part
) 0
644 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
645 result
.high
= ~(cpp_num_part
) 0;
646 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
652 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
653 return parse_defined (pfile
);
654 else if (CPP_OPTION (pfile
, cplusplus
)
655 && (token
->val
.node
== pfile
->spec_nodes
.n_true
656 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
659 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
665 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
666 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
667 NODE_NAME (token
->val
.node
));
671 default: /* CPP_HASH */
672 _cpp_test_assertion (pfile
, &temp
);
677 result
.unsignedp
= !!unsignedp
;
681 /* Operator precedence and flags table.
683 After an operator is returned from the lexer, if it has priority less
684 than the operator on the top of the stack, we reduce the stack by one
685 operator and repeat the test. Since equal priorities do not reduce,
686 this is naturally right-associative.
688 We handle left-associative operators by decrementing the priority of
689 just-lexed operators by one, but retaining the priority of operators
690 already on the stack.
692 The remaining cases are '(' and ')'. We handle '(' by skipping the
693 reduction phase completely. ')' is given lower priority than
694 everything else, including '(', effectively forcing a reduction of the
695 parenthesized expression. If there is a matching '(', the routine
696 reduce() exits immediately. If the normal exit route sees a ')', then
697 there cannot have been a matching '(' and an error message is output.
699 The parser assumes all shifted operators require a left operand unless
700 the flag NO_L_OPERAND is set. These semantics are automatic; any
701 extra semantics need to be handled with operator-specific code. */
703 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
704 operand changes because of integer promotions. */
705 #define NO_L_OPERAND (1 << 0)
706 #define LEFT_ASSOC (1 << 1)
707 #define CHECK_PROMOTION (1 << 2)
709 /* Operator to priority map. Must be in the same order as the first
710 N entries of enum cpp_ttype. */
711 static const struct cpp_operator
717 /* EQ */ {0, 0}, /* Shouldn't happen. */
718 /* NOT */ {16, NO_L_OPERAND
},
719 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
720 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
721 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
722 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
723 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
724 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
725 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
726 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
727 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
728 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
729 /* RSHIFT */ {13, LEFT_ASSOC
},
730 /* LSHIFT */ {13, LEFT_ASSOC
},
732 /* COMPL */ {16, NO_L_OPERAND
},
733 /* AND_AND */ {6, LEFT_ASSOC
},
734 /* OR_OR */ {5, LEFT_ASSOC
},
736 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
737 /* COMMA */ {2, LEFT_ASSOC
},
738 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
739 /* CLOSE_PAREN */ {0, 0},
741 /* EQ_EQ */ {11, LEFT_ASSOC
},
742 /* NOT_EQ */ {11, LEFT_ASSOC
},
743 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
744 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
745 /* UPLUS */ {16, NO_L_OPERAND
},
746 /* UMINUS */ {16, NO_L_OPERAND
}
749 /* Parse and evaluate a C expression, reading from PFILE.
750 Returns the truth value of the expression.
752 The implementation is an operator precedence parser, i.e. a
753 bottom-up parser, using a stack for not-yet-reduced tokens.
755 The stack base is op_stack, and the current stack pointer is 'top'.
756 There is a stack element for each operator (only), and the most
757 recently pushed operator is 'top->op'. An operand (value) is
758 stored in the 'value' field of the stack element of the operator
761 _cpp_parse_expr (cpp_reader
*pfile
)
763 struct op
*top
= pfile
->op_stack
;
764 unsigned int lex_count
;
765 bool saw_leading_not
, want_value
= true;
767 pfile
->state
.skip_eval
= 0;
769 /* Set up detection of #if ! defined(). */
770 pfile
->mi_ind_cmacro
= 0;
771 saw_leading_not
= false;
774 /* Lowest priority operator prevents further reductions. */
782 op
.token
= cpp_get_token (pfile
);
783 op
.op
= op
.token
->type
;
787 /* These tokens convert into values. */
794 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
795 cpp_token_as_text (pfile
, op
.token
));
797 top
->value
= eval_token (pfile
, op
.token
);
801 saw_leading_not
= lex_count
== 1;
813 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
814 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
815 cpp_token_as_text (pfile
, op
.token
));
819 /* Check we have a value or operator as appropriate. */
820 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
823 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
824 cpp_token_as_text (pfile
, op
.token
));
828 /* We want a number (or expression) and haven't got one.
829 Try to emit a specific diagnostic. */
830 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
831 SYNTAX_ERROR ("missing expression between '(' and ')'");
833 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
834 SYNTAX_ERROR ("#if with no expression");
836 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
837 SYNTAX_ERROR2 ("operator '%s' has no right operand",
838 cpp_token_as_text (pfile
, top
->token
));
839 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
840 /* Complain about missing paren during reduction. */;
842 SYNTAX_ERROR2 ("operator '%s' has no left operand",
843 cpp_token_as_text (pfile
, op
.token
));
846 top
= reduce (pfile
, top
, op
.op
);
850 if (op
.op
== CPP_EOF
)
855 case CPP_CLOSE_PAREN
:
858 if (!num_zerop (top
->value
))
859 pfile
->state
.skip_eval
++;
863 if (num_zerop (top
->value
))
864 pfile
->state
.skip_eval
++;
867 if (top
->op
!= CPP_QUERY
)
868 SYNTAX_ERROR (" ':' without preceding '?'");
869 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
870 pfile
->state
.skip_eval
++;
872 pfile
->state
.skip_eval
--;
879 /* Check for and handle stack overflow. */
880 if (++top
== pfile
->op_limit
)
881 top
= _cpp_expand_op_stack (pfile
);
884 top
->token
= op
.token
;
887 /* The controlling macro expression is only valid if we called lex 3
888 times: <!> <defined expression> and <EOF>. push_conditional ()
889 checks that we are at top-of-file. */
890 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
891 pfile
->mi_ind_cmacro
= 0;
893 if (top
!= pfile
->op_stack
)
895 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in #if");
897 return false; /* Return false on syntax error. */
900 return !num_zerop (top
->value
);
903 /* Reduce the operator / value stack if possible, in preparation for
904 pushing operator OP. Returns NULL on error, otherwise the top of
907 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
911 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
914 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
918 if (op
== CPP_OPEN_PAREN
)
921 /* Decrement the priority of left-associative operators to force a
922 reduction with operators of otherwise equal priority. */
923 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
924 while (prio
< optab
[top
->op
].prio
)
926 if (CPP_OPTION (pfile
, warn_num_sign_change
)
927 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
928 check_promotion (pfile
, top
);
936 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
944 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
945 top
->value
, top
->op
);
953 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
959 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
966 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
970 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
975 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
976 top
->value
, top
->op
);
981 if (!num_zerop (top
->value
))
982 pfile
->state
.skip_eval
--;
983 top
->value
.low
= (!num_zerop (top
->value
)
984 || !num_zerop (top
[1].value
));
986 top
->value
.unsignedp
= false;
987 top
->value
.overflow
= false;
992 if (num_zerop (top
->value
))
993 pfile
->state
.skip_eval
--;
994 top
->value
.low
= (!num_zerop (top
->value
)
995 && !num_zerop (top
[1].value
));
997 top
->value
.unsignedp
= false;
998 top
->value
.overflow
= false;
1001 case CPP_OPEN_PAREN
:
1002 if (op
!= CPP_CLOSE_PAREN
)
1004 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in expression");
1008 top
->value
= top
[1].value
;
1013 if (!num_zerop (top
->value
))
1015 pfile
->state
.skip_eval
--;
1016 top
->value
= top
[1].value
;
1019 top
->value
= top
[2].value
;
1020 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1021 || top
[2].value
.unsignedp
);
1025 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1033 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1034 cpp_error (pfile
, CPP_DL_PEDWARN
,
1035 "integer overflow in preprocessor expression");
1038 if (op
== CPP_CLOSE_PAREN
)
1040 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1047 /* Returns the position of the old top of stack after expansion. */
1049 _cpp_expand_op_stack (cpp_reader
*pfile
)
1051 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1052 size_t new_size
= old_size
* 2 + 20;
1054 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1055 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1057 return pfile
->op_stack
+ old_size
;
1060 /* Emits a warning if the effective sign of either operand of OP
1061 changes because of integer promotions. */
1063 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1065 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1068 if (op
->value
.unsignedp
)
1070 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1071 cpp_error (pfile
, CPP_DL_WARNING
,
1072 "the left operand of \"%s\" changes sign when promoted",
1073 cpp_token_as_text (pfile
, op
->token
));
1075 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1076 cpp_error (pfile
, CPP_DL_WARNING
,
1077 "the right operand of \"%s\" changes sign when promoted",
1078 cpp_token_as_text (pfile
, op
->token
));
1081 /* Clears the unused high order bits of the number pointed to by PNUM. */
1083 num_trim (cpp_num num
, size_t precision
)
1085 if (precision
> PART_PRECISION
)
1087 precision
-= PART_PRECISION
;
1088 if (precision
< PART_PRECISION
)
1089 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1093 if (precision
< PART_PRECISION
)
1094 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1101 /* True iff A (presumed signed) >= 0. */
1103 num_positive (cpp_num num
, size_t precision
)
1105 if (precision
> PART_PRECISION
)
1107 precision
-= PART_PRECISION
;
1108 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1111 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1114 /* Sign extend a number, with PRECISION significant bits and all
1115 others assumed clear, to fill out a cpp_num structure. */
1117 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1121 if (precision
> PART_PRECISION
)
1123 precision
-= PART_PRECISION
;
1124 if (precision
< PART_PRECISION
1125 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1126 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1128 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1130 if (precision
< PART_PRECISION
)
1131 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1132 num
.high
= ~(cpp_num_part
) 0;
1139 /* Returns the negative of NUM. */
1141 num_negate (cpp_num num
, size_t precision
)
1146 num
.high
= ~num
.high
;
1150 num
= num_trim (num
, precision
);
1151 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1156 /* Returns true if A >= B. */
1158 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1162 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1166 /* Both numbers have signed type. If they are of different
1167 sign, the answer is the sign of A. */
1168 unsignedp
= num_positive (pa
, precision
);
1170 if (unsignedp
!= num_positive (pb
, precision
))
1173 /* Otherwise we can do an unsigned comparison. */
1176 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1179 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1181 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1182 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1184 lhs
.overflow
= false;
1185 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1187 /* As excess precision is zeroed, there is no need to num_trim () as
1188 these operations cannot introduce a set bit there. */
1192 lhs
.high
&= rhs
.high
;
1194 else if (op
== CPP_OR
)
1197 lhs
.high
|= rhs
.high
;
1202 lhs
.high
^= rhs
.high
;
1208 /* Returns LHS OP RHS, where OP is an inequality. */
1210 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1213 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1215 if (op
== CPP_GREATER_EQ
)
1217 else if (op
== CPP_LESS
)
1219 else if (op
== CPP_GREATER
)
1220 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1221 else /* CPP_LESS_EQ. */
1222 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1225 lhs
.overflow
= false;
1226 lhs
.unsignedp
= false;
1230 /* Returns LHS OP RHS, where OP is == or !=. */
1232 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1233 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1235 /* Work around a 3.0.4 bug; see PR 6950. */
1236 bool eq
= num_eq (lhs
, rhs
);
1237 if (op
== CPP_NOT_EQ
)
1241 lhs
.overflow
= false;
1242 lhs
.unsignedp
= false;
1246 /* Shift NUM, of width PRECISION, right by N bits. */
1248 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1250 cpp_num_part sign_mask
;
1251 bool x
= num_positive (num
, precision
);
1253 if (num
.unsignedp
|| x
)
1256 sign_mask
= ~(cpp_num_part
) 0;
1259 num
.high
= num
.low
= sign_mask
;
1263 if (precision
< PART_PRECISION
)
1264 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1265 else if (precision
< 2 * PART_PRECISION
)
1266 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1268 if (n
>= PART_PRECISION
)
1270 n
-= PART_PRECISION
;
1272 num
.high
= sign_mask
;
1277 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1278 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1282 num
= num_trim (num
, precision
);
1283 num
.overflow
= false;
1287 /* Shift NUM, of width PRECISION, left by N bits. */
1289 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1293 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1294 num
.high
= num
.low
= 0;
1298 cpp_num orig
, maybe_orig
;
1302 if (m
>= PART_PRECISION
)
1304 m
-= PART_PRECISION
;
1310 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1313 num
= num_trim (num
, precision
);
1316 num
.overflow
= false;
1319 maybe_orig
= num_rshift (num
, precision
, n
);
1320 num
.overflow
= !num_eq (orig
, maybe_orig
);
1327 /* The four unary operators: +, -, ! and ~. */
1329 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1334 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1335 cpp_error (pfile
, CPP_DL_WARNING
,
1336 "traditional C rejects the unary plus operator");
1337 num
.overflow
= false;
1341 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1345 num
.high
= ~num
.high
;
1347 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1348 num
.overflow
= false;
1351 default: /* case CPP_NOT: */
1352 num
.low
= num_zerop (num
);
1354 num
.overflow
= false;
1355 num
.unsignedp
= false;
1362 /* The various binary operators. */
1364 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1367 size_t precision
= CPP_OPTION (pfile
, precision
);
1375 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1377 /* A negative shift is a positive shift the other way. */
1378 if (op
== CPP_LSHIFT
)
1382 rhs
= num_negate (rhs
, precision
);
1385 n
= ~0; /* Maximal. */
1388 if (op
== CPP_LSHIFT
)
1389 lhs
= num_lshift (lhs
, precision
, n
);
1391 lhs
= num_rshift (lhs
, precision
, n
);
1396 rhs
= num_negate (rhs
, precision
);
1398 result
.low
= lhs
.low
+ rhs
.low
;
1399 result
.high
= lhs
.high
+ rhs
.high
;
1400 if (result
.low
< lhs
.low
)
1402 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1403 result
.overflow
= false;
1405 result
= num_trim (result
, precision
);
1406 if (!result
.unsignedp
)
1408 bool lhsp
= num_positive (lhs
, precision
);
1409 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1410 && lhsp
!= num_positive (result
, precision
));
1415 default: /* case CPP_COMMA: */
1416 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1417 || !pfile
->state
.skip_eval
))
1418 cpp_error (pfile
, CPP_DL_PEDWARN
,
1419 "comma operator in operand of #if");
1427 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1430 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1433 cpp_num_part middle
[2], temp
;
1435 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1436 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1438 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1439 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1442 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1443 if (result
.low
< temp
)
1447 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1448 if (result
.low
< temp
)
1451 result
.high
+= HIGH_PART (middle
[0]);
1452 result
.high
+= HIGH_PART (middle
[1]);
1453 result
.unsignedp
= true;
1454 result
.overflow
= false;
1459 /* Multiply two preprocessing numbers. */
1461 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1463 cpp_num result
, temp
;
1464 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1465 bool overflow
, negate
= false;
1466 size_t precision
= CPP_OPTION (pfile
, precision
);
1468 /* Prepare for unsigned multiplication. */
1471 if (!num_positive (lhs
, precision
))
1472 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1473 if (!num_positive (rhs
, precision
))
1474 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1477 overflow
= lhs
.high
&& rhs
.high
;
1478 result
= num_part_mul (lhs
.low
, rhs
.low
);
1480 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1481 result
.high
+= temp
.low
;
1485 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1486 result
.high
+= temp
.low
;
1490 temp
.low
= result
.low
, temp
.high
= result
.high
;
1491 result
= num_trim (result
, precision
);
1492 if (!num_eq (result
, temp
))
1496 result
= num_negate (result
, precision
);
1499 result
.overflow
= false;
1501 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1502 && !num_zerop (result
));
1503 result
.unsignedp
= unsignedp
;
1508 /* Divide two preprocessing numbers, returning the answer or the
1509 remainder depending upon OP. */
1511 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1513 cpp_num result
, sub
;
1515 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1516 bool negate
= false, lhs_neg
= false;
1517 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1519 /* Prepare for unsigned division. */
1522 if (!num_positive (lhs
, precision
))
1523 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1524 if (!num_positive (rhs
, precision
))
1525 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1528 /* Find the high bit. */
1532 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1533 for (; ; i
--, mask
>>= 1)
1534 if (rhs
.high
& mask
)
1539 if (precision
> PART_PRECISION
)
1540 i
= precision
- PART_PRECISION
- 1;
1543 mask
= (cpp_num_part
) 1 << i
;
1544 for (; ; i
--, mask
>>= 1)
1550 if (!pfile
->state
.skip_eval
)
1551 cpp_error (pfile
, CPP_DL_ERROR
, "division by zero in #if");
1555 /* First nonzero bit of RHS is bit I. Do naive division by
1556 shifting the RHS fully left, and subtracting from LHS if LHS is
1557 at least as big, and then repeating but with one less shift.
1558 This is not very efficient, but is easy to understand. */
1560 rhs
.unsignedp
= true;
1561 lhs
.unsignedp
= true;
1562 i
= precision
- i
- 1;
1563 sub
= num_lshift (rhs
, precision
, i
);
1565 result
.high
= result
.low
= 0;
1568 if (num_greater_eq (lhs
, sub
, precision
))
1570 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1571 if (i
>= PART_PRECISION
)
1572 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1574 result
.low
|= (cpp_num_part
) 1 << i
;
1578 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1582 /* We divide so that the remainder has the sign of the LHS. */
1585 result
.unsignedp
= unsignedp
;
1586 result
.overflow
= false;
1590 result
= num_negate (result
, precision
);
1591 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1592 && !num_zerop (result
));
1599 lhs
.unsignedp
= unsignedp
;
1600 lhs
.overflow
= false;
1602 lhs
= num_negate (lhs
, precision
);