1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009 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 3, 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; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
25 #define PART_PRECISION (sizeof (cpp_num_part) * CHAR_BIT)
26 #define HALF_MASK (~(cpp_num_part) 0 >> (PART_PRECISION / 2))
27 #define LOW_PART(num_part) (num_part & HALF_MASK)
28 #define HIGH_PART(num_part) (num_part >> (PART_PRECISION / 2))
32 const cpp_token
*token
; /* The token forming op (for diagnostics). */
33 cpp_num value
; /* The value logically "right" of op. */
34 source_location loc
; /* The location of this value. */
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
,
57 static cpp_num
num_lshift (cpp_num
, size_t, size_t);
58 static cpp_num
num_rshift (cpp_num
, size_t, size_t);
60 static cpp_num
append_digit (cpp_num
, int, int, size_t);
61 static cpp_num
parse_defined (cpp_reader
*);
62 static cpp_num
eval_token (cpp_reader
*, const cpp_token
*);
63 static struct op
*reduce (cpp_reader
*, struct op
*, enum cpp_ttype
);
64 static unsigned int interpret_float_suffix (const uchar
*, size_t);
65 static unsigned int interpret_int_suffix (const uchar
*, size_t);
66 static void check_promotion (cpp_reader
*, const struct op
*);
68 /* Token type abuse to create unary plus and minus operators. */
69 #define CPP_UPLUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 1))
70 #define CPP_UMINUS ((enum cpp_ttype) (CPP_LAST_CPP_OP + 2))
72 /* With -O2, gcc appears to produce nice code, moving the error
73 message load and subsequent jump completely out of the main path. */
74 #define SYNTAX_ERROR(msgid) \
75 do { cpp_error (pfile, CPP_DL_ERROR, msgid); goto syntax_error; } while(0)
76 #define SYNTAX_ERROR2(msgid, arg) \
77 do { cpp_error (pfile, CPP_DL_ERROR, msgid, arg); goto syntax_error; } \
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 (const uchar
*s
, size_t len
)
87 size_t f
, d
, l
, w
, q
, i
;
90 f
= d
= l
= w
= q
= i
= 0;
92 /* Process decimal float suffixes, which are two letters starting
93 with d or D. Order and case are significant. */
94 if (len
== 2 && (*s
== 'd' || *s
== 'D'))
96 bool uppercase
= (*s
== 'D');
99 case 'f': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
): 0); break;
100 case 'F': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_SMALL
) : 0); break;
101 case 'd': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
): 0); break;
102 case 'D': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_MEDIUM
) : 0); break;
103 case 'l': return (!uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
104 case 'L': return (uppercase
? (CPP_N_DFLOAT
| CPP_N_LARGE
) : 0); break;
106 /* Additional two-character suffixes beginning with D are not
107 for decimal float constants. */
112 /* Recognize a fixed-point suffix. */
115 case 'k': case 'K': flags
= CPP_N_ACCUM
; break;
116 case 'r': case 'R': flags
= CPP_N_FRACT
; break;
120 /* Continue processing a fixed-point suffix. The suffix is case
121 insensitive except for ll or LL. Order is significant. */
128 if (*s
== 'u' || *s
== 'U')
130 flags
|= CPP_N_UNSIGNED
;
141 return flags
|= CPP_N_SMALL
;
145 return flags
|= CPP_N_MEDIUM
;
146 if (len
== 2 && s
[1] == 'l')
147 return flags
|= CPP_N_LARGE
;
151 return flags
|= CPP_N_MEDIUM
;
152 if (len
== 2 && s
[1] == 'L')
153 return flags
|= CPP_N_LARGE
;
158 /* Anything left at this point is invalid. */
162 /* In any remaining valid suffix, the case and order don't matter. */
166 case 'f': case 'F': f
++; break;
167 case 'd': case 'D': d
++; break;
168 case 'l': case 'L': l
++; break;
169 case 'w': case 'W': w
++; break;
170 case 'q': case 'Q': q
++; break;
172 case 'j': case 'J': i
++; break;
177 if (f
+ d
+ l
+ w
+ q
> 1 || i
> 1)
180 return ((i
? CPP_N_IMAGINARY
: 0)
185 q
? CPP_N_MD_Q
: CPP_N_DEFAULT
));
188 /* Subroutine of cpp_classify_number. S points to an integer suffix
189 of length LEN, possibly zero. Returns 0 for an invalid suffix, or a
190 flag vector describing the suffix. */
192 interpret_int_suffix (const uchar
*s
, size_t len
)
201 case 'u': case 'U': u
++; break;
203 case 'j': case 'J': i
++; break;
204 case 'l': case 'L': l
++;
205 /* If there are two Ls, they must be adjacent and the same case. */
206 if (l
== 2 && s
[len
] != s
[len
+ 1])
213 if (l
> 2 || u
> 1 || i
> 1)
216 return ((i
? CPP_N_IMAGINARY
: 0)
217 | (u
? CPP_N_UNSIGNED
: 0)
218 | ((l
== 0) ? CPP_N_SMALL
219 : (l
== 1) ? CPP_N_MEDIUM
: CPP_N_LARGE
));
222 /* Categorize numeric constants according to their field (integer,
223 floating point, or invalid), radix (decimal, octal, hexadecimal),
224 and type suffixes. */
226 cpp_classify_number (cpp_reader
*pfile
, const cpp_token
*token
)
228 const uchar
*str
= token
->val
.str
.text
;
230 unsigned int max_digit
, result
, radix
;
231 enum {NOT_FLOAT
= 0, AFTER_POINT
, AFTER_EXPON
} float_flag
;
233 /* If the lexer has done its job, length one can only be a single
234 digit. Fast-path this very common case. */
235 if (token
->val
.str
.len
== 1)
236 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
238 limit
= str
+ token
->val
.str
.len
;
239 float_flag
= NOT_FLOAT
;
243 /* First, interpret the radix. */
249 /* Require at least one hex digit to classify it as hex. */
250 if ((*str
== 'x' || *str
== 'X')
251 && (str
[1] == '.' || ISXDIGIT (str
[1])))
256 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
263 /* Now scan for a well-formed integer or float. */
266 unsigned int c
= *str
++;
268 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
276 if (float_flag
== NOT_FLOAT
)
277 float_flag
= AFTER_POINT
;
279 SYNTAX_ERROR ("too many decimal points in number");
281 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
282 || (radix
== 16 && (c
== 'p' || c
== 'P')))
284 float_flag
= AFTER_EXPON
;
289 /* Start of suffix. */
295 /* The suffix may be for decimal fixed-point constants without exponent. */
296 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
298 result
= interpret_float_suffix (str
, limit
- str
);
299 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
301 result
|= CPP_N_FLOATING
;
302 /* We need to restore the radix to 10, if the radix is 8. */
306 if (CPP_PEDANTIC (pfile
))
307 cpp_error (pfile
, CPP_DL_PEDWARN
,
308 "fixed-point constants are a GCC extension");
315 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
318 if (max_digit
>= radix
)
321 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit
);
323 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
326 if (float_flag
!= NOT_FLOAT
)
330 cpp_error (pfile
, CPP_DL_ERROR
,
331 "invalid prefix \"0b\" for floating constant");
332 return CPP_N_INVALID
;
335 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
336 cpp_error (pfile
, CPP_DL_PEDWARN
,
337 "use of C99 hexadecimal floating constant");
339 if (float_flag
== AFTER_EXPON
)
341 if (*str
== '+' || *str
== '-')
344 /* Exponent is decimal, even if string is a hex float. */
346 SYNTAX_ERROR ("exponent has no digits");
350 while (ISDIGIT (*str
));
352 else if (radix
== 16)
353 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
355 result
= interpret_float_suffix (str
, limit
- str
);
358 cpp_error (pfile
, CPP_DL_ERROR
,
359 "invalid suffix \"%.*s\" on floating constant",
360 (int) (limit
- str
), str
);
361 return CPP_N_INVALID
;
364 /* Traditional C didn't accept any floating suffixes. */
366 && CPP_WTRADITIONAL (pfile
)
367 && ! cpp_sys_macro_p (pfile
))
368 cpp_error (pfile
, CPP_DL_WARNING
,
369 "traditional C rejects the \"%.*s\" suffix",
370 (int) (limit
- str
), str
);
372 /* A suffix for double is a GCC extension via decimal float support.
373 If the suffix also specifies an imaginary value we'll catch that
375 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
376 cpp_error (pfile
, CPP_DL_PEDWARN
,
377 "suffix for double constant is a GCC extension");
379 /* Radix must be 10 for decimal floats. */
380 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
382 cpp_error (pfile
, CPP_DL_ERROR
,
383 "invalid suffix \"%.*s\" with hexadecimal floating constant",
384 (int) (limit
- str
), str
);
385 return CPP_N_INVALID
;
388 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
389 cpp_error (pfile
, CPP_DL_PEDWARN
,
390 "fixed-point constants are a GCC extension");
392 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
393 cpp_error (pfile
, CPP_DL_PEDWARN
,
394 "decimal float constants are a GCC extension");
396 result
|= CPP_N_FLOATING
;
400 result
= interpret_int_suffix (str
, limit
- str
);
403 cpp_error (pfile
, CPP_DL_ERROR
,
404 "invalid suffix \"%.*s\" on integer constant",
405 (int) (limit
- str
), str
);
406 return CPP_N_INVALID
;
409 /* Traditional C only accepted the 'L' suffix.
410 Suppress warning about 'LL' with -Wno-long-long. */
411 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
413 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
414 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
;
416 if (u_or_i
|| (large
&& CPP_OPTION (pfile
, warn_long_long
)))
417 cpp_error (pfile
, CPP_DL_WARNING
,
418 "traditional C rejects the \"%.*s\" suffix",
419 (int) (limit
- str
), str
);
422 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
423 && CPP_OPTION (pfile
, warn_long_long
))
425 CPP_OPTION (pfile
, c99
) ? CPP_DL_WARNING
: CPP_DL_PEDWARN
,
426 CPP_OPTION (pfile
, cplusplus
)
427 ? "use of C++0x long long integer constant"
428 : "use of C99 long long integer constant");
430 result
|= CPP_N_INTEGER
;
434 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
435 cpp_error (pfile
, CPP_DL_PEDWARN
,
436 "imaginary constants are a GCC extension");
437 if (radix
== 2 && CPP_PEDANTIC (pfile
))
438 cpp_error (pfile
, CPP_DL_PEDWARN
,
439 "binary constants are a GCC extension");
442 result
|= CPP_N_DECIMAL
;
443 else if (radix
== 16)
446 result
|= CPP_N_BINARY
;
448 result
|= CPP_N_OCTAL
;
453 return CPP_N_INVALID
;
456 /* cpp_interpret_integer converts an integer constant into a cpp_num,
457 of precision options->precision.
459 We do not provide any interface for decimal->float conversion,
460 because the preprocessor doesn't need it and we don't want to
461 drag in GCC's floating point emulator. */
463 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
466 const uchar
*p
, *end
;
471 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
472 result
.overflow
= false;
474 p
= token
->val
.str
.text
;
475 end
= p
+ token
->val
.str
.len
;
477 /* Common case of a single digit. */
478 if (token
->val
.str
.len
== 1)
479 result
.low
= p
[0] - '0';
483 size_t precision
= CPP_OPTION (pfile
, precision
);
484 unsigned int base
= 10, c
= 0;
485 bool overflow
= false;
487 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
492 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
497 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
503 /* We can add a digit to numbers strictly less than this without
504 needing the precision and slowness of double integers. */
505 max
= ~(cpp_num_part
) 0;
506 if (precision
< PART_PRECISION
)
507 max
>>= PART_PRECISION
- precision
;
508 max
= (max
- base
+ 1) / base
+ 1;
514 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
519 /* Strict inequality for when max is set to zero. */
520 if (result
.low
< max
)
521 result
.low
= result
.low
* base
+ c
;
524 result
= append_digit (result
, c
, base
, precision
);
525 overflow
|= result
.overflow
;
531 cpp_error (pfile
, CPP_DL_PEDWARN
,
532 "integer constant is too large for its type");
533 /* If too big to be signed, consider it unsigned. Only warn for
534 decimal numbers. Traditional numbers were always signed (but
535 we still honor an explicit U suffix); but we only have
536 traditional semantics in directives. */
537 else if (!result
.unsignedp
538 && !(CPP_OPTION (pfile
, traditional
)
539 && pfile
->state
.in_directive
)
540 && !num_positive (result
, precision
))
542 /* This is for constants within the range of uintmax_t but
543 not that of intmax_t. For such decimal constants, a
544 diagnostic is required for C99 as the selected type must
545 be signed and not having a type is a constraint violation
546 (DR#298, TC3), so this must be a pedwarn. For C90,
547 unsigned long is specified to be used for a constant that
548 does not fit in signed long; if uintmax_t has the same
549 range as unsigned long this means only a warning is
550 appropriate here. C90 permits the preprocessor to use a
551 wider range than unsigned long in the compiler, so if
552 uintmax_t is wider than unsigned long no diagnostic is
553 required for such constants in preprocessor #if
554 expressions and the compiler will pedwarn for such
555 constants outside the range of unsigned long that reach
556 the compiler so a diagnostic is not required there
557 either; thus, pedwarn for C99 but use a plain warning for
560 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
563 "integer constant is so large that it is unsigned");
564 result
.unsignedp
= true;
571 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
573 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
578 cpp_num_part add_high
, add_low
;
580 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
581 need to worry about add_high overflowing. */
595 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
596 result
.high
= num
.high
<< shift
;
597 result
.low
= num
.low
<< shift
;
598 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
599 result
.unsignedp
= num
.unsignedp
;
603 add_low
= num
.low
<< 1;
604 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
607 add_high
= add_low
= 0;
609 if (add_low
+ digit
< add_low
)
613 if (result
.low
+ add_low
< result
.low
)
615 if (result
.high
+ add_high
< result
.high
)
618 result
.low
+= add_low
;
619 result
.high
+= add_high
;
620 result
.overflow
= overflow
;
622 /* The above code catches overflow of a cpp_num type. This catches
623 overflow of the (possibly shorter) target precision. */
624 num
.low
= result
.low
;
625 num
.high
= result
.high
;
626 result
= num_trim (result
, precision
);
627 if (!num_eq (result
, num
))
628 result
.overflow
= true;
633 /* Handle meeting "defined" in a preprocessor expression. */
635 parse_defined (cpp_reader
*pfile
)
639 cpp_hashnode
*node
= 0;
640 const cpp_token
*token
;
641 cpp_context
*initial_context
= pfile
->context
;
643 /* Don't expand macros. */
644 pfile
->state
.prevent_expansion
++;
646 token
= cpp_get_token (pfile
);
647 if (token
->type
== CPP_OPEN_PAREN
)
650 token
= cpp_get_token (pfile
);
653 if (token
->type
== CPP_NAME
)
655 node
= token
->val
.node
.node
;
656 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
658 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
664 cpp_error (pfile
, CPP_DL_ERROR
,
665 "operator \"defined\" requires an identifier");
666 if (token
->flags
& NAMED_OP
)
671 op
.type
= token
->type
;
672 cpp_error (pfile
, CPP_DL_ERROR
,
673 "(\"%s\" is an alternative token for \"%s\" in C++)",
674 cpp_token_as_text (pfile
, token
),
675 cpp_token_as_text (pfile
, &op
));
681 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
682 cpp_error (pfile
, CPP_DL_WARNING
,
683 "this use of \"defined\" may not be portable");
685 _cpp_mark_macro_used (node
);
686 if (!(node
->flags
& NODE_USED
))
688 node
->flags
|= NODE_USED
;
689 if (node
->type
== NT_MACRO
)
691 if (pfile
->cb
.used_define
)
692 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
696 if (pfile
->cb
.used_undef
)
697 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
701 /* A possible controlling macro of the form #if !defined ().
702 _cpp_parse_expr checks there was no other junk on the line. */
703 pfile
->mi_ind_cmacro
= node
;
706 pfile
->state
.prevent_expansion
--;
708 result
.unsignedp
= false;
710 result
.overflow
= false;
711 result
.low
= node
&& node
->type
== NT_MACRO
;
715 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
716 number or character constant, or the result of the "defined" or "#"
719 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
725 result
.unsignedp
= false;
726 result
.overflow
= false;
731 temp
= cpp_classify_number (pfile
, token
);
732 switch (temp
& CPP_N_CATEGORY
)
735 cpp_error (pfile
, CPP_DL_ERROR
,
736 "floating constant in preprocessor expression");
739 if (!(temp
& CPP_N_IMAGINARY
))
740 return cpp_interpret_integer (pfile
, token
, temp
);
741 cpp_error (pfile
, CPP_DL_ERROR
,
742 "imaginary number in preprocessor expression");
746 /* Error already issued. */
749 result
.high
= result
.low
= 0;
757 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
762 /* Sign-extend the result if necessary. */
763 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
765 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
766 result
.low
|= ~(~(cpp_num_part
) 0
767 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
768 result
.high
= ~(cpp_num_part
) 0;
769 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
775 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
776 return parse_defined (pfile
);
777 else if (CPP_OPTION (pfile
, cplusplus
)
778 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
779 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
782 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
788 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
789 cpp_error (pfile
, CPP_DL_WARNING
, "\"%s\" is not defined",
790 NODE_NAME (token
->val
.node
.node
));
795 if (!pfile
->state
.skipping
)
797 /* A pedantic warning takes precedence over a deprecated
799 if (CPP_PEDANTIC (pfile
))
800 cpp_error (pfile
, CPP_DL_PEDWARN
,
801 "assertions are a GCC extension");
802 else if (CPP_OPTION (pfile
, warn_deprecated
))
803 cpp_error (pfile
, CPP_DL_WARNING
,
804 "assertions are a deprecated extension");
806 _cpp_test_assertion (pfile
, &temp
);
815 result
.unsignedp
= !!unsignedp
;
819 /* Operator precedence and flags table.
821 After an operator is returned from the lexer, if it has priority less
822 than the operator on the top of the stack, we reduce the stack by one
823 operator and repeat the test. Since equal priorities do not reduce,
824 this is naturally right-associative.
826 We handle left-associative operators by decrementing the priority of
827 just-lexed operators by one, but retaining the priority of operators
828 already on the stack.
830 The remaining cases are '(' and ')'. We handle '(' by skipping the
831 reduction phase completely. ')' is given lower priority than
832 everything else, including '(', effectively forcing a reduction of the
833 parenthesized expression. If there is a matching '(', the routine
834 reduce() exits immediately. If the normal exit route sees a ')', then
835 there cannot have been a matching '(' and an error message is output.
837 The parser assumes all shifted operators require a left operand unless
838 the flag NO_L_OPERAND is set. These semantics are automatic; any
839 extra semantics need to be handled with operator-specific code. */
841 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
842 operand changes because of integer promotions. */
843 #define NO_L_OPERAND (1 << 0)
844 #define LEFT_ASSOC (1 << 1)
845 #define CHECK_PROMOTION (1 << 2)
847 /* Operator to priority map. Must be in the same order as the first
848 N entries of enum cpp_ttype. */
849 static const struct cpp_operator
855 /* EQ */ {0, 0}, /* Shouldn't happen. */
856 /* NOT */ {16, NO_L_OPERAND
},
857 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
858 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
859 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
860 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
861 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
862 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
863 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
864 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
865 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
866 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
867 /* RSHIFT */ {13, LEFT_ASSOC
},
868 /* LSHIFT */ {13, LEFT_ASSOC
},
870 /* COMPL */ {16, NO_L_OPERAND
},
871 /* AND_AND */ {6, LEFT_ASSOC
},
872 /* OR_OR */ {5, LEFT_ASSOC
},
873 /* Note that QUERY, COLON, and COMMA must have the same precedence.
874 However, there are some special cases for these in reduce(). */
876 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
877 /* COMMA */ {4, LEFT_ASSOC
},
878 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
879 /* CLOSE_PAREN */ {0, 0},
881 /* EQ_EQ */ {11, LEFT_ASSOC
},
882 /* NOT_EQ */ {11, LEFT_ASSOC
},
883 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
884 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
885 /* UPLUS */ {16, NO_L_OPERAND
},
886 /* UMINUS */ {16, NO_L_OPERAND
}
889 /* Parse and evaluate a C expression, reading from PFILE.
890 Returns the truth value of the expression.
892 The implementation is an operator precedence parser, i.e. a
893 bottom-up parser, using a stack for not-yet-reduced tokens.
895 The stack base is op_stack, and the current stack pointer is 'top'.
896 There is a stack element for each operator (only), and the most
897 recently pushed operator is 'top->op'. An operand (value) is
898 stored in the 'value' field of the stack element of the operator
901 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
903 struct op
*top
= pfile
->op_stack
;
904 unsigned int lex_count
;
905 bool saw_leading_not
, want_value
= true;
907 pfile
->state
.skip_eval
= 0;
909 /* Set up detection of #if ! defined(). */
910 pfile
->mi_ind_cmacro
= 0;
911 saw_leading_not
= false;
914 /* Lowest priority operator prevents further reductions. */
922 op
.token
= cpp_get_token (pfile
);
923 op
.op
= op
.token
->type
;
924 op
.loc
= op
.token
->src_loc
;
928 /* These tokens convert into values. */
937 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
938 cpp_token_as_text (pfile
, op
.token
));
940 top
->value
= eval_token (pfile
, op
.token
);
944 saw_leading_not
= lex_count
== 1;
956 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
957 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
958 cpp_token_as_text (pfile
, op
.token
));
962 /* Check we have a value or operator as appropriate. */
963 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
966 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
967 cpp_token_as_text (pfile
, op
.token
));
971 /* We want a number (or expression) and haven't got one.
972 Try to emit a specific diagnostic. */
973 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
974 SYNTAX_ERROR ("missing expression between '(' and ')'");
976 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
977 SYNTAX_ERROR2 ("%s with no expression", is_if
? "#if" : "#elif");
979 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
980 SYNTAX_ERROR2 ("operator '%s' has no right operand",
981 cpp_token_as_text (pfile
, top
->token
));
982 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
983 /* Complain about missing paren during reduction. */;
985 SYNTAX_ERROR2 ("operator '%s' has no left operand",
986 cpp_token_as_text (pfile
, op
.token
));
989 top
= reduce (pfile
, top
, op
.op
);
993 if (op
.op
== CPP_EOF
)
998 case CPP_CLOSE_PAREN
:
1001 if (!num_zerop (top
->value
))
1002 pfile
->state
.skip_eval
++;
1006 if (num_zerop (top
->value
))
1007 pfile
->state
.skip_eval
++;
1010 if (top
->op
!= CPP_QUERY
)
1011 SYNTAX_ERROR (" ':' without preceding '?'");
1012 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1013 pfile
->state
.skip_eval
++;
1015 pfile
->state
.skip_eval
--;
1022 /* Check for and handle stack overflow. */
1023 if (++top
== pfile
->op_limit
)
1024 top
= _cpp_expand_op_stack (pfile
);
1027 top
->token
= op
.token
;
1028 top
->loc
= op
.token
->src_loc
;
1031 /* The controlling macro expression is only valid if we called lex 3
1032 times: <!> <defined expression> and <EOF>. push_conditional ()
1033 checks that we are at top-of-file. */
1034 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1035 pfile
->mi_ind_cmacro
= 0;
1037 if (top
!= pfile
->op_stack
)
1039 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in %s",
1040 is_if
? "#if" : "#elif");
1042 return false; /* Return false on syntax error. */
1045 return !num_zerop (top
->value
);
1048 /* Reduce the operator / value stack if possible, in preparation for
1049 pushing operator OP. Returns NULL on error, otherwise the top of
1052 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1056 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1059 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1063 if (op
== CPP_OPEN_PAREN
)
1066 /* Decrement the priority of left-associative operators to force a
1067 reduction with operators of otherwise equal priority. */
1068 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1069 while (prio
< optab
[top
->op
].prio
)
1071 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1072 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1073 check_promotion (pfile
, top
);
1081 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1082 top
[-1].loc
= top
->loc
;
1090 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1091 top
->value
, top
->op
);
1092 top
[-1].loc
= top
->loc
;
1097 case CPP_GREATER_EQ
:
1100 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1101 top
[-1].loc
= top
->loc
;
1107 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1108 top
[-1].loc
= top
->loc
;
1115 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1116 top
[-1].loc
= top
->loc
;
1120 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1121 top
[-1].loc
= top
->loc
;
1126 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1127 top
->value
, top
->op
, top
->loc
);
1128 top
[-1].loc
= top
->loc
;
1133 if (!num_zerop (top
->value
))
1134 pfile
->state
.skip_eval
--;
1135 top
->value
.low
= (!num_zerop (top
->value
)
1136 || !num_zerop (top
[1].value
));
1137 top
->value
.high
= 0;
1138 top
->value
.unsignedp
= false;
1139 top
->value
.overflow
= false;
1140 top
->loc
= top
[1].loc
;
1145 if (num_zerop (top
->value
))
1146 pfile
->state
.skip_eval
--;
1147 top
->value
.low
= (!num_zerop (top
->value
)
1148 && !num_zerop (top
[1].value
));
1149 top
->value
.high
= 0;
1150 top
->value
.unsignedp
= false;
1151 top
->value
.overflow
= false;
1152 top
->loc
= top
[1].loc
;
1155 case CPP_OPEN_PAREN
:
1156 if (op
!= CPP_CLOSE_PAREN
)
1158 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1159 top
->token
->src_loc
,
1160 0, "missing ')' in expression");
1164 top
->value
= top
[1].value
;
1165 top
->loc
= top
[1].loc
;
1170 if (!num_zerop (top
->value
))
1172 pfile
->state
.skip_eval
--;
1173 top
->value
= top
[1].value
;
1174 top
->loc
= top
[1].loc
;
1178 top
->value
= top
[2].value
;
1179 top
->loc
= top
[2].loc
;
1181 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1182 || top
[2].value
.unsignedp
);
1186 /* COMMA and COLON should not reduce a QUERY operator. */
1187 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1189 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1197 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1198 cpp_error (pfile
, CPP_DL_PEDWARN
,
1199 "integer overflow in preprocessor expression");
1202 if (op
== CPP_CLOSE_PAREN
)
1204 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1211 /* Returns the position of the old top of stack after expansion. */
1213 _cpp_expand_op_stack (cpp_reader
*pfile
)
1215 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1216 size_t new_size
= old_size
* 2 + 20;
1218 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1219 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1221 return pfile
->op_stack
+ old_size
;
1224 /* Emits a warning if the effective sign of either operand of OP
1225 changes because of integer promotions. */
1227 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1229 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1232 if (op
->value
.unsignedp
)
1234 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1235 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1236 "the left operand of \"%s\" changes sign when promoted",
1237 cpp_token_as_text (pfile
, op
->token
));
1239 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1240 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1241 "the right operand of \"%s\" changes sign when promoted",
1242 cpp_token_as_text (pfile
, op
->token
));
1245 /* Clears the unused high order bits of the number pointed to by PNUM. */
1247 num_trim (cpp_num num
, size_t precision
)
1249 if (precision
> PART_PRECISION
)
1251 precision
-= PART_PRECISION
;
1252 if (precision
< PART_PRECISION
)
1253 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1257 if (precision
< PART_PRECISION
)
1258 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1265 /* True iff A (presumed signed) >= 0. */
1267 num_positive (cpp_num num
, size_t precision
)
1269 if (precision
> PART_PRECISION
)
1271 precision
-= PART_PRECISION
;
1272 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1275 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1278 /* Sign extend a number, with PRECISION significant bits and all
1279 others assumed clear, to fill out a cpp_num structure. */
1281 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1285 if (precision
> PART_PRECISION
)
1287 precision
-= PART_PRECISION
;
1288 if (precision
< PART_PRECISION
1289 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1290 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1292 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1294 if (precision
< PART_PRECISION
)
1295 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1296 num
.high
= ~(cpp_num_part
) 0;
1303 /* Returns the negative of NUM. */
1305 num_negate (cpp_num num
, size_t precision
)
1310 num
.high
= ~num
.high
;
1314 num
= num_trim (num
, precision
);
1315 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1320 /* Returns true if A >= B. */
1322 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1326 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1330 /* Both numbers have signed type. If they are of different
1331 sign, the answer is the sign of A. */
1332 unsignedp
= num_positive (pa
, precision
);
1334 if (unsignedp
!= num_positive (pb
, precision
))
1337 /* Otherwise we can do an unsigned comparison. */
1340 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1343 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1345 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1346 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1348 lhs
.overflow
= false;
1349 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1351 /* As excess precision is zeroed, there is no need to num_trim () as
1352 these operations cannot introduce a set bit there. */
1356 lhs
.high
&= rhs
.high
;
1358 else if (op
== CPP_OR
)
1361 lhs
.high
|= rhs
.high
;
1366 lhs
.high
^= rhs
.high
;
1372 /* Returns LHS OP RHS, where OP is an inequality. */
1374 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1377 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1379 if (op
== CPP_GREATER_EQ
)
1381 else if (op
== CPP_LESS
)
1383 else if (op
== CPP_GREATER
)
1384 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1385 else /* CPP_LESS_EQ. */
1386 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1389 lhs
.overflow
= false;
1390 lhs
.unsignedp
= false;
1394 /* Returns LHS OP RHS, where OP is == or !=. */
1396 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1397 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1399 /* Work around a 3.0.4 bug; see PR 6950. */
1400 bool eq
= num_eq (lhs
, rhs
);
1401 if (op
== CPP_NOT_EQ
)
1405 lhs
.overflow
= false;
1406 lhs
.unsignedp
= false;
1410 /* Shift NUM, of width PRECISION, right by N bits. */
1412 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1414 cpp_num_part sign_mask
;
1415 bool x
= num_positive (num
, precision
);
1417 if (num
.unsignedp
|| x
)
1420 sign_mask
= ~(cpp_num_part
) 0;
1423 num
.high
= num
.low
= sign_mask
;
1427 if (precision
< PART_PRECISION
)
1428 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1429 else if (precision
< 2 * PART_PRECISION
)
1430 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1432 if (n
>= PART_PRECISION
)
1434 n
-= PART_PRECISION
;
1436 num
.high
= sign_mask
;
1441 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1442 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1446 num
= num_trim (num
, precision
);
1447 num
.overflow
= false;
1451 /* Shift NUM, of width PRECISION, left by N bits. */
1453 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1457 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1458 num
.high
= num
.low
= 0;
1462 cpp_num orig
, maybe_orig
;
1466 if (m
>= PART_PRECISION
)
1468 m
-= PART_PRECISION
;
1474 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1477 num
= num_trim (num
, precision
);
1480 num
.overflow
= false;
1483 maybe_orig
= num_rshift (num
, precision
, n
);
1484 num
.overflow
= !num_eq (orig
, maybe_orig
);
1491 /* The four unary operators: +, -, ! and ~. */
1493 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1498 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1499 cpp_error (pfile
, CPP_DL_WARNING
,
1500 "traditional C rejects the unary plus operator");
1501 num
.overflow
= false;
1505 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1509 num
.high
= ~num
.high
;
1511 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1512 num
.overflow
= false;
1515 default: /* case CPP_NOT: */
1516 num
.low
= num_zerop (num
);
1518 num
.overflow
= false;
1519 num
.unsignedp
= false;
1526 /* The various binary operators. */
1528 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1531 size_t precision
= CPP_OPTION (pfile
, precision
);
1539 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1541 /* A negative shift is a positive shift the other way. */
1542 if (op
== CPP_LSHIFT
)
1546 rhs
= num_negate (rhs
, precision
);
1549 n
= ~0; /* Maximal. */
1552 if (op
== CPP_LSHIFT
)
1553 lhs
= num_lshift (lhs
, precision
, n
);
1555 lhs
= num_rshift (lhs
, precision
, n
);
1560 rhs
= num_negate (rhs
, precision
);
1562 result
.low
= lhs
.low
+ rhs
.low
;
1563 result
.high
= lhs
.high
+ rhs
.high
;
1564 if (result
.low
< lhs
.low
)
1566 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1567 result
.overflow
= false;
1569 result
= num_trim (result
, precision
);
1570 if (!result
.unsignedp
)
1572 bool lhsp
= num_positive (lhs
, precision
);
1573 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1574 && lhsp
!= num_positive (result
, precision
));
1579 default: /* case CPP_COMMA: */
1580 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1581 || !pfile
->state
.skip_eval
))
1582 cpp_error (pfile
, CPP_DL_PEDWARN
,
1583 "comma operator in operand of #if");
1591 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1594 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1597 cpp_num_part middle
[2], temp
;
1599 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1600 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1602 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1603 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1606 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1607 if (result
.low
< temp
)
1611 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1612 if (result
.low
< temp
)
1615 result
.high
+= HIGH_PART (middle
[0]);
1616 result
.high
+= HIGH_PART (middle
[1]);
1617 result
.unsignedp
= true;
1618 result
.overflow
= false;
1623 /* Multiply two preprocessing numbers. */
1625 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1627 cpp_num result
, temp
;
1628 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1629 bool overflow
, negate
= false;
1630 size_t precision
= CPP_OPTION (pfile
, precision
);
1632 /* Prepare for unsigned multiplication. */
1635 if (!num_positive (lhs
, precision
))
1636 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1637 if (!num_positive (rhs
, precision
))
1638 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1641 overflow
= lhs
.high
&& rhs
.high
;
1642 result
= num_part_mul (lhs
.low
, rhs
.low
);
1644 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1645 result
.high
+= temp
.low
;
1649 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1650 result
.high
+= temp
.low
;
1654 temp
.low
= result
.low
, temp
.high
= result
.high
;
1655 result
= num_trim (result
, precision
);
1656 if (!num_eq (result
, temp
))
1660 result
= num_negate (result
, precision
);
1663 result
.overflow
= false;
1665 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1666 && !num_zerop (result
));
1667 result
.unsignedp
= unsignedp
;
1672 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1673 or the remainder depending upon OP. LOCATION is the source location
1674 of this operator (for diagnostics). */
1677 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1678 source_location location
)
1680 cpp_num result
, sub
;
1682 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1683 bool negate
= false, lhs_neg
= false;
1684 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1686 /* Prepare for unsigned division. */
1689 if (!num_positive (lhs
, precision
))
1690 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1691 if (!num_positive (rhs
, precision
))
1692 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1695 /* Find the high bit. */
1699 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1700 for (; ; i
--, mask
>>= 1)
1701 if (rhs
.high
& mask
)
1706 if (precision
> PART_PRECISION
)
1707 i
= precision
- PART_PRECISION
- 1;
1710 mask
= (cpp_num_part
) 1 << i
;
1711 for (; ; i
--, mask
>>= 1)
1717 if (!pfile
->state
.skip_eval
)
1718 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
1719 "division by zero in #if");
1723 /* First nonzero bit of RHS is bit I. Do naive division by
1724 shifting the RHS fully left, and subtracting from LHS if LHS is
1725 at least as big, and then repeating but with one less shift.
1726 This is not very efficient, but is easy to understand. */
1728 rhs
.unsignedp
= true;
1729 lhs
.unsignedp
= true;
1730 i
= precision
- i
- 1;
1731 sub
= num_lshift (rhs
, precision
, i
);
1733 result
.high
= result
.low
= 0;
1736 if (num_greater_eq (lhs
, sub
, precision
))
1738 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1739 if (i
>= PART_PRECISION
)
1740 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1742 result
.low
|= (cpp_num_part
) 1 << i
;
1746 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1750 /* We divide so that the remainder has the sign of the LHS. */
1753 result
.unsignedp
= unsignedp
;
1754 result
.overflow
= false;
1758 result
= num_negate (result
, precision
);
1759 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1760 && !num_zerop (result
));
1767 lhs
.unsignedp
= unsignedp
;
1768 lhs
.overflow
= false;
1770 lhs
= num_negate (lhs
, precision
);