ext-int.cpp
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// RUN: %clang_cc1 -fsyntax-only -verify %s -Wimplicit-int-conversion -triple x86_64-gnu-linux
template<int Bounds>
struct HasExtInt {
_ExtInt(Bounds) b;
unsigned _ExtInt(Bounds) b2;
};
// Delcaring variables:
_ExtInt(33) Declarations(_ExtInt(48) &Param) { // Useable in params and returns.
short _ExtInt(43) a; // expected-error {{'short _ExtInt' is invalid}}
_ExtInt(43) long b; // expected-error {{'long _ExtInt' is invalid}}
// These should all be fine:
const _ExtInt(5) c = 3;
const unsigned _ExtInt(5) d; // expected-error {{default initialization of an object of const type 'const unsigned _ExtInt(5)'}}
unsigned _ExtInt(5) e = 5;
_ExtInt(5) unsigned f;
_ExtInt(-3) g; // expected-error{{signed _ExtInt must have a bit size of at least 2}}
_ExtInt(0) h; // expected-error{{signed _ExtInt must have a bit size of at least 2}}
_ExtInt(1) i; // expected-error{{signed _ExtInt must have a bit size of at least 2}}
_ExtInt(2) j;;
unsigned _ExtInt(0) k;// expected-error{{unsigned _ExtInt must have a bit size of at least 1}}
unsigned _ExtInt(1) l;
signed _ExtInt(1) m; // expected-error{{signed _ExtInt must have a bit size of at least 2}}
constexpr _ExtInt(6) n = 33; // expected-warning{{implicit conversion from 'int' to 'const _ExtInt(6)' changes value from 33 to -31}}
constexpr _ExtInt(7) o = 33;
// Check LLVM imposed max size.
_ExtInt(0xFFFFFFFFFF) p; // expected-error {{signed _ExtInt of bit sizes greater than 16777215 not supported}}
unsigned _ExtInt(0xFFFFFFFFFF) q; // expected-error {{unsigned _ExtInt of bit sizes greater than 16777215 not supported}}
// Ensure template params are instantiated correctly.
// expected-error@5{{signed _ExtInt must have a bit size of at least 2}}
// expected-error@6{{unsigned _ExtInt must have a bit size of at least 1}}
// expected-note@+1{{in instantiation of template class }}
HasExtInt<-1> r;
// expected-error@5{{signed _ExtInt must have a bit size of at least 2}}
// expected-error@6{{unsigned _ExtInt must have a bit size of at least 1}}
// expected-note@+1{{in instantiation of template class }}
HasExtInt<0> s;
// expected-error@5{{signed _ExtInt must have a bit size of at least 2}}
// expected-note@+1{{in instantiation of template class }}
HasExtInt<1> t;
HasExtInt<2> u;
_ExtInt(-3.0) v; // expected-error {{integral constant expression must have integral or unscoped enumeration type, not 'double'}}
_ExtInt(3.0) x; // expected-error {{integral constant expression must have integral or unscoped enumeration type, not 'double'}}
return 0;
}
template <_ExtInt(5) I>
struct ExtIntTemplParam {
static constexpr _ExtInt(5) Var = I;
};
template<typename T>
void deduced_whole_type(T){}
template<int I>
void deduced_bound(_ExtInt(I)){}
// Ensure ext-int can be used in template places.
void Templates() {
ExtIntTemplParam<13> a;
constexpr _ExtInt(3) b = 1;
ExtIntTemplParam<b> c;
constexpr _ExtInt(9) d = 1;
ExtIntTemplParam<b> e;
deduced_whole_type(b);
deduced_bound(b);
}
template <typename T, typename U>
struct is_same {
static constexpr bool value = false;
};
template <typename T>
struct is_same<T,T> {
static constexpr bool value = true;
};
// Reject vector types:
// expected-error@+1{{invalid vector element type '_ExtInt(32)'}}
typedef _ExtInt(32) __attribute__((vector_size(16))) VecTy;
// Allow _Complex:
_Complex _ExtInt(3) Cmplx;
// Reject cases of _Atomic:
// expected-error@+1{{_Atomic cannot be applied to integer type '_ExtInt(4)'}}
_Atomic _ExtInt(4) TooSmallAtomic;
// expected-error@+1{{_Atomic cannot be applied to integer type '_ExtInt(9)'}}
_Atomic _ExtInt(9) NotPow2Atomic;
// expected-error@+1{{_Atomic cannot be applied to integer type '_ExtInt(128)'}}
_Atomic _ExtInt(128) JustRightAtomic;
// Test result types of Unary/Bitwise/Binary Operations:
void Ops() {
_ExtInt(43) x43_s = 1, y43_s = 1;
_ExtInt(sizeof(int) * 8) x32_s = 1, y32_s = 1;
unsigned _ExtInt(sizeof(unsigned) * 8) x32_u = 1, y32_u = 1;
_ExtInt(4) x4_s = 1, y4_s = 1;
unsigned _ExtInt(43) x43_u = 1, y43_u = 1;
unsigned _ExtInt(4) x4_u = 1, y4_u = 1;
int x_int = 1, y_int = 1;
unsigned x_uint = 1, y_uint = 1;
bool b;
// Disabling mixed conversions:
// Signed/unsigned mixed.
// expected-error@+1{{invalid operands to binary expression}}
x43_u + y43_s;
// expected-error@+1{{invalid operands to binary expression}}
x4_s - y4_u;
// expected-error@+1{{invalid operands to binary expression}}
x43_s * y43_u;
// expected-error@+1{{invalid operands to binary expression}}
x4_u / y4_s;
// Different Sizes.
// expected-error@+1{{invalid operands to binary expression}}
x43_s + y4_s;
// expected-error@+1{{invalid operands to binary expression}}
x43_s - y4_u;
// expected-error@+1{{invalid operands to binary expression}}
x43_u * y4_u;
// expected-error@+1{{invalid operands to binary expression}}
x4_u / y43_u;
// Mixed with standard types.
// expected-error@+1{{invalid operands to binary expression}}
x43_s + x_int;
// expected-error@+1{{invalid operands to binary expression}}
x43_u - x_int;
// expected-error@+1{{invalid operands to binary expression}}
x32_s * x_int;
// expected-error@+1{{invalid operands to binary expression}}
x32_u / x_int;
// expected-error@+1{{invalid operands to binary expression}}
x32_s * x_uint;
// expected-error@+1{{invalid operands to binary expression}}
x32_u / x_uint;
// expected-error@+1{{invalid operands to binary expression}}
x4_s + x_int;
// expected-error@+1{{invalid operands to binary expression}}
x4_u - x_int;
// expected-error@+1{{invalid operands to binary expression}}
x4_s + b;
// expected-error@+1{{invalid operands to binary expression}}
x4_u - b;
// expected-error@+1{{invalid operands to binary expression}}
x43_s + b;
// expected-error@+1{{invalid operands to binary expression}}
x43_u - b;
// Bitwise checks.
// expected-error@+1{{invalid operands to binary expression}}
x43_s % y4_u;
// expected-error@+1{{invalid operands to binary expression}}
x43_u % y4_s;
// expected-error@+1{{invalid operands to binary expression}}
x4_s | y43_u;
// expected-error@+1{{invalid operands to binary expression}}
x4_u | y43_s;
// compassign.
// expected-error@+1{{invalid operands to binary expression}}
x43_s += 33;
// Comparisons.
// expected-error@+1{{invalid operands to binary expression}}
x43_s > 33;
// expected-error@+1{{invalid operands to binary expression}}
x4_s > 33;
// Same size/sign ops don't change type.
static_assert(is_same<decltype(x43_s + y43_s), _ExtInt(43)>::value,"");
static_assert(is_same<decltype(x4_s - y4_s), _ExtInt(4)>::value,"");
static_assert(is_same<decltype(x43_u * y43_u), unsigned _ExtInt(43)>::value,"");
static_assert(is_same<decltype(x4_u / y4_u), unsigned _ExtInt(4)>::value,"");
// Unary ops shouldn't go through integer promotions.
static_assert(is_same<decltype(~x43_s), _ExtInt(43)>::value,"");
static_assert(is_same<decltype(~x4_s), _ExtInt(4)>::value,"");
static_assert(is_same<decltype(+x43_s), _ExtInt(43)>::value,"");
static_assert(is_same<decltype(+x4_s), _ExtInt(4)>::value,"");
static_assert(is_same<decltype(-x43_u), unsigned _ExtInt(43)>::value,"");
static_assert(is_same<decltype(-x4_u), unsigned _ExtInt(4)>::value,"");
// expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
static_assert(is_same<decltype(++x43_s), _ExtInt(43)&>::value,"");
// expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
static_assert(is_same<decltype(--x4_s), _ExtInt(4)&>::value,"");
// expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
static_assert(is_same<decltype(x43_s--), _ExtInt(43)>::value,"");
// expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
static_assert(is_same<decltype(x4_s++), _ExtInt(4)>::value,"");
static_assert(is_same<decltype(x4_s >> 1), _ExtInt(4)>::value,"");
static_assert(is_same<decltype(x4_u << 1), unsigned _ExtInt(4)>::value,"");
static_assert(sizeof(x43_s) == 8, "");
static_assert(sizeof(x4_s) == 1, "");
static_assert(sizeof(_ExtInt(3340)) == 424, ""); // 424 * 8 == 3392.
static_assert(sizeof(_ExtInt(1049)) == 136, ""); // 136 * 8 == 1088.
static_assert(alignof(decltype(x43_s)) == 8, "");
static_assert(alignof(decltype(x4_s)) == 1, "");
static_assert(alignof(_ExtInt(3340)) == 8, "");
static_assert(alignof(_ExtInt(1049)) == 8, "");
}
constexpr int func() { return 42;}
void ConstexprBitsize() {
_ExtInt(func()) F;
static_assert(is_same<decltype(F), _ExtInt(42)>::value, "");
}
// Useable as an underlying type.
enum AsEnumUnderlyingType : _ExtInt(33) {
};
void overloaded(int);
void overloaded(_ExtInt(32));
void overloaded(_ExtInt(33));
void overloaded(short);
//expected-note@+1{{candidate function}}
void overloaded2(_ExtInt(32));
//expected-note@+1{{candidate function}}
void overloaded2(_ExtInt(33));
//expected-note@+1{{candidate function}}
void overloaded2(short);
void overload_use() {
int i;
_ExtInt(32) i32;
_ExtInt(33) i33;
short s;
// All of these get their corresponding exact matches.
overloaded(i);
overloaded(i32);
overloaded(i33);
overloaded(s);
overloaded2(i); // expected-error{{call to 'overloaded2' is ambiguous}}
overloaded2(i32);
overloaded2(s);
}
// no errors expected, this should 'just work'.
struct UsedAsBitField {
_ExtInt(3) F : 3;
_ExtInt(3) G : 3;
_ExtInt(3) H : 3;
};
// expected-error@+1{{mode attribute only supported for integer and floating-point types}}
typedef _ExtInt(33) IllegalMode __attribute__((mode(DI)));
void ImplicitCasts(_ExtInt(31) s31, _ExtInt(33) s33, int i) {
// expected-warning@+1{{implicit conversion loses integer precision}}
s31 = i;
// expected-warning@+1{{implicit conversion loses integer precision}}
s31 = s33;
s33 = i;
s33 = s31;
i = s31;
// expected-warning@+1{{implicit conversion loses integer precision}}
i = s33;
}
void Ternary(_ExtInt(30) s30, _ExtInt(31) s31a, _ExtInt(31) s31b,
_ExtInt(32) s32, bool b) {
b ? s30 : s31a; // expected-error{{incompatible operand types}}
b ? s31a : s30; // expected-error{{incompatible operand types}}
b ? s32 : (int)0; // expected-error{{incompatible operand types}}
(void)(b ? s31a : s31b);
(void)(s30 ? s31a : s31b);
}