1// RUN: %clang_cc1 -fsyntax-only -verify %s 2// REQUIRES: LP64 3 4struct A {}; 5 6// ----------- const_cast -------------- 7 8typedef char c; 9typedef c *cp; 10typedef cp *cpp; 11typedef cpp *cppp; 12typedef cppp &cpppr; 13typedef const cppp &cpppcr; 14typedef const char cc; 15typedef cc *ccp; 16typedef volatile ccp ccvp; 17typedef ccvp *ccvpp; 18typedef const volatile ccvpp ccvpcvp; 19typedef ccvpcvp *ccvpcvpp; 20typedef int iar[100]; 21typedef iar &iarr; 22typedef int (*f)(int); 23 24void t_cc() 25{ 26 ccvpcvpp var = 0; 27 // Cast away deep consts and volatiles. 28 char ***var2 = (cppp)(var); 29 char ***const &var3 = var2; 30 // Const reference to reference. 31 char ***&var4 = (cpppr)(var3); 32 // Drop reference. Intentionally without qualifier change. 33 char *** var5 = (cppp)(var4); 34 const int ar[100] = {0}; 35 // Array decay. Intentionally without qualifier change. 36 int *pi = (int*)(ar); 37 f fp = 0; 38 // Don't misidentify fn** as a function pointer. 39 f *fpp = (f*)(&fp); 40 int const A::* const A::*icapcap = 0; 41 int A::* A::* iapap = (int A::* A::*)(icapcap); 42} 43 44// ----------- static_cast ------------- 45 46struct B : public A {}; // Single public base. 47struct C1 : public virtual B {}; // Single virtual base. 48struct C2 : public virtual B {}; 49struct D : public C1, public C2 {}; // Diamond 50struct E : private A {}; // Single private base. 51struct F : public C1 {}; // Single path to B with virtual. 52struct G1 : public B {}; 53struct G2 : public B {}; 54struct H : public G1, public G2 {}; // Ambiguous path to B. 55 56enum Enum { En1, En2 }; 57enum Onom { On1, On2 }; 58 59struct Co1 { operator int(); }; 60struct Co2 { Co2(int); }; 61struct Co3 { }; 62struct Co4 { Co4(Co3); operator Co3(); }; 63 64// Explicit implicits 65void t_529_2() 66{ 67 int i = 1; 68 (void)(float)(i); 69 double d = 1.0; 70 (void)(float)(d); 71 (void)(int)(d); 72 (void)(char)(i); 73 (void)(unsigned long)(i); 74 (void)(int)(En1); 75 (void)(double)(En1); 76 (void)(int&)(i); 77 (void)(const int&)(i); 78 79 int ar[1]; 80 (void)(const int*)(ar); 81 (void)(void (*)())(t_529_2); 82 83 (void)(void*)(0); 84 (void)(void*)((int*)0); 85 (void)(volatile const void*)((const int*)0); 86 (void)(A*)((B*)0); 87 (void)(A&)(*((B*)0)); 88 (void)(const B*)((C1*)0); 89 (void)(B&)(*((C1*)0)); 90 (void)(A*)((D*)0); 91 (void)(const A&)(*((D*)0)); 92 (void)(int B::*)((int A::*)0); 93 (void)(void (B::*)())((void (A::*)())0); 94 (void)(A*)((E*)0); // C-style cast ignores access control 95 (void)(void*)((const int*)0); // const_cast appended 96 97 (void)(int)(Co1()); 98 (void)(Co2)(1); 99 (void)(Co3)((Co4)(Co3())); 100 101 // Bad code below 102 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}} 103} 104 105// Anything to void 106void t_529_4() 107{ 108 (void)(1); 109 (void)(t_529_4); 110} 111 112// Static downcasts 113void t_529_5_8() 114{ 115 (void)(B*)((A*)0); 116 (void)(B&)(*((A*)0)); 117 (void)(const G1*)((A*)0); 118 (void)(const G1&)(*((A*)0)); 119 (void)(B*)((const A*)0); // const_cast appended 120 (void)(B&)(*((const A*)0)); // const_cast appended 121 (void)(E*)((A*)0); // access control ignored 122 (void)(E&)(*((A*)0)); // access control ignored 123 124 // Bad code below 125 126 (void)(C1*)((A*)0); // expected-error {{cannot cast 'A *' to 'C1 *' via virtual base 'B'}} 127 (void)(C1&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1 &' via virtual base 'B'}} 128 (void)(D*)((A*)0); // expected-error {{cannot cast 'A *' to 'D *' via virtual base 'B'}} 129 (void)(D&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'D &' via virtual base 'B'}} 130 (void)(H*)((A*)0); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} 131 (void)(H&)(*((A*)0)); // expected-error {{ambiguous cast from base 'A' to derived 'H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} 132 133 // TODO: Test DR427. This requires user-defined conversions, though. 134} 135 136// Enum conversions 137void t_529_7() 138{ 139 (void)(Enum)(1); 140 (void)(Enum)(1.0); 141 (void)(Onom)(En1); 142 143 // Bad code below 144 145 (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}} 146} 147 148// Void pointer to object pointer 149void t_529_10() 150{ 151 (void)(int*)((void*)0); 152 (void)(const A*)((void*)0); 153 (void)(int*)((const void*)0); // const_cast appended 154} 155 156// Member pointer upcast. 157void t_529_9() 158{ 159 (void)(int A::*)((int B::*)0); 160 161 // Bad code below 162 (void)(int A::*)((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}} 163 (void)(int A::*)((int F::*)0); // expected-error {{conversion from pointer to member of class 'F' to pointer to member of class 'A' via virtual base 'B' is not allowed}} 164} 165 166// -------- reinterpret_cast ----------- 167 168enum test { testval = 1 }; 169struct structure { int m; }; 170typedef void (*fnptr)(); 171 172// Test conversion between pointer and integral types, as in p3 and p4. 173void integral_conversion() 174{ 175 void *vp = (void*)(testval); 176 long l = (long)(vp); 177 (void)(float*)(l); 178 fnptr fnp = (fnptr)(l); 179 (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} 180 (void)(long)(fnp); 181} 182 183void pointer_conversion() 184{ 185 int *p1 = 0; 186 float *p2 = (float*)(p1); 187 structure *p3 = (structure*)(p2); 188 typedef int **ppint; 189 ppint *deep = (ppint*)(p3); 190 (void)(fnptr*)(deep); 191} 192 193void constness() 194{ 195 int ***const ipppc = 0; 196 int const *icp = (int const*)(ipppc); 197 (void)(int*)(icp); // const_cast appended 198 int const *const **icpcpp = (int const* const**)(ipppc); // const_cast appended 199 int *ip = (int*)(icpcpp); 200 (void)(int const*)(ip); 201 (void)(int const* const* const*)(ipppc); 202} 203 204void fnptrs() 205{ 206 typedef int (*fnptr2)(int); 207 fnptr fp = 0; 208 (void)(fnptr2)(fp); 209 void *vp = (void*)(fp); 210 (void)(fnptr)(vp); 211} 212 213void refs() 214{ 215 long l = 0; 216 char &c = (char&)(l); 217 // Bad: from rvalue 218 (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}} 219} 220 221void memptrs() 222{ 223 const int structure::*psi = 0; 224 (void)(const float structure::*)(psi); 225 (void)(int structure::*)(psi); // const_cast appended 226 227 void (structure::*psf)() = 0; 228 (void)(int (structure::*)())(psf); 229 230 (void)(void (structure::*)())(psi); // expected-error-re {{C-style cast from 'const int structure::*' to 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' is not allowed}} 231 (void)(int structure::*)(psf); // expected-error-re {{C-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'int structure::*' is not allowed}} 232} 233