blob: 44b4921af8a865aebc2c60eb0f1cc93e6f759aec [file] [log] [blame]
Sebastian Redl9ac68aa2008-10-31 14:43:28 +00001// RUN: clang -fsyntax-only -verify %s
2
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
8struct E : private A {}; // Single private base.
9struct 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
17// Explicit implicits
18void t_529_2()
19{
20 int i = 1;
21 (void)static_cast<float>(i);
22 double d = 1.0;
23 (void)static_cast<float>(d);
24 (void)static_cast<int>(d);
25 (void)static_cast<char>(i);
26 (void)static_cast<unsigned long>(i);
27 (void)static_cast<int>(En1);
28 (void)static_cast<double>(En1);
29 (void)static_cast<int&>(i);
30 (void)static_cast<const int&>(i);
31
32 int ar[1];
33 (void)static_cast<const int*>(ar);
34 (void)static_cast<void (*)()>(t_529_2);
35
36 (void)static_cast<void*>(0);
37 (void)static_cast<void*>((int*)0);
38 (void)static_cast<volatile const void*>((const int*)0);
39 (void)static_cast<A*>((B*)0);
40 // TryCopyInitialization doesn't handle references yet.
41 (void)static_cast<A&>(*((B*)0));
42 (void)static_cast<const B*>((C1*)0);
43 (void)static_cast<B&>(*((C1*)0));
44 (void)static_cast<A*>((D*)0);
45 (void)static_cast<const A&>(*((D*)0));
46
47 // TODO: User-defined conversions
48
49 // Bad code below
50
51 (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}}
52 //(void)static_cast<A*>((E*)0); // {{static_cast from 'struct E *' to 'struct A *' is not allowed}}
53 //(void)static_cast<A*>((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
54 (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
55 (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'struct B **' to 'struct A **' is not allowed}}
56 (void)static_cast<char&>(i); // expected-error {{static_cast from 'int' to 'char &' is not allowed}}
57}
58
59// Anything to void
60void t_529_4()
61{
62 static_cast<void>(1);
63 static_cast<void>(t_529_4);
64}
65
66// Static downcasts
67void t_529_5_8()
68{
69 (void)static_cast<B*>((A*)0);
70 (void)static_cast<B&>(*((A*)0));
71 (void)static_cast<const G1*>((A*)0);
72 (void)static_cast<const G1&>(*((A*)0));
73
74 // Bad code below
75
76 (void)static_cast<C1*>((A*)0); // expected-error {{static_cast from 'struct A *' to 'struct C1 *' is not allowed}}
77 (void)static_cast<C1&>(*((A*)0)); // expected-error {{static_cast from 'struct A' to 'struct C1 &' is not allowed}}
78 (void)static_cast<D*>((A*)0); // expected-error {{static_cast from 'struct A *' to 'struct D *' is not allowed}}
79 (void)static_cast<D&>(*((A*)0)); // expected-error {{static_cast from 'struct A' to 'struct D &' is not allowed}}
80 (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' is not allowed}}
81 (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' is not allowed}}
82 // Accessibility is not yet tested
83 //(void)static_cast<E*>((A*)0); // {{static_cast from 'struct A *' to 'struct E *' is not allowed}}
84 //(void)static_cast<E&>(*((A*)0)); // {{static_cast from 'struct A' to 'struct E &' is not allowed}}
85 (void)static_cast<H*>((A*)0); // expected-error {{static_cast from 'struct A *' to 'struct H *' is not allowed}}
86 (void)static_cast<H&>(*((A*)0)); // expected-error {{static_cast from 'struct A' to 'struct H &' is not allowed}}
87 (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'struct B *' to 'struct E *' is not allowed}}
88 (void)static_cast<E&>(*((B*)0)); // expected-error {{static_cast from 'struct B' to 'struct E &' is not allowed}}
89
90 // TODO: Test inaccessible base in context where it's accessible, i.e.
91 // member function and friend.
92
93 // TODO: Test DR427. This requires user-defined conversions, though.
94}
95
96// Enum conversions
97void t_529_7()
98{
99 (void)static_cast<Enum>(1);
100 (void)static_cast<Enum>(1.0);
101 (void)static_cast<Onom>(En1);
102
103 // Bad code below
104
105 (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}}
106}
107
108// Void pointer to object pointer
109void t_529_10()
110{
111 (void)static_cast<int*>((void*)0);
112 (void)static_cast<const A*>((void*)0);
113
114 // Bad code below
115
116 (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'void const *' to 'int *' is not allowed}}
117 (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)(void)' is not allowed}}
118}
119
120// TODO: Test member pointers.