1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002 Free Software Foundation.
4 Contributed by Per Bothner, 1994.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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 (CPP_LAST_CPP_OP + 1)
69 #define CPP_UMINUS (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, DL_ERROR, msgid); goto syntax_error; } while(0)
75 #define SYNTAX_ERROR2(msgid, arg) \
76 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
78 /* Subroutine of cpp_classify_number. S points to a float suffix of
79 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
80 flag vector describing the suffix. */
82 interpret_float_suffix (const uchar
*s
, size_t len
)
84 size_t f
= 0, l
= 0, i
= 0;
89 case 'f': case 'F': f
++; break;
90 case 'l': case 'L': l
++; break;
92 case 'j': case 'J': i
++; break;
97 if (f
+ l
> 1 || i
> 1)
100 return ((i
? CPP_N_IMAGINARY
: 0)
102 l
? CPP_N_LARGE
: CPP_N_MEDIUM
));
105 /* Subroutine of cpp_classify_number. S points to an integer suffix
106 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
107 flag vector describing the suffix. */
109 interpret_int_suffix (const uchar
*s
, size_t len
)
118 case 'u': case 'U': u
++; break;
120 case 'j': case 'J': i
++; break;
121 case 'l': case 'L': l
++;
122 /* If there are two Ls, they must be adjacent and the same case. */
123 if (l
== 2 && s
[len
] != s
[len
+ 1])
130 if (l
> 2 || u
> 1 || i
> 1)
133 return ((i
? CPP_N_IMAGINARY
: 0)
134 | (u
? CPP_N_UNSIGNED
: 0)
135 | ((l
== 0) ? CPP_N_SMALL
136 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
139 /* Categorize numeric constants according to their field (integer,
140 floating point, or invalid), radix (decimal, octal, hexadecimal),
141 and type suffixes. */
143 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
145 const uchar
*str
= token
->val
.str
.text
;
147 unsigned int max_digit
, result
, radix
;
148 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
150 /* If the lexer has done its job, length one can only be a single
151 digit. Fast-path this very common case. */
152 if (token
->val
.str
.len
== 1)
153 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
155 limit
= str
+ token
->val
.str
.len
;
156 float_flag
= NOT_FLOAT
;
160 /* First, interpret the radix. */
166 /* Require at least one hex digit to classify it as hex. */
167 if ((*str
== 'x' || *str
== 'X')
168 && (str
[1] == '.' || ISXDIGIT (str
[1])))
175 /* Now scan for a well-formed integer or float. */
178 unsigned int c
= *str
++;
180 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
188 if (float_flag
== NOT_FLOAT
)
189 float_flag
= AFTER_POINT
;
191 SYNTAX_ERROR ("too many decimal points in number");
193 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
194 || (radix
== 16 && (c
== 'p' || c
== 'P')))
196 float_flag
= AFTER_EXPON
;
201 /* Start of suffix. */
207 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
210 if (max_digit
>= radix
)
211 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
213 if (float_flag
!= NOT_FLOAT
)
215 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
216 cpp_error (pfile
, DL_PEDWARN
,
217 "use of C99 hexadecimal floating constant");
219 if (float_flag
== AFTER_EXPON
)
221 if (*str
== '+' || *str
== '-')
224 /* Exponent is decimal, even if string is a hex float. */
226 SYNTAX_ERROR ("exponent has no digits");
230 while (ISDIGIT (*str
));
232 else if (radix
== 16)
233 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
235 result
= interpret_float_suffix (str
, limit
- str
);
238 cpp_error (pfile
, DL_ERROR
,
239 "invalid suffix \"%.*s\" on floating constant",
240 (int) (limit
- str
), str
);
241 return CPP_N_INVALID
;
244 /* Traditional C didn't accept any floating suffixes. */
246 && CPP_WTRADITIONAL (pfile
)
247 && ! cpp_sys_macro_p (pfile
))
248 cpp_error (pfile
, DL_WARNING
,
249 "traditional C rejects the \"%.*s\" suffix",
250 (int) (limit
- str
), str
);
252 result
|= CPP_N_FLOATING
;
256 result
= interpret_int_suffix (str
, limit
- str
);
259 cpp_error (pfile
, DL_ERROR
,
260 "invalid suffix \"%.*s\" on integer constant",
261 (int) (limit
- str
), str
);
262 return CPP_N_INVALID
;
265 /* Traditional C only accepted the 'L' suffix.
266 Suppress warning about 'LL' with -Wno-long-long. */
267 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
269 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
270 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
272 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
273 cpp_error (pfile
, DL_WARNING
,
274 "traditional C rejects the \"%.*s\" suffix",
275 (int) (limit
- str
), str
);
278 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
279 && ! CPP_OPTION (pfile
, c99
)
280 && CPP_OPTION (pfile
, warn_long_long
))
281 cpp_error (pfile
, DL_PEDWARN
, "use of C99 long long integer constant");
283 result
|= CPP_N_INTEGER
;
286 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
287 cpp_error (pfile
, DL_PEDWARN
, "imaginary constants are a GCC extension");
290 result
|= CPP_N_DECIMAL
;
291 else if (radix
== 16)
294 result
|= CPP_N_OCTAL
;
299 return CPP_N_INVALID
;
302 /* cpp_interpret_integer converts an integer constant into a cpp_num,
303 of precision options->precision.
305 We do not provide any interface for decimal->float conversion,
306 because the preprocessor doesn't need it and we don't want to
307 drag in GCC's floating point emulator. */
309 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
312 const uchar
*p
, *end
;
317 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
318 result
.overflow
= false;
320 p
= token
->val
.str
.text
;
321 end
= p
+ token
->val
.str
.len
;
323 /* Common case of a single digit. */
324 if (token
->val
.str
.len
== 1)
325 result
.low
= p
[0] - '0';
329 size_t precision
= CPP_OPTION (pfile
, precision
);
330 unsigned int base
= 10, c
= 0;
331 bool overflow
= false;
333 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
338 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
344 /* We can add a digit to numbers strictly less than this without
345 needing the precision and slowness of double integers. */
346 max
= ~(cpp_num_part
) 0;
347 if (precision
< PART_PRECISION
)
348 max
>>= PART_PRECISION
- precision
;
349 max
= (max
- base
+ 1) / base
+ 1;
355 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
360 /* Strict inequality for when max is set to zero. */
361 if (result
.low
< max
)
362 result
.low
= result
.low
* base
+ c
;
365 result
= append_digit (result
, c
, base
, precision
);
366 overflow
|= result
.overflow
;
372 cpp_error (pfile
, DL_PEDWARN
,
373 "integer constant is too large for its type");
374 /* If too big to be signed, consider it unsigned. Only warn for
375 decimal numbers. Traditional numbers were always signed (but
376 we still honor an explicit U suffix); but we only have
377 traditional semantics in directives. */
378 else if (!result
.unsignedp
379 && !(CPP_OPTION (pfile
, traditional
)
380 && pfile
->state
.in_directive
)
381 && !num_positive (result
, precision
))
384 cpp_error (pfile
, DL_WARNING
,
385 "integer constant is so large that it is unsigned");
386 result
.unsignedp
= true;
393 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
395 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
398 unsigned int shift
= 3 + (base
== 16);
400 cpp_num_part add_high
, add_low
;
402 /* Multiply by 8 or 16. Catching this overflow here means we don't
403 need to worry about add_high overflowing. */
404 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
405 result
.high
= num
.high
<< shift
;
406 result
.low
= num
.low
<< shift
;
407 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
411 add_low
= num
.low
<< 1;
412 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
415 add_high
= add_low
= 0;
417 if (add_low
+ digit
< add_low
)
421 if (result
.low
+ add_low
< result
.low
)
423 if (result
.high
+ add_high
< result
.high
)
426 result
.low
+= add_low
;
427 result
.high
+= add_high
;
429 /* The above code catches overflow of a cpp_num type. This catches
430 overflow of the (possibly shorter) target precision. */
431 num
.low
= result
.low
;
432 num
.high
= result
.high
;
433 result
= num_trim (result
, precision
);
434 if (!num_eq (result
, num
))
437 result
.unsignedp
= num
.unsignedp
;
438 result
.overflow
= overflow
;
442 /* Handle meeting "defined" in a preprocessor expression. */
444 parse_defined (cpp_reader
*pfile
)
448 cpp_hashnode
*node
= 0;
449 const cpp_token
*token
;
450 cpp_context
*initial_context
= pfile
->context
;
452 /* Don't expand macros. */
453 pfile
->state
.prevent_expansion
++;
455 token
= cpp_get_token (pfile
);
456 if (token
->type
== CPP_OPEN_PAREN
)
459 token
= cpp_get_token (pfile
);
462 if (token
->type
== CPP_NAME
)
464 node
= token
->val
.node
;
465 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
467 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
473 cpp_error (pfile
, DL_ERROR
,
474 "operator \"defined\" requires an identifier");
475 if (token
->flags
& NAMED_OP
)
480 op
.type
= token
->type
;
481 cpp_error (pfile
, DL_ERROR
,
482 "(\"%s\" is an alternative token for \"%s\" in C++)",
483 cpp_token_as_text (pfile
, token
),
484 cpp_token_as_text (pfile
, &op
));
490 if (pfile
->context
!= initial_context
)
491 cpp_error (pfile
, DL_WARNING
,
492 "this use of \"defined\" may not be portable");
494 _cpp_mark_macro_used (node
);
496 /* A possible controlling macro of the form #if !defined ().
497 _cpp_parse_expr checks there was no other junk on the line. */
498 pfile
->mi_ind_cmacro
= node
;
501 pfile
->state
.prevent_expansion
--;
503 result
.unsignedp
= false;
505 result
.overflow
= false;
506 result
.low
= node
&& node
->type
== NT_MACRO
;
510 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
511 number or character constant, or the result of the "defined" or "#"
514 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
523 temp
= cpp_classify_number (pfile
, token
);
524 switch (temp
& CPP_N_CATEGORY
)
527 cpp_error (pfile
, DL_ERROR
,
528 "floating constant in preprocessor expression");
531 if (!(temp
& CPP_N_IMAGINARY
))
532 return cpp_interpret_integer (pfile
, token
, temp
);
533 cpp_error (pfile
, DL_ERROR
,
534 "imaginary number in preprocessor expression");
538 /* Error already issued. */
541 result
.high
= result
.low
= 0;
547 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
552 /* Sign-extend the result if necessary. */
553 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
555 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
556 result
.low
|= ~(~(cpp_num_part
) 0
557 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
558 result
.high
= ~(cpp_num_part
) 0;
559 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
565 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
566 return parse_defined (pfile
);
567 else if (CPP_OPTION (pfile
, cplusplus
)
568 && (token
->val
.node
== pfile
->spec_nodes
.n_true
569 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
572 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
578 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
579 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
580 NODE_NAME (token
->val
.node
));
584 default: /* CPP_HASH */
585 _cpp_test_assertion (pfile
, &temp
);
590 result
.unsignedp
= !!unsignedp
;
591 result
.overflow
= false;
595 /* Operator precedence and flags table.
597 After an operator is returned from the lexer, if it has priority less
598 than the operator on the top of the stack, we reduce the stack by one
599 operator and repeat the test. Since equal priorities do not reduce,
600 this is naturally right-associative.
602 We handle left-associative operators by decrementing the priority of
603 just-lexed operators by one, but retaining the priority of operators
604 already on the stack.
606 The remaining cases are '(' and ')'. We handle '(' by skipping the
607 reduction phase completely. ')' is given lower priority than
608 everything else, including '(', effectively forcing a reduction of the
609 parenthesized expression. If there is a matching '(', the routine
610 reduce() exits immediately. If the normal exit route sees a ')', then
611 there cannot have been a matching '(' and an error message is output.
613 The parser assumes all shifted operators require a left operand unless
614 the flag NO_L_OPERAND is set. These semantics are automatic; any
615 extra semantics need to be handled with operator-specific code. */
617 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
618 operand changes because of integer promotions. */
619 #define NO_L_OPERAND (1 << 0)
620 #define LEFT_ASSOC (1 << 1)
621 #define CHECK_PROMOTION (1 << 2)
623 /* Operator to priority map. Must be in the same order as the first
624 N entries of enum cpp_ttype. */
625 static const struct operator
631 /* EQ */ {0, 0}, /* Shouldn't happen. */
632 /* NOT */ {16, NO_L_OPERAND
},
633 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
634 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
635 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
636 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
637 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
638 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
639 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
640 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
641 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
642 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
643 /* RSHIFT */ {13, LEFT_ASSOC
},
644 /* LSHIFT */ {13, LEFT_ASSOC
},
646 /* MIN */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
647 /* MAX */ {10, LEFT_ASSOC
| CHECK_PROMOTION
},
649 /* COMPL */ {16, NO_L_OPERAND
},
650 /* AND_AND */ {6, LEFT_ASSOC
},
651 /* OR_OR */ {5, LEFT_ASSOC
},
653 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
654 /* COMMA */ {2, LEFT_ASSOC
},
655 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
656 /* CLOSE_PAREN */ {0, 0},
658 /* EQ_EQ */ {11, LEFT_ASSOC
},
659 /* NOT_EQ */ {11, LEFT_ASSOC
},
660 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
661 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
662 /* UPLUS */ {16, NO_L_OPERAND
},
663 /* UMINUS */ {16, NO_L_OPERAND
}
666 /* Parse and evaluate a C expression, reading from PFILE.
667 Returns the truth value of the expression.
669 The implementation is an operator precedence parser, i.e. a
670 bottom-up parser, using a stack for not-yet-reduced tokens.
672 The stack base is op_stack, and the current stack pointer is 'top'.
673 There is a stack element for each operator (only), and the most
674 recently pushed operator is 'top->op'. An operand (value) is
675 stored in the 'value' field of the stack element of the operator
678 _cpp_parse_expr (cpp_reader
*pfile
)
680 struct op
*top
= pfile
->op_stack
;
681 unsigned int lex_count
;
682 bool saw_leading_not
, want_value
= true;
684 pfile
->state
.skip_eval
= 0;
686 /* Set up detection of #if ! defined(). */
687 pfile
->mi_ind_cmacro
= 0;
688 saw_leading_not
= false;
691 /* Lowest priority operator prevents further reductions. */
699 op
.token
= cpp_get_token (pfile
);
700 op
.op
= op
.token
->type
;
704 /* These tokens convert into values. */
711 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
712 cpp_token_as_text (pfile
, op
.token
));
714 top
->value
= eval_token (pfile
, op
.token
);
718 saw_leading_not
= lex_count
== 1;
730 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
731 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
732 cpp_token_as_text (pfile
, op
.token
));
736 /* Check we have a value or operator as appropriate. */
737 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
740 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
741 cpp_token_as_text (pfile
, op
.token
));
745 /* Ordering here is subtle and intended to favor the
746 missing parenthesis diagnostics over alternatives. */
747 if (op
.op
== CPP_CLOSE_PAREN
)
749 if (top
->op
== CPP_OPEN_PAREN
)
750 SYNTAX_ERROR ("void expression between '(' and ')'");
752 else if (top
->op
== CPP_EOF
)
753 SYNTAX_ERROR ("#if with no expression");
754 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
755 SYNTAX_ERROR2 ("operator '%s' has no right operand",
756 cpp_token_as_text (pfile
, top
->token
));
759 top
= reduce (pfile
, top
, op
.op
);
763 if (op
.op
== CPP_EOF
)
768 case CPP_CLOSE_PAREN
:
771 if (!num_zerop (top
->value
))
772 pfile
->state
.skip_eval
++;
776 if (num_zerop (top
->value
))
777 pfile
->state
.skip_eval
++;
780 if (top
->op
!= CPP_QUERY
)
781 SYNTAX_ERROR (" ':' without preceding '?'");
782 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
783 pfile
->state
.skip_eval
++;
785 pfile
->state
.skip_eval
--;
792 /* Check for and handle stack overflow. */
793 if (++top
== pfile
->op_limit
)
794 top
= _cpp_expand_op_stack (pfile
);
797 top
->token
= op
.token
;
800 /* The controlling macro expression is only valid if we called lex 3
801 times: <!> <defined expression> and <EOF>. push_conditional ()
802 checks that we are at top-of-file. */
803 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
804 pfile
->mi_ind_cmacro
= 0;
806 if (top
!= pfile
->op_stack
)
808 cpp_error (pfile
, DL_ICE
, "unbalanced stack in #if");
810 return false; /* Return false on syntax error. */
813 return !num_zerop (top
->value
);
816 /* Reduce the operator / value stack if possible, in preparation for
817 pushing operator OP. Returns NULL on error, otherwise the top of
820 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
824 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
827 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
831 if (op
== CPP_OPEN_PAREN
)
834 /* Decrement the priority of left-associative operators to force a
835 reduction with operators of otherwise equal priority. */
836 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
837 while (prio
< optab
[top
->op
].prio
)
839 if (CPP_OPTION (pfile
, warn_num_sign_change
)
840 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
841 check_promotion (pfile
, top
);
849 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
859 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
860 top
->value
, top
->op
);
868 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
874 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
881 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
885 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
890 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
891 top
->value
, top
->op
);
896 if (!num_zerop (top
->value
))
897 pfile
->state
.skip_eval
--;
898 top
->value
.low
= (!num_zerop (top
->value
)
899 || !num_zerop (top
[1].value
));
901 top
->value
.unsignedp
= false;
902 top
->value
.overflow
= false;
907 if (num_zerop (top
->value
))
908 pfile
->state
.skip_eval
--;
909 top
->value
.low
= (!num_zerop (top
->value
)
910 && !num_zerop (top
[1].value
));
912 top
->value
.unsignedp
= false;
913 top
->value
.overflow
= false;
917 if (op
!= CPP_CLOSE_PAREN
)
919 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
923 top
->value
= top
[1].value
;
928 if (!num_zerop (top
->value
))
930 pfile
->state
.skip_eval
--;
931 top
->value
= top
[1].value
;
934 top
->value
= top
[2].value
;
935 top
->value
.unsignedp
= (top
[1].value
.unsignedp
936 || top
[2].value
.unsignedp
);
940 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
948 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
949 cpp_error (pfile
, DL_PEDWARN
,
950 "integer overflow in preprocessor expression");
953 if (op
== CPP_CLOSE_PAREN
)
955 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
962 /* Returns the position of the old top of stack after expansion. */
964 _cpp_expand_op_stack (cpp_reader
*pfile
)
966 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
967 size_t new_size
= old_size
* 2 + 20;
969 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
970 new_size
* sizeof (struct op
));
971 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
973 return pfile
->op_stack
+ old_size
;
976 /* Emits a warning if the effective sign of either operand of OP
977 changes because of integer promotions. */
979 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
981 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
984 if (op
->value
.unsignedp
)
986 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
987 cpp_error (pfile
, DL_WARNING
,
988 "the left operand of \"%s\" changes sign when promoted",
989 cpp_token_as_text (pfile
, op
->token
));
991 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
992 cpp_error (pfile
, DL_WARNING
,
993 "the right operand of \"%s\" changes sign when promoted",
994 cpp_token_as_text (pfile
, op
->token
));
997 /* Clears the unused high order bits of the number pointed to by PNUM. */
999 num_trim (cpp_num num
, size_t precision
)
1001 if (precision
> PART_PRECISION
)
1003 precision
-= PART_PRECISION
;
1004 if (precision
< PART_PRECISION
)
1005 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1009 if (precision
< PART_PRECISION
)
1010 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1017 /* True iff A (presumed signed) >= 0. */
1019 num_positive (cpp_num num
, size_t precision
)
1021 if (precision
> PART_PRECISION
)
1023 precision
-= PART_PRECISION
;
1024 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1027 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1030 /* Sign extend a number, with PRECISION significant bits and all
1031 others assumed clear, to fill out a cpp_num structure. */
1033 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1037 if (precision
> PART_PRECISION
)
1039 precision
-= PART_PRECISION
;
1040 if (precision
< PART_PRECISION
1041 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1042 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1044 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1046 if (precision
< PART_PRECISION
)
1047 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1048 num
.high
= ~(cpp_num_part
) 0;
1055 /* Returns the negative of NUM. */
1057 num_negate (cpp_num num
, size_t precision
)
1062 num
.high
= ~num
.high
;
1066 num
= num_trim (num
, precision
);
1067 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1072 /* Returns true if A >= B. */
1074 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1078 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1082 /* Both numbers have signed type. If they are of different
1083 sign, the answer is the sign of A. */
1084 unsignedp
= num_positive (pa
, precision
);
1086 if (unsignedp
!= num_positive (pb
, precision
))
1089 /* Otherwise we can do an unsigned comparison. */
1092 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1095 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1097 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1098 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1100 lhs
.overflow
= false;
1101 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1103 /* As excess precision is zeroed, there is no need to num_trim () as
1104 these operations cannot introduce a set bit there. */
1108 lhs
.high
&= rhs
.high
;
1110 else if (op
== CPP_OR
)
1113 lhs
.high
|= rhs
.high
;
1118 lhs
.high
^= rhs
.high
;
1124 /* Returns LHS OP RHS, where OP is an inequality. */
1126 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1129 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1131 if (op
== CPP_GREATER_EQ
)
1133 else if (op
== CPP_LESS
)
1135 else if (op
== CPP_GREATER
)
1136 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1137 else /* CPP_LESS_EQ. */
1138 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1141 lhs
.overflow
= false;
1142 lhs
.unsignedp
= false;
1146 /* Returns LHS OP RHS, where OP is == or !=. */
1148 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1149 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1151 /* Work around a 3.0.4 bug; see PR 6950. */
1152 bool eq
= num_eq (lhs
, rhs
);
1153 if (op
== CPP_NOT_EQ
)
1157 lhs
.overflow
= false;
1158 lhs
.unsignedp
= false;
1162 /* Shift NUM, of width PRECISION, right by N bits. */
1164 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1166 cpp_num_part sign_mask
;
1168 if (num
.unsignedp
|| num_positive (num
, precision
))
1171 sign_mask
= ~(cpp_num_part
) 0;
1174 num
.high
= num
.low
= sign_mask
;
1178 if (precision
< PART_PRECISION
)
1179 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1180 else if (precision
< 2 * PART_PRECISION
)
1181 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1183 if (n
>= PART_PRECISION
)
1185 n
-= PART_PRECISION
;
1187 num
.high
= sign_mask
;
1192 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1193 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1197 num
= num_trim (num
, precision
);
1198 num
.overflow
= false;
1202 /* Shift NUM, of width PRECISION, left by N bits. */
1204 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1208 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1209 num
.high
= num
.low
= 0;
1213 cpp_num orig
, maybe_orig
;
1217 if (m
>= PART_PRECISION
)
1219 m
-= PART_PRECISION
;
1225 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1228 num
= num_trim (num
, precision
);
1231 num
.overflow
= false;
1234 maybe_orig
= num_rshift (num
, precision
, n
);
1235 num
.overflow
= !num_eq (orig
, maybe_orig
);
1242 /* The four unary operators: +, -, ! and ~. */
1244 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1249 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1250 cpp_error (pfile
, DL_WARNING
,
1251 "traditional C rejects the unary plus operator");
1252 num
.overflow
= false;
1256 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1260 num
.high
= ~num
.high
;
1262 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1263 num
.overflow
= false;
1266 default: /* case CPP_NOT: */
1267 num
.low
= num_zerop (num
);
1269 num
.overflow
= false;
1270 num
.unsignedp
= false;
1277 /* The various binary operators. */
1279 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1282 size_t precision
= CPP_OPTION (pfile
, precision
);
1291 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1293 /* A negative shift is a positive shift the other way. */
1294 if (op
== CPP_LSHIFT
)
1298 rhs
= num_negate (rhs
, precision
);
1301 n
= ~0; /* Maximal. */
1304 if (op
== CPP_LSHIFT
)
1305 lhs
= num_lshift (lhs
, precision
, n
);
1307 lhs
= num_rshift (lhs
, precision
, n
);
1314 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1316 gte
= num_greater_eq (lhs
, rhs
, precision
);
1321 lhs
.unsignedp
= unsignedp
;
1327 rhs
= num_negate (rhs
, precision
);
1329 result
.low
= lhs
.low
+ rhs
.low
;
1330 result
.high
= lhs
.high
+ rhs
.high
;
1331 if (result
.low
< lhs
.low
)
1334 result
= num_trim (result
, precision
);
1335 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1336 if (result
.unsignedp
)
1337 result
.overflow
= false;
1340 bool lhsp
= num_positive (lhs
, precision
);
1341 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1342 && lhsp
!= num_positive (result
, precision
));
1347 default: /* case CPP_COMMA: */
1348 if (CPP_PEDANTIC (pfile
) && !pfile
->state
.skip_eval
)
1349 cpp_error (pfile
, DL_PEDWARN
,
1350 "comma operator in operand of #if");
1358 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1361 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1364 cpp_num_part middle
[2], temp
;
1366 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1367 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1369 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1370 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1373 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1374 if (result
.low
< temp
)
1378 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1379 if (result
.low
< temp
)
1382 result
.high
+= HIGH_PART (middle
[0]);
1383 result
.high
+= HIGH_PART (middle
[1]);
1384 result
.unsignedp
= 1;
1389 /* Multiply two preprocessing numbers. */
1391 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1393 cpp_num result
, temp
;
1394 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1395 bool overflow
, negate
= false;
1396 size_t precision
= CPP_OPTION (pfile
, precision
);
1398 /* Prepare for unsigned multiplication. */
1401 if (!num_positive (lhs
, precision
))
1402 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1403 if (!num_positive (rhs
, precision
))
1404 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1407 overflow
= lhs
.high
&& rhs
.high
;
1408 result
= num_part_mul (lhs
.low
, rhs
.low
);
1410 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1411 result
.high
+= temp
.low
;
1415 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1416 result
.high
+= temp
.low
;
1420 temp
.low
= result
.low
, temp
.high
= result
.high
;
1421 result
= num_trim (result
, precision
);
1422 if (!num_eq (result
, temp
))
1426 result
= num_negate (result
, precision
);
1429 result
.overflow
= false;
1431 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1432 && !num_zerop (result
));
1433 result
.unsignedp
= unsignedp
;
1438 /* Divide two preprocessing numbers, returning the answer or the
1439 remainder depending upon OP. */
1441 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1443 cpp_num result
, sub
;
1445 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1446 bool negate
= false, lhs_neg
= false;
1447 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1449 /* Prepare for unsigned division. */
1452 if (!num_positive (lhs
, precision
))
1453 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1454 if (!num_positive (rhs
, precision
))
1455 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1458 /* Find the high bit. */
1462 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1463 for (; ; i
--, mask
>>= 1)
1464 if (rhs
.high
& mask
)
1469 if (precision
> PART_PRECISION
)
1470 i
= precision
- PART_PRECISION
- 1;
1473 mask
= (cpp_num_part
) 1 << i
;
1474 for (; ; i
--, mask
>>= 1)
1480 if (!pfile
->state
.skip_eval
)
1481 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1485 /* First nonzero bit of RHS is bit I. Do naive division by
1486 shifting the RHS fully left, and subtracting from LHS if LHS is
1487 at least as big, and then repeating but with one less shift.
1488 This is not very efficient, but is easy to understand. */
1490 rhs
.unsignedp
= true;
1491 lhs
.unsignedp
= true;
1492 i
= precision
- i
- 1;
1493 sub
= num_lshift (rhs
, precision
, i
);
1495 result
.high
= result
.low
= 0;
1498 if (num_greater_eq (lhs
, sub
, precision
))
1500 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1501 if (i
>= PART_PRECISION
)
1502 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1504 result
.low
|= (cpp_num_part
) 1 << i
;
1508 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1512 /* We divide so that the remainder has the sign of the LHS. */
1515 result
.unsignedp
= unsignedp
;
1517 result
.overflow
= false;
1521 result
= num_negate (result
, precision
);
1522 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1529 lhs
.unsignedp
= unsignedp
;
1530 lhs
.overflow
= false;
1532 lhs
= num_negate (lhs
, precision
);