blob: 44acf62aa644eecf927941b1b65dd1925c06f41e [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 {
John McCall7c2349b2011-08-25 20:40:09 +000049 CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
John McCall410ffb22011-08-25 23:04:34 +000050 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
101 CGF.EmitCXXGlobalDtorRegistration(function, argument);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000102}
103
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000104void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Richard Smith7ca48502012-02-13 22:16:19 +0000105 llvm::Constant *DeclPtr,
106 bool PerformInit) {
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000107
108 const Expr *Init = D.getInit();
109 QualType T = D.getType();
110
111 if (!T->isReferenceType()) {
Richard Smith7ca48502012-02-13 22:16:19 +0000112 if (PerformInit)
113 EmitDeclInit(*this, D, DeclPtr);
Douglas Gregorcc6a44b2010-05-05 15:38:32 +0000114 EmitDeclDestroy(*this, D, DeclPtr);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000115 return;
116 }
Anders Carlsson045a6d82010-06-27 17:52:15 +0000117
Richard Smith7ca48502012-02-13 22:16:19 +0000118 assert(PerformInit && "cannot have constant initializer which needs "
119 "destruction for reference");
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000120 unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
Anders Carlsson045a6d82010-06-27 17:52:15 +0000121 RValue RV = EmitReferenceBindingToExpr(Init, &D);
Daniel Dunbar91a16fa2010-08-21 02:24:36 +0000122 EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
Anders Carlssonfcbfdc12009-12-10 00:57:45 +0000123}
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000124
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000125void
126CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
127 llvm::Constant *DeclPtr) {
128 // Generate a global destructor entry if not using __cxa_atexit.
129 if (!CGM.getCodeGenOpts().CXAAtExit) {
130 CGM.AddCXXDtorEntry(DtorFn, DeclPtr);
131 return;
132 }
133
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000134 // Get the destructor function type
Chris Lattner8b418682012-02-07 00:39:47 +0000135 llvm::Type *DtorFnTy = llvm::FunctionType::get(VoidTy, Int8PtrTy, false);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000136 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
137
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000138 llvm::Type *Params[] = { DtorFnTy, Int8PtrTy, Int8PtrTy };
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000139
140 // Get the __cxa_atexit function type
141 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Chris Lattner2acc6e32011-07-18 04:24:23 +0000142 llvm::FunctionType *AtExitFnTy =
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000143 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
144
145 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
146 "__cxa_atexit");
Anders Carlssonbd4a0732011-03-20 20:52:32 +0000147 if (llvm::Function *Fn = dyn_cast<llvm::Function>(AtExitFn))
148 Fn->setDoesNotThrow();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000149
150 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
151 "__dso_handle");
152 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
153 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
154 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
Jay Foad4c7d9f12011-07-15 08:37:34 +0000155 Builder.CreateCall(AtExitFn, Args);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000156}
157
John McCall3030eb82010-11-06 09:44:32 +0000158void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
Richard Smith7ca48502012-02-13 22:16:19 +0000159 llvm::GlobalVariable *DeclPtr,
160 bool PerformInit) {
John McCall32096692011-03-18 02:56:14 +0000161 // If we've been asked to forbid guard variables, emit an error now.
162 // This diagnostic is hard-coded for Darwin's use case; we can find
163 // better phrasing if someone else needs it.
164 if (CGM.getCodeGenOpts().ForbidGuardVariables)
165 CGM.Error(D.getLocation(),
166 "this initialization requires a guard variable, which "
167 "the kernel does not support");
168
Richard Smith7ca48502012-02-13 22:16:19 +0000169 CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
John McCall5cd91b52010-09-08 01:44:27 +0000170}
171
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000172static llvm::Function *
173CreateGlobalInitOrDestructFunction(CodeGenModule &CGM,
Chris Lattner2acc6e32011-07-18 04:24:23 +0000174 llvm::FunctionType *FTy,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 StringRef Name) {
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000176 llvm::Function *Fn =
177 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
178 Name, &CGM.getModule());
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000179 if (!CGM.getContext().getLangOptions().AppleKext) {
180 // Set the section if needed.
181 if (const char *Section =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000182 CGM.getContext().getTargetInfo().getStaticInitSectionSpecifier())
Fariborz Jahaniand6c9a0f2011-02-15 18:54:46 +0000183 Fn->setSection(Section);
184 }
Anders Carlsson18af3682010-06-08 22:47:50 +0000185
Anders Carlsson7a178512011-02-28 00:33:03 +0000186 if (!CGM.getLangOptions().Exceptions)
John McCall044cc542010-07-06 04:38:10 +0000187 Fn->setDoesNotThrow();
188
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000189 return Fn;
190}
191
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000192void
John McCall3030eb82010-11-06 09:44:32 +0000193CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000194 llvm::GlobalVariable *Addr,
195 bool PerformInit) {
Chris Lattner8b418682012-02-07 00:39:47 +0000196 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000197
198 // Create a variable initialization function.
199 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000200 CreateGlobalInitOrDestructFunction(*this, FTy, "__cxx_global_var_init");
Eli Friedman6c6bda32010-01-08 00:50:11 +0000201
Richard Smith7ca48502012-02-13 22:16:19 +0000202 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
203 PerformInit);
Eli Friedman6c6bda32010-01-08 00:50:11 +0000204
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000205 if (D->hasAttr<InitPriorityAttr>()) {
206 unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
Chris Lattnerec2830d2010-06-27 06:32:58 +0000207 OrderGlobalInits Key(order, PrioritizedCXXGlobalInits.size());
Fariborz Jahaniane0b691a2010-06-21 21:27:42 +0000208 PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
John McCallbf40cb52010-07-15 23:40:35 +0000209 DelayedCXXInitPosition.erase(D);
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000210 }
John McCallbf40cb52010-07-15 23:40:35 +0000211 else {
212 llvm::DenseMap<const Decl *, unsigned>::iterator I =
213 DelayedCXXInitPosition.find(D);
214 if (I == DelayedCXXInitPosition.end()) {
215 CXXGlobalInits.push_back(Fn);
216 } else {
217 assert(CXXGlobalInits[I->second] == 0);
218 CXXGlobalInits[I->second] = Fn;
219 DelayedCXXInitPosition.erase(I);
220 }
221 }
Eli Friedman6c6bda32010-01-08 00:50:11 +0000222}
223
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000224void
225CodeGenModule::EmitCXXGlobalInitFunc() {
John McCallbf40cb52010-07-15 23:40:35 +0000226 while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
227 CXXGlobalInits.pop_back();
228
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000229 if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000230 return;
231
Chris Lattner8b418682012-02-07 00:39:47 +0000232 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000233
234 // Create our global initialization function.
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000235 llvm::Function *Fn =
236 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__I_a");
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000237
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000238 if (!PrioritizedCXXGlobalInits.empty()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
Fariborz Jahanian027d7ed2010-06-21 19:49:38 +0000240 llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
Fariborz Jahanianf4896882010-06-22 00:23:08 +0000241 PrioritizedCXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000242 for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
243 llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
244 LocalCXXGlobalInits.push_back(Fn);
245 }
John McCallbf40cb52010-07-15 23:40:35 +0000246 LocalCXXGlobalInits.append(CXXGlobalInits.begin(), CXXGlobalInits.end());
Fariborz Jahanian9f967c52010-06-21 18:45:05 +0000247 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
248 &LocalCXXGlobalInits[0],
249 LocalCXXGlobalInits.size());
250 }
251 else
252 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
253 &CXXGlobalInits[0],
254 CXXGlobalInits.size());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000255 AddGlobalCtor(Fn);
Axel Naumann54ec6c52011-05-06 15:24:04 +0000256 CXXGlobalInits.clear();
257 PrioritizedCXXGlobalInits.clear();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000258}
259
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000260void CodeGenModule::EmitCXXGlobalDtorFunc() {
261 if (CXXGlobalDtors.empty())
262 return;
263
Chris Lattner8b418682012-02-07 00:39:47 +0000264 llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000265
266 // Create our global destructor function.
267 llvm::Function *Fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000268 CreateGlobalInitOrDestructFunction(*this, FTy, "_GLOBAL__D_a");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000269
270 CodeGenFunction(*this).GenerateCXXGlobalDtorFunc(Fn, CXXGlobalDtors);
271 AddGlobalDtor(Fn);
272}
273
John McCall3030eb82010-11-06 09:44:32 +0000274/// Emit the code necessary to initialize the given global variable.
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000275void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
John McCall3030eb82010-11-06 09:44:32 +0000276 const VarDecl *D,
Richard Smith7ca48502012-02-13 22:16:19 +0000277 llvm::GlobalVariable *Addr,
278 bool PerformInit) {
John McCalld26bc762011-03-09 04:27:21 +0000279 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
280 getTypes().getNullaryFunctionInfo(),
281 FunctionArgList(), SourceLocation());
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000282
Douglas Gregore67d1512011-07-01 21:54:36 +0000283 // Use guarded initialization if the global variable is weak. This
284 // occurs for, e.g., instantiated static data members and
285 // definitions explicitly marked weak.
286 if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
287 Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
Richard Smith7ca48502012-02-13 22:16:19 +0000288 EmitCXXGuardedInit(*D, Addr, PerformInit);
John McCall3030eb82010-11-06 09:44:32 +0000289 } else {
Richard Smith7ca48502012-02-13 22:16:19 +0000290 EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
Fariborz Jahanian92d835a2010-10-26 22:47:47 +0000291 }
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000292
293 FinishFunction();
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000294}
Daniel Dunbar5c6846e2010-03-20 04:15:29 +0000295
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000296void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
297 llvm::Constant **Decls,
298 unsigned NumDecls) {
John McCalld26bc762011-03-09 04:27:21 +0000299 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
300 getTypes().getNullaryFunctionInfo(),
301 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000302
John McCallf85e1932011-06-15 23:02:42 +0000303 RunCleanupsScope Scope(*this);
304
305 // When building in Objective-C++ ARC mode, create an autorelease pool
306 // around the global initializers.
307 if (getLangOptions().ObjCAutoRefCount && getLangOptions().CPlusPlus) {
308 llvm::Value *token = EmitObjCAutoreleasePoolPush();
309 EmitObjCAutoreleasePoolCleanup(token);
310 }
311
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000312 for (unsigned i = 0; i != NumDecls; ++i)
John McCallbf40cb52010-07-15 23:40:35 +0000313 if (Decls[i])
John McCallf85e1932011-06-15 23:02:42 +0000314 Builder.CreateCall(Decls[i]);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000315
John McCallf85e1932011-06-15 23:02:42 +0000316 Scope.ForceCleanup();
317
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000318 FinishFunction();
319}
320
321void CodeGenFunction::GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +0000322 const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000323 &DtorsAndObjects) {
John McCalld26bc762011-03-09 04:27:21 +0000324 StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
325 getTypes().getNullaryFunctionInfo(),
326 FunctionArgList(), SourceLocation());
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000327
328 // Emit the dtors, in reverse order from construction.
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000329 for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
Chris Lattner810112e2010-06-19 05:52:45 +0000330 llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
Chris Lattnerc9a85f92010-04-26 20:35:54 +0000331 llvm::CallInst *CI = Builder.CreateCall(Callee,
332 DtorsAndObjects[e - i - 1].second);
333 // Make sure the call and the callee agree on calling convention.
334 if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
335 CI->setCallingConv(F->getCallingConv());
336 }
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000337
338 FinishFunction();
Anders Carlssoneb4072e2009-12-10 00:30:05 +0000339}
340
John McCalla91f6662011-07-13 03:01:35 +0000341/// generateDestroyHelper - Generates a helper function which, when
342/// invoked, destroys the given object.
Anders Carlsson77291362010-06-08 22:17:27 +0000343llvm::Function *
John McCalla91f6662011-07-13 03:01:35 +0000344CodeGenFunction::generateDestroyHelper(llvm::Constant *addr,
345 QualType type,
Peter Collingbourne516bbd42012-01-26 03:33:36 +0000346 Destroyer *destroyer,
John McCalla91f6662011-07-13 03:01:35 +0000347 bool useEHCleanupForArray) {
John McCalld26bc762011-03-09 04:27:21 +0000348 FunctionArgList args;
349 ImplicitParamDecl dst(0, SourceLocation(), 0, getContext().VoidPtrTy);
350 args.push_back(&dst);
Anders Carlsson77291362010-06-08 22:17:27 +0000351
Anders Carlsson77291362010-06-08 22:17:27 +0000352 const CGFunctionInfo &FI =
John McCalld26bc762011-03-09 04:27:21 +0000353 CGM.getTypes().getFunctionInfo(getContext().VoidTy, args,
Anders Carlsson77291362010-06-08 22:17:27 +0000354 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +0000355 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
John McCalla91f6662011-07-13 03:01:35 +0000356 llvm::Function *fn =
Anders Carlsson9dc046e2010-06-08 22:40:05 +0000357 CreateGlobalInitOrDestructFunction(CGM, FTy, "__cxx_global_array_dtor");
Anders Carlsson77291362010-06-08 22:17:27 +0000358
John McCalla91f6662011-07-13 03:01:35 +0000359 StartFunction(GlobalDecl(), getContext().VoidTy, fn, FI, args,
John McCalld26bc762011-03-09 04:27:21 +0000360 SourceLocation());
Anders Carlsson77291362010-06-08 22:17:27 +0000361
John McCalla91f6662011-07-13 03:01:35 +0000362 emitDestroy(addr, type, destroyer, useEHCleanupForArray);
Anders Carlsson77291362010-06-08 22:17:27 +0000363
364 FinishFunction();
365
John McCalla91f6662011-07-13 03:01:35 +0000366 return fn;
Anders Carlsson77291362010-06-08 22:17:27 +0000367}