xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/dynamic-cast.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct A {};
4*f4a2713aSLionel Sambuc struct B : A {};
5*f4a2713aSLionel Sambuc struct C : B {};
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc struct D : private A {};
8*f4a2713aSLionel Sambuc struct E : A {};
9*f4a2713aSLionel Sambuc struct F : B, E {};
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc struct Incomplete; // expected-note 2 {{forward declaration of 'Incomplete'}}
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc struct Poly
14*f4a2713aSLionel Sambuc {
15*f4a2713aSLionel Sambuc   virtual void f();
16*f4a2713aSLionel Sambuc };
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc struct PolyDerived : Poly
19*f4a2713aSLionel Sambuc {
20*f4a2713aSLionel Sambuc };
21*f4a2713aSLionel Sambuc 
basic_bad()22*f4a2713aSLionel Sambuc void basic_bad()
23*f4a2713aSLionel Sambuc {
24*f4a2713aSLionel Sambuc   // ptr -> nonptr
25*f4a2713aSLionel Sambuc   (void)dynamic_cast<A>((A*)0); // expected-error {{'A' is not a reference or pointer}}
26*f4a2713aSLionel Sambuc   // nonptr -> ptr
27*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>(0); // expected-error {{'int' is not a pointer}}
28*f4a2713aSLionel Sambuc   // ptr -> noncls
29*f4a2713aSLionel Sambuc   (void)dynamic_cast<int*>((A*)0); // expected-error {{'int' is not a class}}
30*f4a2713aSLionel Sambuc   // noncls -> ptr
31*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((int*)0); // expected-error {{'int' is not a class}}
32*f4a2713aSLionel Sambuc   // ref -> noncls
33*f4a2713aSLionel Sambuc   (void)dynamic_cast<int&>(*((A*)0)); // expected-error {{'int' is not a class}}
34*f4a2713aSLionel Sambuc   // noncls -> ref
35*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((int*)0)); // expected-error {{'int' is not a class}}
36*f4a2713aSLionel Sambuc   // ptr -> incomplete
37*f4a2713aSLionel Sambuc   (void)dynamic_cast<Incomplete*>((A*)0); // expected-error {{'Incomplete' is an incomplete type}}
38*f4a2713aSLionel Sambuc   // incomplete -> ptr
39*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((Incomplete*)0); // expected-error {{'Incomplete' is an incomplete type}}
40*f4a2713aSLionel Sambuc   // rvalue -> lvalue
41*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(A()); // expected-error {{dynamic_cast from rvalue to reference type 'A &'}}
42*f4a2713aSLionel Sambuc }
43*f4a2713aSLionel Sambuc 
same()44*f4a2713aSLionel Sambuc void same()
45*f4a2713aSLionel Sambuc {
46*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((A*)0);
47*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((A*)0));
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
up()50*f4a2713aSLionel Sambuc void up()
51*f4a2713aSLionel Sambuc {
52*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((B*)0);
53*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((B*)0));
54*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((C*)0);
55*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((C*)0));
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   // Inaccessible
58*f4a2713aSLionel Sambuc   //(void)dynamic_cast<A*>((D*)0);
59*f4a2713aSLionel Sambuc   //(void)dynamic_cast<A&>(*((D*)0));
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc   // Ambiguous
62*f4a2713aSLionel Sambuc   (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*f4a2713aSLionel Sambuc   (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}}
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc 
poly()66*f4a2713aSLionel Sambuc void poly()
67*f4a2713aSLionel Sambuc {
68*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((Poly*)0);
69*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((Poly*)0));
70*f4a2713aSLionel Sambuc   (void)dynamic_cast<A*>((PolyDerived*)0);
71*f4a2713aSLionel Sambuc   (void)dynamic_cast<A&>(*((PolyDerived*)0));
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc   // Not polymorphic source
74*f4a2713aSLionel Sambuc   (void)dynamic_cast<Poly*>((A*)0); // expected-error {{'A' is not polymorphic}}
75*f4a2713aSLionel Sambuc   (void)dynamic_cast<PolyDerived&>(*((A*)0)); // expected-error {{'A' is not polymorphic}}
76*f4a2713aSLionel Sambuc }
77