Mercurial > hg > CbC > CbC_gcc
annotate libcpp/expr.c @ 55:77e2b8dfacca gcc-4.4.5
update it from 4.4.3 to 4.5.0
author | ryoma <e075725@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 12 Feb 2010 23:39:51 +0900 |
parents | a06113de4d67 |
children | b7f97abdc517 |
rev | line source |
---|---|
0 | 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. | |
5 | |
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 | |
9 later version. | |
10 | |
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. | |
15 | |
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/>. */ | |
19 | |
20 #include "config.h" | |
21 #include "system.h" | |
22 #include "cpplib.h" | |
23 #include "internal.h" | |
24 | |
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)) | |
29 | |
30 struct op | |
31 { | |
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. */ | |
35 enum cpp_ttype op; | |
36 }; | |
37 | |
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); | |
45 | |
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, | |
51 enum cpp_ttype); | |
52 static cpp_num num_equality_op (cpp_reader *, cpp_num, cpp_num, | |
53 enum cpp_ttype); | |
54 static cpp_num num_mul (cpp_reader *, cpp_num, cpp_num); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
55 static cpp_num num_div_op (cpp_reader *, cpp_num, cpp_num, enum cpp_ttype, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
56 source_location); |
0 | 57 static cpp_num num_lshift (cpp_num, size_t, size_t); |
58 static cpp_num num_rshift (cpp_num, size_t, size_t); | |
59 | |
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 *); | |
67 | |
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)) | |
71 | |
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; } \ | |
78 while(0) | |
79 | |
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. */ | |
83 static unsigned int | |
84 interpret_float_suffix (const uchar *s, size_t len) | |
85 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
86 size_t flags; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
87 size_t f, d, l, w, q, i; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
88 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
89 flags = 0; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
90 f = d = l = w = q = i = 0; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
91 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
92 /* Process decimal float suffixes, which are two letters starting |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
93 with d or D. Order and case are significant. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
94 if (len == 2 && (*s == 'd' || *s == 'D')) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
95 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
96 bool uppercase = (*s == 'D'); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
97 switch (s[1]) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
98 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
99 case 'f': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL): 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
100 case 'F': return (uppercase ? (CPP_N_DFLOAT | CPP_N_SMALL) : 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
101 case 'd': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM): 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
102 case 'D': return (uppercase ? (CPP_N_DFLOAT | CPP_N_MEDIUM) : 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
103 case 'l': return (!uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
104 case 'L': return (uppercase ? (CPP_N_DFLOAT | CPP_N_LARGE) : 0); break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
105 default: |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
106 /* Additional two-character suffixes beginning with D are not |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
107 for decimal float constants. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
108 break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
109 } |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
110 } |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
111 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
112 /* Recognize a fixed-point suffix. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
113 switch (s[len-1]) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
114 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
115 case 'k': case 'K': flags = CPP_N_ACCUM; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
116 case 'r': case 'R': flags = CPP_N_FRACT; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
117 default: break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
118 } |
0 | 119 |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
120 /* Continue processing a fixed-point suffix. The suffix is case |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
121 insensitive except for ll or LL. Order is significant. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
122 if (flags) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
123 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
124 if (len == 1) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
125 return flags; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
126 len--; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
127 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
128 if (*s == 'u' || *s == 'U') |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
129 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
130 flags |= CPP_N_UNSIGNED; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
131 if (len == 1) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
132 return flags; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
133 len--; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
134 s++; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
135 } |
0 | 136 |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
137 switch (*s) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
138 { |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
139 case 'h': case 'H': |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
140 if (len == 1) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
141 return flags |= CPP_N_SMALL; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
142 break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
143 case 'l': |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
144 if (len == 1) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
145 return flags |= CPP_N_MEDIUM; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
146 if (len == 2 && s[1] == 'l') |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
147 return flags |= CPP_N_LARGE; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
148 break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
149 case 'L': |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
150 if (len == 1) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
151 return flags |= CPP_N_MEDIUM; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
152 if (len == 2 && s[1] == 'L') |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
153 return flags |= CPP_N_LARGE; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
154 break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
155 default: |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
156 break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
157 } |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
158 /* Anything left at this point is invalid. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
159 return 0; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
160 } |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
161 |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
162 /* In any remaining valid suffix, the case and order don't matter. */ |
0 | 163 while (len--) |
164 switch (s[len]) | |
165 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
166 case 'f': case 'F': f++; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
167 case 'd': case 'D': d++; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
168 case 'l': case 'L': l++; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
169 case 'w': case 'W': w++; break; |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
170 case 'q': case 'Q': q++; break; |
0 | 171 case 'i': case 'I': |
172 case 'j': case 'J': i++; break; | |
173 default: | |
174 return 0; | |
175 } | |
176 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
177 if (f + d + l + w + q > 1 || i > 1) |
0 | 178 return 0; |
179 | |
180 return ((i ? CPP_N_IMAGINARY : 0) | |
181 | (f ? CPP_N_SMALL : | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
182 d ? CPP_N_MEDIUM : |
0 | 183 l ? CPP_N_LARGE : |
184 w ? CPP_N_MD_W : | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
185 q ? CPP_N_MD_Q : CPP_N_DEFAULT)); |
0 | 186 } |
187 | |
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. */ | |
191 static unsigned int | |
192 interpret_int_suffix (const uchar *s, size_t len) | |
193 { | |
194 size_t u, l, i; | |
195 | |
196 u = l = i = 0; | |
197 | |
198 while (len--) | |
199 switch (s[len]) | |
200 { | |
201 case 'u': case 'U': u++; break; | |
202 case 'i': case 'I': | |
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]) | |
207 return 0; | |
208 break; | |
209 default: | |
210 return 0; | |
211 } | |
212 | |
213 if (l > 2 || u > 1 || i > 1) | |
214 return 0; | |
215 | |
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)); | |
220 } | |
221 | |
222 /* Categorize numeric constants according to their field (integer, | |
223 floating point, or invalid), radix (decimal, octal, hexadecimal), | |
224 and type suffixes. */ | |
225 unsigned int | |
226 cpp_classify_number (cpp_reader *pfile, const cpp_token *token) | |
227 { | |
228 const uchar *str = token->val.str.text; | |
229 const uchar *limit; | |
230 unsigned int max_digit, result, radix; | |
231 enum {NOT_FLOAT = 0, AFTER_POINT, AFTER_EXPON} float_flag; | |
232 | |
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; | |
237 | |
238 limit = str + token->val.str.len; | |
239 float_flag = NOT_FLOAT; | |
240 max_digit = 0; | |
241 radix = 10; | |
242 | |
243 /* First, interpret the radix. */ | |
244 if (*str == '0') | |
245 { | |
246 radix = 8; | |
247 str++; | |
248 | |
249 /* Require at least one hex digit to classify it as hex. */ | |
250 if ((*str == 'x' || *str == 'X') | |
251 && (str[1] == '.' || ISXDIGIT (str[1]))) | |
252 { | |
253 radix = 16; | |
254 str++; | |
255 } | |
256 else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) | |
257 { | |
258 radix = 2; | |
259 str++; | |
260 } | |
261 } | |
262 | |
263 /* Now scan for a well-formed integer or float. */ | |
264 for (;;) | |
265 { | |
266 unsigned int c = *str++; | |
267 | |
268 if (ISDIGIT (c) || (ISXDIGIT (c) && radix == 16)) | |
269 { | |
270 c = hex_value (c); | |
271 if (c > max_digit) | |
272 max_digit = c; | |
273 } | |
274 else if (c == '.') | |
275 { | |
276 if (float_flag == NOT_FLOAT) | |
277 float_flag = AFTER_POINT; | |
278 else | |
279 SYNTAX_ERROR ("too many decimal points in number"); | |
280 } | |
281 else if ((radix <= 10 && (c == 'e' || c == 'E')) | |
282 || (radix == 16 && (c == 'p' || c == 'P'))) | |
283 { | |
284 float_flag = AFTER_EXPON; | |
285 break; | |
286 } | |
287 else | |
288 { | |
289 /* Start of suffix. */ | |
290 str--; | |
291 break; | |
292 } | |
293 } | |
294 | |
295 /* The suffix may be for decimal fixed-point constants without exponent. */ | |
296 if (radix != 16 && float_flag == NOT_FLOAT) | |
297 { | |
298 result = interpret_float_suffix (str, limit - str); | |
299 if ((result & CPP_N_FRACT) || (result & CPP_N_ACCUM)) | |
300 { | |
301 result |= CPP_N_FLOATING; | |
302 /* We need to restore the radix to 10, if the radix is 8. */ | |
303 if (radix == 8) | |
304 radix = 10; | |
305 | |
306 if (CPP_PEDANTIC (pfile)) | |
307 cpp_error (pfile, CPP_DL_PEDWARN, | |
308 "fixed-point constants are a GCC extension"); | |
309 goto syntax_ok; | |
310 } | |
311 else | |
312 result = 0; | |
313 } | |
314 | |
315 if (float_flag != NOT_FLOAT && radix == 8) | |
316 radix = 10; | |
317 | |
318 if (max_digit >= radix) | |
319 { | |
320 if (radix == 2) | |
321 SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit); | |
322 else | |
323 SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); | |
324 } | |
325 | |
326 if (float_flag != NOT_FLOAT) | |
327 { | |
328 if (radix == 2) | |
329 { | |
330 cpp_error (pfile, CPP_DL_ERROR, | |
331 "invalid prefix \"0b\" for floating constant"); | |
332 return CPP_N_INVALID; | |
333 } | |
334 | |
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"); | |
338 | |
339 if (float_flag == AFTER_EXPON) | |
340 { | |
341 if (*str == '+' || *str == '-') | |
342 str++; | |
343 | |
344 /* Exponent is decimal, even if string is a hex float. */ | |
345 if (!ISDIGIT (*str)) | |
346 SYNTAX_ERROR ("exponent has no digits"); | |
347 | |
348 do | |
349 str++; | |
350 while (ISDIGIT (*str)); | |
351 } | |
352 else if (radix == 16) | |
353 SYNTAX_ERROR ("hexadecimal floating constants require an exponent"); | |
354 | |
355 result = interpret_float_suffix (str, limit - str); | |
356 if (result == 0) | |
357 { | |
358 cpp_error (pfile, CPP_DL_ERROR, | |
359 "invalid suffix \"%.*s\" on floating constant", | |
360 (int) (limit - str), str); | |
361 return CPP_N_INVALID; | |
362 } | |
363 | |
364 /* Traditional C didn't accept any floating suffixes. */ | |
365 if (limit != str | |
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); | |
371 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
372 /* A suffix for double is a GCC extension via decimal float support. |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
373 If the suffix also specifies an imaginary value we'll catch that |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
374 later. */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
375 if ((result == CPP_N_MEDIUM) && CPP_PEDANTIC (pfile)) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
376 cpp_error (pfile, CPP_DL_PEDWARN, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
377 "suffix for double constant is a GCC extension"); |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
378 |
0 | 379 /* Radix must be 10 for decimal floats. */ |
380 if ((result & CPP_N_DFLOAT) && radix != 10) | |
381 { | |
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; | |
386 } | |
387 | |
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"); | |
391 | |
392 if ((result & CPP_N_DFLOAT) && CPP_PEDANTIC (pfile)) | |
393 cpp_error (pfile, CPP_DL_PEDWARN, | |
394 "decimal float constants are a GCC extension"); | |
395 | |
396 result |= CPP_N_FLOATING; | |
397 } | |
398 else | |
399 { | |
400 result = interpret_int_suffix (str, limit - str); | |
401 if (result == 0) | |
402 { | |
403 cpp_error (pfile, CPP_DL_ERROR, | |
404 "invalid suffix \"%.*s\" on integer constant", | |
405 (int) (limit - str), str); | |
406 return CPP_N_INVALID; | |
407 } | |
408 | |
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)) | |
412 { | |
413 int u_or_i = (result & (CPP_N_UNSIGNED|CPP_N_IMAGINARY)); | |
414 int large = (result & CPP_N_WIDTH) == CPP_N_LARGE; | |
415 | |
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); | |
420 } | |
421 | |
422 if ((result & CPP_N_WIDTH) == CPP_N_LARGE | |
423 && CPP_OPTION (pfile, warn_long_long)) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
424 cpp_error (pfile, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
425 CPP_OPTION (pfile, c99) ? CPP_DL_WARNING : CPP_DL_PEDWARN, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
426 CPP_OPTION (pfile, cplusplus) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
427 ? "use of C++0x long long integer constant" |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
428 : "use of C99 long long integer constant"); |
0 | 429 |
430 result |= CPP_N_INTEGER; | |
431 } | |
432 | |
433 syntax_ok: | |
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"); | |
440 | |
441 if (radix == 10) | |
442 result |= CPP_N_DECIMAL; | |
443 else if (radix == 16) | |
444 result |= CPP_N_HEX; | |
445 else if (radix == 2) | |
446 result |= CPP_N_BINARY; | |
447 else | |
448 result |= CPP_N_OCTAL; | |
449 | |
450 return result; | |
451 | |
452 syntax_error: | |
453 return CPP_N_INVALID; | |
454 } | |
455 | |
456 /* cpp_interpret_integer converts an integer constant into a cpp_num, | |
457 of precision options->precision. | |
458 | |
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. */ | |
462 cpp_num | |
463 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token, | |
464 unsigned int type) | |
465 { | |
466 const uchar *p, *end; | |
467 cpp_num result; | |
468 | |
469 result.low = 0; | |
470 result.high = 0; | |
471 result.unsignedp = !!(type & CPP_N_UNSIGNED); | |
472 result.overflow = false; | |
473 | |
474 p = token->val.str.text; | |
475 end = p + token->val.str.len; | |
476 | |
477 /* Common case of a single digit. */ | |
478 if (token->val.str.len == 1) | |
479 result.low = p[0] - '0'; | |
480 else | |
481 { | |
482 cpp_num_part max; | |
483 size_t precision = CPP_OPTION (pfile, precision); | |
484 unsigned int base = 10, c = 0; | |
485 bool overflow = false; | |
486 | |
487 if ((type & CPP_N_RADIX) == CPP_N_OCTAL) | |
488 { | |
489 base = 8; | |
490 p++; | |
491 } | |
492 else if ((type & CPP_N_RADIX) == CPP_N_HEX) | |
493 { | |
494 base = 16; | |
495 p += 2; | |
496 } | |
497 else if ((type & CPP_N_RADIX) == CPP_N_BINARY) | |
498 { | |
499 base = 2; | |
500 p += 2; | |
501 } | |
502 | |
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; | |
509 | |
510 for (; p < end; p++) | |
511 { | |
512 c = *p; | |
513 | |
514 if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c))) | |
515 c = hex_value (c); | |
516 else | |
517 break; | |
518 | |
519 /* Strict inequality for when max is set to zero. */ | |
520 if (result.low < max) | |
521 result.low = result.low * base + c; | |
522 else | |
523 { | |
524 result = append_digit (result, c, base, precision); | |
525 overflow |= result.overflow; | |
526 max = 0; | |
527 } | |
528 } | |
529 | |
530 if (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)) | |
541 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
542 /* This is for constants within the range of uintmax_t but |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
543 not that of intmax_t. For such decimal constants, a |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
544 diagnostic is required for C99 as the selected type must |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
545 be signed and not having a type is a constraint violation |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
546 (DR#298, TC3), so this must be a pedwarn. For C90, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
547 unsigned long is specified to be used for a constant that |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
548 does not fit in signed long; if uintmax_t has the same |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
549 range as unsigned long this means only a warning is |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
550 appropriate here. C90 permits the preprocessor to use a |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
551 wider range than unsigned long in the compiler, so if |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
552 uintmax_t is wider than unsigned long no diagnostic is |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
553 required for such constants in preprocessor #if |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
554 expressions and the compiler will pedwarn for such |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
555 constants outside the range of unsigned long that reach |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
556 the compiler so a diagnostic is not required there |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
557 either; thus, pedwarn for C99 but use a plain warning for |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
558 C90. */ |
0 | 559 if (base == 10) |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
560 cpp_error (pfile, (CPP_OPTION (pfile, c99) |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
561 ? CPP_DL_PEDWARN |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
562 : CPP_DL_WARNING), |
0 | 563 "integer constant is so large that it is unsigned"); |
564 result.unsignedp = true; | |
565 } | |
566 } | |
567 | |
568 return result; | |
569 } | |
570 | |
571 /* Append DIGIT to NUM, a number of PRECISION bits being read in base BASE. */ | |
572 static cpp_num | |
573 append_digit (cpp_num num, int digit, int base, size_t precision) | |
574 { | |
575 cpp_num result; | |
576 unsigned int shift; | |
577 bool overflow; | |
578 cpp_num_part add_high, add_low; | |
579 | |
580 /* Multiply by 2, 8 or 16. Catching this overflow here means we don't | |
581 need to worry about add_high overflowing. */ | |
582 switch (base) | |
583 { | |
584 case 2: | |
585 shift = 1; | |
586 break; | |
587 | |
588 case 16: | |
589 shift = 4; | |
590 break; | |
591 | |
592 default: | |
593 shift = 3; | |
594 } | |
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; | |
600 | |
601 if (base == 10) | |
602 { | |
603 add_low = num.low << 1; | |
604 add_high = (num.high << 1) + (num.low >> (PART_PRECISION - 1)); | |
605 } | |
606 else | |
607 add_high = add_low = 0; | |
608 | |
609 if (add_low + digit < add_low) | |
610 add_high++; | |
611 add_low += digit; | |
612 | |
613 if (result.low + add_low < result.low) | |
614 add_high++; | |
615 if (result.high + add_high < result.high) | |
616 overflow = true; | |
617 | |
618 result.low += add_low; | |
619 result.high += add_high; | |
620 result.overflow = overflow; | |
621 | |
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; | |
629 | |
630 return result; | |
631 } | |
632 | |
633 /* Handle meeting "defined" in a preprocessor expression. */ | |
634 static cpp_num | |
635 parse_defined (cpp_reader *pfile) | |
636 { | |
637 cpp_num result; | |
638 int paren = 0; | |
639 cpp_hashnode *node = 0; | |
640 const cpp_token *token; | |
641 cpp_context *initial_context = pfile->context; | |
642 | |
643 /* Don't expand macros. */ | |
644 pfile->state.prevent_expansion++; | |
645 | |
646 token = cpp_get_token (pfile); | |
647 if (token->type == CPP_OPEN_PAREN) | |
648 { | |
649 paren = 1; | |
650 token = cpp_get_token (pfile); | |
651 } | |
652 | |
653 if (token->type == CPP_NAME) | |
654 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
655 node = token->val.node.node; |
0 | 656 if (paren && cpp_get_token (pfile)->type != CPP_CLOSE_PAREN) |
657 { | |
658 cpp_error (pfile, CPP_DL_ERROR, "missing ')' after \"defined\""); | |
659 node = 0; | |
660 } | |
661 } | |
662 else | |
663 { | |
664 cpp_error (pfile, CPP_DL_ERROR, | |
665 "operator \"defined\" requires an identifier"); | |
666 if (token->flags & NAMED_OP) | |
667 { | |
668 cpp_token op; | |
669 | |
670 op.flags = 0; | |
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)); | |
676 } | |
677 } | |
678 | |
679 if (node) | |
680 { | |
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"); | |
684 | |
685 _cpp_mark_macro_used (node); | |
686 if (!(node->flags & NODE_USED)) | |
687 { | |
688 node->flags |= NODE_USED; | |
689 if (node->type == NT_MACRO) | |
690 { | |
691 if (pfile->cb.used_define) | |
692 pfile->cb.used_define (pfile, pfile->directive_line, node); | |
693 } | |
694 else | |
695 { | |
696 if (pfile->cb.used_undef) | |
697 pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
698 } | |
699 } | |
700 | |
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; | |
704 } | |
705 | |
706 pfile->state.prevent_expansion--; | |
707 | |
708 result.unsignedp = false; | |
709 result.high = 0; | |
710 result.overflow = false; | |
711 result.low = node && node->type == NT_MACRO; | |
712 return result; | |
713 } | |
714 | |
715 /* Convert a token into a CPP_NUMBER (an interpreted preprocessing | |
716 number or character constant, or the result of the "defined" or "#" | |
717 operators). */ | |
718 static cpp_num | |
719 eval_token (cpp_reader *pfile, const cpp_token *token) | |
720 { | |
721 cpp_num result; | |
722 unsigned int temp; | |
723 int unsignedp = 0; | |
724 | |
725 result.unsignedp = false; | |
726 result.overflow = false; | |
727 | |
728 switch (token->type) | |
729 { | |
730 case CPP_NUMBER: | |
731 temp = cpp_classify_number (pfile, token); | |
732 switch (temp & CPP_N_CATEGORY) | |
733 { | |
734 case CPP_N_FLOATING: | |
735 cpp_error (pfile, CPP_DL_ERROR, | |
736 "floating constant in preprocessor expression"); | |
737 break; | |
738 case CPP_N_INTEGER: | |
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"); | |
743 break; | |
744 | |
745 case CPP_N_INVALID: | |
746 /* Error already issued. */ | |
747 break; | |
748 } | |
749 result.high = result.low = 0; | |
750 break; | |
751 | |
752 case CPP_WCHAR: | |
753 case CPP_CHAR: | |
754 case CPP_CHAR16: | |
755 case CPP_CHAR32: | |
756 { | |
757 cppchar_t cc = cpp_interpret_charconst (pfile, token, | |
758 &temp, &unsignedp); | |
759 | |
760 result.high = 0; | |
761 result.low = cc; | |
762 /* Sign-extend the result if necessary. */ | |
763 if (!unsignedp && (cppchar_signed_t) cc < 0) | |
764 { | |
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)); | |
770 } | |
771 } | |
772 break; | |
773 | |
774 case CPP_NAME: | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
775 if (token->val.node.node == pfile->spec_nodes.n_defined) |
0 | 776 return parse_defined (pfile); |
777 else if (CPP_OPTION (pfile, cplusplus) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
778 && (token->val.node.node == pfile->spec_nodes.n_true |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
779 || token->val.node.node == pfile->spec_nodes.n_false)) |
0 | 780 { |
781 result.high = 0; | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
782 result.low = (token->val.node.node == pfile->spec_nodes.n_true); |
0 | 783 } |
784 else | |
785 { | |
786 result.high = 0; | |
787 result.low = 0; | |
788 if (CPP_OPTION (pfile, warn_undef) && !pfile->state.skip_eval) | |
789 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" is not defined", | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
790 NODE_NAME (token->val.node.node)); |
0 | 791 } |
792 break; | |
793 | |
794 case CPP_HASH: | |
795 if (!pfile->state.skipping) | |
796 { | |
797 /* A pedantic warning takes precedence over a deprecated | |
798 warning here. */ | |
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"); | |
805 } | |
806 _cpp_test_assertion (pfile, &temp); | |
807 result.high = 0; | |
808 result.low = temp; | |
809 break; | |
810 | |
811 default: | |
812 abort (); | |
813 } | |
814 | |
815 result.unsignedp = !!unsignedp; | |
816 return result; | |
817 } | |
818 | |
819 /* Operator precedence and flags table. | |
820 | |
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. | |
825 | |
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. | |
829 | |
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. | |
836 | |
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. */ | |
840 | |
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) | |
846 | |
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 | |
850 { | |
851 uchar prio; | |
852 uchar flags; | |
853 } optab[] = | |
854 { | |
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}, | |
869 | |
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(). */ | |
875 /* QUERY */ {4, 0}, | |
876 /* COLON */ {4, LEFT_ASSOC | CHECK_PROMOTION}, | |
877 /* COMMA */ {4, LEFT_ASSOC}, | |
878 /* OPEN_PAREN */ {1, NO_L_OPERAND}, | |
879 /* CLOSE_PAREN */ {0, 0}, | |
880 /* EOF */ {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} | |
887 }; | |
888 | |
889 /* Parse and evaluate a C expression, reading from PFILE. | |
890 Returns the truth value of the expression. | |
891 | |
892 The implementation is an operator precedence parser, i.e. a | |
893 bottom-up parser, using a stack for not-yet-reduced tokens. | |
894 | |
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 | |
899 that precedes it. */ | |
900 bool | |
901 _cpp_parse_expr (cpp_reader *pfile, bool is_if) | |
902 { | |
903 struct op *top = pfile->op_stack; | |
904 unsigned int lex_count; | |
905 bool saw_leading_not, want_value = true; | |
906 | |
907 pfile->state.skip_eval = 0; | |
908 | |
909 /* Set up detection of #if ! defined(). */ | |
910 pfile->mi_ind_cmacro = 0; | |
911 saw_leading_not = false; | |
912 lex_count = 0; | |
913 | |
914 /* Lowest priority operator prevents further reductions. */ | |
915 top->op = CPP_EOF; | |
916 | |
917 for (;;) | |
918 { | |
919 struct op op; | |
920 | |
921 lex_count++; | |
922 op.token = cpp_get_token (pfile); | |
923 op.op = op.token->type; | |
924 op.loc = op.token->src_loc; | |
925 | |
926 switch (op.op) | |
927 { | |
928 /* These tokens convert into values. */ | |
929 case CPP_NUMBER: | |
930 case CPP_CHAR: | |
931 case CPP_WCHAR: | |
932 case CPP_CHAR16: | |
933 case CPP_CHAR32: | |
934 case CPP_NAME: | |
935 case CPP_HASH: | |
936 if (!want_value) | |
937 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", | |
938 cpp_token_as_text (pfile, op.token)); | |
939 want_value = false; | |
940 top->value = eval_token (pfile, op.token); | |
941 continue; | |
942 | |
943 case CPP_NOT: | |
944 saw_leading_not = lex_count == 1; | |
945 break; | |
946 case CPP_PLUS: | |
947 if (want_value) | |
948 op.op = CPP_UPLUS; | |
949 break; | |
950 case CPP_MINUS: | |
951 if (want_value) | |
952 op.op = CPP_UMINUS; | |
953 break; | |
954 | |
955 default: | |
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)); | |
959 break; | |
960 } | |
961 | |
962 /* Check we have a value or operator as appropriate. */ | |
963 if (optab[op.op].flags & NO_L_OPERAND) | |
964 { | |
965 if (!want_value) | |
966 SYNTAX_ERROR2 ("missing binary operator before token \"%s\"", | |
967 cpp_token_as_text (pfile, op.token)); | |
968 } | |
969 else if (want_value) | |
970 { | |
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 ')'"); | |
975 | |
976 if (op.op == CPP_EOF && top->op == CPP_EOF) | |
977 SYNTAX_ERROR2 ("%s with no expression", is_if ? "#if" : "#elif"); | |
978 | |
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. */; | |
984 else | |
985 SYNTAX_ERROR2 ("operator '%s' has no left operand", | |
986 cpp_token_as_text (pfile, op.token)); | |
987 } | |
988 | |
989 top = reduce (pfile, top, op.op); | |
990 if (!top) | |
991 goto syntax_error; | |
992 | |
993 if (op.op == CPP_EOF) | |
994 break; | |
995 | |
996 switch (op.op) | |
997 { | |
998 case CPP_CLOSE_PAREN: | |
999 continue; | |
1000 case CPP_OR_OR: | |
1001 if (!num_zerop (top->value)) | |
1002 pfile->state.skip_eval++; | |
1003 break; | |
1004 case CPP_AND_AND: | |
1005 case CPP_QUERY: | |
1006 if (num_zerop (top->value)) | |
1007 pfile->state.skip_eval++; | |
1008 break; | |
1009 case CPP_COLON: | |
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++; | |
1014 else | |
1015 pfile->state.skip_eval--; | |
1016 default: | |
1017 break; | |
1018 } | |
1019 | |
1020 want_value = true; | |
1021 | |
1022 /* Check for and handle stack overflow. */ | |
1023 if (++top == pfile->op_limit) | |
1024 top = _cpp_expand_op_stack (pfile); | |
1025 | |
1026 top->op = op.op; | |
1027 top->token = op.token; | |
1028 top->loc = op.token->src_loc; | |
1029 } | |
1030 | |
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; | |
1036 | |
1037 if (top != pfile->op_stack) | |
1038 { | |
1039 cpp_error (pfile, CPP_DL_ICE, "unbalanced stack in %s", | |
1040 is_if ? "#if" : "#elif"); | |
1041 syntax_error: | |
1042 return false; /* Return false on syntax error. */ | |
1043 } | |
1044 | |
1045 return !num_zerop (top->value); | |
1046 } | |
1047 | |
1048 /* Reduce the operator / value stack if possible, in preparation for | |
1049 pushing operator OP. Returns NULL on error, otherwise the top of | |
1050 the stack. */ | |
1051 static struct op * | |
1052 reduce (cpp_reader *pfile, struct op *top, enum cpp_ttype op) | |
1053 { | |
1054 unsigned int prio; | |
1055 | |
1056 if (top->op <= CPP_EQ || top->op > CPP_LAST_CPP_OP + 2) | |
1057 { | |
1058 bad_op: | |
1059 cpp_error (pfile, CPP_DL_ICE, "impossible operator '%u'", top->op); | |
1060 return 0; | |
1061 } | |
1062 | |
1063 if (op == CPP_OPEN_PAREN) | |
1064 return top; | |
1065 | |
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) | |
1070 { | |
1071 if (CPP_OPTION (pfile, warn_num_sign_change) | |
1072 && optab[top->op].flags & CHECK_PROMOTION) | |
1073 check_promotion (pfile, top); | |
1074 | |
1075 switch (top->op) | |
1076 { | |
1077 case CPP_UPLUS: | |
1078 case CPP_UMINUS: | |
1079 case CPP_NOT: | |
1080 case CPP_COMPL: | |
1081 top[-1].value = num_unary_op (pfile, top->value, top->op); | |
1082 top[-1].loc = top->loc; | |
1083 break; | |
1084 | |
1085 case CPP_PLUS: | |
1086 case CPP_MINUS: | |
1087 case CPP_RSHIFT: | |
1088 case CPP_LSHIFT: | |
1089 case CPP_COMMA: | |
1090 top[-1].value = num_binary_op (pfile, top[-1].value, | |
1091 top->value, top->op); | |
1092 top[-1].loc = top->loc; | |
1093 break; | |
1094 | |
1095 case CPP_GREATER: | |
1096 case CPP_LESS: | |
1097 case CPP_GREATER_EQ: | |
1098 case CPP_LESS_EQ: | |
1099 top[-1].value | |
1100 = num_inequality_op (pfile, top[-1].value, top->value, top->op); | |
1101 top[-1].loc = top->loc; | |
1102 break; | |
1103 | |
1104 case CPP_EQ_EQ: | |
1105 case CPP_NOT_EQ: | |
1106 top[-1].value | |
1107 = num_equality_op (pfile, top[-1].value, top->value, top->op); | |
1108 top[-1].loc = top->loc; | |
1109 break; | |
1110 | |
1111 case CPP_AND: | |
1112 case CPP_OR: | |
1113 case CPP_XOR: | |
1114 top[-1].value | |
1115 = num_bitwise_op (pfile, top[-1].value, top->value, top->op); | |
1116 top[-1].loc = top->loc; | |
1117 break; | |
1118 | |
1119 case CPP_MULT: | |
1120 top[-1].value = num_mul (pfile, top[-1].value, top->value); | |
1121 top[-1].loc = top->loc; | |
1122 break; | |
1123 | |
1124 case CPP_DIV: | |
1125 case CPP_MOD: | |
1126 top[-1].value = num_div_op (pfile, top[-1].value, | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1127 top->value, top->op, top->loc); |
0 | 1128 top[-1].loc = top->loc; |
1129 break; | |
1130 | |
1131 case CPP_OR_OR: | |
1132 top--; | |
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; | |
1141 continue; | |
1142 | |
1143 case CPP_AND_AND: | |
1144 top--; | |
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; | |
1153 continue; | |
1154 | |
1155 case CPP_OPEN_PAREN: | |
1156 if (op != CPP_CLOSE_PAREN) | |
1157 { | |
1158 cpp_error_with_line (pfile, CPP_DL_ERROR, | |
1159 top->token->src_loc, | |
1160 0, "missing ')' in expression"); | |
1161 return 0; | |
1162 } | |
1163 top--; | |
1164 top->value = top[1].value; | |
1165 top->loc = top[1].loc; | |
1166 return top; | |
1167 | |
1168 case CPP_COLON: | |
1169 top -= 2; | |
1170 if (!num_zerop (top->value)) | |
1171 { | |
1172 pfile->state.skip_eval--; | |
1173 top->value = top[1].value; | |
1174 top->loc = top[1].loc; | |
1175 } | |
1176 else | |
1177 { | |
1178 top->value = top[2].value; | |
1179 top->loc = top[2].loc; | |
1180 } | |
1181 top->value.unsignedp = (top[1].value.unsignedp | |
1182 || top[2].value.unsignedp); | |
1183 continue; | |
1184 | |
1185 case CPP_QUERY: | |
1186 /* COMMA and COLON should not reduce a QUERY operator. */ | |
1187 if (op == CPP_COMMA || op == CPP_COLON) | |
1188 return top; | |
1189 cpp_error (pfile, CPP_DL_ERROR, "'?' without following ':'"); | |
1190 return 0; | |
1191 | |
1192 default: | |
1193 goto bad_op; | |
1194 } | |
1195 | |
1196 top--; | |
1197 if (top->value.overflow && !pfile->state.skip_eval) | |
1198 cpp_error (pfile, CPP_DL_PEDWARN, | |
1199 "integer overflow in preprocessor expression"); | |
1200 } | |
1201 | |
1202 if (op == CPP_CLOSE_PAREN) | |
1203 { | |
1204 cpp_error (pfile, CPP_DL_ERROR, "missing '(' in expression"); | |
1205 return 0; | |
1206 } | |
1207 | |
1208 return top; | |
1209 } | |
1210 | |
1211 /* Returns the position of the old top of stack after expansion. */ | |
1212 struct op * | |
1213 _cpp_expand_op_stack (cpp_reader *pfile) | |
1214 { | |
1215 size_t old_size = (size_t) (pfile->op_limit - pfile->op_stack); | |
1216 size_t new_size = old_size * 2 + 20; | |
1217 | |
1218 pfile->op_stack = XRESIZEVEC (struct op, pfile->op_stack, new_size); | |
1219 pfile->op_limit = pfile->op_stack + new_size; | |
1220 | |
1221 return pfile->op_stack + old_size; | |
1222 } | |
1223 | |
1224 /* Emits a warning if the effective sign of either operand of OP | |
1225 changes because of integer promotions. */ | |
1226 static void | |
1227 check_promotion (cpp_reader *pfile, const struct op *op) | |
1228 { | |
1229 if (op->value.unsignedp == op[-1].value.unsignedp) | |
1230 return; | |
1231 | |
1232 if (op->value.unsignedp) | |
1233 { | |
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)); | |
1238 } | |
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)); | |
1243 } | |
1244 | |
1245 /* Clears the unused high order bits of the number pointed to by PNUM. */ | |
1246 static cpp_num | |
1247 num_trim (cpp_num num, size_t precision) | |
1248 { | |
1249 if (precision > PART_PRECISION) | |
1250 { | |
1251 precision -= PART_PRECISION; | |
1252 if (precision < PART_PRECISION) | |
1253 num.high &= ((cpp_num_part) 1 << precision) - 1; | |
1254 } | |
1255 else | |
1256 { | |
1257 if (precision < PART_PRECISION) | |
1258 num.low &= ((cpp_num_part) 1 << precision) - 1; | |
1259 num.high = 0; | |
1260 } | |
1261 | |
1262 return num; | |
1263 } | |
1264 | |
1265 /* True iff A (presumed signed) >= 0. */ | |
1266 static bool | |
1267 num_positive (cpp_num num, size_t precision) | |
1268 { | |
1269 if (precision > PART_PRECISION) | |
1270 { | |
1271 precision -= PART_PRECISION; | |
1272 return (num.high & (cpp_num_part) 1 << (precision - 1)) == 0; | |
1273 } | |
1274 | |
1275 return (num.low & (cpp_num_part) 1 << (precision - 1)) == 0; | |
1276 } | |
1277 | |
1278 /* Sign extend a number, with PRECISION significant bits and all | |
1279 others assumed clear, to fill out a cpp_num structure. */ | |
1280 cpp_num | |
1281 cpp_num_sign_extend (cpp_num num, size_t precision) | |
1282 { | |
1283 if (!num.unsignedp) | |
1284 { | |
1285 if (precision > PART_PRECISION) | |
1286 { | |
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)); | |
1291 } | |
1292 else if (num.low & (cpp_num_part) 1 << (precision - 1)) | |
1293 { | |
1294 if (precision < PART_PRECISION) | |
1295 num.low |= ~(~(cpp_num_part) 0 >> (PART_PRECISION - precision)); | |
1296 num.high = ~(cpp_num_part) 0; | |
1297 } | |
1298 } | |
1299 | |
1300 return num; | |
1301 } | |
1302 | |
1303 /* Returns the negative of NUM. */ | |
1304 static cpp_num | |
1305 num_negate (cpp_num num, size_t precision) | |
1306 { | |
1307 cpp_num copy; | |
1308 | |
1309 copy = num; | |
1310 num.high = ~num.high; | |
1311 num.low = ~num.low; | |
1312 if (++num.low == 0) | |
1313 num.high++; | |
1314 num = num_trim (num, precision); | |
1315 num.overflow = (!num.unsignedp && num_eq (num, copy) && !num_zerop (num)); | |
1316 | |
1317 return num; | |
1318 } | |
1319 | |
1320 /* Returns true if A >= B. */ | |
1321 static bool | |
1322 num_greater_eq (cpp_num pa, cpp_num pb, size_t precision) | |
1323 { | |
1324 bool unsignedp; | |
1325 | |
1326 unsignedp = pa.unsignedp || pb.unsignedp; | |
1327 | |
1328 if (!unsignedp) | |
1329 { | |
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); | |
1333 | |
1334 if (unsignedp != num_positive (pb, precision)) | |
1335 return unsignedp; | |
1336 | |
1337 /* Otherwise we can do an unsigned comparison. */ | |
1338 } | |
1339 | |
1340 return (pa.high > pb.high) || (pa.high == pb.high && pa.low >= pb.low); | |
1341 } | |
1342 | |
1343 /* Returns LHS OP RHS, where OP is a bit-wise operation. */ | |
1344 static cpp_num | |
1345 num_bitwise_op (cpp_reader *pfile ATTRIBUTE_UNUSED, | |
1346 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
1347 { | |
1348 lhs.overflow = false; | |
1349 lhs.unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1350 | |
1351 /* As excess precision is zeroed, there is no need to num_trim () as | |
1352 these operations cannot introduce a set bit there. */ | |
1353 if (op == CPP_AND) | |
1354 { | |
1355 lhs.low &= rhs.low; | |
1356 lhs.high &= rhs.high; | |
1357 } | |
1358 else if (op == CPP_OR) | |
1359 { | |
1360 lhs.low |= rhs.low; | |
1361 lhs.high |= rhs.high; | |
1362 } | |
1363 else | |
1364 { | |
1365 lhs.low ^= rhs.low; | |
1366 lhs.high ^= rhs.high; | |
1367 } | |
1368 | |
1369 return lhs; | |
1370 } | |
1371 | |
1372 /* Returns LHS OP RHS, where OP is an inequality. */ | |
1373 static cpp_num | |
1374 num_inequality_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, | |
1375 enum cpp_ttype op) | |
1376 { | |
1377 bool gte = num_greater_eq (lhs, rhs, CPP_OPTION (pfile, precision)); | |
1378 | |
1379 if (op == CPP_GREATER_EQ) | |
1380 lhs.low = gte; | |
1381 else if (op == CPP_LESS) | |
1382 lhs.low = !gte; | |
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); | |
1387 | |
1388 lhs.high = 0; | |
1389 lhs.overflow = false; | |
1390 lhs.unsignedp = false; | |
1391 return lhs; | |
1392 } | |
1393 | |
1394 /* Returns LHS OP RHS, where OP is == or !=. */ | |
1395 static cpp_num | |
1396 num_equality_op (cpp_reader *pfile ATTRIBUTE_UNUSED, | |
1397 cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
1398 { | |
1399 /* Work around a 3.0.4 bug; see PR 6950. */ | |
1400 bool eq = num_eq (lhs, rhs); | |
1401 if (op == CPP_NOT_EQ) | |
1402 eq = !eq; | |
1403 lhs.low = eq; | |
1404 lhs.high = 0; | |
1405 lhs.overflow = false; | |
1406 lhs.unsignedp = false; | |
1407 return lhs; | |
1408 } | |
1409 | |
1410 /* Shift NUM, of width PRECISION, right by N bits. */ | |
1411 static cpp_num | |
1412 num_rshift (cpp_num num, size_t precision, size_t n) | |
1413 { | |
1414 cpp_num_part sign_mask; | |
1415 bool x = num_positive (num, precision); | |
1416 | |
1417 if (num.unsignedp || x) | |
1418 sign_mask = 0; | |
1419 else | |
1420 sign_mask = ~(cpp_num_part) 0; | |
1421 | |
1422 if (n >= precision) | |
1423 num.high = num.low = sign_mask; | |
1424 else | |
1425 { | |
1426 /* Sign-extend. */ | |
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); | |
1431 | |
1432 if (n >= PART_PRECISION) | |
1433 { | |
1434 n -= PART_PRECISION; | |
1435 num.low = num.high; | |
1436 num.high = sign_mask; | |
1437 } | |
1438 | |
1439 if (n) | |
1440 { | |
1441 num.low = (num.low >> n) | (num.high << (PART_PRECISION - n)); | |
1442 num.high = (num.high >> n) | (sign_mask << (PART_PRECISION - n)); | |
1443 } | |
1444 } | |
1445 | |
1446 num = num_trim (num, precision); | |
1447 num.overflow = false; | |
1448 return num; | |
1449 } | |
1450 | |
1451 /* Shift NUM, of width PRECISION, left by N bits. */ | |
1452 static cpp_num | |
1453 num_lshift (cpp_num num, size_t precision, size_t n) | |
1454 { | |
1455 if (n >= precision) | |
1456 { | |
1457 num.overflow = !num.unsignedp && !num_zerop (num); | |
1458 num.high = num.low = 0; | |
1459 } | |
1460 else | |
1461 { | |
1462 cpp_num orig, maybe_orig; | |
1463 size_t m = n; | |
1464 | |
1465 orig = num; | |
1466 if (m >= PART_PRECISION) | |
1467 { | |
1468 m -= PART_PRECISION; | |
1469 num.high = num.low; | |
1470 num.low = 0; | |
1471 } | |
1472 if (m) | |
1473 { | |
1474 num.high = (num.high << m) | (num.low >> (PART_PRECISION - m)); | |
1475 num.low <<= m; | |
1476 } | |
1477 num = num_trim (num, precision); | |
1478 | |
1479 if (num.unsignedp) | |
1480 num.overflow = false; | |
1481 else | |
1482 { | |
1483 maybe_orig = num_rshift (num, precision, n); | |
1484 num.overflow = !num_eq (orig, maybe_orig); | |
1485 } | |
1486 } | |
1487 | |
1488 return num; | |
1489 } | |
1490 | |
1491 /* The four unary operators: +, -, ! and ~. */ | |
1492 static cpp_num | |
1493 num_unary_op (cpp_reader *pfile, cpp_num num, enum cpp_ttype op) | |
1494 { | |
1495 switch (op) | |
1496 { | |
1497 case CPP_UPLUS: | |
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; | |
1502 break; | |
1503 | |
1504 case CPP_UMINUS: | |
1505 num = num_negate (num, CPP_OPTION (pfile, precision)); | |
1506 break; | |
1507 | |
1508 case CPP_COMPL: | |
1509 num.high = ~num.high; | |
1510 num.low = ~num.low; | |
1511 num = num_trim (num, CPP_OPTION (pfile, precision)); | |
1512 num.overflow = false; | |
1513 break; | |
1514 | |
1515 default: /* case CPP_NOT: */ | |
1516 num.low = num_zerop (num); | |
1517 num.high = 0; | |
1518 num.overflow = false; | |
1519 num.unsignedp = false; | |
1520 break; | |
1521 } | |
1522 | |
1523 return num; | |
1524 } | |
1525 | |
1526 /* The various binary operators. */ | |
1527 static cpp_num | |
1528 num_binary_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op) | |
1529 { | |
1530 cpp_num result; | |
1531 size_t precision = CPP_OPTION (pfile, precision); | |
1532 size_t n; | |
1533 | |
1534 switch (op) | |
1535 { | |
1536 /* Shifts. */ | |
1537 case CPP_LSHIFT: | |
1538 case CPP_RSHIFT: | |
1539 if (!rhs.unsignedp && !num_positive (rhs, precision)) | |
1540 { | |
1541 /* A negative shift is a positive shift the other way. */ | |
1542 if (op == CPP_LSHIFT) | |
1543 op = CPP_RSHIFT; | |
1544 else | |
1545 op = CPP_LSHIFT; | |
1546 rhs = num_negate (rhs, precision); | |
1547 } | |
1548 if (rhs.high) | |
1549 n = ~0; /* Maximal. */ | |
1550 else | |
1551 n = rhs.low; | |
1552 if (op == CPP_LSHIFT) | |
1553 lhs = num_lshift (lhs, precision, n); | |
1554 else | |
1555 lhs = num_rshift (lhs, precision, n); | |
1556 break; | |
1557 | |
1558 /* Arithmetic. */ | |
1559 case CPP_MINUS: | |
1560 rhs = num_negate (rhs, precision); | |
1561 case CPP_PLUS: | |
1562 result.low = lhs.low + rhs.low; | |
1563 result.high = lhs.high + rhs.high; | |
1564 if (result.low < lhs.low) | |
1565 result.high++; | |
1566 result.unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1567 result.overflow = false; | |
1568 | |
1569 result = num_trim (result, precision); | |
1570 if (!result.unsignedp) | |
1571 { | |
1572 bool lhsp = num_positive (lhs, precision); | |
1573 result.overflow = (lhsp == num_positive (rhs, precision) | |
1574 && lhsp != num_positive (result, precision)); | |
1575 } | |
1576 return result; | |
1577 | |
1578 /* Comma. */ | |
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"); | |
1584 lhs = rhs; | |
1585 break; | |
1586 } | |
1587 | |
1588 return lhs; | |
1589 } | |
1590 | |
1591 /* Multiplies two unsigned cpp_num_parts to give a cpp_num. This | |
1592 cannot overflow. */ | |
1593 static cpp_num | |
1594 num_part_mul (cpp_num_part lhs, cpp_num_part rhs) | |
1595 { | |
1596 cpp_num result; | |
1597 cpp_num_part middle[2], temp; | |
1598 | |
1599 result.low = LOW_PART (lhs) * LOW_PART (rhs); | |
1600 result.high = HIGH_PART (lhs) * HIGH_PART (rhs); | |
1601 | |
1602 middle[0] = LOW_PART (lhs) * HIGH_PART (rhs); | |
1603 middle[1] = HIGH_PART (lhs) * LOW_PART (rhs); | |
1604 | |
1605 temp = result.low; | |
1606 result.low += LOW_PART (middle[0]) << (PART_PRECISION / 2); | |
1607 if (result.low < temp) | |
1608 result.high++; | |
1609 | |
1610 temp = result.low; | |
1611 result.low += LOW_PART (middle[1]) << (PART_PRECISION / 2); | |
1612 if (result.low < temp) | |
1613 result.high++; | |
1614 | |
1615 result.high += HIGH_PART (middle[0]); | |
1616 result.high += HIGH_PART (middle[1]); | |
1617 result.unsignedp = true; | |
1618 result.overflow = false; | |
1619 | |
1620 return result; | |
1621 } | |
1622 | |
1623 /* Multiply two preprocessing numbers. */ | |
1624 static cpp_num | |
1625 num_mul (cpp_reader *pfile, cpp_num lhs, cpp_num rhs) | |
1626 { | |
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); | |
1631 | |
1632 /* Prepare for unsigned multiplication. */ | |
1633 if (!unsignedp) | |
1634 { | |
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); | |
1639 } | |
1640 | |
1641 overflow = lhs.high && rhs.high; | |
1642 result = num_part_mul (lhs.low, rhs.low); | |
1643 | |
1644 temp = num_part_mul (lhs.high, rhs.low); | |
1645 result.high += temp.low; | |
1646 if (temp.high) | |
1647 overflow = true; | |
1648 | |
1649 temp = num_part_mul (lhs.low, rhs.high); | |
1650 result.high += temp.low; | |
1651 if (temp.high) | |
1652 overflow = true; | |
1653 | |
1654 temp.low = result.low, temp.high = result.high; | |
1655 result = num_trim (result, precision); | |
1656 if (!num_eq (result, temp)) | |
1657 overflow = true; | |
1658 | |
1659 if (negate) | |
1660 result = num_negate (result, precision); | |
1661 | |
1662 if (unsignedp) | |
1663 result.overflow = false; | |
1664 else | |
1665 result.overflow = overflow || (num_positive (result, precision) ^ !negate | |
1666 && !num_zerop (result)); | |
1667 result.unsignedp = unsignedp; | |
1668 | |
1669 return result; | |
1670 } | |
1671 | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1672 /* Divide two preprocessing numbers, LHS and RHS, returning the answer |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1673 or the remainder depending upon OP. LOCATION is the source location |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1674 of this operator (for diagnostics). */ |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1675 |
0 | 1676 static cpp_num |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1677 num_div_op (cpp_reader *pfile, cpp_num lhs, cpp_num rhs, enum cpp_ttype op, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1678 source_location location) |
0 | 1679 { |
1680 cpp_num result, sub; | |
1681 cpp_num_part mask; | |
1682 bool unsignedp = lhs.unsignedp || rhs.unsignedp; | |
1683 bool negate = false, lhs_neg = false; | |
1684 size_t i, precision = CPP_OPTION (pfile, precision); | |
1685 | |
1686 /* Prepare for unsigned division. */ | |
1687 if (!unsignedp) | |
1688 { | |
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); | |
1693 } | |
1694 | |
1695 /* Find the high bit. */ | |
1696 if (rhs.high) | |
1697 { | |
1698 i = precision - 1; | |
1699 mask = (cpp_num_part) 1 << (i - PART_PRECISION); | |
1700 for (; ; i--, mask >>= 1) | |
1701 if (rhs.high & mask) | |
1702 break; | |
1703 } | |
1704 else if (rhs.low) | |
1705 { | |
1706 if (precision > PART_PRECISION) | |
1707 i = precision - PART_PRECISION - 1; | |
1708 else | |
1709 i = precision - 1; | |
1710 mask = (cpp_num_part) 1 << i; | |
1711 for (; ; i--, mask >>= 1) | |
1712 if (rhs.low & mask) | |
1713 break; | |
1714 } | |
1715 else | |
1716 { | |
1717 if (!pfile->state.skip_eval) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1718 cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1719 "division by zero in #if"); |
0 | 1720 return lhs; |
1721 } | |
1722 | |
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. */ | |
1727 | |
1728 rhs.unsignedp = true; | |
1729 lhs.unsignedp = true; | |
1730 i = precision - i - 1; | |
1731 sub = num_lshift (rhs, precision, i); | |
1732 | |
1733 result.high = result.low = 0; | |
1734 for (;;) | |
1735 { | |
1736 if (num_greater_eq (lhs, sub, precision)) | |
1737 { | |
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); | |
1741 else | |
1742 result.low |= (cpp_num_part) 1 << i; | |
1743 } | |
1744 if (i-- == 0) | |
1745 break; | |
1746 sub.low = (sub.low >> 1) | (sub.high << (PART_PRECISION - 1)); | |
1747 sub.high >>= 1; | |
1748 } | |
1749 | |
1750 /* We divide so that the remainder has the sign of the LHS. */ | |
1751 if (op == CPP_DIV) | |
1752 { | |
1753 result.unsignedp = unsignedp; | |
1754 result.overflow = false; | |
1755 if (!unsignedp) | |
1756 { | |
1757 if (negate) | |
1758 result = num_negate (result, precision); | |
1759 result.overflow = (num_positive (result, precision) ^ !negate | |
1760 && !num_zerop (result)); | |
1761 } | |
1762 | |
1763 return result; | |
1764 } | |
1765 | |
1766 /* CPP_MOD. */ | |
1767 lhs.unsignedp = unsignedp; | |
1768 lhs.overflow = false; | |
1769 if (lhs_neg) | |
1770 lhs = num_negate (lhs, precision); | |
1771 | |
1772 return lhs; | |
1773 } |