ms_class_layout.cpp
13.6 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// RUN: %clang_cc1 -emit-llvm-only -triple i686-pc-win32 -fdump-record-layouts %s 2>/dev/null \
// RUN: | FileCheck %s
#pragma pack(push, 8)
class B {
public:
virtual void b(){}
int b_field;
protected:
private:
};
class A : public B {
public:
int a_field;
virtual void a(){}
char one;
protected:
private:
};
class D {
public:
virtual void b(){}
double a;
};
class C : public virtual A,
public D, public B {
public:
double c1_field;
int c2_field;
double c3_field;
int c4_field;
virtual void foo(){}
virtual void bar(){}
protected:
private:
};
struct BaseStruct
{
BaseStruct(){}
double v0;
float v1;
C fg;
};
struct DerivedStruct : public BaseStruct {
int x;
};
struct G
{
int g_field;
};
struct H : public G,
public virtual D
{
};
struct I : public virtual D
{
virtual ~I(){}
double q;
};
struct K
{
int k;
};
struct L
{
int l;
};
struct M : public virtual K
{
int m;
};
struct N : public L, public M
{
virtual void f(){}
};
struct O : public H, public G {
virtual void fo(){}
};
struct P : public M, public virtual L {
int p;
};
struct R {};
class IA {
public:
virtual ~IA(){}
virtual void ia() = 0;
};
class ICh : public virtual IA {
public:
virtual ~ICh(){}
virtual void ia(){}
virtual void iCh(){}
};
struct f {
virtual int asd() {return -90;}
};
struct s : public virtual f {
virtual ~s(){}
int r;
virtual int asd() {return -9;}
};
struct sd : virtual s, virtual ICh {
virtual ~sd(){}
int q;
char y;
virtual int asd() {return -1;}
};
struct AV {
virtual void foo();
};
struct BV : AV {
};
struct CV : virtual BV {
CV();
virtual void foo();
};
struct DV : BV {
};
struct EV : CV, DV {
};
#pragma pack(pop)
// This needs only for building layouts.
// Without this clang doesn`t dump record layouts.
int main() {
// This avoid "Can't yet mangle constructors!" for MS ABI.
C* c;
c->foo();
DerivedStruct* v;
H* g;
BaseStruct* u;
I* i;
N* n;
O* o;
P* p;
R* r;
sd *h;
EV *j;
return 0;
}
// CHECK: 0 | class D
// CHECK-NEXT: 0 | (D vftable pointer)
// CHECK-NEXT: 8 | double a
// CHECK-NEXT: sizeof=16, align=8
// CHECK-NEXT: nvsize=16, nvalign=8
// CHECK: %class.D = type { i32 (...)**, double }
// CHECK: 0 | class B
// CHECK-NEXT: 0 | (B vftable pointer)
// CHECK-NEXT: 4 | int b_field
// CHECK-NEXT: sizeof=8, align=4
// CHECK-NEXT: nvsize=8, nvalign=4
// CHECK: %class.B = type { i32 (...)**, i32 }
// CHECK: 0 | class A
// CHECK-NEXT: 0 | class B (primary base)
// CHECK-NEXT: 0 | (B vftable pointer)
// CHECK-NEXT: 4 | int b_field
// CHECK-NEXT: 8 | int a_field
// CHECK-NEXT: 12 | char one
// CHECK-NEXT: sizeof=16, align=4
// CHECK-NEXT: nvsize=16, nvalign=4
// CHECK: 0 | class C
// CHECK-NEXT: 0 | class D (primary base)
// CHECK-NEXT: 0 | (D vftable pointer)
// CHECK-NEXT: 8 | double a
// CHECK-NEXT: 16 | class B (base)
// CHECK-NEXT: 16 | (B vftable pointer)
// CHECK-NEXT: 20 | int b_field
// CHECK-NEXT: 24 | (C vbtable pointer)
// CHECK-NEXT: 32 | double c1_field
// CHECK-NEXT: 40 | int c2_field
// CHECK-NEXT: 48 | double c3_field
// CHECK-NEXT: 56 | int c4_field
// CHECK-NEXT: 64 | class A (virtual base)
// CHECK-NEXT: 64 | class B (primary base)
// CHECK-NEXT: 64 | (B vftable pointer)
// CHECK-NEXT: 68 | int b_field
// CHECK-NEXT: 72 | int a_field
// CHECK-NEXT: 76 | char one
// CHECK-NEXT: sizeof=80, align=8
// CHECK-NEXT: nvsize=64, nvalign=8
// CHECK: %class.A = type { %class.B, i32, i8 }
// CHECK: %class.C = type { %class.D, %class.B, i32*, double, i32, double, i32, [4 x i8], %class.A }
// CHECK: %class.C.base = type { %class.D, %class.B, i32*, double, i32, double, i32 }
// CHECK: 0 | struct BaseStruct
// CHECK-NEXT: 0 | double v0
// CHECK-NEXT: 8 | float v1
// CHECK-NEXT: 16 | class C fg
// CHECK-NEXT: 16 | class D (primary base)
// CHECK-NEXT: 16 | (D vftable pointer)
// CHECK-NEXT: 24 | double a
// CHECK-NEXT: 32 | class B (base)
// CHECK-NEXT: 32 | (B vftable pointer)
// CHECK-NEXT: 36 | int b_field
// CHECK-NEXT: 40 | (C vbtable pointer)
// CHECK-NEXT: 48 | double c1_field
// CHECK-NEXT: 56 | int c2_field
// CHECK-NEXT: 64 | double c3_field
// CHECK-NEXT: 72 | int c4_field
// CHECK-NEXT: 80 | class A (virtual base)
// CHECK-NEXT: 80 | class B (primary base)
// CHECK-NEXT: 80 | (B vftable pointer)
// CHECK-NEXT: 84 | int b_field
// CHECK-NEXT: 88 | int a_field
// CHECK-NEXT: 92 | char one
// CHECK-NEXT: sizeof=96, align=8
// CHECK-NEXT: nvsize=96, nvalign=8
// CHECK: %struct.BaseStruct = type { double, float, %class.C }
// CHECK: 0 | struct DerivedStruct
// CHECK-NEXT: 0 | struct BaseStruct (base)
// CHECK-NEXT: 0 | double v0
// CHECK-NEXT: 8 | float v1
// CHECK-NEXT: 16 | class C fg
// CHECK-NEXT: 16 | class D (primary base)
// CHECK-NEXT: 16 | (D vftable pointer)
// CHECK-NEXT: 24 | double a
// CHECK-NEXT: 32 | class B (base)
// CHECK-NEXT: 32 | (B vftable pointer)
// CHECK-NEXT: 36 | int b_field
// CHECK-NEXT: 40 | (C vbtable pointer)
// CHECK-NEXT: 48 | double c1_field
// CHECK-NEXT: 56 | int c2_field
// CHECK-NEXT: 64 | double c3_field
// CHECK-NEXT: 72 | int c4_field
// CHECK-NEXT: 80 | class A (virtual base)
// CHECK-NEXT: 80 | class B (primary base)
// CHECK-NEXT: 80 | (B vftable pointer)
// CHECK-NEXT: 84 | int b_field
// CHECK-NEXT: 88 | int a_field
// CHECK-NEXT: 92 | char one
// CHECK-NEXT: 96 | int x
// CHECK-NEXT: sizeof=104, align=8
// CHECK-NEXT: nvsize=104, nvalign=8
// CHECK: %struct.DerivedStruct = type { %struct.BaseStruct, i32 }
// CHECK: 0 | struct G
// CHECK-NEXT: 0 | int g_field
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct H
// CHECK-NEXT: 0 | struct G (base)
// CHECK-NEXT: 0 | int g_field
// CHECK-NEXT: 4 | (H vbtable pointer)
// CHECK-NEXT: 8 | class D (virtual base)
// CHECK-NEXT: 8 | (D vftable pointer)
// CHECK-NEXT: 16 | double a
// CHECK-NEXT: sizeof=24, align=8
// CHECK-NEXT: nvsize=8, nvalign=8
// CHECK: %struct.H = type { %struct.G, i32*, %class.D }
// CHECK: 0 | struct I
// CHECK-NEXT: 0 | (I vftable pointer)
// CHECK-NEXT: 8 | (I vbtable pointer)
// CHECK-NEXT: 16 | double q
// CHECK-NEXT: 24 | class D (virtual base)
// CHECK-NEXT: 24 | (D vftable pointer)
// CHECK-NEXT: 32 | double a
// CHECK-NEXT: sizeof=40, align=8
// CHECK-NEXT: nvsize=24, nvalign=8
// CHECK: %struct.I = type { i32 (...)**, [4 x i8], i32*, double, %class.D }
// CHECK: %struct.I.base = type { i32 (...)**, [4 x i8], i32*, double }
// CHECK: 0 | struct L
// CHECK-NEXT: 0 | int l
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct K
// CHECK-NEXT: 0 | int k
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct M
// CHECK-NEXT: 0 | (M vbtable pointer)
// CHECK-NEXT: 4 | int m
// CHECK-NEXT: 8 | struct K (virtual base)
// CHECK-NEXT: 8 | int k
// CHECK-NEXT: sizeof=12, align=4
//CHECK: %struct.M = type { i32*, i32, %struct.K }
//CHECK: %struct.M.base = type { i32*, i32 }
// CHECK: 0 | struct N
// CHECK-NEXT: 0 | (N vftable pointer)
// CHECK-NEXT: 4 | struct L (base)
// CHECK-NEXT: 4 | int l
// CHECK-NEXT: 8 | struct M (base)
// CHECK-NEXT: 8 | (M vbtable pointer)
// CHECK-NEXT: 12 | int m
// CHECK-NEXT: 16 | struct K (virtual base)
// CHECK-NEXT: 16 | int k
// CHECK-NEXT: sizeof=20, align=4
// CHECK-NEXT: nvsize=16, nvalign=4
//CHECK: %struct.N = type { i32 (...)**, %struct.L, %struct.M.base, %struct.K }
// CHECK: 0 | struct O
// CHECK-NEXT: 0 | (O vftable pointer)
// CHECK-NEXT: 8 | struct H (base)
// CHECK-NEXT: 8 | struct G (base)
// CHECK-NEXT: 8 | int g_field
// CHECK-NEXT: 12 | (H vbtable pointer)
// CHECK-NEXT: 16 | struct G (base)
// CHECK-NEXT: 16 | int g_field
// CHECK-NEXT: 24 | class D (virtual base)
// CHECK-NEXT: 24 | (D vftable pointer)
// CHECK-NEXT: 32 | double a
// CHECK-NEXT: | [sizeof=40, align=8
// CHECK-NEXT: | nvsize=24, nvalign=8]
// CHECK: struct.O = type { i32 (...)**, [4 x i8], %struct.H.base, %struct.G, %class.D }
// CHECK: struct.O.base = type { i32 (...)**, [4 x i8], %struct.H.base, %struct.G, [4 x i8] }
// CHECK: 0 | struct P
// CHECK-NEXT: 0 | struct M (base)
// CHECK-NEXT: 0 | (M vbtable pointer)
// CHECK-NEXT: 4 | int m
// CHECK-NEXT: 8 | int p
// CHECK-NEXT: 12 | struct K (virtual base)
// CHECK-NEXT: 12 | int k
// CHECK-NEXT: 16 | struct L (virtual base)
// CHECK-NEXT: 16 | int l
// CHECK-NEXT: sizeof=20, align=4
// CHECK-NEXT: nvsize=12, nvalign=4
//CHECK: %struct.P = type { %struct.M.base, i32, %struct.K, %struct.L }
// CHECK: 0 | struct R (empty)
// CHECK-NEXT: sizeof=1, align=1
// CHECK-NEXT: nvsize=0, nvalign=1
//CHECK: %struct.R = type { i8 }
// CHECK: 0 | struct f
// CHECK-NEXT: 0 | (f vftable pointer)
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct s
// CHECK-NEXT: 0 | (s vftable pointer)
// CHECK-NEXT: 4 | (s vbtable pointer)
// CHECK-NEXT: 8 | int r
// CHECK-NEXT: 12 | (vtordisp for vbase f)
// CHECK-NEXT: 16 | struct f (virtual base)
// CHECK-NEXT: 16 | (f vftable pointer)
// CHECK-NEXT: sizeof=20, align=4
// CHECK-NEXT: nvsize=12, nvalign=4
// CHECK: 0 | class IA
// CHECK-NEXT: 0 | (IA vftable pointer)
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | class ICh
// CHECK-NEXT: 0 | (ICh vftable pointer)
// CHECK-NEXT: 4 | (ICh vbtable pointer)
// CHECK-NEXT: 8 | (vtordisp for vbase IA)
// CHECK-NEXT: 12 | class IA (virtual base)
// CHECK-NEXT: 12 | (IA vftable pointer)
// CHECK-NEXT: sizeof=16, align=4
// CHECK-NEXT: nvsize=8, nvalign=4
// CHECK: 0 | struct sd
// CHECK-NEXT: 0 | (sd vbtable pointer)
// CHECK-NEXT: 4 | int q
// CHECK-NEXT: 8 | char y
// CHECK-NEXT: 12 | (vtordisp for vbase f)
// CHECK-NEXT: 16 | struct f (virtual base)
// CHECK-NEXT: 16 | (f vftable pointer)
// CHECK-NEXT: 20 | struct s (virtual base)
// CHECK-NEXT: 20 | (s vftable pointer)
// CHECK-NEXT: 24 | (s vbtable pointer)
// CHECK-NEXT: 28 | int r
// CHECK-NEXT: 32 | (vtordisp for vbase IA)
// CHECK-NEXT: 36 | class IA (virtual base)
// CHECK-NEXT: 36 | (IA vftable pointer)
// CHECK-NEXT: 40 | class ICh (virtual base)
// CHECK-NEXT: 40 | (ICh vftable pointer)
// CHECK-NEXT: 44 | (ICh vbtable pointer)
// CHECK-NEXT: sizeof=48, align=4
// CHECK-NEXT: nvsize=12, nvalign=4
// CHECK: %struct.f = type { i32 (...)** }
// CHECK: %struct.s = type { i32 (...)**, i32*, i32, i32, %struct.f }
// CHECK: %class.IA = type { i32 (...)** }
// CHECK: %class.ICh = type { i32 (...)**, i32*, i32, %class.IA }
// CHECK: %struct.sd = type { i32*, i32, i8, i32, %struct.f, %struct.s.base, i32, %class.IA, %class.ICh.base }
// CHECK: 0 | struct AV
// CHECK-NEXT: 0 | (AV vftable pointer)
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct BV
// CHECK-NEXT: 0 | struct AV (primary base)
// CHECK-NEXT: 0 | (AV vftable pointer)
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: 0 | struct CV
// CHECK-NEXT: 0 | (CV vbtable pointer)
// CHECK-NEXT: 4 | (vtordisp for vbase BV)
// CHECK-NEXT: 8 | struct BV (virtual base)
// CHECK-NEXT: 8 | struct AV (primary base)
// CHECK-NEXT: 8 | (AV vftable pointer)
// CHECK-NEXT: sizeof=12, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: %struct.AV = type { i32 (...)** }
// CHECK: %struct.BV = type { %struct.AV }
// CHECK: %struct.CV = type { i32*, i32, %struct.BV }
// CHECK: %struct.CV.base = type { i32* }
// CHECK: 0 | struct DV
// CHECK-NEXT: 0 | struct BV (primary base)
// CHECK-NEXT: 0 | struct AV (primary base)
// CHECK-NEXT: 0 | (AV vftable pointer)
// CHECK-NEXT: sizeof=4, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
// CHECK: %struct.DV = type { %struct.BV }
// CHECK: 0 | struct EV
// CHECK-NEXT: 0 | struct DV (primary base)
// CHECK-NEXT: 0 | struct BV (primary base)
// CHECK-NEXT: 0 | struct AV (primary base)
// CHECK-NEXT: 0 | (AV vftable pointer)
// CHECK-NEXT: 4 | struct CV (base)
// CHECK-NEXT: 4 | (CV vbtable pointer)
// CHECK-NEXT: 8 | (vtordisp for vbase BV)
// CHECK-NEXT: 12 | struct BV (virtual base)
// CHECK-NEXT: 12 | struct AV (primary base)
// CHECK-NEXT: 12 | (AV vftable pointer)
// CHECK-NEXT: sizeof=16, align=4
// CHECK-NEXT: nvsize=8, nvalign=4
// CHECK: %struct.EV = type { %struct.DV, %struct.CV.base, i32, %struct.BV }
// CHECK: %struct.EV.base = type { %struct.DV, %struct.CV.base }
// Overriding a method means that all the vbases containing that
// method need a vtordisp. Note: this code will cause an error in cl.exe.
namespace test1 {
struct A { virtual void foo(); };
struct B : A {};
struct C : virtual A, virtual B { C(); virtual void foo(); };
void test() { C *c; }
// CHECK: 0 | struct test1::C
// CHECK-NEXT: 0 | (C vbtable pointer)
// CHECK-NEXT: 4 | (vtordisp for vbase A)
// CHECK-NEXT: 8 | struct test1::A (virtual base)
// CHECK-NEXT: 8 | (A vftable pointer)
// CHECK-NEXT: 12 | (vtordisp for vbase B)
// CHECK-NEXT: 16 | struct test1::B (virtual base)
// CHECK-NEXT: 16 | struct test1::A (primary base)
// CHECK-NEXT: 16 | (A vftable pointer)
// CHECK-NEXT: sizeof=20, align=4
// CHECK-NEXT: nvsize=4, nvalign=4
}