blob: 525877903e7a1229150fe22ec030a972ccc7cb4f [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
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"
Anders Carlsson283a0622009-04-13 18:03:33 +000019#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000020#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000021#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000022#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000023#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000024#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000025#include "clang/AST/StmtCXX.h"
John McCalld46f9852010-02-19 01:32:20 +000026#include "clang/CodeGen/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
63 // If any fields have a non-trivial destructor, we have to emit it
64 // separately.
65 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
66 E = Class->field_end(); I != E; ++I)
67 if (const RecordType *RT = (*I)->getType()->getAs<RecordType>())
68 if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor())
69 return true;
70
71 // Try to find a unique base class with a non-trivial destructor.
72 const CXXRecordDecl *UniqueBase = 0;
73 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
74 E = Class->bases_end(); I != E; ++I) {
75
76 // We're in the base destructor, so skip virtual bases.
77 if (I->isVirtual()) continue;
78
79 // Skip base classes with trivial destructors.
80 const CXXRecordDecl *Base
81 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
82 if (Base->hasTrivialDestructor()) continue;
83
84 // If we've already found a base class with a non-trivial
85 // destructor, give up.
86 if (UniqueBase) return true;
87 UniqueBase = Base;
88 }
89
90 // If we didn't find any bases with a non-trivial destructor, then
91 // the base destructor is actually effectively trivial, which can
92 // happen if it was needlessly user-defined or if there are virtual
93 // bases with non-trivial destructors.
94 if (!UniqueBase)
95 return true;
96
John McCall9a708462010-03-03 03:40:11 +000097 /// If we don't have a definition for the destructor yet, don't
98 /// emit. We can't emit aliases to declarations; that's just not
99 /// how aliases work.
100 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(getContext());
101 if (!BaseD->isImplicit() && !BaseD->getBody())
102 return true;
103
John McCallc0bf4622010-02-23 00:48:20 +0000104 // If the base is at a non-zero offset, give up.
105 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
106 if (ClassLayout.getBaseClassOffset(UniqueBase) != 0)
107 return true;
108
John McCallc0bf4622010-02-23 00:48:20 +0000109 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
110 GlobalDecl(BaseD, Dtor_Base));
111}
112
John McCalld46f9852010-02-19 01:32:20 +0000113/// Try to emit a definition as a global alias for another definition.
114bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
115 GlobalDecl TargetDecl) {
116 if (!getCodeGenOpts().CXXCtorDtorAliases)
117 return true;
118
John McCalld46f9852010-02-19 01:32:20 +0000119 // The alias will use the linkage of the referrent. If we can't
120 // support aliases with that linkage, fail.
121 llvm::GlobalValue::LinkageTypes Linkage
122 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl()));
123
124 switch (Linkage) {
125 // We can definitely emit aliases to definitions with external linkage.
126 case llvm::GlobalValue::ExternalLinkage:
127 case llvm::GlobalValue::ExternalWeakLinkage:
128 break;
129
130 // Same with local linkage.
131 case llvm::GlobalValue::InternalLinkage:
132 case llvm::GlobalValue::PrivateLinkage:
133 case llvm::GlobalValue::LinkerPrivateLinkage:
134 break;
135
136 // We should try to support linkonce linkages.
137 case llvm::GlobalValue::LinkOnceAnyLinkage:
138 case llvm::GlobalValue::LinkOnceODRLinkage:
139 return true;
140
141 // Other linkages will probably never be supported.
142 default:
143 return true;
144 }
145
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000146 llvm::GlobalValue::LinkageTypes TargetLinkage
147 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl()));
148
Rafael Espindola3c157452010-03-06 07:35:18 +0000149 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000150 return true;
151
John McCallc0bf4622010-02-23 00:48:20 +0000152 // Derive the type for the alias.
153 const llvm::PointerType *AliasType
154 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
155
John McCallc0bf4622010-02-23 00:48:20 +0000156 // Find the referrent. Some aliases might require a bitcast, in
157 // which case the caller is responsible for ensuring the soundness
158 // of these semantics.
159 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
160 llvm::Constant *Aliasee = Ref;
161 if (Ref->getType() != AliasType)
162 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
163
John McCalld46f9852010-02-19 01:32:20 +0000164 // Create the alias with no name.
165 llvm::GlobalAlias *Alias =
John McCallc0bf4622010-02-23 00:48:20 +0000166 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld46f9852010-02-19 01:32:20 +0000167
John McCall1962bee2010-02-24 20:32:01 +0000168 // Switch any previous uses to the alias.
John McCallf746aa62010-03-19 23:29:14 +0000169 MangleBuffer MangledName;
170 getMangledName(MangledName, AliasDecl);
171 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000172 if (Entry) {
John McCall1962bee2010-02-24 20:32:01 +0000173 assert(Entry->isDeclaration() && "definition already exists for alias");
174 assert(Entry->getType() == AliasType &&
175 "declaration exists with different type");
John McCallf746aa62010-03-19 23:29:14 +0000176 Alias->takeName(Entry);
John McCalld46f9852010-02-19 01:32:20 +0000177 Entry->replaceAllUsesWith(Alias);
178 Entry->eraseFromParent();
John McCallf746aa62010-03-19 23:29:14 +0000179 } else {
180 Alias->setName(MangledName.getString());
John McCalld46f9852010-02-19 01:32:20 +0000181 }
John McCalld46f9852010-02-19 01:32:20 +0000182
183 // Finally, set up the alias with its proper name and attributes.
John McCalld46f9852010-02-19 01:32:20 +0000184 SetCommonAttributes(AliasDecl.getDecl(), Alias);
185
186 return false;
187}
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000188
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000189void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000190 // The constructor used for constructing this as a complete class;
191 // constucts the virtual bases, then calls the base constructor.
John McCall92ac9ff2010-02-17 03:52:49 +0000192 EmitGlobal(GlobalDecl(D, Ctor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000193
194 // The constructor used for constructing this as a base class;
195 // ignores virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000196 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000197}
Anders Carlsson363c1842009-04-16 23:57:24 +0000198
Mike Stump1eb44332009-09-09 15:08:12 +0000199void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000200 CXXCtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000201 // The complete constructor is equivalent to the base constructor
202 // for classes with no virtual bases. Try to emit it as an alias.
203 if (Type == Ctor_Complete &&
204 !D->getParent()->getNumVBases() &&
205 !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete),
206 GlobalDecl(D, Ctor_Base)))
207 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
John McCalld46f9852010-02-19 01:32:20 +0000209 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000210 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000212 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anders Carlsson27ae5362009-04-17 01:58:57 +0000214 SetFunctionDefinitionAttributes(D, Fn);
215 SetLLVMFunctionAttributesForDefinition(D, Fn);
216}
217
John McCalld46f9852010-02-19 01:32:20 +0000218llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000219CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000220 CXXCtorType Type) {
John McCallf746aa62010-03-19 23:29:14 +0000221 MangleBuffer Name;
222 getMangledCXXCtorName(Name, D, Type);
223 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000224 return V;
225
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000226 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000227 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000228 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000229 FPT->isVariadic());
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000230 return cast<llvm::Function>(
231 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000232}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000233
Douglas Gregor35415f52010-05-25 17:04:15 +0000234void CodeGenModule::getMangledName(MangleBuffer &Buffer, const BlockDecl *BD) {
235 getMangleContext().mangleBlock(BD, Buffer.getBuffer());
236}
237
John McCallf746aa62010-03-19 23:29:14 +0000238void CodeGenModule::getMangledCXXCtorName(MangleBuffer &Name,
239 const CXXConstructorDecl *D,
240 CXXCtorType Type) {
241 getMangleContext().mangleCXXCtor(D, Type, Name.getBuffer());
Anders Carlsson27ae5362009-04-17 01:58:57 +0000242}
243
244void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000245 // The destructor in a virtual table is always a 'deleting'
246 // destructor, which calls the complete destructor and then uses the
247 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000248 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000249 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000250
251 // The destructor used for destructing this as a most-derived class;
252 // call the base destructor and then destructs any virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000253 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000254
255 // The destructor used for destructing this as a base class; ignores
256 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000257 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000261 CXXDtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000262 // The complete destructor is equivalent to the base destructor for
263 // classes with no virtual bases, so try to emit it as an alias.
264 if (Type == Dtor_Complete &&
265 !D->getParent()->getNumVBases() &&
266 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete),
267 GlobalDecl(D, Dtor_Base)))
268 return;
269
John McCallc0bf4622010-02-23 00:48:20 +0000270 // The base destructor is equivalent to the base destructor of its
271 // base class if there is exactly one non-virtual base class with a
272 // non-trivial destructor, there are no fields with a non-trivial
273 // destructor, and the body of the destructor is trivial.
274 if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D))
275 return;
276
John McCalld46f9852010-02-19 01:32:20 +0000277 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000278 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000280 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Anders Carlsson27ae5362009-04-17 01:58:57 +0000282 SetFunctionDefinitionAttributes(D, Fn);
283 SetLLVMFunctionAttributesForDefinition(D, Fn);
284}
285
John McCalld46f9852010-02-19 01:32:20 +0000286llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000287CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000288 CXXDtorType Type) {
John McCallf746aa62010-03-19 23:29:14 +0000289 MangleBuffer Name;
290 getMangledCXXDtorName(Name, D, Type);
291 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000292 return V;
293
Anders Carlsson27ae5362009-04-17 01:58:57 +0000294 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000295 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000297 return cast<llvm::Function>(
298 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000299}
300
John McCallf746aa62010-03-19 23:29:14 +0000301void CodeGenModule::getMangledCXXDtorName(MangleBuffer &Name,
302 const CXXDestructorDecl *D,
303 CXXDtorType Type) {
304 getMangleContext().mangleCXXDtor(D, Type, Name.getBuffer());
Anders Carlsson27ae5362009-04-17 01:58:57 +0000305}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000306
Anders Carlsson046c2942010-04-17 20:15:18 +0000307static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000308 llvm::Value *This, const llvm::Type *Ty) {
309 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000310
Anders Carlsson046c2942010-04-17 20:15:18 +0000311 llvm::Value *VTable = CGF.Builder.CreateBitCast(This, Ty);
312 VTable = CGF.Builder.CreateLoad(VTable);
Anders Carlsson566abee2009-11-13 04:45:41 +0000313
314 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000315 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000316 return CGF.Builder.CreateLoad(VFuncPtr);
317}
318
319llvm::Value *
320CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
321 const llvm::Type *Ty) {
322 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000323 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000324
Anders Carlssonaf440352010-03-23 04:11:45 +0000325 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000326}
327
328llvm::Value *
329CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
330 llvm::Value *&This, const llvm::Type *Ty) {
331 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000332 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000333 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000334
Anders Carlssonaf440352010-03-23 04:11:45 +0000335 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000336}
Charles Davis3a811f12010-05-25 19:52:27 +0000337
338CXXABI::~CXXABI() {}