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