blob: 583017cfcd9e2846b168e62a38fd11a3aa37b6a5 [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
Hans Wennborg853ae942014-05-30 16:59:42 +000047 // For exported destructors, we need a full definition.
48 if (D->hasAttr<DLLExportAttr>())
49 return true;
50
John McCallf8ff7b92010-02-23 00:48:20 +000051 const CXXRecordDecl *Class = D->getParent();
52
53 // If we need to manipulate a VTT parameter, give up.
54 if (Class->getNumVBases()) {
55 // Extra Credit: passing extra parameters is perfectly safe
56 // in many calling conventions, so only bail out if the ctor's
57 // calling convention is nonstandard.
58 return true;
59 }
60
John McCall2b3c5532011-02-13 00:46:43 +000061 // If any field has a non-trivial destructor, we have to emit the
62 // destructor separately.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +000063 for (const auto *I : Class->fields())
David Blaikie2d7c57e2012-04-30 02:36:29 +000064 if (I->getType().isDestructedType())
John McCall2b3c5532011-02-13 00:46:43 +000065 return true;
John McCallf8ff7b92010-02-23 00:48:20 +000066
67 // Try to find a unique base class with a non-trivial destructor.
Craig Topper8a13c412014-05-21 05:09:00 +000068 const CXXRecordDecl *UniqueBase = nullptr;
Aaron Ballman574705e2014-03-13 15:41:46 +000069 for (const auto &I : Class->bases()) {
John McCallf8ff7b92010-02-23 00:48:20 +000070
71 // We're in the base destructor, so skip virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +000072 if (I.isVirtual()) continue;
John McCallf8ff7b92010-02-23 00:48:20 +000073
74 // Skip base classes with trivial destructors.
Rafael Espindola2ae250c2014-05-09 00:08:36 +000075 const auto *Base =
76 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
John McCallf8ff7b92010-02-23 00:48:20 +000077 if (Base->hasTrivialDestructor()) continue;
78
79 // If we've already found a base class with a non-trivial
80 // destructor, give up.
81 if (UniqueBase) return true;
82 UniqueBase = Base;
83 }
84
85 // If we didn't find any bases with a non-trivial destructor, then
86 // the base destructor is actually effectively trivial, which can
87 // happen if it was needlessly user-defined or if there are virtual
88 // bases with non-trivial destructors.
89 if (!UniqueBase)
90 return true;
91
92 // If the base is at a non-zero offset, give up.
93 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
Benjamin Kramer2ef30312012-07-04 18:45:14 +000094 if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
John McCallf8ff7b92010-02-23 00:48:20 +000095 return true;
96
Rafael Espindola191b9512014-03-05 21:04:41 +000097 // Give up if the calling conventions don't match. We could update the call,
98 // but it is probably not worth it.
Rafael Espindola961ba212013-11-09 01:57:21 +000099 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
Rafael Espindola191b9512014-03-05 21:04:41 +0000100 if (BaseD->getType()->getAs<FunctionType>()->getCallConv() !=
101 D->getType()->getAs<FunctionType>()->getCallConv())
102 return true;
103
John McCallf8ff7b92010-02-23 00:48:20 +0000104 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000105 GlobalDecl(BaseD, Dtor_Base),
106 false);
John McCallf8ff7b92010-02-23 00:48:20 +0000107}
108
John McCalld4324142010-02-19 01:32:20 +0000109/// Try to emit a definition as a global alias for another definition.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000110/// If \p InEveryTU is true, we know that an equivalent alias can be produced
111/// in every translation unit.
John McCalld4324142010-02-19 01:32:20 +0000112bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000113 GlobalDecl TargetDecl,
114 bool InEveryTU) {
John McCalld4324142010-02-19 01:32:20 +0000115 if (!getCodeGenOpts().CXXCtorDtorAliases)
116 return true;
117
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000118 // The alias will use the linkage of the referent. If we can't
John McCalld4324142010-02-19 01:32:20 +0000119 // support aliases with that linkage, fail.
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000120 llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
John McCalld4324142010-02-19 01:32:20 +0000121
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000122 // We can't use an alias if the linkage is not valid for one.
123 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola4c489c72010-03-05 01:21:10 +0000124 return true;
125
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000126 llvm::GlobalValue::LinkageTypes TargetLinkage =
127 getFunctionLinkage(TargetDecl);
128
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000129 // Check if we have it already.
130 StringRef MangledName = getMangledName(AliasDecl);
131 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
132 if (Entry && !Entry->isDeclaration())
133 return false;
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000134 if (Replacements.count(MangledName))
135 return false;
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000136
John McCallf8ff7b92010-02-23 00:48:20 +0000137 // Derive the type for the alias.
Chris Lattner2192fe52011-07-18 04:24:23 +0000138 llvm::PointerType *AliasType
John McCallf8ff7b92010-02-23 00:48:20 +0000139 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
140
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000141 // Find the referent. Some aliases might require a bitcast, in
John McCallf8ff7b92010-02-23 00:48:20 +0000142 // which case the caller is responsible for ensuring the soundness
143 // of these semantics.
Rafael Espindola2ae250c2014-05-09 00:08:36 +0000144 auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
Rafael Espindola27c60b52014-06-03 02:42:01 +0000145 llvm::Constant *Aliasee = Ref;
146 if (Ref->getType() != AliasType)
147 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
John McCallf8ff7b92010-02-23 00:48:20 +0000148
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +0000149 // Instead of creating as alias to a linkonce_odr, replace all of the uses
Rafael Espindolab2633b92014-05-16 19:35:48 +0000150 // of the aliasee.
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000151 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
152 (TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage ||
153 !TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000154 // FIXME: An extern template instantiation will create functions with
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000155 // linkage "AvailableExternally". In libc++, some classes also define
156 // members with attribute "AlwaysInline" and expect no reference to
157 // be generated. It is desirable to reenable this optimisation after
158 // corresponding LLVM changes.
Rafael Espindola27c60b52014-06-03 02:42:01 +0000159 Replacements[MangledName] = Aliasee;
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +0000160 return false;
161 }
162
Rafael Espindola961ba212013-11-09 01:57:21 +0000163 if (!InEveryTU) {
164 /// If we don't have a definition for the destructor yet, don't
165 /// emit. We can't emit aliases to declarations; that's just not
166 /// how aliases work.
Rafael Espindola27c60b52014-06-03 02:42:01 +0000167 if (Ref->isDeclaration())
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000168 return true;
Rafael Espindola2e2995b2013-11-05 21:37:29 +0000169 }
170
Rafael Espindola129d3132013-11-12 22:06:46 +0000171 // Don't create an alias to a linker weak symbol. This avoids producing
172 // different COMDATs in different TUs. Another option would be to
173 // output the alias both for weak_odr and linkonce_odr, but that
174 // requires explicit comdat support in the IL.
175 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
176 return true;
177
John McCalld4324142010-02-19 01:32:20 +0000178 // Create the alias with no name.
Rafael Espindola234405b2014-05-17 21:30:14 +0000179 auto *Alias = llvm::GlobalAlias::create(AliasType->getElementType(), 0,
Rafael Espindola27c60b52014-06-03 02:42:01 +0000180 Linkage, "", Aliasee, &getModule());
John McCalld4324142010-02-19 01:32:20 +0000181
John McCallaea181d2010-02-24 20:32:01 +0000182 // Switch any previous uses to the alias.
John McCalld4324142010-02-19 01:32:20 +0000183 if (Entry) {
John McCallaea181d2010-02-24 20:32:01 +0000184 assert(Entry->getType() == AliasType &&
185 "declaration exists with different type");
John McCall7ec50432010-03-19 23:29:14 +0000186 Alias->takeName(Entry);
John McCalld4324142010-02-19 01:32:20 +0000187 Entry->replaceAllUsesWith(Alias);
188 Entry->eraseFromParent();
John McCall7ec50432010-03-19 23:29:14 +0000189 } else {
Anders Carlssonea836bc2010-06-22 16:16:50 +0000190 Alias->setName(MangledName);
John McCalld4324142010-02-19 01:32:20 +0000191 }
John McCalld4324142010-02-19 01:32:20 +0000192
193 // Finally, set up the alias with its proper name and attributes.
John McCall457a04e2010-10-22 21:05:15 +0000194 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
John McCalld4324142010-02-19 01:32:20 +0000195
196 return false;
197}
Anders Carlssonbd7d11f2009-05-11 23:37:08 +0000198
John McCall46288ef2011-03-09 08:12:35 +0000199void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
200 CXXCtorType ctorType) {
Reid Kleckner340ad862014-01-13 22:57:31 +0000201 if (!getTarget().getCXXABI().hasConstructorVariants()) {
202 // If there are no constructor variants, always emit the complete destructor.
203 ctorType = Ctor_Complete;
204 } else if (!ctor->getParent()->getNumVBases() &&
205 (ctorType == Ctor_Complete || ctorType == Ctor_Base)) {
206 // The complete constructor is equivalent to the base constructor
207 // for classes with no virtual bases. Try to emit it as an alias.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000208 bool ProducedAlias =
209 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
210 GlobalDecl(ctor, Ctor_Base), true);
211 if (ctorType == Ctor_Complete && ProducedAlias)
212 return;
213 }
Mike Stump11289f42009-09-09 15:08:12 +0000214
John McCalla729c622012-02-17 03:33:10 +0000215 const CGFunctionInfo &fnInfo =
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000216 getTypes().arrangeCXXStructorDeclaration(ctor, getFromCtorType(ctorType));
John McCalla738c252011-03-09 04:27:21 +0000217
Rafael Espindola2ae250c2014-05-09 00:08:36 +0000218 auto *fn = cast<llvm::Function>(
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000219 GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo, true));
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000220 setFunctionLinkage(GlobalDecl(ctor, ctorType), fn);
Mike Stump11289f42009-09-09 15:08:12 +0000221
John McCall46288ef2011-03-09 08:12:35 +0000222 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Rafael Espindolae033c8c2014-05-08 15:26:12 +0000224 setFunctionDefinitionAttributes(ctor, fn);
John McCall46288ef2011-03-09 08:12:35 +0000225 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000226}
227
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000228llvm::GlobalValue *CodeGenModule::getAddrOfCXXStructor(
229 const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo,
230 llvm::FunctionType *FnType, bool DontDefer) {
231 GlobalDecl GD;
232 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
233 GD = GlobalDecl(CD, toCXXCtorType(Type));
234 } else {
235 auto *DD = dyn_cast<CXXDestructorDecl>(MD);
236 GD = GlobalDecl(DD, toCXXDtorType(Type));
237 }
John McCalld4324142010-02-19 01:32:20 +0000238
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000239 StringRef Name = getMangledName(GD);
240 if (llvm::GlobalValue *Existing = GetGlobalValue(Name))
241 return Existing;
John McCall46288ef2011-03-09 08:12:35 +0000242
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000243 if (!FnType) {
244 if (!FnInfo)
245 FnInfo = &getTypes().arrangeCXXStructorDeclaration(MD, Type);
246 FnType = getTypes().GetFunctionType(*FnInfo);
247 }
248
249 return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FnType, GD,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000250 /*ForVTable=*/false,
251 DontDefer));
Anders Carlssone8eeffd2009-04-16 23:57:24 +0000252}
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000253
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000254llvm::GlobalValue *CodeGenModule::GetAddrOfCXXConstructor(
255 const CXXConstructorDecl *ctor, CXXCtorType ctorType,
256 const CGFunctionInfo *fnInfo, bool DontDefer) {
257 return getAddrOfCXXStructor(ctor, getFromCtorType(ctorType), fnInfo, nullptr,
258 DontDefer);
259}
260
John McCall46288ef2011-03-09 08:12:35 +0000261void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
262 CXXDtorType dtorType) {
John McCalld4324142010-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.
Rafael Espindola3f643bd2013-11-04 18:38:59 +0000265 if (!dtor->getParent()->getNumVBases() &&
266 (dtorType == Dtor_Complete || dtorType == Dtor_Base)) {
267 bool ProducedAlias =
268 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
269 GlobalDecl(dtor, Dtor_Base), true);
270 if (ProducedAlias) {
271 if (dtorType == Dtor_Complete)
272 return;
273 if (dtor->isVirtual())
274 getVTables().EmitThunks(GlobalDecl(dtor, Dtor_Complete));
275 }
276 }
John McCalld4324142010-02-19 01:32:20 +0000277
John McCallf8ff7b92010-02-23 00:48:20 +0000278 // The base destructor is equivalent to the base destructor of its
279 // base class if there is exactly one non-virtual base class with a
280 // non-trivial destructor, there are no fields with a non-trivial
281 // destructor, and the body of the destructor is trivial.
John McCall46288ef2011-03-09 08:12:35 +0000282 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallf8ff7b92010-02-23 00:48:20 +0000283 return;
284
John McCalla729c622012-02-17 03:33:10 +0000285 const CGFunctionInfo &fnInfo =
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000286 getTypes().arrangeCXXStructorDeclaration(dtor, getFromDtorType(dtorType));
John McCalla738c252011-03-09 04:27:21 +0000287
Rafael Espindola2ae250c2014-05-09 00:08:36 +0000288 auto *fn = cast<llvm::Function>(
Craig Topper8a13c412014-05-21 05:09:00 +0000289 GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo, nullptr, true));
Peter Collingbourne4d90dba2013-06-05 17:49:37 +0000290 setFunctionLinkage(GlobalDecl(dtor, dtorType), fn);
Mike Stump11289f42009-09-09 15:08:12 +0000291
John McCall46288ef2011-03-09 08:12:35 +0000292 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Rafael Espindolae033c8c2014-05-08 15:26:12 +0000294 setFunctionDefinitionAttributes(dtor, fn);
John McCall46288ef2011-03-09 08:12:35 +0000295 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000296}
297
John McCalld4324142010-02-19 01:32:20 +0000298llvm::GlobalValue *
John McCall46288ef2011-03-09 08:12:35 +0000299CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
300 CXXDtorType dtorType,
Reid Klecknere7de47e2013-07-22 13:51:44 +0000301 const CGFunctionInfo *fnInfo,
Rafael Espindola94abb8f2013-12-09 04:29:47 +0000302 llvm::FunctionType *fnType,
303 bool DontDefer) {
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000304 return getAddrOfCXXStructor(dtor, getFromDtorType(dtorType), fnInfo, fnType,
305 DontDefer);
Anders Carlssoneaa28f72009-04-17 01:58:57 +0000306}
307
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000308static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
309 GlobalDecl GD,
310 llvm::Type *Ty,
311 const CXXRecordDecl *RD) {
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +0000312 assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
313 "No kext in Microsoft ABI");
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000314 GD = GD.getCanonicalDecl();
315 CodeGenModule &CGM = CGF.CGM;
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +0000316 llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000317 Ty = Ty->getPointerTo()->getPointerTo();
318 VTable = CGF.Builder.CreateBitCast(VTable, Ty);
319 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000320 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000321 uint64_t AddressPoint =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +0000322 CGM.getItaniumVTableContext().getVTableLayout(RD)
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000323 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
324 VTableIndex += AddressPoint;
325 llvm::Value *VFuncPtr =
326 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
Anders Carlssone828c362009-11-13 04:45:41 +0000327 return CGF.Builder.CreateLoad(VFuncPtr);
328}
329
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000330/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000331/// indirect call to virtual functions. It makes the call through indexing
332/// into the vtable.
333llvm::Value *
334CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
335 NestedNameSpecifier *Qual,
Chris Lattner2192fe52011-07-18 04:24:23 +0000336 llvm::Type *Ty) {
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000337 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
338 "BuildAppleKextVirtualCall - bad Qual kind");
339
340 const Type *QTy = Qual->getAsType();
341 QualType T = QualType(QTy, 0);
342 const RecordType *RT = T->getAs<RecordType>();
343 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
Rafael Espindola2ae250c2014-05-09 00:08:36 +0000344 const auto *RD = cast<CXXRecordDecl>(RT->getDecl());
345
346 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD))
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000347 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000348
349 return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
Fariborz Jahanian47609b02011-01-20 17:19:02 +0000350}
351
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000352/// BuildVirtualCall - This routine makes indirect vtable call for
353/// call to virtual destructors. It returns 0 if it could not do it.
354llvm::Value *
355CodeGenFunction::BuildAppleKextVirtualDestructorCall(
356 const CXXDestructorDecl *DD,
357 CXXDtorType Type,
358 const CXXRecordDecl *RD) {
Rafael Espindola2ae250c2014-05-09 00:08:36 +0000359 const auto *MD = cast<CXXMethodDecl>(DD);
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000360 // FIXME. Dtor_Base dtor is always direct!!
361 // It need be somehow inline expanded into the caller.
362 // -O does that. But need to support -O0 as well.
363 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000364 // Compute the function type we're calling.
Rafael Espindola8d2a19b2014-09-08 16:01:27 +0000365 const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
366 DD, StructorType::Complete);
John McCalla729c622012-02-17 03:33:10 +0000367 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
Timur Iskhodzhanov03e87462013-07-19 08:14:45 +0000368 return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000369 }
Craig Topper8a13c412014-05-21 05:09:00 +0000370 return nullptr;
Fariborz Jahanian265c3252011-02-01 23:22:34 +0000371}