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