blob: d816c05e3ee3786c17de9588cff4e0f2e31e326f [file] [log] [blame]
Sebastian Redl9cc11e72009-07-25 15:41:38 +00001// RUN: clang-cc -fsyntax-only -verify -faccess-control %s
Sebastian Redl07779722008-10-31 14:43:28 +00002
3struct A {};
4struct B : public A {}; // Single public base.
5struct C1 : public virtual B {}; // Single virtual base.
6struct C2 : public virtual B {};
7struct D : public C1, public C2 {}; // Diamond
Sebastian Redl9cc11e72009-07-25 15:41:38 +00008struct E : private A {}; // Single private base. expected-note 2 {{'private' inheritance specifier here}}
Sebastian Redl07779722008-10-31 14:43:28 +00009struct F : public C1 {}; // Single path to B with virtual.
10struct G1 : public B {};
11struct G2 : public B {};
12struct H : public G1, public G2 {}; // Ambiguous path to B.
13
14enum Enum { En1, En2 };
15enum Onom { On1, On2 };
16
Sebastian Redl9cc11e72009-07-25 15:41:38 +000017struct Co1 { operator int(); };
18struct Co2 { Co2(int); };
19struct Co3 { };
20struct Co4 { Co4(Co3); operator Co3(); };
21
Sebastian Redl07779722008-10-31 14:43:28 +000022// Explicit implicits
23void t_529_2()
24{
25 int i = 1;
26 (void)static_cast<float>(i);
27 double d = 1.0;
28 (void)static_cast<float>(d);
29 (void)static_cast<int>(d);
30 (void)static_cast<char>(i);
31 (void)static_cast<unsigned long>(i);
32 (void)static_cast<int>(En1);
33 (void)static_cast<double>(En1);
34 (void)static_cast<int&>(i);
35 (void)static_cast<const int&>(i);
36
37 int ar[1];
38 (void)static_cast<const int*>(ar);
39 (void)static_cast<void (*)()>(t_529_2);
40
41 (void)static_cast<void*>(0);
42 (void)static_cast<void*>((int*)0);
43 (void)static_cast<volatile const void*>((const int*)0);
44 (void)static_cast<A*>((B*)0);
Sebastian Redl07779722008-10-31 14:43:28 +000045 (void)static_cast<A&>(*((B*)0));
46 (void)static_cast<const B*>((C1*)0);
47 (void)static_cast<B&>(*((C1*)0));
48 (void)static_cast<A*>((D*)0);
49 (void)static_cast<const A&>(*((D*)0));
Sebastian Redl21593ac2009-01-28 18:33:18 +000050 (void)static_cast<int B::*>((int A::*)0);
51 (void)static_cast<void (B::*)()>((void (A::*)())0);
Sebastian Redl07779722008-10-31 14:43:28 +000052
Sebastian Redl9cc11e72009-07-25 15:41:38 +000053 (void)static_cast<int>(Co1());
54 (void)static_cast<Co2>(1);
55 (void)static_cast<Co3>(static_cast<Co4>(Co3()));
Sebastian Redl07779722008-10-31 14:43:28 +000056
57 // Bad code below
58
59 (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}}
60 //(void)static_cast<A*>((E*)0); // {{static_cast from 'struct E *' to 'struct A *' is not allowed}}
61 //(void)static_cast<A*>((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
62 (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
63 (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'struct B **' to 'struct A **' is not allowed}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000064 (void)static_cast<char&>(i); // expected-error {{non-const lvalue reference to type 'char' cannot be initialized with a value of type 'int'}}
Sebastian Redl07779722008-10-31 14:43:28 +000065}
66
67// Anything to void
68void t_529_4()
69{
70 static_cast<void>(1);
71 static_cast<void>(t_529_4);
72}
73
74// Static downcasts
75void t_529_5_8()
76{
77 (void)static_cast<B*>((A*)0);
78 (void)static_cast<B&>(*((A*)0));
79 (void)static_cast<const G1*>((A*)0);
80 (void)static_cast<const G1&>(*((A*)0));
81
82 // Bad code below
83
Sebastian Redle3dc28a2008-11-07 23:29:29 +000084 (void)static_cast<C1*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}}
85 (void)static_cast<C1&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}}
86 (void)static_cast<D*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}}
87 (void)static_cast<D&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}}
88 (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' casts away constness}}
89 (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' casts away constness}}
Sebastian Redl9cc11e72009-07-25 15:41:38 +000090 (void)static_cast<E*>((A*)0); // expected-error {{cannot cast 'struct A' to 'struct E' due to inaccessible}}
91 (void)static_cast<E&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct E' due to inaccessible}}
92 (void)static_cast<H*>((A*)0); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
93 (void)static_cast<H&>(*((A*)0)); // expected-error {{ambiguous cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}}
Sebastian Redl07779722008-10-31 14:43:28 +000094 (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'struct B *' to 'struct E *' is not allowed}}
Sebastian Redl7c80bd62009-03-16 23:22:08 +000095 (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const lvalue reference to type 'struct E' cannot be initialized with a value of type 'struct B'}}
Sebastian Redl07779722008-10-31 14:43:28 +000096
97 // TODO: Test inaccessible base in context where it's accessible, i.e.
98 // member function and friend.
99
100 // TODO: Test DR427. This requires user-defined conversions, though.
101}
102
103// Enum conversions
104void t_529_7()
105{
106 (void)static_cast<Enum>(1);
107 (void)static_cast<Enum>(1.0);
108 (void)static_cast<Onom>(En1);
109
110 // Bad code below
111
112 (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}}
113}
114
115// Void pointer to object pointer
116void t_529_10()
117{
118 (void)static_cast<int*>((void*)0);
119 (void)static_cast<const A*>((void*)0);
120
121 // Bad code below
122
Sebastian Redl4e849352008-11-05 17:54:26 +0000123 (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'void const *' to 'int *' casts away constness}}
Argyrios Kyrtzidis7c94c4b2009-06-03 02:06:50 +0000124 (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)()' is not allowed}}
Sebastian Redl07779722008-10-31 14:43:28 +0000125}
126
Sebastian Redl21593ac2009-01-28 18:33:18 +0000127// Member pointer upcast.
128void t_529_9()
129{
130 (void)static_cast<int A::*>((int B::*)0);
131
132 // Bad code below
133 (void)static_cast<int A::*>((int H::*)0); // expected-error {{ambiguous conversion from pointer to member of derived class 'struct H'}}
134 (void)static_cast<int A::*>((int F::*)0); // expected-error {{conversion from pointer to member of class 'struct F'}}
135}
Sebastian Redl5ed66f72009-10-22 15:07:22 +0000136
137// PR 5261 - static_cast should instantiate template if possible
138namespace pr5261 {
139 struct base {};
140 template<typename E> struct derived : public base {};
141 template<typename E> struct outer {
142 base *pb;
143 ~outer() { (void)static_cast<derived<E>*>(pb); }
144 };
145 outer<int> EntryList;
146}