blob: ccb25f00c24e17d41b6ee81ed5b40eac8bbf6bdf [file] [log] [blame]
John McCall7002f4c2010-04-09 19:03:51 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Sebastian Redl9cc11e72009-07-25 15:41:38 +00002
3struct A {};
4
5// ----------- const_cast --------------
6
7typedef char c;
8typedef c *cp;
9typedef cp *cpp;
10typedef cpp *cppp;
11typedef cppp &cpppr;
12typedef const cppp &cpppcr;
13typedef const char cc;
14typedef cc *ccp;
15typedef volatile ccp ccvp;
16typedef ccvp *ccvpp;
17typedef const volatile ccvpp ccvpcvp;
18typedef ccvpcvp *ccvpcvpp;
19typedef int iar[100];
20typedef iar &iarr;
21typedef int (*f)(int);
22
23void t_cc()
24{
25 ccvpcvpp var = 0;
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.
35 int *pi = (int*)(ar);
36 f fp = 0;
37 // Don't misidentify fn** as a function pointer.
38 f *fpp = (f*)(&fp);
39 int const A::* const A::*icapcap = 0;
40 int A::* A::* iapap = (int A::* A::*)(icapcap);
41}
42
43// ----------- static_cast -------------
44
45struct B : public A {}; // Single public base.
46struct C1 : public virtual B {}; // Single virtual base.
47struct C2 : public virtual B {};
48struct D : public C1, public C2 {}; // Diamond
49struct E : private A {}; // Single private base.
50struct F : public C1 {}; // Single path to B with virtual.
51struct G1 : public B {};
52struct G2 : public B {};
53struct H : public G1, public G2 {}; // Ambiguous path to B.
54
55enum Enum { En1, En2 };
56enum Onom { On1, On2 };
57
58struct Co1 { operator int(); };
59struct Co2 { Co2(int); };
60struct Co3 { };
61struct Co4 { Co4(Co3); operator Co3(); };
62
63// Explicit implicits
64void t_529_2()
65{
66 int i = 1;
67 (void)(float)(i);
68 double d = 1.0;
69 (void)(float)(d);
70 (void)(int)(d);
71 (void)(char)(i);
72 (void)(unsigned long)(i);
73 (void)(int)(En1);
74 (void)(double)(En1);
75 (void)(int&)(i);
76 (void)(const int&)(i);
77
78 int ar[1];
79 (void)(const int*)(ar);
80 (void)(void (*)())(t_529_2);
81
82 (void)(void*)(0);
83 (void)(void*)((int*)0);
84 (void)(volatile const void*)((const int*)0);
85 (void)(A*)((B*)0);
86 (void)(A&)(*((B*)0));
87 (void)(const B*)((C1*)0);
88 (void)(B&)(*((C1*)0));
89 (void)(A*)((D*)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
95
96 (void)(int)(Co1());
97 (void)(Co2)(1);
98 (void)(Co3)((Co4)(Co3()));
99
100 // Bad code below
101 //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
102}
103
104// Anything to void
105void t_529_4()
106{
107 (void)(1);
108 (void)(t_529_4);
109}
110
111// Static downcasts
112void t_529_5_8()
113{
114 (void)(B*)((A*)0);
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
122
123 // Bad code below
124
John McCall7c2342d2010-03-10 11:27:22 +0000125 (void)(C1*)((A*)0); // expected-error {{cannot cast 'A *' to 'C1 *' via virtual base 'B'}}
126 (void)(C1&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1 &' via virtual base 'B'}}
127 (void)(D*)((A*)0); // expected-error {{cannot cast 'A *' to 'D *' via virtual base 'B'}}
128 (void)(D&)(*((A*)0)); // expected-error {{cannot cast 'A' to 'D &' via virtual base 'B'}}
129 (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}}
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}}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000131
132 // TODO: Test DR427. This requires user-defined conversions, though.
133}
134
135// Enum conversions
136void t_529_7()
137{
138 (void)(Enum)(1);
139 (void)(Enum)(1.0);
140 (void)(Onom)(En1);
141
142 // Bad code below
143
John McCall7c2342d2010-03-10 11:27:22 +0000144 (void)(Enum)((int*)0); // expected-error {{C-style cast from 'int *' to 'Enum' is not allowed}}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000145}
146
147// Void pointer to object pointer
148void t_529_10()
149{
150 (void)(int*)((void*)0);
151 (void)(const A*)((void*)0);
152 (void)(int*)((const void*)0); // const_cast appended
153}
154
155// Member pointer upcast.
156void t_529_9()
157{
158 (void)(int A::*)((int B::*)0);
159
160 // Bad code below
John McCall7c2342d2010-03-10 11:27:22 +0000161 (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':}}
162 (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}}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000163}
164
165// -------- reinterpret_cast -----------
166
167enum test { testval = 1 };
168struct structure { int m; };
169typedef void (*fnptr)();
170
171// Test conversion between pointer and integral types, as in p3 and p4.
172void integral_conversion()
173{
174 void *vp = (void*)(testval);
175 long l = (long)(vp);
176 (void)(float*)(l);
177 fnptr fnp = (fnptr)(l);
178 (void)(char)(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
179 (void)(long)(fnp);
180}
181
182void pointer_conversion()
183{
184 int *p1 = 0;
185 float *p2 = (float*)(p1);
186 structure *p3 = (structure*)(p2);
187 typedef int **ppint;
188 ppint *deep = (ppint*)(p3);
189 (void)(fnptr*)(deep);
190}
191
192void constness()
193{
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);
201}
202
203void fnptrs()
204{
205 typedef int (*fnptr2)(int);
206 fnptr fp = 0;
207 (void)(fnptr2)(fp);
208 void *vp = (void*)(fp);
209 (void)(fnptr)(vp);
210}
211
212void refs()
213{
214 long l = 0;
215 char &c = (char&)(l);
216 // Bad: from rvalue
217 (void)(int&)(&c); // expected-error {{C-style cast from rvalue to reference type 'int &'}}
218}
219
220void memptrs()
221{
222 const int structure::*psi = 0;
223 (void)(const float structure::*)(psi);
224 (void)(int structure::*)(psi); // const_cast appended
225
226 void (structure::*psf)() = 0;
227 (void)(int (structure::*)())(psf);
228
John McCall7c2342d2010-03-10 11:27:22 +0000229 (void)(void (structure::*)())(psi); // expected-error {{C-style cast from 'int const structure::*' to 'void (structure::*)()' is not allowed}}
230 (void)(int structure::*)(psf); // expected-error {{C-style cast from 'void (structure::*)()' to 'int structure::*' is not allowed}}
Sebastian Redl9cc11e72009-07-25 15:41:38 +0000231}