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 cpp_num value
; /* The value logically "right" of op. */
37 /* Some simple utility routines on double integers. */
38 #define num_zerop(num) ((num.low | num.high) == 0)
39 #define num_eq(num1, num2) (num1.low == num2.low && num1.high == num2.high)
40 static bool num_positive
PARAMS ((cpp_num
, size_t));
41 static bool num_greater_eq
PARAMS ((cpp_num
, cpp_num
, size_t));
42 static cpp_num num_trim
PARAMS ((cpp_num
, size_t));
43 static cpp_num num_part_mul
PARAMS ((cpp_num_part
, cpp_num_part
));
45 static cpp_num num_unary_op
PARAMS ((cpp_reader
*, cpp_num
, enum cpp_ttype
));
46 static cpp_num num_binary_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
48 static cpp_num num_negate
PARAMS ((cpp_num
, size_t));
49 static cpp_num num_bitwise_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
51 static cpp_num num_inequality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
53 static cpp_num num_equality_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
55 static cpp_num num_mul
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
57 static cpp_num num_div_op
PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
59 static cpp_num num_lshift
PARAMS ((cpp_num
, size_t, size_t));
60 static cpp_num num_rshift
PARAMS ((cpp_num
, size_t, size_t));
62 static cpp_num append_digit
PARAMS ((cpp_num
, int, int, size_t));
63 static cpp_num parse_defined
PARAMS ((cpp_reader
*));
64 static cpp_num eval_token
PARAMS ((cpp_reader
*, const cpp_token
*));
65 static struct op
*reduce
PARAMS ((cpp_reader
*, struct op
*, enum cpp_ttype
));
66 static unsigned int interpret_float_suffix
PARAMS ((const uchar
*, size_t));
67 static unsigned int interpret_int_suffix
PARAMS ((const uchar
*, size_t));
69 /* Token type abuse to create unary plus and minus operators. */
70 #define CPP_UPLUS (CPP_LAST_CPP_OP + 1)
71 #define CPP_UMINUS (CPP_LAST_CPP_OP + 2)
73 /* With -O2, gcc appears to produce nice code, moving the error
74 message load and subsequent jump completely out of the main path. */
75 #define SYNTAX_ERROR(msgid) \
76 do { cpp_error (pfile, DL_ERROR, msgid); goto syntax_error; } while(0)
77 #define SYNTAX_ERROR2(msgid, arg) \
78 do { cpp_error (pfile, DL_ERROR, msgid, arg); goto syntax_error; } while(0)
80 /* Subroutine of cpp_classify_number. S points to a float suffix of
81 length LEN, possibly zero. Returns 0 for an invalid suffix, or a
82 flag vector describing the suffix. */
84 interpret_float_suffix (s
, len
)
88 size_t f
= 0, l
= 0, i
= 0;
93 case 'f': case 'F': f
++; break;
94 case 'l': case 'L': l
++; break;
96 case 'j': case 'J': i
++; break;
101 if (f
+ l
> 1 || i
> 1)
104 return ((i
? CPP_N_IMAGINARY
: 0)
106 l
? CPP_N_LARGE
: CPP_N_MEDIUM
));
109 /* Subroutine of cpp_classify_number. S points to an integer suffix
110 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
111 flag vector describing the suffix. */
113 interpret_int_suffix (s
, len
)
124 case 'u': case 'U': u
++; break;
126 case 'j': case 'J': i
++; break;
127 case 'l': case 'L': l
++;
128 /* If there are two Ls, they must be adjacent and the same case. */
129 if (l
== 2 && s
[len
] != s
[len
+ 1])
136 if (l
> 2 || u
> 1 || i
> 1)
139 return ((i
? CPP_N_IMAGINARY
: 0)
140 | (u
? CPP_N_UNSIGNED
: 0)
141 | ((l
== 0) ? CPP_N_SMALL
142 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
145 /* Categorize numeric constants according to their field (integer,
146 floating point, or invalid), radix (decimal, octal, hexadecimal),
147 and type suffixes. */
149 cpp_classify_number (pfile
, token
)
151 const cpp_token
*token
;
153 const uchar
*str
= token
->val
.str
.text
;
155 unsigned int max_digit
, result
, radix
;
156 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
158 /* If the lexer has done its job, length one can only be a single
159 digit. Fast-path this very common case. */
160 if (token
->val
.str
.len
== 1)
161 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
163 limit
= str
+ token
->val
.str
.len
;
164 float_flag
= NOT_FLOAT
;
168 /* First, interpret the radix. */
174 /* Require at least one hex digit to classify it as hex. */
175 if ((*str
== 'x' || *str
== 'X') && ISXDIGIT (str
[1]))
182 /* Now scan for a well-formed integer or float. */
185 unsigned int c
= *str
++;
187 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
195 if (float_flag
== NOT_FLOAT
)
196 float_flag
= AFTER_POINT
;
198 SYNTAX_ERROR ("too many decimal points in number");
200 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
201 || (radix
== 16 && (c
== 'p' || c
== 'P')))
203 float_flag
= AFTER_EXPON
;
208 /* Start of suffix. */
214 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
217 if (max_digit
>= radix
)
218 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
220 if (float_flag
!= NOT_FLOAT
)
222 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
223 cpp_error (pfile
, DL_PEDWARN
,
224 "use of C99 hexadecimal floating constant");
226 if (float_flag
== AFTER_EXPON
)
228 if (*str
== '+' || *str
== '-')
231 /* Exponent is decimal, even if string is a hex float. */
233 SYNTAX_ERROR ("exponent has no digits");
237 while (ISDIGIT (*str
));
239 else if (radix
== 16)
240 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
242 result
= interpret_float_suffix (str
, limit
- str
);
245 cpp_error (pfile
, DL_ERROR
,
246 "invalid suffix \"%.*s\" on floating constant",
247 (int) (limit
- str
), str
);
248 return CPP_N_INVALID
;
251 /* Traditional C didn't accept any floating suffixes. */
253 && CPP_WTRADITIONAL (pfile
)
254 && ! cpp_sys_macro_p (pfile
))
255 cpp_error (pfile
, DL_WARNING
,
256 "traditional C rejects the \"%.*s\" suffix",
257 (int) (limit
- str
), str
);
259 result
|= CPP_N_FLOATING
;
263 result
= interpret_int_suffix (str
, limit
- str
);
266 cpp_error (pfile
, DL_ERROR
,
267 "invalid suffix \"%.*s\" on integer constant",
268 (int) (limit
- str
), str
);
269 return CPP_N_INVALID
;
272 /* Traditional C only accepted the 'L' suffix. */
273 if (result
!= CPP_N_SMALL
&& result
!= CPP_N_MEDIUM
274 && CPP_WTRADITIONAL (pfile
)
275 && ! cpp_sys_macro_p (pfile
))
276 cpp_error (pfile
, DL_WARNING
,
277 "traditional C rejects the \"%.*s\" suffix",
278 (int) (limit
- str
), str
);
280 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
281 && ! CPP_OPTION (pfile
, c99
)
282 && CPP_OPTION (pfile
, warn_long_long
))
283 cpp_error (pfile
, DL_PEDWARN
, "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
, DL_PEDWARN
, "imaginary constants are a GCC extension");
292 result
|= CPP_N_DECIMAL
;
293 else if (radix
== 16)
296 result
|= CPP_N_OCTAL
;
301 return CPP_N_INVALID
;
304 /* cpp_interpret_integer converts an integer constant into a cpp_num,
305 of precision options->precision.
307 We do not provide any interface for decimal->float conversion,
308 because the preprocessor doesn't need it and the floating point
309 handling in GCC proper is too ugly to speak of. */
311 cpp_interpret_integer (pfile
, token
, type
)
313 const cpp_token
*token
;
316 const uchar
*p
, *end
;
321 result
.unsignedp
= type
& CPP_N_UNSIGNED
;
324 p
= token
->val
.str
.text
;
325 end
= p
+ token
->val
.str
.len
;
327 /* Common case of a single digit. */
328 if (token
->val
.str
.len
== 1)
329 result
.low
= p
[0] - '0';
333 size_t precision
= CPP_OPTION (pfile
, precision
);
334 unsigned int base
= 10, c
= 0;
335 bool overflow
= false;
337 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
342 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
348 /* We can add a digit to numbers strictly less than this without
349 needing the precision and slowness of double integers. */
350 max
= ~(cpp_num_part
) 0;
351 if (precision
< PART_PRECISION
)
352 max
>>= PART_PRECISION
- precision
;
353 max
= (max
- base
+ 1) / base
+ 1;
359 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
364 /* Strict inequality for when max is set to zero. */
365 if (result
.low
< max
)
366 result
.low
= result
.low
* base
+ c
;
369 result
= append_digit (result
, c
, base
, precision
);
370 overflow
|= result
.overflow
;
376 cpp_error (pfile
, DL_PEDWARN
,
377 "integer constant is too large for its type");
378 else if (!result
.unsignedp
&& !num_positive (result
, precision
))
380 /* If too big to be signed, consider it unsigned. Only warn
381 for decimal numbers. */
383 cpp_error (pfile
, DL_WARNING
,
384 "integer constant is so large that it is unsigned");
385 result
.unsignedp
= 1;
392 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
395 append_digit (num
, digit
, base
, 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
);
414 add_low
= num
.low
<< 1;
415 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
418 add_high
= add_low
= 0;
420 if (add_low
+ digit
< add_low
)
424 if (result
.low
+ add_low
< result
.low
)
426 if (result
.high
+ add_high
< result
.high
)
429 result
.low
+= add_low
;
430 result
.high
+= add_high
;
432 /* The above code catches overflow of a cpp_num type. This catches
433 overflow of the (possibly shorter) target precision. */
434 num
.low
= result
.low
;
435 num
.high
= result
.high
;
436 result
= num_trim (result
, precision
);
437 if (!num_eq (result
, num
))
440 result
.unsignedp
= num
.unsignedp
;
441 result
.overflow
= overflow
;
445 /* Handle meeting "defined" in a preprocessor expression. */
447 parse_defined (pfile
)
452 cpp_hashnode
*node
= 0;
453 const cpp_token
*token
;
454 cpp_context
*initial_context
= pfile
->context
;
456 /* Don't expand macros. */
457 pfile
->state
.prevent_expansion
++;
459 token
= cpp_get_token (pfile
);
460 if (token
->type
== CPP_OPEN_PAREN
)
463 token
= cpp_get_token (pfile
);
466 if (token
->type
== CPP_NAME
)
468 node
= token
->val
.node
;
469 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
471 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
477 cpp_error (pfile
, DL_ERROR
,
478 "operator \"defined\" requires an identifier");
479 if (token
->flags
& NAMED_OP
)
484 op
.type
= token
->type
;
485 cpp_error (pfile
, DL_ERROR
,
486 "(\"%s\" is an alternative token for \"%s\" in C++)",
487 cpp_token_as_text (pfile
, token
),
488 cpp_token_as_text (pfile
, &op
));
494 if (pfile
->context
!= initial_context
)
495 cpp_error (pfile
, DL_WARNING
,
496 "this use of \"defined\" may not be portable");
498 /* A possible controlling macro of the form #if !defined ().
499 _cpp_parse_expr checks there was no other junk on the line. */
500 pfile
->mi_ind_cmacro
= node
;
503 pfile
->state
.prevent_expansion
--;
505 result
.unsignedp
= 0;
508 result
.low
= node
&& node
->type
== NT_MACRO
;
512 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
513 number or character constant, or the result of the "defined" or "#"
516 eval_token (pfile
, token
)
518 const cpp_token
*token
;
527 temp
= cpp_classify_number (pfile
, token
);
528 switch (temp
& CPP_N_CATEGORY
)
531 cpp_error (pfile
, DL_ERROR
,
532 "floating constant in preprocessor expression");
535 if (!(temp
& CPP_N_IMAGINARY
))
536 return cpp_interpret_integer (pfile
, token
, temp
);
537 cpp_error (pfile
, DL_ERROR
,
538 "imaginary number in preprocessor expression");
542 /* Error already issued. */
545 result
.high
= result
.low
= 0;
551 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
556 /* Sign-extend the result if necessary. */
557 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
559 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
560 result
.low
|= ~(~(cpp_num_part
) 0
561 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
562 result
.high
= ~(cpp_num_part
) 0;
563 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
569 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
570 return parse_defined (pfile
);
571 else if (CPP_OPTION (pfile
, cplusplus
)
572 && (token
->val
.node
== pfile
->spec_nodes
.n_true
573 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
576 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
578 /* Warn about use of true or false in #if when pedantic
579 and stdbool.h has not been included. */
580 if (CPP_PEDANTIC (pfile
)
581 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
582 cpp_error (pfile
, DL_PEDWARN
,
583 "ISO C++ does not permit \"%s\" in #if",
584 NODE_NAME (token
->val
.node
));
590 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
591 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
592 NODE_NAME (token
->val
.node
));
596 default: /* CPP_HASH */
597 _cpp_test_assertion (pfile
, &temp
);
602 result
.unsignedp
= unsignedp
;
607 /* Operator precedence and flags table.
609 After an operator is returned from the lexer, if it has priority less
610 than the operator on the top of the stack, we reduce the stack by one
611 operator and repeat the test. Since equal priorities do not reduce,
612 this is naturally right-associative.
614 We handle left-associative operators by decrementing the priority of
615 just-lexed operators by one, but retaining the priority of operators
616 already on the stack.
618 The remaining cases are '(' and ')'. We handle '(' by skipping the
619 reduction phase completely. ')' is given lower priority than
620 everything else, including '(', effectively forcing a reduction of the
621 parenthesised expression. If there is a matching '(', the routine
622 reduce() exits immediately. If the normal exit route sees a ')', then
623 there cannot have been a matching '(' and an error message is output.
625 The parser assumes all shifted operators require a left operand unless
626 the flag NO_L_OPERAND is set. These semantics are automatic; any
627 extra semantics need to be handled with operator-specific code. */
630 #define NO_L_OPERAND (1 << 0)
631 #define LEFT_ASSOC (1 << 1)
634 #define UNARY (1 << 0)
635 #define BINARY (1 << 1)
636 #define OTHER (1 << 2)
638 typedef cpp_num (*binary_handler
) PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
640 /* Operator to priority map. Must be in the same order as the first
641 N entries of enum cpp_ttype. */
642 static const struct operator
647 binary_handler handler
;
650 /* EQ */ {0, 0, OTHER
, NULL
}, /* Shouldn't happen. */
651 /* NOT */ {16, NO_L_OPERAND
, UNARY
, NULL
},
652 /* GREATER */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
653 /* LESS */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
654 /* PLUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
655 /* MINUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
656 /* MULT */ {15, LEFT_ASSOC
, BINARY
, num_mul
},
657 /* DIV */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
658 /* MOD */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
659 /* AND */ {9, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
660 /* OR */ {7, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
661 /* XOR */ {8, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
662 /* RSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
663 /* LSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
665 /* MIN */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
666 /* MAX */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
668 /* COMPL */ {16, NO_L_OPERAND
, UNARY
, NULL
},
669 /* AND_AND */ {6, LEFT_ASSOC
, OTHER
, NULL
},
670 /* OR_OR */ {5, LEFT_ASSOC
, OTHER
, NULL
},
671 /* QUERY */ {3, 0, OTHER
, NULL
},
672 /* COLON */ {4, LEFT_ASSOC
, OTHER
, NULL
},
673 /* COMMA */ {2, LEFT_ASSOC
, BINARY
, num_binary_op
},
674 /* OPEN_PAREN */ {1, NO_L_OPERAND
, OTHER
, NULL
},
675 /* CLOSE_PAREN */ {0, 0, OTHER
, NULL
},
676 /* EOF */ {0, 0, OTHER
, NULL
},
677 /* EQ_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
678 /* NOT_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
679 /* GREATER_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
680 /* LESS_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
681 /* UPLUS */ {16, NO_L_OPERAND
, UNARY
, NULL
},
682 /* UMINUS */ {16, NO_L_OPERAND
, UNARY
, NULL
}
685 /* Parse and evaluate a C expression, reading from PFILE.
686 Returns the truth value of the expression.
688 The implementation is an operator precedence parser, i.e. a
689 bottom-up parser, using a stack for not-yet-reduced tokens.
691 The stack base is op_stack, and the current stack pointer is 'top'.
692 There is a stack element for each operator (only), and the most
693 recently pushed operator is 'top->op'. An operand (value) is
694 stored in the 'value' field of the stack element of the operator
697 _cpp_parse_expr (pfile
)
700 struct op
*top
= pfile
->op_stack
;
701 const cpp_token
*token
= NULL
, *prev_token
;
702 unsigned int lex_count
;
703 bool saw_leading_not
, want_value
= true;
705 pfile
->state
.skip_eval
= 0;
707 /* Set up detection of #if ! defined(). */
708 pfile
->mi_ind_cmacro
= 0;
709 saw_leading_not
= false;
712 /* Lowest priority operator prevents further reductions. */
720 token
= cpp_get_token (pfile
);
726 /* These tokens convert into values. */
733 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
734 cpp_token_as_text (pfile
, token
));
736 top
->value
= eval_token (pfile
, token
);
740 saw_leading_not
= lex_count
== 1;
751 if (ISGRAPH (token
->val
.c
))
752 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
754 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
757 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
758 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
759 cpp_token_as_text (pfile
, token
));
763 /* Check we have a value or operator as appropriate. */
764 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
767 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
768 cpp_token_as_text (pfile
, token
));
772 /* Ordering here is subtle and intended to favour the
773 missing parenthesis diagnostics over alternatives. */
774 if (op
.op
== CPP_CLOSE_PAREN
)
776 if (top
->op
== CPP_OPEN_PAREN
)
777 SYNTAX_ERROR ("void expression between '(' and ')'");
779 else if (top
->op
== CPP_EOF
)
780 SYNTAX_ERROR ("#if with no expression");
781 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
782 SYNTAX_ERROR2 ("operator '%s' has no right operand",
783 cpp_token_as_text (pfile
, prev_token
));
786 top
= reduce (pfile
, top
, op
.op
);
790 if (op
.op
== CPP_EOF
)
795 case CPP_CLOSE_PAREN
:
798 if (!num_zerop (top
->value
))
799 pfile
->state
.skip_eval
++;
803 if (num_zerop (top
->value
))
804 pfile
->state
.skip_eval
++;
807 if (top
->op
!= CPP_QUERY
)
808 SYNTAX_ERROR (" ':' without preceding '?'");
809 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
810 pfile
->state
.skip_eval
++;
812 pfile
->state
.skip_eval
--;
819 /* Check for and handle stack overflow. */
820 if (++top
== pfile
->op_limit
)
821 top
= _cpp_expand_op_stack (pfile
);
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
, 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 (pfile
, top
, op
)
853 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
856 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
860 if (op
== CPP_OPEN_PAREN
)
863 /* Decrement the priority of left-associative operators to force a
864 reduction with operators of otherwise equal priority. */
865 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
866 while (prio
< optab
[top
->op
].prio
)
868 if (optab
[top
->op
].arity
== UNARY
)
870 if (!pfile
->state
.skip_eval
)
871 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
874 else if (optab
[top
->op
].arity
== BINARY
)
876 if (!pfile
->state
.skip_eval
)
877 top
[-1].value
= (* (binary_handler
) optab
[top
->op
].handler
)
878 (pfile
, top
[-1].value
, top
->value
, top
->op
);
881 /* Anything changing skip_eval has to be handled here. */
882 else switch (top
--->op
)
885 if (!num_zerop (top
->value
))
886 pfile
->state
.skip_eval
--;
887 top
->value
.low
= !num_zerop (top
->value
) || !num_zerop (top
[1].value
);
889 top
->value
.unsignedp
= false;
890 top
->value
.overflow
= false;
894 if (num_zerop (top
->value
))
895 pfile
->state
.skip_eval
--;
896 top
->value
.low
= !num_zerop (top
->value
) && !num_zerop (top
[1].value
);
898 top
->value
.unsignedp
= false;
899 top
->value
.overflow
= false;
903 if (op
!= CPP_CLOSE_PAREN
)
905 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
908 top
->value
= top
[1].value
;
913 if (!num_zerop (top
->value
))
915 pfile
->state
.skip_eval
--;
916 top
->value
= top
[1].value
;
919 top
->value
= top
[2].value
;
920 top
->value
.unsignedp
= (top
[1].value
.unsignedp
921 || top
[2].value
.unsignedp
);
925 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
932 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
933 cpp_error (pfile
, DL_PEDWARN
,
934 "integer overflow in preprocessor expression");
937 if (op
== CPP_CLOSE_PAREN
)
939 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
946 /* Returns the position of the old top of stack after expansion. */
948 _cpp_expand_op_stack (pfile
)
951 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
952 size_t new_size
= old_size
* 2 + 20;
954 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
955 new_size
* sizeof (struct op
));
956 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
958 return pfile
->op_stack
+ old_size
;
961 /* Clears the unused high order bits of the number pointed to by PNUM. */
963 num_trim (num
, precision
)
967 if (precision
> PART_PRECISION
)
969 precision
-= PART_PRECISION
;
970 if (precision
< PART_PRECISION
)
971 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
975 if (precision
< PART_PRECISION
)
976 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
983 /* True iff A (presumed signed) >= 0. */
985 num_positive (num
, precision
)
989 if (precision
> PART_PRECISION
)
991 precision
-= PART_PRECISION
;
992 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
995 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
998 /* Sign extend a number, with PRECISION significant bits and all
999 others assumed clear, to fill out a cpp_num structure. */
1001 cpp_num_sign_extend (num
, precision
)
1007 if (precision
> PART_PRECISION
)
1009 precision
-= PART_PRECISION
;
1010 if (precision
< PART_PRECISION
1011 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1012 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1014 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1016 if (precision
< PART_PRECISION
)
1017 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1018 num
.high
= ~(cpp_num_part
) 0;
1025 /* Returns the negative of NUM. */
1027 num_negate (num
, precision
)
1034 num
.high
= ~num
.high
;
1038 num
= num_trim (num
, precision
);
1039 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1044 /* Returns true if A >= B. */
1046 num_greater_eq (pa
, pb
, precision
)
1052 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1056 /* Both numbers have signed type. If they are of different
1057 sign, the answer is the sign of A. */
1058 unsignedp
= num_positive (pa
, precision
);
1060 if (unsignedp
!= num_positive (pb
, precision
))
1063 /* Otherwise we can do an unsigned comparison. */
1066 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1069 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1071 num_bitwise_op (pfile
, lhs
, rhs
, op
)
1072 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1076 lhs
.overflow
= false;
1077 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1079 /* As excess precision is zeroed, there is no need to num_trim () as
1080 these operations cannot introduce a set bit there. */
1084 lhs
.high
&= rhs
.high
;
1086 else if (op
== CPP_OR
)
1089 lhs
.high
|= rhs
.high
;
1094 lhs
.high
^= rhs
.high
;
1100 /* Returns LHS OP RHS, where OP is an inequality. */
1102 num_inequality_op (pfile
, lhs
, rhs
, op
)
1107 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1109 if (op
== CPP_GREATER_EQ
)
1111 else if (op
== CPP_LESS
)
1113 else if (op
== CPP_GREATER
)
1114 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1115 else /* CPP_LESS_EQ. */
1116 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1119 lhs
.overflow
= false;
1120 lhs
.unsignedp
= false;
1124 /* Returns LHS OP RHS, where OP is == or !=. */
1126 num_equality_op (pfile
, lhs
, rhs
, op
)
1127 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1131 lhs
.low
= num_eq (lhs
, rhs
);
1132 if (op
== CPP_NOT_EQ
)
1135 lhs
.overflow
= false;
1136 lhs
.unsignedp
= false;
1140 /* Shift NUM, of width PRECISION, right by N bits. */
1142 num_rshift (num
, precision
, n
)
1144 size_t precision
, n
;
1146 cpp_num_part sign_mask
;
1148 if (num
.unsignedp
|| num_positive (num
, precision
))
1151 sign_mask
= ~(cpp_num_part
) 0;
1154 num
.high
= num
.low
= sign_mask
;
1158 if (precision
< PART_PRECISION
)
1159 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1160 else if (precision
< 2 * PART_PRECISION
)
1161 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1163 if (n
>= PART_PRECISION
)
1165 n
-= PART_PRECISION
;
1167 num
.high
= sign_mask
;
1172 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1173 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1177 num
= num_trim (num
, precision
);
1178 num
.overflow
= false;
1182 /* Shift NUM, of width PRECISION, left by N bits. */
1184 num_lshift (num
, precision
, n
)
1186 size_t precision
, n
;
1190 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1191 num
.high
= num
.low
= 0;
1195 cpp_num orig
, maybe_orig
;
1199 if (m
>= PART_PRECISION
)
1201 m
-= PART_PRECISION
;
1207 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1210 num
= num_trim (num
, precision
);
1213 num
.overflow
= false;
1216 maybe_orig
= num_rshift (num
, precision
, n
);
1217 num
.overflow
= !num_eq (orig
, maybe_orig
);
1224 /* The four unary operators: +, -, ! and ~. */
1226 num_unary_op (pfile
, num
, op
)
1234 if (CPP_WTRADITIONAL (pfile
))
1235 cpp_error (pfile
, DL_WARNING
,
1236 "traditional C rejects the unary plus operator");
1237 num
.overflow
= false;
1241 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1245 num
.high
= ~num
.high
;
1247 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1248 num
.overflow
= false;
1251 default: /* case CPP_NOT: */
1252 num
.low
= num_zerop (num
);
1254 num
.overflow
= false;
1255 num
.unsignedp
= false;
1262 /* The various binary operators. */
1264 num_binary_op (pfile
, lhs
, rhs
, op
)
1270 size_t precision
= CPP_OPTION (pfile
, precision
);
1279 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1281 /* A negative shift is a positive shift the other way. */
1282 if (op
== CPP_LSHIFT
)
1286 rhs
= num_negate (rhs
, precision
);
1289 n
= ~0; /* Maximal. */
1292 if (op
== CPP_LSHIFT
)
1293 lhs
= num_lshift (lhs
, precision
, n
);
1295 lhs
= num_rshift (lhs
, precision
, n
);
1302 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1304 gte
= num_greater_eq (lhs
, rhs
, precision
);
1309 lhs
.unsignedp
= unsignedp
;
1315 rhs
= num_negate (rhs
, precision
);
1317 result
.low
= lhs
.low
+ rhs
.low
;
1318 result
.high
= lhs
.high
+ rhs
.high
;
1319 if (result
.low
< lhs
.low
)
1322 result
= num_trim (result
, precision
);
1323 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1324 if (result
.unsignedp
)
1325 result
.overflow
= false;
1328 bool lhsp
= num_positive (lhs
, precision
);
1329 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1330 && lhsp
!= num_positive (result
, precision
));
1335 default: /* case CPP_COMMA: */
1336 if (CPP_PEDANTIC (pfile
))
1337 cpp_error (pfile
, DL_PEDWARN
,
1338 "comma operator in operand of #if");
1346 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1349 num_part_mul (lhs
, rhs
)
1350 cpp_num_part lhs
, rhs
;
1353 cpp_num_part middle
[2], temp
;
1355 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1356 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1358 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1359 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1362 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1363 if (result
.low
< temp
)
1367 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1368 if (result
.low
< temp
)
1371 result
.high
+= HIGH_PART (middle
[0]);
1372 result
.high
+= HIGH_PART (middle
[1]);
1377 /* Multiply two preprocessing numbers. */
1379 num_mul (pfile
, lhs
, rhs
, op
)
1382 enum cpp_ttype op ATTRIBUTE_UNUSED
;
1384 cpp_num result
, temp
;
1385 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1386 bool overflow
, negate
= false;
1387 size_t precision
= CPP_OPTION (pfile
, precision
);
1389 /* Prepare for unsigned multiplication. */
1392 if (!num_positive (lhs
, precision
))
1393 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1394 if (!num_positive (rhs
, precision
))
1395 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1398 overflow
= lhs
.high
&& rhs
.high
;
1399 result
= num_part_mul (lhs
.low
, rhs
.low
);
1401 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1402 result
.high
+= temp
.low
;
1406 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1407 result
.high
+= temp
.low
;
1411 temp
.low
= result
.low
, temp
.high
= result
.high
;
1412 result
= num_trim (result
, precision
);
1413 if (!num_eq (result
, temp
))
1417 result
= num_negate (result
, precision
);
1420 result
.overflow
= false;
1422 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1423 && !num_zerop (result
));
1424 result
.unsignedp
= unsignedp
;
1429 /* Divide two preprocessing numbers, returning the answer or the
1430 remainder depending upon OP. */
1432 num_div_op (pfile
, lhs
, rhs
, op
)
1437 cpp_num result
, sub
;
1439 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1440 bool negate
= false, lhs_neg
= false;
1441 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1443 /* Prepare for unsigned division. */
1446 if (!num_positive (lhs
, precision
))
1447 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1448 if (!num_positive (rhs
, precision
))
1449 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1452 /* Find the high bit. */
1456 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1457 for (; ; i
--, mask
>>= 1)
1458 if (rhs
.high
& mask
)
1463 if (precision
> PART_PRECISION
)
1464 i
= precision
- PART_PRECISION
- 1;
1467 mask
= (cpp_num_part
) 1 << i
;
1468 for (; ; i
--, mask
>>= 1)
1474 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1478 /* First non-zero bit of RHS is bit I. Do naive division by
1479 shifting the RHS fully left, and subtracting from LHS if LHS is
1480 at least as big, and then repeating but with one less shift.
1481 This is not very efficient, but is easy to understand. */
1483 rhs
.unsignedp
= true;
1484 lhs
.unsignedp
= true;
1485 i
= precision
- i
- 1;
1486 sub
= num_lshift (rhs
, precision
, i
);
1488 result
.high
= result
.low
= 0;
1491 if (num_greater_eq (lhs
, sub
, precision
))
1493 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1494 if (i
>= PART_PRECISION
)
1495 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1497 result
.low
|= (cpp_num_part
) 1 << i
;
1501 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1505 /* We divide so that the remainder has the sign of the LHS. */
1508 result
.unsignedp
= unsignedp
;
1510 result
.overflow
= false;
1514 result
= num_negate (result
, precision
);
1515 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1522 lhs
.unsignedp
= unsignedp
;
1523 lhs
.overflow
= false;
1525 lhs
= num_negate (lhs
, precision
);