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, d
= 0;
90 case 'f': case 'F': f
++; break;
91 case 'l': case 'L': l
++; break;
93 case 'j': case 'J': i
++; break;
95 /* Disallow fd, ld suffixes. */
104 if (f
+ l
> 1 || i
> 1)
107 /* Allow dd, df, dl suffixes for decimal float constants. */
108 if (d
&& ((d
+ f
+ l
!= 2) || i
))
111 return ((i
? CPP_N_IMAGINARY
: 0)
113 l
? CPP_N_LARGE
: CPP_N_MEDIUM
)
114 | (d
? CPP_N_DFLOAT
: 0));
117 /* Subroutine of cpp_classify_number. S points to an integer suffix
118 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
119 flag vector describing the suffix. */
121 interpret_int_suffix (const uchar
*s
, size_t len
)
130 case 'u': case 'U': u
++; break;
132 case 'j': case 'J': i
++; break;
133 case 'l': case 'L': l
++;
134 /* If there are two Ls, they must be adjacent and the same case. */
135 if (l
== 2 && s
[len
] != s
[len
+ 1])
142 if (l
> 2 || u
> 1 || i
> 1)
145 return ((i
? CPP_N_IMAGINARY
: 0)
146 | (u
? CPP_N_UNSIGNED
: 0)
147 | ((l
== 0) ? CPP_N_SMALL
148 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
151 /* Categorize numeric constants according to their field (integer,
152 floating point, or invalid), radix (decimal, octal, hexadecimal),
153 and type suffixes. */
155 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
157 const uchar
*str
= token
->val
.str
.text
;
159 unsigned int max_digit
, result
, radix
;
160 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
162 /* If the lexer has done its job, length one can only be a single
163 digit. Fast-path this very common case. */
164 if (token
->val
.str
.len
== 1)
165 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
167 limit
= str
+ token
->val
.str
.len
;
168 float_flag
= NOT_FLOAT
;
172 /* First, interpret the radix. */
178 /* Require at least one hex digit to classify it as hex. */
179 if ((*str
== 'x' || *str
== 'X')
180 && (str
[1] == '.' || ISXDIGIT (str
[1])))
187 /* Now scan for a well-formed integer or float. */
190 unsigned int c
= *str
++;
192 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
200 if (float_flag
== NOT_FLOAT
)
201 float_flag
= AFTER_POINT
;
203 SYNTAX_ERROR ("too many decimal points in number");
205 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
206 || (radix
== 16 && (c
== 'p' || c
== 'P')))
208 float_flag
= AFTER_EXPON
;
213 /* Start of suffix. */
219 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
222 if (max_digit
>= radix
)
223 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
225 if (float_flag
!= NOT_FLOAT
)
227 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
228 cpp_error (pfile
, CPP_DL_PEDWARN
,
229 "use of C99 hexadecimal floating constant");
231 if (float_flag
== AFTER_EXPON
)
233 if (*str
== '+' || *str
== '-')
236 /* Exponent is decimal, even if string is a hex float. */
238 SYNTAX_ERROR ("exponent has no digits");
242 while (ISDIGIT (*str
));
244 else if (radix
== 16)
245 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
247 result
= interpret_float_suffix (str
, limit
- str
);
250 cpp_error (pfile
, CPP_DL_ERROR
,
251 "invalid suffix \"%.*s\" on floating constant",
252 (int) (limit
- str
), str
);
253 return CPP_N_INVALID
;
256 /* Traditional C didn't accept any floating suffixes. */
258 && CPP_WTRADITIONAL (pfile
)
259 && ! cpp_sys_macro_p (pfile
))
260 cpp_error (pfile
, CPP_DL_WARNING
,
261 "traditional C rejects the \"%.*s\" suffix",
262 (int) (limit
- str
), str
);
264 /* Radix must be 10 for decimal floats. */
265 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
267 cpp_error (pfile
, CPP_DL_ERROR
,
268 "invalid suffix \"%.*s\" with hexadecimal floating constant",
269 (int) (limit
- str
), str
);
270 return CPP_N_INVALID
;
273 result
|= CPP_N_FLOATING
;
277 result
= interpret_int_suffix (str
, limit
- str
);
280 cpp_error (pfile
, CPP_DL_ERROR
,
281 "invalid suffix \"%.*s\" on integer constant",
282 (int) (limit
- str
), str
);
283 return CPP_N_INVALID
;
286 /* Traditional C only accepted the 'L' suffix.
287 Suppress warning about 'LL' with -Wno-long-long. */
288 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
290 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
291 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
293 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
294 cpp_error (pfile
, CPP_DL_WARNING
,
295 "traditional C rejects the \"%.*s\" suffix",
296 (int) (limit
- str
), str
);
299 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
300 && ! CPP_OPTION (pfile
, c99
)
301 && CPP_OPTION (pfile
, warn_long_long
))
302 cpp_error (pfile
, CPP_DL_PEDWARN
,
303 "use of C99 long long integer constant");
305 result
|= CPP_N_INTEGER
;
308 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
309 cpp_error (pfile
, CPP_DL_PEDWARN
,
310 "imaginary constants are a GCC extension");
313 result
|= CPP_N_DECIMAL
;
314 else if (radix
== 16)
317 result
|= CPP_N_OCTAL
;
322 return CPP_N_INVALID
;
325 /* cpp_interpret_integer converts an integer constant into a cpp_num,
326 of precision options->precision.
328 We do not provide any interface for decimal->float conversion,
329 because the preprocessor doesn't need it and we don't want to
330 drag in GCC's floating point emulator. */
332 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
335 const uchar
*p
, *end
;
340 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
341 result
.overflow
= false;
343 p
= token
->val
.str
.text
;
344 end
= p
+ token
->val
.str
.len
;
346 /* Common case of a single digit. */
347 if (token
->val
.str
.len
== 1)
348 result
.low
= p
[0] - '0';
352 size_t precision
= CPP_OPTION (pfile
, precision
);
353 unsigned int base
= 10, c
= 0;
354 bool overflow
= false;
356 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
361 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
367 /* We can add a digit to numbers strictly less than this without
368 needing the precision and slowness of double integers. */
369 max
= ~(cpp_num_part
) 0;
370 if (precision
< PART_PRECISION
)
371 max
>>= PART_PRECISION
- precision
;
372 max
= (max
- base
+ 1) / base
+ 1;
378 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
383 /* Strict inequality for when max is set to zero. */
384 if (result
.low
< max
)
385 result
.low
= result
.low
* base
+ c
;
388 result
= append_digit (result
, c
, base
, precision
);
389 overflow
|= result
.overflow
;
395 cpp_error (pfile
, CPP_DL_PEDWARN
,
396 "integer constant is too large for its type");
397 /* If too big to be signed, consider it unsigned. Only warn for
398 decimal numbers. Traditional numbers were always signed (but
399 we still honor an explicit U suffix); but we only have
400 traditional semantics in directives. */
401 else if (!result
.unsignedp
402 && !(CPP_OPTION (pfile
, traditional
)
403 && pfile
->state
.in_directive
)
404 && !num_positive (result
, precision
))
407 cpp_error (pfile
, CPP_DL_WARNING
,
408 "integer constant is so large that it is unsigned");
409 result
.unsignedp
= true;
416 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
418 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
421 unsigned int shift
= 3 + (base
== 16);
423 cpp_num_part add_high
, add_low
;
425 /* Multiply by 8 or 16. Catching this overflow here means we don't
426 need to worry about add_high overflowing. */
427 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
428 result
.high
= num
.high
<< shift
;
429 result
.low
= num
.low
<< shift
;
430 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
431 result
.unsignedp
= num
.unsignedp
;
435 add_low
= num
.low
<< 1;
436 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
439 add_high
= add_low
= 0;
441 if (add_low
+ digit
< add_low
)
445 if (result
.low
+ add_low
< result
.low
)
447 if (result
.high
+ add_high
< result
.high
)
450 result
.low
+= add_low
;
451 result
.high
+= add_high
;
452 result
.overflow
= overflow
;
454 /* The above code catches overflow of a cpp_num type. This catches
455 overflow of the (possibly shorter) target precision. */
456 num
.low
= result
.low
;
457 num
.high
= result
.high
;
458 result
= num_trim (result
, precision
);
459 if (!num_eq (result
, num
))
460 result
.overflow
= true;
465 /* Handle meeting "defined" in a preprocessor expression. */
467 parse_defined (cpp_reader
*pfile
)
471 cpp_hashnode
*node
= 0;
472 const cpp_token
*token
;
473 cpp_context
*initial_context
= pfile
->context
;
475 /* Don't expand macros. */
476 pfile
->state
.prevent_expansion
++;
478 token
= cpp_get_token (pfile
);
479 if (token
->type
== CPP_OPEN_PAREN
)
482 token
= cpp_get_token (pfile
);
485 if (token
->type
== CPP_NAME
)
487 node
= token
->val
.node
;
488 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
490 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
496 cpp_error (pfile
, CPP_DL_ERROR
,
497 "operator \"defined\" requires an identifier");
498 if (token
->flags
& NAMED_OP
)
503 op
.type
= token
->type
;
504 cpp_error (pfile
, CPP_DL_ERROR
,
505 "(\"%s\" is an alternative token for \"%s\" in C++)",
506 cpp_token_as_text (pfile
, token
),
507 cpp_token_as_text (pfile
, &op
));
513 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
514 cpp_error (pfile
, CPP_DL_WARNING
,
515 "this use of \"defined\" may not be portable");
517 _cpp_mark_macro_used (node
);
519 /* A possible controlling macro of the form #if !defined ().
520 _cpp_parse_expr checks there was no other junk on the line. */
521 pfile
->mi_ind_cmacro
= node
;
524 pfile
->state
.prevent_expansion
--;
526 result
.unsignedp
= false;
528 result
.overflow
= false;
529 result
.low
= node
&& node
->type
== NT_MACRO
;
533 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
534 number or character constant, or the result of the "defined" or "#"
537 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
543 result
.unsignedp
= false;
544 result
.overflow
= false;
549 temp
= cpp_classify_number (pfile
, token
);
550 switch (temp
& CPP_N_CATEGORY
)
553 cpp_error (pfile
, CPP_DL_ERROR
,
554 "floating constant in preprocessor expression");
557 if (!(temp
& CPP_N_IMAGINARY
))
558 return cpp_interpret_integer (pfile
, token
, temp
);
559 cpp_error (pfile
, CPP_DL_ERROR
,
560 "imaginary number in preprocessor expression");
564 /* Error already issued. */
567 result
.high
= result
.low
= 0;
573 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
578 /* Sign-extend the result if necessary. */
579 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
581 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
582 result
.low
|= ~(~(cpp_num_part
) 0
583 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
584 result
.high
= ~(cpp_num_part
) 0;
585 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
591 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
592 return parse_defined (pfile
);
593 else if (CPP_OPTION (pfile
, cplusplus
)
594 && (token
->val
.node
== pfile
->spec_nodes
.n_true
595 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
598 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
604 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
605 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
606 NODE_NAME (token
->val
.node
));
610 default: /* CPP_HASH */
611 _cpp_test_assertion (pfile
, &temp
);
616 result
.unsignedp
= !!unsignedp
;
620 /* Operator precedence and flags table.
622 After an operator is returned from the lexer, if it has priority less
623 than the operator on the top of the stack, we reduce the stack by one
624 operator and repeat the test. Since equal priorities do not reduce,
625 this is naturally right-associative.
627 We handle left-associative operators by decrementing the priority of
628 just-lexed operators by one, but retaining the priority of operators
629 already on the stack.
631 The remaining cases are '(' and ')'. We handle '(' by skipping the
632 reduction phase completely. ')' is given lower priority than
633 everything else, including '(', effectively forcing a reduction of the
634 parenthesized expression. If there is a matching '(', the routine
635 reduce() exits immediately. If the normal exit route sees a ')', then
636 there cannot have been a matching '(' and an error message is output.
638 The parser assumes all shifted operators require a left operand unless
639 the flag NO_L_OPERAND is set. These semantics are automatic; any
640 extra semantics need to be handled with operator-specific code. */
642 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
643 operand changes because of integer promotions. */
644 #define NO_L_OPERAND (1 << 0)
645 #define LEFT_ASSOC (1 << 1)
646 #define CHECK_PROMOTION (1 << 2)
648 /* Operator to priority map. Must be in the same order as the first
649 N entries of enum cpp_ttype. */
650 static const struct cpp_operator
656 /* EQ */ {0, 0}, /* Shouldn't happen. */
657 /* NOT */ {16, NO_L_OPERAND
},
658 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
659 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
660 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
661 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
662 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
663 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
664 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
665 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
666 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
667 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
668 /* RSHIFT */ {13, LEFT_ASSOC
},
669 /* LSHIFT */ {13, LEFT_ASSOC
},
671 /* COMPL */ {16, NO_L_OPERAND
},
672 /* AND_AND */ {6, LEFT_ASSOC
},
673 /* OR_OR */ {5, LEFT_ASSOC
},
675 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
676 /* COMMA */ {2, LEFT_ASSOC
},
677 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
678 /* CLOSE_PAREN */ {0, 0},
680 /* EQ_EQ */ {11, LEFT_ASSOC
},
681 /* NOT_EQ */ {11, LEFT_ASSOC
},
682 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
683 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
684 /* UPLUS */ {16, NO_L_OPERAND
},
685 /* UMINUS */ {16, NO_L_OPERAND
}
688 /* Parse and evaluate a C expression, reading from PFILE.
689 Returns the truth value of the expression.
691 The implementation is an operator precedence parser, i.e. a
692 bottom-up parser, using a stack for not-yet-reduced tokens.
694 The stack base is op_stack, and the current stack pointer is 'top'.
695 There is a stack element for each operator (only), and the most
696 recently pushed operator is 'top->op'. An operand (value) is
697 stored in the 'value' field of the stack element of the operator
700 _cpp_parse_expr (cpp_reader
*pfile
)
702 struct op
*top
= pfile
->op_stack
;
703 unsigned int lex_count
;
704 bool saw_leading_not
, want_value
= true;
706 pfile
->state
.skip_eval
= 0;
708 /* Set up detection of #if ! defined(). */
709 pfile
->mi_ind_cmacro
= 0;
710 saw_leading_not
= false;
713 /* Lowest priority operator prevents further reductions. */
721 op
.token
= cpp_get_token (pfile
);
722 op
.op
= op
.token
->type
;
726 /* These tokens convert into values. */
733 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
734 cpp_token_as_text (pfile
, op
.token
));
736 top
->value
= eval_token (pfile
, op
.token
);
740 saw_leading_not
= lex_count
== 1;
752 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
753 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
754 cpp_token_as_text (pfile
, op
.token
));
758 /* Check we have a value or operator as appropriate. */
759 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
762 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
763 cpp_token_as_text (pfile
, op
.token
));
767 /* We want a number (or expression) and haven't got one.
768 Try to emit a specific diagnostic. */
769 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
770 SYNTAX_ERROR ("missing expression between '(' and ')'");
772 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
773 SYNTAX_ERROR ("#if with no expression");
775 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
776 SYNTAX_ERROR2 ("operator '%s' has no right operand",
777 cpp_token_as_text (pfile
, top
->token
));
778 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
779 /* Complain about missing paren during reduction. */;
781 SYNTAX_ERROR2 ("operator '%s' has no left operand",
782 cpp_token_as_text (pfile
, op
.token
));
785 top
= reduce (pfile
, top
, op
.op
);
789 if (op
.op
== CPP_EOF
)
794 case CPP_CLOSE_PAREN
:
797 if (!num_zerop (top
->value
))
798 pfile
->state
.skip_eval
++;
802 if (num_zerop (top
->value
))
803 pfile
->state
.skip_eval
++;
806 if (top
->op
!= CPP_QUERY
)
807 SYNTAX_ERROR (" ':' without preceding '?'");
808 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
809 pfile
->state
.skip_eval
++;
811 pfile
->state
.skip_eval
--;
818 /* Check for and handle stack overflow. */
819 if (++top
== pfile
->op_limit
)
820 top
= _cpp_expand_op_stack (pfile
);
823 top
->token
= op
.token
;
826 /* The controlling macro expression is only valid if we called lex 3
827 times: <!> <defined expression> and <EOF>. push_conditional ()
828 checks that we are at top-of-file. */
829 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
830 pfile
->mi_ind_cmacro
= 0;
832 if (top
!= pfile
->op_stack
)
834 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in #if");
836 return false; /* Return false on syntax error. */
839 return !num_zerop (top
->value
);
842 /* Reduce the operator / value stack if possible, in preparation for
843 pushing operator OP. Returns NULL on error, otherwise the top of
846 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
850 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
853 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
857 if (op
== CPP_OPEN_PAREN
)
860 /* Decrement the priority of left-associative operators to force a
861 reduction with operators of otherwise equal priority. */
862 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
863 while (prio
< optab
[top
->op
].prio
)
865 if (CPP_OPTION (pfile
, warn_num_sign_change
)
866 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
867 check_promotion (pfile
, top
);
875 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
883 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
884 top
->value
, top
->op
);
892 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
898 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
905 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
909 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
914 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
915 top
->value
, top
->op
);
920 if (!num_zerop (top
->value
))
921 pfile
->state
.skip_eval
--;
922 top
->value
.low
= (!num_zerop (top
->value
)
923 || !num_zerop (top
[1].value
));
925 top
->value
.unsignedp
= false;
926 top
->value
.overflow
= false;
931 if (num_zerop (top
->value
))
932 pfile
->state
.skip_eval
--;
933 top
->value
.low
= (!num_zerop (top
->value
)
934 && !num_zerop (top
[1].value
));
936 top
->value
.unsignedp
= false;
937 top
->value
.overflow
= false;
941 if (op
!= CPP_CLOSE_PAREN
)
943 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' in expression");
947 top
->value
= top
[1].value
;
952 if (!num_zerop (top
->value
))
954 pfile
->state
.skip_eval
--;
955 top
->value
= top
[1].value
;
958 top
->value
= top
[2].value
;
959 top
->value
.unsignedp
= (top
[1].value
.unsignedp
960 || top
[2].value
.unsignedp
);
964 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
972 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
973 cpp_error (pfile
, CPP_DL_PEDWARN
,
974 "integer overflow in preprocessor expression");
977 if (op
== CPP_CLOSE_PAREN
)
979 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
986 /* Returns the position of the old top of stack after expansion. */
988 _cpp_expand_op_stack (cpp_reader
*pfile
)
990 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
991 size_t new_size
= old_size
* 2 + 20;
993 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
994 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
996 return pfile
->op_stack
+ old_size
;
999 /* Emits a warning if the effective sign of either operand of OP
1000 changes because of integer promotions. */
1002 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1004 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1007 if (op
->value
.unsignedp
)
1009 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1010 cpp_error (pfile
, CPP_DL_WARNING
,
1011 "the left operand of \"%s\" changes sign when promoted",
1012 cpp_token_as_text (pfile
, op
->token
));
1014 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1015 cpp_error (pfile
, CPP_DL_WARNING
,
1016 "the right operand of \"%s\" changes sign when promoted",
1017 cpp_token_as_text (pfile
, op
->token
));
1020 /* Clears the unused high order bits of the number pointed to by PNUM. */
1022 num_trim (cpp_num num
, size_t precision
)
1024 if (precision
> PART_PRECISION
)
1026 precision
-= PART_PRECISION
;
1027 if (precision
< PART_PRECISION
)
1028 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1032 if (precision
< PART_PRECISION
)
1033 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1040 /* True iff A (presumed signed) >= 0. */
1042 num_positive (cpp_num num
, size_t precision
)
1044 if (precision
> PART_PRECISION
)
1046 precision
-= PART_PRECISION
;
1047 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1050 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1053 /* Sign extend a number, with PRECISION significant bits and all
1054 others assumed clear, to fill out a cpp_num structure. */
1056 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1060 if (precision
> PART_PRECISION
)
1062 precision
-= PART_PRECISION
;
1063 if (precision
< PART_PRECISION
1064 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1065 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1067 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1069 if (precision
< PART_PRECISION
)
1070 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1071 num
.high
= ~(cpp_num_part
) 0;
1078 /* Returns the negative of NUM. */
1080 num_negate (cpp_num num
, size_t precision
)
1085 num
.high
= ~num
.high
;
1089 num
= num_trim (num
, precision
);
1090 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1095 /* Returns true if A >= B. */
1097 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1101 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1105 /* Both numbers have signed type. If they are of different
1106 sign, the answer is the sign of A. */
1107 unsignedp
= num_positive (pa
, precision
);
1109 if (unsignedp
!= num_positive (pb
, precision
))
1112 /* Otherwise we can do an unsigned comparison. */
1115 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1118 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1120 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1121 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1123 lhs
.overflow
= false;
1124 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1126 /* As excess precision is zeroed, there is no need to num_trim () as
1127 these operations cannot introduce a set bit there. */
1131 lhs
.high
&= rhs
.high
;
1133 else if (op
== CPP_OR
)
1136 lhs
.high
|= rhs
.high
;
1141 lhs
.high
^= rhs
.high
;
1147 /* Returns LHS OP RHS, where OP is an inequality. */
1149 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1152 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1154 if (op
== CPP_GREATER_EQ
)
1156 else if (op
== CPP_LESS
)
1158 else if (op
== CPP_GREATER
)
1159 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1160 else /* CPP_LESS_EQ. */
1161 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1164 lhs
.overflow
= false;
1165 lhs
.unsignedp
= false;
1169 /* Returns LHS OP RHS, where OP is == or !=. */
1171 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1172 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1174 /* Work around a 3.0.4 bug; see PR 6950. */
1175 bool eq
= num_eq (lhs
, rhs
);
1176 if (op
== CPP_NOT_EQ
)
1180 lhs
.overflow
= false;
1181 lhs
.unsignedp
= false;
1185 /* Shift NUM, of width PRECISION, right by N bits. */
1187 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1189 cpp_num_part sign_mask
;
1190 bool x
= num_positive (num
, precision
);
1192 if (num
.unsignedp
|| x
)
1195 sign_mask
= ~(cpp_num_part
) 0;
1198 num
.high
= num
.low
= sign_mask
;
1202 if (precision
< PART_PRECISION
)
1203 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1204 else if (precision
< 2 * PART_PRECISION
)
1205 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1207 if (n
>= PART_PRECISION
)
1209 n
-= PART_PRECISION
;
1211 num
.high
= sign_mask
;
1216 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1217 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1221 num
= num_trim (num
, precision
);
1222 num
.overflow
= false;
1226 /* Shift NUM, of width PRECISION, left by N bits. */
1228 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1232 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1233 num
.high
= num
.low
= 0;
1237 cpp_num orig
, maybe_orig
;
1241 if (m
>= PART_PRECISION
)
1243 m
-= PART_PRECISION
;
1249 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1252 num
= num_trim (num
, precision
);
1255 num
.overflow
= false;
1258 maybe_orig
= num_rshift (num
, precision
, n
);
1259 num
.overflow
= !num_eq (orig
, maybe_orig
);
1266 /* The four unary operators: +, -, ! and ~. */
1268 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1273 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1274 cpp_error (pfile
, CPP_DL_WARNING
,
1275 "traditional C rejects the unary plus operator");
1276 num
.overflow
= false;
1280 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1284 num
.high
= ~num
.high
;
1286 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1287 num
.overflow
= false;
1290 default: /* case CPP_NOT: */
1291 num
.low
= num_zerop (num
);
1293 num
.overflow
= false;
1294 num
.unsignedp
= false;
1301 /* The various binary operators. */
1303 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1306 size_t precision
= CPP_OPTION (pfile
, precision
);
1314 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1316 /* A negative shift is a positive shift the other way. */
1317 if (op
== CPP_LSHIFT
)
1321 rhs
= num_negate (rhs
, precision
);
1324 n
= ~0; /* Maximal. */
1327 if (op
== CPP_LSHIFT
)
1328 lhs
= num_lshift (lhs
, precision
, n
);
1330 lhs
= num_rshift (lhs
, precision
, n
);
1335 rhs
= num_negate (rhs
, precision
);
1337 result
.low
= lhs
.low
+ rhs
.low
;
1338 result
.high
= lhs
.high
+ rhs
.high
;
1339 if (result
.low
< lhs
.low
)
1341 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1342 result
.overflow
= false;
1344 result
= num_trim (result
, precision
);
1345 if (!result
.unsignedp
)
1347 bool lhsp
= num_positive (lhs
, precision
);
1348 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1349 && lhsp
!= num_positive (result
, precision
));
1354 default: /* case CPP_COMMA: */
1355 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1356 || !pfile
->state
.skip_eval
))
1357 cpp_error (pfile
, CPP_DL_PEDWARN
,
1358 "comma operator in operand of #if");
1366 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1369 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1372 cpp_num_part middle
[2], temp
;
1374 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1375 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1377 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1378 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1381 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1382 if (result
.low
< temp
)
1386 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1387 if (result
.low
< temp
)
1390 result
.high
+= HIGH_PART (middle
[0]);
1391 result
.high
+= HIGH_PART (middle
[1]);
1392 result
.unsignedp
= true;
1393 result
.overflow
= false;
1398 /* Multiply two preprocessing numbers. */
1400 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1402 cpp_num result
, temp
;
1403 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1404 bool overflow
, negate
= false;
1405 size_t precision
= CPP_OPTION (pfile
, precision
);
1407 /* Prepare for unsigned multiplication. */
1410 if (!num_positive (lhs
, precision
))
1411 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1412 if (!num_positive (rhs
, precision
))
1413 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1416 overflow
= lhs
.high
&& rhs
.high
;
1417 result
= num_part_mul (lhs
.low
, rhs
.low
);
1419 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1420 result
.high
+= temp
.low
;
1424 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1425 result
.high
+= temp
.low
;
1429 temp
.low
= result
.low
, temp
.high
= result
.high
;
1430 result
= num_trim (result
, precision
);
1431 if (!num_eq (result
, temp
))
1435 result
= num_negate (result
, precision
);
1438 result
.overflow
= false;
1440 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1441 && !num_zerop (result
));
1442 result
.unsignedp
= unsignedp
;
1447 /* Divide two preprocessing numbers, returning the answer or the
1448 remainder depending upon OP. */
1450 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1452 cpp_num result
, sub
;
1454 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1455 bool negate
= false, lhs_neg
= false;
1456 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1458 /* Prepare for unsigned division. */
1461 if (!num_positive (lhs
, precision
))
1462 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1463 if (!num_positive (rhs
, precision
))
1464 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1467 /* Find the high bit. */
1471 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1472 for (; ; i
--, mask
>>= 1)
1473 if (rhs
.high
& mask
)
1478 if (precision
> PART_PRECISION
)
1479 i
= precision
- PART_PRECISION
- 1;
1482 mask
= (cpp_num_part
) 1 << i
;
1483 for (; ; i
--, mask
>>= 1)
1489 if (!pfile
->state
.skip_eval
)
1490 cpp_error (pfile
, CPP_DL_ERROR
, "division by zero in #if");
1494 /* First nonzero bit of RHS is bit I. Do naive division by
1495 shifting the RHS fully left, and subtracting from LHS if LHS is
1496 at least as big, and then repeating but with one less shift.
1497 This is not very efficient, but is easy to understand. */
1499 rhs
.unsignedp
= true;
1500 lhs
.unsignedp
= true;
1501 i
= precision
- i
- 1;
1502 sub
= num_lshift (rhs
, precision
, i
);
1504 result
.high
= result
.low
= 0;
1507 if (num_greater_eq (lhs
, sub
, precision
))
1509 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1510 if (i
>= PART_PRECISION
)
1511 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1513 result
.low
|= (cpp_num_part
) 1 << i
;
1517 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1521 /* We divide so that the remainder has the sign of the LHS. */
1524 result
.unsignedp
= unsignedp
;
1525 result
.overflow
= false;
1529 result
= num_negate (result
, precision
);
1530 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1537 lhs
.unsignedp
= unsignedp
;
1538 lhs
.overflow
= false;
1540 lhs
= num_negate (lhs
, precision
);