blob: 85222fe9dbace7fe42420004fa0675ae3f270546 [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
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#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"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
John McCalld46f9852010-02-19 01:32:20 +000025#include "clang/CodeGen/CodeGenOptions.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000027using namespace clang;
28using namespace CodeGen;
29
John McCallc0bf4622010-02-23 00:48:20 +000030/// Determines whether the given function has a trivial body that does
31/// not require any specific codegen.
32static bool HasTrivialBody(const FunctionDecl *FD) {
33 Stmt *S = FD->getBody();
34 if (!S)
35 return true;
36 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
37 return true;
38 return false;
39}
40
41/// Try to emit a base destructor as an alias to its primary
42/// base-class destructor.
43bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
44 if (!getCodeGenOpts().CXXCtorDtorAliases)
45 return true;
46
47 // If the destructor doesn't have a trivial body, we have to emit it
48 // separately.
49 if (!HasTrivialBody(D))
50 return true;
51
52 const CXXRecordDecl *Class = D->getParent();
53
54 // If we need to manipulate a VTT parameter, give up.
55 if (Class->getNumVBases()) {
56 // Extra Credit: passing extra parameters is perfectly safe
57 // in many calling conventions, so only bail out if the ctor's
58 // calling convention is nonstandard.
59 return true;
60 }
61
62 // If any fields have a non-trivial destructor, we have to emit it
63 // separately.
64 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
65 E = Class->field_end(); I != E; ++I)
66 if (const RecordType *RT = (*I)->getType()->getAs<RecordType>())
67 if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor())
68 return true;
69
70 // Try to find a unique base class with a non-trivial destructor.
71 const CXXRecordDecl *UniqueBase = 0;
72 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
73 E = Class->bases_end(); I != E; ++I) {
74
75 // We're in the base destructor, so skip virtual bases.
76 if (I->isVirtual()) continue;
77
78 // Skip base classes with trivial destructors.
79 const CXXRecordDecl *Base
80 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
81 if (Base->hasTrivialDestructor()) continue;
82
83 // If we've already found a base class with a non-trivial
84 // destructor, give up.
85 if (UniqueBase) return true;
86 UniqueBase = Base;
87 }
88
89 // If we didn't find any bases with a non-trivial destructor, then
90 // the base destructor is actually effectively trivial, which can
91 // happen if it was needlessly user-defined or if there are virtual
92 // bases with non-trivial destructors.
93 if (!UniqueBase)
94 return true;
95
John McCall9a708462010-03-03 03:40:11 +000096 /// If we don't have a definition for the destructor yet, don't
97 /// emit. We can't emit aliases to declarations; that's just not
98 /// how aliases work.
99 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(getContext());
100 if (!BaseD->isImplicit() && !BaseD->getBody())
101 return true;
102
John McCallc0bf4622010-02-23 00:48:20 +0000103 // If the base is at a non-zero offset, give up.
104 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
105 if (ClassLayout.getBaseClassOffset(UniqueBase) != 0)
106 return true;
107
John McCallc0bf4622010-02-23 00:48:20 +0000108 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
109 GlobalDecl(BaseD, Dtor_Base));
110}
111
John McCalld46f9852010-02-19 01:32:20 +0000112/// Try to emit a definition as a global alias for another definition.
113bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
114 GlobalDecl TargetDecl) {
115 if (!getCodeGenOpts().CXXCtorDtorAliases)
116 return true;
117
John McCalld46f9852010-02-19 01:32:20 +0000118 // The alias will use the linkage of the referrent. If we can't
119 // support aliases with that linkage, fail.
120 llvm::GlobalValue::LinkageTypes Linkage
121 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl()));
122
123 switch (Linkage) {
124 // We can definitely emit aliases to definitions with external linkage.
125 case llvm::GlobalValue::ExternalLinkage:
126 case llvm::GlobalValue::ExternalWeakLinkage:
127 break;
128
129 // Same with local linkage.
130 case llvm::GlobalValue::InternalLinkage:
131 case llvm::GlobalValue::PrivateLinkage:
132 case llvm::GlobalValue::LinkerPrivateLinkage:
133 break;
134
135 // We should try to support linkonce linkages.
136 case llvm::GlobalValue::LinkOnceAnyLinkage:
137 case llvm::GlobalValue::LinkOnceODRLinkage:
138 return true;
139
140 // Other linkages will probably never be supported.
141 default:
142 return true;
143 }
144
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000145 llvm::GlobalValue::LinkageTypes TargetLinkage
146 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl()));
147
Rafael Espindola3c157452010-03-06 07:35:18 +0000148 if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
Rafael Espindolabc6afd12010-03-05 01:21:10 +0000149 return true;
150
John McCallc0bf4622010-02-23 00:48:20 +0000151 // Derive the type for the alias.
152 const llvm::PointerType *AliasType
153 = getTypes().GetFunctionType(AliasDecl)->getPointerTo();
154
John McCallc0bf4622010-02-23 00:48:20 +0000155 // Find the referrent. Some aliases might require a bitcast, in
156 // which case the caller is responsible for ensuring the soundness
157 // of these semantics.
158 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
159 llvm::Constant *Aliasee = Ref;
160 if (Ref->getType() != AliasType)
161 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
162
John McCalld46f9852010-02-19 01:32:20 +0000163 // Create the alias with no name.
164 llvm::GlobalAlias *Alias =
John McCallc0bf4622010-02-23 00:48:20 +0000165 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule());
John McCalld46f9852010-02-19 01:32:20 +0000166
John McCall1962bee2010-02-24 20:32:01 +0000167 // Switch any previous uses to the alias.
John McCallf746aa62010-03-19 23:29:14 +0000168 MangleBuffer MangledName;
169 getMangledName(MangledName, AliasDecl);
170 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
John McCalld46f9852010-02-19 01:32:20 +0000171 if (Entry) {
John McCall1962bee2010-02-24 20:32:01 +0000172 assert(Entry->isDeclaration() && "definition already exists for alias");
173 assert(Entry->getType() == AliasType &&
174 "declaration exists with different type");
John McCallf746aa62010-03-19 23:29:14 +0000175 Alias->takeName(Entry);
John McCalld46f9852010-02-19 01:32:20 +0000176 Entry->replaceAllUsesWith(Alias);
177 Entry->eraseFromParent();
John McCallf746aa62010-03-19 23:29:14 +0000178 } else {
179 Alias->setName(MangledName.getString());
John McCalld46f9852010-02-19 01:32:20 +0000180 }
John McCalld46f9852010-02-19 01:32:20 +0000181
182 // Finally, set up the alias with its proper name and attributes.
John McCalld46f9852010-02-19 01:32:20 +0000183 SetCommonAttributes(AliasDecl.getDecl(), Alias);
184
185 return false;
186}
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000187
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000188void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000189 // The constructor used for constructing this as a complete class;
190 // constucts the virtual bases, then calls the base constructor.
John McCall92ac9ff2010-02-17 03:52:49 +0000191 EmitGlobal(GlobalDecl(D, Ctor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000192
193 // The constructor used for constructing this as a base class;
194 // ignores virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000195 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000196}
Anders Carlsson363c1842009-04-16 23:57:24 +0000197
Mike Stump1eb44332009-09-09 15:08:12 +0000198void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000199 CXXCtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000200 // The complete constructor is equivalent to the base constructor
201 // for classes with no virtual bases. Try to emit it as an alias.
202 if (Type == Ctor_Complete &&
203 !D->getParent()->getNumVBases() &&
204 !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete),
205 GlobalDecl(D, Ctor_Base)))
206 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
John McCalld46f9852010-02-19 01:32:20 +0000208 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000209 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000211 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlsson27ae5362009-04-17 01:58:57 +0000213 SetFunctionDefinitionAttributes(D, Fn);
214 SetLLVMFunctionAttributesForDefinition(D, Fn);
215}
216
John McCalld46f9852010-02-19 01:32:20 +0000217llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000218CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000219 CXXCtorType Type) {
John McCallf746aa62010-03-19 23:29:14 +0000220 MangleBuffer Name;
221 getMangledCXXCtorName(Name, D, Type);
222 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000223 return V;
224
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000225 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000226 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000227 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000228 FPT->isVariadic());
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000229 return cast<llvm::Function>(
230 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000231}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000232
John McCallf746aa62010-03-19 23:29:14 +0000233void CodeGenModule::getMangledCXXCtorName(MangleBuffer &Name,
234 const CXXConstructorDecl *D,
235 CXXCtorType Type) {
236 getMangleContext().mangleCXXCtor(D, Type, Name.getBuffer());
Anders Carlsson27ae5362009-04-17 01:58:57 +0000237}
238
239void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000240 // The destructor in a virtual table is always a 'deleting'
241 // destructor, which calls the complete destructor and then uses the
242 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000243 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000244 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000245
246 // The destructor used for destructing this as a most-derived class;
247 // call the base destructor and then destructs any virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000248 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000249
250 // The destructor used for destructing this as a base class; ignores
251 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000252 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000253}
254
Mike Stump1eb44332009-09-09 15:08:12 +0000255void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000256 CXXDtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000257 // The complete destructor is equivalent to the base destructor for
258 // classes with no virtual bases, so try to emit it as an alias.
259 if (Type == Dtor_Complete &&
260 !D->getParent()->getNumVBases() &&
261 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete),
262 GlobalDecl(D, Dtor_Base)))
263 return;
264
John McCallc0bf4622010-02-23 00:48:20 +0000265 // The base destructor is equivalent to the base destructor of its
266 // base class if there is exactly one non-virtual base class with a
267 // non-trivial destructor, there are no fields with a non-trivial
268 // destructor, and the body of the destructor is trivial.
269 if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D))
270 return;
271
John McCalld46f9852010-02-19 01:32:20 +0000272 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000273 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000275 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Anders Carlsson27ae5362009-04-17 01:58:57 +0000277 SetFunctionDefinitionAttributes(D, Fn);
278 SetLLVMFunctionAttributesForDefinition(D, Fn);
279}
280
John McCalld46f9852010-02-19 01:32:20 +0000281llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000282CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000283 CXXDtorType Type) {
John McCallf746aa62010-03-19 23:29:14 +0000284 MangleBuffer Name;
285 getMangledCXXDtorName(Name, D, Type);
286 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000287 return V;
288
Anders Carlsson27ae5362009-04-17 01:58:57 +0000289 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000290 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000292 return cast<llvm::Function>(
293 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000294}
295
John McCallf746aa62010-03-19 23:29:14 +0000296void CodeGenModule::getMangledCXXDtorName(MangleBuffer &Name,
297 const CXXDestructorDecl *D,
298 CXXDtorType Type) {
299 getMangleContext().mangleCXXDtor(D, Type, Name.getBuffer());
Anders Carlsson27ae5362009-04-17 01:58:57 +0000300}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000301
Anders Carlsson046c2942010-04-17 20:15:18 +0000302static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000303 llvm::Value *This, const llvm::Type *Ty) {
304 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000305
Anders Carlsson046c2942010-04-17 20:15:18 +0000306 llvm::Value *VTable = CGF.Builder.CreateBitCast(This, Ty);
307 VTable = CGF.Builder.CreateLoad(VTable);
Anders Carlsson566abee2009-11-13 04:45:41 +0000308
309 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000310 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000311 return CGF.Builder.CreateLoad(VFuncPtr);
312}
313
314llvm::Value *
315CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
316 const llvm::Type *Ty) {
317 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000318 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000319
Anders Carlssonaf440352010-03-23 04:11:45 +0000320 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000321}
322
323llvm::Value *
324CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
325 llvm::Value *&This, const llvm::Type *Ty) {
326 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000327 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000328 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000329
Anders Carlssonaf440352010-03-23 04:11:45 +0000330 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000331}