| 1 |
// { dg-do run } |
| 2 |
// Author: Alfred Miniarik <[email protected]> |
| 3 |
// test of dynamic_cast |
| 4 |
// runtime detecting of nonpublic |
| 5 |
// inheritance within a cast |
| 6 |
// and therefor failing with result 0. |
| 7 |
|
| 8 |
extern "C" void abort(); |
| 9 |
extern "C" int printf (const char *, ...); |
| 10 |
|
| 11 |
static int errors = 0; |
| 12 |
void error(int i) |
| 13 |
{ |
| 14 |
printf("Error %i\n",i); |
| 15 |
errors++; |
| 16 |
} |
| 17 |
|
| 18 |
// 1. downcast |
| 19 |
|
| 20 |
// 1.1. single inheritance case |
| 21 |
|
| 22 |
struct A {virtual ~A(){}}; |
| 23 |
struct AA : A {}; |
| 24 |
struct B : A {}; |
| 25 |
struct BB : B {}; |
| 26 |
class C : B {}; |
| 27 |
struct D : C {}; |
| 28 |
|
| 29 |
struct CC : B {}; |
| 30 |
class DD : CC {}; |
| 31 |
|
| 32 |
class CCC : protected B {}; |
| 33 |
class DDD : protected CCC {}; |
| 34 |
|
| 35 |
void |
| 36 |
test01 () |
| 37 |
{ |
| 38 |
D d; |
| 39 |
if(dynamic_cast<D*> ((A*)&d)) error(1); |
| 40 |
if(dynamic_cast<D*> ((B*)&d)) error(2); |
| 41 |
if(&d != dynamic_cast<D*> ((C*)&d)) error(3); //counter example |
| 42 |
if(dynamic_cast<C*> ((B*)&d)) error(4); |
| 43 |
|
| 44 |
DD dd; |
| 45 |
if(dynamic_cast<DD*> ((A*)&dd)) error(5); |
| 46 |
if(dynamic_cast<DD*> ((B*)&dd)) error(6); |
| 47 |
|
| 48 |
DDD ddd; |
| 49 |
if(dynamic_cast<DDD*> ((A*)&ddd)) error(7); |
| 50 |
if(dynamic_cast<DDD*> ((B*)&ddd)) error(8); |
| 51 |
if(dynamic_cast<CCC*> ((B*)&ddd)) error(9); |
| 52 |
} |
| 53 |
|
| 54 |
// 1.2. multiple inheritance case |
| 55 |
// 1.2.1. all bases are public |
| 56 |
|
| 57 |
struct E : D, CC {}; |
| 58 |
struct EE : CC, D {}; //Will search in reverse order. |
| 59 |
|
| 60 |
void |
| 61 |
test02 () |
| 62 |
{ |
| 63 |
E e; |
| 64 |
if(dynamic_cast<E*> ((A*)(D*)&e)) error(10); |
| 65 |
if(dynamic_cast<E*> ((B*)(D*)&e)) error(11); |
| 66 |
if(&e != dynamic_cast<E*> ((C*)(D*)&e)) error(12); //counter example |
| 67 |
if(&e != dynamic_cast<E*> ((B*)(CC*)&e)) error(13); //counter example |
| 68 |
if((CC*)&e != dynamic_cast<CC*> ((B*)(CC*)&e)) error(14); //counter example |
| 69 |
|
| 70 |
EE ee; |
| 71 |
if(dynamic_cast<EE*> ((A*)(D*)&ee)) error(15); |
| 72 |
if(dynamic_cast<EE*> ((B*)(D*)&ee)) error(16); |
| 73 |
if(&ee != dynamic_cast<EE*> ((C*)(D*)&ee)) error(17); //counter example |
| 74 |
if(&ee != dynamic_cast<EE*> ((B*)(CC*)&ee)) error(18); //counter example |
| 75 |
if((CC*)&ee != dynamic_cast<CC*> ((B*)(CC*)&ee)) error(19); //counter example |
| 76 |
} |
| 77 |
|
| 78 |
// 1.2.2 one or more branches are nonpublic |
| 79 |
|
| 80 |
struct X : private BB, E {}; |
| 81 |
struct Y : AA, private B {}; |
| 82 |
|
| 83 |
class XX : BB, E {}; |
| 84 |
|
| 85 |
void |
| 86 |
test03 () |
| 87 |
{ |
| 88 |
X x; |
| 89 |
if(&x != dynamic_cast<X*>((B*)(CC*)(E*)&x)) error(20); //counter example |
| 90 |
XX xx; |
| 91 |
if(dynamic_cast<XX*>((B*)(CC*)(E*)&xx)) error(21); |
| 92 |
Y y; |
| 93 |
if(dynamic_cast<Y*>((B*)&y)) error (22); |
| 94 |
if(dynamic_cast<Y*>((A*)(B*)&y)) error (23); |
| 95 |
} |
| 96 |
|
| 97 |
// 2. crosscast |
| 98 |
|
| 99 |
struct J {virtual ~J(){}}; |
| 100 |
struct K : CC, private J {}; |
| 101 |
class KK : J, CC{}; |
| 102 |
|
| 103 |
void |
| 104 |
test04 () |
| 105 |
{ |
| 106 |
E e; |
| 107 |
if(dynamic_cast<CC*> ((B*)(D*)&e)) error(24); |
| 108 |
if((CC*)&e != dynamic_cast<CC*> ((C*)(D*)&e)) error(25); //counter example |
| 109 |
K k; |
| 110 |
if(dynamic_cast<J*> ((B*)&k)) error(26); |
| 111 |
KK kk; |
| 112 |
if(dynamic_cast<J*> ((CC*)&kk)) error(27); |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
main () |
| 117 |
{ |
| 118 |
test01(); |
| 119 |
test02(); |
| 120 |
test03(); |
| 121 |
test04(); |
| 122 |
return errors ? 1 : 0; |
| 123 |
} |