blob: b605194ecd6491b7d046024418f645de5f66beb7 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002
3struct A {};
4struct B : A {};
5struct C : B {};
6
7struct D : private A {};
8struct E : A {};
9struct F : B, E {};
10
John McCall85f90552010-03-10 11:27:22 +000011struct Incomplete; // expected-note 2 {{forward declaration of 'Incomplete'}}
Sebastian Redl72b8aef2008-10-31 14:43:28 +000012
Sebastian Redlb426f6332008-11-06 15:59:35 +000013struct Poly
14{
15 virtual void f();
16};
17
18struct PolyDerived : Poly
19{
20};
21
Sebastian Redl72b8aef2008-10-31 14:43:28 +000022void basic_bad()
23{
24 // ptr -> nonptr
John McCall85f90552010-03-10 11:27:22 +000025 (void)dynamic_cast<A>((A*)0); // expected-error {{'A' is not a reference or pointer}}
Sebastian Redl72b8aef2008-10-31 14:43:28 +000026 // 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
John McCall85f90552010-03-10 11:27:22 +000037 (void)dynamic_cast<Incomplete*>((A*)0); // expected-error {{'Incomplete' is an incomplete type}}
Sebastian Redl72b8aef2008-10-31 14:43:28 +000038 // incomplete -> ptr
John McCall85f90552010-03-10 11:27:22 +000039 (void)dynamic_cast<A*>((Incomplete*)0); // expected-error {{'Incomplete' is an incomplete type}}
Eli Friedman97b5f2f2013-06-20 18:53:38 +000040 // rvalue -> lvalue
41 (void)dynamic_cast<A&>(A()); // expected-error {{dynamic_cast from rvalue to reference type 'A &'}}
Sebastian Redl72b8aef2008-10-31 14:43:28 +000042}
43
44void same()
45{
46 (void)dynamic_cast<A*>((A*)0);
47 (void)dynamic_cast<A&>(*((A*)0));
48}
49
50void 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
John McCall85f90552010-03-10 11:27:22 +000062 (void)dynamic_cast<A*>((F*)0); // expected-error {{ambiguous conversion from derived class 'F' to base class 'A':\n struct F -> struct B -> struct A\n struct F -> struct E -> struct A}}
63 (void)dynamic_cast<A&>(*((F*)0)); // expected-error {{ambiguous conversion from derived class 'F' to base class 'A':\n struct F -> struct B -> struct A\n struct F -> struct E -> struct A}}
Sebastian Redl72b8aef2008-10-31 14:43:28 +000064}
65
Sebastian Redlb426f6332008-11-06 15:59:35 +000066void 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
John McCall85f90552010-03-10 11:27:22 +000074 (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}}
Sebastian Redlb426f6332008-11-06 15:59:35 +000076}