blob: ea5017d7d08418468e51f5e4c7b6552375571658 [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) {
Anders Carlssondc709a82010-06-09 02:30:12 +0000221 GlobalDecl GD(D, Type);
222
John McCallf746aa62010-03-19 23:29:14 +0000223 MangleBuffer Name;
Anders Carlssondc709a82010-06-09 02:30:12 +0000224 getMangledName(Name, GD);
John McCallf746aa62010-03-19 23:29:14 +0000225 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000226 return V;
227
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000228 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000229 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000230 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000231 FPT->isVariadic());
Anders Carlssondc709a82010-06-09 02:30:12 +0000232 return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD));
Anders Carlsson363c1842009-04-16 23:57:24 +0000233}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000234
Anders Carlsson27ae5362009-04-17 01:58:57 +0000235void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
John McCalld46f9852010-02-19 01:32:20 +0000236 // The destructor in a virtual table is always a 'deleting'
237 // destructor, which calls the complete destructor and then uses the
238 // appropriate operator delete.
Eli Friedmanea9a2082009-11-14 04:19:37 +0000239 if (D->isVirtual())
Eli Friedman624c7d72009-12-15 02:06:15 +0000240 EmitGlobal(GlobalDecl(D, Dtor_Deleting));
John McCalld46f9852010-02-19 01:32:20 +0000241
242 // The destructor used for destructing this as a most-derived class;
243 // call the base destructor and then destructs any virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000244 EmitGlobal(GlobalDecl(D, Dtor_Complete));
John McCalld46f9852010-02-19 01:32:20 +0000245
246 // The destructor used for destructing this as a base class; ignores
247 // virtual bases.
John McCall8e51a1f2010-02-18 21:31:48 +0000248 EmitGlobal(GlobalDecl(D, Dtor_Base));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000249}
250
Mike Stump1eb44332009-09-09 15:08:12 +0000251void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000252 CXXDtorType Type) {
John McCalld46f9852010-02-19 01:32:20 +0000253 // The complete destructor is equivalent to the base destructor for
254 // classes with no virtual bases, so try to emit it as an alias.
255 if (Type == Dtor_Complete &&
256 !D->getParent()->getNumVBases() &&
257 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete),
258 GlobalDecl(D, Dtor_Base)))
259 return;
260
John McCallc0bf4622010-02-23 00:48:20 +0000261 // The base destructor is equivalent to the base destructor of its
262 // base class if there is exactly one non-virtual base class with a
263 // non-trivial destructor, there are no fields with a non-trivial
264 // destructor, and the body of the destructor is trivial.
265 if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D))
266 return;
267
John McCalld46f9852010-02-19 01:32:20 +0000268 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type));
John McCall8b242332010-05-25 04:30:21 +0000269 setFunctionLinkage(D, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000271 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson27ae5362009-04-17 01:58:57 +0000273 SetFunctionDefinitionAttributes(D, Fn);
274 SetLLVMFunctionAttributesForDefinition(D, Fn);
275}
276
John McCalld46f9852010-02-19 01:32:20 +0000277llvm::GlobalValue *
Mike Stump1eb44332009-09-09 15:08:12 +0000278CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000279 CXXDtorType Type) {
Anders Carlssondc709a82010-06-09 02:30:12 +0000280 GlobalDecl GD(D, Type);
281
John McCallf746aa62010-03-19 23:29:14 +0000282 MangleBuffer Name;
Anders Carlssondc709a82010-06-09 02:30:12 +0000283 getMangledName(Name, GD);
John McCallf746aa62010-03-19 23:29:14 +0000284 if (llvm::GlobalValue *V = GetGlobalValue(Name))
John McCalld46f9852010-02-19 01:32:20 +0000285 return V;
286
Anders Carlsson27ae5362009-04-17 01:58:57 +0000287 const llvm::FunctionType *FTy =
Anders Carlssonf6c56e22009-11-25 03:15:49 +0000288 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Anders Carlssondc709a82010-06-09 02:30:12 +0000290 return cast<llvm::Function>(GetOrCreateLLVMFunction(Name, FTy, GD));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000291}
292
Eli Friedmanc00129a2010-05-30 06:03:20 +0000293llvm::Constant *
294CodeGenModule::GetCXXMemberFunctionPointerValue(const CXXMethodDecl *MD) {
295 assert(MD->isInstance() && "Member function must not be static!");
296
297 MD = MD->getCanonicalDecl();
298
299 const llvm::Type *PtrDiffTy = Types.ConvertType(Context.getPointerDiffType());
300
301 // Get the function pointer (or index if this is a virtual function).
302 if (MD->isVirtual()) {
303 uint64_t Index = VTables.getMethodVTableIndex(MD);
304
305 // FIXME: We shouldn't use / 8 here.
306 uint64_t PointerWidthInBytes = Context.Target.getPointerWidth(0) / 8;
307
308 // Itanium C++ ABI 2.3:
309 // For a non-virtual function, this field is a simple function pointer.
310 // For a virtual function, it is 1 plus the virtual table offset
311 // (in bytes) of the function, represented as a ptrdiff_t.
312 return llvm::ConstantInt::get(PtrDiffTy, (Index * PointerWidthInBytes) + 1);
313 }
314
315 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
316 const llvm::Type *Ty;
317 // Check whether the function has a computable LLVM signature.
318 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
319 // The function has a computable LLVM signature; use the correct type.
320 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
321 } else {
322 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
323 // function type is incomplete.
324 Ty = PtrDiffTy;
325 }
326
327 llvm::Constant *FuncPtr = GetAddrOfFunction(MD, Ty);
328 return llvm::ConstantExpr::getPtrToInt(FuncPtr, PtrDiffTy);
329}
330
Anders Carlsson046c2942010-04-17 20:15:18 +0000331static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex,
Anders Carlsson566abee2009-11-13 04:45:41 +0000332 llvm::Value *This, const llvm::Type *Ty) {
333 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +0000334
Anders Carlsson046c2942010-04-17 20:15:18 +0000335 llvm::Value *VTable = CGF.Builder.CreateBitCast(This, Ty);
336 VTable = CGF.Builder.CreateLoad(VTable);
Anders Carlsson566abee2009-11-13 04:45:41 +0000337
338 llvm::Value *VFuncPtr =
Anders Carlsson046c2942010-04-17 20:15:18 +0000339 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn");
Anders Carlsson566abee2009-11-13 04:45:41 +0000340 return CGF.Builder.CreateLoad(VFuncPtr);
341}
342
343llvm::Value *
344CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
345 const llvm::Type *Ty) {
346 MD = MD->getCanonicalDecl();
Anders Carlsson046c2942010-04-17 20:15:18 +0000347 uint64_t VTableIndex = CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlsson566abee2009-11-13 04:45:41 +0000348
Anders Carlssonaf440352010-03-23 04:11:45 +0000349 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Anders Carlsson566abee2009-11-13 04:45:41 +0000350}
351
352llvm::Value *
353CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
354 llvm::Value *&This, const llvm::Type *Ty) {
355 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
Anders Carlssonaf440352010-03-23 04:11:45 +0000356 uint64_t VTableIndex =
Anders Carlsson046c2942010-04-17 20:15:18 +0000357 CGM.getVTables().getMethodVTableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +0000358
Anders Carlssonaf440352010-03-23 04:11:45 +0000359 return ::BuildVirtualCall(*this, VTableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +0000360}
Charles Davis3a811f12010-05-25 19:52:27 +0000361
362CXXABI::~CXXABI() {}