blob: 77d91a3a97df5045e10a58ac12c47a0bf64acbc5 [file] [log] [blame]
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +00001//===--- CGDeclCXX.cpp - Emit LLVM Code for C++ 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 code generation of C++ declarations
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Fariborz Jahanianec805122011-01-13 20:00:54 +000015#include "CGObjCRuntime.h"
John McCall4c40d982010-08-31 07:33:07 +000016#include "CGCXXABI.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000017#include "clang/Frontend/CodeGenOptions.h"
Douglas Gregor86a3a032010-05-16 01:24:12 +000018#include "llvm/Intrinsics.h"
19
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000020using namespace clang;
21using namespace CodeGen;
22
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000023static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
24 llvm::Constant *DeclPtr) {
25 assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
26 assert(!D.getType()->isReferenceType() &&
27 "Should not call EmitDeclInit on a reference!");
28
Anders Carlssonfcbfdc12009-12-10 00:57:45 +000029 ASTContext &Context = CGF.getContext();
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000030
Eli Friedman6da2c712011-12-03 04:14:32 +000031 CharUnits alignment = Context.getDeclAlign(&D);
John McCalla07398e2011-06-16 04:16:24 +000032 QualType type = D.getType();
33 LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
34
35 const Expr *Init = D.getInit();
36 if (!CGF.hasAggregateLLVMType(type)) {
Fariborz Jahanianec805122011-01-13 20:00:54 +000037 CodeGenModule &CGM = CGF.CGM;
John McCalla07398e2011-06-16 04:16:24 +000038 if (lv.isObjCStrong())
John McCallf85e1932011-06-15 23:02:42 +000039 CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
40 DeclPtr, D.isThreadSpecified());
John McCalla07398e2011-06-16 04:16:24 +000041 else if (lv.isObjCWeak())
42 CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
43 DeclPtr);
Fariborz Jahanianec805122011-01-13 20:00:54 +000044 else
John McCalla07398e2011-06-16 04:16:24 +000045 CGF.EmitScalarInit(Init, &D, lv, false);
46 } else if (type->isAnyComplexType()) {
47 CGF.EmitComplexExprIntoAddr(Init, DeclPtr, lv.isVolatile());
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000048 } else {
Chad Rosier649b4a12012-03-29 17:37:10 +000049 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
50 AggValueSlot::DoesNotNeedGCBarriers,
51 AggValueSlot::IsNotAliased));
Anders Carlsson5ec2e7c2009-12-10 00:16:00 +000052 }
53}
54
John McCall5cd91b52010-09-08 01:44:27 +000055/// Emit code to cause the destruction of the given variable with
56/// static storage duration.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000057static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
John McCalla91f6662011-07-13 03:01:35 +000058 llvm::Constant *addr) {
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000059 CodeGenModule &CGM = CGF.CGM;
John McCalla91f6662011-07-13 03:01:35 +000060
61 // FIXME: __attribute__((cleanup)) ?
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000062
John McCalla91f6662011-07-13 03:01:35 +000063 QualType type = D.getType();
64 QualType::DestructionKind dtorKind = type.isDestructedType();
65
66 switch (dtorKind) {
67 case QualType::DK_none:
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000068 return;
John McCalla91f6662011-07-13 03:01:35 +000069
70 case QualType::DK_cxx_destructor:
71 break;
72
73 case QualType::DK_objc_strong_lifetime:
74 case QualType::DK_objc_weak_lifetime:
75 // We don't care about releasing objects during process teardown.
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000076 return;
John McCalla91f6662011-07-13 03:01:35 +000077 }
78
79 llvm::Constant *function;
80 llvm::Constant *argument;
81
82 // Special-case non-array C++ destructors, where there's a function
83 // with the right signature that we can just call.
84 const CXXRecordDecl *record = 0;
85 if (dtorKind == QualType::DK_cxx_destructor &&
86 (record = type->getAsCXXRecordDecl())) {
87 assert(!record->hasTrivialDestructor());
88 CXXDestructorDecl *dtor = record->getDestructor();
89
90 function = CGM.GetAddrOfCXXDestructor(dtor, Dtor_Complete);
91 argument = addr;
92
93 // Otherwise, the standard logic requires a helper function.
94 } else {
95 function = CodeGenFunction(CGM).generateDestroyHelper(addr, type,
96 CGF.getDestroyer(dtorKind),
97 CGF.needsEHCleanup(dtorKind));
98 argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
99 }
100
John McCall20bb1752012-05-01 06:13:13 +0000101 CGM.getCXXABI().registerGlobalDtor(CGF, function, argument);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000102}
103
Richard Smithabb94322012-02-17 07:31:37 +0000104/// Emit code to cause the variable at the given address to be considered as
105/// constant from this point onwards.
Nick Lewyckyef784462012-02-21 00:26:58 +0000106static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
107 llvm::Constant *Addr) {
Richard Smith00a8c3f2012-02-17 20:12:52 +0000108 // Don't emit the intrinsic if we're not optimizing.
109 if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
110 return;
111
Richard Smithabb94322012-02-17 07:31:37 +0000112 // Grab the llvm.invariant.start intrinsic.
113 llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
114 llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
115
Nick Lewyckyef784462012-02-21 00:26:58 +0000116 // Emit a call with the size in bytes of the object.
117 CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
118 uint64_t Width = WidthChars.getQuantity();
119 llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
Richard Smithabb94322012-02-17 07:31:37 +0000120 llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
121 CGF.Builder.CreateCall(InvariantStart, Args);
122}
123
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000124void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Richard Smith7ca48502012-02-13 22:16:19 +0000125 llvm::Constant *DeclPtr,
126 bool PerformInit) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000127
128 const Expr *Init = D.getInit();
129 QualType T = D.getType();
130
131 if (!T->isReferenceType()) {
Richard Smith7ca48502012-02-13 22:16:19 +0000132 if (PerformInit)
133 EmitDeclInit(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000134 if (CGM.isTypeConstant(D.getType(), true))
Nick Lewyckyef784462012-02-21 00:26:58 +0000135 EmitDeclInvariant(*this, D, DeclPtr);
Richard Smithabb94322012-02-17 07:31:37 +0000136 else
137 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000138 return;
139 }
Anders Carlsson045a6d82010-06-27 17:52:15 +0000140
Richard Smith7ca48502012-02-13 22:16:19 +0000141 assert(PerformInit && "cannot have constant initializer which needs "
142 "destruction for reference");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000143 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
Anders Carlsson045a6d82010-06-27 17:52:15 +0000144 RValue RV = EmitReferenceBindingToExpr(Init, &D);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000145 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000146}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000147
John McCall30fa3702012-04-06 18:21:06 +0000148static llvm::Function *
149CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
150 llvm::FunctionType *ty,
151 const Twine &name);
152
153/// Create a stub function, suitable for being passed to atexit,
154/// which passes the given address to the given destructor function.
155static llvm::Constant *createAtExitStub(CodeGenModule &CGM,
156 llvm::Constant *dtor,
157 llvm::Constant *addr) {
158 // Get the destructor function type, void(*)(void).
159 llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
160 llvm::Function *fn =
161 CreateGlobalInitOrDestructFunction(CGM, ty,
162 Twine("__dtor_", addr->getName()));
163
164 CodeGenFunction CGF(CGM);
165
166 CGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, fn,
167 CGM.getTypes().arrangeNullaryFunction(),
168 FunctionArgList(), SourceLocation());
169
170 llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
171
172 // Make sure the call and the callee agree on calling convention.
173 if (llvm::Function *dtorFn =
174 dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
175 call->setCallingConv(dtorFn->getCallingConv());
176
177 CGF.FinishFunction();
178
179 return fn;
180}
181
John McCall20bb1752012-05-01 06:13:13 +0000182/// Register a global destructor using the C atexit runtime function.
183void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtor,
184 llvm::Constant *addr) {
John McCall30fa3702012-04-06 18:21:06 +0000185 // Create a function which calls the destructor.
John McCall20bb1752012-05-01 06:13:13 +0000186 llvm::Constant *dtorStub = createAtExitStub(CGM, dtor, addr);
John McCall30fa3702012-04-06 18:21:06 +0000187
188 // extern "C" int atexit(void (*f)(void));
189 llvm::FunctionType *atexitTy =
John McCall20bb1752012-05-01 06:13:13 +0000190 llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
John McCall30fa3702012-04-06 18:21:06 +0000191
192 llvm::Constant *atexit =
John McCall20bb1752012-05-01 06:13:13 +0000193 CGM.CreateRuntimeFunction(atexitTy, "atexit");
John McCall30fa3702012-04-06 18:21:06 +0000194 if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
195 atexitFn->setDoesNotThrow();
196
John McCall20bb1752012-05-01 06:13:13 +0000197 Builder.CreateCall(atexit, dtorStub)->setDoesNotThrow();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000198}
199
John McCall3030eb82010-11-06 09:44:32 +0000200void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
Chandler Carruth0f30a122012-03-30 19:44:53 +0000201 llvm::GlobalVariable *DeclPtr,
Richard Smith7ca48502012-02-13 22:16:19 +0000202 bool PerformInit) {
John McCall32096692011-03-18 02:56:14 +0000203 // If we've been asked to forbid guard variables, emit an error now.
204 // This diagnostic is hard-coded for Darwin's use case; we can find
205 // better phrasing if someone else needs it.
206 if (CGM.getCodeGenOpts().ForbidGuardVariables)
207 CGM.Error(D.getLocation(),
208 "this initialization requires a guard variable, which "
209 "the kernel does not support");
210
Chandler Carruth0f30a122012-03-30 19:44:53 +0000211 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000212}
213
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000214static llvm::Function *
215CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000216 llvm::FunctionType *FTy,
NAKAMURA Takumia0786c92012-03-28 16:24:29 +0000217 const Twine &Name) {
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000218 llvm::Function *Fn =
219 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
220 Name, &CGM.getModule());
David Blaikie4e4d0842012-03-11 07:00:24 +0000221 if (!CGM.getContext().getLangOpts().AppleKext) {
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000222 // Set the section if needed.
223 if (const char *Section =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000224 CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier())
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000225 Fn->setSection(Section);
226 }
Anders Carlsson18af3682010-06-08 22:47:50 +0000227
David Blaikie4e4d0842012-03-11 07:00:24 +0000228 if (!CGM.getLangOpts().Exceptions)
John McCall044cc542010-07-06 04:38:10 +0000229 Fn->setDoesNotThrow();
230
Kostya Serebryanyb9d2b3b2012-06-26 08:56:33 +0000231 if (CGM.getLangOpts().AddressSanitizer)
Bill Wendlingfac63102012-10-10 03:13:20 +0000232 Fn->addFnAttr(llvm::Attributes::AddressSafety);
Kostya Serebryanyb9d2b3b2012-06-26 08:56:33 +0000233
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000234 return Fn;
235}
236
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000237void
John McCall3030eb82010-11-06 09:44:32 +0000238CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000239 llvm::GlobalVariable *Addr,
240 bool PerformInit) {
Chris Lattner8b418682012-02-07 00:39:47 +0000241 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000242
243 // Create a variable initialization function.
244 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000245 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000246
Richard Smith7ca48502012-02-13 22:16:19 +0000247 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
248 PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000249
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000250 if (D->hasAttr<InitPriorityAttr>()) {
251 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnerec2830d2010-06-27 06:32:58 +0000252 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000253 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000254 DelayedCXXInitPosition.erase(D);
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000255 }
John McCallbf40cb52010-07-15 23:40:35 +0000256 else {
257 llvm::DenseMap<const Decl *, unsigned>::iterator I =
258 DelayedCXXInitPosition.find(D);
259 if (I == DelayedCXXInitPosition.end()) {
260 CXXGlobalInits.push_back(Fn);
261 } else {
262 assert(CXXGlobalInits[I->second] == 0);
263 CXXGlobalInits[I->second] = Fn;
264 DelayedCXXInitPosition.erase(I);
265 }
266 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000267}
268
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000269void
270CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000271 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
272 CXXGlobalInits.pop_back();
273
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000274 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000275 return;
276
Chris Lattner8b418682012-02-07 00:39:47 +0000277 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000278
279 // Create our global initialization function.
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000280 llvm::Function *Fn =
281 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000282
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000283 if (!PrioritizedCXXGlobalInits.empty()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000285 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Fariborz Jahanianf4896882010-06-22 00:23:08 +0000286 PrioritizedCXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000287 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
288 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
289 LocalCXXGlobalInits.push_back(Fn);
290 }
John McCallbf40cb52010-07-15 23:40:35 +0000291 LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000292 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
293 &LocalCXXGlobalInits[0],
294 LocalCXXGlobalInits.size());
295 }
296 else
297 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
298 &CXXGlobalInits[0],
299 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000300 AddGlobalCtor(Fn);
Axel Naumann54ec6c52011-05-06 15:24:04 +0000301 CXXGlobalInits.clear();
302 PrioritizedCXXGlobalInits.clear();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000303}
304
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000305void CodeGenModule::EmitCXXGlobalDtorFunc() {
306 if (CXXGlobalDtors.empty())
307 return;
308
Chris Lattner8b418682012-02-07 00:39:47 +0000309 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000310
311 // Create our global destructor function.
312 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000313 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000314
John McCall3f88f682012-04-06 18:21:03 +0000315 CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000316 AddGlobalDtor(Fn);
317}
318
John McCall3030eb82010-11-06 09:44:32 +0000319/// Emit the code necessary to initialize the given global variable.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000320void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
John McCall3030eb82010-11-06 09:44:32 +0000321 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000322 llvm::GlobalVariable *Addr,
323 bool PerformInit) {
Nick Lewycky78d1a102012-07-24 01:40:49 +0000324 if (CGM.getModuleDebugInfo() && !D->hasAttr<NoDebugAttr>())
325 DebugInfo = CGM.getModuleDebugInfo();
326
327 StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000328 getTypes().arrangeNullaryFunction(),
Nick Lewycky78d1a102012-07-24 01:40:49 +0000329 FunctionArgList(), D->getInit()->getExprLoc());
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000330
Douglas Gregore67d1512011-07-01 21:54:36 +0000331 // Use guarded initialization if the global variable is weak. This
332 // occurs for, e.g., instantiated static data members and
333 // definitions explicitly marked weak.
334 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
335 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
Richard Smith7ca48502012-02-13 22:16:19 +0000336 EmitCXXGuardedInit(*D, Addr, PerformInit);
John McCall3030eb82010-11-06 09:44:32 +0000337 } else {
Richard Smith7ca48502012-02-13 22:16:19 +0000338 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
Fariborz Jahanian92d835a2010-10-26 22:47:47 +0000339 }
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000340
341 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000342}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000343
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000344void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
345 llvm::Constant **Decls,
346 unsigned NumDecls) {
John McCalld26bc762011-03-09 04:27:21 +0000347 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000348 getTypes().arrangeNullaryFunction(),
John McCalld26bc762011-03-09 04:27:21 +0000349 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000350
John McCallf85e1932011-06-15 23:02:42 +0000351 RunCleanupsScope Scope(*this);
352
353 // When building in Objective-C++ ARC mode, create an autorelease pool
354 // around the global initializers.
David Blaikie4e4d0842012-03-11 07:00:24 +0000355 if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
John McCallf85e1932011-06-15 23:02:42 +0000356 llvm::Value *token = EmitObjCAutoreleasePoolPush();
357 EmitObjCAutoreleasePoolCleanup(token);
358 }
359
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000360 for (unsigned i = 0; i != NumDecls; ++i)
John McCallbf40cb52010-07-15 23:40:35 +0000361 if (Decls[i])
John McCallf85e1932011-06-15 23:02:42 +0000362 Builder.CreateCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000363
John McCallf85e1932011-06-15 23:02:42 +0000364 Scope.ForceCleanup();
365
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000366 FinishFunction();
367}
368
John McCall3f88f682012-04-06 18:21:03 +0000369void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000370 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000371 &DtorsAndObjects) {
John McCalld26bc762011-03-09 04:27:21 +0000372 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
John McCallde5d3c72012-02-17 03:33:10 +0000373 getTypes().arrangeNullaryFunction(),
John McCalld26bc762011-03-09 04:27:21 +0000374 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000375
376 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000377 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000378 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000379 llvm::CallInst *CI = Builder.CreateCall(Callee,
380 DtorsAndObjects[e - i - 1].second);
381 // Make sure the call and the callee agree on calling convention.
382 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
383 CI->setCallingConv(F->getCallingConv());
384 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000385
386 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000387}
388
John McCalla91f6662011-07-13 03:01:35 +0000389/// generateDestroyHelper - Generates a helper function which, when
390/// invoked, destroys the given object.
Anders Carlsson77291362010-06-08 22:17:27 +0000391llvm::Function *
John McCalla91f6662011-07-13 03:01:35 +0000392CodeGenFunction::generateDestroyHelper(llvm::Constant *addr,
393 QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000394 Destroyer *destroyer,
John McCalla91f6662011-07-13 03:01:35 +0000395 bool useEHCleanupForArray) {
John McCalld26bc762011-03-09 04:27:21 +0000396 FunctionArgList args;
397 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
398 args.push_back(&dst);
Anders Carlsson77291362010-06-08 22:17:27 +0000399
Anders Carlsson77291362010-06-08 22:17:27 +0000400 const CGFunctionInfo &FI =
John McCallde5d3c72012-02-17 03:33:10 +0000401 CGM.getTypes().arrangeFunctionDeclaration(getContext().VoidTy, args,
402 FunctionType::ExtInfo(),
403 /*variadic*/ false);
404 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
John McCalla91f6662011-07-13 03:01:35 +0000405 llvm::Function *fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000406 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson77291362010-06-08 22:17:27 +0000407
John McCalla91f6662011-07-13 03:01:35 +0000408 StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args,
John McCalld26bc762011-03-09 04:27:21 +0000409 SourceLocation());
Anders Carlsson77291362010-06-08 22:17:27 +0000410
John McCalla91f6662011-07-13 03:01:35 +0000411 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
Anders Carlsson77291362010-06-08 22:17:27 +0000412
413 FinishFunction();
414
John McCalla91f6662011-07-13 03:01:35 +0000415 return fn;
Anders Carlsson77291362010-06-08 22:17:27 +0000416}