aboutsummaryrefslogtreecommitdiff
path: root/test/CodeGenCXX/vtable-layout-abi-examples.cpp
blob: 8f084a9c5b7bd13c2597846149b73011ef6bd610 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm-only -fdump-vtable-layouts > %t 2>&1
// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
// RUN: FileCheck --check-prefix=CHECK-11 %s < %t

/// Examples from the Itanium C++ ABI specification.
/// http://www.codesourcery.com/public/cxx-abi/

namespace Test1 {
  
// This is from http://www.codesourcery.com/public/cxx-abi/cxx-vtable-ex.html

// CHECK-1:      Vtable for 'Test1::A' (5 entries).
// CHECK-1-NEXT:    0 | offset_to_top (0)
// CHECK-1-NEXT:    1 | Test1::A RTTI
// CHECK-1-NEXT:        -- (Test1::A, 0) vtable address --
// CHECK-1-NEXT:    2 | void Test1::A::f()
// CHECK-1-NEXT:    3 | void Test1::A::g()
// CHECK-1-NEXT:    4 | void Test1::A::h()
struct A {
  virtual void f ();
  virtual void g ();
  virtual void h ();
  int ia;
};
void A::f() {}

// CHECK-2:      Vtable for 'Test1::B' (13 entries).
// CHECK-2-NEXT:    0 | vbase_offset (16)
// CHECK-2-NEXT:    1 | offset_to_top (0)
// CHECK-2-NEXT:    2 | Test1::B RTTI
// CHECK-2-NEXT:        -- (Test1::B, 0) vtable address --
// CHECK-2-NEXT:    3 | void Test1::B::f()
// CHECK-2-NEXT:    4 | void Test1::B::h()
// CHECK-2-NEXT:    5 | vcall_offset (-16)
// CHECK-2-NEXT:    6 | vcall_offset (0)
// CHECK-2-NEXT:    7 | vcall_offset (-16)
// CHECK-2-NEXT:    8 | offset_to_top (-16)
// CHECK-2-NEXT:    9 | Test1::B RTTI
// CHECK-2-NEXT:        -- (Test1::A, 16) vtable address --
// CHECK-2-NEXT:   10 | void Test1::B::f()
// CHECK-2-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-2-NEXT:   11 | void Test1::A::g()
// CHECK-2-NEXT:   12 | void Test1::B::h()
// CHECK-2-NEXT:        [this adjustment: 0 non-virtual, -40 vcall offset offset]
struct B: public virtual A {
  void f ();
  void h ();
  int ib;
};
void B::f() {}

// CHECK-3:      Vtable for 'Test1::C' (13 entries).
// CHECK-3-NEXT:    0 | vbase_offset (16)
// CHECK-3-NEXT:    1 | offset_to_top (0)
// CHECK-3-NEXT:    2 | Test1::C RTTI
// CHECK-3-NEXT:        -- (Test1::C, 0) vtable address --
// CHECK-3-NEXT:    3 | void Test1::C::g()
// CHECK-3-NEXT:    4 | void Test1::C::h()
// CHECK-3-NEXT:    5 | vcall_offset (-16)
// CHECK-3-NEXT:    6 | vcall_offset (-16)
// CHECK-3-NEXT:    7 | vcall_offset (0)
// CHECK-3-NEXT:    8 | offset_to_top (-16)
// CHECK-3-NEXT:    9 | Test1::C RTTI
// CHECK-3-NEXT:        -- (Test1::A, 16) vtable address --
// CHECK-3-NEXT:   10 | void Test1::A::f()
// CHECK-3-NEXT:   11 | void Test1::C::g()
// CHECK-3-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]
// CHECK-3-NEXT:   12 | void Test1::C::h()
// CHECK-3-NEXT:        [this adjustment: 0 non-virtual, -40 vcall offset offset]
struct C: public virtual A {
  void g ();
  void h ();
  int ic;
};
void C::g() {}

// CHECK-4:      Vtable for 'Test1::D' (18 entries).
// CHECK-4-NEXT:    0 | vbase_offset (32)
// CHECK-4-NEXT:    1 | offset_to_top (0)
// CHECK-4-NEXT:    2 | Test1::D RTTI
// CHECK-4-NEXT:        -- (Test1::B, 0) vtable address --
// CHECK-4-NEXT:        -- (Test1::D, 0) vtable address --
// CHECK-4-NEXT:    3 | void Test1::B::f()
// CHECK-4-NEXT:    4 | void Test1::D::h()
// CHECK-4-NEXT:    5 | vbase_offset (16)
// CHECK-4-NEXT:    6 | offset_to_top (-16)
// CHECK-4-NEXT:    7 | Test1::D RTTI
// CHECK-4-NEXT:        -- (Test1::C, 16) vtable address --
// CHECK-4-NEXT:    8 | void Test1::C::g()
// CHECK-4-NEXT:    9 | void Test1::D::h()
// CHECK-4-NEXT:        [this adjustment: -16 non-virtual]
// CHECK-4-NEXT:   10 | vcall_offset (-32)
// CHECK-4-NEXT:   11 | vcall_offset (-16)
// CHECK-4-NEXT:   12 | vcall_offset (-32)
// CHECK-4-NEXT:   13 | offset_to_top (-32)
// CHECK-4-NEXT:   14 | Test1::D RTTI
// CHECK-4-NEXT:        -- (Test1::A, 32) vtable address --
// CHECK-4-NEXT:   15 | void Test1::B::f()
// CHECK-4-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-4-NEXT:   16 | void Test1::C::g()
// CHECK-4-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]
// CHECK-4-NEXT:   17 | void Test1::D::h()
// CHECK-4-NEXT:        [this adjustment: 0 non-virtual, -40 vcall offset offset]
struct D: public B, public C {
  void h ();
  int id;
};
void D::h() { }

struct X {
  int ix;
  virtual void x();
};

// CHECK-5:      Vtable for 'Test1::E' (24 entries).
// CHECK-5-NEXT:    0 | vbase_offset (56)
// CHECK-5-NEXT:    1 | offset_to_top (0)
// CHECK-5-NEXT:    2 | Test1::E RTTI
// CHECK-5-NEXT:        -- (Test1::E, 0) vtable address --
// CHECK-5-NEXT:        -- (Test1::X, 0) vtable address --
// CHECK-5-NEXT:    3 | void Test1::X::x()
// CHECK-5-NEXT:    4 | void Test1::E::f()
// CHECK-5-NEXT:    5 | void Test1::E::h()
// CHECK-5-NEXT:    6 | vbase_offset (40)
// CHECK-5-NEXT:    7 | offset_to_top (-16)
// CHECK-5-NEXT:    8 | Test1::E RTTI
// CHECK-5-NEXT:        -- (Test1::B, 16) vtable address --
// CHECK-5-NEXT:        -- (Test1::D, 16) vtable address --
// CHECK-5-NEXT:    9 | void Test1::E::f()
// CHECK-5-NEXT:        [this adjustment: -16 non-virtual]
// CHECK-5-NEXT:   10 | void Test1::E::h()
// CHECK-5-NEXT:        [this adjustment: -16 non-virtual]
// CHECK-5-NEXT:   11 | vbase_offset (24)
// CHECK-5-NEXT:   12 | offset_to_top (-32)
// CHECK-5-NEXT:   13 | Test1::E RTTI
// CHECK-5-NEXT:        -- (Test1::C, 32) vtable address --
// CHECK-5-NEXT:   14 | void Test1::C::g()
// CHECK-5-NEXT:   15 | void Test1::E::h()
// CHECK-5-NEXT:        [this adjustment: -32 non-virtual]
// CHECK-5-NEXT:   16 | vcall_offset (-56)
// CHECK-5-NEXT:   17 | vcall_offset (-24)
// CHECK-5-NEXT:   18 | vcall_offset (-56)
// CHECK-5-NEXT:   19 | offset_to_top (-56)
// CHECK-5-NEXT:   20 | Test1::E RTTI
// CHECK-5-NEXT:        -- (Test1::A, 56) vtable address --
// CHECK-5-NEXT:   21 | void Test1::E::f()
// CHECK-5-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-5-NEXT:   22 | void Test1::C::g()
// CHECK-5-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]
// CHECK-5-NEXT:   23 | void Test1::E::h()
// CHECK-5-NEXT:        [this adjustment: 0 non-virtual, -40 vcall offset offset]
struct E : X, D {
  int ie;
  void f();
  void h ();
};
void E::f() { } 

}

