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