blob: b5e6e0d7d99375678b604bb5fd38cb482f180a99 [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/// Try to emit a base destructor as an alias to its primary
32/// base-class destructor.
33bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
34 if (!getCodeGenOpts().CXXCtorDtorAliases)
35 return true;
36
37 // If the destructor doesn't have a trivial body, we have to emit it
38 // separately.
Anders Carlssonffb945f2011-05-14 23:26:09 +000039 if (!D->hasTrivialBody())
John McCallc0bf4622010-02-23 00:48:20 +000040 return true;
41
42 const CXXRecordDecl *Class = D->getParent();
43
44 // If we need to manipulate a VTT parameter, give up.
45 if (Class->getNumVBases()) {
46 // Extra Credit: passing extra parameters is perfectly safe
47 // in many calling conventions, so only bail out if the ctor's
48 // calling convention is nonstandard.
49 return true;
50 }
51
John McCall0d70d712011-02-13 00:46:43 +000052 // If any field has a non-trivial destructor, we have to emit the
53 // destructor separately.
John McCallc0bf4622010-02-23 00:48:20 +000054 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
55 E = Class->field_end(); I != E; ++I)
John McCall0d70d712011-02-13 00:46:43 +000056 if ((*I)->getType().isDestructedType())
57 return true;
John McCallc0bf4622010-02-23 00:48:20 +000058
59 // Try to find a unique base class with a non-trivial destructor.
60 const CXXRecordDecl *UniqueBase = 0;
61 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
62 E = Class->bases_end(); I != E; ++I) {
63
64 // We're in the base destructor, so skip virtual bases.
65 if (I->isVirtual()) continue;
66
67 // Skip base classes with trivial destructors.
68 const CXXRecordDecl *Base
69 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
70 if (Base->hasTrivialDestructor()) continue;
71
72 // If we've already found a base class with a non-trivial
73 // destructor, give up.
74 if (UniqueBase) return true;
75 UniqueBase = Base;
76 }
77
78 // If we didn't find any bases with a non-trivial destructor, then
79 // the base destructor is actually effectively trivial, which can
80 // happen if it was needlessly user-defined or if there are virtual
81 // bases with non-trivial destructors.
82 if (!UniqueBase)
83 return true;
84
John McCall9a708462010-03-03 03:40:11 +000085 /// If we don't have a definition for the destructor yet, don't
86 /// emit. We can't emit aliases to declarations; that's just not
87 /// how aliases work.
Douglas Gregor1d110e02010-07-01 14:13:13 +000088 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +000089 if (!BaseD->isImplicit() && !BaseD->hasBody())
John McCall9a708462010-03-03 03:40:11 +000090 return true;
91
John McCallc0bf4622010-02-23 00:48:20 +000092 // If the base is at a non-zero offset, give up.
93 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
Anders Carlssona14f5972010-10-31 23:22:37 +000094 if (ClassLayout.getBaseClassOffsetInBits(UniqueBase) != 0)
John McCallc0bf4622010-02-23 00:48:20 +000095 return true;
96
John McCallc0bf4622010-02-23 00:48:20 +000097 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
98 GlobalDecl(BaseD, Dtor_Base));
99}
100
John McCalld46f9852010-02-19 01:32:20 +0000101/// Try to emit a definition as a global alias for another definition.
102bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
103 GlobalDecl TargetDecl) {
104 if (!getCodeGenOpts().CXXCtorDtorAliases)
105 return true;
106
John McCalld46f9852010-02-19 01:32:20 +0000107 // The alias will use the linkage of the referrent. If we can't
108 // support aliases with that linkage, fail.
109 llvm::GlobalValue::LinkageTypes Linkage
110 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl()));
111
112 switch (Linkage) {
113 // We can definitely emit aliases to definitions with external linkage.
114 case llvm::GlobalValue::ExternalLinkage:
115 case llvm::GlobalValue::ExternalWeakLinkage:
116 break;
117
118 // Same with local linkage.
119 case llvm::GlobalValue::InternalLinkage:
120 case llvm::GlobalValue::PrivateLinkage:
121 case llvm::GlobalValue::LinkerPrivateLinkage:
122 break;
123
124 // We should try to support linkonce linkages.
125 case llvm::GlobalValue::LinkOnceAnyLinkage:
126 case llvm::GlobalValue::LinkOnceODRLinkage:
127 return true;
128
129 // Other linkages will probably never be supported.
130 default:
131 return true;
132 }
133
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000134 llvm::GlobalValue::LinkageTypes TargetLinkage
135 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl()));
136
Rafael Espindola3c157452010-03-06 07:35:18 +0000137 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000138 return true;
139
John McCallc0bf4622010-02-23 00:48:20 +0000140 // Derive the type for the alias.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000141 llvm::PointerType *AliasType
John McCallc0bf4622010-02-23 00:48:20 +0000142 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
143
John McCallc0bf4622010-02-23 00:48:20 +0000144 // Find the referrent. Some aliases might require a bitcast, in
145 // which case the caller is responsible for ensuring the soundness
146 // of these semantics.
147 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
148 llvm::Constant *Aliasee = Ref;
149 if (Ref->getType() != AliasType)
150 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
151
John McCalld46f9852010-02-19 01:32:20 +0000152 // Create the alias with no name.
153 llvm::GlobalAlias *Alias =
John McCallc0bf4622010-02-23 00:48:20 +0000154 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld46f9852010-02-19 01:32:20 +0000155
John McCall1962bee2010-02-24 20:32:01 +0000156 // Switch any previous uses to the alias.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157 StringRef MangledName = getMangledName(AliasDecl);
John McCallf746aa62010-03-19 23:29:14 +0000158 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000159 if (Entry) {
John McCall1962bee2010-02-24 20:32:01 +0000160 assert(Entry->isDeclaration() && "definition already exists for alias");
161 assert(Entry->getType() == AliasType &&
162 "declaration exists with different type");
John McCallf746aa62010-03-19 23:29:14 +0000163 Alias->takeName(Entry);
John McCalld46f9852010-02-19 01:32:20 +0000164 Entry->replaceAllUsesWith(Alias);
165 Entry->eraseFromParent();
John McCallf746aa62010-03-19 23:29:14 +0000166 } else {
Anders Carlsson9a20d552010-06-22 16:16:50 +0000167 Alias->setName(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000168 }
John McCalld46f9852010-02-19 01:32:20 +0000169
170 // Finally, set up the alias with its proper name and attributes.
John McCall1fb0caa2010-10-22 21:05:15 +0000171 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
John McCalld46f9852010-02-19 01:32:20 +0000172
173 return false;
174}
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000175
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000176void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000177 // The constructor used for constructing this as a complete class;
178 // constucts the virtual bases, then calls the base constructor.
John McCall492bafc2011-05-17 21:05:49 +0000179 if (!D->getParent()->isAbstract()) {
Anders Carlsson8e0397a2011-05-08 17:25:05 +0000180 // We don't need to emit the complete ctor if the class is abstract.
181 EmitGlobal(GlobalDecl(D, Ctor_Complete));
182 }
John McCalld46f9852010-02-19 01:32:20 +0000183
184 // The constructor used for constructing this as a base class;
185 // ignores virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000186 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000187}
Anders Carlsson363c1842009-04-16 23:57:24 +0000188
John McCall1f6f9612011-03-09 08:12:35 +0000189void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
190 CXXCtorType ctorType) {
John McCalld46f9852010-02-19 01:32:20 +0000191 // The complete constructor is equivalent to the base constructor
192 // for classes with no virtual bases. Try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000193 if (ctorType == Ctor_Complete &&
194 !ctor->getParent()->getNumVBases() &&
195 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
196 GlobalDecl(ctor, Ctor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000197 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall1f6f9612011-03-09 08:12:35 +0000199 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(ctor, ctorType);
John McCalld26bc762011-03-09 04:27:21 +0000200
John McCall1f6f9612011-03-09 08:12:35 +0000201 llvm::Function *fn =
202 cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo));
203 setFunctionLinkage(ctor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
John McCall1f6f9612011-03-09 08:12:35 +0000205 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCall1f6f9612011-03-09 08:12:35 +0000207 SetFunctionDefinitionAttributes(ctor, fn);
208 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000209}
210
John McCalld46f9852010-02-19 01:32:20 +0000211llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000212CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
213 CXXCtorType ctorType,
214 const CGFunctionInfo *fnInfo) {
215 GlobalDecl GD(ctor, ctorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000216
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 StringRef name = getMangledName(GD);
John McCall1f6f9612011-03-09 08:12:35 +0000218 if (llvm::GlobalValue *existing = GetGlobalValue(name))
219 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000220
John McCall1f6f9612011-03-09 08:12:35 +0000221 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(ctor, ctorType);
222
223 const FunctionProtoType *proto = ctor->getType()->castAs<FunctionProtoType>();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000224 llvm::FunctionType *fnType =
John McCall1f6f9612011-03-09 08:12:35 +0000225 getTypes().GetFunctionType(*fnInfo, proto->isVariadic());
226 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000227 /*ForVTable=*/false));
Anders Carlsson363c1842009-04-16 23:57:24 +0000228}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000229
Anders Carlsson27ae5362009-04-17 01:58:57 +0000230void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000231 // The destructor in a virtual table is always a 'deleting'
232 // destructor, which calls the complete destructor and then uses the
233 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000234 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000235 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000236
237 // The destructor used for destructing this as a most-derived class;
238 // call the base destructor and then destructs any virtual bases.
Douglas Gregor3e23d682011-07-26 23:18:30 +0000239 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000240
241 // The destructor used for destructing this as a base class; ignores
242 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000243 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000244}
245
John McCall1f6f9612011-03-09 08:12:35 +0000246void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
247 CXXDtorType dtorType) {
John McCalld46f9852010-02-19 01:32:20 +0000248 // The complete destructor is equivalent to the base destructor for
249 // classes with no virtual bases, so try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000250 if (dtorType == Dtor_Complete &&
251 !dtor->getParent()->getNumVBases() &&
252 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
253 GlobalDecl(dtor, Dtor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000254 return;
255
John McCallc0bf4622010-02-23 00:48:20 +0000256 // The base destructor is equivalent to the base destructor of its
257 // base class if there is exactly one non-virtual base class with a
258 // non-trivial destructor, there are no fields with a non-trivial
259 // destructor, and the body of the destructor is trivial.
John McCall1f6f9612011-03-09 08:12:35 +0000260 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallc0bf4622010-02-23 00:48:20 +0000261 return;
262
John McCall1f6f9612011-03-09 08:12:35 +0000263 const CGFunctionInfo &fnInfo = getTypes().getFunctionInfo(dtor, dtorType);
John McCalld26bc762011-03-09 04:27:21 +0000264
John McCall1f6f9612011-03-09 08:12:35 +0000265 llvm::Function *fn =
266 cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo));
267 setFunctionLinkage(dtor, fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
John McCall1f6f9612011-03-09 08:12:35 +0000269 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
John McCall1f6f9612011-03-09 08:12:35 +0000271 SetFunctionDefinitionAttributes(dtor, fn);
272 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000273}
274
John McCalld46f9852010-02-19 01:32:20 +0000275llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000276CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
277 CXXDtorType dtorType,
278 const CGFunctionInfo *fnInfo) {
279 GlobalDecl GD(dtor, dtorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000280
Chris Lattner5f9e2722011-07-23 10:55:15 +0000281 StringRef name = getMangledName(GD);
John McCall1f6f9612011-03-09 08:12:35 +0000282 if (llvm::GlobalValue *existing = GetGlobalValue(name))
283 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000284
John McCall1f6f9612011-03-09 08:12:35 +0000285 if (!fnInfo) fnInfo = &getTypes().getFunctionInfo(dtor, dtorType);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner2acc6e32011-07-18 04:24:23 +0000287 llvm::FunctionType *fnType =
John McCall1f6f9612011-03-09 08:12:35 +0000288 getTypes().GetFunctionType(*fnInfo, false);
289
290 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000291 /*ForVTable=*/false));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000292}
293
Anders Carlsson046c2942010-04-17 20:15:18 +0000294static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000295 llvm::Value *This, llvm::Type *Ty) {
Dan Gohman043fb9a2010-10-26 18:44:08 +0000296 Ty = Ty->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000297
Dan Gohman043fb9a2010-10-26 18:44:08 +0000298 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000299 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000300 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000301 return CGF.Builder.CreateLoad(VFuncPtr);
302}
303
304llvm::Value *
305CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000306 llvm::Type *Ty) {
Anders Carlsson566abee2009-11-13 04:45:41 +0000307 MD = MD->getCanonicalDecl();
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000308 uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000309
Anders Carlssonaf440352010-03-23 04:11:45 +0000310 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000311}
312
Fariborz Jahanian27262672011-01-20 17:19:02 +0000313/// BuildVirtualCall - This routine is to support gcc's kext ABI making
314/// indirect call to virtual functions. It makes the call through indexing
315/// into the vtable.
316llvm::Value *
317CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
318 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000319 llvm::Type *Ty) {
Fariborz Jahanian27262672011-01-20 17:19:02 +0000320 llvm::Value *VTable = 0;
321 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
322 "BuildAppleKextVirtualCall - bad Qual kind");
323
324 const Type *QTy = Qual->getAsType();
325 QualType T = QualType(QTy, 0);
326 const RecordType *RT = T->getAs<RecordType>();
327 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
328 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000329
330 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
331 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
332
Fariborz Jahanian27262672011-01-20 17:19:02 +0000333 VTable = CGM.getVTables().GetAddrOfVTable(RD);
334 Ty = Ty->getPointerTo()->getPointerTo();
335 VTable = Builder.CreateBitCast(VTable, Ty);
336 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
337 MD = MD->getCanonicalDecl();
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000338 uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD);
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000339 uint64_t AddressPoint =
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000340 CGM.getVTableContext().getVTableLayout(RD)
341 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
Fariborz Jahaniana50e33e2011-01-28 23:42:29 +0000342 VTableIndex += AddressPoint;
Fariborz Jahanian27262672011-01-20 17:19:02 +0000343 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000344 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
345 return Builder.CreateLoad(VFuncPtr);
Fariborz Jahanian27262672011-01-20 17:19:02 +0000346}
347
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000348/// BuildVirtualCall - This routine makes indirect vtable call for
349/// call to virtual destructors. It returns 0 if it could not do it.
350llvm::Value *
351CodeGenFunction::BuildAppleKextVirtualDestructorCall(
352 const CXXDestructorDecl *DD,
353 CXXDtorType Type,
354 const CXXRecordDecl *RD) {
355 llvm::Value * Callee = 0;
356 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
357 // FIXME. Dtor_Base dtor is always direct!!
358 // It need be somehow inline expanded into the caller.
359 // -O does that. But need to support -O0 as well.
360 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000361 // Compute the function type we're calling.
362 const CGFunctionInfo *FInfo =
363 &CGM.getTypes().getFunctionInfo(cast<CXXDestructorDecl>(MD),
364 Dtor_Complete);
365 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000366 llvm::Type *Ty
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000367 = CGM.getTypes().GetFunctionType(*FInfo, FPT->isVariadic());
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000368
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000369 llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD);
370 Ty = Ty->getPointerTo()->getPointerTo();
371 VTable = Builder.CreateBitCast(VTable, Ty);
Fariborz Jahanian771c6782011-02-03 19:27:17 +0000372 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000373 uint64_t VTableIndex =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000374 CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type));
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000375 uint64_t AddressPoint =
Peter Collingbournee09cdf42011-09-26 01:56:50 +0000376 CGM.getVTableContext().getVTableLayout(RD)
377 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000378 VTableIndex += AddressPoint;
379 llvm::Value *VFuncPtr =
John McCalld16c2cf2011-02-08 08:22:06 +0000380 Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
381 Callee = Builder.CreateLoad(VFuncPtr);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000382 }
383 return Callee;
384}
385
Anders Carlsson566abee2009-11-13 04:45:41 +0000386llvm::Value *
387CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000388 llvm::Value *This, llvm::Type *Ty) {
Anders Carlsson566abee2009-11-13 04:45:41 +0000389 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000390 uint64_t VTableIndex =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000391 CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000392
Anders Carlssonaf440352010-03-23 04:11:45 +0000393 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000394}
Charles Davis3a811f12010-05-25 19:52:27 +0000395