namespace Test2 {

// From http://www.codesourcery.com/public/cxx-abi/abi.html#class-types.

struct A { virtual void f(); };
struct B : virtual public A { int i; };
struct C : virtual public A { int j; };

// CHECK-6:      Vtable for 'Test2::D' (11 entries).
// CHECK-6-NEXT:    0 | vbase_offset (0)
// CHECK-6-NEXT:    1 | vcall_offset (0)
// CHECK-6-NEXT:    2 | offset_to_top (0)
// CHECK-6-NEXT:    3 | Test2::D RTTI
// CHECK-6-NEXT:        -- (Test2::A, 0) vtable address --
// CHECK-6-NEXT:        -- (Test2::B, 0) vtable address --
// CHECK-6-NEXT:        -- (Test2::D, 0) vtable address --
// CHECK-6-NEXT:    4 | void Test2::A::f()
// CHECK-6-NEXT:    5 | void Test2::D::d()
// CHECK-6-NEXT:    6 | vbase_offset (-16)
// CHECK-6-NEXT:    7 | vcall_offset (-16)
// CHECK-6-NEXT:    8 | offset_to_top (-16)
// CHECK-6-NEXT:    9 | Test2::D RTTI
// CHECK-6-NEXT:        -- (Test2::C, 16) vtable address --
// CHECK-6-NEXT:   10 | [unused] void Test2::A::f()
struct D : public B, public C {
  virtual void d();
};
void D::d() { } 

}

