blob: 9904906571230401ac39a85c4e888edda83426ec [file] [log] [blame]
Devang Patelcbc7bc92010-10-04 21:15:33 +00001//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
Anders Carlsson87fc5a52008-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 Stump11289f42009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlsson87fc5a52008-08-22 16:00:37 +000015
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "CodeGenModule.h"
Charles Davis4e786dd2010-05-25 19:52:27 +000017#include "CGCXXABI.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000018#include "CodeGenFunction.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlssone5fd6f22009-04-03 22:50:24 +000021#include "clang/AST/DeclCXX.h"
Anders Carlsson131be8b2008-08-23 19:42:54 +000022#include "clang/AST/DeclObjC.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000023#include "clang/AST/Mangle.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/RecordLayout.h"
Anders Carlsson52d78a52009-09-27 18:58:34 +000025#include "clang/AST/StmtCXX.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000027#include "llvm/ADT/StringExtras.h"
Anders Carlsson87fc5a52008-08-22 16:00:37 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallf8ff7b92010-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
Rafael Espindolad967bad2013-11-13 23:20:45 +000037 // Producing an alias to a base class ctor/dtor can degrade debug quality
Alp Tokerf6a24ce2013-12-05 16:25:25 +000038 // as the debugger cannot tell them apart.
Rafael Espindolad967bad2013-11-13 23:20:45 +000039 if (getCodeGenOpts().OptimizationLevel == 0)
40 return true;
41
John McCallf8ff7b92010-02-23 00:48:20 +000042 // If the destructor doesn't have a trivial body, we have to emit it
43 // separately.
Anders Carlsson9bd7d162011-05-14 23:26:09 +000044 if (!D->hasTrivialBody())
John McCallf8ff7b92010-02-23 00:48:20 +000045 return true;
46
47 const CXXRecordDecl *Class = D->getParent();
48
49 // If we need to manipulate a VTT parameter, give up.
50 if (Class->getNumVBases()) {
51 // Extra Credit: passing extra parameters is perfectly safe
52 // in many calling conventions, so only bail out if the ctor's
53 // calling convention is nonstandard.
54 return true;
55 }
56
John McCall2b3c5532011-02-13 00:46:43 +000057 // If any field has a non-trivial destructor, we have to emit the
58 // destructor separately.
John McCallf8ff7b92010-02-23 00:48:20 +000059 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
60 E = Class->field_end(); I != E; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +000061 if (I->getType().isDestructedType())
John McCall2b3c5532011-02-13 00:46:43 +000062 return true;
John McCallf8ff7b92010-02-23 00:48:20 +000063
64 // Try to find a unique base class with a non-trivial destructor.
65 const CXXRecordDecl *UniqueBase = 0;
66 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
67 E = Class->bases_end(); I != E; ++I) {
68
69 // We're in the base destructor, so skip virtual bases.
70 if (I->isVirtual()) continue;
71
72 // Skip base classes with trivial destructors.
73 const CXXRecordDecl *Base
74 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
75 if (Base->hasTrivialDestructor()) continue;
76
77 // If we've already found a base class with a non-trivial
78 // destructor, give up.
79 if (UniqueBase) return true;
80 UniqueBase = Base;
81 }
82
83 // If we didn't find any bases with a non-trivial destructor, then
84 // the base destructor is actually effectively trivial, which can
85 // happen if it was needlessly user-defined or if there are virtual
86 // bases with non-trivial destructors.
87 if (!UniqueBase)
88 return true;
89
90 // If the base is at a non-zero offset, give up.
91 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
Benjamin Kramer2ef30312012-07-04 18:45:14 +000092 if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
John McCallf8ff7b92010-02-23 00:48:20 +000093 return true;
94
Rafael Espindola191b9512014-03-05 21:04:41 +000095 // Give up if the calling conventions don't match. We could update the call,
96 // but it is probably not worth it.
Rafael Espindola961ba212013-11-09 01:57:21 +000097 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
Rafael Espindola191b9512014-03-05 21:04:41 +000098 if (BaseD->getType()->getAs<FunctionType>()->getCallConv() !=
99 D->getType()->getAs<FunctionType>()->getCallConv())
100 return true;
101
John McCallf8ff7b92010-02-23 00:48:20 +0000102 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000103 GlobalDecl(BaseD, Dtor_Base),
104 false);
John McCallf8ff7b92010-02-23 00:48:20 +0000105}
106
John McCalld4324142010-02-19 01:32:20 +0000107/// Try to emit a definition as a global alias for another definition.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000108/// If \p InEveryTU is true, we know that an equivalent alias can be produced
109/// in every translation unit.
John McCalld4324142010-02-19 01:32:20 +0000110bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000111 GlobalDecl TargetDecl,
112 bool InEveryTU) {
John McCalld4324142010-02-19 01:32:20 +0000113 if (!getCodeGenOpts().CXXCtorDtorAliases)
114 return true;
115
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000116 // The alias will use the linkage of the referent. If we can't
John McCalld4324142010-02-19 01:32:20 +0000117 // support aliases with that linkage, fail.
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000118 llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
John McCalld4324142010-02-19 01:32:20 +0000119
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000120 // We can't use an alias if the linkage is not valid for one.
121 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola4c489c72010-03-05 01:21:10 +0000122 return true;
123
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000124 llvm::GlobalValue::LinkageTypes TargetLinkage =
125 getFunctionLinkage(TargetDecl);
126
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000127 // Check if we have it already.
128 StringRef MangledName = getMangledName(AliasDecl);
129 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
130 if (Entry && !Entry->isDeclaration())
131 return false;
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000132 if (Replacements.count(MangledName))
133 return false;
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000134
John McCallf8ff7b92010-02-23 00:48:20 +0000135 // Derive the type for the alias.
Chris Lattner2192fe52011-07-18 04:24:23 +0000136 llvm::PointerType *AliasType
John McCallf8ff7b92010-02-23 00:48:20 +0000137 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
138
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000139 // Find the referent. Some aliases might require a bitcast, in
John McCallf8ff7b92010-02-23 00:48:20 +0000140 // which case the caller is responsible for ensuring the soundness
141 // of these semantics.
142 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
143 llvm::Constant *Aliasee = Ref;
144 if (Ref->getType() != AliasType)
145 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
146
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +0000147 // Instead of creating as alias to a linkonce_odr, replace all of the uses
148 // of the aliassee.
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000149 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
150 (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage ||
151 !TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000152 // FIXME: An extern template instantiation will create functions with
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000153 // linkage "AvailableExternally". In libc++, some classes also define
154 // members with attribute "AlwaysInline" and expect no reference to
155 // be generated. It is desirable to reenable this optimisation after
156 // corresponding LLVM changes.
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +0000157 Replacements[MangledName] = Aliasee;
158 return false;
159 }
160
Rafael Espindola961ba212013-11-09 01:57:21 +0000161 if (!InEveryTU) {
162 /// If we don't have a definition for the destructor yet, don't
163 /// emit. We can't emit aliases to declarations; that's just not
164 /// how aliases work.
165 if (Ref->isDeclaration())
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000166 return true;
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000167 }
168
Rafael Espindola129d3132013-11-12 22:06:46 +0000169 // Don't create an alias to a linker weak symbol. This avoids producing
170 // different COMDATs in different TUs. Another option would be to
171 // output the alias both for weak_odr and linkonce_odr, but that
172 // requires explicit comdat support in the IL.
173 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
174 return true;
175
John McCalld4324142010-02-19 01:32:20 +0000176 // Create the alias with no name.
177 llvm::GlobalAlias *Alias =
John McCallf8ff7b92010-02-23 00:48:20 +0000178 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld4324142010-02-19 01:32:20 +0000179
John McCallaea181d2010-02-24 20:32:01 +0000180 // Switch any previous uses to the alias.
John McCalld4324142010-02-19 01:32:20 +0000181 if (Entry) {
John McCallaea181d2010-02-24 20:32:01 +0000182 assert(Entry->getType() == AliasType &&
183 "declaration exists with different type");
John McCall7ec50432010-03-19 23:29:14 +0000184 Alias->takeName(Entry);
John McCalld4324142010-02-19 01:32:20 +0000185 Entry->replaceAllUsesWith(Alias);
186 Entry->eraseFromParent();
John McCall7ec50432010-03-19 23:29:14 +0000187 } else {
Anders Carlssonea836bc2010-06-22 16:16:50 +0000188 Alias->setName(MangledName);
John McCalld4324142010-02-19 01:32:20 +0000189 }
John McCalld4324142010-02-19 01:32:20 +0000190
191 // Finally, set up the alias with its proper name and attributes.
John McCall457a04e2010-10-22 21:05:15 +0000192 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
John McCalld4324142010-02-19 01:32:20 +0000193
194 return false;
195}
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000196
John McCall46288ef2011-03-09 08:12:35 +0000197void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
198 CXXCtorType ctorType) {
Reid Kleckner340ad862014-01-13 22:57:31 +0000199 if (!getTarget().getCXXABI().hasConstructorVariants()) {
200 // If there are no constructor variants, always emit the complete destructor.
201 ctorType = Ctor_Complete;
202 } else if (!ctor->getParent()->getNumVBases() &&
203 (ctorType == Ctor_Complete || ctorType == Ctor_Base)) {
204 // The complete constructor is equivalent to the base constructor
205 // for classes with no virtual bases. Try to emit it as an alias.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000206 bool ProducedAlias =
207 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
208 GlobalDecl(ctor, Ctor_Base), true);
209 if (ctorType == Ctor_Complete && ProducedAlias)
210 return;
211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
John McCalla729c622012-02-17 03:33:10 +0000213 const CGFunctionInfo &fnInfo =
214 getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
John McCalla738c252011-03-09 04:27:21 +0000215
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000216 llvm::Function *fn = cast<llvm::Function>(
217 GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo, true));
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000218 setFunctionLinkage(GlobalDecl(ctor, ctorType), fn);
Mike Stump11289f42009-09-09 15:08:12 +0000219
John McCall46288ef2011-03-09 08:12:35 +0000220 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000221
John McCall46288ef2011-03-09 08:12:35 +0000222 SetFunctionDefinitionAttributes(ctor, fn);
223 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000224}
225
John McCalld4324142010-02-19 01:32:20 +0000226llvm::GlobalValue *
John McCall46288ef2011-03-09 08:12:35 +0000227CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
228 CXXCtorType ctorType,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000229 const CGFunctionInfo *fnInfo,
230 bool DontDefer) {
John McCall46288ef2011-03-09 08:12:35 +0000231 GlobalDecl GD(ctor, ctorType);
Anders Carlsson09b5fe62010-06-09 02:30:12 +0000232
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000233 StringRef name = getMangledName(GD);
John McCall46288ef2011-03-09 08:12:35 +0000234 if (llvm::GlobalValue *existing = GetGlobalValue(name))
235 return existing;
John McCalld4324142010-02-19 01:32:20 +0000236
John McCalla729c622012-02-17 03:33:10 +0000237 if (!fnInfo)
238 fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
John McCall46288ef2011-03-09 08:12:35 +0000239
John McCalla729c622012-02-17 03:33:10 +0000240 llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo);
John McCall46288ef2011-03-09 08:12:35 +0000241 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000242 /*ForVTable=*/false,
243 DontDefer));
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000244}
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000245
John McCall46288ef2011-03-09 08:12:35 +0000246void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
247 CXXDtorType dtorType) {
John McCalld4324142010-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.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000250 if (!dtor->getParent()->getNumVBases() &&
251 (dtorType == Dtor_Complete || dtorType == Dtor_Base)) {
252 bool ProducedAlias =
253 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
254 GlobalDecl(dtor, Dtor_Base), true);
255 if (ProducedAlias) {
256 if (dtorType == Dtor_Complete)
257 return;
258 if (dtor->isVirtual())
259 getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
260 }
261 }
John McCalld4324142010-02-19 01:32:20 +0000262
John McCallf8ff7b92010-02-23 00:48:20 +0000263 // The base destructor is equivalent to the base destructor of its
264 // base class if there is exactly one non-virtual base class with a
265 // non-trivial destructor, there are no fields with a non-trivial
266 // destructor, and the body of the destructor is trivial.
John McCall46288ef2011-03-09 08:12:35 +0000267 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallf8ff7b92010-02-23 00:48:20 +0000268 return;
269
John McCalla729c622012-02-17 03:33:10 +0000270 const CGFunctionInfo &fnInfo =
271 getTypes().arrangeCXXDestructor(dtor, dtorType);
John McCalla738c252011-03-09 04:27:21 +0000272
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000273 llvm::Function *fn = cast<llvm::Function>(
274 GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo, 0, true));
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000275 setFunctionLinkage(GlobalDecl(dtor, dtorType), fn);
Mike Stump11289f42009-09-09 15:08:12 +0000276
John McCall46288ef2011-03-09 08:12:35 +0000277 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000278
John McCall46288ef2011-03-09 08:12:35 +0000279 SetFunctionDefinitionAttributes(dtor, fn);
280 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000281}
282
John McCalld4324142010-02-19 01:32:20 +0000283llvm::GlobalValue *
John McCall46288ef2011-03-09 08:12:35 +0000284CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
285 CXXDtorType dtorType,
Reid Klecknere7de47e2013-07-22 13:51:44 +0000286 const CGFunctionInfo *fnInfo,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000287 llvm::FunctionType *fnType,
288 bool DontDefer) {
John McCall46288ef2011-03-09 08:12:35 +0000289 GlobalDecl GD(dtor, dtorType);
Anders Carlsson09b5fe62010-06-09 02:30:12 +0000290
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 StringRef name = getMangledName(GD);
John McCall46288ef2011-03-09 08:12:35 +0000292 if (llvm::GlobalValue *existing = GetGlobalValue(name))
293 return existing;
John McCalld4324142010-02-19 01:32:20 +0000294
Reid Klecknere7de47e2013-07-22 13:51:44 +0000295 if (!fnType) {
296 if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType);
297 fnType = getTypes().GetFunctionType(*fnInfo);
298 }
John McCall46288ef2011-03-09 08:12:35 +0000299 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000300 /*ForVTable=*/false,
301 DontDefer));
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000302}
303
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000304static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
305 GlobalDecl GD,
306 llvm::Type *Ty,
307 const CXXRecordDecl *RD) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000308 assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
309 "No kext in Microsoft ABI");
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000310 GD = GD.getCanonicalDecl();
311 CodeGenModule &CGM = CGF.CGM;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000312 llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000313 Ty = Ty->getPointerTo()->getPointerTo();
314 VTable = CGF.Builder.CreateBitCast(VTable, Ty);
315 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000316 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000317 uint64_t AddressPoint =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000318 CGM.getItaniumVTableContext().getVTableLayout(RD)
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000319 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
320 VTableIndex += AddressPoint;
321 llvm::Value *VFuncPtr =
322 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
Anders Carlssone828c362009-11-13 04:45:41 +0000323 return CGF.Builder.CreateLoad(VFuncPtr);
324}
325
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000326/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000327/// indirect call to virtual functions. It makes the call through indexing
328/// into the vtable.
329llvm::Value *
330CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
331 NestedNameSpecifier *Qual,
Chris Lattner2192fe52011-07-18 04:24:23 +0000332 llvm::Type *Ty) {
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000333 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
334 "BuildAppleKextVirtualCall - bad Qual kind");
335
336 const Type *QTy = Qual->getAsType();
337 QualType T = QualType(QTy, 0);
338 const RecordType *RT = T->getAs<RecordType>();
339 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
340 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000341
342 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
343 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000344
345 return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000346}
347
Fariborz Jahanian265c3252011-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) {
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000355 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
356 // FIXME. Dtor_Base dtor is always direct!!
357 // It need be somehow inline expanded into the caller.
358 // -O does that. But need to support -O0 as well.
359 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000360 // Compute the function type we're calling.
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000361 const CGFunctionInfo &FInfo =
362 CGM.getTypes().arrangeCXXDestructor(DD, Dtor_Complete);
John McCalla729c622012-02-17 03:33:10 +0000363 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000364 return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000365 }
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000366 return 0;
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000367}