blob: 184147cb728d9b4859eece6065fef01aabadcc5a [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.
John McCall92ac9ff2010-02-17 03:52:49 +0000190 EmitGlobal(GlobalDecl(D, Ctor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000191
192 // The constructor used for constructing this as a base class;
193 // ignores virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000194 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000195}
Anders Carlsson363c1842009-04-16 23:57:24 +0000196
John McCall1f6f9612011-03-09 08:12:35 +0000197void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
198 CXXCtorType ctorType) {
John McCalld46f9852010-02-19 01:32:20 +0000199 // The complete constructor is equivalent to the base constructor
200 // for classes with no virtual bases. Try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000201 if (ctorType == Ctor_Complete &&
202 !ctor->getParent()->getNumVBases() &&
203 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
204 GlobalDecl(ctor, Ctor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000205 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCall1f6f9612011-03-09 08:12:35 +0000207 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(ctor, ctorType);
John McCalld26bc762011-03-09 04:27:21 +0000208
John McCall1f6f9612011-03-09 08:12:35 +0000209 llvm::Function *fn =
210 cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo));
211 setFunctionLinkage(ctor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
John McCall1f6f9612011-03-09 08:12:35 +0000213 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
John McCall1f6f9612011-03-09 08:12:35 +0000215 SetFunctionDefinitionAttributes(ctor, fn);
216 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000217}
218
John McCalld46f9852010-02-19 01:32:20 +0000219llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000220CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
221 CXXCtorType ctorType,
222 const CGFunctionInfo *fnInfo) {
223 GlobalDecl GD(ctor, ctorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000224
John McCall1f6f9612011-03-09 08:12:35 +0000225 llvm::StringRef name = getMangledName(GD);
226 if (llvm::GlobalValue *existing = GetGlobalValue(name))
227 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000228
John McCall1f6f9612011-03-09 08:12:35 +0000229 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(ctor, ctorType);
230
231 const FunctionProtoType *proto = ctor->getType()->castAs<FunctionProtoType>();
232 const llvm::FunctionType *fnType =
233 getTypes().GetFunctionType(*fnInfo, proto->isVariadic());
234 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000235 /*ForVTable=*/false));
Anders Carlsson363c1842009-04-16 23:57:24 +0000236}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000237
Anders Carlsson27ae5362009-04-17 01:58:57 +0000238void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000239 // The destructor in a virtual table is always a 'deleting'
240 // destructor, which calls the complete destructor and then uses the
241 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000242 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000243 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000244
245 // The destructor used for destructing this as a most-derived class;
246 // call the base destructor and then destructs any virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000247 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000248
249 // The destructor used for destructing this as a base class; ignores
250 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000251 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000252}
253
John McCall1f6f9612011-03-09 08:12:35 +0000254void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
255 CXXDtorType dtorType) {
John McCalld46f9852010-02-19 01:32:20 +0000256 // The complete destructor is equivalent to the base destructor for
257 // classes with no virtual bases, so try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000258 if (dtorType == Dtor_Complete &&
259 !dtor->getParent()->getNumVBases() &&
260 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
261 GlobalDecl(dtor, Dtor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000262 return;
263
John McCallc0bf4622010-02-23 00:48:20 +0000264 // The base destructor is equivalent to the base destructor of its
265 // base class if there is exactly one non-virtual base class with a
266 // non-trivial destructor, there are no fields with a non-trivial
267 // destructor, and the body of the destructor is trivial.
John McCall1f6f9612011-03-09 08:12:35 +0000268 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallc0bf4622010-02-23 00:48:20 +0000269 return;
270
John McCall1f6f9612011-03-09 08:12:35 +0000271 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(dtor, dtorType);
John McCalld26bc762011-03-09 04:27:21 +0000272
John McCall1f6f9612011-03-09 08:12:35 +0000273 llvm::Function *fn =
274 cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo));
275 setFunctionLinkage(dtor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
John McCall1f6f9612011-03-09 08:12:35 +0000277 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
John McCall1f6f9612011-03-09 08:12:35 +0000279 SetFunctionDefinitionAttributes(dtor, fn);
280 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000281}
282
John McCalld46f9852010-02-19 01:32:20 +0000283llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000284CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
285 CXXDtorType dtorType,
286 const CGFunctionInfo *fnInfo) {
287 GlobalDecl GD(dtor, dtorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000288
John McCall1f6f9612011-03-09 08:12:35 +0000289 llvm::StringRef name = getMangledName(GD);
290 if (llvm::GlobalValue *existing = GetGlobalValue(name))
291 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000292
John McCall1f6f9612011-03-09 08:12:35 +0000293 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(dtor, dtorType);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
John McCall1f6f9612011-03-09 08:12:35 +0000295 const llvm::FunctionType *fnType =
296 getTypes().GetFunctionType(*fnInfo, false);
297
298 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000299 /*ForVTable=*/false));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000300}
301
Anders Carlsson046c2942010-04-17 20:15:18 +0000302static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000303 llvm::Value *This, const llvm::Type *Ty) {
Dan Gohman043fb9a2010-10-26 18:44:08 +0000304 Ty = Ty->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000305
Dan Gohman043fb9a2010-10-26 18:44:08 +0000306 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000307 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000308 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000309 return CGF.Builder.CreateLoad(VFuncPtr);
310}
311
312llvm::Value *
313CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
314 const llvm::Type *Ty) {
315 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000316 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000317
Anders Carlssonaf440352010-03-23 04:11:45 +0000318 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000319}
320
Fariborz Jahanian27262672011-01-20 17:19:02 +0000321/// BuildVirtualCall - This routine is to support gcc's kext ABI making
322/// indirect call to virtual functions. It makes the call through indexing
323/// into the vtable.
324llvm::Value *
325CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
326 NestedNameSpecifier *Qual,
Fariborz Jahanian27262672011-01-20 17:19:02 +0000327 const llvm::Type *Ty) {
328 llvm::Value *VTable = 0;
329 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
330 "BuildAppleKextVirtualCall - bad Qual kind");
331
332 const Type *QTy = Qual->getAsType();
333 QualType T = QualType(QTy, 0);
334 const RecordType *RT = T->getAs<RecordType>();
335 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
336 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000337
338 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
339 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
340
Fariborz Jahanian27262672011-01-20 17:19:02 +0000341 VTable = CGM.getVTables().GetAddrOfVTable(RD);
342 Ty = Ty->getPointerTo()->getPointerTo();
343 VTable = Builder.CreateBitCast(VTable, Ty);
344 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
345 MD = MD->getCanonicalDecl();
346 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000347 uint64_t AddressPoint =
Ken Dyck4230d522011-03-24 01:21:01 +0000348 CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000349 VTableIndex += AddressPoint;
Fariborz Jahanian27262672011-01-20 17:19:02 +0000350 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000351 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
352 return Builder.CreateLoad(VFuncPtr);
Fariborz Jahanian27262672011-01-20 17:19:02 +0000353}
354
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000355/// BuildVirtualCall - This routine makes indirect vtable call for
356/// call to virtual destructors. It returns 0 if it could not do it.
357llvm::Value *
358CodeGenFunction::BuildAppleKextVirtualDestructorCall(
359 const CXXDestructorDecl *DD,
360 CXXDtorType Type,
361 const CXXRecordDecl *RD) {
362 llvm::Value * Callee = 0;
363 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
364 // FIXME. Dtor_Base dtor is always direct!!
365 // It need be somehow inline expanded into the caller.
366 // -O does that. But need to support -O0 as well.
367 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000368 // Compute the function type we're calling.
369 const CGFunctionInfo *FInfo =
370 &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
371 Dtor_Complete);
372 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
373 const llvm::Type *Ty
374 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000375
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000376 llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD);
377 Ty = Ty->getPointerTo()->getPointerTo();
378 VTable = Builder.CreateBitCast(VTable, Ty);
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000379 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000380 uint64_t VTableIndex =
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000381 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000382 uint64_t AddressPoint =
Ken Dyck4230d522011-03-24 01:21:01 +0000383 CGM.getVTables().getAddressPoint(BaseSubobject(RD, CharUnits::Zero()), RD);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000384 VTableIndex += AddressPoint;
385 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000386 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
387 Callee = Builder.CreateLoad(VFuncPtr);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000388 }
389 return Callee;
390}
391
Anders Carlsson566abee2009-11-13 04:45:41 +0000392llvm::Value *
393CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
Anders Carlsson83eedd92010-11-28 17:53:32 +0000394 llvm::Value *This, const llvm::Type *Ty) {
Anders Carlsson566abee2009-11-13 04:45:41 +0000395 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000396 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000397 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000398
Anders Carlssonaf440352010-03-23 04:11:45 +0000399 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000400}
Charles Davis3a811f12010-05-25 19:52:27 +0000401