blob: f0d01969a9cebbab44622a626821a64a1a86cf72 [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
John McCall92ac9ff2010-02-17 03:52:49 +000029/// Try to emit a definition as a global alias for another definition.
30bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
31 GlobalDecl TargetDecl) {
32 // Find the referrent.
33 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
34
35 // Look for an existing entry.
36 const char *MangledName = getMangledName(AliasDecl);
37 llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName];
38 if (Entry) {
39 assert(Entry->isDeclaration() && "definition already exists for alias");
40 assert(Entry->getType() == Ref->getType() &&
41 "declaration exists with different type");
42 }
43
44 // The alias will use the linkage of the referrent. If we can't
45 // support aliases with that linkage, fail.
46 llvm::GlobalValue::LinkageTypes Linkage
47 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl()));
48
49 switch (Linkage) {
50 // We can definitely emit aliases to definitions with external linkage.
51 case llvm::GlobalValue::ExternalLinkage:
52 case llvm::GlobalValue::ExternalWeakLinkage:
53 break;
54
55 // Same with local linkage.
56 case llvm::GlobalValue::InternalLinkage:
57 case llvm::GlobalValue::PrivateLinkage:
58 case llvm::GlobalValue::LinkerPrivateLinkage:
59 break;
60
61 // We should try to support linkonce linkages.
62 case llvm::GlobalValue::LinkOnceAnyLinkage:
63 case llvm::GlobalValue::LinkOnceODRLinkage:
64 return true;
65
66 // Other linkages will probably never be supported.
67 default:
68 return true;
69 }
70
71 // Create the alias with no name.
72 llvm::GlobalAlias *Alias =
73 new llvm::GlobalAlias(Ref->getType(), Linkage, "", Ref, &getModule());
74
75 // Switch any previous uses to the alias and continue.
76 if (Entry) {
77 Entry->replaceAllUsesWith(Alias);
78 Entry->eraseFromParent();
79 }
80 Entry = Alias;
81
82 // Finally, set up the alias with its proper name and attributes.
83 Alias->setName(MangledName);
84 SetCommonAttributes(AliasDecl.getDecl(), Alias);
85
86 return false;
87}
Anders Carlssonb9de2c52009-05-11 23:37:08 +000088
Anders Carlsson0f294632009-05-27 04:18:27 +000089
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000090void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
John McCall92ac9ff2010-02-17 03:52:49 +000091 // The constructor used for constructing this as a base class;
92 // ignores virtual bases.
Anders Carlsson2a131fb2009-05-05 04:44:02 +000093 EmitGlobal(GlobalDecl(D, Ctor_Base));
John McCall92ac9ff2010-02-17 03:52:49 +000094
95 // The constructor used for constructing this as a complete class;
96 // constucts the virtual bases, then calls the base constructor.
97 EmitGlobal(GlobalDecl(D, Ctor_Complete));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +000098}
Anders Carlsson363c1842009-04-16 23:57:24 +000099
Mike Stump1eb44332009-09-09 15:08:12 +0000100void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000101 CXXCtorType Type) {
John McCall92ac9ff2010-02-17 03:52:49 +0000102 // The complete constructor is equivalent to the base constructor
103 // for classes with no virtual bases. Try to emit it as an alias.
104 if (Type == Ctor_Complete &&
105 !D->getParent()->getNumVBases() &&
106 !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete),
107 GlobalDecl(D, Ctor_Base)))
108 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
John McCall92ac9ff2010-02-17 03:52:49 +0000110 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type));
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000112 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Anders Carlsson27ae5362009-04-17 01:58:57 +0000114 SetFunctionDefinitionAttributes(D, Fn);
115 SetLLVMFunctionAttributesForDefinition(D, Fn);
116}
117
John McCall92ac9ff2010-02-17 03:52:49 +0000118llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000119CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000120 CXXCtorType Type) {
John McCall92ac9ff2010-02-17 03:52:49 +0000121 const char *Name = getMangledCXXCtorName(D, Type);
122 if (llvm::GlobalValue *V = GlobalDeclMap[Name])
123 return V;
124
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000125 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000126 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000127 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000128 FPT->isVariadic());
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000129 return cast<llvm::Function>(
130 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000131}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000132
Mike Stump1eb44332009-09-09 15:08:12 +0000133const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000134 CXXCtorType Type) {
135 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000136 getMangleContext().mangleCXXCtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Anders Carlsson27ae5362009-04-17 01:58:57 +0000138 Name += '\0';
139 return UniqueMangledName(Name.begin(), Name.end());
140}
141
142void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCall92ac9ff2010-02-17 03:52:49 +0000143 // The destructor used for destructing this as a base class; ignores
144 // virtual bases.
145 EmitGlobal(GlobalDecl(D, Dtor_Base));
146
147 // The destructor used for destructing this as a most-derived class;
148 // call the base destructor and then destructs any virtual bases.
149 EmitGlobal(GlobalDecl(D, Dtor_Complete));
150
151 // The destructor in a virtual table is always a 'deleting'
152 // destructor, which calls the complete destructor and then uses the
153 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000154 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000155 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000156}
157
Mike Stump1eb44332009-09-09 15:08:12 +0000158void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000159 CXXDtorType Type) {
John McCall92ac9ff2010-02-17 03:52:49 +0000160 // The complete destructor is equivalent to the base destructor for
161 // classes with no virtual bases, so try to emit it as an alias.
162 if (Type == Dtor_Complete &&
163 !D->getParent()->getNumVBases() &&
164 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete),
165 GlobalDecl(D, Dtor_Base)))
166 return;
167
168 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type));
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000170 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Anders Carlsson27ae5362009-04-17 01:58:57 +0000172 SetFunctionDefinitionAttributes(D, Fn);
173 SetLLVMFunctionAttributesForDefinition(D, Fn);
174}
175
John McCall92ac9ff2010-02-17 03:52:49 +0000176llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000177CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000178 CXXDtorType Type) {
John McCall92ac9ff2010-02-17 03:52:49 +0000179 const char *Name = getMangledCXXDtorName(D, Type);
180 if (llvm::GlobalValue *V = GlobalDeclMap[Name])
181 return V;
182
Anders Carlsson27ae5362009-04-17 01:58:57 +0000183 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000184 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000186 return cast<llvm::Function>(
187 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000188}
189
Mike Stump1eb44332009-09-09 15:08:12 +0000190const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000191 CXXDtorType Type) {
192 llvm::SmallString<256> Name;
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000193 getMangleContext().mangleCXXDtor(D, Type, Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Anders Carlsson27ae5362009-04-17 01:58:57 +0000195 Name += '\0';
196 return UniqueMangledName(Name.begin(), Name.end());
197}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000198
Anders Carlssona94822e2009-11-26 02:32:05 +0000199llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000200CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
Anders Carlssona94822e2009-11-26 02:32:05 +0000201 bool Extern,
202 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000203 return GenerateCovariantThunk(Fn, GD, Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000204 CovariantThunkAdjustment(ThisAdjustment,
205 ThunkAdjustment()));
Mike Stumped032eb2009-09-04 18:27:16 +0000206}
207
Anders Carlsson7622cd32009-11-26 03:09:37 +0000208llvm::Value *
209CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
210 const ThunkAdjustment &Adjustment) {
211 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
212
Mike Stumpc902d222009-11-03 16:59:27 +0000213 const llvm::Type *OrigTy = V->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000214 if (Adjustment.NonVirtual) {
Mike Stumpc902d222009-11-03 16:59:27 +0000215 // Do the non-virtual adjustment
Anders Carlsson7622cd32009-11-26 03:09:37 +0000216 V = Builder.CreateBitCast(V, Int8PtrTy);
217 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
Mike Stumpc902d222009-11-03 16:59:27 +0000218 V = Builder.CreateBitCast(V, OrigTy);
219 }
Anders Carlsson7622cd32009-11-26 03:09:37 +0000220
221 if (!Adjustment.Virtual)
222 return V;
223
224 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
225 "vtable entry unaligned");
226
227 // Do the virtual this adjustment
228 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
229 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
230
231 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
232 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
233 V = Builder.CreateLoad(V, "vtable");
234
235 llvm::Value *VTablePtr = V;
236 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
237 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
238 V = Builder.CreateLoad(V);
239 V = Builder.CreateGEP(ThisVal, V);
240
241 return Builder.CreateBitCast(V, OrigTy);
Mike Stumpc902d222009-11-03 16:59:27 +0000242}
243
Anders Carlsson7622cd32009-11-26 03:09:37 +0000244llvm::Constant *
245CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
Eli Friedman35c98cc2009-12-03 04:27:05 +0000246 GlobalDecl GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000247 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000248 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
John McCall04a67a62010-02-05 21:31:56 +0000249 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
250 QualType ResultType = FPT->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000251
252 FunctionArgList Args;
253 ImplicitParamDecl *ThisDecl =
254 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
255 MD->getThisType(getContext()));
256 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
257 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
258 e = MD->param_end();
259 i != e; ++i) {
260 ParmVarDecl *D = *i;
261 Args.push_back(std::make_pair(D, D->getType()));
262 }
263 IdentifierInfo *II
264 = &CGM.getContext().Idents.get("__thunk_named_foo_");
265 FunctionDecl *FD = FunctionDecl::Create(getContext(),
266 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000267 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000268 Extern
269 ? FunctionDecl::Extern
270 : FunctionDecl::Static,
271 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000272 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
273
274 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000275 const llvm::Type *Ty =
276 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
277 FPT->isVariadic());
Eli Friedman35c98cc2009-12-03 04:27:05 +0000278 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
Mike Stump919d5e52009-12-03 03:47:56 +0000279
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000280 CallArgList CallArgs;
281
Anders Carlsson7622cd32009-11-26 03:09:37 +0000282 bool ShouldAdjustReturnPointer = true;
Mike Stump736529e2009-11-03 02:12:59 +0000283 QualType ArgType = MD->getThisType(getContext());
284 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Anders Carlsson7622cd32009-11-26 03:09:37 +0000285 if (!Adjustment.ThisAdjustment.isEmpty()) {
Mike Stumpc902d222009-11-03 16:59:27 +0000286 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000287 const llvm::Type *OrigTy = Callee->getType();
Anders Carlsson7622cd32009-11-26 03:09:37 +0000288 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
289
290 if (!Adjustment.ReturnAdjustment.isEmpty()) {
291 const CovariantThunkAdjustment &ReturnAdjustment =
292 CovariantThunkAdjustment(ThunkAdjustment(),
293 Adjustment.ReturnAdjustment);
294
Mike Stump919d5e52009-12-03 03:47:56 +0000295 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000296
Mike Stumpd0fe5362009-11-04 00:53:51 +0000297 Callee = Builder.CreateBitCast(Callee, OrigTy);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000298 ShouldAdjustReturnPointer = false;
Mike Stumpd0fe5362009-11-04 00:53:51 +0000299 }
300 }
301
Mike Stump736529e2009-11-03 02:12:59 +0000302 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
303
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000304 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
305 e = MD->param_end();
306 i != e; ++i) {
307 ParmVarDecl *D = *i;
308 QualType ArgType = D->getType();
309
310 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
Eli Friedman6804fa22009-12-03 04:49:52 +0000311 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(),
312 SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000313 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
314 }
315
John McCall04a67a62010-02-05 21:31:56 +0000316 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
317 FPT->getCallConv(),
318 FPT->getNoReturnAttr()),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000319 Callee, ReturnValueSlot(), CallArgs, MD);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000320 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
Mike Stump03e777e2009-11-05 06:32:02 +0000321 bool CanBeZero = !(ResultType->isReferenceType()
322 // FIXME: attr nonnull can't be zero either
323 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000324 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000325 if (CanBeZero) {
326 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
327 llvm::BasicBlock *ZeroBlock = createBasicBlock();
328 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000329
Mike Stump03e777e2009-11-05 06:32:02 +0000330 const llvm::Type *Ty = RV.getScalarVal()->getType();
331 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
332 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
333 NonZeroBlock, ZeroBlock);
334 EmitBlock(NonZeroBlock);
Anders Carlsson7622cd32009-11-26 03:09:37 +0000335 llvm::Value *NZ =
336 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
Mike Stump03e777e2009-11-05 06:32:02 +0000337 EmitBranch(ContBlock);
338 EmitBlock(ZeroBlock);
339 llvm::Value *Z = RV.getScalarVal();
340 EmitBlock(ContBlock);
341 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
342 RVOrZero->reserveOperandSpace(2);
343 RVOrZero->addIncoming(NZ, NonZeroBlock);
344 RVOrZero->addIncoming(Z, ZeroBlock);
345 RV = RValue::get(RVOrZero);
346 } else
Anders Carlsson7622cd32009-11-26 03:09:37 +0000347 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
348 Adjustment.ReturnAdjustment));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000349 }
350
Mike Stumpf49ed942009-11-02 23:47:45 +0000351 if (!ResultType->isVoidType())
352 EmitReturnOfRValue(RV, ResultType);
353
Mike Stump6e319f62009-09-11 23:25:56 +0000354 FinishFunction();
355 return Fn;
356}
357
Anders Carlssona94822e2009-11-26 02:32:05 +0000358llvm::Constant *
Eli Friedman72649ed2009-12-06 22:01:30 +0000359CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
360 const ThunkAdjustment &ThisAdjustment) {
361 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
362
363 // Compute mangled name
364 llvm::SmallString<256> OutName;
365 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
366 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment,
367 OutName);
368 else
369 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
370 OutName += '\0';
371 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
372
373 // Get function for mangled name
374 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
375 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
376}
377
378llvm::Constant *
379CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD,
380 const CovariantThunkAdjustment &Adjustment) {
381 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
382
383 // Compute mangled name
384 llvm::SmallString<256> OutName;
385 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
386 OutName += '\0';
387 const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
388
389 // Get function for mangled name
390 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
391 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
392}
393
394void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000395 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD);
396 if (!AdjPtr)
397 return;
398 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr;
Eli Friedman72649ed2009-12-06 22:01:30 +0000399 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000400 for (unsigned i = 0; i < Adj.size(); i++) {
401 GlobalDecl OGD = Adj[i].first;
402 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl());
Eli Friedman72649ed2009-12-06 22:01:30 +0000403 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
404 CanQualType oret = getContext().getCanonicalType(nc_oret);
405 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
406 CanQualType ret = getContext().getCanonicalType(nc_ret);
407 ThunkAdjustment ReturnAdjustment;
408 if (oret != ret) {
409 QualType qD = nc_ret->getPointeeType();
410 QualType qB = nc_oret->getPointeeType();
411 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
412 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
413 ReturnAdjustment = ComputeThunkAdjustment(D, B);
414 }
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000415 ThunkAdjustment ThisAdjustment = Adj[i].second;
Eli Friedman72649ed2009-12-06 22:01:30 +0000416 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace();
417 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
418 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment);
419 llvm::Constant *FnConst;
420 if (!ReturnAdjustment.isEmpty())
421 FnConst = GetAddrOfCovariantThunk(GD, CoAdj);
422 else
423 FnConst = GetAddrOfThunk(GD, ThisAdjustment);
424 if (!isa<llvm::Function>(FnConst)) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +0000425 llvm::Constant *SubExpr =
426 cast<llvm::ConstantExpr>(FnConst)->getOperand(0);
427 llvm::Function *OldFn = cast<llvm::Function>(SubExpr);
428 std::string Name = OldFn->getNameStr();
429 GlobalDeclMap.erase(UniqueMangledName(Name.data(),
430 Name.data() + Name.size() + 1));
431 llvm::Constant *NewFnConst;
432 if (!ReturnAdjustment.isEmpty())
433 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj);
434 else
435 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment);
436 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst);
437 NewFn->takeName(OldFn);
438 llvm::Constant *NewPtrForOldDecl =
439 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType());
440 OldFn->replaceAllUsesWith(NewPtrForOldDecl);
441 OldFn->eraseFromParent();
442 FnConst = NewFn;
Eli Friedman72649ed2009-12-06 22:01:30 +0000443 }
444 llvm::Function *Fn = cast<llvm::Function>(FnConst);
445 if (Fn->isDeclaration()) {
446 llvm::GlobalVariable::LinkageTypes linktype;
447 linktype = llvm::GlobalValue::WeakAnyLinkage;
448 if (!Extern)
449 linktype = llvm::GlobalValue::InternalLinkage;
450 Fn->setLinkage(linktype);
451 if (!Features.Exceptions && !Features.ObjCNonFragileABI)
452 Fn->addFnAttr(llvm::Attribute::NoUnwind);
453 Fn->setAlignment(2);
454 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj);
455 }
456 }
Eli Friedman72649ed2009-12-06 22:01:30 +0000457 }
458}
459
460llvm::Constant *
Eli Friedman35c98cc2009-12-03 04:27:05 +0000461CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
Anders Carlssona94822e2009-11-26 02:32:05 +0000462 const ThunkAdjustment &ThisAdjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000463 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stumped032eb2009-09-04 18:27:16 +0000464 llvm::SmallString<256> OutName;
Mike Stump919d5e52009-12-03 03:47:56 +0000465 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
466 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
467 OutName);
468 } else
469 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
Anders Carlssona94822e2009-11-26 02:32:05 +0000470
Mike Stumped032eb2009-09-04 18:27:16 +0000471 llvm::GlobalVariable::LinkageTypes linktype;
472 linktype = llvm::GlobalValue::WeakAnyLinkage;
473 if (!Extern)
474 linktype = llvm::GlobalValue::InternalLinkage;
475 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000476 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000477 const llvm::FunctionType *FTy =
478 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
479 FPT->isVariadic());
480
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000481 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stumped032eb2009-09-04 18:27:16 +0000482 &getModule());
Mike Stump919d5e52009-12-03 03:47:56 +0000483 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
Mike Stumped032eb2009-09-04 18:27:16 +0000484 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
485 return m;
486}
487
Anders Carlsson7622cd32009-11-26 03:09:37 +0000488llvm::Constant *
Mike Stump919d5e52009-12-03 03:47:56 +0000489CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
Anders Carlsson7622cd32009-11-26 03:09:37 +0000490 const CovariantThunkAdjustment &Adjustment) {
Mike Stump919d5e52009-12-03 03:47:56 +0000491 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump6e319f62009-09-11 23:25:56 +0000492 llvm::SmallString<256> OutName;
Anders Carlsson7622cd32009-11-26 03:09:37 +0000493 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
Mike Stump6e319f62009-09-11 23:25:56 +0000494 llvm::GlobalVariable::LinkageTypes linktype;
495 linktype = llvm::GlobalValue::WeakAnyLinkage;
496 if (!Extern)
497 linktype = llvm::GlobalValue::InternalLinkage;
498 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000499 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000500 const llvm::FunctionType *FTy =
501 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
502 FPT->isVariadic());
503
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000504 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
Mike Stump6e319f62009-09-11 23:25:56 +0000505 &getModule());
Anders Carlsson7622cd32009-11-26 03:09:37 +0000506 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
Mike Stump6e319f62009-09-11 23:25:56 +0000507 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
508 return m;
509}
510
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000511static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000512 llvm::Value *This, const llvm::Type *Ty) {
513 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000514
Anders Carlsson566abee2009-11-13 04:45:41 +0000515 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
516 Vtable = CGF.Builder.CreateLoad(Vtable);
517
518 llvm::Value *VFuncPtr =
519 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
520 return CGF.Builder.CreateLoad(VFuncPtr);
521}
522
523llvm::Value *
524CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
525 const llvm::Type *Ty) {
526 MD = MD->getCanonicalDecl();
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000527 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000528
529 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
530}
531
532llvm::Value *
533CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
534 llvm::Value *&This, const llvm::Type *Ty) {
535 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000536 uint64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +0000537 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000538
539 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000540}