1 // RUN: %clang_cc1 -fsyntax-only -verify -faccess-control %s
5 // ----------- const_cast --------------
12 typedef const cppp &cpppcr;
13 typedef const char cc;
15 typedef volatile ccp ccvp;
17 typedef const volatile ccvpp ccvpcvp;
18 typedef ccvpcvp *ccvpcvpp;
21 typedef int (*f)(int);
26 // Cast away deep consts and volatiles.
27 char ***var2 = (cppp)(var);
28 char ***const &var3 = var2;
29 // Const reference to reference.
30 char ***&var4 = (cpppr)(var3);
31 // Drop reference. Intentionally without qualifier change.
32 char *** var5 = (cppp)(var4);
33 const int ar[100] = {0};
34 // Array decay. Intentionally without qualifier change.
37 // Don't misidentify fn** as a function pointer.
39 int const A::* const A::*icapcap = 0;
40 int A::* A::* iapap = (int A::* A::*)(icapcap);
43 // ----------- static_cast -------------
45 struct B : public A {}; // Single public base.
46 struct C1 : public virtual B {}; // Single virtual base.
47 struct C2 : public virtual B {};
48 struct D : public C1, public C2 {}; // Diamond
49 struct E : private A {}; // Single private base.
50 struct F : public C1 {}; // Single path to B with virtual.
51 struct G1 : public B {};
52 struct G2 : public B {};
53 struct H : public G1, public G2 {}; // Ambiguous path to B.
55 enum Enum { En1, En2 };
56 enum Onom { On1, On2 };
58 struct Co1 { operator int(); };
59 struct Co2 { Co2(int); };
61 struct Co4 { Co4(Co3); operator Co3(); };
72 (void)(unsigned long)(i);
76 (void)(const int&)(i);
79 (void)(const int*)(ar);
80 (void)(void (*)())(t_529_2);
83 (void)(void*)((int*)0);
84 (void)(volatile const void*)((const int*)0);
87 (void)(const B*)((C1*)0);
88 (void)(B&)(*((C1*)0));
90 (void)(const A&)(*((D*)0));
91 (void)(int B::*)((int A::*)0);
92 (void)(void (B::*)())((void (A::*)())0);
93 (void)(A*)((E*)0); // C-style cast ignores access control
94 (void)(void*)((const int*)0); // const_cast appended
98 (void)(Co3)((Co4)(Co3()));
101 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
115 (void)(B&)(*((A*)0));
116 (void)(const G1*)((A*)0);
117 (void)(const G1&)(*((A*)0));
118 (void)(B*)((const A*)0); // const_cast appended
119 (void)(B&)(*((const A*)0)); // const_cast appended
120 (void)(E*)((A*)0); // access control ignored
121 (void)(E&)(*((A*)0)); // access control ignored
125 (void)(C1*)((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}}
126 (void)(C1&)(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}}
127 (void)(D*)((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}}
128 (void)(D&)(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}}
129 (void)(H*)((A*)0); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
130 (void)(H&)(*((A*)0)); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
132 // TODO: Test DR427. This requires user-defined conversions, though.
144 (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'enum Enum' is not allowed}}
147 // Void pointer to object pointer
150 (void)(int*)((void*)0);
151 (void)(const A*)((void*)0);
152 (void)(int*)((const void*)0); // const_cast appended
155 // Member pointer upcast.
158 (void)(int A::*)((int B::*)0);
161 (void)(int A::*)((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'struct H'}}
162 (void)(int A::*)((int F::*)0); // expected-error {{conversion from pointer to member of class 'struct F'}}
165 // -------- reinterpret_cast -----------
167 enum test { testval = 1 };
168 struct structure { int m; };
169 typedef void (*fnptr)();
171 // Test conversion between pointer and integral types, as in p3 and p4.
172 void integral_conversion()
174 void *vp = (void*)(testval);
177 fnptr fnp = (fnptr)(l);
178 (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
182 void pointer_conversion()
185 float *p2 = (float*)(p1);
186 structure *p3 = (structure*)(p2);
188 ppint *deep = (ppint*)(p3);
189 (void)(fnptr*)(deep);
194 int ***const ipppc = 0;
195 int const *icp = (int const*)(ipppc);
196 (void)(int*)(icp); // const_cast appended
197 int const *const **icpcpp = (int const* const**)(ipppc); // const_cast appended
198 int *ip = (int*)(icpcpp);
199 (void)(int const*)(ip);
200 (void)(int const* const* const*)(ipppc);
205 typedef int (*fnptr2)(int);
208 void *vp = (void*)(fp);
215 char &c = (char&)(l);
217 (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}}
222 const int structure::*psi = 0;
223 (void)(const float structure::*)(psi);
224 (void)(int structure::*)(psi); // const_cast appended
226 void (structure::*psf)() = 0;
227 (void)(int (structure::*)())(psf);
229 (void)(void (structure::*)())(psi); // expected-error {{C-style cast from 'int const struct structure::*' to 'void (struct structure::*)()' is not allowed}}
230 (void)(int structure::*)(psf); // expected-error {{C-style cast from 'void (struct structure::*)()' to 'int struct structure::*' is not allowed}}