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 /* If too big to be signed, consider it unsigned. Only warn for
379 decimal numbers. Traditional numbers were always signed (but
380 we still honour an explicit U suffix). */
381 else if (!result
.unsignedp
382 && !CPP_OPTION (pfile
, traditional
)
383 && !num_positive (result
, precision
))
386 cpp_error (pfile
, DL_WARNING
,
387 "integer constant is so large that it is unsigned");
388 result
.unsignedp
= 1;
395 /* Append DIGIT to NUM, a number of PRECISION bits being read in base
398 append_digit (num
, digit
, base
, precision
)
404 unsigned int shift
= 3 + (base
== 16);
406 cpp_num_part add_high
, add_low
;
408 /* Multiply by 8 or 16. Catching this overflow here means we don't
409 need to worry about add_high overflowing. */
410 overflow
= num
.high
>> (PART_PRECISION
- shift
);
411 result
.high
= num
.high
<< shift
;
412 result
.low
= num
.low
<< shift
;
413 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
417 add_low
= num
.low
<< 1;
418 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
421 add_high
= add_low
= 0;
423 if (add_low
+ digit
< add_low
)
427 if (result
.low
+ add_low
< result
.low
)
429 if (result
.high
+ add_high
< result
.high
)
432 result
.low
+= add_low
;
433 result
.high
+= add_high
;
435 /* The above code catches overflow of a cpp_num type. This catches
436 overflow of the (possibly shorter) target precision. */
437 num
.low
= result
.low
;
438 num
.high
= result
.high
;
439 result
= num_trim (result
, precision
);
440 if (!num_eq (result
, num
))
443 result
.unsignedp
= num
.unsignedp
;
444 result
.overflow
= overflow
;
448 /* Handle meeting "defined" in a preprocessor expression. */
450 parse_defined (pfile
)
455 cpp_hashnode
*node
= 0;
456 const cpp_token
*token
;
457 cpp_context
*initial_context
= pfile
->context
;
459 /* Don't expand macros. */
460 pfile
->state
.prevent_expansion
++;
462 token
= cpp_get_token (pfile
);
463 if (token
->type
== CPP_OPEN_PAREN
)
466 token
= cpp_get_token (pfile
);
469 if (token
->type
== CPP_NAME
)
471 node
= token
->val
.node
;
472 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
474 cpp_error (pfile
, DL_ERROR
, "missing ')' after \"defined\"");
480 cpp_error (pfile
, DL_ERROR
,
481 "operator \"defined\" requires an identifier");
482 if (token
->flags
& NAMED_OP
)
487 op
.type
= token
->type
;
488 cpp_error (pfile
, DL_ERROR
,
489 "(\"%s\" is an alternative token for \"%s\" in C++)",
490 cpp_token_as_text (pfile
, token
),
491 cpp_token_as_text (pfile
, &op
));
497 if (pfile
->context
!= initial_context
)
498 cpp_error (pfile
, DL_WARNING
,
499 "this use of \"defined\" may not be portable");
501 /* A possible controlling macro of the form #if !defined ().
502 _cpp_parse_expr checks there was no other junk on the line. */
503 pfile
->mi_ind_cmacro
= node
;
506 pfile
->state
.prevent_expansion
--;
508 result
.unsignedp
= 0;
511 result
.low
= node
&& node
->type
== NT_MACRO
;
515 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
516 number or character constant, or the result of the "defined" or "#"
519 eval_token (pfile
, token
)
521 const cpp_token
*token
;
530 temp
= cpp_classify_number (pfile
, token
);
531 switch (temp
& CPP_N_CATEGORY
)
534 cpp_error (pfile
, DL_ERROR
,
535 "floating constant in preprocessor expression");
538 if (!(temp
& CPP_N_IMAGINARY
))
539 return cpp_interpret_integer (pfile
, token
, temp
);
540 cpp_error (pfile
, DL_ERROR
,
541 "imaginary number in preprocessor expression");
545 /* Error already issued. */
548 result
.high
= result
.low
= 0;
554 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
559 /* Sign-extend the result if necessary. */
560 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
562 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
563 result
.low
|= ~(~(cpp_num_part
) 0
564 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
565 result
.high
= ~(cpp_num_part
) 0;
566 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
572 if (token
->val
.node
== pfile
->spec_nodes
.n_defined
)
573 return parse_defined (pfile
);
574 else if (CPP_OPTION (pfile
, cplusplus
)
575 && (token
->val
.node
== pfile
->spec_nodes
.n_true
576 || token
->val
.node
== pfile
->spec_nodes
.n_false
))
579 result
.low
= (token
->val
.node
== pfile
->spec_nodes
.n_true
);
581 /* Warn about use of true or false in #if when pedantic
582 and stdbool.h has not been included. */
583 if (CPP_PEDANTIC (pfile
)
584 && ! cpp_defined (pfile
, DSC("__bool_true_false_are_defined")))
585 cpp_error (pfile
, DL_PEDWARN
,
586 "ISO C++ does not permit \"%s\" in #if",
587 NODE_NAME (token
->val
.node
));
593 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
594 cpp_error (pfile
, DL_WARNING
, "\"%s\" is not defined",
595 NODE_NAME (token
->val
.node
));
599 default: /* CPP_HASH */
600 _cpp_test_assertion (pfile
, &temp
);
605 result
.unsignedp
= unsignedp
;
610 /* Operator precedence and flags table.
612 After an operator is returned from the lexer, if it has priority less
613 than the operator on the top of the stack, we reduce the stack by one
614 operator and repeat the test. Since equal priorities do not reduce,
615 this is naturally right-associative.
617 We handle left-associative operators by decrementing the priority of
618 just-lexed operators by one, but retaining the priority of operators
619 already on the stack.
621 The remaining cases are '(' and ')'. We handle '(' by skipping the
622 reduction phase completely. ')' is given lower priority than
623 everything else, including '(', effectively forcing a reduction of the
624 parenthesised expression. If there is a matching '(', the routine
625 reduce() exits immediately. If the normal exit route sees a ')', then
626 there cannot have been a matching '(' and an error message is output.
628 The parser assumes all shifted operators require a left operand unless
629 the flag NO_L_OPERAND is set. These semantics are automatic; any
630 extra semantics need to be handled with operator-specific code. */
633 #define NO_L_OPERAND (1 << 0)
634 #define LEFT_ASSOC (1 << 1)
637 #define UNARY (1 << 0)
638 #define BINARY (1 << 1)
639 #define OTHER (1 << 2)
641 typedef cpp_num (*binary_handler
) PARAMS ((cpp_reader
*, cpp_num
, cpp_num
,
643 /* Operator to priority map. Must be in the same order as the first
644 N entries of enum cpp_ttype. */
645 static const struct operator
650 binary_handler handler
;
653 /* EQ */ {0, 0, OTHER
, NULL
}, /* Shouldn't happen. */
654 /* NOT */ {16, NO_L_OPERAND
, UNARY
, NULL
},
655 /* GREATER */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
656 /* LESS */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
657 /* PLUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
658 /* MINUS */ {14, LEFT_ASSOC
, BINARY
, num_binary_op
},
659 /* MULT */ {15, LEFT_ASSOC
, BINARY
, num_mul
},
660 /* DIV */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
661 /* MOD */ {15, LEFT_ASSOC
, BINARY
, num_div_op
},
662 /* AND */ {9, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
663 /* OR */ {7, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
664 /* XOR */ {8, LEFT_ASSOC
, BINARY
, num_bitwise_op
},
665 /* RSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
666 /* LSHIFT */ {13, LEFT_ASSOC
, BINARY
, num_binary_op
},
668 /* MIN */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
669 /* MAX */ {10, LEFT_ASSOC
, BINARY
, num_binary_op
},
671 /* COMPL */ {16, NO_L_OPERAND
, UNARY
, NULL
},
672 /* AND_AND */ {6, LEFT_ASSOC
, OTHER
, NULL
},
673 /* OR_OR */ {5, LEFT_ASSOC
, OTHER
, NULL
},
674 /* QUERY */ {3, 0, OTHER
, NULL
},
675 /* COLON */ {4, LEFT_ASSOC
, OTHER
, NULL
},
676 /* COMMA */ {2, LEFT_ASSOC
, BINARY
, num_binary_op
},
677 /* OPEN_PAREN */ {1, NO_L_OPERAND
, OTHER
, NULL
},
678 /* CLOSE_PAREN */ {0, 0, OTHER
, NULL
},
679 /* EOF */ {0, 0, OTHER
, NULL
},
680 /* EQ_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
681 /* NOT_EQ */ {11, LEFT_ASSOC
, BINARY
, num_equality_op
},
682 /* GREATER_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
683 /* LESS_EQ */ {12, LEFT_ASSOC
, BINARY
, num_inequality_op
},
684 /* UPLUS */ {16, NO_L_OPERAND
, UNARY
, NULL
},
685 /* UMINUS */ {16, NO_L_OPERAND
, UNARY
, NULL
}
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 (pfile
)
703 struct op
*top
= pfile
->op_stack
;
704 const cpp_token
*token
= NULL
, *prev_token
;
705 unsigned int lex_count
;
706 bool saw_leading_not
, want_value
= true;
708 pfile
->state
.skip_eval
= 0;
710 /* Set up detection of #if ! defined(). */
711 pfile
->mi_ind_cmacro
= 0;
712 saw_leading_not
= false;
715 /* Lowest priority operator prevents further reductions. */
723 token
= cpp_get_token (pfile
);
729 /* These tokens convert into values. */
736 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
737 cpp_token_as_text (pfile
, token
));
739 top
->value
= eval_token (pfile
, token
);
743 saw_leading_not
= lex_count
== 1;
754 if (ISGRAPH (token
->val
.c
))
755 SYNTAX_ERROR2 ("invalid character '%c' in #if", token
->val
.c
);
757 SYNTAX_ERROR2 ("invalid character '\\%03o' in #if", token
->val
.c
);
760 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
761 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
762 cpp_token_as_text (pfile
, token
));
766 /* Check we have a value or operator as appropriate. */
767 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
770 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
771 cpp_token_as_text (pfile
, token
));
775 /* Ordering here is subtle and intended to favour the
776 missing parenthesis diagnostics over alternatives. */
777 if (op
.op
== CPP_CLOSE_PAREN
)
779 if (top
->op
== CPP_OPEN_PAREN
)
780 SYNTAX_ERROR ("void expression between '(' and ')'");
782 else if (top
->op
== CPP_EOF
)
783 SYNTAX_ERROR ("#if with no expression");
784 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
785 SYNTAX_ERROR2 ("operator '%s' has no right operand",
786 cpp_token_as_text (pfile
, prev_token
));
789 top
= reduce (pfile
, top
, op
.op
);
793 if (op
.op
== CPP_EOF
)
798 case CPP_CLOSE_PAREN
:
801 if (!num_zerop (top
->value
))
802 pfile
->state
.skip_eval
++;
806 if (num_zerop (top
->value
))
807 pfile
->state
.skip_eval
++;
810 if (top
->op
!= CPP_QUERY
)
811 SYNTAX_ERROR (" ':' without preceding '?'");
812 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
813 pfile
->state
.skip_eval
++;
815 pfile
->state
.skip_eval
--;
822 /* Check for and handle stack overflow. */
823 if (++top
== pfile
->op_limit
)
824 top
= _cpp_expand_op_stack (pfile
);
829 /* The controlling macro expression is only valid if we called lex 3
830 times: <!> <defined expression> and <EOF>. push_conditional ()
831 checks that we are at top-of-file. */
832 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
833 pfile
->mi_ind_cmacro
= 0;
835 if (top
!= pfile
->op_stack
)
837 cpp_error (pfile
, DL_ICE
, "unbalanced stack in #if");
839 return false; /* Return false on syntax error. */
842 return !num_zerop (top
->value
);
845 /* Reduce the operator / value stack if possible, in preparation for
846 pushing operator OP. Returns NULL on error, otherwise the top of
849 reduce (pfile
, top
, op
)
856 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
859 cpp_error (pfile
, DL_ICE
, "impossible operator '%u'", top
->op
);
863 if (op
== CPP_OPEN_PAREN
)
866 /* Decrement the priority of left-associative operators to force a
867 reduction with operators of otherwise equal priority. */
868 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
869 while (prio
< optab
[top
->op
].prio
)
871 if (optab
[top
->op
].arity
== UNARY
)
873 if (!pfile
->state
.skip_eval
)
874 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
877 else if (optab
[top
->op
].arity
== BINARY
)
879 if (!pfile
->state
.skip_eval
)
880 top
[-1].value
= (* (binary_handler
) optab
[top
->op
].handler
)
881 (pfile
, top
[-1].value
, top
->value
, top
->op
);
884 /* Anything changing skip_eval has to be handled here. */
885 else switch (top
--->op
)
888 if (!num_zerop (top
->value
))
889 pfile
->state
.skip_eval
--;
890 top
->value
.low
= !num_zerop (top
->value
) || !num_zerop (top
[1].value
);
892 top
->value
.unsignedp
= false;
893 top
->value
.overflow
= false;
897 if (num_zerop (top
->value
))
898 pfile
->state
.skip_eval
--;
899 top
->value
.low
= !num_zerop (top
->value
) && !num_zerop (top
[1].value
);
901 top
->value
.unsignedp
= false;
902 top
->value
.overflow
= false;
906 if (op
!= CPP_CLOSE_PAREN
)
908 cpp_error (pfile
, DL_ERROR
, "missing ')' in expression");
911 top
->value
= top
[1].value
;
916 if (!num_zerop (top
->value
))
918 pfile
->state
.skip_eval
--;
919 top
->value
= top
[1].value
;
922 top
->value
= top
[2].value
;
923 top
->value
.unsignedp
= (top
[1].value
.unsignedp
924 || top
[2].value
.unsignedp
);
928 cpp_error (pfile
, DL_ERROR
, "'?' without following ':'");
935 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
936 cpp_error (pfile
, DL_PEDWARN
,
937 "integer overflow in preprocessor expression");
940 if (op
== CPP_CLOSE_PAREN
)
942 cpp_error (pfile
, DL_ERROR
, "missing '(' in expression");
949 /* Returns the position of the old top of stack after expansion. */
951 _cpp_expand_op_stack (pfile
)
954 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
955 size_t new_size
= old_size
* 2 + 20;
957 pfile
->op_stack
= (struct op
*) xrealloc (pfile
->op_stack
,
958 new_size
* sizeof (struct op
));
959 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
961 return pfile
->op_stack
+ old_size
;
964 /* Clears the unused high order bits of the number pointed to by PNUM. */
966 num_trim (num
, precision
)
970 if (precision
> PART_PRECISION
)
972 precision
-= PART_PRECISION
;
973 if (precision
< PART_PRECISION
)
974 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
978 if (precision
< PART_PRECISION
)
979 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
986 /* True iff A (presumed signed) >= 0. */
988 num_positive (num
, precision
)
992 if (precision
> PART_PRECISION
)
994 precision
-= PART_PRECISION
;
995 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
998 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1001 /* Sign extend a number, with PRECISION significant bits and all
1002 others assumed clear, to fill out a cpp_num structure. */
1004 cpp_num_sign_extend (num
, precision
)
1010 if (precision
> PART_PRECISION
)
1012 precision
-= PART_PRECISION
;
1013 if (precision
< PART_PRECISION
1014 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1015 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1017 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1019 if (precision
< PART_PRECISION
)
1020 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1021 num
.high
= ~(cpp_num_part
) 0;
1028 /* Returns the negative of NUM. */
1030 num_negate (num
, precision
)
1037 num
.high
= ~num
.high
;
1041 num
= num_trim (num
, precision
);
1042 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1047 /* Returns true if A >= B. */
1049 num_greater_eq (pa
, pb
, precision
)
1055 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1059 /* Both numbers have signed type. If they are of different
1060 sign, the answer is the sign of A. */
1061 unsignedp
= num_positive (pa
, precision
);
1063 if (unsignedp
!= num_positive (pb
, precision
))
1066 /* Otherwise we can do an unsigned comparison. */
1069 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1072 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1074 num_bitwise_op (pfile
, lhs
, rhs
, op
)
1075 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1079 lhs
.overflow
= false;
1080 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1082 /* As excess precision is zeroed, there is no need to num_trim () as
1083 these operations cannot introduce a set bit there. */
1087 lhs
.high
&= rhs
.high
;
1089 else if (op
== CPP_OR
)
1092 lhs
.high
|= rhs
.high
;
1097 lhs
.high
^= rhs
.high
;
1103 /* Returns LHS OP RHS, where OP is an inequality. */
1105 num_inequality_op (pfile
, lhs
, rhs
, op
)
1110 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1112 if (op
== CPP_GREATER_EQ
)
1114 else if (op
== CPP_LESS
)
1116 else if (op
== CPP_GREATER
)
1117 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1118 else /* CPP_LESS_EQ. */
1119 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1122 lhs
.overflow
= false;
1123 lhs
.unsignedp
= false;
1127 /* Returns LHS OP RHS, where OP is == or !=. */
1129 num_equality_op (pfile
, lhs
, rhs
, op
)
1130 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
1134 /* Work around a 3.0.4 bug; see PR 6950. */
1135 bool eq
= num_eq (lhs
, rhs
);
1136 if (op
== CPP_NOT_EQ
)
1140 lhs
.overflow
= false;
1141 lhs
.unsignedp
= false;
1145 /* Shift NUM, of width PRECISION, right by N bits. */
1147 num_rshift (num
, precision
, n
)
1149 size_t precision
, n
;
1151 cpp_num_part sign_mask
;
1153 if (num
.unsignedp
|| num_positive (num
, precision
))
1156 sign_mask
= ~(cpp_num_part
) 0;
1159 num
.high
= num
.low
= sign_mask
;
1163 if (precision
< PART_PRECISION
)
1164 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1165 else if (precision
< 2 * PART_PRECISION
)
1166 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1168 if (n
>= PART_PRECISION
)
1170 n
-= PART_PRECISION
;
1172 num
.high
= sign_mask
;
1177 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1178 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1182 num
= num_trim (num
, precision
);
1183 num
.overflow
= false;
1187 /* Shift NUM, of width PRECISION, left by N bits. */
1189 num_lshift (num
, precision
, n
)
1191 size_t precision
, n
;
1195 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1196 num
.high
= num
.low
= 0;
1200 cpp_num orig
, maybe_orig
;
1204 if (m
>= PART_PRECISION
)
1206 m
-= PART_PRECISION
;
1212 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1215 num
= num_trim (num
, precision
);
1218 num
.overflow
= false;
1221 maybe_orig
= num_rshift (num
, precision
, n
);
1222 num
.overflow
= !num_eq (orig
, maybe_orig
);
1229 /* The four unary operators: +, -, ! and ~. */
1231 num_unary_op (pfile
, num
, op
)
1239 if (CPP_WTRADITIONAL (pfile
))
1240 cpp_error (pfile
, DL_WARNING
,
1241 "traditional C rejects the unary plus operator");
1242 num
.overflow
= false;
1246 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1250 num
.high
= ~num
.high
;
1252 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1253 num
.overflow
= false;
1256 default: /* case CPP_NOT: */
1257 num
.low
= num_zerop (num
);
1259 num
.overflow
= false;
1260 num
.unsignedp
= false;
1267 /* The various binary operators. */
1269 num_binary_op (pfile
, lhs
, rhs
, op
)
1275 size_t precision
= CPP_OPTION (pfile
, precision
);
1284 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1286 /* A negative shift is a positive shift the other way. */
1287 if (op
== CPP_LSHIFT
)
1291 rhs
= num_negate (rhs
, precision
);
1294 n
= ~0; /* Maximal. */
1297 if (op
== CPP_LSHIFT
)
1298 lhs
= num_lshift (lhs
, precision
, n
);
1300 lhs
= num_rshift (lhs
, precision
, n
);
1307 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1309 gte
= num_greater_eq (lhs
, rhs
, precision
);
1314 lhs
.unsignedp
= unsignedp
;
1320 rhs
= num_negate (rhs
, precision
);
1322 result
.low
= lhs
.low
+ rhs
.low
;
1323 result
.high
= lhs
.high
+ rhs
.high
;
1324 if (result
.low
< lhs
.low
)
1327 result
= num_trim (result
, precision
);
1328 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1329 if (result
.unsignedp
)
1330 result
.overflow
= false;
1333 bool lhsp
= num_positive (lhs
, precision
);
1334 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1335 && lhsp
!= num_positive (result
, precision
));
1340 default: /* case CPP_COMMA: */
1341 if (CPP_PEDANTIC (pfile
))
1342 cpp_error (pfile
, DL_PEDWARN
,
1343 "comma operator in operand of #if");
1351 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1354 num_part_mul (lhs
, rhs
)
1355 cpp_num_part lhs
, rhs
;
1358 cpp_num_part middle
[2], temp
;
1360 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1361 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1363 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1364 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1367 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1368 if (result
.low
< temp
)
1372 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1373 if (result
.low
< temp
)
1376 result
.high
+= HIGH_PART (middle
[0]);
1377 result
.high
+= HIGH_PART (middle
[1]);
1382 /* Multiply two preprocessing numbers. */
1384 num_mul (pfile
, lhs
, rhs
, op
)
1387 enum cpp_ttype op ATTRIBUTE_UNUSED
;
1389 cpp_num result
, temp
;
1390 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1391 bool overflow
, negate
= false;
1392 size_t precision
= CPP_OPTION (pfile
, precision
);
1394 /* Prepare for unsigned multiplication. */
1397 if (!num_positive (lhs
, precision
))
1398 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1399 if (!num_positive (rhs
, precision
))
1400 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1403 overflow
= lhs
.high
&& rhs
.high
;
1404 result
= num_part_mul (lhs
.low
, rhs
.low
);
1406 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1407 result
.high
+= temp
.low
;
1411 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1412 result
.high
+= temp
.low
;
1416 temp
.low
= result
.low
, temp
.high
= result
.high
;
1417 result
= num_trim (result
, precision
);
1418 if (!num_eq (result
, temp
))
1422 result
= num_negate (result
, precision
);
1425 result
.overflow
= false;
1427 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1428 && !num_zerop (result
));
1429 result
.unsignedp
= unsignedp
;
1434 /* Divide two preprocessing numbers, returning the answer or the
1435 remainder depending upon OP. */
1437 num_div_op (pfile
, lhs
, rhs
, op
)
1442 cpp_num result
, sub
;
1444 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1445 bool negate
= false, lhs_neg
= false;
1446 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1448 /* Prepare for unsigned division. */
1451 if (!num_positive (lhs
, precision
))
1452 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1453 if (!num_positive (rhs
, precision
))
1454 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1457 /* Find the high bit. */
1461 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1462 for (; ; i
--, mask
>>= 1)
1463 if (rhs
.high
& mask
)
1468 if (precision
> PART_PRECISION
)
1469 i
= precision
- PART_PRECISION
- 1;
1472 mask
= (cpp_num_part
) 1 << i
;
1473 for (; ; i
--, mask
>>= 1)
1479 cpp_error (pfile
, DL_ERROR
, "division by zero in #if");
1483 /* First non-zero bit of RHS is bit I. Do naive division by
1484 shifting the RHS fully left, and subtracting from LHS if LHS is
1485 at least as big, and then repeating but with one less shift.
1486 This is not very efficient, but is easy to understand. */
1488 rhs
.unsignedp
= true;
1489 lhs
.unsignedp
= true;
1490 i
= precision
- i
- 1;
1491 sub
= num_lshift (rhs
, precision
, i
);
1493 result
.high
= result
.low
= 0;
1496 if (num_greater_eq (lhs
, sub
, precision
))
1498 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1499 if (i
>= PART_PRECISION
)
1500 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1502 result
.low
|= (cpp_num_part
) 1 << i
;
1506 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1510 /* We divide so that the remainder has the sign of the LHS. */
1513 result
.unsignedp
= unsignedp
;
1515 result
.overflow
= false;
1519 result
= num_negate (result
, precision
);
1520 result
.overflow
= num_positive (result
, precision
) ^ !negate
;
1527 lhs
.unsignedp
= unsignedp
;
1528 lhs
.overflow
= false;
1530 lhs
= num_negate (lhs
, precision
);