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