blob: d181f8d37b249d5d28cde84b5d6c7c2e0a7cb1e0 [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
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CodeGenModule.h"
Charles Davis3a811f12010-05-25 19:52:27 +000017#include "CGCXXABI.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000018#include "CodeGenFunction.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000021#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000022#include "clang/AST/DeclObjC.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000023#include "clang/AST/Mangle.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/AST/RecordLayout.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)
David Blaikie262bc182012-04-30 02:36:29 +000056 if (I->getType().isDestructedType())
John McCall0d70d712011-02-13 00:46:43 +000057 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);
Benjamin Kramerd4f51982012-07-04 18:45:14 +000094 if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
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.
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000109 llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
John McCalld46f9852010-02-19 01:32:20 +0000110
Rafael Espindola57208582013-10-09 16:13:15 +0000111 // We can't use an alias if the linkage is not valid for one.
112 if (!llvm::GlobalAlias::isValidLinkage(Linkage))
John McCalld46f9852010-02-19 01:32:20 +0000113 return true;
114
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000115 llvm::GlobalValue::LinkageTypes TargetLinkage
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000116 = getFunctionLinkage(TargetDecl);
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000117
Rafael Espindola57208582013-10-09 16:13:15 +0000118 // Don't create an strong alias to a linker weak symbol. If the linker
119 // decides to drop the symbol, the alias would become undefined.
120 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage) &&
121 !llvm::GlobalValue::isWeakForLinker(Linkage))
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000122 return true;
123
John McCallc0bf4622010-02-23 00:48:20 +0000124 // Derive the type for the alias.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000125 llvm::PointerType *AliasType
John McCallc0bf4622010-02-23 00:48:20 +0000126 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
127
John McCallc0bf4622010-02-23 00:48:20 +0000128 // Find the referrent. Some aliases might require a bitcast, in
129 // which case the caller is responsible for ensuring the soundness
130 // of these semantics.
131 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
132 llvm::Constant *Aliasee = Ref;
133 if (Ref->getType() != AliasType)
134 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
135
John McCalld46f9852010-02-19 01:32:20 +0000136 // Create the alias with no name.
137 llvm::GlobalAlias *Alias =
John McCallc0bf4622010-02-23 00:48:20 +0000138 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld46f9852010-02-19 01:32:20 +0000139
John McCall1962bee2010-02-24 20:32:01 +0000140 // Switch any previous uses to the alias.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef MangledName = getMangledName(AliasDecl);
John McCallf746aa62010-03-19 23:29:14 +0000142 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000143 if (Entry) {
John McCall1962bee2010-02-24 20:32:01 +0000144 assert(Entry->isDeclaration() && "definition already exists for alias");
145 assert(Entry->getType() == AliasType &&
146 "declaration exists with different type");
John McCallf746aa62010-03-19 23:29:14 +0000147 Alias->takeName(Entry);
John McCalld46f9852010-02-19 01:32:20 +0000148 Entry->replaceAllUsesWith(Alias);
149 Entry->eraseFromParent();
John McCallf746aa62010-03-19 23:29:14 +0000150 } else {
Anders Carlsson9a20d552010-06-22 16:16:50 +0000151 Alias->setName(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000152 }
John McCalld46f9852010-02-19 01:32:20 +0000153
154 // Finally, set up the alias with its proper name and attributes.
John McCall1fb0caa2010-10-22 21:05:15 +0000155 SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
John McCalld46f9852010-02-19 01:32:20 +0000156
157 return false;
158}
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000159
John McCall1f6f9612011-03-09 08:12:35 +0000160void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor,
161 CXXCtorType ctorType) {
John McCalld46f9852010-02-19 01:32:20 +0000162 // The complete constructor is equivalent to the base constructor
163 // for classes with no virtual bases. Try to emit it as an alias.
Timur Iskhodzhanov1d4fff52013-02-27 13:46:31 +0000164 if (getTarget().getCXXABI().hasConstructorVariants() &&
165 ctorType == Ctor_Complete &&
John McCall1f6f9612011-03-09 08:12:35 +0000166 !ctor->getParent()->getNumVBases() &&
167 !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete),
168 GlobalDecl(ctor, Ctor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000169 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
John McCallde5d3c72012-02-17 03:33:10 +0000171 const CGFunctionInfo &fnInfo =
172 getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
John McCalld26bc762011-03-09 04:27:21 +0000173
John McCall1f6f9612011-03-09 08:12:35 +0000174 llvm::Function *fn =
175 cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo));
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000176 setFunctionLinkage(GlobalDecl(ctor, ctorType), fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
John McCall1f6f9612011-03-09 08:12:35 +0000178 CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
John McCall1f6f9612011-03-09 08:12:35 +0000180 SetFunctionDefinitionAttributes(ctor, fn);
181 SetLLVMFunctionAttributesForDefinition(ctor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000182}
183
John McCalld46f9852010-02-19 01:32:20 +0000184llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000185CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
186 CXXCtorType ctorType,
187 const CGFunctionInfo *fnInfo) {
188 GlobalDecl GD(ctor, ctorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000189
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 StringRef name = getMangledName(GD);
John McCall1f6f9612011-03-09 08:12:35 +0000191 if (llvm::GlobalValue *existing = GetGlobalValue(name))
192 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000193
John McCallde5d3c72012-02-17 03:33:10 +0000194 if (!fnInfo)
195 fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType);
John McCall1f6f9612011-03-09 08:12:35 +0000196
John McCallde5d3c72012-02-17 03:33:10 +0000197 llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo);
John McCall1f6f9612011-03-09 08:12:35 +0000198 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000199 /*ForVTable=*/false));
Anders Carlsson363c1842009-04-16 23:57:24 +0000200}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000201
John McCall1f6f9612011-03-09 08:12:35 +0000202void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor,
203 CXXDtorType dtorType) {
John McCalld46f9852010-02-19 01:32:20 +0000204 // The complete destructor is equivalent to the base destructor for
205 // classes with no virtual bases, so try to emit it as an alias.
John McCall1f6f9612011-03-09 08:12:35 +0000206 if (dtorType == Dtor_Complete &&
207 !dtor->getParent()->getNumVBases() &&
208 !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete),
209 GlobalDecl(dtor, Dtor_Base)))
John McCalld46f9852010-02-19 01:32:20 +0000210 return;
211
John McCallc0bf4622010-02-23 00:48:20 +0000212 // The base destructor is equivalent to the base destructor of its
213 // base class if there is exactly one non-virtual base class with a
214 // non-trivial destructor, there are no fields with a non-trivial
215 // destructor, and the body of the destructor is trivial.
John McCall1f6f9612011-03-09 08:12:35 +0000216 if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor))
John McCallc0bf4622010-02-23 00:48:20 +0000217 return;
218
John McCallde5d3c72012-02-17 03:33:10 +0000219 const CGFunctionInfo &fnInfo =
220 getTypes().arrangeCXXDestructor(dtor, dtorType);
John McCalld26bc762011-03-09 04:27:21 +0000221
John McCall1f6f9612011-03-09 08:12:35 +0000222 llvm::Function *fn =
223 cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo));
Peter Collingbourne144a31f2013-06-05 17:49:37 +0000224 setFunctionLinkage(GlobalDecl(dtor, dtorType), fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
John McCall1f6f9612011-03-09 08:12:35 +0000226 CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
John McCall1f6f9612011-03-09 08:12:35 +0000228 SetFunctionDefinitionAttributes(dtor, fn);
229 SetLLVMFunctionAttributesForDefinition(dtor, fn);
Anders Carlsson27ae5362009-04-17 01:58:57 +0000230}
231
John McCalld46f9852010-02-19 01:32:20 +0000232llvm::GlobalValue *
John McCall1f6f9612011-03-09 08:12:35 +0000233CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
234 CXXDtorType dtorType,
Reid Klecknera4130ba2013-07-22 13:51:44 +0000235 const CGFunctionInfo *fnInfo,
236 llvm::FunctionType *fnType) {
237 // If the class has no virtual bases, then the complete and base destructors
238 // are equivalent, for all C++ ABIs supported by clang. We can save on code
239 // size by calling the base dtor directly, especially if we'd have to emit a
240 // thunk otherwise.
241 // FIXME: We should do this for Itanium, after verifying that nothing breaks.
242 if (dtorType == Dtor_Complete && dtor->getParent()->getNumVBases() == 0 &&
243 getCXXABI().useThunkForDtorVariant(dtor, Dtor_Complete))
244 dtorType = Dtor_Base;
245
John McCall1f6f9612011-03-09 08:12:35 +0000246 GlobalDecl GD(dtor, dtorType);
Anders Carlssondc709a82010-06-09 02:30:12 +0000247
Chris Lattner5f9e2722011-07-23 10:55:15 +0000248 StringRef name = getMangledName(GD);
John McCall1f6f9612011-03-09 08:12:35 +0000249 if (llvm::GlobalValue *existing = GetGlobalValue(name))
250 return existing;
John McCalld46f9852010-02-19 01:32:20 +0000251
Reid Klecknera4130ba2013-07-22 13:51:44 +0000252 if (!fnType) {
253 if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType);
254 fnType = getTypes().GetFunctionType(*fnInfo);
255 }
John McCall1f6f9612011-03-09 08:12:35 +0000256 return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD,
Anders Carlsson1faa89f2011-02-05 04:35:53 +0000257 /*ForVTable=*/false));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000258}
259
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000260static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
261 GlobalDecl GD,
262 llvm::Type *Ty,
263 const CXXRecordDecl *RD) {
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +0000264 assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
265 "No kext in Microsoft ABI");
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000266 GD = GD.getCanonicalDecl();
267 CodeGenModule &CGM = CGF.CGM;
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +0000268 llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000269 Ty = Ty->getPointerTo()->getPointerTo();
270 VTable = CGF.Builder.CreateBitCast(VTable, Ty);
271 assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
272 uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(GD);
273 uint64_t AddressPoint =
274 CGM.getVTableContext().getVTableLayout(RD)
275 .getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
276 VTableIndex += AddressPoint;
277 llvm::Value *VFuncPtr =
278 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
Anders Carlsson566abee2009-11-13 04:45:41 +0000279 return CGF.Builder.CreateLoad(VFuncPtr);
280}
281
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000282/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
Fariborz Jahanian27262672011-01-20 17:19:02 +0000283/// indirect call to virtual functions. It makes the call through indexing
284/// into the vtable.
285llvm::Value *
286CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
287 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000288 llvm::Type *Ty) {
Fariborz Jahanian27262672011-01-20 17:19:02 +0000289 assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
290 "BuildAppleKextVirtualCall - bad Qual kind");
291
292 const Type *QTy = Qual->getAsType();
293 QualType T = QualType(QTy, 0);
294 const RecordType *RT = T->getAs<RecordType>();
295 assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
296 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000297
298 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
299 return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000300
301 return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
Fariborz Jahanian27262672011-01-20 17:19:02 +0000302}
303
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000304/// BuildVirtualCall - This routine makes indirect vtable call for
305/// call to virtual destructors. It returns 0 if it could not do it.
306llvm::Value *
307CodeGenFunction::BuildAppleKextVirtualDestructorCall(
308 const CXXDestructorDecl *DD,
309 CXXDtorType Type,
310 const CXXRecordDecl *RD) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000311 const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD);
312 // FIXME. Dtor_Base dtor is always direct!!
313 // It need be somehow inline expanded into the caller.
314 // -O does that. But need to support -O0 as well.
315 if (MD->isVirtual() && Type != Dtor_Base) {
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000316 // Compute the function type we're calling.
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000317 const CGFunctionInfo &FInfo =
318 CGM.getTypes().arrangeCXXDestructor(DD, Dtor_Complete);
John McCallde5d3c72012-02-17 03:33:10 +0000319 llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000320 return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000321 }
Timur Iskhodzhanov6e007f92013-07-19 08:14:45 +0000322 return 0;
Fariborz Jahanianccd52592011-02-01 23:22:34 +0000323}