blob: 6acbe436c85db7ca43ed1312430a95596f92dee0 [file] [log] [blame]
Anders Carlsson046c2942010-04-17 20:15:18 +00001//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
Anders Carlssondbd920c2009-10-11 22:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Anders Carlssond6b07fb2009-11-27 20:47:55 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000018#include "clang/AST/RecordLayout.h"
John McCall7a536902010-08-05 20:39:18 +000019#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson5dd730a2009-11-26 19:32:45 +000020#include "llvm/ADT/DenseSet.h"
Anders Carlssonb9021e92010-02-27 16:18:19 +000021#include "llvm/ADT/SetVector.h"
Chandler Carruthe087f072010-02-13 10:38:52 +000022#include "llvm/Support/Compiler.h"
Anders Carlsson824d7ea2010-02-11 08:02:13 +000023#include "llvm/Support/Format.h"
Anders Carlsson5e454aa2010-03-17 20:06:32 +000024#include <algorithm>
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000025#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000026
27using namespace clang;
28using namespace CodeGen;
29
Anders Carlsson27f69d02009-11-27 22:21:51 +000030namespace {
Anders Carlsson824d7ea2010-02-11 08:02:13 +000031
Anders Carlsson2fca9be2010-02-23 01:34:28 +000032/// BaseOffset - Represents an offset from a derived class to a direct or
33/// indirect base class.
34struct BaseOffset {
35 /// DerivedClass - The derived class.
36 const CXXRecordDecl *DerivedClass;
37
38 /// VirtualBase - If the path from the derived class to the base class
39 /// involves a virtual base class, this holds its declaration.
40 const CXXRecordDecl *VirtualBase;
41
42 /// NonVirtualOffset - The offset from the derived class to the base class.
43 /// (Or the offset from the virtual base class to the base class, if the
44 /// path from the derived class to the base class involves a virtual base
45 /// class.
46 int64_t NonVirtualOffset;
47
48 BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { }
49 BaseOffset(const CXXRecordDecl *DerivedClass,
50 const CXXRecordDecl *VirtualBase, int64_t NonVirtualOffset)
51 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
52 NonVirtualOffset(NonVirtualOffset) { }
53
54 bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; }
55};
56
Anders Carlssonbdf73d82010-02-11 21:24:32 +000057/// FinalOverriders - Contains the final overrider member functions for all
58/// member functions in the base subobjects of a class.
59class FinalOverriders {
Anders Carlssona7575822010-02-12 16:55:34 +000060public:
61 /// OverriderInfo - Information about a final overrider.
62 struct OverriderInfo {
63 /// Method - The method decl of the overrider.
64 const CXXMethodDecl *Method;
Anders Carlsson96309c42010-02-18 16:29:24 +000065
Anders Carlssondb4022c2010-03-10 02:33:41 +000066 /// Offset - the base offset of the overrider in the layout class.
67 uint64_t Offset;
Anders Carlssona7575822010-02-12 16:55:34 +000068
Anders Carlssoncd0d27f2010-03-12 05:02:01 +000069 OverriderInfo() : Method(0), Offset(0) { }
Anders Carlssona7575822010-02-12 16:55:34 +000070 };
71
72private:
Anders Carlssonbdf73d82010-02-11 21:24:32 +000073 /// MostDerivedClass - The most derived class for which the final overriders
74 /// are stored.
75 const CXXRecordDecl *MostDerivedClass;
76
Anders Carlssondb4022c2010-03-10 02:33:41 +000077 /// MostDerivedClassOffset - If we're building final overriders for a
78 /// construction vtable, this holds the offset from the layout class to the
79 /// most derived class.
80 const uint64_t MostDerivedClassOffset;
81
82 /// LayoutClass - The class we're using for layout information. Will be
83 /// different than the most derived class if the final overriders are for a
84 /// construction vtable.
85 const CXXRecordDecl *LayoutClass;
86
Anders Carlssonbdf73d82010-02-11 21:24:32 +000087 ASTContext &Context;
88
89 /// MostDerivedClassLayout - the AST record layout of the most derived class.
90 const ASTRecordLayout &MostDerivedClassLayout;
91
Anders Carlsson0a028602010-06-01 23:52:26 +000092 /// MethodBaseOffsetPairTy - Uniquely identifies a member function
Anders Carlsson0c0eeb22010-02-13 02:02:03 +000093 /// in a base subobject.
Anders Carlsson0a028602010-06-01 23:52:26 +000094 typedef std::pair<const CXXMethodDecl *, uint64_t> MethodBaseOffsetPairTy;
95
96 typedef llvm::DenseMap<MethodBaseOffsetPairTy,
Anders Carlssona7575822010-02-12 16:55:34 +000097 OverriderInfo> OverridersMapTy;
Anders Carlssonbdf73d82010-02-11 21:24:32 +000098
99 /// OverridersMap - The final overriders for all virtual member functions of
100 /// all the base subobjects of the most derived class.
101 OverridersMapTy OverridersMap;
102
Anders Carlsson147a23d2010-06-04 00:54:04 +0000103 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
104 /// as a record decl and a subobject number) and its offsets in the most
105 /// derived class as well as the layout class.
106 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
107 uint64_t> SubobjectOffsetMapTy;
108
109 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
110
111 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
112 /// given base.
113 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
114 uint64_t OffsetInLayoutClass,
115 SubobjectOffsetMapTy &SubobjectOffsets,
116 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
117 SubobjectCountMapTy &SubobjectCounts);
118
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000119 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
120
121 /// dump - dump the final overriders for a base subobject, and all its direct
122 /// and indirect base subobjects.
123 void dump(llvm::raw_ostream &Out, BaseSubobject Base,
124 VisitedVirtualBasesSetTy& VisitedVirtualBases);
125
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000126public:
Anders Carlssondb4022c2010-03-10 02:33:41 +0000127 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
128 uint64_t MostDerivedClassOffset,
129 const CXXRecordDecl *LayoutClass);
Anders Carlsson2157d302010-02-24 22:18:01 +0000130
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000131 /// getOverrider - Get the final overrider for the given method declaration in
Anders Carlsson26387642010-06-01 23:17:37 +0000132 /// the subobject with the given base offset.
133 OverriderInfo getOverrider(const CXXMethodDecl *MD,
134 uint64_t BaseOffset) const {
Anders Carlsson0a028602010-06-01 23:52:26 +0000135 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000136 "Did not find overrider!");
137
Anders Carlsson0a028602010-06-01 23:52:26 +0000138 return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000139 }
140
141 /// dump - dump the final overriders.
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000142 void dump() {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000143 VisitedVirtualBasesSetTy VisitedVirtualBases;
144 dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0), VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000145 }
146
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000147};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000148
Anders Carlsson327568e2010-02-27 20:02:53 +0000149#define DUMP_OVERRIDERS 0
150
Anders Carlssondb4022c2010-03-10 02:33:41 +0000151FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
152 uint64_t MostDerivedClassOffset,
153 const CXXRecordDecl *LayoutClass)
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000154 : MostDerivedClass(MostDerivedClass),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000155 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000156 Context(MostDerivedClass->getASTContext()),
157 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000158
Anders Carlsson147a23d2010-06-04 00:54:04 +0000159 // Compute base offsets.
160 SubobjectOffsetMapTy SubobjectOffsets;
161 SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
162 SubobjectCountMapTy SubobjectCounts;
163 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, 0), /*IsVirtual=*/false,
164 MostDerivedClassOffset, SubobjectOffsets,
165 SubobjectLayoutClassOffsets, SubobjectCounts);
166
167 // Get the the final overriders.
168 CXXFinalOverriderMap FinalOverriders;
169 MostDerivedClass->getFinalOverriders(FinalOverriders);
170
171 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(),
172 E = FinalOverriders.end(); I != E; ++I) {
173 const CXXMethodDecl *MD = I->first;
174 const OverridingMethods& Methods = I->second;
175
176 for (OverridingMethods::const_iterator I = Methods.begin(),
177 E = Methods.end(); I != E; ++I) {
178 unsigned SubobjectNumber = I->first;
179 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(),
180 SubobjectNumber)) &&
181 "Did not find subobject offset!");
182
183 uint64_t BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
184 SubobjectNumber)];
185
186 assert(I->second.size() == 1 && "Final overrider is not unique!");
187 const UniqueVirtualMethod &Method = I->second.front();
188
189 const CXXRecordDecl *OverriderRD = Method.Method->getParent();
190 assert(SubobjectLayoutClassOffsets.count(
191 std::make_pair(OverriderRD, Method.Subobject))
192 && "Did not find subobject offset!");
193 uint64_t OverriderOffset =
194 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD,
195 Method.Subobject)];
196
197 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
198 assert(!Overrider.Method && "Overrider should not exist yet!");
199
200 Overrider.Offset = OverriderOffset;
201 Overrider.Method = Method.Method;
202 }
203 }
Anders Carlsson327568e2010-02-27 20:02:53 +0000204
205#if DUMP_OVERRIDERS
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000206 // And dump them (for now).
207 dump();
Anders Carlsson327568e2010-02-27 20:02:53 +0000208#endif
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000209}
210
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000211static BaseOffset ComputeBaseOffset(ASTContext &Context,
212 const CXXRecordDecl *DerivedRD,
213 const CXXBasePath &Path) {
Anders Carlssondf31af12010-02-13 23:17:31 +0000214 int64_t NonVirtualOffset = 0;
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000215
Anders Carlssona4a54172010-02-13 20:41:15 +0000216 unsigned NonVirtualStart = 0;
217 const CXXRecordDecl *VirtualBase = 0;
218
219 // First, look for the virtual base class.
220 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
221 const CXXBasePathElement &Element = Path[I];
222
223 if (Element.Base->isVirtual()) {
224 // FIXME: Can we break when we find the first virtual base?
225 // (If we can't, can't we just iterate over the path in reverse order?)
226 NonVirtualStart = I + 1;
227 QualType VBaseType = Element.Base->getType();
228 VirtualBase =
229 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
230 }
231 }
232
233 // Now compute the non-virtual offset.
234 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
235 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000236
237 // Check the base class offset.
238 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
Anders Carlssonbf554f62010-02-25 22:18:35 +0000239
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000240 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
241 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
Anders Carlssonbf554f62010-02-25 22:18:35 +0000242
Anders Carlssona14f5972010-10-31 23:22:37 +0000243 NonVirtualOffset += Layout.getBaseClassOffsetInBits(Base);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000244 }
245
246 // FIXME: This should probably use CharUnits or something. Maybe we should
247 // even change the base offsets in ASTRecordLayout to be specified in
248 // CharUnits.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000249 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset / 8);
Anders Carlssone67dc302010-02-14 00:16:19 +0000250
251}
252
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000253static BaseOffset ComputeBaseOffset(ASTContext &Context,
254 const CXXRecordDecl *BaseRD,
255 const CXXRecordDecl *DerivedRD) {
Anders Carlssone67dc302010-02-14 00:16:19 +0000256 CXXBasePaths Paths(/*FindAmbiguities=*/false,
257 /*RecordPaths=*/true, /*DetectVirtual=*/false);
258
259 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
260 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
261 assert(false && "Class must be derived from the passed in base class!");
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000262 return BaseOffset();
Anders Carlssone67dc302010-02-14 00:16:19 +0000263 }
264
265 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000266}
267
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000268static BaseOffset
Anders Carlssondf31af12010-02-13 23:17:31 +0000269ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
270 const CXXMethodDecl *DerivedMD,
271 const CXXMethodDecl *BaseMD) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000272 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
273 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
274
275 // Canonicalize the return types.
276 CanQualType CanDerivedReturnType =
277 Context.getCanonicalType(DerivedFT->getResultType());
278 CanQualType CanBaseReturnType =
279 Context.getCanonicalType(BaseFT->getResultType());
280
281 assert(CanDerivedReturnType->getTypeClass() ==
282 CanBaseReturnType->getTypeClass() &&
283 "Types must have same type class!");
284
285 if (CanDerivedReturnType == CanBaseReturnType) {
286 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000287 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000288 }
289
290 if (isa<ReferenceType>(CanDerivedReturnType)) {
291 CanDerivedReturnType =
292 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
293 CanBaseReturnType =
294 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
295 } else if (isa<PointerType>(CanDerivedReturnType)) {
296 CanDerivedReturnType =
297 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
298 CanBaseReturnType =
299 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
300 } else {
301 assert(false && "Unexpected return type!");
302 }
303
304 // We need to compare unqualified types here; consider
305 // const T *Base::foo();
306 // T *Derived::foo();
307 if (CanDerivedReturnType.getUnqualifiedType() ==
308 CanBaseReturnType.getUnqualifiedType()) {
309 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000310 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000311 }
312
313 const CXXRecordDecl *DerivedRD =
314 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
315
316 const CXXRecordDecl *BaseRD =
317 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
318
Anders Carlssondf31af12010-02-13 23:17:31 +0000319 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000320}
321
Anders Carlsson147a23d2010-06-04 00:54:04 +0000322void
323FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
324 uint64_t OffsetInLayoutClass,
325 SubobjectOffsetMapTy &SubobjectOffsets,
326 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
327 SubobjectCountMapTy &SubobjectCounts) {
328 const CXXRecordDecl *RD = Base.getBase();
329
330 unsigned SubobjectNumber = 0;
331 if (!IsVirtual)
332 SubobjectNumber = ++SubobjectCounts[RD];
333
334 // Set up the subobject to offset mapping.
335 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber))
336 && "Subobject offset already exists!");
337 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber))
338 && "Subobject offset already exists!");
339
340 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] =
341 Base.getBaseOffset();
342 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
343 OffsetInLayoutClass;
344
345 // Traverse our bases.
346 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
347 E = RD->bases_end(); I != E; ++I) {
348 const CXXRecordDecl *BaseDecl =
349 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
350
351 uint64_t BaseOffset;
352 uint64_t BaseOffsetInLayoutClass;
353 if (I->isVirtual()) {
354 // Check if we've visited this virtual base before.
355 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0)))
356 continue;
357
358 const ASTRecordLayout &LayoutClassLayout =
359 Context.getASTRecordLayout(LayoutClass);
360
Anders Carlssona14f5972010-10-31 23:22:37 +0000361 BaseOffset = MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000362 BaseOffsetInLayoutClass =
Anders Carlssona14f5972010-10-31 23:22:37 +0000363 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000364 } else {
365 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000366 uint64_t Offset = Layout.getBaseClassOffsetInBits(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000367
368 BaseOffset = Base.getBaseOffset() + Offset;
369 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
370 }
371
372 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), I->isVirtual(),
373 BaseOffsetInLayoutClass, SubobjectOffsets,
374 SubobjectLayoutClassOffsets, SubobjectCounts);
375 }
376}
377
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000378void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base,
379 VisitedVirtualBasesSetTy &VisitedVirtualBases) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000380 const CXXRecordDecl *RD = Base.getBase();
381 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
382
383 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
384 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000385 const CXXRecordDecl *BaseDecl =
386 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
387
Anders Carlssonb828afa2010-02-14 17:05:59 +0000388 // Ignore bases that don't have any virtual member functions.
389 if (!BaseDecl->isPolymorphic())
390 continue;
391
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000392 uint64_t BaseOffset;
393 if (I->isVirtual()) {
394 if (!VisitedVirtualBases.insert(BaseDecl)) {
395 // We've visited this base before.
396 continue;
397 }
398
Anders Carlssona14f5972010-10-31 23:22:37 +0000399 BaseOffset = MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000400 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +0000401 BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl) +
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000402 Base.getBaseOffset();
403 }
404
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000405 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000406 }
407
408 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
Anders Carlssondb4022c2010-03-10 02:33:41 +0000409 Out << Base.getBaseOffset() / 8 << ")\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000410
411 // Now dump the overriders for this base subobject.
412 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
413 E = RD->method_end(); I != E; ++I) {
414 const CXXMethodDecl *MD = *I;
415
416 if (!MD->isVirtual())
417 continue;
418
Anders Carlsson26387642010-06-01 23:17:37 +0000419 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000420
Anders Carlssond86e5d82010-03-02 03:44:06 +0000421 Out << " " << MD->getQualifiedNameAsString() << " - (";
Anders Carlsson16b73122010-02-12 17:13:23 +0000422 Out << Overrider.Method->getQualifiedNameAsString();
Anders Carlssoncd0d27f2010-03-12 05:02:01 +0000423 Out << ", " << ", " << Overrider.Offset / 8 << ')';
Anders Carlssond86e5d82010-03-02 03:44:06 +0000424
Anders Carlsson7097e752010-06-01 23:47:05 +0000425 BaseOffset Offset;
426 if (!Overrider.Method->isPure())
427 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
Anders Carlssona4a54172010-02-13 20:41:15 +0000428
Anders Carlsson7097e752010-06-01 23:47:05 +0000429 if (!Offset.isEmpty()) {
Anders Carlssona4a54172010-02-13 20:41:15 +0000430 Out << " [ret-adj: ";
431 if (Offset.VirtualBase)
432 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000433
Anders Carlssona4a54172010-02-13 20:41:15 +0000434 Out << Offset.NonVirtualOffset << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000435 }
Anders Carlssondf31af12010-02-13 23:17:31 +0000436
Anders Carlsson16b73122010-02-12 17:13:23 +0000437 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000438 }
439}
440
Anders Carlsson94464812010-04-10 19:13:06 +0000441/// VTableComponent - Represents a single component in a vtable.
442class VTableComponent {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000443public:
444 enum Kind {
445 CK_VCallOffset,
446 CK_VBaseOffset,
447 CK_OffsetToTop,
448 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000449 CK_FunctionPointer,
450
451 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
452 CK_CompleteDtorPointer,
453
454 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
Anders Carlsson1f464662010-02-19 20:08:13 +0000455 CK_DeletingDtorPointer,
456
457 /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer
458 /// will end up never being called. Such vtable function pointers are
459 /// represented as a CK_UnusedFunctionPointer.
460 CK_UnusedFunctionPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000461 };
462
Anders Carlsson94464812010-04-10 19:13:06 +0000463 static VTableComponent MakeVCallOffset(int64_t Offset) {
464 return VTableComponent(CK_VCallOffset, Offset);
Anders Carlssond9a67042010-02-17 06:07:19 +0000465 }
466
Anders Carlsson94464812010-04-10 19:13:06 +0000467 static VTableComponent MakeVBaseOffset(int64_t Offset) {
468 return VTableComponent(CK_VBaseOffset, Offset);
Anders Carlssona661a542010-02-16 04:49:44 +0000469 }
470
Anders Carlsson94464812010-04-10 19:13:06 +0000471 static VTableComponent MakeOffsetToTop(int64_t Offset) {
472 return VTableComponent(CK_OffsetToTop, Offset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000473 }
474
Anders Carlsson94464812010-04-10 19:13:06 +0000475 static VTableComponent MakeRTTI(const CXXRecordDecl *RD) {
476 return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000477 }
478
Anders Carlsson94464812010-04-10 19:13:06 +0000479 static VTableComponent MakeFunction(const CXXMethodDecl *MD) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000480 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000481 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000482
Anders Carlsson94464812010-04-10 19:13:06 +0000483 return VTableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000484 reinterpret_cast<uintptr_t>(MD));
485 }
486
Anders Carlsson94464812010-04-10 19:13:06 +0000487 static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
488 return VTableComponent(CK_CompleteDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000489 reinterpret_cast<uintptr_t>(DD));
490 }
491
Anders Carlsson94464812010-04-10 19:13:06 +0000492 static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
493 return VTableComponent(CK_DeletingDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000494 reinterpret_cast<uintptr_t>(DD));
495 }
496
Anders Carlsson94464812010-04-10 19:13:06 +0000497 static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) {
Anders Carlsson1f464662010-02-19 20:08:13 +0000498 assert(!isa<CXXDestructorDecl>(MD) &&
499 "Don't use MakeUnusedFunction with destructors!");
Anders Carlsson94464812010-04-10 19:13:06 +0000500 return VTableComponent(CK_UnusedFunctionPointer,
Anders Carlsson1f464662010-02-19 20:08:13 +0000501 reinterpret_cast<uintptr_t>(MD));
502 }
503
Anders Carlsson94464812010-04-10 19:13:06 +0000504 static VTableComponent getFromOpaqueInteger(uint64_t I) {
505 return VTableComponent(I);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000506 }
507
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000508 /// getKind - Get the kind of this vtable component.
509 Kind getKind() const {
510 return (Kind)(Value & 0x7);
511 }
512
Anders Carlssond9a67042010-02-17 06:07:19 +0000513 int64_t getVCallOffset() const {
514 assert(getKind() == CK_VCallOffset && "Invalid component kind!");
515
516 return getOffset();
517 }
518
Anders Carlssona661a542010-02-16 04:49:44 +0000519 int64_t getVBaseOffset() const {
520 assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
521
522 return getOffset();
523 }
524
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000525 int64_t getOffsetToTop() const {
526 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
527
528 return getOffset();
529 }
530
531 const CXXRecordDecl *getRTTIDecl() const {
532 assert(getKind() == CK_RTTI && "Invalid component kind!");
533
534 return reinterpret_cast<CXXRecordDecl *>(getPointer());
535 }
536
537 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000538 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000539
540 return reinterpret_cast<CXXMethodDecl *>(getPointer());
541 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000542
543 const CXXDestructorDecl *getDestructorDecl() const {
544 assert((getKind() == CK_CompleteDtorPointer ||
545 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
546
547 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
548 }
549
Anders Carlsson1f464662010-02-19 20:08:13 +0000550 const CXXMethodDecl *getUnusedFunctionDecl() const {
551 assert(getKind() == CK_UnusedFunctionPointer);
552
553 return reinterpret_cast<CXXMethodDecl *>(getPointer());
554 }
555
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000556private:
Anders Carlsson94464812010-04-10 19:13:06 +0000557 VTableComponent(Kind ComponentKind, int64_t Offset) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000558 assert((ComponentKind == CK_VCallOffset ||
559 ComponentKind == CK_VBaseOffset ||
560 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
561 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
562
563 Value = ((Offset << 3) | ComponentKind);
564 }
565
Anders Carlsson94464812010-04-10 19:13:06 +0000566 VTableComponent(Kind ComponentKind, uintptr_t Ptr) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000567 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000568 ComponentKind == CK_FunctionPointer ||
569 ComponentKind == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000570 ComponentKind == CK_DeletingDtorPointer ||
571 ComponentKind == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000572 "Invalid component kind!");
573
574 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
575
576 Value = Ptr | ComponentKind;
577 }
578
579 int64_t getOffset() const {
580 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
581 getKind() == CK_OffsetToTop) && "Invalid component kind!");
582
583 return Value >> 3;
584 }
585
586 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000587 assert((getKind() == CK_RTTI ||
588 getKind() == CK_FunctionPointer ||
589 getKind() == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000590 getKind() == CK_DeletingDtorPointer ||
591 getKind() == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000592 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000593
594 return static_cast<uintptr_t>(Value & ~7ULL);
595 }
596
Anders Carlsson94464812010-04-10 19:13:06 +0000597 explicit VTableComponent(uint64_t Value)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000598 : Value(Value) { }
599
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000600 /// The kind is stored in the lower 3 bits of the value. For offsets, we
601 /// make use of the facts that classes can't be larger than 2^55 bytes,
602 /// so we store the offset in the lower part of the 61 bytes that remain.
603 /// (The reason that we're not simply using a PointerIntPair here is that we
604 /// need the offsets to be 64-bit, even when on a 32-bit machine).
605 int64_t Value;
606};
607
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000608/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
609struct VCallOffsetMap {
610
611 typedef std::pair<const CXXMethodDecl *, int64_t> MethodAndOffsetPairTy;
612
613 /// Offsets - Keeps track of methods and their offsets.
614 // FIXME: This should be a real map and not a vector.
615 llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets;
616
Anders Carlssoneb577d02010-02-18 17:26:40 +0000617 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
618 /// can share the same vcall offset.
619 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
620 const CXXMethodDecl *RHS);
621
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000622public:
623 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
624 /// add was successful, or false if there was already a member function with
625 /// the same signature in the map.
626 bool AddVCallOffset(const CXXMethodDecl *MD, int64_t OffsetOffset);
627
628 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
629 /// vtable address point) for the given virtual member function.
630 int64_t getVCallOffsetOffset(const CXXMethodDecl *MD);
631
Anders Carlssondba6ef82010-02-25 22:23:13 +0000632 // empty - Return whether the offset map is empty or not.
633 bool empty() const { return Offsets.empty(); }
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000634};
635
John McCall331a03a2010-02-26 20:22:44 +0000636static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
637 const CXXMethodDecl *RHS) {
638 ASTContext &C = LHS->getASTContext(); // TODO: thread this down
639 CanQual<FunctionProtoType>
640 LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(),
641 RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>();
642
643 // Fast-path matches in the canonical types.
644 if (LT == RT) return true;
645
646 // Force the signatures to match. We can't rely on the overrides
647 // list here because there isn't necessarily an inheritance
648 // relationship between the two methods.
649 if (LT.getQualifiers() != RT.getQualifiers() ||
650 LT->getNumArgs() != RT->getNumArgs())
651 return false;
652 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
653 if (LT->getArgType(I) != RT->getArgType(I))
654 return false;
655 return true;
656}
657
Anders Carlssoneb577d02010-02-18 17:26:40 +0000658bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
659 const CXXMethodDecl *RHS) {
660 assert(LHS->isVirtual() && "LHS must be virtual!");
661 assert(RHS->isVirtual() && "LHS must be virtual!");
662
Anders Carlsson55609692010-02-27 04:12:52 +0000663 // A destructor can share a vcall offset with another destructor.
664 if (isa<CXXDestructorDecl>(LHS))
665 return isa<CXXDestructorDecl>(RHS);
666
Anders Carlssoneb577d02010-02-18 17:26:40 +0000667 // FIXME: We need to check more things here.
668
Anders Carlsson55609692010-02-27 04:12:52 +0000669 // The methods must have the same name.
Anders Carlssoneb577d02010-02-18 17:26:40 +0000670 DeclarationName LHSName = LHS->getDeclName();
671 DeclarationName RHSName = RHS->getDeclName();
John McCall331a03a2010-02-26 20:22:44 +0000672 if (LHSName != RHSName)
Anders Carlssoneb577d02010-02-18 17:26:40 +0000673 return false;
John McCall331a03a2010-02-26 20:22:44 +0000674
Anders Carlsson55609692010-02-27 04:12:52 +0000675 // And the same signatures.
676 return HasSameVirtualSignature(LHS, RHS);
Anders Carlssoneb577d02010-02-18 17:26:40 +0000677}
678
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000679bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
680 int64_t OffsetOffset) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000681 // Check if we can reuse an offset.
682 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
683 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
684 return false;
685 }
686
687 // Add the offset.
688 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000689 return true;
690}
691
692int64_t VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000693 // Look for an offset.
694 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
695 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
696 return Offsets[I].second;
697 }
698
699 assert(false && "Should always find a vcall offset offset!");
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000700 return 0;
701}
702
Anders Carlssond68c0832010-02-25 03:45:56 +0000703/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
704class VCallAndVBaseOffsetBuilder {
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000705public:
706 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
707 VBaseOffsetOffsetsMapTy;
708
709private:
Anders Carlsson530c40c2010-02-28 01:43:58 +0000710 /// MostDerivedClass - The most derived class for which we're building vcall
711 /// and vbase offsets.
Anders Carlssond68c0832010-02-25 03:45:56 +0000712 const CXXRecordDecl *MostDerivedClass;
713
Anders Carlsson0041a642010-02-27 21:09:00 +0000714 /// LayoutClass - The class we're using for layout information. Will be
715 /// different than the most derived class if we're building a construction
716 /// vtable.
717 const CXXRecordDecl *LayoutClass;
718
Anders Carlssond68c0832010-02-25 03:45:56 +0000719 /// Context - The ASTContext which we will use for layout information.
720 ASTContext &Context;
721
722 /// Components - vcall and vbase offset components
Anders Carlsson94464812010-04-10 19:13:06 +0000723 typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy;
724 VTableComponentVectorTy Components;
Anders Carlssond68c0832010-02-25 03:45:56 +0000725
726 /// VisitedVirtualBases - Visited virtual bases.
727 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
728
729 /// VCallOffsets - Keeps track of vcall offsets.
730 VCallOffsetMap VCallOffsets;
731
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000732
733 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
734 /// relative to the address point.
735 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
736
Anders Carlssond68c0832010-02-25 03:45:56 +0000737 /// FinalOverriders - The final overriders of the most derived class.
738 /// (Can be null when we're not building a vtable of the most derived class).
739 const FinalOverriders *Overriders;
740
741 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
742 /// given base subobject.
743 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
744 uint64_t RealBaseOffset);
745
746 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
747 void AddVCallOffsets(BaseSubobject Base, uint64_t VBaseOffset);
748
749 /// AddVBaseOffsets - Add vbase offsets for the given class.
Anders Carlsson88800662010-03-02 05:40:45 +0000750 void AddVBaseOffsets(const CXXRecordDecl *Base, uint64_t OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000751
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000752 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
753 /// bytes, relative to the vtable address point.
754 int64_t getCurrentOffsetOffset() const;
755
Anders Carlssond68c0832010-02-25 03:45:56 +0000756public:
757 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
Anders Carlsson0041a642010-02-27 21:09:00 +0000758 const CXXRecordDecl *LayoutClass,
Anders Carlssond68c0832010-02-25 03:45:56 +0000759 const FinalOverriders *Overriders,
Anders Carlsson530c40c2010-02-28 01:43:58 +0000760 BaseSubobject Base, bool BaseIsVirtual,
761 uint64_t OffsetInLayoutClass)
762 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
Anders Carlssond68c0832010-02-25 03:45:56 +0000763 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
764
765 // Add vcall and vbase offsets.
Anders Carlsson530c40c2010-02-28 01:43:58 +0000766 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000767 }
768
769 /// Methods for iterating over the components.
Anders Carlsson94464812010-04-10 19:13:06 +0000770 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
Anders Carlssond68c0832010-02-25 03:45:56 +0000771 const_iterator components_begin() const { return Components.rbegin(); }
772 const_iterator components_end() const { return Components.rend(); }
773
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000774 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
775 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000776 return VBaseOffsetOffsets;
777 }
Anders Carlssond68c0832010-02-25 03:45:56 +0000778};
779
780void
781VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
782 bool BaseIsVirtual,
783 uint64_t RealBaseOffset) {
784 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
785
786 // Itanium C++ ABI 2.5.2:
787 // ..in classes sharing a virtual table with a primary base class, the vcall
788 // and vbase offsets added by the derived class all come before the vcall
789 // and vbase offsets required by the base class, so that the latter may be
790 // laid out as required by the base class without regard to additions from
791 // the derived class(es).
792
793 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
794 // emit them for the primary base first).
795 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000796 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlssond68c0832010-02-25 03:45:56 +0000797
798 uint64_t PrimaryBaseOffset;
799
800 // Get the base offset of the primary base.
801 if (PrimaryBaseIsVirtual) {
Anders Carlssona14f5972010-10-31 23:22:37 +0000802 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000803 "Primary vbase should have a zero offset!");
804
805 const ASTRecordLayout &MostDerivedClassLayout =
806 Context.getASTRecordLayout(MostDerivedClass);
807
808 PrimaryBaseOffset =
Anders Carlssona14f5972010-10-31 23:22:37 +0000809 MostDerivedClassLayout.getVBaseClassOffsetInBits(PrimaryBase);
Anders Carlssond68c0832010-02-25 03:45:56 +0000810 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +0000811 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000812 "Primary base should have a zero offset!");
813
814 PrimaryBaseOffset = Base.getBaseOffset();
815 }
816
817 AddVCallAndVBaseOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
818 PrimaryBaseIsVirtual, RealBaseOffset);
819 }
820
Anders Carlsson88800662010-03-02 05:40:45 +0000821 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000822
823 // We only want to add vcall offsets for virtual bases.
824 if (BaseIsVirtual)
825 AddVCallOffsets(Base, RealBaseOffset);
826}
827
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000828int64_t VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
829 // OffsetIndex is the index of this vcall or vbase offset, relative to the
830 // vtable address point. (We subtract 3 to account for the information just
831 // above the address point, the RTTI info, the offset to top, and the
832 // vcall offset itself).
833 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
834
835 // FIXME: We shouldn't use / 8 here.
836 int64_t OffsetOffset = OffsetIndex *
837 (int64_t)Context.Target.getPointerWidth(0) / 8;
838
839 return OffsetOffset;
840}
841
Anders Carlssond68c0832010-02-25 03:45:56 +0000842void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
843 uint64_t VBaseOffset) {
844 const CXXRecordDecl *RD = Base.getBase();
845 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
846
John McCall331a03a2010-02-26 20:22:44 +0000847 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
848
Anders Carlssond68c0832010-02-25 03:45:56 +0000849 // Handle the primary base first.
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000850 // We only want to add vcall offsets if the base is non-virtual; a virtual
851 // primary base will have its vcall and vbase offsets emitted already.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000852 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000853 // Get the base offset of the primary base.
Anders Carlssona14f5972010-10-31 23:22:37 +0000854 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000855 "Primary base should have a zero offset!");
Anders Carlssond68c0832010-02-25 03:45:56 +0000856
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000857 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
Anders Carlssond68c0832010-02-25 03:45:56 +0000858 VBaseOffset);
859 }
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000860
Anders Carlssond68c0832010-02-25 03:45:56 +0000861 // Add the vcall offsets.
862 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
863 E = RD->method_end(); I != E; ++I) {
864 const CXXMethodDecl *MD = *I;
865
866 if (!MD->isVirtual())
867 continue;
868
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000869 int64_t OffsetOffset = getCurrentOffsetOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +0000870
871 // Don't add a vcall offset if we already have one for this member function
872 // signature.
873 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
874 continue;
875
876 int64_t Offset = 0;
877
878 if (Overriders) {
879 // Get the final overrider.
880 FinalOverriders::OverriderInfo Overrider =
Anders Carlsson26387642010-06-01 23:17:37 +0000881 Overriders->getOverrider(MD, Base.getBaseOffset());
Anders Carlssond68c0832010-02-25 03:45:56 +0000882
Anders Carlsson438bc422010-02-28 00:10:58 +0000883 /// The vcall offset is the offset from the virtual base to the object
884 /// where the function was overridden.
Anders Carlssond68c0832010-02-25 03:45:56 +0000885 // FIXME: We should not use / 8 here.
Anders Carlssond86e5d82010-03-02 03:44:06 +0000886 Offset = (int64_t)(Overrider.Offset - VBaseOffset) / 8;
Anders Carlssond68c0832010-02-25 03:45:56 +0000887 }
888
Anders Carlsson94464812010-04-10 19:13:06 +0000889 Components.push_back(VTableComponent::MakeVCallOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000890 }
891
892 // And iterate over all non-virtual bases (ignoring the primary base).
893 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
894 E = RD->bases_end(); I != E; ++I) {
895
896 if (I->isVirtual())
897 continue;
898
899 const CXXRecordDecl *BaseDecl =
900 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
John McCall331a03a2010-02-26 20:22:44 +0000901 if (BaseDecl == PrimaryBase)
902 continue;
Anders Carlssond68c0832010-02-25 03:45:56 +0000903
904 // Get the base offset of this base.
905 uint64_t BaseOffset = Base.getBaseOffset() +
Anders Carlssona14f5972010-10-31 23:22:37 +0000906 Layout.getBaseClassOffsetInBits(BaseDecl);
Anders Carlssond68c0832010-02-25 03:45:56 +0000907
908 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), VBaseOffset);
909 }
910}
911
Anders Carlssond68c0832010-02-25 03:45:56 +0000912void VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
Anders Carlsson88800662010-03-02 05:40:45 +0000913 uint64_t OffsetInLayoutClass) {
Anders Carlsson0041a642010-02-27 21:09:00 +0000914 const ASTRecordLayout &LayoutClassLayout =
915 Context.getASTRecordLayout(LayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000916
917 // Add vbase offsets.
918 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
919 E = RD->bases_end(); I != E; ++I) {
920 const CXXRecordDecl *BaseDecl =
921 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
922
923 // Check if this is a virtual base that we haven't visited before.
924 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
925 // FIXME: We shouldn't use / 8 here.
Anders Carlsson88800662010-03-02 05:40:45 +0000926 int64_t Offset =
Anders Carlssona14f5972010-10-31 23:22:37 +0000927 (int64_t)(LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl) -
Anders Carlsson88800662010-03-02 05:40:45 +0000928 OffsetInLayoutClass) / 8;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000929
930 // Add the vbase offset offset.
931 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
932 "vbase offset offset already exists!");
933
934 int64_t VBaseOffsetOffset = getCurrentOffsetOffset();
935 VBaseOffsetOffsets.insert(std::make_pair(BaseDecl, VBaseOffsetOffset));
936
Anders Carlsson94464812010-04-10 19:13:06 +0000937 Components.push_back(VTableComponent::MakeVBaseOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000938 }
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000939
Anders Carlssond68c0832010-02-25 03:45:56 +0000940 // Check the base class looking for more vbase offsets.
Anders Carlsson88800662010-03-02 05:40:45 +0000941 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000942 }
943}
944
Anders Carlsson94464812010-04-10 19:13:06 +0000945/// VTableBuilder - Class for building vtable layout information.
946class VTableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +0000947public:
Anders Carlssonb9021e92010-02-27 16:18:19 +0000948 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
949 /// primary bases.
950 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
951 PrimaryBasesSetVectorTy;
952
Anders Carlssonccd83d72010-03-24 16:42:11 +0000953 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
954 VBaseOffsetOffsetsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +0000955
956 typedef llvm::DenseMap<BaseSubobject, uint64_t>
957 AddressPointsMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000958
Anders Carlsson57071e22010-02-12 05:25:12 +0000959private:
Anders Carlssonaf440352010-03-23 04:11:45 +0000960 /// VTables - Global vtable information.
961 CodeGenVTables &VTables;
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000962
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000963 /// MostDerivedClass - The most derived class for which we're building this
964 /// vtable.
965 const CXXRecordDecl *MostDerivedClass;
966
Anders Carlssond03a3262010-02-27 20:39:05 +0000967 /// MostDerivedClassOffset - If we're building a construction vtable, this
968 /// holds the offset from the layout class to the most derived class.
969 const uint64_t MostDerivedClassOffset;
970
Anders Carlsson5d7af6b2010-02-28 00:36:23 +0000971 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
972 /// base. (This only makes sense when building a construction vtable).
973 bool MostDerivedClassIsVirtual;
974
Anders Carlssond03a3262010-02-27 20:39:05 +0000975 /// LayoutClass - The class we're using for layout information. Will be
976 /// different than the most derived class if we're building a construction
977 /// vtable.
978 const CXXRecordDecl *LayoutClass;
979
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000980 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +0000981 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000982
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000983 /// FinalOverriders - The final overriders of the most derived class.
Anders Carlssond68c0832010-02-25 03:45:56 +0000984 const FinalOverriders Overriders;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000985
Anders Carlssondba6ef82010-02-25 22:23:13 +0000986 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
987 /// bases in this vtable.
988 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000989
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000990 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
991 /// the most derived class.
992 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
993
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000994 /// Components - The components of the vtable being built.
Anders Carlsson94464812010-04-10 19:13:06 +0000995 llvm::SmallVector<VTableComponent, 64> Components;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000996
Anders Carlsson822307b2010-02-11 17:18:51 +0000997 /// AddressPoints - Address points for the vtable being built.
Anders Carlssonff143f82010-03-25 00:35:49 +0000998 AddressPointsMapTy AddressPoints;
Anders Carlsson822307b2010-02-11 17:18:51 +0000999
Anders Carlssonb9021e92010-02-27 16:18:19 +00001000 /// MethodInfo - Contains information about a method in a vtable.
1001 /// (Used for computing 'this' pointer adjustment thunks.
1002 struct MethodInfo {
1003 /// BaseOffset - The base offset of this method.
1004 const uint64_t BaseOffset;
1005
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001006 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
1007 /// method.
1008 const uint64_t BaseOffsetInLayoutClass;
1009
Anders Carlsson718d0692010-04-11 22:07:06 +00001010 /// VTableIndex - The index in the vtable that this method has.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001011 /// (For destructors, this is the index of the complete destructor).
Anders Carlsson718d0692010-04-11 22:07:06 +00001012 const uint64_t VTableIndex;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001013
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001014 MethodInfo(uint64_t BaseOffset, uint64_t BaseOffsetInLayoutClass,
Anders Carlsson718d0692010-04-11 22:07:06 +00001015 uint64_t VTableIndex)
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001016 : BaseOffset(BaseOffset),
1017 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
Anders Carlsson718d0692010-04-11 22:07:06 +00001018 VTableIndex(VTableIndex) { }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001019
Anders Carlsson718d0692010-04-11 22:07:06 +00001020 MethodInfo() : BaseOffset(0), BaseOffsetInLayoutClass(0), VTableIndex(0) { }
Anders Carlssonb9021e92010-02-27 16:18:19 +00001021 };
1022
1023 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
1024
1025 /// MethodInfoMap - The information for all methods in the vtable we're
1026 /// currently building.
1027 MethodInfoMapTy MethodInfoMap;
1028
Anders Carlsson718d0692010-04-11 22:07:06 +00001029 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
Anders Carlssondd366812010-03-12 04:54:20 +00001030
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001031 /// VTableThunks - The thunks by vtable index in the vtable currently being
1032 /// built.
Anders Carlsson718d0692010-04-11 22:07:06 +00001033 VTableThunksMapTy VTableThunks;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001034
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001035 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
1036 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001037
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001038 /// Thunks - A map that contains all the thunks needed for all methods in the
1039 /// most derived class for which the vtable is currently being built.
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001040 ThunksMapTy Thunks;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001041
1042 /// AddThunk - Add a thunk for the given method.
Anders Carlsson9d734f32010-03-21 20:27:14 +00001043 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001044
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001045 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
1046 /// part of the vtable we're currently building.
1047 void ComputeThisAdjustments();
1048
Anders Carlssona661a542010-02-16 04:49:44 +00001049 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1050
Anders Carlssonb9021e92010-02-27 16:18:19 +00001051 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
Anders Carlssonaf280c02010-02-23 03:48:14 +00001052 /// some other base.
1053 VisitedVirtualBasesSetTy PrimaryVirtualBases;
1054
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001055 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001056 /// adjustment base offset.
Anders Carlsson2fca9be2010-02-23 01:34:28 +00001057 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001058
Anders Carlsson379017f2010-02-27 19:57:44 +00001059 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
1060 /// the 'this' pointer from the base subobject to the derived subobject.
1061 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1062 BaseSubobject Derived) const;
1063
Anders Carlsson60cc72a2010-02-18 17:00:09 +00001064 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
Anders Carlsson617def32010-03-12 05:28:07 +00001065 /// given virtual member function, its offset in the layout class and its
1066 /// final overrider.
1067 ThisAdjustment
1068 ComputeThisAdjustment(const CXXMethodDecl *MD,
1069 uint64_t BaseOffsetInLayoutClass,
1070 FinalOverriders::OverriderInfo Overrider);
1071
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001072 /// AddMethod - Add a single virtual member function to the vtable
1073 /// components vector.
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001074 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001075
Anders Carlsson464c8f12010-02-23 04:26:39 +00001076 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
1077 /// part of the vtable.
1078 ///
1079 /// Itanium C++ ABI 2.5.2:
1080 ///
1081 /// struct A { virtual void f(); };
1082 /// struct B : virtual public A { int i; };
1083 /// struct C : virtual public A { int j; };
1084 /// struct D : public B, public C {};
1085 ///
1086 /// When B and C are declared, A is a primary base in each case, so although
1087 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
1088 /// adjustment is required and no thunk is generated. However, inside D
1089 /// objects, A is no longer a primary base of C, so if we allowed calls to
1090 /// C::f() to use the copy of A's vtable in the C subobject, we would need
1091 /// to adjust this from C* to B::A*, which would require a third-party
1092 /// thunk. Since we require that a call to C::f() first convert to A*,
1093 /// C-in-D's copy of A's vtable is never referenced, so this is not
1094 /// necessary.
Anders Carlsson60396612010-03-10 06:51:42 +00001095 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
1096 uint64_t BaseOffsetInLayoutClass,
1097 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1098 uint64_t FirstBaseOffsetInLayoutClass) const;
1099
Anders Carlsson464c8f12010-02-23 04:26:39 +00001100
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001101 /// AddMethods - Add the methods of this base subobject and all its
1102 /// primary bases to the vtable components vector.
Anders Carlsson60396612010-03-10 06:51:42 +00001103 void AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
1104 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1105 uint64_t FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001106 PrimaryBasesSetVectorTy &PrimaryBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001107
Anders Carlsson718d0692010-04-11 22:07:06 +00001108 // LayoutVTable - Layout the vtable for the given base class, including its
Anders Carlsson89ad4212010-02-16 16:26:28 +00001109 // secondary vtables and any vtables for virtual bases.
Anders Carlsson718d0692010-04-11 22:07:06 +00001110 void LayoutVTable();
Anders Carlsson89ad4212010-02-16 16:26:28 +00001111
Anders Carlsson718d0692010-04-11 22:07:06 +00001112 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
Anders Carlsson89ad4212010-02-16 16:26:28 +00001113 /// given base subobject, as well as all its secondary vtables.
Anders Carlsson91232842010-04-11 22:03:57 +00001114 ///
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001115 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1116 /// or a direct or indirect base of a virtual base.
1117 ///
Anders Carlsson91232842010-04-11 22:03:57 +00001118 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
1119 /// in the layout class.
Anders Carlsson718d0692010-04-11 22:07:06 +00001120 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001121 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001122 bool BaseIsVirtualInLayoutClass,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001123 uint64_t OffsetInLayoutClass);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001124
Anders Carlsson718d0692010-04-11 22:07:06 +00001125 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
Anders Carlssonc7b63162010-02-16 16:02:57 +00001126 /// subobject.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001127 ///
1128 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1129 /// or a direct or indirect base of a virtual base.
Anders Carlsson718d0692010-04-11 22:07:06 +00001130 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
Anders Carlssona96a2e92010-03-10 19:39:11 +00001131 uint64_t OffsetInLayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001132
1133 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
1134 /// class hierarchy.
1135 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Anders Carlssond7fdae52010-03-10 03:02:01 +00001136 uint64_t OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001137 VisitedVirtualBasesSetTy &VBases);
1138
Anders Carlsson718d0692010-04-11 22:07:06 +00001139 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001140 /// given base (excluding any primary bases).
Anders Carlsson718d0692010-04-11 22:07:06 +00001141 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001142 VisitedVirtualBasesSetTy &VBases);
1143
Anders Carlsson718d0692010-04-11 22:07:06 +00001144 /// isBuildingConstructionVTable - Return whether this vtable builder is
Anders Carlsson438bc422010-02-28 00:10:58 +00001145 /// building a construction vtable.
Anders Carlsson718d0692010-04-11 22:07:06 +00001146 bool isBuildingConstructorVTable() const {
Anders Carlsson438bc422010-02-28 00:10:58 +00001147 return MostDerivedClass != LayoutClass;
1148 }
1149
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001150public:
Anders Carlsson94464812010-04-10 19:13:06 +00001151 VTableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass,
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001152 uint64_t MostDerivedClassOffset, bool MostDerivedClassIsVirtual,
Anders Carlssond03a3262010-02-27 20:39:05 +00001153 const CXXRecordDecl *LayoutClass)
Anders Carlssonaf440352010-03-23 04:11:45 +00001154 : VTables(VTables), MostDerivedClass(MostDerivedClass),
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001155 MostDerivedClassOffset(MostDerivedClassOffset),
1156 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
1157 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
Anders Carlssondb4022c2010-03-10 02:33:41 +00001158 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001159
Anders Carlsson718d0692010-04-11 22:07:06 +00001160 LayoutVTable();
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001161 }
1162
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00001163 ThunksMapTy::const_iterator thunks_begin() const {
1164 return Thunks.begin();
1165 }
1166
1167 ThunksMapTy::const_iterator thunks_end() const {
1168 return Thunks.end();
1169 }
1170
Anders Carlssonccd83d72010-03-24 16:42:11 +00001171 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1172 return VBaseOffsetOffsets;
1173 }
1174
1175 /// getNumVTableComponents - Return the number of components in the vtable
1176 /// currently built.
1177 uint64_t getNumVTableComponents() const {
1178 return Components.size();
1179 }
1180
Anders Carlsson66d567d2010-03-25 00:51:13 +00001181 const uint64_t *vtable_components_data_begin() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001182 return reinterpret_cast<const uint64_t *>(Components.begin());
1183 }
1184
Anders Carlsson66d567d2010-03-25 00:51:13 +00001185 const uint64_t *vtable_components_data_end() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001186 return reinterpret_cast<const uint64_t *>(Components.end());
1187 }
1188
Anders Carlsson66d567d2010-03-25 00:51:13 +00001189 AddressPointsMapTy::const_iterator address_points_begin() const {
1190 return AddressPoints.begin();
1191 }
1192
1193 AddressPointsMapTy::const_iterator address_points_end() const {
1194 return AddressPoints.end();
1195 }
1196
Anders Carlsson718d0692010-04-11 22:07:06 +00001197 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001198 return VTableThunks.begin();
1199 }
1200
Anders Carlsson718d0692010-04-11 22:07:06 +00001201 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001202 return VTableThunks.end();
1203 }
1204
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001205 /// dumpLayout - Dump the vtable layout.
1206 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001207};
1208
Anders Carlsson94464812010-04-10 19:13:06 +00001209void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
Anders Carlsson718d0692010-04-11 22:07:06 +00001210 assert(!isBuildingConstructorVTable() &&
Anders Carlssonbca5d372010-03-22 20:06:40 +00001211 "Can't add thunks for construction vtable");
Anders Carlsson9135a842010-03-18 02:44:19 +00001212
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001213 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001214
1215 // Check if we have this thunk already.
1216 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1217 ThunksVector.end())
1218 return;
1219
1220 ThunksVector.push_back(Thunk);
1221}
1222
Anders Carlssonf622b452010-04-10 20:39:29 +00001223typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1224
1225/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1226/// the overridden methods that the function decl overrides.
1227static void
1228ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1229 OverriddenMethodsSetTy& OverriddenMethods) {
1230 assert(MD->isVirtual() && "Method is not virtual!");
1231
Anders Carlsson57071e22010-02-12 05:25:12 +00001232 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1233 E = MD->end_overridden_methods(); I != E; ++I) {
1234 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001235
Anders Carlssonf622b452010-04-10 20:39:29 +00001236 OverriddenMethods.insert(OverriddenMD);
1237
1238 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods);
Anders Carlsson57071e22010-02-12 05:25:12 +00001239 }
Anders Carlsson57071e22010-02-12 05:25:12 +00001240}
1241
Anders Carlsson94464812010-04-10 19:13:06 +00001242void VTableBuilder::ComputeThisAdjustments() {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001243 // Now go through the method info map and see if any of the methods need
1244 // 'this' pointer adjustments.
1245 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1246 E = MethodInfoMap.end(); I != E; ++I) {
1247 const CXXMethodDecl *MD = I->first;
1248 const MethodInfo &MethodInfo = I->second;
1249
Anders Carlsson617def32010-03-12 05:28:07 +00001250 // Ignore adjustments for unused function pointers.
Anders Carlsson718d0692010-04-11 22:07:06 +00001251 uint64_t VTableIndex = MethodInfo.VTableIndex;
1252 if (Components[VTableIndex].getKind() ==
Anders Carlsson94464812010-04-10 19:13:06 +00001253 VTableComponent::CK_UnusedFunctionPointer)
Anders Carlsson617def32010-03-12 05:28:07 +00001254 continue;
1255
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001256 // Get the final overrider for this method.
1257 FinalOverriders::OverriderInfo Overrider =
Anders Carlsson26387642010-06-01 23:17:37 +00001258 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001259
Anders Carlssonbca5d372010-03-22 20:06:40 +00001260 // Check if we need an adjustment at all.
Anders Carlssonadb507d2010-03-29 15:08:41 +00001261 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
1262 // When a return thunk is needed by a derived class that overrides a
1263 // virtual base, gcc uses a virtual 'this' adjustment as well.
1264 // While the thunk itself might be needed by vtables in subclasses or
1265 // in construction vtables, there doesn't seem to be a reason for using
1266 // the thunk in this vtable. Still, we do so to match gcc.
Anders Carlsson718d0692010-04-11 22:07:06 +00001267 if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
Anders Carlssonadb507d2010-03-29 15:08:41 +00001268 continue;
1269 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001270
Anders Carlsson617def32010-03-12 05:28:07 +00001271 ThisAdjustment ThisAdjustment =
1272 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1273
1274 if (ThisAdjustment.isEmpty())
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001275 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001276
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001277 // Add it.
Anders Carlsson718d0692010-04-11 22:07:06 +00001278 VTableThunks[VTableIndex].This = ThisAdjustment;
Anders Carlsson617def32010-03-12 05:28:07 +00001279
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001280 if (isa<CXXDestructorDecl>(MD)) {
1281 // Add an adjustment for the deleting destructor as well.
Anders Carlsson718d0692010-04-11 22:07:06 +00001282 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001283 }
1284 }
1285
1286 /// Clear the method info map.
1287 MethodInfoMap.clear();
Anders Carlsson25394212010-03-22 15:47:01 +00001288
Anders Carlsson718d0692010-04-11 22:07:06 +00001289 if (isBuildingConstructorVTable()) {
Anders Carlsson25394212010-03-22 15:47:01 +00001290 // We don't need to store thunk information for construction vtables.
1291 return;
1292 }
1293
Anders Carlsson718d0692010-04-11 22:07:06 +00001294 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(),
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001295 E = VTableThunks.end(); I != E; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00001296 const VTableComponent &Component = Components[I->first];
Anders Carlsson25394212010-03-22 15:47:01 +00001297 const ThunkInfo &Thunk = I->second;
Anders Carlssonbca5d372010-03-22 20:06:40 +00001298 const CXXMethodDecl *MD;
Anders Carlsson25394212010-03-22 15:47:01 +00001299
1300 switch (Component.getKind()) {
1301 default:
1302 llvm_unreachable("Unexpected vtable component kind!");
Anders Carlsson94464812010-04-10 19:13:06 +00001303 case VTableComponent::CK_FunctionPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001304 MD = Component.getFunctionDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001305 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001306 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001307 MD = Component.getDestructorDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001308 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001309 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson25394212010-03-22 15:47:01 +00001310 // We've already added the thunk when we saw the complete dtor pointer.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001311 continue;
Anders Carlsson25394212010-03-22 15:47:01 +00001312 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001313
1314 if (MD->getParent() == MostDerivedClass)
1315 AddThunk(MD, Thunk);
Anders Carlsson25394212010-03-22 15:47:01 +00001316 }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001317}
1318
Anders Carlsson94464812010-04-10 19:13:06 +00001319ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001320 ReturnAdjustment Adjustment;
1321
1322 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001323 if (Offset.VirtualBase) {
1324 // Get the virtual base offset offset.
Anders Carlsson1972ced2010-03-11 07:00:45 +00001325 if (Offset.DerivedClass == MostDerivedClass) {
1326 // We can get the offset offset directly from our map.
1327 Adjustment.VBaseOffsetOffset =
1328 VBaseOffsetOffsets.lookup(Offset.VirtualBase);
1329 } else {
1330 Adjustment.VBaseOffsetOffset =
Anders Carlssonaf440352010-03-23 04:11:45 +00001331 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1332 Offset.VirtualBase);
Anders Carlsson1972ced2010-03-11 07:00:45 +00001333 }
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001334 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001335
1336 Adjustment.NonVirtual = Offset.NonVirtualOffset;
1337 }
1338
1339 return Adjustment;
1340}
1341
Anders Carlsson379017f2010-02-27 19:57:44 +00001342BaseOffset
Anders Carlsson94464812010-04-10 19:13:06 +00001343VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
Anders Carlsson379017f2010-02-27 19:57:44 +00001344 BaseSubobject Derived) const {
1345 const CXXRecordDecl *BaseRD = Base.getBase();
1346 const CXXRecordDecl *DerivedRD = Derived.getBase();
1347
1348 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1349 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1350
1351 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
1352 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
1353 assert(false && "Class must be derived from the passed in base class!");
1354 return BaseOffset();
1355 }
1356
1357 // We have to go through all the paths, and see which one leads us to the
1358 // right base subobject.
1359 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1360 I != E; ++I) {
1361 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1362
1363 // FIXME: Should not use * 8 here.
1364 uint64_t OffsetToBaseSubobject = Offset.NonVirtualOffset * 8;
1365
1366 if (Offset.VirtualBase) {
1367 // If we have a virtual base class, the non-virtual offset is relative
1368 // to the virtual base class offset.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001369 const ASTRecordLayout &LayoutClassLayout =
1370 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson379017f2010-02-27 19:57:44 +00001371
1372 /// Get the virtual base offset, relative to the most derived class
1373 /// layout.
1374 OffsetToBaseSubobject +=
Anders Carlssona14f5972010-10-31 23:22:37 +00001375 LayoutClassLayout.getVBaseClassOffsetInBits(Offset.VirtualBase);
Anders Carlsson379017f2010-02-27 19:57:44 +00001376 } else {
1377 // Otherwise, the non-virtual offset is relative to the derived class
1378 // offset.
1379 OffsetToBaseSubobject += Derived.getBaseOffset();
1380 }
1381
1382 // Check if this path gives us the right base subobject.
1383 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
1384 // Since we're going from the base class _to_ the derived class, we'll
1385 // invert the non-virtual offset here.
1386 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1387 return Offset;
1388 }
1389 }
1390
1391 return BaseOffset();
1392}
1393
Anders Carlsson80d8d7d2010-03-23 15:13:06 +00001394ThisAdjustment
Anders Carlsson94464812010-04-10 19:13:06 +00001395VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD,
Anders Carlsson617def32010-03-12 05:28:07 +00001396 uint64_t BaseOffsetInLayoutClass,
1397 FinalOverriders::OverriderInfo Overrider) {
Anders Carlsson617def32010-03-12 05:28:07 +00001398 // Ignore adjustments for pure virtual member functions.
1399 if (Overrider.Method->isPure())
1400 return ThisAdjustment();
1401
1402 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1403 BaseOffsetInLayoutClass);
1404
1405 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1406 Overrider.Offset);
1407
1408 // Compute the adjustment offset.
1409 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1410 OverriderBaseSubobject);
1411 if (Offset.isEmpty())
1412 return ThisAdjustment();
1413
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001414 ThisAdjustment Adjustment;
1415
Anders Carlsson617def32010-03-12 05:28:07 +00001416 if (Offset.VirtualBase) {
1417 // Get the vcall offset map for this virtual base.
1418 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
Anders Carlssoneb577d02010-02-18 17:26:40 +00001419
Anders Carlsson617def32010-03-12 05:28:07 +00001420 if (VCallOffsets.empty()) {
1421 // We don't have vcall offsets for this virtual base, go ahead and
1422 // build them.
1423 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
1424 /*FinalOverriders=*/0,
1425 BaseSubobject(Offset.VirtualBase, 0),
1426 /*BaseIsVirtual=*/true,
1427 /*OffsetInLayoutClass=*/0);
1428
1429 VCallOffsets = Builder.getVCallOffsets();
1430 }
1431
1432 Adjustment.VCallOffsetOffset = VCallOffsets.getVCallOffsetOffset(MD);
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001433 }
Anders Carlsson617def32010-03-12 05:28:07 +00001434
1435 // Set the non-virtual part of the adjustment.
1436 Adjustment.NonVirtual = Offset.NonVirtualOffset;
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001437
1438 return Adjustment;
1439}
Anders Carlsson617def32010-03-12 05:28:07 +00001440
Anders Carlsson57071e22010-02-12 05:25:12 +00001441void
Anders Carlsson94464812010-04-10 19:13:06 +00001442VTableBuilder::AddMethod(const CXXMethodDecl *MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001443 ReturnAdjustment ReturnAdjustment) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001444 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1445 assert(ReturnAdjustment.isEmpty() &&
1446 "Destructor can't have return adjustment!");
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001447
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001448 // Add both the complete destructor and the deleting destructor.
Anders Carlsson94464812010-04-10 19:13:06 +00001449 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1450 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001451 } else {
1452 // Add the return adjustment if necessary.
1453 if (!ReturnAdjustment.isEmpty())
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001454 VTableThunks[Components.size()].Return = ReturnAdjustment;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001455
1456 // Add the function.
Anders Carlsson94464812010-04-10 19:13:06 +00001457 Components.push_back(VTableComponent::MakeFunction(MD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001458 }
1459}
1460
Anders Carlssonc784ba22010-02-27 06:38:03 +00001461/// OverridesIndirectMethodInBase - Return whether the given member function
1462/// overrides any methods in the set of given bases.
1463/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1464/// For example, if we have:
1465///
1466/// struct A { virtual void f(); }
1467/// struct B : A { virtual void f(); }
1468/// struct C : B { virtual void f(); }
1469///
1470/// OverridesIndirectMethodInBase will return true if given C::f as the method
Anders Carlssonf622b452010-04-10 20:39:29 +00001471/// and { A } as the set of bases.
Anders Carlssonc784ba22010-02-27 06:38:03 +00001472static bool
1473OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001474 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonfbf05612010-04-17 17:24:33 +00001475 if (Bases.count(MD->getParent()))
1476 return true;
1477
Anders Carlssonc784ba22010-02-27 06:38:03 +00001478 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1479 E = MD->end_overridden_methods(); I != E; ++I) {
1480 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonc784ba22010-02-27 06:38:03 +00001481
1482 // Check "indirect overriders".
1483 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1484 return true;
1485 }
1486
1487 return false;
1488}
1489
Anders Carlsson464c8f12010-02-23 04:26:39 +00001490bool
Anders Carlsson94464812010-04-10 19:13:06 +00001491VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
Anders Carlsson60396612010-03-10 06:51:42 +00001492 uint64_t BaseOffsetInLayoutClass,
1493 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1494 uint64_t FirstBaseOffsetInLayoutClass) const {
Anders Carlsson464c8f12010-02-23 04:26:39 +00001495 // If the base and the first base in the primary base chain have the same
1496 // offsets, then this overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001497 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001498 return true;
1499
1500 // We know now that Base (or a direct or indirect base of it) is a primary
1501 // base in part of the class hierarchy, but not a primary base in the most
1502 // derived class.
1503
1504 // If the overrider is the first base in the primary base chain, we know
1505 // that the overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001506 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
Anders Carlsson464c8f12010-02-23 04:26:39 +00001507 return true;
1508
Anders Carlsson94464812010-04-10 19:13:06 +00001509 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001510
Anders Carlsson60396612010-03-10 06:51:42 +00001511 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001512 PrimaryBases.insert(RD);
1513
1514 // Now traverse the base chain, starting with the first base, until we find
1515 // the base that is no longer a primary base.
1516 while (true) {
1517 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1518 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1519
1520 if (!PrimaryBase)
1521 break;
1522
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001523 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001524 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001525 "Primary base should always be at offset 0!");
1526
Anders Carlssondad0f992010-02-28 18:37:33 +00001527 const ASTRecordLayout &LayoutClassLayout =
1528 Context.getASTRecordLayout(LayoutClass);
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001529
1530 // Now check if this is the primary base that is not a primary base in the
1531 // most derived class.
Anders Carlssona14f5972010-10-31 23:22:37 +00001532 if (LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase) !=
Anders Carlsson60396612010-03-10 06:51:42 +00001533 FirstBaseOffsetInLayoutClass) {
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001534 // We found it, stop walking the chain.
1535 break;
1536 }
1537 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001538 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001539 "Primary base should always be at offset 0!");
1540 }
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001541
Anders Carlssonf89bb6c2010-02-27 19:51:04 +00001542 if (!PrimaryBases.insert(PrimaryBase))
1543 assert(false && "Found a duplicate primary base!");
1544
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001545 RD = PrimaryBase;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001546 }
1547
1548 // If the final overrider is an override of one of the primary bases,
1549 // then we know that it will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001550 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
Anders Carlsson464c8f12010-02-23 04:26:39 +00001551}
1552
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001553/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1554/// from the nearest base. Returns null if no method was found.
1555static const CXXMethodDecl *
1556FindNearestOverriddenMethod(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001557 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonf622b452010-04-10 20:39:29 +00001558 OverriddenMethodsSetTy OverriddenMethods;
1559 ComputeAllOverriddenMethods(MD, OverriddenMethods);
1560
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001561 for (int I = Bases.size(), E = 0; I != E; --I) {
1562 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1563
1564 // Now check the overriden methods.
Anders Carlssonf622b452010-04-10 20:39:29 +00001565 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1566 E = OverriddenMethods.end(); I != E; ++I) {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001567 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonf622b452010-04-10 20:39:29 +00001568
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001569 // We found our overridden method.
1570 if (OverriddenMD->getParent() == PrimaryBase)
1571 return OverriddenMD;
1572 }
1573 }
1574
1575 return 0;
1576}
1577
Anders Carlsson60396612010-03-10 06:51:42 +00001578void
Anders Carlsson94464812010-04-10 19:13:06 +00001579VTableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001580 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1581 uint64_t FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001582 PrimaryBasesSetVectorTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +00001583 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001584 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1585
1586 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson60396612010-03-10 06:51:42 +00001587 uint64_t PrimaryBaseOffset;
1588 uint64_t PrimaryBaseOffsetInLayoutClass;
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001589 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001590 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf280c02010-02-23 03:48:14 +00001591 "Primary vbase should have a zero offset!");
1592
1593 const ASTRecordLayout &MostDerivedClassLayout =
1594 Context.getASTRecordLayout(MostDerivedClass);
1595
Anders Carlsson60396612010-03-10 06:51:42 +00001596 PrimaryBaseOffset =
Anders Carlssona14f5972010-10-31 23:22:37 +00001597 MostDerivedClassLayout.getVBaseClassOffsetInBits(PrimaryBase);
Anders Carlsson60396612010-03-10 06:51:42 +00001598
1599 const ASTRecordLayout &LayoutClassLayout =
1600 Context.getASTRecordLayout(LayoutClass);
1601
1602 PrimaryBaseOffsetInLayoutClass =
Anders Carlssona14f5972010-10-31 23:22:37 +00001603 LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase);
Anders Carlssonaf280c02010-02-23 03:48:14 +00001604 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001605 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlsson57071e22010-02-12 05:25:12 +00001606 "Primary base should have a zero offset!");
Anders Carlssonaf280c02010-02-23 03:48:14 +00001607
Anders Carlsson60396612010-03-10 06:51:42 +00001608 PrimaryBaseOffset = Base.getBaseOffset();
1609 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
Anders Carlssonaf280c02010-02-23 03:48:14 +00001610 }
Anders Carlssondad0f992010-02-28 18:37:33 +00001611
Anders Carlsson60396612010-03-10 06:51:42 +00001612 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1613 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1614 FirstBaseOffsetInLayoutClass, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001615
1616 if (!PrimaryBases.insert(PrimaryBase))
1617 assert(false && "Found a duplicate primary base!");
1618 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001619
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001620 // Now go through all virtual member functions and add them.
1621 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1622 E = RD->method_end(); I != E; ++I) {
1623 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001624
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001625 if (!MD->isVirtual())
1626 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +00001627
1628 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +00001629 FinalOverriders::OverriderInfo Overrider =
Anders Carlsson26387642010-06-01 23:17:37 +00001630 Overriders.getOverrider(MD, Base.getBaseOffset());
Anders Carlsson57071e22010-02-12 05:25:12 +00001631
1632 // Check if this virtual member function overrides a method in a primary
1633 // base. If this is the case, and the return type doesn't require adjustment
1634 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001635 if (const CXXMethodDecl *OverriddenMD =
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001636 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlssondf31af12010-02-13 23:17:31 +00001637 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001638 OverriddenMD).isEmpty()) {
1639 // Replace the method info of the overridden method with our own
1640 // method.
1641 assert(MethodInfoMap.count(OverriddenMD) &&
1642 "Did not find the overridden method!");
1643 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1644
1645 MethodInfo MethodInfo(Base.getBaseOffset(),
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001646 BaseOffsetInLayoutClass,
Anders Carlsson718d0692010-04-11 22:07:06 +00001647 OverriddenMethodInfo.VTableIndex);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001648
1649 assert(!MethodInfoMap.count(MD) &&
1650 "Should not have method info for this method yet!");
1651
1652 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1653 MethodInfoMap.erase(OverriddenMD);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001654
1655 // If the overridden method exists in a virtual base class or a direct
1656 // or indirect base class of a virtual base class, we need to emit a
1657 // thunk if we ever have a class hierarchy where the base class is not
1658 // a primary base in the complete object.
Anders Carlsson718d0692010-04-11 22:07:06 +00001659 if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
Anders Carlssonbca5d372010-03-22 20:06:40 +00001660 // Compute the this adjustment.
1661 ThisAdjustment ThisAdjustment =
1662 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1663 Overrider);
1664
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001665 if (ThisAdjustment.VCallOffsetOffset &&
1666 Overrider.Method->getParent() == MostDerivedClass) {
John McCall4e3b17c2010-11-09 01:18:05 +00001667
1668 // There's no return adjustment from OverriddenMD and MD,
1669 // but that doesn't mean there isn't one between MD and
1670 // the final overrider.
1671 BaseOffset ReturnAdjustmentOffset =
1672 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1673 ReturnAdjustment ReturnAdjustment =
1674 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1675
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001676 // This is a virtual thunk for the most derived class, add it.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001677 AddThunk(Overrider.Method,
John McCall4e3b17c2010-11-09 01:18:05 +00001678 ThunkInfo(ThisAdjustment, ReturnAdjustment));
Anders Carlssonbca5d372010-03-22 20:06:40 +00001679 }
1680 }
1681
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001682 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001683 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001684 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00001685
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001686 // Insert the method info for this method.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001687 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1688 Components.size());
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001689
1690 assert(!MethodInfoMap.count(MD) &&
1691 "Should not have method info for this method yet!");
1692 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001693
1694 // Check if this overrider is going to be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001695 const CXXMethodDecl *OverriderMD = Overrider.Method;
1696 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1697 FirstBaseInPrimaryBaseChain,
1698 FirstBaseOffsetInLayoutClass)) {
Anders Carlsson94464812010-04-10 19:13:06 +00001699 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001700 continue;
1701 }
Anders Carlsson464c8f12010-02-23 04:26:39 +00001702
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001703 // Check if this overrider needs a return adjustment.
Anders Carlsson7097e752010-06-01 23:47:05 +00001704 // We don't want to do this for pure virtual member functions.
1705 BaseOffset ReturnAdjustmentOffset;
1706 if (!OverriderMD->isPure()) {
1707 ReturnAdjustmentOffset =
1708 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1709 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001710
1711 ReturnAdjustment ReturnAdjustment =
1712 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001713
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001714 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001715 }
1716}
1717
Anders Carlsson718d0692010-04-11 22:07:06 +00001718void VTableBuilder::LayoutVTable() {
1719 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, 0),
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001720 /*BaseIsMorallyVirtual=*/false,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001721 MostDerivedClassIsVirtual,
1722 MostDerivedClassOffset);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001723
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001724 VisitedVirtualBasesSetTy VBases;
Anders Carlsson08c26752010-02-27 04:05:52 +00001725
1726 // Determine the primary virtual bases.
Anders Carlssond7fdae52010-03-10 03:02:01 +00001727 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1728 VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001729 VBases.clear();
1730
Anders Carlsson718d0692010-04-11 22:07:06 +00001731 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001732}
1733
Anders Carlsson530c40c2010-02-28 01:43:58 +00001734void
Anders Carlsson718d0692010-04-11 22:07:06 +00001735VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001736 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001737 bool BaseIsVirtualInLayoutClass,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001738 uint64_t OffsetInLayoutClass) {
Chris Lattner04a23112010-02-17 21:42:34 +00001739 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
Anders Carlssonb828afa2010-02-14 17:05:59 +00001740
Anders Carlsson852213e2010-02-16 04:59:55 +00001741 // Add vcall and vbase offsets for this vtable.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001742 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
Anders Carlsson91232842010-04-11 22:03:57 +00001743 Base, BaseIsVirtualInLayoutClass,
1744 OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001745 Components.append(Builder.components_begin(), Builder.components_end());
1746
Anders Carlssondba6ef82010-02-25 22:23:13 +00001747 // Check if we need to add these vcall offsets.
Anders Carlsson91232842010-04-11 22:03:57 +00001748 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
Anders Carlssondba6ef82010-02-25 22:23:13 +00001749 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1750
1751 if (VCallOffsets.empty())
1752 VCallOffsets = Builder.getVCallOffsets();
1753 }
1754
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001755 // If we're laying out the most derived class we want to keep track of the
1756 // virtual base class offset offsets.
1757 if (Base.getBase() == MostDerivedClass)
1758 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1759
Anders Carlsson852213e2010-02-16 04:59:55 +00001760 // Add the offset to top.
Anders Carlssone67dc302010-02-14 00:16:19 +00001761 // FIXME: We should not use / 8 here.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001762 int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass -
1763 MostDerivedClassOffset) / 8;
Anders Carlsson94464812010-04-10 19:13:06 +00001764 Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
Anders Carlsson57071e22010-02-12 05:25:12 +00001765
1766 // Next, add the RTTI.
Anders Carlsson94464812010-04-10 19:13:06 +00001767 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Anders Carlsson57071e22010-02-12 05:25:12 +00001768
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001769 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +00001770
1771 // Now go through all virtual member functions and add them.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001772 PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlsson60396612010-03-10 06:51:42 +00001773 AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass,
1774 PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001775
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001776 // Compute 'this' pointer adjustments.
1777 ComputeThisAdjustments();
1778
Anders Carlsson014a3582010-03-25 21:45:14 +00001779 // Add all address points.
1780 const CXXRecordDecl *RD = Base.getBase();
1781 while (true) {
1782 AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
1783 AddressPoint));
1784
1785 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1786 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001787
Anders Carlsson014a3582010-03-25 21:45:14 +00001788 if (!PrimaryBase)
1789 break;
1790
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001791 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson014a3582010-03-25 21:45:14 +00001792 // Check if this virtual primary base is a primary base in the layout
1793 // class. If it's not, we don't want to add it.
1794 const ASTRecordLayout &LayoutClassLayout =
1795 Context.getASTRecordLayout(LayoutClass);
1796
Anders Carlssona14f5972010-10-31 23:22:37 +00001797 if (LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase) !=
Anders Carlsson014a3582010-03-25 21:45:14 +00001798 OffsetInLayoutClass) {
1799 // We don't want to add this class (or any of its primary bases).
1800 break;
1801 }
1802 }
1803
1804 RD = PrimaryBase;
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001805 }
1806
Anders Carlsson5edcc372010-02-24 16:43:12 +00001807 // Layout secondary vtables.
Anders Carlsson718d0692010-04-11 22:07:06 +00001808 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001809}
1810
Anders Carlsson718d0692010-04-11 22:07:06 +00001811void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
Anders Carlssona96a2e92010-03-10 19:39:11 +00001812 bool BaseIsMorallyVirtual,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001813 uint64_t OffsetInLayoutClass) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001814 // Itanium C++ ABI 2.5.2:
1815 // Following the primary virtual table of a derived class are secondary
1816 // virtual tables for each of its proper base classes, except any primary
1817 // base(s) with which it shares its primary virtual table.
1818
1819 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001820 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1821 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc7b63162010-02-16 16:02:57 +00001822
Anders Carlsson57071e22010-02-12 05:25:12 +00001823 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1824 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001825 // Ignore virtual bases, we'll emit them later.
1826 if (I->isVirtual())
1827 continue;
1828
Anders Carlsson57071e22010-02-12 05:25:12 +00001829 const CXXRecordDecl *BaseDecl =
1830 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb828afa2010-02-14 17:05:59 +00001831
Anders Carlssona661a542010-02-16 04:49:44 +00001832 // Ignore bases that don't have a vtable.
1833 if (!BaseDecl->isDynamicClass())
1834 continue;
Anders Carlssonb828afa2010-02-14 17:05:59 +00001835
Anders Carlsson718d0692010-04-11 22:07:06 +00001836 if (isBuildingConstructorVTable()) {
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001837 // Itanium C++ ABI 2.6.4:
1838 // Some of the base class subobjects may not need construction virtual
1839 // tables, which will therefore not be present in the construction
1840 // virtual table group, even though the subobject virtual tables are
1841 // present in the main virtual table group for the complete object.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001842 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001843 continue;
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001844 }
1845
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001846 // Get the base offset of this base.
Anders Carlssona14f5972010-10-31 23:22:37 +00001847 uint64_t RelativeBaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl);
Anders Carlsson530c40c2010-02-28 01:43:58 +00001848 uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1849
1850 uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset;
Anders Carlssonc7b63162010-02-16 16:02:57 +00001851
1852 // Don't emit a secondary vtable for a primary base. We might however want
1853 // to emit secondary vtables for other bases of this base.
1854 if (BaseDecl == PrimaryBase) {
Anders Carlsson718d0692010-04-11 22:07:06 +00001855 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
Anders Carlssona96a2e92010-03-10 19:39:11 +00001856 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001857 continue;
1858 }
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001859
Anders Carlsson89ad4212010-02-16 16:26:28 +00001860 // Layout the primary vtable (and any secondary vtables) for this base.
Anders Carlsson718d0692010-04-11 22:07:06 +00001861 LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001862 BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001863 /*BaseIsVirtualInLayoutClass=*/false,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001864 BaseOffsetInLayoutClass);
Anders Carlsson57071e22010-02-12 05:25:12 +00001865 }
1866}
1867
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001868void
Anders Carlsson94464812010-04-10 19:13:06 +00001869VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Anders Carlssond7fdae52010-03-10 03:02:01 +00001870 uint64_t OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001871 VisitedVirtualBasesSetTy &VBases) {
1872 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1873
1874 // Check if this base has a primary base.
1875 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001876
1877 // Check if it's virtual.
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001878 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001879 bool IsPrimaryVirtualBase = true;
1880
Anders Carlsson718d0692010-04-11 22:07:06 +00001881 if (isBuildingConstructorVTable()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001882 // Check if the base is actually a primary base in the class we use for
1883 // layout.
Anders Carlssond7fdae52010-03-10 03:02:01 +00001884 const ASTRecordLayout &LayoutClassLayout =
1885 Context.getASTRecordLayout(LayoutClass);
1886
1887 uint64_t PrimaryBaseOffsetInLayoutClass =
Anders Carlssona14f5972010-10-31 23:22:37 +00001888 LayoutClassLayout.getVBaseClassOffsetInBits(PrimaryBase);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001889
1890 // We know that the base is not a primary base in the layout class if
1891 // the base offsets are different.
1892 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
Anders Carlsson0378bf02010-02-28 18:08:38 +00001893 IsPrimaryVirtualBase = false;
1894 }
1895
1896 if (IsPrimaryVirtualBase)
1897 PrimaryVirtualBases.insert(PrimaryBase);
1898 }
Anders Carlsson08c26752010-02-27 04:05:52 +00001899 }
1900
1901 // Traverse bases, looking for more primary virtual bases.
1902 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1903 E = RD->bases_end(); I != E; ++I) {
1904 const CXXRecordDecl *BaseDecl =
1905 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1906
Anders Carlssond7fdae52010-03-10 03:02:01 +00001907 uint64_t BaseOffsetInLayoutClass;
1908
1909 if (I->isVirtual()) {
1910 if (!VBases.insert(BaseDecl))
1911 continue;
1912
1913 const ASTRecordLayout &LayoutClassLayout =
1914 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001915
Anders Carlssona14f5972010-10-31 23:22:37 +00001916 BaseOffsetInLayoutClass =
1917 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001918 } else {
1919 BaseOffsetInLayoutClass =
Anders Carlssona14f5972010-10-31 23:22:37 +00001920 OffsetInLayoutClass + Layout.getBaseClassOffsetInBits(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001921 }
1922
1923 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001924 }
1925}
1926
1927void
Anders Carlsson718d0692010-04-11 22:07:06 +00001928VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001929 VisitedVirtualBasesSetTy &VBases) {
1930 // Itanium C++ ABI 2.5.2:
1931 // Then come the virtual base virtual tables, also in inheritance graph
1932 // order, and again excluding primary bases (which share virtual tables with
1933 // the classes for which they are primary).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001934 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1935 E = RD->bases_end(); I != E; ++I) {
1936 const CXXRecordDecl *BaseDecl =
1937 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1938
Anders Carlssonaf280c02010-02-23 03:48:14 +00001939 // Check if this base needs a vtable. (If it's virtual, not a primary base
1940 // of some other class, and we haven't visited it before).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001941 if (I->isVirtual() && BaseDecl->isDynamicClass() &&
Anders Carlsson08c26752010-02-27 04:05:52 +00001942 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001943 const ASTRecordLayout &MostDerivedClassLayout =
1944 Context.getASTRecordLayout(MostDerivedClass);
1945 uint64_t BaseOffset =
Anders Carlssona14f5972010-10-31 23:22:37 +00001946 MostDerivedClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001947
Anders Carlsson530c40c2010-02-28 01:43:58 +00001948 const ASTRecordLayout &LayoutClassLayout =
1949 Context.getASTRecordLayout(LayoutClass);
1950 uint64_t BaseOffsetInLayoutClass =
Anders Carlssona14f5972010-10-31 23:22:37 +00001951 LayoutClassLayout.getVBaseClassOffsetInBits(BaseDecl);
Anders Carlsson530c40c2010-02-28 01:43:58 +00001952
Anders Carlsson718d0692010-04-11 22:07:06 +00001953 LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001954 /*BaseIsMorallyVirtual=*/true,
1955 /*BaseIsVirtualInLayoutClass=*/true,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001956 BaseOffsetInLayoutClass);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001957 }
1958
1959 // We only need to check the base for virtual base vtables if it actually
1960 // has virtual bases.
1961 if (BaseDecl->getNumVBases())
Anders Carlsson718d0692010-04-11 22:07:06 +00001962 LayoutVTablesForVirtualBases(BaseDecl, VBases);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001963 }
1964}
1965
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001966/// dumpLayout - Dump the vtable layout.
Anders Carlsson94464812010-04-10 19:13:06 +00001967void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001968
Anders Carlsson718d0692010-04-11 22:07:06 +00001969 if (isBuildingConstructorVTable()) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001970 Out << "Construction vtable for ('";
1971 Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
1972 // FIXME: Don't use / 8 .
1973 Out << MostDerivedClassOffset / 8 << ") in '";
1974 Out << LayoutClass->getQualifiedNameAsString();
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001975 } else {
1976 Out << "Vtable for '";
1977 Out << MostDerivedClass->getQualifiedNameAsString();
Anders Carlssond03a3262010-02-27 20:39:05 +00001978 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001979 Out << "' (" << Components.size() << " entries).\n";
1980
Anders Carlsson822307b2010-02-11 17:18:51 +00001981 // Iterate through the address points and insert them into a new map where
1982 // they are keyed by the index and not the base object.
1983 // Since an address point can be shared by multiple subobjects, we use an
1984 // STL multimap.
1985 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
Anders Carlssonff143f82010-03-25 00:35:49 +00001986 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
1987 E = AddressPoints.end(); I != E; ++I) {
Anders Carlsson822307b2010-02-11 17:18:51 +00001988 const BaseSubobject& Base = I->first;
1989 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001990
Anders Carlsson822307b2010-02-11 17:18:51 +00001991 AddressPointsByIndex.insert(std::make_pair(Index, Base));
1992 }
1993
1994 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001995 uint64_t Index = I;
Anders Carlsson822307b2010-02-11 17:18:51 +00001996
1997 Out << llvm::format("%4d | ", I);
1998
Anders Carlsson94464812010-04-10 19:13:06 +00001999 const VTableComponent &Component = Components[I];
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002000
2001 // Dump the component.
2002 switch (Component.getKind()) {
Anders Carlssond9a67042010-02-17 06:07:19 +00002003
Anders Carlsson94464812010-04-10 19:13:06 +00002004 case VTableComponent::CK_VCallOffset:
Anders Carlssond9a67042010-02-17 06:07:19 +00002005 Out << "vcall_offset (" << Component.getVCallOffset() << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002006 break;
Anders Carlsson852213e2010-02-16 04:59:55 +00002007
Anders Carlsson94464812010-04-10 19:13:06 +00002008 case VTableComponent::CK_VBaseOffset:
Anders Carlsson852213e2010-02-16 04:59:55 +00002009 Out << "vbase_offset (" << Component.getVBaseOffset() << ")";
2010 break;
2011
Anders Carlsson94464812010-04-10 19:13:06 +00002012 case VTableComponent::CK_OffsetToTop:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002013 Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
2014 break;
2015
Anders Carlsson94464812010-04-10 19:13:06 +00002016 case VTableComponent::CK_RTTI:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002017 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2018 break;
2019
Anders Carlsson94464812010-04-10 19:13:06 +00002020 case VTableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002021 const CXXMethodDecl *MD = Component.getFunctionDecl();
2022
Anders Carlsson848fa642010-02-11 18:20:28 +00002023 std::string Str =
2024 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2025 MD);
2026 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +00002027 if (MD->isPure())
2028 Out << " [pure]";
2029
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002030 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002031 if (!Thunk.isEmpty()) {
2032 // If this function pointer has a return adjustment, dump it.
2033 if (!Thunk.Return.isEmpty()) {
2034 Out << "\n [return adjustment: ";
2035 Out << Thunk.Return.NonVirtual << " non-virtual";
2036
2037 if (Thunk.Return.VBaseOffsetOffset) {
2038 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2039 Out << " vbase offset offset";
2040 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002041
Anders Carlssondd366812010-03-12 04:54:20 +00002042 Out << ']';
2043 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002044
Anders Carlssondd366812010-03-12 04:54:20 +00002045 // If this function pointer has a 'this' pointer adjustment, dump it.
2046 if (!Thunk.This.isEmpty()) {
2047 Out << "\n [this adjustment: ";
2048 Out << Thunk.This.NonVirtual << " non-virtual";
2049
2050 if (Thunk.This.VCallOffsetOffset) {
2051 Out << ", " << Thunk.This.VCallOffsetOffset;
2052 Out << " vcall offset offset";
2053 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002054
Anders Carlssondd366812010-03-12 04:54:20 +00002055 Out << ']';
2056 }
Anders Carlsson49bac9a2010-02-13 23:40:17 +00002057 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002058
Anders Carlsson848fa642010-02-11 18:20:28 +00002059 break;
2060 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002061
Anders Carlsson94464812010-04-10 19:13:06 +00002062 case VTableComponent::CK_CompleteDtorPointer:
2063 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6065e022010-02-19 06:03:53 +00002064 bool IsComplete =
Anders Carlsson94464812010-04-10 19:13:06 +00002065 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
Anders Carlsson6065e022010-02-19 06:03:53 +00002066
Anders Carlsson848fa642010-02-11 18:20:28 +00002067 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2068
Anders Carlsson6065e022010-02-19 06:03:53 +00002069 Out << DD->getQualifiedNameAsString();
2070 if (IsComplete)
2071 Out << "() [complete]";
2072 else
2073 Out << "() [deleting]";
2074
Anders Carlsson98241422010-02-12 02:38:13 +00002075 if (DD->isPure())
2076 Out << " [pure]";
2077
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002078 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002079 if (!Thunk.isEmpty()) {
2080 // If this destructor has a 'this' pointer adjustment, dump it.
2081 if (!Thunk.This.isEmpty()) {
2082 Out << "\n [this adjustment: ";
2083 Out << Thunk.This.NonVirtual << " non-virtual";
2084
2085 if (Thunk.This.VCallOffsetOffset) {
2086 Out << ", " << Thunk.This.VCallOffsetOffset;
2087 Out << " vcall offset offset";
2088 }
2089
2090 Out << ']';
2091 }
2092 }
Anders Carlsson6065e022010-02-19 06:03:53 +00002093
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002094 break;
2095 }
2096
Anders Carlsson94464812010-04-10 19:13:06 +00002097 case VTableComponent::CK_UnusedFunctionPointer: {
Anders Carlsson1f464662010-02-19 20:08:13 +00002098 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2099
2100 std::string Str =
2101 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2102 MD);
2103 Out << "[unused] " << Str;
2104 if (MD->isPure())
2105 Out << " [pure]";
2106 }
2107
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002108 }
Anders Carlsson822307b2010-02-11 17:18:51 +00002109
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002110 Out << '\n';
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002111
2112 // Dump the next address point.
2113 uint64_t NextIndex = Index + 1;
2114 if (AddressPointsByIndex.count(NextIndex)) {
2115 if (AddressPointsByIndex.count(NextIndex) == 1) {
2116 const BaseSubobject &Base =
2117 AddressPointsByIndex.find(NextIndex)->second;
2118
2119 // FIXME: Instead of dividing by 8, we should be using CharUnits.
2120 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
2121 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
2122 } else {
2123 uint64_t BaseOffset =
2124 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2125
2126 // We store the class names in a set to get a stable order.
2127 std::set<std::string> ClassNames;
2128 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2129 AddressPointsByIndex.lower_bound(NextIndex), E =
2130 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2131 assert(I->second.getBaseOffset() == BaseOffset &&
2132 "Invalid base offset!");
2133 const CXXRecordDecl *RD = I->second.getBase();
2134 ClassNames.insert(RD->getQualifiedNameAsString());
2135 }
2136
2137 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2138 E = ClassNames.end(); I != E; ++I) {
2139 // FIXME: Instead of dividing by 8, we should be using CharUnits.
2140 Out << " -- (" << *I;
2141 Out << ", " << BaseOffset / 8 << ") vtable address --\n";
2142 }
2143 }
2144 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002145 }
Anders Carlssond03a3262010-02-27 20:39:05 +00002146
2147 Out << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002148
Anders Carlsson718d0692010-04-11 22:07:06 +00002149 if (isBuildingConstructorVTable())
Anders Carlsson9135a842010-03-18 02:44:19 +00002150 return;
2151
2152 if (MostDerivedClass->getNumVBases()) {
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002153 // We store the virtual base class names and their offsets in a map to get
2154 // a stable order.
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002155
Anders Carlsson9135a842010-03-18 02:44:19 +00002156 std::map<std::string, int64_t> ClassNamesAndOffsets;
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002157 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2158 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2159 std::string ClassName = I->first->getQualifiedNameAsString();
2160 int64_t OffsetOffset = I->second;
2161 ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
2162 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002163
2164 Out << "Virtual base offset offsets for '";
2165 Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2166 Out << ClassNamesAndOffsets.size();
2167 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002168
2169 for (std::map<std::string, int64_t>::const_iterator I =
2170 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2171 I != E; ++I)
2172 Out << " " << I->first << " | " << I->second << '\n';
2173
2174 Out << "\n";
2175 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002176
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002177 if (!Thunks.empty()) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002178 // We store the method names in a map to get a stable order.
2179 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2180
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002181 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002182 I != E; ++I) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002183 const CXXMethodDecl *MD = I->first;
2184 std::string MethodName =
2185 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2186 MD);
2187
2188 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2189 }
2190
2191 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2192 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2193 I != E; ++I) {
2194 const std::string &MethodName = I->first;
2195 const CXXMethodDecl *MD = I->second;
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002196
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002197 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002198 std::sort(ThunksVector.begin(), ThunksVector.end());
Anders Carlsson9135a842010-03-18 02:44:19 +00002199
2200 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2201 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2202
2203 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2204 const ThunkInfo &Thunk = ThunksVector[I];
2205
2206 Out << llvm::format("%4d | ", I);
2207
Anders Carlsson09daa502010-03-22 16:10:59 +00002208 // If this function pointer has a return pointer adjustment, dump it.
2209 if (!Thunk.Return.isEmpty()) {
2210 Out << "return adjustment: " << Thunk.This.NonVirtual;
2211 Out << " non-virtual";
2212 if (Thunk.Return.VBaseOffsetOffset) {
2213 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2214 Out << " vbase offset offset";
2215 }
2216
2217 if (!Thunk.This.isEmpty())
2218 Out << "\n ";
2219 }
2220
Anders Carlsson9135a842010-03-18 02:44:19 +00002221 // If this function pointer has a 'this' pointer adjustment, dump it.
2222 if (!Thunk.This.isEmpty()) {
Anders Carlsson09daa502010-03-22 16:10:59 +00002223 Out << "this adjustment: ";
2224 Out << Thunk.This.NonVirtual << " non-virtual";
Anders Carlsson9135a842010-03-18 02:44:19 +00002225
2226 if (Thunk.This.VCallOffsetOffset) {
2227 Out << ", " << Thunk.This.VCallOffsetOffset;
Anders Carlsson09daa502010-03-22 16:10:59 +00002228 Out << " vcall offset offset";
Anders Carlsson9135a842010-03-18 02:44:19 +00002229 }
2230 }
2231
2232 Out << '\n';
2233 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00002234
2235 Out << '\n';
2236
Anders Carlsson9135a842010-03-18 02:44:19 +00002237 }
2238 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002239}
2240
2241}
2242
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002243static void
2244CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
2245 VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) {
2246 while (RD) {
2247 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2248 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2249 if (PrimaryBase)
2250 PrimaryBases.insert(PrimaryBase);
2251
2252 RD = PrimaryBase;
2253 }
2254}
2255
Anders Carlsson046c2942010-04-17 20:15:18 +00002256void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002257
2258 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00002259 // The order of the virtual function pointers in a virtual table is the
2260 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002261 //
Anders Carlsson45147d02010-02-02 03:37:46 +00002262 // There is an entry for any virtual function declared in a class,
2263 // whether it is a new function or overrides a base class function,
2264 // unless it overrides a function from the primary base, and conversion
2265 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002266
2267 int64_t CurrentIndex = 0;
2268
2269 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2270 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2271
2272 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00002273 assert(PrimaryBase->isDefinition() &&
2274 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002275
2276 // Since the record decl shares its vtable pointer with the primary base
2277 // we need to start counting at the end of the primary base's vtable.
2278 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2279 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00002280
2281 // Collect all the primary bases, so we can check whether methods override
2282 // a method from the base.
Anders Carlsson94464812010-04-10 19:13:06 +00002283 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002284 CollectPrimaryBases(RD, CGM.getContext(), PrimaryBases);
Eli Friedmanea5ae312009-12-15 03:31:17 +00002285
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002286 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2287
2288 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2289 e = RD->method_end(); i != e; ++i) {
2290 const CXXMethodDecl *MD = *i;
2291
2292 // We only want virtual methods.
2293 if (!MD->isVirtual())
2294 continue;
2295
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002296 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002297 if (const CXXMethodDecl *OverriddenMD =
Anders Carlssonf622b452010-04-10 20:39:29 +00002298 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002299 // Check if converting from the return type of the method to the
2300 // return type of the overridden method requires conversion.
Anders Carlssondf31af12010-02-13 23:17:31 +00002301 if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
2302 OverriddenMD).isEmpty()) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002303 // This index is shared between the index in the vtable of the primary
2304 // base class.
2305 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2306 const CXXDestructorDecl *OverriddenDD =
2307 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002308
Anders Carlsson57071e22010-02-12 05:25:12 +00002309 // Add both the complete and deleting entries.
Anders Carlsson046c2942010-04-17 20:15:18 +00002310 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] =
2311 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2312 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2313 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
Anders Carlsson57071e22010-02-12 05:25:12 +00002314 } else {
Anders Carlsson046c2942010-04-17 20:15:18 +00002315 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
Anders Carlsson57071e22010-02-12 05:25:12 +00002316 }
2317
2318 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00002319 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002320 }
2321 }
2322
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002323 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2324 if (MD->isImplicit()) {
2325 assert(!ImplicitVirtualDtor &&
2326 "Did already see an implicit virtual dtor!");
2327 ImplicitVirtualDtor = DD;
2328 continue;
2329 }
Tilmann Schellerca0df922011-03-02 20:40:50 +00002330
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002331 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002332 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002333
2334 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002335 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002336 } else {
2337 // Add the entry.
Tilmann Schellerca0df922011-03-02 20:40:50 +00002338 MethodVTableIndices[MD] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002339 }
2340 }
2341
2342 if (ImplicitVirtualDtor) {
2343 // Itanium C++ ABI 2.5.2:
Anders Carlssona661a542010-02-16 04:49:44 +00002344 // If a class has an implicitly-defined virtual destructor,
2345 // its entries come after the declared virtual function pointers.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002346
2347 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002348 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002349 CurrentIndex++;
2350
2351 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002352 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002353 CurrentIndex++;
2354 }
2355
2356 NumVirtualFunctionPointers[RD] = CurrentIndex;
2357}
2358
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002359bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
2360 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
2361
2362 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2363 if (TSK == TSK_ExplicitInstantiationDeclaration)
2364 return false;
2365
2366 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
2367 if (!KeyFunction)
2368 return true;
2369
2370 // Itanium C++ ABI, 5.2.6 Instantiated Templates:
2371 // An instantiation of a class template requires:
2372 // - In the object where instantiated, the virtual table...
2373 if (TSK == TSK_ImplicitInstantiation ||
2374 TSK == TSK_ExplicitInstantiationDefinition)
2375 return true;
2376
Anders Carlsson6d7f8472011-01-30 20:45:54 +00002377 // If we're building with optimization, we always emit VTables since that
2378 // allows for virtual function calls to be devirtualized.
2379 // (We don't want to do this in -fapple-kext mode however).
2380 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOptions().AppleKext)
2381 return true;
2382
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002383 return KeyFunction->hasBody();
2384}
2385
Anders Carlssonaf440352010-03-23 04:11:45 +00002386uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002387 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2388 NumVirtualFunctionPointers.find(RD);
2389 if (I != NumVirtualFunctionPointers.end())
2390 return I->second;
2391
Anders Carlsson046c2942010-04-17 20:15:18 +00002392 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002393
2394 I = NumVirtualFunctionPointers.find(RD);
2395 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2396 return I->second;
2397}
2398
Anders Carlsson046c2942010-04-17 20:15:18 +00002399uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2400 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2401 if (I != MethodVTableIndices.end())
Anders Carlssondbd920c2009-10-11 22:13:54 +00002402 return I->second;
2403
Anders Carlssona0fdd912009-11-13 17:08:56 +00002404 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002405
Anders Carlsson046c2942010-04-17 20:15:18 +00002406 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002407
Anders Carlsson046c2942010-04-17 20:15:18 +00002408 I = MethodVTableIndices.find(GD);
2409 assert(I != MethodVTableIndices.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002410 return I->second;
2411}
2412
Anders Carlssonaf440352010-03-23 04:11:45 +00002413int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2414 const CXXRecordDecl *VBase) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002415 ClassPairTy ClassPair(RD, VBase);
2416
Anders Carlssonbba16072010-03-11 07:15:17 +00002417 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2418 VirtualBaseClassOffsetOffsets.find(ClassPair);
2419 if (I != VirtualBaseClassOffsetOffsets.end())
Anders Carlssondbd920c2009-10-11 22:13:54 +00002420 return I->second;
2421
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002422 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
2423 BaseSubobject(RD, 0),
2424 /*BaseIsVirtual=*/false,
2425 /*OffsetInLayoutClass=*/0);
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002426
2427 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2428 Builder.getVBaseOffsetOffsets().begin(),
2429 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002430 // Insert all types.
2431 ClassPairTy ClassPair(RD, I->first);
2432
Anders Carlssonbba16072010-03-11 07:15:17 +00002433 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002434 }
2435
Anders Carlssonbba16072010-03-11 07:15:17 +00002436 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
Anders Carlssonbba16072010-03-11 07:15:17 +00002437 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002438
2439 return I->second;
2440}
2441
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002442uint64_t
2443CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
Anders Carlssonec9c2022010-04-08 16:18:36 +00002444 assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2445 "Did not find address point!");
2446
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002447 uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002448 assert(AddressPoint && "Address point must not be zero!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002449
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002450 return AddressPoint;
2451}
2452
Anders Carlsson19879c92010-03-23 17:17:29 +00002453llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
Anders Carlsson84c49e42011-02-06 17:15:43 +00002454 const ThunkInfo &Thunk) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002455 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2456
2457 // Compute the mangled name.
2458 llvm::SmallString<256> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002459 llvm::raw_svector_ostream Out(Name);
Anders Carlsson19879c92010-03-23 17:17:29 +00002460 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
John McCall4c40d982010-08-31 07:33:07 +00002461 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002462 Thunk.This, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002463 else
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002464 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
2465 Out.flush();
2466
John McCall4c40d982010-08-31 07:33:07 +00002467 const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson84c49e42011-02-06 17:15:43 +00002468 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
Anders Carlsson19879c92010-03-23 17:17:29 +00002469}
2470
Anders Carlsson519c3282010-03-24 00:39:18 +00002471static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2472 llvm::Value *Ptr,
2473 int64_t NonVirtualAdjustment,
2474 int64_t VirtualAdjustment) {
2475 if (!NonVirtualAdjustment && !VirtualAdjustment)
2476 return Ptr;
2477
2478 const llvm::Type *Int8PtrTy =
2479 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2480
2481 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2482
2483 if (NonVirtualAdjustment) {
2484 // Do the non-virtual adjustment.
2485 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2486 }
2487
2488 if (VirtualAdjustment) {
2489 const llvm::Type *PtrDiffTy =
2490 CGF.ConvertType(CGF.getContext().getPointerDiffType());
2491
2492 // Do the virtual adjustment.
2493 llvm::Value *VTablePtrPtr =
2494 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2495
2496 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2497
2498 llvm::Value *OffsetPtr =
2499 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2500
2501 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2502
2503 // Load the adjustment offset from the vtable.
2504 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2505
2506 // Adjust our pointer.
2507 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2508 }
2509
2510 // Cast back to the original type.
2511 return CGF.Builder.CreateBitCast(V, Ptr->getType());
2512}
2513
John McCall65005532010-08-04 23:46:35 +00002514static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
2515 const ThunkInfo &Thunk, llvm::Function *Fn) {
Anders Carlsson0ffeaad2011-01-29 19:39:23 +00002516 CGM.setGlobalVisibility(Fn, MD);
John McCall65005532010-08-04 23:46:35 +00002517
John McCall279b5eb2010-08-12 23:36:15 +00002518 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
2519 return;
2520
John McCall65005532010-08-04 23:46:35 +00002521 // If the thunk has weak/linkonce linkage, but the function must be
2522 // emitted in every translation unit that references it, then we can
2523 // emit its thunks with hidden visibility, since its thunks must be
2524 // emitted when the function is.
2525
John McCall7a536902010-08-05 20:39:18 +00002526 // This follows CodeGenModule::setTypeVisibility; see the comments
2527 // there for explanation.
John McCall65005532010-08-04 23:46:35 +00002528
2529 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
2530 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
2531 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
2532 return;
2533
John McCall65005532010-08-04 23:46:35 +00002534 if (MD->hasAttr<VisibilityAttr>())
2535 return;
2536
2537 switch (MD->getTemplateSpecializationKind()) {
John McCall65005532010-08-04 23:46:35 +00002538 case TSK_ExplicitInstantiationDefinition:
2539 case TSK_ExplicitInstantiationDeclaration:
2540 return;
2541
John McCall65005532010-08-04 23:46:35 +00002542 case TSK_Undeclared:
2543 break;
2544
John McCall7a536902010-08-05 20:39:18 +00002545 case TSK_ExplicitSpecialization:
John McCall65005532010-08-04 23:46:35 +00002546 case TSK_ImplicitInstantiation:
John McCall279b5eb2010-08-12 23:36:15 +00002547 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
John McCall7a536902010-08-05 20:39:18 +00002548 return;
John McCall65005532010-08-04 23:46:35 +00002549 break;
2550 }
2551
2552 // If there's an explicit definition, and that definition is
2553 // out-of-line, then we can't assume that all users will have a
2554 // definition to emit.
2555 const FunctionDecl *Def = 0;
2556 if (MD->hasBody(Def) && Def->isOutOfLine())
2557 return;
2558
2559 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2560}
2561
Anders Carlsson519c3282010-03-24 00:39:18 +00002562void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
2563 const ThunkInfo &Thunk) {
2564 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2565 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2566 QualType ResultType = FPT->getResultType();
2567 QualType ThisType = MD->getThisType(getContext());
2568
2569 FunctionArgList FunctionArgs;
2570
2571 // FIXME: It would be nice if more of this code could be shared with
2572 // CodeGenFunction::GenerateCode.
2573
2574 // Create the implicit 'this' parameter declaration.
John McCall4c40d982010-08-31 07:33:07 +00002575 CurGD = GD;
2576 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
Anders Carlsson519c3282010-03-24 00:39:18 +00002577
2578 // Add the rest of the parameters.
2579 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2580 E = MD->param_end(); I != E; ++I) {
2581 ParmVarDecl *Param = *I;
2582
2583 FunctionArgs.push_back(std::make_pair(Param, Param->getType()));
2584 }
Tilmann Scheller88d117c2011-03-02 19:36:23 +00002585
2586 const bool IsWin64 = CGM.getContext().Target.isWin64();
2587
2588 StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation(),
2589 IsWin64 ? CC_Win64ThisCall : CC_Default);
Anders Carlsson519c3282010-03-24 00:39:18 +00002590
John McCall4c40d982010-08-31 07:33:07 +00002591 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2592
Anders Carlsson519c3282010-03-24 00:39:18 +00002593 // Adjust the 'this' pointer if necessary.
2594 llvm::Value *AdjustedThisPtr =
2595 PerformTypeAdjustment(*this, LoadCXXThis(),
2596 Thunk.This.NonVirtual,
2597 Thunk.This.VCallOffsetOffset);
2598
2599 CallArgList CallArgs;
2600
2601 // Add our adjusted 'this' pointer.
2602 CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
2603
2604 // Add the rest of the parameters.
2605 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2606 E = MD->param_end(); I != E; ++I) {
2607 ParmVarDecl *Param = *I;
2608 QualType ArgType = Param->getType();
John McCall27360712010-05-26 22:34:26 +00002609 RValue Arg = EmitDelegateCallArg(Param);
Douglas Gregor663218b2010-05-21 17:55:12 +00002610
2611 CallArgs.push_back(std::make_pair(Arg, ArgType));
Anders Carlsson519c3282010-03-24 00:39:18 +00002612 }
2613
2614 // Get our callee.
2615 const llvm::Type *Ty =
John McCall4c40d982010-08-31 07:33:07 +00002616 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
Anders Carlsson519c3282010-03-24 00:39:18 +00002617 FPT->isVariadic());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002618 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson519c3282010-03-24 00:39:18 +00002619
Tilmann Scheller88d117c2011-03-02 19:36:23 +00002620 FunctionType::ExtInfo Info = FPT->getExtInfo();
2621
2622 if (IsWin64) {
2623 Info = Info.withCallingConv(CC_Win64ThisCall);
2624 }
2625
Anders Carlsson519c3282010-03-24 00:39:18 +00002626 const CGFunctionInfo &FnInfo =
Tilmann Scheller88d117c2011-03-02 19:36:23 +00002627 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, Info);
Anders Carlsson519c3282010-03-24 00:39:18 +00002628
Douglas Gregorcb359df2010-05-20 05:54:35 +00002629 // Determine whether we have a return value slot to use.
2630 ReturnValueSlot Slot;
2631 if (!ResultType->isVoidType() &&
2632 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2633 hasAggregateLLVMType(CurFnInfo->getReturnType()))
2634 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
2635
Anders Carlsson519c3282010-03-24 00:39:18 +00002636 // Now emit our call.
Douglas Gregorcb359df2010-05-20 05:54:35 +00002637 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
Anders Carlsson519c3282010-03-24 00:39:18 +00002638
2639 if (!Thunk.Return.isEmpty()) {
2640 // Emit the return adjustment.
2641 bool NullCheckValue = !ResultType->isReferenceType();
2642
2643 llvm::BasicBlock *AdjustNull = 0;
2644 llvm::BasicBlock *AdjustNotNull = 0;
2645 llvm::BasicBlock *AdjustEnd = 0;
2646
2647 llvm::Value *ReturnValue = RV.getScalarVal();
2648
2649 if (NullCheckValue) {
2650 AdjustNull = createBasicBlock("adjust.null");
2651 AdjustNotNull = createBasicBlock("adjust.notnull");
2652 AdjustEnd = createBasicBlock("adjust.end");
2653
2654 llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
2655 Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2656 EmitBlock(AdjustNotNull);
2657 }
2658
2659 ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
2660 Thunk.Return.NonVirtual,
2661 Thunk.Return.VBaseOffsetOffset);
2662
2663 if (NullCheckValue) {
2664 Builder.CreateBr(AdjustEnd);
2665 EmitBlock(AdjustNull);
2666 Builder.CreateBr(AdjustEnd);
2667 EmitBlock(AdjustEnd);
2668
2669 llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType());
2670 PHI->reserveOperandSpace(2);
2671 PHI->addIncoming(ReturnValue, AdjustNotNull);
2672 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2673 AdjustNull);
2674 ReturnValue = PHI;
2675 }
2676
2677 RV = RValue::get(ReturnValue);
2678 }
2679
Douglas Gregorcb359df2010-05-20 05:54:35 +00002680 if (!ResultType->isVoidType() && Slot.isNull())
John McCalld16c2cf2011-02-08 08:22:06 +00002681 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
Anders Carlsson519c3282010-03-24 00:39:18 +00002682
2683 FinishFunction();
2684
Anders Carlsson519c3282010-03-24 00:39:18 +00002685 // Set the right linkage.
John McCall8b242332010-05-25 04:30:21 +00002686 CGM.setFunctionLinkage(MD, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002687
2688 // Set the right visibility.
John McCall65005532010-08-04 23:46:35 +00002689 setThunkVisibility(CGM, MD, Thunk, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002690}
2691
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002692void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
2693 bool UseAvailableExternallyLinkage)
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002694{
Anders Carlsson84c49e42011-02-06 17:15:43 +00002695 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +00002696
Anders Carlsson7986ad52010-03-23 18:18:41 +00002697 // Strip off a bitcast if we got one back.
Anders Carlsson13d68982010-03-24 00:35:44 +00002698 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson7986ad52010-03-23 18:18:41 +00002699 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson13d68982010-03-24 00:35:44 +00002700 Entry = CE->getOperand(0);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002701 }
2702
Anders Carlsson7986ad52010-03-23 18:18:41 +00002703 // There's already a declaration with the same name, check if it has the same
2704 // type or if we need to replace it.
Anders Carlsson13d68982010-03-24 00:35:44 +00002705 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
John McCall4c40d982010-08-31 07:33:07 +00002706 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
Anders Carlsson13d68982010-03-24 00:35:44 +00002707 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002708
2709 // If the types mismatch then we have to rewrite the definition.
2710 assert(OldThunkFn->isDeclaration() &&
2711 "Shouldn't replace non-declaration");
2712
2713 // Remove the name from the old thunk function and get a new thunk.
2714 OldThunkFn->setName(llvm::StringRef());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002715 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002716
2717 // If needed, replace the old thunk with a bitcast.
2718 if (!OldThunkFn->use_empty()) {
2719 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson13d68982010-03-24 00:35:44 +00002720 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson7986ad52010-03-23 18:18:41 +00002721 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
2722 }
2723
2724 // Remove the old thunk.
2725 OldThunkFn->eraseFromParent();
2726 }
Anders Carlsson519c3282010-03-24 00:39:18 +00002727
Anders Carlsson519c3282010-03-24 00:39:18 +00002728 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002729
2730 if (!ThunkFn->isDeclaration()) {
2731 if (UseAvailableExternallyLinkage) {
2732 // There is already a thunk emitted for this function, do nothing.
2733 return;
2734 }
2735
Anders Carlsson22df7b12011-02-06 20:09:44 +00002736 // If a function has a body, it should have available_externally linkage.
2737 assert(ThunkFn->hasAvailableExternallyLinkage() &&
2738 "Function should have available_externally linkage!");
2739
2740 // Change the linkage.
2741 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
2742 return;
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002743 }
2744
2745 // Actually generate the thunk body.
Anders Carlsson519c3282010-03-24 00:39:18 +00002746 CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002747
2748 if (UseAvailableExternallyLinkage)
2749 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
2750}
2751
2752void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
2753 const ThunkInfo &Thunk) {
2754 // We only want to do this when building with optimizations.
2755 if (!CGM.getCodeGenOpts().OptimizationLevel)
2756 return;
2757
2758 // We can't emit thunks for member functions with incomplete types.
2759 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2760 if (CGM.getTypes().VerifyFuncTypeComplete(MD->getType().getTypePtr()))
2761 return;
2762
2763 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002764}
2765
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002766void CodeGenVTables::EmitThunks(GlobalDecl GD)
2767{
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002768 const CXXMethodDecl *MD =
2769 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2770
2771 // We don't need to generate thunks for the base destructor.
2772 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2773 return;
2774
2775 const CXXRecordDecl *RD = MD->getParent();
2776
Anders Carlssonccd83d72010-03-24 16:42:11 +00002777 // Compute VTable related info for this class.
John McCalle2132352010-06-02 21:22:02 +00002778 ComputeVTableRelatedInformation(RD, false);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002779
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002780 ThunksMapTy::const_iterator I = Thunks.find(MD);
2781 if (I == Thunks.end()) {
Anders Carlssonccd83d72010-03-24 16:42:11 +00002782 // We did not find a thunk for this method.
2783 return;
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002784 }
Anders Carlssonccd83d72010-03-24 16:42:11 +00002785
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002786 const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2787 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002788 EmitThunk(GD, ThunkInfoVector[I], /*UseAvailableExternallyLinkage=*/false);
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002789}
2790
John McCalle2132352010-06-02 21:22:02 +00002791void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
2792 bool RequireVTable) {
2793 VTableLayoutData &Entry = VTableLayoutMap[RD];
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002794
2795 // We may need to generate a definition for this vtable.
John McCalle2132352010-06-02 21:22:02 +00002796 if (RequireVTable && !Entry.getInt()) {
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002797 if (ShouldEmitVTableInThisTU(RD))
John McCalle2132352010-06-02 21:22:02 +00002798 CGM.DeferredVTables.push_back(RD);
2799
2800 Entry.setInt(true);
2801 }
2802
2803 // Check if we've computed this information before.
2804 if (Entry.getPointer())
2805 return;
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002806
Anders Carlsson94464812010-04-10 19:13:06 +00002807 VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002808
2809 // Add the VTable layout.
2810 uint64_t NumVTableComponents = Builder.getNumVTableComponents();
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002811 // -fapple-kext adds an extra entry at end of vtbl.
2812 bool IsAppleKext = CGM.getContext().getLangOptions().AppleKext;
2813 if (IsAppleKext)
2814 NumVTableComponents += 1;
2815
John McCalle2132352010-06-02 21:22:02 +00002816 uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1];
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002817 if (IsAppleKext)
2818 LayoutData[NumVTableComponents] = 0;
John McCalle2132352010-06-02 21:22:02 +00002819 Entry.setPointer(LayoutData);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002820
2821 // Store the number of components.
2822 LayoutData[0] = NumVTableComponents;
2823
2824 // Store the components.
2825 std::copy(Builder.vtable_components_data_begin(),
2826 Builder.vtable_components_data_end(),
2827 &LayoutData[1]);
2828
2829 // Add the known thunks.
2830 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2831
Anders Carlsson276701f2010-03-29 01:38:05 +00002832 // Add the thunks needed in this vtable.
2833 assert(!VTableThunksMap.count(RD) &&
2834 "Thunks already exists for this vtable!");
2835
2836 VTableThunksTy &VTableThunks = VTableThunksMap[RD];
2837 VTableThunks.append(Builder.vtable_thunks_begin(),
2838 Builder.vtable_thunks_end());
2839
2840 // Sort them.
2841 std::sort(VTableThunks.begin(), VTableThunks.end());
2842
Anders Carlsson66d567d2010-03-25 00:51:13 +00002843 // Add the address points.
Anders Carlsson94464812010-04-10 19:13:06 +00002844 for (VTableBuilder::AddressPointsMapTy::const_iterator I =
Anders Carlsson66d567d2010-03-25 00:51:13 +00002845 Builder.address_points_begin(), E = Builder.address_points_end();
2846 I != E; ++I) {
2847
2848 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
2849
2850 // Check if we already have the address points for this base.
2851 assert(!AddressPoint && "Address point already exists for this base!");
2852
2853 AddressPoint = I->second;
2854 }
2855
Anders Carlssonccd83d72010-03-24 16:42:11 +00002856 // If we don't have the vbase information for this class, insert it.
2857 // getVirtualBaseOffsetOffset will compute it separately without computing
2858 // the rest of the vtable related information.
2859 if (!RD->getNumVBases())
2860 return;
2861
2862 const RecordType *VBaseRT =
2863 RD->vbases_begin()->getType()->getAs<RecordType>();
2864 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
2865
2866 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2867 return;
2868
Anders Carlsson94464812010-04-10 19:13:06 +00002869 for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
Anders Carlssonccd83d72010-03-24 16:42:11 +00002870 Builder.getVBaseOffsetOffsets().begin(),
2871 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2872 // Insert all types.
2873 ClassPairTy ClassPair(RD, I->first);
2874
2875 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2876 }
2877}
2878
Anders Carlsson0d1407e2010-03-25 15:26:28 +00002879llvm::Constant *
2880CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
2881 const uint64_t *Components,
2882 unsigned NumComponents,
2883 const VTableThunksTy &VTableThunks) {
2884 llvm::SmallVector<llvm::Constant *, 64> Inits;
2885
2886 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2887
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002888 const llvm::Type *PtrDiffTy =
2889 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2890
2891 QualType ClassType = CGM.getContext().getTagDeclType(RD);
2892 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
2893
2894 unsigned NextVTableThunkIndex = 0;
2895
Anders Carlsson67d568a2010-03-29 05:40:50 +00002896 llvm::Constant* PureVirtualFn = 0;
2897
Anders Carlsson0d1407e2010-03-25 15:26:28 +00002898 for (unsigned I = 0; I != NumComponents; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00002899 VTableComponent Component =
2900 VTableComponent::getFromOpaqueInteger(Components[I]);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002901
2902 llvm::Constant *Init = 0;
2903
2904 switch (Component.getKind()) {
Anders Carlsson94464812010-04-10 19:13:06 +00002905 case VTableComponent::CK_VCallOffset:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002906 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset());
2907 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2908 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002909 case VTableComponent::CK_VBaseOffset:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002910 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset());
2911 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2912 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002913 case VTableComponent::CK_OffsetToTop:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002914 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop());
2915 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2916 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002917 case VTableComponent::CK_RTTI:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002918 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
2919 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002920 case VTableComponent::CK_FunctionPointer:
2921 case VTableComponent::CK_CompleteDtorPointer:
2922 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002923 GlobalDecl GD;
2924
2925 // Get the right global decl.
2926 switch (Component.getKind()) {
2927 default:
2928 llvm_unreachable("Unexpected vtable component kind");
Anders Carlsson94464812010-04-10 19:13:06 +00002929 case VTableComponent::CK_FunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002930 GD = Component.getFunctionDecl();
2931 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002932 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002933 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
2934 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002935 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002936 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
2937 break;
2938 }
2939
Anders Carlsson67d568a2010-03-29 05:40:50 +00002940 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
2941 // We have a pure virtual member function.
2942 if (!PureVirtualFn) {
2943 const llvm::FunctionType *Ty =
2944 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2945 /*isVarArg=*/false);
2946 PureVirtualFn =
2947 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
2948 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
2949 Int8PtrTy);
2950 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002951
Anders Carlsson67d568a2010-03-29 05:40:50 +00002952 Init = PureVirtualFn;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002953 } else {
Anders Carlsson67d568a2010-03-29 05:40:50 +00002954 // Check if we should use a thunk.
2955 if (NextVTableThunkIndex < VTableThunks.size() &&
2956 VTableThunks[NextVTableThunkIndex].first == I) {
2957 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002958
Anders Carlsson84c49e42011-02-06 17:15:43 +00002959 Init = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002960 MaybeEmitThunkAvailableExternally(GD, Thunk);
2961
Anders Carlsson67d568a2010-03-29 05:40:50 +00002962 NextVTableThunkIndex++;
2963 } else {
John McCall4c40d982010-08-31 07:33:07 +00002964 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson67d568a2010-03-29 05:40:50 +00002965
Anders Carlsson1faa89f2011-02-05 04:35:53 +00002966 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson67d568a2010-03-29 05:40:50 +00002967 }
2968
2969 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002970 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002971 break;
2972 }
2973
Anders Carlsson94464812010-04-10 19:13:06 +00002974 case VTableComponent::CK_UnusedFunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002975 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
2976 break;
2977 };
Anders Carlsson0d1407e2010-03-25 15:26:28 +00002978
2979 Inits.push_back(Init);
2980 }
2981
2982 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
2983 return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
2984}
2985
Anders Carlsson9dc338a2010-03-30 03:35:35 +00002986llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00002987 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002988 llvm::raw_svector_ostream Out(OutName);
2989 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
2990 Out.flush();
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00002991 llvm::StringRef Name = OutName.str();
Mike Stump85615df2009-11-19 04:04:36 +00002992
John McCalle2132352010-06-02 21:22:02 +00002993 ComputeVTableRelatedInformation(RD, true);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002994
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00002995 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlssonccd83d72010-03-24 16:42:11 +00002996 llvm::ArrayType *ArrayType =
2997 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002998
Rafael Espindola9f959db2011-01-11 21:10:26 +00002999 llvm::GlobalVariable *GV =
Anders Carlsson96eaf292011-01-29 18:25:07 +00003000 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
3001 llvm::GlobalValue::ExternalLinkage);
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +00003002 GV->setUnnamedAddr(true);
Rafael Espindola9f959db2011-01-11 21:10:26 +00003003 return GV;
Mike Stump380dd752009-11-10 07:44:33 +00003004}
Mike Stump8cfcb522009-11-11 20:26:26 +00003005
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003006void
3007CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
3008 llvm::GlobalVariable::LinkageTypes Linkage,
3009 const CXXRecordDecl *RD) {
3010 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003011 if (CGM.getLangOptions().DumpVTableLayouts) {
Anders Carlsson94464812010-04-10 19:13:06 +00003012 VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003013
3014 Builder.dumpLayout(llvm::errs());
3015 }
3016
3017 assert(VTableThunksMap.count(RD) &&
3018 "No thunk status for this record decl!");
3019
3020 const VTableThunksTy& Thunks = VTableThunksMap[RD];
3021
3022 // Create and set the initializer.
3023 llvm::Constant *Init =
3024 CreateVTableInitializer(RD, getVTableComponentsData(RD),
3025 getNumVTableComponents(RD), Thunks);
3026 VTable->setInitializer(Init);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003027
3028 // Set the correct linkage.
3029 VTable->setLinkage(Linkage);
Douglas Gregorc66bcfd2010-06-14 23:41:45 +00003030
3031 // Set the right visibility.
Anders Carlssonfa2e99f2011-01-29 20:24:48 +00003032 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003033}
3034
Anders Carlssonff143f82010-03-25 00:35:49 +00003035llvm::GlobalVariable *
3036CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
Anders Carlsson2c822f12010-03-26 03:56:54 +00003037 const BaseSubobject &Base,
3038 bool BaseIsVirtual,
3039 VTableAddressPointsMapTy& AddressPoints) {
Anders Carlsson94464812010-04-10 19:13:06 +00003040 VTableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(),
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003041 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3042
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003043 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003044 if (CGM.getLangOptions().DumpVTableLayouts)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003045 Builder.dumpLayout(llvm::errs());
3046
3047 // Add the address points.
Anders Carlsson2c822f12010-03-26 03:56:54 +00003048 AddressPoints.insert(Builder.address_points_begin(),
3049 Builder.address_points_end());
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003050
3051 // Get the mangled construction vtable name.
3052 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003053 llvm::raw_svector_ostream Out(OutName);
John McCall4c40d982010-08-31 07:33:07 +00003054 CGM.getCXXABI().getMangleContext().
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003055 mangleCXXCtorVTable(RD, Base.getBaseOffset() / 8, Base.getBase(), Out);
3056 Out.flush();
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003057 llvm::StringRef Name = OutName.str();
3058
3059 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3060 llvm::ArrayType *ArrayType =
3061 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
3062
3063 // Create the variable that will hold the construction vtable.
3064 llvm::GlobalVariable *VTable =
Anders Carlsson96eaf292011-01-29 18:25:07 +00003065 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
3066 llvm::GlobalValue::InternalLinkage);
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003067
3068 // Add the thunks.
3069 VTableThunksTy VTableThunks;
3070 VTableThunks.append(Builder.vtable_thunks_begin(),
3071 Builder.vtable_thunks_end());
3072
Anders Carlssonaed924e2010-03-29 01:28:05 +00003073 // Sort them.
3074 std::sort(VTableThunks.begin(), VTableThunks.end());
3075
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003076 // Create and set the initializer.
3077 llvm::Constant *Init =
3078 CreateVTableInitializer(Base.getBase(),
3079 Builder.vtable_components_data_begin(),
3080 Builder.getNumVTableComponents(), VTableThunks);
3081 VTable->setInitializer(Init);
3082
Anders Carlssonff143f82010-03-25 00:35:49 +00003083 return VTable;
3084}
3085
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003086void
3087CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3088 const CXXRecordDecl *RD) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003089 llvm::GlobalVariable *&VTable = VTables[RD];
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003090 if (VTable) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003091 assert(VTable->getInitializer() && "VTable doesn't have a definition!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003092 return;
3093 }
3094
Anders Carlsson9dc338a2010-03-30 03:35:35 +00003095 VTable = GetAddrOfVTable(RD);
3096 EmitVTableDefinition(VTable, Linkage, RD);
3097
Anders Carlsson1cbce122011-01-29 19:16:51 +00003098 if (RD->getNumVBases()) {
3099 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
3100 EmitVTTDefinition(VTT, Linkage, RD);
3101 }
Douglas Gregor1e201b42010-04-08 15:52:03 +00003102
3103 // If this is the magic class __cxxabiv1::__fundamental_type_info,
3104 // we will emit the typeinfo for the fundamental types. This is the
3105 // same behaviour as GCC.
3106 const DeclContext *DC = RD->getDeclContext();
3107 if (RD->getIdentifier() &&
3108 RD->getIdentifier()->isStr("__fundamental_type_info") &&
3109 isa<NamespaceDecl>(DC) &&
3110 cast<NamespaceDecl>(DC)->getIdentifier() &&
3111 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
3112 DC->getParent()->isTranslationUnit())
3113 CGM.EmitFundamentalRTTIDescriptors();
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003114}