| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 |
|
| 3 |
struct A {}; |
| 4 |
struct B : A {}; |
| 5 |
struct C : B {}; |
| 6 |
|
| 7 |
struct D : private A {}; |
| 8 |
struct E : A {}; |
| 9 |
struct F : B, E {}; |
| 10 |
|
| 11 |
struct Incomplete; // expected-note 2 {{forward declaration of 'Incomplete'}} |
| 12 |
|
| 13 |
struct Poly |
| 14 |
{ |
| 15 |
virtual void f(); |
| 16 |
}; |
| 17 |
|
| 18 |
struct PolyDerived : Poly |
| 19 |
{ |
| 20 |
}; |
| 21 |
|
| 22 |
void basic_bad() |
| 23 |
{ |
| 24 |
// ptr -> nonptr |
| 25 |
(void)dynamic_cast<A>((A*)0); // expected-error {{invalid target type 'A' for dynamic_cast; target type must be a reference or pointer type to a defined class}} |
| 26 |
// nonptr -> ptr |
| 27 |
(void)dynamic_cast<A*>(0); // expected-error {{cannot use dynamic_cast to convert from 'int' to 'A *'}} |
| 28 |
// ptr -> noncls |
| 29 |
(void)dynamic_cast<int*>((A*)0); // expected-error {{'int' is not a class type}} |
| 30 |
// noncls -> ptr |
| 31 |
(void)dynamic_cast<A*>((int*)0); // expected-error {{'int' is not a class type}} |
| 32 |
// ref -> noncls |
| 33 |
(void)dynamic_cast<int&>(*((A*)0)); // expected-error {{'int' is not a class type}} |
| 34 |
// noncls -> ref |
| 35 |
(void)dynamic_cast<A&>(*((int*)0)); // expected-error {{'int' is not a class type}} |
| 36 |
// ptr -> incomplete |
| 37 |
(void)dynamic_cast<Incomplete*>((A*)0); // expected-error {{'Incomplete' is an incomplete type}} |
| 38 |
// incomplete -> ptr |
| 39 |
(void)dynamic_cast<A*>((Incomplete*)0); // expected-error {{'Incomplete' is an incomplete type}} |
| 40 |
// rvalue -> lvalue |
| 41 |
(void)dynamic_cast<A&>(A()); // expected-error {{dynamic_cast from rvalue to reference type 'A &'}} |
| 42 |
} |
| 43 |
|
| 44 |
void same() |
| 45 |
{ |
| 46 |
(void)dynamic_cast<A*>((A*)0); |
| 47 |
(void)dynamic_cast<A&>(*((A*)0)); |
| 48 |
} |
| 49 |
|
| 50 |
void up() |
| 51 |
{ |
| 52 |
(void)dynamic_cast<A*>((B*)0); |
| 53 |
(void)dynamic_cast<A&>(*((B*)0)); |
| 54 |
(void)dynamic_cast<A*>((C*)0); |
| 55 |
(void)dynamic_cast<A&>(*((C*)0)); |
| 56 |
|
| 57 |
// Inaccessible |
| 58 |
//(void)dynamic_cast<A*>((D*)0); |
| 59 |
//(void)dynamic_cast<A&>(*((D*)0)); |
| 60 |
|
| 61 |
// Ambiguous |
| 62 |
(void)dynamic_cast<A*>((F*)0); // expected-error {{ambiguous conversion from derived class 'F' to base class 'A':\n struct F -> B -> A\n struct F -> E -> A}} |
| 63 |
(void)dynamic_cast<A&>(*((F*)0)); // expected-error {{ambiguous conversion from derived class 'F' to base class 'A':\n struct F -> B -> A\n struct F -> E -> A}} |
| 64 |
} |
| 65 |
|
| 66 |
void poly() |
| 67 |
{ |
| 68 |
(void)dynamic_cast<A*>((Poly*)0); |
| 69 |
(void)dynamic_cast<A&>(*((Poly*)0)); |
| 70 |
(void)dynamic_cast<A*>((PolyDerived*)0); |
| 71 |
(void)dynamic_cast<A&>(*((PolyDerived*)0)); |
| 72 |
|
| 73 |
// Not polymorphic source |
| 74 |
(void)dynamic_cast<Poly*>((A*)0); // expected-error {{'A' is not polymorphic}} |
| 75 |
(void)dynamic_cast<PolyDerived&>(*((A*)0)); // expected-error {{'A' is not polymorphic}} |
| 76 |
} |