xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/static-cast.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc struct A {};
3*f4a2713aSLionel Sambuc struct B : public A {};             // Single public base.
4*f4a2713aSLionel Sambuc struct C1 : public virtual B {};    // Single virtual base.
5*f4a2713aSLionel Sambuc struct C2 : public virtual B {};
6*f4a2713aSLionel Sambuc struct D : public C1, public C2 {}; // Diamond
7*f4a2713aSLionel Sambuc struct E : private A {};            // Single private base. expected-note 3 {{declared private here}}
8*f4a2713aSLionel Sambuc struct F : public C1 {};            // Single path to B with virtual.
9*f4a2713aSLionel Sambuc struct G1 : public B {};
10*f4a2713aSLionel Sambuc struct G2 : public B {};
11*f4a2713aSLionel Sambuc struct H : public G1, public G2 {}; // Ambiguous path to B.
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc enum Enum { En1, En2 };
14*f4a2713aSLionel Sambuc enum Onom { On1, On2 };
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc struct Co1 { operator int(); };
17*f4a2713aSLionel Sambuc struct Co2 { Co2(int); };
18*f4a2713aSLionel Sambuc struct Co3 { };
19*f4a2713aSLionel Sambuc struct Co4 { Co4(Co3); operator Co3(); };
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc // Explicit implicits
22*f4a2713aSLionel Sambuc void t_529_2()
23*f4a2713aSLionel Sambuc {
24*f4a2713aSLionel Sambuc   int i = 1;
25*f4a2713aSLionel Sambuc   (void)static_cast<float>(i);
26*f4a2713aSLionel Sambuc   double d = 1.0;
27*f4a2713aSLionel Sambuc   (void)static_cast<float>(d);
28*f4a2713aSLionel Sambuc   (void)static_cast<int>(d);
29*f4a2713aSLionel Sambuc   (void)static_cast<char>(i);
30*f4a2713aSLionel Sambuc   (void)static_cast<unsigned long>(i);
31*f4a2713aSLionel Sambuc   (void)static_cast<int>(En1);
32*f4a2713aSLionel Sambuc   (void)static_cast<double>(En1);
33*f4a2713aSLionel Sambuc   (void)static_cast<int&>(i);
34*f4a2713aSLionel Sambuc   (void)static_cast<const int&>(i);
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc   int ar[1];
37*f4a2713aSLionel Sambuc   (void)static_cast<const int*>(ar);
38*f4a2713aSLionel Sambuc   (void)static_cast<void (*)()>(t_529_2);
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc   (void)static_cast<void*>(0);
41*f4a2713aSLionel Sambuc   (void)static_cast<void*>((int*)0);
42*f4a2713aSLionel Sambuc   (void)static_cast<volatile const void*>((const int*)0);
43*f4a2713aSLionel Sambuc   (void)static_cast<A*>((B*)0);
44*f4a2713aSLionel Sambuc   (void)static_cast<A&>(*((B*)0));
45*f4a2713aSLionel Sambuc   (void)static_cast<const B*>((C1*)0);
46*f4a2713aSLionel Sambuc   (void)static_cast<B&>(*((C1*)0));
47*f4a2713aSLionel Sambuc   (void)static_cast<A*>((D*)0);
48*f4a2713aSLionel Sambuc   (void)static_cast<const A&>(*((D*)0));
49*f4a2713aSLionel Sambuc   (void)static_cast<int B::*>((int A::*)0);
50*f4a2713aSLionel Sambuc   (void)static_cast<void (B::*)()>((void (A::*)())0);
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   (void)static_cast<int>(Co1());
53*f4a2713aSLionel Sambuc   (void)static_cast<Co2>(1);
54*f4a2713aSLionel Sambuc   (void)static_cast<Co3>(static_cast<Co4>(Co3()));
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc   // Bad code below
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'const int *' to 'void *' is not allowed}}
59*f4a2713aSLionel Sambuc   (void)static_cast<A*>((E*)0); // expected-error {{cannot cast 'E' to its private base class 'A'}}
60*f4a2713aSLionel Sambuc   (void)static_cast<A*>((H*)0); // expected-error {{ambiguous conversion}}
61*f4a2713aSLionel Sambuc   (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
62*f4a2713aSLionel Sambuc   (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'B **' to 'A **' is not allowed}}
63*f4a2713aSLionel Sambuc   (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot bind to a value of unrelated type 'int'}}
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc // Anything to void
67*f4a2713aSLionel Sambuc void t_529_4()
68*f4a2713aSLionel Sambuc {
69*f4a2713aSLionel Sambuc   static_cast<void>(1);
70*f4a2713aSLionel Sambuc   static_cast<void>(t_529_4);
71*f4a2713aSLionel Sambuc }
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc // Static downcasts
74*f4a2713aSLionel Sambuc void t_529_5_8()
75*f4a2713aSLionel Sambuc {
76*f4a2713aSLionel Sambuc   (void)static_cast<B*>((A*)0);
77*f4a2713aSLionel Sambuc   (void)static_cast<B&>(*((A*)0));
78*f4a2713aSLionel Sambuc   (void)static_cast<const G1*>((A*)0);
79*f4a2713aSLionel Sambuc   (void)static_cast<const G1&>(*((A*)0));
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc   // Bad code below
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc   (void)static_cast<C1*>((A*)0); // expected-error {{cannot cast 'A *' to 'C1 *' via virtual base 'B'}}
84*f4a2713aSLionel Sambuc   (void)static_cast<C1&>(*((A*)0)); // expected-error {{cannot cast 'A' to 'C1 &' via virtual base 'B'}}
85*f4a2713aSLionel Sambuc   (void)static_cast<D*>((A*)0); // expected-error {{cannot cast 'A *' to 'D *' via virtual base 'B'}}
86*f4a2713aSLionel Sambuc   (void)static_cast<D&>(*((A*)0)); // expected-error {{cannot cast 'A' to 'D &' via virtual base 'B'}}
87*f4a2713aSLionel Sambuc   (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'const A *' to 'B *' casts away qualifiers}}
88*f4a2713aSLionel Sambuc   (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'const A' to 'B &' casts away qualifiers}}
89*f4a2713aSLionel Sambuc   (void)static_cast<E*>((A*)0); // expected-error {{cannot cast private base class 'A' to 'E'}}
90*f4a2713aSLionel Sambuc   (void)static_cast<E&>(*((A*)0)); // expected-error {{cannot cast private base class 'A' to 'E'}}
91*f4a2713aSLionel Sambuc   (void)static_cast<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}}
92*f4a2713aSLionel Sambuc   (void)static_cast<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}}
93*f4a2713aSLionel Sambuc   (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'B *' to 'E *' is not allowed}}
94*f4a2713aSLionel Sambuc   (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'E' cannot bind to a value of unrelated type 'B'}}
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   // TODO: Test inaccessible base in context where it's accessible, i.e.
97*f4a2713aSLionel Sambuc   // member function and friend.
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc   // TODO: Test DR427. This requires user-defined conversions, though.
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc // Enum conversions
103*f4a2713aSLionel Sambuc void t_529_7()
104*f4a2713aSLionel Sambuc {
105*f4a2713aSLionel Sambuc   (void)static_cast<Enum>(1);
106*f4a2713aSLionel Sambuc   (void)static_cast<Enum>(1.0);
107*f4a2713aSLionel Sambuc   (void)static_cast<Onom>(En1);
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc   // Bad code below
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc   (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'Enum' is not allowed}}
112*f4a2713aSLionel Sambuc }
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc // Void pointer to object pointer
115*f4a2713aSLionel Sambuc void t_529_10()
116*f4a2713aSLionel Sambuc {
117*f4a2713aSLionel Sambuc   (void)static_cast<int*>((void*)0);
118*f4a2713aSLionel Sambuc   (void)static_cast<const A*>((void*)0);
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc   // Bad code below
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc   (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'const void *' to 'int *' casts away qualifiers}}
123*f4a2713aSLionel Sambuc   (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)()' is not allowed}}
124*f4a2713aSLionel Sambuc }
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc // Member pointer upcast.
127*f4a2713aSLionel Sambuc void t_529_9()
128*f4a2713aSLionel Sambuc {
129*f4a2713aSLionel Sambuc   (void)static_cast<int A::*>((int B::*)0);
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc   // Bad code below
132*f4a2713aSLionel Sambuc   (void)static_cast<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':}}
133*f4a2713aSLionel Sambuc   (void)static_cast<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}}
134*f4a2713aSLionel Sambuc }
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc // PR 5261 - static_cast should instantiate template if possible
137*f4a2713aSLionel Sambuc namespace pr5261 {
138*f4a2713aSLionel Sambuc   struct base {};
139*f4a2713aSLionel Sambuc   template<typename E> struct derived : public base {};
140*f4a2713aSLionel Sambuc   template<typename E> struct outer {
141*f4a2713aSLionel Sambuc     base *pb;
142*f4a2713aSLionel Sambuc     ~outer() { (void)static_cast<derived<E>*>(pb); }
143*f4a2713aSLionel Sambuc   };
144*f4a2713aSLionel Sambuc   outer<int> EntryList;
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc // Initialization by constructor
149*f4a2713aSLionel Sambuc struct X0;
150*f4a2713aSLionel Sambuc 
151*f4a2713aSLionel Sambuc struct X1 {
152*f4a2713aSLionel Sambuc   X1();
153*f4a2713aSLionel Sambuc   X1(X1&);
154*f4a2713aSLionel Sambuc   X1(const X0&);
155*f4a2713aSLionel Sambuc 
156*f4a2713aSLionel Sambuc   operator X0() const;
157*f4a2713aSLionel Sambuc };
158*f4a2713aSLionel Sambuc 
159*f4a2713aSLionel Sambuc struct X0 { };
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc void test_ctor_init() {
162*f4a2713aSLionel Sambuc   (void)static_cast<X1>(X1());
163*f4a2713aSLionel Sambuc }
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc // Casting away constness
166*f4a2713aSLionel Sambuc struct X2 {
167*f4a2713aSLionel Sambuc };
168*f4a2713aSLionel Sambuc 
169*f4a2713aSLionel Sambuc struct X3 : X2 {
170*f4a2713aSLionel Sambuc };
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc struct X4 {
173*f4a2713aSLionel Sambuc   typedef const X3 X3_typedef;
174*f4a2713aSLionel Sambuc 
175*f4a2713aSLionel Sambuc   void f() const {
176*f4a2713aSLionel Sambuc     (void)static_cast<X3_typedef*>(x2);
177*f4a2713aSLionel Sambuc   }
178*f4a2713aSLionel Sambuc 
179*f4a2713aSLionel Sambuc   const X2 *x2;
180*f4a2713aSLionel Sambuc };
181*f4a2713aSLionel Sambuc 
182*f4a2713aSLionel Sambuc // PR5897 - accept static_cast from const void* to const int (*)[1].
183*f4a2713aSLionel Sambuc void PR5897() { (void)static_cast<const int(*)[1]>((const void*)0); }
184*f4a2713aSLionel Sambuc 
185*f4a2713aSLionel Sambuc namespace PR6072 {
186*f4a2713aSLionel Sambuc   struct A { };
187*f4a2713aSLionel Sambuc   struct B : A { void f(int); void f(); };  // expected-note 2{{candidate function}}
188*f4a2713aSLionel Sambuc   struct C : B { };
189*f4a2713aSLionel Sambuc   struct D { };
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc   void f() {
192*f4a2713aSLionel Sambuc     (void)static_cast<void (A::*)()>(&B::f);
193*f4a2713aSLionel Sambuc     (void)static_cast<void (B::*)()>(&B::f);
194*f4a2713aSLionel Sambuc     (void)static_cast<void (C::*)()>(&B::f);
195*f4a2713aSLionel Sambuc     (void)static_cast<void (D::*)()>(&B::f); // expected-error{{address of overloaded function 'f' cannot be static_cast to type 'void (PR6072::D::*)()'}}
196*f4a2713aSLionel Sambuc   }
197*f4a2713aSLionel Sambuc }
198