1 // RUN: %clang_cc1 -fsyntax-only -verify %s -Wimplicit-int-conversion -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>
61 void deduced_whole_type(T){}
62 template<int I>
63 void deduced_bound(_ExtInt(I)){}
64 
65 // Ensure ext-int can be used in template places.
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)' with less than 1 byte of precision}}
95 _Atomic _ExtInt(4) TooSmallAtomic;
96 // expected-error@+1{{_Atomic cannot be applied to integer type '_ExtInt(9)' with a non power of 2 precision}}
97 _Atomic _ExtInt(9) NotPow2Atomic;
98 _Atomic _ExtInt(128) JustRightAtomic;
99 
100 // Test result types of Unary/Bitwise/Binary Operations:
101 void Ops() {
102   _ExtInt(43) x43_s = 1, y43_s = 1;
103   _ExtInt(sizeof(int) * 8) x32_s = 1, y32_s = 1;
104   unsigned _ExtInt(sizeof(unsigned) * 8) x32_u = 1, y32_u = 1;
105   _ExtInt(4) x4_s = 1, y4_s = 1;
106   unsigned _ExtInt(43) x43_u = 1, y43_u = 1;
107   unsigned _ExtInt(4) x4_u = 1, y4_u = 1;
108   int x_int = 1, y_int = 1;
109   unsigned x_uint = 1, y_uint = 1;
110   bool b;
111 
112   // Disabling mixed conversions:
113   // Signed/unsigned mixed.
114   // expected-error@+1{{invalid operands to binary expression}}
115   x43_u + y43_s;
116   // expected-error@+1{{invalid operands to binary expression}}
117   x4_s - y4_u;
118   // expected-error@+1{{invalid operands to binary expression}}
119   x43_s * y43_u;
120   // expected-error@+1{{invalid operands to binary expression}}
121   x4_u / y4_s;
122 
123   // Different Sizes.
124   // expected-error@+1{{invalid operands to binary expression}}
125   x43_s + y4_s;
126   // expected-error@+1{{invalid operands to binary expression}}
127   x43_s - y4_u;
128   // expected-error@+1{{invalid operands to binary expression}}
129   x43_u * y4_u;
130   // expected-error@+1{{invalid operands to binary expression}}
131   x4_u / y43_u;
132 
133   // Mixed with standard types.
134   // expected-error@+1{{invalid operands to binary expression}}
135   x43_s + x_int;
136   // expected-error@+1{{invalid operands to binary expression}}
137   x43_u - x_int;
138   // expected-error@+1{{invalid operands to binary expression}}
139   x32_s * x_int;
140   // expected-error@+1{{invalid operands to binary expression}}
141   x32_u / x_int;
142   // expected-error@+1{{invalid operands to binary expression}}
143   x32_s * x_uint;
144   // expected-error@+1{{invalid operands to binary expression}}
145   x32_u / x_uint;
146   // expected-error@+1{{invalid operands to binary expression}}
147   x4_s + x_int;
148   // expected-error@+1{{invalid operands to binary expression}}
149   x4_u - x_int;
150   // expected-error@+1{{invalid operands to binary expression}}
151   x4_s + b;
152   // expected-error@+1{{invalid operands to binary expression}}
153   x4_u - b;
154   // expected-error@+1{{invalid operands to binary expression}}
155   x43_s + b;
156   // expected-error@+1{{invalid operands to binary expression}}
157   x43_u - b;
158 
159   // Bitwise checks.
160   // expected-error@+1{{invalid operands to binary expression}}
161   x43_s % y4_u;
162   // expected-error@+1{{invalid operands to binary expression}}
163   x43_u % y4_s;
164   // expected-error@+1{{invalid operands to binary expression}}
165   x4_s | y43_u;
166   // expected-error@+1{{invalid operands to binary expression}}
167   x4_u | y43_s;
168 
169   // compassign.
170   // expected-error@+1{{invalid operands to binary expression}}
171   x43_s += 33;
172 
173   // Comparisons.
174   // expected-error@+1{{invalid operands to binary expression}}
175   x43_s > 33;
176   // expected-error@+1{{invalid operands to binary expression}}
177   x4_s > 33;
178 
179   // Same size/sign ops don't change type.
180   static_assert(is_same<decltype(x43_s + y43_s), _ExtInt(43)>::value,"");
181   static_assert(is_same<decltype(x4_s - y4_s), _ExtInt(4)>::value,"");
182   static_assert(is_same<decltype(x43_u * y43_u), unsigned _ExtInt(43)>::value,"");
183   static_assert(is_same<decltype(x4_u / y4_u), unsigned _ExtInt(4)>::value,"");
184 
185   // Unary ops shouldn't go through integer promotions.
186   static_assert(is_same<decltype(~x43_s), _ExtInt(43)>::value,"");
187   static_assert(is_same<decltype(~x4_s), _ExtInt(4)>::value,"");
188   static_assert(is_same<decltype(+x43_s), _ExtInt(43)>::value,"");
189   static_assert(is_same<decltype(+x4_s), _ExtInt(4)>::value,"");
190   static_assert(is_same<decltype(-x43_u), unsigned _ExtInt(43)>::value,"");
191   static_assert(is_same<decltype(-x4_u), unsigned _ExtInt(4)>::value,"");
192   // expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
193   static_assert(is_same<decltype(++x43_s), _ExtInt(43)&>::value,"");
194   // expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
195   static_assert(is_same<decltype(--x4_s), _ExtInt(4)&>::value,"");
196   // expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
197   static_assert(is_same<decltype(x43_s--), _ExtInt(43)>::value,"");
198   // expected-warning@+1{{expression with side effects has no effect in an unevaluated context}}
199   static_assert(is_same<decltype(x4_s++), _ExtInt(4)>::value,"");
200   static_assert(is_same<decltype(x4_s >> 1), _ExtInt(4)>::value,"");
201   static_assert(is_same<decltype(x4_u << 1), unsigned _ExtInt(4)>::value,"");
202 
203   static_assert(sizeof(x43_s) == 8, "");
204   static_assert(sizeof(x4_s) == 1, "");
205 
206   static_assert(sizeof(_ExtInt(3340)) == 424, ""); // 424 * 8 == 3392.
207   static_assert(sizeof(_ExtInt(1049)) == 136, ""); // 136  *  8 == 1088.
208 
209   static_assert(alignof(decltype(x43_s)) == 8, "");
210   static_assert(alignof(decltype(x4_s)) == 1, "");
211 
212   static_assert(alignof(_ExtInt(3340)) == 8, "");
213   static_assert(alignof(_ExtInt(1049)) == 8, "");
214 }
215 
216 constexpr int func() { return 42;}
217 
218 void ConstexprBitsize() {
219   _ExtInt(func()) F;
220   static_assert(is_same<decltype(F), _ExtInt(42)>::value, "");
221 }
222 
223 // Useable as an underlying type.
224 enum AsEnumUnderlyingType : _ExtInt(33) {
225 };
226 
227 void overloaded(int);
228 void overloaded(_ExtInt(32));
229 void overloaded(_ExtInt(33));
230 void overloaded(short);
231 //expected-note@+1{{candidate function}}
232 void overloaded2(_ExtInt(32));
233 //expected-note@+1{{candidate function}}
234 void overloaded2(_ExtInt(33));
235 //expected-note@+1{{candidate function}}
236 void overloaded2(short);
237 
238 void overload_use() {
239   int i;
240   _ExtInt(32) i32;
241   _ExtInt(33) i33;
242   short s;
243 
244   // All of these get their corresponding exact matches.
245   overloaded(i);
246   overloaded(i32);
247   overloaded(i33);
248   overloaded(s);
249 
250   overloaded2(i); // expected-error{{call to 'overloaded2' is ambiguous}}
251 
252   overloaded2(i32);
253 
254   overloaded2(s);
255 }
256 
257 // no errors expected, this should 'just work'.
258 struct UsedAsBitField {
259   _ExtInt(3) F : 3;
260   _ExtInt(3) G : 3;
261   _ExtInt(3) H : 3;
262 };
263 
264 // expected-error@+1{{mode attribute only supported for integer and floating-point types}}
265 typedef _ExtInt(33) IllegalMode __attribute__((mode(DI)));
266 
267 void ImplicitCasts(_ExtInt(31) s31, _ExtInt(33) s33, int i) {
268   // expected-warning@+1{{implicit conversion loses integer precision}}
269   s31 = i;
270   // expected-warning@+1{{implicit conversion loses integer precision}}
271   s31 = s33;
272   s33 = i;
273   s33 = s31;
274   i = s31;
275   // expected-warning@+1{{implicit conversion loses integer precision}}
276   i = s33;
277 }
278 
279