namespace Test3 {

// From http://www.codesourcery.com/public/cxx-abi/abi-examples.html#vtable-ctor

struct V1 {
  int v1;
  virtual void f();
};

struct V2 : virtual V1 {
  int v2;
  virtual void f();
};

// CHECK-7:      Vtable for 'Test3::C' (14 entries).
// CHECK-7-NEXT:    0 | vbase_offset (32)
// CHECK-7-NEXT:    1 | vbase_offset (16)
// CHECK-7-NEXT:    2 | offset_to_top (0)
// CHECK-7-NEXT:    3 | Test3::C RTTI
// CHECK-7-NEXT:        -- (Test3::C, 0) vtable address --
// CHECK-7-NEXT:    4 | void Test3::C::f()
// CHECK-7-NEXT:    5 | vcall_offset (-16)
// CHECK-7-NEXT:    6 | offset_to_top (-16)
// CHECK-7-NEXT:    7 | Test3::C RTTI
// CHECK-7-NEXT:        -- (Test3::V1, 16) vtable address --
// CHECK-7-NEXT:    8 | void Test3::C::f()
// CHECK-7-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-7-NEXT:    9 | vcall_offset (-32)
// CHECK-7-NEXT:   10 | vbase_offset (-16)
// CHECK-7-NEXT:   11 | offset_to_top (-32)
// CHECK-7-NEXT:   12 | Test3::C RTTI
// CHECK-7-NEXT:        -- (Test3::V2, 32) vtable address --
// CHECK-7-NEXT:   13 | void Test3::C::f()
// CHECK-7-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]

// CHECK-8:      Construction vtable for ('Test3::V2', 32) in 'Test3::C' (9 entries).
// CHECK-8-NEXT:    0 | vcall_offset (0)
// CHECK-8-NEXT:    1 | vbase_offset (-16)
// CHECK-8-NEXT:    2 | offset_to_top (0)
// CHECK-8-NEXT:    3 | Test3::V2 RTTI
// CHECK-8-NEXT:        -- (Test3::V2, 32) vtable address --
// CHECK-8-NEXT:    4 | void Test3::V2::f()
// CHECK-8-NEXT:    5 | vcall_offset (16)
// CHECK-8-NEXT:    6 | offset_to_top (16)
// CHECK-8-NEXT:    7 | Test3::V2 RTTI
// CHECK-8-NEXT:        -- (Test3::V1, 16) vtable address --
// CHECK-8-NEXT:    8 | void Test3::V2::f()
// CHECK-8-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
struct C : virtual V1, virtual V2 {
  int c;
  virtual void f();
};
void C::f() { }

struct B {
  int b;
};

// CHECK-9:      Vtable for 'Test3::D' (15 entries).
// CHECK-9-NEXT:    0 | vbase_offset (40)
// CHECK-9-NEXT:    1 | vbase_offset (24)
// CHECK-9-NEXT:    2 | offset_to_top (0)
// CHECK-9-NEXT:    3 | Test3::D RTTI
// CHECK-9-NEXT:        -- (Test3::C, 0) vtable address --
// CHECK-9-NEXT:        -- (Test3::D, 0) vtable address --
// CHECK-9-NEXT:    4 | void Test3::C::f()
// CHECK-9-NEXT:    5 | void Test3::D::g()
// CHECK-9-NEXT:    6 | vcall_offset (-24)
// CHECK-9-NEXT:    7 | offset_to_top (-24)
// CHECK-9-NEXT:    8 | Test3::D RTTI
// CHECK-9-NEXT:        -- (Test3::V1, 24) vtable address --
// CHECK-9-NEXT:    9 | void Test3::C::f()
// CHECK-9-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-9-NEXT:   10 | vcall_offset (-40)
// CHECK-9-NEXT:   11 | vbase_offset (-16)
// CHECK-9-NEXT:   12 | offset_to_top (-40)
// CHECK-9-NEXT:   13 | Test3::D RTTI
// CHECK-9-NEXT:        -- (Test3::V2, 40) vtable address --
// CHECK-9-NEXT:   14 | void Test3::C::f()
// CHECK-9-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]

// CHECK-10:      Construction vtable for ('Test3::C', 0) in 'Test3::D' (14 entries).
// CHECK-10-NEXT:    0 | vbase_offset (40)
// CHECK-10-NEXT:    1 | vbase_offset (24)
// CHECK-10-NEXT:    2 | offset_to_top (0)
// CHECK-10-NEXT:    3 | Test3::C RTTI
// CHECK-10-NEXT:        -- (Test3::C, 0) vtable address --
// CHECK-10-NEXT:    4 | void Test3::C::f()
// CHECK-10-NEXT:    5 | vcall_offset (-24)
// CHECK-10-NEXT:    6 | offset_to_top (-24)
// CHECK-10-NEXT:    7 | Test3::C RTTI
// CHECK-10-NEXT:        -- (Test3::V1, 24) vtable address --
// CHECK-10-NEXT:    8 | void Test3::C::f()
// CHECK-10-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
// CHECK-10-NEXT:    9 | vcall_offset (-40)
// CHECK-10-NEXT:   10 | vbase_offset (-16)
// CHECK-10-NEXT:   11 | offset_to_top (-40)
// CHECK-10-NEXT:   12 | Test3::C RTTI
// CHECK-10-NEXT:        -- (Test3::V2, 40) vtable address --
// CHECK-10-NEXT:   13 | void Test3::C::f()
// CHECK-10-NEXT:        [this adjustment: 0 non-virtual, -32 vcall offset offset]

// CHECK-11:      Construction vtable for ('Test3::V2', 40) in 'Test3::D' (9 entries).
// CHECK-11-NEXT:    0 | vcall_offset (0)
// CHECK-11-NEXT:    1 | vbase_offset (-16)
// CHECK-11-NEXT:    2 | offset_to_top (0)
// CHECK-11-NEXT:    3 | Test3::V2 RTTI
// CHECK-11-NEXT:        -- (Test3::V2, 40) vtable address --
// CHECK-11-NEXT:    4 | void Test3::V2::f()
// CHECK-11-NEXT:    5 | vcall_offset (16)
// CHECK-11-NEXT:    6 | offset_to_top (16)
// CHECK-11-NEXT:    7 | Test3::V2 RTTI
// CHECK-11-NEXT:        -- (Test3::V1, 24) vtable address --
// CHECK-11-NEXT:    8 | void Test3::V2::f()
// CHECK-11-NEXT:        [this adjustment: 0 non-virtual, -24 vcall offset offset]
struct D : B, C {
  int d;
  virtual void g();
};
void D::g() { }

}