blob: 4323f84d9633864aff4092bcbf2107ea1cce5c5a [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());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000168 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000169
170 FunctionArgList Args;
171 ImplicitParamDecl *ThisDecl =
172 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
173 MD->getThisType(getContext()));
174 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
175 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
176 e = MD->param_end();
177 i != e; ++i) {
178 ParmVarDecl *D = *i;
179 Args.push_back(std::make_pair(D, D->getType()));
180 }
181 IdentifierInfo *II
182 = &CGM.getContext().Idents.get("__thunk_named_foo_");
183 FunctionDecl *FD = FunctionDecl::Create(getContext(),
184 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000185 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000186 Extern
187 ? FunctionDecl::Extern
188 : FunctionDecl::Static,
189 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000190 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
191
192 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000193 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
194 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
Mike Stumpf49ed942009-11-02 23:47:45 +0000235 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000236 Callee, ReturnValueSlot(), CallArgs, MD);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000237 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
Mike Stump03e777e2009-11-05 06:32:02 +0000238 bool CanBeZero = !(ResultType->isReferenceType()
239 // FIXME: attr nonnull can't be zero either
240 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000241 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000242 if (CanBeZero) {
243 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
244 llvm::BasicBlock *ZeroBlock = createBasicBlock();
245 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000246
Mike Stump03e777e2009-11-05 06:32:02 +0000247 const llvm::Type *Ty = RV.getScalarVal()->getType();
248 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
249 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
250 NonZeroBlock, ZeroBlock);
251 EmitBlock(NonZeroBlock);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000252 llvm::Value *NZ =
253 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
Mike Stump03e777e2009-11-05 06:32:02 +0000254 EmitBranch(ContBlock);
255 EmitBlock(ZeroBlock);
256 llvm::Value *Z = RV.getScalarVal();
257 EmitBlock(ContBlock);
258 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
259 RVOrZero->reserveOperandSpace(2);
260 RVOrZero->addIncoming(NZ, NonZeroBlock);
261 RVOrZero->addIncoming(Z, ZeroBlock);
262 RV = RValue::get(RVOrZero);
263 } else
Anders Carlsson7622cd32009-11-26 03:09:37 +0000264 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
265 Adjustment.ReturnAdjustment));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000266 }
267
Mike Stumpf49ed942009-11-02 23:47:45 +0000268 if (!ResultType->isVoidType())
269 EmitReturnOfRValue(RV, ResultType);
270
Mike Stump6e319f62009-09-11 23:25:56 +0000271 FinishFunction();
272 return Fn;
273}
274
Anders Carlssona94822e2009-11-26 02:32:05 +0000275llvm::Constant *
Eli Friedman72649ed2009-12-06 22:01:30 +0000276CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
277 const ThunkAdjustment &ThisAdjustment) {
278 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
279
280 // Compute mangled name
281 llvm::SmallString<256> OutName;
282 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
283 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment,
284 OutName);
285 else
286 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
287 OutName += '\0';
288 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
289
290 // Get function for mangled name
291 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
292 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
293}
294
295llvm::Constant *
296CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD,
297 const CovariantThunkAdjustment &Adjustment) {
298 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
299
300 // Compute mangled name
301 llvm::SmallString<256> OutName;
302 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
303 OutName += '\0';
304 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
305
306 // Get function for mangled name
307 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
308 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
309}
310
311void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000312 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD);
313 if (!AdjPtr)
314 return;
315 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr;
Eli Friedman72649ed2009-12-06 22:01:30 +0000316 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000317 for (unsigned i = 0; i < Adj.size(); i++) {
318 GlobalDecl OGD = Adj[i].first;
319 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl());
Eli Friedman72649ed2009-12-06 22:01:30 +0000320 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
321 CanQualType oret = getContext().getCanonicalType(nc_oret);
322 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
323 CanQualType ret = getContext().getCanonicalType(nc_ret);
324 ThunkAdjustment ReturnAdjustment;
325 if (oret != ret) {
326 QualType qD = nc_ret->getPointeeType();
327 QualType qB = nc_oret->getPointeeType();
328 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
329 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
330 ReturnAdjustment = ComputeThunkAdjustment(D, B);
331 }
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000332 ThunkAdjustment ThisAdjustment = Adj[i].second;
Eli Friedman72649ed2009-12-06 22:01:30 +0000333 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace();
334 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
335 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment);
336 llvm::Constant *FnConst;
337 if (!ReturnAdjustment.isEmpty())
338 FnConst = GetAddrOfCovariantThunk(GD, CoAdj);
339 else
340 FnConst = GetAddrOfThunk(GD, ThisAdjustment);
341 if (!isa<llvm::Function>(FnConst)) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000342 llvm::Constant *SubExpr =
343 cast<llvm::ConstantExpr>(FnConst)->getOperand(0);
344 llvm::Function *OldFn = cast<llvm::Function>(SubExpr);
345 std::string Name = OldFn->getNameStr();
346 GlobalDeclMap.erase(UniqueMangledName(Name.data(),
347 Name.data() + Name.size() + 1));
348 llvm::Constant *NewFnConst;
349 if (!ReturnAdjustment.isEmpty())
350 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj);
351 else
352 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment);
353 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst);
354 NewFn->takeName(OldFn);
355 llvm::Constant *NewPtrForOldDecl =
356 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType());
357 OldFn->replaceAllUsesWith(NewPtrForOldDecl);
358 OldFn->eraseFromParent();
359 FnConst = NewFn;
Eli Friedman72649ed2009-12-06 22:01:30 +0000360 }
361 llvm::Function *Fn = cast<llvm::Function>(FnConst);
362 if (Fn->isDeclaration()) {
363 llvm::GlobalVariable::LinkageTypes linktype;
364 linktype = llvm::GlobalValue::WeakAnyLinkage;
365 if (!Extern)
366 linktype = llvm::GlobalValue::InternalLinkage;
367 Fn->setLinkage(linktype);
368 if (!Features.Exceptions && !Features.ObjCNonFragileABI)
369 Fn->addFnAttr(llvm::Attribute::NoUnwind);
370 Fn->setAlignment(2);
371 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj);
372 }
373 }
Eli Friedman72649ed2009-12-06 22:01:30 +0000374 }
375}
376
377llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000378CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
Anders Carlssona94822e2009-11-26 02:32:05 +0000379 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000380 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stumped032eb2009-09-04 18:27:16 +0000381 llvm::SmallString<256> OutName;
Mike Stump919d5e52009-12-03 03:47:56 +0000382 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
383 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
384 OutName);
385 } else
386 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
Anders Carlssona94822e2009-11-26 02:32:05 +0000387
Mike Stumped032eb2009-09-04 18:27:16 +0000388 llvm::GlobalVariable::LinkageTypes linktype;
389 linktype = llvm::GlobalValue::WeakAnyLinkage;
390 if (!Extern)
391 linktype = llvm::GlobalValue::InternalLinkage;
392 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000393 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000394 const llvm::FunctionType *FTy =
395 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
396 FPT->isVariadic());
397
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000398 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stumped032eb2009-09-04 18:27:16 +0000399 &getModule());
Mike Stump919d5e52009-12-03 03:47:56 +0000400 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
Mike Stumped032eb2009-09-04 18:27:16 +0000401 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
402 return m;
403}
404
Anders Carlsson7622cd32009-11-26 03:09:37 +0000405llvm::Constant *
Mike Stump919d5e52009-12-03 03:47:56 +0000406CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000407 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000408 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump6e319f62009-09-11 23:25:56 +0000409 llvm::SmallString<256> OutName;
Anders Carlsson7622cd32009-11-26 03:09:37 +0000410 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
Mike Stump6e319f62009-09-11 23:25:56 +0000411 llvm::GlobalVariable::LinkageTypes linktype;
412 linktype = llvm::GlobalValue::WeakAnyLinkage;
413 if (!Extern)
414 linktype = llvm::GlobalValue::InternalLinkage;
415 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000416 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000417 const llvm::FunctionType *FTy =
418 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
419 FPT->isVariadic());
420
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000421 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump6e319f62009-09-11 23:25:56 +0000422 &getModule());
Anders Carlsson7622cd32009-11-26 03:09:37 +0000423 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
Mike Stump6e319f62009-09-11 23:25:56 +0000424 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
425 return m;
426}
427
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000428static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000429 llvm::Value *This, const llvm::Type *Ty) {
430 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000431
Anders Carlsson566abee2009-11-13 04:45:41 +0000432 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
433 Vtable = CGF.Builder.CreateLoad(Vtable);
434
435 llvm::Value *VFuncPtr =
436 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
437 return CGF.Builder.CreateLoad(VFuncPtr);
438}
439
440llvm::Value *
441CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
442 const llvm::Type *Ty) {
443 MD = MD->getCanonicalDecl();
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000444 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000445
446 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
447}
448
449llvm::Value *
450CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
451 llvm::Value *&This, const llvm::Type *Ty) {
452 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000453 uint64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +0000454 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000455
456 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000457}