blob: 4390f3a3e3e940b45d7147fe46b04c067d426aa4 [file] [log] [blame]
Devang Pateldaaf1de2010-10-04 21:15:33 +00001//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
Anders Carlssone1b29ef2008-08-22 16:00:37 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
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
Charles Davis3a811f12010-05-25 19:52:27 +000016#include "CGCXXABI.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000017#include "CodeGenFunction.h"
18#include "CodeGenModule.h"
19#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"
Peter Collingbourne14110472011-01-13 18:57:25 +000024#include "clang/AST/Mangle.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000025#include "clang/AST/StmtCXX.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000027#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallc0bf4622010-02-23 00:48:20 +000031/// Determines whether the given function has a trivial body that does
32/// not require any specific codegen.
33static bool HasTrivialBody(const FunctionDecl *FD) {
34 Stmt *S = FD->getBody();
35 if (!S)
36 return true;
37 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
38 return true;
39 return false;
40}
41
42/// Try to emit a base destructor as an alias to its primary
43/// base-class destructor.
44bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
45 if (!getCodeGenOpts().CXXCtorDtorAliases)
46 return true;
47
48 // If the destructor doesn't have a trivial body, we have to emit it
49 // separately.
50 if (!HasTrivialBody(D))
51 return true;
52
53 const CXXRecordDecl *Class = D->getParent();
54
55 // If we need to manipulate a VTT parameter, give up.
56 if (Class->getNumVBases()) {
57 // Extra Credit: passing extra parameters is perfectly safe
58 // in many calling conventions, so only bail out if the ctor's
59 // calling convention is nonstandard.
60 return true;
61 }
62
John McCall0d70d712011-02-13 00:46:43 +000063 // If any field has a non-trivial destructor, we have to emit the
64 // destructor separately.
John McCallc0bf4622010-02-23 00:48:20 +000065 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
66 E = Class->field_end(); I != E; ++I)
John McCall0d70d712011-02-13 00:46:43 +000067 if ((*I)->getType().isDestructedType())
68 return true;
John McCallc0bf4622010-02-23 00:48:20 +000069
70 // Try to find a unique base class with a non-trivial destructor.
71 const CXXRecordDecl *UniqueBase = 0;
72 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
73 E = Class->bases_end(); I != E; ++I) {
74
75 // We're in the base destructor, so skip virtual bases.
76 if (I->isVirtual()) continue;
77
78 // Skip base classes with trivial destructors.
79 const CXXRecordDecl *Base
80 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
81 if (Base->hasTrivialDestructor()) continue;
82
83 // If we've already found a base class with a non-trivial
84 // destructor, give up.
85 if (UniqueBase) return true;
86 UniqueBase = Base;
87 }
88
89 // If we didn't find any bases with a non-trivial destructor, then
90 // the base destructor is actually effectively trivial, which can
91 // happen if it was needlessly user-defined or if there are virtual
92 // bases with non-trivial destructors.
93 if (!UniqueBase)
94 return true;
95
John McCall9a708462010-03-03 03:40:11 +000096 /// If we don't have a definition for the destructor yet, don't
97 /// emit. We can't emit aliases to declarations; that's just not
98 /// how aliases work.
Douglas Gregor1d110e02010-07-01 14:13:13 +000099 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000100 if (!BaseD->isImplicit() && !BaseD->hasBody())
John McCall9a708462010-03-03 03:40:11 +0000101 return true;
102
John McCallc0bf4622010-02-23 00:48:20 +0000103 // If the base is at a non-zero offset, give up.
104 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
Anders Carlssona14f5972010-10-31 23:22:37 +0000105 if (ClassLayout.getBaseClassOffsetInBits(UniqueBase) != 0)
John McCallc0bf4622010-02-23 00:48:20 +0000106 return true;
107
John McCallc0bf4622010-02-23 00:48:20 +0000108 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
109 GlobalDecl(BaseD, Dtor_Base));
110}
111
John McCalld46f9852010-02-19 01:32:20 +0000112/// Try to emit a definition as a global alias for another definition.
113bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
114 GlobalDecl TargetDecl) {
115 if (!getCodeGenOpts().CXXCtorDtorAliases)
116 return true;
117
John McCalld46f9852010-02-19 01:32:20 +0000118 // The alias will use the linkage of the referrent. If we can't
119 // support aliases with that linkage, fail.
120 llvm::GlobalValue::LinkageTypes Linkage
121 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl()));
122
123 switch (Linkage) {
124 // We can definitely emit aliases to definitions with external linkage.
125 case llvm::GlobalValue::ExternalLinkage:
126 case llvm::GlobalValue::ExternalWeakLinkage:
127 break;
128
129 // Same with local linkage.
130 case llvm::GlobalValue::InternalLinkage:
131 case llvm::GlobalValue::PrivateLinkage:
132 case llvm::GlobalValue::LinkerPrivateLinkage:
133 break;
134
135 // We should try to support linkonce linkages.
136 case llvm::GlobalValue::LinkOnceAnyLinkage:
137 case llvm::GlobalValue::LinkOnceODRLinkage:
138 return true;
139
140 // Other linkages will probably never be supported.
141 default:
142 return true;
143 }
144
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000145 llvm::GlobalValue::LinkageTypes TargetLinkage
146 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl()));
147
Rafael Espindola3c157452010-03-06 07:35:18 +0000148 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000149 return true;
150
John McCallc0bf4622010-02-23 00:48:20 +0000151 // Derive the type for the alias.
152 const llvm::PointerType *AliasType
153 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
154
John McCallc0bf4622010-02-23 00:48:20 +0000155 // Find the referrent. Some aliases might require a bitcast, in
156 // which case the caller is responsible for ensuring the soundness
157 // of these semantics.
158 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
159 llvm::Constant *Aliasee = Ref;
160 if (Ref->getType() != AliasType)
161 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
162
John McCalld46f9852010-02-19 01:32:20 +0000163 // Create the alias with no name.
164 llvm::GlobalAlias *Alias =
John McCallc0bf4622010-02-23 00:48:20 +0000165 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld46f9852010-02-19 01:32:20 +0000166
John McCall1962bee2010-02-24 20:32:01 +0000167 // Switch any previous uses to the alias.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000168 llvm::StringRef MangledName = getMangledName(AliasDecl);
John McCallf746aa62010-03-19 23:29:14 +0000169 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000170 if (Entry) {
John McCall1962bee2010-02-24 20:32:01 +0000171 assert(Entry->isDeclaration() && "definition already exists for alias");
172 assert(Entry->getType() == AliasType &&
173 "declaration exists with different type");
John McCallf746aa62010-03-19 23:29:14 +0000174 Alias->takeName(Entry);
John McCalld46f9852010-02-19 01:32:20 +0000175 Entry->replaceAllUsesWith(Alias);
176 Entry->eraseFromParent();
John McCallf746aa62010-03-19 23:29:14 +0000177 } else {
Anders Carlsson9a20d552010-06-22 16:16:50 +0000178 Alias->setName(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000179 }
John McCalld46f9852010-02-19 01:32:20 +0000180
181 // Finally, set up the alias with its proper name and attributes.
John McCall1fb0caa2010-10-22 21:05:15 +0000182 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
John McCalld46f9852010-02-19 01:32:20 +0000183
184 return false;
185}
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000186
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000187void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000188 // The constructor used for constructing this as a complete class;
189 // constucts the virtual bases, then calls the base constructor.
Anders Carlsson8e0397a2011-05-08 17:25:05 +0000190 if (!D->getParent()->isAbstract()) {
191 // We don't need to emit the complete ctor if the class is abstract.
192 EmitGlobal(GlobalDecl(D, Ctor_Complete));
193 }
John McCalld46f9852010-02-19 01:32:20 +0000194
195 // The constructor used for constructing this as a base class;
196 // ignores virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000197 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000198}
Anders Carlsson363c1842009-04-16 23:57:24 +0000199
John McCall1f6f9612011-03-09 08:12:35 +0000200void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
201 CXXCtorType ctorType) {
John McCalld46f9852010-02-19 01:32:20 +0000202 // The complete constructor is equivalent to the base constructor
203 // for classes with no virtual bases. Try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000204 if (ctorType == Ctor_Complete &&
205 !ctor->getParent()->getNumVBases() &&
206 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
207 GlobalDecl(ctor, Ctor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000208 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
John McCall1f6f9612011-03-09 08:12:35 +0000210 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(ctor, ctorType);
John McCalld26bc762011-03-09 04:27:21 +0000211
John McCall1f6f9612011-03-09 08:12:35 +0000212 llvm::Function *fn =
213 cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo));
214 setFunctionLinkage(ctor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
John McCall1f6f9612011-03-09 08:12:35 +0000216 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000217
John McCall1f6f9612011-03-09 08:12:35 +0000218 SetFunctionDefinitionAttributes(ctor, fn);
219 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000220}
221
John McCalld46f9852010-02-19 01:32:20 +0000222llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000223CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
224 CXXCtorType ctorType,
225 const CGFunctionInfo *fnInfo) {
226 GlobalDecl GD(ctor, ctorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000227
John McCall1f6f9612011-03-09 08:12:35 +0000228 llvm::StringRef name = getMangledName(GD);
229 if (llvm::GlobalValue *existing = GetGlobalValue(name))
230 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000231
John McCall1f6f9612011-03-09 08:12:35 +0000232 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(ctor, ctorType);
233
234 const FunctionProtoType *proto = ctor->getType()->castAs<FunctionProtoType>();
235 const llvm::FunctionType *fnType =
236 getTypes().GetFunctionType(*fnInfo, proto->isVariadic());
237 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000238 /*ForVTable=*/false));
Anders Carlsson363c1842009-04-16 23:57:24 +0000239}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000240
Anders Carlsson27ae5362009-04-17 01:58:57 +0000241void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000242 // The destructor in a virtual table is always a 'deleting'
243 // destructor, which calls the complete destructor and then uses the
244 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000245 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000246 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000247
248 // The destructor used for destructing this as a most-derived class;
249 // call the base destructor and then destructs any virtual bases.
Anders Carlsson8e0397a2011-05-08 17:25:05 +0000250 if (!D->getParent()->isAbstract() || D->isVirtual()) {
251 // We don't need to emit the complete ctor if the class is abstract,
252 // unless the destructor is virtual and needs to be in the vtable.
253 EmitGlobal(GlobalDecl(D, Dtor_Complete));
254 }
John McCalld46f9852010-02-19 01:32:20 +0000255
256 // The destructor used for destructing this as a base class; ignores
257 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000258 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000259}
260
John McCall1f6f9612011-03-09 08:12:35 +0000261void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
262 CXXDtorType dtorType) {
John McCalld46f9852010-02-19 01:32:20 +0000263 // The complete destructor is equivalent to the base destructor for
264 // classes with no virtual bases, so try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000265 if (dtorType == Dtor_Complete &&
266 !dtor->getParent()->getNumVBases() &&
267 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
268 GlobalDecl(dtor, Dtor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000269 return;
270
John McCallc0bf4622010-02-23 00:48:20 +0000271 // The base destructor is equivalent to the base destructor of its
272 // base class if there is exactly one non-virtual base class with a
273 // non-trivial destructor, there are no fields with a non-trivial
274 // destructor, and the body of the destructor is trivial.
John McCall1f6f9612011-03-09 08:12:35 +0000275 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallc0bf4622010-02-23 00:48:20 +0000276 return;
277
John McCall1f6f9612011-03-09 08:12:35 +0000278 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(dtor, dtorType);
John McCalld26bc762011-03-09 04:27:21 +0000279
John McCall1f6f9612011-03-09 08:12:35 +0000280 llvm::Function *fn =
281 cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo));
282 setFunctionLinkage(dtor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
John McCall1f6f9612011-03-09 08:12:35 +0000284 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
John McCall1f6f9612011-03-09 08:12:35 +0000286 SetFunctionDefinitionAttributes(dtor, fn);
287 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000288}
289
John McCalld46f9852010-02-19 01:32:20 +0000290llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000291CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
292 CXXDtorType dtorType,
293 const CGFunctionInfo *fnInfo) {
294 GlobalDecl GD(dtor, dtorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000295
John McCall1f6f9612011-03-09 08:12:35 +0000296 llvm::StringRef name = getMangledName(GD);
297 if (llvm::GlobalValue *existing = GetGlobalValue(name))
298 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000299
John McCall1f6f9612011-03-09 08:12:35 +0000300 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(dtor, dtorType);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
John McCall1f6f9612011-03-09 08:12:35 +0000302 const llvm::FunctionType *fnType =
303 getTypes().GetFunctionType(*fnInfo, false);
304
305 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000306 /*ForVTable=*/false));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000307}
308
Anders Carlsson046c2942010-04-17 20:15:18 +0000309static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000310 llvm::Value *This, const llvm::Type *Ty) {
Dan Gohman043fb9a2010-10-26 18:44:08 +0000311 Ty = Ty->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000312
Dan Gohman043fb9a2010-10-26 18:44:08 +0000313 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000314 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000315 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000316 return CGF.Builder.CreateLoad(VFuncPtr);
317}
318
319llvm::Value *
320CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
321 const llvm::Type *Ty) {
322 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000323 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000324
Anders Carlssonaf440352010-03-23 04:11:45 +0000325 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000326}
327
Fariborz Jahanian27262672011-01-20 17:19:02 +0000328/// BuildVirtualCall - This routine is to support gcc's kext ABI making
329/// indirect call to virtual functions. It makes the call through indexing
330/// into the vtable.
331llvm::Value *
332CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
333 NestedNameSpecifier *Qual,
Fariborz Jahanian27262672011-01-20 17:19:02 +0000334 const llvm::Type *Ty) {
335 llvm::Value *VTable = 0;
336 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
337 "BuildAppleKextVirtualCall - bad Qual kind");
338
339 const Type *QTy = Qual->getAsType();
340 QualType T = QualType(QTy, 0);
341 const RecordType *RT = T->getAs<RecordType>();
342 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
343 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000344
345 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
346 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
347
Fariborz Jahanian27262672011-01-20 17:19:02 +0000348 VTable = CGM.getVTables().GetAddrOfVTable(RD);
349 Ty = Ty->getPointerTo()->getPointerTo();
350 VTable = Builder.CreateBitCast(VTable, Ty);
351 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
352 MD = MD->getCanonicalDecl();
353 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000354 uint64_t AddressPoint =
Ken Dyck4230d522011-03-24 01:21:01 +0000355 CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000356 VTableIndex += AddressPoint;
Fariborz Jahanian27262672011-01-20 17:19:02 +0000357 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000358 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
359 return Builder.CreateLoad(VFuncPtr);
Fariborz Jahanian27262672011-01-20 17:19:02 +0000360}
361
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000362/// BuildVirtualCall - This routine makes indirect vtable call for
363/// call to virtual destructors. It returns 0 if it could not do it.
364llvm::Value *
365CodeGenFunction::BuildAppleKextVirtualDestructorCall(
366 const CXXDestructorDecl *DD,
367 CXXDtorType Type,
368 const CXXRecordDecl *RD) {
369 llvm::Value * Callee = 0;
370 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
371 // FIXME. Dtor_Base dtor is always direct!!
372 // It need be somehow inline expanded into the caller.
373 // -O does that. But need to support -O0 as well.
374 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000375 // Compute the function type we're calling.
376 const CGFunctionInfo *FInfo =
377 &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
378 Dtor_Complete);
379 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
380 const llvm::Type *Ty
381 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000382
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000383 llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD);
384 Ty = Ty->getPointerTo()->getPointerTo();
385 VTable = Builder.CreateBitCast(VTable, Ty);
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000386 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000387 uint64_t VTableIndex =
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000388 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000389 uint64_t AddressPoint =
Ken Dyck4230d522011-03-24 01:21:01 +0000390 CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000391 VTableIndex += AddressPoint;
392 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000393 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
394 Callee = Builder.CreateLoad(VFuncPtr);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000395 }
396 return Callee;
397}
398
Anders Carlsson566abee2009-11-13 04:45:41 +0000399llvm::Value *
400CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
Anders Carlsson83eedd92010-11-28 17:53:32 +0000401 llvm::Value *This, const llvm::Type *Ty) {
Anders Carlsson566abee2009-11-13 04:45:41 +0000402 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000403 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000404 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000405
Anders Carlssonaf440352010-03-23 04:11:45 +0000406 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000407}
Charles Davis3a811f12010-05-25 19:52:27 +0000408