aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.old-deja/g++.abi/vtable3.h
blob: 327d346db2767047a4e9991e9dd7a9af5560065c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (C) 2000 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 4 February 2001 <nathan@codesourcery.com>

// Check constructor vtables work. This is included from numerous test
// files, which set the #defines necessary to specify the heirarchy.

#include <typeinfo>
#include <stdio.h>

// XXX. vcall offsets are still broken, remove this define to re-enable
// testing when fixed.
#define NO_VCALL_TEST

int fail;

template <typename BASE, typename DERIVED>
int Test (DERIVED *d, int expect)
{
  BASE *b = static_cast <BASE *> (d);
  void *full_b = dynamic_cast <void *> (b);
  void *full_d = dynamic_cast <void *> (d);
  
  if (full_b != full_d)
    {
      fail++;
      fprintf (stderr, "base %s and derived %s have different full objects\n",
              typeid (BASE).name (), typeid (DERIVED).name ());
      return 1;
    }

  DERIVED *dynamic_d = dynamic_cast <DERIVED *> (b);
  
  if (dynamic_d != d)
    {
      fail++;
      fprintf (stderr, "dynamic_cast from %s to %s failed\n",
              typeid (BASE).name (), typeid (DERIVED).name ());
      return 1;
    }
#ifndef NO_VCALL_TEST
  b->Baz (static_cast <void *> (b));
  
  int res = b->Foo (static_cast <void *> (d));
  
  if (res != expect)
    {
      fail++;
      fprintf (stderr, "%s::Foo returned %d, expected %d\n",
              typeid (BASE).name (), res, expect);
      return 1;
    }
#endif
  return 0;
}

template <typename T>
int Test (T *self, void *expected, int result)
{
  if (self != expected)
    {
      fail++;
      fprintf (stderr, "%s::Foo wrong this pointer\n", typeid (T).name ());
    }
  return result;
}

struct A {
#ifndef A_EMPTY
  int a_m;
#endif
  virtual int Foo (void *p) {return Test (this, p, 1);}
  virtual int Baz (void *p) {return Test (this, p, 1);}
  A ();
  ~A ();
};

struct B1: virtual A {
#ifndef B1_EMPTY
  int b1_m;
#endif
  virtual int Foo (void *p) {return Test (this, p, 2);}
  B1();
  ~B1();
};

struct B2: virtual A {
#ifndef B2_EMPTY
  int b2_m;
#endif
  virtual int Foo (void *p) {return Test (this, p, 3);}
  B2();
  ~B2();
};

struct Empty {};

struct C : C_PARENTS {
#ifndef C_EMPTY
  int c_m;
#endif
  virtual int Foo (void *p) {return Test (this, p, 4);}
  C();
  ~C();
};

A::A ()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 1);
}
A::~A ()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 1);
}

B1::B1()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 2);
  Test <B1> (this, 2);
}
B1::~B1()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 2);
  Test <B1> (this, 2);
}
B2::B2()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 3);
  Test <B2> (this, 3);
}
B2::~B2()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 3);
  Test <B2> (this, 3);
}
C::C()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 4);
  Test <C> (this, 4);
}
C::~C()
{
  fprintf (stderr, "%s\n", __PRETTY_FUNCTION__);
  Test <A> (this, 4);
  Test <C> (this, 4);
}

struct D : C {};
struct D1 : virtual C {};
struct D2 : virtual A, virtual C {};

int main()
{
  {
    fprintf (stderr, "C\n");
    C c;
  }
  {
    fprintf (stderr, "D\n");
    D d;
  }
  {
    fprintf (stderr, "D1\n");
    D1 d1;
  }
  {
    fprintf (stderr, "D2\n");
    D2 d2;
  }
  if (fail)
    fprintf (stderr, "There are %d failings\n", fail);
  else
    fprintf (stderr, "Passed\n");
  return fail ? 1 : 0;
}