blob: 464ea79aed9757963e156085cb346f7bd3493d1f [file] [log] [blame]
Sebastian Redl07779722008-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);
Sebastian Redl07779722008-10-31 14:43:28 +000040 (void)static_cast<A&>(*((B*)0));
41 (void)static_cast<const B*>((C1*)0);
42 (void)static_cast<B&>(*((C1*)0));
43 (void)static_cast<A*>((D*)0);
44 (void)static_cast<const A&>(*((D*)0));
45
46 // TODO: User-defined conversions
47
48 // Bad code below
49
50 (void)static_cast<void*>((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}}
51 //(void)static_cast<A*>((E*)0); // {{static_cast from 'struct E *' to 'struct A *' is not allowed}}
52 //(void)static_cast<A*>((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}}
53 (void)static_cast<int>((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}}
54 (void)static_cast<A**>((B**)0); // expected-error {{static_cast from 'struct B **' to 'struct A **' is not allowed}}
Sebastian Redle3dc28a2008-11-07 23:29:29 +000055 (void)static_cast<char&>(i); // expected-error {{non-const reference to type 'char' cannot be initialized with a value of type 'int'}}
Sebastian Redl07779722008-10-31 14:43:28 +000056}
57
58// Anything to void
59void t_529_4()
60{
61 static_cast<void>(1);
62 static_cast<void>(t_529_4);
63}
64
65// Static downcasts
66void t_529_5_8()
67{
68 (void)static_cast<B*>((A*)0);
69 (void)static_cast<B&>(*((A*)0));
70 (void)static_cast<const G1*>((A*)0);
71 (void)static_cast<const G1&>(*((A*)0));
72
73 // Bad code below
74
Sebastian Redle3dc28a2008-11-07 23:29:29 +000075 (void)static_cast<C1*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}}
76 (void)static_cast<C1&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}}
77 (void)static_cast<D*>((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}}
78 (void)static_cast<D&>(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}}
79 (void)static_cast<B*>((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' casts away constness}}
80 (void)static_cast<B&>(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' casts away constness}}
Sebastian Redl07779722008-10-31 14:43:28 +000081 // Accessibility is not yet tested
82 //(void)static_cast<E*>((A*)0); // {{static_cast from 'struct A *' to 'struct E *' is not allowed}}
83 //(void)static_cast<E&>(*((A*)0)); // {{static_cast from 'struct A' to 'struct E &' is not allowed}}
Sebastian Redle3dc28a2008-11-07 23:29:29 +000084 (void)static_cast<H*>((A*)0); // expected-error {{ambiguous static_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}}
85 (void)static_cast<H&>(*((A*)0)); // expected-error {{ambiguous static_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 +000086 (void)static_cast<E*>((B*)0); // expected-error {{static_cast from 'struct B *' to 'struct E *' is not allowed}}
Sebastian Redle3dc28a2008-11-07 23:29:29 +000087 (void)static_cast<E&>(*((B*)0)); // expected-error {{non-const reference to type 'struct E' cannot be initialized with a value of type 'struct B'}}
Sebastian Redl07779722008-10-31 14:43:28 +000088
89 // TODO: Test inaccessible base in context where it's accessible, i.e.
90 // member function and friend.
91
92 // TODO: Test DR427. This requires user-defined conversions, though.
93}
94
95// Enum conversions
96void t_529_7()
97{
98 (void)static_cast<Enum>(1);
99 (void)static_cast<Enum>(1.0);
100 (void)static_cast<Onom>(En1);
101
102 // Bad code below
103
104 (void)static_cast<Enum>((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}}
105}
106
107// Void pointer to object pointer
108void t_529_10()
109{
110 (void)static_cast<int*>((void*)0);
111 (void)static_cast<const A*>((void*)0);
112
113 // Bad code below
114
Sebastian Redl4e849352008-11-05 17:54:26 +0000115 (void)static_cast<int*>((const void*)0); // expected-error {{static_cast from 'void const *' to 'int *' casts away constness}}
Sebastian Redl07779722008-10-31 14:43:28 +0000116 (void)static_cast<void (*)()>((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)(void)' is not allowed}}
117}
118
119// TODO: Test member pointers.