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