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