Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1 | // RUN: clang -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 | |
Sebastian Redl | 3cb0692 | 2009-02-07 19:52:04 +0000 | [diff] [blame^] | 11 | struct Incomplete; // expected-note 2 {{forward declaration of 'struct Incomplete'}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 12 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 13 | struct Poly |
| 14 | { |
| 15 | virtual void f(); |
| 16 | }; |
| 17 | |
| 18 | struct PolyDerived : Poly |
| 19 | { |
| 20 | }; |
| 21 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 22 | void basic_bad() |
| 23 | { |
| 24 | // ptr -> nonptr |
| 25 | (void)dynamic_cast<A>((A*)0); // expected-error {{'struct A' is not a reference or pointer}} |
| 26 | // nonptr -> ptr |
| 27 | (void)dynamic_cast<A*>(0); // expected-error {{'int' is not a pointer}} |
| 28 | // ptr -> noncls |
| 29 | (void)dynamic_cast<int*>((A*)0); // expected-error {{'int' is not a class}} |
| 30 | // noncls -> ptr |
| 31 | (void)dynamic_cast<A*>((int*)0); // expected-error {{'int' is not a class}} |
| 32 | // ref -> noncls |
| 33 | (void)dynamic_cast<int&>(*((A*)0)); // expected-error {{'int' is not a class}} |
| 34 | // noncls -> ref |
| 35 | (void)dynamic_cast<A&>(*((int*)0)); // expected-error {{'int' is not a class}} |
| 36 | // ptr -> incomplete |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 37 | (void)dynamic_cast<Incomplete*>((A*)0); // expected-error {{'struct Incomplete' is an incomplete type}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 38 | // incomplete -> ptr |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 39 | (void)dynamic_cast<A*>((Incomplete*)0); // expected-error {{'struct Incomplete' is an incomplete type}} |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void same() |
| 43 | { |
| 44 | (void)dynamic_cast<A*>((A*)0); |
| 45 | (void)dynamic_cast<A&>(*((A*)0)); |
| 46 | } |
| 47 | |
| 48 | void up() |
| 49 | { |
| 50 | (void)dynamic_cast<A*>((B*)0); |
| 51 | (void)dynamic_cast<A&>(*((B*)0)); |
| 52 | (void)dynamic_cast<A*>((C*)0); |
| 53 | (void)dynamic_cast<A&>(*((C*)0)); |
| 54 | |
| 55 | // Inaccessible |
| 56 | //(void)dynamic_cast<A*>((D*)0); |
| 57 | //(void)dynamic_cast<A&>(*((D*)0)); |
| 58 | |
| 59 | // Ambiguous |
| 60 | (void)dynamic_cast<A*>((F*)0); // expected-error {{ambiguous conversion from derived class 'struct F' to base class 'struct A':\n struct F -> struct B -> struct A\n struct F -> struct E -> struct A}} |
| 61 | (void)dynamic_cast<A&>(*((F*)0)); // expected-error {{ambiguous conversion from derived class 'struct F' to base class 'struct A':\n struct F -> struct B -> struct A\n struct F -> struct E -> struct A}} |
| 62 | } |
| 63 | |
Sebastian Redl | d93f0dd | 2008-11-06 15:59:35 +0000 | [diff] [blame] | 64 | void poly() |
| 65 | { |
| 66 | (void)dynamic_cast<A*>((Poly*)0); |
| 67 | (void)dynamic_cast<A&>(*((Poly*)0)); |
| 68 | (void)dynamic_cast<A*>((PolyDerived*)0); |
| 69 | (void)dynamic_cast<A&>(*((PolyDerived*)0)); |
| 70 | |
| 71 | // Not polymorphic source |
| 72 | (void)dynamic_cast<Poly*>((A*)0); // expected-error {{'struct A' is not polymorphic}} |
| 73 | (void)dynamic_cast<PolyDerived&>(*((A*)0)); // expected-error {{'struct A' is not polymorphic}} |
| 74 | } |