Clang Project

clang_source_code/test/CodeGenCXX/microsoft-abi-vtables-return-thunks.cpp
1// RUN: %clang_cc1 -fno-rtti %s -emit-llvm -o %t -triple=i386-pc-win32 -fdump-vtable-layouts 2>&1 | FileCheck --check-prefix=VFTABLES %s
2// RUN: FileCheck --check-prefix=GLOBALS %s < %t
3// RUN: FileCheck --check-prefix=CODEGEN %s < %t
4
5namespace test1 {
6
7// Some covariant types.
8struct A { int a; };
9struct B { int b; };
10struct C : A, B { int c; };
11struct D : C { int d; };
12struct E : D { int e; };
13
14// One base class and two overrides, all with covariant return types.
15struct H     { virtual B *foo(); };
16struct I : H { virtual C *foo(); };
17struct J : I { virtual D *foo(); J(); };
18struct K : J { virtual E *foo(); K(); };
19
20J::J() {}
21
22// VFTABLES-LABEL: VFTable for 'test1::H' in 'test1::I' in 'test1::J' (3 entries).
23// VFTABLES-NEXT:   0 | test1::D *test1::J::foo()
24// VFTABLES-NEXT:       [return adjustment (to type 'struct test1::B *'): 4 non-virtual]
25// VFTABLES-NEXT:   1 | test1::D *test1::J::foo()
26// VFTABLES-NEXT:       [return adjustment (to type 'struct test1::C *'): 0 non-virtual]
27// VFTABLES-NEXT:   2 | test1::D *test1::J::foo()
28
29// GLOBALS-LABEL: @"??_7J@test1@@6B@" = linkonce_odr unnamed_addr constant { [3 x i8*] }
30// GLOBALS: @"?foo@J@test1@@QAEPAUB@2@XZ"
31// GLOBALS: @"?foo@J@test1@@QAEPAUC@2@XZ"
32// GLOBALS: @"?foo@J@test1@@UAEPAUD@2@XZ"
33
34K::K() {}
35
36// VFTABLES-LABEL: VFTable for 'test1::H' in 'test1::I' in 'test1::J' in 'test1::K' (4 entries).
37// VFTABLES-NEXT:   0 | test1::E *test1::K::foo()
38// VFTABLES-NEXT:       [return adjustment (to type 'struct test1::B *'): 4 non-virtual]
39// VFTABLES-NEXT:   1 | test1::E *test1::K::foo()
40// VFTABLES-NEXT:       [return adjustment (to type 'struct test1::C *'): 0 non-virtual]
41// VFTABLES-NEXT:   2 | test1::E *test1::K::foo()
42// VFTABLES-NEXT:       [return adjustment (to type 'struct test1::D *'): 0 non-virtual]
43// VFTABLES-NEXT:   3 | test1::E *test1::K::foo()
44
45// Only B to C requires adjustment, but we get 3 thunks in K's vftable, two of
46// which are trivial.
47// GLOBALS-LABEL: @"??_7K@test1@@6B@" = linkonce_odr unnamed_addr constant { [4 x i8*] }
48// GLOBALS: @"?foo@K@test1@@QAEPAUB@2@XZ"
49// GLOBALS: @"?foo@K@test1@@QAEPAUC@2@XZ"
50// GLOBALS: @"?foo@K@test1@@QAEPAUD@2@XZ"
51// GLOBALS: @"?foo@K@test1@@UAEPAUE@2@XZ"
52
53//  This thunk has a return adjustment.
54// CODEGEN-LABEL: define {{.*}} @"?foo@K@test1@@QAEPAUB@2@XZ"
55// CODEGEN: call {{.*}} @"?foo@K@test1@@UAEPAUE@2@XZ"
56// CODEGEN: icmp {{.*}}, null
57// CODEGEN: getelementptr
58// CODEGEN: ret
59
60//  These two don't.
61// CODEGEN-LABEL: define {{.*}} @"?foo@K@test1@@QAEPAUC@2@XZ"
62// CODEGEN: call {{.*}} @"?foo@K@test1@@UAEPAUE@2@XZ"
63// CODEGEN-NEXT: ret
64
65// CODEGEN-LABEL: define {{.*}} @"?foo@K@test1@@QAEPAUD@2@XZ"
66// CODEGEN: call {{.*}} @"?foo@K@test1@@UAEPAUE@2@XZ"
67// CODEGEN-NEXT: ret
68
69}
70
71namespace test2 {
72
73// Covariant types.  D* is not trivially convertible to C*.
74struct A { int a; };
75struct B { int b; };
76struct C : B { int c; };
77struct D : A, C { int d; };
78struct E : D { int e; };
79
80// J's foo will require an adjusting thunk, and K will require a trivial thunk.
81struct H     { virtual B *foo(); };
82struct I : H { virtual C *foo(); };
83struct J : I { virtual D *foo(); J(); };
84struct K : J { virtual E *foo(); K(); };
85
86J::J() {}
87
88// VFTABLES-LABEL: VFTable for 'test2::H' in 'test2::I' in 'test2::J' (2 entries).
89// VFTABLES-NEXT:    0 | test2::D *test2::J::foo()
90// VFTABLES-NEXT:         [return adjustment (to type 'struct test2::B *'): 4 non-virtual]
91// VFTABLES-NEXT:    1 | test2::D *test2::J::foo()
92
93// GLOBALS-LABEL: @"??_7J@test2@@6B@" = linkonce_odr unnamed_addr constant { [2 x i8*] }
94
95K::K() {}
96
97// VFTABLES-LABEL: VFTable for 'test2::H' in 'test2::I' in 'test2::J' in 'test2::K' (3 entries).
98// VFTABLES-NEXT:    0 | test2::E *test2::K::foo()
99// VFTABLES-NEXT:         [return adjustment (to type 'struct test2::B *'): 4 non-virtual]
100// VFTABLES-NEXT:    1 | test2::E *test2::K::foo()
101// VFTABLES-NEXT:         [return adjustment (to type 'struct test2::D *'): 0 non-virtual]
102// VFTABLES-NEXT:    2 | test2::E *test2::K::foo()
103
104// GLOBALS-LABEL: @"??_7K@test2@@6B@" = linkonce_odr unnamed_addr constant { [3 x i8*] }
105
106}
107
108namespace pr20479 {
109struct A {
110  virtual A *f();
111};
112
113struct B : virtual A {
114  virtual B *f();
115};
116
117struct C : virtual A, B {
118// VFTABLES-LABEL: VFTable for 'pr20479::A' in 'pr20479::B' in 'pr20479::C' (2 entries).
119// VFTABLES-NEXT:   0 | pr20479::B *pr20479::B::f()
120// VFTABLES-NEXT:       [return adjustment (to type 'struct pr20479::A *'): vbase #1, 0 non-virtual]
121// VFTABLES-NEXT:   1 | pr20479::B *pr20479::B::f()
122  C();
123};
124
125C::C() {}
126
127// GLOBALS-LABEL: @"??_7C@pr20479@@6B@" = linkonce_odr unnamed_addr constant { [2 x i8*] }
128// GLOBALS: @"?f@B@pr20479@@QAEPAUA@2@XZ"
129// GLOBALS: @"?f@B@pr20479@@UAEPAU12@XZ"
130}
131
132namespace pr21073 {
133struct A {
134  virtual A *f();
135};
136
137struct B : virtual A {
138  virtual B *f();
139};
140
141struct C : virtual A, virtual B {
142// VFTABLES-LABEL: VFTable for 'pr21073::A' in 'pr21073::B' in 'pr21073::C' (2 entries).
143// VFTABLES-NEXT:   0 | pr21073::B *pr21073::B::f()
144// VFTABLES-NEXT:       [return adjustment (to type 'struct pr21073::A *'): vbase #1, 0 non-virtual]
145// VFTABLES-NEXT:       [this adjustment: 8 non-virtual]
146// VFTABLES-NEXT:   1 | pr21073::B *pr21073::B::f()
147// VFTABLES-NEXT:       [return adjustment (to type 'struct pr21073::B *'): 0 non-virtual]
148// VFTABLES-NEXT:       [this adjustment: 8 non-virtual]
149  C();
150};
151
152C::C() {}
153
154// GLOBALS-LABEL: @"??_7C@pr21073@@6B@" = linkonce_odr unnamed_addr constant { [2 x i8*] }
155// GLOBALS: @"?f@B@pr21073@@WPPPPPPPI@AEPAUA@2@XZ"
156// GLOBALS: @"?f@B@pr21073@@WPPPPPPPI@AEPAU12@XZ"
157}
158
159namespace pr21073_2 {
160struct A { virtual A *foo(); };
161struct B : virtual A {};
162struct C : virtual A { virtual C *foo(); };
163struct D : B, C { D(); };
164D::D() {}
165
166// VFTABLES-LABEL: VFTable for 'pr21073_2::A' in 'pr21073_2::C' in 'pr21073_2::D' (2 entries)
167// VFTABLES-NEXT:   0 | pr21073_2::C *pr21073_2::C::foo()
168// VFTABLES-NEXT:       [return adjustment (to type 'struct pr21073_2::A *'): vbase #1, 0 non-virtual]
169// VFTABLES-NEXT:   1 | pr21073_2::C *pr21073_2::C::foo()
170
171// GLOBALS-LABEL: @"??_7D@pr21073_2@@6B@" = {{.*}} constant { [2 x i8*] }
172// GLOBALS: @"?foo@C@pr21073_2@@QAEPAUA@2@XZ"
173// GLOBALS: @"?foo@C@pr21073_2@@UAEPAU12@XZ"
174}
175
176namespace test3 {
177struct A { virtual A *fn(); };
178struct B : virtual A { virtual B *fn(); };
179struct X : virtual B {};
180struct Y : virtual B {};
181struct C : X, Y {};
182struct D : virtual B, virtual A, C {
183  D *fn();
184  D();
185};
186D::D() {}
187
188// VFTABLES-LABEL: VFTable for 'test3::A' in 'test3::B' in 'test3::X' in 'test3::C' in 'test3::D' (3 entries).
189// VFTABLES-NEXT:   0 | test3::D *test3::D::fn()
190// VFTABLES-NEXT:       [return adjustment (to type 'struct test3::A *'): vbase #1, 0 non-virtual]
191// VFTABLES-NEXT:       [this adjustment: vtordisp at -4, 0 non-virtual]
192// VFTABLES-NEXT:   1 | test3::D *test3::D::fn()
193// VFTABLES-NEXT:       [return adjustment (to type 'struct test3::B *'): vbase #2, 0 non-virtual]
194// VFTABLES-NEXT:       [this adjustment: vtordisp at -4, 0 non-virtual]
195// VFTABLES-NEXT:   2 | test3::D *test3::D::fn()
196// VFTABLES-NEXT:       [return adjustment (to type 'struct test3::D *'): 0 non-virtual]
197// VFTABLES-NEXT:       [this adjustment: vtordisp at -4, 0 non-virtual]
198
199// GLOBALS-LABEL: @"??_7D@test3@@6B@" = {{.*}} constant { [3 x i8*] }
200// GLOBALS: @"?fn@D@test3@@$4PPPPPPPM@A@AEPAUA@2@XZ"
201// GLOBALS: @"?fn@D@test3@@$4PPPPPPPM@A@AEPAUB@2@XZ"
202// GLOBALS: @"?fn@D@test3@@$4PPPPPPPM@A@AEPAU12@XZ"
203}
204
205namespace pr34302 {
206// C::f is lives in the vftable inside its virtual B subobject. In the MS ABI,
207// covariant return type virtual methods extend vftables from virtual bases,
208// even though that can make it impossible to implement certain diamond
209// hierarchies correctly.
210struct A { virtual ~A(); };
211struct B : A { virtual B *f(); };
212struct C : virtual B { C *f(); };
213C c;
214// VFTABLES-LABEL: VFTable indices for 'pr34302::C' (2 entries).
215// VFTABLES-NEXT:  -- accessible via vbtable index 1, vfptr at offset 0 --
216// VFTABLES-NEXT:    0 | pr34302::C::~C() [scalar deleting]
217// VFTABLES-NEXT:    2 | pr34302::C *pr34302::C::f()
218}
219