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
= 0, l
= 0, i
= 0;
90 case 'f': case 'F': f
++; break;
91 case 'l': case 'L': l
++; break;
93 case 'j': case 'J': i
++; break;
98 if (f
+ l
> 1 || i
> 1)
101 return ((i
? CPP_N_IMAGINARY
: 0)
103 l
? CPP_N_LARGE
: CPP_N_MEDIUM
));
106 /* Subroutine of cpp_classify_number. S points to an integer suffix
107 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
108 flag vector describing the suffix. */
110 interpret_int_suffix (const uchar
*s
, size_t len
)
119 case 'u': case 'U': u
++; break;
121 case 'j': case 'J': i
++; break;
122 case 'l': case 'L': l
++;
123 /* If there are two Ls, they must be adjacent and the same case. */
124 if (l
== 2 && s
[len
] != s
[len
+ 1])
131 if (l
> 2 || u
> 1 || i
> 1)
134 return ((i
? CPP_N_IMAGINARY
: 0)
135 | (u
? CPP_N_UNSIGNED
: 0)
136 | ((l
== 0) ? CPP_N_SMALL
137 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
140 /* Categorize numeric constants according to their field (integer,
141 floating point, or invalid), radix (decimal, octal, hexadecimal),
142 and type suffixes. */
144 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
146 const uchar
*str
= token
->val
.str
.text
;
148 unsigned int max_digit
, result
, radix
;
149 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
151 /* If the lexer has done its job, length one can only be a single
152 digit. Fast-path this very common case. */
153 if (token
->val
.str
.len
== 1)
154 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
156 limit
= str
+ token
->val
.str
.len
;
157 float_flag
= NOT_FLOAT
;
161 /* First, interpret the radix. */
167 /* Require at least one hex digit to classify it as hex. */
168 if ((*str
== 'x' || *str
== 'X')
169 && (str
[1] == '.' || ISXDIGIT (str
[1])))
176 /* Now scan for a well-formed integer or float. */
179 unsigned int c
= *str
++;
181 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
189 if (float_flag
== NOT_FLOAT
)
190 float_flag
= AFTER_POINT
;
192 SYNTAX_ERROR ("too many decimal points in number");
194 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
195 || (radix
== 16 && (c
== 'p' || c
== 'P')))
197 float_flag
= AFTER_EXPON
;
202 /* Start of suffix. */
208 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
211 if (max_digit
>= radix
)
212 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
214 if (float_flag
!= NOT_FLOAT
)
216 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
217 cpp_error (pfile
, CPP_DL_PEDWARN
,
218 "use of C99 hexadecimal floating constant");
220 if (float_flag
== AFTER_EXPON
)
222 if (*str
== '+' || *str
== '-')
225 /* Exponent is decimal, even if string is a hex float. */
227 SYNTAX_ERROR ("exponent has no digits");
231 while (ISDIGIT (*str
));
233 else if (radix
== 16)
234 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
236 result
= interpret_float_suffix (str
, limit
- str
);
239 cpp_error (pfile
, CPP_DL_ERROR
,
240 "invalid suffix \"%.*s\" on floating constant",
241 (int) (limit
- str
), str
);
242 return CPP_N_INVALID
;
245 /* Traditional C didn't accept any floating suffixes. */
247 && CPP_WTRADITIONAL (pfile
)
248 && ! cpp_sys_macro_p (pfile
))
249 cpp_error (pfile
, CPP_DL_WARNING
,
250 "traditional C rejects the \"%.*s\" suffix",
251 (int) (limit
- str
), str
);
253 result
|= CPP_N_FLOATING
;
257 result
= interpret_int_suffix (str
, limit
- str
);
260 cpp_error (pfile
, CPP_DL_ERROR
,
261 "invalid suffix \"%.*s\" on integer constant",
262 (int) (limit
- str
), str
);
263 return CPP_N_INVALID
;
266 /* Traditional C only accepted the 'L' suffix.
267 Suppress warning about 'LL' with -Wno-long-long. */
268 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
270 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
271 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
273 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
274 cpp_error (pfile
, CPP_DL_WARNING
,
275 "traditional C rejects the \"%.*s\" suffix",
276 (int) (limit
- str
), str
);
279 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
280 && ! CPP_OPTION (pfile
, c99
)
281 && CPP_OPTION (pfile
, warn_long_long
))
282 cpp_error (pfile
, CPP_DL_PEDWARN
,
283 "use of C99 long long integer constant");
285 result
|= CPP_N_INTEGER
;
288 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
289 cpp_error (pfile
, CPP_DL_PEDWARN
,
290 "imaginary constants are a GCC extension");
293 result
|= CPP_N_DECIMAL
;
294 else if (radix
== 16)
297 result
|= CPP_N_OCTAL
;
302 return CPP_N_INVALID
;
305 /* cpp_interpret_integer converts an integer constant into a cpp_num,
306 of precision options->precision.
308 We do not provide any interface for decimal->float conversion,
309 because the preprocessor doesn't need it and we don't want to
310 drag in GCC's floating point emulator. */
312 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
315 const uchar
*p
, *end
;
320 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
321 result
.overflow
= false;
323 p
= token
->val
.str
.text
;
324 end
= p
+ token
->val
.str
.len
;
326 /* Common case of a single digit. */
327 if (token
->val
.str
.len
== 1)
328 result
.low
= p
[0] - '0';
332 size_t precision
= CPP_OPTION (pfile
, precision
);
333 unsigned int base
= 10, c
= 0;
334 bool overflow
= false;
336 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
341 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
347 /* We can add a digit to numbers strictly less than this without
348 needing the precision and slowness of double integers. */
349 max
= ~(cpp_num_part
) 0;
350 if (precision
< PART_PRECISION
)
351 max
>>= PART_PRECISION
- precision
;
352 max
= (max
- base
+ 1) / base
+ 1;
358 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
363 /* Strict inequality for when max is set to zero. */
364 if (result
.low
< max
)
365 result
.low
= result
.low
* base
+ c
;
368 result
= append_digit (result
, c
, base
, precision
);
369 overflow
|= result
.overflow
;
375 cpp_error (pfile
, CPP_DL_PEDWARN
,
376 "integer constant is too large for its type");
377 /* If too big to be signed, consider it unsigned. Only warn for
378 decimal numbers. Traditional numbers were always signed (but
379 we still honor an explicit U suffix); but we only have
380 traditional semantics in directives. */
381 else if (!result
.unsignedp
382 && !(CPP_OPTION (pfile
, traditional
)
383 && pfile
->state
.in_directive
)
384 && !num_positive (result
, precision
))
387 cpp_error (pfile
, CPP_DL_WARNING
,
388 "integer constant is so large that it is unsigned");
389 result
.unsignedp
= true;
396 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
398 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
401 unsigned int shift
= 3 + (base
== 16);
403 cpp_num_part add_high
, add_low
;
405 /* Multiply by 8 or 16. Catching this overflow here means we don't
406 need to worry about add_high overflowing. */
407 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
408 result
.high
= num
.high
<< shift
;
409 result
.low
= num
.low
<< shift
;
410 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
411 result
.unsignedp
= num
.unsignedp
;
415 add_low
= num
.low
<< 1;
416 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
419 add_high
= add_low
= 0;
421 if (add_low
+ digit
< add_low
)
425 if (result
.low
+ add_low
< result
.low
)
427 if (result
.high
+ add_high
< result
.high
)
430 result
.low
+= add_low
;
431 result
.high
+= add_high
;
432 result
.overflow
= overflow
;
434 /* The above code catches overflow of a cpp_num type. This catches
435 overflow of the (possibly shorter) target precision. */
436 num
.low
= result
.low
;
437 num
.high
= result
.high
;
438 result
= num_trim (result
, precision
);
439 if (!num_eq (result
, num
))
440 result
.overflow
= true;
445 /* Handle meeting "defined" in a preprocessor expression. */
447 parse_defined (cpp_reader
*pfile
)
451 cpp_hashnode
*node
= 0;
452 const cpp_token
*token
;
453 cpp_context
*initial_context
= pfile
->context
;
455 /* Don't expand macros. */
456 pfile
->state
.prevent_expansion
++;
458 token
= cpp_get_token (pfile
);
459 if (token
->type
== CPP_OPEN_PAREN
)
462 token
= cpp_get_token (pfile
);
465 if (token
->type
== CPP_NAME
)
467 node
= token
->val
.node
;
468 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
470 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
476 cpp_error (pfile
, CPP_DL_ERROR
,
477 "operator \"defined\" requires an identifier");
478 if (token
->flags
& NAMED_OP
)
483 op
.type
= token
->type
;
484 cpp_error (pfile
, CPP_DL_ERROR
,
485 "(\"%s\" is an alternative token for \"%s\" in C++)",
486 cpp_token_as_text (pfile
, token
),
487 cpp_token_as_text (pfile
, &op
));
493 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
494 cpp_error (pfile
, CPP_DL_WARNING
,
495 "this use of \"defined\" may not be portable");
497 _cpp_mark_macro_used (node
);
499 /* A possible controlling macro of the form #if !defined ().
500 _cpp_parse_expr checks there was no other junk on the line. */
501 pfile
->mi_ind_cmacro
= node
;
504 pfile
->state
.prevent_expansion
--;
506 result
.unsignedp
= false;
508 result
.overflow
= false;
509 result
.low
= node
&& node
->type
== NT_MACRO
;
513 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
514 number or character constant, or the result of the "defined" or "#"
517 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
523 result
.unsignedp
= false;
524 result
.overflow
= false;
529 temp
= cpp_classify_number (pfile
, token
);
530 switch (temp
& CPP_N_CATEGORY
)
533 cpp_error (pfile
, CPP_DL_ERROR
,
534 "floating constant in preprocessor expression");
537 if (!(temp
& CPP_N_IMAGINARY
))
538 return cpp_interpret_integer (pfile
, token
, temp
);
539 cpp_error (pfile
, CPP_DL_ERROR
,
540 "imaginary number in preprocessor expression");
544 /* Error already issued. */
547 result
.high
= result
.low
= 0;
553 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
558 /* Sign-extend the result if necessary. */
559 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
561 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
562 result
.low
|= ~(~(cpp_num_part
) 0
563 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
564 result
.high
= ~(cpp_num_part
) 0;
565 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
571 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
572 return parse_defined (pfile
);
573 else if (CPP_OPTION (pfile
, cplusplus
)
574 && (token
->val
.node
== pfile
->spec_nodes
.n_true
575 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
578 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
584 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
585 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
586 NODE_NAME (token
->val
.node
));
590 default: /* CPP_HASH */
591 _cpp_test_assertion (pfile
, &temp
);
596 result
.unsignedp
= !!unsignedp
;
600 /* Operator precedence and flags table.
602 After an operator is returned from the lexer, if it has priority less
603 than the operator on the top of the stack, we reduce the stack by one
604 operator and repeat the test. Since equal priorities do not reduce,
605 this is naturally right-associative.
607 We handle left-associative operators by decrementing the priority of
608 just-lexed operators by one, but retaining the priority of operators
609 already on the stack.
611 The remaining cases are '(' and ')'. We handle '(' by skipping the
612 reduction phase completely. ')' is given lower priority than
613 everything else, including '(', effectively forcing a reduction of the
614 parenthesized expression. If there is a matching '(', the routine
615 reduce() exits immediately. If the normal exit route sees a ')', then
616 there cannot have been a matching '(' and an error message is output.
618 The parser assumes all shifted operators require a left operand unless
619 the flag NO_L_OPERAND is set. These semantics are automatic; any
620 extra semantics need to be handled with operator-specific code. */
622 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
623 operand changes because of integer promotions. */
624 #define NO_L_OPERAND (1 << 0)
625 #define LEFT_ASSOC (1 << 1)
626 #define CHECK_PROMOTION (1 << 2)
628 /* Operator to priority map. Must be in the same order as the first
629 N entries of enum cpp_ttype. */
630 static const struct cpp_operator
636 /* EQ */ {0, 0}, /* Shouldn't happen. */
637 /* NOT */ {16, NO_L_OPERAND
},
638 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
639 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
640 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
641 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
642 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
643 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
644 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
645 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
646 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
647 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
648 /* RSHIFT */ {13, LEFT_ASSOC
},
649 /* LSHIFT */ {13, LEFT_ASSOC
},
651 /* MIN */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
652 /* MAX */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
654 /* COMPL */ {16, NO_L_OPERAND
},
655 /* AND_AND */ {6, LEFT_ASSOC
},
656 /* OR_OR */ {5, LEFT_ASSOC
},
658 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
659 /* COMMA */ {2, LEFT_ASSOC
},
660 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
661 /* CLOSE_PAREN */ {0, 0},
663 /* EQ_EQ */ {11, LEFT_ASSOC
},
664 /* NOT_EQ */ {11, LEFT_ASSOC
},
665 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
666 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
667 /* UPLUS */ {16, NO_L_OPERAND
},
668 /* UMINUS */ {16, NO_L_OPERAND
}
671 /* Parse and evaluate a C expression, reading from PFILE.
672 Returns the truth value of the expression.
674 The implementation is an operator precedence parser, i.e. a
675 bottom-up parser, using a stack for not-yet-reduced tokens.
677 The stack base is op_stack, and the current stack pointer is 'top'.
678 There is a stack element for each operator (only), and the most
679 recently pushed operator is 'top->op'. An operand (value) is
680 stored in the 'value' field of the stack element of the operator
683 _cpp_parse_expr (cpp_reader
*pfile
)
685 struct op
*top
= pfile
->op_stack
;
686 unsigned int lex_count
;
687 bool saw_leading_not
, want_value
= true;
689 pfile
->state
.skip_eval
= 0;
691 /* Set up detection of #if ! defined(). */
692 pfile
->mi_ind_cmacro
= 0;
693 saw_leading_not
= false;
696 /* Lowest priority operator prevents further reductions. */
704 op
.token
= cpp_get_token (pfile
);
705 op
.op
= op
.token
->type
;
709 /* These tokens convert into values. */
716 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
717 cpp_token_as_text (pfile
, op
.token
));
719 top
->value
= eval_token (pfile
, op
.token
);
723 saw_leading_not
= lex_count
== 1;
735 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
736 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
737 cpp_token_as_text (pfile
, op
.token
));
741 /* Check we have a value or operator as appropriate. */
742 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
745 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
746 cpp_token_as_text (pfile
, op
.token
));
750 /* We want a number (or expression) and haven't got one.
751 Try to emit a specific diagnostic. */
752 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
753 SYNTAX_ERROR ("missing expression between '(' and ')'");
755 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
756 SYNTAX_ERROR ("#if with no expression");
758 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
759 SYNTAX_ERROR2 ("operator '%s' has no right operand",
760 cpp_token_as_text (pfile
, top
->token
));
761 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
762 /* Complain about missing paren during reduction. */;
764 SYNTAX_ERROR2 ("operator '%s' has no left operand",
765 cpp_token_as_text (pfile
, op
.token
));
768 top
= reduce (pfile
, top
, op
.op
);
772 if (op
.op
== CPP_EOF
)
777 case CPP_CLOSE_PAREN
:
780 if (!num_zerop (top
->value
))
781 pfile
->state
.skip_eval
++;
785 if (num_zerop (top
->value
))
786 pfile
->state
.skip_eval
++;
789 if (top
->op
!= CPP_QUERY
)
790 SYNTAX_ERROR (" ':' without preceding '?'");
791 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
792 pfile
->state
.skip_eval
++;
794 pfile
->state
.skip_eval
--;
801 /* Check for and handle stack overflow. */
802 if (++top
== pfile
->op_limit
)
803 top
= _cpp_expand_op_stack (pfile
);
806 top
->token
= op
.token
;
809 /* The controlling macro expression is only valid if we called lex 3
810 times: <!> <defined expression> and <EOF>. push_conditional ()
811 checks that we are at top-of-file. */
812 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
813 pfile
->mi_ind_cmacro
= 0;
815 if (top
!= pfile
->op_stack
)
817 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in #if");
819 return false; /* Return false on syntax error. */
822 return !num_zerop (top
->value
);
825 /* Reduce the operator / value stack if possible, in preparation for
826 pushing operator OP. Returns NULL on error, otherwise the top of
829 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
833 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
836 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
840 if (op
== CPP_OPEN_PAREN
)
843 /* Decrement the priority of left-associative operators to force a
844 reduction with operators of otherwise equal priority. */
845 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
846 while (prio
< optab
[top
->op
].prio
)
848 if (CPP_OPTION (pfile
, warn_num_sign_change
)
849 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
850 check_promotion (pfile
, top
);
858 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
868 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
869 top
->value
, top
->op
);
877 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
883 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
890 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
894 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
899 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
900 top
->value
, top
->op
);
905 if (!num_zerop (top
->value
))
906 pfile
->state
.skip_eval
--;
907 top
->value
.low
= (!num_zerop (top
->value
)
908 || !num_zerop (top
[1].value
));
910 top
->value
.unsignedp
= false;
911 top
->value
.overflow
= false;
916 if (num_zerop (top
->value
))
917 pfile
->state
.skip_eval
--;
918 top
->value
.low
= (!num_zerop (top
->value
)
919 && !num_zerop (top
[1].value
));
921 top
->value
.unsignedp
= false;
922 top
->value
.overflow
= false;
926 if (op
!= CPP_CLOSE_PAREN
)
928 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in expression");
932 top
->value
= top
[1].value
;
937 if (!num_zerop (top
->value
))
939 pfile
->state
.skip_eval
--;
940 top
->value
= top
[1].value
;
943 top
->value
= top
[2].value
;
944 top
->value
.unsignedp
= (top
[1].value
.unsignedp
945 || top
[2].value
.unsignedp
);
949 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
957 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
958 cpp_error (pfile
, CPP_DL_PEDWARN
,
959 "integer overflow in preprocessor expression");
962 if (op
== CPP_CLOSE_PAREN
)
964 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
971 /* Returns the position of the old top of stack after expansion. */
973 _cpp_expand_op_stack (cpp_reader
*pfile
)
975 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
976 size_t new_size
= old_size
* 2 + 20;
978 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
979 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
981 return pfile
->op_stack
+ old_size
;
984 /* Emits a warning if the effective sign of either operand of OP
985 changes because of integer promotions. */
987 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
989 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
992 if (op
->value
.unsignedp
)
994 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
995 cpp_error (pfile
, CPP_DL_WARNING
,
996 "the left operand of \"%s\" changes sign when promoted",
997 cpp_token_as_text (pfile
, op
->token
));
999 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1000 cpp_error (pfile
, CPP_DL_WARNING
,
1001 "the right operand of \"%s\" changes sign when promoted",
1002 cpp_token_as_text (pfile
, op
->token
));
1005 /* Clears the unused high order bits of the number pointed to by PNUM. */
1007 num_trim (cpp_num num
, size_t precision
)
1009 if (precision
> PART_PRECISION
)
1011 precision
-= PART_PRECISION
;
1012 if (precision
< PART_PRECISION
)
1013 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1017 if (precision
< PART_PRECISION
)
1018 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1025 /* True iff A (presumed signed) >= 0. */
1027 num_positive (cpp_num num
, size_t precision
)
1029 if (precision
> PART_PRECISION
)
1031 precision
-= PART_PRECISION
;
1032 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1035 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1038 /* Sign extend a number, with PRECISION significant bits and all
1039 others assumed clear, to fill out a cpp_num structure. */
1041 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1045 if (precision
> PART_PRECISION
)
1047 precision
-= PART_PRECISION
;
1048 if (precision
< PART_PRECISION
1049 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1050 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1052 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1054 if (precision
< PART_PRECISION
)
1055 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1056 num
.high
= ~(cpp_num_part
) 0;
1063 /* Returns the negative of NUM. */
1065 num_negate (cpp_num num
, size_t precision
)
1070 num
.high
= ~num
.high
;
1074 num
= num_trim (num
, precision
);
1075 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1080 /* Returns true if A >= B. */
1082 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1086 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1090 /* Both numbers have signed type. If they are of different
1091 sign, the answer is the sign of A. */
1092 unsignedp
= num_positive (pa
, precision
);
1094 if (unsignedp
!= num_positive (pb
, precision
))
1097 /* Otherwise we can do an unsigned comparison. */
1100 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1103 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1105 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1106 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1108 lhs
.overflow
= false;
1109 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1111 /* As excess precision is zeroed, there is no need to num_trim () as
1112 these operations cannot introduce a set bit there. */
1116 lhs
.high
&= rhs
.high
;
1118 else if (op
== CPP_OR
)
1121 lhs
.high
|= rhs
.high
;
1126 lhs
.high
^= rhs
.high
;
1132 /* Returns LHS OP RHS, where OP is an inequality. */
1134 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1137 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1139 if (op
== CPP_GREATER_EQ
)
1141 else if (op
== CPP_LESS
)
1143 else if (op
== CPP_GREATER
)
1144 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1145 else /* CPP_LESS_EQ. */
1146 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1149 lhs
.overflow
= false;
1150 lhs
.unsignedp
= false;
1154 /* Returns LHS OP RHS, where OP is == or !=. */
1156 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1157 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1159 /* Work around a 3.0.4 bug; see PR 6950. */
1160 bool eq
= num_eq (lhs
, rhs
);
1161 if (op
== CPP_NOT_EQ
)
1165 lhs
.overflow
= false;
1166 lhs
.unsignedp
= false;
1170 /* Shift NUM, of width PRECISION, right by N bits. */
1172 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1174 cpp_num_part sign_mask
;
1175 bool x
= num_positive (num
, precision
);
1177 if (num
.unsignedp
|| x
)
1180 sign_mask
= ~(cpp_num_part
) 0;
1183 num
.high
= num
.low
= sign_mask
;
1187 if (precision
< PART_PRECISION
)
1188 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1189 else if (precision
< 2 * PART_PRECISION
)
1190 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1192 if (n
>= PART_PRECISION
)
1194 n
-= PART_PRECISION
;
1196 num
.high
= sign_mask
;
1201 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1202 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1206 num
= num_trim (num
, precision
);
1207 num
.overflow
= false;
1211 /* Shift NUM, of width PRECISION, left by N bits. */
1213 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1217 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1218 num
.high
= num
.low
= 0;
1222 cpp_num orig
, maybe_orig
;
1226 if (m
>= PART_PRECISION
)
1228 m
-= PART_PRECISION
;
1234 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1237 num
= num_trim (num
, precision
);
1240 num
.overflow
= false;
1243 maybe_orig
= num_rshift (num
, precision
, n
);
1244 num
.overflow
= !num_eq (orig
, maybe_orig
);
1251 /* The four unary operators: +, -, ! and ~. */
1253 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1258 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1259 cpp_error (pfile
, CPP_DL_WARNING
,
1260 "traditional C rejects the unary plus operator");
1261 num
.overflow
= false;
1265 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1269 num
.high
= ~num
.high
;
1271 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1272 num
.overflow
= false;
1275 default: /* case CPP_NOT: */
1276 num
.low
= num_zerop (num
);
1278 num
.overflow
= false;
1279 num
.unsignedp
= false;
1286 /* The various binary operators. */
1288 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1291 size_t precision
= CPP_OPTION (pfile
, precision
);
1300 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1302 /* A negative shift is a positive shift the other way. */
1303 if (op
== CPP_LSHIFT
)
1307 rhs
= num_negate (rhs
, precision
);
1310 n
= ~0; /* Maximal. */
1313 if (op
== CPP_LSHIFT
)
1314 lhs
= num_lshift (lhs
, precision
, n
);
1316 lhs
= num_rshift (lhs
, precision
, n
);
1323 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1325 gte
= num_greater_eq (lhs
, rhs
, precision
);
1330 lhs
.unsignedp
= unsignedp
;
1336 rhs
= num_negate (rhs
, precision
);
1338 result
.low
= lhs
.low
+ rhs
.low
;
1339 result
.high
= lhs
.high
+ rhs
.high
;
1340 if (result
.low
< lhs
.low
)
1342 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1343 result
.overflow
= false;
1345 result
= num_trim (result
, precision
);
1346 if (!result
.unsignedp
)
1348 bool lhsp
= num_positive (lhs
, precision
);
1349 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1350 && lhsp
!= num_positive (result
, precision
));
1355 default: /* case CPP_COMMA: */
1356 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1357 || !pfile
->state
.skip_eval
))
1358 cpp_error (pfile
, CPP_DL_PEDWARN
,
1359 "comma operator in operand of #if");
1367 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1370 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1373 cpp_num_part middle
[2], temp
;
1375 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1376 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1378 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1379 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1382 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1383 if (result
.low
< temp
)
1387 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1388 if (result
.low
< temp
)
1391 result
.high
+= HIGH_PART (middle
[0]);
1392 result
.high
+= HIGH_PART (middle
[1]);
1393 result
.unsignedp
= true;
1394 result
.overflow
= false;
1399 /* Multiply two preprocessing numbers. */
1401 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1403 cpp_num result
, temp
;
1404 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1405 bool overflow
, negate
= false;
1406 size_t precision
= CPP_OPTION (pfile
, precision
);
1408 /* Prepare for unsigned multiplication. */
1411 if (!num_positive (lhs
, precision
))
1412 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1413 if (!num_positive (rhs
, precision
))
1414 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1417 overflow
= lhs
.high
&& rhs
.high
;
1418 result
= num_part_mul (lhs
.low
, rhs
.low
);
1420 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1421 result
.high
+= temp
.low
;
1425 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1426 result
.high
+= temp
.low
;
1430 temp
.low
= result
.low
, temp
.high
= result
.high
;
1431 result
= num_trim (result
, precision
);
1432 if (!num_eq (result
, temp
))
1436 result
= num_negate (result
, precision
);
1439 result
.overflow
= false;
1441 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1442 && !num_zerop (result
));
1443 result
.unsignedp
= unsignedp
;
1448 /* Divide two preprocessing numbers, returning the answer or the
1449 remainder depending upon OP. */
1451 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1453 cpp_num result
, sub
;
1455 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1456 bool negate
= false, lhs_neg
= false;
1457 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1459 /* Prepare for unsigned division. */
1462 if (!num_positive (lhs
, precision
))
1463 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1464 if (!num_positive (rhs
, precision
))
1465 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1468 /* Find the high bit. */
1472 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1473 for (; ; i
--, mask
>>= 1)
1474 if (rhs
.high
& mask
)
1479 if (precision
> PART_PRECISION
)
1480 i
= precision
- PART_PRECISION
- 1;
1483 mask
= (cpp_num_part
) 1 << i
;
1484 for (; ; i
--, mask
>>= 1)
1490 if (!pfile
->state
.skip_eval
)
1491 cpp_error (pfile
, CPP_DL_ERROR
, "division by zero in #if");
1495 /* First nonzero bit of RHS is bit I. Do naive division by
1496 shifting the RHS fully left, and subtracting from LHS if LHS is
1497 at least as big, and then repeating but with one less shift.
1498 This is not very efficient, but is easy to understand. */
1500 rhs
.unsignedp
= true;
1501 lhs
.unsignedp
= true;
1502 i
= precision
- i
- 1;
1503 sub
= num_lshift (rhs
, precision
, i
);
1505 result
.high
= result
.low
= 0;
1508 if (num_greater_eq (lhs
, sub
, precision
))
1510 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1511 if (i
>= PART_PRECISION
)
1512 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1514 result
.low
|= (cpp_num_part
) 1 << i
;
1518 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1522 /* We divide so that the remainder has the sign of the LHS. */
1525 result
.unsignedp
= unsignedp
;
1526 result
.overflow
= false;
1530 result
= num_negate (result
, precision
);
1531 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1538 lhs
.unsignedp
= unsignedp
;
1539 lhs
.overflow
= false;
1541 lhs
= num_negate (lhs
, precision
);