blob: 8ccba1fa2f6b6dda8861f6e39be223d8ad67173c [file] [log] [blame]
Anders Carlsson046c2942010-04-17 20:15:18 +00001//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
Anders Carlssondbd920c2009-10-11 22:13:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Anders Carlssond6b07fb2009-11-27 20:47:55 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000018#include "clang/AST/RecordLayout.h"
John McCall7a536902010-08-05 20:39:18 +000019#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson5dd730a2009-11-26 19:32:45 +000020#include "llvm/ADT/DenseSet.h"
Anders Carlssonb9021e92010-02-27 16:18:19 +000021#include "llvm/ADT/SetVector.h"
Chandler Carruthe087f072010-02-13 10:38:52 +000022#include "llvm/Support/Compiler.h"
Anders Carlsson824d7ea2010-02-11 08:02:13 +000023#include "llvm/Support/Format.h"
Anders Carlsson5e454aa2010-03-17 20:06:32 +000024#include <algorithm>
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000025#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000026
27using namespace clang;
28using namespace CodeGen;
29
Anders Carlsson27f69d02009-11-27 22:21:51 +000030namespace {
Anders Carlsson824d7ea2010-02-11 08:02:13 +000031
Anders Carlsson2fca9be2010-02-23 01:34:28 +000032/// BaseOffset - Represents an offset from a derived class to a direct or
33/// indirect base class.
34struct BaseOffset {
35 /// DerivedClass - The derived class.
36 const CXXRecordDecl *DerivedClass;
37
38 /// VirtualBase - If the path from the derived class to the base class
39 /// involves a virtual base class, this holds its declaration.
40 const CXXRecordDecl *VirtualBase;
41
42 /// NonVirtualOffset - The offset from the derived class to the base class.
43 /// (Or the offset from the virtual base class to the base class, if the
44 /// path from the derived class to the base class involves a virtual base
45 /// class.
Ken Dyck396e4f62011-04-02 01:32:03 +000046 CharUnits NonVirtualOffset;
Anders Carlsson2fca9be2010-02-23 01:34:28 +000047
Ken Dyck396e4f62011-04-02 01:32:03 +000048 BaseOffset() : DerivedClass(0), VirtualBase(0),
49 NonVirtualOffset(CharUnits::Zero()) { }
Anders Carlsson2fca9be2010-02-23 01:34:28 +000050 BaseOffset(const CXXRecordDecl *DerivedClass,
Ken Dyck396e4f62011-04-02 01:32:03 +000051 const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset)
Anders Carlsson2fca9be2010-02-23 01:34:28 +000052 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
53 NonVirtualOffset(NonVirtualOffset) { }
54
Ken Dyck396e4f62011-04-02 01:32:03 +000055 bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
Anders Carlsson2fca9be2010-02-23 01:34:28 +000056};
57
Anders Carlssonbdf73d82010-02-11 21:24:32 +000058/// FinalOverriders - Contains the final overrider member functions for all
59/// member functions in the base subobjects of a class.
60class FinalOverriders {
Anders Carlssona7575822010-02-12 16:55:34 +000061public:
62 /// OverriderInfo - Information about a final overrider.
63 struct OverriderInfo {
64 /// Method - The method decl of the overrider.
65 const CXXMethodDecl *Method;
Anders Carlsson96309c42010-02-18 16:29:24 +000066
Anders Carlssondb4022c2010-03-10 02:33:41 +000067 /// Offset - the base offset of the overrider in the layout class.
Ken Dyck17d85192011-03-26 01:29:16 +000068 CharUnits Offset;
Anders Carlssona7575822010-02-12 16:55:34 +000069
Ken Dyck17d85192011-03-26 01:29:16 +000070 OverriderInfo() : Method(0), Offset(CharUnits::Zero()) { }
Anders Carlssona7575822010-02-12 16:55:34 +000071 };
72
73private:
Anders Carlssonbdf73d82010-02-11 21:24:32 +000074 /// MostDerivedClass - The most derived class for which the final overriders
75 /// are stored.
76 const CXXRecordDecl *MostDerivedClass;
77
Anders Carlssondb4022c2010-03-10 02:33:41 +000078 /// MostDerivedClassOffset - If we're building final overriders for a
79 /// construction vtable, this holds the offset from the layout class to the
80 /// most derived class.
Ken Dyckfc724202011-03-27 20:10:29 +000081 const CharUnits MostDerivedClassOffset;
Anders Carlssondb4022c2010-03-10 02:33:41 +000082
83 /// LayoutClass - The class we're using for layout information. Will be
84 /// different than the most derived class if the final overriders are for a
85 /// construction vtable.
86 const CXXRecordDecl *LayoutClass;
87
Anders Carlssonbdf73d82010-02-11 21:24:32 +000088 ASTContext &Context;
89
90 /// MostDerivedClassLayout - the AST record layout of the most derived class.
91 const ASTRecordLayout &MostDerivedClassLayout;
92
Anders Carlsson0a028602010-06-01 23:52:26 +000093 /// MethodBaseOffsetPairTy - Uniquely identifies a member function
Anders Carlsson0c0eeb22010-02-13 02:02:03 +000094 /// in a base subobject.
Ken Dycke6203132011-03-26 01:18:17 +000095 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
Anders Carlsson0a028602010-06-01 23:52:26 +000096
97 typedef llvm::DenseMap<MethodBaseOffsetPairTy,
Anders Carlssona7575822010-02-12 16:55:34 +000098 OverriderInfo> OverridersMapTy;
Anders Carlssonbdf73d82010-02-11 21:24:32 +000099
100 /// OverridersMap - The final overriders for all virtual member functions of
101 /// all the base subobjects of the most derived class.
102 OverridersMapTy OverridersMap;
103
Anders Carlsson147a23d2010-06-04 00:54:04 +0000104 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
105 /// as a record decl and a subobject number) and its offsets in the most
106 /// derived class as well as the layout class.
107 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
Ken Dyck35c416b2011-03-26 01:09:13 +0000108 CharUnits> SubobjectOffsetMapTy;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000109
110 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
111
112 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
113 /// given base.
114 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000115 CharUnits OffsetInLayoutClass,
Anders Carlsson147a23d2010-06-04 00:54:04 +0000116 SubobjectOffsetMapTy &SubobjectOffsets,
117 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
118 SubobjectCountMapTy &SubobjectCounts);
119
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000120 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
121
122 /// dump - dump the final overriders for a base subobject, and all its direct
123 /// and indirect base subobjects.
124 void dump(llvm::raw_ostream &Out, BaseSubobject Base,
125 VisitedVirtualBasesSetTy& VisitedVirtualBases);
126
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000127public:
Anders Carlssondb4022c2010-03-10 02:33:41 +0000128 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
Ken Dyckfc724202011-03-27 20:10:29 +0000129 CharUnits MostDerivedClassOffset,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000130 const CXXRecordDecl *LayoutClass);
Anders Carlsson2157d302010-02-24 22:18:01 +0000131
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000132 /// getOverrider - Get the final overrider for the given method declaration in
Anders Carlsson26387642010-06-01 23:17:37 +0000133 /// the subobject with the given base offset.
134 OverriderInfo getOverrider(const CXXMethodDecl *MD,
Ken Dycke6203132011-03-26 01:18:17 +0000135 CharUnits BaseOffset) const {
Anders Carlsson0a028602010-06-01 23:52:26 +0000136 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000137 "Did not find overrider!");
138
Anders Carlsson0a028602010-06-01 23:52:26 +0000139 return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000140 }
141
142 /// dump - dump the final overriders.
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000143 void dump() {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000144 VisitedVirtualBasesSetTy VisitedVirtualBases;
Ken Dyck4230d522011-03-24 01:21:01 +0000145 dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
146 VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000147 }
148
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000149};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000150
Anders Carlsson327568e2010-02-27 20:02:53 +0000151#define DUMP_OVERRIDERS 0
152
Anders Carlssondb4022c2010-03-10 02:33:41 +0000153FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
Ken Dyckfc724202011-03-27 20:10:29 +0000154 CharUnits MostDerivedClassOffset,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000155 const CXXRecordDecl *LayoutClass)
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000156 : MostDerivedClass(MostDerivedClass),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000157 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000158 Context(MostDerivedClass->getASTContext()),
159 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000160
Anders Carlsson147a23d2010-06-04 00:54:04 +0000161 // Compute base offsets.
162 SubobjectOffsetMapTy SubobjectOffsets;
163 SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
164 SubobjectCountMapTy SubobjectCounts;
Ken Dyck4230d522011-03-24 01:21:01 +0000165 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
166 /*IsVirtual=*/false,
Ken Dyckfc724202011-03-27 20:10:29 +0000167 MostDerivedClassOffset,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000168 SubobjectOffsets, SubobjectLayoutClassOffsets,
169 SubobjectCounts);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000170
171 // Get the the final overriders.
172 CXXFinalOverriderMap FinalOverriders;
173 MostDerivedClass->getFinalOverriders(FinalOverriders);
174
175 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(),
176 E = FinalOverriders.end(); I != E; ++I) {
177 const CXXMethodDecl *MD = I->first;
178 const OverridingMethods& Methods = I->second;
179
180 for (OverridingMethods::const_iterator I = Methods.begin(),
181 E = Methods.end(); I != E; ++I) {
182 unsigned SubobjectNumber = I->first;
183 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(),
184 SubobjectNumber)) &&
185 "Did not find subobject offset!");
186
Ken Dyck35c416b2011-03-26 01:09:13 +0000187 CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
Anders Carlsson147a23d2010-06-04 00:54:04 +0000188 SubobjectNumber)];
189
190 assert(I->second.size() == 1 && "Final overrider is not unique!");
191 const UniqueVirtualMethod &Method = I->second.front();
192
193 const CXXRecordDecl *OverriderRD = Method.Method->getParent();
194 assert(SubobjectLayoutClassOffsets.count(
195 std::make_pair(OverriderRD, Method.Subobject))
196 && "Did not find subobject offset!");
Ken Dyck35c416b2011-03-26 01:09:13 +0000197 CharUnits OverriderOffset =
Anders Carlsson147a23d2010-06-04 00:54:04 +0000198 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD,
199 Method.Subobject)];
200
Ken Dycke6203132011-03-26 01:18:17 +0000201 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
Anders Carlsson147a23d2010-06-04 00:54:04 +0000202 assert(!Overrider.Method && "Overrider should not exist yet!");
203
Ken Dyck17d85192011-03-26 01:29:16 +0000204 Overrider.Offset = OverriderOffset;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000205 Overrider.Method = Method.Method;
206 }
207 }
Anders Carlsson327568e2010-02-27 20:02:53 +0000208
209#if DUMP_OVERRIDERS
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000210 // And dump them (for now).
211 dump();
Anders Carlsson327568e2010-02-27 20:02:53 +0000212#endif
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000213}
214
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000215static BaseOffset ComputeBaseOffset(ASTContext &Context,
216 const CXXRecordDecl *DerivedRD,
217 const CXXBasePath &Path) {
Ken Dyck396e4f62011-04-02 01:32:03 +0000218 CharUnits NonVirtualOffset = CharUnits::Zero();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000219
Anders Carlssona4a54172010-02-13 20:41:15 +0000220 unsigned NonVirtualStart = 0;
221 const CXXRecordDecl *VirtualBase = 0;
222
223 // First, look for the virtual base class.
224 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
225 const CXXBasePathElement &Element = Path[I];
226
227 if (Element.Base->isVirtual()) {
228 // FIXME: Can we break when we find the first virtual base?
229 // (If we can't, can't we just iterate over the path in reverse order?)
230 NonVirtualStart = I + 1;
231 QualType VBaseType = Element.Base->getType();
232 VirtualBase =
233 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
234 }
235 }
236
237 // Now compute the non-virtual offset.
238 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
239 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000240
241 // Check the base class offset.
242 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
Anders Carlssonbf554f62010-02-25 22:18:35 +0000243
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000244 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
245 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
Anders Carlssonbf554f62010-02-25 22:18:35 +0000246
Ken Dyck396e4f62011-04-02 01:32:03 +0000247 NonVirtualOffset += Layout.getBaseClassOffset(Base);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000248 }
249
250 // FIXME: This should probably use CharUnits or something. Maybe we should
251 // even change the base offsets in ASTRecordLayout to be specified in
252 // CharUnits.
Ken Dyck396e4f62011-04-02 01:32:03 +0000253 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset);
Anders Carlssone67dc302010-02-14 00:16:19 +0000254
255}
256
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000257static BaseOffset ComputeBaseOffset(ASTContext &Context,
258 const CXXRecordDecl *BaseRD,
259 const CXXRecordDecl *DerivedRD) {
Anders Carlssone67dc302010-02-14 00:16:19 +0000260 CXXBasePaths Paths(/*FindAmbiguities=*/false,
261 /*RecordPaths=*/true, /*DetectVirtual=*/false);
262
263 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
264 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
265 assert(false && "Class must be derived from the passed in base class!");
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000266 return BaseOffset();
Anders Carlssone67dc302010-02-14 00:16:19 +0000267 }
268
269 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000270}
271
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000272static BaseOffset
Anders Carlssondf31af12010-02-13 23:17:31 +0000273ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
274 const CXXMethodDecl *DerivedMD,
275 const CXXMethodDecl *BaseMD) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000276 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
277 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
278
279 // Canonicalize the return types.
280 CanQualType CanDerivedReturnType =
281 Context.getCanonicalType(DerivedFT->getResultType());
282 CanQualType CanBaseReturnType =
283 Context.getCanonicalType(BaseFT->getResultType());
284
285 assert(CanDerivedReturnType->getTypeClass() ==
286 CanBaseReturnType->getTypeClass() &&
287 "Types must have same type class!");
288
289 if (CanDerivedReturnType == CanBaseReturnType) {
290 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000291 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000292 }
293
294 if (isa<ReferenceType>(CanDerivedReturnType)) {
295 CanDerivedReturnType =
296 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
297 CanBaseReturnType =
298 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
299 } else if (isa<PointerType>(CanDerivedReturnType)) {
300 CanDerivedReturnType =
301 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
302 CanBaseReturnType =
303 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
304 } else {
305 assert(false && "Unexpected return type!");
306 }
307
308 // We need to compare unqualified types here; consider
309 // const T *Base::foo();
310 // T *Derived::foo();
311 if (CanDerivedReturnType.getUnqualifiedType() ==
312 CanBaseReturnType.getUnqualifiedType()) {
313 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000314 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000315 }
316
317 const CXXRecordDecl *DerivedRD =
318 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
319
320 const CXXRecordDecl *BaseRD =
321 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
322
Anders Carlssondf31af12010-02-13 23:17:31 +0000323 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000324}
325
Anders Carlsson147a23d2010-06-04 00:54:04 +0000326void
327FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
Ken Dyckb4c82b42011-03-26 01:36:26 +0000328 CharUnits OffsetInLayoutClass,
Anders Carlsson147a23d2010-06-04 00:54:04 +0000329 SubobjectOffsetMapTy &SubobjectOffsets,
330 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
331 SubobjectCountMapTy &SubobjectCounts) {
332 const CXXRecordDecl *RD = Base.getBase();
333
334 unsigned SubobjectNumber = 0;
335 if (!IsVirtual)
336 SubobjectNumber = ++SubobjectCounts[RD];
337
338 // Set up the subobject to offset mapping.
339 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber))
340 && "Subobject offset already exists!");
341 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber))
342 && "Subobject offset already exists!");
343
Ken Dyck35c416b2011-03-26 01:09:13 +0000344 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset();
Anders Carlsson147a23d2010-06-04 00:54:04 +0000345 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
Ken Dyckb4c82b42011-03-26 01:36:26 +0000346 OffsetInLayoutClass;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000347
348 // Traverse our bases.
349 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
350 E = RD->bases_end(); I != E; ++I) {
351 const CXXRecordDecl *BaseDecl =
352 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
353
Ken Dyck57ebe332011-03-26 00:51:24 +0000354 CharUnits BaseOffset;
355 CharUnits BaseOffsetInLayoutClass;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000356 if (I->isVirtual()) {
357 // Check if we've visited this virtual base before.
358 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0)))
359 continue;
360
361 const ASTRecordLayout &LayoutClassLayout =
362 Context.getASTRecordLayout(LayoutClass);
363
Ken Dyck57ebe332011-03-26 00:51:24 +0000364 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000365 BaseOffsetInLayoutClass =
Ken Dyck57ebe332011-03-26 00:51:24 +0000366 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000367 } else {
368 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ken Dyck57ebe332011-03-26 00:51:24 +0000369 CharUnits Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000370
Ken Dyck57ebe332011-03-26 00:51:24 +0000371 BaseOffset = Base.getBaseOffset() + Offset;
Ken Dyckb4c82b42011-03-26 01:36:26 +0000372 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
Anders Carlsson147a23d2010-06-04 00:54:04 +0000373 }
374
Ken Dyck57ebe332011-03-26 00:51:24 +0000375 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset),
Ken Dyckb4c82b42011-03-26 01:36:26 +0000376 I->isVirtual(), BaseOffsetInLayoutClass,
Ken Dyck4230d522011-03-24 01:21:01 +0000377 SubobjectOffsets, SubobjectLayoutClassOffsets,
378 SubobjectCounts);
Anders Carlsson147a23d2010-06-04 00:54:04 +0000379 }
380}
381
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000382void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base,
383 VisitedVirtualBasesSetTy &VisitedVirtualBases) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000384 const CXXRecordDecl *RD = Base.getBase();
385 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
386
387 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
388 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000389 const CXXRecordDecl *BaseDecl =
390 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
391
Anders Carlssonb828afa2010-02-14 17:05:59 +0000392 // Ignore bases that don't have any virtual member functions.
393 if (!BaseDecl->isPolymorphic())
394 continue;
395
Ken Dyck4230d522011-03-24 01:21:01 +0000396 CharUnits BaseOffset;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000397 if (I->isVirtual()) {
398 if (!VisitedVirtualBases.insert(BaseDecl)) {
399 // We've visited this base before.
400 continue;
401 }
402
Ken Dyck4230d522011-03-24 01:21:01 +0000403 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000404 } else {
Ken Dyck4230d522011-03-24 01:21:01 +0000405 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000406 }
407
Anders Carlssonb9cae1d2010-06-04 00:59:37 +0000408 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000409 }
410
411 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
Ken Dyck4230d522011-03-24 01:21:01 +0000412 Out << Base.getBaseOffset().getQuantity() << ")\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000413
414 // Now dump the overriders for this base subobject.
415 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
416 E = RD->method_end(); I != E; ++I) {
417 const CXXMethodDecl *MD = *I;
418
419 if (!MD->isVirtual())
420 continue;
421
Ken Dycke6203132011-03-26 01:18:17 +0000422 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000423
Anders Carlssond86e5d82010-03-02 03:44:06 +0000424 Out << " " << MD->getQualifiedNameAsString() << " - (";
Anders Carlsson16b73122010-02-12 17:13:23 +0000425 Out << Overrider.Method->getQualifiedNameAsString();
Ken Dyck17d85192011-03-26 01:29:16 +0000426 Out << ", " << ", " << Overrider.Offset.getQuantity() << ')';
Anders Carlssond86e5d82010-03-02 03:44:06 +0000427
Anders Carlsson7097e752010-06-01 23:47:05 +0000428 BaseOffset Offset;
429 if (!Overrider.Method->isPure())
430 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
Anders Carlssona4a54172010-02-13 20:41:15 +0000431
Anders Carlsson7097e752010-06-01 23:47:05 +0000432 if (!Offset.isEmpty()) {
Anders Carlssona4a54172010-02-13 20:41:15 +0000433 Out << " [ret-adj: ";
434 if (Offset.VirtualBase)
435 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000436
Ken Dyck396e4f62011-04-02 01:32:03 +0000437 Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000438 }
Anders Carlssondf31af12010-02-13 23:17:31 +0000439
Anders Carlsson16b73122010-02-12 17:13:23 +0000440 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000441 }
442}
443
Anders Carlsson94464812010-04-10 19:13:06 +0000444/// VTableComponent - Represents a single component in a vtable.
445class VTableComponent {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000446public:
447 enum Kind {
448 CK_VCallOffset,
449 CK_VBaseOffset,
450 CK_OffsetToTop,
451 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000452 CK_FunctionPointer,
453
454 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
455 CK_CompleteDtorPointer,
456
457 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
Anders Carlsson1f464662010-02-19 20:08:13 +0000458 CK_DeletingDtorPointer,
459
460 /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer
461 /// will end up never being called. Such vtable function pointers are
462 /// represented as a CK_UnusedFunctionPointer.
463 CK_UnusedFunctionPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000464 };
465
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000466 static VTableComponent MakeVCallOffset(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000467 return VTableComponent(CK_VCallOffset, Offset);
Anders Carlssond9a67042010-02-17 06:07:19 +0000468 }
469
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000470 static VTableComponent MakeVBaseOffset(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000471 return VTableComponent(CK_VBaseOffset, Offset);
Anders Carlssona661a542010-02-16 04:49:44 +0000472 }
473
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000474 static VTableComponent MakeOffsetToTop(CharUnits Offset) {
Anders Carlsson94464812010-04-10 19:13:06 +0000475 return VTableComponent(CK_OffsetToTop, Offset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000476 }
477
Anders Carlsson94464812010-04-10 19:13:06 +0000478 static VTableComponent MakeRTTI(const CXXRecordDecl *RD) {
479 return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000480 }
481
Anders Carlsson94464812010-04-10 19:13:06 +0000482 static VTableComponent MakeFunction(const CXXMethodDecl *MD) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000483 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000484 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000485
Anders Carlsson94464812010-04-10 19:13:06 +0000486 return VTableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000487 reinterpret_cast<uintptr_t>(MD));
488 }
489
Anders Carlsson94464812010-04-10 19:13:06 +0000490 static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
491 return VTableComponent(CK_CompleteDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000492 reinterpret_cast<uintptr_t>(DD));
493 }
494
Anders Carlsson94464812010-04-10 19:13:06 +0000495 static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
496 return VTableComponent(CK_DeletingDtorPointer,
Anders Carlsson848fa642010-02-11 18:20:28 +0000497 reinterpret_cast<uintptr_t>(DD));
498 }
499
Anders Carlsson94464812010-04-10 19:13:06 +0000500 static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) {
Anders Carlsson1f464662010-02-19 20:08:13 +0000501 assert(!isa<CXXDestructorDecl>(MD) &&
502 "Don't use MakeUnusedFunction with destructors!");
Anders Carlsson94464812010-04-10 19:13:06 +0000503 return VTableComponent(CK_UnusedFunctionPointer,
Anders Carlsson1f464662010-02-19 20:08:13 +0000504 reinterpret_cast<uintptr_t>(MD));
505 }
506
Anders Carlsson94464812010-04-10 19:13:06 +0000507 static VTableComponent getFromOpaqueInteger(uint64_t I) {
508 return VTableComponent(I);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000509 }
510
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000511 /// getKind - Get the kind of this vtable component.
512 Kind getKind() const {
513 return (Kind)(Value & 0x7);
514 }
515
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000516 CharUnits getVCallOffset() const {
Anders Carlssond9a67042010-02-17 06:07:19 +0000517 assert(getKind() == CK_VCallOffset && "Invalid component kind!");
518
519 return getOffset();
520 }
521
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000522 CharUnits getVBaseOffset() const {
Anders Carlssona661a542010-02-16 04:49:44 +0000523 assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
524
525 return getOffset();
526 }
527
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000528 CharUnits getOffsetToTop() const {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000529 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
530
531 return getOffset();
532 }
533
534 const CXXRecordDecl *getRTTIDecl() const {
535 assert(getKind() == CK_RTTI && "Invalid component kind!");
536
537 return reinterpret_cast<CXXRecordDecl *>(getPointer());
538 }
539
540 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000541 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000542
543 return reinterpret_cast<CXXMethodDecl *>(getPointer());
544 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000545
546 const CXXDestructorDecl *getDestructorDecl() const {
547 assert((getKind() == CK_CompleteDtorPointer ||
548 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
549
550 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
551 }
552
Anders Carlsson1f464662010-02-19 20:08:13 +0000553 const CXXMethodDecl *getUnusedFunctionDecl() const {
554 assert(getKind() == CK_UnusedFunctionPointer);
555
556 return reinterpret_cast<CXXMethodDecl *>(getPointer());
557 }
558
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000559private:
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000560 VTableComponent(Kind ComponentKind, CharUnits Offset) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000561 assert((ComponentKind == CK_VCallOffset ||
562 ComponentKind == CK_VBaseOffset ||
563 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000564 assert(Offset.getQuantity() <= ((1LL << 56) - 1) && "Offset is too big!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000565
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000566 Value = ((Offset.getQuantity() << 3) | ComponentKind);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000567 }
568
Anders Carlsson94464812010-04-10 19:13:06 +0000569 VTableComponent(Kind ComponentKind, uintptr_t Ptr) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000570 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000571 ComponentKind == CK_FunctionPointer ||
572 ComponentKind == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000573 ComponentKind == CK_DeletingDtorPointer ||
574 ComponentKind == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000575 "Invalid component kind!");
576
577 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
578
579 Value = Ptr | ComponentKind;
580 }
581
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000582 CharUnits getOffset() const {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000583 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
584 getKind() == CK_OffsetToTop) && "Invalid component kind!");
585
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000586 return CharUnits::fromQuantity(Value >> 3);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000587 }
588
589 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000590 assert((getKind() == CK_RTTI ||
591 getKind() == CK_FunctionPointer ||
592 getKind() == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000593 getKind() == CK_DeletingDtorPointer ||
594 getKind() == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000595 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000596
597 return static_cast<uintptr_t>(Value & ~7ULL);
598 }
599
Anders Carlsson94464812010-04-10 19:13:06 +0000600 explicit VTableComponent(uint64_t Value)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000601 : Value(Value) { }
602
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000603 /// The kind is stored in the lower 3 bits of the value. For offsets, we
604 /// make use of the facts that classes can't be larger than 2^55 bytes,
605 /// so we store the offset in the lower part of the 61 bytes that remain.
606 /// (The reason that we're not simply using a PointerIntPair here is that we
607 /// need the offsets to be 64-bit, even when on a 32-bit machine).
608 int64_t Value;
609};
610
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000611/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
612struct VCallOffsetMap {
613
Ken Dyckc952d382011-04-02 00:51:15 +0000614 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000615
616 /// Offsets - Keeps track of methods and their offsets.
617 // FIXME: This should be a real map and not a vector.
618 llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets;
619
Anders Carlssoneb577d02010-02-18 17:26:40 +0000620 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
621 /// can share the same vcall offset.
622 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
623 const CXXMethodDecl *RHS);
624
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000625public:
626 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
627 /// add was successful, or false if there was already a member function with
628 /// the same signature in the map.
Ken Dyckc952d382011-04-02 00:51:15 +0000629 bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000630
631 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
632 /// vtable address point) for the given virtual member function.
Ken Dyckc952d382011-04-02 00:51:15 +0000633 CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000634
Anders Carlssondba6ef82010-02-25 22:23:13 +0000635 // empty - Return whether the offset map is empty or not.
636 bool empty() const { return Offsets.empty(); }
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000637};
638
John McCall331a03a2010-02-26 20:22:44 +0000639static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
640 const CXXMethodDecl *RHS) {
641 ASTContext &C = LHS->getASTContext(); // TODO: thread this down
642 CanQual<FunctionProtoType>
643 LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(),
644 RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>();
645
646 // Fast-path matches in the canonical types.
647 if (LT == RT) return true;
648
649 // Force the signatures to match. We can't rely on the overrides
650 // list here because there isn't necessarily an inheritance
651 // relationship between the two methods.
652 if (LT.getQualifiers() != RT.getQualifiers() ||
653 LT->getNumArgs() != RT->getNumArgs())
654 return false;
655 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
656 if (LT->getArgType(I) != RT->getArgType(I))
657 return false;
658 return true;
659}
660
Anders Carlssoneb577d02010-02-18 17:26:40 +0000661bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
662 const CXXMethodDecl *RHS) {
663 assert(LHS->isVirtual() && "LHS must be virtual!");
664 assert(RHS->isVirtual() && "LHS must be virtual!");
665
Anders Carlsson55609692010-02-27 04:12:52 +0000666 // A destructor can share a vcall offset with another destructor.
667 if (isa<CXXDestructorDecl>(LHS))
668 return isa<CXXDestructorDecl>(RHS);
669
Anders Carlssoneb577d02010-02-18 17:26:40 +0000670 // FIXME: We need to check more things here.
671
Anders Carlsson55609692010-02-27 04:12:52 +0000672 // The methods must have the same name.
Anders Carlssoneb577d02010-02-18 17:26:40 +0000673 DeclarationName LHSName = LHS->getDeclName();
674 DeclarationName RHSName = RHS->getDeclName();
John McCall331a03a2010-02-26 20:22:44 +0000675 if (LHSName != RHSName)
Anders Carlssoneb577d02010-02-18 17:26:40 +0000676 return false;
John McCall331a03a2010-02-26 20:22:44 +0000677
Anders Carlsson55609692010-02-27 04:12:52 +0000678 // And the same signatures.
679 return HasSameVirtualSignature(LHS, RHS);
Anders Carlssoneb577d02010-02-18 17:26:40 +0000680}
681
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000682bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
Ken Dyckc952d382011-04-02 00:51:15 +0000683 CharUnits OffsetOffset) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000684 // Check if we can reuse an offset.
685 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
686 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
687 return false;
688 }
689
690 // Add the offset.
691 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000692 return true;
693}
694
Ken Dyckc952d382011-04-02 00:51:15 +0000695CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000696 // Look for an offset.
697 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
698 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
699 return Offsets[I].second;
700 }
701
702 assert(false && "Should always find a vcall offset offset!");
Ken Dyckc952d382011-04-02 00:51:15 +0000703 return CharUnits::Zero();
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000704}
705
Anders Carlssond68c0832010-02-25 03:45:56 +0000706/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
707class VCallAndVBaseOffsetBuilder {
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000708public:
Ken Dyckd28c33c2011-04-07 00:55:01 +0000709 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000710 VBaseOffsetOffsetsMapTy;
711
712private:
Anders Carlsson530c40c2010-02-28 01:43:58 +0000713 /// MostDerivedClass - The most derived class for which we're building vcall
714 /// and vbase offsets.
Anders Carlssond68c0832010-02-25 03:45:56 +0000715 const CXXRecordDecl *MostDerivedClass;
716
Anders Carlsson0041a642010-02-27 21:09:00 +0000717 /// LayoutClass - The class we're using for layout information. Will be
718 /// different than the most derived class if we're building a construction
719 /// vtable.
720 const CXXRecordDecl *LayoutClass;
721
Anders Carlssond68c0832010-02-25 03:45:56 +0000722 /// Context - The ASTContext which we will use for layout information.
723 ASTContext &Context;
724
725 /// Components - vcall and vbase offset components
Anders Carlsson94464812010-04-10 19:13:06 +0000726 typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy;
727 VTableComponentVectorTy Components;
Anders Carlssond68c0832010-02-25 03:45:56 +0000728
729 /// VisitedVirtualBases - Visited virtual bases.
730 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
731
732 /// VCallOffsets - Keeps track of vcall offsets.
733 VCallOffsetMap VCallOffsets;
734
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000735
736 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
737 /// relative to the address point.
738 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
739
Anders Carlssond68c0832010-02-25 03:45:56 +0000740 /// FinalOverriders - The final overriders of the most derived class.
741 /// (Can be null when we're not building a vtable of the most derived class).
742 const FinalOverriders *Overriders;
743
744 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
745 /// given base subobject.
746 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000747 CharUnits RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000748
749 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
Ken Dyck301af862011-03-30 10:11:40 +0000750 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000751
752 /// AddVBaseOffsets - Add vbase offsets for the given class.
Ken Dyckbb625e92011-03-31 00:56:47 +0000753 void AddVBaseOffsets(const CXXRecordDecl *Base,
754 CharUnits OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000755
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000756 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
Ken Dyck3ed994b2011-03-31 01:08:46 +0000757 /// chars, relative to the vtable address point.
758 CharUnits getCurrentOffsetOffset() const;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000759
Anders Carlssond68c0832010-02-25 03:45:56 +0000760public:
761 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
Anders Carlsson0041a642010-02-27 21:09:00 +0000762 const CXXRecordDecl *LayoutClass,
Anders Carlssond68c0832010-02-25 03:45:56 +0000763 const FinalOverriders *Overriders,
Anders Carlsson530c40c2010-02-28 01:43:58 +0000764 BaseSubobject Base, bool BaseIsVirtual,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +0000765 CharUnits OffsetInLayoutClass)
Anders Carlsson530c40c2010-02-28 01:43:58 +0000766 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
Anders Carlssond68c0832010-02-25 03:45:56 +0000767 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
768
769 // Add vcall and vbase offsets.
Ken Dyck3ecfa2d2011-03-31 00:48:04 +0000770 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000771 }
772
773 /// Methods for iterating over the components.
Anders Carlsson94464812010-04-10 19:13:06 +0000774 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
Anders Carlssond68c0832010-02-25 03:45:56 +0000775 const_iterator components_begin() const { return Components.rbegin(); }
776 const_iterator components_end() const { return Components.rend(); }
777
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000778 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
779 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000780 return VBaseOffsetOffsets;
781 }
Anders Carlssond68c0832010-02-25 03:45:56 +0000782};
783
784void
785VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
786 bool BaseIsVirtual,
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000787 CharUnits RealBaseOffset) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000788 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
789
790 // Itanium C++ ABI 2.5.2:
791 // ..in classes sharing a virtual table with a primary base class, the vcall
792 // and vbase offsets added by the derived class all come before the vcall
793 // and vbase offsets required by the base class, so that the latter may be
794 // laid out as required by the base class without regard to additions from
795 // the derived class(es).
796
797 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
798 // emit them for the primary base first).
799 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000800 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlssond68c0832010-02-25 03:45:56 +0000801
Ken Dyck57ebe332011-03-26 00:51:24 +0000802 CharUnits PrimaryBaseOffset;
Anders Carlssond68c0832010-02-25 03:45:56 +0000803
804 // Get the base offset of the primary base.
805 if (PrimaryBaseIsVirtual) {
Anders Carlssona14f5972010-10-31 23:22:37 +0000806 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000807 "Primary vbase should have a zero offset!");
808
809 const ASTRecordLayout &MostDerivedClassLayout =
810 Context.getASTRecordLayout(MostDerivedClass);
811
812 PrimaryBaseOffset =
Ken Dyck57ebe332011-03-26 00:51:24 +0000813 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssond68c0832010-02-25 03:45:56 +0000814 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +0000815 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssond68c0832010-02-25 03:45:56 +0000816 "Primary base should have a zero offset!");
817
Ken Dyck57ebe332011-03-26 00:51:24 +0000818 PrimaryBaseOffset = Base.getBaseOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +0000819 }
820
Ken Dyck4230d522011-03-24 01:21:01 +0000821 AddVCallAndVBaseOffsets(
Ken Dyck57ebe332011-03-26 00:51:24 +0000822 BaseSubobject(PrimaryBase,PrimaryBaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +0000823 PrimaryBaseIsVirtual, RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000824 }
825
Ken Dyckbb625e92011-03-31 00:56:47 +0000826 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000827
828 // We only want to add vcall offsets for virtual bases.
829 if (BaseIsVirtual)
Ken Dyckf38d8ae2011-03-31 00:37:24 +0000830 AddVCallOffsets(Base, RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000831}
832
Ken Dyck3ed994b2011-03-31 01:08:46 +0000833CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000834 // OffsetIndex is the index of this vcall or vbase offset, relative to the
835 // vtable address point. (We subtract 3 to account for the information just
836 // above the address point, the RTTI info, the offset to top, and the
837 // vcall offset itself).
838 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
839
Ken Dyck3ed994b2011-03-31 01:08:46 +0000840 CharUnits PointerWidth =
841 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
842 CharUnits OffsetOffset = PointerWidth * OffsetIndex;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000843 return OffsetOffset;
844}
845
Anders Carlssond68c0832010-02-25 03:45:56 +0000846void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
Ken Dyck301af862011-03-30 10:11:40 +0000847 CharUnits VBaseOffset) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000848 const CXXRecordDecl *RD = Base.getBase();
849 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
850
John McCall331a03a2010-02-26 20:22:44 +0000851 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
852
Anders Carlssond68c0832010-02-25 03:45:56 +0000853 // Handle the primary base first.
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000854 // We only want to add vcall offsets if the base is non-virtual; a virtual
855 // primary base will have its vcall and vbase offsets emitted already.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000856 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000857 // Get the base offset of the primary base.
Anders Carlssona14f5972010-10-31 23:22:37 +0000858 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000859 "Primary base should have a zero offset!");
Anders Carlssond68c0832010-02-25 03:45:56 +0000860
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000861 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
Anders Carlssond68c0832010-02-25 03:45:56 +0000862 VBaseOffset);
863 }
Anders Carlssonaf6ddf22010-04-11 20:04:11 +0000864
Anders Carlssond68c0832010-02-25 03:45:56 +0000865 // Add the vcall offsets.
866 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
867 E = RD->method_end(); I != E; ++I) {
868 const CXXMethodDecl *MD = *I;
869
870 if (!MD->isVirtual())
871 continue;
872
Ken Dyck3ed994b2011-03-31 01:08:46 +0000873 CharUnits OffsetOffset = getCurrentOffsetOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +0000874
875 // Don't add a vcall offset if we already have one for this member function
876 // signature.
Ken Dyckc952d382011-04-02 00:51:15 +0000877 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
Anders Carlssond68c0832010-02-25 03:45:56 +0000878 continue;
879
Ken Dyck0c64b7d2011-03-30 01:21:33 +0000880 CharUnits Offset = CharUnits::Zero();
Anders Carlssond68c0832010-02-25 03:45:56 +0000881
882 if (Overriders) {
883 // Get the final overrider.
884 FinalOverriders::OverriderInfo Overrider =
Ken Dycke6203132011-03-26 01:18:17 +0000885 Overriders->getOverrider(MD, Base.getBaseOffset());
Anders Carlssond68c0832010-02-25 03:45:56 +0000886
Anders Carlsson438bc422010-02-28 00:10:58 +0000887 /// The vcall offset is the offset from the virtual base to the object
888 /// where the function was overridden.
Ken Dyck301af862011-03-30 10:11:40 +0000889 Offset = Overrider.Offset - VBaseOffset;
Anders Carlssond68c0832010-02-25 03:45:56 +0000890 }
891
Ken Dyck0c64b7d2011-03-30 01:21:33 +0000892 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000893 VTableComponent::MakeVCallOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000894 }
895
896 // And iterate over all non-virtual bases (ignoring the primary base).
897 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
898 E = RD->bases_end(); I != E; ++I) {
899
900 if (I->isVirtual())
901 continue;
902
903 const CXXRecordDecl *BaseDecl =
904 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
John McCall331a03a2010-02-26 20:22:44 +0000905 if (BaseDecl == PrimaryBase)
906 continue;
Anders Carlssond68c0832010-02-25 03:45:56 +0000907
908 // Get the base offset of this base.
Ken Dyck57ebe332011-03-26 00:51:24 +0000909 CharUnits BaseOffset = Base.getBaseOffset() +
910 Layout.getBaseClassOffset(BaseDecl);
Anders Carlssond68c0832010-02-25 03:45:56 +0000911
Ken Dyck57ebe332011-03-26 00:51:24 +0000912 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
913 VBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000914 }
915}
916
Ken Dyckbb625e92011-03-31 00:56:47 +0000917void
918VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
919 CharUnits OffsetInLayoutClass) {
Anders Carlsson0041a642010-02-27 21:09:00 +0000920 const ASTRecordLayout &LayoutClassLayout =
921 Context.getASTRecordLayout(LayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000922
923 // Add vbase offsets.
924 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
925 E = RD->bases_end(); I != E; ++I) {
926 const CXXRecordDecl *BaseDecl =
927 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
928
929 // Check if this is a virtual base that we haven't visited before.
930 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
Ken Dyckbb625e92011-03-31 00:56:47 +0000931 CharUnits Offset =
932 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000933
934 // Add the vbase offset offset.
935 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
936 "vbase offset offset already exists!");
937
Ken Dyck3ed994b2011-03-31 01:08:46 +0000938 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
939 VBaseOffsetOffsets.insert(
Ken Dyckd28c33c2011-04-07 00:55:01 +0000940 std::make_pair(BaseDecl, VBaseOffsetOffset));
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000941
Ken Dyckbb625e92011-03-31 00:56:47 +0000942 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000943 VTableComponent::MakeVBaseOffset(Offset));
Anders Carlssond68c0832010-02-25 03:45:56 +0000944 }
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000945
Anders Carlssond68c0832010-02-25 03:45:56 +0000946 // Check the base class looking for more vbase offsets.
Anders Carlsson88800662010-03-02 05:40:45 +0000947 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000948 }
949}
950
Anders Carlsson94464812010-04-10 19:13:06 +0000951/// VTableBuilder - Class for building vtable layout information.
952class VTableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +0000953public:
Anders Carlssonb9021e92010-02-27 16:18:19 +0000954 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
955 /// primary bases.
956 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
957 PrimaryBasesSetVectorTy;
958
Ken Dyckd28c33c2011-04-07 00:55:01 +0000959 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
Anders Carlssonccd83d72010-03-24 16:42:11 +0000960 VBaseOffsetOffsetsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +0000961
962 typedef llvm::DenseMap<BaseSubobject, uint64_t>
963 AddressPointsMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000964
Anders Carlsson57071e22010-02-12 05:25:12 +0000965private:
Anders Carlssonaf440352010-03-23 04:11:45 +0000966 /// VTables - Global vtable information.
967 CodeGenVTables &VTables;
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000968
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000969 /// MostDerivedClass - The most derived class for which we're building this
970 /// vtable.
971 const CXXRecordDecl *MostDerivedClass;
972
Anders Carlssond03a3262010-02-27 20:39:05 +0000973 /// MostDerivedClassOffset - If we're building a construction vtable, this
974 /// holds the offset from the layout class to the most derived class.
Ken Dyck23da84a2011-03-27 20:20:28 +0000975 const CharUnits MostDerivedClassOffset;
Anders Carlssond03a3262010-02-27 20:39:05 +0000976
Anders Carlsson5d7af6b2010-02-28 00:36:23 +0000977 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
978 /// base. (This only makes sense when building a construction vtable).
979 bool MostDerivedClassIsVirtual;
980
Anders Carlssond03a3262010-02-27 20:39:05 +0000981 /// LayoutClass - The class we're using for layout information. Will be
982 /// different than the most derived class if we're building a construction
983 /// vtable.
984 const CXXRecordDecl *LayoutClass;
985
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000986 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +0000987 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000988
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000989 /// FinalOverriders - The final overriders of the most derived class.
Anders Carlssond68c0832010-02-25 03:45:56 +0000990 const FinalOverriders Overriders;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000991
Anders Carlssondba6ef82010-02-25 22:23:13 +0000992 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
993 /// bases in this vtable.
994 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000995
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000996 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
997 /// the most derived class.
998 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
999
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001000 /// Components - The components of the vtable being built.
Anders Carlsson94464812010-04-10 19:13:06 +00001001 llvm::SmallVector<VTableComponent, 64> Components;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001002
Anders Carlsson822307b2010-02-11 17:18:51 +00001003 /// AddressPoints - Address points for the vtable being built.
Anders Carlssonff143f82010-03-25 00:35:49 +00001004 AddressPointsMapTy AddressPoints;
Anders Carlsson822307b2010-02-11 17:18:51 +00001005
Anders Carlssonb9021e92010-02-27 16:18:19 +00001006 /// MethodInfo - Contains information about a method in a vtable.
1007 /// (Used for computing 'this' pointer adjustment thunks.
1008 struct MethodInfo {
1009 /// BaseOffset - The base offset of this method.
Ken Dyck82abeae2011-03-27 18:57:53 +00001010 const CharUnits BaseOffset;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001011
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001012 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
1013 /// method.
Ken Dyck82abeae2011-03-27 18:57:53 +00001014 const CharUnits BaseOffsetInLayoutClass;
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001015
Anders Carlsson718d0692010-04-11 22:07:06 +00001016 /// VTableIndex - The index in the vtable that this method has.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001017 /// (For destructors, this is the index of the complete destructor).
Anders Carlsson718d0692010-04-11 22:07:06 +00001018 const uint64_t VTableIndex;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001019
Ken Dyck82abeae2011-03-27 18:57:53 +00001020 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson718d0692010-04-11 22:07:06 +00001021 uint64_t VTableIndex)
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001022 : BaseOffset(BaseOffset),
1023 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
Anders Carlsson718d0692010-04-11 22:07:06 +00001024 VTableIndex(VTableIndex) { }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001025
Ken Dyck82abeae2011-03-27 18:57:53 +00001026 MethodInfo()
1027 : BaseOffset(CharUnits::Zero()),
1028 BaseOffsetInLayoutClass(CharUnits::Zero()),
1029 VTableIndex(0) { }
Anders Carlssonb9021e92010-02-27 16:18:19 +00001030 };
1031
1032 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
1033
1034 /// MethodInfoMap - The information for all methods in the vtable we're
1035 /// currently building.
1036 MethodInfoMapTy MethodInfoMap;
1037
Anders Carlsson718d0692010-04-11 22:07:06 +00001038 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
Anders Carlssondd366812010-03-12 04:54:20 +00001039
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001040 /// VTableThunks - The thunks by vtable index in the vtable currently being
1041 /// built.
Anders Carlsson718d0692010-04-11 22:07:06 +00001042 VTableThunksMapTy VTableThunks;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001043
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001044 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
1045 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001046
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001047 /// Thunks - A map that contains all the thunks needed for all methods in the
1048 /// most derived class for which the vtable is currently being built.
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001049 ThunksMapTy Thunks;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001050
1051 /// AddThunk - Add a thunk for the given method.
Anders Carlsson9d734f32010-03-21 20:27:14 +00001052 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001053
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001054 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
1055 /// part of the vtable we're currently building.
1056 void ComputeThisAdjustments();
1057
Anders Carlssona661a542010-02-16 04:49:44 +00001058 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1059
Anders Carlssonb9021e92010-02-27 16:18:19 +00001060 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
Anders Carlssonaf280c02010-02-23 03:48:14 +00001061 /// some other base.
1062 VisitedVirtualBasesSetTy PrimaryVirtualBases;
1063
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001064 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001065 /// adjustment base offset.
Anders Carlsson2fca9be2010-02-23 01:34:28 +00001066 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001067
Anders Carlsson379017f2010-02-27 19:57:44 +00001068 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
1069 /// the 'this' pointer from the base subobject to the derived subobject.
1070 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1071 BaseSubobject Derived) const;
1072
Anders Carlsson60cc72a2010-02-18 17:00:09 +00001073 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
Anders Carlsson617def32010-03-12 05:28:07 +00001074 /// given virtual member function, its offset in the layout class and its
1075 /// final overrider.
1076 ThisAdjustment
1077 ComputeThisAdjustment(const CXXMethodDecl *MD,
Ken Dyck0e55ade2011-03-27 19:03:44 +00001078 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson617def32010-03-12 05:28:07 +00001079 FinalOverriders::OverriderInfo Overrider);
1080
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001081 /// AddMethod - Add a single virtual member function to the vtable
1082 /// components vector.
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001083 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001084
Anders Carlsson464c8f12010-02-23 04:26:39 +00001085 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
1086 /// part of the vtable.
1087 ///
1088 /// Itanium C++ ABI 2.5.2:
1089 ///
1090 /// struct A { virtual void f(); };
1091 /// struct B : virtual public A { int i; };
1092 /// struct C : virtual public A { int j; };
1093 /// struct D : public B, public C {};
1094 ///
1095 /// When B and C are declared, A is a primary base in each case, so although
1096 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
1097 /// adjustment is required and no thunk is generated. However, inside D
1098 /// objects, A is no longer a primary base of C, so if we allowed calls to
1099 /// C::f() to use the copy of A's vtable in the C subobject, we would need
1100 /// to adjust this from C* to B::A*, which would require a third-party
1101 /// thunk. Since we require that a call to C::f() first convert to A*,
1102 /// C-in-D's copy of A's vtable is never referenced, so this is not
1103 /// necessary.
Anders Carlsson60396612010-03-10 06:51:42 +00001104 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
Ken Dyck413e7d22011-04-02 00:44:58 +00001105 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001106 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001107 CharUnits FirstBaseOffsetInLayoutClass) const;
Anders Carlsson60396612010-03-10 06:51:42 +00001108
Anders Carlsson464c8f12010-02-23 04:26:39 +00001109
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001110 /// AddMethods - Add the methods of this base subobject and all its
1111 /// primary bases to the vtable components vector.
Ken Dyck3a726802011-03-27 19:15:11 +00001112 void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001113 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck3a726802011-03-27 19:15:11 +00001114 CharUnits FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001115 PrimaryBasesSetVectorTy &PrimaryBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001116
Anders Carlsson718d0692010-04-11 22:07:06 +00001117 // LayoutVTable - Layout the vtable for the given base class, including its
Anders Carlsson89ad4212010-02-16 16:26:28 +00001118 // secondary vtables and any vtables for virtual bases.
Anders Carlsson718d0692010-04-11 22:07:06 +00001119 void LayoutVTable();
Anders Carlsson89ad4212010-02-16 16:26:28 +00001120
Anders Carlsson718d0692010-04-11 22:07:06 +00001121 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
Anders Carlsson89ad4212010-02-16 16:26:28 +00001122 /// given base subobject, as well as all its secondary vtables.
Anders Carlsson91232842010-04-11 22:03:57 +00001123 ///
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001124 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1125 /// or a direct or indirect base of a virtual base.
1126 ///
Anders Carlsson91232842010-04-11 22:03:57 +00001127 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
1128 /// in the layout class.
Anders Carlsson718d0692010-04-11 22:07:06 +00001129 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001130 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001131 bool BaseIsVirtualInLayoutClass,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001132 CharUnits OffsetInLayoutClass);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001133
Anders Carlsson718d0692010-04-11 22:07:06 +00001134 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
Anders Carlssonc7b63162010-02-16 16:02:57 +00001135 /// subobject.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001136 ///
1137 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1138 /// or a direct or indirect base of a virtual base.
Anders Carlsson718d0692010-04-11 22:07:06 +00001139 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
Ken Dyck75762b32011-03-30 00:50:28 +00001140 CharUnits OffsetInLayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001141
1142 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
1143 /// class hierarchy.
1144 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Ken Dyck8fe59932011-03-30 01:10:23 +00001145 CharUnits OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001146 VisitedVirtualBasesSetTy &VBases);
1147
Anders Carlsson718d0692010-04-11 22:07:06 +00001148 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001149 /// given base (excluding any primary bases).
Anders Carlsson718d0692010-04-11 22:07:06 +00001150 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001151 VisitedVirtualBasesSetTy &VBases);
1152
Anders Carlsson718d0692010-04-11 22:07:06 +00001153 /// isBuildingConstructionVTable - Return whether this vtable builder is
Anders Carlsson438bc422010-02-28 00:10:58 +00001154 /// building a construction vtable.
Anders Carlsson718d0692010-04-11 22:07:06 +00001155 bool isBuildingConstructorVTable() const {
Anders Carlsson438bc422010-02-28 00:10:58 +00001156 return MostDerivedClass != LayoutClass;
1157 }
1158
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001159public:
Anders Carlsson94464812010-04-10 19:13:06 +00001160 VTableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass,
Ken Dyck23da84a2011-03-27 20:20:28 +00001161 CharUnits MostDerivedClassOffset,
1162 bool MostDerivedClassIsVirtual, const
1163 CXXRecordDecl *LayoutClass)
Anders Carlssonaf440352010-03-23 04:11:45 +00001164 : VTables(VTables), MostDerivedClass(MostDerivedClass),
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001165 MostDerivedClassOffset(MostDerivedClassOffset),
1166 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
1167 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
Ken Dyck23da84a2011-03-27 20:20:28 +00001168 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001169
Anders Carlsson718d0692010-04-11 22:07:06 +00001170 LayoutVTable();
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001171 }
1172
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00001173 ThunksMapTy::const_iterator thunks_begin() const {
1174 return Thunks.begin();
1175 }
1176
1177 ThunksMapTy::const_iterator thunks_end() const {
1178 return Thunks.end();
1179 }
1180
Anders Carlssonccd83d72010-03-24 16:42:11 +00001181 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1182 return VBaseOffsetOffsets;
1183 }
1184
1185 /// getNumVTableComponents - Return the number of components in the vtable
1186 /// currently built.
1187 uint64_t getNumVTableComponents() const {
1188 return Components.size();
1189 }
1190
Anders Carlsson66d567d2010-03-25 00:51:13 +00001191 const uint64_t *vtable_components_data_begin() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001192 return reinterpret_cast<const uint64_t *>(Components.begin());
1193 }
1194
Anders Carlsson66d567d2010-03-25 00:51:13 +00001195 const uint64_t *vtable_components_data_end() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001196 return reinterpret_cast<const uint64_t *>(Components.end());
1197 }
1198
Anders Carlsson66d567d2010-03-25 00:51:13 +00001199 AddressPointsMapTy::const_iterator address_points_begin() const {
1200 return AddressPoints.begin();
1201 }
1202
1203 AddressPointsMapTy::const_iterator address_points_end() const {
1204 return AddressPoints.end();
1205 }
1206
Anders Carlsson718d0692010-04-11 22:07:06 +00001207 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001208 return VTableThunks.begin();
1209 }
1210
Anders Carlsson718d0692010-04-11 22:07:06 +00001211 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001212 return VTableThunks.end();
1213 }
1214
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001215 /// dumpLayout - Dump the vtable layout.
1216 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001217};
1218
Anders Carlsson94464812010-04-10 19:13:06 +00001219void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
Anders Carlsson718d0692010-04-11 22:07:06 +00001220 assert(!isBuildingConstructorVTable() &&
Anders Carlssonbca5d372010-03-22 20:06:40 +00001221 "Can't add thunks for construction vtable");
Anders Carlsson9135a842010-03-18 02:44:19 +00001222
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001223 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001224
1225 // Check if we have this thunk already.
1226 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1227 ThunksVector.end())
1228 return;
1229
1230 ThunksVector.push_back(Thunk);
1231}
1232
Anders Carlssonf622b452010-04-10 20:39:29 +00001233typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1234
1235/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1236/// the overridden methods that the function decl overrides.
1237static void
1238ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1239 OverriddenMethodsSetTy& OverriddenMethods) {
1240 assert(MD->isVirtual() && "Method is not virtual!");
1241
Anders Carlsson57071e22010-02-12 05:25:12 +00001242 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1243 E = MD->end_overridden_methods(); I != E; ++I) {
1244 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001245
Anders Carlssonf622b452010-04-10 20:39:29 +00001246 OverriddenMethods.insert(OverriddenMD);
1247
1248 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods);
Anders Carlsson57071e22010-02-12 05:25:12 +00001249 }
Anders Carlsson57071e22010-02-12 05:25:12 +00001250}
1251
Anders Carlsson94464812010-04-10 19:13:06 +00001252void VTableBuilder::ComputeThisAdjustments() {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001253 // Now go through the method info map and see if any of the methods need
1254 // 'this' pointer adjustments.
1255 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1256 E = MethodInfoMap.end(); I != E; ++I) {
1257 const CXXMethodDecl *MD = I->first;
1258 const MethodInfo &MethodInfo = I->second;
1259
Anders Carlsson617def32010-03-12 05:28:07 +00001260 // Ignore adjustments for unused function pointers.
Anders Carlsson718d0692010-04-11 22:07:06 +00001261 uint64_t VTableIndex = MethodInfo.VTableIndex;
1262 if (Components[VTableIndex].getKind() ==
Anders Carlsson94464812010-04-10 19:13:06 +00001263 VTableComponent::CK_UnusedFunctionPointer)
Anders Carlsson617def32010-03-12 05:28:07 +00001264 continue;
1265
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001266 // Get the final overrider for this method.
1267 FinalOverriders::OverriderInfo Overrider =
Ken Dyck82abeae2011-03-27 18:57:53 +00001268 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001269
Anders Carlssonbca5d372010-03-22 20:06:40 +00001270 // Check if we need an adjustment at all.
Ken Dyck82abeae2011-03-27 18:57:53 +00001271 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
Anders Carlssonadb507d2010-03-29 15:08:41 +00001272 // When a return thunk is needed by a derived class that overrides a
1273 // virtual base, gcc uses a virtual 'this' adjustment as well.
1274 // While the thunk itself might be needed by vtables in subclasses or
1275 // in construction vtables, there doesn't seem to be a reason for using
1276 // the thunk in this vtable. Still, we do so to match gcc.
Anders Carlsson718d0692010-04-11 22:07:06 +00001277 if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
Anders Carlssonadb507d2010-03-29 15:08:41 +00001278 continue;
1279 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001280
Anders Carlsson617def32010-03-12 05:28:07 +00001281 ThisAdjustment ThisAdjustment =
Ken Dyck0e55ade2011-03-27 19:03:44 +00001282 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
Anders Carlsson617def32010-03-12 05:28:07 +00001283
1284 if (ThisAdjustment.isEmpty())
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001285 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001286
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001287 // Add it.
Anders Carlsson718d0692010-04-11 22:07:06 +00001288 VTableThunks[VTableIndex].This = ThisAdjustment;
Anders Carlsson617def32010-03-12 05:28:07 +00001289
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001290 if (isa<CXXDestructorDecl>(MD)) {
1291 // Add an adjustment for the deleting destructor as well.
Anders Carlsson718d0692010-04-11 22:07:06 +00001292 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001293 }
1294 }
1295
1296 /// Clear the method info map.
1297 MethodInfoMap.clear();
Anders Carlsson25394212010-03-22 15:47:01 +00001298
Anders Carlsson718d0692010-04-11 22:07:06 +00001299 if (isBuildingConstructorVTable()) {
Anders Carlsson25394212010-03-22 15:47:01 +00001300 // We don't need to store thunk information for construction vtables.
1301 return;
1302 }
1303
Anders Carlsson718d0692010-04-11 22:07:06 +00001304 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(),
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001305 E = VTableThunks.end(); I != E; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00001306 const VTableComponent &Component = Components[I->first];
Anders Carlsson25394212010-03-22 15:47:01 +00001307 const ThunkInfo &Thunk = I->second;
Anders Carlssonbca5d372010-03-22 20:06:40 +00001308 const CXXMethodDecl *MD;
Anders Carlsson25394212010-03-22 15:47:01 +00001309
1310 switch (Component.getKind()) {
1311 default:
1312 llvm_unreachable("Unexpected vtable component kind!");
Anders Carlsson94464812010-04-10 19:13:06 +00001313 case VTableComponent::CK_FunctionPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001314 MD = Component.getFunctionDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001315 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001316 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001317 MD = Component.getDestructorDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001318 break;
Anders Carlsson94464812010-04-10 19:13:06 +00001319 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson25394212010-03-22 15:47:01 +00001320 // We've already added the thunk when we saw the complete dtor pointer.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001321 continue;
Anders Carlsson25394212010-03-22 15:47:01 +00001322 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001323
1324 if (MD->getParent() == MostDerivedClass)
1325 AddThunk(MD, Thunk);
Anders Carlsson25394212010-03-22 15:47:01 +00001326 }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001327}
1328
Anders Carlsson94464812010-04-10 19:13:06 +00001329ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001330 ReturnAdjustment Adjustment;
1331
1332 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001333 if (Offset.VirtualBase) {
1334 // Get the virtual base offset offset.
Anders Carlsson1972ced2010-03-11 07:00:45 +00001335 if (Offset.DerivedClass == MostDerivedClass) {
1336 // We can get the offset offset directly from our map.
1337 Adjustment.VBaseOffsetOffset =
Ken Dyckd28c33c2011-04-07 00:55:01 +00001338 VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
Anders Carlsson1972ced2010-03-11 07:00:45 +00001339 } else {
1340 Adjustment.VBaseOffsetOffset =
Anders Carlssonaf440352010-03-23 04:11:45 +00001341 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
Devang Patel62c117d2011-04-04 20:36:06 +00001342 Offset.VirtualBase);
Anders Carlsson1972ced2010-03-11 07:00:45 +00001343 }
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001344 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001345
Ken Dyck396e4f62011-04-02 01:32:03 +00001346 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001347 }
1348
1349 return Adjustment;
1350}
1351
Anders Carlsson379017f2010-02-27 19:57:44 +00001352BaseOffset
Anders Carlsson94464812010-04-10 19:13:06 +00001353VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
Anders Carlsson379017f2010-02-27 19:57:44 +00001354 BaseSubobject Derived) const {
1355 const CXXRecordDecl *BaseRD = Base.getBase();
1356 const CXXRecordDecl *DerivedRD = Derived.getBase();
1357
1358 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1359 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1360
1361 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
1362 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
1363 assert(false && "Class must be derived from the passed in base class!");
1364 return BaseOffset();
1365 }
1366
1367 // We have to go through all the paths, and see which one leads us to the
1368 // right base subobject.
1369 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1370 I != E; ++I) {
1371 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1372
Ken Dyck396e4f62011-04-02 01:32:03 +00001373 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
Anders Carlsson379017f2010-02-27 19:57:44 +00001374
1375 if (Offset.VirtualBase) {
1376 // If we have a virtual base class, the non-virtual offset is relative
1377 // to the virtual base class offset.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001378 const ASTRecordLayout &LayoutClassLayout =
1379 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson379017f2010-02-27 19:57:44 +00001380
1381 /// Get the virtual base offset, relative to the most derived class
1382 /// layout.
1383 OffsetToBaseSubobject +=
Ken Dyck57ebe332011-03-26 00:51:24 +00001384 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
Anders Carlsson379017f2010-02-27 19:57:44 +00001385 } else {
1386 // Otherwise, the non-virtual offset is relative to the derived class
1387 // offset.
Ken Dyck57ebe332011-03-26 00:51:24 +00001388 OffsetToBaseSubobject += Derived.getBaseOffset();
Anders Carlsson379017f2010-02-27 19:57:44 +00001389 }
1390
1391 // Check if this path gives us the right base subobject.
Ken Dyck57ebe332011-03-26 00:51:24 +00001392 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
Anders Carlsson379017f2010-02-27 19:57:44 +00001393 // Since we're going from the base class _to_ the derived class, we'll
1394 // invert the non-virtual offset here.
1395 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1396 return Offset;
1397 }
1398 }
1399
1400 return BaseOffset();
1401}
1402
Anders Carlsson80d8d7d2010-03-23 15:13:06 +00001403ThisAdjustment
Anders Carlsson94464812010-04-10 19:13:06 +00001404VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD,
Ken Dyck0e55ade2011-03-27 19:03:44 +00001405 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson617def32010-03-12 05:28:07 +00001406 FinalOverriders::OverriderInfo Overrider) {
Anders Carlsson617def32010-03-12 05:28:07 +00001407 // Ignore adjustments for pure virtual member functions.
1408 if (Overrider.Method->isPure())
1409 return ThisAdjustment();
1410
Ken Dyck0e55ade2011-03-27 19:03:44 +00001411 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1412 BaseOffsetInLayoutClass);
Anders Carlsson617def32010-03-12 05:28:07 +00001413
1414 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
Ken Dyck17d85192011-03-26 01:29:16 +00001415 Overrider.Offset);
Anders Carlsson617def32010-03-12 05:28:07 +00001416
1417 // Compute the adjustment offset.
1418 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1419 OverriderBaseSubobject);
1420 if (Offset.isEmpty())
1421 return ThisAdjustment();
1422
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001423 ThisAdjustment Adjustment;
1424
Anders Carlsson617def32010-03-12 05:28:07 +00001425 if (Offset.VirtualBase) {
1426 // Get the vcall offset map for this virtual base.
1427 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
Anders Carlssoneb577d02010-02-18 17:26:40 +00001428
Anders Carlsson617def32010-03-12 05:28:07 +00001429 if (VCallOffsets.empty()) {
1430 // We don't have vcall offsets for this virtual base, go ahead and
1431 // build them.
1432 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
1433 /*FinalOverriders=*/0,
Ken Dyck4230d522011-03-24 01:21:01 +00001434 BaseSubobject(Offset.VirtualBase,
1435 CharUnits::Zero()),
Anders Carlsson617def32010-03-12 05:28:07 +00001436 /*BaseIsVirtual=*/true,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00001437 /*OffsetInLayoutClass=*/
1438 CharUnits::Zero());
Anders Carlsson617def32010-03-12 05:28:07 +00001439
1440 VCallOffsets = Builder.getVCallOffsets();
1441 }
1442
Ken Dyckc952d382011-04-02 00:51:15 +00001443 Adjustment.VCallOffsetOffset =
1444 VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001445 }
Anders Carlsson617def32010-03-12 05:28:07 +00001446
1447 // Set the non-virtual part of the adjustment.
Ken Dyck396e4f62011-04-02 01:32:03 +00001448 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001449
1450 return Adjustment;
1451}
Anders Carlsson617def32010-03-12 05:28:07 +00001452
Anders Carlsson57071e22010-02-12 05:25:12 +00001453void
Anders Carlsson94464812010-04-10 19:13:06 +00001454VTableBuilder::AddMethod(const CXXMethodDecl *MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001455 ReturnAdjustment ReturnAdjustment) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001456 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1457 assert(ReturnAdjustment.isEmpty() &&
1458 "Destructor can't have return adjustment!");
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001459
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001460 // Add both the complete destructor and the deleting destructor.
Anders Carlsson94464812010-04-10 19:13:06 +00001461 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1462 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001463 } else {
1464 // Add the return adjustment if necessary.
1465 if (!ReturnAdjustment.isEmpty())
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001466 VTableThunks[Components.size()].Return = ReturnAdjustment;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001467
1468 // Add the function.
Anders Carlsson94464812010-04-10 19:13:06 +00001469 Components.push_back(VTableComponent::MakeFunction(MD));
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001470 }
1471}
1472
Anders Carlssonc784ba22010-02-27 06:38:03 +00001473/// OverridesIndirectMethodInBase - Return whether the given member function
1474/// overrides any methods in the set of given bases.
1475/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1476/// For example, if we have:
1477///
1478/// struct A { virtual void f(); }
1479/// struct B : A { virtual void f(); }
1480/// struct C : B { virtual void f(); }
1481///
1482/// OverridesIndirectMethodInBase will return true if given C::f as the method
Anders Carlssonf622b452010-04-10 20:39:29 +00001483/// and { A } as the set of bases.
Anders Carlssonc784ba22010-02-27 06:38:03 +00001484static bool
1485OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001486 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonfbf05612010-04-17 17:24:33 +00001487 if (Bases.count(MD->getParent()))
1488 return true;
1489
Anders Carlssonc784ba22010-02-27 06:38:03 +00001490 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1491 E = MD->end_overridden_methods(); I != E; ++I) {
1492 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonc784ba22010-02-27 06:38:03 +00001493
1494 // Check "indirect overriders".
1495 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1496 return true;
1497 }
1498
1499 return false;
1500}
1501
Anders Carlsson464c8f12010-02-23 04:26:39 +00001502bool
Anders Carlsson94464812010-04-10 19:13:06 +00001503VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
Ken Dyck413e7d22011-04-02 00:44:58 +00001504 CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001505 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001506 CharUnits FirstBaseOffsetInLayoutClass) const {
Anders Carlsson464c8f12010-02-23 04:26:39 +00001507 // If the base and the first base in the primary base chain have the same
1508 // offsets, then this overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001509 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001510 return true;
1511
1512 // We know now that Base (or a direct or indirect base of it) is a primary
1513 // base in part of the class hierarchy, but not a primary base in the most
1514 // derived class.
1515
1516 // If the overrider is the first base in the primary base chain, we know
1517 // that the overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001518 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
Anders Carlsson464c8f12010-02-23 04:26:39 +00001519 return true;
1520
Anders Carlsson94464812010-04-10 19:13:06 +00001521 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001522
Anders Carlsson60396612010-03-10 06:51:42 +00001523 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001524 PrimaryBases.insert(RD);
1525
1526 // Now traverse the base chain, starting with the first base, until we find
1527 // the base that is no longer a primary base.
1528 while (true) {
1529 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1530 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1531
1532 if (!PrimaryBase)
1533 break;
1534
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001535 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001536 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001537 "Primary base should always be at offset 0!");
1538
Anders Carlssondad0f992010-02-28 18:37:33 +00001539 const ASTRecordLayout &LayoutClassLayout =
1540 Context.getASTRecordLayout(LayoutClass);
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001541
1542 // Now check if this is the primary base that is not a primary base in the
1543 // most derived class.
Ken Dyck413e7d22011-04-02 00:44:58 +00001544 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
Anders Carlsson60396612010-03-10 06:51:42 +00001545 FirstBaseOffsetInLayoutClass) {
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001546 // We found it, stop walking the chain.
1547 break;
1548 }
1549 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001550 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001551 "Primary base should always be at offset 0!");
1552 }
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001553
Anders Carlssonf89bb6c2010-02-27 19:51:04 +00001554 if (!PrimaryBases.insert(PrimaryBase))
1555 assert(false && "Found a duplicate primary base!");
1556
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001557 RD = PrimaryBase;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001558 }
1559
1560 // If the final overrider is an override of one of the primary bases,
1561 // then we know that it will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001562 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
Anders Carlsson464c8f12010-02-23 04:26:39 +00001563}
1564
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001565/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1566/// from the nearest base. Returns null if no method was found.
1567static const CXXMethodDecl *
1568FindNearestOverriddenMethod(const CXXMethodDecl *MD,
Anders Carlsson94464812010-04-10 19:13:06 +00001569 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonf622b452010-04-10 20:39:29 +00001570 OverriddenMethodsSetTy OverriddenMethods;
1571 ComputeAllOverriddenMethods(MD, OverriddenMethods);
1572
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001573 for (int I = Bases.size(), E = 0; I != E; --I) {
1574 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1575
1576 // Now check the overriden methods.
Anders Carlssonf622b452010-04-10 20:39:29 +00001577 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1578 E = OverriddenMethods.end(); I != E; ++I) {
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001579 const CXXMethodDecl *OverriddenMD = *I;
Anders Carlssonf622b452010-04-10 20:39:29 +00001580
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001581 // We found our overridden method.
1582 if (OverriddenMD->getParent() == PrimaryBase)
1583 return OverriddenMD;
1584 }
1585 }
1586
1587 return 0;
1588}
1589
Anders Carlsson60396612010-03-10 06:51:42 +00001590void
Ken Dyck3a726802011-03-27 19:15:11 +00001591VTableBuilder::AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001592 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
Ken Dyck3a726802011-03-27 19:15:11 +00001593 CharUnits FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001594 PrimaryBasesSetVectorTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +00001595 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001596 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1597
1598 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Ken Dyck57ebe332011-03-26 00:51:24 +00001599 CharUnits PrimaryBaseOffset;
Ken Dyck3a726802011-03-27 19:15:11 +00001600 CharUnits PrimaryBaseOffsetInLayoutClass;
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001601 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssona14f5972010-10-31 23:22:37 +00001602 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlssonaf280c02010-02-23 03:48:14 +00001603 "Primary vbase should have a zero offset!");
1604
1605 const ASTRecordLayout &MostDerivedClassLayout =
1606 Context.getASTRecordLayout(MostDerivedClass);
1607
Anders Carlsson60396612010-03-10 06:51:42 +00001608 PrimaryBaseOffset =
Ken Dyck57ebe332011-03-26 00:51:24 +00001609 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlsson60396612010-03-10 06:51:42 +00001610
1611 const ASTRecordLayout &LayoutClassLayout =
1612 Context.getASTRecordLayout(LayoutClass);
1613
1614 PrimaryBaseOffsetInLayoutClass =
Ken Dyck3a726802011-03-27 19:15:11 +00001615 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssonaf280c02010-02-23 03:48:14 +00001616 } else {
Anders Carlssona14f5972010-10-31 23:22:37 +00001617 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
Anders Carlsson57071e22010-02-12 05:25:12 +00001618 "Primary base should have a zero offset!");
Anders Carlssonaf280c02010-02-23 03:48:14 +00001619
Ken Dyck57ebe332011-03-26 00:51:24 +00001620 PrimaryBaseOffset = Base.getBaseOffset();
Anders Carlsson60396612010-03-10 06:51:42 +00001621 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
Anders Carlssonaf280c02010-02-23 03:48:14 +00001622 }
Anders Carlssondad0f992010-02-28 18:37:33 +00001623
Ken Dyck57ebe332011-03-26 00:51:24 +00001624 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
Anders Carlsson60396612010-03-10 06:51:42 +00001625 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1626 FirstBaseOffsetInLayoutClass, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001627
1628 if (!PrimaryBases.insert(PrimaryBase))
1629 assert(false && "Found a duplicate primary base!");
1630 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001631
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001632 // Now go through all virtual member functions and add them.
1633 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1634 E = RD->method_end(); I != E; ++I) {
1635 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001636
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001637 if (!MD->isVirtual())
1638 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +00001639
1640 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +00001641 FinalOverriders::OverriderInfo Overrider =
Ken Dycke6203132011-03-26 01:18:17 +00001642 Overriders.getOverrider(MD, Base.getBaseOffset());
Anders Carlsson57071e22010-02-12 05:25:12 +00001643
1644 // Check if this virtual member function overrides a method in a primary
1645 // base. If this is the case, and the return type doesn't require adjustment
1646 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001647 if (const CXXMethodDecl *OverriddenMD =
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001648 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlssondf31af12010-02-13 23:17:31 +00001649 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001650 OverriddenMD).isEmpty()) {
1651 // Replace the method info of the overridden method with our own
1652 // method.
1653 assert(MethodInfoMap.count(OverriddenMD) &&
1654 "Did not find the overridden method!");
1655 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1656
Ken Dyck3a726802011-03-27 19:15:11 +00001657 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1658 OverriddenMethodInfo.VTableIndex);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001659
1660 assert(!MethodInfoMap.count(MD) &&
1661 "Should not have method info for this method yet!");
1662
1663 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1664 MethodInfoMap.erase(OverriddenMD);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001665
1666 // If the overridden method exists in a virtual base class or a direct
1667 // or indirect base class of a virtual base class, we need to emit a
1668 // thunk if we ever have a class hierarchy where the base class is not
1669 // a primary base in the complete object.
Anders Carlsson718d0692010-04-11 22:07:06 +00001670 if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
Anders Carlssonbca5d372010-03-22 20:06:40 +00001671 // Compute the this adjustment.
1672 ThisAdjustment ThisAdjustment =
Ken Dyck3a726802011-03-27 19:15:11 +00001673 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1674 Overrider);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001675
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001676 if (ThisAdjustment.VCallOffsetOffset &&
1677 Overrider.Method->getParent() == MostDerivedClass) {
John McCall4e3b17c2010-11-09 01:18:05 +00001678
1679 // There's no return adjustment from OverriddenMD and MD,
1680 // but that doesn't mean there isn't one between MD and
1681 // the final overrider.
1682 BaseOffset ReturnAdjustmentOffset =
1683 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1684 ReturnAdjustment ReturnAdjustment =
1685 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1686
Anders Carlssonacfa6ae2010-03-29 02:53:58 +00001687 // This is a virtual thunk for the most derived class, add it.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001688 AddThunk(Overrider.Method,
John McCall4e3b17c2010-11-09 01:18:05 +00001689 ThunkInfo(ThisAdjustment, ReturnAdjustment));
Anders Carlssonbca5d372010-03-22 20:06:40 +00001690 }
1691 }
1692
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001693 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001694 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001695 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00001696
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001697 // Insert the method info for this method.
Ken Dyck3a726802011-03-27 19:15:11 +00001698 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
Ken Dyck82abeae2011-03-27 18:57:53 +00001699 Components.size());
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001700
1701 assert(!MethodInfoMap.count(MD) &&
1702 "Should not have method info for this method yet!");
1703 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001704
1705 // Check if this overrider is going to be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001706 const CXXMethodDecl *OverriderMD = Overrider.Method;
Ken Dyck413e7d22011-04-02 00:44:58 +00001707 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001708 FirstBaseInPrimaryBaseChain,
Ken Dyck413e7d22011-04-02 00:44:58 +00001709 FirstBaseOffsetInLayoutClass)) {
Anders Carlsson94464812010-04-10 19:13:06 +00001710 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001711 continue;
1712 }
Anders Carlsson464c8f12010-02-23 04:26:39 +00001713
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001714 // Check if this overrider needs a return adjustment.
Anders Carlsson7097e752010-06-01 23:47:05 +00001715 // We don't want to do this for pure virtual member functions.
1716 BaseOffset ReturnAdjustmentOffset;
1717 if (!OverriderMD->isPure()) {
1718 ReturnAdjustmentOffset =
1719 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1720 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001721
1722 ReturnAdjustment ReturnAdjustment =
1723 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001724
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001725 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001726 }
1727}
1728
Anders Carlsson718d0692010-04-11 22:07:06 +00001729void VTableBuilder::LayoutVTable() {
Ken Dyck4230d522011-03-24 01:21:01 +00001730 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1731 CharUnits::Zero()),
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001732 /*BaseIsMorallyVirtual=*/false,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001733 MostDerivedClassIsVirtual,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001734 MostDerivedClassOffset);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001735
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001736 VisitedVirtualBasesSetTy VBases;
Anders Carlsson08c26752010-02-27 04:05:52 +00001737
1738 // Determine the primary virtual bases.
Ken Dyck8fe59932011-03-30 01:10:23 +00001739 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1740 VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001741 VBases.clear();
1742
Anders Carlsson718d0692010-04-11 22:07:06 +00001743 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001744}
1745
Anders Carlsson530c40c2010-02-28 01:43:58 +00001746void
Anders Carlsson718d0692010-04-11 22:07:06 +00001747VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
Anders Carlsson73e6fa02010-04-11 22:20:36 +00001748 bool BaseIsMorallyVirtual,
Anders Carlsson91232842010-04-11 22:03:57 +00001749 bool BaseIsVirtualInLayoutClass,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001750 CharUnits OffsetInLayoutClass) {
Chris Lattner04a23112010-02-17 21:42:34 +00001751 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
Anders Carlssonb828afa2010-02-14 17:05:59 +00001752
Anders Carlsson852213e2010-02-16 04:59:55 +00001753 // Add vcall and vbase offsets for this vtable.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001754 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
Anders Carlsson91232842010-04-11 22:03:57 +00001755 Base, BaseIsVirtualInLayoutClass,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00001756 OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001757 Components.append(Builder.components_begin(), Builder.components_end());
1758
Anders Carlssondba6ef82010-02-25 22:23:13 +00001759 // Check if we need to add these vcall offsets.
Anders Carlsson91232842010-04-11 22:03:57 +00001760 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
Anders Carlssondba6ef82010-02-25 22:23:13 +00001761 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1762
1763 if (VCallOffsets.empty())
1764 VCallOffsets = Builder.getVCallOffsets();
1765 }
1766
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001767 // If we're laying out the most derived class we want to keep track of the
1768 // virtual base class offset offsets.
1769 if (Base.getBase() == MostDerivedClass)
1770 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1771
Anders Carlsson852213e2010-02-16 04:59:55 +00001772 // Add the offset to top.
Ken Dyck7ba8f522011-03-27 20:33:38 +00001773 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1774 Components.push_back(
Ken Dyckc40a3fd2011-04-02 01:14:48 +00001775 VTableComponent::MakeOffsetToTop(OffsetToTop));
Anders Carlsson57071e22010-02-12 05:25:12 +00001776
1777 // Next, add the RTTI.
Anders Carlsson94464812010-04-10 19:13:06 +00001778 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Anders Carlsson57071e22010-02-12 05:25:12 +00001779
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001780 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +00001781
1782 // Now go through all virtual member functions and add them.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001783 PrimaryBasesSetVectorTy PrimaryBases;
Ken Dyck7ba8f522011-03-27 20:33:38 +00001784 AddMethods(Base, OffsetInLayoutClass,
1785 Base.getBase(), OffsetInLayoutClass,
Anders Carlsson60396612010-03-10 06:51:42 +00001786 PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001787
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001788 // Compute 'this' pointer adjustments.
1789 ComputeThisAdjustments();
1790
Anders Carlsson014a3582010-03-25 21:45:14 +00001791 // Add all address points.
1792 const CXXRecordDecl *RD = Base.getBase();
1793 while (true) {
Ken Dyck4230d522011-03-24 01:21:01 +00001794 AddressPoints.insert(std::make_pair(
Ken Dyck7ba8f522011-03-27 20:33:38 +00001795 BaseSubobject(RD, OffsetInLayoutClass),
Ken Dyck4230d522011-03-24 01:21:01 +00001796 AddressPoint));
Anders Carlsson014a3582010-03-25 21:45:14 +00001797
1798 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1799 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001800
Anders Carlsson014a3582010-03-25 21:45:14 +00001801 if (!PrimaryBase)
1802 break;
1803
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001804 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson014a3582010-03-25 21:45:14 +00001805 // Check if this virtual primary base is a primary base in the layout
1806 // class. If it's not, we don't want to add it.
1807 const ASTRecordLayout &LayoutClassLayout =
1808 Context.getASTRecordLayout(LayoutClass);
1809
Ken Dyck7ba8f522011-03-27 20:33:38 +00001810 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
Anders Carlsson014a3582010-03-25 21:45:14 +00001811 OffsetInLayoutClass) {
1812 // We don't want to add this class (or any of its primary bases).
1813 break;
1814 }
1815 }
1816
1817 RD = PrimaryBase;
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001818 }
1819
Anders Carlsson5edcc372010-02-24 16:43:12 +00001820 // Layout secondary vtables.
Ken Dyck75762b32011-03-30 00:50:28 +00001821 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001822}
1823
Anders Carlsson718d0692010-04-11 22:07:06 +00001824void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
Anders Carlssona96a2e92010-03-10 19:39:11 +00001825 bool BaseIsMorallyVirtual,
Ken Dyck75762b32011-03-30 00:50:28 +00001826 CharUnits OffsetInLayoutClass) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001827 // Itanium C++ ABI 2.5.2:
1828 // Following the primary virtual table of a derived class are secondary
1829 // virtual tables for each of its proper base classes, except any primary
1830 // base(s) with which it shares its primary virtual table.
1831
1832 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001833 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1834 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc7b63162010-02-16 16:02:57 +00001835
Anders Carlsson57071e22010-02-12 05:25:12 +00001836 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1837 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001838 // Ignore virtual bases, we'll emit them later.
1839 if (I->isVirtual())
1840 continue;
1841
Anders Carlsson57071e22010-02-12 05:25:12 +00001842 const CXXRecordDecl *BaseDecl =
1843 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb828afa2010-02-14 17:05:59 +00001844
Anders Carlssona661a542010-02-16 04:49:44 +00001845 // Ignore bases that don't have a vtable.
1846 if (!BaseDecl->isDynamicClass())
1847 continue;
Anders Carlssonb828afa2010-02-14 17:05:59 +00001848
Anders Carlsson718d0692010-04-11 22:07:06 +00001849 if (isBuildingConstructorVTable()) {
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001850 // Itanium C++ ABI 2.6.4:
1851 // Some of the base class subobjects may not need construction virtual
1852 // tables, which will therefore not be present in the construction
1853 // virtual table group, even though the subobject virtual tables are
1854 // present in the main virtual table group for the complete object.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001855 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001856 continue;
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001857 }
1858
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001859 // Get the base offset of this base.
Ken Dyck57ebe332011-03-26 00:51:24 +00001860 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1861 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
Anders Carlsson530c40c2010-02-28 01:43:58 +00001862
Ken Dyck57ebe332011-03-26 00:51:24 +00001863 CharUnits BaseOffsetInLayoutClass =
Ken Dyck75762b32011-03-30 00:50:28 +00001864 OffsetInLayoutClass + RelativeBaseOffset;
Anders Carlssonc7b63162010-02-16 16:02:57 +00001865
1866 // Don't emit a secondary vtable for a primary base. We might however want
1867 // to emit secondary vtables for other bases of this base.
1868 if (BaseDecl == PrimaryBase) {
Ken Dyck75762b32011-03-30 00:50:28 +00001869 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1870 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001871 continue;
1872 }
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001873
Anders Carlsson89ad4212010-02-16 16:26:28 +00001874 // Layout the primary vtable (and any secondary vtables) for this base.
Ken Dyck4230d522011-03-24 01:21:01 +00001875 LayoutPrimaryAndSecondaryVTables(
Ken Dyck57ebe332011-03-26 00:51:24 +00001876 BaseSubobject(BaseDecl, BaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +00001877 BaseIsMorallyVirtual,
1878 /*BaseIsVirtualInLayoutClass=*/false,
Ken Dyck7ba8f522011-03-27 20:33:38 +00001879 BaseOffsetInLayoutClass);
Anders Carlsson57071e22010-02-12 05:25:12 +00001880 }
1881}
1882
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001883void
Anders Carlsson94464812010-04-10 19:13:06 +00001884VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Ken Dyck8fe59932011-03-30 01:10:23 +00001885 CharUnits OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001886 VisitedVirtualBasesSetTy &VBases) {
1887 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1888
1889 // Check if this base has a primary base.
1890 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001891
1892 // Check if it's virtual.
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001893 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001894 bool IsPrimaryVirtualBase = true;
1895
Anders Carlsson718d0692010-04-11 22:07:06 +00001896 if (isBuildingConstructorVTable()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00001897 // Check if the base is actually a primary base in the class we use for
1898 // layout.
Anders Carlssond7fdae52010-03-10 03:02:01 +00001899 const ASTRecordLayout &LayoutClassLayout =
1900 Context.getASTRecordLayout(LayoutClass);
1901
Ken Dyck8fe59932011-03-30 01:10:23 +00001902 CharUnits PrimaryBaseOffsetInLayoutClass =
1903 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001904
1905 // We know that the base is not a primary base in the layout class if
1906 // the base offsets are different.
1907 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
Anders Carlsson0378bf02010-02-28 18:08:38 +00001908 IsPrimaryVirtualBase = false;
1909 }
1910
1911 if (IsPrimaryVirtualBase)
1912 PrimaryVirtualBases.insert(PrimaryBase);
1913 }
Anders Carlsson08c26752010-02-27 04:05:52 +00001914 }
1915
1916 // Traverse bases, looking for more primary virtual bases.
1917 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1918 E = RD->bases_end(); I != E; ++I) {
1919 const CXXRecordDecl *BaseDecl =
1920 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1921
Ken Dyck8fe59932011-03-30 01:10:23 +00001922 CharUnits BaseOffsetInLayoutClass;
Anders Carlssond7fdae52010-03-10 03:02:01 +00001923
1924 if (I->isVirtual()) {
1925 if (!VBases.insert(BaseDecl))
1926 continue;
1927
1928 const ASTRecordLayout &LayoutClassLayout =
1929 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001930
Anders Carlssona14f5972010-10-31 23:22:37 +00001931 BaseOffsetInLayoutClass =
Ken Dyck8fe59932011-03-30 01:10:23 +00001932 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001933 } else {
1934 BaseOffsetInLayoutClass =
Ken Dyck8fe59932011-03-30 01:10:23 +00001935 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssond7fdae52010-03-10 03:02:01 +00001936 }
1937
1938 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001939 }
1940}
1941
1942void
Anders Carlsson718d0692010-04-11 22:07:06 +00001943VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001944 VisitedVirtualBasesSetTy &VBases) {
1945 // Itanium C++ ABI 2.5.2:
1946 // Then come the virtual base virtual tables, also in inheritance graph
1947 // order, and again excluding primary bases (which share virtual tables with
1948 // the classes for which they are primary).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001949 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1950 E = RD->bases_end(); I != E; ++I) {
1951 const CXXRecordDecl *BaseDecl =
1952 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1953
Anders Carlssonaf280c02010-02-23 03:48:14 +00001954 // Check if this base needs a vtable. (If it's virtual, not a primary base
1955 // of some other class, and we haven't visited it before).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001956 if (I->isVirtual() && BaseDecl->isDynamicClass() &&
Anders Carlsson08c26752010-02-27 04:05:52 +00001957 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001958 const ASTRecordLayout &MostDerivedClassLayout =
1959 Context.getASTRecordLayout(MostDerivedClass);
Ken Dyck7262e902011-03-30 01:14:22 +00001960 CharUnits BaseOffset =
1961 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001962
Anders Carlsson530c40c2010-02-28 01:43:58 +00001963 const ASTRecordLayout &LayoutClassLayout =
1964 Context.getASTRecordLayout(LayoutClass);
Ken Dyck7ba8f522011-03-27 20:33:38 +00001965 CharUnits BaseOffsetInLayoutClass =
1966 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson530c40c2010-02-28 01:43:58 +00001967
Ken Dyck4230d522011-03-24 01:21:01 +00001968 LayoutPrimaryAndSecondaryVTables(
Ken Dyck7262e902011-03-30 01:14:22 +00001969 BaseSubobject(BaseDecl, BaseOffset),
Ken Dyck4230d522011-03-24 01:21:01 +00001970 /*BaseIsMorallyVirtual=*/true,
1971 /*BaseIsVirtualInLayoutClass=*/true,
1972 BaseOffsetInLayoutClass);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001973 }
1974
1975 // We only need to check the base for virtual base vtables if it actually
1976 // has virtual bases.
1977 if (BaseDecl->getNumVBases())
Anders Carlsson718d0692010-04-11 22:07:06 +00001978 LayoutVTablesForVirtualBases(BaseDecl, VBases);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001979 }
1980}
1981
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001982/// dumpLayout - Dump the vtable layout.
Anders Carlsson94464812010-04-10 19:13:06 +00001983void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001984
Anders Carlsson718d0692010-04-11 22:07:06 +00001985 if (isBuildingConstructorVTable()) {
Anders Carlssond03a3262010-02-27 20:39:05 +00001986 Out << "Construction vtable for ('";
1987 Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
Ken Dyck23da84a2011-03-27 20:20:28 +00001988 Out << MostDerivedClassOffset.getQuantity() << ") in '";
Anders Carlssond03a3262010-02-27 20:39:05 +00001989 Out << LayoutClass->getQualifiedNameAsString();
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001990 } else {
1991 Out << "Vtable for '";
1992 Out << MostDerivedClass->getQualifiedNameAsString();
Anders Carlssond03a3262010-02-27 20:39:05 +00001993 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001994 Out << "' (" << Components.size() << " entries).\n";
1995
Anders Carlsson822307b2010-02-11 17:18:51 +00001996 // Iterate through the address points and insert them into a new map where
1997 // they are keyed by the index and not the base object.
1998 // Since an address point can be shared by multiple subobjects, we use an
1999 // STL multimap.
2000 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
Anders Carlssonff143f82010-03-25 00:35:49 +00002001 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
2002 E = AddressPoints.end(); I != E; ++I) {
Anders Carlsson822307b2010-02-11 17:18:51 +00002003 const BaseSubobject& Base = I->first;
2004 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002005
Anders Carlsson822307b2010-02-11 17:18:51 +00002006 AddressPointsByIndex.insert(std::make_pair(Index, Base));
2007 }
2008
2009 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00002010 uint64_t Index = I;
Anders Carlsson822307b2010-02-11 17:18:51 +00002011
2012 Out << llvm::format("%4d | ", I);
2013
Anders Carlsson94464812010-04-10 19:13:06 +00002014 const VTableComponent &Component = Components[I];
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002015
2016 // Dump the component.
2017 switch (Component.getKind()) {
Anders Carlssond9a67042010-02-17 06:07:19 +00002018
Anders Carlsson94464812010-04-10 19:13:06 +00002019 case VTableComponent::CK_VCallOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002020 Out << "vcall_offset ("
2021 << Component.getVCallOffset().getQuantity()
2022 << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002023 break;
Anders Carlsson852213e2010-02-16 04:59:55 +00002024
Anders Carlsson94464812010-04-10 19:13:06 +00002025 case VTableComponent::CK_VBaseOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002026 Out << "vbase_offset ("
2027 << Component.getVBaseOffset().getQuantity()
2028 << ")";
Anders Carlsson852213e2010-02-16 04:59:55 +00002029 break;
2030
Anders Carlsson94464812010-04-10 19:13:06 +00002031 case VTableComponent::CK_OffsetToTop:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002032 Out << "offset_to_top ("
2033 << Component.getOffsetToTop().getQuantity()
2034 << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002035 break;
2036
Anders Carlsson94464812010-04-10 19:13:06 +00002037 case VTableComponent::CK_RTTI:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002038 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2039 break;
2040
Anders Carlsson94464812010-04-10 19:13:06 +00002041 case VTableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002042 const CXXMethodDecl *MD = Component.getFunctionDecl();
2043
Anders Carlsson848fa642010-02-11 18:20:28 +00002044 std::string Str =
2045 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2046 MD);
2047 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +00002048 if (MD->isPure())
2049 Out << " [pure]";
2050
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002051 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002052 if (!Thunk.isEmpty()) {
2053 // If this function pointer has a return adjustment, dump it.
2054 if (!Thunk.Return.isEmpty()) {
2055 Out << "\n [return adjustment: ";
2056 Out << Thunk.Return.NonVirtual << " non-virtual";
2057
2058 if (Thunk.Return.VBaseOffsetOffset) {
2059 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2060 Out << " vbase offset offset";
2061 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002062
Anders Carlssondd366812010-03-12 04:54:20 +00002063 Out << ']';
2064 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002065
Anders Carlssondd366812010-03-12 04:54:20 +00002066 // If this function pointer has a 'this' pointer adjustment, dump it.
2067 if (!Thunk.This.isEmpty()) {
2068 Out << "\n [this adjustment: ";
2069 Out << Thunk.This.NonVirtual << " non-virtual";
2070
2071 if (Thunk.This.VCallOffsetOffset) {
2072 Out << ", " << Thunk.This.VCallOffsetOffset;
2073 Out << " vcall offset offset";
2074 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002075
Anders Carlssondd366812010-03-12 04:54:20 +00002076 Out << ']';
2077 }
Anders Carlsson49bac9a2010-02-13 23:40:17 +00002078 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002079
Anders Carlsson848fa642010-02-11 18:20:28 +00002080 break;
2081 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002082
Anders Carlsson94464812010-04-10 19:13:06 +00002083 case VTableComponent::CK_CompleteDtorPointer:
2084 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6065e022010-02-19 06:03:53 +00002085 bool IsComplete =
Anders Carlsson94464812010-04-10 19:13:06 +00002086 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
Anders Carlsson6065e022010-02-19 06:03:53 +00002087
Anders Carlsson848fa642010-02-11 18:20:28 +00002088 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2089
Anders Carlsson6065e022010-02-19 06:03:53 +00002090 Out << DD->getQualifiedNameAsString();
2091 if (IsComplete)
2092 Out << "() [complete]";
2093 else
2094 Out << "() [deleting]";
2095
Anders Carlsson98241422010-02-12 02:38:13 +00002096 if (DD->isPure())
2097 Out << " [pure]";
2098
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002099 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002100 if (!Thunk.isEmpty()) {
2101 // If this destructor has a 'this' pointer adjustment, dump it.
2102 if (!Thunk.This.isEmpty()) {
2103 Out << "\n [this adjustment: ";
2104 Out << Thunk.This.NonVirtual << " non-virtual";
2105
2106 if (Thunk.This.VCallOffsetOffset) {
2107 Out << ", " << Thunk.This.VCallOffsetOffset;
2108 Out << " vcall offset offset";
2109 }
2110
2111 Out << ']';
2112 }
2113 }
Anders Carlsson6065e022010-02-19 06:03:53 +00002114
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002115 break;
2116 }
2117
Anders Carlsson94464812010-04-10 19:13:06 +00002118 case VTableComponent::CK_UnusedFunctionPointer: {
Anders Carlsson1f464662010-02-19 20:08:13 +00002119 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2120
2121 std::string Str =
2122 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2123 MD);
2124 Out << "[unused] " << Str;
2125 if (MD->isPure())
2126 Out << " [pure]";
2127 }
2128
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002129 }
Anders Carlsson822307b2010-02-11 17:18:51 +00002130
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002131 Out << '\n';
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002132
2133 // Dump the next address point.
2134 uint64_t NextIndex = Index + 1;
2135 if (AddressPointsByIndex.count(NextIndex)) {
2136 if (AddressPointsByIndex.count(NextIndex) == 1) {
2137 const BaseSubobject &Base =
2138 AddressPointsByIndex.find(NextIndex)->second;
2139
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002140 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
Ken Dyck4230d522011-03-24 01:21:01 +00002141 Out << ", " << Base.getBaseOffset().getQuantity();
2142 Out << ") vtable address --\n";
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002143 } else {
Ken Dyck57ebe332011-03-26 00:51:24 +00002144 CharUnits BaseOffset =
2145 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002146
2147 // We store the class names in a set to get a stable order.
2148 std::set<std::string> ClassNames;
2149 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2150 AddressPointsByIndex.lower_bound(NextIndex), E =
2151 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
Ken Dyck57ebe332011-03-26 00:51:24 +00002152 assert(I->second.getBaseOffset() == BaseOffset &&
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002153 "Invalid base offset!");
2154 const CXXRecordDecl *RD = I->second.getBase();
2155 ClassNames.insert(RD->getQualifiedNameAsString());
2156 }
2157
2158 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2159 E = ClassNames.end(); I != E; ++I) {
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002160 Out << " -- (" << *I;
Ken Dyck57ebe332011-03-26 00:51:24 +00002161 Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002162 }
2163 }
2164 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002165 }
Anders Carlssond03a3262010-02-27 20:39:05 +00002166
2167 Out << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002168
Anders Carlsson718d0692010-04-11 22:07:06 +00002169 if (isBuildingConstructorVTable())
Anders Carlsson9135a842010-03-18 02:44:19 +00002170 return;
2171
2172 if (MostDerivedClass->getNumVBases()) {
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002173 // We store the virtual base class names and their offsets in a map to get
2174 // a stable order.
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002175
Ken Dyck3015c4c2011-04-07 00:59:42 +00002176 std::map<std::string, CharUnits> ClassNamesAndOffsets;
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002177 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2178 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2179 std::string ClassName = I->first->getQualifiedNameAsString();
Ken Dyckd28c33c2011-04-07 00:55:01 +00002180 CharUnits OffsetOffset = I->second;
2181 ClassNamesAndOffsets.insert(
Ken Dyck3015c4c2011-04-07 00:59:42 +00002182 std::make_pair(ClassName, OffsetOffset));
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002183 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002184
2185 Out << "Virtual base offset offsets for '";
2186 Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2187 Out << ClassNamesAndOffsets.size();
2188 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002189
Ken Dyck3015c4c2011-04-07 00:59:42 +00002190 for (std::map<std::string, CharUnits>::const_iterator I =
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002191 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2192 I != E; ++I)
Ken Dyck3015c4c2011-04-07 00:59:42 +00002193 Out << " " << I->first << " | " << I->second.getQuantity() << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002194
2195 Out << "\n";
2196 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002197
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002198 if (!Thunks.empty()) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002199 // We store the method names in a map to get a stable order.
2200 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2201
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002202 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002203 I != E; ++I) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002204 const CXXMethodDecl *MD = I->first;
2205 std::string MethodName =
2206 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2207 MD);
2208
2209 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2210 }
2211
2212 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2213 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2214 I != E; ++I) {
2215 const std::string &MethodName = I->first;
2216 const CXXMethodDecl *MD = I->second;
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002217
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002218 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002219 std::sort(ThunksVector.begin(), ThunksVector.end());
Anders Carlsson9135a842010-03-18 02:44:19 +00002220
2221 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2222 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2223
2224 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2225 const ThunkInfo &Thunk = ThunksVector[I];
2226
2227 Out << llvm::format("%4d | ", I);
2228
Anders Carlsson09daa502010-03-22 16:10:59 +00002229 // If this function pointer has a return pointer adjustment, dump it.
2230 if (!Thunk.Return.isEmpty()) {
2231 Out << "return adjustment: " << Thunk.This.NonVirtual;
2232 Out << " non-virtual";
2233 if (Thunk.Return.VBaseOffsetOffset) {
2234 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2235 Out << " vbase offset offset";
2236 }
2237
2238 if (!Thunk.This.isEmpty())
2239 Out << "\n ";
2240 }
2241
Anders Carlsson9135a842010-03-18 02:44:19 +00002242 // If this function pointer has a 'this' pointer adjustment, dump it.
2243 if (!Thunk.This.isEmpty()) {
Anders Carlsson09daa502010-03-22 16:10:59 +00002244 Out << "this adjustment: ";
2245 Out << Thunk.This.NonVirtual << " non-virtual";
Anders Carlsson9135a842010-03-18 02:44:19 +00002246
2247 if (Thunk.This.VCallOffsetOffset) {
2248 Out << ", " << Thunk.This.VCallOffsetOffset;
Anders Carlsson09daa502010-03-22 16:10:59 +00002249 Out << " vcall offset offset";
Anders Carlsson9135a842010-03-18 02:44:19 +00002250 }
2251 }
2252
2253 Out << '\n';
2254 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00002255
2256 Out << '\n';
2257
Anders Carlsson9135a842010-03-18 02:44:19 +00002258 }
2259 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002260}
2261
2262}
2263
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002264static void
2265CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
2266 VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) {
2267 while (RD) {
2268 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2269 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2270 if (PrimaryBase)
2271 PrimaryBases.insert(PrimaryBase);
2272
2273 RD = PrimaryBase;
2274 }
2275}
2276
Anders Carlsson046c2942010-04-17 20:15:18 +00002277void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002278
2279 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00002280 // The order of the virtual function pointers in a virtual table is the
2281 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002282 //
Anders Carlsson45147d02010-02-02 03:37:46 +00002283 // There is an entry for any virtual function declared in a class,
2284 // whether it is a new function or overrides a base class function,
2285 // unless it overrides a function from the primary base, and conversion
2286 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002287
2288 int64_t CurrentIndex = 0;
2289
2290 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2291 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2292
2293 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00002294 assert(PrimaryBase->isDefinition() &&
2295 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002296
2297 // Since the record decl shares its vtable pointer with the primary base
2298 // we need to start counting at the end of the primary base's vtable.
2299 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2300 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00002301
2302 // Collect all the primary bases, so we can check whether methods override
2303 // a method from the base.
Anders Carlsson94464812010-04-10 19:13:06 +00002304 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonbdb4a9d2010-11-24 23:04:03 +00002305 CollectPrimaryBases(RD, CGM.getContext(), PrimaryBases);
Eli Friedmanea5ae312009-12-15 03:31:17 +00002306
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002307 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2308
2309 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2310 e = RD->method_end(); i != e; ++i) {
2311 const CXXMethodDecl *MD = *i;
2312
2313 // We only want virtual methods.
2314 if (!MD->isVirtual())
2315 continue;
2316
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002317 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002318 if (const CXXMethodDecl *OverriddenMD =
Anders Carlssonf622b452010-04-10 20:39:29 +00002319 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002320 // Check if converting from the return type of the method to the
2321 // return type of the overridden method requires conversion.
Anders Carlssondf31af12010-02-13 23:17:31 +00002322 if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
2323 OverriddenMD).isEmpty()) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002324 // This index is shared between the index in the vtable of the primary
2325 // base class.
2326 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2327 const CXXDestructorDecl *OverriddenDD =
2328 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002329
Anders Carlsson57071e22010-02-12 05:25:12 +00002330 // Add both the complete and deleting entries.
Anders Carlsson046c2942010-04-17 20:15:18 +00002331 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] =
2332 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2333 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2334 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
Anders Carlsson57071e22010-02-12 05:25:12 +00002335 } else {
Anders Carlsson046c2942010-04-17 20:15:18 +00002336 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
Anders Carlsson57071e22010-02-12 05:25:12 +00002337 }
2338
2339 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00002340 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002341 }
2342 }
2343
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002344 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2345 if (MD->isImplicit()) {
2346 assert(!ImplicitVirtualDtor &&
2347 "Did already see an implicit virtual dtor!");
2348 ImplicitVirtualDtor = DD;
2349 continue;
2350 }
Tilmann Schellerca0df922011-03-02 20:40:50 +00002351
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002352 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002353 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002354
2355 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002356 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002357 } else {
2358 // Add the entry.
Tilmann Schellerca0df922011-03-02 20:40:50 +00002359 MethodVTableIndices[MD] = CurrentIndex++;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002360 }
2361 }
2362
2363 if (ImplicitVirtualDtor) {
2364 // Itanium C++ ABI 2.5.2:
Anders Carlssona661a542010-02-16 04:49:44 +00002365 // If a class has an implicitly-defined virtual destructor,
2366 // its entries come after the declared virtual function pointers.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002367
2368 // Add the complete dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002369 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002370 CurrentIndex++;
2371
2372 // Add the deleting dtor.
Anders Carlsson046c2942010-04-17 20:15:18 +00002373 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002374 CurrentIndex++;
2375 }
2376
2377 NumVirtualFunctionPointers[RD] = CurrentIndex;
2378}
2379
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002380bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
2381 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
2382
2383 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2384 if (TSK == TSK_ExplicitInstantiationDeclaration)
2385 return false;
2386
2387 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
2388 if (!KeyFunction)
2389 return true;
2390
2391 // Itanium C++ ABI, 5.2.6 Instantiated Templates:
2392 // An instantiation of a class template requires:
2393 // - In the object where instantiated, the virtual table...
2394 if (TSK == TSK_ImplicitInstantiation ||
2395 TSK == TSK_ExplicitInstantiationDefinition)
2396 return true;
2397
Anders Carlsson6d7f8472011-01-30 20:45:54 +00002398 // If we're building with optimization, we always emit VTables since that
2399 // allows for virtual function calls to be devirtualized.
2400 // (We don't want to do this in -fapple-kext mode however).
2401 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOptions().AppleKext)
2402 return true;
2403
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002404 return KeyFunction->hasBody();
2405}
2406
Anders Carlssonaf440352010-03-23 04:11:45 +00002407uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002408 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2409 NumVirtualFunctionPointers.find(RD);
2410 if (I != NumVirtualFunctionPointers.end())
2411 return I->second;
2412
Anders Carlsson046c2942010-04-17 20:15:18 +00002413 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002414
2415 I = NumVirtualFunctionPointers.find(RD);
2416 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2417 return I->second;
2418}
2419
Anders Carlsson046c2942010-04-17 20:15:18 +00002420uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2421 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2422 if (I != MethodVTableIndices.end())
Anders Carlssondbd920c2009-10-11 22:13:54 +00002423 return I->second;
2424
Anders Carlssona0fdd912009-11-13 17:08:56 +00002425 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002426
Anders Carlsson046c2942010-04-17 20:15:18 +00002427 ComputeMethodVTableIndices(RD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002428
Anders Carlsson046c2942010-04-17 20:15:18 +00002429 I = MethodVTableIndices.find(GD);
2430 assert(I != MethodVTableIndices.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002431 return I->second;
2432}
2433
Devang Patel62c117d2011-04-04 20:36:06 +00002434int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2435 const CXXRecordDecl *VBase) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002436 ClassPairTy ClassPair(RD, VBase);
2437
Anders Carlssonbba16072010-03-11 07:15:17 +00002438 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2439 VirtualBaseClassOffsetOffsets.find(ClassPair);
2440 if (I != VirtualBaseClassOffsetOffsets.end())
Ken Dyck1a7f7522011-04-07 01:22:42 +00002441 return I->second.getQuantity();
Anders Carlssondbd920c2009-10-11 22:13:54 +00002442
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002443 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
Ken Dyck4230d522011-03-24 01:21:01 +00002444 BaseSubobject(RD, CharUnits::Zero()),
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002445 /*BaseIsVirtual=*/false,
Ken Dyck3ecfa2d2011-03-31 00:48:04 +00002446 /*OffsetInLayoutClass=*/CharUnits::Zero());
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00002447
2448 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2449 Builder.getVBaseOffsetOffsets().begin(),
2450 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002451 // Insert all types.
2452 ClassPairTy ClassPair(RD, I->first);
2453
Ken Dyckd28c33c2011-04-07 00:55:01 +00002454 VirtualBaseClassOffsetOffsets.insert(
Ken Dyck1a7f7522011-04-07 01:22:42 +00002455 std::make_pair(ClassPair, I->second));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002456 }
2457
Anders Carlssonbba16072010-03-11 07:15:17 +00002458 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
Anders Carlssonbba16072010-03-11 07:15:17 +00002459 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002460
Ken Dyck1a7f7522011-04-07 01:22:42 +00002461 return I->second.getQuantity();
Anders Carlssondbd920c2009-10-11 22:13:54 +00002462}
2463
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002464uint64_t
2465CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
Anders Carlssonec9c2022010-04-08 16:18:36 +00002466 assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2467 "Did not find address point!");
2468
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002469 uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002470 assert(AddressPoint && "Address point must not be zero!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00002471
Anders Carlsson64c9eca2010-03-29 02:08:26 +00002472 return AddressPoint;
2473}
2474
Anders Carlsson19879c92010-03-23 17:17:29 +00002475llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
Anders Carlsson84c49e42011-02-06 17:15:43 +00002476 const ThunkInfo &Thunk) {
Anders Carlsson19879c92010-03-23 17:17:29 +00002477 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2478
2479 // Compute the mangled name.
2480 llvm::SmallString<256> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002481 llvm::raw_svector_ostream Out(Name);
Anders Carlsson19879c92010-03-23 17:17:29 +00002482 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
John McCall4c40d982010-08-31 07:33:07 +00002483 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002484 Thunk.This, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +00002485 else
Rafael Espindolaf0be9792011-02-11 02:52:17 +00002486 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
2487 Out.flush();
2488
John McCall4c40d982010-08-31 07:33:07 +00002489 const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson84c49e42011-02-06 17:15:43 +00002490 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
Anders Carlsson19879c92010-03-23 17:17:29 +00002491}
2492
Anders Carlsson519c3282010-03-24 00:39:18 +00002493static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2494 llvm::Value *Ptr,
2495 int64_t NonVirtualAdjustment,
2496 int64_t VirtualAdjustment) {
2497 if (!NonVirtualAdjustment && !VirtualAdjustment)
2498 return Ptr;
2499
2500 const llvm::Type *Int8PtrTy =
2501 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2502
2503 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2504
2505 if (NonVirtualAdjustment) {
2506 // Do the non-virtual adjustment.
2507 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2508 }
2509
2510 if (VirtualAdjustment) {
2511 const llvm::Type *PtrDiffTy =
2512 CGF.ConvertType(CGF.getContext().getPointerDiffType());
2513
2514 // Do the virtual adjustment.
2515 llvm::Value *VTablePtrPtr =
2516 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2517
2518 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2519
2520 llvm::Value *OffsetPtr =
2521 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2522
2523 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2524
2525 // Load the adjustment offset from the vtable.
2526 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2527
2528 // Adjust our pointer.
2529 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2530 }
2531
2532 // Cast back to the original type.
2533 return CGF.Builder.CreateBitCast(V, Ptr->getType());
2534}
2535
John McCall65005532010-08-04 23:46:35 +00002536static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
2537 const ThunkInfo &Thunk, llvm::Function *Fn) {
Anders Carlsson0ffeaad2011-01-29 19:39:23 +00002538 CGM.setGlobalVisibility(Fn, MD);
John McCall65005532010-08-04 23:46:35 +00002539
John McCall279b5eb2010-08-12 23:36:15 +00002540 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
2541 return;
2542
John McCall65005532010-08-04 23:46:35 +00002543 // If the thunk has weak/linkonce linkage, but the function must be
2544 // emitted in every translation unit that references it, then we can
2545 // emit its thunks with hidden visibility, since its thunks must be
2546 // emitted when the function is.
2547
John McCall7a536902010-08-05 20:39:18 +00002548 // This follows CodeGenModule::setTypeVisibility; see the comments
2549 // there for explanation.
John McCall65005532010-08-04 23:46:35 +00002550
2551 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
2552 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
2553 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
2554 return;
2555
Douglas Gregor4421d2b2011-03-26 12:10:19 +00002556 if (MD->getExplicitVisibility())
John McCall65005532010-08-04 23:46:35 +00002557 return;
2558
2559 switch (MD->getTemplateSpecializationKind()) {
John McCall65005532010-08-04 23:46:35 +00002560 case TSK_ExplicitInstantiationDefinition:
2561 case TSK_ExplicitInstantiationDeclaration:
2562 return;
2563
John McCall65005532010-08-04 23:46:35 +00002564 case TSK_Undeclared:
2565 break;
2566
John McCall7a536902010-08-05 20:39:18 +00002567 case TSK_ExplicitSpecialization:
John McCall65005532010-08-04 23:46:35 +00002568 case TSK_ImplicitInstantiation:
John McCall279b5eb2010-08-12 23:36:15 +00002569 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
John McCall7a536902010-08-05 20:39:18 +00002570 return;
John McCall65005532010-08-04 23:46:35 +00002571 break;
2572 }
2573
2574 // If there's an explicit definition, and that definition is
2575 // out-of-line, then we can't assume that all users will have a
2576 // definition to emit.
2577 const FunctionDecl *Def = 0;
2578 if (MD->hasBody(Def) && Def->isOutOfLine())
2579 return;
2580
2581 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2582}
2583
John McCall311b4422011-03-09 07:12:35 +00002584#ifndef NDEBUG
2585static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
2586 const ABIArgInfo &infoR, CanQualType typeR) {
2587 return (infoL.getKind() == infoR.getKind() &&
2588 (typeL == typeR ||
2589 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
2590 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
2591}
2592#endif
2593
John McCalld26bc762011-03-09 04:27:21 +00002594void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
2595 const CGFunctionInfo &FnInfo,
2596 GlobalDecl GD, const ThunkInfo &Thunk) {
Anders Carlsson519c3282010-03-24 00:39:18 +00002597 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2598 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2599 QualType ResultType = FPT->getResultType();
2600 QualType ThisType = MD->getThisType(getContext());
2601
2602 FunctionArgList FunctionArgs;
2603
2604 // FIXME: It would be nice if more of this code could be shared with
2605 // CodeGenFunction::GenerateCode.
2606
2607 // Create the implicit 'this' parameter declaration.
John McCall4c40d982010-08-31 07:33:07 +00002608 CurGD = GD;
2609 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
Anders Carlsson519c3282010-03-24 00:39:18 +00002610
2611 // Add the rest of the parameters.
2612 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2613 E = MD->param_end(); I != E; ++I) {
2614 ParmVarDecl *Param = *I;
2615
John McCalld26bc762011-03-09 04:27:21 +00002616 FunctionArgs.push_back(Param);
Anders Carlsson519c3282010-03-24 00:39:18 +00002617 }
Tilmann Scheller9c6082f2011-03-02 21:36:49 +00002618
John McCalld26bc762011-03-09 04:27:21 +00002619 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
2620 SourceLocation());
Anders Carlsson519c3282010-03-24 00:39:18 +00002621
John McCall4c40d982010-08-31 07:33:07 +00002622 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2623
Anders Carlsson519c3282010-03-24 00:39:18 +00002624 // Adjust the 'this' pointer if necessary.
2625 llvm::Value *AdjustedThisPtr =
2626 PerformTypeAdjustment(*this, LoadCXXThis(),
2627 Thunk.This.NonVirtual,
2628 Thunk.This.VCallOffsetOffset);
2629
2630 CallArgList CallArgs;
2631
2632 // Add our adjusted 'this' pointer.
2633 CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
2634
2635 // Add the rest of the parameters.
2636 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2637 E = MD->param_end(); I != E; ++I) {
John McCall413ebdb2011-03-11 20:59:21 +00002638 ParmVarDecl *param = *I;
2639 EmitDelegateCallArg(CallArgs, param);
Anders Carlsson519c3282010-03-24 00:39:18 +00002640 }
2641
2642 // Get our callee.
2643 const llvm::Type *Ty =
John McCall4c40d982010-08-31 07:33:07 +00002644 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
Anders Carlsson519c3282010-03-24 00:39:18 +00002645 FPT->isVariadic());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002646 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson519c3282010-03-24 00:39:18 +00002647
John McCalld26bc762011-03-09 04:27:21 +00002648#ifndef NDEBUG
2649 const CGFunctionInfo &CallFnInfo =
2650 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getExtInfo());
John McCall311b4422011-03-09 07:12:35 +00002651 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
2652 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
2653 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
2654 assert(similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
2655 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
2656 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
2657 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
2658 assert(similar(CallFnInfo.arg_begin()[i].info,
2659 CallFnInfo.arg_begin()[i].type,
2660 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
John McCalld26bc762011-03-09 04:27:21 +00002661#endif
Anders Carlsson519c3282010-03-24 00:39:18 +00002662
Douglas Gregorcb359df2010-05-20 05:54:35 +00002663 // Determine whether we have a return value slot to use.
2664 ReturnValueSlot Slot;
2665 if (!ResultType->isVoidType() &&
2666 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2667 hasAggregateLLVMType(CurFnInfo->getReturnType()))
2668 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
2669
Anders Carlsson519c3282010-03-24 00:39:18 +00002670 // Now emit our call.
Douglas Gregorcb359df2010-05-20 05:54:35 +00002671 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
Anders Carlsson519c3282010-03-24 00:39:18 +00002672
2673 if (!Thunk.Return.isEmpty()) {
2674 // Emit the return adjustment.
2675 bool NullCheckValue = !ResultType->isReferenceType();
2676
2677 llvm::BasicBlock *AdjustNull = 0;
2678 llvm::BasicBlock *AdjustNotNull = 0;
2679 llvm::BasicBlock *AdjustEnd = 0;
2680
2681 llvm::Value *ReturnValue = RV.getScalarVal();
2682
2683 if (NullCheckValue) {
2684 AdjustNull = createBasicBlock("adjust.null");
2685 AdjustNotNull = createBasicBlock("adjust.notnull");
2686 AdjustEnd = createBasicBlock("adjust.end");
2687
2688 llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
2689 Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2690 EmitBlock(AdjustNotNull);
2691 }
2692
2693 ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
2694 Thunk.Return.NonVirtual,
2695 Thunk.Return.VBaseOffsetOffset);
2696
2697 if (NullCheckValue) {
2698 Builder.CreateBr(AdjustEnd);
2699 EmitBlock(AdjustNull);
2700 Builder.CreateBr(AdjustEnd);
2701 EmitBlock(AdjustEnd);
2702
Jay Foadbbf3bac2011-03-30 11:28:58 +00002703 llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType(), 2);
Anders Carlsson519c3282010-03-24 00:39:18 +00002704 PHI->addIncoming(ReturnValue, AdjustNotNull);
2705 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2706 AdjustNull);
2707 ReturnValue = PHI;
2708 }
2709
2710 RV = RValue::get(ReturnValue);
2711 }
2712
Douglas Gregorcb359df2010-05-20 05:54:35 +00002713 if (!ResultType->isVoidType() && Slot.isNull())
John McCalld16c2cf2011-02-08 08:22:06 +00002714 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
Anders Carlsson519c3282010-03-24 00:39:18 +00002715
2716 FinishFunction();
2717
Anders Carlsson519c3282010-03-24 00:39:18 +00002718 // Set the right linkage.
John McCall8b242332010-05-25 04:30:21 +00002719 CGM.setFunctionLinkage(MD, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002720
2721 // Set the right visibility.
John McCall65005532010-08-04 23:46:35 +00002722 setThunkVisibility(CGM, MD, Thunk, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +00002723}
2724
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002725void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
2726 bool UseAvailableExternallyLinkage)
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002727{
John McCalld26bc762011-03-09 04:27:21 +00002728 const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(GD);
2729
2730 // FIXME: re-use FnInfo in this computation.
Anders Carlsson84c49e42011-02-06 17:15:43 +00002731 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +00002732
Anders Carlsson7986ad52010-03-23 18:18:41 +00002733 // Strip off a bitcast if we got one back.
Anders Carlsson13d68982010-03-24 00:35:44 +00002734 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson7986ad52010-03-23 18:18:41 +00002735 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson13d68982010-03-24 00:35:44 +00002736 Entry = CE->getOperand(0);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002737 }
2738
Anders Carlsson7986ad52010-03-23 18:18:41 +00002739 // There's already a declaration with the same name, check if it has the same
2740 // type or if we need to replace it.
Anders Carlsson13d68982010-03-24 00:35:44 +00002741 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
John McCall4c40d982010-08-31 07:33:07 +00002742 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
Anders Carlsson13d68982010-03-24 00:35:44 +00002743 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002744
2745 // If the types mismatch then we have to rewrite the definition.
2746 assert(OldThunkFn->isDeclaration() &&
2747 "Shouldn't replace non-declaration");
2748
2749 // Remove the name from the old thunk function and get a new thunk.
2750 OldThunkFn->setName(llvm::StringRef());
Anders Carlsson84c49e42011-02-06 17:15:43 +00002751 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +00002752
2753 // If needed, replace the old thunk with a bitcast.
2754 if (!OldThunkFn->use_empty()) {
2755 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson13d68982010-03-24 00:35:44 +00002756 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson7986ad52010-03-23 18:18:41 +00002757 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
2758 }
2759
2760 // Remove the old thunk.
2761 OldThunkFn->eraseFromParent();
2762 }
Anders Carlsson519c3282010-03-24 00:39:18 +00002763
Anders Carlsson519c3282010-03-24 00:39:18 +00002764 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002765
2766 if (!ThunkFn->isDeclaration()) {
2767 if (UseAvailableExternallyLinkage) {
2768 // There is already a thunk emitted for this function, do nothing.
2769 return;
2770 }
2771
Anders Carlsson22df7b12011-02-06 20:09:44 +00002772 // If a function has a body, it should have available_externally linkage.
2773 assert(ThunkFn->hasAvailableExternallyLinkage() &&
2774 "Function should have available_externally linkage!");
2775
2776 // Change the linkage.
2777 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
2778 return;
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002779 }
2780
2781 // Actually generate the thunk body.
John McCalld26bc762011-03-09 04:27:21 +00002782 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002783
2784 if (UseAvailableExternallyLinkage)
2785 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
2786}
2787
2788void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
2789 const ThunkInfo &Thunk) {
2790 // We only want to do this when building with optimizations.
2791 if (!CGM.getCodeGenOpts().OptimizationLevel)
2792 return;
2793
2794 // We can't emit thunks for member functions with incomplete types.
2795 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2796 if (CGM.getTypes().VerifyFuncTypeComplete(MD->getType().getTypePtr()))
2797 return;
2798
2799 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002800}
2801
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002802void CodeGenVTables::EmitThunks(GlobalDecl GD)
2803{
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002804 const CXXMethodDecl *MD =
2805 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2806
2807 // We don't need to generate thunks for the base destructor.
2808 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2809 return;
2810
2811 const CXXRecordDecl *RD = MD->getParent();
2812
Anders Carlssonccd83d72010-03-24 16:42:11 +00002813 // Compute VTable related info for this class.
John McCalle2132352010-06-02 21:22:02 +00002814 ComputeVTableRelatedInformation(RD, false);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002815
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002816 ThunksMapTy::const_iterator I = Thunks.find(MD);
2817 if (I == Thunks.end()) {
Anders Carlssonccd83d72010-03-24 16:42:11 +00002818 // We did not find a thunk for this method.
2819 return;
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002820 }
Anders Carlssonccd83d72010-03-24 16:42:11 +00002821
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00002822 const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2823 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
Anders Carlsson14e82fd2011-02-06 18:31:40 +00002824 EmitThunk(GD, ThunkInfoVector[I], /*UseAvailableExternallyLinkage=*/false);
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002825}
2826
John McCalle2132352010-06-02 21:22:02 +00002827void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
2828 bool RequireVTable) {
2829 VTableLayoutData &Entry = VTableLayoutMap[RD];
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002830
2831 // We may need to generate a definition for this vtable.
John McCalle2132352010-06-02 21:22:02 +00002832 if (RequireVTable && !Entry.getInt()) {
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +00002833 if (ShouldEmitVTableInThisTU(RD))
John McCalle2132352010-06-02 21:22:02 +00002834 CGM.DeferredVTables.push_back(RD);
2835
2836 Entry.setInt(true);
2837 }
2838
2839 // Check if we've computed this information before.
2840 if (Entry.getPointer())
2841 return;
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002842
Ken Dyck23da84a2011-03-27 20:20:28 +00002843 VTableBuilder Builder(*this, RD, CharUnits::Zero(),
2844 /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002845
2846 // Add the VTable layout.
2847 uint64_t NumVTableComponents = Builder.getNumVTableComponents();
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002848 // -fapple-kext adds an extra entry at end of vtbl.
2849 bool IsAppleKext = CGM.getContext().getLangOptions().AppleKext;
2850 if (IsAppleKext)
2851 NumVTableComponents += 1;
2852
John McCalle2132352010-06-02 21:22:02 +00002853 uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1];
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +00002854 if (IsAppleKext)
2855 LayoutData[NumVTableComponents] = 0;
John McCalle2132352010-06-02 21:22:02 +00002856 Entry.setPointer(LayoutData);
Anders Carlssonccd83d72010-03-24 16:42:11 +00002857
2858 // Store the number of components.
2859 LayoutData[0] = NumVTableComponents;
2860
2861 // Store the components.
2862 std::copy(Builder.vtable_components_data_begin(),
2863 Builder.vtable_components_data_end(),
2864 &LayoutData[1]);
2865
2866 // Add the known thunks.
2867 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2868
Anders Carlsson276701f2010-03-29 01:38:05 +00002869 // Add the thunks needed in this vtable.
2870 assert(!VTableThunksMap.count(RD) &&
2871 "Thunks already exists for this vtable!");
2872
2873 VTableThunksTy &VTableThunks = VTableThunksMap[RD];
2874 VTableThunks.append(Builder.vtable_thunks_begin(),
2875 Builder.vtable_thunks_end());
2876
2877 // Sort them.
2878 std::sort(VTableThunks.begin(), VTableThunks.end());
2879
Anders Carlsson66d567d2010-03-25 00:51:13 +00002880 // Add the address points.
Anders Carlsson94464812010-04-10 19:13:06 +00002881 for (VTableBuilder::AddressPointsMapTy::const_iterator I =
Anders Carlsson66d567d2010-03-25 00:51:13 +00002882 Builder.address_points_begin(), E = Builder.address_points_end();
2883 I != E; ++I) {
2884
2885 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
2886
2887 // Check if we already have the address points for this base.
2888 assert(!AddressPoint && "Address point already exists for this base!");
2889
2890 AddressPoint = I->second;
2891 }
2892
Anders Carlssonccd83d72010-03-24 16:42:11 +00002893 // If we don't have the vbase information for this class, insert it.
2894 // getVirtualBaseOffsetOffset will compute it separately without computing
2895 // the rest of the vtable related information.
2896 if (!RD->getNumVBases())
2897 return;
2898
2899 const RecordType *VBaseRT =
2900 RD->vbases_begin()->getType()->getAs<RecordType>();
2901 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
2902
2903 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2904 return;
2905
Anders Carlsson94464812010-04-10 19:13:06 +00002906 for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
Anders Carlssonccd83d72010-03-24 16:42:11 +00002907 Builder.getVBaseOffsetOffsets().begin(),
2908 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2909 // Insert all types.
2910 ClassPairTy ClassPair(RD, I->first);
2911
Ken Dyckd28c33c2011-04-07 00:55:01 +00002912 VirtualBaseClassOffsetOffsets.insert(
Ken Dyck1a7f7522011-04-07 01:22:42 +00002913 std::make_pair(ClassPair, I->second));
Anders Carlssonccd83d72010-03-24 16:42:11 +00002914 }
2915}
2916
Anders Carlsson0d1407e2010-03-25 15:26:28 +00002917llvm::Constant *
2918CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
2919 const uint64_t *Components,
2920 unsigned NumComponents,
2921 const VTableThunksTy &VTableThunks) {
2922 llvm::SmallVector<llvm::Constant *, 64> Inits;
2923
2924 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2925
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002926 const llvm::Type *PtrDiffTy =
2927 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2928
2929 QualType ClassType = CGM.getContext().getTagDeclType(RD);
2930 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
2931
2932 unsigned NextVTableThunkIndex = 0;
2933
Anders Carlsson67d568a2010-03-29 05:40:50 +00002934 llvm::Constant* PureVirtualFn = 0;
2935
Anders Carlsson0d1407e2010-03-25 15:26:28 +00002936 for (unsigned I = 0; I != NumComponents; ++I) {
Anders Carlsson94464812010-04-10 19:13:06 +00002937 VTableComponent Component =
2938 VTableComponent::getFromOpaqueInteger(Components[I]);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002939
2940 llvm::Constant *Init = 0;
2941
2942 switch (Component.getKind()) {
Anders Carlsson94464812010-04-10 19:13:06 +00002943 case VTableComponent::CK_VCallOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002944 Init = llvm::ConstantInt::get(PtrDiffTy,
2945 Component.getVCallOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002946 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2947 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002948 case VTableComponent::CK_VBaseOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002949 Init = llvm::ConstantInt::get(PtrDiffTy,
2950 Component.getVBaseOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002951 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2952 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002953 case VTableComponent::CK_OffsetToTop:
Ken Dyckc40a3fd2011-04-02 01:14:48 +00002954 Init = llvm::ConstantInt::get(PtrDiffTy,
2955 Component.getOffsetToTop().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002956 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2957 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002958 case VTableComponent::CK_RTTI:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002959 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
2960 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002961 case VTableComponent::CK_FunctionPointer:
2962 case VTableComponent::CK_CompleteDtorPointer:
2963 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002964 GlobalDecl GD;
2965
2966 // Get the right global decl.
2967 switch (Component.getKind()) {
2968 default:
2969 llvm_unreachable("Unexpected vtable component kind");
Anders Carlsson94464812010-04-10 19:13:06 +00002970 case VTableComponent::CK_FunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002971 GD = Component.getFunctionDecl();
2972 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002973 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002974 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
2975 break;
Anders Carlsson94464812010-04-10 19:13:06 +00002976 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002977 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
2978 break;
2979 }
2980
Anders Carlsson67d568a2010-03-29 05:40:50 +00002981 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
2982 // We have a pure virtual member function.
2983 if (!PureVirtualFn) {
2984 const llvm::FunctionType *Ty =
2985 llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2986 /*isVarArg=*/false);
2987 PureVirtualFn =
2988 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
2989 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
2990 Int8PtrTy);
2991 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002992
Anders Carlsson67d568a2010-03-29 05:40:50 +00002993 Init = PureVirtualFn;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002994 } else {
Anders Carlsson67d568a2010-03-29 05:40:50 +00002995 // Check if we should use a thunk.
2996 if (NextVTableThunkIndex < VTableThunks.size() &&
2997 VTableThunks[NextVTableThunkIndex].first == I) {
2998 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00002999
Anders Carlsson84c49e42011-02-06 17:15:43 +00003000 Init = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +00003001 MaybeEmitThunkAvailableExternally(GD, Thunk);
3002
Anders Carlsson67d568a2010-03-29 05:40:50 +00003003 NextVTableThunkIndex++;
3004 } else {
John McCall4c40d982010-08-31 07:33:07 +00003005 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003006
Anders Carlsson1faa89f2011-02-05 04:35:53 +00003007 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003008 }
3009
3010 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003011 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003012 break;
3013 }
3014
Anders Carlsson94464812010-04-10 19:13:06 +00003015 case VTableComponent::CK_UnusedFunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003016 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
3017 break;
3018 };
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003019
3020 Inits.push_back(Init);
3021 }
3022
3023 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
3024 return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
3025}
3026
Anders Carlsson9dc338a2010-03-30 03:35:35 +00003027llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003028 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003029 llvm::raw_svector_ostream Out(OutName);
3030 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
3031 Out.flush();
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003032 llvm::StringRef Name = OutName.str();
Mike Stump85615df2009-11-19 04:04:36 +00003033
John McCalle2132352010-06-02 21:22:02 +00003034 ComputeVTableRelatedInformation(RD, true);
Anders Carlssonccd83d72010-03-24 16:42:11 +00003035
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003036 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlssonccd83d72010-03-24 16:42:11 +00003037 llvm::ArrayType *ArrayType =
3038 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003039
Rafael Espindola9f959db2011-01-11 21:10:26 +00003040 llvm::GlobalVariable *GV =
Anders Carlsson96eaf292011-01-29 18:25:07 +00003041 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
3042 llvm::GlobalValue::ExternalLinkage);
Rafael Espindolacd3ac4b2011-01-15 08:23:14 +00003043 GV->setUnnamedAddr(true);
Rafael Espindola9f959db2011-01-11 21:10:26 +00003044 return GV;
Mike Stump380dd752009-11-10 07:44:33 +00003045}
Mike Stump8cfcb522009-11-11 20:26:26 +00003046
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003047void
3048CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
3049 llvm::GlobalVariable::LinkageTypes Linkage,
3050 const CXXRecordDecl *RD) {
3051 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003052 if (CGM.getLangOptions().DumpVTableLayouts) {
Ken Dyck23da84a2011-03-27 20:20:28 +00003053 VTableBuilder Builder(*this, RD, CharUnits::Zero(),
3054 /*MostDerivedClassIsVirtual=*/0, RD);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003055
3056 Builder.dumpLayout(llvm::errs());
3057 }
3058
3059 assert(VTableThunksMap.count(RD) &&
3060 "No thunk status for this record decl!");
3061
3062 const VTableThunksTy& Thunks = VTableThunksMap[RD];
3063
3064 // Create and set the initializer.
3065 llvm::Constant *Init =
3066 CreateVTableInitializer(RD, getVTableComponentsData(RD),
3067 getNumVTableComponents(RD), Thunks);
3068 VTable->setInitializer(Init);
Anders Carlsson67d568a2010-03-29 05:40:50 +00003069
3070 // Set the correct linkage.
3071 VTable->setLinkage(Linkage);
Douglas Gregorc66bcfd2010-06-14 23:41:45 +00003072
3073 // Set the right visibility.
Anders Carlssonfa2e99f2011-01-29 20:24:48 +00003074 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003075}
3076
Anders Carlssonff143f82010-03-25 00:35:49 +00003077llvm::GlobalVariable *
3078CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
Anders Carlsson2c822f12010-03-26 03:56:54 +00003079 const BaseSubobject &Base,
3080 bool BaseIsVirtual,
John McCallbda0d6b2011-03-27 09:00:25 +00003081 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +00003082 VTableAddressPointsMapTy& AddressPoints) {
Ken Dyck4230d522011-03-24 01:21:01 +00003083 VTableBuilder Builder(*this, Base.getBase(),
Ken Dyck23da84a2011-03-27 20:20:28 +00003084 Base.getBaseOffset(),
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003085 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3086
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003087 // Dump the vtable layout if necessary.
Anders Carlsson046c2942010-04-17 20:15:18 +00003088 if (CGM.getLangOptions().DumpVTableLayouts)
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00003089 Builder.dumpLayout(llvm::errs());
3090
3091 // Add the address points.
Anders Carlsson2c822f12010-03-26 03:56:54 +00003092 AddressPoints.insert(Builder.address_points_begin(),
3093 Builder.address_points_end());
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003094
3095 // Get the mangled construction vtable name.
3096 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003097 llvm::raw_svector_ostream Out(OutName);
John McCall4c40d982010-08-31 07:33:07 +00003098 CGM.getCXXABI().getMangleContext().
Ken Dyck4230d522011-03-24 01:21:01 +00003099 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
3100 Out);
Rafael Espindolaf0be9792011-02-11 02:52:17 +00003101 Out.flush();
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003102 llvm::StringRef Name = OutName.str();
3103
3104 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3105 llvm::ArrayType *ArrayType =
3106 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
3107
3108 // Create the variable that will hold the construction vtable.
3109 llvm::GlobalVariable *VTable =
John McCallbda0d6b2011-03-27 09:00:25 +00003110 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
3111 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
3112
3113 // V-tables are always unnamed_addr.
3114 VTable->setUnnamedAddr(true);
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003115
3116 // Add the thunks.
3117 VTableThunksTy VTableThunks;
3118 VTableThunks.append(Builder.vtable_thunks_begin(),
3119 Builder.vtable_thunks_end());
3120
Anders Carlssonaed924e2010-03-29 01:28:05 +00003121 // Sort them.
3122 std::sort(VTableThunks.begin(), VTableThunks.end());
3123
Anders Carlsson0d1407e2010-03-25 15:26:28 +00003124 // Create and set the initializer.
3125 llvm::Constant *Init =
3126 CreateVTableInitializer(Base.getBase(),
3127 Builder.vtable_components_data_begin(),
3128 Builder.getNumVTableComponents(), VTableThunks);
3129 VTable->setInitializer(Init);
3130
Anders Carlssonff143f82010-03-25 00:35:49 +00003131 return VTable;
3132}
3133
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003134void
3135CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3136 const CXXRecordDecl *RD) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003137 llvm::GlobalVariable *&VTable = VTables[RD];
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003138 if (VTable) {
Anders Carlsson046c2942010-04-17 20:15:18 +00003139 assert(VTable->getInitializer() && "VTable doesn't have a definition!");
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003140 return;
3141 }
3142
Anders Carlsson9dc338a2010-03-30 03:35:35 +00003143 VTable = GetAddrOfVTable(RD);
3144 EmitVTableDefinition(VTable, Linkage, RD);
3145
Anders Carlsson1cbce122011-01-29 19:16:51 +00003146 if (RD->getNumVBases()) {
3147 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
3148 EmitVTTDefinition(VTT, Linkage, RD);
3149 }
Douglas Gregor1e201b42010-04-08 15:52:03 +00003150
3151 // If this is the magic class __cxxabiv1::__fundamental_type_info,
3152 // we will emit the typeinfo for the fundamental types. This is the
3153 // same behaviour as GCC.
3154 const DeclContext *DC = RD->getDeclContext();
3155 if (RD->getIdentifier() &&
3156 RD->getIdentifier()->isStr("__fundamental_type_info") &&
3157 isa<NamespaceDecl>(DC) &&
3158 cast<NamespaceDecl>(DC)->getIdentifier() &&
3159 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
3160 DC->getParent()->isTranslationUnit())
3161 CGM.EmitFundamentalRTTIDescriptors();
Anders Carlssona7cde3b2010-03-29 03:38:52 +00003162}