blob: 28c4c6b4b57b37ee20b83b1efd9335a189ac6ead [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 Carlsson5f4307b2009-04-14 16:58:56 +000031llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +000032 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +000033 "Must be in a C++ member function decl to load 'this'");
34 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
35 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +000036
Anders Carlsson5f4307b2009-04-14 16:58:56 +000037 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +000038 // ans: See how CodeGenFunction::LoadObjCSelf() uses
39 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +000040 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
41}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000042
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000043void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +000044 EmitGlobal(GlobalDecl(D, Ctor_Complete));
45 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000046}
Anders Carlsson363c1842009-04-16 23:57:24 +000047
Mike Stump1eb44332009-09-09 15:08:12 +000048void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000049 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +000050
Anders Carlsson27ae5362009-04-17 01:58:57 +000051 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000053 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +000054
Anders Carlsson27ae5362009-04-17 01:58:57 +000055 SetFunctionDefinitionAttributes(D, Fn);
56 SetLLVMFunctionAttributesForDefinition(D, Fn);
57}
58
Anders Carlsson363c1842009-04-16 23:57:24 +000059llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +000060CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +000061 CXXCtorType Type) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +000062 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +000063 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +000064 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +000065 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +000066
Anders Carlsson363c1842009-04-16 23:57:24 +000067 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +000068 return cast<llvm::Function>(
69 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +000070}
Anders Carlsson27ae5362009-04-17 01:58:57 +000071
Mike Stump1eb44332009-09-09 15:08:12 +000072const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000073 CXXCtorType Type) {
74 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000075 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Anders Carlsson27ae5362009-04-17 01:58:57 +000077 Name += '\0';
78 return UniqueMangledName(Name.begin(), Name.end());
79}
80
81void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Eli Friedmanea9a2082009-11-14 04:19:37 +000082 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +000083 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
84 EmitGlobal(GlobalDecl(D, Dtor_Complete));
85 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +000086}
87
Mike Stump1eb44332009-09-09 15:08:12 +000088void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +000089 CXXDtorType Type) {
90 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +000091
Anders Carlsson0ff8baf2009-09-11 00:07:24 +000092 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +000093
Anders Carlsson27ae5362009-04-17 01:58:57 +000094 SetFunctionDefinitionAttributes(D, Fn);
95 SetLLVMFunctionAttributesForDefinition(D, Fn);
96}
97
98llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +000099CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000100 CXXDtorType Type) {
101 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000102 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Anders Carlsson27ae5362009-04-17 01:58:57 +0000104 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000105 return cast<llvm::Function>(
106 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000107}
108
Mike Stump1eb44332009-09-09 15:08:12 +0000109const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000110 CXXDtorType Type) {
111 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000112 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Anders Carlsson27ae5362009-04-17 01:58:57 +0000114 Name += '\0';
115 return UniqueMangledName(Name.begin(), Name.end());
116}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000117
Anders Carlssona94822e2009-11-26 02:32:05 +0000118llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000119CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
Anders Carlssona94822e2009-11-26 02:32:05 +0000120 bool Extern,
121 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000122 return GenerateCovariantThunk(Fn, GD, Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000123 CovariantThunkAdjustment(ThisAdjustment,
124 ThunkAdjustment()));
Mike Stumped032eb2009-09-04 18:27:16 +0000125}
126
Anders Carlsson7622cd32009-11-26 03:09:37 +0000127llvm::Value *
128CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
129 const ThunkAdjustment &Adjustment) {
130 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
131
Mike Stumpc902d222009-11-03 16:59:27 +0000132 const llvm::Type *OrigTy = V->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000133 if (Adjustment.NonVirtual) {
Mike Stumpc902d222009-11-03 16:59:27 +0000134 // Do the non-virtual adjustment
Anders Carlsson7622cd32009-11-26 03:09:37 +0000135 V = Builder.CreateBitCast(V, Int8PtrTy);
136 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
Mike Stumpc902d222009-11-03 16:59:27 +0000137 V = Builder.CreateBitCast(V, OrigTy);
138 }
Anders Carlsson7622cd32009-11-26 03:09:37 +0000139
140 if (!Adjustment.Virtual)
141 return V;
142
143 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
144 "vtable entry unaligned");
145
146 // Do the virtual this adjustment
147 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
148 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
149
150 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
151 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
152 V = Builder.CreateLoad(V, "vtable");
153
154 llvm::Value *VTablePtr = V;
155 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
156 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
157 V = Builder.CreateLoad(V);
158 V = Builder.CreateGEP(ThisVal, V);
159
160 return Builder.CreateBitCast(V, OrigTy);
Mike Stumpc902d222009-11-03 16:59:27 +0000161}
162
Anders Carlsson7622cd32009-11-26 03:09:37 +0000163llvm::Constant *
164CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
Eli Friedman35c98cc2009-12-03 04:27:05 +0000165 GlobalDecl GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000166 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000167 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
John McCall04a67a62010-02-05 21:31:56 +0000168 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
169 QualType ResultType = FPT->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000170
171 FunctionArgList Args;
172 ImplicitParamDecl *ThisDecl =
173 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
174 MD->getThisType(getContext()));
175 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
176 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
177 e = MD->param_end();
178 i != e; ++i) {
179 ParmVarDecl *D = *i;
180 Args.push_back(std::make_pair(D, D->getType()));
181 }
182 IdentifierInfo *II
183 = &CGM.getContext().Idents.get("__thunk_named_foo_");
184 FunctionDecl *FD = FunctionDecl::Create(getContext(),
185 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000186 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000187 Extern
188 ? FunctionDecl::Extern
189 : FunctionDecl::Static,
190 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000191 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
192
193 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000194 const llvm::Type *Ty =
195 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
196 FPT->isVariadic());
Eli Friedman35c98cc2009-12-03 04:27:05 +0000197 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
Mike Stump919d5e52009-12-03 03:47:56 +0000198
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000199 CallArgList CallArgs;
200
Anders Carlsson7622cd32009-11-26 03:09:37 +0000201 bool ShouldAdjustReturnPointer = true;
Mike Stump736529e2009-11-03 02:12:59 +0000202 QualType ArgType = MD->getThisType(getContext());
203 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Anders Carlsson7622cd32009-11-26 03:09:37 +0000204 if (!Adjustment.ThisAdjustment.isEmpty()) {
Mike Stumpc902d222009-11-03 16:59:27 +0000205 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000206 const llvm::Type *OrigTy = Callee->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000207 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
208
209 if (!Adjustment.ReturnAdjustment.isEmpty()) {
210 const CovariantThunkAdjustment &ReturnAdjustment =
211 CovariantThunkAdjustment(ThunkAdjustment(),
212 Adjustment.ReturnAdjustment);
213
Mike Stump919d5e52009-12-03 03:47:56 +0000214 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000215
Mike Stumpd0fe5362009-11-04 00:53:51 +0000216 Callee = Builder.CreateBitCast(Callee, OrigTy);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000217 ShouldAdjustReturnPointer = false;
Mike Stumpd0fe5362009-11-04 00:53:51 +0000218 }
219 }
220
Mike Stump736529e2009-11-03 02:12:59 +0000221 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
222
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000223 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
224 e = MD->param_end();
225 i != e; ++i) {
226 ParmVarDecl *D = *i;
227 QualType ArgType = D->getType();
228
229 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
Eli Friedman6804fa22009-12-03 04:49:52 +0000230 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(),
231 SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000232 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
233 }
234
John McCall04a67a62010-02-05 21:31:56 +0000235 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
236 FPT->getCallConv(),
237 FPT->getNoReturnAttr()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000238 Callee, ReturnValueSlot(), CallArgs, MD);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000239 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
Mike Stump03e777e2009-11-05 06:32:02 +0000240 bool CanBeZero = !(ResultType->isReferenceType()
241 // FIXME: attr nonnull can't be zero either
242 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000243 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000244 if (CanBeZero) {
245 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
246 llvm::BasicBlock *ZeroBlock = createBasicBlock();
247 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000248
Mike Stump03e777e2009-11-05 06:32:02 +0000249 const llvm::Type *Ty = RV.getScalarVal()->getType();
250 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
251 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
252 NonZeroBlock, ZeroBlock);
253 EmitBlock(NonZeroBlock);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000254 llvm::Value *NZ =
255 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
Mike Stump03e777e2009-11-05 06:32:02 +0000256 EmitBranch(ContBlock);
257 EmitBlock(ZeroBlock);
258 llvm::Value *Z = RV.getScalarVal();
259 EmitBlock(ContBlock);
260 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
261 RVOrZero->reserveOperandSpace(2);
262 RVOrZero->addIncoming(NZ, NonZeroBlock);
263 RVOrZero->addIncoming(Z, ZeroBlock);
264 RV = RValue::get(RVOrZero);
265 } else
Anders Carlsson7622cd32009-11-26 03:09:37 +0000266 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
267 Adjustment.ReturnAdjustment));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000268 }
269
Mike Stumpf49ed942009-11-02 23:47:45 +0000270 if (!ResultType->isVoidType())
271 EmitReturnOfRValue(RV, ResultType);
272
Mike Stump6e319f62009-09-11 23:25:56 +0000273 FinishFunction();
274 return Fn;
275}
276
Anders Carlssona94822e2009-11-26 02:32:05 +0000277llvm::Constant *
Eli Friedman72649ed2009-12-06 22:01:30 +0000278CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
279 const ThunkAdjustment &ThisAdjustment) {
280 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
281
282 // Compute mangled name
283 llvm::SmallString<256> OutName;
284 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
285 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment,
286 OutName);
287 else
288 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
289 OutName += '\0';
290 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
291
292 // Get function for mangled name
293 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
294 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
295}
296
297llvm::Constant *
298CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD,
299 const CovariantThunkAdjustment &Adjustment) {
300 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
301
302 // Compute mangled name
303 llvm::SmallString<256> OutName;
304 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
305 OutName += '\0';
306 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
307
308 // Get function for mangled name
309 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
310 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
311}
312
313void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000314 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD);
315 if (!AdjPtr)
316 return;
317 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr;
Eli Friedman72649ed2009-12-06 22:01:30 +0000318 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000319 for (unsigned i = 0; i < Adj.size(); i++) {
320 GlobalDecl OGD = Adj[i].first;
321 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl());
Eli Friedman72649ed2009-12-06 22:01:30 +0000322 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
323 CanQualType oret = getContext().getCanonicalType(nc_oret);
324 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
325 CanQualType ret = getContext().getCanonicalType(nc_ret);
326 ThunkAdjustment ReturnAdjustment;
327 if (oret != ret) {
328 QualType qD = nc_ret->getPointeeType();
329 QualType qB = nc_oret->getPointeeType();
330 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
331 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
332 ReturnAdjustment = ComputeThunkAdjustment(D, B);
333 }
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000334 ThunkAdjustment ThisAdjustment = Adj[i].second;
Eli Friedman72649ed2009-12-06 22:01:30 +0000335 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace();
336 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
337 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment);
338 llvm::Constant *FnConst;
339 if (!ReturnAdjustment.isEmpty())
340 FnConst = GetAddrOfCovariantThunk(GD, CoAdj);
341 else
342 FnConst = GetAddrOfThunk(GD, ThisAdjustment);
343 if (!isa<llvm::Function>(FnConst)) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000344 llvm::Constant *SubExpr =
345 cast<llvm::ConstantExpr>(FnConst)->getOperand(0);
346 llvm::Function *OldFn = cast<llvm::Function>(SubExpr);
347 std::string Name = OldFn->getNameStr();
348 GlobalDeclMap.erase(UniqueMangledName(Name.data(),
349 Name.data() + Name.size() + 1));
350 llvm::Constant *NewFnConst;
351 if (!ReturnAdjustment.isEmpty())
352 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj);
353 else
354 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment);
355 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst);
356 NewFn->takeName(OldFn);
357 llvm::Constant *NewPtrForOldDecl =
358 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType());
359 OldFn->replaceAllUsesWith(NewPtrForOldDecl);
360 OldFn->eraseFromParent();
361 FnConst = NewFn;
Eli Friedman72649ed2009-12-06 22:01:30 +0000362 }
363 llvm::Function *Fn = cast<llvm::Function>(FnConst);
364 if (Fn->isDeclaration()) {
365 llvm::GlobalVariable::LinkageTypes linktype;
366 linktype = llvm::GlobalValue::WeakAnyLinkage;
367 if (!Extern)
368 linktype = llvm::GlobalValue::InternalLinkage;
369 Fn->setLinkage(linktype);
370 if (!Features.Exceptions && !Features.ObjCNonFragileABI)
371 Fn->addFnAttr(llvm::Attribute::NoUnwind);
372 Fn->setAlignment(2);
373 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj);
374 }
375 }
Eli Friedman72649ed2009-12-06 22:01:30 +0000376 }
377}
378
379llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000380CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
Anders Carlssona94822e2009-11-26 02:32:05 +0000381 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000382 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stumped032eb2009-09-04 18:27:16 +0000383 llvm::SmallString<256> OutName;
Mike Stump919d5e52009-12-03 03:47:56 +0000384 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
385 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
386 OutName);
387 } else
388 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
Anders Carlssona94822e2009-11-26 02:32:05 +0000389
Mike Stumped032eb2009-09-04 18:27:16 +0000390 llvm::GlobalVariable::LinkageTypes linktype;
391 linktype = llvm::GlobalValue::WeakAnyLinkage;
392 if (!Extern)
393 linktype = llvm::GlobalValue::InternalLinkage;
394 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000395 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000396 const llvm::FunctionType *FTy =
397 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
398 FPT->isVariadic());
399
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000400 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stumped032eb2009-09-04 18:27:16 +0000401 &getModule());
Mike Stump919d5e52009-12-03 03:47:56 +0000402 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
Mike Stumped032eb2009-09-04 18:27:16 +0000403 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
404 return m;
405}
406
Anders Carlsson7622cd32009-11-26 03:09:37 +0000407llvm::Constant *
Mike Stump919d5e52009-12-03 03:47:56 +0000408CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000409 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000410 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump6e319f62009-09-11 23:25:56 +0000411 llvm::SmallString<256> OutName;
Anders Carlsson7622cd32009-11-26 03:09:37 +0000412 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
Mike Stump6e319f62009-09-11 23:25:56 +0000413 llvm::GlobalVariable::LinkageTypes linktype;
414 linktype = llvm::GlobalValue::WeakAnyLinkage;
415 if (!Extern)
416 linktype = llvm::GlobalValue::InternalLinkage;
417 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000418 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000419 const llvm::FunctionType *FTy =
420 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
421 FPT->isVariadic());
422
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000423 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump6e319f62009-09-11 23:25:56 +0000424 &getModule());
Anders Carlsson7622cd32009-11-26 03:09:37 +0000425 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
Mike Stump6e319f62009-09-11 23:25:56 +0000426 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
427 return m;
428}
429
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000430static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000431 llvm::Value *This, const llvm::Type *Ty) {
432 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000433
Anders Carlsson566abee2009-11-13 04:45:41 +0000434 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
435 Vtable = CGF.Builder.CreateLoad(Vtable);
436
437 llvm::Value *VFuncPtr =
438 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
439 return CGF.Builder.CreateLoad(VFuncPtr);
440}
441
442llvm::Value *
443CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
444 const llvm::Type *Ty) {
445 MD = MD->getCanonicalDecl();
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000446 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000447
448 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
449}
450
451llvm::Value *
452CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
453 llvm::Value *&This, const llvm::Type *Ty) {
454 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000455 uint64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +0000456 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000457
458 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000459}