blob: 64c79395882d0714b31ae10aef6bd135a9073a68 [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
Anders Carlssondbd920c2009-10-11 22:13:54 +000014#include "CodeGenFunction.h"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CodeGenModule.h"
Anders Carlssond6b07fb2009-11-27 20:47:55 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000018#include "clang/AST/RecordLayout.h"
John McCall7a536902010-08-05 20:39:18 +000019#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson5dd730a2009-11-26 19:32:45 +000020#include "llvm/ADT/DenseSet.h"
Anders Carlssonb9021e92010-02-27 16:18:19 +000021#include "llvm/ADT/SetVector.h"
Chandler Carruthe087f072010-02-13 10:38:52 +000022#include "llvm/Support/Compiler.h"
Anders Carlsson824d7ea2010-02-11 08:02:13 +000023#include "llvm/Support/Format.h"
Eli Friedman7dcdf5b2011-05-06 17:27:27 +000024#include "llvm/Transforms/Utils/Cloning.h"
Anders Carlsson5e454aa2010-03-17 20:06:32 +000025#include <algorithm>
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000026#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000027
28using namespace clang;
29using namespace CodeGen;
30
Peter Collingbourne1d2b3172011-09-26 01:56:30 +000031CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
32 : CGM(CGM), VTContext(CGM.getContext()) { }
33
Argyrios Kyrtzidisd2c47bd2010-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 Carlsson6d7f8472011-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 Blaikie4e4d0842012-03-11 07:00:24 +000055 if (CGM.getCodeGenOpts().OptimizationLevel && !CGM.getLangOpts().AppleKext)
Anders Carlsson6d7f8472011-01-30 20:45:54 +000056 return true;
57
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +000058 return KeyFunction->hasBody();
59}
60
Anders Carlsson19879c92010-03-23 17:17:29 +000061llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
Anders Carlsson84c49e42011-02-06 17:15:43 +000062 const ThunkInfo &Thunk) {
Anders Carlsson19879c92010-03-23 17:17:29 +000063 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
64
65 // Compute the mangled name.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000066 SmallString<256> Name;
Rafael Espindolaf0be9792011-02-11 02:52:17 +000067 llvm::raw_svector_ostream Out(Name);
Anders Carlsson19879c92010-03-23 17:17:29 +000068 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
John McCall4c40d982010-08-31 07:33:07 +000069 getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
Rafael Espindolaf0be9792011-02-11 02:52:17 +000070 Thunk.This, Out);
Anders Carlsson19879c92010-03-23 17:17:29 +000071 else
Rafael Espindolaf0be9792011-02-11 02:52:17 +000072 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
73 Out.flush();
74
Chris Lattner2acc6e32011-07-18 04:24:23 +000075 llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson84c49e42011-02-06 17:15:43 +000076 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
Anders Carlsson19879c92010-03-23 17:17:29 +000077}
78
Anders Carlsson519c3282010-03-24 00:39:18 +000079static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
80 llvm::Value *Ptr,
81 int64_t NonVirtualAdjustment,
Eli Friedman82bad6b2012-09-14 01:45:09 +000082 int64_t VirtualAdjustment,
83 bool IsReturnAdjustment) {
Anders Carlsson519c3282010-03-24 00:39:18 +000084 if (!NonVirtualAdjustment && !VirtualAdjustment)
85 return Ptr;
86
Chris Lattner8b418682012-02-07 00:39:47 +000087 llvm::Type *Int8PtrTy = CGF.Int8PtrTy;
Anders Carlsson519c3282010-03-24 00:39:18 +000088 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
89
Eli Friedman82bad6b2012-09-14 01:45:09 +000090 if (NonVirtualAdjustment && !IsReturnAdjustment) {
91 // Perform the non-virtual adjustment for a base-to-derived cast.
Anders Carlsson519c3282010-03-24 00:39:18 +000092 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
93 }
94
95 if (VirtualAdjustment) {
Chris Lattner2acc6e32011-07-18 04:24:23 +000096 llvm::Type *PtrDiffTy =
Anders Carlsson519c3282010-03-24 00:39:18 +000097 CGF.ConvertType(CGF.getContext().getPointerDiffType());
98
Eli Friedman82bad6b2012-09-14 01:45:09 +000099 // Perform the virtual adjustment.
Anders Carlsson519c3282010-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 Friedman82bad6b2012-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 Carlsson519c3282010-03-24 00:39:18 +0000122 // Cast back to the original type.
123 return CGF.Builder.CreateBitCast(V, Ptr->getType());
124}
125
John McCall65005532010-08-04 23:46:35 +0000126static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
127 const ThunkInfo &Thunk, llvm::Function *Fn) {
Anders Carlsson0ffeaad2011-01-29 19:39:23 +0000128 CGM.setGlobalVisibility(Fn, MD);
John McCall65005532010-08-04 23:46:35 +0000129
John McCall279b5eb2010-08-12 23:36:15 +0000130 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
131 return;
132
John McCall65005532010-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 McCall7a536902010-08-05 20:39:18 +0000138 // This follows CodeGenModule::setTypeVisibility; see the comments
139 // there for explanation.
John McCall65005532010-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 Gregor4421d2b2011-03-26 12:10:19 +0000146 if (MD->getExplicitVisibility())
John McCall65005532010-08-04 23:46:35 +0000147 return;
148
149 switch (MD->getTemplateSpecializationKind()) {
John McCall65005532010-08-04 23:46:35 +0000150 case TSK_ExplicitInstantiationDefinition:
151 case TSK_ExplicitInstantiationDeclaration:
152 return;
153
John McCall65005532010-08-04 23:46:35 +0000154 case TSK_Undeclared:
155 break;
156
John McCall7a536902010-08-05 20:39:18 +0000157 case TSK_ExplicitSpecialization:
John McCall65005532010-08-04 23:46:35 +0000158 case TSK_ImplicitInstantiation:
Douglas Gregoraafd1112012-10-24 14:11:55 +0000159 return;
John McCall65005532010-08-04 23:46:35 +0000160 break;
161 }
162
163 // If there's an explicit definition, and that definition is
164 // out-of-line, then we can't assume that all users will have a
165 // definition to emit.
166 const FunctionDecl *Def = 0;
167 if (MD->hasBody(Def) && Def->isOutOfLine())
168 return;
169
170 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
171}
172
John McCall311b4422011-03-09 07:12:35 +0000173#ifndef NDEBUG
174static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
175 const ABIArgInfo &infoR, CanQualType typeR) {
176 return (infoL.getKind() == infoR.getKind() &&
177 (typeL == typeR ||
178 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
179 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
180}
181#endif
182
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000183static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
184 QualType ResultType, RValue RV,
185 const ThunkInfo &Thunk) {
186 // Emit the return adjustment.
187 bool NullCheckValue = !ResultType->isReferenceType();
188
189 llvm::BasicBlock *AdjustNull = 0;
190 llvm::BasicBlock *AdjustNotNull = 0;
191 llvm::BasicBlock *AdjustEnd = 0;
192
193 llvm::Value *ReturnValue = RV.getScalarVal();
194
195 if (NullCheckValue) {
196 AdjustNull = CGF.createBasicBlock("adjust.null");
197 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
198 AdjustEnd = CGF.createBasicBlock("adjust.end");
199
200 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
201 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
202 CGF.EmitBlock(AdjustNotNull);
203 }
204
205 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
206 Thunk.Return.NonVirtual,
Eli Friedman82bad6b2012-09-14 01:45:09 +0000207 Thunk.Return.VBaseOffsetOffset,
208 /*IsReturnAdjustment*/true);
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000209
210 if (NullCheckValue) {
211 CGF.Builder.CreateBr(AdjustEnd);
212 CGF.EmitBlock(AdjustNull);
213 CGF.Builder.CreateBr(AdjustEnd);
214 CGF.EmitBlock(AdjustEnd);
215
216 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
217 PHI->addIncoming(ReturnValue, AdjustNotNull);
218 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
219 AdjustNull);
220 ReturnValue = PHI;
221 }
222
223 return RValue::get(ReturnValue);
224}
225
226// This function does roughly the same thing as GenerateThunk, but in a
227// very different way, so that va_start and va_end work correctly.
228// FIXME: This function assumes "this" is the first non-sret LLVM argument of
229// a function, and that there is an alloca built in the entry block
230// for all accesses to "this".
231// FIXME: This function assumes there is only one "ret" statement per function.
232// FIXME: Cloning isn't correct in the presence of indirect goto!
233// FIXME: This implementation of thunks bloats codesize by duplicating the
234// function definition. There are alternatives:
235// 1. Add some sort of stub support to LLVM for cases where we can
236// do a this adjustment, then a sibcall.
237// 2. We could transform the definition to take a va_list instead of an
238// actual variable argument list, then have the thunks (including a
239// no-op thunk for the regular definition) call va_start/va_end.
240// There's a bit of per-call overhead for this solution, but it's
241// better for codesize if the definition is long.
242void CodeGenFunction::GenerateVarArgsThunk(
243 llvm::Function *Fn,
244 const CGFunctionInfo &FnInfo,
245 GlobalDecl GD, const ThunkInfo &Thunk) {
246 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
247 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
248 QualType ResultType = FPT->getResultType();
249
250 // Get the original function
John McCallde5d3c72012-02-17 03:33:10 +0000251 assert(FnInfo.isVariadic());
252 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000253 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
254 llvm::Function *BaseFn = cast<llvm::Function>(Callee);
255
256 // Clone to thunk.
Benjamin Kramer9b5ede52012-09-19 13:13:52 +0000257 llvm::ValueToValueMapTy VMap;
258 llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
259 /*ModuleLevelChanges=*/false);
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000260 CGM.getModule().getFunctionList().push_back(NewFn);
261 Fn->replaceAllUsesWith(NewFn);
262 NewFn->takeName(Fn);
263 Fn->eraseFromParent();
264 Fn = NewFn;
265
266 // "Initialize" CGF (minimally).
267 CurFn = Fn;
268
269 // Get the "this" value
270 llvm::Function::arg_iterator AI = Fn->arg_begin();
271 if (CGM.ReturnTypeUsesSRet(FnInfo))
272 ++AI;
273
274 // Find the first store of "this", which will be to the alloca associated
275 // with "this".
276 llvm::Value *ThisPtr = &*AI;
277 llvm::BasicBlock *EntryBB = Fn->begin();
278 llvm::Instruction *ThisStore = 0;
279 for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
280 I != E; I++) {
281 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
282 ThisStore = cast<llvm::StoreInst>(I);
283 break;
284 }
285 }
286 assert(ThisStore && "Store of this should be in entry block?");
287 // Adjust "this", if necessary.
288 Builder.SetInsertPoint(ThisStore);
289 llvm::Value *AdjustedThisPtr =
290 PerformTypeAdjustment(*this, ThisPtr,
291 Thunk.This.NonVirtual,
Eli Friedman82bad6b2012-09-14 01:45:09 +0000292 Thunk.This.VCallOffsetOffset,
293 /*IsReturnAdjustment*/false);
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000294 ThisStore->setOperand(0, AdjustedThisPtr);
295
296 if (!Thunk.Return.isEmpty()) {
297 // Fix up the returned value, if necessary.
298 for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
299 llvm::Instruction *T = I->getTerminator();
300 if (isa<llvm::ReturnInst>(T)) {
301 RValue RV = RValue::get(T->getOperand(0));
302 T->eraseFromParent();
303 Builder.SetInsertPoint(&*I);
304 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
305 Builder.CreateRet(RV.getScalarVal());
306 break;
307 }
308 }
309 }
310}
311
John McCalld26bc762011-03-09 04:27:21 +0000312void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
313 const CGFunctionInfo &FnInfo,
314 GlobalDecl GD, const ThunkInfo &Thunk) {
Anders Carlsson519c3282010-03-24 00:39:18 +0000315 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
316 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
317 QualType ResultType = FPT->getResultType();
318 QualType ThisType = MD->getThisType(getContext());
319
320 FunctionArgList FunctionArgs;
321
322 // FIXME: It would be nice if more of this code could be shared with
323 // CodeGenFunction::GenerateCode.
324
325 // Create the implicit 'this' parameter declaration.
John McCall4c40d982010-08-31 07:33:07 +0000326 CurGD = GD;
327 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
Anders Carlsson519c3282010-03-24 00:39:18 +0000328
329 // Add the rest of the parameters.
330 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
331 E = MD->param_end(); I != E; ++I) {
332 ParmVarDecl *Param = *I;
333
John McCalld26bc762011-03-09 04:27:21 +0000334 FunctionArgs.push_back(Param);
Anders Carlsson519c3282010-03-24 00:39:18 +0000335 }
Alexey Samsonov34b41f82012-10-25 10:18:50 +0000336
337 // Initialize debug info if needed.
338 maybeInitializeDebugInfo();
339
John McCalld26bc762011-03-09 04:27:21 +0000340 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
341 SourceLocation());
Anders Carlsson519c3282010-03-24 00:39:18 +0000342
John McCall4c40d982010-08-31 07:33:07 +0000343 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
Eli Friedmancec5ebd2012-02-11 02:57:39 +0000344 CXXThisValue = CXXABIThisValue;
John McCall4c40d982010-08-31 07:33:07 +0000345
Anders Carlsson519c3282010-03-24 00:39:18 +0000346 // Adjust the 'this' pointer if necessary.
347 llvm::Value *AdjustedThisPtr =
348 PerformTypeAdjustment(*this, LoadCXXThis(),
349 Thunk.This.NonVirtual,
Eli Friedman82bad6b2012-09-14 01:45:09 +0000350 Thunk.This.VCallOffsetOffset,
351 /*IsReturnAdjustment*/false);
Anders Carlsson519c3282010-03-24 00:39:18 +0000352
353 CallArgList CallArgs;
354
355 // Add our adjusted 'this' pointer.
Eli Friedman04c9a492011-05-02 17:57:46 +0000356 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
Anders Carlsson519c3282010-03-24 00:39:18 +0000357
358 // Add the rest of the parameters.
359 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
360 E = MD->param_end(); I != E; ++I) {
John McCall413ebdb2011-03-11 20:59:21 +0000361 ParmVarDecl *param = *I;
362 EmitDelegateCallArg(CallArgs, param);
Anders Carlsson519c3282010-03-24 00:39:18 +0000363 }
364
365 // Get our callee.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000366 llvm::Type *Ty =
John McCallde5d3c72012-02-17 03:33:10 +0000367 CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
Anders Carlsson84c49e42011-02-06 17:15:43 +0000368 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson519c3282010-03-24 00:39:18 +0000369
John McCalld26bc762011-03-09 04:27:21 +0000370#ifndef NDEBUG
John McCall0f3d0972012-07-07 06:41:13 +0000371 const CGFunctionInfo &CallFnInfo =
372 CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
John McCallde5d3c72012-02-17 03:33:10 +0000373 RequiredArgs::forPrototypePlus(FPT, 1));
John McCall311b4422011-03-09 07:12:35 +0000374 assert(CallFnInfo.getRegParm() == FnInfo.getRegParm() &&
375 CallFnInfo.isNoReturn() == FnInfo.isNoReturn() &&
376 CallFnInfo.getCallingConvention() == FnInfo.getCallingConvention());
John McCall0f3d0972012-07-07 06:41:13 +0000377 assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
378 similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
John McCall311b4422011-03-09 07:12:35 +0000379 FnInfo.getReturnInfo(), FnInfo.getReturnType()));
380 assert(CallFnInfo.arg_size() == FnInfo.arg_size());
381 for (unsigned i = 0, e = FnInfo.arg_size(); i != e; ++i)
382 assert(similar(CallFnInfo.arg_begin()[i].info,
383 CallFnInfo.arg_begin()[i].type,
384 FnInfo.arg_begin()[i].info, FnInfo.arg_begin()[i].type));
John McCalld26bc762011-03-09 04:27:21 +0000385#endif
Anders Carlsson519c3282010-03-24 00:39:18 +0000386
Douglas Gregorcb359df2010-05-20 05:54:35 +0000387 // Determine whether we have a return value slot to use.
388 ReturnValueSlot Slot;
389 if (!ResultType->isVoidType() &&
390 FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
391 hasAggregateLLVMType(CurFnInfo->getReturnType()))
392 Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
393
Anders Carlsson519c3282010-03-24 00:39:18 +0000394 // Now emit our call.
Douglas Gregorcb359df2010-05-20 05:54:35 +0000395 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
Anders Carlsson519c3282010-03-24 00:39:18 +0000396
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000397 if (!Thunk.Return.isEmpty())
398 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
Anders Carlsson519c3282010-03-24 00:39:18 +0000399
Douglas Gregorcb359df2010-05-20 05:54:35 +0000400 if (!ResultType->isVoidType() && Slot.isNull())
John McCalld16c2cf2011-02-08 08:22:06 +0000401 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
Anders Carlsson519c3282010-03-24 00:39:18 +0000402
John McCallbd9b65a2012-07-31 00:33:55 +0000403 // Disable the final ARC autorelease.
404 AutoreleaseResult = false;
405
Anders Carlsson519c3282010-03-24 00:39:18 +0000406 FinishFunction();
407
Anders Carlsson519c3282010-03-24 00:39:18 +0000408 // Set the right linkage.
John McCall8b242332010-05-25 04:30:21 +0000409 CGM.setFunctionLinkage(MD, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +0000410
411 // Set the right visibility.
John McCall65005532010-08-04 23:46:35 +0000412 setThunkVisibility(CGM, MD, Thunk, Fn);
Anders Carlsson519c3282010-03-24 00:39:18 +0000413}
414
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000415void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
416 bool UseAvailableExternallyLinkage)
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000417{
John McCallde5d3c72012-02-17 03:33:10 +0000418 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
John McCalld26bc762011-03-09 04:27:21 +0000419
420 // FIXME: re-use FnInfo in this computation.
Anders Carlsson84c49e42011-02-06 17:15:43 +0000421 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson19879c92010-03-23 17:17:29 +0000422
Anders Carlsson7986ad52010-03-23 18:18:41 +0000423 // Strip off a bitcast if we got one back.
Anders Carlsson13d68982010-03-24 00:35:44 +0000424 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson7986ad52010-03-23 18:18:41 +0000425 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson13d68982010-03-24 00:35:44 +0000426 Entry = CE->getOperand(0);
Anders Carlsson7986ad52010-03-23 18:18:41 +0000427 }
428
Anders Carlsson7986ad52010-03-23 18:18:41 +0000429 // There's already a declaration with the same name, check if it has the same
430 // type or if we need to replace it.
Anders Carlsson13d68982010-03-24 00:35:44 +0000431 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
John McCall4c40d982010-08-31 07:33:07 +0000432 CGM.getTypes().GetFunctionTypeForVTable(GD)) {
Anders Carlsson13d68982010-03-24 00:35:44 +0000433 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson7986ad52010-03-23 18:18:41 +0000434
435 // If the types mismatch then we have to rewrite the definition.
436 assert(OldThunkFn->isDeclaration() &&
437 "Shouldn't replace non-declaration");
438
439 // Remove the name from the old thunk function and get a new thunk.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000440 OldThunkFn->setName(StringRef());
Anders Carlsson84c49e42011-02-06 17:15:43 +0000441 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +0000442
443 // If needed, replace the old thunk with a bitcast.
444 if (!OldThunkFn->use_empty()) {
445 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson13d68982010-03-24 00:35:44 +0000446 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson7986ad52010-03-23 18:18:41 +0000447 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
448 }
449
450 // Remove the old thunk.
451 OldThunkFn->eraseFromParent();
452 }
Anders Carlsson519c3282010-03-24 00:39:18 +0000453
Anders Carlsson519c3282010-03-24 00:39:18 +0000454 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000455
456 if (!ThunkFn->isDeclaration()) {
457 if (UseAvailableExternallyLinkage) {
458 // There is already a thunk emitted for this function, do nothing.
459 return;
460 }
461
Anders Carlsson22df7b12011-02-06 20:09:44 +0000462 // If a function has a body, it should have available_externally linkage.
463 assert(ThunkFn->hasAvailableExternallyLinkage() &&
464 "Function should have available_externally linkage!");
465
466 // Change the linkage.
467 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
468 return;
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000469 }
470
Rafael Espindola022301b2012-09-21 20:39:32 +0000471 CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
472
Eli Friedman7dcdf5b2011-05-06 17:27:27 +0000473 if (ThunkFn->isVarArg()) {
474 // Varargs thunks are special; we can't just generate a call because
475 // we can't copy the varargs. Our implementation is rather
476 // expensive/sucky at the moment, so don't generate the thunk unless
477 // we have to.
478 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
479 if (!UseAvailableExternallyLinkage)
480 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
481 } else {
482 // Normal thunk body generation.
483 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
484 }
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000485
486 if (UseAvailableExternallyLinkage)
487 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
488}
489
490void CodeGenVTables::MaybeEmitThunkAvailableExternally(GlobalDecl GD,
491 const ThunkInfo &Thunk) {
492 // We only want to do this when building with optimizations.
493 if (!CGM.getCodeGenOpts().OptimizationLevel)
494 return;
495
496 // We can't emit thunks for member functions with incomplete types.
497 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Chris Lattnerf742eb02011-07-10 00:18:59 +0000498 if (!CGM.getTypes().isFuncTypeConvertible(
499 cast<FunctionType>(MD->getType().getTypePtr())))
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000500 return;
501
502 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000503}
504
Anders Carlssonee5ab9f2010-03-23 04:59:02 +0000505void CodeGenVTables::EmitThunks(GlobalDecl GD)
506{
Anders Carlssonfbf6ed42010-03-23 16:36:50 +0000507 const CXXMethodDecl *MD =
508 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
509
510 // We don't need to generate thunks for the base destructor.
511 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
512 return;
513
Peter Collingbourne84fcc482011-09-26 01:56:41 +0000514 const VTableContext::ThunkInfoVectorTy *ThunkInfoVector =
515 VTContext.getThunkInfo(MD);
516 if (!ThunkInfoVector)
Anders Carlssonccd83d72010-03-24 16:42:11 +0000517 return;
Anders Carlssonccd83d72010-03-24 16:42:11 +0000518
Peter Collingbourne84fcc482011-09-26 01:56:41 +0000519 for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
520 EmitThunk(GD, (*ThunkInfoVector)[I],
521 /*UseAvailableExternallyLinkage=*/false);
Anders Carlssonee5ab9f2010-03-23 04:59:02 +0000522}
523
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000524llvm::Constant *
525CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000526 const VTableComponent *Components,
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000527 unsigned NumComponents,
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000528 const VTableLayout::VTableThunkTy *VTableThunks,
529 unsigned NumVTableThunks) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000530 SmallVector<llvm::Constant *, 64> Inits;
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000531
Chris Lattner8b418682012-02-07 00:39:47 +0000532 llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000533
Chris Lattner2acc6e32011-07-18 04:24:23 +0000534 llvm::Type *PtrDiffTy =
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000535 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
536
537 QualType ClassType = CGM.getContext().getTagDeclType(RD);
538 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
539
540 unsigned NextVTableThunkIndex = 0;
541
David Blaikie2eb9a952012-10-16 22:56:05 +0000542 llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0;
Anders Carlsson67d568a2010-03-29 05:40:50 +0000543
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000544 for (unsigned I = 0; I != NumComponents; ++I) {
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000545 VTableComponent Component = Components[I];
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000546
547 llvm::Constant *Init = 0;
548
549 switch (Component.getKind()) {
Anders Carlsson94464812010-04-10 19:13:06 +0000550 case VTableComponent::CK_VCallOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000551 Init = llvm::ConstantInt::get(PtrDiffTy,
552 Component.getVCallOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000553 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
554 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000555 case VTableComponent::CK_VBaseOffset:
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000556 Init = llvm::ConstantInt::get(PtrDiffTy,
557 Component.getVBaseOffset().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000558 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
559 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000560 case VTableComponent::CK_OffsetToTop:
Ken Dyckc40a3fd2011-04-02 01:14:48 +0000561 Init = llvm::ConstantInt::get(PtrDiffTy,
562 Component.getOffsetToTop().getQuantity());
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000563 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
564 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000565 case VTableComponent::CK_RTTI:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000566 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
567 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000568 case VTableComponent::CK_FunctionPointer:
569 case VTableComponent::CK_CompleteDtorPointer:
570 case VTableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000571 GlobalDecl GD;
572
573 // Get the right global decl.
574 switch (Component.getKind()) {
575 default:
576 llvm_unreachable("Unexpected vtable component kind");
Anders Carlsson94464812010-04-10 19:13:06 +0000577 case VTableComponent::CK_FunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000578 GD = Component.getFunctionDecl();
579 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000580 case VTableComponent::CK_CompleteDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000581 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
582 break;
Anders Carlsson94464812010-04-10 19:13:06 +0000583 case VTableComponent::CK_DeletingDtorPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000584 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
585 break;
586 }
587
Anders Carlsson67d568a2010-03-29 05:40:50 +0000588 if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
589 // We have a pure virtual member function.
Joao Matose9af3e62012-07-17 19:17:58 +0000590 if (!PureVirtualFn) {
Eli Friedmancf15f172012-09-14 01:19:01 +0000591 llvm::FunctionType *Ty =
592 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
593 StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
594 PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
595 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
Joao Matose9af3e62012-07-17 19:17:58 +0000596 CGM.Int8PtrTy);
Anders Carlsson67d568a2010-03-29 05:40:50 +0000597 }
Anders Carlsson67d568a2010-03-29 05:40:50 +0000598 Init = PureVirtualFn;
David Blaikie2eb9a952012-10-16 22:56:05 +0000599 } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
600 if (!DeletedVirtualFn) {
601 llvm::FunctionType *Ty =
602 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
603 StringRef DeletedCallName =
604 CGM.getCXXABI().GetDeletedVirtualCallName();
605 DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
606 DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
607 CGM.Int8PtrTy);
608 }
609 Init = DeletedVirtualFn;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000610 } else {
Anders Carlsson67d568a2010-03-29 05:40:50 +0000611 // Check if we should use a thunk.
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000612 if (NextVTableThunkIndex < NumVTableThunks &&
Anders Carlsson67d568a2010-03-29 05:40:50 +0000613 VTableThunks[NextVTableThunkIndex].first == I) {
614 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000615
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000616 MaybeEmitThunkAvailableExternally(GD, Thunk);
Benjamin Kramerfce80092012-03-20 20:18:13 +0000617 Init = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson14e82fd2011-02-06 18:31:40 +0000618
Anders Carlsson67d568a2010-03-29 05:40:50 +0000619 NextVTableThunkIndex++;
620 } else {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000621 llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
Anders Carlsson67d568a2010-03-29 05:40:50 +0000622
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000623 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
Anders Carlsson67d568a2010-03-29 05:40:50 +0000624 }
625
626 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000627 }
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000628 break;
629 }
630
Anders Carlsson94464812010-04-10 19:13:06 +0000631 case VTableComponent::CK_UnusedFunctionPointer:
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000632 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
633 break;
634 };
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000635
636 Inits.push_back(Init);
637 }
638
639 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
Jay Foad97357602011-06-22 09:24:39 +0000640 return llvm::ConstantArray::get(ArrayType, Inits);
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000641}
642
Anders Carlsson9dc338a2010-03-30 03:35:35 +0000643llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Peter Collingbournebf1c5ae2011-09-26 01:56:36 +0000644 llvm::GlobalVariable *&VTable = VTables[RD];
645 if (VTable)
646 return VTable;
647
648 // We may need to generate a definition for this vtable.
649 if (ShouldEmitVTableInThisTU(RD))
650 CGM.DeferredVTables.push_back(RD);
651
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000652 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000653 llvm::raw_svector_ostream Out(OutName);
654 CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, Out);
655 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000656 StringRef Name = OutName.str();
Mike Stump85615df2009-11-19 04:04:36 +0000657
Anders Carlssonccd83d72010-03-24 16:42:11 +0000658 llvm::ArrayType *ArrayType =
Chris Lattner8b418682012-02-07 00:39:47 +0000659 llvm::ArrayType::get(CGM.Int8PtrTy,
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000660 VTContext.getVTableLayout(RD).getNumVTableComponents());
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000661
Peter Collingbournebf1c5ae2011-09-26 01:56:36 +0000662 VTable =
Anders Carlsson96eaf292011-01-29 18:25:07 +0000663 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
664 llvm::GlobalValue::ExternalLinkage);
Peter Collingbournebf1c5ae2011-09-26 01:56:36 +0000665 VTable->setUnnamedAddr(true);
666 return VTable;
Mike Stump380dd752009-11-10 07:44:33 +0000667}
Mike Stump8cfcb522009-11-11 20:26:26 +0000668
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000669void
670CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
671 llvm::GlobalVariable::LinkageTypes Linkage,
672 const CXXRecordDecl *RD) {
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000673 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD);
674
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000675 // Create and set the initializer.
676 llvm::Constant *Init =
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000677 CreateVTableInitializer(RD,
678 VTLayout.vtable_component_begin(),
679 VTLayout.getNumVTableComponents(),
680 VTLayout.vtable_thunk_begin(),
681 VTLayout.getNumVTableThunks());
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000682 VTable->setInitializer(Init);
Anders Carlsson67d568a2010-03-29 05:40:50 +0000683
684 // Set the correct linkage.
685 VTable->setLinkage(Linkage);
Douglas Gregorc66bcfd2010-06-14 23:41:45 +0000686
687 // Set the right visibility.
Anders Carlssonfa2e99f2011-01-29 20:24:48 +0000688 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000689}
690
Anders Carlssonff143f82010-03-25 00:35:49 +0000691llvm::GlobalVariable *
692CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000693 const BaseSubobject &Base,
694 bool BaseIsVirtual,
John McCallbda0d6b2011-03-27 09:00:25 +0000695 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000696 VTableAddressPointsMapTy& AddressPoints) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000697 OwningPtr<VTableLayout> VTLayout(
Peter Collingbourneab172b52011-09-26 01:57:04 +0000698 VTContext.createConstructionVTableLayout(Base.getBase(),
699 Base.getBaseOffset(),
700 BaseIsVirtual, RD));
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000701
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000702 // Add the address points.
Peter Collingbourneab172b52011-09-26 01:57:04 +0000703 AddressPoints = VTLayout->getAddressPoints();
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000704
705 // Get the mangled construction vtable name.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000706 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000707 llvm::raw_svector_ostream Out(OutName);
John McCall4c40d982010-08-31 07:33:07 +0000708 CGM.getCXXABI().getMangleContext().
Ken Dyck4230d522011-03-24 01:21:01 +0000709 mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(), Base.getBase(),
710 Out);
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000711 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000712 StringRef Name = OutName.str();
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000713
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000714 llvm::ArrayType *ArrayType =
Chris Lattner8b418682012-02-07 00:39:47 +0000715 llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000716
717 // Create the variable that will hold the construction vtable.
718 llvm::GlobalVariable *VTable =
John McCallbda0d6b2011-03-27 09:00:25 +0000719 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
720 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
721
722 // V-tables are always unnamed_addr.
723 VTable->setUnnamedAddr(true);
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000724
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000725 // Create and set the initializer.
726 llvm::Constant *Init =
727 CreateVTableInitializer(Base.getBase(),
Peter Collingbourneab172b52011-09-26 01:57:04 +0000728 VTLayout->vtable_component_begin(),
729 VTLayout->getNumVTableComponents(),
730 VTLayout->vtable_thunk_begin(),
731 VTLayout->getNumVTableThunks());
Anders Carlsson0d1407e2010-03-25 15:26:28 +0000732 VTable->setInitializer(Init);
733
Anders Carlssonff143f82010-03-25 00:35:49 +0000734 return VTable;
735}
736
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000737void
738CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
739 const CXXRecordDecl *RD) {
Peter Collingbournebf1c5ae2011-09-26 01:56:36 +0000740 llvm::GlobalVariable *VTable = GetAddrOfVTable(RD);
741 if (VTable->hasInitializer())
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000742 return;
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000743
Anders Carlsson9dc338a2010-03-30 03:35:35 +0000744 EmitVTableDefinition(VTable, Linkage, RD);
745
Anders Carlsson1cbce122011-01-29 19:16:51 +0000746 if (RD->getNumVBases()) {
747 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
748 EmitVTTDefinition(VTT, Linkage, RD);
749 }
Douglas Gregor1e201b42010-04-08 15:52:03 +0000750
751 // If this is the magic class __cxxabiv1::__fundamental_type_info,
752 // we will emit the typeinfo for the fundamental types. This is the
753 // same behaviour as GCC.
754 const DeclContext *DC = RD->getDeclContext();
755 if (RD->getIdentifier() &&
756 RD->getIdentifier()->isStr("__fundamental_type_info") &&
757 isa<NamespaceDecl>(DC) &&
758 cast<NamespaceDecl>(DC)->getIdentifier() &&
759 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
760 DC->getParent()->isTranslationUnit())
761 CGM.EmitFundamentalRTTIDescriptors();
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000762}