blob: 76d2e9bc4bdb0fe3119e6b125067aa1bf346c6a3 [file] [log] [blame]
Anders Carlsson11e51402010-04-17 20:15:18 +00001//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
Anders Carlsson2bb27f52009-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 McCall5d865c322010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Anders Carlssonf942ee02009-11-27 20:47:55 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000018#include "clang/AST/RecordLayout.h"
John McCall5513fce92010-08-05 20:39:18 +000019#include "clang/Frontend/CodeGenOptions.h"
Anders Carlssond420a312009-11-26 19:32:45 +000020#include "llvm/ADT/DenseSet.h"
Anders Carlssond46ed892010-02-27 16:18:19 +000021#include "llvm/ADT/SetVector.h"
Chandler Carruth94eab4a2010-02-13 10:38:52 +000022#include "llvm/Support/Compiler.h"
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000023#include "llvm/Support/Format.h"
Eli Friedman49a94b12011-05-06 17:27:27 +000024#include "llvm/Transforms/Utils/Cloning.h"
Anders Carlsson56446142010-03-17 20:06:32 +000025#include <algorithm>
Zhongxing Xu1721ef72009-11-13 05:46:16 +000026#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000027
28using namespace clang;
29using namespace CodeGen;
30
Peter Collingbournea8341662011-09-26 01:56:30 +000031CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
32 : CGM(CGM), VTContext(CGM.getContext()) { }
33
Argyrios Kyrtzidis0c34b132010-10-11 03:25:57 +000034bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
35 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
36
37 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
38 if (TSK == TSK_ExplicitInstantiationDeclaration)
39 return false;
40
41 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
42 if (!KeyFunction)
43 return true;
44
45 // Itanium C++ ABI, 5.2.6 Instantiated Templates:
46 // An instantiation of a class template requires:
47 // - In the object where instantiated, the virtual table...
48 if (TSK == TSK_ImplicitInstantiation ||
49 TSK == TSK_ExplicitInstantiationDefinition)
50 return true;
51
Anders Carlssona03f3a82011-01-30 20:45:54 +000052 // If we're building with optimization, we always emit VTables since that
53 // allows for virtual function calls to be devirtualized.
54 // (We don't want to do this in -fapple-kext mode however).
David Blaikiebbafb8a2012-03-11 07:00:24 +000055 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOpts().AppleKext)
Anders Carlssona03f3a82011-01-30 20:45:54 +000056 return true;
57
Argyrios Kyrtzidis0c34b132010-10-11 03:25:57 +000058 return KeyFunction->hasBody();
59}
60
Anders Carlssoncd836f02010-03-23 17:17:29 +000061llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
Anders Carlssonfe8a9932011-02-06 17:15:43 +000062 const ThunkInfo &Thunk) {
Anders Carlssoncd836f02010-03-23 17:17:29 +000063 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
64
65 // Compute the mangled name.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000066 SmallString<256> Name;
Rafael Espindola3968cd02011-02-11 02:52:17 +000067 llvm::raw_svector_ostream Out(Name);
Anders Carlssoncd836f02010-03-23 17:17:29 +000068 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
John McCall5d865c322010-08-31 07:33:07 +000069 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
Rafael Espindola3968cd02011-02-11 02:52:17 +000070 Thunk.This, Out);
Anders Carlssoncd836f02010-03-23 17:17:29 +000071 else
Rafael Espindola3968cd02011-02-11 02:52:17 +000072 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
73 Out.flush();
74
Chris Lattner2192fe52011-07-18 04:24:23 +000075 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
Anders Carlssonfe8a9932011-02-06 17:15:43 +000076 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
Anders Carlssoncd836f02010-03-23 17:17:29 +000077}
78
Anders Carlssonbad991d2010-03-24 00:39:18 +000079static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
80 llvm::Value *Ptr,
81 int64_t NonVirtualAdjustment,
Eli Friedman00755e92012-09-14 01:45:09 +000082 int64_t VirtualAdjustment,
83 bool IsReturnAdjustment) {
Anders Carlssonbad991d2010-03-24 00:39:18 +000084 if (!NonVirtualAdjustment && !VirtualAdjustment)
85 return Ptr;
86
Chris Lattnerece04092012-02-07 00:39:47 +000087 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Anders Carlssonbad991d2010-03-24 00:39:18 +000088 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
89
Eli Friedman00755e92012-09-14 01:45:09 +000090 if (NonVirtualAdjustment && !IsReturnAdjustment) {
91 // Perform the non-virtual adjustment for a base-to-derived cast.
Anders Carlssonbad991d2010-03-24 00:39:18 +000092 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
93 }
94
95 if (VirtualAdjustment) {
Chris Lattner2192fe52011-07-18 04:24:23 +000096 llvm::Type *PtrDiffTy =
Anders Carlssonbad991d2010-03-24 00:39:18 +000097 CGF.ConvertType(CGF.getContext().getPointerDiffType());
98
Eli Friedman00755e92012-09-14 01:45:09 +000099 // Perform the virtual adjustment.
Anders Carlssonbad991d2010-03-24 00:39:18 +0000100 llvm::Value *VTablePtrPtr =
101 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
102
103 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
104
105 llvm::Value *OffsetPtr =
106 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
107
108 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
109
110 // Load the adjustment offset from the vtable.
111 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
112
113 // Adjust our pointer.
114 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
115 }
116
Eli Friedman00755e92012-09-14 01:45:09 +0000117 if (NonVirtualAdjustment && IsReturnAdjustment) {
118 // Perform the non-virtual adjustment for a derived-to-base cast.
119 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
120 }
121
Anders Carlssonbad991d2010-03-24 00:39:18 +0000122 // Cast back to the original type.
123 return CGF.Builder.CreateBitCast(V, Ptr->getType());
124}
125
John McCallc8bd9c22010-08-04 23:46:35 +0000126static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
127 const ThunkInfo &Thunk, llvm::Function *Fn) {
Anders Carlssonc6a47892011-01-29 19:39:23 +0000128 CGM.setGlobalVisibility(Fn, MD);
John McCallc8bd9c22010-08-04 23:46:35 +0000129
John McCallb3732bb2010-08-12 23:36:15 +0000130 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
131 return;
132
John McCallc8bd9c22010-08-04 23:46:35 +0000133 // If the thunk has weak/linkonce linkage, but the function must be
134 // emitted in every translation unit that references it, then we can
135 // emit its thunks with hidden visibility, since its thunks must be
136 // emitted when the function is.
137
John McCall5513fce92010-08-05 20:39:18 +0000138 // This follows CodeGenModule::setTypeVisibility; see the comments
139 // there for explanation.
John McCallc8bd9c22010-08-04 23:46:35 +0000140
141 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
142 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
143 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
144 return;
145
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000146 if (MD->getExplicitVisibility())
John McCallc8bd9c22010-08-04 23:46:35 +0000147 return;
148
149 switch (MD->getTemplateSpecializationKind()) {
John McCallc8bd9c22010-08-04 23:46:35 +0000150 case TSK_ExplicitInstantiationDefinition:
151 case TSK_ExplicitInstantiationDeclaration:
152 return;
153
John McCallc8bd9c22010-08-04 23:46:35 +0000154 case TSK_Undeclared:
155 break;
156
John McCall5513fce92010-08-05 20:39:18 +0000157 case TSK_ExplicitSpecialization:
John McCallc8bd9c22010-08-04 23:46:35 +0000158 case TSK_ImplicitInstantiation:
John McCallb3732bb2010-08-12 23:36:15 +0000159 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
John McCall5513fce92010-08-05 20:39:18 +0000160 return;
John McCallc8bd9c22010-08-04 23:46:35 +0000161 break;
162 }
163
164 // If there's an explicit definition, and that definition is
165 // out-of-line, then we can't assume that all users will have a
166 // definition to emit.
167 const FunctionDecl *Def = 0;
168 if (MD->hasBody(Def) && Def->isOutOfLine())
169 return;
170
171 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
172}
173
John McCall5fe00962011-03-09 07:12:35 +0000174#ifndef NDEBUG
175static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
176 const ABIArgInfo &infoR, CanQualType typeR) {
177 return (infoL.getKind() == infoR.getKind() &&
178 (typeL == typeR ||
179 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
180 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
181}
182#endif
183
Eli Friedman49a94b12011-05-06 17:27:27 +0000184static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
185 QualType ResultType, RValue RV,
186 const ThunkInfo &Thunk) {
187 // Emit the return adjustment.
188 bool NullCheckValue = !ResultType->isReferenceType();
189
190 llvm::BasicBlock *AdjustNull = 0;
191 llvm::BasicBlock *AdjustNotNull = 0;
192 llvm::BasicBlock *AdjustEnd = 0;
193
194 llvm::Value *ReturnValue = RV.getScalarVal();
195
196 if (NullCheckValue) {
197 AdjustNull = CGF.createBasicBlock("adjust.null");
198 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
199 AdjustEnd = CGF.createBasicBlock("adjust.end");
200
201 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
202 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
203 CGF.EmitBlock(AdjustNotNull);
204 }
205
206 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
207 Thunk.Return.NonVirtual,
Eli Friedman00755e92012-09-14 01:45:09 +0000208 Thunk.Return.VBaseOffsetOffset,
209 /*IsReturnAdjustment*/true);
Eli Friedman49a94b12011-05-06 17:27:27 +0000210
211 if (NullCheckValue) {
212 CGF.Builder.CreateBr(AdjustEnd);
213 CGF.EmitBlock(AdjustNull);
214 CGF.Builder.CreateBr(AdjustEnd);
215 CGF.EmitBlock(AdjustEnd);
216
217 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
218 PHI->addIncoming(ReturnValue, AdjustNotNull);
219 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
220 AdjustNull);
221 ReturnValue = PHI;
222 }
223
224 return RValue::get(ReturnValue);
225}
226
227// This function does roughly the same thing as GenerateThunk, but in a
228// very different way, so that va_start and va_end work correctly.
229// FIXME: This function assumes "this" is the first non-sret LLVM argument of
230// a function, and that there is an alloca built in the entry block
231// for all accesses to "this".
232// FIXME: This function assumes there is only one "ret" statement per function.
233// FIXME: Cloning isn't correct in the presence of indirect goto!
234// FIXME: This implementation of thunks bloats codesize by duplicating the
235// function definition. There are alternatives:
236// 1. Add some sort of stub support to LLVM for cases where we can
237// do a this adjustment, then a sibcall.
238// 2. We could transform the definition to take a va_list instead of an
239// actual variable argument list, then have the thunks (including a
240// no-op thunk for the regular definition) call va_start/va_end.
241// There's a bit of per-call overhead for this solution, but it's
242// better for codesize if the definition is long.
243void CodeGenFunction::GenerateVarArgsThunk(
244 llvm::Function *Fn,
245 const CGFunctionInfo &FnInfo,
246 GlobalDecl GD, const ThunkInfo &Thunk) {
247 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
248 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
249 QualType ResultType = FPT->getResultType();
250
251 // Get the original function
John McCalla729c622012-02-17 03:33:10 +0000252 assert(FnInfo.isVariadic());
253 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
Eli Friedman49a94b12011-05-06 17:27:27 +0000254 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
255 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
256
257 // Clone to thunk.
Benjamin Kramer6ca42102012-09-19 13:13:52 +0000258 llvm::ValueToValueMapTy VMap;
259 llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
260 /*ModuleLevelChanges=*/false);
Eli Friedman49a94b12011-05-06 17:27:27 +0000261 CGM.getModule().getFunctionList().push_back(NewFn);
262 Fn->replaceAllUsesWith(NewFn);
263 NewFn->takeName(Fn);
264 Fn->eraseFromParent();
265 Fn = NewFn;
266
267 // "Initialize" CGF (minimally).
268 CurFn = Fn;
269
270 // Get the "this" value
271 llvm::Function::arg_iterator AI = Fn->arg_begin();
272 if (CGM.ReturnTypeUsesSRet(FnInfo))
273 ++AI;
274
275 // Find the first store of "this", which will be to the alloca associated
276 // with "this".
277 llvm::Value *ThisPtr = &*AI;
278 llvm::BasicBlock *EntryBB = Fn->begin();
279 llvm::Instruction *ThisStore = 0;
280 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
281 I != E; I++) {
282 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
283 ThisStore = cast<llvm::StoreInst>(I);
284 break;
285 }
286 }
287 assert(ThisStore && "Store of this should be in entry block?");
288 // Adjust "this", if necessary.
289 Builder.SetInsertPoint(ThisStore);
290 llvm::Value *AdjustedThisPtr =
291 PerformTypeAdjustment(*this, ThisPtr,
292 Thunk.This.NonVirtual,
Eli Friedman00755e92012-09-14 01:45:09 +0000293 Thunk.This.VCallOffsetOffset,
294 /*IsReturnAdjustment*/false);
Eli Friedman49a94b12011-05-06 17:27:27 +0000295 ThisStore->setOperand(0, AdjustedThisPtr);
296
297 if (!Thunk.Return.isEmpty()) {
298 // Fix up the returned value, if necessary.
299 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
300 llvm::Instruction *T = I->getTerminator();
301 if (isa<llvm::ReturnInst>(T)) {
302 RValue RV = RValue::get(T->getOperand(0));
303 T->eraseFromParent();
304 Builder.SetInsertPoint(&*I);
305 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
306 Builder.CreateRet(RV.getScalarVal());
307 break;
308 }
309 }
310 }
311}
312
John McCalla738c252011-03-09 04:27:21 +0000313void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
314 const CGFunctionInfo &FnInfo,
315 GlobalDecl GD, const ThunkInfo &Thunk) {
Anders Carlssonbad991d2010-03-24 00:39:18 +0000316 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
317 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
318 QualType ResultType = FPT->getResultType();
319 QualType ThisType = MD->getThisType(getContext());
320
321 FunctionArgList FunctionArgs;
322
323 // FIXME: It would be nice if more of this code could be shared with
324 // CodeGenFunction::GenerateCode.
325
326 // Create the implicit 'this' parameter declaration.
John McCall5d865c322010-08-31 07:33:07 +0000327 CurGD = GD;
328 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000329
330 // Add the rest of the parameters.
331 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
332 E = MD->param_end(); I != E; ++I) {
333 ParmVarDecl *Param = *I;
334
John McCalla738c252011-03-09 04:27:21 +0000335 FunctionArgs.push_back(Param);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000336 }
Tilmann Scheller99cc30c2011-03-02 21:36:49 +0000337
John McCalla738c252011-03-09 04:27:21 +0000338 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
339 SourceLocation());
Anders Carlssonbad991d2010-03-24 00:39:18 +0000340
John McCall5d865c322010-08-31 07:33:07 +0000341 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
Eli Friedman9fbeba02012-02-11 02:57:39 +0000342 CXXThisValue = CXXABIThisValue;
John McCall5d865c322010-08-31 07:33:07 +0000343
Anders Carlssonbad991d2010-03-24 00:39:18 +0000344 // Adjust the 'this' pointer if necessary.
345 llvm::Value *AdjustedThisPtr =
346 PerformTypeAdjustment(*this, LoadCXXThis(),
347 Thunk.This.NonVirtual,
Eli Friedman00755e92012-09-14 01:45:09 +0000348 Thunk.This.VCallOffsetOffset,
349 /*IsReturnAdjustment*/false);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000350
351 CallArgList CallArgs;
352
353 // Add our adjusted 'this' pointer.
Eli Friedman43dca6a2011-05-02 17:57:46 +0000354 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000355
356 // Add the rest of the parameters.
357 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
358 E = MD->param_end(); I != E; ++I) {
John McCall32ea9692011-03-11 20:59:21 +0000359 ParmVarDecl *param = *I;
360 EmitDelegateCallArg(CallArgs, param);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000361 }
362
363 // Get our callee.
Chris Lattner2192fe52011-07-18 04:24:23 +0000364 llvm::Type *Ty =
John McCalla729c622012-02-17 03:33:10 +0000365 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000366 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000367
John McCalla738c252011-03-09 04:27:21 +0000368#ifndef NDEBUG
John McCall8dda7b22012-07-07 06:41:13 +0000369 const CGFunctionInfo &CallFnInfo =
370 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
John McCalla729c622012-02-17 03:33:10 +0000371 RequiredArgs::forPrototypePlus(FPT, 1));
John McCall5fe00962011-03-09 07:12:35 +0000372 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
373 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
374 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
John McCall8dda7b22012-07-07 06:41:13 +0000375 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
376 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
John McCall5fe00962011-03-09 07:12:35 +0000377 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
378 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
379 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
380 assert(similar(CallFnInfo.arg_begin()[i].info,
381 CallFnInfo.arg_begin()[i].type,
382 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
John McCalla738c252011-03-09 04:27:21 +0000383#endif
Anders Carlssonbad991d2010-03-24 00:39:18 +0000384
Douglas Gregoraa2ac802010-05-20 05:54:35 +0000385 // Determine whether we have a return value slot to use.
386 ReturnValueSlot Slot;
387 if (!ResultType->isVoidType() &&
388 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
389 hasAggregateLLVMType(CurFnInfo->getReturnType()))
390 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
391
Anders Carlssonbad991d2010-03-24 00:39:18 +0000392 // Now emit our call.
Douglas Gregoraa2ac802010-05-20 05:54:35 +0000393 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000394
Eli Friedman49a94b12011-05-06 17:27:27 +0000395 if (!Thunk.Return.isEmpty())
396 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000397
Douglas Gregoraa2ac802010-05-20 05:54:35 +0000398 if (!ResultType->isVoidType() && Slot.isNull())
John McCallad7c5c12011-02-08 08:22:06 +0000399 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000400
John McCallff755cd2012-07-31 00:33:55 +0000401 // Disable the final ARC autorelease.
402 AutoreleaseResult = false;
403
Anders Carlssonbad991d2010-03-24 00:39:18 +0000404 FinishFunction();
405
Anders Carlssonbad991d2010-03-24 00:39:18 +0000406 // Set the right linkage.
John McCall7cb02202010-05-25 04:30:21 +0000407 CGM.setFunctionLinkage(MD, Fn);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000408
409 // Set the right visibility.
John McCallc8bd9c22010-08-04 23:46:35 +0000410 setThunkVisibility(CGM, MD, Thunk, Fn);
Anders Carlssonbad991d2010-03-24 00:39:18 +0000411}
412
Anders Carlsson8b021832011-02-06 18:31:40 +0000413void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
414 bool UseAvailableExternallyLinkage)
Anders Carlsson5c5abad2010-03-23 16:36:50 +0000415{
John McCalla729c622012-02-17 03:33:10 +0000416 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
John McCalla738c252011-03-09 04:27:21 +0000417
418 // FIXME: re-use FnInfo in this computation.
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000419 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlssoncd836f02010-03-23 17:17:29 +0000420
Anders Carlsson55e89f82010-03-23 18:18:41 +0000421 // Strip off a bitcast if we got one back.
Anders Carlsson4a3cdf52010-03-24 00:35:44 +0000422 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson55e89f82010-03-23 18:18:41 +0000423 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson4a3cdf52010-03-24 00:35:44 +0000424 Entry = CE->getOperand(0);
Anders Carlsson55e89f82010-03-23 18:18:41 +0000425 }
426
Anders Carlsson55e89f82010-03-23 18:18:41 +0000427 // There's already a declaration with the same name, check if it has the same
428 // type or if we need to replace it.
Anders Carlsson4a3cdf52010-03-24 00:35:44 +0000429 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
John McCall5d865c322010-08-31 07:33:07 +0000430 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
Anders Carlsson4a3cdf52010-03-24 00:35:44 +0000431 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson55e89f82010-03-23 18:18:41 +0000432
433 // If the types mismatch then we have to rewrite the definition.
434 assert(OldThunkFn->isDeclaration() &&
435 "Shouldn't replace non-declaration");
436
437 // Remove the name from the old thunk function and get a new thunk.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000438 OldThunkFn->setName(StringRef());
Anders Carlssonfe8a9932011-02-06 17:15:43 +0000439 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson55e89f82010-03-23 18:18:41 +0000440
441 // If needed, replace the old thunk with a bitcast.
442 if (!OldThunkFn->use_empty()) {
443 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson4a3cdf52010-03-24 00:35:44 +0000444 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson55e89f82010-03-23 18:18:41 +0000445 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
446 }
447
448 // Remove the old thunk.
449 OldThunkFn->eraseFromParent();
450 }
Anders Carlssonbad991d2010-03-24 00:39:18 +0000451
Anders Carlssonbad991d2010-03-24 00:39:18 +0000452 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
Anders Carlsson8b021832011-02-06 18:31:40 +0000453
454 if (!ThunkFn->isDeclaration()) {
455 if (UseAvailableExternallyLinkage) {
456 // There is already a thunk emitted for this function, do nothing.
457 return;
458 }
459
Anders Carlssone866d442011-02-06 20:09:44 +0000460 // If a function has a body, it should have available_externally linkage.
461 assert(ThunkFn->hasAvailableExternallyLinkage() &&
462 "Function should have available_externally linkage!");
463
464 // Change the linkage.
465 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
466 return;
Anders Carlsson8b021832011-02-06 18:31:40 +0000467 }
468
Eli Friedman49a94b12011-05-06 17:27:27 +0000469 if (ThunkFn->isVarArg()) {
470 // Varargs thunks are special; we can't just generate a call because
471 // we can't copy the varargs. Our implementation is rather
472 // expensive/sucky at the moment, so don't generate the thunk unless
473 // we have to.
474 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
475 if (!UseAvailableExternallyLinkage)
476 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
477 } else {
478 // Normal thunk body generation.
479 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
480 }
Anders Carlsson8b021832011-02-06 18:31:40 +0000481
482 if (UseAvailableExternallyLinkage)
483 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
484}
485
486void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
487 const ThunkInfo &Thunk) {
488 // We only want to do this when building with optimizations.
489 if (!CGM.getCodeGenOpts().OptimizationLevel)
490 return;
491
492 // We can't emit thunks for member functions with incomplete types.
493 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Chris Lattner8806e322011-07-10 00:18:59 +0000494 if (!CGM.getTypes().isFuncTypeConvertible(
495 cast<FunctionType>(MD->getType().getTypePtr())))
Anders Carlsson8b021832011-02-06 18:31:40 +0000496 return;
497
498 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
Anders Carlsson5c5abad2010-03-23 16:36:50 +0000499}
500
Anders Carlsson917229c2010-03-23 04:59:02 +0000501void CodeGenVTables::EmitThunks(GlobalDecl GD)
502{
Anders Carlsson5c5abad2010-03-23 16:36:50 +0000503 const CXXMethodDecl *MD =
504 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
505
506 // We don't need to generate thunks for the base destructor.
507 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
508 return;
509
Peter Collingbourne5ee9ee42011-09-26 01:56:41 +0000510 const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
511 VTContext.getThunkInfo(MD);
512 if (!ThunkInfoVector)
Anders Carlssone90954d2010-03-24 16:42:11 +0000513 return;
Anders Carlssone90954d2010-03-24 16:42:11 +0000514
Peter Collingbourne5ee9ee42011-09-26 01:56:41 +0000515 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
516 EmitThunk(GD, (*ThunkInfoVector)[I],
517 /*UseAvailableExternallyLinkage=*/false);
Anders Carlsson917229c2010-03-23 04:59:02 +0000518}
519
Anders Carlssona4147142010-03-25 15:26:28 +0000520llvm::Constant *
521CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000522 const VTableComponent *Components,
Anders Carlssona4147142010-03-25 15:26:28 +0000523 unsigned NumComponents,
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000524 const VTableLayout::VTableThunkTy *VTableThunks,
525 unsigned NumVTableThunks) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000526 SmallVector<llvm::Constant *, 64> Inits;
Anders Carlssona4147142010-03-25 15:26:28 +0000527
Chris Lattnerece04092012-02-07 00:39:47 +0000528 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
Anders Carlssona4147142010-03-25 15:26:28 +0000529
Chris Lattner2192fe52011-07-18 04:24:23 +0000530 llvm::Type *PtrDiffTy =
Anders Carlssona5736bd2010-03-25 16:49:53 +0000531 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
532
533 QualType ClassType = CGM.getContext().getTagDeclType(RD);
534 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
535
536 unsigned NextVTableThunkIndex = 0;
537
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000538 llvm::Constant* PureVirtualFn = 0;
539
Anders Carlssona4147142010-03-25 15:26:28 +0000540 for (unsigned I = 0; I != NumComponents; ++I) {
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000541 VTableComponent Component = Components[I];
Anders Carlssona5736bd2010-03-25 16:49:53 +0000542
543 llvm::Constant *Init = 0;
544
545 switch (Component.getKind()) {
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000546 case VTableComponent::CK_VCallOffset:
Ken Dyck872d74a2011-04-02 01:14:48 +0000547 Init = llvm::ConstantInt::get(PtrDiffTy,
548 Component.getVCallOffset().getQuantity());
Anders Carlssona5736bd2010-03-25 16:49:53 +0000549 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
550 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000551 case VTableComponent::CK_VBaseOffset:
Ken Dyck872d74a2011-04-02 01:14:48 +0000552 Init = llvm::ConstantInt::get(PtrDiffTy,
553 Component.getVBaseOffset().getQuantity());
Anders Carlssona5736bd2010-03-25 16:49:53 +0000554 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
555 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000556 case VTableComponent::CK_OffsetToTop:
Ken Dyck872d74a2011-04-02 01:14:48 +0000557 Init = llvm::ConstantInt::get(PtrDiffTy,
558 Component.getOffsetToTop().getQuantity());
Anders Carlssona5736bd2010-03-25 16:49:53 +0000559 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
560 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000561 case VTableComponent::CK_RTTI:
Anders Carlssona5736bd2010-03-25 16:49:53 +0000562 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
563 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000564 case VTableComponent::CK_FunctionPointer:
565 case VTableComponent::CK_CompleteDtorPointer:
566 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlssona5736bd2010-03-25 16:49:53 +0000567 GlobalDecl GD;
568
569 // Get the right global decl.
570 switch (Component.getKind()) {
571 default:
572 llvm_unreachable("Unexpected vtable component kind");
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000573 case VTableComponent::CK_FunctionPointer:
Anders Carlssona5736bd2010-03-25 16:49:53 +0000574 GD = Component.getFunctionDecl();
575 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000576 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlssona5736bd2010-03-25 16:49:53 +0000577 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
578 break;
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000579 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlssona5736bd2010-03-25 16:49:53 +0000580 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
581 break;
582 }
583
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000584 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
585 // We have a pure virtual member function.
Joao Matos718a8832012-07-17 19:17:58 +0000586 if (!PureVirtualFn) {
Eli Friedman48a32912012-09-14 01:19:01 +0000587 llvm::FunctionType *Ty =
588 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
589 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
590 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
591 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
Joao Matos718a8832012-07-17 19:17:58 +0000592 CGM.Int8PtrTy);
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000593 }
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000594 Init = PureVirtualFn;
Anders Carlssona5736bd2010-03-25 16:49:53 +0000595 } else {
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000596 // Check if we should use a thunk.
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000597 if (NextVTableThunkIndex < NumVTableThunks &&
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000598 VTableThunks[NextVTableThunkIndex].first == I) {
599 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
Anders Carlssona5736bd2010-03-25 16:49:53 +0000600
Anders Carlsson8b021832011-02-06 18:31:40 +0000601 MaybeEmitThunkAvailableExternally(GD, Thunk);
Benjamin Kramere6b4a162012-03-20 20:18:13 +0000602 Init = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson8b021832011-02-06 18:31:40 +0000603
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000604 NextVTableThunkIndex++;
605 } else {
Chris Lattner2192fe52011-07-18 04:24:23 +0000606 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000607
Anders Carlsson3c239482011-02-05 04:35:53 +0000608 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000609 }
610
611 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
Anders Carlssona5736bd2010-03-25 16:49:53 +0000612 }
Anders Carlssona5736bd2010-03-25 16:49:53 +0000613 break;
614 }
615
Anders Carlssonbe1b9cb2010-04-10 19:13:06 +0000616 case VTableComponent::CK_UnusedFunctionPointer:
Anders Carlssona5736bd2010-03-25 16:49:53 +0000617 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
618 break;
619 };
Anders Carlssona4147142010-03-25 15:26:28 +0000620
621 Inits.push_back(Init);
622 }
623
624 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
Jay Foad83be3612011-06-22 09:24:39 +0000625 return llvm::ConstantArray::get(ArrayType, Inits);
Anders Carlssona4147142010-03-25 15:26:28 +0000626}
627
Anders Carlssona086edc2010-03-30 03:35:35 +0000628llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Peter Collingbourne71c26932011-09-26 01:56:36 +0000629 llvm::GlobalVariable *&VTable = VTables[RD];
630 if (VTable)
631 return VTable;
632
633 // We may need to generate a definition for this vtable.
634 if (ShouldEmitVTableInThisTU(RD))
635 CGM.DeferredVTables.push_back(RD);
636
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000637 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000638 llvm::raw_svector_ostream Out(OutName);
639 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
640 Out.flush();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000641 StringRef Name = OutName.str();
Mike Stumpaa51ad62009-11-19 04:04:36 +0000642
Anders Carlssone90954d2010-03-24 16:42:11 +0000643 llvm::ArrayType *ArrayType =
Chris Lattnerece04092012-02-07 00:39:47 +0000644 llvm::ArrayType::get(CGM.Int8PtrTy,
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000645 VTContext.getVTableLayout(RD).getNumVTableComponents());
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000646
Peter Collingbourne71c26932011-09-26 01:56:36 +0000647 VTable =
Anders Carlsson93be9a92011-01-29 18:25:07 +0000648 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
649 llvm::GlobalValue::ExternalLinkage);
Peter Collingbourne71c26932011-09-26 01:56:36 +0000650 VTable->setUnnamedAddr(true);
651 return VTable;
Mike Stumpd846d082009-11-10 07:44:33 +0000652}
Mike Stumpeac45592009-11-11 20:26:26 +0000653
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000654void
655CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
656 llvm::GlobalVariable::LinkageTypes Linkage,
657 const CXXRecordDecl *RD) {
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000658 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
659
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000660 // Create and set the initializer.
661 llvm::Constant *Init =
Peter Collingbourneaffe1112011-09-26 01:56:50 +0000662 CreateVTableInitializer(RD,
663 VTLayout.vtable_component_begin(),
664 VTLayout.getNumVTableComponents(),
665 VTLayout.vtable_thunk_begin(),
666 VTLayout.getNumVTableThunks());
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000667 VTable->setInitializer(Init);
Anders Carlssoncb6207f2010-03-29 05:40:50 +0000668
669 // Set the correct linkage.
670 VTable->setLinkage(Linkage);
Douglas Gregor4f6e8de2010-06-14 23:41:45 +0000671
672 // Set the right visibility.
Anders Carlsson265aa7c2011-01-29 20:24:48 +0000673 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000674}
675
Anders Carlsson0534b022010-03-25 00:35:49 +0000676llvm::GlobalVariable *
677CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
Anders Carlssona208b392010-03-26 03:56:54 +0000678 const BaseSubobject &Base,
679 bool BaseIsVirtual,
John McCall358d0562011-03-27 09:00:25 +0000680 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssona208b392010-03-26 03:56:54 +0000681 VTableAddressPointsMapTy& AddressPoints) {
Dylan Noblesmithe2778992012-02-05 02:12:40 +0000682 OwningPtr<VTableLayout> VTLayout(
Peter Collingbourne1c593c62011-09-26 01:57:04 +0000683 VTContext.createConstructionVTableLayout(Base.getBase(),
684 Base.getBaseOffset(),
685 BaseIsVirtual, RD));
Anders Carlssona4147142010-03-25 15:26:28 +0000686
Anders Carlssona5736bd2010-03-25 16:49:53 +0000687 // Add the address points.
Peter Collingbourne1c593c62011-09-26 01:57:04 +0000688 AddressPoints = VTLayout->getAddressPoints();
Anders Carlssona4147142010-03-25 15:26:28 +0000689
690 // Get the mangled construction vtable name.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000691 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000692 llvm::raw_svector_ostream Out(OutName);
John McCall5d865c322010-08-31 07:33:07 +0000693 CGM.getCXXABI().getMangleContext().
Ken Dyck16ffcac2011-03-24 01:21:01 +0000694 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
695 Out);
Rafael Espindola3968cd02011-02-11 02:52:17 +0000696 Out.flush();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000697 StringRef Name = OutName.str();
Anders Carlssona4147142010-03-25 15:26:28 +0000698
Anders Carlssona4147142010-03-25 15:26:28 +0000699 llvm::ArrayType *ArrayType =
Chris Lattnerece04092012-02-07 00:39:47 +0000700 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
Anders Carlssona4147142010-03-25 15:26:28 +0000701
702 // Create the variable that will hold the construction vtable.
703 llvm::GlobalVariable *VTable =
John McCall358d0562011-03-27 09:00:25 +0000704 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
705 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
706
707 // V-tables are always unnamed_addr.
708 VTable->setUnnamedAddr(true);
Anders Carlssona4147142010-03-25 15:26:28 +0000709
Anders Carlssona4147142010-03-25 15:26:28 +0000710 // Create and set the initializer.
711 llvm::Constant *Init =
712 CreateVTableInitializer(Base.getBase(),
Peter Collingbourne1c593c62011-09-26 01:57:04 +0000713 VTLayout->vtable_component_begin(),
714 VTLayout->getNumVTableComponents(),
715 VTLayout->vtable_thunk_begin(),
716 VTLayout->getNumVTableThunks());
Anders Carlssona4147142010-03-25 15:26:28 +0000717 VTable->setInitializer(Init);
718
Anders Carlsson0534b022010-03-25 00:35:49 +0000719 return VTable;
720}
721
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000722void
723CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
724 const CXXRecordDecl *RD) {
Peter Collingbourne71c26932011-09-26 01:56:36 +0000725 llvm::GlobalVariable *VTable = GetAddrOfVTable(RD);
726 if (VTable->hasInitializer())
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000727 return;
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000728
Anders Carlssona086edc2010-03-30 03:35:35 +0000729 EmitVTableDefinition(VTable, Linkage, RD);
730
Anders Carlsson883fc722011-01-29 19:16:51 +0000731 if (RD->getNumVBases()) {
732 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
733 EmitVTTDefinition(VTT, Linkage, RD);
734 }
Douglas Gregoreadd3ca2010-04-08 15:52:03 +0000735
736 // If this is the magic class __cxxabiv1::__fundamental_type_info,
737 // we will emit the typeinfo for the fundamental types. This is the
738 // same behaviour as GCC.
739 const DeclContext *DC = RD->getDeclContext();
740 if (RD->getIdentifier() &&
741 RD->getIdentifier()->isStr("__fundamental_type_info") &&
742 isa<NamespaceDecl>(DC) &&
743 cast<NamespaceDecl>(DC)->getIdentifier() &&
744 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
745 DC->getParent()->isTranslationUnit())
746 CGM.EmitFundamentalRTTIDescriptors();
Anders Carlssona627ac7e2010-03-29 03:38:52 +0000747}