blob: 04c2b77256e27996d10a7c4c20342988a56b1e9e [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
Mike Stump1eb44332009-09-09 15:08:12 +0000197void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000198 CXXCtorType Type) {
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.
201 if (Type == Ctor_Complete &&
202 !D->getParent()->getNumVBases() &&
203 !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete),
204 GlobalDecl(D, Ctor_Base)))
205 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCalld26bc762011-03-09 04:27:21 +0000207 const CGFunctionInfo &FnInfo = getTypes().getFunctionInfo(D, Type);
208
209 // FIXME: re-use FnInfo in this computation!
John McCalld46f9852010-02-19 01:32:20 +0000210 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000211 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
John McCalld26bc762011-03-09 04:27:21 +0000213 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn, FnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlsson27ae5362009-04-17 01:58:57 +0000215 SetFunctionDefinitionAttributes(D, Fn);
216 SetLLVMFunctionAttributesForDefinition(D, Fn);
217}
218
John McCalld46f9852010-02-19 01:32:20 +0000219llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000220CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000221 CXXCtorType Type) {
Anders Carlssondc709a82010-06-09 02:30:12 +0000222 GlobalDecl GD(D, Type);
223
Anders Carlsson9a20d552010-06-22 16:16:50 +0000224 llvm::StringRef Name = getMangledName(GD);
John McCallf746aa62010-03-19 23:29:14 +0000225 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000226 return V;
227
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000228 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000229 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000230 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000231 FPT->isVariadic());
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000232 return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD,
233 /*ForVTable=*/false));
Anders Carlsson363c1842009-04-16 23:57:24 +0000234}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000235
Anders Carlsson27ae5362009-04-17 01:58:57 +0000236void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000237 // The destructor in a virtual table is always a 'deleting'
238 // destructor, which calls the complete destructor and then uses the
239 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000240 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000241 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000242
243 // The destructor used for destructing this as a most-derived class;
244 // call the base destructor and then destructs any virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000245 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000246
247 // The destructor used for destructing this as a base class; ignores
248 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000249 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000250}
251
Mike Stump1eb44332009-09-09 15:08:12 +0000252void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000253 CXXDtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000254 // The complete destructor is equivalent to the base destructor for
255 // classes with no virtual bases, so try to emit it as an alias.
256 if (Type == Dtor_Complete &&
257 !D->getParent()->getNumVBases() &&
258 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete),
259 GlobalDecl(D, Dtor_Base)))
260 return;
261
John McCallc0bf4622010-02-23 00:48:20 +0000262 // The base destructor is equivalent to the base destructor of its
263 // base class if there is exactly one non-virtual base class with a
264 // non-trivial destructor, there are no fields with a non-trivial
265 // destructor, and the body of the destructor is trivial.
266 if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D))
267 return;
268
John McCalld26bc762011-03-09 04:27:21 +0000269 const CGFunctionInfo &FnInfo = getTypes().getFunctionInfo(D, Type);
270
271 // FIXME: re-use FnInfo in this computation!
John McCalld46f9852010-02-19 01:32:20 +0000272 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000273 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
John McCalld26bc762011-03-09 04:27:21 +0000275 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn, FnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Anders Carlsson27ae5362009-04-17 01:58:57 +0000277 SetFunctionDefinitionAttributes(D, Fn);
278 SetLLVMFunctionAttributesForDefinition(D, Fn);
279}
280
John McCalld46f9852010-02-19 01:32:20 +0000281llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000282CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000283 CXXDtorType Type) {
Anders Carlssondc709a82010-06-09 02:30:12 +0000284 GlobalDecl GD(D, Type);
285
Anders Carlsson9a20d552010-06-22 16:16:50 +0000286 llvm::StringRef Name = getMangledName(GD);
John McCallf746aa62010-03-19 23:29:14 +0000287 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000288 return V;
289
Anders Carlsson27ae5362009-04-17 01:58:57 +0000290 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000291 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000293 return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD,
294 /*ForVTable=*/false));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000295}
296
Anders Carlsson046c2942010-04-17 20:15:18 +0000297static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000298 llvm::Value *This, const llvm::Type *Ty) {
Dan Gohman043fb9a2010-10-26 18:44:08 +0000299 Ty = Ty->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000300
Dan Gohman043fb9a2010-10-26 18:44:08 +0000301 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000302 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000303 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000304 return CGF.Builder.CreateLoad(VFuncPtr);
305}
306
307llvm::Value *
308CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
309 const llvm::Type *Ty) {
310 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000311 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000312
Anders Carlssonaf440352010-03-23 04:11:45 +0000313 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000314}
315
Fariborz Jahanian27262672011-01-20 17:19:02 +0000316/// BuildVirtualCall - This routine is to support gcc's kext ABI making
317/// indirect call to virtual functions. It makes the call through indexing
318/// into the vtable.
319llvm::Value *
320CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
321 NestedNameSpecifier *Qual,
Fariborz Jahanian27262672011-01-20 17:19:02 +0000322 const llvm::Type *Ty) {
323 llvm::Value *VTable = 0;
324 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
325 "BuildAppleKextVirtualCall - bad Qual kind");
326
327 const Type *QTy = Qual->getAsType();
328 QualType T = QualType(QTy, 0);
329 const RecordType *RT = T->getAs<RecordType>();
330 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
331 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000332
333 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
334 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
335
Fariborz Jahanian27262672011-01-20 17:19:02 +0000336 VTable = CGM.getVTables().GetAddrOfVTable(RD);
337 Ty = Ty->getPointerTo()->getPointerTo();
338 VTable = Builder.CreateBitCast(VTable, Ty);
339 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
340 MD = MD->getCanonicalDecl();
341 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000342 uint64_t AddressPoint =
343 CGM.getVTables().getAddressPoint(BaseSubobject(RD, 0), RD);
344 VTableIndex += AddressPoint;
Fariborz Jahanian27262672011-01-20 17:19:02 +0000345 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000346 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
347 return Builder.CreateLoad(VFuncPtr);
Fariborz Jahanian27262672011-01-20 17:19:02 +0000348}
349
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000350/// BuildVirtualCall - This routine makes indirect vtable call for
351/// call to virtual destructors. It returns 0 if it could not do it.
352llvm::Value *
353CodeGenFunction::BuildAppleKextVirtualDestructorCall(
354 const CXXDestructorDecl *DD,
355 CXXDtorType Type,
356 const CXXRecordDecl *RD) {
357 llvm::Value * Callee = 0;
358 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
359 // FIXME. Dtor_Base dtor is always direct!!
360 // It need be somehow inline expanded into the caller.
361 // -O does that. But need to support -O0 as well.
362 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000363 // Compute the function type we're calling.
364 const CGFunctionInfo *FInfo =
365 &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
366 Dtor_Complete);
367 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
368 const llvm::Type *Ty
369 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000370
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000371 llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD);
372 Ty = Ty->getPointerTo()->getPointerTo();
373 VTable = Builder.CreateBitCast(VTable, Ty);
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000374 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000375 uint64_t VTableIndex =
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000376 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000377 uint64_t AddressPoint =
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000378 CGM.getVTables().getAddressPoint(BaseSubobject(RD, 0), RD);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000379 VTableIndex += AddressPoint;
380 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000381 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
382 Callee = Builder.CreateLoad(VFuncPtr);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000383 }
384 return Callee;
385}
386
Anders Carlsson566abee2009-11-13 04:45:41 +0000387llvm::Value *
388CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
Anders Carlsson83eedd92010-11-28 17:53:32 +0000389 llvm::Value *This, const llvm::Type *Ty) {
Anders Carlsson566abee2009-11-13 04:45:41 +0000390 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000391 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000392 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000393
Anders Carlssonaf440352010-03-23 04:11:45 +0000394 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000395}
Charles Davis3a811f12010-05-25 19:52:27 +0000396