Debian Codegraph @

clang/test/AST/Interp/intap.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++11 -verify %s
2 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++20 -verify %s
3 // RUN: %clang_cc1 -std=c++11 -fms-extensions -verify=ref %s
4 // RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref %s
5
6
7 using MaxBitInt = _BitInt(128);
8 #define INT_MIN (~__INT_MAX__)
9
10 constexpr _BitInt(2) A = 0;
11 constexpr _BitInt(2) B = A + 1;
12 constexpr _BitInt(2) C = B + 1; // expected-warning {{from 2 to -2}} \
13 // ref-warning {{from 2 to -2}}
14 static_assert(C == -2, "");
15 static_assert(C - B == A, ""); // expected-error {{not an integral constant expression}} \
16 // expected-note {{value -3 is outside the range of representable values}} \
17 // ref-error {{not an integral constant expression}} \
18 // ref-note {{value -3 is outside the range of representable values}}
19
20 static_assert(B - 1 == 0, "");
21
22 constexpr MaxBitInt A_ = 0;
23 constexpr MaxBitInt B_ = A_ + 1;
24 static_assert(B_ == 1, "");
25
26 constexpr MaxBitInt BitIntZero{};
27 static_assert(BitIntZero == 0, "");
28 constexpr unsigned _BitInt(128) UBitIntZero{};
29 static_assert(UBitIntZero == 0, "");
30
31 constexpr _BitInt(2) BitIntZero2{};
32 static_assert(BitIntZero2 == 0, "");
33 constexpr unsigned _BitInt(1) UBitIntZero1{};
34 static_assert(UBitIntZero1 == 0, "");
35
36 constexpr unsigned _BitInt(2) BI1 = 3u;
37 static_assert(BI1 == 3, "");
38
39 constexpr _BitInt(4) MulA = 5;
40 constexpr _BitInt(4) MulB = 7;
41 static_assert(MulA * MulB == 50, ""); // ref-error {{not an integral constant expression}} \
42 // ref-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}} \
43 // expected-error {{not an integral constant expression}} \
44 // expected-note {{value 35 is outside the range of representable values of type '_BitInt(4)'}}
45 static_assert(MulA * 5 == 25, "");
46 static_assert(-1 * MulB == -7, "");
47
48
49 constexpr _BitInt(4) DivA = 2;
50 constexpr _BitInt(2) DivB = 1;
51 static_assert(DivA / DivB == 2, "");
52
53 constexpr _BitInt(4) DivC = DivA / 0; // ref-error {{must be initialized by a constant expression}} \
54 // ref-note {{division by zero}} \
55 // expected-error {{must be initialized by a constant expression}} \
56 // expected-note {{division by zero}}
57
58 constexpr _BitInt(7) RemA = 47;
59 constexpr _BitInt(6) RemB = 9;
60 static_assert(RemA % RemB == 2, "");
61 static_assert(RemA % 0 == 1, ""); // ref-error {{not an integral constant expression}} \
62 // ref-note {{division by zero}} \
63 // expected-error {{not an integral constant expression}} \
64 // expected-note {{division by zero}}
65
66 constexpr _BitInt(32) bottom = -1;
67 constexpr _BitInt(32) top = INT_MIN;
68 constexpr _BitInt(32) nope = top / bottom; // ref-error {{must be initialized by a constant expression}} \
69 // ref-note {{value 2147483648 is outside the range}} \
70 // expected-error {{must be initialized by a constant expression}} \
71 // expected-note {{value 2147483648 is outside the range}}
72 constexpr _BitInt(32) noooo = top % bottom; // ref-error {{must be initialized by a constant expression}} \
73 // ref-note {{value 2147483648 is outside the range}} \
74 // expected-error {{must be initialized by a constant expression}} \
75 // expected-note {{value 2147483648 is outside the range}}
76
77 namespace APCast {
78 constexpr _BitInt(10) A = 1;
79 constexpr _BitInt(11) B = A;
80 static_assert(B == 1, "");
81 constexpr _BitInt(16) B2 = A;
82 static_assert(B2 == 1, "");
83 constexpr _BitInt(32) B3 = A;
84 static_assert(B3 == 1, "");
85 constexpr unsigned _BitInt(32) B4 = A;
86 static_assert(B4 == 1, "");
87 }
88
89 #ifdef __SIZEOF_INT128__
90 typedef __int128 int128_t;
91 typedef unsigned __int128 uint128_t;
92 static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
93 static_assert(UINT128_MAX == -1, "");
94 static_assert(UINT128_MAX == 1, ""); // expected-error {{static assertion failed}} \
95 // expected-note {{'340282366920938463463374607431768211455 == 1'}} \
96 // ref-error {{static assertion failed}} \
97 // ref-note {{'340282366920938463463374607431768211455 == 1'}}
98
99 static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
100 static_assert(INT128_MAX != 0, "");
101 static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
102 // expected-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} \
103 // ref-error {{failed}} \
104 // ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
105 static const __int128_t INT128_MIN = -INT128_MAX - 1;
106
107 namespace i128 {
108
109 constexpr int128_t I128_1 = 12;
110 static_assert(I128_1 == 12, "");
111 static_assert(I128_1 != 10, "");
112 static_assert(I128_1 != 12, ""); // expected-error{{failed}} \
113 // ref-error{{failed}} \
114 // expected-note{{evaluates to}} \
115 // ref-note{{evaluates to}}
116
117 static const __uint128_t UINT128_MAX =__uint128_t(__int128_t(-1L));
118 static_assert(UINT128_MAX == -1, "");
119 static_assert(UINT128_MAX == 1, ""); // expected-error {{static assertion failed}} \
120 // expected-note {{'340282366920938463463374607431768211455 == 1'}} \
121 // ref-error {{static assertion failed}} \
122 // ref-note {{'340282366920938463463374607431768211455 == 1'}}
123
124 constexpr uint128_t TooMuch = UINT128_MAX * 2;
125
126 static const __int128_t INT128_MAX = UINT128_MAX >> (__int128_t)1;
127 static_assert(INT128_MAX != 0, "");
128 static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
129 // expected-note {{evaluates to '170141183460469231731687303715884105727 == 0'}} \
130 // ref-error {{failed}} \
131 // ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
132
133 constexpr int128_t TooMuch2 = INT128_MAX * INT128_MAX; // ref-error {{must be initialized by a constant expression}} \
134 // ref-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}} \
135 // expected-error {{must be initialized by a constant expression}} \
136 // expected-note {{value 28948022309329048855892746252171976962977213799489202546401021394546514198529 is outside the range of representable}}
137
138 static const __int128_t INT128_MIN = -INT128_MAX - 1;
139 constexpr __int128 A = INT128_MAX + 1; // expected-error {{must be initialized by a constant expression}} \
140 // expected-note {{value 170141183460469231731687303715884105728 is outside the range}} \
141 // ref-error {{must be initialized by a constant expression}} \
142 // ref-note {{value 170141183460469231731687303715884105728 is outside the range}}
143 constexpr int128_t Two = (int128_t)1 << 1ul;
144 static_assert(Two == 2, "");
145 static_assert(Two, "");
146 constexpr bool CastedToBool = Two;
147 static_assert(CastedToBool, "");
148
149 constexpr uint128_t AllOnes = ~static_cast<uint128_t>(0);
150 static_assert(AllOnes == UINT128_MAX, "");
151
152 constexpr uint128_t i128Zero{};
153 static_assert(i128Zero == 0, "");
154 constexpr uint128_t ui128Zero{};
155 static_assert(ui128Zero == 0, "");
156
157
158 enum LargeEnum : signed __int128 {
159 LV = (signed __int128)1 << 127,
160 };
161
162 constexpr LargeEnum F = LV;
163 static_assert(F == (signed __int128)1 << 127, "");
164 constexpr LargeEnum getLargeEnum() {
165 return LV;
166 }
167 static_assert(getLargeEnum() == (signed __int128)1 << 127, "");
168
169
170
171 #if __cplusplus >= 201402L
172 template <typename T>
173 constexpr T CastFrom(__int128_t A) {
174 T B = (T)A;
175 return B;
176 }
177 static_assert(CastFrom<char>(12) == 12, "");
178 static_assert(CastFrom<unsigned char>(12) == 12, "");
179 static_assert(CastFrom<long>(12) == 12, "");
180 static_assert(CastFrom<unsigned short>(12) == 12, "");
181 static_assert(CastFrom<int128_t>(12) == 12, "");
182 static_assert(CastFrom<float>(12) == 12, "");
183 static_assert(CastFrom<double>(12) == 12, "");
184 static_assert(CastFrom<long double>(12) == 12, "");
185
186 static_assert(CastFrom<char>(AllOnes) == -1, "");
187 static_assert(CastFrom<unsigned char>(AllOnes) == 0xFF, "");
188 static_assert(CastFrom<long>(AllOnes) == -1, "");
189 static_assert(CastFrom<unsigned short>(AllOnes) == 0xFFFF, "");
190 static_assert(CastFrom<int>(AllOnes) == -1, "");
191 static_assert(CastFrom<int128_t>(AllOnes) == -1, "");
192 static_assert(CastFrom<uint128_t>(AllOnes) == AllOnes, "");
193
194 template <typename T>
195 constexpr __int128 CastTo(T A) {
196 int128_t B = (int128_t)A;
197 return B;
198 }
199 static_assert(CastTo<char>(12) == 12, "");
200 static_assert(CastTo<unsigned char>(12) == 12, "");
201 static_assert(CastTo<long>(12) == 12, "");
202 static_assert(CastTo<unsigned long long>(12) == 12, "");
203 static_assert(CastTo<float>(12) == 12, "");
204 static_assert(CastTo<double>(12) == 12, "");
205 static_assert(CastTo<long double>(12) == 12, "");
206 #endif
207
208 constexpr int128_t Error = __LDBL_MAX__; // ref-warning {{implicit conversion of out of range value}} \
209 // ref-error {{must be initialized by a constant expression}} \
210 // ref-note {{is outside the range of representable values of type}} \
211 // expected-warning {{implicit conversion of out of range value}} \
212 // expected-error {{must be initialized by a constant expression}} \
213 // expected-note {{is outside the range of representable values of type}}
214
215 constexpr uint128_t Zero = 0;
216 static_assert((Zero -1) == -1, "");
217 constexpr int128_t Five = 5;
218 static_assert(Five - Zero == Five, "");
219
220 constexpr int128_t Sub1 = INT128_MIN - 1; // expected-error {{must be initialized by a constant expression}} \
221 // expected-note {{-170141183460469231731687303715884105729 is outside the range}} \
222 // ref-error {{must be initialized by a constant expression}} \
223 // ref-note {{-170141183460469231731687303715884105729 is outside the range}}
224 }
225
226 namespace AddSubOffset {
227 constexpr __int128 A = 1;
228 constexpr int arr[] = {1,2,3};
229 constexpr const int *P = arr + A;
230 static_assert(*P == 2, "");
231 constexpr const int *P2 = P - A;
232 static_assert(*P2 == 1,"");
233 }
234
235 namespace Bitfields {
236 struct S1 {
237 unsigned _BitInt(128) a : 2;
238 };
239 constexpr S1 s1{100}; // ref-warning {{changes value from 100 to 0}} \
240 // expected-warning {{changes value from 100 to 0}}
241 constexpr S1 s12{3};
242 static_assert(s12.a == 3, "");
243
244 struct S2 {
245 unsigned __int128 a : 2;
246 };
247 constexpr S2 s2{100}; // ref-warning {{changes value from 100 to 0}} \
248 // expected-warning {{changes value from 100 to 0}}
249 }
250
251 namespace BitOps {
252 constexpr unsigned __int128 UZero = 0;
253 constexpr unsigned __int128 Max = ~UZero;
254 static_assert(Max == ~0, "");
255 static_assert((Max & 0) == 0, "");
256 static_assert((UZero | 0) == 0, "");
257 static_assert((Max ^ Max) == 0, "");
258 static_assert((Max & 1) == 1, "");
259 static_assert((UZero | 1) == 1, "");
260 static_assert((Max ^ UZero) == Max, "");
261 }
262
263 namespace IncDec {
264 #if __cplusplus >= 201402L
265 constexpr int128_t maxPlus1(bool Pre) {
266 int128_t a = INT128_MAX;
267
268 if (Pre)
269 ++a; // ref-note {{value 170141183460469231731687303715884105728 is outside the range}} \
270 // expected-note {{value 170141183460469231731687303715884105728 is outside the range}}
271 else
272 a++; // ref-note {{value 170141183460469231731687303715884105728 is outside the range}} \
273 // expected-note {{value 170141183460469231731687303715884105728 is outside the range}}
274 return a;
275 }
276 static_assert(maxPlus1(true) == 0, ""); // ref-error {{not an integral constant expression}} \
277 // ref-note {{in call to}} \
278 // expected-error {{not an integral constant expression}} \
279 // expected-note {{in call to}}
280 static_assert(maxPlus1(false) == 0, ""); // ref-error {{not an integral constant expression}} \
281 // ref-note {{in call to}} \
282 // expected-error {{not an integral constant expression}} \
283 // expected-note {{in call to}}
284
285 constexpr int128_t inc1(bool Pre) {
286 int128_t A = 0;
287 if (Pre)
288 ++A;
289 else
290 A++;
291 return A;
292 }
293 static_assert(inc1(true) == 1, "");
294 static_assert(inc1(false) == 1, "");
295
296 constexpr int128_t dec1(bool Pre) {
297 int128_t A = 2;
298 if (Pre)
299 --A;
300 else
301 A--;
302 return A;
303 }
304 static_assert(dec1(true) == 1, "");
305 static_assert(dec1(false) == 1, "");
306 #endif
307 }
308
309 #endif