blob: c7ef9c74183d368bcfa44ac07c0d823fd51ec235 [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"
Eli Friedman7dcdf5b2011-05-06 17:27:27 +000024#include "llvm/Transforms/Utils/Cloning.h"
Anders Carlsson5e454aa2010-03-17 20:06:32 +000025#include <algorithm>
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000026#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000027
28using namespace clang;
29using namespace CodeGen;
30
Anders Carlsson27f69d02009-11-27 22:21:51 +000031namespace {
Anders Carlsson824d7ea2010-02-11 08:02:13 +000032
Anders Carlsson2fca9be2010-02-23 01:34:28 +000033/// BaseOffset - Represents an offset from a derived class to a direct or
34/// indirect base class.
35struct BaseOffset {
36 /// DerivedClass - The derived class.
37 const CXXRecordDecl *DerivedClass;
38
39 /// VirtualBase - If the path from the derived class to the base class
40 /// involves a virtual base class, this holds its declaration.
41 const CXXRecordDecl *VirtualBase;
42
43 /// NonVirtualOffset - The offset from the derived class to the base class.
44 /// (Or the offset from the virtual base class to the base class, if the
45 /// path from the derived class to the base class involves a virtual base
46 /// class.
Ken Dyck396e4f62011-04-02 01:32:03 +000047 CharUnits NonVirtualOffset;
Anders Carlsson2fca9be2010-02-23 01:34:28 +000048
Ken Dyck396e4f62011-04-02 01:32:03 +000049 BaseOffset() : DerivedClass(0), VirtualBase(0),
50 NonVirtualOffset(CharUnits::Zero()) { }
Anders Carlsson2fca9be2010-02-23 01:34:28 +000051 BaseOffset(const CXXRecordDecl *DerivedClass,
Ken Dyck396e4f62011-04-02 01:32:03 +000052 const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset)
Anders Carlsson2fca9be2010-02-23 01:34:28 +000053 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
54 NonVirtualOffset(NonVirtualOffset) { }
55
Ken Dyck396e4f62011-04-02 01:32:03 +000056 bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
Anders Carlsson2fca9be2010-02-23 01:34:28 +000057};
58
Anders Carlssonbdf73d82010-02-11 21:24:32 +000059/// FinalOverriders - Contains the final overrider member functions for all
60/// member functions in the base subobjects of a class.
61class FinalOverriders {
Anders Carlssona7575822010-02-12 16:55:34 +000062public:
63 /// OverriderInfo - Information about a final overrider.
64 struct OverriderInfo {
65 /// Method - The method decl of the overrider.
66 const CXXMethodDecl *Method;
Anders Carlsson96309c42010-02-18 16:29:24 +000067
Anders Carlssondb4022c2010-03-10 02:33:41 +000068 /// Offset - the base offset of the overrider in the layout class.
Ken Dyck17d85192011-03-26 01:29:16 +000069 CharUnits Offset;
Anders Carlssona7575822010-02-12 16:55:34 +000070
Ken Dyck17d85192011-03-26 01:29:16 +000071 OverriderInfo() : Method(0), Offset(CharUnits::Zero()) { }
Anders Carlssona7575822010-02-12 16:55:34 +000072 };
73
74private:
Anders Carlssonbdf73d82010-02-11 21:24:32 +000075 /// MostDerivedClass - The most derived class for which the final overriders
76 /// are stored.
77 const CXXRecordDecl *MostDerivedClass;
78
Anders Carlssondb4022c2010-03-10 02:33:41 +000079 /// MostDerivedClassOffset - If we're building final overriders for a
80 /// construction vtable, this holds the offset from the layout class to the
81 /// most derived class.
Ken Dyckfc724202011-03-27 20:10:29 +000082 const CharUnits MostDerivedClassOffset;
Anders Carlssondb4022c2010-03-10 02:33:41 +000083
84 /// LayoutClass - The class we're using for layout information. Will be
85 /// different than the most derived class if the final overriders are for a
86 /// construction vtable.
87 const CXXRecordDecl *LayoutClass;
88
Anders Carlssonbdf73d82010-02-11 21:24:32 +000089 ASTContext &Context;
90
91 /// MostDerivedClassLayout - the AST record layout of the most derived class.
92 const ASTRecordLayout &MostDerivedClassLayout;
93
Anders Carlsson0a028602010-06-01 23:52:26 +000094 /// MethodBaseOffsetPairTy - Uniquely identifies a member function
Anders Carlsson0c0eeb22010-02-13 02:02:03 +000095 /// in a base subobject.
Ken Dycke6203132011-03-26 01:18:17 +000096 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
Anders Carlsson0a028602010-06-01 23:52:26 +000097
98 typedef llvm::DenseMap<MethodBaseOffsetPairTy,
Anders Carlssona7575822010-02-12 16:55:34 +000099 OverriderInfo> OverridersMapTy;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000100
101 /// OverridersMap - The final overriders for all virtual member functions of
102 /// all the base subobjects of the most derived class.
103 OverridersMapTy OverridersMap;
104
Anders Carlsson147a23d2010-06-04 00:54:04 +0000105 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
106 /// as a record decl and a subobject number) and its offsets in the most
107 /// derived class as well as the layout class.
108 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
Ken Dyck35c416b2011-03-26 01:09:13 +0000109 CharUnits> SubobjectOffsetMapTy;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000110
111 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
112
113 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
114 /// given base.
115 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000116 CharUnits OffsetInLayoutClass,
Anders Carlsson147a23d2010-06-04 00:54:04 +0000117 SubobjectOffsetMapTy &SubobjectOffsets,
118 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
119 SubobjectCountMapTy &SubobjectCounts);
120
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000121 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
122
123 /// dump - dump the final overriders for a base subobject, and all its direct
124 /// and indirect base subobjects.
125 void dump(llvm::raw_ostream &Out, BaseSubobject Base,
126 VisitedVirtualBasesSetTy& VisitedVirtualBases);
127
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000128public:
Anders Carlssondb4022c2010-03-10 02:33:41 +0000129 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
Ken Dyckfc724202011-03-27 20:10:29 +0000130 CharUnits MostDerivedClassOffset,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000131 const CXXRecordDecl *LayoutClass);
Anders Carlsson2157d302010-02-24 22:18:01 +0000132
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000133 /// getOverrider - Get the final overrider for the given method declaration in
Anders Carlsson26387642010-06-01 23:17:37 +0000134 /// the subobject with the given base offset.
135 OverriderInfo getOverrider(const CXXMethodDecl *MD,
Ken Dycke6203132011-03-26 01:18:17 +0000136 CharUnits BaseOffset) const {
Anders Carlsson0a028602010-06-01 23:52:26 +0000137 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000138 "Did not find overrider!");
139
Anders Carlsson0a028602010-06-01 23:52:26 +0000140 return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000141 }
142
143 /// dump - dump the final overriders.
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000144 void dump() {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000145 VisitedVirtualBasesSetTy VisitedVirtualBases;
Ken Dyck4230d522011-03-24 01:21:01 +0000146 dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
147 VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000148 }
149
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000150};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000151
Anders Carlsson327568e2010-02-27 20:02:53 +0000152#define DUMP_OVERRIDERS 0
153
Anders Carlssondb4022c2010-03-10 02:33:41 +0000154FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
Ken Dyckfc724202011-03-27 20:10:29 +0000155 CharUnits MostDerivedClassOffset,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000156 const CXXRecordDecl *LayoutClass)
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000157 : MostDerivedClass(MostDerivedClass),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000158 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000159 Context(MostDerivedClass->getASTContext()),
160 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000161
Anders Carlsson147a23d2010-06-04 00:54:04 +0000162 // Compute base offsets.
163 SubobjectOffsetMapTy SubobjectOffsets;
164 SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
165 SubobjectCountMapTy SubobjectCounts;
Ken Dyck4230d522011-03-24 01:21:01 +0000166 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
167 /*IsVirtual=*/false,
Ken Dyckfc724202011-03-27 20:10:29 +0000168 MostDerivedClassOffset,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000169 SubobjectOffsets, SubobjectLayoutClassOffsets,
170 SubobjectCounts);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000171
172 // Get the the final overriders.
173 CXXFinalOverriderMap FinalOverriders;
174 MostDerivedClass->getFinalOverriders(FinalOverriders);
175
176 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(),
177 E = FinalOverriders.end(); I != E; ++I) {
178 const CXXMethodDecl *MD = I->first;
179 const OverridingMethods& Methods = I->second;
180
181 for (OverridingMethods::const_iterator I = Methods.begin(),
182 E = Methods.end(); I != E; ++I) {
183 unsigned SubobjectNumber = I->first;
184 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(),
185 SubobjectNumber)) &&
186 "Did not find subobject offset!");
187
Ken Dyck35c416b2011-03-26 01:09:13 +0000188 CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
Anders Carlsson147a23d2010-06-04 00:54:04 +0000189 SubobjectNumber)];
190
191 assert(I->second.size() == 1 && "Final overrider is not unique!");
192 const UniqueVirtualMethod &Method = I->second.front();
193
194 const CXXRecordDecl *OverriderRD = Method.Method->getParent();
195 assert(SubobjectLayoutClassOffsets.count(
196 std::make_pair(OverriderRD, Method.Subobject))
197 && "Did not find subobject offset!");
Ken Dyck35c416b2011-03-26 01:09:13 +0000198 CharUnits OverriderOffset =
Anders Carlsson147a23d2010-06-04 00:54:04 +0000199 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD,
200 Method.Subobject)];
201
Ken Dycke6203132011-03-26 01:18:17 +0000202 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
Anders Carlsson147a23d2010-06-04 00:54:04 +0000203 assert(!Overrider.Method && "Overrider should not exist yet!");
204
Ken Dyck17d85192011-03-26 01:29:16 +0000205 Overrider.Offset = OverriderOffset;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000206 Overrider.Method = Method.Method;
207 }
208 }
Anders Carlsson327568e2010-02-27 20:02:53 +0000209
210#if DUMP_OVERRIDERS
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000211 // And dump them (for now).
212 dump();
Anders Carlsson327568e2010-02-27 20:02:53 +0000213#endif
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000214}
215
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000216static BaseOffset ComputeBaseOffset(ASTContext &Context,
217 const CXXRecordDecl *DerivedRD,
218 const CXXBasePath &Path) {
Ken Dyck396e4f62011-04-02 01:32:03 +0000219 CharUnits NonVirtualOffset = CharUnits::Zero();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000220
Anders Carlssona4a54172010-02-13 20:41:15 +0000221 unsigned NonVirtualStart = 0;
222 const CXXRecordDecl *VirtualBase = 0;
223
224 // First, look for the virtual base class.
225 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
226 const CXXBasePathElement &Element = Path[I];
227
228 if (Element.Base->isVirtual()) {
229 // FIXME: Can we break when we find the first virtual base?
230 // (If we can't, can't we just iterate over the path in reverse order?)
231 NonVirtualStart = I + 1;
232 QualType VBaseType = Element.Base->getType();
233 VirtualBase =
234 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
235 }
236 }
237
238 // Now compute the non-virtual offset.
239 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
240 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000241
242 // Check the base class offset.
243 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
Anders Carlssonbf554f62010-02-25 22:18:35 +0000244
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000245 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
246 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
Anders Carlssonbf554f62010-02-25 22:18:35 +0000247
Ken Dyck396e4f62011-04-02 01:32:03 +0000248 NonVirtualOffset += Layout.getBaseClassOffset(Base);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000249 }
250
251 // FIXME: This should probably use CharUnits or something. Maybe we should
252 // even change the base offsets in ASTRecordLayout to be specified in
253 // CharUnits.
Ken Dyck396e4f62011-04-02 01:32:03 +0000254 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset);
Anders Carlssone67dc302010-02-14 00:16:19 +0000255
256}
257
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000258static BaseOffset ComputeBaseOffset(ASTContext &Context,
259 const CXXRecordDecl *BaseRD,
260 const CXXRecordDecl *DerivedRD) {
Anders Carlssone67dc302010-02-14 00:16:19 +0000261 CXXBasePaths Paths(/*FindAmbiguities=*/false,
262 /*RecordPaths=*/true, /*DetectVirtual=*/false);
263
264 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
265 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
266 assert(false && "Class must be derived from the passed in base class!");
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000267 return BaseOffset();
Anders Carlssone67dc302010-02-14 00:16:19 +0000268 }
269
270 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000271}
272
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000273static BaseOffset
Anders Carlssondf31af12010-02-13 23:17:31 +0000274ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
275 const CXXMethodDecl *DerivedMD,
276 const CXXMethodDecl *BaseMD) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000277 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
278 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
279
280 // Canonicalize the return types.
281 CanQualType CanDerivedReturnType =
282 Context.getCanonicalType(DerivedFT->getResultType());
283 CanQualType CanBaseReturnType =
284 Context.getCanonicalType(BaseFT->getResultType());
285
286 assert(CanDerivedReturnType->getTypeClass() ==
287 CanBaseReturnType->getTypeClass() &&
288 "Types must have same type class!");
289
290 if (CanDerivedReturnType == CanBaseReturnType) {
291 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000292 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000293 }
294
295 if (isa<ReferenceType>(CanDerivedReturnType)) {
296 CanDerivedReturnType =
297 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
298 CanBaseReturnType =
299 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
300 } else if (isa<PointerType>(CanDerivedReturnType)) {
301 CanDerivedReturnType =
302 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
303 CanBaseReturnType =
304 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
305 } else {
306 assert(false && "Unexpected return type!");
307 }
308
309 // We need to compare unqualified types here; consider
310 // const T *Base::foo();
311 // T *Derived::foo();
312 if (CanDerivedReturnType.getUnqualifiedType() ==
313 CanBaseReturnType.getUnqualifiedType()) {
314 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000315 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000316 }
317
318 const CXXRecordDecl *DerivedRD =
319 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
320
321 const CXXRecordDecl *BaseRD =
322 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
323
Anders Carlssondf31af12010-02-13 23:17:31 +0000324 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000325}
326
Anders Carlsson147a23d2010-06-04 00:54:04 +0000327void
328FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000329 CharUnits OffsetInLayoutClass,
Anders Carlsson147a23d2010-06-04 00:54:04 +0000330 SubobjectOffsetMapTy &SubobjectOffsets,
331 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
332 SubobjectCountMapTy &SubobjectCounts) {
333 const CXXRecordDecl *RD = Base.getBase();
334
335 unsigned SubobjectNumber = 0;
336 if (!IsVirtual)
337 SubobjectNumber = ++SubobjectCounts[RD];
338
339 // Set up the subobject to offset mapping.
340 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber))
341 && "Subobject offset already exists!");
342 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber))
343 && "Subobject offset already exists!");
344
Ken Dyck35c416b2011-03-26 01:09:13 +0000345 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset();
Anders Carlsson147a23d2010-06-04 00:54:04 +0000346 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
Ken Dyckb4c82b42011-03-26 01:36:26 +0000347 OffsetInLayoutClass;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000348
349 // Traverse our bases.
350 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
351 E = RD->bases_end(); I != E; ++I) {
352 const CXXRecordDecl *BaseDecl =
353 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
354
Ken Dyck57ebe332011-03-26 00:51:24 +0000355 CharUnits BaseOffset;
356 CharUnits BaseOffsetInLayoutClass;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000357 if (I->isVirtual()) {
358 // Check if we've visited this virtual base before.
359 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0)))
360 continue;
361
362 const ASTRecordLayout &LayoutClassLayout =
363 Context.getASTRecordLayout(LayoutClass);
364
Ken Dyck57ebe332011-03-26 00:51:24 +0000365 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000366 BaseOffsetInLayoutClass =
Ken Dyck57ebe332011-03-26 00:51:24 +0000367 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000368 } else {
369 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ken Dyck57ebe332011-03-26 00:51:24 +0000370 CharUnits Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000371
Ken Dyck57ebe332011-03-26 00:51:24 +0000372 BaseOffset = Base.getBaseOffset() + Offset;
Ken Dyckb4c82b42011-03-26 01:36:26 +0000373 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000374 }
375
Ken Dyck57ebe332011-03-26 00:51:24 +0000376 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset),
Ken Dyckb4c82b42011-03-26 01:36:26 +0000377 I->isVirtual(), BaseOffsetInLayoutClass,
Ken Dyck4230d522011-03-24 01:21:01 +0000378 SubobjectOffsets, SubobjectLayoutClassOffsets,
379 SubobjectCounts);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000380 }
381}
382
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000383void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base,
384 VisitedVirtualBasesSetTy &VisitedVirtualBases) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000385 const CXXRecordDecl *RD = Base.getBase();
386 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
387
388 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
389 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000390 const CXXRecordDecl *BaseDecl =
391 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
392
Anders Carlssonb828afa2010-02-14 17:05:59 +0000393 // Ignore bases that don't have any virtual member functions.
394 if (!BaseDecl->isPolymorphic())
395 continue;
396
Ken Dyck4230d522011-03-24 01:21:01 +0000397 CharUnits BaseOffset;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000398 if (I->isVirtual()) {
399 if (!VisitedVirtualBases.insert(BaseDecl)) {
400 // We've visited this base before.
401 continue;
402 }
403
Ken Dyck4230d522011-03-24 01:21:01 +0000404 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000405 } else {
Ken Dyck4230d522011-03-24 01:21:01 +0000406 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000407 }
408
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000409 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000410 }
411
412 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
Ken Dyck4230d522011-03-24 01:21:01 +0000413 Out << Base.getBaseOffset().getQuantity() << ")\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000414
415 // Now dump the overriders for this base subobject.
416 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
417 E = RD->method_end(); I != E; ++I) {
418 const CXXMethodDecl *MD = *I;
419
420 if (!MD->isVirtual())
421 continue;
422
Ken Dycke6203132011-03-26 01:18:17 +0000423 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000424
Anders Carlssond86e5d82010-03-02 03:44:06 +0000425 Out << " " << MD->getQualifiedNameAsString() << " - (";
Anders Carlsson16b73122010-02-12 17:13:23 +0000426 Out << Overrider.Method->getQualifiedNameAsString();
Ken Dyck17d85192011-03-26 01:29:16 +0000427 Out << ", " << ", " << Overrider.Offset.getQuantity() << ')';
Anders Carlssond86e5d82010-03-02 03:44:06 +0000428
Anders Carlsson7097e752010-06-01 23:47:05 +0000429 BaseOffset Offset;
430 if (!Overrider.Method->isPure())
431 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
Anders Carlssona4a54172010-02-13 20:41:15 +0000432
Anders Carlsson7097e752010-06-01 23:47:05 +0000433 if (!Offset.isEmpty()) {
Anders Carlssona4a54172010-02-13 20:41:15 +0000434 Out << " [ret-adj: ";
435 if (Offset.VirtualBase)
436 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000437
Ken Dyck396e4f62011-04-02 01:32:03 +0000438 Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000439 }
Anders Carlssondf31af12010-02-13 23:17:31 +0000440
Anders Carlsson16b73122010-02-12 17:13:23 +0000441 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000442 }
443}
444
Anders Carlsson94464812010-04-10 19:13:06 +0000445/// VTableComponent - Represents a single component in a vtable.
446class VTableComponent {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000447public:
448 enum Kind {
449 CK_VCallOffset,
450 CK_VBaseOffset,
451 CK_OffsetToTop,
452 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000453 CK_FunctionPointer,
454
455 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
456 CK_CompleteDtorPointer,
457
458 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
Anders Carlsson1f464662010-02-19 20:08:13 +0000459 CK_DeletingDtorPointer,
460
461 /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer
462 /// will end up never being called. Such vtable function pointers are
463 /// represented as a CK_UnusedFunctionPointer.
464 CK_UnusedFunctionPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000465 };
466
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000467 static VTableComponent MakeVCallOffset(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000468 return VTableComponent(CK_VCallOffset, Offset);
Anders Carlssond9a67042010-02-17 06:07:19 +0000469 }
470
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000471 static VTableComponent MakeVBaseOffset(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000472 return VTableComponent(CK_VBaseOffset, Offset);
Anders Carlssona661a542010-02-16 04:49:44 +0000473 }
474
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000475 static VTableComponent MakeOffsetToTop(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000476 return VTableComponent(CK_OffsetToTop, Offset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000477 }
478
Anders Carlsson94464812010-04-10 19:13:06 +0000479 static VTableComponent MakeRTTI(const CXXRecordDecl *RD) {
480 return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000481 }
482
Anders Carlsson94464812010-04-10 19:13:06 +0000483 static VTableComponent MakeFunction(const CXXMethodDecl *MD) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000484 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000485 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000486
Anders Carlsson94464812010-04-10 19:13:06 +0000487 return VTableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000488 reinterpret_cast<uintptr_t>(MD));
489 }
490
Anders Carlsson94464812010-04-10 19:13:06 +0000491 static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
492 return VTableComponent(CK_CompleteDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000493 reinterpret_cast<uintptr_t>(DD));
494 }
495
Anders Carlsson94464812010-04-10 19:13:06 +0000496 static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
497 return VTableComponent(CK_DeletingDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000498 reinterpret_cast<uintptr_t>(DD));
499 }
500
Anders Carlsson94464812010-04-10 19:13:06 +0000501 static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) {
Anders Carlsson1f464662010-02-19 20:08:13 +0000502 assert(!isa<CXXDestructorDecl>(MD) &&
503 "Don't use MakeUnusedFunction with destructors!");
Anders Carlsson94464812010-04-10 19:13:06 +0000504 return VTableComponent(CK_UnusedFunctionPointer,
Anders Carlsson1f464662010-02-19 20:08:13 +0000505 reinterpret_cast<uintptr_t>(MD));
506 }
507
Anders Carlsson94464812010-04-10 19:13:06 +0000508 static VTableComponent getFromOpaqueInteger(uint64_t I) {
509 return VTableComponent(I);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000510 }
511
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000512 /// getKind - Get the kind of this vtable component.
513 Kind getKind() const {
514 return (Kind)(Value & 0x7);
515 }
516
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000517 CharUnits getVCallOffset() const {
Anders Carlssond9a67042010-02-17 06:07:19 +0000518 assert(getKind() == CK_VCallOffset && "Invalid component kind!");
519
520 return getOffset();
521 }
522
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000523 CharUnits getVBaseOffset() const {
Anders Carlssona661a542010-02-16 04:49:44 +0000524 assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
525
526 return getOffset();
527 }
528
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000529 CharUnits getOffsetToTop() const {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000530 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
531
532 return getOffset();
533 }
534
535 const CXXRecordDecl *getRTTIDecl() const {
536 assert(getKind() == CK_RTTI && "Invalid component kind!");
537
538 return reinterpret_cast<CXXRecordDecl *>(getPointer());
539 }
540
541 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000542 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000543
544 return reinterpret_cast<CXXMethodDecl *>(getPointer());
545 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000546
547 const CXXDestructorDecl *getDestructorDecl() const {
548 assert((getKind() == CK_CompleteDtorPointer ||
549 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
550
551 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
552 }
553
Anders Carlsson1f464662010-02-19 20:08:13 +0000554 const CXXMethodDecl *getUnusedFunctionDecl() const {
555 assert(getKind() == CK_UnusedFunctionPointer);
556
557 return reinterpret_cast<CXXMethodDecl *>(getPointer());
558 }
559
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000560private:
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000561 VTableComponent(Kind ComponentKind, CharUnits Offset) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000562 assert((ComponentKind == CK_VCallOffset ||
563 ComponentKind == CK_VBaseOffset ||
564 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000565 assert(Offset.getQuantity() <= ((1LL << 56) - 1) && "Offset is too big!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000566
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000567 Value = ((Offset.getQuantity() << 3) | ComponentKind);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000568 }
569
Anders Carlsson94464812010-04-10 19:13:06 +0000570 VTableComponent(Kind ComponentKind, uintptr_t Ptr) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000571 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000572 ComponentKind == CK_FunctionPointer ||
573 ComponentKind == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000574 ComponentKind == CK_DeletingDtorPointer ||
575 ComponentKind == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000576 "Invalid component kind!");
577
578 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
579
580 Value = Ptr | ComponentKind;
581 }
582
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000583 CharUnits getOffset() const {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000584 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
585 getKind() == CK_OffsetToTop) && "Invalid component kind!");
586
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000587 return CharUnits::fromQuantity(Value >> 3);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000588 }
589
590 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000591 assert((getKind() == CK_RTTI ||
592 getKind() == CK_FunctionPointer ||
593 getKind() == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000594 getKind() == CK_DeletingDtorPointer ||
595 getKind() == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000596 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000597
598 return static_cast<uintptr_t>(Value & ~7ULL);
599 }
600
Anders Carlsson94464812010-04-10 19:13:06 +0000601 explicit VTableComponent(uint64_t Value)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000602 : Value(Value) { }
603
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000604 /// The kind is stored in the lower 3 bits of the value. For offsets, we
605 /// make use of the facts that classes can't be larger than 2^55 bytes,
606 /// so we store the offset in the lower part of the 61 bytes that remain.
607 /// (The reason that we're not simply using a PointerIntPair here is that we
608 /// need the offsets to be 64-bit, even when on a 32-bit machine).
609 int64_t Value;
610};
611
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000612/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
613struct VCallOffsetMap {
614
Ken Dyckc952d382011-04-02 00:51:15 +0000615 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000616
617 /// Offsets - Keeps track of methods and their offsets.
618 // FIXME: This should be a real map and not a vector.
619 llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets;
620
Anders Carlssoneb577d02010-02-18 17:26:40 +0000621 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
622 /// can share the same vcall offset.
623 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
624 const CXXMethodDecl *RHS);
625
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000626public:
627 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
628 /// add was successful, or false if there was already a member function with
629 /// the same signature in the map.
Ken Dyckc952d382011-04-02 00:51:15 +0000630 bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000631
632 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
633 /// vtable address point) for the given virtual member function.
Ken Dyckc952d382011-04-02 00:51:15 +0000634 CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000635
Anders Carlssondba6ef82010-02-25 22:23:13 +0000636 // empty - Return whether the offset map is empty or not.
637 bool empty() const { return Offsets.empty(); }
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000638};
639
John McCall331a03a2010-02-26 20:22:44 +0000640static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
641 const CXXMethodDecl *RHS) {
642 ASTContext &C = LHS->getASTContext(); // TODO: thread this down
643 CanQual<FunctionProtoType>
644 LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(),
645 RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>();
646
647 // Fast-path matches in the canonical types.
648 if (LT == RT) return true;
649
650 // Force the signatures to match. We can't rely on the overrides
651 // list here because there isn't necessarily an inheritance
652 // relationship between the two methods.
653 if (LT.getQualifiers() != RT.getQualifiers() ||
654 LT->getNumArgs() != RT->getNumArgs())
655 return false;
656 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
657 if (LT->getArgType(I) != RT->getArgType(I))
658 return false;
659 return true;
660}
661
Anders Carlssoneb577d02010-02-18 17:26:40 +0000662bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
663 const CXXMethodDecl *RHS) {
664 assert(LHS->isVirtual() && "LHS must be virtual!");
665 assert(RHS->isVirtual() && "LHS must be virtual!");
666
Anders Carlsson55609692010-02-27 04:12:52 +0000667 // A destructor can share a vcall offset with another destructor.
668 if (isa<CXXDestructorDecl>(LHS))
669 return isa<CXXDestructorDecl>(RHS);
670
Anders Carlssoneb577d02010-02-18 17:26:40 +0000671 // FIXME: We need to check more things here.
672
Anders Carlsson55609692010-02-27 04:12:52 +0000673 // The methods must have the same name.
Anders Carlssoneb577d02010-02-18 17:26:40 +0000674 DeclarationName LHSName = LHS->getDeclName();
675 DeclarationName RHSName = RHS->getDeclName();
John McCall331a03a2010-02-26 20:22:44 +0000676 if (LHSName != RHSName)
Anders Carlssoneb577d02010-02-18 17:26:40 +0000677 return false;
John McCall331a03a2010-02-26 20:22:44 +0000678
Anders Carlsson55609692010-02-27 04:12:52 +0000679 // And the same signatures.
680 return HasSameVirtualSignature(LHS, RHS);
Anders Carlssoneb577d02010-02-18 17:26:40 +0000681}
682
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000683bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
Ken Dyckc952d382011-04-02 00:51:15 +0000684 CharUnits OffsetOffset) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000685 // Check if we can reuse an offset.
686 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
687 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
688 return false;
689 }
690
691 // Add the offset.
692 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000693 return true;
694}
695
Ken Dyckc952d382011-04-02 00:51:15 +0000696CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000697 // Look for an offset.
698 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
699 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
700 return Offsets[I].second;
701 }
702
703 assert(false && "Should always find a vcall offset offset!");
Ken Dyckc952d382011-04-02 00:51:15 +0000704 return CharUnits::Zero();
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000705}
706
Anders Carlssond68c0832010-02-25 03:45:56 +0000707/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
708class VCallAndVBaseOffsetBuilder {
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000709public:
Ken Dyckd28c33c2011-04-07 00:55:01 +0000710 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000711 VBaseOffsetOffsetsMapTy;
712
713private:
Anders Carlsson530c40c2010-02-28 01:43:58 +0000714 /// MostDerivedClass - The most derived class for which we're building vcall
715 /// and vbase offsets.
Anders Carlssond68c0832010-02-25 03:45:56 +0000716 const CXXRecordDecl *MostDerivedClass;
717
Anders Carlsson0041a642010-02-27 21:09:00 +0000718 /// LayoutClass - The class we're using for layout information. Will be
719 /// different than the most derived class if we're building a construction
720 /// vtable.
721 const CXXRecordDecl *LayoutClass;
722
Anders Carlssond68c0832010-02-25 03:45:56 +0000723 /// Context - The ASTContext which we will use for layout information.
724 ASTContext &Context;
725
726 /// Components - vcall and vbase offset components
Anders Carlsson94464812010-04-10 19:13:06 +0000727 typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy;
728 VTableComponentVectorTy Components;
Anders Carlssond68c0832010-02-25 03:45:56 +0000729
730 /// VisitedVirtualBases - Visited virtual bases.
731 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
732
733 /// VCallOffsets - Keeps track of vcall offsets.
734 VCallOffsetMap VCallOffsets;
735
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000736
737 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
738 /// relative to the address point.
739 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
740
Anders Carlssond68c0832010-02-25 03:45:56 +0000741 /// FinalOverriders - The final overriders of the most derived class.
742 /// (Can be null when we're not building a vtable of the most derived class).
743 const FinalOverriders *Overriders;
744
745 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
746 /// given base subobject.
747 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000748 CharUnits RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000749
750 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
Ken Dyck301af862011-03-30 10:11:40 +0000751 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000752
753 /// AddVBaseOffsets - Add vbase offsets for the given class.
Ken Dyckbb625e92011-03-31 00:56:47 +0000754 void AddVBaseOffsets(const CXXRecordDecl *Base,
755 CharUnits OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000756
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000757 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
Ken Dyck3ed994b2011-03-31 01:08:46 +0000758 /// chars, relative to the vtable address point.
759 CharUnits getCurrentOffsetOffset() const;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000760
Anders Carlssond68c0832010-02-25 03:45:56 +0000761public:
762 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
Anders Carlsson0041a642010-02-27 21:09:00 +0000763 const CXXRecordDecl *LayoutClass,
Anders Carlssond68c0832010-02-25 03:45:56 +0000764 const FinalOverriders *Overriders,
Anders Carlsson530c40c2010-02-28 01:43:58 +0000765 BaseSubobject Base, bool BaseIsVirtual,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +0000766 CharUnits OffsetInLayoutClass)
Anders Carlsson530c40c2010-02-28 01:43:58 +0000767 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
Anders Carlssond68c0832010-02-25 03:45:56 +0000768 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
769
770 // Add vcall and vbase offsets.
Ken Dyck3ecfa2d2011-03-31 00:48:04 +0000771 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000772 }
773
774 /// Methods for iterating over the components.
Anders Carlsson94464812010-04-10 19:13:06 +0000775 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
Anders Carlssond68c0832010-02-25 03:45:56 +0000776 const_iterator components_begin() const { return Components.rbegin(); }
777 const_iterator components_end() const { return Components.rend(); }
778
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000779 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
780 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000781 return VBaseOffsetOffsets;
782 }
Anders Carlssond68c0832010-02-25 03:45:56 +0000783};
784
785void
786VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
787 bool BaseIsVirtual,
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000788 CharUnits RealBaseOffset) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000789 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
790
791 // Itanium C++ ABI 2.5.2:
792 // ..in classes sharing a virtual table with a primary base class, the vcall
793 // and vbase offsets added by the derived class all come before the vcall
794 // and vbase offsets required by the base class, so that the latter may be
795 // laid out as required by the base class without regard to additions from
796 // the derived class(es).
797
798 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
799 // emit them for the primary base first).
800 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000801 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlssond68c0832010-02-25 03:45:56 +0000802
Ken Dyck57ebe332011-03-26 00:51:24 +0000803 CharUnits PrimaryBaseOffset;
Anders Carlssond68c0832010-02-25 03:45:56 +0000804
805 // Get the base offset of the primary base.
806 if (PrimaryBaseIsVirtual) {
Anders Carlssona14f5972010-10-31 23:22:37 +0000807 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000808 "Primary vbase should have a zero offset!");
809
810 const ASTRecordLayout &MostDerivedClassLayout =
811 Context.getASTRecordLayout(MostDerivedClass);
812
813 PrimaryBaseOffset =
Ken Dyck57ebe332011-03-26 00:51:24 +0000814 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssond68c0832010-02-25 03:45:56 +0000815 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +0000816 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000817 "Primary base should have a zero offset!");
818
Ken Dyck57ebe332011-03-26 00:51:24 +0000819 PrimaryBaseOffset = Base.getBaseOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +0000820 }
821
Ken Dyck4230d522011-03-24 01:21:01 +0000822 AddVCallAndVBaseOffsets(
Ken Dyck57ebe332011-03-26 00:51:24 +0000823 BaseSubobject(PrimaryBase,PrimaryBaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +0000824 PrimaryBaseIsVirtual, RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000825 }
826
Ken Dyckbb625e92011-03-31 00:56:47 +0000827 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000828
829 // We only want to add vcall offsets for virtual bases.
830 if (BaseIsVirtual)
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000831 AddVCallOffsets(Base, RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000832}
833
Ken Dyck3ed994b2011-03-31 01:08:46 +0000834CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000835 // OffsetIndex is the index of this vcall or vbase offset, relative to the
836 // vtable address point. (We subtract 3 to account for the information just
837 // above the address point, the RTTI info, the offset to top, and the
838 // vcall offset itself).
839 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
840
Ken Dyck3ed994b2011-03-31 01:08:46 +0000841 CharUnits PointerWidth =
842 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
843 CharUnits OffsetOffset = PointerWidth * OffsetIndex;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000844 return OffsetOffset;
845}
846
Anders Carlssond68c0832010-02-25 03:45:56 +0000847void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
Ken Dyck301af862011-03-30 10:11:40 +0000848 CharUnits VBaseOffset) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000849 const CXXRecordDecl *RD = Base.getBase();
850 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
851
John McCall331a03a2010-02-26 20:22:44 +0000852 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
853
Anders Carlssond68c0832010-02-25 03:45:56 +0000854 // Handle the primary base first.
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000855 // We only want to add vcall offsets if the base is non-virtual; a virtual
856 // primary base will have its vcall and vbase offsets emitted already.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000857 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000858 // Get the base offset of the primary base.
Anders Carlssona14f5972010-10-31 23:22:37 +0000859 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000860 "Primary base should have a zero offset!");
Anders Carlssond68c0832010-02-25 03:45:56 +0000861
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000862 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
Anders Carlssond68c0832010-02-25 03:45:56 +0000863 VBaseOffset);
864 }
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000865
Anders Carlssond68c0832010-02-25 03:45:56 +0000866 // Add the vcall offsets.
867 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
868 E = RD->method_end(); I != E; ++I) {
869 const CXXMethodDecl *MD = *I;
870
871 if (!MD->isVirtual())
872 continue;
873
Ken Dyck3ed994b2011-03-31 01:08:46 +0000874 CharUnits OffsetOffset = getCurrentOffsetOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +0000875
876 // Don't add a vcall offset if we already have one for this member function
877 // signature.
Ken Dyckc952d382011-04-02 00:51:15 +0000878 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
Anders Carlssond68c0832010-02-25 03:45:56 +0000879 continue;
880
Ken Dyck0c64b7d2011-03-30 01:21:33 +0000881 CharUnits Offset = CharUnits::Zero();
Anders Carlssond68c0832010-02-25 03:45:56 +0000882
883 if (Overriders) {
884 // Get the final overrider.
885 FinalOverriders::OverriderInfo Overrider =
Ken Dycke6203132011-03-26 01:18:17 +0000886 Overriders->getOverrider(MD, Base.getBaseOffset());
Anders Carlssond68c0832010-02-25 03:45:56 +0000887
Anders Carlsson438bc422010-02-28 00:10:58 +0000888 /// The vcall offset is the offset from the virtual base to the object
889 /// where the function was overridden.
Ken Dyck301af862011-03-30 10:11:40 +0000890 Offset = Overrider.Offset - VBaseOffset;
Anders Carlssond68c0832010-02-25 03:45:56 +0000891 }
892
Ken Dyck0c64b7d2011-03-30 01:21:33 +0000893 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000894 VTableComponent::MakeVCallOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000895 }
896
897 // And iterate over all non-virtual bases (ignoring the primary base).
898 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
899 E = RD->bases_end(); I != E; ++I) {
900
901 if (I->isVirtual())
902 continue;
903
904 const CXXRecordDecl *BaseDecl =
905 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
John McCall331a03a2010-02-26 20:22:44 +0000906 if (BaseDecl == PrimaryBase)
907 continue;
Anders Carlssond68c0832010-02-25 03:45:56 +0000908
909 // Get the base offset of this base.
Ken Dyck57ebe332011-03-26 00:51:24 +0000910 CharUnits BaseOffset = Base.getBaseOffset() +
911 Layout.getBaseClassOffset(BaseDecl);
Anders Carlssond68c0832010-02-25 03:45:56 +0000912
Ken Dyck57ebe332011-03-26 00:51:24 +0000913 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
914 VBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000915 }
916}
917
Ken Dyckbb625e92011-03-31 00:56:47 +0000918void
919VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
920 CharUnits OffsetInLayoutClass) {
Anders Carlsson0041a642010-02-27 21:09:00 +0000921 const ASTRecordLayout &LayoutClassLayout =
922 Context.getASTRecordLayout(LayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000923
924 // Add vbase offsets.
925 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
926 E = RD->bases_end(); I != E; ++I) {
927 const CXXRecordDecl *BaseDecl =
928 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
929
930 // Check if this is a virtual base that we haven't visited before.
931 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
Ken Dyckbb625e92011-03-31 00:56:47 +0000932 CharUnits Offset =
933 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000934
935 // Add the vbase offset offset.
936 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
937 "vbase offset offset already exists!");
938
Ken Dyck3ed994b2011-03-31 01:08:46 +0000939 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
940 VBaseOffsetOffsets.insert(
Ken Dyckd28c33c2011-04-07 00:55:01 +0000941 std::make_pair(BaseDecl, VBaseOffsetOffset));
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000942
Ken Dyckbb625e92011-03-31 00:56:47 +0000943 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000944 VTableComponent::MakeVBaseOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000945 }
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000946
Anders Carlssond68c0832010-02-25 03:45:56 +0000947 // Check the base class looking for more vbase offsets.
Anders Carlsson88800662010-03-02 05:40:45 +0000948 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000949 }
950}
951
Anders Carlsson94464812010-04-10 19:13:06 +0000952/// VTableBuilder - Class for building vtable layout information.
953class VTableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +0000954public:
Anders Carlssonb9021e92010-02-27 16:18:19 +0000955 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
956 /// primary bases.
957 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
958 PrimaryBasesSetVectorTy;
959
Ken Dyckd28c33c2011-04-07 00:55:01 +0000960 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
Anders Carlssonccd83d72010-03-24 16:42:11 +0000961 VBaseOffsetOffsetsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +0000962
963 typedef llvm::DenseMap<BaseSubobject, uint64_t>
964 AddressPointsMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000965
Anders Carlsson57071e22010-02-12 05:25:12 +0000966private:
Anders Carlssonaf440352010-03-23 04:11:45 +0000967 /// VTables - Global vtable information.
968 CodeGenVTables &VTables;
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000969
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000970 /// MostDerivedClass - The most derived class for which we're building this
971 /// vtable.
972 const CXXRecordDecl *MostDerivedClass;
973
Anders Carlssond03a3262010-02-27 20:39:05 +0000974 /// MostDerivedClassOffset - If we're building a construction vtable, this
975 /// holds the offset from the layout class to the most derived class.
Ken Dyck23da84a2011-03-27 20:20:28 +0000976 const CharUnits MostDerivedClassOffset;
Anders Carlssond03a3262010-02-27 20:39:05 +0000977
Anders Carlsson5d7af6b2010-02-28 00:36:23 +0000978 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
979 /// base. (This only makes sense when building a construction vtable).
980 bool MostDerivedClassIsVirtual;
981
Anders Carlssond03a3262010-02-27 20:39:05 +0000982 /// LayoutClass - The class we're using for layout information. Will be
983 /// different than the most derived class if we're building a construction
984 /// vtable.
985 const CXXRecordDecl *LayoutClass;
986
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000987 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +0000988 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000989
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000990 /// FinalOverriders - The final overriders of the most derived class.
Anders Carlssond68c0832010-02-25 03:45:56 +0000991 const FinalOverriders Overriders;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000992
Anders Carlssondba6ef82010-02-25 22:23:13 +0000993 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
994 /// bases in this vtable.
995 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000996
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000997 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
998 /// the most derived class.
999 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
1000
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001001 /// Components - The components of the vtable being built.
Anders Carlsson94464812010-04-10 19:13:06 +00001002 llvm::SmallVector<VTableComponent, 64> Components;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001003
Anders Carlsson822307b2010-02-11 17:18:51 +00001004 /// AddressPoints - Address points for the vtable being built.
Anders Carlssonff143f82010-03-25 00:35:49 +00001005 AddressPointsMapTy AddressPoints;
Anders Carlsson822307b2010-02-11 17:18:51 +00001006
Anders Carlssonb9021e92010-02-27 16:18:19 +00001007 /// MethodInfo - Contains information about a method in a vtable.
1008 /// (Used for computing 'this' pointer adjustment thunks.
1009 struct MethodInfo {
1010 /// BaseOffset - The base offset of this method.
Ken Dyck82abeae2011-03-27 18:57:53 +00001011 const CharUnits BaseOffset;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001012
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001013 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
1014 /// method.
Ken Dyck82abeae2011-03-27 18:57:53 +00001015 const CharUnits BaseOffsetInLayoutClass;
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001016
Anders Carlsson718d0692010-04-11 22:07:06 +00001017 /// VTableIndex - The index in the vtable that this method has.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001018 /// (For destructors, this is the index of the complete destructor).
Anders Carlsson718d0692010-04-11 22:07:06 +00001019 const uint64_t VTableIndex;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001020
Ken Dyck82abeae2011-03-27 18:57:53 +00001021 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson718d0692010-04-11 22:07:06 +00001022 uint64_t VTableIndex)
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001023 : BaseOffset(BaseOffset),
1024 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
Anders Carlsson718d0692010-04-11 22:07:06 +00001025 VTableIndex(VTableIndex) { }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001026
Ken Dyck82abeae2011-03-27 18:57:53 +00001027 MethodInfo()
1028 : BaseOffset(CharUnits::Zero()),
1029 BaseOffsetInLayoutClass(CharUnits::Zero()),
1030 VTableIndex(0) { }
Anders Carlssonb9021e92010-02-27 16:18:19 +00001031 };
1032
1033 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
1034
1035 /// MethodInfoMap - The information for all methods in the vtable we're
1036 /// currently building.
1037 MethodInfoMapTy MethodInfoMap;
1038
Anders Carlsson718d0692010-04-11 22:07:06 +00001039 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
Anders Carlssondd366812010-03-12 04:54:20 +00001040
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001041 /// VTableThunks - The thunks by vtable index in the vtable currently being
1042 /// built.
Anders Carlsson718d0692010-04-11 22:07:06 +00001043 VTableThunksMapTy VTableThunks;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001044
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001045 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
1046 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001047
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001048 /// Thunks - A map that contains all the thunks needed for all methods in the
1049 /// most derived class for which the vtable is currently being built.
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001050 ThunksMapTy Thunks;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001051
1052 /// AddThunk - Add a thunk for the given method.
Anders Carlsson9d734f32010-03-21 20:27:14 +00001053 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001054
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001055 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
1056 /// part of the vtable we're currently building.
1057 void ComputeThisAdjustments();
1058
Anders Carlssona661a542010-02-16 04:49:44 +00001059 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1060
Anders Carlssonb9021e92010-02-27 16:18:19 +00001061 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
Anders Carlssonaf280c02010-02-23 03:48:14 +00001062 /// some other base.
1063 VisitedVirtualBasesSetTy PrimaryVirtualBases;
1064
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001065 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001066 /// adjustment base offset.
Anders Carlsson2fca9be2010-02-23 01:34:28 +00001067 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001068
Anders Carlsson379017f2010-02-27 19:57:44 +00001069 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
1070 /// the 'this' pointer from the base subobject to the derived subobject.
1071 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1072 BaseSubobject Derived) const;
1073
Anders Carlsson60cc72a2010-02-18 17:00:09 +00001074 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
Anders Carlsson617def32010-03-12 05:28:07 +00001075 /// given virtual member function, its offset in the layout class and its
1076 /// final overrider.
1077 ThisAdjustment
1078 ComputeThisAdjustment(const CXXMethodDecl *MD,
Ken Dyck0e55ade2011-03-27 19:03:44 +00001079 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson617def32010-03-12 05:28:07 +00001080 FinalOverriders::OverriderInfo Overrider);
1081
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001082 /// AddMethod - Add a single virtual member function to the vtable
1083 /// components vector.
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001084 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001085
Anders Carlsson464c8f12010-02-23 04:26:39 +00001086 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
1087 /// part of the vtable.
1088 ///
1089 /// Itanium C++ ABI 2.5.2:
1090 ///
1091 /// struct A { virtual void f(); };
1092 /// struct B : virtual public A { int i; };
1093 /// struct C : virtual public A { int j; };
1094 /// struct D : public B, public C {};
1095 ///
1096 /// When B and C are declared, A is a primary base in each case, so although
1097 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
1098 /// adjustment is required and no thunk is generated. However, inside D
1099 /// objects, A is no longer a primary base of C, so if we allowed calls to
1100 /// C::f() to use the copy of A's vtable in the C subobject, we would need
1101 /// to adjust this from C* to B::A*, which would require a third-party
1102 /// thunk. Since we require that a call to C::f() first convert to A*,
1103 /// C-in-D's copy of A's vtable is never referenced, so this is not
1104 /// necessary.
Anders Carlsson60396612010-03-10 06:51:42 +00001105 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
Ken Dyck413e7d22011-04-02 00:44:58 +00001106 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001107 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001108 CharUnits FirstBaseOffsetInLayoutClass) const;
Anders Carlsson60396612010-03-10 06:51:42 +00001109
Anders Carlsson464c8f12010-02-23 04:26:39 +00001110
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001111 /// AddMethods - Add the methods of this base subobject and all its
1112 /// primary bases to the vtable components vector.
Ken Dyck3a726802011-03-27 19:15:11 +00001113 void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001114 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck3a726802011-03-27 19:15:11 +00001115 CharUnits FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001116 PrimaryBasesSetVectorTy &PrimaryBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001117
Anders Carlsson718d0692010-04-11 22:07:06 +00001118 // LayoutVTable - Layout the vtable for the given base class, including its
Anders Carlsson89ad4212010-02-16 16:26:28 +00001119 // secondary vtables and any vtables for virtual bases.
Anders Carlsson718d0692010-04-11 22:07:06 +00001120 void LayoutVTable();
Anders Carlsson89ad4212010-02-16 16:26:28 +00001121
Anders Carlsson718d0692010-04-11 22:07:06 +00001122 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
Anders Carlsson89ad4212010-02-16 16:26:28 +00001123 /// given base subobject, as well as all its secondary vtables.
Anders Carlsson91232842010-04-11 22:03:57 +00001124 ///
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001125 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1126 /// or a direct or indirect base of a virtual base.
1127 ///
Anders Carlsson91232842010-04-11 22:03:57 +00001128 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
1129 /// in the layout class.
Anders Carlsson718d0692010-04-11 22:07:06 +00001130 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001131 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001132 bool BaseIsVirtualInLayoutClass,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001133 CharUnits OffsetInLayoutClass);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001134
Anders Carlsson718d0692010-04-11 22:07:06 +00001135 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
Anders Carlssonc7b63162010-02-16 16:02:57 +00001136 /// subobject.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001137 ///
1138 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1139 /// or a direct or indirect base of a virtual base.
Anders Carlsson718d0692010-04-11 22:07:06 +00001140 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
Ken Dyck75762b32011-03-30 00:50:28 +00001141 CharUnits OffsetInLayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001142
1143 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
1144 /// class hierarchy.
1145 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Ken Dyck8fe59932011-03-30 01:10:23 +00001146 CharUnits OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001147 VisitedVirtualBasesSetTy &VBases);
1148
Anders Carlsson718d0692010-04-11 22:07:06 +00001149 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001150 /// given base (excluding any primary bases).
Anders Carlsson718d0692010-04-11 22:07:06 +00001151 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001152 VisitedVirtualBasesSetTy &VBases);
1153
Anders Carlsson718d0692010-04-11 22:07:06 +00001154 /// isBuildingConstructionVTable - Return whether this vtable builder is
Anders Carlsson438bc422010-02-28 00:10:58 +00001155 /// building a construction vtable.
Anders Carlsson718d0692010-04-11 22:07:06 +00001156 bool isBuildingConstructorVTable() const {
Anders Carlsson438bc422010-02-28 00:10:58 +00001157 return MostDerivedClass != LayoutClass;
1158 }
1159
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001160public:
Anders Carlsson94464812010-04-10 19:13:06 +00001161 VTableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass,
Ken Dyck23da84a2011-03-27 20:20:28 +00001162 CharUnits MostDerivedClassOffset,
1163 bool MostDerivedClassIsVirtual, const
1164 CXXRecordDecl *LayoutClass)
Anders Carlssonaf440352010-03-23 04:11:45 +00001165 : VTables(VTables), MostDerivedClass(MostDerivedClass),
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001166 MostDerivedClassOffset(MostDerivedClassOffset),
1167 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
1168 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
Ken Dyck23da84a2011-03-27 20:20:28 +00001169 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001170
Anders Carlsson718d0692010-04-11 22:07:06 +00001171 LayoutVTable();
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001172 }
1173
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00001174 ThunksMapTy::const_iterator thunks_begin() const {
1175 return Thunks.begin();
1176 }
1177
1178 ThunksMapTy::const_iterator thunks_end() const {
1179 return Thunks.end();
1180 }
1181
Anders Carlssonccd83d72010-03-24 16:42:11 +00001182 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1183 return VBaseOffsetOffsets;
1184 }
1185
1186 /// getNumVTableComponents - Return the number of components in the vtable
1187 /// currently built.
1188 uint64_t getNumVTableComponents() const {
1189 return Components.size();
1190 }
1191
Anders Carlsson66d567d2010-03-25 00:51:13 +00001192 const uint64_t *vtable_components_data_begin() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001193 return reinterpret_cast<const uint64_t *>(Components.begin());
1194 }
1195
Anders Carlsson66d567d2010-03-25 00:51:13 +00001196 const uint64_t *vtable_components_data_end() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001197 return reinterpret_cast<const uint64_t *>(Components.end());
1198 }
1199
Anders Carlsson66d567d2010-03-25 00:51:13 +00001200 AddressPointsMapTy::const_iterator address_points_begin() const {
1201 return AddressPoints.begin();
1202 }
1203
1204 AddressPointsMapTy::const_iterator address_points_end() const {
1205 return AddressPoints.end();
1206 }
1207
Anders Carlsson718d0692010-04-11 22:07:06 +00001208 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001209 return VTableThunks.begin();
1210 }
1211
Anders Carlsson718d0692010-04-11 22:07:06 +00001212 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001213 return VTableThunks.end();
1214 }
1215
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001216 /// dumpLayout - Dump the vtable layout.
1217 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001218};
1219
Anders Carlsson94464812010-04-10 19:13:06 +00001220void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
Anders Carlsson718d0692010-04-11 22:07:06 +00001221 assert(!isBuildingConstructorVTable() &&
Anders Carlssonbca5d372010-03-22 20:06:40 +00001222 "Can't add thunks for construction vtable");
Anders Carlsson9135a842010-03-18 02:44:19 +00001223
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001224 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001225
1226 // Check if we have this thunk already.
1227 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1228 ThunksVector.end())
1229 return;
1230
1231 ThunksVector.push_back(Thunk);
1232}
1233
Anders Carlssonf622b452010-04-10 20:39:29 +00001234typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1235
1236/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1237/// the overridden methods that the function decl overrides.
1238static void
1239ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1240 OverriddenMethodsSetTy& OverriddenMethods) {
1241 assert(MD->isVirtual() && "Method is not virtual!");
1242
Anders Carlsson57071e22010-02-12 05:25:12 +00001243 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1244 E = MD->end_overridden_methods(); I != E; ++I) {
1245 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001246
Anders Carlssonf622b452010-04-10 20:39:29 +00001247 OverriddenMethods.insert(OverriddenMD);
1248
1249 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods);
Anders Carlsson57071e22010-02-12 05:25:12 +00001250 }
Anders Carlsson57071e22010-02-12 05:25:12 +00001251}
1252
Anders Carlsson94464812010-04-10 19:13:06 +00001253void VTableBuilder::ComputeThisAdjustments() {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001254 // Now go through the method info map and see if any of the methods need
1255 // 'this' pointer adjustments.
1256 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1257 E = MethodInfoMap.end(); I != E; ++I) {
1258 const CXXMethodDecl *MD = I->first;
1259 const MethodInfo &MethodInfo = I->second;
1260
Anders Carlsson617def32010-03-12 05:28:07 +00001261 // Ignore adjustments for unused function pointers.
Anders Carlsson718d0692010-04-11 22:07:06 +00001262 uint64_t VTableIndex = MethodInfo.VTableIndex;
1263 if (Components[VTableIndex].getKind() ==
Anders Carlsson94464812010-04-10 19:13:06 +00001264 VTableComponent::CK_UnusedFunctionPointer)
Anders Carlsson617def32010-03-12 05:28:07 +00001265 continue;
1266
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001267 // Get the final overrider for this method.
1268 FinalOverriders::OverriderInfo Overrider =
Ken Dyck82abeae2011-03-27 18:57:53 +00001269 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001270
Anders Carlssonbca5d372010-03-22 20:06:40 +00001271 // Check if we need an adjustment at all.
Ken Dyck82abeae2011-03-27 18:57:53 +00001272 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
Anders Carlssonadb507d2010-03-29 15:08:41 +00001273 // When a return thunk is needed by a derived class that overrides a
1274 // virtual base, gcc uses a virtual 'this' adjustment as well.
1275 // While the thunk itself might be needed by vtables in subclasses or
1276 // in construction vtables, there doesn't seem to be a reason for using
1277 // the thunk in this vtable. Still, we do so to match gcc.
Anders Carlsson718d0692010-04-11 22:07:06 +00001278 if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
Anders Carlssonadb507d2010-03-29 15:08:41 +00001279 continue;
1280 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001281
Anders Carlsson617def32010-03-12 05:28:07 +00001282 ThisAdjustment ThisAdjustment =
Ken Dyck0e55ade2011-03-27 19:03:44 +00001283 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
Anders Carlsson617def32010-03-12 05:28:07 +00001284
1285 if (ThisAdjustment.isEmpty())
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001286 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001287
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001288 // Add it.
Anders Carlsson718d0692010-04-11 22:07:06 +00001289 VTableThunks[VTableIndex].This = ThisAdjustment;
Anders Carlsson617def32010-03-12 05:28:07 +00001290
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001291 if (isa<CXXDestructorDecl>(MD)) {
1292 // Add an adjustment for the deleting destructor as well.
Anders Carlsson718d0692010-04-11 22:07:06 +00001293 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001294 }
1295 }
1296
1297 /// Clear the method info map.
1298 MethodInfoMap.clear();
Anders Carlsson25394212010-03-22 15:47:01 +00001299
Anders Carlsson718d0692010-04-11 22:07:06 +00001300 if (isBuildingConstructorVTable()) {
Anders Carlsson25394212010-03-22 15:47:01 +00001301 // We don't need to store thunk information for construction vtables.
1302 return;
1303 }
1304
Anders Carlsson718d0692010-04-11 22:07:06 +00001305 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(),
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001306 E = VTableThunks.end(); I != E; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00001307 const VTableComponent &Component = Components[I->first];
Anders Carlsson25394212010-03-22 15:47:01 +00001308 const ThunkInfo &Thunk = I->second;
Anders Carlssonbca5d372010-03-22 20:06:40 +00001309 const CXXMethodDecl *MD;
Anders Carlsson25394212010-03-22 15:47:01 +00001310
1311 switch (Component.getKind()) {
1312 default:
1313 llvm_unreachable("Unexpected vtable component kind!");
Anders Carlsson94464812010-04-10 19:13:06 +00001314 case VTableComponent::CK_FunctionPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001315 MD = Component.getFunctionDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001316 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001317 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001318 MD = Component.getDestructorDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001319 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001320 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson25394212010-03-22 15:47:01 +00001321 // We've already added the thunk when we saw the complete dtor pointer.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001322 continue;
Anders Carlsson25394212010-03-22 15:47:01 +00001323 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001324
1325 if (MD->getParent() == MostDerivedClass)
1326 AddThunk(MD, Thunk);
Anders Carlsson25394212010-03-22 15:47:01 +00001327 }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001328}
1329
Anders Carlsson94464812010-04-10 19:13:06 +00001330ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001331 ReturnAdjustment Adjustment;
1332
1333 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001334 if (Offset.VirtualBase) {
1335 // Get the virtual base offset offset.
Anders Carlsson1972ced2010-03-11 07:00:45 +00001336 if (Offset.DerivedClass == MostDerivedClass) {
1337 // We can get the offset offset directly from our map.
1338 Adjustment.VBaseOffsetOffset =
Ken Dyckd28c33c2011-04-07 00:55:01 +00001339 VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
Anders Carlsson1972ced2010-03-11 07:00:45 +00001340 } else {
1341 Adjustment.VBaseOffsetOffset =
Anders Carlssonaf440352010-03-23 04:11:45 +00001342 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
Ken Dyck14c65ca2011-04-07 12:37:09 +00001343 Offset.VirtualBase).getQuantity();
Anders Carlsson1972ced2010-03-11 07:00:45 +00001344 }
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001345 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001346
Ken Dyck396e4f62011-04-02 01:32:03 +00001347 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001348 }
1349
1350 return Adjustment;
1351}
1352
Anders Carlsson379017f2010-02-27 19:57:44 +00001353BaseOffset
Anders Carlsson94464812010-04-10 19:13:06 +00001354VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
Anders Carlsson379017f2010-02-27 19:57:44 +00001355 BaseSubobject Derived) const {
1356 const CXXRecordDecl *BaseRD = Base.getBase();
1357 const CXXRecordDecl *DerivedRD = Derived.getBase();
1358
1359 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1360 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1361
1362 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
1363 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
1364 assert(false && "Class must be derived from the passed in base class!");
1365 return BaseOffset();
1366 }
1367
1368 // We have to go through all the paths, and see which one leads us to the
1369 // right base subobject.
1370 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1371 I != E; ++I) {
1372 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1373
Ken Dyck396e4f62011-04-02 01:32:03 +00001374 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
Anders Carlsson379017f2010-02-27 19:57:44 +00001375
1376 if (Offset.VirtualBase) {
1377 // If we have a virtual base class, the non-virtual offset is relative
1378 // to the virtual base class offset.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001379 const ASTRecordLayout &LayoutClassLayout =
1380 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson379017f2010-02-27 19:57:44 +00001381
1382 /// Get the virtual base offset, relative to the most derived class
1383 /// layout.
1384 OffsetToBaseSubobject +=
Ken Dyck57ebe332011-03-26 00:51:24 +00001385 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
Anders Carlsson379017f2010-02-27 19:57:44 +00001386 } else {
1387 // Otherwise, the non-virtual offset is relative to the derived class
1388 // offset.
Ken Dyck57ebe332011-03-26 00:51:24 +00001389 OffsetToBaseSubobject += Derived.getBaseOffset();
Anders Carlsson379017f2010-02-27 19:57:44 +00001390 }
1391
1392 // Check if this path gives us the right base subobject.
Ken Dyck57ebe332011-03-26 00:51:24 +00001393 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
Anders Carlsson379017f2010-02-27 19:57:44 +00001394 // Since we're going from the base class _to_ the derived class, we'll
1395 // invert the non-virtual offset here.
1396 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1397 return Offset;
1398 }
1399 }
1400
1401 return BaseOffset();
1402}
1403
Anders Carlsson80d8d7d2010-03-23 15:13:06 +00001404ThisAdjustment
Anders Carlsson94464812010-04-10 19:13:06 +00001405VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD,
Ken Dyck0e55ade2011-03-27 19:03:44 +00001406 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson617def32010-03-12 05:28:07 +00001407 FinalOverriders::OverriderInfo Overrider) {
Anders Carlsson617def32010-03-12 05:28:07 +00001408 // Ignore adjustments for pure virtual member functions.
1409 if (Overrider.Method->isPure())
1410 return ThisAdjustment();
1411
Ken Dyck0e55ade2011-03-27 19:03:44 +00001412 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1413 BaseOffsetInLayoutClass);
Anders Carlsson617def32010-03-12 05:28:07 +00001414
1415 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
Ken Dyck17d85192011-03-26 01:29:16 +00001416 Overrider.Offset);
Anders Carlsson617def32010-03-12 05:28:07 +00001417
1418 // Compute the adjustment offset.
1419 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1420 OverriderBaseSubobject);
1421 if (Offset.isEmpty())
1422 return ThisAdjustment();
1423
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001424 ThisAdjustment Adjustment;
1425
Anders Carlsson617def32010-03-12 05:28:07 +00001426 if (Offset.VirtualBase) {
1427 // Get the vcall offset map for this virtual base.
1428 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
Anders Carlssoneb577d02010-02-18 17:26:40 +00001429
Anders Carlsson617def32010-03-12 05:28:07 +00001430 if (VCallOffsets.empty()) {
1431 // We don't have vcall offsets for this virtual base, go ahead and
1432 // build them.
1433 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
1434 /*FinalOverriders=*/0,
Ken Dyck4230d522011-03-24 01:21:01 +00001435 BaseSubobject(Offset.VirtualBase,
1436 CharUnits::Zero()),
Anders Carlsson617def32010-03-12 05:28:07 +00001437 /*BaseIsVirtual=*/true,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00001438 /*OffsetInLayoutClass=*/
1439 CharUnits::Zero());
Anders Carlsson617def32010-03-12 05:28:07 +00001440
1441 VCallOffsets = Builder.getVCallOffsets();
1442 }
1443
Ken Dyckc952d382011-04-02 00:51:15 +00001444 Adjustment.VCallOffsetOffset =
1445 VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001446 }
Anders Carlsson617def32010-03-12 05:28:07 +00001447
1448 // Set the non-virtual part of the adjustment.
Ken Dyck396e4f62011-04-02 01:32:03 +00001449 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001450
1451 return Adjustment;
1452}
Anders Carlsson617def32010-03-12 05:28:07 +00001453
Anders Carlsson57071e22010-02-12 05:25:12 +00001454void
Anders Carlsson94464812010-04-10 19:13:06 +00001455VTableBuilder::AddMethod(const CXXMethodDecl *MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001456 ReturnAdjustment ReturnAdjustment) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001457 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1458 assert(ReturnAdjustment.isEmpty() &&
1459 "Destructor can't have return adjustment!");
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001460
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001461 // Add both the complete destructor and the deleting destructor.
Anders Carlsson94464812010-04-10 19:13:06 +00001462 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1463 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001464 } else {
1465 // Add the return adjustment if necessary.
1466 if (!ReturnAdjustment.isEmpty())
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001467 VTableThunks[Components.size()].Return = ReturnAdjustment;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001468
1469 // Add the function.
Anders Carlsson94464812010-04-10 19:13:06 +00001470 Components.push_back(VTableComponent::MakeFunction(MD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001471 }
1472}
1473
Anders Carlssonc784ba22010-02-27 06:38:03 +00001474/// OverridesIndirectMethodInBase - Return whether the given member function
1475/// overrides any methods in the set of given bases.
1476/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1477/// For example, if we have:
1478///
1479/// struct A { virtual void f(); }
1480/// struct B : A { virtual void f(); }
1481/// struct C : B { virtual void f(); }
1482///
1483/// OverridesIndirectMethodInBase will return true if given C::f as the method
Anders Carlssonf622b452010-04-10 20:39:29 +00001484/// and { A } as the set of bases.
Anders Carlssonc784ba22010-02-27 06:38:03 +00001485static bool
1486OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001487 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonfbf05612010-04-17 17:24:33 +00001488 if (Bases.count(MD->getParent()))
1489 return true;
1490
Anders Carlssonc784ba22010-02-27 06:38:03 +00001491 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1492 E = MD->end_overridden_methods(); I != E; ++I) {
1493 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonc784ba22010-02-27 06:38:03 +00001494
1495 // Check "indirect overriders".
1496 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1497 return true;
1498 }
1499
1500 return false;
1501}
1502
Anders Carlsson464c8f12010-02-23 04:26:39 +00001503bool
Anders Carlsson94464812010-04-10 19:13:06 +00001504VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
Ken Dyck413e7d22011-04-02 00:44:58 +00001505 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001506 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001507 CharUnits FirstBaseOffsetInLayoutClass) const {
Anders Carlsson464c8f12010-02-23 04:26:39 +00001508 // If the base and the first base in the primary base chain have the same
1509 // offsets, then this overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001510 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001511 return true;
1512
1513 // We know now that Base (or a direct or indirect base of it) is a primary
1514 // base in part of the class hierarchy, but not a primary base in the most
1515 // derived class.
1516
1517 // If the overrider is the first base in the primary base chain, we know
1518 // that the overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001519 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
Anders Carlsson464c8f12010-02-23 04:26:39 +00001520 return true;
1521
Anders Carlsson94464812010-04-10 19:13:06 +00001522 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001523
Anders Carlsson60396612010-03-10 06:51:42 +00001524 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001525 PrimaryBases.insert(RD);
1526
1527 // Now traverse the base chain, starting with the first base, until we find
1528 // the base that is no longer a primary base.
1529 while (true) {
1530 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1531 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1532
1533 if (!PrimaryBase)
1534 break;
1535
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001536 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001537 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001538 "Primary base should always be at offset 0!");
1539
Anders Carlssondad0f992010-02-28 18:37:33 +00001540 const ASTRecordLayout &LayoutClassLayout =
1541 Context.getASTRecordLayout(LayoutClass);
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001542
1543 // Now check if this is the primary base that is not a primary base in the
1544 // most derived class.
Ken Dyck413e7d22011-04-02 00:44:58 +00001545 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
Anders Carlsson60396612010-03-10 06:51:42 +00001546 FirstBaseOffsetInLayoutClass) {
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001547 // We found it, stop walking the chain.
1548 break;
1549 }
1550 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001551 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001552 "Primary base should always be at offset 0!");
1553 }
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001554
Anders Carlssonf89bb6c2010-02-27 19:51:04 +00001555 if (!PrimaryBases.insert(PrimaryBase))
1556 assert(false && "Found a duplicate primary base!");
1557
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001558 RD = PrimaryBase;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001559 }
1560
1561 // If the final overrider is an override of one of the primary bases,
1562 // then we know that it will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001563 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
Anders Carlsson464c8f12010-02-23 04:26:39 +00001564}
1565
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001566/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1567/// from the nearest base. Returns null if no method was found.
1568static const CXXMethodDecl *
1569FindNearestOverriddenMethod(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001570 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonf622b452010-04-10 20:39:29 +00001571 OverriddenMethodsSetTy OverriddenMethods;
1572 ComputeAllOverriddenMethods(MD, OverriddenMethods);
1573
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001574 for (int I = Bases.size(), E = 0; I != E; --I) {
1575 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1576
1577 // Now check the overriden methods.
Anders Carlssonf622b452010-04-10 20:39:29 +00001578 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1579 E = OverriddenMethods.end(); I != E; ++I) {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001580 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonf622b452010-04-10 20:39:29 +00001581
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001582 // We found our overridden method.
1583 if (OverriddenMD->getParent() == PrimaryBase)
1584 return OverriddenMD;
1585 }
1586 }
1587
1588 return 0;
1589}
1590
Anders Carlsson60396612010-03-10 06:51:42 +00001591void
Ken Dyck3a726802011-03-27 19:15:11 +00001592VTableBuilder::AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001593 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck3a726802011-03-27 19:15:11 +00001594 CharUnits FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001595 PrimaryBasesSetVectorTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +00001596 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001597 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1598
1599 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Ken Dyck57ebe332011-03-26 00:51:24 +00001600 CharUnits PrimaryBaseOffset;
Ken Dyck3a726802011-03-27 19:15:11 +00001601 CharUnits PrimaryBaseOffsetInLayoutClass;
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001602 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001603 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf280c02010-02-23 03:48:14 +00001604 "Primary vbase should have a zero offset!");
1605
1606 const ASTRecordLayout &MostDerivedClassLayout =
1607 Context.getASTRecordLayout(MostDerivedClass);
1608
Anders Carlsson60396612010-03-10 06:51:42 +00001609 PrimaryBaseOffset =
Ken Dyck57ebe332011-03-26 00:51:24 +00001610 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlsson60396612010-03-10 06:51:42 +00001611
1612 const ASTRecordLayout &LayoutClassLayout =
1613 Context.getASTRecordLayout(LayoutClass);
1614
1615 PrimaryBaseOffsetInLayoutClass =
Ken Dyck3a726802011-03-27 19:15:11 +00001616 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssonaf280c02010-02-23 03:48:14 +00001617 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001618 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlsson57071e22010-02-12 05:25:12 +00001619 "Primary base should have a zero offset!");
Anders Carlssonaf280c02010-02-23 03:48:14 +00001620
Ken Dyck57ebe332011-03-26 00:51:24 +00001621 PrimaryBaseOffset = Base.getBaseOffset();
Anders Carlsson60396612010-03-10 06:51:42 +00001622 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
Anders Carlssonaf280c02010-02-23 03:48:14 +00001623 }
Anders Carlssondad0f992010-02-28 18:37:33 +00001624
Ken Dyck57ebe332011-03-26 00:51:24 +00001625 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
Anders Carlsson60396612010-03-10 06:51:42 +00001626 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1627 FirstBaseOffsetInLayoutClass, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001628
1629 if (!PrimaryBases.insert(PrimaryBase))
1630 assert(false && "Found a duplicate primary base!");
1631 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001632
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001633 // Now go through all virtual member functions and add them.
1634 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1635 E = RD->method_end(); I != E; ++I) {
1636 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001637
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001638 if (!MD->isVirtual())
1639 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +00001640
1641 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +00001642 FinalOverriders::OverriderInfo Overrider =
Ken Dycke6203132011-03-26 01:18:17 +00001643 Overriders.getOverrider(MD, Base.getBaseOffset());
Anders Carlsson57071e22010-02-12 05:25:12 +00001644
1645 // Check if this virtual member function overrides a method in a primary
1646 // base. If this is the case, and the return type doesn't require adjustment
1647 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001648 if (const CXXMethodDecl *OverriddenMD =
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001649 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlssondf31af12010-02-13 23:17:31 +00001650 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001651 OverriddenMD).isEmpty()) {
1652 // Replace the method info of the overridden method with our own
1653 // method.
1654 assert(MethodInfoMap.count(OverriddenMD) &&
1655 "Did not find the overridden method!");
1656 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1657
Ken Dyck3a726802011-03-27 19:15:11 +00001658 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1659 OverriddenMethodInfo.VTableIndex);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001660
1661 assert(!MethodInfoMap.count(MD) &&
1662 "Should not have method info for this method yet!");
1663
1664 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1665 MethodInfoMap.erase(OverriddenMD);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001666
1667 // If the overridden method exists in a virtual base class or a direct
1668 // or indirect base class of a virtual base class, we need to emit a
1669 // thunk if we ever have a class hierarchy where the base class is not
1670 // a primary base in the complete object.
Anders Carlsson718d0692010-04-11 22:07:06 +00001671 if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
Anders Carlssonbca5d372010-03-22 20:06:40 +00001672 // Compute the this adjustment.
1673 ThisAdjustment ThisAdjustment =
Ken Dyck3a726802011-03-27 19:15:11 +00001674 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1675 Overrider);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001676
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001677 if (ThisAdjustment.VCallOffsetOffset &&
1678 Overrider.Method->getParent() == MostDerivedClass) {
John McCall4e3b17c2010-11-09 01:18:05 +00001679
1680 // There's no return adjustment from OverriddenMD and MD,
1681 // but that doesn't mean there isn't one between MD and
1682 // the final overrider.
1683 BaseOffset ReturnAdjustmentOffset =
1684 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1685 ReturnAdjustment ReturnAdjustment =
1686 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1687
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001688 // This is a virtual thunk for the most derived class, add it.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001689 AddThunk(Overrider.Method,
John McCall4e3b17c2010-11-09 01:18:05 +00001690 ThunkInfo(ThisAdjustment, ReturnAdjustment));
Anders Carlssonbca5d372010-03-22 20:06:40 +00001691 }
1692 }
1693
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001694 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001695 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001696 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00001697
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001698 // Insert the method info for this method.
Ken Dyck3a726802011-03-27 19:15:11 +00001699 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
Ken Dyck82abeae2011-03-27 18:57:53 +00001700 Components.size());
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001701
1702 assert(!MethodInfoMap.count(MD) &&
1703 "Should not have method info for this method yet!");
1704 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001705
1706 // Check if this overrider is going to be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001707 const CXXMethodDecl *OverriderMD = Overrider.Method;
Ken Dyck413e7d22011-04-02 00:44:58 +00001708 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001709 FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001710 FirstBaseOffsetInLayoutClass)) {
Anders Carlsson94464812010-04-10 19:13:06 +00001711 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001712 continue;
1713 }
Anders Carlsson464c8f12010-02-23 04:26:39 +00001714
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001715 // Check if this overrider needs a return adjustment.
Anders Carlsson7097e752010-06-01 23:47:05 +00001716 // We don't want to do this for pure virtual member functions.
1717 BaseOffset ReturnAdjustmentOffset;
1718 if (!OverriderMD->isPure()) {
1719 ReturnAdjustmentOffset =
1720 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1721 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001722
1723 ReturnAdjustment ReturnAdjustment =
1724 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001725
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001726 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001727 }
1728}
1729
Anders Carlsson718d0692010-04-11 22:07:06 +00001730void VTableBuilder::LayoutVTable() {
Ken Dyck4230d522011-03-24 01:21:01 +00001731 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1732 CharUnits::Zero()),
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001733 /*BaseIsMorallyVirtual=*/false,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001734 MostDerivedClassIsVirtual,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001735 MostDerivedClassOffset);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001736
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001737 VisitedVirtualBasesSetTy VBases;
Anders Carlsson08c26752010-02-27 04:05:52 +00001738
1739 // Determine the primary virtual bases.
Ken Dyck8fe59932011-03-30 01:10:23 +00001740 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1741 VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001742 VBases.clear();
1743
Anders Carlsson718d0692010-04-11 22:07:06 +00001744 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001745}
1746
Anders Carlsson530c40c2010-02-28 01:43:58 +00001747void
Anders Carlsson718d0692010-04-11 22:07:06 +00001748VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001749 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001750 bool BaseIsVirtualInLayoutClass,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001751 CharUnits OffsetInLayoutClass) {
Chris Lattner04a23112010-02-17 21:42:34 +00001752 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
Anders Carlssonb828afa2010-02-14 17:05:59 +00001753
Anders Carlsson852213e2010-02-16 04:59:55 +00001754 // Add vcall and vbase offsets for this vtable.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001755 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
Anders Carlsson91232842010-04-11 22:03:57 +00001756 Base, BaseIsVirtualInLayoutClass,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00001757 OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001758 Components.append(Builder.components_begin(), Builder.components_end());
1759
Anders Carlssondba6ef82010-02-25 22:23:13 +00001760 // Check if we need to add these vcall offsets.
Anders Carlsson91232842010-04-11 22:03:57 +00001761 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
Anders Carlssondba6ef82010-02-25 22:23:13 +00001762 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1763
1764 if (VCallOffsets.empty())
1765 VCallOffsets = Builder.getVCallOffsets();
1766 }
1767
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001768 // If we're laying out the most derived class we want to keep track of the
1769 // virtual base class offset offsets.
1770 if (Base.getBase() == MostDerivedClass)
1771 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1772
Anders Carlsson852213e2010-02-16 04:59:55 +00001773 // Add the offset to top.
Ken Dyck7ba8f522011-03-27 20:33:38 +00001774 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1775 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +00001776 VTableComponent::MakeOffsetToTop(OffsetToTop));
Anders Carlsson57071e22010-02-12 05:25:12 +00001777
1778 // Next, add the RTTI.
Anders Carlsson94464812010-04-10 19:13:06 +00001779 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Anders Carlsson57071e22010-02-12 05:25:12 +00001780
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001781 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +00001782
1783 // Now go through all virtual member functions and add them.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001784 PrimaryBasesSetVectorTy PrimaryBases;
Ken Dyck7ba8f522011-03-27 20:33:38 +00001785 AddMethods(Base, OffsetInLayoutClass,
1786 Base.getBase(), OffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001787 PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001788
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001789 // Compute 'this' pointer adjustments.
1790 ComputeThisAdjustments();
1791
Anders Carlsson014a3582010-03-25 21:45:14 +00001792 // Add all address points.
1793 const CXXRecordDecl *RD = Base.getBase();
1794 while (true) {
Ken Dyck4230d522011-03-24 01:21:01 +00001795 AddressPoints.insert(std::make_pair(
Ken Dyck7ba8f522011-03-27 20:33:38 +00001796 BaseSubobject(RD, OffsetInLayoutClass),
Ken Dyck4230d522011-03-24 01:21:01 +00001797 AddressPoint));
Anders Carlsson014a3582010-03-25 21:45:14 +00001798
1799 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1800 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001801
Anders Carlsson014a3582010-03-25 21:45:14 +00001802 if (!PrimaryBase)
1803 break;
1804
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001805 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson014a3582010-03-25 21:45:14 +00001806 // Check if this virtual primary base is a primary base in the layout
1807 // class. If it's not, we don't want to add it.
1808 const ASTRecordLayout &LayoutClassLayout =
1809 Context.getASTRecordLayout(LayoutClass);
1810
Ken Dyck7ba8f522011-03-27 20:33:38 +00001811 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
Anders Carlsson014a3582010-03-25 21:45:14 +00001812 OffsetInLayoutClass) {
1813 // We don't want to add this class (or any of its primary bases).
1814 break;
1815 }
1816 }
1817
1818 RD = PrimaryBase;
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001819 }
1820
Anders Carlsson5edcc372010-02-24 16:43:12 +00001821 // Layout secondary vtables.
Ken Dyck75762b32011-03-30 00:50:28 +00001822 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001823}
1824
Anders Carlsson718d0692010-04-11 22:07:06 +00001825void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
Anders Carlssona96a2e92010-03-10 19:39:11 +00001826 bool BaseIsMorallyVirtual,
Ken Dyck75762b32011-03-30 00:50:28 +00001827 CharUnits OffsetInLayoutClass) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001828 // Itanium C++ ABI 2.5.2:
1829 // Following the primary virtual table of a derived class are secondary
1830 // virtual tables for each of its proper base classes, except any primary
1831 // base(s) with which it shares its primary virtual table.
1832
1833 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001834 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1835 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc7b63162010-02-16 16:02:57 +00001836
Anders Carlsson57071e22010-02-12 05:25:12 +00001837 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1838 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001839 // Ignore virtual bases, we'll emit them later.
1840 if (I->isVirtual())
1841 continue;
1842
Anders Carlsson57071e22010-02-12 05:25:12 +00001843 const CXXRecordDecl *BaseDecl =
1844 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb828afa2010-02-14 17:05:59 +00001845
Anders Carlssona661a542010-02-16 04:49:44 +00001846 // Ignore bases that don't have a vtable.
1847 if (!BaseDecl->isDynamicClass())
1848 continue;
Anders Carlssonb828afa2010-02-14 17:05:59 +00001849
Anders Carlsson718d0692010-04-11 22:07:06 +00001850 if (isBuildingConstructorVTable()) {
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001851 // Itanium C++ ABI 2.6.4:
1852 // Some of the base class subobjects may not need construction virtual
1853 // tables, which will therefore not be present in the construction
1854 // virtual table group, even though the subobject virtual tables are
1855 // present in the main virtual table group for the complete object.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001856 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001857 continue;
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001858 }
1859
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001860 // Get the base offset of this base.
Ken Dyck57ebe332011-03-26 00:51:24 +00001861 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1862 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
Anders Carlsson530c40c2010-02-28 01:43:58 +00001863
Ken Dyck57ebe332011-03-26 00:51:24 +00001864 CharUnits BaseOffsetInLayoutClass =
Ken Dyck75762b32011-03-30 00:50:28 +00001865 OffsetInLayoutClass + RelativeBaseOffset;
Anders Carlssonc7b63162010-02-16 16:02:57 +00001866
1867 // Don't emit a secondary vtable for a primary base. We might however want
1868 // to emit secondary vtables for other bases of this base.
1869 if (BaseDecl == PrimaryBase) {
Ken Dyck75762b32011-03-30 00:50:28 +00001870 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1871 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001872 continue;
1873 }
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001874
Anders Carlsson89ad4212010-02-16 16:26:28 +00001875 // Layout the primary vtable (and any secondary vtables) for this base.
Ken Dyck4230d522011-03-24 01:21:01 +00001876 LayoutPrimaryAndSecondaryVTables(
Ken Dyck57ebe332011-03-26 00:51:24 +00001877 BaseSubobject(BaseDecl, BaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +00001878 BaseIsMorallyVirtual,
1879 /*BaseIsVirtualInLayoutClass=*/false,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001880 BaseOffsetInLayoutClass);
Anders Carlsson57071e22010-02-12 05:25:12 +00001881 }
1882}
1883
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001884void
Anders Carlsson94464812010-04-10 19:13:06 +00001885VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Ken Dyck8fe59932011-03-30 01:10:23 +00001886 CharUnits OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001887 VisitedVirtualBasesSetTy &VBases) {
1888 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1889
1890 // Check if this base has a primary base.
1891 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001892
1893 // Check if it's virtual.
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001894 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001895 bool IsPrimaryVirtualBase = true;
1896
Anders Carlsson718d0692010-04-11 22:07:06 +00001897 if (isBuildingConstructorVTable()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001898 // Check if the base is actually a primary base in the class we use for
1899 // layout.
Anders Carlssond7fdae52010-03-10 03:02:01 +00001900 const ASTRecordLayout &LayoutClassLayout =
1901 Context.getASTRecordLayout(LayoutClass);
1902
Ken Dyck8fe59932011-03-30 01:10:23 +00001903 CharUnits PrimaryBaseOffsetInLayoutClass =
1904 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001905
1906 // We know that the base is not a primary base in the layout class if
1907 // the base offsets are different.
1908 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
Anders Carlsson0378bf02010-02-28 18:08:38 +00001909 IsPrimaryVirtualBase = false;
1910 }
1911
1912 if (IsPrimaryVirtualBase)
1913 PrimaryVirtualBases.insert(PrimaryBase);
1914 }
Anders Carlsson08c26752010-02-27 04:05:52 +00001915 }
1916
1917 // Traverse bases, looking for more primary virtual bases.
1918 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1919 E = RD->bases_end(); I != E; ++I) {
1920 const CXXRecordDecl *BaseDecl =
1921 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1922
Ken Dyck8fe59932011-03-30 01:10:23 +00001923 CharUnits BaseOffsetInLayoutClass;
Anders Carlssond7fdae52010-03-10 03:02:01 +00001924
1925 if (I->isVirtual()) {
1926 if (!VBases.insert(BaseDecl))
1927 continue;
1928
1929 const ASTRecordLayout &LayoutClassLayout =
1930 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001931
Anders Carlssona14f5972010-10-31 23:22:37 +00001932 BaseOffsetInLayoutClass =
Ken Dyck8fe59932011-03-30 01:10:23 +00001933 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001934 } else {
1935 BaseOffsetInLayoutClass =
Ken Dyck8fe59932011-03-30 01:10:23 +00001936 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001937 }
1938
1939 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001940 }
1941}
1942
1943void
Anders Carlsson718d0692010-04-11 22:07:06 +00001944VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001945 VisitedVirtualBasesSetTy &VBases) {
1946 // Itanium C++ ABI 2.5.2:
1947 // Then come the virtual base virtual tables, also in inheritance graph
1948 // order, and again excluding primary bases (which share virtual tables with
1949 // the classes for which they are primary).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001950 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1951 E = RD->bases_end(); I != E; ++I) {
1952 const CXXRecordDecl *BaseDecl =
1953 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1954
Anders Carlssonaf280c02010-02-23 03:48:14 +00001955 // Check if this base needs a vtable. (If it's virtual, not a primary base
1956 // of some other class, and we haven't visited it before).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001957 if (I->isVirtual() && BaseDecl->isDynamicClass() &&
Anders Carlsson08c26752010-02-27 04:05:52 +00001958 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001959 const ASTRecordLayout &MostDerivedClassLayout =
1960 Context.getASTRecordLayout(MostDerivedClass);
Ken Dyck7262e902011-03-30 01:14:22 +00001961 CharUnits BaseOffset =
1962 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001963
Anders Carlsson530c40c2010-02-28 01:43:58 +00001964 const ASTRecordLayout &LayoutClassLayout =
1965 Context.getASTRecordLayout(LayoutClass);
Ken Dyck7ba8f522011-03-27 20:33:38 +00001966 CharUnits BaseOffsetInLayoutClass =
1967 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson530c40c2010-02-28 01:43:58 +00001968
Ken Dyck4230d522011-03-24 01:21:01 +00001969 LayoutPrimaryAndSecondaryVTables(
Ken Dyck7262e902011-03-30 01:14:22 +00001970 BaseSubobject(BaseDecl, BaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +00001971 /*BaseIsMorallyVirtual=*/true,
1972 /*BaseIsVirtualInLayoutClass=*/true,
1973 BaseOffsetInLayoutClass);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001974 }
1975
1976 // We only need to check the base for virtual base vtables if it actually
1977 // has virtual bases.
1978 if (BaseDecl->getNumVBases())
Anders Carlsson718d0692010-04-11 22:07:06 +00001979 LayoutVTablesForVirtualBases(BaseDecl, VBases);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001980 }
1981}
1982
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001983/// dumpLayout - Dump the vtable layout.
Anders Carlsson94464812010-04-10 19:13:06 +00001984void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001985
Anders Carlsson718d0692010-04-11 22:07:06 +00001986 if (isBuildingConstructorVTable()) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001987 Out << "Construction vtable for ('";
1988 Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
Ken Dyck23da84a2011-03-27 20:20:28 +00001989 Out << MostDerivedClassOffset.getQuantity() << ") in '";
Anders Carlssond03a3262010-02-27 20:39:05 +00001990 Out << LayoutClass->getQualifiedNameAsString();
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001991 } else {
1992 Out << "Vtable for '";
1993 Out << MostDerivedClass->getQualifiedNameAsString();
Anders Carlssond03a3262010-02-27 20:39:05 +00001994 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001995 Out << "' (" << Components.size() << " entries).\n";
1996
Anders Carlsson822307b2010-02-11 17:18:51 +00001997 // Iterate through the address points and insert them into a new map where
1998 // they are keyed by the index and not the base object.
1999 // Since an address point can be shared by multiple subobjects, we use an
2000 // STL multimap.
2001 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
Anders Carlssonff143f82010-03-25 00:35:49 +00002002 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
2003 E = AddressPoints.end(); I != E; ++I) {
Anders Carlsson822307b2010-02-11 17:18:51 +00002004 const BaseSubobject& Base = I->first;
2005 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002006
Anders Carlsson822307b2010-02-11 17:18:51 +00002007 AddressPointsByIndex.insert(std::make_pair(Index, Base));
2008 }
2009
2010 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00002011 uint64_t Index = I;
Anders Carlsson822307b2010-02-11 17:18:51 +00002012
2013 Out << llvm::format("%4d | ", I);
2014
Anders Carlsson94464812010-04-10 19:13:06 +00002015 const VTableComponent &Component = Components[I];
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002016
2017 // Dump the component.
2018 switch (Component.getKind()) {
Anders Carlssond9a67042010-02-17 06:07:19 +00002019
Anders Carlsson94464812010-04-10 19:13:06 +00002020 case VTableComponent::CK_VCallOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002021 Out << "vcall_offset ("
2022 << Component.getVCallOffset().getQuantity()
2023 << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002024 break;
Anders Carlsson852213e2010-02-16 04:59:55 +00002025
Anders Carlsson94464812010-04-10 19:13:06 +00002026 case VTableComponent::CK_VBaseOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002027 Out << "vbase_offset ("
2028 << Component.getVBaseOffset().getQuantity()
2029 << ")";
Anders Carlsson852213e2010-02-16 04:59:55 +00002030 break;
2031
Anders Carlsson94464812010-04-10 19:13:06 +00002032 case VTableComponent::CK_OffsetToTop:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002033 Out << "offset_to_top ("
2034 << Component.getOffsetToTop().getQuantity()
2035 << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002036 break;
2037
Anders Carlsson94464812010-04-10 19:13:06 +00002038 case VTableComponent::CK_RTTI:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002039 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2040 break;
2041
Anders Carlsson94464812010-04-10 19:13:06 +00002042 case VTableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002043 const CXXMethodDecl *MD = Component.getFunctionDecl();
2044
Anders Carlsson848fa642010-02-11 18:20:28 +00002045 std::string Str =
2046 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2047 MD);
2048 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +00002049 if (MD->isPure())
2050 Out << " [pure]";
2051
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002052 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002053 if (!Thunk.isEmpty()) {
2054 // If this function pointer has a return adjustment, dump it.
2055 if (!Thunk.Return.isEmpty()) {
2056 Out << "\n [return adjustment: ";
2057 Out << Thunk.Return.NonVirtual << " non-virtual";
2058
2059 if (Thunk.Return.VBaseOffsetOffset) {
2060 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2061 Out << " vbase offset offset";
2062 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002063
Anders Carlssondd366812010-03-12 04:54:20 +00002064 Out << ']';
2065 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002066
Anders Carlssondd366812010-03-12 04:54:20 +00002067 // If this function pointer has a 'this' pointer adjustment, dump it.
2068 if (!Thunk.This.isEmpty()) {
2069 Out << "\n [this adjustment: ";
2070 Out << Thunk.This.NonVirtual << " non-virtual";
2071
2072 if (Thunk.This.VCallOffsetOffset) {
2073 Out << ", " << Thunk.This.VCallOffsetOffset;
2074 Out << " vcall offset offset";
2075 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002076
Anders Carlssondd366812010-03-12 04:54:20 +00002077 Out << ']';
2078 }
Anders Carlsson49bac9a2010-02-13 23:40:17 +00002079 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002080
Anders Carlsson848fa642010-02-11 18:20:28 +00002081 break;
2082 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002083
Anders Carlsson94464812010-04-10 19:13:06 +00002084 case VTableComponent::CK_CompleteDtorPointer:
2085 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6065e022010-02-19 06:03:53 +00002086 bool IsComplete =
Anders Carlsson94464812010-04-10 19:13:06 +00002087 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
Anders Carlsson6065e022010-02-19 06:03:53 +00002088
Anders Carlsson848fa642010-02-11 18:20:28 +00002089 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2090
Anders Carlsson6065e022010-02-19 06:03:53 +00002091 Out << DD->getQualifiedNameAsString();
2092 if (IsComplete)
2093 Out << "() [complete]";
2094 else
2095 Out << "() [deleting]";
2096
Anders Carlsson98241422010-02-12 02:38:13 +00002097 if (DD->isPure())
2098 Out << " [pure]";
2099
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002100 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002101 if (!Thunk.isEmpty()) {
2102 // If this destructor has a 'this' pointer adjustment, dump it.
2103 if (!Thunk.This.isEmpty()) {
2104 Out << "\n [this adjustment: ";
2105 Out << Thunk.This.NonVirtual << " non-virtual";
2106
2107 if (Thunk.This.VCallOffsetOffset) {
2108 Out << ", " << Thunk.This.VCallOffsetOffset;
2109 Out << " vcall offset offset";
2110 }
2111
2112 Out << ']';
2113 }
2114 }
Anders Carlsson6065e022010-02-19 06:03:53 +00002115
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002116 break;
2117 }
2118
Anders Carlsson94464812010-04-10 19:13:06 +00002119 case VTableComponent::CK_UnusedFunctionPointer: {
Anders Carlsson1f464662010-02-19 20:08:13 +00002120 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2121
2122 std::string Str =
2123 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2124 MD);
2125 Out << "[unused] " << Str;
2126 if (MD->isPure())
2127 Out << " [pure]";
2128 }
2129
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002130 }
Anders Carlsson822307b2010-02-11 17:18:51 +00002131
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002132 Out << '\n';
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002133
2134 // Dump the next address point.
2135 uint64_t NextIndex = Index + 1;
2136 if (AddressPointsByIndex.count(NextIndex)) {
2137 if (AddressPointsByIndex.count(NextIndex) == 1) {
2138 const BaseSubobject &Base =
2139 AddressPointsByIndex.find(NextIndex)->second;
2140
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002141 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
Ken Dyck4230d522011-03-24 01:21:01 +00002142 Out << ", " << Base.getBaseOffset().getQuantity();
2143 Out << ") vtable address --\n";
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002144 } else {
Ken Dyck57ebe332011-03-26 00:51:24 +00002145 CharUnits BaseOffset =
2146 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002147
2148 // We store the class names in a set to get a stable order.
2149 std::set<std::string> ClassNames;
2150 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2151 AddressPointsByIndex.lower_bound(NextIndex), E =
2152 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
Ken Dyck57ebe332011-03-26 00:51:24 +00002153 assert(I->second.getBaseOffset() == BaseOffset &&
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002154 "Invalid base offset!");
2155 const CXXRecordDecl *RD = I->second.getBase();
2156 ClassNames.insert(RD->getQualifiedNameAsString());
2157 }
2158
2159 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2160 E = ClassNames.end(); I != E; ++I) {
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002161 Out << " -- (" << *I;
Ken Dyck57ebe332011-03-26 00:51:24 +00002162 Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002163 }
2164 }
2165 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002166 }
Anders Carlssond03a3262010-02-27 20:39:05 +00002167
2168 Out << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002169
Anders Carlsson718d0692010-04-11 22:07:06 +00002170 if (isBuildingConstructorVTable())
Anders Carlsson9135a842010-03-18 02:44:19 +00002171 return;
2172
2173 if (MostDerivedClass->getNumVBases()) {
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002174 // We store the virtual base class names and their offsets in a map to get
2175 // a stable order.
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002176
Ken Dyck3015c4c2011-04-07 00:59:42 +00002177 std::map<std::string, CharUnits> ClassNamesAndOffsets;
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002178 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2179 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2180 std::string ClassName = I->first->getQualifiedNameAsString();
Ken Dyckd28c33c2011-04-07 00:55:01 +00002181 CharUnits OffsetOffset = I->second;
2182 ClassNamesAndOffsets.insert(
Ken Dyck3015c4c2011-04-07 00:59:42 +00002183 std::make_pair(ClassName, OffsetOffset));
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002184 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002185
2186 Out << "Virtual base offset offsets for '";
2187 Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2188 Out << ClassNamesAndOffsets.size();
2189 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002190
Ken Dyck3015c4c2011-04-07 00:59:42 +00002191 for (std::map<std::string, CharUnits>::const_iterator I =
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002192 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2193 I != E; ++I)
Ken Dyck3015c4c2011-04-07 00:59:42 +00002194 Out << " " << I->first << " | " << I->second.getQuantity() << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002195
2196 Out << "\n";
2197 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002198
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002199 if (!Thunks.empty()) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002200 // We store the method names in a map to get a stable order.
2201 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2202
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002203 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002204 I != E; ++I) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002205 const CXXMethodDecl *MD = I->first;
2206 std::string MethodName =
2207 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2208 MD);
2209
2210 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2211 }
2212
2213 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2214 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2215 I != E; ++I) {
2216 const std::string &MethodName = I->first;
2217 const CXXMethodDecl *MD = I->second;
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002218
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002219 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002220 std::sort(ThunksVector.begin(), ThunksVector.end());
Anders Carlsson9135a842010-03-18 02:44:19 +00002221
2222 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2223 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2224
2225 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2226 const ThunkInfo &Thunk = ThunksVector[I];
2227
2228 Out << llvm::format("%4d | ", I);
2229
Anders Carlsson09daa502010-03-22 16:10:59 +00002230 // If this function pointer has a return pointer adjustment, dump it.
2231 if (!Thunk.Return.isEmpty()) {
2232 Out << "return adjustment: " << Thunk.This.NonVirtual;
2233 Out << " non-virtual";
2234 if (Thunk.Return.VBaseOffsetOffset) {
2235 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2236 Out << " vbase offset offset";
2237 }
2238
2239 if (!Thunk.This.isEmpty())
2240 Out << "\n ";
2241 }
2242
Anders Carlsson9135a842010-03-18 02:44:19 +00002243 // If this function pointer has a 'this' pointer adjustment, dump it.
2244 if (!Thunk.This.isEmpty()) {
Anders Carlsson09daa502010-03-22 16:10:59 +00002245 Out << "this adjustment: ";
2246 Out << Thunk.This.NonVirtual << " non-virtual";
Anders Carlsson9135a842010-03-18 02:44:19 +00002247
2248 if (Thunk.This.VCallOffsetOffset) {
2249 Out << ", " << Thunk.This.VCallOffsetOffset;
Anders Carlsson09daa502010-03-22 16:10:59 +00002250 Out << " vcall offset offset";
Anders Carlsson9135a842010-03-18 02:44:19 +00002251 }
2252 }
2253
2254 Out << '\n';
2255 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00002256
2257 Out << '\n';
Anders Carlsson9135a842010-03-18 02:44:19 +00002258 }
2259 }
Anders Carlssond24e50c2011-04-10 17:42:45 +00002260
2261 // Compute the vtable indices for all the member functions.
2262 // Store them in a map keyed by the index so we'll get a sorted table.
2263 std::map<uint64_t, std::string> IndicesMap;
2264
2265 for (CXXRecordDecl::method_iterator i = MostDerivedClass->method_begin(),
2266 e = MostDerivedClass->method_end(); i != e; ++i) {
2267 const CXXMethodDecl *MD = *i;
2268
2269 // We only want virtual member functions.
2270 if (!MD->isVirtual())
2271 continue;
2272
2273 std::string MethodName =
2274 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2275 MD);
2276
2277 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2278 IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Complete))] =
2279 MethodName + " [complete]";
2280 IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Deleting))] =
2281 MethodName + " [deleting]";
2282 } else {
2283 IndicesMap[VTables.getMethodVTableIndex(MD)] = MethodName;
2284 }
2285 }
2286
2287 // Print the vtable indices for all the member functions.
2288 if (!IndicesMap.empty()) {
2289 Out << "VTable indices for '";
2290 Out << MostDerivedClass->getQualifiedNameAsString();
2291 Out << "' (" << IndicesMap.size() << " entries).\n";
2292
2293 for (std::map<uint64_t, std::string>::const_iterator I = IndicesMap.begin(),
2294 E = IndicesMap.end(); I != E; ++I) {
2295 uint64_t VTableIndex = I->first;
2296 const std::string &MethodName = I->second;
2297
2298 Out << llvm::format(" %4u | ", VTableIndex) << MethodName << '\n';
2299 }
2300 }
2301
2302 Out << '\n';
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002303}
2304
2305}
2306
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002307static void
2308CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
2309 VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) {
Anders Carlssonb8bced02011-04-10 18:00:32 +00002310 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2311 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002312
Anders Carlssonb8bced02011-04-10 18:00:32 +00002313 if (!PrimaryBase)
2314 return;
2315
2316 CollectPrimaryBases(PrimaryBase, Context, PrimaryBases);
2317
2318 if (!PrimaryBases.insert(PrimaryBase))
2319 assert(false && "Found a duplicate primary base!");
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002320}
2321
Anders Carlsson046c2942010-04-17 20:15:18 +00002322void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002323
2324 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00002325 // The order of the virtual function pointers in a virtual table is the
2326 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002327 //
Anders Carlsson45147d02010-02-02 03:37:46 +00002328 // There is an entry for any virtual function declared in a class,
2329 // whether it is a new function or overrides a base class function,
2330 // unless it overrides a function from the primary base, and conversion
2331 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002332
2333 int64_t CurrentIndex = 0;
2334
2335 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2336 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2337
2338 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00002339 assert(PrimaryBase->isDefinition() &&
2340 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002341
2342 // Since the record decl shares its vtable pointer with the primary base
2343 // we need to start counting at the end of the primary base's vtable.
2344 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2345 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00002346
2347 // Collect all the primary bases, so we can check whether methods override
2348 // a method from the base.
Anders Carlsson94464812010-04-10 19:13:06 +00002349 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002350 CollectPrimaryBases(RD, CGM.getContext(), PrimaryBases);
Eli Friedmanea5ae312009-12-15 03:31:17 +00002351
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002352 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2353
2354 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2355 e = RD->method_end(); i != e; ++i) {
2356 const CXXMethodDecl *MD = *i;
2357
2358 // We only want virtual methods.
2359 if (!MD->isVirtual())
2360 continue;
2361
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002362 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002363 if (const CXXMethodDecl *OverriddenMD =
Anders Carlssonf622b452010-04-10 20:39:29 +00002364 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002365 // Check if converting from the return type of the method to the
2366 // return type of the overridden method requires conversion.
Anders Carlssondf31af12010-02-13 23:17:31 +00002367 if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
2368 OverriddenMD).isEmpty()) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002369 // This index is shared between the index in the vtable of the primary
2370 // base class.
2371 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2372 const CXXDestructorDecl *OverriddenDD =
2373 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002374
Anders Carlsson57071e22010-02-12 05:25:12 +00002375 // Add both the complete and deleting entries.
Anders Carlsson046c2942010-04-17 20:15:18 +00002376 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] =
2377 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2378 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2379 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
Anders Carlsson57071e22010-02-12 05:25:12 +00002380 } else {
Anders Carlsson046c2942010-04-17 20:15:18 +00002381 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
Anders Carlsson57071e22010-02-12 05:25:12 +00002382 }
2383
2384 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00002385 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002386 }
2387 }
2388
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002389 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2390 if (MD->isImplicit()) {
2391 assert(!ImplicitVirtualDtor &&
2392 "Did already see an implicit virtual dtor!");
2393 ImplicitVirtualDtor = DD;
2394 continue;
2395 }
Tilmann Schellerca0df922011-03-02 20:40:50 +00002396
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002397 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002398 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002399
2400 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002401 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002402 } else {
2403 // Add the entry.
Tilmann Schellerca0df922011-03-02 20:40:50 +00002404 MethodVTableIndices[MD] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002405 }
2406 }
2407
2408 if (ImplicitVirtualDtor) {
2409 // Itanium C++ ABI 2.5.2:
Anders Carlssona661a542010-02-16 04:49:44 +00002410 // If a class has an implicitly-defined virtual destructor,
2411 // its entries come after the declared virtual function pointers.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002412
2413 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002414 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002415 CurrentIndex++;
2416
2417 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002418 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002419 CurrentIndex++;
2420 }
2421
2422 NumVirtualFunctionPointers[RD] = CurrentIndex;
2423}
2424
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002425bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
2426 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
2427
2428 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2429 if (TSK == TSK_ExplicitInstantiationDeclaration)
2430 return false;
2431
2432 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
2433 if (!KeyFunction)
2434 return true;
2435
2436 // Itanium C++ ABI, 5.2.6 Instantiated Templates:
2437 // An instantiation of a class template requires:
2438 // - In the object where instantiated, the virtual table...
2439 if (TSK == TSK_ImplicitInstantiation ||
2440 TSK == TSK_ExplicitInstantiationDefinition)
2441 return true;
2442
Anders Carlsson6d7f8472011-01-30 20:45:54 +00002443 // If we're building with optimization, we always emit VTables since that
2444 // allows for virtual function calls to be devirtualized.
2445 // (We don't want to do this in -fapple-kext mode however).
2446 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOptions().AppleKext)
2447 return true;
2448
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002449 return KeyFunction->hasBody();
2450}
2451
Anders Carlssonaf440352010-03-23 04:11:45 +00002452uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002453 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2454 NumVirtualFunctionPointers.find(RD);
2455 if (I != NumVirtualFunctionPointers.end())
2456 return I->second;
2457
Anders Carlsson046c2942010-04-17 20:15:18 +00002458 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002459
2460 I = NumVirtualFunctionPointers.find(RD);
2461 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2462 return I->second;
2463}
2464
Anders Carlsson046c2942010-04-17 20:15:18 +00002465uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2466 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2467 if (I != MethodVTableIndices.end())
Anders Carlssondbd920c2009-10-11 22:13:54 +00002468 return I->second;
2469
Anders Carlssona0fdd912009-11-13 17:08:56 +00002470 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002471
Anders Carlsson046c2942010-04-17 20:15:18 +00002472 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002473
Anders Carlsson046c2942010-04-17 20:15:18 +00002474 I = MethodVTableIndices.find(GD);
2475 assert(I != MethodVTableIndices.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002476 return I->second;
2477}
2478
Ken Dyck14c65ca2011-04-07 12:37:09 +00002479CharUnits
2480CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2481 const CXXRecordDecl *VBase) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002482 ClassPairTy ClassPair(RD, VBase);
2483
Anders Carlssonbba16072010-03-11 07:15:17 +00002484 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2485 VirtualBaseClassOffsetOffsets.find(ClassPair);
2486 if (I != VirtualBaseClassOffsetOffsets.end())
Ken Dyck14c65ca2011-04-07 12:37:09 +00002487 return I->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002488
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002489 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
Ken Dyck4230d522011-03-24 01:21:01 +00002490 BaseSubobject(RD, CharUnits::Zero()),
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002491 /*BaseIsVirtual=*/false,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00002492 /*OffsetInLayoutClass=*/CharUnits::Zero());
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002493
2494 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2495 Builder.getVBaseOffsetOffsets().begin(),
2496 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002497 // Insert all types.
2498 ClassPairTy ClassPair(RD, I->first);
2499
Ken Dyckd28c33c2011-04-07 00:55:01 +00002500 VirtualBaseClassOffsetOffsets.insert(
Ken Dyck1a7f7522011-04-07 01:22:42 +00002501 std::make_pair(ClassPair, I->second));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002502 }
2503
Anders Carlssonbba16072010-03-11 07:15:17 +00002504 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
Anders Carlssonbba16072010-03-11 07:15:17 +00002505 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002506
Ken Dyck14c65ca2011-04-07 12:37:09 +00002507 return I->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002508}
2509
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002510uint64_t
2511CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
Anders Carlssonec9c2022010-04-08 16:18:36 +00002512 assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2513 "Did not find address point!");
2514
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002515 uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002516 assert(AddressPoint && "Address point must not be zero!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002517
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002518 return AddressPoint;
2519}
2520
Anders Carlsson19879c92010-03-23 17:17:29 +00002521llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
Anders Carlsson84c49e42011-02-06 17:15:43 +00002522 const ThunkInfo &Thunk) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002523 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2524
2525 // Compute the mangled name.
2526 llvm::SmallString<256> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002527 llvm::raw_svector_ostream Out(Name);
Anders Carlsson19879c92010-03-23 17:17:29 +00002528 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
John McCall4c40d982010-08-31 07:33:07 +00002529 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002530 Thunk.This, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002531 else
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002532 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
2533 Out.flush();
2534
John McCall4c40d982010-08-31 07:33:07 +00002535 const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson84c49e42011-02-06 17:15:43 +00002536 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
Anders Carlsson19879c92010-03-23 17:17:29 +00002537}
2538
Anders Carlsson519c3282010-03-24 00:39:18 +00002539static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2540 llvm::Value *Ptr,
2541 int64_t NonVirtualAdjustment,
2542 int64_t VirtualAdjustment) {
2543 if (!NonVirtualAdjustment && !VirtualAdjustment)
2544 return Ptr;
2545
2546 const llvm::Type *Int8PtrTy =
2547 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2548
2549 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2550
2551 if (NonVirtualAdjustment) {
2552 // Do the non-virtual adjustment.
2553 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2554 }
2555
2556 if (VirtualAdjustment) {
2557 const llvm::Type *PtrDiffTy =
2558 CGF.ConvertType(CGF.getContext().getPointerDiffType());
2559
2560 // Do the virtual adjustment.
2561 llvm::Value *VTablePtrPtr =
2562 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2563
2564 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2565
2566 llvm::Value *OffsetPtr =
2567 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2568
2569 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2570
2571 // Load the adjustment offset from the vtable.
2572 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2573
2574 // Adjust our pointer.
2575 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2576 }
2577
2578 // Cast back to the original type.
2579 return CGF.Builder.CreateBitCast(V, Ptr->getType());
2580}
2581
John McCall65005532010-08-04 23:46:35 +00002582static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
2583 const ThunkInfo &Thunk, llvm::Function *Fn) {
Anders Carlsson0ffeaad2011-01-29 19:39:23 +00002584 CGM.setGlobalVisibility(Fn, MD);
John McCall65005532010-08-04 23:46:35 +00002585
John McCall279b5eb2010-08-12 23:36:15 +00002586 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
2587 return;
2588
John McCall65005532010-08-04 23:46:35 +00002589 // If the thunk has weak/linkonce linkage, but the function must be
2590 // emitted in every translation unit that references it, then we can
2591 // emit its thunks with hidden visibility, since its thunks must be
2592 // emitted when the function is.
2593
John McCall7a536902010-08-05 20:39:18 +00002594 // This follows CodeGenModule::setTypeVisibility; see the comments
2595 // there for explanation.
John McCall65005532010-08-04 23:46:35 +00002596
2597 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
2598 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
2599 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
2600 return;
2601
Douglas Gregor4421d2b2011-03-26 12:10:19 +00002602 if (MD->getExplicitVisibility())
John McCall65005532010-08-04 23:46:35 +00002603 return;
2604
2605 switch (MD->getTemplateSpecializationKind()) {
John McCall65005532010-08-04 23:46:35 +00002606 case TSK_ExplicitInstantiationDefinition:
2607 case TSK_ExplicitInstantiationDeclaration:
2608 return;
2609
John McCall65005532010-08-04 23:46:35 +00002610 case TSK_Undeclared:
2611 break;
2612
John McCall7a536902010-08-05 20:39:18 +00002613 case TSK_ExplicitSpecialization:
John McCall65005532010-08-04 23:46:35 +00002614 case TSK_ImplicitInstantiation:
John McCall279b5eb2010-08-12 23:36:15 +00002615 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
John McCall7a536902010-08-05 20:39:18 +00002616 return;
John McCall65005532010-08-04 23:46:35 +00002617 break;
2618 }
2619
2620 // If there's an explicit definition, and that definition is
2621 // out-of-line, then we can't assume that all users will have a
2622 // definition to emit.
2623 const FunctionDecl *Def = 0;
2624 if (MD->hasBody(Def) && Def->isOutOfLine())
2625 return;
2626
2627 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2628}
2629
John McCall311b4422011-03-09 07:12:35 +00002630#ifndef NDEBUG
2631static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
2632 const ABIArgInfo &infoR, CanQualType typeR) {
2633 return (infoL.getKind() == infoR.getKind() &&
2634 (typeL == typeR ||
2635 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
2636 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
2637}
2638#endif
2639
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00002640static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
2641 QualType ResultType, RValue RV,
2642 const ThunkInfo &Thunk) {
2643 // Emit the return adjustment.
2644 bool NullCheckValue = !ResultType->isReferenceType();
2645
2646 llvm::BasicBlock *AdjustNull = 0;
2647 llvm::BasicBlock *AdjustNotNull = 0;
2648 llvm::BasicBlock *AdjustEnd = 0;
2649
2650 llvm::Value *ReturnValue = RV.getScalarVal();
2651
2652 if (NullCheckValue) {
2653 AdjustNull = CGF.createBasicBlock("adjust.null");
2654 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
2655 AdjustEnd = CGF.createBasicBlock("adjust.end");
2656
2657 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
2658 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2659 CGF.EmitBlock(AdjustNotNull);
2660 }
2661
2662 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
2663 Thunk.Return.NonVirtual,
2664 Thunk.Return.VBaseOffsetOffset);
2665
2666 if (NullCheckValue) {
2667 CGF.Builder.CreateBr(AdjustEnd);
2668 CGF.EmitBlock(AdjustNull);
2669 CGF.Builder.CreateBr(AdjustEnd);
2670 CGF.EmitBlock(AdjustEnd);
2671
2672 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
2673 PHI->addIncoming(ReturnValue, AdjustNotNull);
2674 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2675 AdjustNull);
2676 ReturnValue = PHI;
2677 }
2678
2679 return RValue::get(ReturnValue);
2680}
2681
2682// This function does roughly the same thing as GenerateThunk, but in a
2683// very different way, so that va_start and va_end work correctly.
2684// FIXME: This function assumes "this" is the first non-sret LLVM argument of
2685// a function, and that there is an alloca built in the entry block
2686// for all accesses to "this".
2687// FIXME: This function assumes there is only one "ret" statement per function.
2688// FIXME: Cloning isn't correct in the presence of indirect goto!
2689// FIXME: This implementation of thunks bloats codesize by duplicating the
2690// function definition. There are alternatives:
2691// 1. Add some sort of stub support to LLVM for cases where we can
2692// do a this adjustment, then a sibcall.
2693// 2. We could transform the definition to take a va_list instead of an
2694// actual variable argument list, then have the thunks (including a
2695// no-op thunk for the regular definition) call va_start/va_end.
2696// There's a bit of per-call overhead for this solution, but it's
2697// better for codesize if the definition is long.
2698void CodeGenFunction::GenerateVarArgsThunk(
2699 llvm::Function *Fn,
2700 const CGFunctionInfo &FnInfo,
2701 GlobalDecl GD, const ThunkInfo &Thunk) {
2702 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2703 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2704 QualType ResultType = FPT->getResultType();
2705
2706 // Get the original function
2707 const llvm::Type *Ty =
2708 CGM.getTypes().GetFunctionType(FnInfo, /*IsVariadic*/true);
2709 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
2710 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
2711
2712 // Clone to thunk.
2713 llvm::Function *NewFn = llvm::CloneFunction(BaseFn);
2714 CGM.getModule().getFunctionList().push_back(NewFn);
2715 Fn->replaceAllUsesWith(NewFn);
2716 NewFn->takeName(Fn);
2717 Fn->eraseFromParent();
2718 Fn = NewFn;
2719
2720 // "Initialize" CGF (minimally).
2721 CurFn = Fn;
2722
2723 // Get the "this" value
2724 llvm::Function::arg_iterator AI = Fn->arg_begin();
2725 if (CGM.ReturnTypeUsesSRet(FnInfo))
2726 ++AI;
2727
2728 // Find the first store of "this", which will be to the alloca associated
2729 // with "this".
2730 llvm::Value *ThisPtr = &*AI;
2731 llvm::BasicBlock *EntryBB = Fn->begin();
2732 llvm::Instruction *ThisStore = 0;
2733 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
2734 I != E; I++) {
2735 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
2736 ThisStore = cast<llvm::StoreInst>(I);
2737 break;
2738 }
2739 }
2740 assert(ThisStore && "Store of this should be in entry block?");
2741 // Adjust "this", if necessary.
2742 Builder.SetInsertPoint(ThisStore);
2743 llvm::Value *AdjustedThisPtr =
2744 PerformTypeAdjustment(*this, ThisPtr,
2745 Thunk.This.NonVirtual,
2746 Thunk.This.VCallOffsetOffset);
2747 ThisStore->setOperand(0, AdjustedThisPtr);
2748
2749 if (!Thunk.Return.isEmpty()) {
2750 // Fix up the returned value, if necessary.
2751 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
2752 llvm::Instruction *T = I->getTerminator();
2753 if (isa<llvm::ReturnInst>(T)) {
2754 RValue RV = RValue::get(T->getOperand(0));
2755 T->eraseFromParent();
2756 Builder.SetInsertPoint(&*I);
2757 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
2758 Builder.CreateRet(RV.getScalarVal());
2759 break;
2760 }
2761 }
2762 }
2763}
2764
John McCalld26bc762011-03-09 04:27:21 +00002765void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
2766 const CGFunctionInfo &FnInfo,
2767 GlobalDecl GD, const ThunkInfo &Thunk) {
Anders Carlsson519c3282010-03-24 00:39:18 +00002768 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2769 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2770 QualType ResultType = FPT->getResultType();
2771 QualType ThisType = MD->getThisType(getContext());
2772
2773 FunctionArgList FunctionArgs;
2774
2775 // FIXME: It would be nice if more of this code could be shared with
2776 // CodeGenFunction::GenerateCode.
2777
2778 // Create the implicit 'this' parameter declaration.
John McCall4c40d982010-08-31 07:33:07 +00002779 CurGD = GD;
2780 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
Anders Carlsson519c3282010-03-24 00:39:18 +00002781
2782 // Add the rest of the parameters.
2783 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2784 E = MD->param_end(); I != E; ++I) {
2785 ParmVarDecl *Param = *I;
2786
John McCalld26bc762011-03-09 04:27:21 +00002787 FunctionArgs.push_back(Param);
Anders Carlsson519c3282010-03-24 00:39:18 +00002788 }
Tilmann Scheller9c6082f2011-03-02 21:36:49 +00002789
John McCalld26bc762011-03-09 04:27:21 +00002790 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
2791 SourceLocation());
Anders Carlsson519c3282010-03-24 00:39:18 +00002792
John McCall4c40d982010-08-31 07:33:07 +00002793 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2794
Anders Carlsson519c3282010-03-24 00:39:18 +00002795 // Adjust the 'this' pointer if necessary.
2796 llvm::Value *AdjustedThisPtr =
2797 PerformTypeAdjustment(*this, LoadCXXThis(),
2798 Thunk.This.NonVirtual,
2799 Thunk.This.VCallOffsetOffset);
2800
2801 CallArgList CallArgs;
2802
2803 // Add our adjusted 'this' pointer.
Eli Friedman04c9a492011-05-02 17:57:46 +00002804 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
Anders Carlsson519c3282010-03-24 00:39:18 +00002805
2806 // Add the rest of the parameters.
2807 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2808 E = MD->param_end(); I != E; ++I) {
John McCall413ebdb2011-03-11 20:59:21 +00002809 ParmVarDecl *param = *I;
2810 EmitDelegateCallArg(CallArgs, param);
Anders Carlsson519c3282010-03-24 00:39:18 +00002811 }
2812
2813 // Get our callee.
2814 const llvm::Type *Ty =
John McCall4c40d982010-08-31 07:33:07 +00002815 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
Anders Carlsson519c3282010-03-24 00:39:18 +00002816 FPT->isVariadic());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002817 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson519c3282010-03-24 00:39:18 +00002818
John McCalld26bc762011-03-09 04:27:21 +00002819#ifndef NDEBUG
2820 const CGFunctionInfo &CallFnInfo =
2821 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getExtInfo());
John McCall311b4422011-03-09 07:12:35 +00002822 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
2823 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
2824 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
2825 assert(similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
2826 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
2827 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
2828 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
2829 assert(similar(CallFnInfo.arg_begin()[i].info,
2830 CallFnInfo.arg_begin()[i].type,
2831 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
John McCalld26bc762011-03-09 04:27:21 +00002832#endif
Anders Carlsson519c3282010-03-24 00:39:18 +00002833
Douglas Gregorcb359df2010-05-20 05:54:35 +00002834 // Determine whether we have a return value slot to use.
2835 ReturnValueSlot Slot;
2836 if (!ResultType->isVoidType() &&
2837 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2838 hasAggregateLLVMType(CurFnInfo->getReturnType()))
2839 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
2840
Anders Carlsson519c3282010-03-24 00:39:18 +00002841 // Now emit our call.
Douglas Gregorcb359df2010-05-20 05:54:35 +00002842 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
Anders Carlsson519c3282010-03-24 00:39:18 +00002843
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00002844 if (!Thunk.Return.isEmpty())
2845 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
Anders Carlsson519c3282010-03-24 00:39:18 +00002846
Douglas Gregorcb359df2010-05-20 05:54:35 +00002847 if (!ResultType->isVoidType() && Slot.isNull())
John McCalld16c2cf2011-02-08 08:22:06 +00002848 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
Anders Carlsson519c3282010-03-24 00:39:18 +00002849
2850 FinishFunction();
2851
Anders Carlsson519c3282010-03-24 00:39:18 +00002852 // Set the right linkage.
John McCall8b242332010-05-25 04:30:21 +00002853 CGM.setFunctionLinkage(MD, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002854
2855 // Set the right visibility.
John McCall65005532010-08-04 23:46:35 +00002856 setThunkVisibility(CGM, MD, Thunk, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002857}
2858
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002859void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
2860 bool UseAvailableExternallyLinkage)
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002861{
John McCalld26bc762011-03-09 04:27:21 +00002862 const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(GD);
2863
2864 // FIXME: re-use FnInfo in this computation.
Anders Carlsson84c49e42011-02-06 17:15:43 +00002865 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +00002866
Anders Carlsson7986ad52010-03-23 18:18:41 +00002867 // Strip off a bitcast if we got one back.
Anders Carlsson13d68982010-03-24 00:35:44 +00002868 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson7986ad52010-03-23 18:18:41 +00002869 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson13d68982010-03-24 00:35:44 +00002870 Entry = CE->getOperand(0);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002871 }
2872
Anders Carlsson7986ad52010-03-23 18:18:41 +00002873 // There's already a declaration with the same name, check if it has the same
2874 // type or if we need to replace it.
Anders Carlsson13d68982010-03-24 00:35:44 +00002875 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
John McCall4c40d982010-08-31 07:33:07 +00002876 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
Anders Carlsson13d68982010-03-24 00:35:44 +00002877 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002878
2879 // If the types mismatch then we have to rewrite the definition.
2880 assert(OldThunkFn->isDeclaration() &&
2881 "Shouldn't replace non-declaration");
2882
2883 // Remove the name from the old thunk function and get a new thunk.
2884 OldThunkFn->setName(llvm::StringRef());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002885 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002886
2887 // If needed, replace the old thunk with a bitcast.
2888 if (!OldThunkFn->use_empty()) {
2889 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson13d68982010-03-24 00:35:44 +00002890 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson7986ad52010-03-23 18:18:41 +00002891 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
2892 }
2893
2894 // Remove the old thunk.
2895 OldThunkFn->eraseFromParent();
2896 }
Anders Carlsson519c3282010-03-24 00:39:18 +00002897
Anders Carlsson519c3282010-03-24 00:39:18 +00002898 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002899
2900 if (!ThunkFn->isDeclaration()) {
2901 if (UseAvailableExternallyLinkage) {
2902 // There is already a thunk emitted for this function, do nothing.
2903 return;
2904 }
2905
Anders Carlsson22df7b12011-02-06 20:09:44 +00002906 // If a function has a body, it should have available_externally linkage.
2907 assert(ThunkFn->hasAvailableExternallyLinkage() &&
2908 "Function should have available_externally linkage!");
2909
2910 // Change the linkage.
2911 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
2912 return;
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002913 }
2914
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00002915 if (ThunkFn->isVarArg()) {
2916 // Varargs thunks are special; we can't just generate a call because
2917 // we can't copy the varargs. Our implementation is rather
2918 // expensive/sucky at the moment, so don't generate the thunk unless
2919 // we have to.
2920 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
2921 if (!UseAvailableExternallyLinkage)
2922 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
2923 } else {
2924 // Normal thunk body generation.
2925 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
2926 }
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002927
2928 if (UseAvailableExternallyLinkage)
2929 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
2930}
2931
2932void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
2933 const ThunkInfo &Thunk) {
2934 // We only want to do this when building with optimizations.
2935 if (!CGM.getCodeGenOpts().OptimizationLevel)
2936 return;
2937
2938 // We can't emit thunks for member functions with incomplete types.
2939 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2940 if (CGM.getTypes().VerifyFuncTypeComplete(MD->getType().getTypePtr()))
2941 return;
2942
2943 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002944}
2945
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002946void CodeGenVTables::EmitThunks(GlobalDecl GD)
2947{
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002948 const CXXMethodDecl *MD =
2949 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2950
2951 // We don't need to generate thunks for the base destructor.
2952 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2953 return;
2954
2955 const CXXRecordDecl *RD = MD->getParent();
2956
Anders Carlssonccd83d72010-03-24 16:42:11 +00002957 // Compute VTable related info for this class.
John McCalle2132352010-06-02 21:22:02 +00002958 ComputeVTableRelatedInformation(RD, false);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002959
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002960 ThunksMapTy::const_iterator I = Thunks.find(MD);
2961 if (I == Thunks.end()) {
Anders Carlssonccd83d72010-03-24 16:42:11 +00002962 // We did not find a thunk for this method.
2963 return;
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002964 }
Anders Carlssonccd83d72010-03-24 16:42:11 +00002965
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002966 const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2967 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002968 EmitThunk(GD, ThunkInfoVector[I], /*UseAvailableExternallyLinkage=*/false);
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002969}
2970
John McCalle2132352010-06-02 21:22:02 +00002971void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
2972 bool RequireVTable) {
2973 VTableLayoutData &Entry = VTableLayoutMap[RD];
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002974
2975 // We may need to generate a definition for this vtable.
John McCalle2132352010-06-02 21:22:02 +00002976 if (RequireVTable && !Entry.getInt()) {
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002977 if (ShouldEmitVTableInThisTU(RD))
John McCalle2132352010-06-02 21:22:02 +00002978 CGM.DeferredVTables.push_back(RD);
2979
2980 Entry.setInt(true);
2981 }
2982
2983 // Check if we've computed this information before.
2984 if (Entry.getPointer())
2985 return;
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002986
Ken Dyck23da84a2011-03-27 20:20:28 +00002987 VTableBuilder Builder(*this, RD, CharUnits::Zero(),
2988 /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002989
2990 // Add the VTable layout.
2991 uint64_t NumVTableComponents = Builder.getNumVTableComponents();
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002992 // -fapple-kext adds an extra entry at end of vtbl.
2993 bool IsAppleKext = CGM.getContext().getLangOptions().AppleKext;
2994 if (IsAppleKext)
2995 NumVTableComponents += 1;
2996
John McCalle2132352010-06-02 21:22:02 +00002997 uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1];
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002998 if (IsAppleKext)
2999 LayoutData[NumVTableComponents] = 0;
John McCalle2132352010-06-02 21:22:02 +00003000 Entry.setPointer(LayoutData);
Anders Carlssonccd83d72010-03-24 16:42:11 +00003001
3002 // Store the number of components.
3003 LayoutData[0] = NumVTableComponents;
3004
3005 // Store the components.
3006 std::copy(Builder.vtable_components_data_begin(),
3007 Builder.vtable_components_data_end(),
3008 &LayoutData[1]);
3009
3010 // Add the known thunks.
3011 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
3012
Anders Carlsson276701f2010-03-29 01:38:05 +00003013 // Add the thunks needed in this vtable.
3014 assert(!VTableThunksMap.count(RD) &&
3015 "Thunks already exists for this vtable!");
3016
3017 VTableThunksTy &VTableThunks = VTableThunksMap[RD];
3018 VTableThunks.append(Builder.vtable_thunks_begin(),
3019 Builder.vtable_thunks_end());
3020
3021 // Sort them.
3022 std::sort(VTableThunks.begin(), VTableThunks.end());
3023
Anders Carlsson66d567d2010-03-25 00:51:13 +00003024 // Add the address points.
Anders Carlsson94464812010-04-10 19:13:06 +00003025 for (VTableBuilder::AddressPointsMapTy::const_iterator I =
Anders Carlsson66d567d2010-03-25 00:51:13 +00003026 Builder.address_points_begin(), E = Builder.address_points_end();
3027 I != E; ++I) {
3028
3029 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
3030
3031 // Check if we already have the address points for this base.
3032 assert(!AddressPoint && "Address point already exists for this base!");
3033
3034 AddressPoint = I->second;
3035 }
3036
Anders Carlssonccd83d72010-03-24 16:42:11 +00003037 // If we don't have the vbase information for this class, insert it.
3038 // getVirtualBaseOffsetOffset will compute it separately without computing
3039 // the rest of the vtable related information.
3040 if (!RD->getNumVBases())
3041 return;
3042
3043 const RecordType *VBaseRT =
3044 RD->vbases_begin()->getType()->getAs<RecordType>();
3045 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
3046
3047 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
3048 return;
3049
Anders Carlsson94464812010-04-10 19:13:06 +00003050 for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
Anders Carlssonccd83d72010-03-24 16:42:11 +00003051 Builder.getVBaseOffsetOffsets().begin(),
3052 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
3053 // Insert all types.
3054 ClassPairTy ClassPair(RD, I->first);
3055
Ken Dyckd28c33c2011-04-07 00:55:01 +00003056 VirtualBaseClassOffsetOffsets.insert(
Ken Dyck1a7f7522011-04-07 01:22:42 +00003057 std::make_pair(ClassPair, I->second));
Anders Carlssonccd83d72010-03-24 16:42:11 +00003058 }
3059}
3060
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003061llvm::Constant *
3062CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
3063 const uint64_t *Components,
3064 unsigned NumComponents,
3065 const VTableThunksTy &VTableThunks) {
3066 llvm::SmallVector<llvm::Constant *, 64> Inits;
3067
3068 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3069
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003070 const llvm::Type *PtrDiffTy =
3071 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3072
3073 QualType ClassType = CGM.getContext().getTagDeclType(RD);
3074 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
3075
3076 unsigned NextVTableThunkIndex = 0;
3077
Anders Carlsson67d568a2010-03-29 05:40:50 +00003078 llvm::Constant* PureVirtualFn = 0;
3079
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003080 for (unsigned I = 0; I != NumComponents; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00003081 VTableComponent Component =
3082 VTableComponent::getFromOpaqueInteger(Components[I]);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003083
3084 llvm::Constant *Init = 0;
3085
3086 switch (Component.getKind()) {
Anders Carlsson94464812010-04-10 19:13:06 +00003087 case VTableComponent::CK_VCallOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00003088 Init = llvm::ConstantInt::get(PtrDiffTy,
3089 Component.getVCallOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003090 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
3091 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003092 case VTableComponent::CK_VBaseOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00003093 Init = llvm::ConstantInt::get(PtrDiffTy,
3094 Component.getVBaseOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003095 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
3096 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003097 case VTableComponent::CK_OffsetToTop:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00003098 Init = llvm::ConstantInt::get(PtrDiffTy,
3099 Component.getOffsetToTop().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003100 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
3101 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003102 case VTableComponent::CK_RTTI:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003103 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
3104 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003105 case VTableComponent::CK_FunctionPointer:
3106 case VTableComponent::CK_CompleteDtorPointer:
3107 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003108 GlobalDecl GD;
3109
3110 // Get the right global decl.
3111 switch (Component.getKind()) {
3112 default:
3113 llvm_unreachable("Unexpected vtable component kind");
Anders Carlsson94464812010-04-10 19:13:06 +00003114 case VTableComponent::CK_FunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003115 GD = Component.getFunctionDecl();
3116 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003117 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003118 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
3119 break;
Anders Carlsson94464812010-04-10 19:13:06 +00003120 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003121 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
3122 break;
3123 }
3124
Anders Carlsson67d568a2010-03-29 05:40:50 +00003125 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
3126 // We have a pure virtual member function.
3127 if (!PureVirtualFn) {
3128 const llvm::FunctionType *Ty =
3129 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
3130 /*isVarArg=*/false);
3131 PureVirtualFn =
3132 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
3133 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
3134 Int8PtrTy);
3135 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003136
Anders Carlsson67d568a2010-03-29 05:40:50 +00003137 Init = PureVirtualFn;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003138 } else {
Anders Carlsson67d568a2010-03-29 05:40:50 +00003139 // Check if we should use a thunk.
3140 if (NextVTableThunkIndex < VTableThunks.size() &&
3141 VTableThunks[NextVTableThunkIndex].first == I) {
3142 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003143
Anders Carlsson84c49e42011-02-06 17:15:43 +00003144 Init = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00003145 MaybeEmitThunkAvailableExternally(GD, Thunk);
3146
Anders Carlsson67d568a2010-03-29 05:40:50 +00003147 NextVTableThunkIndex++;
3148 } else {
John McCall4c40d982010-08-31 07:33:07 +00003149 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003150
Anders Carlsson1faa89f2011-02-05 04:35:53 +00003151 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003152 }
3153
3154 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003155 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003156 break;
3157 }
3158
Anders Carlsson94464812010-04-10 19:13:06 +00003159 case VTableComponent::CK_UnusedFunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003160 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
3161 break;
3162 };
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003163
3164 Inits.push_back(Init);
3165 }
3166
3167 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
3168 return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
3169}
3170
Anders Carlsson9dc338a2010-03-30 03:35:35 +00003171llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003172 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003173 llvm::raw_svector_ostream Out(OutName);
3174 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
3175 Out.flush();
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003176 llvm::StringRef Name = OutName.str();
Mike Stump85615df2009-11-19 04:04:36 +00003177
John McCalle2132352010-06-02 21:22:02 +00003178 ComputeVTableRelatedInformation(RD, true);
Anders Carlssonccd83d72010-03-24 16:42:11 +00003179
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003180 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlssonccd83d72010-03-24 16:42:11 +00003181 llvm::ArrayType *ArrayType =
3182 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003183
Rafael Espindola9f959db2011-01-11 21:10:26 +00003184 llvm::GlobalVariable *GV =
Anders Carlsson96eaf292011-01-29 18:25:07 +00003185 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
3186 llvm::GlobalValue::ExternalLinkage);
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +00003187 GV->setUnnamedAddr(true);
Rafael Espindola9f959db2011-01-11 21:10:26 +00003188 return GV;
Mike Stump380dd752009-11-10 07:44:33 +00003189}
Mike Stump8cfcb522009-11-11 20:26:26 +00003190
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003191void
3192CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
3193 llvm::GlobalVariable::LinkageTypes Linkage,
3194 const CXXRecordDecl *RD) {
3195 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003196 if (CGM.getLangOptions().DumpVTableLayouts) {
Ken Dyck23da84a2011-03-27 20:20:28 +00003197 VTableBuilder Builder(*this, RD, CharUnits::Zero(),
3198 /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003199
3200 Builder.dumpLayout(llvm::errs());
3201 }
3202
3203 assert(VTableThunksMap.count(RD) &&
3204 "No thunk status for this record decl!");
3205
3206 const VTableThunksTy& Thunks = VTableThunksMap[RD];
3207
3208 // Create and set the initializer.
3209 llvm::Constant *Init =
3210 CreateVTableInitializer(RD, getVTableComponentsData(RD),
3211 getNumVTableComponents(RD), Thunks);
3212 VTable->setInitializer(Init);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003213
3214 // Set the correct linkage.
3215 VTable->setLinkage(Linkage);
Douglas Gregorc66bcfd2010-06-14 23:41:45 +00003216
3217 // Set the right visibility.
Anders Carlssonfa2e99f2011-01-29 20:24:48 +00003218 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003219}
3220
Anders Carlssonff143f82010-03-25 00:35:49 +00003221llvm::GlobalVariable *
3222CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
Anders Carlsson2c822f12010-03-26 03:56:54 +00003223 const BaseSubobject &Base,
3224 bool BaseIsVirtual,
John McCallbda0d6b2011-03-27 09:00:25 +00003225 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +00003226 VTableAddressPointsMapTy& AddressPoints) {
Ken Dyck4230d522011-03-24 01:21:01 +00003227 VTableBuilder Builder(*this, Base.getBase(),
Ken Dyck23da84a2011-03-27 20:20:28 +00003228 Base.getBaseOffset(),
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003229 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3230
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003231 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003232 if (CGM.getLangOptions().DumpVTableLayouts)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003233 Builder.dumpLayout(llvm::errs());
3234
3235 // Add the address points.
Anders Carlsson2c822f12010-03-26 03:56:54 +00003236 AddressPoints.insert(Builder.address_points_begin(),
3237 Builder.address_points_end());
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003238
3239 // Get the mangled construction vtable name.
3240 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003241 llvm::raw_svector_ostream Out(OutName);
John McCall4c40d982010-08-31 07:33:07 +00003242 CGM.getCXXABI().getMangleContext().
Ken Dyck4230d522011-03-24 01:21:01 +00003243 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
3244 Out);
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003245 Out.flush();
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003246 llvm::StringRef Name = OutName.str();
3247
3248 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3249 llvm::ArrayType *ArrayType =
3250 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
3251
3252 // Create the variable that will hold the construction vtable.
3253 llvm::GlobalVariable *VTable =
John McCallbda0d6b2011-03-27 09:00:25 +00003254 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
3255 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
3256
3257 // V-tables are always unnamed_addr.
3258 VTable->setUnnamedAddr(true);
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003259
3260 // Add the thunks.
3261 VTableThunksTy VTableThunks;
3262 VTableThunks.append(Builder.vtable_thunks_begin(),
3263 Builder.vtable_thunks_end());
3264
Anders Carlssonaed924e2010-03-29 01:28:05 +00003265 // Sort them.
3266 std::sort(VTableThunks.begin(), VTableThunks.end());
3267
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003268 // Create and set the initializer.
3269 llvm::Constant *Init =
3270 CreateVTableInitializer(Base.getBase(),
3271 Builder.vtable_components_data_begin(),
3272 Builder.getNumVTableComponents(), VTableThunks);
3273 VTable->setInitializer(Init);
3274
Anders Carlssonff143f82010-03-25 00:35:49 +00003275 return VTable;
3276}
3277
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003278void
3279CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3280 const CXXRecordDecl *RD) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003281 llvm::GlobalVariable *&VTable = VTables[RD];
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003282 if (VTable) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003283 assert(VTable->getInitializer() && "VTable doesn't have a definition!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003284 return;
3285 }
3286
Anders Carlsson9dc338a2010-03-30 03:35:35 +00003287 VTable = GetAddrOfVTable(RD);
3288 EmitVTableDefinition(VTable, Linkage, RD);
3289
Anders Carlsson1cbce122011-01-29 19:16:51 +00003290 if (RD->getNumVBases()) {
3291 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
3292 EmitVTTDefinition(VTT, Linkage, RD);
3293 }
Douglas Gregor1e201b42010-04-08 15:52:03 +00003294
3295 // If this is the magic class __cxxabiv1::__fundamental_type_info,
3296 // we will emit the typeinfo for the fundamental types. This is the
3297 // same behaviour as GCC.
3298 const DeclContext *DC = RD->getDeclContext();
3299 if (RD->getIdentifier() &&
3300 RD->getIdentifier()->isStr("__fundamental_type_info") &&
3301 isa<NamespaceDecl>(DC) &&
3302 cast<NamespaceDecl>(DC)->getIdentifier() &&
3303 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
3304 DC->getParent()->isTranslationUnit())
3305 CGM.EmitFundamentalRTTIDescriptors();
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003306}