1 /* Parse C expressions for cpplib.
2 Copyright (C) 1987, 1992, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2002, 2004, 2008, 2009, 2010 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
;
234 /* If the lexer has done its job, length one can only be a single
235 digit. Fast-path this very common case. */
236 if (token
->val
.str
.len
== 1)
237 return CPP_N_INTEGER
| CPP_N_SMALL
| CPP_N_DECIMAL
;
239 limit
= str
+ token
->val
.str
.len
;
240 float_flag
= NOT_FLOAT
;
245 /* First, interpret the radix. */
251 /* Require at least one hex digit to classify it as hex. */
252 if ((*str
== 'x' || *str
== 'X')
253 && (str
[1] == '.' || ISXDIGIT (str
[1])))
258 else if ((*str
== 'b' || *str
== 'B') && (str
[1] == '0' || str
[1] == '1'))
265 /* Now scan for a well-formed integer or float. */
268 unsigned int c
= *str
++;
270 if (ISDIGIT (c
) || (ISXDIGIT (c
) && radix
== 16))
279 if (float_flag
== NOT_FLOAT
)
280 float_flag
= AFTER_POINT
;
282 SYNTAX_ERROR ("too many decimal points in number");
284 else if ((radix
<= 10 && (c
== 'e' || c
== 'E'))
285 || (radix
== 16 && (c
== 'p' || c
== 'P')))
287 float_flag
= AFTER_EXPON
;
292 /* Start of suffix. */
298 /* The suffix may be for decimal fixed-point constants without exponent. */
299 if (radix
!= 16 && float_flag
== NOT_FLOAT
)
301 result
= interpret_float_suffix (str
, limit
- str
);
302 if ((result
& CPP_N_FRACT
) || (result
& CPP_N_ACCUM
))
304 result
|= CPP_N_FLOATING
;
305 /* We need to restore the radix to 10, if the radix is 8. */
309 if (CPP_PEDANTIC (pfile
))
310 cpp_error (pfile
, CPP_DL_PEDWARN
,
311 "fixed-point constants are a GCC extension");
318 if (float_flag
!= NOT_FLOAT
&& radix
== 8)
321 if (max_digit
>= radix
)
324 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit
);
326 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit
);
329 if (float_flag
!= NOT_FLOAT
)
333 cpp_error (pfile
, CPP_DL_ERROR
,
334 "invalid prefix \"0b\" for floating constant");
335 return CPP_N_INVALID
;
338 if (radix
== 16 && !seen_digit
)
339 SYNTAX_ERROR ("no digits in hexadecimal floating constant");
341 if (radix
== 16 && CPP_PEDANTIC (pfile
) && !CPP_OPTION (pfile
, c99
))
342 cpp_error (pfile
, CPP_DL_PEDWARN
,
343 "use of C99 hexadecimal floating constant");
345 if (float_flag
== AFTER_EXPON
)
347 if (*str
== '+' || *str
== '-')
350 /* Exponent is decimal, even if string is a hex float. */
352 SYNTAX_ERROR ("exponent has no digits");
356 while (ISDIGIT (*str
));
358 else if (radix
== 16)
359 SYNTAX_ERROR ("hexadecimal floating constants require an exponent");
361 result
= interpret_float_suffix (str
, limit
- str
);
364 cpp_error (pfile
, CPP_DL_ERROR
,
365 "invalid suffix \"%.*s\" on floating constant",
366 (int) (limit
- str
), str
);
367 return CPP_N_INVALID
;
370 /* Traditional C didn't accept any floating suffixes. */
372 && CPP_WTRADITIONAL (pfile
)
373 && ! cpp_sys_macro_p (pfile
))
374 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
375 "traditional C rejects the \"%.*s\" suffix",
376 (int) (limit
- str
), str
);
378 /* A suffix for double is a GCC extension via decimal float support.
379 If the suffix also specifies an imaginary value we'll catch that
381 if ((result
== CPP_N_MEDIUM
) && CPP_PEDANTIC (pfile
))
382 cpp_error (pfile
, CPP_DL_PEDWARN
,
383 "suffix for double constant is a GCC extension");
385 /* Radix must be 10 for decimal floats. */
386 if ((result
& CPP_N_DFLOAT
) && radix
!= 10)
388 cpp_error (pfile
, CPP_DL_ERROR
,
389 "invalid suffix \"%.*s\" with hexadecimal floating constant",
390 (int) (limit
- str
), str
);
391 return CPP_N_INVALID
;
394 if ((result
& (CPP_N_FRACT
| CPP_N_ACCUM
)) && CPP_PEDANTIC (pfile
))
395 cpp_error (pfile
, CPP_DL_PEDWARN
,
396 "fixed-point constants are a GCC extension");
398 if ((result
& CPP_N_DFLOAT
) && CPP_PEDANTIC (pfile
))
399 cpp_error (pfile
, CPP_DL_PEDWARN
,
400 "decimal float constants are a GCC extension");
402 result
|= CPP_N_FLOATING
;
406 result
= interpret_int_suffix (str
, limit
- str
);
409 cpp_error (pfile
, CPP_DL_ERROR
,
410 "invalid suffix \"%.*s\" on integer constant",
411 (int) (limit
- str
), str
);
412 return CPP_N_INVALID
;
415 /* Traditional C only accepted the 'L' suffix.
416 Suppress warning about 'LL' with -Wno-long-long. */
417 if (CPP_WTRADITIONAL (pfile
) && ! cpp_sys_macro_p (pfile
))
419 int u_or_i
= (result
& (CPP_N_UNSIGNED
|CPP_N_IMAGINARY
));
420 int large
= (result
& CPP_N_WIDTH
) == CPP_N_LARGE
421 && CPP_OPTION (pfile
, warn_long_long
);
424 cpp_warning (pfile
, large
? CPP_W_LONG_LONG
: CPP_W_TRADITIONAL
,
425 "traditional C rejects the \"%.*s\" suffix",
426 (int) (limit
- str
), str
);
429 if ((result
& CPP_N_WIDTH
) == CPP_N_LARGE
430 && CPP_OPTION (pfile
, warn_long_long
))
432 const char *message
= CPP_OPTION (pfile
, cplusplus
)
433 ? N_("use of C++0x long long integer constant")
434 : N_("use of C99 long long integer constant");
436 if (CPP_OPTION (pfile
, c99
))
437 cpp_warning (pfile
, CPP_W_LONG_LONG
, message
);
439 cpp_pedwarning (pfile
, CPP_W_LONG_LONG
, message
);
442 result
|= CPP_N_INTEGER
;
446 if ((result
& CPP_N_IMAGINARY
) && CPP_PEDANTIC (pfile
))
447 cpp_error (pfile
, CPP_DL_PEDWARN
,
448 "imaginary constants are a GCC extension");
449 if (radix
== 2 && CPP_PEDANTIC (pfile
))
450 cpp_error (pfile
, CPP_DL_PEDWARN
,
451 "binary constants are a GCC extension");
454 result
|= CPP_N_DECIMAL
;
455 else if (radix
== 16)
458 result
|= CPP_N_BINARY
;
460 result
|= CPP_N_OCTAL
;
465 return CPP_N_INVALID
;
468 /* cpp_interpret_integer converts an integer constant into a cpp_num,
469 of precision options->precision.
471 We do not provide any interface for decimal->float conversion,
472 because the preprocessor doesn't need it and we don't want to
473 drag in GCC's floating point emulator. */
475 cpp_interpret_integer (cpp_reader
*pfile
, const cpp_token
*token
,
478 const uchar
*p
, *end
;
483 result
.unsignedp
= !!(type
& CPP_N_UNSIGNED
);
484 result
.overflow
= false;
486 p
= token
->val
.str
.text
;
487 end
= p
+ token
->val
.str
.len
;
489 /* Common case of a single digit. */
490 if (token
->val
.str
.len
== 1)
491 result
.low
= p
[0] - '0';
495 size_t precision
= CPP_OPTION (pfile
, precision
);
496 unsigned int base
= 10, c
= 0;
497 bool overflow
= false;
499 if ((type
& CPP_N_RADIX
) == CPP_N_OCTAL
)
504 else if ((type
& CPP_N_RADIX
) == CPP_N_HEX
)
509 else if ((type
& CPP_N_RADIX
) == CPP_N_BINARY
)
515 /* We can add a digit to numbers strictly less than this without
516 needing the precision and slowness of double integers. */
517 max
= ~(cpp_num_part
) 0;
518 if (precision
< PART_PRECISION
)
519 max
>>= PART_PRECISION
- precision
;
520 max
= (max
- base
+ 1) / base
+ 1;
526 if (ISDIGIT (c
) || (base
== 16 && ISXDIGIT (c
)))
531 /* Strict inequality for when max is set to zero. */
532 if (result
.low
< max
)
533 result
.low
= result
.low
* base
+ c
;
536 result
= append_digit (result
, c
, base
, precision
);
537 overflow
|= result
.overflow
;
543 cpp_error (pfile
, CPP_DL_PEDWARN
,
544 "integer constant is too large for its type");
545 /* If too big to be signed, consider it unsigned. Only warn for
546 decimal numbers. Traditional numbers were always signed (but
547 we still honor an explicit U suffix); but we only have
548 traditional semantics in directives. */
549 else if (!result
.unsignedp
550 && !(CPP_OPTION (pfile
, traditional
)
551 && pfile
->state
.in_directive
)
552 && !num_positive (result
, precision
))
554 /* This is for constants within the range of uintmax_t but
555 not that of intmax_t. For such decimal constants, a
556 diagnostic is required for C99 as the selected type must
557 be signed and not having a type is a constraint violation
558 (DR#298, TC3), so this must be a pedwarn. For C90,
559 unsigned long is specified to be used for a constant that
560 does not fit in signed long; if uintmax_t has the same
561 range as unsigned long this means only a warning is
562 appropriate here. C90 permits the preprocessor to use a
563 wider range than unsigned long in the compiler, so if
564 uintmax_t is wider than unsigned long no diagnostic is
565 required for such constants in preprocessor #if
566 expressions and the compiler will pedwarn for such
567 constants outside the range of unsigned long that reach
568 the compiler so a diagnostic is not required there
569 either; thus, pedwarn for C99 but use a plain warning for
572 cpp_error (pfile
, (CPP_OPTION (pfile
, c99
)
575 "integer constant is so large that it is unsigned");
576 result
.unsignedp
= true;
583 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */
585 append_digit (cpp_num num
, int digit
, int base
, size_t precision
)
590 cpp_num_part add_high
, add_low
;
592 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't
593 need to worry about add_high overflowing. */
607 overflow
= !!(num
.high
>> (PART_PRECISION
- shift
));
608 result
.high
= num
.high
<< shift
;
609 result
.low
= num
.low
<< shift
;
610 result
.high
|= num
.low
>> (PART_PRECISION
- shift
);
611 result
.unsignedp
= num
.unsignedp
;
615 add_low
= num
.low
<< 1;
616 add_high
= (num
.high
<< 1) + (num
.low
>> (PART_PRECISION
- 1));
619 add_high
= add_low
= 0;
621 if (add_low
+ digit
< add_low
)
625 if (result
.low
+ add_low
< result
.low
)
627 if (result
.high
+ add_high
< result
.high
)
630 result
.low
+= add_low
;
631 result
.high
+= add_high
;
632 result
.overflow
= overflow
;
634 /* The above code catches overflow of a cpp_num type. This catches
635 overflow of the (possibly shorter) target precision. */
636 num
.low
= result
.low
;
637 num
.high
= result
.high
;
638 result
= num_trim (result
, precision
);
639 if (!num_eq (result
, num
))
640 result
.overflow
= true;
645 /* Handle meeting "defined" in a preprocessor expression. */
647 parse_defined (cpp_reader
*pfile
)
651 cpp_hashnode
*node
= 0;
652 const cpp_token
*token
;
653 cpp_context
*initial_context
= pfile
->context
;
655 /* Don't expand macros. */
656 pfile
->state
.prevent_expansion
++;
658 token
= cpp_get_token (pfile
);
659 if (token
->type
== CPP_OPEN_PAREN
)
662 token
= cpp_get_token (pfile
);
665 if (token
->type
== CPP_NAME
)
667 node
= token
->val
.node
.node
;
668 if (paren
&& cpp_get_token (pfile
)->type
!= CPP_CLOSE_PAREN
)
670 cpp_error (pfile
, CPP_DL_ERROR
, "missing ')' after \"defined\"");
676 cpp_error (pfile
, CPP_DL_ERROR
,
677 "operator \"defined\" requires an identifier");
678 if (token
->flags
& NAMED_OP
)
683 op
.type
= token
->type
;
684 cpp_error (pfile
, CPP_DL_ERROR
,
685 "(\"%s\" is an alternative token for \"%s\" in C++)",
686 cpp_token_as_text (pfile
, token
),
687 cpp_token_as_text (pfile
, &op
));
693 if (pfile
->context
!= initial_context
&& CPP_PEDANTIC (pfile
))
694 cpp_error (pfile
, CPP_DL_WARNING
,
695 "this use of \"defined\" may not be portable");
697 _cpp_mark_macro_used (node
);
698 if (!(node
->flags
& NODE_USED
))
700 node
->flags
|= NODE_USED
;
701 if (node
->type
== NT_MACRO
)
703 if (pfile
->cb
.used_define
)
704 pfile
->cb
.used_define (pfile
, pfile
->directive_line
, node
);
708 if (pfile
->cb
.used_undef
)
709 pfile
->cb
.used_undef (pfile
, pfile
->directive_line
, node
);
713 /* A possible controlling macro of the form #if !defined ().
714 _cpp_parse_expr checks there was no other junk on the line. */
715 pfile
->mi_ind_cmacro
= node
;
718 pfile
->state
.prevent_expansion
--;
720 result
.unsignedp
= false;
722 result
.overflow
= false;
723 result
.low
= node
&& node
->type
== NT_MACRO
;
727 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing
728 number or character constant, or the result of the "defined" or "#"
731 eval_token (cpp_reader
*pfile
, const cpp_token
*token
)
737 result
.unsignedp
= false;
738 result
.overflow
= false;
743 temp
= cpp_classify_number (pfile
, token
);
744 switch (temp
& CPP_N_CATEGORY
)
747 cpp_error (pfile
, CPP_DL_ERROR
,
748 "floating constant in preprocessor expression");
751 if (!(temp
& CPP_N_IMAGINARY
))
752 return cpp_interpret_integer (pfile
, token
, temp
);
753 cpp_error (pfile
, CPP_DL_ERROR
,
754 "imaginary number in preprocessor expression");
758 /* Error already issued. */
761 result
.high
= result
.low
= 0;
769 cppchar_t cc
= cpp_interpret_charconst (pfile
, token
,
774 /* Sign-extend the result if necessary. */
775 if (!unsignedp
&& (cppchar_signed_t
) cc
< 0)
777 if (PART_PRECISION
> BITS_PER_CPPCHAR_T
)
778 result
.low
|= ~(~(cpp_num_part
) 0
779 >> (PART_PRECISION
- BITS_PER_CPPCHAR_T
));
780 result
.high
= ~(cpp_num_part
) 0;
781 result
= num_trim (result
, CPP_OPTION (pfile
, precision
));
787 if (token
->val
.node
.node
== pfile
->spec_nodes
.n_defined
)
788 return parse_defined (pfile
);
789 else if (CPP_OPTION (pfile
, cplusplus
)
790 && (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
791 || token
->val
.node
.node
== pfile
->spec_nodes
.n_false
))
794 result
.low
= (token
->val
.node
.node
== pfile
->spec_nodes
.n_true
);
800 if (CPP_OPTION (pfile
, warn_undef
) && !pfile
->state
.skip_eval
)
801 cpp_warning (pfile
, CPP_W_UNDEF
, "\"%s\" is not defined",
802 NODE_NAME (token
->val
.node
.node
));
807 if (!pfile
->state
.skipping
)
809 /* A pedantic warning takes precedence over a deprecated
811 if (CPP_PEDANTIC (pfile
))
812 cpp_error (pfile
, CPP_DL_PEDWARN
,
813 "assertions are a GCC extension");
814 else if (CPP_OPTION (pfile
, warn_deprecated
))
815 cpp_warning (pfile
, CPP_W_DEPRECATED
,
816 "assertions are a deprecated extension");
818 _cpp_test_assertion (pfile
, &temp
);
827 result
.unsignedp
= !!unsignedp
;
831 /* Operator precedence and flags table.
833 After an operator is returned from the lexer, if it has priority less
834 than the operator on the top of the stack, we reduce the stack by one
835 operator and repeat the test. Since equal priorities do not reduce,
836 this is naturally right-associative.
838 We handle left-associative operators by decrementing the priority of
839 just-lexed operators by one, but retaining the priority of operators
840 already on the stack.
842 The remaining cases are '(' and ')'. We handle '(' by skipping the
843 reduction phase completely. ')' is given lower priority than
844 everything else, including '(', effectively forcing a reduction of the
845 parenthesized expression. If there is a matching '(', the routine
846 reduce() exits immediately. If the normal exit route sees a ')', then
847 there cannot have been a matching '(' and an error message is output.
849 The parser assumes all shifted operators require a left operand unless
850 the flag NO_L_OPERAND is set. These semantics are automatic; any
851 extra semantics need to be handled with operator-specific code. */
853 /* Flags. If CHECK_PROMOTION, we warn if the effective sign of an
854 operand changes because of integer promotions. */
855 #define NO_L_OPERAND (1 << 0)
856 #define LEFT_ASSOC (1 << 1)
857 #define CHECK_PROMOTION (1 << 2)
859 /* Operator to priority map. Must be in the same order as the first
860 N entries of enum cpp_ttype. */
861 static const struct cpp_operator
867 /* EQ */ {0, 0}, /* Shouldn't happen. */
868 /* NOT */ {16, NO_L_OPERAND
},
869 /* GREATER */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
870 /* LESS */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
871 /* PLUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
872 /* MINUS */ {14, LEFT_ASSOC
| CHECK_PROMOTION
},
873 /* MULT */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
874 /* DIV */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
875 /* MOD */ {15, LEFT_ASSOC
| CHECK_PROMOTION
},
876 /* AND */ {9, LEFT_ASSOC
| CHECK_PROMOTION
},
877 /* OR */ {7, LEFT_ASSOC
| CHECK_PROMOTION
},
878 /* XOR */ {8, LEFT_ASSOC
| CHECK_PROMOTION
},
879 /* RSHIFT */ {13, LEFT_ASSOC
},
880 /* LSHIFT */ {13, LEFT_ASSOC
},
882 /* COMPL */ {16, NO_L_OPERAND
},
883 /* AND_AND */ {6, LEFT_ASSOC
},
884 /* OR_OR */ {5, LEFT_ASSOC
},
885 /* Note that QUERY, COLON, and COMMA must have the same precedence.
886 However, there are some special cases for these in reduce(). */
888 /* COLON */ {4, LEFT_ASSOC
| CHECK_PROMOTION
},
889 /* COMMA */ {4, LEFT_ASSOC
},
890 /* OPEN_PAREN */ {1, NO_L_OPERAND
},
891 /* CLOSE_PAREN */ {0, 0},
893 /* EQ_EQ */ {11, LEFT_ASSOC
},
894 /* NOT_EQ */ {11, LEFT_ASSOC
},
895 /* GREATER_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
896 /* LESS_EQ */ {12, LEFT_ASSOC
| CHECK_PROMOTION
},
897 /* UPLUS */ {16, NO_L_OPERAND
},
898 /* UMINUS */ {16, NO_L_OPERAND
}
901 /* Parse and evaluate a C expression, reading from PFILE.
902 Returns the truth value of the expression.
904 The implementation is an operator precedence parser, i.e. a
905 bottom-up parser, using a stack for not-yet-reduced tokens.
907 The stack base is op_stack, and the current stack pointer is 'top'.
908 There is a stack element for each operator (only), and the most
909 recently pushed operator is 'top->op'. An operand (value) is
910 stored in the 'value' field of the stack element of the operator
913 _cpp_parse_expr (cpp_reader
*pfile
, bool is_if
)
915 struct op
*top
= pfile
->op_stack
;
916 unsigned int lex_count
;
917 bool saw_leading_not
, want_value
= true;
919 pfile
->state
.skip_eval
= 0;
921 /* Set up detection of #if ! defined(). */
922 pfile
->mi_ind_cmacro
= 0;
923 saw_leading_not
= false;
926 /* Lowest priority operator prevents further reductions. */
934 op
.token
= cpp_get_token (pfile
);
935 op
.op
= op
.token
->type
;
936 op
.loc
= op
.token
->src_loc
;
940 /* These tokens convert into values. */
949 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
950 cpp_token_as_text (pfile
, op
.token
));
952 top
->value
= eval_token (pfile
, op
.token
);
956 saw_leading_not
= lex_count
== 1;
968 if ((int) op
.op
<= (int) CPP_EQ
|| (int) op
.op
>= (int) CPP_PLUS_EQ
)
969 SYNTAX_ERROR2 ("token \"%s\" is not valid in preprocessor expressions",
970 cpp_token_as_text (pfile
, op
.token
));
974 /* Check we have a value or operator as appropriate. */
975 if (optab
[op
.op
].flags
& NO_L_OPERAND
)
978 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"",
979 cpp_token_as_text (pfile
, op
.token
));
983 /* We want a number (or expression) and haven't got one.
984 Try to emit a specific diagnostic. */
985 if (op
.op
== CPP_CLOSE_PAREN
&& top
->op
== CPP_OPEN_PAREN
)
986 SYNTAX_ERROR ("missing expression between '(' and ')'");
988 if (op
.op
== CPP_EOF
&& top
->op
== CPP_EOF
)
989 SYNTAX_ERROR2 ("%s with no expression", is_if
? "#if" : "#elif");
991 if (top
->op
!= CPP_EOF
&& top
->op
!= CPP_OPEN_PAREN
)
992 SYNTAX_ERROR2 ("operator '%s' has no right operand",
993 cpp_token_as_text (pfile
, top
->token
));
994 else if (op
.op
== CPP_CLOSE_PAREN
|| op
.op
== CPP_EOF
)
995 /* Complain about missing paren during reduction. */;
997 SYNTAX_ERROR2 ("operator '%s' has no left operand",
998 cpp_token_as_text (pfile
, op
.token
));
1001 top
= reduce (pfile
, top
, op
.op
);
1005 if (op
.op
== CPP_EOF
)
1010 case CPP_CLOSE_PAREN
:
1013 if (!num_zerop (top
->value
))
1014 pfile
->state
.skip_eval
++;
1018 if (num_zerop (top
->value
))
1019 pfile
->state
.skip_eval
++;
1022 if (top
->op
!= CPP_QUERY
)
1023 SYNTAX_ERROR (" ':' without preceding '?'");
1024 if (!num_zerop (top
[-1].value
)) /* Was '?' condition true? */
1025 pfile
->state
.skip_eval
++;
1027 pfile
->state
.skip_eval
--;
1034 /* Check for and handle stack overflow. */
1035 if (++top
== pfile
->op_limit
)
1036 top
= _cpp_expand_op_stack (pfile
);
1039 top
->token
= op
.token
;
1040 top
->loc
= op
.token
->src_loc
;
1043 /* The controlling macro expression is only valid if we called lex 3
1044 times: <!> <defined expression> and <EOF>. push_conditional ()
1045 checks that we are at top-of-file. */
1046 if (pfile
->mi_ind_cmacro
&& !(saw_leading_not
&& lex_count
== 3))
1047 pfile
->mi_ind_cmacro
= 0;
1049 if (top
!= pfile
->op_stack
)
1051 cpp_error (pfile
, CPP_DL_ICE
, "unbalanced stack in %s",
1052 is_if
? "#if" : "#elif");
1054 return false; /* Return false on syntax error. */
1057 return !num_zerop (top
->value
);
1060 /* Reduce the operator / value stack if possible, in preparation for
1061 pushing operator OP. Returns NULL on error, otherwise the top of
1064 reduce (cpp_reader
*pfile
, struct op
*top
, enum cpp_ttype op
)
1068 if (top
->op
<= CPP_EQ
|| top
->op
> CPP_LAST_CPP_OP
+ 2)
1071 cpp_error (pfile
, CPP_DL_ICE
, "impossible operator '%u'", top
->op
);
1075 if (op
== CPP_OPEN_PAREN
)
1078 /* Decrement the priority of left-associative operators to force a
1079 reduction with operators of otherwise equal priority. */
1080 prio
= optab
[op
].prio
- ((optab
[op
].flags
& LEFT_ASSOC
) != 0);
1081 while (prio
< optab
[top
->op
].prio
)
1083 if (CPP_OPTION (pfile
, warn_num_sign_change
)
1084 && optab
[top
->op
].flags
& CHECK_PROMOTION
)
1085 check_promotion (pfile
, top
);
1093 top
[-1].value
= num_unary_op (pfile
, top
->value
, top
->op
);
1094 top
[-1].loc
= top
->loc
;
1102 top
[-1].value
= num_binary_op (pfile
, top
[-1].value
,
1103 top
->value
, top
->op
);
1104 top
[-1].loc
= top
->loc
;
1109 case CPP_GREATER_EQ
:
1112 = num_inequality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1113 top
[-1].loc
= top
->loc
;
1119 = num_equality_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1120 top
[-1].loc
= top
->loc
;
1127 = num_bitwise_op (pfile
, top
[-1].value
, top
->value
, top
->op
);
1128 top
[-1].loc
= top
->loc
;
1132 top
[-1].value
= num_mul (pfile
, top
[-1].value
, top
->value
);
1133 top
[-1].loc
= top
->loc
;
1138 top
[-1].value
= num_div_op (pfile
, top
[-1].value
,
1139 top
->value
, top
->op
, top
->loc
);
1140 top
[-1].loc
= top
->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
;
1157 if (num_zerop (top
->value
))
1158 pfile
->state
.skip_eval
--;
1159 top
->value
.low
= (!num_zerop (top
->value
)
1160 && !num_zerop (top
[1].value
));
1161 top
->value
.high
= 0;
1162 top
->value
.unsignedp
= false;
1163 top
->value
.overflow
= false;
1164 top
->loc
= top
[1].loc
;
1167 case CPP_OPEN_PAREN
:
1168 if (op
!= CPP_CLOSE_PAREN
)
1170 cpp_error_with_line (pfile
, CPP_DL_ERROR
,
1171 top
->token
->src_loc
,
1172 0, "missing ')' in expression");
1176 top
->value
= top
[1].value
;
1177 top
->loc
= top
[1].loc
;
1182 if (!num_zerop (top
->value
))
1184 pfile
->state
.skip_eval
--;
1185 top
->value
= top
[1].value
;
1186 top
->loc
= top
[1].loc
;
1190 top
->value
= top
[2].value
;
1191 top
->loc
= top
[2].loc
;
1193 top
->value
.unsignedp
= (top
[1].value
.unsignedp
1194 || top
[2].value
.unsignedp
);
1198 /* COMMA and COLON should not reduce a QUERY operator. */
1199 if (op
== CPP_COMMA
|| op
== CPP_COLON
)
1201 cpp_error (pfile
, CPP_DL_ERROR
, "'?' without following ':'");
1209 if (top
->value
.overflow
&& !pfile
->state
.skip_eval
)
1210 cpp_error (pfile
, CPP_DL_PEDWARN
,
1211 "integer overflow in preprocessor expression");
1214 if (op
== CPP_CLOSE_PAREN
)
1216 cpp_error (pfile
, CPP_DL_ERROR
, "missing '(' in expression");
1223 /* Returns the position of the old top of stack after expansion. */
1225 _cpp_expand_op_stack (cpp_reader
*pfile
)
1227 size_t old_size
= (size_t) (pfile
->op_limit
- pfile
->op_stack
);
1228 size_t new_size
= old_size
* 2 + 20;
1230 pfile
->op_stack
= XRESIZEVEC (struct op
, pfile
->op_stack
, new_size
);
1231 pfile
->op_limit
= pfile
->op_stack
+ new_size
;
1233 return pfile
->op_stack
+ old_size
;
1236 /* Emits a warning if the effective sign of either operand of OP
1237 changes because of integer promotions. */
1239 check_promotion (cpp_reader
*pfile
, const struct op
*op
)
1241 if (op
->value
.unsignedp
== op
[-1].value
.unsignedp
)
1244 if (op
->value
.unsignedp
)
1246 if (!num_positive (op
[-1].value
, CPP_OPTION (pfile
, precision
)))
1247 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
[-1].loc
, 0,
1248 "the left operand of \"%s\" changes sign when promoted",
1249 cpp_token_as_text (pfile
, op
->token
));
1251 else if (!num_positive (op
->value
, CPP_OPTION (pfile
, precision
)))
1252 cpp_error_with_line (pfile
, CPP_DL_WARNING
, op
->loc
, 0,
1253 "the right operand of \"%s\" changes sign when promoted",
1254 cpp_token_as_text (pfile
, op
->token
));
1257 /* Clears the unused high order bits of the number pointed to by PNUM. */
1259 num_trim (cpp_num num
, size_t precision
)
1261 if (precision
> PART_PRECISION
)
1263 precision
-= PART_PRECISION
;
1264 if (precision
< PART_PRECISION
)
1265 num
.high
&= ((cpp_num_part
) 1 << precision
) - 1;
1269 if (precision
< PART_PRECISION
)
1270 num
.low
&= ((cpp_num_part
) 1 << precision
) - 1;
1277 /* True iff A (presumed signed) >= 0. */
1279 num_positive (cpp_num num
, size_t precision
)
1281 if (precision
> PART_PRECISION
)
1283 precision
-= PART_PRECISION
;
1284 return (num
.high
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1287 return (num
.low
& (cpp_num_part
) 1 << (precision
- 1)) == 0;
1290 /* Sign extend a number, with PRECISION significant bits and all
1291 others assumed clear, to fill out a cpp_num structure. */
1293 cpp_num_sign_extend (cpp_num num
, size_t precision
)
1297 if (precision
> PART_PRECISION
)
1299 precision
-= PART_PRECISION
;
1300 if (precision
< PART_PRECISION
1301 && (num
.high
& (cpp_num_part
) 1 << (precision
- 1)))
1302 num
.high
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1304 else if (num
.low
& (cpp_num_part
) 1 << (precision
- 1))
1306 if (precision
< PART_PRECISION
)
1307 num
.low
|= ~(~(cpp_num_part
) 0 >> (PART_PRECISION
- precision
));
1308 num
.high
= ~(cpp_num_part
) 0;
1315 /* Returns the negative of NUM. */
1317 num_negate (cpp_num num
, size_t precision
)
1322 num
.high
= ~num
.high
;
1326 num
= num_trim (num
, precision
);
1327 num
.overflow
= (!num
.unsignedp
&& num_eq (num
, copy
) && !num_zerop (num
));
1332 /* Returns true if A >= B. */
1334 num_greater_eq (cpp_num pa
, cpp_num pb
, size_t precision
)
1338 unsignedp
= pa
.unsignedp
|| pb
.unsignedp
;
1342 /* Both numbers have signed type. If they are of different
1343 sign, the answer is the sign of A. */
1344 unsignedp
= num_positive (pa
, precision
);
1346 if (unsignedp
!= num_positive (pb
, precision
))
1349 /* Otherwise we can do an unsigned comparison. */
1352 return (pa
.high
> pb
.high
) || (pa
.high
== pb
.high
&& pa
.low
>= pb
.low
);
1355 /* Returns LHS OP RHS, where OP is a bit-wise operation. */
1357 num_bitwise_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1358 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1360 lhs
.overflow
= false;
1361 lhs
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1363 /* As excess precision is zeroed, there is no need to num_trim () as
1364 these operations cannot introduce a set bit there. */
1368 lhs
.high
&= rhs
.high
;
1370 else if (op
== CPP_OR
)
1373 lhs
.high
|= rhs
.high
;
1378 lhs
.high
^= rhs
.high
;
1384 /* Returns LHS OP RHS, where OP is an inequality. */
1386 num_inequality_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
,
1389 bool gte
= num_greater_eq (lhs
, rhs
, CPP_OPTION (pfile
, precision
));
1391 if (op
== CPP_GREATER_EQ
)
1393 else if (op
== CPP_LESS
)
1395 else if (op
== CPP_GREATER
)
1396 lhs
.low
= gte
&& !num_eq (lhs
, rhs
);
1397 else /* CPP_LESS_EQ. */
1398 lhs
.low
= !gte
|| num_eq (lhs
, rhs
);
1401 lhs
.overflow
= false;
1402 lhs
.unsignedp
= false;
1406 /* Returns LHS OP RHS, where OP is == or !=. */
1408 num_equality_op (cpp_reader
*pfile ATTRIBUTE_UNUSED
,
1409 cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1411 /* Work around a 3.0.4 bug; see PR 6950. */
1412 bool eq
= num_eq (lhs
, rhs
);
1413 if (op
== CPP_NOT_EQ
)
1417 lhs
.overflow
= false;
1418 lhs
.unsignedp
= false;
1422 /* Shift NUM, of width PRECISION, right by N bits. */
1424 num_rshift (cpp_num num
, size_t precision
, size_t n
)
1426 cpp_num_part sign_mask
;
1427 bool x
= num_positive (num
, precision
);
1429 if (num
.unsignedp
|| x
)
1432 sign_mask
= ~(cpp_num_part
) 0;
1435 num
.high
= num
.low
= sign_mask
;
1439 if (precision
< PART_PRECISION
)
1440 num
.high
= sign_mask
, num
.low
|= sign_mask
<< precision
;
1441 else if (precision
< 2 * PART_PRECISION
)
1442 num
.high
|= sign_mask
<< (precision
- PART_PRECISION
);
1444 if (n
>= PART_PRECISION
)
1446 n
-= PART_PRECISION
;
1448 num
.high
= sign_mask
;
1453 num
.low
= (num
.low
>> n
) | (num
.high
<< (PART_PRECISION
- n
));
1454 num
.high
= (num
.high
>> n
) | (sign_mask
<< (PART_PRECISION
- n
));
1458 num
= num_trim (num
, precision
);
1459 num
.overflow
= false;
1463 /* Shift NUM, of width PRECISION, left by N bits. */
1465 num_lshift (cpp_num num
, size_t precision
, size_t n
)
1469 num
.overflow
= !num
.unsignedp
&& !num_zerop (num
);
1470 num
.high
= num
.low
= 0;
1474 cpp_num orig
, maybe_orig
;
1478 if (m
>= PART_PRECISION
)
1480 m
-= PART_PRECISION
;
1486 num
.high
= (num
.high
<< m
) | (num
.low
>> (PART_PRECISION
- m
));
1489 num
= num_trim (num
, precision
);
1492 num
.overflow
= false;
1495 maybe_orig
= num_rshift (num
, precision
, n
);
1496 num
.overflow
= !num_eq (orig
, maybe_orig
);
1503 /* The four unary operators: +, -, ! and ~. */
1505 num_unary_op (cpp_reader
*pfile
, cpp_num num
, enum cpp_ttype op
)
1510 if (CPP_WTRADITIONAL (pfile
) && !pfile
->state
.skip_eval
)
1511 cpp_warning (pfile
, CPP_W_TRADITIONAL
,
1512 "traditional C rejects the unary plus operator");
1513 num
.overflow
= false;
1517 num
= num_negate (num
, CPP_OPTION (pfile
, precision
));
1521 num
.high
= ~num
.high
;
1523 num
= num_trim (num
, CPP_OPTION (pfile
, precision
));
1524 num
.overflow
= false;
1527 default: /* case CPP_NOT: */
1528 num
.low
= num_zerop (num
);
1530 num
.overflow
= false;
1531 num
.unsignedp
= false;
1538 /* The various binary operators. */
1540 num_binary_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
)
1543 size_t precision
= CPP_OPTION (pfile
, precision
);
1551 if (!rhs
.unsignedp
&& !num_positive (rhs
, precision
))
1553 /* A negative shift is a positive shift the other way. */
1554 if (op
== CPP_LSHIFT
)
1558 rhs
= num_negate (rhs
, precision
);
1561 n
= ~0; /* Maximal. */
1564 if (op
== CPP_LSHIFT
)
1565 lhs
= num_lshift (lhs
, precision
, n
);
1567 lhs
= num_rshift (lhs
, precision
, n
);
1572 rhs
= num_negate (rhs
, precision
);
1574 result
.low
= lhs
.low
+ rhs
.low
;
1575 result
.high
= lhs
.high
+ rhs
.high
;
1576 if (result
.low
< lhs
.low
)
1578 result
.unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1579 result
.overflow
= false;
1581 result
= num_trim (result
, precision
);
1582 if (!result
.unsignedp
)
1584 bool lhsp
= num_positive (lhs
, precision
);
1585 result
.overflow
= (lhsp
== num_positive (rhs
, precision
)
1586 && lhsp
!= num_positive (result
, precision
));
1591 default: /* case CPP_COMMA: */
1592 if (CPP_PEDANTIC (pfile
) && (!CPP_OPTION (pfile
, c99
)
1593 || !pfile
->state
.skip_eval
))
1594 cpp_error (pfile
, CPP_DL_PEDWARN
,
1595 "comma operator in operand of #if");
1603 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This
1606 num_part_mul (cpp_num_part lhs
, cpp_num_part rhs
)
1609 cpp_num_part middle
[2], temp
;
1611 result
.low
= LOW_PART (lhs
) * LOW_PART (rhs
);
1612 result
.high
= HIGH_PART (lhs
) * HIGH_PART (rhs
);
1614 middle
[0] = LOW_PART (lhs
) * HIGH_PART (rhs
);
1615 middle
[1] = HIGH_PART (lhs
) * LOW_PART (rhs
);
1618 result
.low
+= LOW_PART (middle
[0]) << (PART_PRECISION
/ 2);
1619 if (result
.low
< temp
)
1623 result
.low
+= LOW_PART (middle
[1]) << (PART_PRECISION
/ 2);
1624 if (result
.low
< temp
)
1627 result
.high
+= HIGH_PART (middle
[0]);
1628 result
.high
+= HIGH_PART (middle
[1]);
1629 result
.unsignedp
= true;
1630 result
.overflow
= false;
1635 /* Multiply two preprocessing numbers. */
1637 num_mul (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
)
1639 cpp_num result
, temp
;
1640 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1641 bool overflow
, negate
= false;
1642 size_t precision
= CPP_OPTION (pfile
, precision
);
1644 /* Prepare for unsigned multiplication. */
1647 if (!num_positive (lhs
, precision
))
1648 negate
= !negate
, lhs
= num_negate (lhs
, precision
);
1649 if (!num_positive (rhs
, precision
))
1650 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1653 overflow
= lhs
.high
&& rhs
.high
;
1654 result
= num_part_mul (lhs
.low
, rhs
.low
);
1656 temp
= num_part_mul (lhs
.high
, rhs
.low
);
1657 result
.high
+= temp
.low
;
1661 temp
= num_part_mul (lhs
.low
, rhs
.high
);
1662 result
.high
+= temp
.low
;
1666 temp
.low
= result
.low
, temp
.high
= result
.high
;
1667 result
= num_trim (result
, precision
);
1668 if (!num_eq (result
, temp
))
1672 result
= num_negate (result
, precision
);
1675 result
.overflow
= false;
1677 result
.overflow
= overflow
|| (num_positive (result
, precision
) ^ !negate
1678 && !num_zerop (result
));
1679 result
.unsignedp
= unsignedp
;
1684 /* Divide two preprocessing numbers, LHS and RHS, returning the answer
1685 or the remainder depending upon OP. LOCATION is the source location
1686 of this operator (for diagnostics). */
1689 num_div_op (cpp_reader
*pfile
, cpp_num lhs
, cpp_num rhs
, enum cpp_ttype op
,
1690 source_location location
)
1692 cpp_num result
, sub
;
1694 bool unsignedp
= lhs
.unsignedp
|| rhs
.unsignedp
;
1695 bool negate
= false, lhs_neg
= false;
1696 size_t i
, precision
= CPP_OPTION (pfile
, precision
);
1698 /* Prepare for unsigned division. */
1701 if (!num_positive (lhs
, precision
))
1702 negate
= !negate
, lhs_neg
= true, lhs
= num_negate (lhs
, precision
);
1703 if (!num_positive (rhs
, precision
))
1704 negate
= !negate
, rhs
= num_negate (rhs
, precision
);
1707 /* Find the high bit. */
1711 mask
= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1712 for (; ; i
--, mask
>>= 1)
1713 if (rhs
.high
& mask
)
1718 if (precision
> PART_PRECISION
)
1719 i
= precision
- PART_PRECISION
- 1;
1722 mask
= (cpp_num_part
) 1 << i
;
1723 for (; ; i
--, mask
>>= 1)
1729 if (!pfile
->state
.skip_eval
)
1730 cpp_error_with_line (pfile
, CPP_DL_ERROR
, location
, 0,
1731 "division by zero in #if");
1735 /* First nonzero bit of RHS is bit I. Do naive division by
1736 shifting the RHS fully left, and subtracting from LHS if LHS is
1737 at least as big, and then repeating but with one less shift.
1738 This is not very efficient, but is easy to understand. */
1740 rhs
.unsignedp
= true;
1741 lhs
.unsignedp
= true;
1742 i
= precision
- i
- 1;
1743 sub
= num_lshift (rhs
, precision
, i
);
1745 result
.high
= result
.low
= 0;
1748 if (num_greater_eq (lhs
, sub
, precision
))
1750 lhs
= num_binary_op (pfile
, lhs
, sub
, CPP_MINUS
);
1751 if (i
>= PART_PRECISION
)
1752 result
.high
|= (cpp_num_part
) 1 << (i
- PART_PRECISION
);
1754 result
.low
|= (cpp_num_part
) 1 << i
;
1758 sub
.low
= (sub
.low
>> 1) | (sub
.high
<< (PART_PRECISION
- 1));
1762 /* We divide so that the remainder has the sign of the LHS. */
1765 result
.unsignedp
= unsignedp
;
1766 result
.overflow
= false;
1770 result
= num_negate (result
, precision
);
1771 result
.overflow
= (num_positive (result
, precision
) ^ !negate
1772 && !num_zerop (result
));
1779 lhs
.unsignedp
= unsignedp
;
1780 lhs
.overflow
= false;
1782 lhs
= num_negate (lhs
, precision
);