blob: b3fe49a88c6bfc67dbbb0e7a6e4d0b9d254190d3 [file] [log] [blame]
John McCall7002f4c2010-04-09 19:03:51 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Sebastian Redl07779722008-10-31 14:43:28 +00002struct A {};
3struct B : public A {}; // Single public base.
4struct C1 : public virtual B {}; // Single virtual base.
5struct C2 : public virtual B {};
6struct D : public C1, public C2 {}; // Diamond
John McCall6b2accb2010-02-10 09:31:12 +00007struct E : private A {}; // Single private base. expected-note 3 {{declared private here}}
Sebastian Redl07779722008-10-31 14:43:28 +00008struct F : public C1 {}; // Single path to B with virtual.
9struct G1 : public B {};
10struct G2 : public B {};
11struct H : public G1, public G2 {}; // Ambiguous path to B.
Stephen Hines0e2c34f2015-03-23 12:09:02 -070012struct I; // Incomplete. expected-note {{'I' is incomplete}}
13struct J; // Incomplete. expected-note {{'J' is incomplete}}
Sebastian Redl07779722008-10-31 14:43:28 +000014
15enum Enum { En1, En2 };
16enum Onom { On1, On2 };
17
Sebastian Redl9cc11e72009-07-25 15:41:38 +000018struct Co1 { operator int(); };
19struct Co2 { Co2(int); };
20struct Co3 { };
21struct Co4 { Co4(Co3); operator Co3(); };
22
Sebastian Redl07779722008-10-31 14:43:28 +000023// Explicit implicits
24void 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);
Sebastian Redl07779722008-10-31 14:43:28 +000046 (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));
Sebastian Redl21593ac2009-01-28 18:33:18 +000051 (void)static_cast<int B::*>((int A::*)0);
52 (void)static_cast<void (B::*)()>((void (A::*)())0);
Sebastian Redl07779722008-10-31 14:43:28 +000053
Sebastian Redl9cc11e72009-07-25 15:41:38 +000054 (void)static_cast<int>(Co1());
55 (void)static_cast<Co2>(1);
56 (void)static_cast<Co3>(static_cast<Co4>(Co3()));
Sebastian Redl07779722008-10-31 14:43:28 +000057
58 // Bad code below
59
Chris Lattner58f9e132010-09-05 00:04:01 +000060 (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'const int *' to 'void *' is not allowed}}
John McCall7c2342d2010-03-10 11:27:22 +000061 (void)static_cast<A*>((E*)0); // expected-error {{cannot cast 'E' to its private base class 'A'}}
Sebastian Redla82e4ae2009-11-14 21:15:49 +000062 (void)static_cast<A*>((H*)0); // expected-error {{ambiguous conversion}}
Sebastian Redl07779722008-10-31 14:43:28 +000063 (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
John McCall7c2342d2010-03-10 11:27:22 +000064 (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'B **' to 'A **' is not allowed}}
Douglas Gregorc6e378e2010-03-24 23:38:29 +000065 (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot bind to a value of unrelated type 'int'}}
Sebastian Redl07779722008-10-31 14:43:28 +000066}
67
68// Anything to void
69void t_529_4()
70{
71 static_cast<void>(1);
72 static_cast<void>(t_529_4);
73}
74
75// Static downcasts
76void 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
John McCall7c2342d2010-03-10 11:27:22 +000085 (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'}}
Douglas Gregord4c5f842011-04-15 17:59:54 +000089 (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}}
John McCall7c2342d2010-03-10 11:27:22 +000091 (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}}
Stephen Hines0e2c34f2015-03-23 12:09:02 -070095 (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'B *' to 'E *', which are not related by inheritance, is not allowed}}
Douglas Gregorc6e378e2010-03-24 23:38:29 +000096 (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'E' cannot bind to a value of unrelated type 'B'}}
Sebastian Redl07779722008-10-31 14:43:28 +000097
Stephen Hines0e2c34f2015-03-23 12:09:02 -070098
99 (void)static_cast<E*>((J*)0); // expected-error {{static_cast from 'J *' to 'E *', which are not related by inheritance, is not allowed}}
100 (void)static_cast<I*>((B*)0); // expected-error {{static_cast from 'B *' to 'I *', which are not related by inheritance, is not allowed}}
101
Sebastian Redl07779722008-10-31 14:43:28 +0000102 // TODO: Test inaccessible base in context where it's accessible, i.e.
103 // member function and friend.
104
105 // TODO: Test DR427. This requires user-defined conversions, though.
106}
107
108// Enum conversions
109void t_529_7()
110{
111 (void)static_cast<Enum>(1);
112 (void)static_cast<Enum>(1.0);
113 (void)static_cast<Onom>(En1);
114
115 // Bad code below
116
John McCall7c2342d2010-03-10 11:27:22 +0000117 (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'Enum' is not allowed}}
Sebastian Redl07779722008-10-31 14:43:28 +0000118}
119
120// Void pointer to object pointer
121void t_529_10()
122{
123 (void)static_cast<int*>((void*)0);
124 (void)static_cast<const A*>((void*)0);
125
126 // Bad code below
127
Douglas Gregord4c5f842011-04-15 17:59:54 +0000128 (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'const void *' to 'int *' casts away qualifiers}}
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +0000129 (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)()' is not allowed}}
Sebastian Redl07779722008-10-31 14:43:28 +0000130}
131
Sebastian Redl21593ac2009-01-28 18:33:18 +0000132// Member pointer upcast.
133void t_529_9()
134{
135 (void)static_cast<int A::*>((int B::*)0);
136
137 // Bad code below
John McCall7c2342d2010-03-10 11:27:22 +0000138 (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':}}
139 (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}}
Stephen Hines651f13c2014-04-23 16:59:28 -0700140 (void)static_cast<int I::*>((int J::*)0); // expected-error {{static_cast from 'int J::*' to 'int I::*' is not allowed}}
Sebastian Redl21593ac2009-01-28 18:33:18 +0000141}
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000142
143// PR 5261 - static_cast should instantiate template if possible
144namespace pr5261 {
145 struct base {};
146 template<typename E> struct derived : public base {};
147 template<typename E> struct outer {
148 base *pb;
149 ~outer() { (void)static_cast<derived<E>*>(pb); }
150 };
151 outer<int> EntryList;
152}
Douglas Gregor19aeac62009-11-14 03:27:21 +0000153
154
155// Initialization by constructor
156struct X0;
157
158struct X1 {
159 X1();
160 X1(X1&);
161 X1(const X0&);
162
163 operator X0() const;
164};
165
166struct X0 { };
167
168void test_ctor_init() {
169 (void)static_cast<X1>(X1());
170}
Douglas Gregorab15d0e2009-11-15 09:20:52 +0000171
172// Casting away constness
173struct X2 {
174};
175
176struct X3 : X2 {
177};
178
179struct X4 {
180 typedef const X3 X3_typedef;
181
182 void f() const {
183 (void)static_cast<X3_typedef*>(x2);
184 }
185
186 const X2 *x2;
187};
Chandler Carruthc099d9b2010-01-31 11:51:51 +0000188
189// PR5897 - accept static_cast from const void* to const int (*)[1].
Chandler Carruthe1cd3372010-01-31 11:52:52 +0000190void PR5897() { (void)static_cast<const int(*)[1]>((const void*)0); }
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000191
192namespace PR6072 {
193 struct A { };
Douglas Gregor8e960432010-11-08 03:40:48 +0000194 struct B : A { void f(int); void f(); }; // expected-note 2{{candidate function}}
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000195 struct C : B { };
196 struct D { };
197
198 void f() {
199 (void)static_cast<void (A::*)()>(&B::f);
200 (void)static_cast<void (B::*)()>(&B::f);
201 (void)static_cast<void (C::*)()>(&B::f);
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 (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\)\))?}}'}}
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000203 }
204}