1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc // REQUIRES: LP64
3f4a2713aSLionel Sambuc
4f4a2713aSLionel Sambuc // ------------ not interpreted as C-style cast ------------
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuc struct SimpleValueInit {
7f4a2713aSLionel Sambuc int i;
8f4a2713aSLionel Sambuc };
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc struct InitViaConstructor {
11f4a2713aSLionel Sambuc InitViaConstructor(int i = 7);
12f4a2713aSLionel Sambuc };
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc struct NoValueInit { // expected-note 2 {{candidate constructor (the implicit copy constructor)}}
15f4a2713aSLionel Sambuc NoValueInit(int i, int j); // expected-note 2 {{candidate constructor}}
16f4a2713aSLionel Sambuc };
17f4a2713aSLionel Sambuc
test_cxx_functional_value_init()18f4a2713aSLionel Sambuc void test_cxx_functional_value_init() {
19f4a2713aSLionel Sambuc (void)SimpleValueInit();
20f4a2713aSLionel Sambuc (void)InitViaConstructor();
21f4a2713aSLionel Sambuc (void)NoValueInit(); // expected-error{{no matching constructor for initialization}}
22f4a2713aSLionel Sambuc }
23f4a2713aSLionel Sambuc
test_cxx_function_cast_multi()24f4a2713aSLionel Sambuc void test_cxx_function_cast_multi() {
25f4a2713aSLionel Sambuc (void)NoValueInit(0, 0);
26f4a2713aSLionel Sambuc (void)NoValueInit(0, 0, 0); // expected-error{{no matching constructor for initialization}}
27f4a2713aSLionel Sambuc (void)int(1, 2); // expected-error{{excess elements in scalar initializer}}
28f4a2713aSLionel Sambuc }
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc // ------------------ everything else --------------------
32f4a2713aSLionel Sambuc
33f4a2713aSLionel Sambuc struct A {};
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc // ----------- const_cast --------------
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc typedef char c;
38f4a2713aSLionel Sambuc typedef c *cp;
39f4a2713aSLionel Sambuc typedef cp *cpp;
40f4a2713aSLionel Sambuc typedef cpp *cppp;
41f4a2713aSLionel Sambuc typedef cppp &cpppr;
42f4a2713aSLionel Sambuc typedef const cppp &cpppcr;
43f4a2713aSLionel Sambuc typedef const char cc;
44f4a2713aSLionel Sambuc typedef cc *ccp;
45f4a2713aSLionel Sambuc typedef volatile ccp ccvp;
46f4a2713aSLionel Sambuc typedef ccvp *ccvpp;
47f4a2713aSLionel Sambuc typedef const volatile ccvpp ccvpcvp;
48f4a2713aSLionel Sambuc typedef ccvpcvp *ccvpcvpp;
49f4a2713aSLionel Sambuc typedef int iar[100];
50f4a2713aSLionel Sambuc typedef iar &iarr;
51f4a2713aSLionel Sambuc typedef int (*f)(int);
52f4a2713aSLionel Sambuc
t_cc()53f4a2713aSLionel Sambuc void t_cc()
54f4a2713aSLionel Sambuc {
55f4a2713aSLionel Sambuc ccvpcvpp var = 0;
56f4a2713aSLionel Sambuc // Cast away deep consts and volatiles.
57f4a2713aSLionel Sambuc char ***var2 = cppp(var);
58f4a2713aSLionel Sambuc char ***const &var3 = var2;
59f4a2713aSLionel Sambuc // Const reference to reference.
60f4a2713aSLionel Sambuc char ***&var4 = cpppr(var3);
61f4a2713aSLionel Sambuc // Drop reference. Intentionally without qualifier change.
62f4a2713aSLionel Sambuc char *** var5 = cppp(var4);
63f4a2713aSLionel Sambuc const int ar[100] = {0};
64f4a2713aSLionel Sambuc // Array decay. Intentionally without qualifier change.
65f4a2713aSLionel Sambuc typedef int *intp;
66f4a2713aSLionel Sambuc int *pi = intp(ar);
67f4a2713aSLionel Sambuc f fp = 0;
68f4a2713aSLionel Sambuc // Don't misidentify fn** as a function pointer.
69f4a2713aSLionel Sambuc typedef f *fp_t;
70f4a2713aSLionel Sambuc f *fpp = fp_t(&fp);
71f4a2713aSLionel Sambuc int const A::* const A::*icapcap = 0;
72f4a2713aSLionel Sambuc typedef int A::* A::*iapap_t;
73f4a2713aSLionel Sambuc iapap_t iapap = iapap_t(icapcap);
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc // ----------- static_cast -------------
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc struct B : public A {}; // Single public base.
79f4a2713aSLionel Sambuc struct C1 : public virtual B {}; // Single virtual base.
80f4a2713aSLionel Sambuc struct C2 : public virtual B {};
81f4a2713aSLionel Sambuc struct D : public C1, public C2 {}; // Diamond
82f4a2713aSLionel Sambuc struct E : private A {}; // Single private base.
83f4a2713aSLionel Sambuc struct F : public C1 {}; // Single path to B with virtual.
84f4a2713aSLionel Sambuc struct G1 : public B {};
85f4a2713aSLionel Sambuc struct G2 : public B {};
86f4a2713aSLionel Sambuc struct H : public G1, public G2 {}; // Ambiguous path to B.
87f4a2713aSLionel Sambuc
88f4a2713aSLionel Sambuc enum Enum { En1, En2 };
89f4a2713aSLionel Sambuc enum Onom { On1, On2 };
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc struct Co1 { operator int(); };
92f4a2713aSLionel Sambuc struct Co2 { Co2(int); };
93f4a2713aSLionel Sambuc struct Co3 { };
94f4a2713aSLionel Sambuc struct Co4 { Co4(Co3); operator Co3(); };
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc // Explicit implicits
t_529_2()97f4a2713aSLionel Sambuc void t_529_2()
98f4a2713aSLionel Sambuc {
99f4a2713aSLionel Sambuc int i = 1;
100f4a2713aSLionel Sambuc (void)float(i);
101f4a2713aSLionel Sambuc double d = 1.0;
102f4a2713aSLionel Sambuc (void)float(d);
103f4a2713aSLionel Sambuc (void)int(d);
104f4a2713aSLionel Sambuc (void)char(i);
105f4a2713aSLionel Sambuc typedef unsigned long ulong;
106f4a2713aSLionel Sambuc (void)ulong(i);
107f4a2713aSLionel Sambuc (void)int(En1);
108f4a2713aSLionel Sambuc (void)double(En1);
109f4a2713aSLionel Sambuc typedef int &intr;
110f4a2713aSLionel Sambuc (void)intr(i);
111f4a2713aSLionel Sambuc typedef const int &cintr;
112f4a2713aSLionel Sambuc (void)cintr(i);
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc int ar[1];
115f4a2713aSLionel Sambuc typedef const int *cintp;
116f4a2713aSLionel Sambuc (void)cintp(ar);
117f4a2713aSLionel Sambuc typedef void (*pfvv)();
118f4a2713aSLionel Sambuc (void)pfvv(t_529_2);
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc typedef void *voidp;
121f4a2713aSLionel Sambuc (void)voidp(0);
122f4a2713aSLionel Sambuc (void)voidp((int*)0);
123f4a2713aSLionel Sambuc typedef volatile const void *vcvoidp;
124f4a2713aSLionel Sambuc (void)vcvoidp((const int*)0);
125f4a2713aSLionel Sambuc typedef A *Ap;
126f4a2713aSLionel Sambuc (void)Ap((B*)0);
127f4a2713aSLionel Sambuc typedef A &Ar;
128f4a2713aSLionel Sambuc (void)Ar(*((B*)0));
129f4a2713aSLionel Sambuc typedef const B *cBp;
130f4a2713aSLionel Sambuc (void)cBp((C1*)0);
131f4a2713aSLionel Sambuc typedef B &Br;
132f4a2713aSLionel Sambuc (void)Br(*((C1*)0));
133f4a2713aSLionel Sambuc (void)Ap((D*)0);
134f4a2713aSLionel Sambuc typedef const A &cAr;
135f4a2713aSLionel Sambuc (void)cAr(*((D*)0));
136f4a2713aSLionel Sambuc typedef int B::*Bmp;
137f4a2713aSLionel Sambuc (void)Bmp((int A::*)0);
138f4a2713aSLionel Sambuc typedef void (B::*Bmfp)();
139f4a2713aSLionel Sambuc (void)Bmfp((void (A::*)())0);
140f4a2713aSLionel Sambuc (void)Ap((E*)0); // functional-style cast ignores access control
141f4a2713aSLionel Sambuc (void)voidp((const int*)0); // const_cast appended
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc (void)int(Co1());
144f4a2713aSLionel Sambuc (void)Co2(1);
145f4a2713aSLionel Sambuc (void)Co3((Co4)(Co3()));
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc // Bad code below
148f4a2713aSLionel Sambuc //(void)(A*)((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc // Anything to void
t_529_4()152f4a2713aSLionel Sambuc void t_529_4()
153f4a2713aSLionel Sambuc {
154f4a2713aSLionel Sambuc void(1);
155f4a2713aSLionel Sambuc (void(t_529_4));
156f4a2713aSLionel Sambuc }
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc // Static downcasts
t_529_5_8()159f4a2713aSLionel Sambuc void t_529_5_8()
160f4a2713aSLionel Sambuc {
161f4a2713aSLionel Sambuc typedef B *Bp;
162f4a2713aSLionel Sambuc (void)Bp((A*)0);
163f4a2713aSLionel Sambuc typedef B &Br;
164f4a2713aSLionel Sambuc (void)Br(*((A*)0));
165f4a2713aSLionel Sambuc typedef const G1 *cG1p;
166f4a2713aSLionel Sambuc (void)cG1p((A*)0);
167f4a2713aSLionel Sambuc typedef const G1 &cG1r;
168f4a2713aSLionel Sambuc (void)cG1r(*((A*)0));
169f4a2713aSLionel Sambuc (void)Bp((const A*)0); // const_cast appended
170f4a2713aSLionel Sambuc (void)Br(*((const A*)0)); // const_cast appended
171f4a2713aSLionel Sambuc typedef E *Ep;
172f4a2713aSLionel Sambuc (void)Ep((A*)0); // access control ignored
173f4a2713aSLionel Sambuc typedef E &Er;
174f4a2713aSLionel Sambuc (void)Er(*((A*)0)); // access control ignored
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc // Bad code below
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc typedef C1 *C1p;
179f4a2713aSLionel Sambuc (void)C1p((A*)0); // expected-error {{cannot cast 'A *' to 'C1p' (aka 'C1 *') via virtual base 'B'}}
180f4a2713aSLionel Sambuc typedef C1 &C1r;
181f4a2713aSLionel Sambuc (void)C1r(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1r' (aka 'C1 &') via virtual base 'B'}}
182f4a2713aSLionel Sambuc typedef D *Dp;
183f4a2713aSLionel Sambuc (void)Dp((A*)0); // expected-error {{cannot cast 'A *' to 'Dp' (aka 'D *') via virtual base 'B'}}
184f4a2713aSLionel Sambuc typedef D &Dr;
185f4a2713aSLionel Sambuc (void)Dr(*((A*)0)); // expected-error {{cannot cast 'A' to 'Dr' (aka 'D &') via virtual base 'B'}}
186f4a2713aSLionel Sambuc typedef H *Hp;
187f4a2713aSLionel Sambuc (void)Hp((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}}
188f4a2713aSLionel Sambuc typedef H &Hr;
189f4a2713aSLionel Sambuc (void)Hr(*((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}}
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc // TODO: Test DR427. This requires user-defined conversions, though.
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc // Enum conversions
t_529_7()195f4a2713aSLionel Sambuc void t_529_7()
196f4a2713aSLionel Sambuc {
197f4a2713aSLionel Sambuc (void)Enum(1);
198f4a2713aSLionel Sambuc (void)Enum(1.0);
199f4a2713aSLionel Sambuc (void)Onom(En1);
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc // Bad code below
202f4a2713aSLionel Sambuc
203f4a2713aSLionel Sambuc (void)Enum((int*)0); // expected-error {{functional-style cast from 'int *' to 'Enum' is not allowed}}
204f4a2713aSLionel Sambuc }
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc // Void pointer to object pointer
t_529_10()207f4a2713aSLionel Sambuc void t_529_10()
208f4a2713aSLionel Sambuc {
209f4a2713aSLionel Sambuc typedef int *intp;
210f4a2713aSLionel Sambuc (void)intp((void*)0);
211f4a2713aSLionel Sambuc typedef const A *cAp;
212f4a2713aSLionel Sambuc (void)cAp((void*)0);
213f4a2713aSLionel Sambuc (void)intp((const void*)0); // const_cast appended
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc
216f4a2713aSLionel Sambuc // Member pointer upcast.
t_529_9()217f4a2713aSLionel Sambuc void t_529_9()
218f4a2713aSLionel Sambuc {
219f4a2713aSLionel Sambuc typedef int A::*Amp;
220f4a2713aSLionel Sambuc (void)Amp((int B::*)0);
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc // Bad code below
223f4a2713aSLionel Sambuc (void)Amp((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'H' to pointer to member of base class 'A':}}
224f4a2713aSLionel Sambuc (void)Amp((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}}
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc // -------- reinterpret_cast -----------
228f4a2713aSLionel Sambuc
229f4a2713aSLionel Sambuc enum test { testval = 1 };
230f4a2713aSLionel Sambuc struct structure { int m; };
231f4a2713aSLionel Sambuc typedef void (*fnptr)();
232f4a2713aSLionel Sambuc
233f4a2713aSLionel Sambuc // Test conversion between pointer and integral types, as in p3 and p4.
integral_conversion()234f4a2713aSLionel Sambuc void integral_conversion()
235f4a2713aSLionel Sambuc {
236f4a2713aSLionel Sambuc typedef void *voidp;
237f4a2713aSLionel Sambuc void *vp = voidp(testval);
238f4a2713aSLionel Sambuc long l = long(vp);
239f4a2713aSLionel Sambuc typedef float *floatp;
240f4a2713aSLionel Sambuc (void)floatp(l);
241f4a2713aSLionel Sambuc fnptr fnp = fnptr(l);
242f4a2713aSLionel Sambuc (void)char(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}}
243f4a2713aSLionel Sambuc (void)long(fnp);
244f4a2713aSLionel Sambuc }
245f4a2713aSLionel Sambuc
pointer_conversion()246f4a2713aSLionel Sambuc void pointer_conversion()
247f4a2713aSLionel Sambuc {
248f4a2713aSLionel Sambuc int *p1 = 0;
249f4a2713aSLionel Sambuc typedef float *floatp;
250f4a2713aSLionel Sambuc float *p2 = floatp(p1);
251f4a2713aSLionel Sambuc typedef structure *structurep;
252f4a2713aSLionel Sambuc structure *p3 = structurep(p2);
253f4a2713aSLionel Sambuc typedef int **ppint;
254f4a2713aSLionel Sambuc typedef ppint *pppint;
255f4a2713aSLionel Sambuc ppint *deep = pppint(p3);
256f4a2713aSLionel Sambuc typedef fnptr fnptrp;
257f4a2713aSLionel Sambuc (void)fnptrp(deep);
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc
constness()260f4a2713aSLionel Sambuc void constness()
261f4a2713aSLionel Sambuc {
262f4a2713aSLionel Sambuc int ***const ipppc = 0;
263f4a2713aSLionel Sambuc typedef int const *icp_t;
264f4a2713aSLionel Sambuc int const *icp = icp_t(ipppc);
265f4a2713aSLionel Sambuc typedef int *intp;
266f4a2713aSLionel Sambuc (void)intp(icp); // const_cast appended
267f4a2713aSLionel Sambuc typedef int const *const ** intcpcpp;
268f4a2713aSLionel Sambuc intcpcpp icpcpp = intcpcpp(ipppc); // const_cast appended
269f4a2713aSLionel Sambuc int *ip = intp(icpcpp);
270f4a2713aSLionel Sambuc (void)icp_t(ip);
271f4a2713aSLionel Sambuc typedef int const *const *const *intcpcpcp;
272f4a2713aSLionel Sambuc (void)intcpcpcp(ipppc);
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc
fnptrs()275f4a2713aSLionel Sambuc void fnptrs()
276f4a2713aSLionel Sambuc {
277f4a2713aSLionel Sambuc typedef int (*fnptr2)(int);
278f4a2713aSLionel Sambuc fnptr fp = 0;
279f4a2713aSLionel Sambuc (void)fnptr2(fp);
280f4a2713aSLionel Sambuc typedef void *voidp;
281f4a2713aSLionel Sambuc void *vp = voidp(fp);
282f4a2713aSLionel Sambuc (void)fnptr(vp);
283f4a2713aSLionel Sambuc }
284f4a2713aSLionel Sambuc
refs()285f4a2713aSLionel Sambuc void refs()
286f4a2713aSLionel Sambuc {
287f4a2713aSLionel Sambuc long l = 0;
288f4a2713aSLionel Sambuc typedef char &charr;
289f4a2713aSLionel Sambuc char &c = charr(l);
290f4a2713aSLionel Sambuc // Bad: from rvalue
291f4a2713aSLionel Sambuc typedef int &intr;
292f4a2713aSLionel Sambuc (void)intr(&c); // expected-error {{functional-style cast from rvalue to reference type 'intr' (aka 'int &')}}
293f4a2713aSLionel Sambuc }
294f4a2713aSLionel Sambuc
memptrs()295f4a2713aSLionel Sambuc void memptrs()
296f4a2713aSLionel Sambuc {
297f4a2713aSLionel Sambuc const int structure::*psi = 0;
298f4a2713aSLionel Sambuc typedef const float structure::*structurecfmp;
299f4a2713aSLionel Sambuc (void)structurecfmp(psi);
300f4a2713aSLionel Sambuc typedef int structure::*structureimp;
301f4a2713aSLionel Sambuc (void)structureimp(psi); // const_cast appended
302f4a2713aSLionel Sambuc
303f4a2713aSLionel Sambuc void (structure::*psf)() = 0;
304f4a2713aSLionel Sambuc typedef int (structure::*structureimfp)();
305f4a2713aSLionel Sambuc (void)structureimfp(psf);
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc typedef void (structure::*structurevmfp)();
308*0a6a1f1dSLionel Sambuc (void)structurevmfp(psi); // expected-error-re {{functional-style cast from 'const int structure::*' to 'structurevmfp' (aka 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}') is not allowed}}
309*0a6a1f1dSLionel Sambuc (void)structureimp(psf); // expected-error-re {{functional-style cast from 'void (structure::*)(){{( __attribute__\(\(thiscall\)\))?}}' to 'structureimp' (aka 'int structure::*') is not allowed}}
310f4a2713aSLionel Sambuc }
311f4a2713aSLionel Sambuc
312f4a2713aSLionel Sambuc // ---------------- misc ------------------
313f4a2713aSLionel Sambuc
crash_on_invalid_1()314f4a2713aSLionel Sambuc void crash_on_invalid_1()
315f4a2713aSLionel Sambuc {
316f4a2713aSLionel Sambuc typedef itn Typo; // expected-error {{unknown type name 'itn'}}
317f4a2713aSLionel Sambuc (void)Typo(1); // used to crash
318f4a2713aSLionel Sambuc
319f4a2713aSLionel Sambuc typedef int &int_ref;
320f4a2713aSLionel Sambuc (void)int_ref(); // expected-error {{reference to type 'int' requires an initializer}}
321f4a2713aSLionel Sambuc }
322