blob: c4c52aac034af3e39b04517cc83fbf25dac2847e [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
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.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Anders Carlssonb9de2c52009-05-11 23:37:08 +000029
Anders Carlsson0f294632009-05-27 04:18:27 +000030
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000031void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +000032 EmitGlobal(GlobalDecl(D, Ctor_Complete));
33 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000034}
Anders Carlsson363c1842009-04-16 23:57:24 +000035
Mike Stump1eb44332009-09-09 15:08:12 +000036void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000037 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +000038
Anders Carlsson27ae5362009-04-17 01:58:57 +000039 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +000040
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000041 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson27ae5362009-04-17 01:58:57 +000043 SetFunctionDefinitionAttributes(D, Fn);
44 SetLLVMFunctionAttributesForDefinition(D, Fn);
45}
46
Anders Carlsson363c1842009-04-16 23:57:24 +000047llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +000048CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +000049 CXXCtorType Type) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +000050 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +000051 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +000052 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +000053 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +000054
Anders Carlsson363c1842009-04-16 23:57:24 +000055 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +000056 return cast<llvm::Function>(
57 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +000058}
Anders Carlsson27ae5362009-04-17 01:58:57 +000059
Mike Stump1eb44332009-09-09 15:08:12 +000060const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000061 CXXCtorType Type) {
62 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000063 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +000064
Anders Carlsson27ae5362009-04-17 01:58:57 +000065 Name += '\0';
66 return UniqueMangledName(Name.begin(), Name.end());
67}
68
69void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanea9a2082009-11-14 04:19:37 +000070 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +000071 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
72 EmitGlobal(GlobalDecl(D, Dtor_Complete));
73 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +000074}
75
Mike Stump1eb44332009-09-09 15:08:12 +000076void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000077 CXXDtorType Type) {
78 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000080 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +000081
Anders Carlsson27ae5362009-04-17 01:58:57 +000082 SetFunctionDefinitionAttributes(D, Fn);
83 SetLLVMFunctionAttributesForDefinition(D, Fn);
84}
85
86llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +000087CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000088 CXXDtorType Type) {
89 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +000090 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +000091
Anders Carlsson27ae5362009-04-17 01:58:57 +000092 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +000093 return cast<llvm::Function>(
94 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +000095}
96
Mike Stump1eb44332009-09-09 15:08:12 +000097const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000098 CXXDtorType Type) {
99 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000100 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Anders Carlsson27ae5362009-04-17 01:58:57 +0000102 Name += '\0';
103 return UniqueMangledName(Name.begin(), Name.end());
104}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000105
Anders Carlssona94822e2009-11-26 02:32:05 +0000106llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000107CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
Anders Carlssona94822e2009-11-26 02:32:05 +0000108 bool Extern,
109 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000110 return GenerateCovariantThunk(Fn, GD, Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000111 CovariantThunkAdjustment(ThisAdjustment,
112 ThunkAdjustment()));
Mike Stumped032eb2009-09-04 18:27:16 +0000113}
114
Anders Carlsson7622cd32009-11-26 03:09:37 +0000115llvm::Value *
116CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
117 const ThunkAdjustment &Adjustment) {
118 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
119
Mike Stumpc902d222009-11-03 16:59:27 +0000120 const llvm::Type *OrigTy = V->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000121 if (Adjustment.NonVirtual) {
Mike Stumpc902d222009-11-03 16:59:27 +0000122 // Do the non-virtual adjustment
Anders Carlsson7622cd32009-11-26 03:09:37 +0000123 V = Builder.CreateBitCast(V, Int8PtrTy);
124 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
Mike Stumpc902d222009-11-03 16:59:27 +0000125 V = Builder.CreateBitCast(V, OrigTy);
126 }
Anders Carlsson7622cd32009-11-26 03:09:37 +0000127
128 if (!Adjustment.Virtual)
129 return V;
130
131 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
132 "vtable entry unaligned");
133
134 // Do the virtual this adjustment
135 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
136 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
137
138 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
139 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
140 V = Builder.CreateLoad(V, "vtable");
141
142 llvm::Value *VTablePtr = V;
143 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
144 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
145 V = Builder.CreateLoad(V);
146 V = Builder.CreateGEP(ThisVal, V);
147
148 return Builder.CreateBitCast(V, OrigTy);
Mike Stumpc902d222009-11-03 16:59:27 +0000149}
150
Anders Carlsson7622cd32009-11-26 03:09:37 +0000151llvm::Constant *
152CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
Eli Friedman35c98cc2009-12-03 04:27:05 +0000153 GlobalDecl GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000154 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000155 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
John McCall04a67a62010-02-05 21:31:56 +0000156 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
157 QualType ResultType = FPT->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000158
159 FunctionArgList Args;
160 ImplicitParamDecl *ThisDecl =
161 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
162 MD->getThisType(getContext()));
163 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
164 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
165 e = MD->param_end();
166 i != e; ++i) {
167 ParmVarDecl *D = *i;
168 Args.push_back(std::make_pair(D, D->getType()));
169 }
170 IdentifierInfo *II
171 = &CGM.getContext().Idents.get("__thunk_named_foo_");
172 FunctionDecl *FD = FunctionDecl::Create(getContext(),
173 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000174 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000175 Extern
176 ? FunctionDecl::Extern
177 : FunctionDecl::Static,
178 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000179 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
180
181 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000182 const llvm::Type *Ty =
183 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
184 FPT->isVariadic());
Eli Friedman35c98cc2009-12-03 04:27:05 +0000185 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
Mike Stump919d5e52009-12-03 03:47:56 +0000186
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000187 CallArgList CallArgs;
188
Anders Carlsson7622cd32009-11-26 03:09:37 +0000189 bool ShouldAdjustReturnPointer = true;
Mike Stump736529e2009-11-03 02:12:59 +0000190 QualType ArgType = MD->getThisType(getContext());
191 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Anders Carlsson7622cd32009-11-26 03:09:37 +0000192 if (!Adjustment.ThisAdjustment.isEmpty()) {
Mike Stumpc902d222009-11-03 16:59:27 +0000193 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000194 const llvm::Type *OrigTy = Callee->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000195 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
196
197 if (!Adjustment.ReturnAdjustment.isEmpty()) {
198 const CovariantThunkAdjustment &ReturnAdjustment =
199 CovariantThunkAdjustment(ThunkAdjustment(),
200 Adjustment.ReturnAdjustment);
201
Mike Stump919d5e52009-12-03 03:47:56 +0000202 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000203
Mike Stumpd0fe5362009-11-04 00:53:51 +0000204 Callee = Builder.CreateBitCast(Callee, OrigTy);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000205 ShouldAdjustReturnPointer = false;
Mike Stumpd0fe5362009-11-04 00:53:51 +0000206 }
207 }
208
Mike Stump736529e2009-11-03 02:12:59 +0000209 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
210
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000211 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
212 e = MD->param_end();
213 i != e; ++i) {
214 ParmVarDecl *D = *i;
215 QualType ArgType = D->getType();
216
217 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
Eli Friedman6804fa22009-12-03 04:49:52 +0000218 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(),
219 SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000220 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
221 }
222
John McCall04a67a62010-02-05 21:31:56 +0000223 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
224 FPT->getCallConv(),
225 FPT->getNoReturnAttr()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000226 Callee, ReturnValueSlot(), CallArgs, MD);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000227 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
Mike Stump03e777e2009-11-05 06:32:02 +0000228 bool CanBeZero = !(ResultType->isReferenceType()
229 // FIXME: attr nonnull can't be zero either
230 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000231 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000232 if (CanBeZero) {
233 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
234 llvm::BasicBlock *ZeroBlock = createBasicBlock();
235 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000236
Mike Stump03e777e2009-11-05 06:32:02 +0000237 const llvm::Type *Ty = RV.getScalarVal()->getType();
238 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
239 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
240 NonZeroBlock, ZeroBlock);
241 EmitBlock(NonZeroBlock);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000242 llvm::Value *NZ =
243 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
Mike Stump03e777e2009-11-05 06:32:02 +0000244 EmitBranch(ContBlock);
245 EmitBlock(ZeroBlock);
246 llvm::Value *Z = RV.getScalarVal();
247 EmitBlock(ContBlock);
248 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
249 RVOrZero->reserveOperandSpace(2);
250 RVOrZero->addIncoming(NZ, NonZeroBlock);
251 RVOrZero->addIncoming(Z, ZeroBlock);
252 RV = RValue::get(RVOrZero);
253 } else
Anders Carlsson7622cd32009-11-26 03:09:37 +0000254 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
255 Adjustment.ReturnAdjustment));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000256 }
257
Mike Stumpf49ed942009-11-02 23:47:45 +0000258 if (!ResultType->isVoidType())
259 EmitReturnOfRValue(RV, ResultType);
260
Mike Stump6e319f62009-09-11 23:25:56 +0000261 FinishFunction();
262 return Fn;
263}
264
Anders Carlssona94822e2009-11-26 02:32:05 +0000265llvm::Constant *
Eli Friedman72649ed2009-12-06 22:01:30 +0000266CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
267 const ThunkAdjustment &ThisAdjustment) {
268 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
269
270 // Compute mangled name
271 llvm::SmallString<256> OutName;
272 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
273 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment,
274 OutName);
275 else
276 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
277 OutName += '\0';
278 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
279
280 // Get function for mangled name
281 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
282 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
283}
284
285llvm::Constant *
286CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD,
287 const CovariantThunkAdjustment &Adjustment) {
288 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
289
290 // Compute mangled name
291 llvm::SmallString<256> OutName;
292 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
293 OutName += '\0';
294 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
295
296 // Get function for mangled name
297 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
298 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
299}
300
301void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000302 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD);
303 if (!AdjPtr)
304 return;
305 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr;
Eli Friedman72649ed2009-12-06 22:01:30 +0000306 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000307 for (unsigned i = 0; i < Adj.size(); i++) {
308 GlobalDecl OGD = Adj[i].first;
309 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl());
Eli Friedman72649ed2009-12-06 22:01:30 +0000310 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
311 CanQualType oret = getContext().getCanonicalType(nc_oret);
312 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
313 CanQualType ret = getContext().getCanonicalType(nc_ret);
314 ThunkAdjustment ReturnAdjustment;
315 if (oret != ret) {
316 QualType qD = nc_ret->getPointeeType();
317 QualType qB = nc_oret->getPointeeType();
318 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
319 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
320 ReturnAdjustment = ComputeThunkAdjustment(D, B);
321 }
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000322 ThunkAdjustment ThisAdjustment = Adj[i].second;
Eli Friedman72649ed2009-12-06 22:01:30 +0000323 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace();
324 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
325 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment);
326 llvm::Constant *FnConst;
327 if (!ReturnAdjustment.isEmpty())
328 FnConst = GetAddrOfCovariantThunk(GD, CoAdj);
329 else
330 FnConst = GetAddrOfThunk(GD, ThisAdjustment);
331 if (!isa<llvm::Function>(FnConst)) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000332 llvm::Constant *SubExpr =
333 cast<llvm::ConstantExpr>(FnConst)->getOperand(0);
334 llvm::Function *OldFn = cast<llvm::Function>(SubExpr);
335 std::string Name = OldFn->getNameStr();
336 GlobalDeclMap.erase(UniqueMangledName(Name.data(),
337 Name.data() + Name.size() + 1));
338 llvm::Constant *NewFnConst;
339 if (!ReturnAdjustment.isEmpty())
340 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj);
341 else
342 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment);
343 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst);
344 NewFn->takeName(OldFn);
345 llvm::Constant *NewPtrForOldDecl =
346 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType());
347 OldFn->replaceAllUsesWith(NewPtrForOldDecl);
348 OldFn->eraseFromParent();
349 FnConst = NewFn;
Eli Friedman72649ed2009-12-06 22:01:30 +0000350 }
351 llvm::Function *Fn = cast<llvm::Function>(FnConst);
352 if (Fn->isDeclaration()) {
353 llvm::GlobalVariable::LinkageTypes linktype;
354 linktype = llvm::GlobalValue::WeakAnyLinkage;
355 if (!Extern)
356 linktype = llvm::GlobalValue::InternalLinkage;
357 Fn->setLinkage(linktype);
358 if (!Features.Exceptions && !Features.ObjCNonFragileABI)
359 Fn->addFnAttr(llvm::Attribute::NoUnwind);
360 Fn->setAlignment(2);
361 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj);
362 }
363 }
Eli Friedman72649ed2009-12-06 22:01:30 +0000364 }
365}
366
367llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000368CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
Anders Carlssona94822e2009-11-26 02:32:05 +0000369 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000370 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stumped032eb2009-09-04 18:27:16 +0000371 llvm::SmallString<256> OutName;
Mike Stump919d5e52009-12-03 03:47:56 +0000372 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
373 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
374 OutName);
375 } else
376 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
Anders Carlssona94822e2009-11-26 02:32:05 +0000377
Mike Stumped032eb2009-09-04 18:27:16 +0000378 llvm::GlobalVariable::LinkageTypes linktype;
379 linktype = llvm::GlobalValue::WeakAnyLinkage;
380 if (!Extern)
381 linktype = llvm::GlobalValue::InternalLinkage;
382 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000383 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000384 const llvm::FunctionType *FTy =
385 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
386 FPT->isVariadic());
387
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000388 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stumped032eb2009-09-04 18:27:16 +0000389 &getModule());
Mike Stump919d5e52009-12-03 03:47:56 +0000390 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
Mike Stumped032eb2009-09-04 18:27:16 +0000391 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
392 return m;
393}
394
Anders Carlsson7622cd32009-11-26 03:09:37 +0000395llvm::Constant *
Mike Stump919d5e52009-12-03 03:47:56 +0000396CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000397 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000398 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump6e319f62009-09-11 23:25:56 +0000399 llvm::SmallString<256> OutName;
Anders Carlsson7622cd32009-11-26 03:09:37 +0000400 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
Mike Stump6e319f62009-09-11 23:25:56 +0000401 llvm::GlobalVariable::LinkageTypes linktype;
402 linktype = llvm::GlobalValue::WeakAnyLinkage;
403 if (!Extern)
404 linktype = llvm::GlobalValue::InternalLinkage;
405 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000406 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000407 const llvm::FunctionType *FTy =
408 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
409 FPT->isVariadic());
410
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000411 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump6e319f62009-09-11 23:25:56 +0000412 &getModule());
Anders Carlsson7622cd32009-11-26 03:09:37 +0000413 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
Mike Stump6e319f62009-09-11 23:25:56 +0000414 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
415 return m;
416}
417
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000418static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000419 llvm::Value *This, const llvm::Type *Ty) {
420 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000421
Anders Carlsson566abee2009-11-13 04:45:41 +0000422 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
423 Vtable = CGF.Builder.CreateLoad(Vtable);
424
425 llvm::Value *VFuncPtr =
426 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
427 return CGF.Builder.CreateLoad(VFuncPtr);
428}
429
430llvm::Value *
431CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
432 const llvm::Type *Ty) {
433 MD = MD->getCanonicalDecl();
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000434 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000435
436 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
437}
438
439llvm::Value *
440CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
441 llvm::Value *&This, const llvm::Type *Ty) {
442 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000443 uint64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +0000444 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000445
446 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000447}